ARP
A host that wants to send an IP packet to 192.168.1.42 on its own LAN has a problem: Ethernet delivers frames to MAC addresses, not IP addresses. The Address Resolution Protocol (ARP, RFC 826) answers the question "who has 192.168.1.42? tell me your MAC address".
How ARP works
- The sender checks its ARP cache. If the IP is there, it uses the cached MAC.
- Otherwise it broadcasts an ARP request to
ff:ff:ff:ff:ff:ff: "Who has 192.168.1.42? Tell 192.168.1.20." - Every host on the LAN receives it; only the owner of 192.168.1.42 answers with a unicast ARP reply: "192.168.1.42 is at 00:1a:2b:3c:4d:5e."
- The sender caches the answer (typically for a few minutes) and sends its packet.
Note: ARP only works inside one broadcast domain. For a destination on another network, the host ARPs for its default gateway's IP and sends the frame to the router's MAC. The destination IP in the packet stays the remote one.
The ARP packet
An ARP message for IPv4 over Ethernet is 28 bytes, carried directly in an Ethernet frame with EtherType 0x0806:
| Field | Size | Value for IPv4/Ethernet |
|---|---|---|
| Hardware type | 2 | 1 (Ethernet) |
| Protocol type | 2 | 0x0800 (IPv4) |
| Hardware / protocol address length | 1 + 1 | 6, 4 |
| Operation | 2 | 1 = request, 2 = reply |
| Sender MAC, sender IP | 6 + 4 | |
| Target MAC, target IP | 6 + 4 | target MAC is all zeros in a request |
import socket
import struct
def mac(text):
return bytes.fromhex(text.replace(":", ""))
def arp_packet(op, sender_mac, sender_ip, target_mac, target_ip):
return struct.pack("!HHBBH6s4s6s4s", 1, 0x0800, 6, 4, op,
mac(sender_mac), socket.inet_aton(sender_ip),
mac(target_mac), socket.inet_aton(target_ip))
def parse_arp(data):
htype, ptype, hlen, plen, op, smac, sip, tmac, tip = struct.unpack("!HHBBH6s4s6s4s", data)
kind = {1: "request", 2: "reply"}[op]
return kind, smac.hex(":"), socket.inet_ntoa(sip), tmac.hex(":"), socket.inet_ntoa(tip)
request = arp_packet(1, "3c:22:fb:91:0a:7e", "192.168.1.20", "00:00:00:00:00:00", "192.168.1.42")
print(len(request), "bytes:", request.hex(" "))
kind, smac, sip, tmac, tip = parse_arp(request)
print(f"{kind}: who has {tip}? tell {sip} ({smac})")
reply = arp_packet(2, "00:1a:2b:3c:4d:5e", "192.168.1.42", "3c:22:fb:91:0a:7e", "192.168.1.20")
kind, smac, sip, tmac, tip = parse_arp(reply)
print(f"{kind}: {sip} is at {smac} (to {tip})")28 bytes: 00 01 08 00 06 04 00 01 3c 22 fb 91 0a 7e c0 a8 01 14 00 00 00 00 00 00 c0 a8 01 2a
request: who has 192.168.1.42? tell 192.168.1.20 (3c:22:fb:91:0a:7e)
reply: 192.168.1.42 is at 00:1a:2b:3c:4d:5e (to 192.168.1.20)
socket.inet_aton and socket.inet_ntoa convert between the dotted text form and the 4-byte binary form of an IPv4 address. They are pure conversion functions: no network access is involved.
Simulating an ARP cache
A host keeps answers in a cache with a lifetime. Let's model a small LAN where each host answers ARP requests for its own IP:
LAN = { # who owns which IP on this broadcast domain
"192.168.1.1": "00:1a:2b:3c:4d:01", # the router
"192.168.1.20": "3c:22:fb:91:0a:7e", # us
"192.168.1.42": "00:1a:2b:3c:4d:5e", # a file server
}
class ArpCache:
TTL = 60
def __init__(self, my_ip):
self.my_ip = my_ip
self.entries = {} # ip -> (mac, expires_at)
def resolve(self, ip, now):
entry = self.entries.get(ip)
if entry and entry[1] > now:
print(f"t={now:>3} {ip:<13} cache hit -> {entry[0]}")
return entry[0]
print(f"t={now:>3} {ip:<13} broadcast: who has {ip}? tell {self.my_ip}")
owner = LAN.get(ip)
if owner is None:
print(f" no reply: host unreachable")
return None
self.entries[ip] = (owner, now + self.TTL)
print(f" reply: {ip} is at {owner} (cached until t={now + self.TTL})")
return owner
cache = ArpCache("192.168.1.20")
cache.resolve("192.168.1.42", now=0)
cache.resolve("192.168.1.42", now=5)
cache.resolve("192.168.1.1", now=10)
cache.resolve("192.168.1.99", now=20)
cache.resolve("192.168.1.42", now=75) # expiredt= 0 192.168.1.42 broadcast: who has 192.168.1.42? tell 192.168.1.20
reply: 192.168.1.42 is at 00:1a:2b:3c:4d:5e (cached until t=60)
t= 5 192.168.1.42 cache hit -> 00:1a:2b:3c:4d:5e
t= 10 192.168.1.1 broadcast: who has 192.168.1.1? tell 192.168.1.20
reply: 192.168.1.1 is at 00:1a:2b:3c:4d:01 (cached until t=70)
t= 20 192.168.1.99 broadcast: who has 192.168.1.99? tell 192.168.1.20
no reply: host unreachable
t= 75 192.168.1.42 broadcast: who has 192.168.1.42? tell 192.168.1.20
reply: 192.168.1.42 is at 00:1a:2b:3c:4d:5e (cached until t=135)
Local or remote? Choosing whom to ARP for
Before ARP, the host decides whether the destination is on its own subnet. If not, it resolves the gateway instead:
import ipaddress
me = ipaddress.ip_interface("192.168.1.20/24")
gateway = "192.168.1.1"
def next_hop(dest):
if ipaddress.ip_address(dest) in me.network:
return dest
return gateway
for dest in ["192.168.1.42", "8.8.8.8", "10.20.30.40"]:
hop = next_hop(dest)
print(f"packet to {dest:<13} -> ARP for {hop:<13} -> frame to that MAC, IP dst stays {dest}")packet to 192.168.1.42 -> ARP for 192.168.1.42 -> frame to that MAC, IP dst stays 192.168.1.42
packet to 8.8.8.8 -> ARP for 192.168.1.1 -> frame to that MAC, IP dst stays 8.8.8.8
packet to 10.20.30.40 -> ARP for 192.168.1.1 -> frame to that MAC, IP dst stays 10.20.30.40
Viewing the ARP cache
ip neigh showSample output:
192.168.1.1 dev wlp2s0 lladdr 00:1a:2b:3c:4d:01 REACHABLE
192.168.1.42 dev wlp2s0 lladdr 00:1a:2b:3c:4d:5e STALE
192.168.1.99 dev wlp2s0 FAILEDOn Windows and macOS, arp -a shows the same table.
Gratuitous ARP and ARP spoofing
A gratuitous ARP is an unsolicited reply in which a host announces its own IP-to-MAC mapping, for example after booting or when a backup server takes over a shared IP. Other hosts update their caches.
ARP has no authentication: anyone can send a reply saying "192.168.1.1 is at my MAC". Hosts believe it, and send their traffic for the router to the attacker, who can read or modify it before forwarding (a man-in-the-middle attack known as ARP spoofing or ARP poisoning). Defences include Dynamic ARP Inspection on switches, static ARP entries for critical hosts, and above all encrypting traffic (TLS) so intercepted packets are useless.
Tip: IPv6 doesn't use ARP. It uses Neighbor Discovery (NDP), which does the same job with ICMPv6 messages sent to multicast addresses instead of broadcasts.
Go further: ARP Spoofing: Mechanism, Detection and Fix poisons a simulated LAN's caches and shows how dynamic ARP inspection stops it.
Test yourself
What does ARP map?
- Domain names to IP addresses
- IP addresses to MAC addresses
- MAC addresses to switch ports
- Ports to programs
Where is an ARP request sent?
- To the DNS server
- To the default gateway only
- To the Ethernet broadcast address ff:ff:ff:ff:ff:ff
- To the destination's IP on the Internet
A host sends a packet to 8.8.8.8, which is not on its subnet. Whose MAC address does it ARP for?
- 8.8.8.8's
- Its default gateway's
- The DNS server's
- Its own