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" "Linux Programmer's Manual"
13 CMSG_ALIGN, CMSG_SPACE, CMSG_NXTHDR, CMSG_FIRSTHDR \- access ancillary data
16 .B #include <sys/socket.h>
18 .BI "struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *" msgh );
19 .BI "struct cmsghdr *CMSG_NXTHDR(struct msghdr *" msgh ,
20 .BR " struct cmsghdr *" cmsg );
21 .BI "size_t CMSG_ALIGN(size_t " length );
22 .BI "size_t CMSG_SPACE(size_t " length );
23 .BI "size_t CMSG_LEN(size_t " length );
24 .BI "unsigned char *CMSG_DATA(struct cmsghdr *" cmsg );
27 These macros are used to create and access control messages (also called
28 ancillary data) that are not a part of the socket payload.
29 This control information may
30 include the interface the packet was received on, various rarely used header
31 fields, an extended error description, a set of file descriptors, or UNIX
33 For instance, control messages can be used to send
34 additional header fields such as IP options.
35 Ancillary data is sent by calling
37 and received by calling
39 See their manual pages for more information.
41 Ancillary data is a sequence of
43 structures with appended data.
44 See the specific protocol man pages for the available control message types.
45 The maximum ancillary buffer size allowed per socket can be set using
46 .IR /proc/sys/net/core/optmem_max ;
52 structure is defined as follows:
57 size_t cmsg_len; /* Data byte count, including header
58 (type is socklen_t in POSIX) */
59 int cmsg_level; /* Originating protocol */
60 int cmsg_type; /* Protocol\-specific type */
62 unsigned char cmsg_data[]; */
69 structures should never be accessed directly.
70 Instead, use only the following macros:
73 returns a pointer to the first
76 data buffer associated with the passed
78 It returns NULL if there isn't enough space for a
83 returns the next valid
87 It returns NULL when there isn't enough space left in the buffer.
89 When initializing a buffer that will contain a series of
91 structures (e.g., to be sent with
93 that buffer should first be zero-initialized
94 to ensure the correct operation of
98 given a length, returns it including the required alignment.
103 returns the number of bytes an ancillary element with payload of the
104 passed data length occupies.
105 This is a constant expression.
108 returns a pointer to the data portion of a
110 The pointer returned cannot be assumed to be suitably aligned for
111 accessing arbitrary payload data types.
112 Applications should not cast it to a pointer type matching the payload,
113 but should instead use
115 to copy data to or from a suitably declared object.
118 returns the value to store in the
122 structure, taking into account any necessary
124 It takes the data length as an argument.
128 To create ancillary data, first initialize the
132 with the length of the control message buffer.
137 to get the first control message and
139 to get all subsequent ones.
140 In each control message, initialize
146 header fields, and the data portion using
152 should be set to the sum of the
155 all control messages in the buffer.
156 For more information on the
161 This ancillary data model conforms to the POSIX.1g draft, 4.4BSD-Lite,
162 the IPv6 advanced API described in RFC\ 2292 and SUSv2.
163 .BR CMSG_FIRSTHDR (),
167 are specified in POSIX.1-2008.
171 .\" https://www.austingroupbugs.net/view.php?id=978#c3242
172 will be included in the next POSIX release (Issue 8).
175 is a Linux extension.
177 For portability, ancillary data should be accessed using only the macros
180 is a Linux extension and should not be used in portable programs.
187 are constant expressions (assuming their argument is constant),
188 meaning that these values can be used to declare the size of global variables.
189 This may not be portable, however.
191 This code looks for the
193 option in a received ancillary buffer:
198 struct cmsghdr *cmsg;
201 /* Receive auxiliary data in msgh */
203 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
204 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
205 if (cmsg\->cmsg_level == IPPROTO_IP
206 && cmsg\->cmsg_type == IP_TTL) {
207 memcpy(&receive_ttl, CMSG_DATA(cmsg), sizeof(received_ttl));
213 /* Error: IP_TTL not enabled or small buffer or I/O error */
218 The code below passes an array of file descriptors over a
219 UNIX domain socket using
224 struct msghdr msg = { 0 };
225 struct cmsghdr *cmsg;
226 int myfds[NUM_FD]; /* Contains the file descriptors to pass */
230 .iov_len = sizeof(iobuf)
232 union { /* Ancillary data buffer, wrapped in a union
233 in order to ensure it is suitably aligned */
234 char buf[CMSG_SPACE(sizeof(myfds))];
235 struct cmsghdr align;
240 msg.msg_control = u.buf;
241 msg.msg_controllen = sizeof(u.buf);
242 cmsg = CMSG_FIRSTHDR(&msg);
243 cmsg\->cmsg_level = SOL_SOCKET;
244 cmsg\->cmsg_type = SCM_RIGHTS;
245 cmsg\->cmsg_len = CMSG_LEN(sizeof(myfds));
246 memcpy(CMSG_DATA(cmsg), myfds, sizeof(myfds));
250 For a complete code example that shows passing of file descriptors
251 over a UNIX domain socket, see
252 .BR seccomp_unotify (2).