Sub topic 9
TTL, Hop Limit, and ICMP error messages
This chapter assumes you know how longest prefix match picks a route and what the per-packet forwarding steps are. It also assumes you can read ip route output and run commands in a terminal. Here you will see how a router stops a packet that has travelled too far, how it reports that to the sender, and how traceroute turns that report into a path map.
Why a hop counter exists
Routing tables can be wrong. Two routers can each believe the other is the next hop toward a network, and a packet then bounces between them. Nothing in the destination address changes, so longest prefix match keeps giving the same answer. Without a limit, such a packet would circulate until a link failed.
The IPv4 header therefore carries an 8-bit field called Time to Live (TTL). IPv6 has the same field under a more honest name, Hop Limit. Despite the name, TTL counts router hops, not seconds. The original design allowed a router to subtract time spent queuing, but in practice every router subtracts exactly one.
The rules are the same in both protocols:
- The sender sets an initial value. Linux and macOS typically use 64, Windows 128, and many network devices 255.
- Each router that forwards the packet decrements the value by one.
- A router that would produce 0 discards the packet instead of forwarding it.
- The router then tries to send an ICMP error to the packet's source address.
The loop is not prevented. It is bounded: a packet with TTL 64 makes at most 64 trips through a loop and is then discarded.
The TTL step in the forwarding path
TTL handling happens after the route lookup is decided to be a forward, not a local delivery, and before the packet leaves. This function mirrors the check:
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, because decrementing it would give 0. A packet that arrives already at TTL 0 is also dropped. A packet that arrives with TTL 1 at its final destination is accepted normally, because the destination is a host and does not forward.
Two practical consequences follow:
- In IPv4, changing TTL changes the header checksum. Routers update the checksum incrementally rather than recomputing it over the whole header. IPv6 has no header checksum, so the router only changes the Hop Limit byte. This is one reason IPv6 forwarding is slightly simpler.
- The TTL check is part of the data plane, but generating the ICMP reply usually is not. Many routers hand expired packets to the CPU. This matters when you read traceroute latencies, as described below.
ICMP error messages
ICMP (Internet Control Message Protocol) carries errors and diagnostics about IP delivery. IPv4 uses ICMP. IPv6 uses ICMPv6, which also carries neighbor discovery. An ICMP error is itself an IP packet, addressed to the source of the packet that caused the error.
The body of an error includes the original IP header plus the start of the original payload. For ICMPv4 the minimum is the first 8 bytes of the payload. Modern routers often send more, and ICMPv6 includes as much as fits in a 1280-byte message. The sender uses this to match the error to the connection or probe that triggered it.
| Situation | ICMPv4 | ICMPv6 |
|---|---|---|
| TTL or Hop Limit reached zero in transit | Type 11, code 0 | Type 3, code 0 |
| No route to the destination network | Type 3, code 0 | Type 1, code 0 |
| Destination host did not answer (ARP or ND failed) | Type 3, code 1 | Type 1, code 3 |
| No listener on the destination port | Type 3, code 3 | Type 1, code 4 |
| Packet too large for the next link | Type 3, code 4 (fragmentation needed, DF set) | Type 2 (Packet Too Big) |
The first row is the subject of this chapter. The others show that "Time Exceeded" is one member of a family. A missing route produces Destination Unreachable, and a failed Layer 2 resolution on the last hop produces a host-unreachable error. Chapter material on MTU covers the last row in more detail.
Rules that limit error traffic
ICMP errors could easily cause storms, so routers follow restrictions:
- No error is sent in response to another ICMP error.
- No error is sent for packets sent to broadcast or multicast addresses.
- No error is sent for any fragment except the first.
- Errors are rate limited, often to a few per second per router or per destination.
These rules explain why a missing reply does not prove a packet was delivered or dropped. It may mean the router chose to stay silent.
How traceroute uses TTL
traceroute sends probes with TTL 1, then 2, then 3, and so on. Each expiry produces a Time Exceeded message from a different router, and the source address of that message identifies the hop. Typically that address belongs to the interface where the probe arrived.
When the TTL is large enough to reach the destination, the probe arrives and the host must answer differently. Linux traceroute sends UDP to a high port that nothing listens on, so the final answer is ICMP Port Unreachable. Windows tracert sends ICMP Echo Requests, so the final answer is an Echo Reply.
This simulation reproduces the behaviour with the forward function above:
def forward(packet_ttl):
if packet_ttl <= 1:
return None
return packet_ttl - 1
path = ["192.0.2.1", "198.51.100.1", "203.0.113.1"]
destination = "203.0.113.50"
def probe(ttl):
for router in path:
ttl = forward(ttl)
if ttl is None:
return ("time-exceeded", router)
return ("port-unreachable", destination)
for ttl in range(1, 5):
kind, source = probe(ttl)
print(f"TTL {ttl}: {kind} from {source}")The output shows three Time Exceeded replies from the three routers, then a Port Unreachable from the destination at TTL 4.
Running it on a real system
# Default TTL the Linux kernel puts on outgoing packets
sysctl net.ipv4.ip_default_ttl
# Send one ping with TTL 1 and watch the first router answer
ping -c 1 -t 1 8.8.8.8
# Trace the path without DNS lookups, at most 10 hops
traceroute -n -m 10 8.8.8.8The ping -t 1 output reads "Time to live exceeded" and names the first router. If you trace to a destination that does not answer probes, the last lines show only * * * until the maximum hop count.
Reading traceroute results correctly
The output is easy to misread. These are the usual traps:
- Asterisks do not mean loss. A router may forward traffic perfectly and still not send Time Exceeded, because of rate limiting or filtering. If later hops respond, the silent hop is fine.
- High latency at one hop can be misleading. Generating ICMP is handled by the router's CPU, which may be busy. If the next hop shows lower latency, the data plane is not slow. Only a delay that persists to all later hops points to a real problem.
- The path shown is the forward path only. The Time Exceeded messages return by whatever route the reverse direction takes, which can differ.
- Load balancing can mix paths. With equal-cost multipath, probes with different port numbers can take different links, so hop 4 and hop 5 may come from different routes.
- Some hops are invisible. MPLS networks and some tunnels may not decrement the visible TTL, or may not send errors to outside sources.
- Initial TTL hints at the hop count. If a packet arrives with TTL 57 and the sender is a Linux host, the packet likely crossed 7 routers. This is only an estimate.
Summary
TTL and Hop Limit bound how long a packet can live in the network. The router decrements the field on every forward, discards the packet when it would reach zero, and sends Time Exceeded to the source. Traceroute exploits this by deliberately sending packets that expire at each hop in turn. Reading its output correctly requires knowing that routers treat ICMP as optional, low-priority traffic.
Exercise
A Linux host sends a UDP probe with TTL 3 toward a destination that is 5 router hops away. Which device sends a reply, what kind of message is it, and what TTL value did the packet have when that device received it?
Show answer
The third router on the path sends an ICMP Time Exceeded message (type 11, code 0) to the host. The first router receives the packet with TTL 3 and forwards it with TTL 2. The second router forwards it with TTL 1. The third router receives it with TTL 1, so decrementing would give 0, and it discards the packet and sends the error.
Exercise
A traceroute shows * * * at hop 5, but hops 6 through 9 respond normally and the destination is reached. Is hop 5 broken? Give two possible explanations.
Show answer
No. Traffic evidently passes through hop 5 because later hops answer. Possible explanations are that hop 5 rate limits or deprioritises ICMP Time Exceeded generation, and that a firewall or router policy blocks ICMP replies from that device. A third possibility is that the replies are lost on the reverse path.