* mikeOS 16 bit and amd64 baremetal
[mascara-docs.git] / amd64 / bareMetalOS-0.5.2 / baremetal0.5.2 / os / ipv4 / arp.asm
blobdc00457587fbadd07b75651b1d760a9c3c232315
1 ; =============================================================================
2 ; BareMetal -- a 64-bit OS written in Assembly for x86-64 systems
3 ; Copyright (C) 2008-2011 Return Infinity -- see LICENSE.TXT
5 ; ARP (Address Resolution Protocol)
6 ; =============================================================================
8 align 16
9 db 'DEBUG: ARP '
10 align 16
13 ; ARP Incoming Request layout:
14 ; Ethernet header:
15 ; 0-5, Broadcast MAC (0xFFFFFFFFFFFF)
16 ; 6-11, Source MAC
17 ; 12-13, Type ARP (0x0806)
18 ; ARP data:
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)
24 ; 22-27, Sender MAC
25 ; 28-31, Sender IP
26 ; 32-37, Target MAC (0x000000000000)
27 ; 38-41, Target IP
29 ; ARP Outgoing Request layout:
30 ; Ethernet header:
31 ; 0-5, Broadcast MAC (0xFFFFFFFFFFFF)
32 ; 6-11, Source MAC (This host)
33 ; 12-13, Type ARP (0x0806)
34 ; ARP data:
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)
43 ; 38-41, Target IP
45 ; ARP Outgoing Reply layout:
46 ; Ethernet header:
47 ; 0-5, Destination MAC (This host)
48 ; 6-11, Source MAC
49 ; 12-13, Type ARP (0x0806)
50 ; ARP data:
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)
56 ; 22-27, Sender MAC
57 ; 28-31, Sender IP
58 ; 32-37, Target MAC
59 ; 38-41, Target IP
62 ; -----------------------------------------------------------------------------
63 os_arp_request:
65 ret
66 ; -----------------------------------------------------------------------------
69 ; -----------------------------------------------------------------------------
70 ; os_arp_handler -- Handle an incoming ARP packet
71 ; IN: RCX = packet length
72 ; RSI = location of received ARP packet
73 os_arp_handler:
74 push rdi
75 push rsi
76 push rax
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 ; call os_debug_dump_eax
89 cmp eax, [ip] ; Does it match our IP?
90 jne os_arp_handler_end ; If not then we don't need to respond
92 push rsi ; Save the address of the packet
93 mov rdi, rsi
94 add rsi, 0x1C ; Skip to Sender IP
95 mov eax, [rsi]
96 push rax ; Save the Sender IP
97 sub rsi, 0x16 ; Skip back to Source MAC
99 movsd ; Copy destination MAC address
100 movsw
102 push rsi ; Copy source MAC address
103 mov rsi, os_NetMAC
104 movsd
105 movsw
106 pop rsi
108 add rdi, 9
109 mov al, 0x02 ; Change the opcode to a reply
110 stosb
112 push rsi ; Copy source MAC address (again)
113 mov rsi, os_NetMAC
114 movsd
115 movsw
116 pop rsi
118 mov eax, [ip] ; Copy our IP
119 stosd
121 sub rsi, 12 ; Copy destination MAC
122 movsd
123 movsw
125 pop rax ; Restore the Sender IP
126 stosd
128 pop rsi ; Restore the packet address
130 mov cx, 60
131 call os_ethernet_tx_raw ; Send the packet
133 jmp os_arp_handler_end
135 os_arp_handler_reply:
137 jmp os_arp_handler_end
139 os_arp_handler_end:
140 pop rax
141 pop rsi
142 pop rdi
144 ; -----------------------------------------------------------------------------
147 ; =============================================================================
148 ; EOF