Sub topic 16
Tracing a Packet's Path and Debugging Forwarding
This chapter assumes you know the following:
- longest prefix match
- next hops
- the Layer 2 rewrite
- TTL handling
- the split between control plane and data plane
Here you will use those ideas as a debugging method. You will ask a host or router where it would send a packet, watch the path the packet takes, and read the ICMP messages that come back.
A method for debugging forwarding
Most forwarding problems fall into a few categories. Work through them in order, from the sending host outward.
- Does the sender have a route? Check that the host or router has a matching prefix, or a default route.
- Is the next hop reachable at Layer 2? The next hop must be on a connected network, and ARP (or neighbor discovery for IPv6) must resolve its MAC address.
- Does each router along the path have a route to the destination? Do the same for the return path.
- Is anything dropping the packet? Candidates are a firewall, an access list, an MTU limit, or a forwarding setting that is turned off.
Always test both directions. A packet can reach the destination and the reply can still be lost because the destination, or a router on the way back, has no route to the source. This failure is common, and it looks like "no response" from the sender's side.
Asking the kernel which route it would use
The routing table lists routes. ip route get shows the result of an actual lookup, so it applies longest prefix match for you.
ip route show
ip route get 10.1.2.3Typical output looks like this:
10.1.2.3 via 192.0.2.3 dev eth0 src 192.0.2.10 uid 1000
cacheRead it from left to right:
via 192.0.2.3is the next hop. Ifviais missing, the destination is on a directly connected network and the host sends to it directly.dev eth0is the outgoing interface.src 192.0.2.10is the source address the host will put in the packet.
If the output is RTNETLINK answers: Network is unreachable, no route matches the destination and there is no default route. The packet never leaves the machine.
You can reproduce the lookup logic in a few lines of Python. This is the same longest prefix match you saw earlier, and it is useful for checking a route table by hand.
import ipaddress
routes = [
(ipaddress.ip_network("0.0.0.0/0"), "192.0.2.1"),
(ipaddress.ip_network("10.0.0.0/8"), "192.0.2.2"),
(ipaddress.ip_network("10.1.0.0/16"), "192.0.2.3"),
]
def lookup(destination):
addr = ipaddress.ip_address(destination)
matches = [(net, hop) for net, hop in routes if addr in net]
if not matches:
return None
return max(matches, key=lambda m: m[0].prefixlen)
print(lookup("10.1.2.3")) # (IPv4Network('10.1.0.0/16'), '192.0.2.3')
print(lookup("10.9.9.9")) # (IPv4Network('10.0.0.0/8'), '192.0.2.2')
print(lookup("8.8.8.8")) # (IPv4Network('0.0.0.0/0'), '192.0.2.1')A common surprise is that a more specific route wins even when it is wrong. If a stale /24 points to a dead next hop, it overrides a working /16 or default route for every address in that /24. When traffic to one small range fails and everything else works, look for a too-specific route.
Checking the Layer 2 step
Having a route does not mean the packet can be sent. The router must also learn the MAC address of the next hop.
ip neigh showEntries have states. REACHABLE and STALE are normal. FAILED or INCOMPLETE means ARP requests got no reply. The usual causes are:
- the next hop address is wrong
- the next hop is down
- the next hop is on a different VLAN or segment than you think
- the interface itself is down
If the neighbor entry is INCOMPLETE, the route is fine and the Layer 2 resolution is what has failed.
How traceroute works
traceroute uses the TTL field and ICMP Time Exceeded messages. It sends probes with a TTL of 1, then 2, then 3, and so on. Each router decrements the TTL. The router that sees the TTL reach zero drops the packet and sends back an ICMP Time Exceeded message from its own address. That address becomes one line of output.
The forwarding rule each router applies is simple:
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)) # NoneThe exchange for a three-hop path looks like this:
Run it like this:
traceroute 8.8.8.8
traceroute -I 8.8.8.8
traceroute -n 8.8.8.8- By default, Linux
traceroutesends UDP probes to high port numbers. The destination answers with ICMP Port Unreachable, which marks the end of the path. -Iuses ICMP Echo probes instead. This helps when UDP is filtered.-nskips DNS lookups, which makes output faster and avoids confusion from reverse DNS names.
Reading traceroute output
1 192.0.2.1 0.4 ms 0.3 ms 0.3 ms
2 198.51.100.1 5.1 ms 5.0 ms 5.2 ms
3 * * *
4 203.0.113.9 12.3 ms 12.1 ms 12.4 msEach row is a hop, and each hop has three probes. Here is how to read the patterns you will meet.
| Pattern | Meaning | What to check |
|---|---|---|
| `* * *` in the middle, later hops respond | That router does not send Time Exceeded, or a filter drops it. Forwarding still works. | Nothing, usually. This is common and not a fault. |
| `* * *` from some hop to the end | Probes or replies are lost after that point. | The router after the last responding hop. It may lack a route, or a filter may drop the traffic. Also check the return path. |
| Same two addresses alternating | A forwarding loop between two routers, or load balancing across paths. | Routing tables on both routers. A loop continues until TTL expires. |
| Sudden latency jump that stays high | A long link, such as a transcontinental hop, or a congested link. | Compare with later hops. |
| One high latency hop, later hops low | The router is slow to answer ICMP. This does not affect forwarded traffic. | Nothing. ICMP generation runs on the router's control plane, which is slower than forwarding. |
| `!H`, `!N`, `!X` | ICMP Host, Network, or Administratively Prohibited Unreachable. | The router that sent it. |
The last latency rows show why the control plane and data plane distinction matters. Traceroute measures how long a router takes to generate an ICMP message. That is not the same as how long it takes to forward your traffic.
Reading ICMP errors
Routers report forwarding failures with ICMP messages sent to the source address of the dropped packet.
| ICMP message | Sent when | Typical cause |
|---|---|---|
| Time Exceeded (type 11) | TTL reached zero | Long path or routing loop |
| Destination Unreachable, Network (type 3, code 0) | No route to the destination network | Missing route, no default route |
| Destination Unreachable, Host (type 3, code 1) | Router has the network but ARP for the host failed | Host down or wrong address |
| Destination Unreachable, Port (type 3, code 3) | Destination host has nothing listening on the port | Service not running |
| Destination Unreachable, Admin Prohibited (type 3, code 13) | A filter rejected the packet | Firewall or access list |
| Fragmentation Needed (type 3, code 4) | Packet exceeds the next link MTU and the Don't Fragment bit is set | MTU mismatch |
The message tells you which device is at fault. The source address of the ICMP packet is the router that dropped yours.
Checking whether a router will forward
A Linux machine acting as a router must have forwarding enabled. By default it behaves like a host and drops packets that are not addressed to it.
sysctl net.ipv4.ip_forward
sudo sysctl -w net.ipv4.ip_forward=1If a Linux router receives a packet but never sends it out, check this value first. You can also add a missing static route:
sudo ip route add 10.2.0.0/16 via 192.0.2.2 dev eth0Setting it with sysctl -w lasts until reboot. Put it in /etc/sysctl.conf or a file under /etc/sysctl.d/ to make it permanent.
Finding MTU problems
If small packets work and large ones hang, suspect MTU. A typical case is a tunnel or VPN that lowers the usable MTU, together with a filter that blocks the ICMP Fragmentation Needed message. The sender never learns to use smaller packets.
Test by sending pings with the Don't Fragment bit set and a chosen size:
ping -M do -s 1472 8.8.8.8
ping -M do -s 1400 8.8.8.8The -s value is the payload size. Add 28 bytes (20 for the IP header, 8 for ICMP) to get the packet size, so 1472 corresponds to a 1500 byte packet. Lower the size until pings succeed. That gives you the path MTU.
Putting the checks together
Common mistakes
- Testing only the forward path and forgetting that replies need a route back.
- Reading
* * *in the middle of a trace as a failure when later hops respond. - Assuming a high latency at one hop means slow forwarding, when it is only slow ICMP generation.
- Forgetting to enable forwarding on a Linux router.
- Missing a more specific route that overrides the correct broader route.
Exercise
A traceroute to 203.0.113.50 shows hops 1 and 2 responding, then * * * for every hop from 3 onward. From the host, ip route get 203.0.113.50 shows a valid route and ip neigh show shows the next hop as REACHABLE. Name two possible causes, and one check for each.
Show answer
First, the router at hop 2 may have no route to 203.0.113.50 (or a bad next hop). Check its routing table with ip route get 203.0.113.50 on that router, or ask its administrator. Second, the return path may be broken, so replies from hop 3 and beyond never reach the host, or a filter on hop 2 or 3 may drop the probes. Check by tracing from the far side back toward the host, and review access lists on the routers near the break. Because the first two hops answer, the host's own route and Layer 2 are working.
Exercise
Pings of 100 bytes to a remote server succeed, but a large file transfer over the same path stalls. Which test confirms an MTU problem, and what does it tell you?
Show answer
Run ping -M do -s 1472 <server> and lower the -s value until replies return. The largest size that works, plus 28 bytes, is the path MTU. If large sizes fail silently and smaller ones succeed, a link on the path has a lower MTU and the ICMP Fragmentation Needed message is probably being blocked.