2013-05-28 19:34:26 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2024-05-22 17:15:14 +02:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2024. All rights reserved. ;;
|
2013-05-28 19:34:26 +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 ;;
|
|
|
|
;; Version 2, June- 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
|
|
|
|
ARP_NO_ENTRY = 0
|
|
|
|
ARP_VALID_MAPPING = 1
|
|
|
|
ARP_AWAITING_RESPONSE = 2
|
|
|
|
ARP_RESPONSE_TIMEOUT = 3
|
|
|
|
|
|
|
|
ARP_REQUEST_TTL = 31 ; 20 s
|
|
|
|
ARP_ENTRY_TTL = 937 ; 600 s
|
|
|
|
ARP_STATIC_ENTRY = -1
|
|
|
|
|
|
|
|
ARP_REQ_OPCODE = 0x0100 ; request
|
|
|
|
ARP_REP_OPCODE = 0x0200 ; reply
|
|
|
|
|
|
|
|
ARP_TABLE_SIZE = 20 ; Size of table
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
struct ARP_entry
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
IP dd ?
|
|
|
|
MAC dp ?
|
|
|
|
Status dw ?
|
|
|
|
TTL dw ?
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ends
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
struct ARP_header
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
HardwareType dw ?
|
|
|
|
ProtocolType dw ?
|
|
|
|
HardwareSize db ?
|
|
|
|
ProtocolSize db ?
|
|
|
|
Opcode dw ?
|
|
|
|
SenderMAC dp ?
|
|
|
|
SenderIP dd ?
|
|
|
|
TargetMAC dp ?
|
|
|
|
TargetIP dd ?
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ends
|
|
|
|
|
|
|
|
uglobal
|
2013-06-24 13:39:05 +02:00
|
|
|
align 4
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
ARP_table rb NET_DEVICES_MAX*(ARP_TABLE_SIZE * sizeof.ARP_entry)
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2019-08-25 20:21:44 +02:00
|
|
|
ARP_entries rd NET_DEVICES_MAX
|
|
|
|
ARP_packets_tx rd NET_DEVICES_MAX
|
|
|
|
ARP_packets_rx rd NET_DEVICES_MAX
|
|
|
|
ARP_conflicts rd NET_DEVICES_MAX
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
endg
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; arp_init: Resets all ARP variables. ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
macro arp_init {
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
xor eax, eax
|
2019-08-25 20:21:44 +02:00
|
|
|
mov edi, ARP_entries
|
2013-06-05 02:21:20 +02:00
|
|
|
mov ecx, 4*NET_DEVICES_MAX
|
2013-06-27 01:35:43 +02:00
|
|
|
rep stosd
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; arp_decrease_entry_ttls ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
macro arp_decrease_entry_ttls {
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
local .loop
|
|
|
|
local .exit
|
|
|
|
|
|
|
|
; 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
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
xor edi, edi
|
|
|
|
.loop_outer:
|
2019-08-25 20:21:44 +02:00
|
|
|
mov ecx, [ARP_entries + 4*edi]
|
2013-05-28 19:34:26 +02:00
|
|
|
test ecx, ecx
|
|
|
|
jz .exit
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
mov esi, (ARP_TABLE_SIZE * sizeof.ARP_entry)
|
|
|
|
imul esi, edi
|
|
|
|
add esi, ARP_table
|
2013-05-28 19:34:26 +02:00
|
|
|
.loop:
|
|
|
|
cmp [esi + ARP_entry.TTL], ARP_STATIC_ENTRY
|
|
|
|
je .next
|
|
|
|
|
|
|
|
dec [esi + ARP_entry.TTL]
|
|
|
|
jz .time_out
|
|
|
|
|
|
|
|
.next:
|
|
|
|
add esi, sizeof.ARP_entry
|
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
|
|
|
jmp .exit
|
|
|
|
|
|
|
|
.time_out:
|
|
|
|
cmp [esi + ARP_entry.Status], ARP_AWAITING_RESPONSE
|
|
|
|
je .response_timeout
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
push esi edi ecx
|
2015-12-27 16:37:31 +01:00
|
|
|
call arp_del_entry
|
2013-06-05 02:21:20 +02:00
|
|
|
pop ecx edi esi
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
jmp .next
|
|
|
|
|
|
|
|
.response_timeout:
|
|
|
|
mov [esi + ARP_entry.Status], ARP_RESPONSE_TIMEOUT
|
|
|
|
mov [esi + ARP_entry.TTL], 10
|
|
|
|
|
|
|
|
jmp .next
|
|
|
|
|
|
|
|
.exit:
|
2013-06-05 02:21:20 +02:00
|
|
|
inc edi
|
|
|
|
cmp edi, NET_DEVICES_MAX
|
|
|
|
jb .loop_outer
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; arp_input ;
|
|
|
|
; ;
|
|
|
|
; IN: [esp] = Pointer to buffer ;
|
|
|
|
; [esp+4] = size of buffer ;
|
|
|
|
; ecx = packet size (without ethernet header) ;
|
|
|
|
; edx = packet ptr ;
|
|
|
|
; ebx = device ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
arp_input:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;-----------------------------------------
|
|
|
|
; Check validity and print some debug info
|
|
|
|
|
|
|
|
cmp ecx, sizeof.ARP_header
|
|
|
|
jb .exit
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_ptr_to_num4
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp edi, -1
|
|
|
|
jz .exit
|
|
|
|
|
2019-08-25 20:21:44 +02:00
|
|
|
inc [ARP_packets_rx + edi] ; update stats
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-11 12:46:29 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_input: got packet from %u.%u.%u.%u (device*4=%u)\n",\
|
2013-05-28 19:34:26 +02:00
|
|
|
[edx + ARP_header.SenderIP]:1, [edx + ARP_header.SenderIP + 1]:1,\
|
|
|
|
[edx + ARP_header.SenderIP + 2]:1, [edx + ARP_header.SenderIP + 3]:1, edi
|
|
|
|
|
|
|
|
;------------------------------
|
|
|
|
; First, check for IP collision
|
|
|
|
|
|
|
|
mov eax, [edx + ARP_header.SenderIP]
|
2019-08-25 20:21:44 +02:00
|
|
|
cmp eax, [IPv4_address + edi]
|
2013-05-28 19:34:26 +02:00
|
|
|
je .collision
|
|
|
|
|
|
|
|
;---------------------
|
|
|
|
; Handle reply packets
|
|
|
|
|
|
|
|
cmp [edx + ARP_header.Opcode], ARP_REP_OPCODE
|
|
|
|
jne .maybe_request
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_input: It's a reply\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2019-08-25 20:21:44 +02:00
|
|
|
mov ecx, [ARP_entries + edi]
|
2013-05-28 19:34:26 +02:00
|
|
|
test ecx, ecx
|
|
|
|
jz .exit
|
|
|
|
|
2013-06-11 12:46:29 +02:00
|
|
|
mov esi, edi
|
|
|
|
imul esi, (ARP_TABLE_SIZE * sizeof.ARP_entry)/4
|
2013-06-05 02:21:20 +02:00
|
|
|
add esi, ARP_table
|
2013-05-28 19:34:26 +02:00
|
|
|
.loop:
|
|
|
|
cmp [esi + ARP_entry.IP], eax
|
|
|
|
je .gotit
|
|
|
|
add esi, sizeof.ARP_entry
|
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_input: no matching entry found\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .exit
|
|
|
|
|
|
|
|
.gotit:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_input: found matching entry\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
cmp [esi + ARP_entry.TTL], ARP_STATIC_ENTRY ; if it is a static entry, dont touch it
|
|
|
|
je .exit
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_input: updating entry\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov [esi + ARP_entry.Status], ARP_VALID_MAPPING
|
|
|
|
mov [esi + ARP_entry.TTL], ARP_ENTRY_TTL
|
|
|
|
|
|
|
|
mov eax, dword [edx + ARP_header.SenderMAC]
|
|
|
|
mov dword [esi + ARP_entry.MAC], eax
|
|
|
|
mov cx, word [edx + ARP_header.SenderMAC + 4]
|
|
|
|
mov word [esi + ARP_entry.MAC + 4], cx
|
|
|
|
|
|
|
|
jmp .exit
|
|
|
|
|
|
|
|
;-----------------------
|
|
|
|
; Handle request packets
|
|
|
|
|
|
|
|
.maybe_request:
|
|
|
|
cmp [edx + ARP_header.Opcode], ARP_REQ_OPCODE
|
|
|
|
jne .exit
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_input: its a request\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2019-08-25 20:21:44 +02:00
|
|
|
mov eax, [IPv4_address + edi]
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp eax, [edx + ARP_header.TargetIP] ; Is it looking for my IP address?
|
|
|
|
jne .exit
|
|
|
|
|
|
|
|
push eax
|
|
|
|
push edi
|
|
|
|
|
|
|
|
; 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)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
pop esi
|
2019-08-25 20:46:47 +02:00
|
|
|
mov esi, [net_device_list + esi]
|
2013-05-28 19:34:26 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
mov [edx + ARP_header.Opcode], ARP_REP_OPCODE
|
|
|
|
|
|
|
|
; Now, Fill in ETHERNET header
|
|
|
|
|
|
|
|
mov edi, [esp]
|
2018-11-06 20:50:07 +01:00
|
|
|
add edi, [edi + NET_BUFF.offset]
|
2013-05-28 19:34:26 +02:00
|
|
|
lea esi, [edx + ARP_header.TargetMAC]
|
|
|
|
movsd
|
|
|
|
movsw
|
|
|
|
lea esi, [edx + ARP_header.SenderMAC]
|
|
|
|
movsd
|
|
|
|
movsw
|
|
|
|
; mov ax , ETHER_ARP ; It's already there, I'm sure of it!
|
|
|
|
; stosw
|
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_input: Sending reply\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
call [ebx + NET_DEVICE.transmit]
|
|
|
|
ret
|
|
|
|
|
|
|
|
.collision:
|
2019-08-25 20:21:44 +02:00
|
|
|
inc [ARP_conflicts + edi]
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_input: IP address conflict detected!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.exit:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_input: exiting\n"
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_buff_free
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; arp_output_request ;
|
|
|
|
; ;
|
|
|
|
; IN: ebx = device ptr ;
|
|
|
|
; eax = IP ;
|
|
|
|
; ;
|
|
|
|
; OUT: scratched: probably everything ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
arp_output_request:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
push eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_output_request: ip=%u.%u.%u.%u device=0x%x\n",\
|
|
|
|
[esp]:1, [esp + 1]:1, [esp + 2]:1, [esp + 3]:1, ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-08-02 14:48:32 +02:00
|
|
|
mov ax, ETHER_PROTO_ARP
|
2013-05-28 19:34:26 +02:00
|
|
|
mov ecx, sizeof.ARP_header
|
2014-08-02 14:48:32 +02:00
|
|
|
mov edx, ETH_BROADCAST ; broadcast mac
|
2015-12-27 16:37:31 +01:00
|
|
|
call eth_output
|
2013-05-28 19:34:26 +02:00
|
|
|
jz .exit
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
add edi, ARP_header.SenderMAC
|
|
|
|
lea esi, [ebx + ETH_DEVICE.mac] ; SenderMac
|
|
|
|
movsw ;
|
|
|
|
movsd ;
|
|
|
|
|
2013-06-11 13:11:26 +02:00
|
|
|
push edi
|
2015-12-27 16:37:31 +01:00
|
|
|
call net_ptr_to_num4
|
2019-08-25 20:21:44 +02:00
|
|
|
inc [ARP_packets_tx + edi] ; assume we will succeed
|
|
|
|
lea esi, [IPv4_address + edi] ; SenderIP
|
2013-06-11 13:11:26 +02:00
|
|
|
pop edi
|
2013-06-05 02:21:20 +02:00
|
|
|
movsd
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
mov esi, ETH_BROADCAST ; DestMac
|
|
|
|
movsw ;
|
|
|
|
movsd ;
|
|
|
|
popd [edi] ; DestIP
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
push eax
|
2013-05-28 19:34:26 +02:00
|
|
|
call [ebx + NET_DEVICE.transmit]
|
|
|
|
ret
|
|
|
|
|
|
|
|
.exit:
|
2013-06-05 02:21:20 +02:00
|
|
|
add esp, 4
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "ARP_output_request: send failed\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; arp_add_entry: Add or update an entry in the ARP table. ;
|
|
|
|
; ;
|
|
|
|
; IN: esi = ptr to entry (can easily be made on the stack) ;
|
|
|
|
; edi = device num*4 ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = entry number on success ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; esi = ptr to newly created entry ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
arp_add_entry:
|
|
|
|
|
|
|
|
; TODO: use a mutex to lock ARP table
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_add_entry: device=%u\n", edi
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2019-08-25 20:21:44 +02:00
|
|
|
mov ecx, [ARP_entries + edi]
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp ecx, ARP_TABLE_SIZE ; list full ?
|
2013-06-05 02:21:20 +02:00
|
|
|
jae .full
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
; From this point on, we can only fail if IP has a static entry, or if table is corrupt.
|
|
|
|
|
2019-08-25 20:21:44 +02:00
|
|
|
inc [ARP_entries + edi] ; assume we will succeed
|
2013-06-05 02:21:20 +02:00
|
|
|
|
|
|
|
push edi
|
|
|
|
xor ecx, ecx
|
2013-06-10 22:19:12 +02:00
|
|
|
imul edi, ARP_TABLE_SIZE*sizeof.ARP_entry/4
|
2013-06-05 02:21:20 +02:00
|
|
|
add edi, ARP_table
|
2013-06-06 07:19:15 +02:00
|
|
|
mov eax, [esi + ARP_entry.IP]
|
2013-05-28 19:34:26 +02:00
|
|
|
.loop:
|
|
|
|
cmp [edi + ARP_entry.Status], ARP_NO_ENTRY ; is this slot empty?
|
|
|
|
je .add
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
cmp [edi + ARP_entry.IP], eax ; if not, check if it doesnt collide
|
2013-05-28 19:34:26 +02:00
|
|
|
jne .maybe_next
|
|
|
|
|
|
|
|
cmp [edi + ARP_entry.TTL], ARP_STATIC_ENTRY ; ok, its the same IP, update it if not static
|
|
|
|
jne .add
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "ARP_add_entry: failed, IP already has a static entry\n"
|
|
|
|
jmp .error
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
.maybe_next: ; try the next slot
|
|
|
|
add edi, sizeof.ARP_entry
|
2013-06-05 02:21:20 +02:00
|
|
|
inc ecx
|
|
|
|
cmp ecx, ARP_TABLE_SIZE
|
|
|
|
jb .loop
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.add:
|
2013-06-05 02:21:20 +02:00
|
|
|
push ecx
|
2013-05-28 19:34:26 +02:00
|
|
|
mov ecx, sizeof.ARP_entry/2
|
2013-06-27 01:35:43 +02:00
|
|
|
rep movsw
|
2013-06-05 02:21:20 +02:00
|
|
|
pop ecx
|
|
|
|
lea esi, [edi - sizeof.ARP_entry]
|
|
|
|
pop edi
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_add_entry: entry=%u\n", ecx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
2013-06-05 02:21:20 +02:00
|
|
|
pop edi
|
2019-08-25 20:21:44 +02:00
|
|
|
dec [ARP_entries + edi]
|
2013-06-05 02:21:20 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "ARP_add_entry_failed\n"
|
|
|
|
.full:
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; arp_del_entry: Remove an entry from the ARP table. ;
|
|
|
|
; ;
|
|
|
|
; IN: esi = ptr to arp entry ;
|
|
|
|
; edi = device number ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
arp_del_entry:
|
|
|
|
|
|
|
|
; TODO: use a mutex to lock ARP table
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2019-09-02 22:45:28 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_del_entry: entry=0x%x entrys=%u\n", esi, [ARP_entries + 4*edi]
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_del_entry: IP=%u.%u.%u.%u\n", \
|
2013-05-28 19:34:26 +02:00
|
|
|
[esi + ARP_entry.IP]:1, [esi + ARP_entry.IP + 1]:1, [esi + ARP_entry.IP + 2]:1, [esi + ARP_entry.IP + 3]:1
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
push edi
|
|
|
|
imul edi, (ARP_TABLE_SIZE) * sizeof.ARP_entry
|
|
|
|
lea ecx, [ARP_table + (ARP_TABLE_SIZE - 1) * sizeof.ARP_entry + edi]
|
2013-05-28 19:34:26 +02:00
|
|
|
sub ecx, esi
|
|
|
|
shr ecx, 1
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
; move all trailing entries, sizeof.ARP_entry bytes to left.
|
2013-05-28 19:34:26 +02:00
|
|
|
mov edi, esi
|
|
|
|
add esi, sizeof.ARP_entry
|
2013-06-27 01:35:43 +02:00
|
|
|
rep movsw
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
; now add an empty entry to the end (erasing previous one)
|
2013-05-28 19:34:26 +02:00
|
|
|
xor eax, eax
|
|
|
|
mov ecx, sizeof.ARP_entry/2
|
2013-06-27 01:35:43 +02:00
|
|
|
rep stosw
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
pop edi
|
2019-08-25 20:21:44 +02:00
|
|
|
dec [ARP_entries + 4*edi]
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_del_entry: success\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; arp_ip_to_mac: Translate an IP address to a MAC address. ;
|
|
|
|
; ;
|
|
|
|
; IN: eax = IPv4 address ;
|
|
|
|
; edi = device number * 4 ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = -1 on error ;
|
|
|
|
; eax = -2 when request send ;
|
|
|
|
; eax = first two bytes of mac on success ;
|
|
|
|
; ebx = last four bytes of mac on success ;
|
|
|
|
; edi = unchanged ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
arp_ip_to_mac:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_IP_to_MAC: %u.%u", al, ah
|
2013-05-28 19:34:26 +02:00
|
|
|
rol eax, 16
|
2013-06-10 22:19:12 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, ".%u.%u device*4: %u\n", al, ah, edi
|
2013-05-28 19:34:26 +02:00
|
|
|
rol eax, 16
|
|
|
|
|
|
|
|
cmp eax, 0xffffffff
|
|
|
|
je .broadcast
|
|
|
|
|
|
|
|
;--------------------------------
|
|
|
|
; Try to find the IP in ARP_table
|
|
|
|
|
2019-08-25 20:21:44 +02:00
|
|
|
mov ecx, [ARP_entries + edi]
|
2013-05-28 19:34:26 +02:00
|
|
|
test ecx, ecx
|
|
|
|
jz .not_in_list
|
2013-06-06 07:19:15 +02:00
|
|
|
mov esi, edi
|
2013-06-11 00:26:26 +02:00
|
|
|
imul esi, (sizeof.ARP_entry * ARP_TABLE_SIZE)/4
|
2013-06-06 07:19:15 +02:00
|
|
|
add esi, ARP_table + ARP_entry.IP
|
2013-05-28 19:34:26 +02:00
|
|
|
.scan_loop:
|
|
|
|
cmp [esi], eax
|
|
|
|
je .found_it
|
|
|
|
add esi, sizeof.ARP_entry
|
2013-06-05 02:21:20 +02:00
|
|
|
dec ecx
|
|
|
|
jnz .scan_loop
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.not_in_list:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_IP_to_MAC: preparing for ARP request\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
push eax edi ; save IP for ARP_output_request
|
2013-06-05 02:21:20 +02:00
|
|
|
; Now craft the ARP entry on the stack
|
2013-05-28 19:34:26 +02:00
|
|
|
pushw ARP_REQUEST_TTL ; TTL
|
|
|
|
pushw ARP_AWAITING_RESPONSE ; status
|
|
|
|
pushd 0 ; mac
|
|
|
|
pushw 0
|
2015-12-27 16:37:31 +01:00
|
|
|
pushd eax ; IP
|
2013-05-28 19:34:26 +02:00
|
|
|
mov esi, esp
|
2013-06-05 02:21:20 +02:00
|
|
|
|
|
|
|
; Add it to the list
|
2015-12-27 16:37:31 +01:00
|
|
|
call arp_add_entry
|
2013-06-05 02:21:20 +02:00
|
|
|
|
|
|
|
; Delete the temporary entry
|
2013-05-28 19:34:26 +02:00
|
|
|
add esp, sizeof.ARP_entry ; clear the entry from stack
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
; If we could not add it to the list, give up
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp eax, -1 ; did ARP_add_entry fail?
|
|
|
|
je .full
|
|
|
|
|
2013-06-05 02:21:20 +02:00
|
|
|
;-----------------------------------------------
|
|
|
|
; At this point, we got an ARP entry in the list
|
|
|
|
|
|
|
|
; Now send a request packet on the network
|
|
|
|
pop edi eax ; IP in eax, device number in ebx, for ARP_output_request
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
push esi edi
|
2019-08-25 20:46:47 +02:00
|
|
|
mov ebx, [net_device_list + edi]
|
2015-12-27 16:37:31 +01:00
|
|
|
call arp_output_request
|
2013-05-28 19:34:26 +02:00
|
|
|
pop edi esi
|
|
|
|
.found_it:
|
|
|
|
cmp [esi + ARP_entry.Status], ARP_VALID_MAPPING ; Does it have a MAC assigned?
|
|
|
|
je .valid
|
|
|
|
|
|
|
|
if ARP_BLOCK
|
|
|
|
|
|
|
|
cmp [esi + ARP_entry.Status], ARP_AWAITING_RESPONSE ; Are we waiting for reply from remote end?
|
|
|
|
jne .give_up
|
2013-06-05 02:44:59 +02:00
|
|
|
push esi
|
2013-05-28 19:34:26 +02:00
|
|
|
mov esi, 10 ; wait 10 ms
|
|
|
|
call delay_ms
|
2013-06-05 02:44:59 +02:00
|
|
|
pop esi
|
2013-05-28 19:34:26 +02:00
|
|
|
jmp .found_it ; now check again
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
jmp .give_up
|
|
|
|
|
|
|
|
end if
|
|
|
|
|
|
|
|
.valid:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_IP_to_MAC: found MAC\n"
|
2013-06-05 02:44:59 +02:00
|
|
|
movzx eax, word[esi + ARP_entry.MAC]
|
|
|
|
mov ebx, dword[esi + ARP_entry.MAC + 2]
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.full:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_IP_to_MAC: table is full!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
add esp, 8
|
|
|
|
.give_up:
|
2013-05-28 21:19:23 +02:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ARP_IP_to_MAC: entry has no valid mapping!\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
.broadcast:
|
|
|
|
mov eax, 0x0000ffff
|
|
|
|
mov ebx, 0xffffffff
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-27 16:37:31 +01:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; arp_api: Part of system function 76. ;
|
|
|
|
; ;
|
|
|
|
; IN: bl = subfunction number ;
|
|
|
|
; bh = device number ;
|
|
|
|
; ecx, edx, .. depends on subfunction ;
|
|
|
|
; ;
|
|
|
|
; OUT: depends on subfunction ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
2015-12-27 16:37:31 +01:00
|
|
|
arp_api:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
movzx eax, bh
|
|
|
|
shl eax, 2
|
|
|
|
|
|
|
|
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
|
|
|
|
dd .send_announce ; 6
|
|
|
|
dd .conflicts ; 7
|
|
|
|
.number = ($ - .table) / 4 - 1
|
|
|
|
|
|
|
|
.error:
|
|
|
|
mov eax, -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_tx:
|
2019-08-25 20:21:44 +02:00
|
|
|
mov eax, [ARP_packets_tx + eax]
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.packets_rx:
|
2019-08-25 20:21:44 +02:00
|
|
|
mov eax, [ARP_packets_rx + eax]
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.conflicts:
|
2019-08-25 20:21:44 +02:00
|
|
|
mov eax, [ARP_conflicts + eax]
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.entries:
|
2019-08-25 20:21:44 +02:00
|
|
|
mov eax, [ARP_entries + eax]
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.read:
|
2019-08-25 20:21:44 +02:00
|
|
|
cmp ecx, [ARP_entries + eax]
|
2013-05-28 19:34:26 +02:00
|
|
|
jae .error
|
2013-06-05 02:21:20 +02:00
|
|
|
shr eax, 2
|
|
|
|
imul eax, sizeof.ARP_entry*ARP_TABLE_SIZE
|
|
|
|
add eax, ARP_table
|
2013-05-28 19:34:26 +02:00
|
|
|
; edi = pointer to buffer
|
|
|
|
; ecx = # entry
|
|
|
|
imul ecx, sizeof.ARP_entry
|
2013-06-05 02:21:20 +02:00
|
|
|
lea esi, [eax + ecx]
|
2013-05-28 19:34:26 +02:00
|
|
|
mov ecx, sizeof.ARP_entry/2
|
2013-06-27 01:35:43 +02:00
|
|
|
rep movsw
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.write:
|
|
|
|
; esi = pointer to buffer
|
2013-06-05 02:21:20 +02:00
|
|
|
mov edi, eax
|
2015-12-27 16:37:31 +01:00
|
|
|
call arp_add_entry ; out: eax = entry number, -1 on error
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.remove:
|
|
|
|
; ecx = # entry
|
2019-08-25 20:21:44 +02:00
|
|
|
cmp ecx, [ARP_entries + eax]
|
2013-05-28 19:34:26 +02:00
|
|
|
jae .error
|
|
|
|
imul ecx, sizeof.ARP_entry
|
|
|
|
lea esi, [ARP_table + ecx]
|
2013-06-05 02:21:20 +02:00
|
|
|
mov edi, eax
|
|
|
|
shr edi, 2
|
2015-12-27 16:37:31 +01:00
|
|
|
call arp_del_entry
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.send_announce:
|
2019-08-25 20:46:47 +02:00
|
|
|
mov ebx, [net_device_list + eax]
|
2019-08-25 20:21:44 +02:00
|
|
|
mov eax, [IPv4_address + eax]
|
2015-12-27 16:37:31 +01:00
|
|
|
call arp_output_request ; now send a gratuitous ARP
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|