ICMP
Internet Control Message Protocol
The network’s way of telling you what went wrong.
System design›Protocols
Basics
What the protocol is for, and the ideas it is built on.
1.1What it does
The protocol in plain words, before any bytes.
ICMP travels inside IP but carries no user data. It is how routers and hosts report errors and answer simple questions: this host is unreachable, this packet is too big, your TTL expired, are you there?
Two famous tools are built on it. ping sends Echo requests and times the replies. traceroute sends packets with TTL 1, 2, 3 and so on and collects the Time Exceeded replies to learn each router on the path.
1.2Key ideas
The handful of ideas everything else follows from.
Types and codes
Type 3 is destination unreachable with codes for network, host, port and “fragmentation needed”. Type 11 is time exceeded.
Path MTU discovery
A router that cannot forward a large packet with DF set returns type 3 code 4 with the largest size it accepts.
Rate limits
Routers deprioritise and rate-limit ICMP, so a slow traceroute hop does not always mean a slow path.
ICMPv6 is essential
In IPv6 it also does neighbour discovery and address autoconfiguration, so you cannot block it wholesale.
Question
What this lesson prepares you to answer. Keep these in mind while you watch the packets.
2.1Target questions
- How does traceroute work?
- Why can a ping time vary a lot?
The answers are in part 4.
Detailed answer
Watch a real exchange step by step, then take each message apart field by field.
3.1Follow the packets
Step through the exchange between the two sides.
ping sends an ICMP echo with an identifier, a sequence number and some data.
3.2Decode it byte by byte
Click any byte or row. Each colour is one field.
What ping sends. Real pings carry 56 data bytes; this example uses 8 to keep the dump readable.
8 = Echo request. 0 = Echo reply. 3 = Destination unreachable. 11 = Time exceeded.
| Field | Offset | Bytes | Hex | |
|---|---|---|---|---|
| Type | 0 | 1 | 08 | |
| Code | 1 | 1 | 00 | |
| Checksum | 2 | 2 | 54 35 | |
| Identifier | 4 | 2 | 12 34 | |
| Sequence number | 6 | 2 | 00 01 | |
| Data | 8 | 8 | 61 62 63 64 65 66 67 68 |
3.3Decode it in code
A small decoder, tested against the exact bytes shown above.
Building and reading ping
Ping is this one packet, sent and echoed back.
1/** The same checksum IP uses: add 16-bit words, fold the carries, invert. */2function internetChecksum(b: Uint8Array): number {3 let sum = 0;4 for (let i = 0; i < b.length; i += 2) sum += (b[i] << 8) | (b[i + 1] ?? 0);5 while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16);6 return ~sum & 0xffff;7}8 9/** Builds an echo request, the packet ping sends. */10export function echoRequest(id: number, seq: number, data: Uint8Array): Uint8Array {11 const p = new Uint8Array(8 + data.length);12 const view = new DataView(p.buffer);13 p[0] = 8; // type 8 is echo request; a reply is type 014 view.setUint16(4, id);15 view.setUint16(6, seq);16 p.set(data, 8);17 view.setUint16(2, internetChecksum(p)); // computed while its own field is zero18 return p;19}20 21export function parseIcmp(b: Uint8Array) {22 const view = new DataView(b.buffer, b.byteOffset, b.byteLength);23 return {24 type: b[0],25 code: b[1],26 isEcho: b[0] === 8 || b[0] === 0,27 // Time exceeded (11) is how traceroute maps a path, one TTL at a time.28 isTimeExceeded: b[0] === 11,29 id: view.getUint16(4),30 seq: view.getUint16(6),31 checksumOk: internetChecksum(b) === 0,32 };33}p[0] = 8- Type 8 is a request, and the reply is type 0.
view.setUint16(2, internetChecksum(p))- The checksum is computed while its own field is zero.
isTimeExceeded- Traceroute sends TTL 1, 2, 3 and reads who replies with this.
Tested by npm run test:modules.
Q&A
Check your understanding. Answer out loud, then open.
4.1Check yourself
How does traceroute work?
Why can a ping time vary a lot?
Extra depth
The mistakes people make with this protocol in real systems.
5.1Watch out for
- No ping reply does not mean the host is down. Firewalls often drop echo requests.
- Blocking all ICMP breaks path MTU discovery.
- ICMP can be abused for floods and tunnels, so networks rate-limit it, not remove it.