* mikeOS 16 bit and amd64 baremetal
[mascara-docs.git] / amd64 / bareMetalOS-0.5.3 / os / ipv4 / arp.asm
blob96ca9eb8db9c19436e039e59b64c7cb471b2da19
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 ; =============================================================================
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; Called by Network interrupt
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 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
92 mov rdi, rsi
93 add rsi, 0x1C ; Skip to Sender IP
94 mov eax, [rsi]
95 push rax ; Save the Sender IP
96 sub rsi, 0x16 ; Skip back to Source MAC
97 movsd ; Copy destination MAC address
98 movsw
99 push rsi ; Copy source MAC address
100 mov rsi, os_NetMAC
101 movsd
102 movsw
103 pop rsi
104 add rdi, 9
105 mov al, 0x02 ; Change the opcode to a reply
106 stosb
107 push rsi ; Copy source MAC address (again)
108 mov rsi, os_NetMAC
109 movsd
110 movsw
111 pop rsi
112 mov eax, [ip] ; Copy our IP
113 stosd
114 sub rsi, 12 ; Copy destination MAC
115 movsd
116 movsw
117 pop rax ; Restore the Sender IP
118 stosd
119 pop rsi ; Restore the packet address
120 mov cx, 60
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
128 os_arp_handler_end:
129 pop rax
130 pop rsi
131 pop rdi
133 ; -----------------------------------------------------------------------------
136 ; =============================================================================
137 ; EOF