Subversion Repositories Kolibri OS

Rev

Rev 2434 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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