ICMP
Created · Updated
ICMP (Internet Control Message Protocol) is how IP reports on itself: small messages, carried in IP packets with protocol number 1, that say a packet couldn’t be delivered, or ask whether a host is there. IP has no error channel of its own. Without ICMP, a packet dropped for a missing route, an expired TTL, or a link too small would vanish, and the sender could only wait for a timeout. ICMPv6, protocol 58, does the same for IPv6 and also carries IPv6’s neighbor discovery.
ICMP has these parts:
- Message: a type, a code that refines it, and a checksum, then fields that depend on the type.
- Queries: an echo request and echo reply, used by
ping. - Errors: destination unreachable, time exceeded, and others, each quoting the start of the packet that caused it, so the sender can match it to a connection.
- Redirects: a router telling a host about a better first hop.
- Limits: errors are never sent about errors, and hosts limit how many they send.
The lab is five Alpine containers on Linux 6.12. client at 10.50.1.10 and peer at 10.50.1.20 share one network; server at 10.50.2.20 and quiet at 10.50.2.30 share another; router joins the two. Client and server each delay outgoing packets by 25 ms with tc netem, for a round trip of 50 ms. tcpdump captured on the client, strace recorded system calls, and ping is iputils 20240905.
Ping
ping sends echo requests and prints each reply with its round-trip time:
ping -c 2 -i 1 -s 56 10.50.2.20pingSend echo requests and report the replies.-c 2Stop after 2 requests; without it, ping runs until interrupted.-i 1Default, can be left out. Wait 1 s between requests.-s 56Default, can be left out. Send 56 bytes of data, for an ICMP message of 64 bytes and an IP packet of 84.10.50.2.20The host to ask.
Two requests got replies with ttl=63 in 52.8 and 51.9 ms. The first request on the wire:
08 00 65 75 00 23 00 01 30 7c 0b 6b 00 00 00 00 8c ac 0b 00 00 00 00 00 10 11 12 … 37
- Type 8, code 0: echo request. The reply is type 0, and since only the type changed, its checksum was
0x6d75,0x0800more. - Identifier 35 marks this ping’s requests, and the sequence number counts them from 1. A reply copies both, so ping can match it to its request.
- The data starts with a 16-byte timestamp, the time the request was sent, then 40 bytes counting up from
0x10. The reply copies the data, so ping computes the round trip from the timestamp it gets back. ttl=63is the reply’s IP TTL. Linux starts at 64, so the reply crossed one router.
What ping reported as the network changed:
| Network | Ping’s report |
|---|---|
| 50 ms round trip | Average 52.4 ms, mdev 0.4 ms |
| 20% of the server’s packets dropped | 100 sent, 82 received, 18% packet loss |
| 20% of the server’s packets duplicated | 10 received, +2 duplicates, marked (DUP!) |
| Delay varying by ±20 ms | 35.2 to 76.5 ms, average 51.4, mdev 11.5 |
Flood, -f, 500 requests | All 500 answered in 6.2 s, up to 5 in flight |
| Host ignoring echo requests | 2 sent, 0 received |
- mdev is the spread of the round-trip times, so a high one means jitter. The host that ignored echo requests had
icmp_echo_ignore_allset. - Ping doesn’t retransmit; a lost request or reply is just counted as loss.
- No reply doesn’t mean no host.
quietignored every echo request, yet a TCP connection to its port 80 was refused at once, with an RST, so it was up. - The first ping to a new address on a link took 105 ms instead of 52, because the kernel first resolved the address with ARP.
The kernel answers
No process on the server handled the echo requests: the kernel’s IP code answers them itself, the moment they arrive. That’s why ping reaches a host whose programs are all hung, and why a reply says only that the kernel and the network work.
The sending side also runs through the kernel:
- iputils
pingopenedsocket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP), an ICMP datagram socket, even as root. Such a socket needs no privilege, only a group innet.ipv4.ping_group_range, which was0 2147483647, every group, in the lab’s containers. A raw socket, the older way, needsCAP_NET_RAW. - With a datagram socket, the kernel owns the identifier. Ping wrote
0xffff, and the request left with 35; the kernel also filled in the checksum. Replies with another identifier go to other sockets, so two pings never see each other’s replies. - Ping set
IP_RECVERR, so ICMP errors about its requests reach it as messages, which it prints, such asTime to live exceeded.
Errors
An error quotes the packet that caused it: the original IP header, then at least 8 bytes of its payload, enough for the ports. The port unreachable above was a 61-byte packet that quoted all 33 bytes of the UDP datagram that caused it. Linux quoted the whole original in every error in the lab: a time exceeded for an 84-byte ping was 112 bytes. The quoted IP header showed TTL 63, the datagram as it arrived, after the router.
Errors the lab produced:
| Cause | Sent by | Message | Type, code | What the client saw |
|---|---|---|---|---|
| UDP to a port with no socket | server | Port unreachable | 3, 3 | Connection refused on a connected UDP socket |
| TCP to a port the firewall rejects | server | Port unreachable | 3, 3 | connect(): Connection refused after 54 ms |
TCP to a port rejected with icmp-admin-prohibited | server | Administratively prohibited | 3, 13 | connect(): Host is unreachable after 54 ms |
ping -t 1: TTL 1 | router | Time exceeded in transit | 11, 0 | From 10.50.1.254 … Time to live exceeded |
A route of type unreachable on the router | router | Host unreachable | 3, 1 | Destination Host Unreachable |
A route of type prohibit | router | Administratively prohibited | 3, 13 | Packet filtered |
A route of type blackhole | none | none | Nothing until ping’s timeout | |
No host at 10.50.1.99 on the client’s own network | client | none on the wire | From 10.50.1.10 … Destination Host Unreachable, after 3 unanswered ARP requests 1 s apart |
- A TCP port with no listener sends an RST, not ICMP, and
connect()reported the sameConnection refusedafter 53 ms. A firewall can reject with either. - The UDP socket learned of the error late. Its first
send()succeeded; the error then arrived and was reported by the secondsend(); the third succeeded again. - Fragmentation needed, type 3 code 4, puts the next link’s MTU in the unused field, which is how path MTU discovery learns it.
- A host never sends an error about an ICMP error, about a broadcast or multicast packet, or about any fragment but the first, so errors can’t multiply.
Common types:
| Type | Message | Notes |
|---|---|---|
| 0 | Echo reply | Answer to type 8 |
| 3 | Destination unreachable | Codes: 0 network, 1 host, 3 port, 4 fragmentation needed, 13 administratively prohibited |
| 5 | Redirect | Code 1: for one host |
| 8 | Echo request | ping |
| 11 | Time exceeded | Code 0: TTL reached 0; code 1: fragments not reassembled in time |
| 12 | Parameter problem | A header field the receiver couldn’t process |
Rate limits
Linux limits the errors it sends, so a flood of bad packets can’t turn a host into a flood of ICMP:
icmp_ratemask, 6168, marks the limited types: destination unreachable, source quench, time exceeded, and parameter problem. Echo replies aren’t limited; all 500 flood pings were answered.icmp_ratelimit, 1000 ms, limits errors to each destination. 200 UDP datagrams to a closed port, sent at once, drew 6 port unreachables. The same burst 3 s later drew 3.icmp_msgs_per_sec, 1,000, andicmp_msgs_burst, 50, cap the same types across all destinations together.
A missing error therefore proves nothing. traceroute, which relies on errors, can show gaps when routers limit them.
Redirects
A router that forwards a packet back out the interface it came in on knows the sender had a better first hop. The client had a route to peer, on its own network, via the router:
- The router forwarded the first request, then answered the second with a redirect, type 5 code 1, naming
10.50.1.20as the new next hop. Ping printedRedirect Host(New nexthop: 10.50.1.20). - The client’s kernel added the redirect to its route cache:
ip route get 10.50.1.20showedvia 10.50.1.20and<redirected> expires 299sec. Later requests went to the peer’s MAC address directly. - Linux accepts redirects only while it isn’t forwarding itself, and many networks turn them off, since a forged redirect can steer a host’s traffic.
Traceroute
traceroute finds each router on a path by sending probes with TTL 1, 2, 3, and so on, and listing who returns each error:
- By default it sent UDP probes to ports 33434 upward, 16 at once, TTL 1 to 16. The router answered TTL 1 with time exceeded; the server answered TTL 2 with port unreachable, which ended the trace at hop 2.
traceroute -Isends echo requests instead, and stops at the echo reply. Both found10.50.1.254at about 26 ms, then10.50.2.20at about 53 ms.- A router that sends no time exceeded shows as
*, while traffic through it still flows.
ICMPv6
ICMPv6 uses its own numbers: 1 destination unreachable, 2 packet too big, 3 time exceeded, 4 parameter problem, 128 echo request, and 129 echo reply. ping fd50:2::20 sent type 128 and got 129, with identifier 0x0033 and a 64-byte payload.
- Types 133 to 137 are neighbor discovery: router and neighbor solicitations and advertisements, and redirects. They replace ARP.
- Packet too big replaces fragmentation needed, and IPv6 routers never fragment, so path MTU discovery depends on it entirely.
- The ICMPv6 checksum also covers the source and destination addresses, since the IPv6 header has no checksum of its own.
ICMPv4 can be blocked with some loss, such as broken path MTU discovery. Blocking ICMPv6 breaks IPv6 itself: without neighbor discovery, hosts on a link can’t find each other.
In a program
Go’s golang.org/x/net/icmp package builds and parses ICMP messages:
icmp.ListenPacket("udp4", "0.0.0.0")opens an ICMP datagram socket with no privilege. A Go pinger run asnobodyopenedsocket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP), sent an echo with ID 1234, and got the reply in 51.7 ms with ID 52: the kernel had replaced the ID.icmp.ListenPacket("ip4:icmp", "0.0.0.0")opens a raw socket instead, which needsCAP_NET_RAWand receives every ICMP message to the host.- An
icmp.MessagewithType: ipv4.ICMPTypeEchoandBody: &icmp.Echo{ID, Seq, Data}is encoded withMarshal;icmp.ParseMessage(1, b)decodes a reply, where 1 is the protocol number, 58 for ICMPv6. - Errors from ICMP surface in ordinary calls:
syscall.ECONNREFUSEDfor port unreachable,EHOSTUNREACHfor host unreachable and administratively prohibited.
IP packets covers the header that carries ICMP, and addressing and routing path MTU discovery and TTL.
References: RFC 792: ICMP, RFC 1122: Requirements for internet hosts, RFC 1812: Requirements for IPv4 routers, RFC 4443: ICMPv6, RFC 4861: Neighbor discovery, RFC 4890: Filtering ICMPv6 messages, IANA ICMP parameters, ping(8), traceroute(8), icmp(7), ip(7), Linux IP sysctl, ip-route(8), Go x/net/icmp