Sub topic 9
Why MAC addresses change at every router hop
This chapter assumes you know what a MAC address looks like, how an Ethernet II frame is laid out, and how ARP maps an IPv4 address to a MAC address on one LAN. You should also be comfortable with IP subnets and default gateways.
The short answer
A MAC address only means something on one link. It names the sender and receiver of a single frame on a single LAN. An IP address names the two endpoints of the whole journey.
A router connects separate links. When it forwards a packet, it removes the incoming frame and builds a new one for the next link. The new frame carries new MAC addresses. The IP packet inside keeps its source and destination addresses. Only the TTL changes, and the header checksum changes with it.
| Layer | Identifies | Scope | Changes at a router? |
|---|---|---|---|
| Ethernet (link layer) | The two interfaces on this link | One LAN segment | Yes, rewritten for each link |
| IP (network layer) | The original sender and final receiver | End to end | No, except through NAT |
Two addresses, two questions
Every forwarding decision answers two separate questions.
- Where is this packet ultimately going? The destination IP address answers this. Routers use it to choose a route.
- Who on this link should receive this frame right now? The destination MAC address answers this. It names the next device, which is either the final host or the next router.
A host on a LAN cannot put a faraway server's MAC address on a frame, because it has no way to learn it. ARP only works inside one broadcast domain. Broadcasts stop at routers, so a host in London never learns the MAC address of a server in Tokyo. The host asks a different question: "Which device on my link will take this packet toward that IP?" The answer is the default gateway, so the host puts the gateway's MAC in the frame.
How a host chooses the destination MAC
The sending host compares the destination IP with its own subnet.
- If the destination is on the same subnet, the host ARPs for the destination itself. The frame goes straight to the target's MAC.
- If the destination is on another subnet, the host ARPs for the next hop from its routing table, usually the default gateway. The frame goes to the gateway's MAC, while the IP destination stays the faraway host.
This is why one frame can carry an IP destination that has nothing to do with the frame's destination MAC.
Following one packet across two routers
Host A (10.0.1.10) sends a packet to Host B (10.0.3.20). Two routers sit between them, so the path has three links.
Here are the addresses on each link.
| Link | Source MAC | Destination MAC | Source IP | Destination IP | TTL |
|---|---|---|---|---|---|
| 1 (Host A to Router 1) | Host A | Router 1, LAN-side interface | `10.0.1.10` | `10.0.3.20` | 64 |
| 2 (Router 1 to Router 2) | Router 1, link-side interface | Router 2, link-side interface | `10.0.1.10` | `10.0.3.20` | 63 |
| 3 (Router 2 to Host B) | Router 2, LAN-side interface | Host B | `10.0.1.10` | `10.0.3.20` | 62 |
Notice that a router has a different MAC address on each interface. Each interface sits on a different link, so each has its own identity there.
What a router does with each frame
When a frame arrives, the router goes through these steps.
- Accept or ignore. The NIC checks the destination MAC. The frame is accepted if the address matches the interface, is broadcast, or is a multicast group the interface has joined. Otherwise the frame is dropped.
- Strip the Ethernet header and trailer. The router keeps only the IP packet. Because the frame is discarded, the old MAC addresses are gone.
- Validate and decrement. The router checks the IP header, subtracts 1 from the TTL, and recomputes the header checksum. If the TTL reaches 0, it drops the packet and sends back an ICMP "time exceeded" message.
- Look up the route. The router matches the destination IP against its routing table. The result is an outgoing interface and a next-hop IP address. For a directly connected destination, the next hop is the destination itself.
- Resolve the next hop. The router looks in its ARP cache for the MAC of the next-hop IP on the outgoing link. If there is no entry, it sends an ARP request and queues the packet until a reply arrives.
- Build a new frame. The source MAC is the outgoing interface's MAC. The destination MAC is the next hop's MAC. The EtherType is
0x0800for IPv4. The router transmits it.
Link-layer details can also change at each hop. The next link might be Wi-Fi or a VLAN-tagged port, or it might have a different MTU. The router rebuilds the frame for whatever the next link needs.
Simulating the rewrite
This script models the three links above. It does no real networking. It shows which fields change and which stay fixed.
from dataclasses import dataclass
@dataclass(frozen=True)
class Packet:
src_ip: str
dst_ip: str
ttl: int
@dataclass(frozen=True)
class Frame:
src_mac: str
dst_mac: str
packet: Packet
def forward(frame: Frame, in_mac: str, out_mac: str, next_hop_mac: str) -> Frame:
"""Behave like a router: check, strip, decrement TTL, re-frame."""
if frame.dst_mac != in_mac:
raise ValueError("frame is not addressed to this router interface")
pkt = frame.packet
if pkt.ttl <= 1:
raise ValueError("TTL expired, router would send ICMP time exceeded")
new_packet = Packet(pkt.src_ip, pkt.dst_ip, pkt.ttl - 1)
return Frame(src_mac=out_mac, dst_mac=next_hop_mac, packet=new_packet)
HOST_A = "02:00:00:00:00:0a"
R1_ETH0 = "02:00:00:00:01:00"
R1_ETH1 = "02:00:00:00:01:01"
R2_ETH0 = "02:00:00:00:02:00"
R2_ETH1 = "02:00:00:00:02:01"
HOST_B = "02:00:00:00:00:0b"
frame = Frame(HOST_A, R1_ETH0, Packet("10.0.1.10", "10.0.3.20", 64))
print("link 1:", frame)
frame = forward(frame, in_mac=R1_ETH0, out_mac=R1_ETH1, next_hop_mac=R2_ETH0)
print("link 2:", frame)
frame = forward(frame, in_mac=R2_ETH0, out_mac=R2_ETH1, next_hop_mac=HOST_B)
print("link 3:", frame)In the output, src_mac and dst_mac differ on every line. src_ip and dst_ip are identical on every line, and ttl falls from 64 to 62.
Seeing it on a real machine
Ask Linux which next hop it would use for an off-link destination, then look up that hop's MAC.
# Which gateway and interface does the kernel pick for this destination?
ip route get 8.8.8.8
# Find the default gateway's IP and show its neighbor entry
gw=$(ip route show default | awk '/default/ {print $3; exit}')
ping -c 1 "$gw" > /dev/null
ip neigh show "$gw"The output of ip route get contains via <gateway>. The neighbor entry shows the gateway's MAC as lladdr. Your machine has no neighbor entry for 8.8.8.8 itself, because it never ARPs for it.
To watch the frames, capture with link-level headers shown.
sudo tcpdump -eni eth0 icmpReplace eth0 with your interface name. The -e flag prints the Ethernet source and destination. When you ping an Internet host, the destination MAC in each outgoing packet is your gateway's MAC, while the destination IP is the Internet host.
traceroute shows the IP address of each router, but never the MAC addresses. MACs from other links never reach your machine.
Edge cases and common errors
NAT also rewrites IP addresses
The rule "IP stays the same end to end" holds for ordinary routing. A router that performs NAT rewrites the source IP address (and usually the port) on the way out and reverses it on the way back. Such a router does everything above and also changes the IP header. A home router almost always does this. The Internet host therefore sees the router's public address, not your private address.
Switches do not rewrite MACs
A switch forwards frames within one LAN and leaves the source and destination MACs unchanged. Only a device that performs routing creates new frames. If the MACs on a frame stay the same between two capture points, you crossed a switch. If they changed, you crossed a router.
Broadcasts stop at the router
A frame with destination ff:ff:ff:ff:ff:ff is delivered to every device on the link, but a router does not forward it. The router accepts the frame and then does not treat it as a packet to route. This is why ARP requests never leave their LAN.
Wrong subnet mask breaks the gateway decision
If a host's mask is too wide, it believes a remote address is on its own link. It then ARPs for that address directly. Nobody answers, because the real destination is behind a router. The symptom is an INCOMPLETE or FAILED neighbor entry for an address that should be reached through the gateway. If the mask is too narrow, the host sends local traffic through the gateway unnecessarily.
Proxy ARP hides the problem
With proxy ARP, a router answers ARP requests for addresses it can reach elsewhere and replies with its own MAC. The host then sends frames for a remote address straight to the router's MAC. It looks as if the remote host has the router's MAC address. You can recognize it when several different IP addresses in the neighbor table share a single MAC.
Tunnels and VPNs add another header layer
A VPN or tunnel wraps the original packet inside a new IP packet. The outer packet is routed hop by hop with its own MAC rewriting. The inner packet keeps its addresses until the tunnel endpoint removes the outer layer.
Why the design works this way
Separating the two address types lets each layer scale to its job. MAC addresses are flat and have no hierarchy, so devices on a LAN can be identified without any configuration. IP addresses are hierarchical, so routers can summarize millions of destinations into small routing tables. If routers had to track every MAC address in the world, routing tables would be enormous and would change whenever someone replaced a network card. Rewriting the MAC at each hop keeps the link layer local and the network layer global.
Exercise
Host A at 10.0.1.10 sends a packet to 10.0.3.20 through a single router. What are the source and destination MAC and IP addresses on the frame that arrives at the destination host?
Show answer
The destination MAC is the destination host's own MAC. The source MAC is the router's interface MAC on the destination's LAN. The source IP is still 10.0.1.10 and the destination IP is still 10.0.3.20, unless the router performs NAT. The TTL is one lower than when Host A sent it.
Exercise
On your laptop, you run ip neigh show after browsing the web for a while. You see an entry for your default gateway but none for any Internet server. Why?
Show answer
Your laptop only ARPs for the next hop on its own link. Traffic to Internet servers is sent to the default gateway's MAC, so only the gateway needs a neighbor entry. ARP requests are broadcasts that never cross a router, so the laptop could not learn a distant server's MAC even if it asked.
Exercise
You capture the same ping on two machines. On the first, the frame has destination MAC 02:00:00:00:01:00. On the second, which is on a different segment, the destination MAC is different but the IP addresses are identical. What does this tell you about the path between them?
Show answer
A router sits between the two capture points. The router received the frame addressed to its own MAC, stripped it, and built a new frame with new MAC addresses. The identical IP addresses show that the same packet was forwarded without NAT. A switch alone would have left the MAC addresses unchanged.