ICMP · Internet Control Message Protocol

The network’s way of telling you what went wrong.

03 · Internet · IP protocol 1

ICMP

Internet Control Message Protocol

The network’s way of telling you what went wrong.

LearnProtocolTier 2 · MechanismsUnderstand10 minInfrastructure

You will be able to: trace a ICMP exchange and decode it byte by byte.

Prove it: Ray Serve Distributed Actor Deployment

System design›Protocols

Study with AI
ClaudeChatGPTGeminiPerplexityMore

Part 1 of 5

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.

Part 2 of 5

Question

What this lesson prepares you to answer. Keep these in mind while you watch the packets.

2.1Target questions

  1. How does traceroute work?
  2. Why can a ping time vary a lot?

The answers are in part 4.

Part 3 of 5

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.

You
Target
Echo request (type 8)
Echo reply (type 0)
Packet with TTL 1
Time exceeded (type 11)
Step 1 of 4

ping sends an ICMP echo with an identifier, a sequence number and some data.

← BackPlayNext →

3.2Decode it byte by byte

Click any byte or row. Each colour is one field.

Echo requestTime exceeded

What ping sends. Real pings carry 56 data bytes; this example uses 8 to keep the dump readable.

000008005435123400016162636465666768··T5·4··abcdefgh
FieldType
Length1 byte
Decimal8

8 = Echo request. 0 = Echo reply. 3 = Destination unreachable. 11 = Time exceeded.

FieldOffsetBytesHex
Type0108
Code1100
Checksum2254 35
Identifier4212 34
Sequence number6200 01
Data8861 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.

icmp.tsTypeScriptCopy
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}
33 linesUTF-8TypeScript
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.

Part 4 of 5

Q&A

Check your understanding. Answer out loud, then open.

4.1Check yourself

How does traceroute work?
It sends probes with increasing TTL. Each router that drops a packet at TTL 0 replies with ICMP Time Exceeded, revealing its address.
Why can a ping time vary a lot?
Routers handle ICMP at low priority, and the path may change between probes.
Part 5 of 5

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.
← IPMark as understoodUDP →