Sub topic 8
ARP: finding the MAC address for an IP address
This chapter assumes you know what an IPv4 address is, how a MAC address is written, and what an Ethernet frame header contains. It also assumes you can run commands in a terminal.
The problem ARP solves
Applications work with IP addresses. Ethernet delivers frames by MAC address. Before a host can send an IPv4 packet to a neighbor on the same LAN, it needs the neighbor's MAC address to put in the frame's destination field.
The Address Resolution Protocol (ARP) provides that mapping. It asks the LAN who owns an IPv4 address, and the owner answers with its MAC address. ARP is defined in RFC 826 and applies to IPv4 only. IPv6 uses Neighbor Discovery, which a later chapter covers.
How a lookup works
Suppose host A (192.168.1.10) wants to send to host B (192.168.1.20) on the same subnet.
- A checks its ARP cache for 192.168.1.20. If an entry exists, A uses it and skips the rest.
- If there is no entry, A broadcasts an ARP request. The Ethernet destination is
ff:ff:ff:ff:ff:ffand the EtherType is0x0806. - Every host on the LAN receives the request. Only the host that owns 192.168.1.20 replies.
- B sends an ARP reply as a unicast frame straight to A's MAC address.
- A stores the mapping in its cache and sends the queued IP packet.
What is in an ARP message
An ARP message sits directly inside an Ethernet frame. It has no IP header. The main fields are:
| Field | Meaning in a request | Meaning in a reply |
|---|---|---|
| Operation | 1 (request) | 2 (reply) |
| Sender MAC and IP | The asker's addresses | The answerer's addresses |
| Target MAC | All zeros (unknown) | The asker's MAC |
| Target IP | The IP being resolved | The asker's IP |
The request also teaches the receiver something. Host B can learn A's IP-to-MAC mapping from the sender fields and cache it. This saves B an ARP request when it answers.
The ARP cache
Hosts keep resolved mappings in an ARP cache, also called the neighbor table. Entries expire after a short time, often tens of seconds to a few minutes, depending on the operating system. Expiry lets the host notice when an IP address moves to different hardware.
On Linux, view the cache with:
ip neigh showEach line shows an IP address, the interface, the MAC address (lladdr), and a state such as REACHABLE, STALE, or FAILED. A FAILED entry means nobody answered the ARP requests.
Off-subnet destinations
A host only ARPs for addresses on its own subnet. If the destination is on another network, the host ARPs for its default gateway's IP address and sends the frame to the router's MAC. The IP destination stays the remote host's address. The next chapter covers this in detail.
Gratuitous ARP
A gratuitous ARP is an ARP message a host sends about its own address, with the sender and target IP set to the same value. Hosts use it to announce a new address, to update neighbors' caches after a failover, or to detect an address conflict. If another host replies, two devices claim the same IP.
Common errors
- Incomplete or failed entries. The target is offline, on a different VLAN, or blocking ARP. Check the cable, VLAN, and subnet mask.
- Wrong subnet mask. A host that thinks a remote address is local ARPs for it and never gets an answer.
- Duplicate IP addresses. Two hosts answer the same request, and the cache flips between two MACs. Intermittent connectivity is the typical symptom.
- Stale entries after hardware swaps. The old MAC stays cached until it expires. Clear it with
sudo ip neigh flush dev eth0.
Exercise
Host A (192.168.1.10/24) wants to send a packet to 8.8.8.8, and its default gateway is 192.168.1.1. Which IP address does A send an ARP request for, and what is the destination MAC of the ARP request frame?
Show answer
A sends an ARP request for 192.168.1.1, the gateway, because 8.8.8.8 is outside its subnet. The request frame's destination MAC is the broadcast address ff:ff:ff:ff:ff:ff. After the reply, A sends the IP packet in a frame addressed to the gateway's MAC, with the destination IP still 8.8.8.8.