The Internet Protocol (IP) is designed as an unreliable, connectionless, best-effort network layer protocol. It lacks built-in mechanisms for error reporting, congestion feedback, and diagnostic querying. To address these operational limitations, the Internet Control Message Protocol (ICMP) was developed as a core companion protocol to provide vital feedback and diagnostic services within the TCP/IP suite.
At the conceptual level, although ICMP is treated as an integral part of the network layer, its messages are not sent directly to the data link layer. Instead, ICMP messages are encapsulated inside standard IP datagrams.
+-------------------------------------------------------------+
| IP Datagram |
| +--------------------+----------------------------------+ |
| | IP Header | IP Payload | |
| | (Protocol field | +----------------------------+ | |
| | is set to 1) | | ICMP Message | | |
| | | | [Type] [Code] [Checksum] | | |
| | | +----------------------------+ | |
| +--------------------+----------------------------------+ |
+-------------------------------------------------------------+
|
v
+-------------------------------------------------------------+
| Ethernet Frame |
| +--------------------+------------------+---------------+ |
| | Ethernet Header | IP Datagram | Ethernet FCS | |
| | (Type = 0x0800) | | | |
| +--------------------+------------------+---------------+ |
+-------------------------------------------------------------+1 to indicate that the payload contains an ICMP packet.All ICMP messages share a common 8-byte header format, followed by a variable-length data field. However, the specific layout of the remaining bytes in the header depends on the message type and code.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type | Code | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Rest of Header (Specific to Type) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Data Field |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ICMP error-reporting messages inform source hosts when a router or destination host encounters a problem processing an IP datagram.
When an error occurs, the generating device constructs an ICMP error payload containing:
+-----------------------+------------------------+-------------------------+
| 8-byte ICMP Header | Original IP Header | First 8 bytes of |
| (Type, Code, etc.) | of discarded packet | original packet payload |
+-----------------------+------------------------+-------------------------+
|<---- 8 Bytes -------->|<---- Min 20 Bytes ---->|<------ 8 Bytes -------->|To prevent network congestion from infinite feedback loops, ICMP error messages are not generated in the following situations:
127.0.0.1 or unspecified 0.0.0.0).| Type | Name | Code | Description |
| :--- | :--- | :--- | :--- |
| 3 | Destination Unreachable | 0 to 15 | The datagram cannot be delivered. |
| 4 | Source Quench | 0 | Tells the sender to slow down due to congestion (obsolete/deprecated but historically important). |
| 5 | Redirection | 0 to 3 | Informs a host to update its routing table with a more optimal next-hop router. |
| 11 | Time Exceeded | 0 or 1 | TTL reached zero, or fragment reassembly timer expired. |
| 12 | Parameter Problem | 0 or 1 | Corrupted or missing parameter in the IP header. |
A router or destination host returns a Type 3 message when a datagram cannot reach its destination.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type = 3 | Code | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Unused |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+1.The Time Exceeded message handles packet routing lifetime issues and fragmentation reassembly.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type = 11 | Code (0/1) | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Unused |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+If a router or host discovers an invalid or ambiguous field value in an IP header, it discards the packet and sends a Parameter Problem message.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type = 12 | Code | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Pointer | Unused |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+The 8-bit Pointer field points directly to the exact byte offset in the discarded IP header where the error was detected.
Redirection optimizes routing decisions at local hosts. When a host boots, its routing table is typically populated with a single default gateway.
[Host A] --------------------> [Router R1] (Default Gateway)
| |
| | (R1 realizes R2 is on
| | the same subnet and has
| | a direct path to Host B)
| v
| [Router R2]
| |
| v
+-------------------------> [Host B]
(Direct Path after Redirection)ICMP query messages help network administrators and hosts diagnose network connectivity and latency issues. They are sent in matching request/reply pairs.
These messages test host-to-host or host-to-router reachability at the IP layer.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type (8/0) | Code = 0 | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identifier | Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data Field |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+These messages estimate the Round-Trip Time (RTT) and synchronize system clocks across two network devices.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type (13/14) | Code = 0 | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identifier | Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Originate Timestamp |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Receive Timestamp |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Transmit Timestamp |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+Timestamps are recorded as the number of milliseconds past midnight Coordinated Universal Time (UTC).
Using these three timestamps, we can calculate the Round-Trip Time () and Clock Offset between the sender and receiver. Let:
The total elapsed round-trip time is calculated as:
Assuming symmetric network delay (i.e., one-way outbound delay equals one-way return delay), the One-Way Latency () is:
The Clock Offset () of the receiver relative to the sender is calculated as:
ICMP uses the standard 16-bit Internet Checksum algorithm to detect header and payload corruption.
Let the ICMP packet (including header and payload) be represented as a sequence of 16-bit integers: .
When a device validates the checksum, it computes the ones' complement sum over the entire message, including the checksum field. If the computed sum is (or depending on representation), the packet is valid and error-free.
To generate ICMP packets manually, we must construct the byte structure of the ICMP header and calculate its checksum. The following script shows how to build an ICMP Echo Request (Type 8) packet and compute its checksum in Python.
import socket
import struct
import time
def calculate_checksum(data: bytes) -> int:
"""
Computes the 16-bit One's Complement Checksum of the input bytes.
"""
if len(data) % 2 == 1:
data += b'\x00' # Pad with zero if odd length
total_sum = 0
for i in range(0, len(data), 2):
# Combine two bytes into a 16-bit integer (network byte order)
word = (data[i] << 8) + data[i+1]
total_sum += word
Two fundamental network diagnostic tools, Ping and Traceroute, rely directly on the operational mechanics of ICMP.
ping verifies network-layer connectivity to a target IP address.
Source Host Target Host
| |
| ----- ICMP Echo Request (Type 8, Code 0) ---> |
| | (Processes request)
| <---- ICMP Echo Reply (Type 0, Code 0) ------ |
| |traceroute maps the exact hop-by-hop path packets take from a source host to a destination host, and measures transit delays across each hop.
Source Router R1 Router R2 Destination
| | | |
|-- UDP/ICMP (TTL=1) ---------->| | |
|<-- ICMP Time Exceeded (Type 11)-| | |
| | | |
|-- UDP/ICMP (TTL=2) -------------------------------------->| |
|<-- ICMP Time Exceeded (Type 11)---------------------------| |
| | | |
|-- UDP/ICMP (TTL=3) ------------------------------------------------------------------>|
|<-- ICMP Port Unreachable (Type 3) <---------------------------------------------------|1.0, discards it, and returns an ICMP Time Exceeded (Type 11, Code 0) message to the source. The source records R1's IP address and calculates the round-trip latency to the first hop.2.1 and forwards it to Router R2. R2 decrements the TTL to 0, discards the packet, and returns an ICMP Time Exceeded message. This identifies the second-hop router.1.ping and traceroute rely on ICMP Echo messages and Time Exceeded messages to map network paths and measure latencies.ICMP acts as a helper protocol to IP, providing the feedback, error-reporting, and diagnostic mechanisms that IP lacks.
ICMP messages are encapsulated directly within IP datagrams, meaning they bypass transport-layer protocols like TCP or UDP.
ICMP messages are strictly divided into error-reporting messages (e.g., Destination Unreachable) and query messages (e.g., Echo Request/Reply).
ICMP Time-Exceeded messages are generated either when a router decrements a datagram's TTL to zero or when a destination host's reassembly timer expires.
A routing optimization technique where a gateway informs a local host of a more optimal first-hop router for a specific destination.
Test your understanding with 5 questions
Which of the following ICMP Destination Unreachable codes can be generated ONLY by the destination host itself?
Under which of the following conditions is a router or host strictly prohibited from generating an ICMP error-reporting message?
In an ICMP Time-Exceeded message, what does Code 1 indicate?
How does ICMP handle IP layer congestion issues when a router's buffer capacity is exceeded?
What is the primary function of the Pointer field in an ICMP Parameter Problem (Type 12) message?
6 Modules
6 Modules