Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1159 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
1196 | hidnplayr | 3 | ;; Copyright (C) KolibriOS team 2004-2009. All rights reserved. ;; |
1159 | hidnplayr | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;; ICMP.INC ;; |
||
7 | ;; ;; |
||
8 | ;; Part of the tcp/ip network stack for KolibriOS ;; |
||
9 | ;; ;; |
||
10 | ;; Based on the work of [Johnny_B] and [smb] ;; |
||
11 | ;; ;; |
||
12 | ;; Written by hidnplayr@kolibrios.org ;; |
||
13 | ;; ;; |
||
14 | ;; GNU GENERAL PUBLIC LICENSE ;; |
||
15 | ;; Version 2, June 1991 ;; |
||
16 | ;; ;; |
||
17 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
18 | |||
19 | |||
1206 | hidnplayr | 20 | $Revision: 1257 $ |
1159 | hidnplayr | 21 | |
22 | ; ICMP types & codes |
||
23 | |||
24 | ICMP_ECHOREPLY equ 0 ; echo reply message |
||
25 | |||
26 | ICMP_UNREACH equ 3 |
||
27 | ICMP_UNREACH_NET equ 0 ; bad net |
||
28 | ICMP_UNREACH_HOST equ 1 ; bad host |
||
29 | ICMP_UNREACH_PROTOCOL equ 2 ; bad protocol |
||
30 | ICMP_UNREACH_PORT equ 3 ; bad port |
||
31 | ICMP_UNREACH_NEEDFRAG equ 4 ; IP_DF caused drop |
||
32 | ICMP_UNREACH_SRCFAIL equ 5 ; src route failed |
||
33 | ICMP_UNREACH_NET_UNKNOWN equ 6 ; unknown net |
||
34 | ICMP_UNREACH_HOST_UNKNOWN equ 7 ; unknown host |
||
35 | ICMP_UNREACH_ISOLATED equ 8 ; src host isolated |
||
36 | ICMP_UNREACH_NET_PROHIB equ 9 ; prohibited access |
||
37 | ICMP_UNREACH_HOST_PROHIB equ 10 ; ditto |
||
38 | ICMP_UNREACH_TOSNET equ 11 ; bad tos for net |
||
39 | ICMP_UNREACH_TOSHOST equ 12 ; bad tos for host |
||
40 | ICMP_UNREACH_FILTER_PROHIB equ 13 ; admin prohib |
||
41 | ICMP_UNREACH_HOST_PRECEDENCE equ 14 ; host prec vio. |
||
42 | ICMP_UNREACH_PRECEDENCE_CUTOFF equ 15 ; prec cutoff |
||
43 | |||
44 | ICMP_SOURCEQUENCH equ 4 ; Packet lost, slow down |
||
45 | |||
46 | ICMP_REDIRECT equ 5 ; shorter route, codes: |
||
47 | ICMP_REDIRECT_NET equ 0 ; for network |
||
48 | ICMP_REDIRECT_HOST equ 1 ; for host |
||
49 | ICMP_REDIRECT_TOSNET equ 2 ; for tos and net |
||
50 | ICMP_REDIRECT_TOSHOST equ 3 ; for tos and host |
||
51 | |||
52 | ICMP_ALTHOSTADDR equ 6 ; alternate host address |
||
53 | ICMP_ECHO equ 8 ; echo service |
||
54 | ICMP_ROUTERADVERT equ 9 ; router advertisement |
||
55 | ICMP_ROUTERADVERT_NORMAL equ 0 ; normal advertisement |
||
56 | ICMP_ROUTERADVERT_NOROUTE_COMMON equ 16 ; selective routing |
||
57 | |||
58 | ICMP_ROUTERSOLICIT equ 10 ; router solicitation |
||
59 | ICMP_TIMXCEED equ 11 ; time exceeded, code: |
||
60 | ICMP_TIMXCEED_INTRANS equ 0 ; ttl==0 in transit |
||
61 | ICMP_TIMXCEED_REASS equ 1 ; ttl==0 in reass |
||
62 | |||
63 | ICMP_PARAMPROB equ 12 ; ip header bad |
||
64 | ICMP_PARAMPROB_ERRATPTR equ 0 ; error at param ptr |
||
65 | ICMP_PARAMPROB_OPTABSENT equ 1 ; req. opt. absent |
||
66 | ICMP_PARAMPROB_LENGTH equ 2 ; bad length |
||
67 | |||
68 | ICMP_TSTAMP equ 13 ; timestamp request |
||
69 | ICMP_TSTAMPREPLY equ 14 ; timestamp reply |
||
70 | ICMP_IREQ equ 15 ; information request |
||
71 | ICMP_IREQREPLY equ 16 ; information reply |
||
72 | ICMP_MASKREQ equ 17 ; address mask request |
||
73 | ICMP_MASKREPLY equ 18 ; address mask reply |
||
74 | ICMP_TRACEROUTE equ 30 ; traceroute |
||
75 | ICMP_DATACONVERR equ 31 ; data conversion error |
||
76 | ICMP_MOBILE_REDIRECT equ 32 ; mobile host redirect |
||
77 | ICMP_IPV6_WHEREAREYOU equ 33 ; IPv6 where-are-you |
||
78 | ICMP_IPV6_IAMHERE equ 34 ; IPv6 i-am-here |
||
79 | ICMP_MOBILE_REGREQUEST equ 35 ; mobile registration req |
||
80 | ICMP_MOBILE_REGREPLY equ 36 ; mobile registreation reply |
||
81 | ICMP_SKIP equ 39 ; SKIP |
||
82 | |||
83 | ICMP_PHOTURIS equ 40 ; Photuris |
||
84 | ICMP_PHOTURIS_UNKNOWN_INDEX equ 1 ; unknown sec index |
||
85 | ICMP_PHOTURIS_AUTH_FAILED equ 2 ; auth failed |
||
86 | ICMP_PHOTURIS_DECRYPT_FAILED equ 3 ; decrypt failed |
||
87 | |||
88 | |||
89 | |||
90 | struct ICMP_Packet |
||
91 | .Type db ? |
||
92 | .Code db ? |
||
93 | .Checksum dw ? |
||
94 | .Identifier dw ? |
||
95 | .SequenceNumber dw ? |
||
96 | .Data: |
||
97 | ends |
||
98 | |||
99 | |||
100 | align 4 |
||
101 | uglobal |
||
102 | ICMP_PACKETS_TX rd MAX_IP |
||
103 | ICMP_PACKETS_RX rd MAX_IP |
||
104 | endg |
||
105 | |||
1185 | hidnplayr | 106 | |
107 | |||
1159 | hidnplayr | 108 | ;----------------------------------------------------------------- |
109 | ; |
||
110 | ; ICMP_init |
||
111 | ; |
||
112 | ; This function resets all ICMP variables |
||
113 | ; |
||
114 | ; IN: / |
||
115 | ; OUT: / |
||
116 | ; |
||
117 | ;----------------------------------------------------------------- |
||
118 | align 4 |
||
119 | ICMP_init: |
||
120 | |||
121 | xor eax, eax |
||
122 | mov edi, ICMP_PACKETS_TX |
||
123 | mov ecx, 2*MAX_IP |
||
124 | rep stosd |
||
125 | |||
126 | ret |
||
1171 | hidnplayr | 127 | |
128 | |||
129 | |||
130 | |||
1257 | hidnplayr | 131 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 132 | ; |
133 | ; ICMP_Handler: |
||
134 | ; |
||
1249 | hidnplayr | 135 | ; this procedure will send reply's to ICMP echo's |
136 | ; and insert packets into sockets when needed ;;; TODO: update this to work with fragmented packets too! |
||
1159 | hidnplayr | 137 | ; |
138 | ; IN: Pointer to buffer in [esp] |
||
139 | ; size of buffer in [esp+4] |
||
140 | ; pointer to device struct in ebx |
||
141 | ; ICMP Packet size in ecx |
||
142 | ; pointer to ICMP Packet data in edx |
||
143 | ; OUT: / |
||
144 | ; |
||
1257 | hidnplayr | 145 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 146 | align 4 |
1196 | hidnplayr | 147 | ICMP_handler: ;TODO: works only on pure ethernet right now ! |
1159 | hidnplayr | 148 | |
149 | DEBUGF 1,"ICMP_Handler - start\n" |
||
1185 | hidnplayr | 150 | cmp byte [edx + ICMP_Packet.Type], ICMP_ECHO ; Is this an echo request? |
1159 | hidnplayr | 151 | jne .check_sockets |
152 | |||
153 | mov byte [edx + ICMP_Packet.Type], ICMP_ECHOREPLY ; Change Packet type to reply |
||
154 | mov word [edx + ICMP_Packet.Checksum], 0 ; Set checksum to 0, needed to calculate new checksum |
||
155 | |||
1171 | hidnplayr | 156 | call ETH_struc2dev |
157 | cmp edi,-1 |
||
158 | je .dump |
||
159 | inc [ICMP_PACKETS_RX+4*edi] |
||
1185 | hidnplayr | 160 | inc [ICMP_PACKETS_TX+4*edi] |
1171 | hidnplayr | 161 | |
1159 | hidnplayr | 162 | ; exchange dest and source address in IP header |
163 | ; exchange dest and source MAC in ETH header |
||
164 | mov esi, [esp] |
||
165 | |||
166 | mov eax, dword [esi + ETH_FRAME.DstMAC] |
||
167 | mov ecx, dword [esi + ETH_FRAME.SrcMAC] |
||
168 | mov dword [esi + ETH_FRAME.SrcMAC], eax |
||
169 | mov dword [esi + ETH_FRAME.DstMAC], ecx |
||
170 | |||
171 | mov ax, word [esi + ETH_FRAME.DstMAC + 4] |
||
172 | mov cx, word [esi + ETH_FRAME.SrcMAC + 4] |
||
173 | mov word [esi + ETH_FRAME.SrcMAC + 4], ax |
||
174 | mov word [esi + ETH_FRAME.DstMAC + 4], cx |
||
175 | |||
176 | mov eax, dword [esi + ETH_FRAME.Data + IPv4_Packet.SourceAddress] |
||
177 | mov ecx, dword [esi + ETH_FRAME.Data + IPv4_Packet.DestinationAddress] |
||
178 | mov dword [esi + ETH_FRAME.Data + IPv4_Packet.DestinationAddress], eax |
||
179 | mov dword [esi + ETH_FRAME.Data + IPv4_Packet.SourceAddress], ecx |
||
180 | |||
181 | ; Recalculate ip header checksum |
||
182 | add esi, ETH_FRAME.Data ; Point esi to start of IP Packet |
||
1249 | hidnplayr | 183 | movzx ecx, byte [esi + IPv4_Packet.VersionAndIHL] ; Calculate IP Header length by using IHL field |
184 | and ecx, 0x0000000F ; |
||
185 | shl cx , 2 |
||
186 | push ebx edx ecx esi |
||
187 | xor edx, edx |
||
188 | call checksum_1 |
||
189 | call checksum_2 |
||
190 | pop esi |
||
191 | mov word [esi + IPv4_Packet.HeaderChecksum], dx ; Store it in the IP Packet header |
||
1159 | hidnplayr | 192 | |
193 | ; Recalculate ICMP CheckSum |
||
1249 | hidnplayr | 194 | movzx eax, word[esi + IPv4_Packet.TotalLength] ; Find length of IP Packet |
1159 | hidnplayr | 195 | xchg ah , al ; |
1249 | hidnplayr | 196 | sub eax, [esp] ; Now we know the length of ICMP data in eax |
197 | mov ecx, eax |
||
198 | mov esi, [esp + 4] |
||
199 | xor edx, edx |
||
200 | call checksum_1 |
||
201 | call checksum_2 |
||
202 | mov ax , dx |
||
203 | pop ecx edx ebx |
||
1159 | hidnplayr | 204 | mov word [edx + ICMP_Packet.Checksum], ax |
205 | |||
1249 | hidnplayr | 206 | jmp ETH_sender ; Send the reply |
1159 | hidnplayr | 207 | |
1171 | hidnplayr | 208 | |
209 | |||
210 | |||
211 | |||
1159 | hidnplayr | 212 | .check_sockets: |
1206 | hidnplayr | 213 | ; TODO: validate the header & checksum. |
1159 | hidnplayr | 214 | |
215 | ; Look for an open ICMP socket |
||
216 | |||
217 | mov esi, net_sockets |
||
218 | .try_more: |
||
219 | mov ax , [edx + ICMP_Packet.Identifier] |
||
220 | .next_socket: |
||
1249 | hidnplayr | 221 | mov esi, [esi + SOCKET_head.NextPtr] |
1159 | hidnplayr | 222 | or esi, esi |
223 | jz .dump |
||
1249 | hidnplayr | 224 | cmp [esi + SOCKET_head.Type], IP_PROTO_ICMP |
1159 | hidnplayr | 225 | jne .next_socket |
1249 | hidnplayr | 226 | cmp [esi + SOCKET_head.end + IPv4_SOCKET.end + ICMP_SOCKET.Identifier], ax |
1159 | hidnplayr | 227 | jne .next_socket |
228 | |||
1171 | hidnplayr | 229 | call IPv4_dest_to_dev |
230 | cmp edi,-1 |
||
231 | je .dump |
||
232 | inc [ICMP_PACKETS_RX+4*edi] |
||
233 | |||
1159 | hidnplayr | 234 | DEBUGF 1,"Found valid ICMP packet for socket %x\n", esi |
235 | |||
1249 | hidnplayr | 236 | lea ebx, [esi + SOCKET_head.lock] |
1159 | hidnplayr | 237 | call wait_mutex |
238 | |||
1249 | hidnplayr | 239 | ; Now, assign data to socket. We have socket address in esi. |
1159 | hidnplayr | 240 | ; We have ICMP Packet in edx |
241 | ; number of bytes in ecx |
||
242 | |||
1249 | hidnplayr | 243 | mov eax, esi |
1159 | hidnplayr | 244 | pop esi |
1249 | hidnplayr | 245 | add esp, 4 |
246 | sub edx, esi |
||
247 | mov edi, edx |
||
248 | jmp socket_internal_receiver |
||
1159 | hidnplayr | 249 | |
250 | .dump: |
||
251 | DEBUGF 1,"ICMP_Handler - dumping\n" |
||
252 | |||
253 | call kernel_free |
||
1249 | hidnplayr | 254 | add esp, 4 ; pop (balance stack) |
1159 | hidnplayr | 255 | |
256 | ret |
||
257 | |||
258 | |||
1257 | hidnplayr | 259 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 260 | ; |
261 | ; ICMP_Handler_fragments: |
||
262 | ; |
||
263 | ; Called by IP_handler, |
||
264 | ; this procedure will send reply's to ICMP echo's etc |
||
265 | ; |
||
266 | ; IN: Pointer to buffer in [esp] |
||
267 | ; size of buffer in [esp+4] |
||
268 | ; pointer to device struct in ebx |
||
269 | ; ICMP Packet size in ecx |
||
270 | ; pointer to ICMP Packet data in edx |
||
271 | ; OUT: / |
||
272 | ; |
||
1257 | hidnplayr | 273 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 274 | align 4 |
1196 | hidnplayr | 275 | ICMP_handler_fragments: ; works only on pure ethernet right now ! |
1159 | hidnplayr | 276 | |
277 | DEBUGF 1,"ICMP_Handler_fragments - start\n" |
||
278 | |||
279 | cmp ecx, 65500 |
||
280 | jg .dump |
||
281 | |||
282 | cmp byte [edx + ICMP_Packet.Type], ICMP_ECHO ; Is this an echo request? discard if not |
||
283 | jne .dump |
||
284 | |||
285 | mov esi, [esp] |
||
286 | |||
287 | sub ecx, ICMP_Packet.Data |
||
288 | mov eax, [esi + IPv4_Packet.SourceAddress] |
||
289 | mov ebx, [esi + IPv4_Packet.DestinationAddress] |
||
290 | push word [esi + IPv4_Packet.Identification] |
||
291 | |||
292 | mov di , [edx + ICMP_Packet.Identifier] |
||
293 | shl edi, 16 |
||
294 | mov di , [edx + ICMP_Packet.SequenceNumber] |
||
295 | |||
296 | mov esi, edx |
||
297 | add esi, ICMP_Packet.Data |
||
298 | pop dx |
||
299 | shl edx, 16 |
||
300 | mov dx , ICMP_ECHOREPLY shl 8 + 0 ; Type + Code |
||
301 | |||
1196 | hidnplayr | 302 | call ICMP_create_packet |
1159 | hidnplayr | 303 | |
304 | .dump: |
||
305 | DEBUGF 1,"ICMP_Handler_fragments - end\n" |
||
306 | |||
307 | call kernel_free |
||
1249 | hidnplayr | 308 | add esp, 4 ; pop (balance stack) |
1159 | hidnplayr | 309 | ret |
310 | |||
1257 | hidnplayr | 311 | |
1159 | hidnplayr | 312 | ;----------------------------------------------------------------- |
313 | ; |
||
314 | ; Note: ICMP only works on top of IP protocol :) |
||
315 | ; |
||
316 | ; inputs: |
||
317 | ; |
||
318 | ; eax = dest ip |
||
319 | ; ebx = source ip |
||
320 | ; ecx = data length |
||
321 | ; dh = type |
||
322 | ; dl = code |
||
323 | ; high 16 bits of edx = fragment id (for IP header) |
||
324 | ; esi = data offset |
||
325 | ; edi = identifier shl 16 + sequence number |
||
326 | ; |
||
327 | ;----------------------------------------------------------------- |
||
328 | align 4 |
||
1196 | hidnplayr | 329 | ICMP_create_packet: |
1159 | hidnplayr | 330 | |
331 | DEBUGF 1,"Create ICMP Packet\n" |
||
332 | |||
333 | push esi edi edx |
||
334 | |||
335 | add ecx, ICMP_Packet.Data |
||
336 | mov di , IP_PROTO_ICMP |
||
337 | shr edx, 16 |
||
338 | |||
1196 | hidnplayr | 339 | call IPv4_create_packet |
1159 | hidnplayr | 340 | |
341 | cmp edi, -1 |
||
342 | je .exit |
||
343 | |||
1206 | hidnplayr | 344 | DEBUGF 1,"full icmp packet size: %u\n", edx |
1165 | hidnplayr | 345 | |
1159 | hidnplayr | 346 | pop eax |
347 | mov word [edi + ICMP_Packet.Type], ax ; Write both type and code bytes at once |
||
348 | pop eax |
||
349 | mov [edi + ICMP_Packet.SequenceNumber], ax |
||
350 | shr eax, 16 |
||
351 | mov [edi + ICMP_Packet.Identifier], ax |
||
352 | mov [edi + ICMP_Packet.Checksum], 0 |
||
353 | |||
1249 | hidnplayr | 354 | push eax ebx ecx edx |
355 | mov esi, edi |
||
356 | xor edx, edx |
||
357 | call checksum_1 |
||
358 | call checksum_2 |
||
359 | mov [edi + ICMP_Packet.Checksum], dx |
||
360 | pop edx ecx ebx eax esi |
||
1159 | hidnplayr | 361 | |
362 | sub ecx, ICMP_Packet.Data |
||
363 | add edi, ICMP_Packet.Data |
||
364 | push cx |
||
365 | shr cx , 2 |
||
366 | rep movsd |
||
367 | pop cx |
||
368 | and cx , 3 |
||
369 | rep movsb |
||
370 | |||
1206 | hidnplayr | 371 | sub edi, edx ;; TODO: find a better way to remember start of packet |
1159 | hidnplayr | 372 | mov ecx, [ebx + ETH_DEVICE.transmit] |
373 | push edx edi ecx |
||
374 | DEBUGF 1,"Sending ICMP Packet\n" |
||
375 | ret ; Send the packet (create_packet routine outputs pointer to routine to send packet in eax) |
||
376 | |||
377 | .exit: |
||
1165 | hidnplayr | 378 | DEBUGF 1,"Creating ICMP Packet failed\n" |
379 | add esp, 3*4 |
||
1159 | hidnplayr | 380 | ret |
381 | |||
382 | |||
383 | |||
384 | |||
1257 | hidnplayr | 385 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 386 | ; |
387 | ; ICMP_API |
||
388 | ; |
||
389 | ; This function is called by system function 75 |
||
390 | ; |
||
391 | ; IN: subfunction number in bl |
||
392 | ; device number in bh |
||
393 | ; ecx, edx, .. depends on subfunction |
||
394 | ; |
||
395 | ; OUT: |
||
396 | ; |
||
1257 | hidnplayr | 397 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 398 | align 4 |
399 | ICMP_API: |
||
400 | |||
401 | movzx eax, bh |
||
402 | shl eax, 2 |
||
403 | |||
404 | test bl, bl |
||
405 | jz .packets_tx ; 0 |
||
406 | dec bl |
||
407 | jz .packets_rx ; 1 |
||
408 | |||
409 | .error: |
||
410 | mov eax, -1 |
||
411 | ret |
||
412 | |||
413 | .packets_tx: |
||
414 | add eax, ICMP_PACKETS_TX |
||
415 | mov eax, [eax] |
||
416 | ret |
||
417 | |||
418 | .packets_rx: |
||
419 | add eax, ICMP_PACKETS_RX |
||
420 | mov eax, [eax] |
||
421 | ret |