Sub topic 8
Step by step: what happens to one packet
This chapter follows a single IPv4 packet through one router. It assumes you know what an IP prefix is, how longest prefix match picks a route, what a next hop is, and how ARP maps an IP address to a MAC address.
The sequence
A router repeats the same steps for every packet it forwards.
- Receive the frame. The Ethernet frame arrives on an ingress interface. The router accepts it only if the destination MAC is its own (or a broadcast or multicast address it listens to). It checks the frame check sequence and strips the Layer 2 header.
- Validate the IP header. It checks the version, header length, and header checksum. A damaged header is dropped silently.
- Check and decrement TTL. If the TTL would reach zero, the router drops the packet and sends an ICMP Time Exceeded message to the source. Otherwise it subtracts one and recomputes the header checksum.
- Look up the destination. The router applies longest prefix match to the destination address. If nothing matches, it drops the packet and sends ICMP Destination Unreachable.
- Pick the next hop and egress interface. The matching route gives a next-hop IP address. For a directly connected network, the next hop is the destination host itself.
- Check the MTU. If the packet is larger than the egress link's MTU, the router fragments it. If the Don't Fragment bit is set, it drops the packet and sends ICMP Fragmentation Needed.
- Resolve the next hop's MAC. The router looks in its ARP cache. On a miss, it sends an ARP request and holds the packet briefly.
- Build a new frame and transmit. The source MAC is the egress interface's MAC. The destination MAC is the next hop's MAC.
What changes and what stays the same
| Field | Changed by the router? |
|---|---|
| Source and destination IP | No (without NAT) |
| TTL | Yes, decreased by one |
| IP header checksum | Yes, recomputed |
| Source MAC | Yes, the egress interface's MAC |
| Destination MAC | Yes, the next hop's MAC |
The IP addresses describe the end-to-end journey. The MAC addresses describe only the current link, so they are rewritten at every hop.
A simplified model in code
This sketch combines the TTL check, the route lookup, and the TTL decrement. It leaves out checksums, MTU, and ARP.
import ipaddress
routes = [
(ipaddress.ip_network("0.0.0.0/0"), "192.0.2.1"),
(ipaddress.ip_network("10.1.0.0/16"), "192.0.2.3"),
]
def forward(packet):
if packet["ttl"] <= 1:
return "drop: ICMP Time Exceeded"
addr = ipaddress.ip_address(packet["dst"])
matches = [(net, hop) for net, hop in routes if addr in net]
if not matches:
return "drop: ICMP Destination Unreachable"
net, hop = max(matches, key=lambda m: m[0].prefixlen)
return dict(packet, ttl=packet["ttl"] - 1), hop
print(forward({"dst": "10.1.2.3", "ttl": 64})) # ({'dst': '10.1.2.3', 'ttl': 63}, '192.0.2.3')
print(forward({"dst": "8.8.8.8", "ttl": 1})) # drop: ICMP Time ExceededCommon mistakes
- Expecting the destination MAC to match the destination IP's owner. On every hop except the last, the destination MAC belongs to the next router.
- Forgetting that the TTL check comes before the lookup. A packet with an expired TTL gets Time Exceeded even if a route exists.
- Blaming the route when ARP fails. A correct route with an unresolvable next hop still drops packets, and the router reports Host Unreachable.
Exercise
A router receives a packet for 10.1.2.3 with TTL 1. It has a route for 10.1.0.0/16. What does the router do, and why?
Show answer
It drops the packet and sends an ICMP Time Exceeded message to the source. The TTL check happens before forwarding, and a TTL of 1 would become zero after the decrement, so the existing route is never used.