Chapters

ICMP: ping & traceroute

When a packet is dropped, someone should say why. ICMP is how routers and hosts report problems, and two of the most used debugging tools, ping and traceroute, are built on it.

Layers, routing and transport · chapter 13 of 34 · 8 min read

What ICMP is

ICMP (Internet Control Message Protocol) is a helper protocol at the Internet layer. Routers and hosts use it to send status and error messages: "your packet could not be delivered", "TTL expired", "are you there?". It never carries application data.

Unlike TCP and UDP, ICMP has no ports. It rides directly inside an IP packet. Compare the two envelopes:

TCP packet

frameIP · protocol 6 (TCP)TCP · ports 40001 → 443HTTP data
frameIP · protocol 1 (ICMP)ICMP · type, code, checksumsmall payload (no app data)
The IP header has a "protocol" field that says what is inside: 6 = TCP, 17 = UDP, 1 = ICMP. ICMP is part of the IP world (green), not a transport like TCP (amber).

Common ICMP messages

Every ICMP message has a type (what kind of message) and a code (more detail):

TypeNameCodes worth knowingUsed by
8Echo Request0ping: "are you there?"
0Echo Reply0ping: "yes"
11Time Exceeded0 = TTL reached 0 in transitLoops (chapter 12), traceroute
3Destination Unreachable0 = no route to network · 1 = host unreachable · 3 = port unreachable · 4 = packet too big (section 8)Routers and hosts reporting failure
5Redirect1 = better next hop for this hostA router suggesting a better gateway on the same LAN (usually ignored today)

ping

ping sends an Echo Request and waits for an Echo Reply. If the reply comes back, the host is up and reachable. The time between sending and receiving is the RTT (round-trip time): how you measure latency.

Laptop (home)kade-apiEcho Request · type 8 · seq 0Echo Reply · type 0 · seq 0RTT41.2 msone per second: seq 1, seq 2 …

Wide by design - scroll it sideways.

Pinging kade-api from home:

$ ping -c 4 api.kade.lk
PING api.kade.lk (34.87.120.15): 56 data bytes
64 bytes from 34.87.120.15: icmp_seq=0 ttl=57 time=41.2 ms
64 bytes from 34.87.120.15: icmp_seq=1 ttl=57 time=40.8 ms
64 bytes from 34.87.120.15: icmp_seq=2 ttl=57 time=42.0 ms
64 bytes from 34.87.120.15: icmp_seq=3 ttl=57 time=41.3 ms

--- api.kade.lk ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 40.8/41.3/42.0/0.4 ms
PartMeaning
(34.87.120.15)DNS ran first (chapter 1). ping works on IPs.
icmp_seq=0,1,2…Each request's number. A missing number = a lost packet.
ttl=57The reply's TTL on arrival. kade-api (Linux) starts at 64, so the reply passed about 64 − 57 = 7 routers.
time=41.2 msRTT. Colombo to Singapore and back; most of it is distance (section 6).
0.0% packet lossAny steady loss means a problem on the path.
stddev = 0.4 msHow much the RTT jumps around (jitter). Matters for calls and games.

Time Exceeded: the key to traceroute

Recall from chapter 12: every router lowers a packet's TTL by 1. When it reaches 0, the router drops the packet and sends an ICMP Time Exceeded message back to the sender. That message comes from the router, so its source IP tells the sender exactly which router the packet died at.

How traceroute works

traceroute uses this on purpose. It sends a probe with TTL=1: the first router lowers it to 0 and replies with Time Exceeded, revealing hop 1. Then TTL=2 reveals hop 2, TTL=3 reveals hop 3, and so on until the destination itself answers. Run it from the office front desk PC, along chapter 12's path. Press the button to send each probe:

  1. Front desk PCsender
  2. R1 office172.16.1.1
  3. R2 ISP edge198.51.100.1
  4. R3 ISP border10.200.0.2
  5. R4 Google edge192.0.2.2
  6. Google routersilent
  7. kade-api34.87.120.15

Press the button to send the first probe, with TTL = 1.

traceroute to api.kade.lk (34.87.120.15), 64 hops max
ToolProbe sentHow the destination answers
traceroutemacOS, Linux defaultUDP to high ports (33434 and up)ICMP Destination Unreachable, code 3 (port unreachable)
tracertWindows · traceroute -IICMP Echo RequestICMP Echo Reply
traceroute -T -p 443Linux, needs sudoTCP to port 443A TCP reply, since the port is open
Routers along the way answer Time Exceeded in every mode. Only the last line depends on the mode. If kade-vpc's firewall only allows TCP 443, the UDP and ICMP modes show stars for the last hop, and the TCP mode gets through.

Reading traceroute output

$ traceroute api.kade.lk
traceroute to api.kade.lk (34.87.120.15), 64 hops max
 1  172.16.1.1     0.9 ms   0.8 ms   1.0 ms
 2  198.51.100.1   5.8 ms   6.1 ms   5.9 ms
 3  10.200.0.2     7.1 ms   7.3 ms   7.0 ms
 4  192.0.2.2     36.4 ms  36.2 ms  36.8 ms
 5  * * *
 6  34.87.120.15  37.2 ms  37.0 ms  37.5 ms
#You seeIt means
1Three times per linetraceroute sends 3 probes per TTL. Each time is the RTT to that router.
3A private IP (10.200.0.2) in the middleAn ISP's internal link. Normal.
4A jump from 7 ms to 36 msThe link from Colombo to Singapore. A jump that stays for every later hop is distance or a slow link at that point.
5* * *No answer within the time limit. Often a router that does not send Time Exceeded, or limits how many it sends. If later hops answer, the path is fine.
-Stars from some hop to the endTraffic really is stopped there, or the destination's firewall blocks the probe type (section 5).
-One hop slow, the next ones fastThat router is just slow to answer ICMP (low priority). Not a real problem.
Traceroute only shows the path to the destination. Replies can come back another way (asymmetric routing, chapter 12), and every RTT includes that return trip.

mtr combines both tools: it runs traceroute continuously and shows loss and latency per hop, which makes a flaky hop much easier to spot than a single run.

Destination Unreachable

Type 3 tells the sender its packet went nowhere, and the code says why:

CodeSent byKadé example
0 networkA router with no route for the destinationR1 loses its default route: every internet destination fails at once
1 hostThe last router: ARP for the host got no answerThe office printer is switched off
3 portThe host itself: nothing listens on that UDP portWhat traceroute's last hop sends (section 5)
4 too bigA router: packet too big for the next link, and "don't fragment" is setSection 8

Packet too big: path MTU discovery

Chapter 1 set the MTU at 1,500 bytes. Some links carry less: VPN tunnels add their own headers, and GCP VPCs use an MTU of 1,460 by default. When a big packet reaches a smaller link, IP could split it into pieces (fragmentation), but TCP packets are normally marked "don't fragment". So the router drops it and replies ICMP type 3, code 4: "too big, the limit here is N". The sender then uses smaller packets. This is path MTU discovery.

1Office PC sends a 1,500-byte packet, "don't fragment" set, toward kade-db over the future VPN.
2The VPN link carries at most 1,400. The router drops it and sends ICMP 3/4: MTU 1,400.
3The PC resends in 1,400-byte packets. Everything works.
!If a firewall blocks all ICMP, step 2's message never arrives. Small requests work, but big responses hang forever. This is called a PMTU black hole, and it is very confusing to debug.

ICMP and firewalls

Because ICMP has no ports, firewalls treat it as its own protocol, allowed or blocked as a whole or by type. Some networks block ping so their hosts are harder to discover. That is fine, but blocking all ICMP breaks section 8 and makes debugging much harder.

For kade-vpc, allow ping only from Kadé's own public IPs (the home and office NAT addresses from chapter 10):

gcloud compute firewall-rules create kade-allow-icmp \  --network=kade-vpc --direction=INGRESS --allow=icmp \  --source-ranges=203.0.113.45/32,198.51.100.20/32

Summary

#IdeaIn one lineNew terms
1ICMPInternet-layer status and error messages; no ports, no app dataICMP, IP protocol number
2MessagesType + code: Echo (8/0), Time Exceeded (11), Unreachable (3), Redirect (5)type, code
3pingEcho Request/Reply; RTT, loss, jitter; reply TTL hints at hop countRTT, jitter, packet loss
5tracerouteTTL 1, 2, 3 … each router answers Time Exceededtraceroute, probe
6Reading itJumps that stay = real; lone stars = often fine; path is one-waymtr
8Too bigICMP 3/4 drives path MTU discovery; block it and big transfers hangfragmentation, PMTUD, black hole
9FirewallsTreat ICMP by type; allow errors back in

Try it yourself

Reachability and RTT

  • What to look for: time, ttl, loss.
  • Runs on: Windows: ping -n 4 1.1.1.1
  • From section: 3. ping
ping -c 4 1.1.1.1

A ping with TTL=1

  • What to look for: "Time to live exceeded" from your gateway: traceroute's trick by hand.
  • Runs on: macOS · Linux: ping -c 1 -t 1 · Windows: ping -n 1 -i 1
  • From section: 4. Time Exceeded: the key to traceroute
ping -c 1 -m 1 1.1.1.1

Every router on the way

  • What to look for: Hop 1 is your gateway; watch for jumps that stay.
  • Runs on: Windows: tracert 1.1.1.1
  • From section: 5. How traceroute works
traceroute 1.1.1.1

Same path, different probes

  • What to look for: Differences in the last hops show what firewalls allow.
  • Runs on: ICMP mode · Linux TCP mode: sudo traceroute -T -p 443 1.1.1.1
  • From section: 5. How traceroute works
traceroute -I 1.1.1.1

Live traceroute with loss per hop

  • What to look for: Loss that starts at one hop and stays for all later ones.
  • Runs on: install mtr
  • From section: 6. Reading traceroute output
mtr 1.1.1.1

Largest packet that fits

ping -D -s 1472 1.1.1.1

ICMP messages live

  • What to look for: "echo request", "echo reply", "time exceeded in-transit".
  • From section: 2. Common ICMP messages
sudo tcpdump -n -i en0 icmp

Spotted a mistake, or something unclear? Suggest an edit on GitHub