Sub topic 7
Rewriting the Layer 2 frame: ARP and neighbor discovery
This chapter assumes you know what an IP address and a subnet are, how a longest prefix match picks a route, and what a next hop is. It also assumes you know that an Ethernet frame carries source and destination MAC addresses. You should be comfortable reading terminal output.
Why the router must rewrite the frame
Once a router has chosen a route, it knows the next-hop IP address and the outgoing interface. That is not enough to send the packet. Ethernet delivers frames by MAC address, not by IP address. The router must put the correct destination MAC address on the frame before it leaves.
Every router hop builds a new Layer 2 frame. The IP packet inside stays the same, apart from the TTL and the header checksum. The old frame header is discarded when the frame arrives, and a new one is created on the way out.
The new frame header has these fields:
- Source MAC: the MAC address of the router's outgoing interface.
- Destination MAC: the MAC address of the next hop, or of the final host if the destination is on a directly connected network.
- EtherType:
0x0800for IPv4 or0x86DDfor IPv6.
The IP source and destination addresses do not change. The MAC addresses change at every hop.
Which IP address do you resolve?
The router resolves the next-hop address, not the packet's destination address. There are two cases.
| Route type | Address the router resolves | Resulting destination MAC |
|---|---|---|
| Directly connected network | The packet's destination IP | The destination host's MAC |
| Route through a next hop | The next-hop IP | The next-hop router's MAC |
A common mistake is to expect the destination MAC to belong to the final host when the packet crosses a router. It belongs to the final host only on the last hop.
ARP for IPv4
The Address Resolution Protocol (ARP) maps an IPv4 address to a MAC address on one Layer 2 network. It works like this:
- The router needs the MAC address for
192.0.2.2on interfaceeth0. - It checks its ARP cache. If an entry exists, it uses it.
- Otherwise it broadcasts an ARP request to
ff:ff:ff:ff:ff:ff, asking who has192.0.2.2. - Every device on the segment receives the request. Only the owner of that address answers.
- The owner sends an ARP reply directly (unicast) to the router, giving its MAC address.
- The router stores the mapping in its ARP cache and sends the frame.
Entries in the cache expire. The timeout varies by implementation, and values from tens of seconds to several minutes are common. Linux marks entries stale after a reachability timeout and checks them again when they are used.
What happens while the router waits
When the router has no entry, the first packet cannot be sent immediately. Implementations handle this differently. Some queue a small number of packets while ARP runs, and some drop the first packet. This is why the first ping to a new host is sometimes lost, or shows a higher round-trip time than the later ones.
If nobody answers, the router retries a few times and then gives up. It typically sends an ICMP "Destination Host Unreachable" message back to the source. On Linux, the neighbor entry then shows FAILED or INCOMPLETE.
Neighbor discovery for IPv6
IPv6 has no ARP. It uses Neighbor Discovery (ND), which is part of ICMPv6. The idea is the same, but the mechanics differ.
- The request is a Neighbor Solicitation (ICMPv6 type 135).
- The answer is a Neighbor Advertisement (ICMPv6 type 136).
- The solicitation is not a broadcast. IPv6 has no broadcast. It goes to a solicited-node multicast address derived from the last 24 bits of the target address, in the form
ff02::1:ffXX:XXXX. - Only hosts whose addresses share those last 24 bits listen to that group, so most hosts on the segment are never disturbed.
The router keeps the result in its neighbor cache. ND also tracks reachability with states such as REACHABLE, STALE, DELAY, and PROBE.
| Feature | ARP (IPv4) | Neighbor Discovery (IPv6) |
|---|---|---|
| Carried in | Its own Ethernet protocol | ICMPv6 |
| Request message | ARP request | Neighbor Solicitation |
| Reply message | ARP reply | Neighbor Advertisement |
| Request delivered by | Broadcast | Solicited-node multicast |
| Cache name | ARP cache | Neighbor cache |
Inspecting the cache
On Linux, one command shows both IPv4 and IPv6 neighbors.
# Show all neighbor entries (ARP and ND)
ip neigh show
# Show only IPv6 neighbors
ip -6 neigh show
# Show entries for one interface
ip neigh show dev eth0
# Remove a possibly bad entry so it is resolved again
sudo ip neigh flush to 192.0.2.2 dev eth0A typical line looks like this:
192.0.2.2 dev eth0 lladdr 02:00:5e:10:00:02 REACHABLEThe states you will see most often are:
REACHABLE: the entry was confirmed recently.STALE: the entry is old. It is still used, but is checked again on next use.INCOMPLETE: a request was sent and no answer has arrived yet.FAILED: resolution did not succeed.
Modeling the rewrite
The following Python code models a router's last step. It looks up the next hop's MAC address in a neighbor table and builds the new frame header. It does not send real traffic.
neighbors = {
"192.0.2.2": "02:00:5e:10:00:02",
"192.0.2.3": "02:00:5e:10:00:03",
}
ROUTER_MAC = {"eth0": "02:00:5e:10:00:01"}
def rewrite_frame(out_if, next_hop_ip):
"""Return the new Layer 2 header, or None if ARP must run first."""
dst_mac = neighbors.get(next_hop_ip)
if dst_mac is None:
# No cache entry: the router would send an ARP request here.
return None
return {
"src_mac": ROUTER_MAC[out_if],
"dst_mac": dst_mac,
"ethertype": "0x0800",
}
print(rewrite_frame("eth0", "192.0.2.2"))
print(rewrite_frame("eth0", "192.0.2.9")) # None: needs ARP firstThe first call returns a header dictionary. The second returns None because 192.0.2.9 is not in the table.
Related behavior and edge cases
Proxy ARP
With proxy ARP, a router answers ARP requests on behalf of hosts that are on other networks. The requesting host believes the destination is local. It sends the frame to the router's MAC address, and the router forwards the packet. This can rescue a host with a wrong subnet mask. It also makes a network harder to debug, because hosts ARP for addresses that are not on their segment. Many networks disable it.
Gratuitous ARP
A gratuitous ARP is an ARP message a device sends about its own address, without being asked. Devices use it to announce a new address, to detect duplicates, and to update caches after a failover. Redundant gateway setups use it so that hosts learn the new MAC address quickly when a backup takes over.
Stale entries after a change
If a host's MAC address changes, for example after a network card is replaced, routers keep the old mapping until it expires. Traffic goes to the old MAC address and is lost. Clearing the entry, or a gratuitous ARP from the host, fixes it.
ARP spoofing
ARP has no authentication. Any device on the segment can send a forged reply and claim an IP address, which lets it intercept traffic. Defenses include dynamic ARP inspection and static entries on switches and routers. This is why ARP is trusted only inside one Layer 2 segment.
Point-to-point links
Some links have only one possible neighbor, so no resolution is needed. On a PPP link, the router does not need ARP. Ethernet links, including ones used as point-to-point, still need it.
Putting it together
For one forwarded packet, the order is as follows. The router looks up the destination with longest prefix match. It reads the next hop and the outgoing interface from the chosen route. It decrements the TTL and updates the header checksum. It finds the next hop's MAC address in the ARP or neighbor cache, running ARP or ND if needed. It builds a new Ethernet header with its own interface as the source and the next hop as the destination. Then it transmits the frame.
Exercise
A router receives a packet for 10.1.2.3. Its best route is 10.1.0.0/16 via next hop 192.0.2.3 on eth0. Which IP address does the router ARP for, and what happens to the IP source and destination addresses in the packet?
Show answer
It ARPs for 192.0.2.3, the next hop, not for 10.1.2.3. The packet's IP source and destination addresses stay the same. Only the Ethernet header changes: the source MAC becomes eth0's MAC and the destination MAC becomes the MAC of 192.0.2.3.
Exercise
Why does the first ping to a host on a freshly connected network often take longer than later pings, or get lost?
Show answer
The sender or router has no cache entry for the target, so it must run ARP (or ND for IPv6) first. Resolution adds a round trip, and some implementations drop or delay the first packet while the request is outstanding.
Exercise
How does IPv6 Neighbor Solicitation avoid sending a broadcast, and what is the benefit?
Show answer
It sends the solicitation to the solicited-node multicast address built from the last 24 bits of the target address. Only hosts that share those bits process it, so most hosts on the segment are not interrupted.