Rev 2629 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2629 | Rev 2731 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
2 | ;; ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2011. All rights reserved. ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
5 | ;; ;; |
6 | ;; IPv4.INC ;; |
6 | ;; IPv4.INC ;; |
7 | ;; ;; |
7 | ;; ;; |
8 | ;; Part of the tcp/ip network stack for KolibriOS ;; |
8 | ;; Part of the tcp/ip network stack for KolibriOS ;; |
Line 14... | Line 14... | ||
14 | ;; GNU GENERAL PUBLIC LICENSE ;; |
14 | ;; GNU GENERAL PUBLIC LICENSE ;; |
15 | ;; Version 2, June 1991 ;; |
15 | ;; Version 2, June 1991 ;; |
16 | ;; ;; |
16 | ;; ;; |
17 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
17 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
Line 18... | Line 18... | ||
18 | 18 | ||
Line 19... | Line 19... | ||
19 | $Revision: 2629 $ |
19 | $Revision: 2731 $ |
20 | 20 | ||
21 | MAX_FRAGMENTS = 64 |
21 | MAX_FRAGMENTS = 64 |
Line 50... | Line 50... | ||
50 | struct FRAGMENT_entry ; This structure will replace the ethernet header in fragmented ip packets |
50 | struct FRAGMENT_entry ; This structure will replace the ethernet header in fragmented ip packets |
Line 51... | Line 51... | ||
51 | 51 | ||
52 | PrevPtr dd ? ; Pointer to previous fragment entry (-1 for first packet) |
52 | PrevPtr dd ? ; Pointer to previous fragment entry (-1 for first packet) |
53 | NextPtr dd ? ; Pointer to next fragment entry (-1 for last packet) |
53 | NextPtr dd ? ; Pointer to next fragment entry (-1 for last packet) |
54 | Owner dd ? ; Pointer to structure of driver |
54 | Owner dd ? ; Pointer to structure of driver |
55 | rb 2 ; to match ethernet header size ; TODO: fix this hack |
55 | rb 2 ; to match ethernet header size ;;; FIXME |
56 | ; Ip header begins here (we will need the IP header to re-construct the complete packet) |
56 | ; Ip header begins here (we will need the IP header to re-construct the complete packet) |
Line 57... | Line 57... | ||
57 | ends |
57 | ends |
Line 62... | Line 62... | ||
62 | 62 | ||
63 | IP_LIST rd MAX_IP |
63 | IP_LIST rd MAX_IP |
64 | SUBNET_LIST rd MAX_IP |
64 | SUBNET_LIST rd MAX_IP |
65 | DNS_LIST rd MAX_IP |
65 | DNS_LIST rd MAX_IP |
- | 66 | GATEWAY_LIST rd MAX_IP |
|
Line 66... | Line 67... | ||
66 | GATEWAY_LIST rd MAX_IP |
67 | BROADCAST_LIST rd MAX_IP |
67 | 68 | ||
Line 68... | Line 69... | ||
68 | IP_PACKETS_TX rd MAX_IP |
69 | IP_PACKETS_TX rd MAX_IP |
Line 81... | Line 82... | ||
81 | ;----------------------------------------------------------------- |
82 | ;----------------------------------------------------------------- |
82 | macro IPv4_init { |
83 | macro IPv4_init { |
Line 83... | Line 84... | ||
83 | 84 | ||
84 | xor eax, eax |
85 | xor eax, eax |
85 | mov edi, IP_LIST |
- | |
86 | mov ecx, 4*MAX_IP |
- | |
87 | rep stosd |
- | |
88 | - | ||
89 | mov edi, FRAGMENT_LIST |
86 | mov edi, IP_LIST |
90 | mov ecx, sizeof.FRAGMENT_slot*MAX_FRAGMENTS/4 + 2*MAX_IP |
87 | mov ecx, 7*MAX_IP + (sizeof.FRAGMENT_slot*MAX_FRAGMENTS)/4 |
Line 91... | Line 88... | ||
91 | rep stosd |
88 | rep stosd |
Line 98... | Line 95... | ||
98 | ; Decrease TimeToLive of all fragment slots |
95 | ; Decrease TimeToLive of all fragment slots |
99 | ; |
96 | ; |
100 | ;----------------------------------------------------------------- |
97 | ;----------------------------------------------------------------- |
101 | macro IPv4_decrease_fragment_ttls { |
98 | macro IPv4_decrease_fragment_ttls { |
Line 102... | Line 99... | ||
102 | 99 | ||
Line 103... | Line 100... | ||
103 | local .loop |
100 | local .loop, .next |
104 | 101 | ||
105 | mov esi, FRAGMENT_LIST |
102 | mov esi, FRAGMENT_LIST |
106 | mov ecx, MAX_FRAGMENTS |
103 | mov ecx, MAX_FRAGMENTS |
107 | .loop: |
104 | .loop: |
108 | cmp [esi + FRAGMENT_slot.ttl], 0 |
105 | cmp [esi + FRAGMENT_slot.ttl], 0 |
- | 106 | je .next |
|
- | 107 | dec [esi + FRAGMENT_slot.ttl] |
|
- | 108 | jz .died |
|
- | 109 | .next: |
|
109 | je .try_next |
110 | add esi, sizeof.FRAGMENT_slot |
- | 111 | dec ecx |
|
- | 112 | jnz .loop |
|
- | 113 | jmp .done |
|
110 | dec [esi + FRAGMENT_slot.ttl] |
114 | |
111 | jnz .try_next |
115 | .died: |
112 | DEBUGF 1,"Fragment slot timed-out!\n" |
- | |
113 | ;;; TODO: clear all entry's of timed-out slot |
116 | DEBUGF 1,"Fragment slot timed-out!\n" |
- | 117 | ;;; TODO: clear all entry's of timed-out slot |
|
114 | .try_next: |
118 | jmp .next |
115 | add esi, 4 |
119 | |
Line 116... | Line 120... | ||
116 | loop .loop |
120 | .done: |
Line 184... | Line 188... | ||
184 | 188 | ||
185 | ;----------------------------------------------------------------- |
189 | ;----------------------------------------------------------------- |
186 | ; |
190 | ; |
187 | ; IPv4_input: |
191 | ; IPv4_input: |
188 | ; |
192 | ; |
189 | ; Will check if IP Packet isnt damaged |
193 | ; Will check if IPv4 Packet isnt damaged |
190 | ; and call appropriate handler. (TCP/UDP/ICMP/..) |
194 | ; and call appropriate handler. (TCP/UDP/ICMP/..) |
191 | ; |
195 | ; |
192 | ; It will also re-construct fragmented packets |
196 | ; It will also re-construct fragmented packets |
193 | ; |
197 | ; |
194 | ; IN: Pointer to buffer in [esp] |
198 | ; IN: Pointer to buffer in [esp] |
195 | ; size of buffer in [esp+4] |
199 | ; size of buffer in [esp+4] |
196 | ; pointer to device struct in ebx |
200 | ; pointer to device struct in ebx |
197 | ; pointer to IP header in edx |
201 | ; pointer to IPv4 header in edx |
198 | ; size of IP packet in ecx |
202 | ; size of IPv4 packet in ecx |
199 | ; OUT: / |
203 | ; OUT: / |
200 | ; |
204 | ; |
201 | ;----------------------------------------------------------------- |
205 | ;----------------------------------------------------------------- |
202 | align 4 |
206 | align 4 |
Line 203... | Line 207... | ||
203 | IPv4_input: ; TODO: add code for raw sockets |
207 | IPv4_input: ; TODO: add IPv4 raw sockets support |
- | 208 | ||
204 | 209 | DEBUGF 1,"IPv4_input, packet from: %u.%u.%u.%u ",\ |
|
205 | DEBUGF 1,"IPv4_input, packet from: %u.%u.%u.%u ",\ |
210 | [edx + IPv4_header.SourceAddress + 0]:1,[edx + IPv4_header.SourceAddress + 1]:1,\ |
206 | [edx + IPv4_header.SourceAddress]:1,[edx + IPv4_header.SourceAddress + 1]:1,[edx + IPv4_header.SourceAddress + 2]:1,[edx + IPv4_header.SourceAddress + 3]:1 |
211 | [edx + IPv4_header.SourceAddress + 2]:1,[edx + IPv4_header.SourceAddress + 3]:1 |
207 | DEBUGF 1,"to: %u.%u.%u.%u\n",\ |
- | |
208 | [edx + IPv4_header.DestinationAddress]:1,[edx + IPv4_header.DestinationAddress + 1]:1,[edx + IPv4_header.DestinationAddress + 2]:1,[edx + IPv4_header.DestinationAddress + 3]:1 |
- | |
209 | - | ||
210 | ;------------------------------------------- |
- | |
211 | ; Check if the packet still has time to live |
212 | DEBUGF 1,"to: %u.%u.%u.%u\n",\ |
212 | - | ||
Line 213... | Line 213... | ||
213 | cmp byte [edx + IPv4_header.TimeToLive], 0 |
213 | [edx + IPv4_header.DestinationAddress + 0]:1,[edx + IPv4_header.DestinationAddress + 1]:1,\ |
214 | je .dump |
214 | [edx + IPv4_header.DestinationAddress + 2]:1,[edx + IPv4_header.DestinationAddress + 3]:1 |
Line 215... | Line 215... | ||
215 | 215 | ||
216 | ;------------------------------- |
216 | ;------------------------------- |
Line 217... | Line 217... | ||
217 | ; Now, re-calculate the checksum |
217 | ; re-calculate the checksum |
Line 227... | Line 227... | ||
227 | call NET_ptr_to_num |
227 | call NET_ptr_to_num |
228 | shl edi, 2 |
228 | shl edi, 2 |
Line 229... | Line 229... | ||
229 | 229 | ||
Line 230... | Line 230... | ||
230 | ; check if it matches local ip |
230 | ; check if it matches local ip |
231 | 231 | ||
232 | mov eax, [IP_LIST+edi] |
232 | mov eax, [edx + IPv4_header.DestinationAddress] |
Line 233... | Line 233... | ||
233 | cmp [edx + IPv4_header.DestinationAddress], eax |
233 | cmp eax, [IP_LIST+edi] |
Line 234... | Line 234... | ||
234 | je .ip_ok |
234 | je .ip_ok |
235 | - | ||
236 | ; check for broadcast |
- | |
237 | - | ||
238 | mov eax, [SUBNET_LIST+edi] |
235 | |
Line 239... | Line 236... | ||
239 | not eax |
236 | ; check for broadcast (IP or (not SUBNET)) |
Line 240... | Line 237... | ||
240 | or eax, [IP_LIST+edi] |
237 | |
241 | cmp [edx + IPv4_header.DestinationAddress], eax |
238 | cmp eax, [BROADCAST_LIST+edi] |
Line 242... | Line 239... | ||
242 | je .ip_ok |
239 | je .ip_ok |
Line 243... | Line -... | ||
243 | - | ||
244 | ; or a special broadcast |
240 | |
245 | 241 | ; or a special broadcast (255.255.255.255) |
|
246 | cmp [edx + IPv4_header.DestinationAddress], -1 |
242 | |
Line 247... | Line 243... | ||
247 | je .ip_ok |
243 | cmp eax, 0xffffffff |
Line -... | Line 244... | ||
- | 244 | je .ip_ok |
|
248 | 245 | ||
249 | ; maybe it's a multicast then |
246 | ; maybe it's a multicast (224.0.0.0/4) |
Line 250... | Line 247... | ||
250 | 247 | ||
Line 251... | Line 248... | ||
251 | mov eax, [edx + IPv4_header.DestinationAddress] |
248 | and eax, 0x0fffffff |
252 | and eax, 0xff000000 |
249 | cmp eax, 224 |
Line 253... | Line 250... | ||
253 | ; cmp eax, 224 shl 24 |
250 | je .ip_ok |
254 | ; je .ip_ok |
251 | |
Line 302... | Line 299... | ||
302 | je UDP_input |
299 | je UDP_input |
Line 303... | Line 300... | ||
303 | 300 | ||
304 | cmp al, IP_PROTO_ICMP |
301 | cmp al, IP_PROTO_ICMP |
Line 305... | Line 302... | ||
305 | je ICMP_input |
302 | je ICMP_input |
Line 306... | Line 303... | ||
306 | 303 | ||
307 | DEBUGF 2,"unknown Internet protocol: %u\n", al |
304 | DEBUGF 2,"IPv4_input - unknown protocol: %u\n", al |
308 | 305 | ||
309 | .dump: |
306 | .dump: |
310 | DEBUGF 2,"IP_Handler - dumping\n" |
307 | DEBUGF 2,"IPv4_input - dumping\n" |
311 | ; inc [dumped_rx_count] |
308 | ; inc [dumped_rx_count] |
Line 497... | Line 494... | ||
497 | pop ecx |
494 | pop ecx |
498 | xchg cl, ch |
495 | xchg cl, ch |
499 | mov edx, eax |
496 | mov edx, eax |
500 | mov [edx + IPv4_header.TotalLength], cx |
497 | mov [edx + IPv4_header.TotalLength], cx |
501 | add esp, 8 |
498 | add esp, 8 |
- | 499 | xchg cl, ch |
|
- | 500 | push ecx |
|
Line 502... | Line -... | ||
502 | - | ||
503 | xchg cl, ch ; |
- | |
504 | - | ||
505 | push ecx ;;;; |
- | |
506 | push eax ;;;; |
- | |
507 | - | ||
508 | ; mov esi, edx ; This prints the IP packet to the debug board (usefull when using serial output debug..) |
- | |
509 | ; ; |
501 | |
510 | ; packet_to_debug |
- | |
511 | 502 | push eax |
|
Line 512... | Line 503... | ||
512 | jmp .handle_it ; edx = buf ptr, ecx = size, [esp] buf ptr, [esp+4], total size, ebx=device ptr |
503 | jmp .handle_it ; edx = buf ptr, ecx = size, [esp] buf ptr, [esp+4], total size, ebx=device ptr |
513 | 504 | ||
514 | .destroy_slot_pop: |
505 | .destroy_slot_pop: |
Line 525... | Line 516... | ||
525 | ;----------------------------------------------------------------- |
516 | ;----------------------------------------------------------------- |
526 | ; |
517 | ; |
527 | ; find fragment slot |
518 | ; find fragment slot |
528 | ; |
519 | ; |
529 | ; IN: pointer to fragmented packet in edx |
520 | ; IN: pointer to fragmented packet in edx |
530 | ; OUT: pointer to slot in edi, -1 on error |
521 | ; OUT: pointer to slot in esi, -1 on error |
531 | ; |
522 | ; |
532 | ;----------------------------------------------------------------- |
523 | ;----------------------------------------------------------------- |
533 | align 4 |
524 | align 4 |
534 | IPv4_find_fragment_slot: |
525 | IPv4_find_fragment_slot: |
Line 883... | Line 874... | ||
883 | 874 | ||
884 | cmp ebx, edx |
875 | cmp ebx, edx |
885 | je .found_it |
876 | je .found_it |
886 | .next: |
877 | .next: |
- | 878 | add edi, 4 |
|
887 | add edi, 4 |
879 | dec ecx |
Line 888... | Line 880... | ||
888 | loop .loop |
880 | jnz .loop |
889 | 881 | ||
Line 890... | Line 882... | ||
890 | .invalid: |
882 | .invalid: |
Line 965... | Line 957... | ||
965 | mov eax, [IP_LIST + eax] |
957 | mov eax, [IP_LIST + eax] |
966 | ret |
958 | ret |
Line 967... | Line 959... | ||
967 | 959 | ||
968 | .write_ip: |
960 | .write_ip: |
- | 961 | mov [IP_LIST + eax], ecx |
|
- | 962 | ||
- | 963 | ; pre-calculate the local broadcast address |
|
- | 964 | mov ebx, [SUBNET_LIST + eax] |
|
- | 965 | not ebx |
|
- | 966 | or ecx, ebx |
|
- | 967 | mov [BROADCAST_LIST + eax], ecx |
|
969 | mov [IP_LIST + eax], ecx |
968 | |
970 | xor eax, eax |
969 | xor eax, eax |
Line 971... | Line 970... | ||
971 | ret |
970 | ret |
972 | 971 | ||
Line 983... | Line 982... | ||
983 | mov eax, [SUBNET_LIST + eax] |
982 | mov eax, [SUBNET_LIST + eax] |
984 | ret |
983 | ret |
Line 985... | Line 984... | ||
985 | 984 | ||
986 | .write_subnet: |
985 | .write_subnet: |
- | 986 | mov [SUBNET_LIST + eax], ecx |
|
- | 987 | ||
- | 988 | ; pre-calculate the local broadcast address |
|
- | 989 | mov ebx, [IP_LIST + eax] |
|
- | 990 | not ecx |
|
- | 991 | or ecx, ebx |
|
- | 992 | mov [BROADCAST_LIST + eax], ecx |
|
987 | mov [SUBNET_LIST + eax], ecx |
993 | |
988 | xor eax, eax |
994 | xor eax, eax |
Line 989... | Line 995... | ||
989 | ret |
995 | ret |
990 | 996 |