Subversion Repositories Kolibri OS

Rev

Rev 1251 | Go to most recent revision | Details | Compare with Previous | 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
;;                                                                 ;;
1249 hidnplayr 6
;;  IPv4.INC                                                       ;;
1159 hidnplayr 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
 
1206 hidnplayr 19
$Revision: 1254 $
1159 hidnplayr 20
 
21
; IP underlying protocols numbers
22
 
23
ETHER_IPv4	equ 0x0008	; Reversed from 0800 for intel
24
 
25
MAX_FRAGMENTS	equ 16
26
MAX_IP		equ MAX_NET_DEVICES
27
 
28
struct	IPv4_Packet
29
	.VersionAndIHL		db  ?  ; Version[0-3 bits] and IHL(header length)[4-7 bits]
30
	.TypeOfService		db  ?
31
	.TotalLength		dw  ?
32
	.Identification 	dw  ?
33
	.FlagsAndFragmentOffset dw  ?  ; Flags[0-2] and FragmentOffset[3-15]
34
	.TimeToLive		db  ?  ;
35
	.Protocol		db  ?
36
	.HeaderChecksum 	dw  ?
37
	.SourceAddress		dd  ?
38
	.DestinationAddress	dd  ?
39
	.DataOrOptional:
40
ends
41
 
42
struct	FRAGMENT_slot
43
	.ttl			dw  ?  ; Time to live for this entry, 0 for empty slot's
44
	.id			dw  ?  ; Identification field from IP header
45
	.SrcIP			dd  ?  ; .. from IP header
46
	.DstIP			dd  ?  ; .. from IP header
47
	.ptr			dd  ?  ; Pointer to first packet
48
	.size:
49
ends
50
 
51
struct	FRAGMENT_entry		       ; This structure will replace the ethernet header in fragmented ip packets
52
	.PrevPtr		dd  ?  ; Pointer to previous fragment entry  (-1 for first packet)
53
	.NextPtr		dd  ?  ; Pointer to next fragment entry (-1 for last packet)
54
	.Owner			dd  ?  ; Pointer to structure of driver
1249 hidnplayr 55
				rb  2  ; to match ethernet header size          ; TODO: fix this hack
1159 hidnplayr 56
	.Data:			       ; Ip header begins here (we will need the IP header to re-construct the complete packet)
57
ends
58
 
59
align 4
60
uglobal
61
	BROADCAST	dd  ?
62
	IP_LIST 	rd  MAX_IP
63
	SUBNET_LIST	rd  MAX_IP
64
	DNS_LIST	rd  MAX_IP
65
	GATEWAY_LIST	rd  MAX_IP
66
	IP_PACKETS_TX	rd  MAX_IP
67
	IP_PACKETS_RX	rd  MAX_IP
68
	FRAGMENT_LIST	rb  MAX_FRAGMENTS*FRAGMENT_slot.size
69
endg
70
 
71
 
72
;-----------------------------------------------------------------
73
;
74
; IPv4_init
75
;
76
;  This function resets all IP variables
77
;
78
;  IN:  /
79
;  OUT: /
80
;
81
;-----------------------------------------------------------------
82
 
83
align 4
84
IPv4_init:
85
 
86
	or	eax, -1
87
	mov	edi, BROADCAST
1200 hidnplayr 88
	mov	ecx, 4*MAX_IP+1
1159 hidnplayr 89
	rep	stosd
90
 
91
	xor	eax, eax
92
	mov	edi, FRAGMENT_LIST
93
	mov	ecx, FRAGMENT_slot.size*MAX_FRAGMENTS/4 + 2*MAX_IP
94
	rep	stosd
95
 
96
	ret
97
 
98
 
99
 
100
;-----------------------------------------------------------------
101
;
1249 hidnplayr 102
; IPv4_Handler:
1159 hidnplayr 103
;
1249 hidnplayr 104
;  Will check if IP Packet isnt damaged
1159 hidnplayr 105
;  and call appropriate handler. (TCP/UDP/ICMP/..)
106
;
107
;  It will also re-construct fragmented packets
108
;
109
;  IN:  Pointer to buffer in [esp]
110
;       size of buffer in [esp+4]
111
;       pointer to device struct in ebx
112
;       pointer to IP Packet data in edx
113
;  OUT: /
114
;
115
;-----------------------------------------------------------------
116
 
117
align 4
1196 hidnplayr 118
IPv4_handler:
1159 hidnplayr 119
 
120
	DEBUGF	1,"IP_Handler - start\n"
121
 
1249 hidnplayr 122
	push	edx ebx
1159 hidnplayr 123
 
1249 hidnplayr 124
	; save checksum, and clear it in original packet
125
	mov	di , [edx + IPv4_Packet.HeaderChecksum]
126
	mov	word [edx + IPv4_Packet.HeaderChecksum], 0
1159 hidnplayr 127
 
1249 hidnplayr 128
	; Re-calculate checksum
129
	movzx	ecx, byte [edx + IPv4_Packet.VersionAndIHL]	; Calculate Header length by using IHL field
130
	and	ecx, 0x0000000F  ;
131
	shl	cx , 2		 ;
132
	mov	esi, edx
133
	xor	edx, edx
134
	call	checksum_1
135
	call	checksum_2
136
 
137
	; now compare the two..
138
	cmp	dx, di
139
	pop	ebx edx
140
	jne	.dump						; if checksum isn't valid then dump packet
141
 
1254 hidnplayr 142
	DEBUGF	1,"IPv4 Checksum is correct\n",di
143
 
1159 hidnplayr 144
	mov	eax, [edx + IPv4_Packet.DestinationAddress]
145
	mov	edi, BROADCAST
146
	mov	ecx, MAX_IP+1
147
	repnz	scasd
148
	jz	.ip_ok
149
 
150
	not	eax
151
	test	eax, 127 shl 24 ; 127.x.x.x
152
	jz	.ip_ok
153
 
154
;  TODO: we need to check for broadcasts (other then 255.255.255.255)
155
 
156
	jmp	.dump
157
 
158
  .ip_ok:
1171 hidnplayr 159
	call	ETH_struc2dev					; TODO: make this work on other protocols too!
160
	inc	[IP_PACKETS_RX+4*edi]
1254 hidnplayr 161
	DEBUGF	1,"packet comes from %u.%u.%u.%u\n",\
1159 hidnplayr 162
	[edx + IPv4_Packet.SourceAddress]:1,[edx + IPv4_Packet.SourceAddress + 1]:1,[edx + IPv4_Packet.SourceAddress + 2]:1,[edx + IPv4_Packet.SourceAddress + 3]:1
163
 
164
	mov	al , [edx + IPv4_Packet.VersionAndIHL]
165
	and	al , 0x0f					; get IHL(header length)
166
	cmp	al , 0x05					; IHL!= 5*4(20 bytes)
167
	jnz	.dump						; TODO: dont dump packets wich have optional fiels !!!                   /!\
168
 
169
	cmp	byte [edx + IPv4_Packet.TimeToLive], 0
170
	je	.dump
171
 
172
	movzx	eax, word [edx + IPv4_Packet.FlagsAndFragmentOffset]
173
	xchg	al , ah
174
 
175
	test	ax , 1 shl 13					; Is 'more fragments' flag set ?
176
	jnz	.yes_fragments					; If so, we definately have a fragmented packet
177
 
178
	test	ax , 0x1fff					; If flag is not set, but there is a fragment offset, the packet is last in series of fragmented packets
179
	jnz	.last_fragment
180
 
181
   .handle_it:							; We reach here if packet hasnt been fragmented, or when it already has been re-constructed
182
	movzx	eax, byte [edx + IPv4_Packet.VersionAndIHL]	; Calculate Header length by using IHL field
183
	and	eax, 0x0000000F 				;
184
	shl	eax, 2						;
185
 
186
	movzx	ecx, word [edx + IPv4_Packet.TotalLength]	; Calculate length of encapsulated Packet
187
	xchg	cl , ch 					;
188
	sub	ecx, eax					;
189
 
190
	add	eax, edx
191
	push	eax
192
	mov	al , [edx + IPv4_Packet.Protocol]
1249 hidnplayr 193
	mov	esi, [edx + IPv4_Packet.SourceAddress]
194
	mov	edi, [edx + IPv4_Packet.DestinationAddress]
1159 hidnplayr 195
	pop	edx						; Offset to data (tcp/udp/icmp/.. Packet)
196
 
197
	cmp	al , IP_PROTO_TCP
1249 hidnplayr 198
	je	TCP_handler
1159 hidnplayr 199
 
200
	cmp	al , IP_PROTO_UDP
1196 hidnplayr 201
	je	UDP_handler
1159 hidnplayr 202
 
203
	cmp	al , IP_PROTO_ICMP
1196 hidnplayr 204
	je	ICMP_handler
1159 hidnplayr 205
 
1254 hidnplayr 206
	DEBUGF	1,"unknown protocol: %u\n",al
1159 hidnplayr 207
 
208
  .dump:
209
	DEBUGF	1,"IP_Handler - done\n"
210
;        inc     [dumped_rx_count]
211
	call	kernel_free
212
	add	esp, 4						; pop (balance stack)
213
	ret
214
 
215
 
216
  .yes_fragments:
217
	shl	ax , 3
218
	DEBUGF	1,"Fragmented packet, offset:%u, id:%x\n", ax, [edx + IPv4_Packet.Identification]:4
219
 
220
	test	ax , ax 					; Is this the first packet of the fragment?
221
	jnz	.not_first_fragment
222
 
223
	DEBUGF	1,"First fragmented packet received!\n"
224
								; try to locate a free slot..
225
	mov	ecx, MAX_FRAGMENTS
226
	mov	esi, FRAGMENT_LIST
227
     .find_free_slot:
228
	cmp	word [esi + FRAGMENT_slot.ttl], 0
229
	je	.found_free_slot
230
	add	esi, FRAGMENT_slot.size
231
	loop	.find_free_slot
232
	jmp	.dump						; If no free slot was found, dump the packet
233
 
234
     .found_free_slot:						; We found a free slot, let's fill in the FRAGMENT_slot structure
235
	mov	word [esi + FRAGMENT_slot.ttl], 15		; RFC recommends 15 secs as ttl
236
	mov	ax , word [edx + IPv4_Packet.Identification]
237
	mov	word [esi + FRAGMENT_slot.id], ax
238
	mov	eax, dword [edx + IPv4_Packet.SourceAddress]
239
	mov	dword [esi + FRAGMENT_slot.SrcIP], eax
240
	mov	eax, dword [edx + IPv4_Packet.DestinationAddress]
241
	mov	dword [esi + FRAGMENT_slot.DstIP], eax
242
	pop	eax
243
	mov	dword [esi + FRAGMENT_slot.ptr], eax
244
								; Now, replace ethernet header in original buffer with a FRAGMENT_entry structure
245
	mov	[eax + FRAGMENT_entry.NextPtr], -1
246
	mov	[eax + FRAGMENT_entry.PrevPtr], -1
247
	mov	[eax + FRAGMENT_entry.Owner], ebx
248
 
249
	add	esp, 4						; balance stack and exit
250
	ret
251
 
252
 
253
 
254
  .not_first_fragment:
255
	DEBUGF	1,"Middle fragmented packet received!\n"
256
 
257
	call	.find_fragment_slot
258
	cmp	esi, -1
259
	je	.dump
260
 
261
	mov	word [esi + FRAGMENT_slot.ttl], 15		; Reset the ttl
262
	mov	esi, [esi + FRAGMENT_slot.ptr]
263
	or	edi, -1
264
     .find_last_entry:						; The following routine will try to find the last entry
265
	cmp	edi, [esi + FRAGMENT_entry.PrevPtr]
266
	jne	.destroy_slot					; Damn, something screwed up, remove the whole slot (and free buffers too if possible!)
267
	mov	edi, esi
268
	mov	esi, [esi + FRAGMENT_entry.NextPtr]
269
	cmp	esi, -1
270
	jne	.find_last_entry
271
								; We found the last entry (pointer is noww in edi)
272
								; We are going to overwrite the ethernet header in received packet with a FRAGMENT_entry structure
273
 
274
	pop	eax						; pointer to packet
275
	mov	[edi + FRAGMENT_entry.NextPtr], eax		; update pointer of previous entry to the new entry
276
	mov	[eax + FRAGMENT_entry.NextPtr], -1
277
	mov	[eax + FRAGMENT_entry.PrevPtr], edi
278
	mov	[eax + FRAGMENT_entry.Owner], ebx
279
 
280
	add	esp, 4
281
	ret
282
 
283
 
284
 
285
  .last_fragment:
286
	DEBUGF	1,"Last fragmented packet received!\n"
287
	call	.find_fragment_slot
288
	cmp	esi, -1
289
	je	.dump
290
 
291
	mov	esi, [esi + FRAGMENT_slot.ptr]			; We found the first entry, let's calculate total size of the packet in eax, so we can allocate a buffer
292
	push	esi
293
	xor	eax, eax					;
294
	or	edi, -1
295
     .count_bytes:
296
	cmp	[esi + FRAGMENT_entry.PrevPtr], edi
297
	jne	.destroy_slot_pop						; Damn, something screwed up, remove the whole slot (and free buffers too if possible!)
298
	mov	cx, word [esi + FRAGMENT_entry.Data + IPv4_Packet.TotalLength]	  ; Add total length
299
	xchg	cl, ch
300
	DEBUGF	1,"Packet size: %u\n", cx
301
	add	ax, cx
302
	movzx	cx, byte [esi + FRAGMENT_entry.Data + IPv4_Packet.VersionAndIHL]  ; Sub Header length
303
	and	cx, 0x000F
304
	shl	cx, 2
305
	DEBUGF	1,"Header size: %u\n", cx
306
	sub	ax, cx
307
	mov	edi, esi
308
	mov	esi, [esi + FRAGMENT_entry.NextPtr]
309
	cmp	esi, -1
310
	jne	.count_bytes
311
 
312
	mov	esi, [esp+4]  ;;;
313
	mov	[edi + FRAGMENT_entry.NextPtr], esi			       ; Add this packet to the chain, this simplifies the following code
314
	mov	[esi + FRAGMENT_entry.NextPtr], -1
315
	mov	[esi + FRAGMENT_entry.PrevPtr], edi
316
	mov	[esi + FRAGMENT_entry.Owner], ebx
317
 
318
	mov	cx, [edx + IPv4_Packet.TotalLength]			       ; Note: This time we dont substract Header length
319
	xchg	cl , ch
320
	DEBUGF	1,"Packet size: %u\n", cx
321
	add	ax , cx
322
	DEBUGF	1,"Total Received data size: %u\n", eax
323
 
324
	push	eax
325
	mov	ax , [edx + IPv4_Packet.FlagsAndFragmentOffset]
326
	xchg	al , ah
327
	shl	ax , 3
328
	add	cx , ax
329
	pop	eax
330
	DEBUGF	1,"Total Fragment size: %u\n", ecx
331
 
332
	cmp	ax, cx
333
	jne	.destroy_slot_pop
334
 
335
	push	eax
336
	push	eax
337
	call	kernel_alloc
338
	test	eax, eax
339
	je	.destroy_slot_pop							; If we dont have enough space to allocate the buffer, discard all packets in slot
340
	mov	edx, [esp+4]								; Get pointer to first fragment entry back in edx
341
 
342
     .rebuild_packet_loop:
343
	movzx	ecx, word [edx + FRAGMENT_entry.Data + IPv4_Packet.FlagsAndFragmentOffset]	; Calculate the fragment offset
344
	xchg	cl , ch 									;   intel byte order
345
	shl	cx , 3										;   multiply by 8 and clear first 3 bits
346
	DEBUGF	1,"Fragment offset: %u\n", cx
347
 
348
	lea	edi, [eax + ecx]								; Notice that edi will be equal to eax for first fragment
349
	movzx	ebx, byte [edx + FRAGMENT_entry.Data + IPv4_Packet.VersionAndIHL]		; Find header size (in ebx) of fragment
350
	and	bx , 0x000F									;
351
	shl	bx , 2										;
352
 
353
	lea	esi, [edx + FRAGMENT_entry.Data]						; Set esi to the correct begin of fragment
354
	movzx	ecx, word [edx + FRAGMENT_entry.Data + IPv4_Packet.TotalLength] 		; Calculate total length of fragment
355
	xchg	cl, ch										;  intel byte order
356
 
357
	cmp	edi, eax									; Is this packet the first fragment ?
358
	je	.first_fragment
359
	sub	cx, bx										; If not, dont copy the header
360
	add	esi, ebx									;
361
     .first_fragment:
362
 
363
	push	cx										; First copy dword-wise, then byte-wise
364
	shr	cx, 2										;
365
	rep	movsd										;
366
	pop	cx										;
367
	and	cx, 3										;
368
	rep	movsb										;
369
 
370
	push	eax
371
	push	edx										; Push pointer to fragment onto stack
372
	mov	edx, [edx + FRAGMENT_entry.NextPtr]						; Set edx to the next pointer
373
	call	kernel_free									; free the previous fragment buffer (this uses the value from stack)
374
	pop	eax
375
	cmp	edx, -1 									; Check if it is last fragment in chain
376
	jne	.rebuild_packet_loop
377
 
378
	pop	ecx										;
379
	xchg	cl, ch
380
	mov	edx, eax
381
	mov	word [edx + IPv4_Packet.TotalLength], cx
382
	add	esp, 8
383
 
384
	xchg	cl, ch		   ;  This prints the IP packet to the debug board (usefull when using serial output debug..)
385
	push	ecx  ;;;;
386
	push	eax	;;;;
387
;        mov     esi, edx           ;
388
;                                   ;
389
;       @@:                         ;
390
;        lodsb                      ;
391
;        DEBUGF  1,"%x ", eax:2     ;
392
;        loop    @r                 ;
393
 
394
	movzx	eax, byte [edx + IPv4_Packet.VersionAndIHL]	; Calculate Header length by using IHL field
395
	and	ax, 0x000F					;
396
	shl	ax, 2						;
1249 hidnplayr 397
 
398
	sub	ecx, eax
399
 
400
 
1159 hidnplayr 401
	add	eax, edx
402
	push	eax
403
	mov	al , [edx + IPv4_Packet.Protocol]
1249 hidnplayr 404
	mov	esi, [edx + IPv4_Packet.SourceAddress]
405
	mov	edi, [edx + IPv4_Packet.DestinationAddress]
1159 hidnplayr 406
	pop	edx						; Offset to data (tcp/udp/icmp/.. Packet)
407
 
1196 hidnplayr 408
	cmp	al , IP_PROTO_TCP
1249 hidnplayr 409
	je	TCP_handler
1159 hidnplayr 410
 
1171 hidnplayr 411
	cmp	al , IP_PROTO_UDP
1196 hidnplayr 412
	je	UDP_handler
1159 hidnplayr 413
 
414
	cmp	al , IP_PROTO_ICMP
1196 hidnplayr 415
	je	ICMP_handler_fragments
1159 hidnplayr 416
 
417
	DEBUGF	1,"IP_Handler - unknown protocol:%u\n",al
418
 
1166 hidnplayr 419
	call	kernel_free
420
	add	esp, 8						; pop (balance stack)
421
 
1165 hidnplayr 422
	ret
1159 hidnplayr 423
 
424
 
425
  .destroy_slot_pop:
426
	add	esp, 4
427
  .destroy_slot:
1166 hidnplayr 428
	DEBUGF	1,"Destroy fragment slot!\n"
1159 hidnplayr 429
	; TODO!
430
	jmp	.dump
431
 
432
 
433
 
434
;-----------------------------------------------------------------
435
;
436
; find fragment slot
437
;
438
; IN: pointer to fragmented packet in edx         ; TODO: the RFC says we should check protocol too
439
; OUT: pointer to slot in edi, -1 on error
440
;
441
;-----------------------------------------------------------------
442
 
443
  .find_fragment_slot:
444
 
445
	push	eax ebx ecx edx
446
	mov	ax , word [edx + IPv4_Packet.Identification]
447
	mov	ecx, MAX_FRAGMENTS
448
	mov	esi, FRAGMENT_LIST
449
	mov	ebx, dword [edx + IPv4_Packet.SourceAddress]
450
	mov	edx, dword [edx + IPv4_Packet.DestinationAddress]
451
  .find_slot:
452
	cmp	word [esi + FRAGMENT_slot.id], ax
453
	jne	.try_next
454
	cmp	dword [esi + FRAGMENT_slot.SrcIP], ebx
455
	jne	.try_next
456
	cmp	dword [esi + FRAGMENT_slot.DstIP], edx
457
	je	.found_slot
458
  .try_next:
459
	add	esi, FRAGMENT_slot.size
460
	loop	.find_slot
461
 ;       pop     edx ebx
462
	or	esi, -1
463
;        ret
464
 
465
  .found_slot:
466
	pop	edx ecx ebx eax
467
	ret
468
 
469
 
470
;-----------------------------------------------------------------
471
;
472
; Decrease TimeToLive of all fragment slots
473
;
474
; IN: /
475
; OUT: /
476
;
477
;-----------------------------------------------------------------
478
 
479
align 4
480
IPv4_decrease_fragment_ttls:
481
 
482
	mov	esi, FRAGMENT_LIST
483
	mov	ecx, MAX_FRAGMENTS
484
  .loop:
485
	cmp	[esi + FRAGMENT_slot.ttl], 0
486
	je	.try_next
487
	dec	[esi + FRAGMENT_slot.ttl]
488
	jnz	.try_next
489
	DEBUGF 1,"Fragment slot timed-out!\n"
490
	; TODO: clear all entry's of timed-out slot
491
  .try_next:
492
	add	esi, 4
493
	loop	.loop
494
	ret
495
 
496
 
497
 
498
 
499
 
500
;-----------------------------------------------------------------
501
;
502
; Create_IPv4_Packet
503
;
504
; IN: eax = dest ip
505
;     ebx = source ip
506
;     ecx = data length
507
;     dx  = fragment id
508
;     di  = protocol
509
;
1249 hidnplayr 510
; OUT: eax = pointer to buffer start
511
;      ebx = pointer to device struct (needed for sending procedure)
1159 hidnplayr 512
;      ecx = unchanged (packet size of embedded data)
1249 hidnplayr 513
;      edx = size of complete buffer
1159 hidnplayr 514
;      esi = pointer to sending procedure
1249 hidnplayr 515
;      edi = pointer to start of data (-1 on error)
1159 hidnplayr 516
;
517
;-----------------------------------------------------------------
518
 
519
;;; TODO: create fragmented packets
520
 
521
align 4
1196 hidnplayr 522
IPv4_create_packet:
1159 hidnplayr 523
 
1206 hidnplayr 524
	DEBUGF 1,"Create IPv4 Packet (size=%u)\n", ecx
1159 hidnplayr 525
 
1206 hidnplayr 526
	cmp	ecx, 1480
1159 hidnplayr 527
	jg	.exit_
528
 
1251 clevermous 529
	test	ebx, ebx	      ; if dest ip = 0
1208 hidnplayr 530
	jnz	.ip_ok		      ; and local ip is valid
531
				      ; use local ip instead
532
	cmp	[IP_LIST],0xffffffff  ;
533
	je	.ip_ok		      ; TODO: find solution to send broadcast
534
				      ; on device other then device 0
535
	mov	ebx, [IP_LIST]	      ;
536
				      ;
537
  .ip_ok:			      ;
538
 
1206 hidnplayr 539
	push	ecx eax ebx dx di
540
 
1159 hidnplayr 541
	cmp	eax, -1
542
	je	.broadcast		  ; If it is broadcast, just send
543
 
1196 hidnplayr 544
	call	ARP_IP_to_MAC
1159 hidnplayr 545
 
1196 hidnplayr 546
	cmp	eax, -1
1206 hidnplayr 547
	je	.not_found
1196 hidnplayr 548
 
1206 hidnplayr 549
	push	ebx
1196 hidnplayr 550
	push	ax
551
 
552
	jmp	.send
553
 
1159 hidnplayr 554
  .broadcast:
1196 hidnplayr 555
	push	word -1
556
	push	dword -1
1159 hidnplayr 557
 
558
  .send:
559
	call	IPv4_dest_to_dev
1171 hidnplayr 560
	inc	[IP_PACKETS_TX+4*edi]
1206 hidnplayr 561
	mov	edx, [ETH_DRV_LIST + 4*edi]
562
	lea	eax, [edx + ETH_DEVICE.mac]
563
	mov	ebx, esp
564
	mov	ecx, [esp+18]	 ;; 18 or  22 ??
1159 hidnplayr 565
	add	ecx, IPv4_Packet.DataOrOptional
566
	mov	di , ETHER_IPv4
1249 hidnplayr 567
	call	ETH_create_packet		   ; TODO: figure out a way to make this work with other protocols too
1206 hidnplayr 568
	add	esp, 6
1159 hidnplayr 569
	cmp	edi, -1
570
	je	.exit
571
 
572
	mov	[edi + IPv4_Packet.VersionAndIHL], 0x45   ; IPv4, normal length (no Optional header)
573
	mov	[edi + IPv4_Packet.TypeOfService], 0
574
	xchg	ch, cl
575
	mov	[edi + IPv4_Packet.TotalLength], cx
576
	mov	[edi + IPv4_Packet.FlagsAndFragmentOffset], 0x0000
577
	mov	[edi + IPv4_Packet.TimeToLive], 128
578
	mov	[edi + IPv4_Packet.HeaderChecksum], 0
579
	pop	cx
580
	mov	[edi + IPv4_Packet.Protocol], cl
581
	pop	cx
582
	mov	[edi + IPv4_Packet.Identification], cx
583
	pop	ecx
584
	mov	[edi + IPv4_Packet.SourceAddress], ecx
585
	pop	ecx
586
	mov	[edi + IPv4_Packet.DestinationAddress], ecx
587
 
1249 hidnplayr 588
	push	eax ebx edx
589
	; calculate checksum
590
	xor	edx, edx
591
	mov	esi, edi
592
	mov	ecx, IPv4_Packet.DataOrOptional
593
	call	checksum_1
594
	call	checksum_2
595
	mov	[edi + IPv4_Packet.HeaderChecksum], dx
596
	pop	edx ebx eax ecx
1159 hidnplayr 597
	add	edi, IPv4_Packet.DataOrOptional
598
 
1249 hidnplayr 599
	DEBUGF 1,"IPv4 Packet for device %x created successfully\n", ebx
1159 hidnplayr 600
 
601
	ret
602
 
1206 hidnplayr 603
 
604
  .not_found:
605
	DEBUGF 1,"Create IPv4 Packet - ARP entry not found!\n"
1249 hidnplayr 606
	; TODO: QUEUE the packet to resend later!
1159 hidnplayr 607
  .exit:
1206 hidnplayr 608
	add	esp, 16
1159 hidnplayr 609
  .exit_:
1165 hidnplayr 610
	DEBUGF 1,"Create IPv4 Packet - failed\n"
1159 hidnplayr 611
	or	edi, -1
612
	ret
613
 
614
 
615
 
1206 hidnplayr 616
 
1159 hidnplayr 617
;---------------------------------------------------------------------------
618
;
619
; IPv4_dest_to_dev
620
;
621
; IN: Destination IP in eax
622
; OUT: device id in edi
623
;
624
;---------------------------------------------------------------------------
625
 
626
align 4
627
IPv4_dest_to_dev:
628
 
629
	DEBUGF 1,"IPv4 destination to device: "
630
 
631
	xor	edi, edi
632
	mov	ecx, MAX_IP
633
 
634
  .loop:
635
	mov	ebx, [IP_LIST+edi]		; we dont need to worry about non exisiting ip interfaces
636
	and	ebx, [SUBNET_LIST+edi]		; they have IP and SUBNET set to all one's, so they will have no match except 255.255.255.255
637
						; (only a moron would insert that ip into this function..)
638
	mov	edx, eax
639
	and	edx, [SUBNET_LIST+edi]
640
 
641
	cmp	ebx, edx
642
	je	.found_it
643
 
644
	add	edi, 4
645
	loop	.loop
646
 
647
	xor	edi, edi	; if none found, use device 0 as default device
648
 
649
  .found_it:
650
	shr	edi, 2
651
 
652
	DEBUGF 1,"%u\n",edi
653
 
654
	ret
655
 
656
 
657
 
658
;---------------------------------------------------------------------------
659
;
660
; IPv4_get_frgmnt_num
661
;
662
; IN: /
663
; OUT: fragment number in ax
664
;
665
;---------------------------------------------------------------------------
666
 
667
align 4
668
IPv4_get_frgmnt_num:
669
	xor	ax, ax	;;; TODO: replace this with real code
670
 
671
	ret
672
 
673
 
674
;---------------------------------------------------------------------------
675
;
676
; IPv4_API
677
;
678
; This function is called by system function 75
679
;
680
; IN:  subfunction number in bl
681
;      device number in bh
682
;      ecx, edx, .. depends on subfunction
683
;
684
; OUT:
685
;
686
;---------------------------------------------------------------------------
687
 
688
align 4
689
IPv4_API:
690
 
691
	movzx	eax, bh
692
	shl	eax, 2
693
 
694
	test	bl, bl
695
	jz	.packets_tx	; 0
696
	dec	bl
697
	jz	.packets_rx	; 1
698
	dec	bl
699
	jz	.read_ip	; 2
700
	dec	bl
701
	jz	.write_ip	; 3
702
	dec	bl
703
	jz	.read_dns	; 4
704
	dec	bl
705
	jz	.write_dns	; 5
706
	dec	bl
707
	jz	.read_subnet	; 6
708
	dec	bl
709
	jz	.write_subnet	; 7
710
	dec	bl
711
	jz	.read_gateway	; 8
712
	dec	bl
713
	jz	.write_gateway	; 9
714
 
715
.error:
716
	mov	eax, -1
717
	ret
718
 
719
.packets_tx:
720
	add	eax, IP_PACKETS_TX
721
	mov	eax, [eax]
722
	ret
723
 
724
.packets_rx:
725
	add	eax, IP_PACKETS_RX
726
	mov	eax, [eax]
727
	ret
728
 
729
.read_ip:
730
	add	eax, IP_LIST
731
	mov	eax, [eax]
732
	ret
733
 
734
.write_ip:
735
	add	eax, IP_LIST
736
	mov	[eax], ecx
737
	xor	eax, eax
738
	ret
739
 
740
.read_dns:
741
	add	eax, DNS_LIST
742
	mov	eax, [eax]
743
	ret
744
 
745
.write_dns:
746
	add	eax, DNS_LIST
747
	mov	[eax], ecx
748
	xor	eax, eax
749
	ret
750
 
751
.read_subnet:
752
	add	eax, SUBNET_LIST
753
	mov	eax, [eax]
754
	ret
755
 
756
.write_subnet:
757
	add	eax, SUBNET_LIST
758
	mov	[eax], ecx
759
	xor	eax, eax
760
	ret
761
 
762
.read_gateway:
763
	add	eax, GATEWAY_LIST
764
	mov	eax, [eax]
765
	ret
766
 
767
.write_gateway:
768
	add	eax, GATEWAY_LIST
769
	mov	[eax], ecx
770
	xor	eax, eax
771
	ret
772