Subversion Repositories Kolibri OS

Rev

Rev 1541 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1159 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
1514 hidnplayr 3
;; Copyright (C) KolibriOS team 2004-2010. 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: 1542 $
1159 hidnplayr 21
 
22
; ICMP types & codes
23
 
1514 hidnplayr 24
ICMP_ECHOREPLY			equ 0		    ; echo reply message
1159 hidnplayr 25
 
1514 hidnplayr 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
1159 hidnplayr 43
 
1514 hidnplayr 44
ICMP_SOURCEQUENCH		equ 4		    ; Packet lost, slow down
1159 hidnplayr 45
 
1514 hidnplayr 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
1159 hidnplayr 51
 
1514 hidnplayr 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
1159 hidnplayr 57
 
1514 hidnplayr 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
1159 hidnplayr 62
 
1514 hidnplayr 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
1159 hidnplayr 67
 
1514 hidnplayr 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
1159 hidnplayr 82
 
1514 hidnplayr 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
1159 hidnplayr 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
;-----------------------------------------------------------------
113
 
1529 hidnplayr 114
macro ICMP_init {
115
 
1159 hidnplayr 116
	xor	eax, eax
117
	mov	edi, ICMP_PACKETS_TX
118
	mov	ecx, 2*MAX_IP
119
	rep	stosd
120
 
1529 hidnplayr 121
}
1171 hidnplayr 122
 
123
 
124
 
125
 
1257 hidnplayr 126
;-----------------------------------------------------------------
1159 hidnplayr 127
;
1514 hidnplayr 128
; ICMP_input:
1159 hidnplayr 129
;
1514 hidnplayr 130
;  This procedure will send reply's to ICMP echo's
131
;   and insert packets into sockets when needed
1159 hidnplayr 132
;
133
;  IN:  Pointer to buffer in [esp]
134
;       size of buffer in [esp+4]
1541 hidnplayr 135
;       ebx = pointer to device struct
136
;       ecx = ICMP Packet size
137
;       edx = ptr to ICMP Packet data
138
;       esi = ipv4 source address
139
;       edi = ipv4 dest   address
1159 hidnplayr 140
;  OUT: /
141
;
1257 hidnplayr 142
;-----------------------------------------------------------------
1159 hidnplayr 143
align 4
1514 hidnplayr 144
ICMP_input:
1159 hidnplayr 145
 
1541 hidnplayr 146
;;; TODO: check checksum!
1514 hidnplayr 147
 
1541 hidnplayr 148
	DEBUGF	1,"ICMP_input - start\n"
1185 hidnplayr 149
	cmp	byte [edx + ICMP_Packet.Type], ICMP_ECHO		    ; Is this an echo request?
1159 hidnplayr 150
	jne	.check_sockets
151
 
1541 hidnplayr 152
	DEBUGF	1,"ICMP_input - echo request\n"
1473 hidnplayr 153
 
1159 hidnplayr 154
	mov	byte [edx + ICMP_Packet.Type], ICMP_ECHOREPLY		    ; Change Packet type to reply
155
	mov	word [edx + ICMP_Packet.Checksum], 0			    ; Set checksum to 0, needed to calculate new checksum
156
 
1514 hidnplayr 157
	call	NET_ptr_to_num
1171 hidnplayr 158
	cmp	edi,-1
159
	je	.dump
160
	inc	[ICMP_PACKETS_RX+4*edi]
1185 hidnplayr 161
	inc	[ICMP_PACKETS_TX+4*edi]
1171 hidnplayr 162
 
1159 hidnplayr 163
; exchange dest and source address in IP header
164
; exchange dest and source MAC in ETH header
165
	mov	esi, [esp]
166
 
167
	mov	eax, dword [esi + ETH_FRAME.DstMAC]
168
	mov	ecx, dword [esi + ETH_FRAME.SrcMAC]
169
	mov	dword [esi + ETH_FRAME.SrcMAC], eax
170
	mov	dword [esi + ETH_FRAME.DstMAC], ecx
171
 
172
	mov	ax, word [esi + ETH_FRAME.DstMAC + 4]
173
	mov	cx, word [esi + ETH_FRAME.SrcMAC + 4]
174
	mov	word [esi + ETH_FRAME.SrcMAC + 4], ax
175
	mov	word [esi + ETH_FRAME.DstMAC + 4], cx
176
 
177
	mov	eax, dword [esi + ETH_FRAME.Data + IPv4_Packet.SourceAddress]
178
	mov	ecx, dword [esi + ETH_FRAME.Data + IPv4_Packet.DestinationAddress]
179
	mov	dword [esi + ETH_FRAME.Data + IPv4_Packet.DestinationAddress], eax
180
	mov	dword [esi + ETH_FRAME.Data + IPv4_Packet.SourceAddress], ecx
181
 
182
; Recalculate ip header checksum
183
	add	esi, ETH_FRAME.Data					    ; Point esi to start of IP Packet
1249 hidnplayr 184
	movzx	ecx, byte [esi + IPv4_Packet.VersionAndIHL]		    ; Calculate IP Header length by using IHL field
185
	and	ecx, 0x0000000F 					    ;
186
	shl	cx , 2
187
	push	ebx edx ecx esi
188
	xor	edx, edx
189
	call	checksum_1
190
	call	checksum_2
191
	pop	esi
192
	mov	word [esi + IPv4_Packet.HeaderChecksum], dx		    ; Store it in the IP Packet header
1159 hidnplayr 193
 
194
; Recalculate ICMP CheckSum
1249 hidnplayr 195
	movzx	eax, word[esi + IPv4_Packet.TotalLength]		    ; Find length of IP Packet
1159 hidnplayr 196
	xchg	ah , al 						    ;
1249 hidnplayr 197
	sub	eax, [esp]						     ; Now we know the length of ICMP data in eax
198
	mov	ecx, eax
199
	mov	esi, [esp + 4]
200
	xor	edx, edx
201
	call	checksum_1
202
	call	checksum_2
203
	mov	ax , dx
204
	pop	ecx edx ebx
1159 hidnplayr 205
	mov	word [edx + ICMP_Packet.Checksum], ax
206
 
1519 hidnplayr 207
	call	[ebx + NET_DEVICE.transmit]
208
	ret
1159 hidnplayr 209
 
1171 hidnplayr 210
 
211
 
212
 
1159 hidnplayr 213
       .check_sockets:
214
	; Look for an open ICMP socket
1541 hidnplayr 215
	; esi = sender ip
1159 hidnplayr 216
 
1541 hidnplayr 217
	mov	ebx, net_sockets
1159 hidnplayr 218
  .try_more:
1541 hidnplayr 219
;        mov     ax , [edx + ICMP_Packet.Identifier]
1159 hidnplayr 220
  .next_socket:
1541 hidnplayr 221
	mov	ebx, [ebx + SOCKET.NextPtr]
222
	or	ebx, ebx
1159 hidnplayr 223
	jz	.dump
1541 hidnplayr 224
 
225
	cmp	[ebx + SOCKET.Domain], AF_INET4
1159 hidnplayr 226
	jne	.next_socket
1541 hidnplayr 227
 
228
	cmp	[ebx + SOCKET.Protocol], IP_PROTO_ICMP
229
	jne	.next_socket
1171 hidnplayr 230
 
1541 hidnplayr 231
	cmp	[ebx + IP_SOCKET.RemoteIP], esi
232
	jne	.next_socket
1159 hidnplayr 233
 
1541 hidnplayr 234
;        cmp     [esi + ICMP_SOCKET.Identifier], ax
235
;        jne     .next_socket
236
 
237
;        call    IPv4_dest_to_dev
238
;        cmp     edi,-1
239
;        je      .dump
240
;        inc     [ICMP_PACKETS_RX+edi]
241
 
242
	DEBUGF 1,"Found valid ICMP packet for socket %x\n", ebx
243
 
244
	mov	eax, ebx
245
	add	ebx, SOCKET.lock
1159 hidnplayr 246
	call	wait_mutex
247
 
1541 hidnplayr 248
	mov	esi, edx
1519 hidnplayr 249
	jmp	SOCKET_input
1159 hidnplayr 250
 
1541 hidnplayr 251
 
1159 hidnplayr 252
       .dump:
253
	DEBUGF	1,"ICMP_Handler - dumping\n"
254
 
255
	call	kernel_free
1249 hidnplayr 256
	add	esp, 4 ; pop (balance stack)
1159 hidnplayr 257
 
258
	ret
259
 
260
 
1257 hidnplayr 261
;-----------------------------------------------------------------
1159 hidnplayr 262
;
1519 hidnplayr 263
; ICMP_output
1159 hidnplayr 264
;
1519 hidnplayr 265
; IN:  eax = dest ip
266
;      ebx = source ip
267
;      ecx = data length
268
;      dh  = type
269
;      dl  = code
270
;      high 16 bits of edx = fragment id (for IP header)
271
;      esi = data offset
272
;      edi = identifier shl 16 + sequence number
1159 hidnplayr 273
;
274
;-----------------------------------------------------------------
275
align 4
1514 hidnplayr 276
ICMP_output:
1159 hidnplayr 277
 
1519 hidnplayr 278
	DEBUGF	1,"Creating ICMP Packet\n"
1159 hidnplayr 279
 
280
	push	esi edi edx
281
 
1541 hidnplayr 282
	mov	ebx, [eax + IP_SOCKET.LocalIP]
283
	mov	eax, [eax + IP_SOCKET.RemoteIP]
1159 hidnplayr 284
	add	ecx, ICMP_Packet.Data
1529 hidnplayr 285
	mov	di , IP_PROTO_ICMP SHL 8 + 128	; TTL
1159 hidnplayr 286
	shr	edx, 16
287
 
1529 hidnplayr 288
	call	IPv4_output
1514 hidnplayr 289
	jz	.exit
1159 hidnplayr 290
 
1519 hidnplayr 291
	DEBUGF	1,"full icmp packet size: %u\n", edx
1165 hidnplayr 292
 
1159 hidnplayr 293
	pop	eax
294
	mov	word [edi + ICMP_Packet.Type], ax	; Write both type and code bytes at once
295
	pop	eax
296
	mov	[edi + ICMP_Packet.SequenceNumber], ax
297
	shr	eax, 16
298
	mov	[edi + ICMP_Packet.Identifier], ax
299
	mov	[edi + ICMP_Packet.Checksum], 0
300
 
1249 hidnplayr 301
	push	eax ebx ecx edx
302
	mov	esi, edi
303
	xor	edx, edx
304
	call	checksum_1
305
	call	checksum_2
306
	mov	[edi + ICMP_Packet.Checksum], dx
307
	pop	edx ecx ebx eax esi
1159 hidnplayr 308
 
309
	sub	ecx, ICMP_Packet.Data
310
	add	edi, ICMP_Packet.Data
311
	push	cx
312
	shr	cx , 2
313
	rep	movsd
314
	pop	cx
315
	and	cx , 3
316
	rep	movsb
317
 
1514 hidnplayr 318
	sub	edi, edx				;;; TODO: find a better way to remember start of packet
1519 hidnplayr 319
	push	edx edi
320
	DEBUGF	1,"Sending ICMP Packet\n"
321
	call	[ebx + NET_DEVICE.transmit]
322
	ret
1159 hidnplayr 323
  .exit:
1519 hidnplayr 324
	DEBUGF	1,"Creating ICMP Packet failed\n"
1165 hidnplayr 325
	add	esp, 3*4
1159 hidnplayr 326
	ret
327
 
328
 
329
 
330
 
1257 hidnplayr 331
;-----------------------------------------------------------------
1159 hidnplayr 332
;
1541 hidnplayr 333
; ICMP_output
334
;
335
; IN:  eax = socket ptr
336
;      ecx = data length
337
;      esi = data offset
338
;
339
;-----------------------------------------------------------------
340
align 4
341
ICMP_output_raw:
342
 
343
	DEBUGF	1,"Creating ICMP Packet for socket %x, data ptr=%x\n", eax, edx
344
 
345
	push	edx
346
 
347
	mov	di, IP_PROTO_ICMP SHL 8 + 128  ; TTL
348
	shr	edx, 16
349
	mov	ebx, [eax + IP_SOCKET.LocalIP]
350
	mov	eax, [eax + IP_SOCKET.RemoteIP]
351
	call	IPv4_output
352
	jz	.exit
353
 
354
	pop	esi
355
	push	edx
356
	push	eax
357
 
358
	push	edi ecx
359
	DEBUGF	1,"copying %u bytes from %x to %x\n", ecx, esi, edi
360
	rep	movsb
361
	pop	ecx edi
362
 
363
	mov	[edi + ICMP_Packet.Checksum], 0
364
 
365
	mov	esi, edi
366
	xor	edx, edx
367
	call	checksum_1
368
	call	checksum_2
369
	mov	[edi + ICMP_Packet.Checksum], dx
370
 
371
	DEBUGF	1,"Sending ICMP Packet\n"
372
	call	[ebx + NET_DEVICE.transmit]
373
	ret
374
  .exit:
375
	DEBUGF	1,"Creating ICMP Packet failed\n"
376
	add	esp, 4
377
	ret
378
 
379
 
380
 
381
 
382
;-----------------------------------------------------------------
383
;
1159 hidnplayr 384
; ICMP_API
385
;
386
; This function is called by system function 75
387
;
388
; IN:  subfunction number in bl
389
;      device number in bh
390
;      ecx, edx, .. depends on subfunction
391
;
392
; OUT:
393
;
1257 hidnplayr 394
;-----------------------------------------------------------------
1159 hidnplayr 395
align 4
396
ICMP_API:
397
 
398
	movzx	eax, bh
399
	shl	eax, 2
400
 
401
	test	bl, bl
402
	jz	.packets_tx	; 0
403
	dec	bl
404
	jz	.packets_rx	; 1
405
 
406
.error:
407
	mov	eax, -1
408
	ret
409
 
410
.packets_tx:
411
	add	eax, ICMP_PACKETS_TX
412
	mov	eax, [eax]
413
	ret
414
 
415
.packets_rx:
416
	add	eax, ICMP_PACKETS_RX
417
	mov	eax, [eax]
418
	ret