Sub topic 8
ARP: finding the MAC address for an IP address
This chapter assumes you know what an IP address and a subnet mask are. It also assumes you can read hexadecimal and run commands in a terminal. You should already know the Ethernet frame layout and the difference between unicast and broadcast destinations from earlier chapters.
The problem ARP solves
Applications work with IP addresses. Ethernet and Wi-Fi deliver frames using MAC addresses. Before a host can send an IPv4 packet to a neighbor, it has to put that neighbor's MAC address in the frame's destination field. The host knows only the IP address, so it needs a way to translate.
The Address Resolution Protocol (ARP, RFC 826) does this translation for IPv4 on a single LAN. A host broadcasts a question ("who has this IP address?"). The owner of that address answers with its MAC address. The asker stores the answer for later use.
ARP is not a routing protocol, and it never crosses a router. Its messages stay inside one broadcast domain.
Which address does the host ask about?
A host never ARPs for an address it cannot reach directly. It first compares the destination IP with its own address and subnet mask.
- If the destination is in the same subnet, the host ARPs for the destination itself.
- If the destination is in another subnet, the host ARPs for its default gateway and sends the frame to the gateway's MAC address. The IP packet still carries the final destination's IP address.
A common beginner error is to think ARP resolves the address of a remote web server. It resolves only the next hop on your own link.
The ARP request and reply
ARP is not carried inside IP. It has its own EtherType, 0x0806, and rides directly in an Ethernet frame. The ARP message for IPv4 over Ethernet is 28 bytes.
| Field | Size | Value for IPv4 over Ethernet |
|---|---|---|
| Hardware type | 2 bytes | 1 (Ethernet) |
| Protocol type | 2 bytes | 0x0800 (IPv4) |
| Hardware address length | 1 byte | 6 |
| Protocol address length | 1 byte | 4 |
| Operation | 2 bytes | 1 = request, 2 = reply |
| Sender MAC | 6 bytes | MAC of the sender |
| Sender IP | 4 bytes | IP of the sender |
| Target MAC | 6 bytes | All zeros in a request, the asker's MAC in a reply |
| Target IP | 4 bytes | The IP being resolved |
In a request, the Ethernet destination is the broadcast address ff:ff:ff:ff:ff:ff, so every device on the LAN receives it. The target MAC field inside the ARP message is zeros because that is the value being asked for. In a reply, the Ethernet destination is the asker's MAC address (unicast), and the sender fields carry the answer.
Two details matter in practice.
- Host B learns A's mapping from the request itself, because the request contains A's sender MAC and sender IP. B normally adds or refreshes that entry in its own cache, so it does not need to send a separate ARP request when it replies to A's traffic.
- Other hosts such as C ignore the request. Many implementations do not create a cache entry for a sender when they are not the target, which limits cache pollution.
Building and parsing an ARP packet
The following program builds a complete ARP request inside an Ethernet frame and then parses it back. It uses only the standard library and sends nothing on the network.
import socket
import struct
def mac_bytes(mac: str) -> bytes:
return bytes.fromhex(mac.replace(':', '').replace('-', ''))
def mac_str(b: bytes) -> str:
return ':'.join(f'{x:02x}' for x in b)
def build_arp_request(src_mac: str, src_ip: str, target_ip: str) -> bytes:
broadcast = mac_bytes('ff:ff:ff:ff:ff:ff')
eth = broadcast + mac_bytes(src_mac) + struct.pack('!H', 0x0806)
arp = struct.pack(
'!HHBBH6s4s6s4s',
1, # hardware type: Ethernet
0x0800, # protocol type: IPv4
6, # hardware address length
4, # protocol address length
1, # operation: request
mac_bytes(src_mac),
socket.inet_aton(src_ip),
b'\x00' * 6, # target MAC: unknown
socket.inet_aton(target_ip),
)
return eth + arp
def parse_arp(frame: bytes) -> dict:
ethertype = struct.unpack('!H', frame[12:14])[0]
if ethertype != 0x0806:
raise ValueError('not an ARP frame')
htype, ptype, hlen, plen, op, smac, sip, tmac, tip = struct.unpack(
'!HHBBH6s4s6s4s', frame[14:42]
)
return {
'operation': {1: 'request', 2: 'reply'}.get(op, op),
'sender_mac': mac_str(smac),
'sender_ip': socket.inet_ntoa(sip),
'target_mac': mac_str(tmac),
'target_ip': socket.inet_ntoa(tip),
}
frame = build_arp_request('02:00:00:00:00:01', '192.168.1.10', '192.168.1.20')
print(len(frame), 'bytes before padding')
print(parse_arp(frame))The frame is 42 bytes: 14 for the Ethernet header and 28 for ARP. A real network card pads it with zeros to the 60-byte minimum (64 with the frame check sequence). The padding is why captured ARP frames often show trailing zero bytes.
The ARP cache
Sending a broadcast for every packet would waste bandwidth, so each host keeps an ARP cache (also called the neighbor table). Entries expire, because a device can change its IP address or MAC address, or it can leave the network.
On Linux the cache is part of the kernel's neighbor subsystem, and each entry has a state.
| State | Meaning |
|---|---|
| INCOMPLETE | A request was sent and no reply has arrived yet |
| REACHABLE | The mapping was confirmed recently |
| STALE | The confirmation is old. The entry is still used, but it is re-checked when traffic needs it |
| DELAY / PROBE | The kernel is verifying a stale entry with unicast probes |
| FAILED | No reply arrived, so the host reports the destination as unreachable |
| PERMANENT | A static entry that was added by hand and never expires |
The default "reachable" time on Linux is randomized around 30 seconds, but it is tunable, and other operating systems use different values. Do not rely on a specific timeout in a design.
If ARP fails, the symptom is usually "destination host unreachable" from the local machine, even though the other host is powered on. In that case, the IP packet was never sent, because the host could not build the Ethernet frame.
Commands for working with ARP
# Show the neighbor table with states
ip neigh show
# Ask for one address and see the reply (needs the iputils-arping package on some systems)
sudo arping -I eth0 -c 3 192.168.1.20
# Watch ARP traffic only
sudo tcpdump -n -e -i eth0 arp
# Add and remove entries (requires root)
sudo ip neigh add 192.168.1.50 lladdr 02:00:00:00:00:50 dev eth0 nud permanent
sudo ip neigh del 192.168.1.50 dev eth0Replace eth0 with your interface name, which you can find with ip link show. In the tcpdump -e output, look for Request who-has 192.168.1.20 tell 192.168.1.10 followed by Reply 192.168.1.20 is-at 02:.... On Windows and macOS, arp -a shows the cache.
Special ARP messages
Not every ARP packet is a simple lookup.
- Gratuitous ARP. A host announces its own mapping without being asked. The sender IP and target IP are both its own address. It is used after an address change, at boot, and by failover systems that move an IP address to a standby machine. Neighbors update their caches with the new MAC address.
- ARP probe. Before using an address, a host sends a request with sender IP
0.0.0.0and the candidate address as the target. A reply means the address is already taken (RFC 5227 address conflict detection). Using0.0.0.0keeps the probe from poisoning other caches. - Proxy ARP. A router answers ARP requests on behalf of hosts in another network, using its own MAC address. Hosts then behave as if the remote addresses were on-link. It can rescue badly configured networks, but it hides routing problems and increases ARP traffic.
Common errors
| Symptom | Likely cause |
|---|---|
| Entry shows `FAILED` or `INCOMPLETE` | The target is offline, on another VLAN, or filtering ARP |
| Two hosts get intermittent connectivity | Duplicate IP address. The cache flips between two MAC addresses |
| Local ping works, remote does not | The default gateway's ARP entry is missing, or the gateway address is wrong |
| Wrong subnet mask | Host ARPs for a remote address that nobody on the LAN owns, or sends local traffic to the gateway |
| Traffic goes to the wrong device after a failover | No gratuitous ARP was sent, so neighbors keep the old MAC until the entry expires |
Limits to remember
- ARP applies to IPv4 only. IPv6 does the same job with Neighbor Discovery, which uses ICMPv6 and multicast instead of broadcast.
- ARP works only within a broadcast domain. VLAN boundaries and routers stop it.
- The cache is a performance optimization and a trust decision. A stale or forged entry misdirects frames, even when IP routing is correct.
Exercise
Host 10.0.0.5/24 wants to send a packet to 8.8.8.8. Its default gateway is 10.0.0.1. For which IP address does the host send an ARP request, and what is the destination MAC address of the resulting data frame?
Show answer
The host sends an ARP request for 10.0.0.1, the default gateway, because 8.8.8.8 is outside the 10.0.0.0/24 subnet. The data frame's destination MAC address is the gateway's MAC address from the ARP reply, while the IP destination in the packet remains 8.8.8.8.
Exercise
In an ARP request, what are the Ethernet destination address and the target MAC field inside the ARP message, and why?
Show answer
The Ethernet destination is the broadcast address ff:ff:ff:ff:ff:ff, so that every device on the LAN receives the question. The target MAC field is all zeros because it holds the unknown value being requested.
Exercise
A failover pair moves the shared address 192.168.1.100 from server A to server B. Clients keep sending traffic to server A for several minutes. What was likely missing, and what would fix it?
Show answer
Server B did not announce the move, so clients kept the old IP-to-MAC mapping in their ARP caches until the entries expired. Having B send a gratuitous ARP for 192.168.1.100 after taking over makes neighbors update their caches immediately.