Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 79 ยท The Network Layer: IP

ICMP: Errors and Diagnostics

Learn how ICMP reports errors and powers ping and traceroute: echo, destination unreachable, time exceeded, redirect, message layout, checksums and ICMPv6.

IP delivers packets on a best-effort basis. It does not promise that a packet arrives, and by itself it has no way to tell the sender what went wrong. If a router has no route, or a packet's TTL runs out, the packet is simply dropped. The Internet Control Message Protocol (ICMP) fills that gap: it lets routers and hosts send small messages back to the source that say "I could not deliver your packet, and here is why", or "are you alive?".

ICMP is part of the network layer, not a transport protocol. It travels inside IP packets (protocol number 1), has no ports, and is not used to carry user data. Two of the most useful tools you will ever run, ping and traceroute, are built entirely from ICMP messages.

The ICMP message format

Every ICMP message starts with the same three fields, followed by data that depends on the type:

Field Size Meaning
Type 1 byte The kind of message (echo request, unreachable, ...).
Code 1 byte A subtype that refines the type.
Checksum 2 bytes The Internet checksum of the whole ICMP message.
Rest of header 4 bytes Depends on the type (for echo: an identifier and a sequence number).
Data variable For errors: the original IP header plus the first 8 bytes of the packet that caused the error.

The last row is clever. An error message carries the beginning of the offending packet, which includes the transport header's port numbers. That lets the sender's operating system work out which connection failed and report it to the right application.

The most important types:

Type Name Used for
0 Echo reply The answer to a ping.
3 Destination unreachable Network, host, protocol or port unreachable; fragmentation needed with DF set; administratively prohibited.
5 Redirect A router telling a host that a better first hop exists.
8 Echo request The question sent by ping.
11 Time exceeded TTL reached zero in transit (code 0) or reassembly timed out (code 1).

ICMP errors follow two safety rules. An ICMP error is never sent in response to another ICMP error, and never for a fragment other than the first. Otherwise an error could cause more errors and flood the network.

Echo: how ping works

ping sends an echo request (type 8) with an identifier, a sequence number and some payload bytes. The destination host replies with an echo reply (type 0) that copies the identifier, sequence and payload. The sender measures the time between the two and prints it as the round-trip time. Missing sequence numbers show up as packet loss.

Ping answers two questions: "is the host reachable at the IP level?" and "how long is the round trip?". It does not say that a service works: a web server can be down while ping succeeds, and many hosts block ping while being perfectly healthy.

Python Build an echo request and read the reply

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In real ping output, 56(84) bytes of data means 56 bytes of payload and 84 bytes in total (payload, the 8-byte ICMP header and the 20-byte IP header). Each reply line says 64 bytes, which counts the ICMP header plus the payload. A sample exchange looks like this (read-only, values will differ on your machine):

TEXT
$ ping -c 3 192.0.2.10
PING 192.0.2.10 (192.0.2.10) 56(84) bytes of data.
64 bytes from 192.0.2.10: icmp_seq=1 ttl=57 time=12.4 ms
64 bytes from 192.0.2.10: icmp_seq=2 ttl=57 time=11.9 ms
64 bytes from 192.0.2.10: icmp_seq=3 ttl=57 time=12.1 ms

--- 192.0.2.10 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 11.9/12.1/12.4/0.2 ms

The ttl=57 in the reply is the TTL left when the reply arrived. Most systems start at 64, 128 or 255, so a value of 57 suggests the reply crossed about seven routers.

Destination unreachable

When a router or host cannot deliver a packet, it sends type 3. The code says why:

  • 0 Network unreachable and 1 Host unreachable: no route, or the last-hop router could not resolve the host (its ARP request got no answer).
  • 2 Protocol unreachable: the host does not support the IP protocol number in the packet.
  • 3 Port unreachable: the host is alive, but nothing listens on that UDP port. A closed TCP port gets a TCP reset instead.
  • 4 Fragmentation needed and DF set: the packet is bigger than the next link's MTU, and it may not be fragmented. This message carries the MTU of that link and is what makes path MTU discovery work (see the IPv4 packet).
  • 13 Communication administratively prohibited: a firewall rule refused the packet, and the firewall chose to say so (many just drop silently).
Python Which ICMP error would come back?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Time exceeded: how traceroute works

Every router decrements the TTL (hop limit) of a packet, and when it reaches zero the router discards the packet and sends back type 11, time exceeded, with its own address as the source. That behavior was meant as a loop guard. traceroute turns it into a map:

  1. Send a probe with TTL 1. The first router drops it and replies, so you learn hop 1 and the time to reach it.
  2. Send a probe with TTL 2. The second router answers.
  3. Keep increasing the TTL until the probe reaches the destination, which answers differently (a port-unreachable for UDP probes, an echo reply for ICMP probes).
sequenceDiagram
    participant H as Your host
    participant R1 as Router 1
    participant R2 as Router 2
    participant D as Destination
    H->>R1: probe, TTL=1
    R1-->>H: time exceeded (from R1)
    H->>R2: probe, TTL=2
    R2-->>H: time exceeded (from R2)
    H->>D: probe, TTL=3
    D-->>H: reply (destination reached)

Each probe lives one hop longer than the previous one, and each dying probe reveals one router.

Linux traceroute sends UDP probes to high ports by default, Windows tracert sends ICMP echo requests, and both work the same way. A sample (read-only, addresses from documentation ranges):

TEXT
$ traceroute 198.51.100.7
traceroute to 198.51.100.7, 30 hops max, 60 byte packets
 1  192.168.1.1     1.1 ms   0.9 ms   1.0 ms
 2  203.0.113.1     8.4 ms   8.2 ms   8.6 ms
 3  * * *
 4  198.51.100.1   14.9 ms  15.3 ms  15.0 ms
 5  198.51.100.7   15.6 ms  15.1 ms  15.4 ms

A line of * * * does not mean the path is broken, since packets clearly passed hop 3. It means that router did not answer: many routers rate-limit or ignore ICMP, or the reply was filtered. Traceroute also shows only the forward direction, and the reverse path can be different.

Python A traceroute across a simulated path

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Redirect and other messages

An ICMP redirect (type 5) is a hint from a router. Suppose host A sends a packet to its default gateway R1, but R1 sees that the next hop for that destination is R2, which is on the same subnet as A. R1 forwards the packet and also tells A: "next time, send directly to R2". Hosts can be tricked with fake redirects to send traffic through an attacker, so modern systems ignore them by default and routers do not send them unless configured.

Older types such as source quench (asked a sender to slow down) were abandoned long ago, because congestion control in TCP does that job better. Other types in use are parameter problem (type 12, a bad IP header field) and the timestamp and address-mask requests, which are obsolete.

ICMPv6

IPv6 cannot work without ICMPv6 (next header 58). It includes the same ideas with different numbers, and it takes over jobs that were separate protocols in IPv4:

ICMPv6 type Meaning
1 Destination unreachable
2 Packet too big (carries the MTU; routers never fragment)
3 Time exceeded (hop limit)
128 / 129 Echo request / echo reply
133 / 134 Router solicitation / router advertisement
135 / 136 Neighbor solicitation / neighbor advertisement

Types 133 to 136 are Neighbor Discovery, which replaces ARP and drives address autoconfiguration. Types below 128 are errors, and types 128 and above are informational. Because path MTU discovery and address resolution depend on it, blocking all ICMPv6 breaks IPv6. Firewalls must allow at least types 1 to 4 and the neighbor discovery types.

Security and good practice

ICMP is useful for attackers too: ping sweeps map live hosts, ICMP tunnels smuggle data past naive firewalls, and floods of echo requests can overwhelm a victim ("ping flood", or amplification when sent to broadcast addresses). The usual response is not to block ICMP entirely, but to rate-limit it and allow the types that the network needs: echo (optional), destination unreachable (especially code 4), and time exceeded. Dropping the fragmentation-needed message creates "black hole" connections where small packets work and large ones hang, which is a classic troubleshooting puzzle (see troubleshooting).

Two classic ICMP attacks

Two historic attacks show why ICMP needs care.

The ping of death exploited how IP fragments are reassembled. An IPv4 packet can be at most 65,535 bytes. An attacker sent an echo request as fragments whose offsets and lengths added up to more than 65,535 bytes. Vulnerable systems of the 1990s allocated a buffer for the maximum size and overflowed it while reassembling, which crashed the host. Operating systems have since checked the total length of reassembled packets, so it no longer works, but the lesson (never trust length fields from the network) is the one behind many later bugs.

The smurf attack is an amplification flood. The attacker sends echo requests to the broadcast address of a network, with the source address forged to be the victim's. Every host on that network answers the request, so one packet becomes hundreds of replies, all aimed at the victim. Defenses are to make routers drop directed broadcasts (now the default), to configure hosts not to answer broadcast pings, and to filter forged source addresses at the network edge (see attacks and defenses).

ICMP also carries operational queries such as echo, timestamp and address-mask requests. Those are optional features that are mostly disabled today because they reveal information to attackers.

Python Smurf amplification and the size of a ping of death

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which ICMP message does ping receive when the destination host is alive and reachable?

    Ping sends an echo request (type 8) and the destination answers with an echo reply (type 0), copying the identifier, sequence number and data.
  2. 2. How does traceroute discover the routers along a path?

    A probe with TTL n expires at the nth router, which reports itself in a time-exceeded message. Increasing n one at a time reveals the whole path.
  3. 3. A UDP datagram arrives at a live host where nothing is listening on the port. What does the host send back?

    For UDP there is no connection setup, so ICMP type 3 code 3 is how the host says that the port is closed. A closed TCP port answers with a TCP RST instead.
  4. 4. Why is it a bad idea to block all ICMP at a firewall?

    Blocking fragmentation-needed or packet-too-big messages creates black holes for large packets, and blocking ICMPv6 neighbor discovery breaks IPv6 completely.
  5. 5. A traceroute line shows * * * for hop 6, but hop 7 answers normally. What does this mean?

    Probes with a higher TTL passed hop 6, so it forwards traffic. It simply does not report itself.

Summary

  • ICMP reports errors and answers diagnostic questions for IP. It runs inside IP packets (protocol 1) and has a type, a code and a checksum.
  • Ping uses echo request (8) and echo reply (0). Traceroute uses increasing TTL values and the time-exceeded (11) messages routers send.
  • Destination unreachable (3) has codes for network, host, protocol, port, fragmentation needed and administratively prohibited.
  • Errors carry the start of the offending packet and are never sent about other ICMP errors.
  • ICMPv6 adds packet too big and neighbor discovery, so it must not be blocked. Rate-limit ICMP instead of dropping it all.

Next, see how routers decide where to send each packet: routing and forwarding.