2009-10-05 22:47:27 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; Copyright (C) KolibriOS team 2004-2009. All rights reserved. ;;
|
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; ARP.INC ;;
|
|
|
|
;; ;;
|
|
|
|
;; Part of the tcp/ip network stack for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Based on the work of [Johnny_B] and [smb] ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
$Revision$
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
ARP_NO_ENTRY equ 0
|
|
|
|
ARP_VALID_MAPPING equ 1
|
|
|
|
ARP_AWAITING_RESPONSE equ 2
|
|
|
|
ARP_RESPONSE_TIMEOUT equ 3
|
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
ARP_REQUEST_TTL = 20 ; in seconds
|
2009-10-12 16:39:59 +02:00
|
|
|
ARP_ENTRY_TTL = 600 ; in seconds
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
ETHER_ARP equ 0x0608
|
|
|
|
|
|
|
|
ARP_REQ_OPCODE equ 0x0100 ; request
|
|
|
|
ARP_REP_OPCODE equ 0x0200 ; reply
|
|
|
|
|
|
|
|
ARP_TABLE_SIZE equ 20 ; Size of table
|
|
|
|
|
|
|
|
struct ARP_ENTRY
|
|
|
|
.IP dd ?
|
|
|
|
.MAC dp ?
|
|
|
|
.Status dw ?
|
|
|
|
.TTL dw ? ; in seconds
|
|
|
|
.size:
|
|
|
|
ends
|
|
|
|
|
|
|
|
struct ARP_Packet
|
|
|
|
.HardwareType dw ?
|
|
|
|
.ProtocolType dw ?
|
|
|
|
.HardwareSize db ?
|
|
|
|
.ProtocolSize db ?
|
|
|
|
.Opcode dw ?
|
|
|
|
.SenderMAC dp ?
|
|
|
|
.SenderIP dd ?
|
|
|
|
.TargetMAC dp ?
|
|
|
|
.TargetIP dd ?
|
|
|
|
ends
|
|
|
|
|
|
|
|
|
|
|
|
; The TTL field is decremented every second, and is deleted when it
|
|
|
|
; reaches 0. It is refreshed every time a packet is received
|
|
|
|
; If the TTL field is 0xFFFF it is a static entry and is never deleted
|
|
|
|
; The status field can be the following values:
|
|
|
|
; 0x0000 entry not used
|
|
|
|
; 0x0001 entry holds a valid mapping
|
|
|
|
; 0x0002 entry contains an IP address, awaiting ARP response
|
|
|
|
; 0x0003 No response received to ARP request.
|
|
|
|
; The last status value is provided to allow the network layer to delete
|
|
|
|
; a packet that is queued awaiting an ARP response
|
|
|
|
|
|
|
|
align 4
|
|
|
|
uglobal
|
|
|
|
|
|
|
|
NumARP dd ?
|
2009-11-09 17:52:58 +01:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
ARPTable rb ARP_ENTRY.size * ARP_TABLE_SIZE
|
|
|
|
|
|
|
|
ARP_PACKETS_TX rd MAX_NET_DEVICES
|
|
|
|
ARP_PACKETS_RX rd MAX_NET_DEVICES
|
|
|
|
|
|
|
|
|
|
|
|
endg
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; ARP_init
|
|
|
|
;
|
|
|
|
; This function resets all ARP variables
|
|
|
|
;
|
|
|
|
; IN: /
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
2009-09-17 13:55:38 +02:00
|
|
|
ARP_init:
|
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
|
|
|
|
mov [NumARP], eax
|
|
|
|
|
|
|
|
mov edi, ARP_PACKETS_TX
|
|
|
|
mov ecx, 2*MAX_NET_DEVICES
|
|
|
|
rep stosd
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-10-05 22:47:27 +02:00
|
|
|
; ARP_IP_to_MAC
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-10-05 22:47:27 +02:00
|
|
|
; This function resets all ARP variables
|
|
|
|
;
|
|
|
|
; IN: eax = IPv4 address
|
|
|
|
; OUT: eax = -1 on error, else eax = first two bytes of mac
|
|
|
|
; ( high 16 bits are zero)
|
|
|
|
; ebx = last four bytes of mac ; TODO: special eax value for 'request send'
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
align 4
|
2009-10-05 22:47:27 +02:00
|
|
|
ARP_IP_to_MAC:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
DEBUGF 1,"ARP_IP_to_MAC\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
; first, check destination IP to see if it is on 'this' network.
|
|
|
|
; The test is:
|
|
|
|
; if ( destIP & subnet_mask == stack_ip & subnet_mask )
|
|
|
|
; destination is local
|
|
|
|
; else
|
|
|
|
; destination is remote, so pass to gateway
|
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
xor edx, edx ; TODO: find device num in edx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
mov ebx, [IP_LIST+edx]
|
|
|
|
and ebx, [SUBNET_LIST+edx]
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
mov ecx, eax
|
|
|
|
and ecx, [SUBNET_LIST+edx]
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
cmp ecx, ebx
|
|
|
|
je .local
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
mov eax, [GATEWAY_LIST+edx]
|
|
|
|
DEBUGF 1,"requested IP is not on subnet, using gateway\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
.local:
|
|
|
|
; try to find it on the list
|
|
|
|
mov ecx, [NumARP]
|
2009-10-12 16:39:59 +02:00
|
|
|
test ecx, ecx
|
2009-10-05 22:47:27 +02:00
|
|
|
jz .not_in_list
|
|
|
|
mov esi, ARPTable + ARP_ENTRY.IP
|
|
|
|
.scan_loop:
|
2009-10-12 16:39:59 +02:00
|
|
|
cmp [esi], eax
|
|
|
|
je .found_it
|
|
|
|
add esi, ARP_ENTRY.size
|
2009-10-05 22:47:27 +02:00
|
|
|
loop .scan_loop
|
|
|
|
.not_in_list:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
DEBUGF 1,"IP not found on list, preparing for ARP request\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
; if not, reserve an entry in list and send an ARP request packet
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
push eax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-09 17:52:58 +01:00
|
|
|
pushw ARP_REQUEST_TTL
|
|
|
|
pushw ARP_AWAITING_RESPONSE
|
|
|
|
pushd 0
|
|
|
|
pushw 0
|
|
|
|
pushd eax
|
2009-10-05 22:47:27 +02:00
|
|
|
call ARP_add_entry
|
|
|
|
cmp eax, -1
|
|
|
|
je .full
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-09 17:52:58 +01:00
|
|
|
; <Some dirty test code>
|
|
|
|
|
|
|
|
; This piece of code waits for an ARP reply
|
|
|
|
|
|
|
|
mov ebx, eax
|
2009-10-05 22:47:27 +02:00
|
|
|
pop eax
|
2009-11-09 17:52:58 +01:00
|
|
|
push ebx
|
2009-10-05 22:47:27 +02:00
|
|
|
call ARP_create_request
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-09 17:52:58 +01:00
|
|
|
push [timer_ticks]
|
|
|
|
add dword[esp], 100*ARP_REQUEST_TTL
|
|
|
|
DEBUGF 1,"Waiting for ARP reply, time: %x, entry:%u\n",[timer_ticks], [esp + 4]
|
|
|
|
.dirty_loop:
|
|
|
|
|
|
|
|
call change_task ; The ARP reply hasnt been received yet, tell the processor to do some other stuff first
|
|
|
|
|
|
|
|
mov eax, [esp + 4]
|
|
|
|
imul eax, ARP_ENTRY.size
|
|
|
|
add eax, ARPTable
|
|
|
|
cmp [eax + ARP_ENTRY.Status], ARP_VALID_MAPPING
|
|
|
|
je .gogogo
|
|
|
|
|
|
|
|
mov eax, [esp] ; Check if the reply hasnt timed-out yet
|
|
|
|
cmp [timer_ticks], eax
|
|
|
|
jl .dirty_loop
|
|
|
|
|
|
|
|
; </Some dirty test code>
|
|
|
|
or eax, -1
|
|
|
|
add esp, 8
|
2009-10-05 22:47:27 +02:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
.found_it:
|
2009-11-09 17:52:58 +01:00
|
|
|
DEBUGF 1,"found MAC in ARPTable\n"
|
|
|
|
movzx eax, word [esi+ARP_ENTRY.MAC]
|
|
|
|
mov ebx, dword[esi+ARP_ENTRY.MAC+2]
|
2009-10-05 22:47:27 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.full:
|
|
|
|
add esp, 4
|
|
|
|
mov eax, -1
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-09 17:52:58 +01:00
|
|
|
.gogogo:
|
|
|
|
DEBUGF 1,"got ARP reply, time: %x\n",[timer_ticks]
|
|
|
|
mov ebx, dword[eax+ARP_ENTRY.MAC+2]
|
|
|
|
movzx eax, word [eax+ARP_ENTRY.MAC]
|
|
|
|
add esp, 8
|
|
|
|
ret
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
;---------------------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-11-09 17:52:58 +01:00
|
|
|
; ARP_create_request
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-10-05 22:47:27 +02:00
|
|
|
; IN: ip in eax
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-10-05 22:47:27 +02:00
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
ARP_create_request:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
DEBUGF 1,"Create ARP Packet\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
call IPv4_dest_to_dev
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
push eax ; DestIP
|
|
|
|
mov eax, [IP_LIST+4*edi] ; senderIP
|
|
|
|
push eax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
mov edi, [ETH_DRV_LIST + 4*edi]
|
|
|
|
lea eax, [edi + ETH_DEVICE.mac]
|
|
|
|
mov ebx, ETH_BROADCAST
|
|
|
|
mov ecx, 60 ; minimum packet size
|
|
|
|
mov edx, edi ;;;
|
|
|
|
mov di , ETHER_ARP
|
2009-11-06 21:45:08 +01:00
|
|
|
call ETH_create_packet
|
2009-10-05 22:47:27 +02:00
|
|
|
cmp edi, -1
|
|
|
|
je .exit
|
2009-09-22 18:42:54 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
mov ecx, eax
|
|
|
|
|
|
|
|
mov [edi + ARP_Packet.HardwareType], 0x0100 ;Ethernet
|
|
|
|
mov [edi + ARP_Packet.ProtocolType], 0x0008 ;IP
|
|
|
|
mov [edi + ARP_Packet.HardwareSize], 6 ;MAC-addr length
|
|
|
|
mov [edi + ARP_Packet.ProtocolSize], 4 ;IP-addr length
|
|
|
|
mov [edi + ARP_Packet.Opcode], ARP_REQ_OPCODE ;Request
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
add edi, ARP_Packet.SenderMAC ; sendermac
|
2009-10-12 16:39:59 +02:00
|
|
|
lea esi, [ebx + ETH_DEVICE.mac] ;
|
2009-10-05 22:47:27 +02:00
|
|
|
movsw ;
|
|
|
|
movsd ;
|
2009-10-12 16:39:59 +02:00
|
|
|
pop eax ;
|
2009-10-05 22:47:27 +02:00
|
|
|
stosd ;
|
2009-10-12 16:39:59 +02:00
|
|
|
mov eax, -1 ; destmac
|
|
|
|
stosd ;
|
|
|
|
stosw ;
|
2009-10-05 22:47:27 +02:00
|
|
|
pop eax
|
2009-10-12 16:39:59 +02:00
|
|
|
stosd ;
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
DEBUGF 1,"ARP Packet for device %x created successfully\n", ebx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
push edx ecx
|
2009-11-06 21:45:08 +01:00
|
|
|
jmp ETH_sender
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
.exit:
|
|
|
|
add esp, 8
|
|
|
|
DEBUGF 1,"Create ARP Packet - failed\n"
|
|
|
|
mov eax, -1
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-28 21:48:32 +02:00
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; ARP_decrease_entry_ttls
|
|
|
|
;
|
|
|
|
; IN: /
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
ARP_decrease_entry_ttls:
|
|
|
|
|
|
|
|
mov ecx, [NumARP]
|
|
|
|
test ecx, ecx
|
|
|
|
jz .exit
|
|
|
|
|
|
|
|
mov ebx, ARPTable
|
|
|
|
|
|
|
|
.timer_loop:
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
cmp [ebx + ARP_ENTRY.TTL], 0xFFFF
|
2009-09-28 21:48:32 +02:00
|
|
|
je .timer_loop_end ;if TTL==0xFFFF then it's static entry
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
cmp [ebx + ARP_ENTRY.TTL], 0
|
2009-09-28 21:48:32 +02:00
|
|
|
jnz .timer_loop_end_with_dec ;if TTL!=0
|
|
|
|
|
|
|
|
; Ok, TTL is 0
|
|
|
|
;if Status==AWAITING_RESPONSE and TTL==0
|
|
|
|
;then we have to change it to ARP_RESPONSE_TIMEOUT
|
2009-10-12 16:39:59 +02:00
|
|
|
cmp [ebx + ARP_ENTRY.Status], ARP_AWAITING_RESPONSE
|
2009-09-28 21:48:32 +02:00
|
|
|
jne @f
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
mov [ebx + ARP_ENTRY.Status], ARP_RESPONSE_TIMEOUT
|
|
|
|
mov [ebx + ARP_ENTRY.TTL], word 0x000A ;10 sec
|
2009-09-28 21:48:32 +02:00
|
|
|
jmp .timer_loop_end
|
|
|
|
|
|
|
|
@@:
|
|
|
|
;if TTL==0 and Status==VALID_MAPPING, we have to delete it
|
|
|
|
;if TTL==0 and Status==RESPONSE_TIMEOUT, delete too
|
2009-10-12 16:39:59 +02:00
|
|
|
mov esi, [NumARP]
|
2009-09-28 21:48:32 +02:00
|
|
|
sub esi, ecx ;esi=index of entry, will be deleted
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
push ebx ecx
|
2009-09-28 21:48:32 +02:00
|
|
|
call ARP_del_entry
|
2009-10-12 16:39:59 +02:00
|
|
|
pop ecx ebx
|
2009-09-28 21:48:32 +02:00
|
|
|
|
|
|
|
jmp .timer_loop_end
|
|
|
|
|
|
|
|
|
|
|
|
.timer_loop_end_with_dec:
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
dec [ebx + ARP_ENTRY.TTL] ;decrease TTL
|
2009-09-28 21:48:32 +02:00
|
|
|
|
|
|
|
.timer_loop_end:
|
|
|
|
|
|
|
|
add ebx, ARP_ENTRY.size
|
|
|
|
loop .timer_loop
|
|
|
|
|
|
|
|
.exit:
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-10-05 22:47:27 +02:00
|
|
|
;
|
|
|
|
; ARP_add_entry (or update)
|
|
|
|
;
|
|
|
|
; IN: arp entry in stack: esp .IP
|
|
|
|
; esp+4 .MAC
|
|
|
|
; esp+10 .Status
|
|
|
|
; esp+12 .TTL
|
|
|
|
; esp+14
|
|
|
|
;
|
|
|
|
; OUT: eax = entry #, -1 on error
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;----------------------------------------------------------------- ; TODO: use a mutex
|
2009-10-05 22:47:27 +02:00
|
|
|
align 4
|
|
|
|
ARP_add_entry:
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
DEBUGF 1,"ARP add entry: "
|
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
mov ecx, [NumARP]
|
|
|
|
test ecx, ecx
|
|
|
|
jz .add
|
|
|
|
|
2009-11-07 17:13:47 +01:00
|
|
|
mov eax, dword[esp + 4 + ARP_ENTRY.MAC]
|
|
|
|
mov bx , word[esp + 4 + ARP_ENTRY.MAC + 4]
|
2009-10-05 22:47:27 +02:00
|
|
|
mov esi, ARPTable
|
|
|
|
|
|
|
|
.loop:
|
|
|
|
cmp dword [esi + ARP_ENTRY.MAC], eax
|
|
|
|
jne .maybe_next
|
|
|
|
cmp word [esi + ARP_ENTRY.MAC + 4], bx
|
|
|
|
jne .maybe_next
|
|
|
|
|
|
|
|
cmp dword[esi + ARP_ENTRY.TTL], 0xFFFF ; static entry
|
|
|
|
jne .notstatic
|
2009-11-07 17:13:47 +01:00
|
|
|
cmp dword[esp + 4 + ARP_ENTRY.TTL], 0xFFFF
|
2009-10-12 16:39:59 +02:00
|
|
|
jne .error
|
2009-10-05 22:47:27 +02:00
|
|
|
.notstatic:
|
|
|
|
|
|
|
|
mov ebx, [NumARP]
|
|
|
|
xchg ebx, ecx
|
|
|
|
sub ecx, ebx
|
|
|
|
jmp .add
|
|
|
|
|
|
|
|
.maybe_next:
|
|
|
|
add esi, ARP_ENTRY.size
|
|
|
|
loop .loop
|
|
|
|
|
|
|
|
mov ecx, [NumARP]
|
|
|
|
cmp ecx, ARP_TABLE_SIZE
|
2009-10-12 16:39:59 +02:00
|
|
|
jge .error
|
2009-10-05 22:47:27 +02:00
|
|
|
|
|
|
|
.add:
|
|
|
|
push ecx
|
|
|
|
imul ecx, ARP_ENTRY.size
|
|
|
|
lea edi, [ecx + ARPTable]
|
2009-10-12 16:39:59 +02:00
|
|
|
lea esi, [esp + 8]
|
2009-10-05 22:47:27 +02:00
|
|
|
mov ecx, ARP_ENTRY.size/2
|
|
|
|
repz movsw
|
|
|
|
|
|
|
|
inc [NumARP]
|
|
|
|
pop eax
|
2009-11-09 17:52:58 +01:00
|
|
|
DEBUGF 1,"New entry created: %u\n", eax
|
2009-10-05 22:47:27 +02:00
|
|
|
.exit:
|
2009-10-12 16:39:59 +02:00
|
|
|
DEBUGF 1,"Exiting\n"
|
2009-11-07 17:13:47 +01:00
|
|
|
ret ARP_ENTRY.size
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
.error:
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
DEBUGF 1,"error! \n"
|
2009-10-05 22:47:27 +02:00
|
|
|
|
|
|
|
mov eax, -1
|
|
|
|
jmp .exit
|
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-28 21:48:32 +02:00
|
|
|
;
|
|
|
|
; ARP_del_entry
|
|
|
|
;
|
|
|
|
; IN: entry # in esi
|
|
|
|
; OUT: /
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-28 21:48:32 +02:00
|
|
|
align 4
|
|
|
|
ARP_del_entry:
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
DEBUGF 1,"ARP del entry %u, total entrys: %u\n", esi, [NumARP]
|
|
|
|
|
|
|
|
cmp esi, [NumARP]
|
|
|
|
jge .error
|
|
|
|
|
2009-09-28 21:48:32 +02:00
|
|
|
imul esi, ARP_ENTRY.size
|
|
|
|
|
|
|
|
mov ecx, (ARP_TABLE_SIZE - 1) * ARP_ENTRY.size
|
|
|
|
sub ecx, esi
|
|
|
|
|
2010-06-07 18:10:07 +02:00
|
|
|
lea edi, [ARPTable + esi] ;edi=ptr to entry that should be deleted
|
2009-09-28 21:48:32 +02:00
|
|
|
lea esi, [edi + ARP_ENTRY.size] ;esi=ptr to next entry
|
|
|
|
|
|
|
|
shr ecx,1 ;ecx/2 => ARP_ENTRY_SIZE MUST BE EVEN NUMBER!
|
|
|
|
rep movsw
|
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
dec [NumARP] ;decrease arp-entries counter
|
2010-06-07 18:10:07 +02:00
|
|
|
DEBUGF 1,"ARP entry deleted\n"
|
2009-10-12 16:39:59 +02:00
|
|
|
.error:
|
2009-09-28 21:48:32 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; ARP_Handler:
|
|
|
|
;
|
|
|
|
; This function handles ARP protocol over ethernet
|
|
|
|
; (other protocols may follow in the future)
|
|
|
|
;
|
|
|
|
; IN: Pointer to buffer in [esp]
|
|
|
|
; size of buffer in [esp+4]
|
|
|
|
; packet size (without ethernet header) in ecx
|
|
|
|
; OUT: /
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
align 4
|
2009-10-05 22:47:27 +02:00
|
|
|
ARP_handler:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
DEBUGF 1,"ARP_Handler - start\n"
|
|
|
|
cmp ecx, 28
|
|
|
|
jl .exit
|
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
cmp word [edx + ARP_Packet.Opcode], ARP_REP_OPCODE ; Is this a reply packet?
|
|
|
|
jne .maybe_request
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
DEBUGF 1,"ARP_Handler - it's a reply packet from %u.%u.%u.%u\n",\
|
|
|
|
[edx + ARP_Packet.SenderIP]:1,[edx + ARP_Packet.SenderIP+1]:1,[edx + ARP_Packet.SenderIP+2]:1,[edx + ARP_Packet.SenderIP+3]:1,
|
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
mov ecx, [NumARP]
|
|
|
|
test ecx, ecx
|
|
|
|
jz .exit
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
mov eax, [edx + ARP_Packet.SenderIP]
|
2009-10-05 22:47:27 +02:00
|
|
|
mov esi, ARPTable+ARP_ENTRY.IP
|
|
|
|
|
|
|
|
.loop:
|
2009-10-12 16:39:59 +02:00
|
|
|
cmp [esi], eax
|
|
|
|
je .gotit
|
|
|
|
add esi, ARP_ENTRY.size
|
2009-10-05 22:47:27 +02:00
|
|
|
loop .loop
|
|
|
|
|
|
|
|
jmp .exit
|
|
|
|
|
|
|
|
.gotit:
|
2009-10-12 16:39:59 +02:00
|
|
|
|
|
|
|
DEBUGF 1,"ARP_Handler - found matching entry\n"
|
|
|
|
|
|
|
|
cmp [esi+ARP_ENTRY.Status], 0x0300 ;if it is a static entry, dont touch it
|
2009-10-05 22:47:27 +02:00
|
|
|
je .exit
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
DEBUGF 1,"ARP_Handler - updating entry\n"
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
mov [esi+ARP_ENTRY.Status], ARP_VALID_MAPPING
|
|
|
|
mov [esi+ARP_ENTRY.TTL], ARP_ENTRY_TTL
|
|
|
|
|
|
|
|
mov eax, dword [edx + ARP_Packet.SenderMAC]
|
|
|
|
mov dword [esi+ARP_ENTRY.MAC], eax
|
|
|
|
mov ax , word [edx + ARP_Packet.SenderMAC + 4]
|
|
|
|
mov word [esi+ARP_ENTRY.MAC+4], ax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
jmp .exit
|
|
|
|
|
|
|
|
|
|
|
|
;------
|
|
|
|
|
|
|
|
|
|
|
|
.maybe_request:
|
2009-09-17 13:55:38 +02:00
|
|
|
cmp word [edx + ARP_Packet.Opcode], ARP_REQ_OPCODE ; Is this a request packet?
|
|
|
|
jne .exit
|
|
|
|
|
|
|
|
call ETH_struc2dev
|
2009-10-12 16:39:59 +02:00
|
|
|
DEBUGF 1,"ARP Request packet through device: %u\n", edi
|
2009-09-22 18:42:54 +02:00
|
|
|
inc [ARP_PACKETS_RX+4*edi]
|
2009-09-17 13:55:38 +02:00
|
|
|
cmp edi, -1
|
|
|
|
jz .exit
|
|
|
|
|
|
|
|
mov eax, edi
|
|
|
|
shl eax, 2
|
|
|
|
add eax, IP_LIST
|
|
|
|
mov eax, [eax]
|
|
|
|
cmp eax, [edx + ARP_Packet.TargetIP] ; Is it looking for my IP address?
|
|
|
|
jnz .exit
|
|
|
|
push eax
|
|
|
|
push edi
|
|
|
|
|
|
|
|
; OK, it is a request for one of our MAC addresses. Build the frame and send it
|
2009-10-05 22:47:27 +02:00
|
|
|
; We can reuse the buffer. (faster then using ARP_create_packet)
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
cld
|
|
|
|
lea esi, [edx + ARP_Packet.SenderMAC]
|
|
|
|
lea edi, [edx + ARP_Packet.TargetMAC]
|
|
|
|
movsd ; Move Sender Mac to Dest MAC
|
|
|
|
movsw ;
|
|
|
|
movsd ; Move sender IP to Dest IP
|
|
|
|
|
|
|
|
pop esi
|
|
|
|
mov esi, [ETH_DRV_LIST + 4*esi]
|
|
|
|
lea esi, [esi + ETH_DEVICE.mac]
|
|
|
|
lea edi, [edx + ARP_Packet.SenderMAC]
|
|
|
|
movsd ; Copy MAC address from in MAC_LIST
|
|
|
|
movsw ;
|
|
|
|
pop eax
|
|
|
|
stosd ; Write our IP
|
|
|
|
|
|
|
|
mov word [edx + ARP_Packet.Opcode], ARP_REP_OPCODE
|
|
|
|
|
|
|
|
; Now, Fill in ETHERNET header
|
|
|
|
|
|
|
|
mov edi, [esp]
|
|
|
|
lea esi, [edx + ARP_Packet.TargetMAC]
|
|
|
|
movsd
|
|
|
|
movsw
|
|
|
|
lea esi, [edx + ARP_Packet.SenderMAC]
|
|
|
|
movsd
|
|
|
|
movsw
|
2009-10-05 22:47:27 +02:00
|
|
|
; mov ax , ETHER_ARP
|
|
|
|
; stosw
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
DEBUGF 1,"ARP_Handler - Sending reply \n"
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
jmp ETH_sender ; And send it!
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
.exit:
|
2009-09-17 13:55:38 +02:00
|
|
|
call kernel_free
|
|
|
|
add esp, 4 ; pop (balance stack)
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
DEBUGF 1,"ARP_Handler - exiting\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; ARP_API
|
|
|
|
;
|
|
|
|
; This function is called by system function 75
|
|
|
|
;
|
|
|
|
; IN: subfunction number in bl
|
|
|
|
; device number in bh
|
|
|
|
; ecx, edx, .. depends on subfunction
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
; OUT: ?
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
align 4
|
|
|
|
ARP_API:
|
|
|
|
|
|
|
|
movzx eax, bh
|
|
|
|
shl eax, 2
|
|
|
|
|
|
|
|
test bl, bl
|
|
|
|
jz .packets_tx ; 0
|
|
|
|
dec bl
|
|
|
|
jz .packets_rx ; 1
|
|
|
|
dec bl
|
|
|
|
jz .entries ; 2
|
|
|
|
dec bl
|
|
|
|
jz .read ; 3
|
|
|
|
dec bl
|
|
|
|
jz .write ; 4
|
|
|
|
dec bl
|
|
|
|
jz .remove ; 5
|
|
|
|
dec bl
|
|
|
|
|
|
|
|
.error:
|
|
|
|
mov eax, -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_tx:
|
|
|
|
add eax, ARP_PACKETS_TX
|
|
|
|
mov eax, [eax]
|
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_rx:
|
|
|
|
add eax, ARP_PACKETS_RX
|
|
|
|
mov eax, [eax]
|
|
|
|
ret
|
|
|
|
|
|
|
|
.entries:
|
|
|
|
mov eax, [NumARP]
|
|
|
|
ret
|
|
|
|
|
|
|
|
.read:
|
2009-10-07 22:36:58 +02:00
|
|
|
cmp ecx, [NumARP]
|
|
|
|
jge .error
|
|
|
|
; edi = pointer to buffer
|
|
|
|
; ecx = # entry
|
|
|
|
imul ecx, ARP_ENTRY.size
|
|
|
|
add ecx, ARPTable
|
|
|
|
mov esi, ecx
|
|
|
|
mov ecx, ARP_ENTRY.size/2
|
|
|
|
rep movsw
|
|
|
|
|
|
|
|
xor eax, eax
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.write:
|
2009-10-07 22:36:58 +02:00
|
|
|
; esi = pointer to buffer
|
|
|
|
sub esp, ARP_ENTRY.size
|
|
|
|
mov edi, esp
|
|
|
|
mov ecx, ARP_ENTRY.size/2
|
|
|
|
rep movsw
|
2009-11-07 17:13:47 +01:00
|
|
|
call ARP_add_entry ;out: eax = entry number, -1 on error
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.remove:
|
2009-10-07 22:36:58 +02:00
|
|
|
; ecx = # entry
|
|
|
|
mov esi, ecx
|
2009-10-05 22:47:27 +02:00
|
|
|
call ARP_del_entry
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|