---
name: "icmp"
section: "4"
platform: "macos"
package: "xnu"
version: "12377.121.6"
release: "macOS 26.5"
platforms: ["linux", "freebsd", "openbsd", "macos"]
url: "https://new.carta.tech/icmp/4"
---

# icmp(4)

Internet Control Message Protocol

## NAME

**icmp** – Internet Control Message Protocol

## SYNOPSIS

**#include \<sys/socket.h\>**

**#include \<netinet/in.h\>**

*int*\

**socket**(*AF\_INET*, *SOCK\_RAW*, *proto*);

## DESCRIPTION

ICMP is the error and control message protocol used by IP and the Internet protocol family.  It may be accessed through a “raw socket” for network monitoring and diagnostic functions. The *proto* parameter to the socket call to create an ICMP socket is obtained from [getprotobyname(3)](/getprotobyname/3.md). ICMP sockets are connectionless, and are normally used with the sendto and recvfrom calls, though the [connect(2)](/connect/2.md) call may also be used to fix the destination for future packets (in which case the  [read(2)](/read/2.md) or [recv(2)](/recv/2.md) and  [write(2)](/write/2.md) or [send(2)](/send/2.md) system calls may be used).

Outgoing packets automatically have an IP header prepended to them (based on the destination address). Incoming packets are received with the IP header and options intact. Note that the ip\_off and ip\_len fields are in host byte order. For more information about the IP header structure, see  [ip(4)](/ip/4.md).

### Non-privileged ICMP

ICMP sockets can be opened with the  SOCK\_DGRAM socket type without requiring root privileges. The synopsis is the following:

**socket**(*AF\_INET*, *SOCK\_DGRAM*, *IPPROTO\_ICMP*)

Datagram oriented ICMP sockets offer a subset of the functionality available to raw  ICMP sockets. Only IMCP request messages of the following types can be sent:  ICMP\_ECHO, ICMP\_TSTAMP or ICMP\_MASKREQ.  The code field must be the value zero (0).  The minimal length of an  ICMP message request is eight (8) octets.

The advantage of using datagram oriented ICMP sockets is that even a non-privileged process can use  ICMP echo requests to gauge the quality of the connectivity to a host,  or to receive  ICMP destination unreachable message for path MTU discovery, or to receive  time exceeded messages for traceroute.

The following  IP level option can be used with datagram oriented  ICMP sockets:

| |
| --- |
| IP\_OPTIONS |
| IP\_HDRINCL |
| IP\_TOS |
| IP\_TTL |
| IP\_RECVOPTS |
| IP\_RECVRETOPTS |
| IP\_RECVDSTADDR |
| IP\_RETOPTS |
| IP\_MULTICAST\_IF |
| IP\_MULTICAST\_TTL |
| IP\_MULTICAST\_LOOP |
| IP\_ADD\_MEMBERSHIP |
| IP\_DROP\_MEMBERSHIP |
| IP\_MULTICAST\_VIF |
| IP\_PORTRANGE |
| IP\_RECVIF |
| IP\_IPSEC\_POLICY |
| IP\_STRIPHDR |

When the   IP option IP\_HDRINCL is used, the provided  IP header must obey the following rules:

| | |
| --- | --- |
| ip\_v | Must be IPVERSION (4); |
| ip\_hl | Between 5 and 10 (inclusive); |
| ip\_tos | Any value; |
| ip\_len | Must be the total length of IP datagram (IP header + ICMP message); |
| ip\_id | Must be zero, will be automatically set; |
| ip\_off | Must be zero, will be automatically set; |
| ip\_ttl | Any value; |
| ip\_p | Must be IPPROTO\_IP; |
| ip\_sum | Value ignored, will be automatically set; |
| ip\_src | Must be an |
| ip\_dst | Any address; |
| ip\_opts | Any option. |

The maximum length of a IMCP message that can be sent is controlled  by the sysctl  variable net.inet.raw.maxdgram.

## DIAGNOSTICS

A socket operation may fail with one of the following errors returned:

- \[EISCONN\]

  when trying to establish a connection on a socket which already has one, or when trying to send a datagram with the destination address specified and the socket is already connected;

- \[ENOTCONN\]

  when trying to send a datagram, but no destination address is specified, and the socket hasn't been connected;

- \[ENOBUFS\]

  when the system runs out of memory for an internal data structure;

- \[EADDRNOTAVAIL\]

  when an attempt is made to create a  socket with a network address for which no network interface exists;

- \[EINVAL\]

  when an invalid value is used with IMCP datagram socket for a field of the   IP or ICMP header.

## SEE ALSO

[recv(2)](/recv/2.md), [send(2)](/send/2.md), [inet(4)](/inet/4.md), [intro(4)](/intro/4.md), [ip(4)](/ip/4.md)

## HISTORY

The **icmp** protocol appeared in 4.3BSD.
