xref: /xnu-11215/bsd/sys/socket.h (revision 8d741a5d)
1 /*
2  * Copyright (c) 2000-2022 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_H_
73 #define _SYS_SOCKET_H_
74 
75 #include <sys/types.h>
76 #include <sys/cdefs.h>
77 #include <sys/constrained_ctypes.h>
78 #include <machine/_param.h>
79 #include <net/net_kev.h>
80 
81 #ifndef KERNEL
82 #include <Availability.h>
83 #endif
84 
85 /*
86  * Definitions related to sockets: types, address families, options.
87  */
88 
89 /*
90  * Data types.
91  */
92 
93 #include <sys/_types/_gid_t.h>
94 #include <sys/_types/_off_t.h>
95 #include <sys/_types/_pid_t.h>
96 #include <sys/_types/_sa_family_t.h>
97 #include <sys/_types/_socklen_t.h>
98 
99 /* XXX Not explicitly defined by POSIX, but function return types are */
100 #include <sys/_types/_size_t.h>
101 
102 /* XXX Not explicitly defined by POSIX, but function return types are */
103 #include <sys/_types/_ssize_t.h>
104 
105 /*
106  * [XSI] The iovec structure shall be defined as described in <sys/uio.h>.
107  */
108 #include <sys/_types/_iovec_t.h>
109 
110 /*
111  * Types
112  */
113 #define SOCK_STREAM     1               /* stream socket */
114 #define SOCK_DGRAM      2               /* datagram socket */
115 #define SOCK_RAW        3               /* raw-protocol interface */
116 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
117 #define SOCK_RDM        4               /* reliably-delivered message */
118 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
119 #define SOCK_SEQPACKET  5               /* sequenced packet stream */
120 
121 /*
122  * Option flags per-socket.
123  */
124 #define SO_DEBUG        0x0001          /* turn on debugging info recording */
125 #define SO_ACCEPTCONN   0x0002          /* socket has had listen() */
126 #define SO_REUSEADDR    0x0004          /* allow local address reuse */
127 #define SO_KEEPALIVE    0x0008          /* keep connections alive */
128 #define SO_DONTROUTE    0x0010          /* just use interface addresses */
129 #define SO_BROADCAST    0x0020          /* permit sending of broadcast msgs */
130 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
131 #define SO_USELOOPBACK  0x0040          /* bypass hardware when possible */
132 #define SO_LINGER       0x0080          /* linger on close if data present (in ticks) */
133 #define SO_LINGER_SEC   0x1080          /* linger on close if data present (in seconds) */
134 #else
135 #define SO_LINGER       0x1080          /* linger on close if data present (in seconds) */
136 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
137 #define SO_OOBINLINE    0x0100          /* leave received OOB data in line */
138 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
139 #define SO_REUSEPORT    0x0200          /* allow local address & port reuse */
140 #define SO_TIMESTAMP    0x0400          /* timestamp received dgram traffic */
141 #define SO_TIMESTAMP_MONOTONIC  0x0800  /* Monotonically increasing timestamp on rcvd dgram */
142 #ifndef __APPLE__
143 #define SO_ACCEPTFILTER 0x1000          /* there is an accept filter */
144 #else
145 #define SO_DONTTRUNC    0x2000          /* APPLE: Retain unread data */
146                                         /*  (ATOMIC proto) */
147 #define SO_WANTMORE     0x4000          /* APPLE: Give hint when more data ready */
148 #define SO_WANTOOBFLAG  0x8000          /* APPLE: Want OOB in MSG_FLAG on receive */
149 
150 #ifdef PRIVATE
151 /* See socket_private.h for extended flags */
152 #endif
153 
154 #endif  /* (!__APPLE__) */
155 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
156 
157 /*
158  * Additional options, not kept in so_options.
159  */
160 #define SO_SNDBUF       0x1001          /* send buffer size */
161 #define SO_RCVBUF       0x1002          /* receive buffer size */
162 #define SO_SNDLOWAT     0x1003          /* send low-water mark */
163 #define SO_RCVLOWAT     0x1004          /* receive low-water mark */
164 #define SO_SNDTIMEO     0x1005          /* send timeout */
165 #define SO_RCVTIMEO     0x1006          /* receive timeout */
166 #define SO_ERROR        0x1007          /* get error status and clear */
167 #define SO_TYPE         0x1008          /* get socket type */
168 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
169 #define SO_LABEL        0x1010          /* deprecated */
170 #define SO_PEERLABEL    0x1011          /* deprecated */
171 #ifdef __APPLE__
172 #define SO_NREAD        0x1020          /* APPLE: get 1st-packet byte count */
173 #define SO_NKE          0x1021          /* APPLE: Install socket-level NKE */
174 #define SO_NOSIGPIPE    0x1022          /* APPLE: No SIGPIPE on EPIPE */
175 #define SO_NOADDRERR    0x1023          /* APPLE: Returns EADDRNOTAVAIL when src is not available anymore */
176 #define SO_NWRITE       0x1024          /* APPLE: Get number of bytes currently in send socket buffer */
177 #define SO_REUSESHAREUID        0x1025          /* APPLE: Allow reuse of port/socket by different userids */
178 #ifdef __APPLE_API_PRIVATE
179 #define SO_NOTIFYCONFLICT       0x1026  /* APPLE: send notification if there is a bind on a port which is already in use */
180 #define SO_UPCALLCLOSEWAIT      0x1027  /* APPLE: block on close until an upcall returns */
181 #endif
182 #ifdef PRIVATE
183 /* See socket_private.h for extended options */
184 #endif /* PRIVATE */
185 #define SO_RANDOMPORT   0x1082  /* APPLE: request local port randomization */
186 #define SO_NP_EXTENSIONS        0x1083  /* To turn off some POSIX behavior */
187 #endif
188 
189 #define SO_NUMRCVPKT            0x1112  /* number of datagrams in receive socket buffer */
190 #ifdef PRIVATE
191 /* See socket_private.h for extended options */
192 #endif /* PRIVATE */
193 #define SO_NET_SERVICE_TYPE     0x1116  /* Network service type */
194 
195 #ifdef PRIVATE
196 /* See socket_private.h for extended options */
197 #endif /* PRIVATE */
198 
199 #define SO_NETSVC_MARKING_LEVEL    0x1119  /* Get QoS marking in effect for socket */
200 
201 #ifdef PRIVATE
202 /* See socket_private.h for extended options */
203 #endif /* PRIVATE */
204 
205 #define SO_RESOLVER_SIGNATURE      0x1131  /* A signed data blob from the system resolver */
206 #ifdef PRIVATE
207 /* See socket_private.h for extended options */
208 #endif
209 
210 #define SO_BINDTODEVICE            0x1134  /* bind socket to a network device (max valid option length IFNAMSIZ) */
211 #ifdef PRIVATE
212 /* See socket_private.h for extended options */
213 #endif
214 
215 /* When adding new socket-options, you need to make sure MPTCP supports these as well! */
216 
217 /*
218  * Network Service Type for option SO_NET_SERVICE_TYPE
219  *
220  * The vast majority of sockets should use Best Effort that is the default
221  * Network Service Type. Other Network Service Types have to be used only if
222  * the traffic actually matches the description of the Network Service Type.
223  *
224  * Network Service Types do not represent priorities but rather describe
225  * different categories of delay, jitter and loss parameters.
226  * Those parameters may influence protocols from layer 4 protocols like TCP
227  * to layer 2 protocols like Wi-Fi. The Network Service Type can determine
228  * how the traffic is queued and scheduled by the host networking stack and
229  * by other entities on the network like switches and routers. For example
230  * for Wi-Fi, the Network Service Type can select the marking of the
231  * layer 2 packet with the appropriate WMM Access Category.
232  *
233  * There is no point in attempting to game the system and use
234  * a Network Service Type that does not correspond to the actual
235  * traffic characteristic but one that seems to have a higher precedence.
236  * The reason is that for service classes that have lower tolerance
237  * for delay and jitter, the queues size is lower than for service
238  * classes that are more tolerant to delay and jitter.
239  *
240  * For example using a voice service type for bulk data transfer will lead
241  * to disastrous results as soon as congestion happens because the voice
242  * queue overflows and packets get dropped. This is not only bad for the bulk
243  * data transfer but it is also bad for VoIP apps that legitimately are using
244  * the voice  service type.
245  *
246  * The characteristics of the Network Service Types are based on the service
247  * classes defined in RFC 4594 "Configuration Guidelines for DiffServ Service
248  * Classes"
249  *
250  * When system detects the outgoing interface belongs to a DiffServ domain
251  * that follows the recommendation of the IETF draft "Guidelines for DiffServ to
252  * IEEE 802.11 Mapping", the packet will marked at layer 3 with a DSCP value
253  * that corresponds to Network Service Type.
254  *
255  * NET_SERVICE_TYPE_BE
256  *	"Best Effort", unclassified/standard.  This is the default service
257  *	class and cover the majority of the traffic.
258  *
259  * NET_SERVICE_TYPE_BK
260  *	"Background", high delay tolerant, loss tolerant. elastic flow,
261  *	variable size & long-lived. E.g: non-interactive network bulk transfer
262  *	like synching or backup.
263  *
264  * NET_SERVICE_TYPE_RD
265  *	"Responsive Data", a notch higher than "Best Effort", medium delay
266  *	tolerant, elastic & inelastic flow, bursty, long-lived. E.g. email,
267  *	instant messaging, for which there is a sense of interactivity and
268  *	urgency (user waiting for output).
269  *
270  * NET_SERVICE_TYPE_OAM
271  *	"Operations, Administration, and Management", medium delay tolerant,
272  *	low-medium loss tolerant, elastic & inelastic flows, variable size.
273  *	E.g. VPN tunnels.
274  *
275  * NET_SERVICE_TYPE_AV
276  *	"Multimedia Audio/Video Streaming", medium delay tolerant, low-medium
277  *	loss tolerant, elastic flow, constant packet interval, variable rate
278  *	and size. E.g. video and audio playback with buffering.
279  *
280  * NET_SERVICE_TYPE_RV
281  *	"Responsive Multimedia Audio/Video", low delay tolerant, low-medium
282  *	loss tolerant, elastic flow, variable packet interval, rate and size.
283  *	E.g. screen sharing.
284  *
285  * NET_SERVICE_TYPE_VI
286  *	"Interactive Video", low delay tolerant, low-medium loss tolerant,
287  *	elastic flow, constant packet interval, variable rate & size. E.g.
288  *	video telephony.
289  *
290  * NET_SERVICE_TYPE_SIG
291  *	"Signaling", low delay tolerant, low loss tolerant, inelastic flow,
292  *	jitter tolerant, rate is bursty but short, variable size. E.g. SIP.
293  *
294  * NET_SERVICE_TYPE_VO
295  *	"Interactive Voice", very low delay tolerant, very low loss tolerant,
296  *	inelastic flow, constant packet rate, somewhat fixed size.
297  *	E.g. VoIP.
298  */
299 
300 #define NET_SERVICE_TYPE_BE     0 /* Best effort */
301 #define NET_SERVICE_TYPE_BK     1 /* Background system initiated */
302 #define NET_SERVICE_TYPE_SIG    2 /* Signaling */
303 #define NET_SERVICE_TYPE_VI     3 /* Interactive Video */
304 #define NET_SERVICE_TYPE_VO     4 /* Interactive Voice */
305 #define NET_SERVICE_TYPE_RV     5 /* Responsive Multimedia Audio/Video */
306 #define NET_SERVICE_TYPE_AV     6 /* Multimedia Audio/Video Streaming */
307 #define NET_SERVICE_TYPE_OAM    7 /* Operations, Administration, and Management */
308 #define NET_SERVICE_TYPE_RD     8 /* Responsive Data */
309 
310 #if PRIVATE
311 /* See socket_private.h for extended types */
312 #endif /* PRIVATE */
313 
314 /* These are supported values for SO_NETSVC_MARKING_LEVEL */
315 #define NETSVC_MRKNG_UNKNOWN            0       /* The outgoing network interface is not known */
316 #define NETSVC_MRKNG_LVL_L2             1       /* Default marking at layer 2 (for example Wi-Fi WMM) */
317 #define NETSVC_MRKNG_LVL_L3L2_ALL       2       /* Layer 3 DSCP marking and layer 2 marking for all Network Service Types */
318 #define NETSVC_MRKNG_LVL_L3L2_BK        3       /* The system policy limits layer 3 DSCP marking and layer 2 marking
319 	                                         * to background Network Service Types */
320 
321 
322 typedef __uint32_t sae_associd_t;
323 #define SAE_ASSOCID_ANY 0
324 #define SAE_ASSOCID_ALL ((sae_associd_t)(-1ULL))
325 
326 typedef __uint32_t sae_connid_t;
327 #define SAE_CONNID_ANY  0
328 #define SAE_CONNID_ALL  ((sae_connid_t)(-1ULL))
329 
330 /* connectx() flag parameters */
331 #define CONNECT_RESUME_ON_READ_WRITE    0x1 /* resume connect() on read/write */
332 #define CONNECT_DATA_IDEMPOTENT         0x2 /* data is idempotent */
333 #define CONNECT_DATA_AUTHENTICATED      0x4 /* data includes security that replaces the TFO-cookie */
334 
335 /* sockaddr endpoints */
336 typedef struct sa_endpoints {
337 	unsigned int            sae_srcif;      /* optional source interface */
338 	const struct sockaddr   *sae_srcaddr;   /* optional source address */
339 	socklen_t               sae_srcaddrlen; /* size of source address */
340 	const struct sockaddr   *sae_dstaddr;   /* destination address */
341 	socklen_t               sae_dstaddrlen; /* size of destination address */
342 } sa_endpoints_t;
343 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
344 
345 /*
346  * Structure used for manipulating linger option.
347  */
348 struct  linger {
349 	int     l_onoff;                /* option on/off */
350 	int     l_linger;               /* linger time */
351 };
352 
353 #ifndef __APPLE__
354 struct  accept_filter_arg {
355 	char    af_name[16];
356 	char    af_arg[256 - 16];
357 };
358 #endif
359 
360 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
361 #ifdef __APPLE__
362 
363 /*
364  * Structure to control non-portable Sockets extension to POSIX
365  */
366 struct so_np_extensions {
367 	u_int32_t       npx_flags;
368 	u_int32_t       npx_mask;
369 };
370 
371 #define SONPX_SETOPTSHUT        0x000000001     /* flag for allowing setsockopt after shutdown */
372 
373 
374 #endif
375 #endif
376 
377 /*
378  * Level number for (get/set)sockopt() to apply to socket itself.
379  */
380 #define SOL_SOCKET      0xffff          /* options for socket level */
381 
382 
383 /*
384  * Address families.
385  */
386 #define AF_UNSPEC       0               /* unspecified */
387 #define AF_UNIX         1               /* local to host (pipes) */
388 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
389 #define AF_LOCAL        AF_UNIX         /* backward compatibility */
390 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
391 #define AF_INET         2               /* internetwork: UDP, TCP, etc. */
392 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
393 #define AF_IMPLINK      3               /* arpanet imp addresses */
394 #define AF_PUP          4               /* pup protocols: e.g. BSP */
395 #define AF_CHAOS        5               /* mit CHAOS protocols */
396 #define AF_NS           6               /* XEROX NS protocols */
397 #define AF_ISO          7               /* ISO protocols */
398 #define AF_OSI          AF_ISO
399 #define AF_ECMA         8               /* European computer manufacturers */
400 #define AF_DATAKIT      9               /* datakit protocols */
401 #define AF_CCITT        10              /* CCITT protocols, X.25 etc */
402 #define AF_SNA          11              /* IBM SNA */
403 #define AF_DECnet       12              /* DECnet */
404 #define AF_DLI          13              /* DEC Direct data link interface */
405 #define AF_LAT          14              /* LAT */
406 #define AF_HYLINK       15              /* NSC Hyperchannel */
407 #define AF_APPLETALK    16              /* Apple Talk */
408 #define AF_ROUTE        17              /* Internal Routing Protocol */
409 #define AF_LINK         18              /* Link layer interface */
410 #define pseudo_AF_XTP   19              /* eXpress Transfer Protocol (no AF) */
411 #define AF_COIP         20              /* connection-oriented IP, aka ST II */
412 #define AF_CNT          21              /* Computer Network Technology */
413 #define pseudo_AF_RTIP  22              /* Help Identify RTIP packets */
414 #define AF_IPX          23              /* Novell Internet Protocol */
415 #define AF_SIP          24              /* Simple Internet Protocol */
416 #define pseudo_AF_PIP   25              /* Help Identify PIP packets */
417 #define AF_NDRV         27              /* Network Driver 'raw' access */
418 #define AF_ISDN         28              /* Integrated Services Digital Network */
419 #define AF_E164         AF_ISDN         /* CCITT E.164 recommendation */
420 #define pseudo_AF_KEY   29              /* Internal key-management function */
421 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
422 #define AF_INET6        30              /* IPv6 */
423 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
424 #define AF_NATM         31              /* native ATM access */
425 #define AF_SYSTEM       32              /* Kernel event messages */
426 #define AF_NETBIOS      33              /* NetBIOS */
427 #define AF_PPP          34              /* PPP communication protocol */
428 #define pseudo_AF_HDRCMPLT 35           /* Used by BPF to not rewrite headers
429 	                                 *  in interface output routine */
430 #ifdef PRIVATE
431 /* See socket_private.h for extended families */
432 #else
433 #define AF_RESERVED_36  36              /* Reserved for internal usage */
434 #endif
435 #define AF_IEEE80211    37              /* IEEE 802.11 protocol */
436 #define AF_UTUN         38
437 #ifdef PRIVATE
438 /* See socket_private.h for extended families */
439 #endif /* PRIVATE */
440 #define AF_VSOCK        40              /* VM Sockets */
441 #define AF_MAX          41
442 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
443 
444 /*
445  * [XSI] Structure used by kernel to store most addresses.
446  */
447 struct sockaddr {
448 	__uint8_t       sa_len;         /* total length */
449 	sa_family_t     sa_family;      /* [XSI] address family */
450 	char            sa_data[14];    /* [XSI] addr value */
451 };
452 __CCT_DECLARE_CONSTRAINED_PTR_TYPES(struct sockaddr, sockaddr);
453 
454 /*
455  * Least amount of information that a sockaddr requires.
456  * Sockaddr_header is a compatible prefix structure of
457  * all sockaddr objects.
458  */
459 struct __sockaddr_header {
460 	__uint8_t           sa_len;
461 	sa_family_t         sa_family;
462 };
463 
464 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
465 #define SOCK_MAXADDRLEN 255             /* longest possible addresses */
466 
467 /*
468  * Structure used by kernel to pass protocol
469  * information in raw sockets.
470  */
471 struct sockproto {
472 	__uint16_t      sp_family;              /* address family */
473 	__uint16_t      sp_protocol;            /* protocol */
474 };
475 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
476 
477 /*
478  * RFC 2553: protocol-independent placeholder for socket addresses
479  */
480 #define _SS_MAXSIZE     128
481 #define _SS_ALIGNSIZE   (sizeof(__int64_t))
482 #define _SS_PAD1SIZE    \
483 	        (_SS_ALIGNSIZE - sizeof(__uint8_t) - sizeof(sa_family_t))
484 #define _SS_PAD2SIZE    \
485 	        (_SS_MAXSIZE - sizeof(__uint8_t) - sizeof(sa_family_t) - \
486 	                        _SS_PAD1SIZE - _SS_ALIGNSIZE)
487 
488 /*
489  * [XSI] sockaddr_storage
490  */
491 struct sockaddr_storage {
492 	__uint8_t       ss_len;         /* address length */
493 	sa_family_t     ss_family;      /* [XSI] address family */
494 	char                    __ss_pad1[_SS_PAD1SIZE];
495 	__int64_t       __ss_align;     /* force structure storage alignment */
496 	char                    __ss_pad2[_SS_PAD2SIZE];
497 };
498 __CCT_DECLARE_CONSTRAINED_PTR_TYPES(struct sockaddr_storage, sockaddr_storage);
499 
500 /*
501  * Protocol families, same as address families for now.
502  */
503 #define PF_UNSPEC       AF_UNSPEC
504 #define PF_LOCAL        AF_LOCAL
505 #define PF_UNIX         PF_LOCAL        /* backward compatibility */
506 #define PF_INET         AF_INET
507 #define PF_IMPLINK      AF_IMPLINK
508 #define PF_PUP          AF_PUP
509 #define PF_CHAOS        AF_CHAOS
510 #define PF_NS           AF_NS
511 #define PF_ISO          AF_ISO
512 #define PF_OSI          AF_ISO
513 #define PF_ECMA         AF_ECMA
514 #define PF_DATAKIT      AF_DATAKIT
515 #define PF_CCITT        AF_CCITT
516 #define PF_SNA          AF_SNA
517 #define PF_DECnet       AF_DECnet
518 #define PF_DLI          AF_DLI
519 #define PF_LAT          AF_LAT
520 #define PF_HYLINK       AF_HYLINK
521 #define PF_APPLETALK    AF_APPLETALK
522 #define PF_ROUTE        AF_ROUTE
523 #define PF_LINK         AF_LINK
524 #define PF_XTP          pseudo_AF_XTP   /* really just proto family, no AF */
525 #define PF_COIP         AF_COIP
526 #define PF_CNT          AF_CNT
527 #define PF_SIP          AF_SIP
528 #define PF_IPX          AF_IPX          /* same format as AF_NS */
529 #define PF_RTIP         pseudo_AF_RTIP  /* same format as AF_INET */
530 #define PF_PIP          pseudo_AF_PIP
531 #define PF_NDRV         AF_NDRV
532 #define PF_ISDN         AF_ISDN
533 #define PF_KEY          pseudo_AF_KEY
534 #define PF_INET6        AF_INET6
535 #define PF_NATM         AF_NATM
536 #define PF_SYSTEM       AF_SYSTEM
537 #define PF_NETBIOS      AF_NETBIOS
538 #define PF_PPP          AF_PPP
539 #ifdef PRIVATE
540 /* See socket_private.h for extended families */
541 #else
542 #define PF_RESERVED_36  AF_RESERVED_36
543 #endif
544 #define PF_UTUN         AF_UTUN
545 #ifdef PRIVATE
546 /* See socket_private.h for extended families */
547 #endif /* PRIVATE */
548 #define PF_VSOCK        AF_VSOCK
549 #define PF_MAX          AF_MAX
550 
551 /*
552  * These do not have socket-layer support:
553  */
554 #define PF_VLAN         ((uint32_t)0x766c616e)  /* 'vlan' */
555 #define PF_BOND         ((uint32_t)0x626f6e64)  /* 'bond' */
556 
557 /*
558  * Definitions for network related sysctl, CTL_NET.
559  *
560  * Second level is protocol family.
561  * Third level is protocol number.
562  *
563  * Further levels are defined by the individual families below.
564  */
565 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
566 #define NET_MAXID       AF_MAX
567 
568 /*
569  * PF_ROUTE - Routing table
570  *
571  * Three additional levels are defined:
572  *	Fourth: address family, 0 is wildcard
573  *	Fifth: type of info, defined below
574  *	Sixth: flag(s) to mask with for NET_RT_FLAGS
575  */
576 #define NET_RT_DUMP             1       /* dump; may limit to a.f. */
577 #define NET_RT_FLAGS            2       /* by flags, e.g. RESOLVING */
578 #define NET_RT_IFLIST           3       /* survey interface list */
579 #define NET_RT_STAT             4       /* routing statistics */
580 #define NET_RT_TRASH            5       /* routes not in table but not freed */
581 #define NET_RT_IFLIST2          6       /* interface list with addresses */
582 #define NET_RT_DUMP2            7       /* dump; may limit to a.f. */
583 #ifdef PRIVATE
584 /* See socket_private.h for extended types */
585 #endif /* PRIVATE */
586 /*
587  * Allows read access non-local host's MAC address
588  * if the process has neighbor cache entitlement.
589  */
590 #define NET_RT_FLAGS_PRIV       10
591 #define NET_RT_MAXID            11
592 #endif /* (_POSIX_C_SOURCE && !_DARWIN_C_SOURCE) */
593 
594 /*
595  * Maximum queue length specifiable by listen.
596  */
597 #define SOMAXCONN       128
598 
599 /*
600  * [XSI] Message header for recvmsg and sendmsg calls.
601  * Used value-result for recvmsg, value only for sendmsg.
602  */
603 struct msghdr {
604 	void            *__sized_by(msg_namelen) msg_name; /* [XSI] optional address */
605 	socklen_t       msg_namelen;    /* [XSI] size of address */
606 	struct          iovec *msg_iov; /* [XSI] scatter/gather array */
607 	int             msg_iovlen;     /* [XSI] # elements in msg_iov */
608 	void            *__sized_by(msg_controllen) msg_control; /* [XSI] ancillary data, see below */
609 	socklen_t       msg_controllen; /* [XSI] ancillary data buffer len */
610 	int             msg_flags;      /* [XSI] flags on received message */
611 };
612 
613 #define MSG_OOB         0x1             /* process out-of-band data */
614 #define MSG_PEEK        0x2             /* peek at incoming message */
615 #define MSG_DONTROUTE   0x4             /* send without using routing tables */
616 #define MSG_EOR         0x8             /* data completes record */
617 #define MSG_TRUNC       0x10            /* data discarded before delivery */
618 #define MSG_CTRUNC      0x20            /* control data lost before delivery */
619 #define MSG_WAITALL     0x40            /* wait for full request or error */
620 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
621 #define MSG_DONTWAIT    0x80            /* this message should be nonblocking */
622 #define MSG_EOF         0x100           /* data completes connection */
623 #ifdef __APPLE__
624 #ifdef __APPLE_API_OBSOLETE
625 #define MSG_WAITSTREAM  0x200           /* wait up to full request.. may return partial */
626 #endif
627 #define MSG_FLUSH       0x400           /* Start of 'hold' seq; dump so_temp, deprecated */
628 #define MSG_HOLD        0x800           /* Hold frag in so_temp, deprecated */
629 #define MSG_SEND        0x1000          /* Send the packet in so_temp, deprecated */
630 #define MSG_HAVEMORE    0x2000          /* Data ready to be read */
631 #define MSG_RCVMORE     0x4000          /* Data remains in current pkt */
632 #endif
633 #define MSG_NEEDSA      0x10000         /* Fail receive if socket address cannot be allocated */
634 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
635 
636 #if __DARWIN_C_LEVEL >= 200809L
637 #define MSG_NOSIGNAL    0x80000         /* do not generate SIGPIPE on EOF */
638 #endif /* __DARWIN_C_LEVEL */
639 
640 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
641 #ifdef  KERNEL
642 #define MSG_USEUPCALL   0x80000000      /* Inherit upcall in sock_accept */
643 #endif
644 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
645 
646 /*
647  * Header for ancillary data objects in msg_control buffer.
648  * Used for additional information with/about a datagram
649  * not expressible by flags.  The format is a sequence
650  * of message elements headed by cmsghdr structures.
651  */
652 struct cmsghdr {
653 	socklen_t       cmsg_len;       /* [XSI] data byte count, including hdr */
654 	int             cmsg_level;     /* [XSI] originating protocol */
655 	int             cmsg_type;      /* [XSI] protocol-specific type */
656 /* followed by	unsigned char  cmsg_data[]; */
657 };
658 
659 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
660 #ifndef __APPLE__
661 /*
662  * While we may have more groups than this, the cmsgcred struct must
663  * be able to fit in an mbuf, and NGROUPS_MAX is too large to allow
664  * this.
665  */
666 #define CMGROUP_MAX 16
667 
668 /*
669  * Credentials structure, used to verify the identity of a peer
670  * process that has sent us a message. This is allocated by the
671  * peer process but filled in by the kernel. This prevents the
672  * peer from lying about its identity. (Note that cmcred_groups[0]
673  * is the effective GID.)
674  */
675 struct cmsgcred {
676 	pid_t   cmcred_pid;             /* PID of sending process */
677 	uid_t   cmcred_uid;             /* real UID of sending process */
678 	uid_t   cmcred_euid;            /* effective UID of sending process */
679 	gid_t   cmcred_gid;             /* real GID of sending process */
680 	short   cmcred_ngroups;         /* number or groups */
681 	gid_t   cmcred_groups[CMGROUP_MAX];     /* groups */
682 };
683 #endif
684 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
685 
686 /* given pointer to struct cmsghdr, return pointer to data */
687 #define CMSG_DATA(cmsg)         ((unsigned char *)(cmsg) + \
688 	__DARWIN_ALIGN32(sizeof(struct cmsghdr)))
689 
690 /*
691  * RFC 2292 requires to check msg_controllen, in case that the kernel returns
692  * an empty list for some reasons.
693  */
694 #define CMSG_FIRSTHDR(mhdr) \
695 	((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
696 	    (struct cmsghdr *)(mhdr)->msg_control : \
697 	    (struct cmsghdr *)0L)
698 
699 
700 /*
701  * Given pointer to struct cmsghdr, return pointer to next cmsghdr
702  * RFC 2292 says that CMSG_NXTHDR(mhdr, NULL) is equivalent to CMSG_FIRSTHDR(mhdr)
703  */
704 #define CMSG_NXTHDR(mhdr, cmsg)                                         \
705 	((char *)(cmsg) == (char *)0L ? CMSG_FIRSTHDR(mhdr) :           \
706 	    ((((unsigned char *)(cmsg) +                                \
707 	    __DARWIN_ALIGN32((__uint32_t)(cmsg)->cmsg_len) +            \
708 	    __DARWIN_ALIGN32(sizeof(struct cmsghdr))) >                 \
709 	    ((unsigned char *)(mhdr)->msg_control +                     \
710 	    (mhdr)->msg_controllen)) ?                                  \
711 	        (struct cmsghdr *)0L /* NULL */ :                       \
712 	        (struct cmsghdr *)(void *)((unsigned char *)(cmsg) +    \
713 	            __DARWIN_ALIGN32((__uint32_t)(cmsg)->cmsg_len))))
714 
715 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
716 /* RFC 2292 additions */
717 #define CMSG_SPACE(l)           (__DARWIN_ALIGN32(sizeof(struct cmsghdr)) + __DARWIN_ALIGN32(l))
718 #define CMSG_LEN(l)             (__DARWIN_ALIGN32(sizeof(struct cmsghdr)) + (l))
719 
720 #ifdef KERNEL
721 #define CMSG_ALIGN(n)   ((typeof(n))__DARWIN_ALIGN32(n))
722 #endif
723 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
724 
725 /* "Socket"-level control message types: */
726 #define SCM_RIGHTS                      0x01    /* access rights (array of int) */
727 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
728 #define SCM_TIMESTAMP                   0x02    /* timestamp (struct timeval) */
729 #define SCM_CREDS                       0x03    /* process creds (struct cmsgcred) */
730 #define SCM_TIMESTAMP_MONOTONIC         0x04    /* timestamp (uint64_t) */
731 
732 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
733 
734 /*
735  * howto arguments for shutdown(2), specified by Posix.1g.
736  */
737 #define SHUT_RD         0               /* shut down the reading side */
738 #define SHUT_WR         1               /* shut down the writing side */
739 #define SHUT_RDWR       2               /* shut down both sides */
740 
741 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
742 /*
743  * sendfile(2) header/trailer struct
744  */
745 struct sf_hdtr {
746 	struct iovec *headers;  /* pointer to an array of header struct iovec's */
747 	int hdr_cnt;            /* number of header iovec's */
748 	struct iovec *trailers; /* pointer to an array of trailer struct iovec's */
749 	int trl_cnt;            /* number of trailer iovec's */
750 };
751 
752 #ifdef KERNEL
753 
754 /* In-kernel representation */
755 struct user_sf_hdtr {
756 	user_addr_t headers;    /* pointer to an array of header struct iovec's */
757 	int hdr_cnt;            /* number of header iovec's */
758 	user_addr_t trailers;   /* pointer to an array of trailer struct iovec's */
759 	int trl_cnt;            /* number of trailer iovec's */
760 };
761 
762 /* LP64 user version of struct sf_hdtr */
763 struct user64_sf_hdtr {
764 	user64_addr_t headers;  /* pointer to an array of header struct iovec's */
765 	int hdr_cnt;            /* number of header iovec's */
766 	user64_addr_t trailers; /* pointer to an array of trailer struct iovec's */
767 	int trl_cnt;            /* number of trailer iovec's */
768 };
769 
770 /* ILP32 user version of struct sf_hdtr */
771 struct user32_sf_hdtr {
772 	user32_addr_t headers;  /* pointer to an array of header struct iovec's */
773 	int hdr_cnt;            /* number of header iovec's */
774 	user32_addr_t trailers; /* pointer to an array of trailer struct iovec's */
775 	int trl_cnt;            /* number of trailer iovec's */
776 };
777 
778 #endif /* KERNEL */
779 
780 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
781 
782 #ifndef KERNEL
783 __BEGIN_DECLS
784 
785 int     accept(int, struct sockaddr * __restrict, socklen_t * __restrict)
786 __DARWIN_ALIAS_C(accept);
787 int     bind(int, const struct sockaddr *, socklen_t) __DARWIN_ALIAS(bind);
788 int     connect(int, const struct sockaddr *, socklen_t) __DARWIN_ALIAS_C(connect);
789 int     getpeername(int, struct sockaddr * __restrict, socklen_t * __restrict)
790 __DARWIN_ALIAS(getpeername);
791 int     getsockname(int, struct sockaddr * __restrict, socklen_t * __restrict)
792 __DARWIN_ALIAS(getsockname);
793 int     getsockopt(int, int, int, void * __restrict, socklen_t * __restrict);
794 int     listen(int, int) __DARWIN_ALIAS(listen);
795 ssize_t recv(int, void *, size_t, int) __DARWIN_ALIAS_C(recv);
796 ssize_t recvfrom(int, void *, size_t, int, struct sockaddr * __restrict,
797     socklen_t * __restrict) __DARWIN_ALIAS_C(recvfrom);
798 ssize_t recvmsg(int, struct msghdr *, int) __DARWIN_ALIAS_C(recvmsg);
799 ssize_t send(int, const void *, size_t, int) __DARWIN_ALIAS_C(send);
800 ssize_t sendmsg(int, const struct msghdr *, int) __DARWIN_ALIAS_C(sendmsg);
801 ssize_t sendto(int, const void *, size_t,
802     int, const struct sockaddr *, socklen_t) __DARWIN_ALIAS_C(sendto);
803 int     setsockopt(int, int, int, const void *, socklen_t);
804 int     shutdown(int, int);
805 int     sockatmark(int) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
806 int     socket(int, int, int);
807 int     socketpair(int, int, int, int *) __DARWIN_ALIAS(socketpair);
808 
809 #if !defined(_POSIX_C_SOURCE)
810 int     sendfile(int, int, off_t, off_t *, struct sf_hdtr *, int);
811 #endif  /* !_POSIX_C_SOURCE */
812 
813 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
814 void    pfctlinput(int, struct sockaddr *);
815 
816 __API_AVAILABLE(macosx(10.11), ios(9.0), tvos(9.0), watchos(2.0))
817 int connectx(int, const sa_endpoints_t *, sae_associd_t, unsigned int,
818     const struct iovec *, unsigned int, size_t *, sae_connid_t *);
819 
820 __API_AVAILABLE(macosx(10.11), ios(9.0), tvos(9.0), watchos(2.0))
821 int disconnectx(int, sae_associd_t, sae_connid_t);
822 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
823 __END_DECLS
824 #endif /* !KERNEL */
825 
826 #ifdef KERNEL
827 #include <sys/kpi_socket.h>
828 #endif
829 
830 #ifdef PRIVATE
831 #include <sys/socket_private.h>
832 #endif
833 
834 #endif /* !_SYS_SOCKET_H_ */
835