1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
3 .\" %%%LICENSE_START(VERBATIM_ONE_PARA)
4 .\" Permission is granted to distribute possibly modified copies
5 .\" of this page provided the header is included verbatim,
6 .\" and in case of nontrivial modification author and date
7 .\" of the modification is added to the header.
10 .\" $Id: cmsg.3,v 1.8 2000/12/20 18:10:31 ak Exp $
11 .TH CMSG 3 2021-03-22 "Linux man-pages (unreleased)"
13 CMSG_ALIGN, CMSG_SPACE, CMSG_NXTHDR, CMSG_FIRSTHDR \- access ancillary data
16 .RI ( libc ", " \-lc )
19 .B #include <sys/socket.h>
21 .BI "struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *" msgh );
22 .BI "struct cmsghdr *CMSG_NXTHDR(struct msghdr *" msgh ,
23 .BR " struct cmsghdr *" cmsg );
24 .BI "size_t CMSG_ALIGN(size_t " length );
25 .BI "size_t CMSG_SPACE(size_t " length );
26 .BI "size_t CMSG_LEN(size_t " length );
27 .BI "unsigned char *CMSG_DATA(struct cmsghdr *" cmsg );
30 These macros are used to create and access control messages (also called
31 ancillary data) that are not a part of the socket payload.
32 This control information may
33 include the interface the packet was received on, various rarely used header
34 fields, an extended error description, a set of file descriptors, or UNIX
36 For instance, control messages can be used to send
37 additional header fields such as IP options.
38 Ancillary data is sent by calling
40 and received by calling
42 See their manual pages for more information.
44 Ancillary data is a sequence of
46 structures with appended data.
47 See the specific protocol man pages for the available control message types.
48 The maximum ancillary buffer size allowed per socket can be set using
49 .IR /proc/sys/net/core/optmem_max ;
55 structure is defined as follows:
60 size_t cmsg_len; /* Data byte count, including header
61 (type is socklen_t in POSIX) */
62 int cmsg_level; /* Originating protocol */
63 int cmsg_type; /* Protocol\-specific type */
65 unsigned char cmsg_data[]; */
72 structures should never be accessed directly.
73 Instead, use only the following macros:
76 returns a pointer to the first
79 data buffer associated with the passed
81 It returns NULL if there isn't enough space for a
86 returns the next valid
90 It returns NULL when there isn't enough space left in the buffer.
92 When initializing a buffer that will contain a series of
94 structures (e.g., to be sent with
96 that buffer should first be zero-initialized
97 to ensure the correct operation of
101 given a length, returns it including the required alignment.
106 returns the number of bytes an ancillary element with payload of the
107 passed data length occupies.
108 This is a constant expression.
111 returns a pointer to the data portion of a
113 The pointer returned cannot be assumed to be suitably aligned for
114 accessing arbitrary payload data types.
115 Applications should not cast it to a pointer type matching the payload,
116 but should instead use
118 to copy data to or from a suitably declared object.
121 returns the value to store in the
125 structure, taking into account any necessary
127 It takes the data length as an argument.
131 To create ancillary data, first initialize the
135 with the length of the control message buffer.
140 to get the first control message and
142 to get all subsequent ones.
143 In each control message, initialize
149 header fields, and the data portion using
155 should be set to the sum of the
158 all control messages in the buffer.
159 For more information on the
164 This ancillary data model conforms to the POSIX.1g draft, 4.4BSD-Lite,
165 the IPv6 advanced API described in RFC\ 2292 and SUSv2.
166 .BR CMSG_FIRSTHDR (),
170 are specified in POSIX.1-2008.
174 .\" https://www.austingroupbugs.net/view.php?id=978#c3242
175 will be included in the next POSIX release (Issue 8).
178 is a Linux extension.
180 For portability, ancillary data should be accessed using only the macros
183 is a Linux extension and should not be used in portable programs.
190 are constant expressions (assuming their argument is constant),
191 meaning that these values can be used to declare the size of global variables.
192 This may not be portable, however.
194 This code looks for the
196 option in a received ancillary buffer:
201 struct cmsghdr *cmsg;
204 /* Receive auxiliary data in msgh */
206 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
207 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
208 if (cmsg\->cmsg_level == IPPROTO_IP
209 && cmsg\->cmsg_type == IP_TTL) {
210 memcpy(&receive_ttl, CMSG_DATA(cmsg), sizeof(received_ttl));
216 /* Error: IP_TTL not enabled or small buffer or I/O error */
221 The code below passes an array of file descriptors over a
222 UNIX domain socket using
227 struct msghdr msg = { 0 };
228 struct cmsghdr *cmsg;
229 int myfds[NUM_FD]; /* Contains the file descriptors to pass */
233 .iov_len = sizeof(iobuf)
235 union { /* Ancillary data buffer, wrapped in a union
236 in order to ensure it is suitably aligned */
237 char buf[CMSG_SPACE(sizeof(myfds))];
238 struct cmsghdr align;
243 msg.msg_control = u.buf;
244 msg.msg_controllen = sizeof(u.buf);
245 cmsg = CMSG_FIRSTHDR(&msg);
246 cmsg\->cmsg_level = SOL_SOCKET;
247 cmsg\->cmsg_type = SCM_RIGHTS;
248 cmsg\->cmsg_len = CMSG_LEN(sizeof(myfds));
249 memcpy(CMSG_DATA(cmsg), myfds, sizeof(myfds));
253 For a complete code example that shows passing of file descriptors
254 over a UNIX domain socket, see
255 .BR seccomp_unotify (2).