Subversion Repositories Kolibri OS

Rev

Rev 261 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 261 Rev 373
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
2
;;                                                                 ;;
3
;;  IP.INC                                                         ;;
3
;;  IP.INC                                                         ;;
4
;;                                                                 ;;
4
;;                                                                 ;;
5
;;  IP Processes for Menuet OS  TCP/IP stack                       ;;
5
;;  IP Processes for Menuet OS  TCP/IP stack                       ;;
6
;;                                                                 ;;
6
;;                                                                 ;;
7
;;  Version 0.3  29 August 2002                                    ;;
7
;;  Version 0.3  29 August 2002                                    ;;
8
;;                                                                 ;;
8
;;                                                                 ;;
9
;;  Copyright 2002 Mike Hibbett, mikeh@oceanfree.net               ;;
9
;;  Copyright 2002 Mike Hibbett, mikeh@oceanfree.net               ;;
10
;;                                                                 ;;
10
;;                                                                 ;;
11
;;  See file COPYING for details                                   ;;
11
;;  See file COPYING for details                                   ;;
12
;;                                                                 ;;
12
;;                                                                 ;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
 
14
 
15
; IP underlying protocols numbers
15
; IP underlying protocols numbers
16
PROTOCOL_ICMP     equ      1
16
PROTOCOL_ICMP     equ      1
17
PROTOCOL_TCP      equ      6
17
PROTOCOL_TCP      equ      6
18
PROTOCOL_UDP      equ      17
18
PROTOCOL_UDP      equ      17
19
 
19
 
20
struc IP_PACKET
20
struc IP_PACKET
21
{  .VersionAndIHL           db   ?  ;+00 - Version[0-3 bits] and IHL(header length)[4-7 bits]
21
{  .VersionAndIHL           db   ?  ;+00 - Version[0-3 bits] and IHL(header length)[4-7 bits]
22
   .TypeOfService           db   ?  ;+01
22
   .TypeOfService           db   ?  ;+01
23
   .TotalLength             dw   ?  ;+02
23
   .TotalLength             dw   ?  ;+02
24
   .Identification          dw   ?  ;+04
24
   .Identification          dw   ?  ;+04
25
   .FlagsAndFragmentOffset  dw   ?  ;+06 - Flags[0-2] and FragmentOffset[3-15]
25
   .FlagsAndFragmentOffset  dw   ?  ;+06 - Flags[0-2] and FragmentOffset[3-15]
26
   .TimeToLive              db   ?  ;+08
26
   .TimeToLive              db   ?  ;+08
27
   .Protocol                db   ?  ;+09
27
   .Protocol                db   ?  ;+09
28
   .HeaderChecksum          dw   ?  ;+10
28
   .HeaderChecksum          dw   ?  ;+10
29
   .SourceAddress           dd   ?  ;+12
29
   .SourceAddress           dd   ?  ;+12
30
   .DestinationAddress      dd   ?  ;+16
30
   .DestinationAddress      dd   ?  ;+16
31
   .DataOrOptional          dd   ?  ;+20
31
   .DataOrOptional          dd   ?  ;+20
32
}
32
}
33
 
33
 
34
virtual at 0
34
virtual at 0
35
  IP_PACKET IP_PACKET
35
  IP_PACKET IP_PACKET
36
end virtual
36
end virtual
37
 
37
 
38
 
38
 
39
;*******************************************************************
39
;*******************************************************************
40
;   Interface
40
;   Interface
41
;
41
;
42
;       ip_rx       processes all packets received by the network layer
42
;       ip_rx       processes all packets received by the network layer
43
;                   It calls the appropriate protocol handler
43
;                   It calls the appropriate protocol handler
44
;
44
;
45
;
45
;
46
;
46
;
47
;*******************************************************************
47
;*******************************************************************
48
 
48
 
49
 
49
 
50
;
50
;
51
;   IP Packet after reception - Normal IP packet format
51
;   IP Packet after reception - Normal IP packet format
52
;
52
;
53
;           0               1               2               3
53
;           0               1               2               3
54
;    0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
54
;    0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
55
;
55
;
56
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57
;0  |Version|  IHL  |Type of Service|       Total Length            |
57
;0  |Version|  IHL  |Type of Service|       Total Length            |
58
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59
;4  |         Identification        |Flags|      Fragment Offset    |
59
;4  |         Identification        |Flags|      Fragment Offset    |
60
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61
;8  |  Time to Live |    Protocol   |         Header Checksum       |
61
;8  |  Time to Live |    Protocol   |         Header Checksum       |
62
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63
;12 |                       Source Address                          |
63
;12 |                       Source Address                          |
64
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65
;16 |                    Destination Address                        |
65
;16 |                    Destination Address                        |
66
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67
;20 |      Data                                                     |
67
;20 |      Data                                                     |
68
;   +-+-+-..........                                               -+
68
;   +-+-+-..........                                               -+
69
;
69
;
70
;
70
;
71
;    [smb] attention! according to RFC 791 IP packet may have 'options' sections,
71
;    [smb] attention! according to RFC 791 IP packet may have 'options' sections,
72
; so we can't simply think, that data have offset 20. We must calculate offset from
72
; so we can't simply think, that data have offset 20. We must calculate offset from
73
; IHL field
73
; IHL field
74
;
74
;
75
macro	GET_IHL reg, header_addr
75
macro	GET_IHL reg, header_addr
76
{
76
{
77
	movzx	reg, byte [header_addr]
77
	movzx	reg, byte [header_addr]
78
	
78
	
79
	; we need 4-7 bits, so....
79
	; we need 4-7 bits, so....
80
	and	reg, 0x0000000F
80
	and	reg, 0x0000000F
81
	
81
	
82
	; IHL keeps number of octets, so we need to << 2 'reg'
82
	; IHL keeps number of octets, so we need to << 2 'reg'
83
	shl     reg, 2
83
	shl     reg, 2
84
}
84
}
85
 
85
 
86
 
86
 
87
;***************************************************************************
87
;***************************************************************************
88
;   Function
88
;   Function
89
;      ip_rx
89
;      ip_rx
90
;
90
;
91
;   Description
91
;   Description
92
;       This is a kernel function, called by stack_handler
92
;       This is a kernel function, called by stack_handler
93
;       Processes all IP-packets received by the network layer
93
;       Processes all IP-packets received by the network layer
94
;       It calls the appropriate protocol handler
94
;       It calls the appropriate protocol handler
95
;
95
;
96
;***************************************************************************
96
;***************************************************************************
97
proc ip_rx stdcall
97
proc ip_rx stdcall
98
local buffer_number dd ?
98
local buffer_number dd ?
99
 
99
 
100
    ; Look for a buffer to tx
100
    ; Look for a buffer to tx
101
    mov     eax, IPIN_QUEUE
101
    mov     eax, IPIN_QUEUE
102
    call    dequeue
102
    call    dequeue
103
    cmp     ax, NO_BUFFER
103
    cmp     ax, NO_BUFFER
104
    je      .exit         ; Exit if no buffer available
104
    je      .exit         ; Exit if no buffer available
105
 
105
 
106
    mov     [buffer_number], eax    ;save buffer number
106
    mov     [buffer_number], eax    ;save buffer number
107
 
107
 
108
    ; convert buffer pointer eax to the absolute address
108
    ; convert buffer pointer eax to the absolute address
109
    imul    eax, IPBUFFSIZE
109
    imul    eax, IPBUFFSIZE
110
    add     eax, IPbuffs
110
    add     eax, IPbuffs
111
 
111
 
112
    mov     ebx, eax  ; ebx=pointer to IP_PACKET
112
    mov     ebx, eax  ; ebx=pointer to IP_PACKET
113
 
113
 
114
    ; Validate the IP checksum
114
    ; Validate the IP checksum
115
    mov     dx, word[ebx + IP_PACKET.HeaderChecksum]
115
    mov     dx, word[ebx + IP_PACKET.HeaderChecksum]
116
    xchg    dh,dl          ; Get the checksum in intel format
116
    xchg    dh,dl          ; Get the checksum in intel format
117
  
117
  
118
    mov     [ebx + IP_PACKET.HeaderChecksum], word 0  ; clear checksum field - need to when
118
    mov     [ebx + IP_PACKET.HeaderChecksum], word 0  ; clear checksum field - need to when
119
                                ; recalculating checksum
119
                                ; recalculating checksum
120
    ;  this needs two data pointers and two size #.
120
    ;  this needs two data pointers and two size #.
121
    ;  2nd pointer can be of length 0
121
    ;  2nd pointer can be of length 0
122
 
122
 
123
    GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
123
    GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
124
    stdcall checksum_jb, ebx, ecx   ;buf_ptr, buf_size
124
    stdcall checksum_jb, ebx, ecx   ;buf_ptr, buf_size
125
    cmp     dx, ax
125
    cmp     dx, ax
126
 
126
 
127
    mov     edx, ebx ; EDX (IP-BUFFER POINTER) WILL BE USED FOR *_rx HANDLERS BELOW!!!
127
    mov     edx, ebx ; EDX (IP-BUFFER POINTER) WILL BE USED FOR *_rx HANDLERS BELOW!!!
128
    jnz     .dump  ;if CHECKSUM isn't valid then dump packet
128
    jnz     .dump  ;if CHECKSUM isn't valid then dump packet
129
 
129
 
130
    ; Validate the IP address, if it isn't broadcast
130
    ; Validate the IP address, if it isn't broadcast
131
    mov     eax, [stack_ip]
131
    mov     eax, [stack_ip]
132
    cmp     dword[ebx + IP_PACKET.DestinationAddress], eax
132
    cmp     dword[ebx + IP_PACKET.DestinationAddress], eax
133
    je      @f
133
    je      @f
134
 
134
 
135
    ; If the IP address is 255.255.255.255, accept it
135
    ; If the IP address is 255.255.255.255, accept it
136
    ; - it is a broadcast packet, which we need for dhcp
136
    ; - it is a broadcast packet, which we need for dhcp
137
    cmp     dword[ebx + IP_PACKET.DestinationAddress], 0xffffffff
137
    cmp     dword[ebx + IP_PACKET.DestinationAddress], 0xffffffff
138
    jne     .dump
138
    jne     .dump
139
 
139
 
140
  @@:
140
  @@:
141
    mov     al, [ebx + IP_PACKET.VersionAndIHL]
141
    mov     al, [ebx + IP_PACKET.VersionAndIHL]
142
    and     al, 0x0f  ;get IHL(header length)
142
    and     al, 0x0f  ;get IHL(header length)
143
    cmp     al, 0x05  ;if IHL!= 5*4(20 bytes)
143
    cmp     al, 0x05  ;if IHL!= 5*4(20 bytes)
144
    jnz     .dump     ;then dump it
144
    jnz     .dump     ;then dump it
145
 
145
 
146
    cmp     byte[ebx + IP_PACKET.TimeToLive], byte 0
146
    cmp     byte[ebx + IP_PACKET.TimeToLive], byte 0
147
    je      .dump     ;if TTL==0 then dump it
147
    je      .dump     ;if TTL==0 then dump it
148
 
148
 
149
    mov     ax, word[ebx + IP_PACKET.FlagsAndFragmentOffset]
149
    mov     ax, word[ebx + IP_PACKET.FlagsAndFragmentOffset]
150
    and     ax, 0xFFBF   ;get flags
150
    and     ax, 0xFFBF   ;get flags
151
    cmp     ax, 0        ;if some flags was set then we dump this packet
151
    cmp     ax, 0        ;if some flags was set then we dump this packet
152
    jnz     .dump        ;the flags should be used for fragmented packets
152
    jnz     .dump        ;the flags should be used for fragmented packets
153
 
153
 
154
    ; Check the protocol, and call the appropriate handler
154
    ; Check the protocol, and call the appropriate handler
155
    ; Each handler will re-use or free the queue buffer as appropriate
155
    ; Each handler will re-use or free the queue buffer as appropriate
156
 
156
 
157
    mov     al, [ebx + IP_PACKET.Protocol]
157
    mov     al, [ebx + IP_PACKET.Protocol]
158
 
158
 
159
    cmp     al , PROTOCOL_TCP
159
    cmp     al , PROTOCOL_TCP
160
    jne     .not_tcp
160
    jne     .not_tcp
-
 
161
    DEBUGF  1,"K : ip_rx - TCP packet\n"
161
    mov     eax, dword[buffer_number]
162
    mov     eax, dword[buffer_number]
162
    call    tcp_rx
163
    call    tcp_rx
163
    jmp     .exit
164
    jmp     .exit
164
 
165
 
165
  .not_tcp:
166
  .not_tcp:
166
    cmp     al, PROTOCOL_UDP
167
    cmp     al, PROTOCOL_UDP
167
    jne     .not_udp
168
    jne     .not_udp
-
 
169
    DEBUGF  1,"K : ip_rx - UDP packet\n"
168
    mov     eax, dword[buffer_number]
170
    mov     eax, dword[buffer_number]
169
    call    udp_rx
171
    call    udp_rx
170
    jmp     .exit
172
    jmp     .exit
171
 
173
 
172
  .not_udp:
174
  .not_udp:
173
    cmp     al , PROTOCOL_ICMP
175
    cmp     al , PROTOCOL_ICMP
174
    jne     .dump              ;protocol ain't supported
176
    jne     .dump              ;protocol ain't supported
-
 
177
 
175
 
178
    DEBUGF  1,"K : ip_rx - ICMP packet\n"
176
    ;GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
179
    ;GET_IHL ecx, ebx + IP_PACKET.VersionAndIHL ;get packet length in ecx
177
    mov     eax, dword[buffer_number]
180
    mov     eax, dword[buffer_number]
178
    stdcall icmp_rx,eax,ebx,ecx  ;buffer_number,IPPacketBase,IPHeaderLength
181
    stdcall icmp_rx,eax,ebx,ecx  ;buffer_number,IPPacketBase,IPHeaderLength
179
    jmp     .exit
182
    jmp     .exit
180
 
183
 
181
 
184
 
182
.dump:
185
.dump:
183
    ; No protocol handler available, so
186
    ; No protocol handler available, so
184
    ; silently dump the packet, freeing up the queue buffer
187
    ; silently dump the packet, freeing up the queue buffer
185
 
188
 
186
    inc     dword [dumped_rx_count]
189
    inc     dword [dumped_rx_count]
187
 
190
 
188
    mov     eax, dword[buffer_number]
191
    mov     eax, dword[buffer_number]
189
    call    freeBuff
192
    call    freeBuff
190
 
193
 
191
  .exit:
194
  .exit:
192
    ret
195
    ret
193
endp
196
endp