Rev 3643 | Rev 3698 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3545 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
3 | ;; Copyright (C) KolibriOS team 2004-2010. All rights reserved. ;; |
||
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 | $Revision: 2924 $ |
||
20 | |||
21 | ; ICMP types & codes |
||
22 | |||
23 | ICMP_ECHOREPLY = 0 ; echo reply message |
||
24 | |||
25 | ICMP_UNREACH = 3 |
||
26 | ICMP_UNREACH_NET = 0 ; bad net |
||
27 | ICMP_UNREACH_HOST = 1 ; bad host |
||
28 | ICMP_UNREACH_PROTOCOL = 2 ; bad protocol |
||
29 | ICMP_UNREACH_PORT = 3 ; bad port |
||
30 | ICMP_UNREACH_NEEDFRAG = 4 ; IP_DF caused drop |
||
31 | ICMP_UNREACH_SRCFAIL = 5 ; src route failed |
||
32 | ICMP_UNREACH_NET_UNKNOWN = 6 ; unknown net |
||
33 | ICMP_UNREACH_HOST_UNKNOWN = 7 ; unknown host |
||
34 | ICMP_UNREACH_ISOLATED = 8 ; src host isolated |
||
35 | ICMP_UNREACH_NET_PROHIB = 9 ; prohibited access |
||
36 | ICMP_UNREACH_HOST_PROHIB = 10 ; ditto |
||
37 | ICMP_UNREACH_TOSNET = 11 ; bad tos for net |
||
38 | ICMP_UNREACH_TOSHOST = 12 ; bad tos for host |
||
39 | ICMP_UNREACH_FILTER_PROHIB = 13 ; admin prohib |
||
3556 | hidnplayr | 40 | ICMP_UNREACH_HOST_PRECEDENCE = 14 ; host prec vio. |
41 | ICMP_UNREACH_PRECEDENCE_CUTOFF = 15 ; prec cutoff |
||
3545 | hidnplayr | 42 | |
43 | ICMP_SOURCEQUENCH = 4 ; Packet lost, slow down |
||
44 | |||
45 | ICMP_REDIRECT = 5 ; shorter route, codes: |
||
46 | ICMP_REDIRECT_NET = 0 ; for network |
||
47 | ICMP_REDIRECT_HOST = 1 ; for host |
||
48 | ICMP_REDIRECT_TOSNET = 2 ; for tos and net |
||
49 | ICMP_REDIRECT_TOSHOST = 3 ; for tos and host |
||
50 | |||
51 | ICMP_ALTHOSTADDR = 6 ; alternate host address |
||
52 | ICMP_ECHO = 8 ; echo service |
||
53 | ICMP_ROUTERADVERT = 9 ; router advertisement |
||
54 | ICMP_ROUTERADVERT_NORMAL = 0 ; normal advertisement |
||
55 | ICMP_ROUTERADVERT_NOROUTE_COMMON= 16 ; selective routing |
||
56 | |||
57 | ICMP_ROUTERSOLICIT = 10 ; router solicitation |
||
58 | ICMP_TIMXCEED = 11 ; time exceeded, code: |
||
59 | ICMP_TIMXCEED_INTRANS = 0 ; ttl==0 in transit |
||
60 | ICMP_TIMXCEED_REASS = 1 ; ttl==0 in reass |
||
61 | |||
62 | ICMP_PARAMPROB = 12 ; ip header bad |
||
63 | ICMP_PARAMPROB_ERRATPTR = 0 ; error at param ptr |
||
64 | ICMP_PARAMPROB_OPTABSENT = 1 ; req. opt. absent |
||
65 | ICMP_PARAMPROB_LENGTH = 2 ; bad length |
||
66 | |||
67 | ICMP_TSTAMP = 13 ; timestamp request |
||
68 | ICMP_TSTAMPREPLY = 14 ; timestamp reply |
||
69 | ICMP_IREQ = 15 ; information request |
||
70 | ICMP_IREQREPLY = 16 ; information reply |
||
71 | ICMP_MASKREQ = 17 ; address mask request |
||
72 | ICMP_MASKREPLY = 18 ; address mask reply |
||
73 | ICMP_TRACEROUTE = 30 ; traceroute |
||
74 | ICMP_DATACONVERR = 31 ; data conversion error |
||
75 | ICMP_MOBILE_REDIRECT = 32 ; mobile host redirect |
||
76 | ICMP_IPV6_WHEREAREYOU = 33 ; IPv6 where-are-you |
||
77 | ICMP_IPV6_IAMHERE = 34 ; IPv6 i-am-here |
||
78 | ICMP_MOBILE_REGREQUEST = 35 ; mobile registration req |
||
79 | ICMP_MOBILE_REGREPLY = 36 ; mobile registreation reply |
||
80 | ICMP_SKIP = 39 ; SKIP |
||
81 | |||
82 | ICMP_PHOTURIS = 40 ; Photuris |
||
83 | ICMP_PHOTURIS_UNKNOWN_INDEX = 1 ; unknown sec index |
||
84 | ICMP_PHOTURIS_AUTH_FAILED = 2 ; auth failed |
||
85 | ICMP_PHOTURIS_DECRYPT_FAILED = 3 ; decrypt failed |
||
86 | |||
87 | |||
88 | |||
89 | struct ICMP_header |
||
90 | |||
91 | Type db ? |
||
92 | Code db ? |
||
93 | Checksum dw ? |
||
94 | Identifier dw ? |
||
95 | SequenceNumber dw ? |
||
96 | |||
97 | ends |
||
98 | |||
99 | |||
100 | align 4 |
||
101 | uglobal |
||
3600 | hidnplayr | 102 | ICMP_PACKETS_TX rd NET_DEVICES_MAX |
103 | ICMP_PACKETS_RX rd NET_DEVICES_MAX |
||
3545 | hidnplayr | 104 | endg |
105 | |||
106 | |||
107 | |||
108 | ;----------------------------------------------------------------- |
||
109 | ; |
||
110 | ; ICMP_init |
||
111 | ; |
||
112 | ;----------------------------------------------------------------- |
||
113 | |||
114 | macro ICMP_init { |
||
115 | |||
116 | xor eax, eax |
||
117 | mov edi, ICMP_PACKETS_TX |
||
3600 | hidnplayr | 118 | mov ecx, 2*NET_DEVICES_MAX |
3545 | hidnplayr | 119 | rep stosd |
120 | |||
121 | } |
||
122 | |||
123 | |||
124 | ;----------------------------------------------------------------- |
||
125 | ; |
||
126 | ; ICMP_input: |
||
127 | ; |
||
128 | ; This procedure will send reply's to ICMP echo's |
||
129 | ; and insert packets into sockets when needed |
||
130 | ; |
||
131 | ; IN: Pointer to buffer in [esp] |
||
132 | ; size of buffer in [esp+4] |
||
133 | ; ebx = pointer to device struct |
||
134 | ; ecx = ICMP Packet size |
||
135 | ; esi = ptr to ICMP Packet data |
||
136 | ; edi = ptr to ipv4 source and dest address |
||
137 | ; |
||
138 | ; OUT: / |
||
139 | ; |
||
140 | ;----------------------------------------------------------------- |
||
141 | align 4 |
||
142 | ICMP_input: |
||
143 | |||
3556 | hidnplayr | 144 | DEBUGF DEBUG_NETWORK_VERBOSE, "ICMP_input:\n" |
3545 | hidnplayr | 145 | |
146 | ; First, check the checksum (altough some implementations ignore it) |
||
147 | |||
148 | push esi ecx |
||
149 | push [esi + ICMP_header.Checksum] |
||
150 | mov [esi + ICMP_header.Checksum], 0 |
||
151 | xor edx, edx |
||
152 | call checksum_1 |
||
153 | call checksum_2 |
||
154 | pop si |
||
155 | cmp dx, si |
||
156 | pop ecx edx |
||
157 | jne .checksum_mismatch |
||
158 | |||
3643 | hidnplayr | 159 | ; Check packet type |
160 | |||
3545 | hidnplayr | 161 | cmp [edx + ICMP_header.Type], ICMP_ECHO ; Is this an echo request? |
162 | jne .check_sockets |
||
163 | |||
3643 | hidnplayr | 164 | ; Update stats (and validate device ptr) |
165 | call NET_ptr_to_num4 |
||
166 | cmp edi, -1 |
||
167 | je .dump |
||
168 | inc [ICMP_PACKETS_RX + edi] |
||
169 | |||
3545 | hidnplayr | 170 | ; We well re-use the packet so we can create the response as fast as possible |
171 | ; Notice: this only works on pure ethernet |
||
172 | |||
3556 | hidnplayr | 173 | DEBUGF DEBUG_NETWORK_VERBOSE, "got echo request\n" |
3545 | hidnplayr | 174 | mov [edx + ICMP_header.Type], ICMP_ECHOREPLY ; Change Packet type to reply |
175 | |||
176 | mov esi, [esp] ; Start of buffer |
||
3601 | hidnplayr | 177 | cmp ebx, LOOPBACK_DEVICE |
178 | je .loopback |
||
179 | |||
180 | ; FIXME: dont assume device is an ethernet device! |
||
181 | |||
3545 | hidnplayr | 182 | ; exchange dest and source address in IP header |
183 | ; exchange dest and source MAC in ETH header |
||
184 | push dword [esi + ETH_header.DstMAC] |
||
185 | push dword [esi + ETH_header.SrcMAC] |
||
186 | pop dword [esi + ETH_header.DstMAC] |
||
187 | pop dword [esi + ETH_header.SrcMAC] |
||
188 | push word [esi + ETH_header.DstMAC + 4] |
||
189 | push word [esi + ETH_header.SrcMAC + 4] |
||
190 | pop word [esi + ETH_header.DstMAC + 4] |
||
191 | pop word [esi + ETH_header.SrcMAC + 4] |
||
3601 | hidnplayr | 192 | add esi, sizeof.ETH_header-4 |
3545 | hidnplayr | 193 | |
194 | .loopback: |
||
3601 | hidnplayr | 195 | add esi, 4 |
3545 | hidnplayr | 196 | push [esi + IPv4_header.SourceAddress] |
197 | push [esi + IPv4_header.DestinationAddress] |
||
198 | pop [esi + IPv4_header.SourceAddress] |
||
199 | pop [esi + IPv4_header.DestinationAddress] |
||
200 | |||
201 | ; Recalculate ip header checksum |
||
202 | movzx ecx, [esi + IPv4_header.VersionAndIHL] ; Calculate IP Header length by using IHL field |
||
203 | and ecx, 0x0f |
||
204 | shl cx, 2 |
||
205 | mov edi, ecx ; IP header length |
||
206 | mov eax, edx ; ICMP packet start addr |
||
207 | |||
208 | push esi ; Calculate the IP checksum |
||
209 | xor edx, edx ; |
||
210 | call checksum_1 ; |
||
211 | call checksum_2 ; |
||
212 | pop esi ; |
||
213 | mov [esi + IPv4_header.HeaderChecksum], dx ; |
||
214 | |||
215 | ; Recalculate ICMP CheckSum |
||
216 | movzx ecx, [esi + IPv4_header.TotalLength] ; Find length of IP Packet |
||
217 | xchg ch, cl ; |
||
218 | sub ecx, edi ; IP packet length - IP header length = ICMP packet length |
||
219 | |||
220 | mov esi, eax ; Calculate ICMP checksum |
||
221 | xor edx, edx ; |
||
222 | call checksum_1 ; |
||
223 | call checksum_2 ; |
||
224 | mov [eax + ICMP_header.Checksum], dx ; |
||
225 | |||
226 | ; Transmit the packet (notice that packet ptr and packet size have been on stack since start of the procedure!) |
||
227 | call [ebx + NET_DEVICE.transmit] |
||
3643 | hidnplayr | 228 | test eax, eax |
229 | jnz @f |
||
230 | call NET_ptr_to_num4 |
||
231 | inc [UDP_PACKETS_TX + edi] |
||
232 | @@: |
||
3545 | hidnplayr | 233 | ret |
234 | |||
235 | |||
236 | |||
237 | |||
238 | .check_sockets: |
||
239 | ; Look for an open ICMP socket |
||
240 | |||
3647 | hidnplayr | 241 | pusha |
242 | mov ecx, socket_mutex |
||
243 | call mutex_lock |
||
244 | popa |
||
245 | |||
3545 | hidnplayr | 246 | mov esi, [edi] ; ipv4 source address |
247 | mov eax, net_sockets |
||
248 | .try_more: |
||
249 | ; mov , [edx + ICMP_header.Identifier] |
||
250 | .next_socket: |
||
251 | mov eax, [eax + SOCKET.NextPtr] |
||
252 | or eax, eax |
||
3647 | hidnplayr | 253 | jz .dump_ |
3545 | hidnplayr | 254 | |
255 | cmp [eax + SOCKET.Domain], AF_INET4 |
||
256 | jne .next_socket |
||
257 | |||
258 | cmp [eax + SOCKET.Protocol], IP_PROTO_ICMP |
||
259 | jne .next_socket |
||
260 | |||
261 | cmp [eax + IP_SOCKET.RemoteIP], esi |
||
262 | jne .next_socket |
||
263 | |||
264 | ; cmp [eax + ICMP_SOCKET.Identifier], |
||
265 | ; jne .next_socket |
||
266 | |||
3643 | hidnplayr | 267 | ; Update stats (and validate device ptr) |
268 | call NET_ptr_to_num4 |
||
269 | cmp edi, -1 |
||
3647 | hidnplayr | 270 | je .dump_ |
3643 | hidnplayr | 271 | inc [ICMP_PACKETS_RX + edi] |
3545 | hidnplayr | 272 | |
3647 | hidnplayr | 273 | pusha |
274 | mov ecx, socket_mutex |
||
275 | call mutex_unlock |
||
276 | popa |
||
277 | |||
3556 | hidnplayr | 278 | DEBUGF DEBUG_NETWORK_VERBOSE, "socket=%x\n", eax |
3545 | hidnplayr | 279 | |
280 | pusha |
||
281 | lea ecx, [eax + SOCKET.mutex] |
||
282 | call mutex_lock |
||
283 | popa |
||
284 | |||
285 | mov esi, edx |
||
286 | jmp SOCKET_input |
||
287 | |||
3647 | hidnplayr | 288 | .dump_: |
3545 | hidnplayr | 289 | |
3647 | hidnplayr | 290 | pusha |
291 | mov ecx, socket_mutex |
||
292 | call mutex_unlock |
||
293 | popa |
||
294 | |||
295 | DEBUGF DEBUG_NETWORK_VERBOSE, "ICMP_input: no socket found\n" |
||
296 | jmp .dump |
||
297 | |||
298 | |||
3545 | hidnplayr | 299 | .checksum_mismatch: |
3556 | hidnplayr | 300 | DEBUGF DEBUG_NETWORK_VERBOSE, "checksum mismatch\n" |
3545 | hidnplayr | 301 | |
302 | .dump: |
||
3556 | hidnplayr | 303 | DEBUGF DEBUG_NETWORK_VERBOSE, "ICMP_input: dumping\n" |
3545 | hidnplayr | 304 | |
305 | call kernel_free |
||
306 | add esp, 4 ; pop (balance stack) |
||
307 | |||
308 | ret |
||
309 | |||
310 | |||
311 | ;----------------------------------------------------------------- |
||
312 | ; |
||
313 | ; ICMP_output |
||
314 | ; |
||
315 | ; IN: eax = dest ip |
||
316 | ; ebx = source ip |
||
317 | ; ecx = data length |
||
318 | ; dh = type |
||
319 | ; dl = code |
||
320 | ; esi = data offset |
||
321 | ; edi = identifier shl 16 + sequence number |
||
322 | ; |
||
323 | ;----------------------------------------------------------------- |
||
324 | align 4 |
||
325 | ICMP_output: |
||
326 | |||
3556 | hidnplayr | 327 | DEBUGF DEBUG_NETWORK_VERBOSE, "Creating ICMP Packet\n" |
3545 | hidnplayr | 328 | |
329 | push esi edi dx |
||
330 | |||
331 | mov edx, [eax + IP_SOCKET.LocalIP] |
||
332 | mov eax, [eax + IP_SOCKET.RemoteIP] |
||
333 | add ecx, sizeof.ICMP_header |
||
334 | mov di, IP_PROTO_ICMP SHL 8 + 128 ; TTL |
||
335 | call IPv4_output |
||
336 | jz .exit |
||
337 | |||
3556 | hidnplayr | 338 | DEBUGF DEBUG_NETWORK_VERBOSE, "full icmp packet size: %u\n", edx |
3545 | hidnplayr | 339 | |
340 | pop word [edi + ICMP_header.Type] ; Write both type and code bytes at once |
||
341 | pop dword [edi + ICMP_header.Identifier] ; identifier and sequence number |
||
342 | mov [edi + ICMP_header.Checksum], 0 |
||
343 | |||
344 | push ebx ecx edx |
||
345 | mov esi, edi |
||
346 | xor edx, edx |
||
347 | call checksum_1 |
||
348 | call checksum_2 |
||
349 | mov [edi + ICMP_header.Checksum], dx |
||
350 | pop edx ecx ebx esi |
||
351 | |||
352 | sub ecx, sizeof.ICMP_header |
||
353 | add edi, sizeof.ICMP_header |
||
354 | push cx |
||
355 | shr cx, 2 |
||
356 | rep movsd |
||
357 | pop cx |
||
358 | and cx, 3 |
||
359 | rep movsb |
||
360 | |||
361 | sub edi, edx ;;; TODO: find a better way to remember start of packet |
||
362 | push edx edi |
||
3556 | hidnplayr | 363 | DEBUGF DEBUG_NETWORK_VERBOSE, "Sending ICMP Packet\n" |
3545 | hidnplayr | 364 | call [ebx + NET_DEVICE.transmit] |
3643 | hidnplayr | 365 | test eax, eax |
366 | jnz @f |
||
367 | call NET_ptr_to_num4 |
||
368 | inc [ICMP_PACKETS_TX + edi] |
||
369 | @@: |
||
3545 | hidnplayr | 370 | ret |
371 | .exit: |
||
3556 | hidnplayr | 372 | DEBUGF DEBUG_NETWORK_ERROR, "Creating ICMP Packet failed\n" |
3545 | hidnplayr | 373 | add esp, 2*4 + 2 |
374 | ret |
||
375 | |||
376 | |||
377 | |||
378 | |||
379 | ;----------------------------------------------------------------- |
||
380 | ; |
||
381 | ; ICMP_output |
||
382 | ; |
||
383 | ; IN: eax = socket ptr |
||
384 | ; ecx = data length |
||
385 | ; esi = data offset |
||
386 | ; |
||
387 | ;----------------------------------------------------------------- |
||
388 | align 4 |
||
389 | ICMP_output_raw: |
||
390 | |||
3556 | hidnplayr | 391 | DEBUGF DEBUG_NETWORK_VERBOSE, "Creating ICMP Packet for socket %x, data ptr=%x\n", eax, edx |
3545 | hidnplayr | 392 | |
393 | push edx |
||
394 | |||
395 | mov di, IP_PROTO_ICMP SHL 8 + 128 ; TTL |
||
396 | mov edx, [eax + IP_SOCKET.LocalIP] |
||
397 | mov eax, [eax + IP_SOCKET.RemoteIP] |
||
398 | call IPv4_output |
||
399 | jz .exit |
||
400 | |||
401 | pop esi |
||
402 | push edx |
||
403 | push eax |
||
404 | |||
405 | push edi ecx |
||
3556 | hidnplayr | 406 | DEBUGF DEBUG_NETWORK_VERBOSE, "copying %u bytes from %x to %x\n", ecx, esi, edi |
3545 | hidnplayr | 407 | rep movsb |
408 | pop ecx edi |
||
409 | |||
410 | mov [edi + ICMP_header.Checksum], 0 |
||
411 | |||
412 | mov esi, edi |
||
413 | xor edx, edx |
||
414 | call checksum_1 |
||
415 | call checksum_2 |
||
416 | mov [edi + ICMP_header.Checksum], dx |
||
417 | |||
3556 | hidnplayr | 418 | DEBUGF DEBUG_NETWORK_VERBOSE, "Sending ICMP Packet\n" |
3545 | hidnplayr | 419 | call [ebx + NET_DEVICE.transmit] |
3643 | hidnplayr | 420 | test eax, eax |
421 | jnz @f |
||
422 | call NET_ptr_to_num4 |
||
423 | inc [ICMP_PACKETS_TX + edi] |
||
424 | @@: |
||
3545 | hidnplayr | 425 | ret |
426 | .exit: |
||
3556 | hidnplayr | 427 | DEBUGF DEBUG_NETWORK_ERROR, "Creating ICMP Packet failed\n" |
3545 | hidnplayr | 428 | add esp, 4 |
429 | ret |
||
430 | |||
431 | |||
432 | |||
433 | |||
434 | ;----------------------------------------------------------------- |
||
435 | ; |
||
436 | ; ICMP_API |
||
437 | ; |
||
438 | ; This function is called by system function 75 |
||
439 | ; |
||
440 | ; IN: subfunction number in bl |
||
441 | ; device number in bh |
||
442 | ; ecx, edx, .. depends on subfunction |
||
443 | ; |
||
444 | ; OUT: |
||
445 | ; |
||
446 | ;----------------------------------------------------------------- |
||
447 | align 4 |
||
448 | ICMP_api: |
||
449 | |||
450 | movzx eax, bh |
||
451 | shl eax, 2 |
||
452 | |||
453 | test bl, bl |
||
454 | jz .packets_tx ; 0 |
||
455 | dec bl |
||
456 | jz .packets_rx ; 1 |
||
457 | |||
458 | .error: |
||
459 | mov eax, -1 |
||
460 | ret |
||
461 | |||
462 | .packets_tx: |
||
463 | mov eax, [ICMP_PACKETS_TX + eax] |
||
464 | ret |
||
465 | |||
466 | .packets_rx: |
||
467 | mov eax, [ICMP_PACKETS_RX + eax] |
||
468 | ret |