Sub topic 10
Finding the next MAC address with ARP
This chapter assumes you know what encapsulation is and how an Ethernet frame wraps an IP packet. It also assumes you can read an IPv4 address and a MAC address, and that you can run commands in a Linux terminal. The ARP examples use IPv4. IPv6 does the same job with a different protocol, covered near the end.
The problem ARP solves
An IP packet carries a destination IP address. An Ethernet frame needs a destination MAC address, because the network card and the switch do not understand IP. When a host builds a frame, it knows the IP address of the next device to reach, but it may not know that device's MAC address.
The Address Resolution Protocol (ARP) fills that gap. ARP is a protocol that asks, "Who owns this IPv4 address on my local network? Tell me your MAC address." The answer is stored for a while so the question is not repeated for every packet.
ARP sits between the network layer and the link layer. It has its own EtherType, 0x0806, so a receiver can tell an ARP frame from an IPv4 frame (0x0800). ARP packets are not carried inside IP. They ride directly in Ethernet frames, and they never cross a router.
Deciding whom to ask
A host does not always ARP for the final destination. Before sending, it compares the destination IP with its own address and subnet mask. This gives two cases:
- The destination is on the same subnet. The host asks for the destination's MAC address and sends the frame straight to it.
- The destination is on another subnet. The host asks for the MAC address of its default gateway, the local router, and sends the frame there.
The second case shows why the IP destination and the MAC destination can differ. Suppose host 192.168.1.10/24 sends to 203.0.113.5. The IP packet keeps 203.0.113.5 as its destination. The frame's destination MAC is the gateway's, because the gateway is the next hop on this link.
The request and reply exchange
ARP uses two messages.
- ARP request. The sender broadcasts to the Ethernet address
ff:ff:ff:ff:ff:ff. Every device on the segment receives it. The message says "Who has 192.168.1.1? Tell 192.168.1.10." - ARP reply. Only the device that owns
192.168.1.1answers. It sends a unicast frame straight back to the requester, saying "192.168.1.1 is at aa:bb:cc:dd:ee:ff."
The receiver of a request also learns something. The request carries the sender's IP and MAC address, so the gateway can store 192.168.1.10 as 11:22:33:44:55:66 without asking. That is why the reply can be unicast and why the second direction of a conversation rarely needs its own ARP exchange.
The ARP packet layout
For Ethernet and IPv4, an ARP message is 28 bytes. It follows the Ethernet header.
| Field | Size | Value for Ethernet and IPv4 |
|---|---|---|
| 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 | Address of the sender |
| Sender IP | 4 bytes | Address of the sender |
| Target MAC | 6 bytes | All zeros in a request, the requester's MAC in a reply |
| Target IP | 4 bytes | The address being asked about |
The frame is 14 bytes of Ethernet header plus 28 bytes of ARP, so 42 bytes. Ethernet requires at least 60 bytes before the frame check sequence, so the network card pads the rest with zeros. A capture taken on the sending host often shows 42 bytes because the padding is added after the capture point.
Building and reading ARP frames in Python
The script below builds a request and a reply by hand, then parses both. It follows the same approach as the encapsulation example earlier in this tutorial: pack the fields in network byte order with struct.
import socket
import struct
BROADCAST = b"\xff" * 6
ZERO_MAC = bytes(6)
a_mac = bytes.fromhex("112233445566")
a_ip = socket.inet_aton("192.168.1.10")
gw_mac = bytes.fromhex("aabbccddeeff")
gw_ip = socket.inet_aton("192.168.1.1")
def fmt_mac(raw):
return ":".join(f"{b:02x}" for b in raw)
def arp_frame(op, eth_dst, eth_src, s_mac, s_ip, t_mac, t_ip):
# Ethernet II header: destination, source, EtherType 0x0806 (ARP)
eth = struct.pack("!6s6sH", eth_dst, eth_src, 0x0806)
# ARP body: hardware type 1, protocol type 0x0800, lengths 6 and 4
arp = struct.pack("!HHBBH6s4s6s4s", 1, 0x0800, 6, 4, op,
s_mac, s_ip, t_mac, t_ip)
return eth + arp
def parse(frame):
eth_dst, eth_src, ethertype = struct.unpack("!6s6sH", frame[:14])
assert ethertype == 0x0806, "not an ARP frame"
(_, _, _, _, op, s_mac, s_ip, _, t_ip) = struct.unpack(
"!HHBBH6s4s6s4s", frame[14:42])
if op == 1:
return (f"request: who has {socket.inet_ntoa(t_ip)}? "
f"tell {socket.inet_ntoa(s_ip)}")
return f"reply: {socket.inet_ntoa(s_ip)} is at {fmt_mac(s_mac)}"
# Host A asks for the gateway. The target MAC is unknown, so it is zero.
request = arp_frame(1, BROADCAST, a_mac, a_mac, a_ip, ZERO_MAC, gw_ip)
# The gateway answers A directly (unicast).
reply = arp_frame(2, a_mac, gw_mac, gw_mac, gw_ip, a_mac, a_ip)
print(len(request), "bytes")
print(parse(request))
print(parse(reply))Running it prints:
42 bytes
request: who has 192.168.1.1? tell 192.168.1.10
reply: 192.168.1.1 is at aa:bb:cc:dd:ee:ffNotice that the Ethernet destination of the request is broadcast, while the ARP target MAC inside it is zero. These are two separate fields at two layers. The first tells the switch where to send the frame. The second is simply "unknown yet".
The ARP cache
Hosts store answers in an ARP cache, also called a neighbor table, so they do not ask before every packet. Entries expire because MAC addresses can change, for example when a network card is replaced or an IP address moves to another machine.
On Linux, view the cache with this command:
ip neigh showEach line shows an IP address, the interface, the MAC address and a state. The states you will see most often are:
REACHABLE: the mapping was confirmed recently.STALE: the entry is old but still used. The kernel rechecks it when traffic next uses it.DELAYandPROBE: the kernel is verifying a stale entry.INCOMPLETE: a request was sent and no reply has arrived yet.FAILED: no reply came after the retries.
To watch the exchange happen, capture ARP traffic in one terminal and trigger a lookup in another. Replace eth0 with your interface name and the address with a neighbor on your LAN.
sudo tcpdump -i eth0 -e -nn arp
sudo arping -I eth0 -c 3 192.168.1.1The capture shows lines in this form, which map directly to the fields above:
11:22:33:44:55:66 > ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42: Request who-has 192.168.1.1 tell 192.168.1.10, length 28
aa:bb:cc:dd:ee:ff > 11:22:33:44:55:66, ethertype ARP (0x0806), length 42: Reply 192.168.1.1 is-at aa:bb:cc:dd:ee:ff, length 28The outer length 42 is the whole frame and the inner length 28 is the ARP message.
What goes wrong with ARP
ARP has no authentication, and that causes most of its failure modes.
- ARP spoofing. Any host on the segment can send a reply claiming to own another host's IP address. Receivers usually accept it and update their cache. The attacker then receives traffic meant for the victim, which enables interception. Defenses include dynamic ARP inspection on managed switches, static entries for critical hosts such as gateways, and encrypted protocols so intercepted traffic is useless.
- Duplicate IP addresses. If two hosts hold the same IP, they answer the same request, and the cache flips between two MAC addresses. Connections fail intermittently. Operating systems often detect this by sending a probe for their own address when they configure it.
- Gratuitous ARP. A host can announce its own mapping without being asked, by sending a request or reply for its own IP. Failover systems use it so that when a virtual IP moves to a standby machine, neighbors update their caches at once instead of waiting for entries to expire.
- Proxy ARP. A router answers requests on behalf of addresses behind it. This can make misconfigured subnet masks appear to work, and it hides routing problems.
- Stale entries after a change. If a device is replaced but keeps its IP, peers may send frames to the old MAC address until the entry expires. Clearing the entry with
sudo ip neigh flush dev eth0forces a fresh lookup. - Broadcast load. Every request reaches every device on the segment. Very large flat Layer 2 networks can produce a lot of ARP chatter, which is one reason networks are split into subnets.
ARP and IPv6
IPv6 has no ARP. It uses Neighbor Discovery, which sends Neighbor Solicitation and Neighbor Advertisement messages inside ICMPv6. The idea is the same: map a next-hop IP address to a link-layer address. The differences are that the messages travel as IP packets, and solicitations go to a multicast group instead of a broadcast, so unrelated hosts are not interrupted. On Linux, ip neigh show lists both ARP and Neighbor Discovery entries.
Back to the bigger picture
Within the end-to-end story of this tutorial, ARP explains the "changes at every hop" part. At each link, the sender needs a destination MAC address for the next device, and ARP supplies it. The source and destination IP addresses stay the same. The MAC addresses are replaced at every router, using a fresh ARP lookup on each new link.
Exercise
Host 192.168.1.10/24 with default gateway 192.168.1.1 sends a packet to 203.0.113.5. For which IP address does the host send an ARP request, and what are the destination IP address and destination MAC address in the resulting frame?
Show answer
The host sends an ARP request for 192.168.1.1, the gateway, because 203.0.113.5 is outside its subnet. The frame carries destination IP 203.0.113.5 and destination MAC equal to the gateway's MAC address from the ARP reply.
Exercise
In an ARP request, the Ethernet destination is ff:ff:ff:ff:ff:ff and the ARP target MAC is all zeros. Why are these two values different, and why does the reply not use broadcast?
Show answer
The Ethernet destination is the link-layer address the switch uses to deliver the frame, and broadcast makes every device receive it. The ARP target MAC is the answer being requested, so it is unknown and set to zero. The reply is unicast because the request already told the target the requester's MAC address.
Exercise
A failover pair moves a virtual IP to a standby server, but clients keep sending traffic to the failed server for several minutes. What ARP behavior explains this, and what does the standby do to fix it?
Show answer
Clients hold a cached mapping from the virtual IP to the failed server's MAC address until the entry expires. The standby sends a gratuitous ARP announcing the virtual IP with its own MAC address, which makes neighbors update their caches immediately.