Sub topic 8
Step by step: what happens to one packet
This chapter follows a single IPv4 packet through one router, from the moment its frame arrives to the moment a new frame leaves. It assumes you know what an IP address and subnet are, and that you can read a routing table and apply longest prefix match (both covered in earlier chapters). It also assumes you know that Ethernet frames carry source and destination MAC addresses. Every router along the path repeats the sequence below.
The core idea: a frame is rebuilt at every hop
A router does not pass a frame along unchanged. It accepts a frame, discards the Layer 2 header, works on the IP packet inside, and builds a new frame for the next link. The IP source and destination addresses normally stay the same from end to end. The MAC addresses, the TTL, and the header checksum change at each hop.
This is why a router has a separate MAC address on each interface, and why the destination MAC of an incoming frame is the router's own address, even though the destination IP is somewhere else.
The steps in order
The numbered list gives the usual order. Real devices may reorder some checks or run them in parallel in hardware, but the outcomes are the same.
- Receive and accept the frame. The interface checks the frame check sequence (FCS). A corrupted frame is dropped silently. The router also checks the destination MAC. It accepts frames addressed to its own interface MAC, to broadcast, or to a multicast group it has joined.
- Strip the Layer 2 header. The EtherType tells the router the payload is IPv4 (0x0800) or IPv6 (0x86DD). An ARP frame (0x0806) goes to the ARP handler instead.
- Validate the IP header. The router checks the version, the header length, the total length against the frame size, and the header checksum. A bad header is dropped, usually without any ICMP message, because the source address cannot be trusted.
- Decide whether the packet is for the router itself. If the destination matches one of the router's own addresses, the packet goes up to the router's control software (for example a ping reply, SSH, or a routing protocol message). Otherwise it is transit traffic and continues.
- Check the TTL. If the TTL is 1 or 0, the router drops the packet and sends an ICMP Time Exceeded message (type 11) to the source.
- Look up the destination. Longest prefix match against the forwarding table gives an outgoing interface and, for non-connected routes, a next-hop address. If nothing matches, the router drops the packet and sends ICMP Destination Unreachable (type 3, code 0, network unreachable).
- Decrement the TTL and fix the checksum. The TTL drops by one. Because the TTL is part of the header, the checksum changes too. Routers update it incrementally instead of summing the whole header again.
- Check the MTU. If the packet is larger than the outgoing interface's MTU, the router either fragments it or, if the Don't Fragment (DF) bit is set, drops it and sends ICMP Fragmentation Needed (type 3, code 4) with the MTU value.
- Resolve the next hop to a MAC address. The router looks in its ARP table (neighbor discovery for IPv6) for the next hop. For a connected route the next hop is the destination itself. For a gateway route it is the gateway address. On a miss, the router sends an ARP request and holds the packet briefly.
- Build the new frame and transmit. The source MAC is the outgoing interface's MAC. The destination MAC is the one just resolved. The router queues the frame on the interface, and it may be delayed or dropped there if the queue is full.
A worked trace
Host A (10.1.2.9) sends a packet to 8.8.8.8. A's default gateway is router R, whose LAN-side interface has the address 10.1.2.1.
| Field | On the wire from A to R | On the wire from R to the next router |
|---|---|---|
| Source MAC | A's MAC | R's outgoing interface MAC |
| Destination MAC | R's LAN interface MAC | Next router's MAC |
| Source IP | 10.1.2.9 | 10.1.2.9 (unchanged, unless NAT applies) |
| Destination IP | 8.8.8.8 | 8.8.8.8 |
| TTL | 64 | 63 |
| Header checksum | value for TTL 64 | recomputed for TTL 63 |
Notice that A resolved R's MAC with ARP, not 8.8.8.8's. A knows 8.8.8.8 is off-subnet, so it addresses the frame to its gateway. R then does the same thing toward its own next hop.
The same logic in code
This Python function models steps 5, 6, 7, 8, and 9 for one router. It is a teaching model, not a forwarder. It leaves out header validation, checksums, and real queues.
import ipaddress
# (prefix, gateway or None for a connected network, outgoing interface)
ROUTES = [
(ipaddress.ip_network("0.0.0.0/0"), "192.0.2.1", "eth1"),
(ipaddress.ip_network("10.1.0.0/16"), None, "eth0"),
]
ARP = {"192.0.2.1": "aa:bb:cc:00:00:01", "10.1.2.9": "aa:bb:cc:00:00:09"}
MTU = {"eth0": 1500, "eth1": 1400}
def forward(pkt):
dst = ipaddress.ip_address(pkt["dst"])
if pkt["ttl"] <= 1:
return "drop: ICMP Time Exceeded sent to source"
matches = [r for r in ROUTES if dst in r[0]]
if not matches:
return "drop: ICMP Destination Unreachable"
net, gateway, iface = max(matches, key=lambda r: r[0].prefixlen)
next_hop = gateway or pkt["dst"]
action = "send"
if pkt["size"] > MTU[iface]:
if pkt["df"]:
return f"drop: ICMP Fragmentation Needed, MTU {MTU[iface]}"
action = "fragment and send"
mac = ARP.get(next_hop)
if mac is None:
return f"hold: ARP request for {next_hop} on {iface}"
return f"{action} out {iface} to {mac} with ttl {pkt['ttl'] - 1}"
print(forward({"dst": "10.1.2.9", "ttl": 64, "size": 1000, "df": False}))
print(forward({"dst": "8.8.8.8", "ttl": 64, "size": 1500, "df": True}))
print(forward({"dst": "8.8.8.8", "ttl": 1, "size": 100, "df": False}))
print(forward({"dst": "10.1.7.7", "ttl": 5, "size": 100, "df": False}))The four calls print:
send out eth0 to aa:bb:cc:00:00:09 with ttl 63
drop: ICMP Fragmentation Needed, MTU 1400
drop: ICMP Time Exceeded sent to source
hold: ARP request for 10.1.7.7 on eth0The first packet matches the connected /16, so the next hop is the destination itself. The second matches the default route, and its 1500-byte size exceeds the 1400-byte MTU of eth1 while DF is set. The third has a TTL of 1. The fourth is on a connected network but has no ARP entry yet.
Common errors and what they look like
- Packets work in one direction only. The forward path has a route, but the return path does not. Each router decides independently, so check the route for the source address on the far side too.
- Pings to a remote host time out, but the first ping is slower. The first packet waited on an ARP resolution. If every packet times out, the next hop may not be answering ARP at all.
- Small packets pass, large ones hang. A link has a smaller MTU, DF is set, and the ICMP Fragmentation Needed message is being filtered somewhere. Path MTU discovery then fails silently.
- Traceroute shows
* * *. A router on the path is dropping the packet or not sending (or having its replies filtered) ICMP Time Exceeded. It is not necessarily failing to forward. - The router forwards nothing on Linux. IP forwarding is off by default. Check
sysctl net.ipv4.ip_forward.
Differences for IPv6
The sequence is the same, with three changes. IPv6 has a Hop Limit instead of a TTL, with the same decrement-and-drop rule. IPv6 has no header checksum, so step 7 involves no checksum update. Routers never fragment IPv6 packets. If the packet is too large, the router drops it and sends ICMPv6 Packet Too Big (type 2), and the sending host must reduce its packet size. Neighbor Discovery replaces ARP in step 9.
Exercise
A router receives a 1480-byte IPv4 packet with DF set and TTL 12. The best route points out an interface with MTU 1400, and the next-hop MAC is not in the ARP table. What does the router do, and does the ARP request happen?
Show answer
The router drops the packet and sends ICMP Destination Unreachable, code 4 (Fragmentation Needed), with the next-hop MTU 1400 to the source. The MTU check (step 8) comes before next-hop resolution (step 9), and the packet is dropped there, so no ARP request is sent for this packet. The TTL is not a problem because 12 is greater than 1.
Exercise
In the worked trace, which fields in the packet change between the frame A sends and the frame R sends, and which stay the same?
Show answer
The source MAC, destination MAC, TTL (64 to 63), and header checksum change. The source IP (10.1.2.9) and destination IP (8.8.8.8) stay the same, because no NAT is involved.