Subversion Repositories Kolibri OS

Rev

Rev 1473 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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