Subversion Repositories Kolibri OS

Rev

Rev 2301 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2301 Rev 2305
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: 2301 $
19
$Revision: 2305 $
20
 
20
 
21
ARP_NO_ENTRY		equ 0
21
ARP_NO_ENTRY		equ 0
22
ARP_VALID_MAPPING	equ 1
22
ARP_VALID_MAPPING	equ 1
Line 23... Line 23...
23
ARP_AWAITING_RESPONSE	equ 2
23
ARP_AWAITING_RESPONSE	equ 2
24
ARP_RESPONSE_TIMEOUT	equ 3
24
ARP_RESPONSE_TIMEOUT	equ 3
-
 
25
 
Line 25... Line 26...
25
 
26
ARP_REQUEST_TTL 	equ 31		; 20 s
26
ARP_REQUEST_TTL 	equ 31		; 20 s
27
ARP_ENTRY_TTL		equ 937 	; 600 s
Line 27... Line 28...
27
ARP_ENTRY_TTL		equ 937 	; 600 s
28
ARP_STATIC_ENTRY	equ -1
Line 28... Line 29...
28
 
29
 
-
 
30
ARP_REQ_OPCODE		equ 0x0100	; request
29
ARP_REQ_OPCODE		equ 0x0100	; request
31
ARP_REP_OPCODE		equ 0x0200	; reply
30
ARP_REP_OPCODE		equ 0x0200	; reply
32
 
31
 
33
ARP_TABLE_SIZE		equ 20		; Size of table
32
ARP_TABLE_SIZE		equ 20		; Size of table
34
 
33
 
-
 
34
struct ARP_ENTRY
-
 
Line 35... Line -...
35
       .IP		dd  ?
-
 
36
       .MAC		dp  ?
-
 
37
       .Status		dw  ?
-
 
38
       .TTL		dw  ?
-
 
39
       .size:
-
 
40
ends
-
 
41
 
-
 
42
struct ARP_Packet
-
 
43
       .HardwareType	dw  ?
-
 
44
       .ProtocolType	dw  ?
-
 
45
       .HardwareSize	db  ?
-
 
46
       .ProtocolSize	db  ?
35
struct ARP_entry
Line -... Line 36...
-
 
36
 
-
 
37
       IP		dd  ?
-
 
38
       MAC		dp  ?
-
 
39
       Status		dw  ?
-
 
40
       TTL		dw  ?
-
 
41
 
-
 
42
ends
-
 
43
 
-
 
44
struct ARP_header
-
 
45
 
-
 
46
       HardwareType	dw  ?
-
 
47
       ProtocolType	dw  ?
-
 
48
       HardwareSize	db  ?
Line 47... Line 49...
47
       .Opcode		dw  ?
49
       ProtocolSize	db  ?
48
       .SenderMAC	dp  ?
50
       Opcode		dw  ?
Line 49... Line 51...
49
       .SenderIP	dd  ?
51
       SenderMAC	dp  ?
Line 50... Line 52...
50
       .TargetMAC	dp  ?
52
       SenderIP 	dd  ?
Line 51... Line 53...
51
       .TargetIP	dd  ?
53
       TargetMAC	dp  ?
52
       .size:
54
       TargetIP 	dd  ?
Line 112... Line 114...
112
	test	ecx, ecx
114
	test	ecx, ecx
113
	jz	.exit
115
	jz	.exit
Line 114... Line 116...
114
 
116
 
115
	mov	esi, ARP_table
117
	mov	esi, ARP_table
116
  .loop:
118
  .loop:
117
	cmp	[esi + ARP_ENTRY.TTL], 0xffff	; 0xffff = static entry
119
	cmp	[esi + ARP_entry.TTL], ARP_STATIC_ENTRY
Line 118... Line 120...
118
	je	.next
120
	je	.next
119
 
121
 
Line 120... Line 122...
120
	dec	[esi + ARP_ENTRY.TTL]
122
	dec	[esi + ARP_entry.TTL]
121
	jz	.time_out
123
	jz	.time_out
122
 
124
 
123
  .next:
125
  .next:
124
	add	esi, ARP_ENTRY.size
126
	add	esi, sizeof.ARP_entry
Line 125... Line 127...
125
	dec	ecx
127
	dec	ecx
126
	jnz	.loop
128
	jnz	.loop
127
	jmp	.exit
129
	jmp	.exit
Line 128... Line 130...
128
 
130
 
129
  .time_out:
131
  .time_out:
130
	cmp	[esi + ARP_ENTRY.Status], ARP_AWAITING_RESPONSE
132
	cmp	[esi + ARP_entry.Status], ARP_AWAITING_RESPONSE
Line 131... Line 133...
131
	jz	.response_timeout
133
	je	.response_timeout
Line 132... Line 134...
132
 
134
 
133
	push	esi ecx
135
	push	esi ecx
134
	call	ARP_del_entry
136
	call	ARP_del_entry
Line 135... Line 137...
135
	pop	ecx esi
137
	pop	ecx esi
Line 136... Line 138...
136
 
138
 
Line 159... Line 161...
159
;-----------------------------------------------------------------
161
;-----------------------------------------------------------------
160
align 4
162
align 4
161
ARP_input:
163
ARP_input:
Line 162... Line 164...
162
 
164
 
163
	DEBUGF	1,"ARP_Handler - start\n"
165
	DEBUGF	1,"ARP_Handler - start\n"
164
	cmp	ecx, ARP_Packet.size
166
	cmp	ecx, sizeof.ARP_header
Line 165... Line 167...
165
	jb	.exit
167
	jb	.exit
166
 
168
 
Line 167... Line 169...
167
;---------------------
169
;---------------------
168
; Handle Reply packets
170
; Handle Reply packets
Line 169... Line 171...
169
 
171
 
170
	cmp	word [edx + ARP_Packet.Opcode], ARP_REP_OPCODE
172
	cmp	[edx + ARP_header.Opcode], ARP_REP_OPCODE
Line 171... Line 173...
171
	jne	.maybe_request
173
	jne	.maybe_request
172
 
174
 
173
	DEBUGF	1,"ARP_Handler - it's a reply packet from %u.%u.%u.%u\n",\
175
	DEBUGF	1,"ARP_Handler - it's a reply packet from %u.%u.%u.%u\n",\
Line 174... Line 176...
174
	[edx + ARP_Packet.SenderIP]:1,[edx + ARP_Packet.SenderIP+1]:1,[edx + ARP_Packet.SenderIP+2]:1,[edx + ARP_Packet.SenderIP+3]:1,
176
	[edx + ARP_header.SenderIP]:1, [edx + ARP_header.SenderIP+1]:1, [edx + ARP_header.SenderIP+2]:1, [edx + ARP_header.SenderIP+3]:1
175
 
177
 
Line 176... Line 178...
176
	mov	ecx, [NumARP]
178
	mov	ecx, [NumARP]
177
	test	ecx, ecx
179
	test	ecx, ecx
178
	jz	.exit
180
	jz	.exit
179
 
181
 
180
	mov	eax, [edx + ARP_Packet.SenderIP]
182
	mov	eax, [edx + ARP_header.SenderIP]
181
	mov	esi, ARP_table
183
	mov	esi, ARP_table
Line 182... Line 184...
182
 
184
 
Line 183... Line 185...
183
  .loop:
185
  .loop:
184
	cmp	[esi + ARP_ENTRY.IP], eax
186
	cmp	[esi + ARP_entry.IP], eax
Line 185... Line 187...
185
	je	.gotit
187
	je	.gotit
186
	add	esi, ARP_ENTRY.size
188
	add	esi, sizeof.ARP_entry
Line 187... Line 189...
187
	dec	ecx
189
	dec	ecx
Line 188... Line 190...
188
	jnz	.loop
190
	jnz	.loop
189
 
191
 
Line 190... Line 192...
190
	jmp	.exit
192
	jmp	.exit
191
 
193
 
192
  .gotit:
194
  .gotit:
193
	DEBUGF	1,"ARP_Handler - found matching entry\n"
195
	DEBUGF	1,"ARP_Handler - found matching entry\n"
Line 194... Line 196...
194
 
196
 
Line 195... Line 197...
195
	cmp	[esi+ARP_ENTRY.TTL], 0xffff		; if it is a static entry, dont touch it
197
	cmp	[esi + ARP_entry.TTL], ARP_STATIC_ENTRY 	; if it is a static entry, dont touch it
196
	je	.exit
198
	je	.exit
Line 197... Line 199...
197
 
199
 
198
	DEBUGF	1,"ARP_Handler - updating entry\n"
200
	DEBUGF	1,"ARP_Handler - updating entry\n"
199
 
201
 
Line 200... Line 202...
200
	mov	[esi+ARP_ENTRY.Status], ARP_VALID_MAPPING
202
	mov	[esi + ARP_entry.Status], ARP_VALID_MAPPING
201
	mov	[esi+ARP_ENTRY.TTL], ARP_ENTRY_TTL
203
	mov	[esi + ARP_entry.TTL], ARP_ENTRY_TTL
202
 
204
 
203
	mov	eax, dword [edx + ARP_Packet.SenderMAC]
205
	mov	eax, dword [edx + ARP_header.SenderMAC]
204
	mov	dword [esi+ARP_ENTRY.MAC], eax
206
	mov	dword [esi+ARP_entry.MAC], eax
Line 205... Line 207...
205
	mov	ax , word [edx + ARP_Packet.SenderMAC + 4]
207
	mov	ax , word [edx + ARP_header.SenderMAC + 4]
206
	mov	word [esi+ARP_ENTRY.MAC+4], ax
208
	mov	word [esi+ARP_entry.MAC+4], ax
207
 
209
 
Line 208... Line 210...
208
	jmp	.exit
210
	jmp	.exit
209
 
211
 
Line 210... Line 212...
210
 
212
 
211
;-----------------------
213
;-----------------------
Line 212... Line 214...
212
; Handle Request packets
214
; Handle Request packets
213
 
215
 
214
  .maybe_request:
216
  .maybe_request:
215
	cmp	word [edx + ARP_Packet.Opcode], ARP_REQ_OPCODE
217
	cmp	[edx + ARP_header.Opcode], ARP_REQ_OPCODE
216
	jne	.exit
218
	jne	.exit
Line 217... Line 219...
217
 
219
 
218
	call	NET_ptr_to_num
220
	call	NET_ptr_to_num
219
	cmp	edi, -1
221
	cmp	edi, -1
220
	jz	.exit
222
	jz	.exit
221
	DEBUGF	1,"ARP Request packet through device: %u\n", edi
223
	DEBUGF	1,"ARP Request packet through device: %u\n", edi
222
	inc	[ARP_PACKETS_RX+4*edi]
224
	inc	[ARP_PACKETS_RX+4*edi]
223
 
225
 
224
	mov	eax, [IP_LIST+4*edi]
226
	mov	eax, [IP_LIST+4*edi]
Line 225... Line 227...
225
	cmp	eax, [edx + ARP_Packet.TargetIP]		; Is it looking for my IP address?
227
	cmp	eax, [edx + ARP_header.TargetIP]		; Is it looking for my IP address?
Line 226... Line 228...
226
	jne	.exit						; TODO: instead of quitting, update local entrys with matching IP's ?
228
	jne	.exit						; TODO: instead of quitting, update local entrys with matching IP's ?
Line 227... Line 229...
227
 
229
 
228
	push	eax
230
	push	eax
229
	push	edi
231
	push	edi
230
 
232
 
231
; OK, it is a request for one of our MAC addresses.
233
; OK, it is a request for one of our MAC addresses.
232
; Build the frame and send it. We can reuse the buffer.  (faster then using ARP_create_packet)
234
; Build the frame and send it. We can reuse the buffer.  (faster then using ARP_create_packet)
233
 
235
 
234
	lea	esi, [edx + ARP_Packet.SenderMAC]
236
	lea	esi, [edx + ARP_header.SenderMAC]
235
	lea	edi, [edx + ARP_Packet.TargetMAC]
237
	lea	edi, [edx + ARP_header.TargetMAC]
Line 292... Line 294...
292
 
294
 
Line 293... Line 295...
293
	mov	ebx, [NET_DRV_LIST+edi] 	; device ptr
295
	mov	ebx, [NET_DRV_LIST+edi] 	; device ptr
294
 
296
 
295
	lea	eax, [ebx + ETH_DEVICE.mac]	; local device mac
297
	lea	eax, [ebx + ETH_DEVICE.mac]	; local device mac
296
	mov	edx, ETH_BROADCAST		; broadcast mac
298
	mov	edx, ETH_BROADCAST		; broadcast mac
297
	mov	ecx, ARP_Packet.size
299
	mov	ecx, sizeof.ARP_header
298
	mov	di, ETHER_ARP
300
	mov	di, ETHER_ARP
Line 299... Line 301...
299
	call	ETH_output
301
	call	ETH_output
Line 300... Line 302...
300
	jz	.exit
302
	jz	.exit
301
 
303
 
302
	mov	ecx, eax
304
	mov	ecx, eax
303
 
305
 
304
	mov	[edi + ARP_Packet.HardwareType], 0x0100 	; Ethernet
306
	mov	[edi + ARP_header.HardwareType], 0x0100 	; Ethernet
Line 305... Line 307...
305
	mov	[edi + ARP_Packet.ProtocolType], 0x0008 	; IP
307
	mov	[edi + ARP_header.ProtocolType], 0x0008 	; IP
Line 306... Line 308...
306
	mov	[edi + ARP_Packet.HardwareSize], 6		; MAC-addr length
308
	mov	[edi + ARP_header.HardwareSize], 6		; MAC-addr length
307
	mov	[edi + ARP_Packet.ProtocolSize], 4		; IP-addr length
309
	mov	[edi + ARP_header.ProtocolSize], 4		; IP-addr length
308
	mov	[edi + ARP_Packet.Opcode], ARP_REQ_OPCODE	; Request
310
	mov	[edi + ARP_header.Opcode], ARP_REQ_OPCODE	; Request
309
 
311
 
Line 351... Line 353...
351
	test	ecx, ecx		; first entry?
353
	test	ecx, ecx		; first entry?
352
	jz	.add
354
	jz	.add
353
	cmp	ecx, ARP_TABLE_SIZE	; list full ?
355
	cmp	ecx, ARP_TABLE_SIZE	; list full ?
354
	jae	.error
356
	jae	.error
Line 355... Line 357...
355
 
357
 
356
	mov	eax, dword[esi + ARP_ENTRY.MAC]
358
	mov	eax, dword [esi + ARP_entry.MAC]
357
	mov	bx , word[esi + ARP_ENTRY.MAC + 4]
359
	mov	bx , word [esi + ARP_entry.MAC + 4]
Line 358... Line 360...
358
	mov	edi, ARP_table
360
	mov	edi, ARP_table
359
 
361
 
360
  .loop:
362
  .loop:
361
	cmp	dword [edi + ARP_ENTRY.MAC], eax	; Check for duplicate MAC's
363
	cmp	dword [edi + ARP_entry.MAC], eax	; Check for duplicate MAC's
362
	jne	.maybe_next				;
364
	jne	.maybe_next				;
Line 363... Line 365...
363
	cmp	word [edi + ARP_ENTRY.MAC + 4], bx	;
365
	cmp	word [edi + ARP_entry.MAC + 4], bx	;
364
	jne	.maybe_next				;
366
	jne	.maybe_next				;
365
 
367
 
366
	cmp	dword[edi + ARP_ENTRY.TTL], 0xFFFF	; static entry
368
	cmp	[edi + ARP_entry.TTL], ARP_STATIC_ENTRY
367
	jne	.notstatic
369
	jne	.notstatic
Line 368... Line 370...
368
	cmp	dword[esi + ARP_ENTRY.TTL], 0xFFFF
370
	cmp	[esi + ARP_entry.TTL], ARP_STATIC_ENTRY
369
	jne	.error
371
	jne	.error
370
  .notstatic:
372
  .notstatic:
Line 371... Line 373...
371
 
373
 
372
	neg	ecx
374
	neg	ecx
373
	add	ecx, [NumARP]
375
	add	ecx, [NumARP]
Line 374... Line 376...
374
	jmp	.add
376
	jmp	.add
375
 
377
 
376
  .maybe_next:
378
  .maybe_next:
377
	add	esi, ARP_ENTRY.size
379
	add	esi, sizeof.ARP_entry
378
	loop	.loop
380
	loop	.loop
379
 
381
 
380
	mov	ecx, [NumARP]
382
	mov	ecx, [NumARP]
Line 381... Line 383...
381
  .add:
383
  .add:
382
	push	ecx
384
	push	ecx
383
	imul	ecx, ARP_ENTRY.size
385
	imul	ecx, sizeof.ARP_entry
384
	lea	edi, [ecx + ARP_table]
386
	lea	edi, [ecx + ARP_table]
Line 385... Line 387...
385
	mov	ecx, ARP_ENTRY.size/2
387
	mov	ecx, sizeof.ARP_entry/2
Line 411... Line 413...
411
align 4
413
align 4
412
ARP_del_entry:
414
ARP_del_entry:
Line 413... Line 415...
413
 
415
 
Line 414... Line 416...
414
	DEBUGF 1,"ARP del entry %x, total entrys: %u\n", esi, [NumARP]
416
	DEBUGF 1,"ARP del entry %x, total entrys: %u\n", esi, [NumARP]
415
 
417
 
416
	mov	ecx, ARP_table + (ARP_TABLE_SIZE - 1) * ARP_ENTRY.size
418
	mov	ecx, ARP_table + (ARP_TABLE_SIZE - 1) * sizeof.ARP_entry
Line 417... Line 419...
417
	sub	ecx, esi
419
	sub	ecx, esi
418
	shr	ecx, 1
420
	shr	ecx, 1
419
 
421
 
Line 420... Line 422...
420
	mov	edi, esi
422
	mov	edi, esi
421
	lea	esi, [edi + ARP_ENTRY.size]
423
	lea	esi, [edi + sizeof.ARP_entry]
Line 472... Line 474...
472
 
474
 
473
  .local:
475
  .local:
474
	mov	ecx, [NumARP]
476
	mov	ecx, [NumARP]
475
	test	ecx, ecx
477
	test	ecx, ecx
476
	jz	.not_in_list
478
	jz	.not_in_list
477
	mov	esi, ARP_table + ARP_ENTRY.IP
479
	mov	esi, ARP_table + ARP_entry.IP
478
  .scan_loop:
480
  .scan_loop:
479
	cmp	[esi], eax
481
	cmp	[esi], eax
480
	je	.found_it
482
	je	.found_it
481
	add	esi, ARP_ENTRY.size
483
	add	esi, sizeof.ARP_entry
Line 482... Line 484...
482
	loop	.scan_loop
484
	loop	.scan_loop
483
 
485
 
Line 494... Line 496...
494
	pushd	0
496
	pushd	0
495
	pushw	0
497
	pushw	0
496
	pushd	eax
498
	pushd	eax
497
	mov	esi, esp
499
	mov	esi, esp
498
	call	ARP_add_entry
500
	call	ARP_add_entry
499
	add	esp, ARP_ENTRY.size
501
	add	esp, sizeof.ARP_entry
Line 500... Line 502...
500
 
502
 
501
	cmp	eax, -1
503
	cmp	eax, -1
Line 502... Line 504...
502
	je	.full
504
	je	.full
Line 507... Line 509...
507
	call	ARP_output_request
509
	call	ARP_output_request
Line 508... Line 510...
508
 
510
 
Line 509... Line 511...
509
;; TODO: check if driver could transmit packet
511
;; TODO: check if driver could transmit packet
510
 
512
 
511
	pop	esi
513
	pop	esi
Line 512... Line 514...
512
	imul	esi, ARP_ENTRY.size
514
	imul	esi, sizeof.ARP_entry
513
	add	esi, ARP_table
515
	add	esi, ARP_table
514
 
516
 
515
	mov	ecx, 25
517
	mov	ecx, 25
516
  .wait_loop:
518
  .wait_loop:
517
	cmp	[esi + ARP_ENTRY.Status], 1
519
	cmp	[esi + ARP_entry.Status], 1
518
	je	.got_it
520
	je	.got_it
519
	push	esi
521
	push	esi
Line 525... Line 527...
525
	mov	eax, -2 	; request send
527
	mov	eax, -2 	; request send
526
	ret
528
	ret
Line 527... Line 529...
527
 
529
 
528
  .found_it:
530
  .found_it:
529
	DEBUGF	1,"found IP in ARPTable\n"
531
	DEBUGF	1,"found IP in ARPTable\n"
530
	cmp	[esi + ARP_ENTRY.Status], 1
532
	cmp	[esi + ARP_entry.Status], 1
Line 531... Line 533...
531
	jne	.invalid
533
	jne	.invalid
532
 
534
 
533
  .got_it:
535
  .got_it:
534
	movzx	eax, word [esi+ARP_ENTRY.MAC]
536
	movzx	eax, word [esi + ARP_entry.MAC]
Line 535... Line 537...
535
	mov	ebx, dword[esi+ARP_ENTRY.MAC+2]
537
	mov	ebx, dword[esi + ARP_entry.MAC+2]
536
	ret
538
	ret
537
 
539
 
Line 605... Line 607...
605
.read:
607
.read:
606
	cmp	ecx, [NumARP]
608
	cmp	ecx, [NumARP]
607
	jae	.error
609
	jae	.error
608
	; edi = pointer to buffer
610
	; edi = pointer to buffer
609
	; ecx = # entry
611
	; ecx = # entry
610
	imul	ecx, ARP_ENTRY.size
612
	imul	ecx, sizeof.ARP_entry
611
	add	ecx, ARP_table
613
	add	ecx, ARP_table
612
	mov	esi, ecx
614
	mov	esi, ecx
613
	mov	ecx, ARP_ENTRY.size/2
615
	mov	ecx, sizeof.ARP_entry/2
614
	rep	movsw
616
	rep	movsw
Line 615... Line 617...
615
 
617
 
616
	xor	eax, eax
618
	xor	eax, eax
Line 623... Line 625...
623
 
625
 
624
.remove:
626
.remove:
625
	; ecx = # entry
627
	; ecx = # entry
626
	cmp	ecx, [NumARP]
628
	cmp	ecx, [NumARP]
627
	jae	.error
629
	jae	.error
628
	imul	ecx, ARP_ENTRY.size
630
	imul	ecx, sizeof.ARP_entry
629
	lea	esi, [ARP_table + ecx]
631
	lea	esi, [ARP_table + ecx]
630
	call	ARP_del_entry
632
	call	ARP_del_entry