Sub topic 2
ICMP: the protocol behind ping and traceroute
This chapter assumes you know what an IP address is, can open a command prompt or terminal, and know that routers forward packets between networks. It explains the protocol that ping and traceroute use, so their output makes sense in later chapters.
What ICMP is and why it exists
IP delivers packets on a best-effort basis. It has no built-in way to tell a sender that a packet was dropped, that a destination does not exist, or that a router is overloaded. The Internet Control Message Protocol (ICMP) fills that gap. It carries diagnostic and error messages between hosts and routers.
ICMP has three properties that shape how you use it:
- It rides directly on IP. An ICMP message sits inside an IP packet with protocol number 1. IPv6 uses ICMPv6, with next-header value 58.
- It has no ports. TCP and UDP use ports to tell applications apart. ICMP does not, so "ping port 80" is meaningless. Ping tests whether the host's IP stack answers, not whether a service is running.
- It is part of IP, not an application. Every compliant IP implementation must handle some ICMP. Because ICMPv6 also does neighbor discovery, blocking it outright breaks IPv6 networks.
The ICMP message format
Every ICMP message starts with the same four bytes. The rest depends on the message type.
| Field | Size | Purpose |
|---|---|---|
| Type | 1 byte | The kind of message, such as echo request or time exceeded |
| Code | 1 byte | A subtype that refines the type |
| Checksum | 2 bytes | Detects corruption in the ICMP message |
| Rest of header and data | Varies | Content that depends on the type |
For echo messages, the "rest of header" is a 16-bit identifier and a 16-bit sequence number. These let the sender match each reply to the request that caused it. After them comes an arbitrary payload, which the replier must send back unchanged.
The message types you will meet
Dozens of types exist, but troubleshooting uses only a few.
| Type (ICMPv4) | Name | Used by |
|---|---|---|
| 8 | Echo Request | ping, sent by the tester |
| 0 | Echo Reply | ping, sent by the target |
| 3 | Destination Unreachable | Reports a delivery failure |
| 11 | Time Exceeded | traceroute, sent by routers when TTL reaches zero |
| 5 | Redirect | A router suggests a better first hop |
| 12 | Parameter Problem | Reports a malformed IP header |
ICMPv6 uses different numbers for the same ideas: 128 for echo request, 129 for echo reply, 1 for destination unreachable, 2 for packet too big, and 3 for time exceeded.
Destination Unreachable codes
Type 3 messages carry a code that says why delivery failed. These are the codes you will see most often:
- Code 0, network unreachable. A router has no route toward the destination network.
- Code 1, host unreachable. The router reached the destination network but could not find the host, for example because ARP got no answer.
- Code 3, port unreachable. The host was reached, but no program listens on that UDP port. Traceroute on Linux and macOS relies on this.
- Code 4, fragmentation needed and DF set. The packet is larger than the next link allows and has the Don't Fragment bit set. Path MTU discovery depends on this message.
- Codes 9, 10 and 13. Administrative prohibition. A filter rejected the traffic and said so.
Windows ping shows these as lines such as "Reply from 192.168.1.1: Destination host unreachable." Read the address carefully. It names the device that sent the error, which is not necessarily the target.
Echo: how ping works at the protocol level
Ping is a thin wrapper around two messages:
- The sender builds an Echo Request with an identifier, a sequence number, and a payload. It records the send time.
- The target's IP stack receives the request. Without involving any application, it swaps the source and destination addresses, changes the type to 0, recomputes the checksum, and returns the message.
- The sender receives the Echo Reply, matches the identifier and sequence number, and subtracts the send time from the receive time. The result is the round-trip time.
Ping reports on a stack, not on a service. A web server can be down while its host still answers ping. A host can also be perfectly healthy and silent if a firewall drops echo requests.
Time Exceeded: the mechanism behind traceroute
Every IP packet carries a Time to Live (TTL) field. In IPv6 the same field is called the hop limit. Each router decrements it by one before forwarding. If the value reaches zero, the router discards the packet and sends an ICMP Time Exceeded message (type 11, code 0) to the original sender. This stops packets from circulating forever in a routing loop.
Traceroute turns this safety feature into a measuring tool. It sends probes with TTL 1, then 2, then 3, and so on. The first router expires the first probe, the second router expires the second, and each one reveals its address in its Time Exceeded message. The chapter on TTL covers this in detail. For now, remember that traceroute works because routers report their own failures with ICMP.
What an ICMP error contains
An ICMP error message includes the IP header of the packet that triggered it, plus the first 8 bytes of that packet's payload. That is enough for the original sender to find which connection or probe failed. For a TCP packet, those 8 bytes cover the source and destination ports. Traceroute uses the same data to match each Time Exceeded message to the probe that caused it.
Two rules stop ICMP from causing floods:
- A device must not send an ICMP error in response to another ICMP error.
- Routers rate-limit the ICMP messages they generate, and many deprioritize them. This explains some odd traceroute results covered in a later chapter.
Seeing ICMP on the wire
You can watch ICMP with a packet capture. On Linux or macOS, run this in one terminal, then run ping -c 3 8.8.8.8 in another:
sudo tcpdump -n icmpYou should see lines showing ICMP echo request followed by ICMP echo reply, each with an id and seq value. The seq increases by one per ping, which is the sequence number described above.
Building an echo request by hand
The following script builds a valid Echo Request message without sending it. It shows the header layout and the checksum, which is the 16-bit ones'-complement sum used throughout the IP family.
import struct
def checksum(data: bytes) -> int:
if len(data) % 2:
data += b"\x00"
total = 0
for i in range(0, len(data), 2):
total += (data[i] << 8) | data[i + 1]
while total >> 16:
total = (total & 0xFFFF) + (total >> 16)
return ~total & 0xFFFF
def build_echo_request(identifier: int, sequence: int, payload: bytes) -> bytes:
header = struct.pack("!BBHHH", 8, 0, 0, identifier, sequence)
csum = checksum(header + payload)
return struct.pack("!BBHHH", 8, 0, csum, identifier, sequence) + payload
packet = build_echo_request(0x1234, 1, b"abcdefgh")
print(packet.hex())
print(len(packet), "bytes")
print("verify:", checksum(packet))The message is 16 bytes: 8 bytes of header and 8 bytes of payload. The last line prints verify: 0. Running the checksum over a message that already contains a correct checksum always gives zero, and receivers use that property to detect corruption. Sending the packet would need a raw socket and administrator rights, which is why ping tools are often privileged or setuid.
Payload size and the headers around it
Windows ping sends a 32-byte payload by default. Linux and macOS send 56 bytes. Add the 8-byte ICMP header and the 20-byte IPv4 header, and Linux's default packet is 84 bytes on the wire. This matters later when you test path MTU. A 1472-byte payload plus 8 bytes of ICMP header and 20 bytes of IP header makes exactly 1500 bytes.
Common mistakes
- Treating ping failure as host failure. Many hosts and firewalls drop echo requests while serving traffic normally.
- Assuming the error came from the target. A "Destination host unreachable" line comes from whichever device generated it, usually the last router.
- Looking for ports. ICMP has none. Use a TCP or UDP test to check a specific service.
- Ignoring the type and code. Two failures that both look like "no reply" can have different causes. An explicit Destination Unreachable means something answered. A timeout means nothing did.
Exercise
You run ping against a server and get "Reply from 10.0.0.1: Destination host unreachable", but 10.0.0.1 is your own default gateway and the server is on another network. Which device generated that message, what ICMP type is it, and what does it tell you about the path?
Show answer
The default gateway, 10.0.0.1, generated it. It is an ICMP Destination Unreachable message (type 3, here with code 1 for host unreachable or code 0 for network unreachable). It means your packet reached the gateway, but the gateway could not forward it toward the server, for example because it has no route or cannot find the next hop. The fault lies beyond your own machine, at or after the gateway, so check the gateway's routing, not your local configuration.
Exercise
Why can a host answer ping while its web server is down, and why can a healthy web server fail to answer ping?
Show answer
Echo replies come from the host's IP stack, with no application involved, so the host can answer while the web server program is stopped. In the other direction, a firewall may drop ICMP echo requests while allowing TCP port 80 or 443, so the web server works but ping times out.