ICMP: Message Types, Encapsulation and the Rules for Sending Errors
What ICMP is for and how its messages are built: the shared Type, Code and Checksum header, how ICMPv4 and ICMPv6 ride inside IP, error versus informational messages, how hosts process them, the rules for when an error must never be sent, and ICMP rate limiting measured on a real Linux host.
kvi5hnuCode Sprout team member18 min readOn this page
IP delivers packets on a best-effort basis and says nothing when it fails. If a router has no route, if a packet's TTL runs out, or if nothing is listening on the destination port, IP itself has no way to tell the sender. The Internet Control Message Protocol (ICMP) fills that gap. It carries error reports and a small set of diagnostic and control messages, and every IP implementation must include it.
There are two versions:
- ICMPv4, defined in RFC 792 and refined by RFC 1122 (hosts) and RFC 1812 (routers);
- ICMPv6, defined in RFC 4443.
ICMPv6 does much more than ICMPv4. Beyond errors and ping, it carries neighbor discovery (IPv6's replacement for ARP, article 42), router discovery and address configuration (article 32), and multicast group management (article 44). An IPv6 network can't work without ICMPv6.
ICMP sits in an odd place in the layering. It's carried inside IP datagrams like a transport protocol, but it serves the IP layer itself, so it's usually treated as part of IP.
ICMP doesn't make IP reliable, either. The most common reason packets are lost, a full queue at a congested router, produces no ICMP message at all. Transport protocols such as TCP handle loss on their own (article 65).
Encapsulation and the common header
An ICMP message travels as the payload of an IP datagram:
- In IPv4, the Protocol field is 1.
- In IPv6, the last header before the message has a Next Header value of 58. There may be extension headers in between.
Every message starts with the same 4 bytes:

- Type says which message this is, such as Echo Request or Destination Unreachable.
- Code refines it, such as which kind of unreachable.
- Checksum is the Internet checksum (article 24) over the whole ICMP message. In ICMPv6, it also covers a pseudo-header: the IPv6 source and destination addresses, the length and the Next Header value. IPv6 has no header checksum, so this is what protects the addresses. A message with a bad checksum is silently discarded. There's no ICMP error about a bad ICMP checksum.
Unlike the IPv4 header checksum, which every router recomputes because it changes the TTL, the ICMP checksum is end to end: it's computed by the sender and checked only by the final receiver.
The lab pinged the server once over IPv4 and once over IPv6, and tshark pulled out the key fields:
ip.proto ipv6.nxt icmp.type icmpv6.type icmp.code icmpv6.code icmp.checksum icmpv6.checksum
1 8 0 0x401d
1 0 0 0x481d
58 128 0 0xc7ff
58 129 0 0xc6ff
Each line is one packet. IPv4 packets fill only the ip.proto/icmp.* columns and IPv6 packets only the ipv6.nxt/icmpv6.* ones, so the empty columns are just skipped tabs.
- The IPv4 packets have protocol 1, with types 8 (Echo Request) and 0 (Echo Reply).
- The IPv6 packets have next header 58, with types 128 and 129.
The request and reply checksums differ by exactly the change in the Type field. For IPv4, 0x401d versus 0x481d reflects the type going from 8 to 0, and the rest of the message is the same. Type and Code form the first 16-bit word, so it goes from 0x0800 to 0x0000. The sum of the message drops by 0x0800, and since the checksum is the complement of the sum, the checksum rises by the same amount: 0x401d + 0x0800 = 0x481d. For IPv6, the word goes from 0x8000 to 0x8100, so the checksum falls by 0x0100: 0xc7ff − 0x0100 = 0xc6ff. For IPv6, the type changes from 128 to 129, and the source and destination addresses swap, which doesn't change their sum.
The pseudo-header is easy to see. The lab took the captured requests, changed only the IP source address, and recomputed the checksums:
ICMPv4 echo request checksum on the wire 0x401d
same message, IPv4 source changed to 192.0.2.99: ICMP checksum 0x401d (unchanged)
ICMPv6 echo request checksum on the wire 0xc7ff
same message, IPv6 source changed to 2001:db8:1::99: ICMPv6 checksum 0xc776 (changed: pseudo-header)
The ICMPv4 checksum didn't change, because it covers only the ICMP message. The ICMPv6 checksum did, because the source address is part of what it protects. That's also why IPv4/IPv6 translators (article 38) always recompute ICMP checksums.
Error messages and informational messages
ICMP messages fall into two classes:
- Error messages report a problem with a packet that someone sent. Each one carries part of the packet that caused it.
- Informational (or query) messages ask for or provide information. Echo Request and Reply (ping) are the best known.
The diagram lists the common types of each version, grouped by class:

ICMPv4's type numbers grew over time, so errors and queries are mixed together. About 40 types have been assigned, but only a handful are used today. Many old ones have been formally deprecated:
- Source Quench (type 4) once asked senders to slow down; RFC 6633 deprecated it.
- Information Request and Reply and Address Mask Request and Reply were replaced by DHCP; RFC 6918 deprecated these and others.
ICMPv6 was designed with that experience. It defines far fewer types, and the high bit of the type separates the classes: types 0–127 are errors, and 128–255 are informational. Some ICMPv6 messages, the neighbor discovery ones, also carry options in a type-length-value format, which ICMPv4 doesn't have (article 42).
The codes that matter most:
| Message | ICMPv4 | ICMPv6 | Common codes | Article |
|---|---|---|---|---|
| Destination Unreachable | 3 | 1 | v4: net 0, host 1, protocol 2, port 3, fragmentation needed 4, administratively prohibited 13. v6: no route 0, administratively prohibited 1, address unreachable 3, port 4 | 40 |
| Packet Too Big | type 3, code 4 | 2 | (the MTU is in the message) | 40, 47 |
| Redirect | 5 | 137 | v4: network 0, host 1 | 40 |
| Time Exceeded | 11 | 3 | TTL or hop limit exceeded 0, reassembly time exceeded 1 | 40, 41 |
| Parameter Problem | 12 | 4 | v4: pointer indicates the error 0. v6: erroneous header field 0, unrecognized next header 1, unrecognized option 2 | 40 |
| Echo Request / Reply | 8 / 0 | 128 / 129 | 0 | 41 |
The official registries are maintained by IANA, and new types are still added occasionally. For example, RFC 8335 added Extended Echo (PROBE), which asks about an interface by name or index, using types 42 and 43 in ICMPv4 and 160 and 161 in ICMPv6.
How hosts process ICMP
What a host does with an incoming ICMP message depends on its type:
- Informational requests, such as Echo Request, are answered by the operating system automatically.
- Errors are matched to the connection or socket that sent the original packet, and handed to the transport protocol or the application, which decides what to do. The match uses the copy of the original packet inside the error: its protocol field picks TCP, UDP or another protocol, and its port numbers pick the socket.
- A few messages change the IP layer's own behavior directly. A Redirect updates the host's routing information. A "fragmentation needed" or Packet Too Big message lowers the cached path MTU for that destination (article 47).
RFC 4443 tightened the rules for ICMPv6:
- Unknown error types must still be passed to the upper-layer protocol that sent the original packet, if it can be identified.
- Unknown informational types are silently dropped.
- Errors must include as much of the original packet as fits without making the error larger than the IPv6 minimum MTU, 1280 bytes.
- Errors are delivered according to the protocol in the copy of the original packet, and dropped if it can't be found.
- Nodes must rate-limit the errors they send.
What an error message carries
Here's a real error. The client sent a UDP datagram to port 9 on the server (the old Discard service's port, which is why tshark labels the data "Discard Protocol"), where nothing was listening, and the server answered with Port Unreachable. tshark decodes it (the less interesting header fields are left out):
Internet Control Message Protocol
Type: 3 (Destination unreachable)
Code: 3 (Port unreachable)
Unused: 00000000
Internet Protocol Version 4, Src: 192.0.2.10, Dst: 198.51.100.10
Protocol: UDP (17)
Source Address: 192.0.2.10
Destination Address: 198.51.100.10
User Datagram Protocol, Src Port: 40000, Dst Port: 9
Source Port: 40000
Destination Port: 9
Length: 32
UDP payload (24 bytes)
Discard Protocol
Data: 68656c6c6f2c20616e796f6e65206f6e20706f727420393f
After the 8-byte ICMP header comes a copy of the client's own IPv4 header, then its UDP header (source port 40000, destination port 9), then the data it sent. That's how the client's operating system knows which socket the error is for. The client's UDP socket, if it's connected, then gets an error such as "Connection refused".
How much of the original packet is quoted has grown over time. RFC 792 required only the IP header and the first 8 bytes of the payload, which is just enough for the UDP or TCP ports. RFC 1812 told routers to include as much as possible without the ICMPv4 message exceeding 576 bytes. ICMPv6 allows up to 1280 bytes. The extra data helps diagnose problems with tunnels and other layered protocols, and it lets ICMP errors carry extension objects (article 40).
When not to send an error
ICMP errors are generated automatically by every router and host, so a careless rule could make a few packets cause a flood. Imagine an error about an error, which causes another error, and so on, or one broadcast packet causing hundreds of hosts to send errors at once. So there are firm rules about when an error must not be sent. An ICMPv4 error is never sent in response to:
- another ICMPv4 error (though errors about ICMP queries, such as an unreachable ping, are fine);
- a datagram sent to a broadcast or multicast IP address;
- a datagram that arrived in a link-layer broadcast frame;
- any fragment except the first;
- a datagram whose source address doesn't identify a single host, such as
0.0.0.0, a loopback, broadcast or multicast address.
The ICMPv6 rules are similar. No error is sent in response to:
- another ICMPv6 error, or a Redirect;
- a packet to a multicast address, or one sent as link-layer multicast or broadcast;
- a packet whose source is unspecified, multicast, or a known anycast address.
There are two important exceptions to the multicast rule: Packet Too Big and Parameter Problem code 2 (an unrecognized option whose type says to report it) are sent even for multicast packets. Without them, path MTU discovery and option processing wouldn't work for multicast.
The lab tested the broadcast rule. A host on the server's subnet sent one UDP datagram to port 9 on the server's own address, and one to the subnet's broadcast address, 198.51.100.255. The server received both, and nothing listens on port 9, but only one error came back:
sent 1 datagram to 198.51.100.10:9 (unicast)
sent 1 datagram to 198.51.100.255:9 (broadcast)
ICMP messages received by the sender:
IP 198.51.100.10 > 198.51.100.53: ICMP 198.51.100.10 udp port 9 unreachable, length 37
The unicast datagram produced Port Unreachable. The broadcast one was silently ignored.
Rate limiting
Even with those rules, a host or router can be asked to produce errors very quickly, for example by someone sending a flood of packets to closed ports. Generating errors costs CPU and bandwidth, and they can be used to amplify attacks. RFC 4443 therefore requires ICMPv6 nodes to limit the rate of errors they send, and suggests a token bucket: a bucket holds up to B tokens, it's refilled at N tokens per second, and each error uses a token. RFC 1812 allows the same for ICMPv4 routers, and every modern stack does it.
On Linux, the limits are sysctls, kernel settings read and changed with the sysctl command:
net.ipv4.icmp_ratelimit = 1000
net.ipv4.icmp_ratemask = 6168
net.ipv4.icmp_msgs_per_sec = 1000
net.ipv4.icmp_msgs_burst = 50
net.ipv6.icmp.ratelimit = 1000
icmp_ratelimitis the minimum interval between ICMPv4 errors to the same destination, in milliseconds.icmp_ratemaskchooses which types are limited. Bit n stands for ICMP type n. The default value 6168 is 4096 + 2048 + 16 + 8 = 2¹² + 2¹¹ + 2⁴ + 2³, binary1100000011000, with bits 3, 4, 11 and 12 set: Destination Unreachable, Source Quench, Time Exceeded and Parameter Problem. Echo replies aren't limited.icmp_msgs_per_secandicmp_msgs_burstlimit all ICMP output from the host, whatever the destination.net.ipv6.icmp.ratelimitis the IPv6 interval.
To see these limits work, the lab client sent a UDP probe to a closed port on the server every 10 milliseconds for 4 seconds, over both IPv4 and IPv6, and counted the Port Unreachable replies:
sent 314 UDP datagrams over IPv4 and 314 over IPv6 to closed port 9, one every 10 ms for 4 s
IPv4: 314 probes, 9 port unreachable replies
IPv6: 314 probes, 70 port unreachable replies
IPv4 reply times (s): 0.00 0.01 0.03 0.04 0.05 0.06 1.01 2.00 3.01
IPv6 reply times (s): 0.00 0.01 0.03 0.04 0.05 0.06 0.08 0.14 0.20 0.26 0.32 0.38 0.44 0.50 0.56 0.63 0.69 0.75 0.81 0.87 0.93 1.00 1.06 1.13 1.18 1.25 1.30 1.38 1.44 1.49 1.55 1.62 1.68 1.74 1.81 1.87 1.92 1.99 2.05 2.12 2.18 2.23 2.30 2.37 2.42 2.49 2.54 2.61 2.67 2.73 2.79 2.86 2.91 2.98 3.05 3.10 3.16 3.23 3.29 3.35 3.41 3.47 3.55 3.60 3.67 3.73 3.79 3.85 3.91 3.97

The curves show a token bucket at work:
- IPv4: a burst of 6 errors at the start, using up the bucket, then exactly one per second (1000 ms), the configured interval.
- IPv6: the same initial burst, then one error about every 62 milliseconds, not one per second. Linux "gives more bandwidth to wider prefixes": it divides the interval by 2 for every 32 bits that the route to the destination is shorter than /128. The server reached the client through its default route (
::/0), which is 128 bits shorter than /128, or 4 steps of 32 bits, so the interval was 1000 ms divided by 2⁴ = 16, or 62.5 ms. A destination reached through a /64 route would get 250 ms.
This has practical consequences. A UDP port scan of a Linux host is slow, because closed ports answer at one per second. A traceroute that sends probes quickly sees some hops as *, because routers rate-limit their Time Exceeded messages (article 41), not because packets were lost.
Blocking ICMP
ICMP has been used in many attacks (article 42 covers them), so some administrators block it entirely at their firewalls. That breaks things:
pingandtraceroutestop working.- Path MTU discovery fails, so large packets vanish silently. This is the classic "black hole" problem (article 47).
- On IPv6, neighbor discovery and address configuration stop working.
RFC 4890 gives detailed advice for ICMPv6 filtering. The short version: always allow Destination Unreachable, Packet Too Big, Time Exceeded and Parameter Problem, allow neighbor and router discovery on the local link, and rate-limit echo rather than blocking it. For ICMPv4, allow at least Destination Unreachable (especially fragmentation needed) and Time Exceeded.
Common questions
Is ICMP a transport protocol?
Not really. It's carried inside IP like a transport protocol, with its own protocol number, but it has no ports and doesn't carry application data. It exists to report on and control the IP layer, so it's normally considered part of IP. Every IP implementation must include it.
Why doesn't a router send an ICMP message when it drops a packet because of congestion?
Sending an extra packet when the network is already overloaded would make congestion worse. ICMPv4 once had Source Quench for this, but it was ineffective and was deprecated. Today, congestion is signaled implicitly by loss, which TCP detects, or explicitly with ECN bits in the IP header (article 25).
Why does ping work but my UDP application gets no error when the port is closed?
Possibly a firewall on the destination drops the datagrams silently instead of letting the host send Port Unreachable, or a firewall along the path drops ICMP errors. Also, an unconnected UDP socket on Linux doesn't report ICMP errors to the application by default. A connected socket does, as "Connection refused" on the next send or receive.
Can ICMP messages be fragmented?
Yes, like any IP datagram, but it's rare. Error messages are kept within 576 bytes (IPv4) or 1280 bytes (IPv6), so they normally fit in one packet. Large echo requests, such as ping -s 5000, are fragmented, which is a handy way to test fragmentation.
Why are ICMP errors not sent about other ICMP errors?
To prevent endless loops and storms. If an error about an error were allowed, two misconfigured devices could keep sending errors to each other forever, or one bad packet could trigger a cascade. Errors about ICMP queries are allowed, which is why a ping to an unreachable host gets a Destination Unreachable.
Does ICMPv6 have anything like ICMPv4's address mask request?
No. Prefix information comes from router advertisements (article 32), which are part of ICMPv6 neighbor discovery. ICMPv4's address mask and information request messages were replaced by DHCP and have been deprecated.
Summary
- ICMP reports errors and carries diagnostic and control messages for IP. ICMPv4 (RFC 792) is carried as IPv4 protocol 1, and ICMPv6 (RFC 4443) as IPv6 next header 58. ICMPv6 also carries neighbor discovery, router discovery and multicast listener messages.
- Every message starts with Type, Code and Checksum. The checksum is end to end, and in ICMPv6 it also covers a pseudo-header with the IPv6 addresses, as the lab showed by changing a source address.
- Messages are errors or informational. In ICMPv6, types 0–127 are errors and 128–255 are informational. Many old ICMPv4 types are deprecated.
- Errors carry as much of the offending packet as fits in 576 bytes (IPv4) or 1280 bytes (IPv6), so the receiver can match the error to a protocol and socket.
- Errors are never sent about errors, about broadcast or multicast packets (except Packet Too Big and some Parameter Problems in IPv6), about non-first fragments, or about packets with an invalid source address. The lab's broadcast datagram got no error.
- Errors are rate-limited with token buckets. The lab measured a burst of 6, then one per second for IPv4, and a faster rate for IPv6 to a destination reached through a short prefix.
- Blocking ICMP breaks ping, traceroute, path MTU discovery and, for IPv6, basic operation. Filter selectively.
Practice questions
1. A packet capture shows an IPv6 packet whose Next Header is 0 (Hop-by-Hop Options), followed by a header whose Next Header is 58. What's in the payload after the options header?
Answer
An ICMPv6 message. Next Header 58 means ICMPv6, and it can follow any number of extension headers. The last extension header's Next Header field identifies it. (A Hop-by-Hop header followed by ICMPv6 is typical of MLD messages, article 44.)
2. Why does the ICMPv6 checksum include a pseudo-header, while the ICMPv4 checksum doesn't?
Answer
IPv6 has no header checksum, so without the pseudo-header nothing would detect a corrupted source or destination address, and a corrupted message could be accepted by the wrong host or attributed to the wrong sender. IPv4 has a header checksum that covers its addresses at every hop, so ICMPv4 didn't need one. TCP and UDP use pseudo-headers in both versions for the same end-to-end reason.
3. A host receives a UDP datagram for a closed port. In which of these cases should it send Port Unreachable: (a) the datagram was sent to its unicast address, (b) to the subnet broadcast address, (c) to a multicast group it joined, (d) it was the second fragment of a datagram?
Answer
Only (a). Errors must not be sent for datagrams to broadcast or multicast addresses, which rules out (b) and (c), to avoid floods of errors from many receivers. For (d), errors are sent only about first fragments, and in any case a host checks the port only after reassembly.
4. Linux's default icmp_ratelimit is 1000. Someone scans 1,000 closed UDP ports on a Linux host as fast as possible. Roughly how long until they've received an answer for every port?
Answer
After an initial burst of about 6, the host sends one Port Unreachable per second to that scanner, so the remaining ports take about 1,000 seconds, over 16 minutes. In practice, a scanner can't tell an unanswered port (rate-limited) from a filtered one without waiting and retrying, which is why UDP scans are so slow.
5. An ICMPv6 message has type 135. Is it an error or informational? How about type 4?
Answer
Type 135 has its high bit set (135 ≥ 128), so it's informational. It's a Neighbor Solicitation. Type 4 is below 128, so it's an error, Parameter Problem.
6. Why is it a bad idea to block all ICMP at a firewall, and what should be allowed at minimum?
Answer
Blocking all ICMP breaks path MTU discovery, because the fragmentation-needed or Packet Too Big messages never arrive, so large packets vanish. It also breaks ping and traceroute, and for IPv6 it breaks neighbor and router discovery. At minimum, allow Destination Unreachable (including fragmentation needed), Packet Too Big, Time Exceeded and Parameter Problem. For IPv6, also allow neighbor and router discovery on the local link. Rate-limit echo rather than block it.
Previous: IPv4/IPv6 Coexistence: Dual Stack, DS-Lite, NAT64, DNS64, 464XLAT and Teredo
Next: ICMP Error Messages: Unreachable, Packet Too Big, Redirect, Time Exceeded and Parameter Problem

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.