1 ; =============================================================================
2 ; BareMetal -- a 64-bit OS written in Assembly for x86-64 systems
3 ; Copyright (C) 2008-2012 Return Infinity -- see LICENSE.TXT
5 ; ARP (Address Resolution Protocol)
6 ; =============================================================================
13 ; ARP Incoming Request layout:
15 ; 0-5, Broadcast MAC (0xFFFFFFFFFFFF)
17 ; 12-13, Type ARP (0x0806)
19 ; 14-15, Hardware type (0x0001 Ethernet)
20 ; 16-17, Protocol type (0x0800 IP)
21 ; 18, Hardware size (0x06)
22 ; 19, Protocol size (0x04)
23 ; 20-21, Opcode (0x0001 Request)
26 ; 32-37, Target MAC (0x000000000000)
29 ; ARP Outgoing Request layout:
31 ; 0-5, Broadcast MAC (0xFFFFFFFFFFFF)
32 ; 6-11, Source MAC (This host)
33 ; 12-13, Type ARP (0x0806)
35 ; 14-15, Hardware type (0x0001 Ethernet)
36 ; 16-17, Protocol type (0x0800 IP)
37 ; 18, Hardware size (0x06)
38 ; 19, Protocol size (0x04)
39 ; 20-21, Opcode (0x0001 Request)
40 ; 22-27, Sender MAC (This host)
41 ; 28-31, Sender IP (This host)
42 ; 32-37, Target MAC (0x000000000000)
45 ; ARP Outgoing Reply layout:
47 ; 0-5, Destination MAC (This host)
49 ; 12-13, Type ARP (0x0806)
51 ; 14-15, Hardware type (0x0001 Ethernet)
52 ; 16-17, Protocol type (0x0800 IP)
53 ; 18, Hardware size (0x06)
54 ; 19, Protocol size (0x04)
55 ; 20-21, Opcode (0x0002 Reply)
62 ; -----------------------------------------------------------------------------
66 ; -----------------------------------------------------------------------------
69 ; -----------------------------------------------------------------------------
70 ; os_arp_handler -- Handle an incoming ARP packet; Called by Network interrupt
71 ; IN: RCX = packet length
72 ; RSI = location of received ARP packet
78 mov ax, [rsi
+0x14] ; Grab the Opcode
79 xchg al, ah ; Convert to proper endianess
80 cmp ax, 0x0001 ; Request
81 je os_arp_handler_request
; Respond if the ARP packet is for us
82 cmp ax, 0x0002 ; Reply
83 je os_arp_handler_reply
; Add to our local ARP table
84 jmp os_arp_handler_end
; Bail out
86 os_arp_handler_request:
87 mov eax, [rsi
+0x26] ; Grab the target IP address
88 cmp eax, [ip
] ; Does it match our IP?
89 jne os_arp_handler_end
; If not then we don't need to respond
91 push rsi
; Save the address of the packet
93 add rsi
, 0x1C ; Skip to Sender IP
95 push rax
; Save the Sender IP
96 sub rsi
, 0x16 ; Skip back to Source MAC
97 movsd ; Copy destination MAC address
99 push rsi
; Copy source MAC address
105 mov al, 0x02 ; Change the opcode to a reply
107 push rsi
; Copy source MAC address (again)
112 mov eax, [ip
] ; Copy our IP
114 sub rsi
, 12 ; Copy destination MAC
117 pop rax
; Restore the Sender IP
119 pop rsi
; Restore the packet address
121 call os_ethernet_tx_raw
; Send the packet
122 jmp os_arp_handler_end
124 os_arp_handler_reply:
125 ; If this was a reply to a request that this computer sent out then this should be added to the local ARP table
126 jmp os_arp_handler_end
133 ; -----------------------------------------------------------------------------
136 ; =============================================================================