Sub topic 9
TTL, Hop Limit, and ICMP Error Messages
This chapter assumes you know how a router picks a next hop with longest prefix match and rewrites the Layer 2 frame. It covers the field that stops packets from circulating forever, and the messages routers send back when they drop a packet.
Why packets need a hop budget
Routing tables can be wrong for a short time. Two routers may each believe the other is the best next hop, so a packet bounces between them. Without a limit, such a packet would loop until a link failed.
IPv4 puts an 8-bit field called Time to Live (TTL) in every header. The sender sets the starting value. Common defaults are 64 (Linux, macOS) and 128 (Windows). The IPv6 equivalent is the Hop Limit, which works the same way. Despite the name, TTL counts hops, not seconds.
What a router does with TTL
Each router that forwards the packet does the following:
- It checks the TTL. If the TTL is 1 or less, the router does not forward the packet.
- Otherwise it subtracts 1 from the TTL.
- It updates the header checksum, because the header changed. IPv6 has no header checksum, so this step is IPv4 only.
def forward(packet_ttl):
"""Return the new TTL, or None if the packet must be dropped."""
if packet_ttl <= 1:
# Router discards the packet and sends ICMP Time Exceeded to the source.
return None
return packet_ttl - 1
print(forward(64)) # 63
print(forward(1)) # NoneA packet that arrives with TTL 1 is dropped even though the router could technically still send it. The check happens before the decrement, so a TTL of 1 means "no more hops."
ICMP error messages
The Internet Control Message Protocol (ICMP) lets routers report problems to the sender. An ICMP error travels in its own IP packet, addressed to the source of the packet that failed. It includes the IP header and the first bytes of the original packet, so the sender can tell which flow was affected. IPv6 uses ICMPv6, which carries the same kinds of errors.
| Situation | ICMPv4 message | Meaning |
|---|---|---|
| TTL reached zero | Time Exceeded (type 11) | The packet ran out of hops |
| No matching route | Destination Unreachable, net unreachable (type 3, code 0) | The router has no route to the destination |
| Host not answering on the last link | Destination Unreachable, host unreachable (type 3, code 1) | The router could not resolve the host's MAC address |
| Packet too big and DF set | Destination Unreachable, fragmentation needed (type 3, code 4) | The next link's MTU is too small |
Routers do not report errors for ICMP errors themselves. This prevents error storms. Many routers also rate-limit the ICMP they generate, because creating these messages is handled by the router's slower control path.
How traceroute uses TTL
traceroute turns the TTL rule into a mapping tool. It sends probes with TTL 1, then 2, then 3, and so on. Each router along the path drops the probe at the point its TTL runs out and replies with Time Exceeded. The source address of each reply identifies that hop.
A line of * * * in traceroute output means no reply arrived for that hop. The router may be dropping the probe silently, rate-limiting ICMP, or filtering it with a firewall. It does not always mean forwarding is broken, since later hops can still answer.
Common errors
- Reading a
* * *hop as a failure when later hops respond. - Assuming TTL is measured in seconds.
- Blocking all ICMP at a firewall. This hides Time Exceeded and fragmentation-needed messages and causes hard-to-diagnose stalls.
Exercise
A packet leaves a host with TTL 64 and crosses 5 routers. What TTL does the destination see? What happens if the host had sent it with TTL 5?
Show answer
Each router subtracts 1, so the destination sees 59. With TTL 5, the fifth router receives the packet with TTL 1 and drops it instead of forwarding it. That router sends an ICMP Time Exceeded message to the source, and the destination never receives the packet.