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.

An ICMP echo request, 32 bits to a row, with the values ping sent. Byte 0: type, 8 bits, value 8; code, 8 bits, 0; checksum, 16 bits, 0x6575. Byte 4: identifier, 16 bits, 35; sequence number, 16 bits, 1. Byte 8: data, 56 bytes: a 16-byte timestamp, then the bytes 0x10 to 0x37.

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.20
  • ping Send echo requests and report the replies.
  • -c 2 Stop after 2 requests; without it, ping runs until interrupted.
  • -i 1 Default, can be left out. Wait 1 s between requests.
  • -s 56 Default, 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.20 The 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, 0x0800 more.
  • 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=63 is the reply’s IP TTL. Linux starts at 64, so the reply crossed one router.

What ping reported as the network changed:

NetworkPing’s report
50 ms round tripAverage 52.4 ms, mdev 0.4 ms
20% of the server’s packets dropped100 sent, 82 received, 18% packet loss
20% of the server’s packets duplicated10 received, +2 duplicates, marked (DUP!)
Delay varying by ±20 ms35.2 to 76.5 ms, average 51.4, mdev 11.5
Flood, -f, 500 requestsAll 500 answered in 6.2 s, up to 5 in flight
Host ignoring echo requests2 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_all set.
  • Ping doesn’t retransmit; a lost request or reply is just counted as loss.
  • No reply doesn’t mean no host. quiet ignored 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 ping opened socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP), an ICMP datagram socket, even as root. Such a socket needs no privilege, only a group in net.ipv4.ping_group_range, which was 0 2147483647, every group, in the lab’s containers. A raw socket, the older way, needs CAP_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 as Time to live exceeded.

Errors

An ICMP port unreachable, 32 bits to a row, with the values a server sent. Byte 0: type, 8 bits, value 3; code, 8 bits, 3; checksum, 16 bits, 0xf917. Byte 4: unused, 32 bits, 0; for code 4 it holds the next-hop MTU. Byte 8: the original IP header, 20 bytes, from 10.50.1.10 to 10.50.2.20, protocol 17. Byte 28: the original UDP header, 8 bytes, port 33110 to 9999. Byte 36: the original data, hello.

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:

CauseSent byMessageType, codeWhat the client saw
UDP to a port with no socketserverPort unreachable3, 3Connection refused on a connected UDP socket
TCP to a port the firewall rejectsserverPort unreachable3, 3connect(): Connection refused after 54 ms
TCP to a port rejected with icmp-admin-prohibitedserverAdministratively prohibited3, 13connect(): Host is unreachable after 54 ms
ping -t 1: TTL 1routerTime exceeded in transit11, 0From 10.50.1.254 … Time to live exceeded
A route of type unreachable on the routerrouterHost unreachable3, 1Destination Host Unreachable
A route of type prohibitrouterAdministratively prohibited3, 13Packet filtered
A route of type blackholenonenoneNothing until ping’s timeout
No host at 10.50.1.99 on the client’s own networkclientnone on the wireFrom 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 same Connection refused after 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 second send(); 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:

TypeMessageNotes
0Echo replyAnswer to type 8
3Destination unreachableCodes: 0 network, 1 host, 3 port, 4 fragmentation needed, 13 administratively prohibited
5RedirectCode 1: for one host
8Echo requestping
11Time exceededCode 0: TTL reached 0; code 1: fragments not reassembled in time
12Parameter problemA 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, and icmp_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.20 as the new next hop. Ping printed Redirect Host(New nexthop: 10.50.1.20).
  • The client’s kernel added the redirect to its route cache: ip route get 10.50.1.20 showed via 10.50.1.20 and <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 -I sends echo requests instead, and stops at the echo reply. Both found 10.50.1.254 at about 26 ms, then 10.50.2.20 at 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 as nobody opened socket(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 needs CAP_NET_RAW and receives every ICMP message to the host.
  • An icmp.Message with Type: ipv4.ICMPTypeEcho and Body: &icmp.Echo{ID, Seq, Data} is encoded with Marshal; icmp.ParseMessage(1, b) decodes a reply, where 1 is the protocol number, 58 for ICMPv6.
  • Errors from ICMP surface in ordinary calls: syscall.ECONNREFUSED for port unreachable, EHOSTUNREACH for 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