Subversion Repositories Kolibri OS

Rev

Rev 1208 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1208 Rev 1249
Line 1... Line 1...
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2004-2009. All rights reserved.    ;;
3
;; Copyright (C) KolibriOS team 2004-2009. 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
;;  IP.INC                                                         ;;
6
;;  IPv4.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
;;                                                                 ;;
Line 14... Line 14...
14
;;          GNU GENERAL PUBLIC LICENSE                             ;;
14
;;          GNU GENERAL PUBLIC LICENSE                             ;;
15
;;             Version 2, June 1991                                ;;
15
;;             Version 2, June 1991                                ;;
16
;;                                                                 ;;
16
;;                                                                 ;;
17
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 18... Line 18...
18
 
18
 
Line 19... Line 19...
19
$Revision: 1208 $
19
$Revision: 1249 $
Line 20... Line 20...
20
 
20
 
Line 50... Line 50...
50
 
50
 
51
struct	FRAGMENT_entry		       ; This structure will replace the ethernet header in fragmented ip packets
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)
52
	.PrevPtr		dd  ?  ; Pointer to previous fragment entry  (-1 for first packet)
53
	.NextPtr		dd  ?  ; Pointer to next fragment entry (-1 for last packet)
53
	.NextPtr		dd  ?  ; Pointer to next fragment entry (-1 for last packet)
54
	.Owner			dd  ?  ; Pointer to structure of driver
54
	.Owner			dd  ?  ; Pointer to structure of driver
55
				rb  2  ; to match ethernet header size
55
				rb  2  ; to match ethernet header size          ; TODO: fix this hack
56
	.Data:			       ; Ip header begins here (we will need the IP header to re-construct the complete packet)
56
	.Data:			       ; Ip header begins here (we will need the IP header to re-construct the complete packet)
Line 57... Line 57...
57
ends
57
ends
58
 
58
 
Line 97... Line 97...
97
 
97
 
98
 
98
 
99
 
99
 
100
;-----------------------------------------------------------------
100
;-----------------------------------------------------------------
101
;
-
 
102
; IP_Handler:
101
;
103
;
102
; IPv4_Handler:
104
;  Called by eth_handler,
103
;
105
;  will check if IP Packet isnt damaged
104
;  Will check if IP Packet isnt damaged
106
;  and call appropriate handler. (TCP/UDP/ICMP/..)
105
;  and call appropriate handler. (TCP/UDP/ICMP/..)
107
;
106
;
Line 117... Line 116...
117
 
116
 
118
align 4
117
align 4
Line 119... Line 118...
119
IPv4_handler:
118
IPv4_handler:
120
 
-
 
121
	DEBUGF	1,"IP_Handler - start\n"
-
 
Line 122... Line 119...
122
	mov	cx , [edx + IPv4_Packet.HeaderChecksum]
119
 
Line -... Line 120...
-
 
120
	DEBUGF	1,"IP_Handler - start\n"
-
 
121
 
-
 
122
	push	edx ebx
-
 
123
 
-
 
124
	; save checksum, and clear it in original packet
-
 
125
	mov	di , [edx + IPv4_Packet.HeaderChecksum]
123
	xchg	ch , cl 					; Get the checksum in intel format
126
	DEBUGF	1,"checksum: %x\n",di
124
  
127
	mov	word [edx + IPv4_Packet.HeaderChecksum], 0
125
	mov	word [edx + IPv4_Packet.HeaderChecksum], 0	; Clear checksum field to recalculating checksum
128
 
-
 
129
	; Re-calculate checksum
-
 
130
	movzx	ecx, byte [edx + IPv4_Packet.VersionAndIHL]	; Calculate Header length by using IHL field
-
 
131
	and	ecx, 0x0000000F  ;
-
 
132
	shl	cx , 2		 ;
126
 
133
	mov	esi, edx
127
	movzx	eax, byte [edx + IPv4_Packet.VersionAndIHL]	; Calculate Header length by using IHL field
-
 
128
	and	eax, 0x0000000F  ;
134
	xor	edx, edx
129
	shl	eax, 2		 ;
135
	call	checksum_1
130
 
136
	call	checksum_2
131
	push	edx
137
 
Line 132... Line 138...
132
	stdcall checksum_jb, edx, eax				; buf_ptr, buf_size
138
	; now compare the two..
133
	pop	edx
139
	cmp	dx, di
134
	cmp	cx , ax
140
	pop	ebx edx
135
	jnz	.dump						; if CHECKSUM isn't valid then dump Packet
141
	jne	.dump						; if checksum isn't valid then dump packet
Line 181... Line 187...
181
	sub	ecx, eax					;
187
	sub	ecx, eax					;
Line 182... Line 188...
182
 
188
 
183
	add	eax, edx
189
	add	eax, edx
184
	push	eax
190
	push	eax
-
 
191
	mov	al , [edx + IPv4_Packet.Protocol]
-
 
192
;----------------------- experimental
-
 
193
	mov	esi, [edx + IPv4_Packet.SourceAddress]
-
 
194
	mov	edi, [edx + IPv4_Packet.DestinationAddress]
185
	mov	al , [edx + IPv4_Packet.Protocol]
195
;-----------------------
Line 186... Line 196...
186
	pop	edx						; Offset to data (tcp/udp/icmp/.. Packet)
196
	pop	edx						; Offset to data (tcp/udp/icmp/.. Packet)
187
 
197
 
Line 188... Line 198...
188
	cmp	al , IP_PROTO_TCP
198
	cmp	al , IP_PROTO_TCP
189
;        je      TCP_handler
199
	je	TCP_handler
Line 190... Line 200...
190
 
200
 
Line 383... Line 393...
383
;        loop    @r                 ;
393
;        loop    @r                 ;
Line 384... Line 394...
384
 
394
 
385
	movzx	eax, byte [edx + IPv4_Packet.VersionAndIHL]	; Calculate Header length by using IHL field
395
	movzx	eax, byte [edx + IPv4_Packet.VersionAndIHL]	; Calculate Header length by using IHL field
386
	and	ax, 0x000F					;
396
	and	ax, 0x000F					;
-
 
397
	shl	ax, 2						;
387
	shl	ax, 2						;
398
 
-
 
399
	sub	ecx, eax
-
 
400
 
388
	sub	ecx, eax					;
401
 
389
	add	eax, edx
402
	add	eax, edx
390
	push	eax
403
	push	eax
-
 
404
	mov	al , [edx + IPv4_Packet.Protocol]
-
 
405
;----------------------- experimental
-
 
406
	mov	esi, [edx + IPv4_Packet.SourceAddress]
-
 
407
	mov	edi, [edx + IPv4_Packet.DestinationAddress]
391
	mov	al , [edx + IPv4_Packet.Protocol]
408
;-----------------------
Line 392... Line 409...
392
	pop	edx						; Offset to data (tcp/udp/icmp/.. Packet)
409
	pop	edx						; Offset to data (tcp/udp/icmp/.. Packet)
393
 
410
 
Line 394... Line 411...
394
	cmp	al , IP_PROTO_TCP
411
	cmp	al , IP_PROTO_TCP
395
;        je      TCP_handler
412
	je	TCP_handler
Line 396... Line 413...
396
 
413
 
Line 491... Line 508...
491
;     ebx = source ip
508
;     ebx = source ip
492
;     ecx = data length
509
;     ecx = data length
493
;     dx  = fragment id
510
;     dx  = fragment id
494
;     di  = protocol
511
;     di  = protocol
495
;
512
;
496
; OUT: eax points to buffer start
513
; OUT: eax = pointer to buffer start
497
;      ebx is size of complete buffer
-
 
498
;      edi = pointer to start of data (-1 on error)
514
;      ebx = pointer to device struct (needed for sending procedure)
499
;      ecx = unchanged (packet size of embedded data)
515
;      ecx = unchanged (packet size of embedded data)
500
;      edx = pointer to device struct (needed for sending procedure)
516
;      edx = size of complete buffer
501
;      esi = pointer to sending procedure
517
;      esi = pointer to sending procedure
-
 
518
;      edi = pointer to start of data (-1 on error)
502
;
519
;
503
;-----------------------------------------------------------------
520
;-----------------------------------------------------------------
Line 504... Line 521...
504
 
521
 
Line 548... Line 565...
548
	lea	eax, [edx + ETH_DEVICE.mac]
565
	lea	eax, [edx + ETH_DEVICE.mac]
549
	mov	ebx, esp
566
	mov	ebx, esp
550
	mov	ecx, [esp+18]	 ;; 18 or  22 ??
567
	mov	ecx, [esp+18]	 ;; 18 or  22 ??
551
	add	ecx, IPv4_Packet.DataOrOptional
568
	add	ecx, IPv4_Packet.DataOrOptional
552
	mov	di , ETHER_IPv4
569
	mov	di , ETHER_IPv4
553
	call	ETH_create_Packet		   ; TODO: figure out a way to make this work with other protocols too
570
	call	ETH_create_packet		   ; TODO: figure out a way to make this work with other protocols too
554
	add	esp, 6
571
	add	esp, 6
555
	cmp	edi, -1
572
	cmp	edi, -1
556
	je	.exit
573
	je	.exit
Line 557... Line 574...
557
 
574
 
Line 569... Line 586...
569
	pop	ecx
586
	pop	ecx
570
	mov	[edi + IPv4_Packet.SourceAddress], ecx
587
	mov	[edi + IPv4_Packet.SourceAddress], ecx
571
	pop	ecx
588
	pop	ecx
572
	mov	[edi + IPv4_Packet.DestinationAddress], ecx
589
	mov	[edi + IPv4_Packet.DestinationAddress], ecx
Line 573... Line 590...
573
 
590
 
-
 
591
	push	eax ebx edx
-
 
592
	; calculate checksum
-
 
593
	xor	edx, edx
574
	push	eax
594
	mov	esi, edi
-
 
595
	mov	ecx, IPv4_Packet.DataOrOptional
575
	stdcall checksum_jb, edi, IPv4_Packet.DataOrOptional ; buf_ptr, buf_size
596
	call	checksum_1
576
	xchg	al, ah
597
	call	checksum_2
577
	mov	[edi + IPv4_Packet.HeaderChecksum], ax
598
	mov	[edi + IPv4_Packet.HeaderChecksum], dx
578
	pop	eax ecx
599
	pop	edx ebx eax ecx
Line 579... Line 600...
579
	add	edi, IPv4_Packet.DataOrOptional
600
	add	edi, IPv4_Packet.DataOrOptional
Line 580... Line 601...
580
 
601
 
Line 581... Line 602...
581
	DEBUGF 1,"IPv4 Packet for device %x created successfully\n", edx
602
	DEBUGF 1,"IPv4 Packet for device %x created successfully\n", ebx
582
 
603
 
583
	ret
604
	ret
584
 
605
 
585
 
606
 
586
  .not_found:
607
  .not_found:
587
	DEBUGF 1,"Create IPv4 Packet - ARP entry not found!\n"
608
	DEBUGF 1,"Create IPv4 Packet - ARP entry not found!\n"
588
	; TODO: QUEUE!
609
	; TODO: QUEUE the packet to resend later!