CMSG(3)
โจ CMSG(3) โจ โ the ancillary data girlies ๐
okay bestie, so this is a page from the Library Functions Manual for CMSG(3), and no cap, if you've ever wanted to send secret little side-notes alongside your socket payload (fds, credentials, IP options, the whole vibe) this is your page. ๐
๐ธ NAME ๐ธ
CMSG_ALIGN, CMSG_SPACE, CMSG_NXTHDR, CMSG_FIRSTHDR โ access ancillary data. it's giving "control messages, but make it accessible". periodt. ๐
๐ซถ LIBRARY ๐ซถ
Standard C library (libc, -lc). the classic. the icon. ๐
โจ SYNOPSIS โจ
#include <sys/socket.h>
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *msgh);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *msgh,
struct cmsghdr *cmsg);
size_t CMSG_ALIGN(size_t length);
size_t CMSG_SPACE(size_t length);
size_t CMSG_LEN(size_t length);
unsigned char *CMSG_DATA(struct cmsghdr *cmsg);
six whole macros. we love a full roster. ๐
๐ DESCRIPTION ๐
so these macros are literally the way you create and access control messages (aka ancillary data), which are the lil extras that are not part of the socket payload. main character energy that travels out-of-band, lowkey. โจ
this control info can include:
- the interface the packet was received on ๐ฆ
- various rarely used header fields
- an extended error description ๐ญ
- a set of file descriptors
- UNIX credentials ๐
for instance (pun intended), control messages can be used to send additional header fields such as IP options.
Ancillary data is sent by calling sendmsg(2) and received by calling recvmsg(2). go read their manual pages for more info, they're the ones who do the actual sending and receiving. ๐ซถ
Ancillary data is a sequence of cmsghdr structures with appended data. See the specific protocol man pages for the available control message types. The maximum ancillary buffer size allowed per socket can be set using /proc/sys/net/core/optmem_max; see socket(7). ๐พ
The cmsghdr structure is defined as follows:
struct cmsghdr {
size_t cmsg_len; /* Data byte count, including header
(type is socklen_t in POSIX) */
int cmsg_level; /* Originating protocol */
int cmsg_type; /* Protocol-specific type */
/* followed by
unsigned char cmsg_data[]; */
};
๐จ GIRLIE LISTEN. The sequence of cmsghdr structures should never be accessed directly. not even once. not even a peek. Instead, use only the following macros. this is not a drill, this is the vibe check. ๐
๐ the macros, one by one ๐
CMSG_FIRSTHDR()โ returns a pointer to the firstcmsghdrin the ancillary data buffer associated with the passedmsghdr. It returnsNULLif there isn't enough space for acmsghdrin the buffer. it's the opening act. ๐CMSG_NXTHDR()โ returns the next validcmsghdrafter the passedcmsghdr. It returnsNULLwhen there isn't enough space left in the buffer. the sequel, the part two. ๐ฆ
When initializing a buffer that will contain a series ofcmsghdrstructures (e.g., to be sent withsendmsg(2)), that buffer should first be zero-initialized to ensure the correct operation ofCMSG_NXTHDR(). zero it out bestie, fr fr. ๐งผ
CMSG_ALIGN(), โ given a length, returns it including the required alignment. This is a constant expression. โจCMSG_SPACE()โ returns the number of bytes an ancillary element with payload of the passed data length occupies. This is a constant expression. ๐CMSG_DATA()โ returns a pointer to the data portion of acmsghdr. The pointer returned cannot be assumed to be suitably aligned for accessing arbitrary payload data types. Applications should not cast it to a pointer type matching the payload, but should instead usememcpy(3)to copy data to or from a suitably declared object. no casting, we don't do that here. ๐CMSG_LEN()โ returns the value to store in thecmsg_lenmember of thecmsghdrstructure, taking into account any necessary alignment. It takes the data length as an argument. This is a constant expression. ๐
๐ ๏ธ how to actually build one of these ๐ ๏ธ
To create ancillary data, first initialize the msg_controllen member of the msghdr with the length of the control message buffer. Use CMSG_FIRSTHDR() on the msghdr to get the first control message and CMSG_NXTHDR() to get all subsequent ones. In each control message, initialize cmsg_len (with CMSG_LEN()), the other cmsghdr header fields, and the data portion using CMSG_DATA(). Finally, the msg_controllen field of the msghdr should be set to the sum of the CMSG_SPACE() of the length of all control messages in the buffer. For more information on the msghdr, see recvmsg(2). ๐ซถโจ
๐ VERSIONS ๐
For portability, ancillary data should be accessed using only the macros described here. no freestyling. ๐
In Linux, CMSG_LEN(), CMSG_DATA(), and CMSG_ALIGN() are constant expressions (assuming their argument is constant), meaning that these values can be used to declare the size of global variables. This may not be portable, however. โ ๏ธ
๐ STANDARDS ๐
CMSG_FIRSTHDR()CMSG_NXTHDR()CMSG_DATA()
โ POSIX.1-2008. ๐
CMSG_SPACE()CMSG_LEN()CMSG_ALIGN()
โ Linux. ๐ง
๐ฆ HISTORY ๐ฆ
This ancillary data model conforms to the POSIX.1g draft, 4.4BSD-Lite, the IPv6 advanced API described in RFC 2292 and SUSv2. a whole lineage, we love a well-documented origin story. ๐
CMSG_SPACE() and CMSG_LEN() will be included in the next POSIX release (Issue 8). manifesting it. โจ
๐ฅ EXAMPLES ๐ฅ
example one: sniffing out IP_TTL ๐
This code looks for the IP_TTL option in a received ancillary buffer:
struct msghdr msgh;
struct cmsghdr *cmsg;
int received_ttl;
/* Receive auxiliary data in msgh */
for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
if (cmsg->cmsg_level == IPPROTO_IP
&& cmsg->cmsg_type == IP_TTL) {
memcpy(&receive_ttl, CMSG_DATA(cmsg), sizeof(received_ttl));
break;
}
}
if (cmsg == NULL) {
/* Error: IP_TTL not enabled or small buffer or I/O error */
}
if cmsg comes back NULL, it's giving: IP_TTL not enabled, or small buffer, or I/O error. ๐ญ
example two: passing fds with SCM_RIGHTS ๐
The code below passes an array of file descriptors over a UNIX domain socket using SCM_RIGHTS:
struct msghdr msg = { 0 };
struct cmsghdr *cmsg;
int myfds[NUM_FD]; /* Contains the file descriptors to pass */
char iobuf[1];
struct iovec io = {
.iov_base = iobuf,
.iov_len = sizeof(iobuf)
};
union { /* Ancillary data buffer, wrapped in a union
in order to ensure it is suitably aligned */
char buf[CMSG_SPACE(sizeof(myfds))];
struct cmsghdr align;
} u;
msg.msg_iov = &io;
msg.msg_iovlen = 1;
msg.msg_control = u.buf;
msg.msg_controllen = sizeof(u.buf);
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(myfds));
memcpy(CMSG_DATA(cmsg), myfds, sizeof(myfds));
the union trick for alignment? iconic. it ate and left no crumbs. ๐
For a complete code example that shows passing of file descriptors over a UNIX domain socket, see seccomp_unotify(2). ๐ซถ
๐ SEE ALSO ๐
and that's the whole page bestie!! you now know more about ancillary data than like 90% of the people you will ever meet. obsessed for you. ๐โจ
so this page came out of Linux man-pages 6.20, and it's dated 2026-02-08. ๐ go forth, send your control messages, zero-initialize your buffers, and slay. โจ๐ฆ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-1-para
- Copyright 1999, Andi Kleen <andi@firstfloor.org>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms