2009-10-05 22:47:27 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2012-07-24 22:29:46 +02:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;;
|
2009-10-05 22:47:27 +02:00
|
|
|
;; 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 ;;
|
2010-07-27 20:53:38 +02:00
|
|
|
;; Version 2, June- 1991 ;;
|
2009-10-05 22:47:27 +02:00
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
$Revision$
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-14 23:15:00 +02:00
|
|
|
ARP_NO_ENTRY = 0
|
|
|
|
ARP_VALID_MAPPING = 1
|
|
|
|
ARP_AWAITING_RESPONSE = 2
|
|
|
|
ARP_RESPONSE_TIMEOUT = 3
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-14 23:15:00 +02:00
|
|
|
ARP_REQUEST_TTL = 31 ; 20 s
|
|
|
|
ARP_ENTRY_TTL = 937 ; 600 s
|
|
|
|
ARP_STATIC_ENTRY = -1
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-14 23:15:00 +02:00
|
|
|
ARP_REQ_OPCODE = 0x0100 ; request
|
|
|
|
ARP_REP_OPCODE = 0x0200 ; reply
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-14 23:15:00 +02:00
|
|
|
ARP_TABLE_SIZE = 20 ; Size of table
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-09 00:06:26 +01:00
|
|
|
struct ARP_entry
|
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
IP dd ?
|
|
|
|
MAC dp ?
|
|
|
|
Status dw ?
|
|
|
|
TTL dw ?
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
ends
|
|
|
|
|
2011-11-09 00:06:26 +01:00
|
|
|
struct ARP_header
|
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
HardwareType dw ?
|
|
|
|
ProtocolType dw ?
|
|
|
|
HardwareSize db ?
|
|
|
|
ProtocolSize db ?
|
|
|
|
Opcode dw ?
|
|
|
|
SenderMAC dp ?
|
|
|
|
SenderIP dd ?
|
|
|
|
TargetMAC dp ?
|
|
|
|
TargetIP dd ?
|
2011-11-09 00:06:26 +01:00
|
|
|
|
|
|
|
ends
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
align 4
|
|
|
|
uglobal
|
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
NumARP dd ?
|
2009-11-09 17:52:58 +01:00
|
|
|
|
2012-08-15 18:15:57 +02:00
|
|
|
ARP_table rb ARP_TABLE_SIZE * sizeof.ARP_entry ; TODO: separate ARP table and stats per interface
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
ARP_PACKETS_TX rd MAX_NET_DEVICES
|
|
|
|
ARP_PACKETS_RX rd MAX_NET_DEVICES
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
endg
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-05 22:47:27 +02:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; ARP_init
|
|
|
|
;
|
|
|
|
; This function resets all ARP variables
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
2010-07-27 20:53:38 +02:00
|
|
|
macro ARP_init {
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
xor eax, eax
|
|
|
|
mov [NumARP], eax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov edi, ARP_PACKETS_TX
|
|
|
|
mov ecx, 2*MAX_NET_DEVICES
|
|
|
|
rep stosd
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
}
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
;---------------------------------------------------------------------------
|
2009-10-05 22:47:27 +02:00
|
|
|
;
|
2010-07-27 20:53:38 +02:00
|
|
|
; ARP_decrease_entry_ttls
|
2009-10-05 22:47:27 +02:00
|
|
|
;
|
2010-07-27 20:53:38 +02:00
|
|
|
;---------------------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
macro ARP_decrease_entry_ttls {
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
local .loop
|
|
|
|
local .exit
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
; 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
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov ecx, [NumARP]
|
|
|
|
test ecx, ecx
|
|
|
|
jz .exit
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov esi, ARP_table
|
2010-07-27 20:53:38 +02:00
|
|
|
.loop:
|
2011-11-12 23:35:01 +01:00
|
|
|
cmp [esi + ARP_entry.TTL], ARP_STATIC_ENTRY
|
|
|
|
je .next
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
dec [esi + ARP_entry.TTL]
|
|
|
|
jz .time_out
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.next:
|
2011-11-12 23:35:01 +01:00
|
|
|
add esi, sizeof.ARP_entry
|
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
|
|
|
jmp .exit
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.time_out:
|
2011-11-12 23:35:01 +01:00
|
|
|
cmp [esi + ARP_entry.Status], ARP_AWAITING_RESPONSE
|
|
|
|
je .response_timeout
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
push esi ecx
|
|
|
|
call ARP_del_entry
|
|
|
|
pop ecx esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
jmp .next
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.response_timeout:
|
2011-11-12 23:35:01 +01:00
|
|
|
mov [esi + ARP_entry.Status], ARP_RESPONSE_TIMEOUT
|
|
|
|
mov [esi + ARP_entry.TTL], 10
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
jmp .next
|
2009-11-09 17:52:58 +01:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.exit:
|
2009-11-09 17:52:58 +01:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
}
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-09 17:52:58 +01:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; ARP_input
|
|
|
|
;
|
|
|
|
; IN: Pointer to buffer in [esp]
|
|
|
|
; size of buffer in [esp+4]
|
|
|
|
; packet size (without ethernet header) in ecx
|
2012-04-18 18:01:38 +02:00
|
|
|
; packet ptr in edx
|
2010-07-27 20:53:38 +02:00
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
ARP_input:
|
2009-11-09 17:52:58 +01:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
cmp ecx, sizeof.ARP_header
|
|
|
|
jb .exit
|
2009-11-09 17:52:58 +01:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
;---------------------
|
|
|
|
; Handle Reply packets
|
2009-11-09 17:52:58 +01:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
cmp [edx + ARP_header.Opcode], ARP_REP_OPCODE
|
|
|
|
jne .maybe_request
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"ARP_input: got reply packet from %u.%u.%u.%u\n",\
|
|
|
|
[edx + ARP_header.SenderIP]:1, [edx + ARP_header.SenderIP + 1]:1,\
|
|
|
|
[edx + ARP_header.SenderIP + 2]:1, [edx + ARP_header.SenderIP + 3]:1
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov ecx, [NumARP]
|
|
|
|
test ecx, ecx
|
|
|
|
jz .exit
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov eax, [edx + ARP_header.SenderIP]
|
|
|
|
mov esi, ARP_table
|
2009-11-09 17:52:58 +01:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.loop:
|
2011-11-12 23:35:01 +01:00
|
|
|
cmp [esi + ARP_entry.IP], eax
|
|
|
|
je .gotit
|
|
|
|
add esi, sizeof.ARP_entry
|
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
jmp .exit
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.gotit:
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"ARP_input: found matching entry\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
cmp [esi + ARP_entry.TTL], ARP_STATIC_ENTRY ; if it is a static entry, dont touch it
|
|
|
|
je .exit
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"ARP_input: updating entry\n"
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov [esi + ARP_entry.Status], ARP_VALID_MAPPING
|
|
|
|
mov [esi + ARP_entry.TTL], ARP_ENTRY_TTL
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov eax, dword [edx + ARP_header.SenderMAC]
|
2012-07-27 01:21:35 +02:00
|
|
|
mov dword [esi + ARP_entry.MAC], eax
|
2012-04-18 18:01:38 +02:00
|
|
|
mov cx, word [edx + ARP_header.SenderMAC + 4]
|
2012-07-27 01:21:35 +02:00
|
|
|
mov word [esi + ARP_entry.MAC + 4], cx
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
jmp .exit
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
;-----------------------
|
|
|
|
; Handle Request packets
|
|
|
|
|
|
|
|
.maybe_request:
|
2011-11-12 23:35:01 +01:00
|
|
|
cmp [edx + ARP_header.Opcode], ARP_REQ_OPCODE
|
|
|
|
jne .exit
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
call NET_ptr_to_num
|
|
|
|
cmp edi, -1
|
|
|
|
jz .exit
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"ARP_input: got request packet through device: %u\n", edi
|
|
|
|
inc [ARP_PACKETS_RX + 4*edi]
|
2009-09-22 18:42:54 +02:00
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
mov eax, [IP_LIST + 4*edi]
|
2011-11-12 23:35:01 +01:00
|
|
|
cmp eax, [edx + ARP_header.TargetIP] ; Is it looking for my IP address?
|
2012-04-18 18:01:38 +02:00
|
|
|
jne .exit
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
push eax
|
|
|
|
push edi
|
2010-07-27 20:53:38 +02:00
|
|
|
|
|
|
|
; OK, it is a request for one of our MAC addresses.
|
|
|
|
; Build the frame and send it. We can reuse the buffer. (faster then using ARP_create_packet)
|
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
lea esi, [edx + ARP_header.SenderMAC]
|
|
|
|
lea edi, [edx + ARP_header.TargetMAC]
|
|
|
|
movsd ; Move Sender Mac to Dest MAC
|
|
|
|
movsw ;
|
|
|
|
movsd ; Move sender IP to Dest IP
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
pop esi
|
|
|
|
mov esi, [NET_DRV_LIST + 4*esi]
|
|
|
|
lea esi, [esi + ETH_DEVICE.mac]
|
|
|
|
lea edi, [edx + ARP_header.SenderMAC]
|
|
|
|
movsd ; Copy MAC address from in MAC_LIST
|
|
|
|
movsw ;
|
|
|
|
pop eax
|
|
|
|
stosd ; Write our IP
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov [edx + ARP_header.Opcode], ARP_REP_OPCODE
|
2010-07-27 20:53:38 +02:00
|
|
|
|
|
|
|
; Now, Fill in ETHERNET header
|
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov edi, [esp]
|
|
|
|
lea esi, [edx + ARP_header.TargetMAC]
|
|
|
|
movsd
|
|
|
|
movsw
|
|
|
|
lea esi, [edx + ARP_header.SenderMAC]
|
|
|
|
movsd
|
|
|
|
movsw
|
2012-04-18 18:01:38 +02:00
|
|
|
; mov ax , ETHER_ARP ; It's already there, I'm sure of it!
|
2010-07-27 20:53:38 +02:00
|
|
|
; stosw
|
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"ARP_input: Sending reply\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
call [ebx + NET_DEVICE.transmit]
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-18 18:01:38 +02:00
|
|
|
.exit:
|
2011-11-12 23:35:01 +01:00
|
|
|
call kernel_free
|
|
|
|
add esp, 4 ; pop (balance stack)
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"ARP_input: exiting\n"
|
2011-11-12 23:35:01 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2009-09-28 21:48:32 +02:00
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
;
|
2010-07-27 20:53:38 +02:00
|
|
|
; ARP_output_request
|
2009-09-28 21:48:32 +02:00
|
|
|
;
|
2010-07-27 20:53:38 +02:00
|
|
|
; IN: ip in eax
|
2012-07-17 23:36:34 +02:00
|
|
|
; device in edi
|
2009-09-28 21:48:32 +02:00
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
align 4
|
2010-07-27 20:53:38 +02:00
|
|
|
ARP_output_request:
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
push eax ; DestIP
|
2012-07-27 01:21:35 +02:00
|
|
|
pushd [IP_LIST + edi] ; SenderIP
|
|
|
|
|
|
|
|
DEBUGF 1,"ARP_output_request: ip=%u.%u.%u.%u\n",\
|
|
|
|
[esp]:1, [esp + 1]:1, [esp + 2]:1, [esp + 3]:1
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
mov ebx, [NET_DRV_LIST + edi] ; device ptr
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
lea eax, [ebx + ETH_DEVICE.mac] ; local device mac
|
|
|
|
mov edx, ETH_BROADCAST ; broadcast mac
|
|
|
|
mov ecx, sizeof.ARP_header
|
|
|
|
mov di, ETHER_ARP
|
|
|
|
call ETH_output
|
|
|
|
jz .exit
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov ecx, eax
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov [edi + ARP_header.HardwareType], 0x0100 ; Ethernet
|
|
|
|
mov [edi + ARP_header.ProtocolType], 0x0008 ; IP
|
|
|
|
mov [edi + ARP_header.HardwareSize], 6 ; MAC-addr length
|
|
|
|
mov [edi + ARP_header.ProtocolSize], 4 ; IP-addr length
|
|
|
|
mov [edi + ARP_header.Opcode], ARP_REQ_OPCODE ; Request
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
add edi, ARP_header.SenderMAC
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
lea esi, [ebx + ETH_DEVICE.mac] ; SenderMac
|
|
|
|
movsw ;
|
|
|
|
movsd ;
|
|
|
|
pop eax ; SenderIP
|
|
|
|
stosd ;
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov eax, -1 ; DestMac
|
|
|
|
stosd ;
|
|
|
|
stosw ;
|
|
|
|
pop eax ; DestIP
|
|
|
|
stosd ;
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"ARP_output_request: device=%x\n", ebx
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
push edx ecx
|
|
|
|
call [ebx + NET_DEVICE.transmit]
|
|
|
|
ret
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.exit:
|
2012-07-27 01:21:35 +02:00
|
|
|
add esp, 4 + 4
|
|
|
|
DEBUGF 1,"ARP_output_request: failed\n"
|
2011-11-12 23:35:01 +01:00
|
|
|
sub eax, eax
|
|
|
|
ret
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-10-05 22:47:27 +02:00
|
|
|
;
|
|
|
|
; ARP_add_entry (or update)
|
|
|
|
;
|
2010-07-27 20:53:38 +02:00
|
|
|
; IN: esi = ptr to entry (can easily be made on the stack)
|
2009-10-05 22:47:27 +02:00
|
|
|
; 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:
|
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"ARP_add_entry: "
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov ecx, [NumARP]
|
|
|
|
test ecx, ecx ; first entry?
|
|
|
|
jz .add
|
|
|
|
cmp ecx, ARP_TABLE_SIZE ; list full ?
|
|
|
|
jae .error
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov eax, dword [esi + ARP_entry.MAC]
|
|
|
|
mov bx , word [esi + ARP_entry.MAC + 4]
|
|
|
|
mov edi, ARP_table
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.loop:
|
2011-11-12 23:35:01 +01:00
|
|
|
cmp dword [edi + ARP_entry.MAC], eax ; Check for duplicate MAC's
|
|
|
|
jne .maybe_next ;
|
|
|
|
cmp word [edi + ARP_entry.MAC + 4], bx ;
|
|
|
|
jne .maybe_next ;
|
|
|
|
|
|
|
|
cmp [edi + ARP_entry.TTL], ARP_STATIC_ENTRY
|
|
|
|
jne .notstatic
|
|
|
|
cmp [esi + ARP_entry.TTL], ARP_STATIC_ENTRY
|
|
|
|
jne .error
|
2010-07-27 20:53:38 +02:00
|
|
|
.notstatic:
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
neg ecx
|
|
|
|
add ecx, [NumARP]
|
|
|
|
jmp .add
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.maybe_next:
|
2011-11-12 23:35:01 +01:00
|
|
|
add esi, sizeof.ARP_entry
|
|
|
|
loop .loop
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov ecx, [NumARP]
|
2010-07-27 20:53:38 +02:00
|
|
|
.add:
|
2011-11-12 23:35:01 +01:00
|
|
|
push ecx
|
|
|
|
imul ecx, sizeof.ARP_entry
|
|
|
|
lea edi, [ecx + ARP_table]
|
|
|
|
mov ecx, sizeof.ARP_entry/2
|
|
|
|
rep movsw
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
lea esi, [edi - sizeof.ARP_entry]
|
|
|
|
inc [NumARP]
|
|
|
|
pop eax
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"entry=%u\n", eax
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
ret
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.error:
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"failed\n"
|
2011-11-12 23:35:01 +01:00
|
|
|
mov eax, -1
|
|
|
|
ret
|
2009-10-05 22:47:27 +02:00
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-28 21:48:32 +02:00
|
|
|
;
|
|
|
|
; ARP_del_entry
|
|
|
|
;
|
2010-07-27 20:53:38 +02:00
|
|
|
; IN: esi = ptr to arp entry
|
2009-09-28 21:48:32 +02:00
|
|
|
; OUT: /
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-28 21:48:32 +02:00
|
|
|
align 4
|
|
|
|
ARP_del_entry:
|
|
|
|
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"ARP_del_entry: entry=%u entrys=%u\n", esi, [NumARP]
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov ecx, ARP_table + (ARP_TABLE_SIZE - 1) * sizeof.ARP_entry
|
|
|
|
sub ecx, esi
|
|
|
|
shr ecx, 1
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov edi, esi
|
|
|
|
lea esi, [edi + sizeof.ARP_entry]
|
|
|
|
rep movsw
|
2009-09-28 21:48:32 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
dec [NumARP]
|
2012-07-27 01:21:35 +02:00
|
|
|
DEBUGF 1,"ARP_del_entry: success\n"
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
ret
|
2009-09-28 21:48:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2010-07-27 20:53:38 +02:00
|
|
|
; ARP_IP_to_MAC
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2010-07-27 20:53:38 +02:00
|
|
|
; This function translates an IP address to a MAC address
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2010-07-27 20:53:38 +02:00
|
|
|
; IN: eax = IPv4 address
|
2012-07-17 23:36:34 +02:00
|
|
|
; edi = device number
|
2010-07-27 20:53:38 +02:00
|
|
|
; OUT: eax = -1 on error, -2 means request send
|
|
|
|
; else, ax = first two bytes of mac (high 16 bits of eax will be 0)
|
|
|
|
; ebx = last four bytes of mac
|
2012-07-17 23:36:34 +02:00
|
|
|
; edi = unchanged
|
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
|
2010-07-27 20:53:38 +02:00
|
|
|
ARP_IP_to_MAC:
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2012-04-03 20:26:53 +02:00
|
|
|
DEBUGF 1,"ARP_IP_to_MAC: %u.%u", al, ah
|
|
|
|
rol eax, 16
|
|
|
|
DEBUGF 1,".%u.%u\n", al, ah
|
|
|
|
rol eax, 16
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
cmp eax, 0xffffffff
|
|
|
|
je .broadcast
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
;--------------------------------
|
|
|
|
; Try to find the IP in ARP_table
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
mov ecx, [NumARP]
|
|
|
|
test ecx, ecx
|
|
|
|
jz .not_in_list
|
|
|
|
mov esi, ARP_table + ARP_entry.IP
|
2010-07-27 20:53:38 +02:00
|
|
|
.scan_loop:
|
2011-11-12 23:35:01 +01:00
|
|
|
cmp [esi], eax
|
|
|
|
je .found_it
|
|
|
|
add esi, sizeof.ARP_entry
|
|
|
|
loop .scan_loop
|
2009-10-05 22:47:27 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.not_in_list:
|
2012-07-24 22:29:46 +02:00
|
|
|
DEBUGF 1,"ARP_IP_to_MAC: preparing for ARP request\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
;--------------------
|
|
|
|
; Send an ARP request
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-07-17 23:36:34 +02:00
|
|
|
push eax edi ; save IP for ARP_output_request
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-18 18:01:38 +02:00
|
|
|
; Now create the ARP entry
|
|
|
|
pushw ARP_REQUEST_TTL ; TTL
|
|
|
|
pushw ARP_AWAITING_RESPONSE ; status
|
|
|
|
pushd 0 ; mac
|
2011-11-12 23:35:01 +01:00
|
|
|
pushw 0
|
2012-04-18 18:01:38 +02:00
|
|
|
pushd eax ; ip
|
2011-11-12 23:35:01 +01:00
|
|
|
mov esi, esp
|
|
|
|
call ARP_add_entry
|
|
|
|
add esp, sizeof.ARP_entry
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
cmp eax, -1
|
|
|
|
je .full
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-18 18:01:38 +02:00
|
|
|
; And send a request
|
2012-07-17 23:36:34 +02:00
|
|
|
pop edi eax
|
2012-04-18 18:01:38 +02:00
|
|
|
call ARP_output_request ; IP in eax
|
2010-07-28 00:08:21 +02:00
|
|
|
;; TODO: check if driver could transmit packet
|
|
|
|
|
2012-04-18 18:01:38 +02:00
|
|
|
mov eax, -2 ; request send
|
2011-11-12 23:35:01 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.found_it:
|
2012-07-24 22:29:46 +02:00
|
|
|
DEBUGF 1,"ARP_IP_to_MAC: found IP\n"
|
2012-04-18 18:01:38 +02:00
|
|
|
cmp [esi + ARP_entry.Status], ARP_VALID_MAPPING
|
2011-11-12 23:35:01 +01:00
|
|
|
jne .invalid
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
movzx eax, word [esi + ARP_entry.MAC]
|
2012-07-17 23:36:34 +02:00
|
|
|
mov ebx, dword[esi + ARP_entry.MAC + 2]
|
2011-11-12 23:35:01 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.invalid:
|
2012-07-24 22:29:46 +02:00
|
|
|
DEBUGF 1,"ARP_IP_to_MAC: entry has no valid mapping!\n"
|
2011-11-12 23:35:01 +01:00
|
|
|
mov eax, -1
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.full:
|
2012-07-24 22:29:46 +02:00
|
|
|
DEBUGF 1,"ARP_IP_to_MAC: table is full!\n"
|
2012-07-17 23:36:34 +02:00
|
|
|
add esp, 8
|
2011-11-12 23:35:01 +01:00
|
|
|
mov eax, -1
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
.broadcast:
|
2011-11-12 23:35:01 +01:00
|
|
|
mov eax, 0x0000ffff
|
|
|
|
mov ebx, 0xffffffff
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
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
|
2012-04-14 23:15:00 +02:00
|
|
|
ARP_api:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-12 23:35:01 +01:00
|
|
|
movzx eax, bh
|
|
|
|
shl eax, 2
|
|
|
|
|
2012-04-14 23:15:00 +02:00
|
|
|
and ebx, 0xff
|
|
|
|
cmp ebx, .number
|
|
|
|
ja .error
|
|
|
|
jmp dword [.table + 4*ebx]
|
|
|
|
|
|
|
|
.table:
|
|
|
|
dd .packets_tx ; 0
|
|
|
|
dd .packets_rx ; 1
|
|
|
|
dd .entries ; 2
|
|
|
|
dd .read ; 3
|
|
|
|
dd .write ; 4
|
|
|
|
dd .remove ; 5
|
|
|
|
.number = ($ - .table) / 4 - 1
|
|
|
|
|
|
|
|
.error:
|
2011-11-12 23:35:01 +01:00
|
|
|
mov eax, -1
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-14 23:15:00 +02:00
|
|
|
.packets_tx:
|
|
|
|
mov eax, [ARP_PACKETS_TX + eax]
|
2011-11-12 23:35:01 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-14 23:15:00 +02:00
|
|
|
.packets_rx:
|
|
|
|
mov eax, [ARP_PACKETS_RX + eax]
|
2011-11-12 23:35:01 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-14 23:15:00 +02:00
|
|
|
.entries:
|
2011-11-12 23:35:01 +01:00
|
|
|
mov eax, [NumARP]
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-14 23:15:00 +02:00
|
|
|
.read:
|
2011-11-12 23:35:01 +01:00
|
|
|
cmp ecx, [NumARP]
|
|
|
|
jae .error
|
|
|
|
; edi = pointer to buffer
|
|
|
|
; ecx = # entry
|
|
|
|
imul ecx, sizeof.ARP_entry
|
|
|
|
add ecx, ARP_table
|
|
|
|
mov esi, ecx
|
|
|
|
mov ecx, sizeof.ARP_entry/2
|
|
|
|
rep movsw
|
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-14 23:15:00 +02:00
|
|
|
.write:
|
2011-11-12 23:35:01 +01:00
|
|
|
; esi = pointer to buffer
|
|
|
|
call ARP_add_entry ;out: eax = entry number, -1 on error
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2012-04-14 23:15:00 +02:00
|
|
|
.remove:
|
2011-11-12 23:35:01 +01:00
|
|
|
; ecx = # entry
|
|
|
|
cmp ecx, [NumARP]
|
|
|
|
jae .error
|
|
|
|
imul ecx, sizeof.ARP_entry
|
|
|
|
lea esi, [ARP_table + ecx]
|
|
|
|
call ARP_del_entry
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|