xref: /xnu-11215/bsd/sys/socket_private.h (revision 8d741a5d)
1 /*
2  * Copyright (c) 2023 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
29 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 /*
31  * Copyright (c) 1982, 1985, 1986, 1988, 1993, 1994
32  *	The Regents of the University of California.  All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  * 3. All advertising materials mentioning features or use of this software
43  *    must display the following acknowledgement:
44  *	This product includes software developed by the University of
45  *	California, Berkeley and its contributors.
46  * 4. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  *
62  *	@(#)socket.h	8.4 (Berkeley) 2/21/94
63  * $FreeBSD: src/sys/sys/socket.h,v 1.39.2.7 2001/07/03 11:02:01 ume Exp $
64  */
65 /*
66  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
67  * support for mandatory and extensible security protections.  This notice
68  * is included in support of clause 2.2 (b) of the Apple Public License,
69  * Version 2.0.
70  */
71 
72 #ifndef _SYS_SOCKET_PRIVATE_H_
73 #define _SYS_SOCKET_PRIVATE_H_
74 
75 #include <sys/types.h>
76 #include <sys/cdefs.h>
77 #include <sys/constrained_ctypes.h>
78 #include <sys/socket.h>
79 
80 #include <sys/param.h>
81 #include <uuid/uuid.h>
82 
83 #ifdef XNU_KERNEL_PRIVATE
84 #include <kern/assert.h>
85 #include <kern/kalloc.h>
86 #endif /* XNU_KERNEL_PRIVATE */
87 
88 /*
89  * Option flags per-socket.
90  */
91 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
92 #ifdef __APPLE__
93 #define SO_NOWAKEFROMSLEEP      0x10000 /* Don't wake for traffic to this socket */
94 #define SO_NOAPNFALLBK          0x20000 /* Don't attempt APN fallback for the socket */
95 #define SO_TIMESTAMP_CONTINUOUS 0x40000 /* Continuous monotonic timestamp on rcvd dgram */
96 
97 /*
98  * Additional options, not kept in so_options.
99  */
100 #define SO_RESTRICTIONS 0x1081          /* APPLE: deny flag set */
101 #define  SO_RESTRICT_DENY_IN    0x1     /* deny inbound (trapdoor) */
102 #define  SO_RESTRICT_DENY_OUT   0x2     /* deny outbound (trapdoor) */
103 #define  SO_RESTRICT_DENY_CELLULAR 0x4  /* deny use of cellular (trapdoor) */
104 #define  SO_RESTRICT_DENY_EXPENSIVE 0x8 /* deny use of expensive if (trapdoor) */
105 #define  SO_RESTRICT_DENY_CONSTRAINED 0x10 /* deny use of expensive if (trapdoor) */
106 #endif
107 
108 #define SO_EXECPATH     0x1085          /* Application Firewall Socket option */
109 
110 /*
111  * Traffic service class definitions (lowest to highest):
112  *
113  * SO_TC_BK_SYS
114  *	"Background System-Initiated", high delay tolerant, high loss
115  *	tolerant, elastic flow, variable size & long-lived.  E.g: system-
116  *	initiated iCloud synching or Time Capsule backup, for which there
117  *	is no progress feedbacks.
118  *
119  * SO_TC_BK
120  *	"Background", user-initiated, high delay tolerant, high loss tolerant,
121  *	elastic flow, variable size.  E.g. user-initiated iCloud synching or
122  *	Time Capsule backup; or traffics of background applications, for which
123  *	there is some progress feedbacks.
124  *
125  * SO_TC_BE
126  *	"Best Effort", unclassified/standard.  This is the default service
127  *	class; pretty much a mix of everything.
128  *
129  * SO_TC_RD
130  *	"Responsive Data", a notch higher than "Best Effort", medium delay
131  *	tolerant, elastic & inelastic flow, bursty, long-lived.  E.g. email,
132  *	instant messaging, for which there is a sense of interactivity and
133  *	urgency (user waiting for output).
134  *
135  * SO_TC_OAM
136  *	"Operations, Administration, and Management", medium delay tolerant,
137  *	low-medium loss tolerant, elastic & inelastic flows, variable size.
138  *	E.g. VPN tunnels.
139  *
140  * SO_TC_AV
141  *	"Multimedia Audio/Video Streaming", medium delay tolerant, low-medium
142  *	loss tolerant, elastic flow, constant packet interval, variable rate &
143  *	size.  E.g. AirPlay playback (both video and audio).
144  *
145  * SO_TC_RV
146  *	"Responsive Multimedia Audio/Video", low delay tolerant, low-medium
147  *	loss tolerant, elastic flow, variable packet interval, rate and size.
148  *	E.g. AirPlay mirroring, screen sharing.
149  *
150  * SO_TC_VI
151  *	"Interactive Video", low delay tolerant, low-medium loss tolerant,
152  *	elastic flow, constant packet interval, variable rate & size.  E.g.
153  *	FaceTime video.
154  *
155  * SO_TC_VO
156  *	"Interactive Voice", low delay tolerant, low loss tolerant, inelastic
157  *	flow, constant packet rate, somewhat fixed size.  E.g. VoIP including
158  *	FaceTime audio.
159  *
160  * SO_TC_CTL
161  *	"Network Control", low delay tolerant, low loss tolerant, inelastic
162  *	flow, rate is bursty but short, variable size.  E.g. DNS queries;
163  *	certain types of locally-originated ICMP, ICMPv6; IGMP/MLD join/leave,
164  *	ARP.
165  */
166 #define SO_TRAFFIC_CLASS        0x1086  /* Traffic service class (int) */
167 #define SO_TC_BK_SYS    100             /* lowest class */
168 #define SO_TC_BK        200
169 #define SO_TC_BE        0
170 #define SO_TC_RD        300
171 #define SO_TC_OAM       400
172 #define SO_TC_AV        500
173 #define SO_TC_RV        600
174 #define SO_TC_VI        700
175 #define SO_TC_VO        800
176 #define SO_TC_CTL       900             /* highest class */
177 #define SO_TC_MAX       10              /* Total # of traffic classes */
178 #ifdef XNU_KERNEL_PRIVATE
179 #define _SO_TC_BK       1               /* deprecated */
180 #define _SO_TC_VI       2               /* deprecated */
181 #define _SO_TC_VO       3               /* deprecated */
182 #define _SO_TC_MAX      4               /* deprecated */
183 
184 #define SO_VALID_TC(c)                                                  \
185 	(c == SO_TC_BK_SYS || c == SO_TC_BK || c == SO_TC_BE ||         \
186 	c == SO_TC_RD || c == SO_TC_OAM || c == SO_TC_AV ||             \
187 	c == SO_TC_RV || c == SO_TC_VI || c == SO_TC_VO ||              \
188 	c == SO_TC_CTL || c == SO_TC_NETSVC_SIG)
189 
190 #define SO_TC_UNSPEC    ((int)-1)               /* Traffic class not specified */
191 
192 #define SO_TC_SIG       SO_TC_VI                /* to be removed XXX */
193 
194 #define SOTCIX_BK_SYS   0
195 #define SOTCIX_BK       1
196 #define SOTCIX_BE       2
197 #define SOTCIX_RD       3
198 #define SOTCIX_OAM      4
199 #define SOTCIX_AV       5
200 #define SOTCIX_RV       6
201 #define SOTCIX_VI       7
202 #define SOTCIX_VO       8
203 #define SOTCIX_CTL      9
204 #endif /* XNU_KERNEL_PRIVATE */
205 
206 /* Background socket configuration flags  */
207 #define TRAFFIC_MGT_SO_BACKGROUND       0x0001  /* background socket */
208 #define TRAFFIC_MGT_TCP_RECVBG          0x0002  /* Only TCP sockets, receiver throttling */
209 
210 #define SO_RECV_TRAFFIC_CLASS   0x1087          /* Receive traffic class (bool) */
211 #define SO_TRAFFIC_CLASS_DBG    0x1088          /* Debug traffic class (struct so_tcdbg) */
212 #define SO_OPTION_UNUSED_0      0x1089          /* Traffic class statistics */
213 #define SO_PRIVILEGED_TRAFFIC_CLASS 0x1090      /* Privileged traffic class (bool) */
214 #define SO_DEFUNCTIT    0x1091          /* Defunct a socket (only in internal builds) */
215 #define SO_DEFUNCTOK    0x1100          /* can be defunct'd */
216 #define SO_ISDEFUNCT    0x1101          /* get defunct status */
217 
218 #define SO_OPPORTUNISTIC        0x1102  /* deprecated; use SO_TRAFFIC_CLASS */
219 
220 /*
221  * SO_FLUSH flushes any unsent data generated by a given socket.  It takes
222  * an integer parameter, which can be any of the SO_TC traffic class values,
223  * or the special SO_TC_ALL value.
224  */
225 #define SO_FLUSH        0x1103          /* flush unsent data (int) */
226 #define  SO_TC_ALL      (-1)
227 
228 #define SO_RECV_ANYIF   0x1104          /* unrestricted inbound processing */
229 #define SO_TRAFFIC_MGT_BACKGROUND       0x1105  /* Background traffic management */
230 
231 #define SO_FLOW_DIVERT_TOKEN    0x1106  /* flow divert token */
232 
233 #define SO_DELEGATED            0x1107  /* set socket as delegate (pid_t) */
234 #define SO_DELEGATED_UUID       0x1108  /* set socket as delegate (uuid_t) */
235 #define SO_NECP_ATTRIBUTES      0x1109  /* NECP socket attributes (domain, account, etc.) */
236 #define SO_CFIL_SOCK_ID         0x1110  /* get content filter socket ID (cfil_sock_id_t) */
237 #define SO_NECP_CLIENTUUID      0x1111  /* NECP Client uuid */
238 
239 #define SO_AWDL_UNRESTRICTED    0x1113  /* try to use AWDL in restricted mode */
240 #define SO_EXTENDED_BK_IDLE     0x1114  /* extended time to keep socket idle after app is suspended (int) */
241 #define SO_MARK_CELLFALLBACK    0x1115  /* Mark as initiated by cell fallback */
242 
243 #define SO_QOSMARKING_POLICY_OVERRIDE   0x1117  /* int */
244 #define SO_INTCOPROC_ALLOW              0x1118  /* Try to use internal co-processor interfaces. */
245 
246 #define SO_NECP_LISTENUUID         0x1120  /* NECP client UUID for listener */
247 #define SO_MPKL_SEND_INFO          0x1122  /* (struct so_mpkl_send_info) */
248 #define SO_STATISTICS_EVENT        0x1123  /* int64 argument, an event in statistics collection */
249 #define SO_WANT_KEV_SOCKET_CLOSED  0x1124  /* want delivery of KEV_SOCKET_CLOSED (int) */
250 #define SO_MARK_KNOWN_TRACKER      0x1125  /* Mark as a connection to a known tracker */
251 #define SO_MARK_KNOWN_TRACKER_NON_APP_INITIATED 0x1126  /* Mark tracker connection to be non-app initiated */
252 #define SO_MARK_WAKE_PKT           0x1127  /* Mark next packet as a wake packet, one shot (int) */
253 #define SO_RECV_WAKE_PKT           0x1128  /* Receive wake packet indication as ancillary data (int) */
254 #define SO_MARK_APPROVED_APP_DOMAIN 0x1129 /* Mark connection as being for an approved associated app domain */
255 #define SO_FALLBACK_MODE           0x1130  /* Indicates the mode of fallback used */
256 
257 #define SO_MARK_CELLFALLBACK_UUID  0x1132  /* Mark as initiated by cell fallback using UUID of the connection */
258 #define SO_APPLICATION_ID          0x1133  /* ID of attributing app - so_application_id_t */
259                                            /* 0x1134 is SO_BINDTODEVICE, see socket.h */
260 #define SO_MARK_DOMAIN_INFO_SILENT 0x1135  /* Domain information should be silently withheld */
261 
262 struct so_mark_cellfallback_uuid_args {
263 	uuid_t flow_uuid;
264 	int flow_cellfallback;
265 };
266 
267 typedef struct {
268 	uid_t               uid;
269 	uuid_t              effective_uuid;
270 	uid_t               persona_id;
271 } so_application_id_t;
272 
273 #define _NET_SERVICE_TYPE_COUNT 9
274 #define _NET_SERVICE_TYPE_UNSPEC        ((int)-1)
275 
276 #define IS_VALID_NET_SERVICE_TYPE(c)                            \
277 	(c >= NET_SERVICE_TYPE_BE && c <= NET_SERVICE_TYPE_RD)
278 
279 extern const int sotc_by_netservicetype[_NET_SERVICE_TYPE_COUNT];
280 
281 /*
282  * Facility to pass Network Service Type values using SO_TRAFFIC_CLASS
283  * Mostly useful to simplify implementation of frameworks to adopt the new
284  * Network Service Type values for Signaling.
285  */
286 #define SO_TC_NET_SERVICE_OFFSET        10000
287 #define SO_TC_NETSVC_SIG        (SO_TC_NET_SERVICE_OFFSET + NET_SERVICE_TYPE_SIG)
288 
289 #ifdef __APPLE__
290 
291 #ifdef KERNEL_PRIVATE
292 #define SONPX_MASK_VALID                (SONPX_SETOPTSHUT)
293 #define IS_SO_TC_BACKGROUND(_tc_) ((_tc_) == SO_TC_BK || (_tc_) == SO_TC_BK_SYS)
294 #define IS_SO_TC_BACKGROUNDSYSTEM(_tc_) ((_tc_) == SO_TC_BK_SYS)
295 #endif /* KERNEL_PRIVATE */
296 
297 #endif
298 
299 /*
300  * Address families.
301  */
302 #define AF_AFP  36                      /* Used by AFP */
303 #define AF_MULTIPATH    39
304 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
305 
306 /*
307  * Protocol families, same as address families for now.
308  */
309 #define PF_AFP          AF_AFP
310 #define PF_MULTIPATH    AF_MULTIPATH
311 
312 #ifdef KERNEL_PRIVATE
313 #define PF_BRIDGE       ((uint32_t)0x62726467)  /* 'brdg' */
314 #define PF_NULL         ((uint32_t)0x6e756c6c)  /* 'null' */
315 
316 #define CTL_NET_NAMES { \
317 	{ 0, 0 }, \
318 	{ "local", CTLTYPE_NODE }, \
319 	{ "inet", CTLTYPE_NODE }, \
320 	{ "implink", CTLTYPE_NODE }, \
321 	{ "pup", CTLTYPE_NODE }, \
322 	{ "chaos", CTLTYPE_NODE }, \
323 	{ "xerox_ns", CTLTYPE_NODE }, \
324 	{ "iso", CTLTYPE_NODE }, \
325 	{ "emca", CTLTYPE_NODE }, \
326 	{ "datakit", CTLTYPE_NODE }, \
327 	{ "ccitt", CTLTYPE_NODE }, \
328 	{ "ibm_sna", CTLTYPE_NODE }, \
329 	{ "decnet", CTLTYPE_NODE }, \
330 	{ "dec_dli", CTLTYPE_NODE }, \
331 	{ "lat", CTLTYPE_NODE }, \
332 	{ "hylink", CTLTYPE_NODE }, \
333 	{ "appletalk", CTLTYPE_NODE }, \
334 	{ "route", CTLTYPE_NODE }, \
335 	{ "link_layer", CTLTYPE_NODE }, \
336 	{ "xtp", CTLTYPE_NODE }, \
337 	{ "coip", CTLTYPE_NODE }, \
338 	{ "cnt", CTLTYPE_NODE }, \
339 	{ "rtip", CTLTYPE_NODE }, \
340 	{ "ipx", CTLTYPE_NODE }, \
341 	{ "sip", CTLTYPE_NODE }, \
342 	{ "pip", CTLTYPE_NODE }, \
343 	{ 0, 0 }, \
344 	{ "ndrv", CTLTYPE_NODE }, \
345 	{ "isdn", CTLTYPE_NODE }, \
346 	{ "key", CTLTYPE_NODE }, \
347 	{ "inet6", CTLTYPE_NODE }, \
348 	{ "natm", CTLTYPE_NODE }, \
349 	{ "sys", CTLTYPE_NODE }, \
350 	{ "netbios", CTLTYPE_NODE }, \
351 	{ "ppp", CTLTYPE_NODE }, \
352 	{ "hdrcomplete", CTLTYPE_NODE }, \
353 	{ "vsock", CTLTYPE_NODE }, \
354 }
355 #endif /* KERNEL_PRIVATE */
356 
357 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
358 /*
359  * PF_ROUTE - Routing table
360  *
361  * Three additional levels are defined:
362  *	Fourth: address family, 0 is wildcard
363  *	Fifth: type of info, defined below
364  *	Sixth: flag(s) to mask with for NET_RT_FLAGS
365  */
366 #define NET_RT_DUMPX            8       /* private */
367 #define NET_RT_DUMPX_FLAGS      9       /* private */
368 #define NET_RT_STAT_64          11      /* private */
369 #endif /* (_POSIX_C_SOURCE && !_DARWIN_C_SOURCE) */
370 
371 /* These are supported values for SO_STATISTICS_EVENT */
372 #define SO_STATISTICS_EVENT_ENTER_CELLFALLBACK (1 << 0)
373 #define SO_STATISTICS_EVENT_EXIT_CELLFALLBACK  (1 << 1)
374 #define SO_STATISTICS_EVENT_RESERVED_1         (1 << 2)
375 #define SO_STATISTICS_EVENT_RESERVED_2         (1 << 3)
376 
377 
378 #ifdef KERNEL_PRIVATE
379 #define CTL_NET_RT_NAMES { \
380 	{ 0, 0 }, \
381 	{ "dump", CTLTYPE_STRUCT }, \
382 	{ "flags", CTLTYPE_STRUCT }, \
383 	{ "iflist", CTLTYPE_STRUCT }, \
384 	{ "stat", CTLTYPE_STRUCT }, \
385 	{ "trash", CTLTYPE_INT }, \
386 	{ "iflist2", CTLTYPE_STRUCT }, \
387 	{ "dump2", CTLTYPE_STRUCT }, \
388 	{ "dumpx", CTLTYPE_STRUCT }, \
389 	{ "dumpx_flags", CTLTYPE_STRUCT }, \
390 }
391 
392 #endif /* KERNEL_PRIVATE */
393 
394 /*
395  * Extended version for sendmsg_x() and recvmsg_x() calls
396  *
397  * For recvmsg_x(), the size of the data received is given by the field
398  * msg_datalen.
399  *
400  * For sendmsg_x(), the size of the data to send is given by the length of
401  * the iovec array -- like sendmsg(). The field msg_datalen is ignored.
402  */
403 struct msghdr_x {
404 	void            *__sized_by(msg_namelen) msg_name; /* optional address */
405 	socklen_t       msg_namelen;    /* size of address */
406 	struct iovec    *msg_iov;       /* scatter/gather array */
407 	int             msg_iovlen;     /* # elements in msg_iov */
408 	void            *__sized_by(msg_controllen) msg_control; /* ancillary data, see below */
409 	socklen_t       msg_controllen; /* ancillary data buffer len */
410 	int             msg_flags;      /* flags on received message */
411 	size_t          msg_datalen;    /* byte length of buffer in msg_iov */
412 };
413 
414 #ifdef XNU_KERNEL_PRIVATE
415 /*
416  * In-kernel representation of "struct msghdr" from
417  * userspace. Has enough precision for 32-bit or
418  * 64-bit clients, but does not need to be packed.
419  */
420 
421 struct user_msghdr {
422 	user_addr_t     msg_name;               /* optional address */
423 	socklen_t       msg_namelen;            /* size of address */
424 	vm_address_t    msg_iov;                /* scatter/gather array */
425 	int             msg_iovlen;             /* # elements in msg_iov */
426 	user_addr_t     msg_control;            /* ancillary data, see below */
427 	socklen_t       msg_controllen;         /* ancillary data buffer len */
428 	int             msg_flags;              /* flags on received message */
429 };
430 __CCT_DECLARE_CONSTRAINED_PTR_TYPES(struct user_msghdr, user_msghdr);
431 
432 /*
433  * LP64 user version of struct msghdr.
434  * WARNING - keep in sync with struct msghdr
435  */
436 
437 struct user64_msghdr {
438 	user64_addr_t   msg_name;               /* optional address */
439 	socklen_t       msg_namelen;            /* size of address */
440 	user64_addr_t   msg_iov;                /* scatter/gather array */
441 	int             msg_iovlen;             /* # elements in msg_iov */
442 	user64_addr_t   msg_control;            /* ancillary data, see below */
443 	socklen_t       msg_controllen;         /* ancillary data buffer len */
444 	int             msg_flags;              /* flags on received message */
445 };
446 __CCT_DECLARE_CONSTRAINED_PTR_TYPES(struct user64_msghdr, user64_msghdr);
447 
448 /*
449  * ILP32 user version of struct msghdr.
450  * WARNING - keep in sync with struct msghdr
451  */
452 
453 struct user32_msghdr {
454 	user32_addr_t   msg_name;       /* optional address */
455 	socklen_t       msg_namelen;    /* size of address */
456 	user32_addr_t   msg_iov;        /* scatter/gather array */
457 	int             msg_iovlen;     /* # elements in msg_iov */
458 	user32_addr_t   msg_control;    /* ancillary data, see below */
459 	socklen_t       msg_controllen; /* ancillary data buffer len */
460 	int             msg_flags;      /* flags on received message */
461 };
462 __CCT_DECLARE_CONSTRAINED_PTR_TYPES(struct user32_msghdr, user32_msghdr);
463 
464 /*
465  * In-kernel representation of "struct msghdr_x" from
466  * userspace. Has enough precision for 32-bit or
467  * 64-bit clients, but does not need to be packed.
468  */
469 
470 struct user_msghdr_x {
471 	user_addr_t     msg_name;       /* optional address */
472 	socklen_t       msg_namelen;    /* size of address */
473 	vm_address_t    msg_iov;        /* scatter/gather array */
474 	int             msg_iovlen;     /* # elements in msg_iov */
475 	user_addr_t     msg_control;    /* ancillary data, see below */
476 	socklen_t       msg_controllen; /* ancillary data buffer len */
477 	int             msg_flags;      /* flags on received message */
478 	size_t          msg_datalen;    /* byte length of buffer in msg_iov */
479 };
480 __CCT_DECLARE_CONSTRAINED_PTR_TYPES(struct user_msghdr_x, user_msghdr_x);
481 
482 
483 /*
484  * LP64 user version of struct msghdr_x
485  * WARNING - keep in sync with struct msghdr_x
486  */
487 
488 struct user64_msghdr_x {
489 	user64_addr_t   msg_name;       /* optional address */
490 	socklen_t       msg_namelen;    /* size of address */
491 	user64_addr_t   msg_iov;        /* scatter/gather array */
492 	int             msg_iovlen;     /* # elements in msg_iov */
493 	user64_addr_t   msg_control;    /* ancillary data, see below */
494 	socklen_t       msg_controllen; /* ancillary data buffer len */
495 	int             msg_flags;      /* flags on received message */
496 	user64_size_t   msg_datalen;    /* byte length of buffer in msg_iov */
497 };
498 __CCT_DECLARE_CONSTRAINED_PTR_TYPES(struct user64_msghdr_x, user64_msghdr_x);
499 
500 /*
501  * ILP32 user version of struct msghdr_x
502  * WARNING - keep in sync with struct msghdr_x
503  */
504 
505 struct user32_msghdr_x {
506 	user32_addr_t   msg_name;       /* optional address */
507 	socklen_t       msg_namelen;    /* size of address */
508 	user32_addr_t   msg_iov;        /* scatter/gather array */
509 	int             msg_iovlen;     /* # elements in msg_iov */
510 	user32_addr_t   msg_control;    /* ancillary data, see below */
511 	socklen_t       msg_controllen; /* ancillary data buffer len */
512 	int             msg_flags;      /* flags on received message */
513 	user32_size_t   msg_datalen;    /* byte length of buffer in msg_iov */
514 };
515 __CCT_DECLARE_CONSTRAINED_PTR_TYPES(struct user32_msghdr_x, user32_msghdr_x);
516 
517 /*
518  * In-kernel representation of "struct sa_endpoints" from
519  * userspace. Has enough precision for 32-bit or
520  * 64-bit clients, but does not need to be packed.
521  */
522 
523 struct user_sa_endpoints {
524 	unsigned int    sae_srcif;      /* optional source interface */
525 	user_addr_t     sae_srcaddr;    /* optional source address */
526 	socklen_t       sae_srcaddrlen; /* size of source address */
527 	user_addr_t     sae_dstaddr;    /* destination address */
528 	socklen_t       sae_dstaddrlen; /* size of destination address */
529 };
530 
531 /*
532  * LP64 user version of struct sa_endpoints
533  * WARNING - keep in sync with struct sa_endpoints
534  */
535 
536 struct user64_sa_endpoints {
537 	unsigned int    sae_srcif;      /* optional source interface */
538 	user64_addr_t   sae_srcaddr;    /* optional source address */
539 	socklen_t       sae_srcaddrlen; /* size of source address */
540 	user64_addr_t   sae_dstaddr;    /* destination address */
541 	socklen_t       sae_dstaddrlen; /* size of destination address */
542 };
543 
544 /*
545  * ILP32 user version of struct sa_endpoints
546  * WARNING - keep in sync with struct sa_endpoints
547  */
548 
549 struct user32_sa_endpoints {
550 	unsigned int    sae_srcif;      /* optional source interface */
551 	user32_addr_t   sae_srcaddr;    /* optional source address */
552 	socklen_t       sae_srcaddrlen; /* size of source address */
553 	user32_addr_t   sae_dstaddr;    /* destination address */
554 	socklen_t       sae_dstaddrlen; /* size of destination address */
555 };
556 
557 #endif /* XNU_KERNEL_PRIVATE */
558 
559 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
560 #ifdef __APPLE__
561 #ifndef __APPLE_API_OBSOLETE
562 #define MSG_WAITSTREAM  0x200           /* wait up to full request.. may return partial */
563 #endif
564 #endif
565 #ifdef KERNEL_PRIVATE
566 #define MSG_COMPAT      0x8000          /* deprecated */
567 #define MSG_NBIO        0x20000         /* FIONBIO mode, used by fifofs */
568 #define MSG_SKIPCFIL    0x40000         /* skip pass content filter */
569 #endif
570 
571 #define SCM_TIMESTAMP_CONTINUOUS        0x07    /* timestamp (uint64_t) */
572 #define SCM_MPKL_SEND_INFO              0x08    /* send info for multi-layer packet logging (struct so_mpkl_send_info) */
573 #define SCM_MPKL_RECV_INFO              0x09    /* receive info for multi-layer packet logging (struct so_mpkl_recv_info */
574 
575 #ifdef KERNEL_PRIVATE
576 /*
577  * 4.3 compat sockaddr (deprecated)
578  */
579 struct osockaddr {
580 	__uint16_t      sa_family;      /* address family */
581 	char    sa_data[14];            /* up to 14 bytes of direct address */
582 };
583 
584 /*
585  * 4.3-compat message header (deprecated)
586  */
587 struct omsghdr {
588 	void            *msg_name;              /* optional address */
589 	socklen_t       msg_namelen;            /* size of address */
590 	struct  iovec   *msg_iov;               /* scatter/gather array */
591 	int             msg_iovlen;             /* # elements in msg_iov */
592 	void            *msg_accrights;         /* access rights sent/rcvd */
593 	int             msg_accrightslen;
594 };
595 
596 #define SA(s)   ((struct sockaddr *)(void *)(s))
597 #endif /* KERNEL_PRIVATE */
598 
599 
600 /*
601  * Structure for SIOCGASSOCIDS
602  */
603 struct so_aidreq {
604 	__uint32_t      sar_cnt;        /* number of associations */
605 	sae_associd_t   *sar_aidp;      /* array of association IDs */
606 };
607 
608 #ifdef BSD_KERNEL_PRIVATE
609 struct so_aidreq32 {
610 	__uint32_t      sar_cnt;
611 	user32_addr_t   sar_aidp;
612 };
613 
614 struct so_aidreq64 {
615 	__uint32_t      sar_cnt;
616 	user64_addr_t   sar_aidp __attribute__((aligned(8)));
617 };
618 #endif /* BSD_KERNEL_PRIVATE */
619 
620 /*
621  * Structure for SIOCGCONNIDS
622  */
623 struct so_cidreq {
624 	sae_associd_t   scr_aid;        /* association ID */
625 	__uint32_t      scr_cnt;        /* number of connections */
626 	sae_connid_t    *scr_cidp;      /* array of connection IDs */
627 };
628 
629 #ifdef BSD_KERNEL_PRIVATE
630 struct so_cidreq32 {
631 	sae_associd_t   scr_aid;
632 	__uint32_t      scr_cnt;
633 	user32_addr_t   scr_cidp;
634 };
635 
636 struct so_cidreq64 {
637 	sae_associd_t   scr_aid;
638 	__uint32_t      scr_cnt;
639 	user64_addr_t   scr_cidp __attribute__((aligned(8)));
640 };
641 #endif /* BSD_KERNEL_PRIVATE */
642 
643 /*
644  * Structure for SIOCGCONNINFO
645  */
646 struct so_cinforeq {
647 	sae_connid_t    scir_cid;               /* connection ID */
648 	__uint32_t      scir_flags;             /* see flags below */
649 	__uint32_t      scir_ifindex;           /* (last) outbound interface */
650 	__int32_t       scir_error;             /* most recent error */
651 	struct sockaddr *scir_src;              /* source address */
652 	socklen_t       scir_src_len;           /* source address len */
653 	struct sockaddr *scir_dst;              /* destination address */
654 	socklen_t       scir_dst_len;           /* destination address len */
655 	__uint32_t      scir_aux_type;          /* aux data type (CIAUX) */
656 	void            *scir_aux_data;         /* aux data */
657 	__uint32_t      scir_aux_len;           /* aux data len */
658 };
659 
660 #ifdef BSD_KERNEL_PRIVATE
661 struct so_cinforeq32 {
662 	sae_connid_t    scir_cid;
663 	__uint32_t      scir_flags;
664 	__uint32_t      scir_ifindex;
665 	__int32_t       scir_error;
666 	user32_addr_t   scir_src;
667 	socklen_t       scir_src_len;
668 	user32_addr_t   scir_dst;
669 	socklen_t       scir_dst_len;
670 	__uint32_t      scir_aux_type;
671 	user32_addr_t   scir_aux_data;
672 	__uint32_t      scir_aux_len;
673 };
674 
675 struct so_cinforeq64 {
676 	sae_connid_t    scir_cid;
677 	__uint32_t      scir_flags;
678 	__uint32_t      scir_ifindex;
679 	__int32_t       scir_error;
680 	user64_addr_t   scir_src        __attribute__((aligned(8)));
681 	socklen_t       scir_src_len;
682 	user64_addr_t   scir_dst        __attribute__((aligned(8)));
683 	socklen_t       scir_dst_len;
684 	__uint32_t      scir_aux_type;
685 	user64_addr_t   scir_aux_data   __attribute__((aligned(8)));
686 	__uint32_t      scir_aux_len;
687 };
688 
689 #endif /* BSD_KERNEL_PRIVATE */
690 
691 /* valid connection info flags */
692 #define CIF_CONNECTING          0x1     /* connection was attempted */
693 #define CIF_CONNECTED           0x2     /* connection is established */
694 #define CIF_DISCONNECTING       0x4     /* disconnection was attempted */
695 #define CIF_DISCONNECTED        0x8     /* has been disconnected */
696 #define CIF_BOUND_IF            0x10    /* bound to an interface */
697 #define CIF_BOUND_IP            0x20    /* bound to a src address */
698 #define CIF_BOUND_PORT          0x40    /* bound to a src port */
699 #define CIF_PREFERRED           0x80    /* connection is primary/preferred */
700 #define CIF_MP_CAPABLE          0x100   /* supports multipath protocol */
701 #define CIF_MP_READY            0x200   /* multipath protocol confirmed */
702 #define CIF_MP_DEGRADED         0x400   /* has lost its multipath capability */
703 #define CIF_MP_ACTIVE           0x800   /* this is the active subflow */
704 #define CIF_MP_V1               0x1000  /* MPTCP v1 is used */
705 
706 /* valid connection info auxiliary data types */
707 #define CIAUX_TCP       0x1     /* TCP auxiliary data (conninfo_tcp_t) */
708 #define CIAUX_MPTCP     0x2     /* MPTCP auxiliary data (conninfo_multipathtcp) */
709 
710 /*
711  * Structure for SIOC{S,G}CONNORDER
712  */
713 struct so_cordreq {
714 	sae_connid_t    sco_cid;                /* connection ID */
715 	__uint32_t      sco_rank;               /* rank (0 means unspecified) */
716 };
717 
718 /*
719  * Common structure for KEV_NETPOLICY_SUBCLASS
720  */
721 struct netpolicy_event_data {
722 	__uint64_t      eupid;          /* effective unique PID */
723 	__uint64_t      epid;           /* effective PID */
724 	uuid_t          euuid;          /* effective UUID */
725 };
726 
727 /*
728  * NETPOLICY_IFDENIED event structure
729  */
730 struct kev_netpolicy_ifdenied {
731 	struct netpolicy_event_data     ev_data;
732 	__uint32_t ev_if_functional_type;
733 };
734 
735 /*
736  * KEV_NETPOLICY_NETDENIED event structure
737  */
738 struct kev_netpolicy_netdenied {
739 	struct netpolicy_event_data     ev_data;
740 	__uint32_t ev_network_type;
741 };
742 
743 /*
744  * Network Service Type to DiffServ Code Point mapping
745  */
746 struct netsvctype_dscp_map {
747 	int             netsvctype;
748 	u_int8_t        dscp; /* 6 bits diffserv code point */
749 };
750 
751 /*
752  * Multi-layer packet logging require SO_MPK_LOG to be set
753  */
754 struct so_mpkl_send_info {
755 	uuid_t          mpkl_uuid;
756 	__uint8_t       mpkl_proto;     /* see net/multi_layer_pkt_log.h */
757 };
758 
759 struct so_mpkl_recv_info {
760 	__uint32_t      mpkl_seq;
761 	__uint8_t       mpkl_proto;     /* see net/multi_layer_pkt_log.h */
762 };
763 
764 #ifndef KERNEL
765 __BEGIN_DECLS
766 
767 extern int peeloff(int s, sae_associd_t);
768 extern int socket_delegate(int, int, int, pid_t);
769 
770 /*
771  * recvmsg_x() is a system call similar to recvmsg(2) to receive
772  * several datagrams at once in the array of message headers "msgp".
773  *
774  * recvmsg_x() can be used only with protocols handlers that have been specially
775  * modified to support sending and receiving several datagrams at once.
776  *
777  * The size of the array "msgp" is given by the argument "cnt".
778  *
779  * The "flags" arguments supports only the value MSG_DONTWAIT.
780  *
781  * Each member of "msgp" array is of type "struct msghdr_x".
782  *
783  * The "msg_iov" and "msg_iovlen" are input parameters that describe where to
784  * store a datagram in a scatter gather locations of buffers -- see recvmsg(2).
785  * On output the field "msg_datalen" gives the length of the received datagram.
786  *
787  * The field "msg_flags" must be set to zero on input. On output, "msg_flags"
788  * may have MSG_TRUNC set to indicate the trailing portion of the datagram was
789  * discarded because the datagram was larger than the buffer supplied.
790  * recvmsg_x() returns as soon as a datagram is truncated.
791  *
792  * recvmsg_x() may return with less than "cnt" datagrams received based on
793  * the low water mark and the amount of data pending in the socket buffer.
794  *
795  * recvmsg_x() returns the number of datagrams that have been received,
796  * or -1 if an error occurred.
797  *
798  * NOTE: This a private system call, the API is subject to change.
799  */
800 ssize_t recvmsg_x(int s, const struct msghdr_x *msgp, u_int cnt, int flags);
801 
802 /*
803  * sendmsg_x() is a system call similar to send(2) to send
804  * several datagrams at once in the array of message headers "msgp".
805  *
806  * sendmsg_x() can be used only with protocols handlers that have been specially
807  * modified to support sending and receiving several datagrams at once.
808  *
809  * The size of the array "msgp" is given by the argument "cnt".
810  *
811  * The "flags" arguments supports only the value MSG_DONTWAIT.
812  *
813  * Each member of "msgp" array is of type "struct msghdr_x".
814  *
815  * The "msg_iov" and "msg_iovlen" are input parameters that specify the
816  * data to be sent in a scatter gather locations of buffers -- see sendmsg(2).
817  *
818  * sendmsg_x() fails with EMSGSIZE if the sum of the length of the datagrams
819  * is greater than the high water mark.
820  *
821  * Address and ancillary data are not supported so the following fields
822  * must be set to zero on input:
823  *   "msg_name", "msg_namelen", "msg_control" and "msg_controllen".
824  *
825  * The field "msg_flags" and "msg_datalen" must be set to zero on input.
826  *
827  * sendmsg_x() returns the number of datagrams that have been sent,
828  * or -1 if an error occurred.
829  *
830  * NOTE: This a private system call, the API is subject to change.
831  */
832 ssize_t sendmsg_x(int s, const struct msghdr_x *msgp, u_int cnt, int flags);
833 __END_DECLS
834 #endif /* !KERNEL */
835 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
836 
837 #endif /* !_SYS_SOCKET_PRIVATE_H_ */
838