1/*
2 * Copyright (c) 2000-2012 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 <machine/_param.h>
78
79#ifdef PRIVATE
80#include <sys/param.h>
81#endif /* PRIVATE */
82
83#ifndef KERNEL
84#include <Availability.h>
85#endif
86
87/*
88 * Definitions related to sockets: types, address families, options.
89 */
90
91/*
92 * Data types.
93 */
94#ifndef _GID_T
95typedef __darwin_gid_t		gid_t;
96#define _GID_T
97#endif
98
99#ifndef _OFF_T
100typedef __darwin_off_t		off_t;
101#define _OFF_T
102#endif
103
104#ifndef _PID_T
105typedef __darwin_pid_t		pid_t;
106#define _PID_T
107#endif
108
109#ifndef _SA_FAMILY_T
110#define _SA_FAMILY_T
111typedef __uint8_t		sa_family_t;
112#endif
113
114#ifndef _SOCKLEN_T
115#define _SOCKLEN_T
116typedef	__darwin_socklen_t	socklen_t;
117#endif
118
119/* XXX Not explicitly defined by POSIX, but function return types are */
120#ifndef _SIZE_T
121#define _SIZE_T
122typedef __darwin_size_t		size_t;
123#endif
124
125/* XXX Not explicitly defined by POSIX, but function return types are */
126#ifndef	_SSIZE_T
127#define	_SSIZE_T
128typedef	__darwin_ssize_t	ssize_t;
129#endif
130
131/*
132 * [XSI] The iovec structure shall be defined as described in <sys/uio.h>.
133 */
134#ifndef _STRUCT_IOVEC
135#define	_STRUCT_IOVEC
136struct iovec {
137	void *   iov_base;	/* [XSI] Base address of I/O memory region */
138	size_t	 iov_len;	/* [XSI] Size of region iov_base points to */
139};
140#endif
141
142#ifdef PRIVATE
143#define SO_TCDBG_PID		0x01	/* Set/get traffic class for PID */
144#define SO_TCDBG_PNAME		0x02	/* Set/get traffic class for processes of that name */
145#define SO_TCDBG_PURGE		0x04	/* Purge entries for unused PIDs */
146#define SO_TCDBG_FLUSH		0x08	/* Flush all entries */
147#define SO_TCDBG_COUNT		0x10	/* Get count of entries */
148#define SO_TCDBG_LIST		0x20	/* List entries */
149#define SO_TCDBG_DELETE		0x40	/* Delete a process entry */
150#define SO_TCDBG_TCFLUSH_PID	0x80	/* Flush traffic class for PID */
151
152struct so_tcdbg {
153	u_int32_t	so_tcdbg_cmd;
154	int32_t		so_tcdbg_tclass;
155	u_int32_t	so_tcdbg_count;
156	pid_t		so_tcdbg_pid;
157	char		so_tcdbg_pname[MAXCOMLEN + 1];
158	int32_t		so_tcdbg_opportunistic; /* -1: unspecified, 0: off, 1: on, other: errors */
159};
160#endif /* PRIVATE */
161
162/*
163 * Types
164 */
165#define	SOCK_STREAM	1		/* stream socket */
166#define	SOCK_DGRAM	2		/* datagram socket */
167#define	SOCK_RAW	3		/* raw-protocol interface */
168#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
169#define	SOCK_RDM	4		/* reliably-delivered message */
170#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
171#define	SOCK_SEQPACKET	5		/* sequenced packet stream */
172
173/*
174 * Option flags per-socket.
175 */
176#define	SO_DEBUG	0x0001		/* turn on debugging info recording */
177#define	SO_ACCEPTCONN	0x0002		/* socket has had listen() */
178#define	SO_REUSEADDR	0x0004		/* allow local address reuse */
179#define	SO_KEEPALIVE	0x0008		/* keep connections alive */
180#define	SO_DONTROUTE	0x0010		/* just use interface addresses */
181#define	SO_BROADCAST	0x0020		/* permit sending of broadcast msgs */
182#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
183#define	SO_USELOOPBACK	0x0040		/* bypass hardware when possible */
184#define SO_LINGER	0x0080          /* linger on close if data present (in ticks) */
185#else
186#define SO_LINGER	0x1080          /* linger on close if data present (in seconds) */
187#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
188#define	SO_OOBINLINE	0x0100		/* leave received OOB data in line */
189#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
190#define	SO_REUSEPORT	0x0200		/* allow local address & port reuse */
191#define	SO_TIMESTAMP	0x0400		/* timestamp received dgram traffic */
192#define SO_TIMESTAMP_MONOTONIC	0x0800	/* Monotonically increasing timestamp on rcvd dgram */
193#ifndef __APPLE__
194#define	SO_ACCEPTFILTER	0x1000		/* there is an accept filter */
195#else
196#define SO_DONTTRUNC	0x2000		/* APPLE: Retain unread data */
197					/*  (ATOMIC proto) */
198#define SO_WANTMORE	0x4000		/* APPLE: Give hint when more data ready */
199#define SO_WANTOOBFLAG	0x8000		/* APPLE: Want OOB in MSG_FLAG on receive */
200
201#endif  /* (!__APPLE__) */
202#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
203
204/*
205 * Additional options, not kept in so_options.
206 */
207#define SO_SNDBUF	0x1001		/* send buffer size */
208#define SO_RCVBUF	0x1002		/* receive buffer size */
209#define SO_SNDLOWAT	0x1003		/* send low-water mark */
210#define SO_RCVLOWAT	0x1004		/* receive low-water mark */
211#define SO_SNDTIMEO	0x1005		/* send timeout */
212#define SO_RCVTIMEO	0x1006		/* receive timeout */
213#define	SO_ERROR	0x1007		/* get error status and clear */
214#define	SO_TYPE		0x1008		/* get socket type */
215#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
216/*efine	SO_PRIVSTATE	0x1009		   get/deny privileged state */
217#define SO_LABEL        0x1010          /* socket's MAC label */
218#define SO_PEERLABEL    0x1011          /* socket's peer MAC label */
219#ifdef __APPLE__
220#define SO_NREAD	0x1020		/* APPLE: get 1st-packet byte count */
221#define SO_NKE		0x1021		/* APPLE: Install socket-level NKE */
222#define SO_NOSIGPIPE	0x1022		/* APPLE: No SIGPIPE on EPIPE */
223#define SO_NOADDRERR	0x1023		/* APPLE: Returns EADDRNOTAVAIL when src is not available anymore */
224#define SO_NWRITE	0x1024		/* APPLE: Get number of bytes currently in send socket buffer */
225#define SO_REUSESHAREUID	0x1025		/* APPLE: Allow reuse of port/socket by different userids */
226#ifdef __APPLE_API_PRIVATE
227#define SO_NOTIFYCONFLICT	0x1026	/* APPLE: send notification if there is a bind on a port which is already in use */
228#define	SO_UPCALLCLOSEWAIT	0x1027	/* APPLE: block on close until an upcall returns */
229#endif
230#define SO_LINGER_SEC	0x1080          /* linger on close if data present (in seconds) */
231#define SO_RESTRICTIONS	0x1081	/* APPLE: deny inbound/outbound/both/flag set */
232#define SO_RESTRICT_DENYIN		0x00000001	/* flag for SO_RESTRICTIONS - deny inbound */
233#define SO_RESTRICT_DENYOUT		0x00000002	/* flag for SO_RESTRICTIONS - deny outbound */
234#define SO_RESTRICT_DENYSET		0x80000000	/* flag for SO_RESTRICTIONS - deny has been set */
235#define SO_RANDOMPORT   0x1082  /* APPLE: request local port randomization */
236#define SO_NP_EXTENSIONS	0x1083	/* To turn off some POSIX behavior */
237#endif
238
239#ifdef PRIVATE
240#define	SO_EXECPATH	0x1085 		/* Application Firewall Socket option */
241/*
242 * Traffic service class definitions (lowest to highest):
243 *
244 * SO_TC_BK_SYS
245 *	"Background System-Initiated", high delay tolerant, high loss
246 *	tolerant, elastic flow, variable size & long-lived.  E.g: system-
247 *	initiated iCloud synching or Time Capsule backup, for which there
248 *	is no progress feedbacks.
249 *
250 * SO_TC_BK
251 *	"Background", user-initiated, high delay tolerant, high loss tolerant,
252 *	elastic flow, variable size.  E.g. user-initiated iCloud synching or
253 *	Time Capsule backup; or traffics of background applications, for which
254 *	there is some progress feedbacks.
255 *
256 * SO_TC_BE
257 *	"Best Effort", unclassified/standard.  This is the default service
258 *	class; pretty much a mix of everything.
259 *
260 * SO_TC_RD
261 *	"Responsive Data", a notch higher than "Best Effort", medium delay
262 *	tolerant, elastic & inelastic flow, bursty, long-lived.  E.g. email,
263 *	instant messaging, for which there is a sense of interactivity and
264 *	urgency (user waiting for output).
265 *
266 * SO_TC_OAM
267 *	"Operations, Administration, and Management", medium delay tolerant,
268 *	low-medium loss tolerant, elastic & inelastic flows, variable size.
269 *	E.g. VPN tunnels.
270 *
271 * SO_TC_AV
272 *	"Multimedia Audio/Video Streaming", medium delay tolerant, low-medium
273 *	loss tolerant, elastic flow, constant packet interval, variable rate &
274 *	size.  E.g. AirPlay playback (both video and audio).
275 *
276 * SO_TC_RV
277 *	"Responsive Multimedia Audio/Video", low delay tolerant, low-medium
278 *	loss tolerant, elastic flow, variable packet interval, rate and size.
279 *	E.g. AirPlay mirroring, screen sharing.
280 *
281 * SO_TC_VI
282 *	"Interactive Video", low delay tolerant, low-medium loss tolerant,
283 *	elastic flow, constant packet interval, variable rate & size.  E.g.
284 *	FaceTime video.
285 *
286 * SO_TC_VO
287 *	"Interactive Voice", low delay tolerant, low loss tolerant, inelastic
288 *	flow, constant packet rate, somewhat fixed size.  E.g. VoIP including
289 *	FaceTime audio.
290 *
291 * SO_TC_CTL
292 *	"Network Control", low delay tolerant, low loss tolerant, inelastic
293 *	flow, rate is bursty but short, variable size.  E.g. DNS queries;
294 *	certain types of locally-originated ICMP, ICMPv6; IGMP/MLD join/leave,
295 *	ARP.
296 */
297#define SO_TRAFFIC_CLASS	0x1086	/* Traffic service class (int) */
298#define	 SO_TC_BK_SYS	100		/* lowest class */
299#define	 SO_TC_BK	200
300#define  SO_TC_BE	0
301#define	 SO_TC_RD	300
302#define	 SO_TC_OAM	400
303#define	 SO_TC_AV	500
304#define	 SO_TC_RV	600
305#define	 SO_TC_VI	700
306#define	 SO_TC_VO	800
307#define	 SO_TC_CTL	900		/* highest class */
308#define  SO_TC_MAX	10		/* Total # of traffic classes */
309#ifdef XNU_KERNEL_PRIVATE
310#define  _SO_TC_BK	1		/* deprecated */
311#define  _SO_TC_VI	2		/* deprecated */
312#define  _SO_TC_VO	3		/* deprecated */
313#define  _SO_TC_MAX	4		/* deprecated */
314
315#define	SO_VALID_TC(c)							\
316	(c == SO_TC_BK_SYS || c == SO_TC_BK || c == SO_TC_BE ||		\
317	c == SO_TC_RD || c == SO_TC_OAM || c == SO_TC_AV ||		\
318	c == SO_TC_RV || c == SO_TC_VI || c == SO_TC_VO || c == SO_TC_CTL)
319#endif /* XNU_KERNEL_PRIVATE */
320
321/* Background socket configuration flags */
322#define TRAFFIC_MGT_SO_BACKGROUND       0x0001  /* background socket */
323#define TRAFFIC_MGT_TCP_RECVBG          0x0002  /* Only TCP sockets, receiver throttling */
324
325#define SO_RECV_TRAFFIC_CLASS	0x1087		/* Receive traffic class (bool)*/
326#define SO_TRAFFIC_CLASS_DBG	0x1088		/* Debug traffic class (struct so_tcdbg) */
327#define SO_TRAFFIC_CLASS_STATS	0x1089		/* Traffic class statistics */
328#define SO_PRIVILEGED_TRAFFIC_CLASS 0x1090	/* Privileged traffic class (bool) */
329#define	SO_DEFUNCTOK	0x1100		/* can be defunct'd */
330#define	SO_ISDEFUNCT	0x1101		/* get defunct status */
331
332#define	SO_OPPORTUNISTIC	0x1102	/* deprecated; use SO_TRAFFIC_CLASS */
333
334/*
335 * SO_FLUSH flushes any unsent data generated by a given socket.  It takes
336 * an integer parameter, which can be any of the SO_TC traffic class values,
337 * or the special SO_TC_ALL value.
338 */
339#define	SO_FLUSH	0x1103		/* flush unsent data (int) */
340#define	 SO_TC_ALL	(-1)
341
342#define	SO_RECV_ANYIF	0x1104		/* unrestricted inbound processing */
343#endif /* PRIVATE */
344#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
345
346/*
347 * Structure used for manipulating linger option.
348 */
349struct	linger {
350	int	l_onoff;		/* option on/off */
351	int	l_linger;		/* linger time */
352};
353
354#ifndef __APPLE__
355struct	accept_filter_arg {
356	char	af_name[16];
357	char	af_arg[256-16];
358};
359#endif
360
361#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
362#ifdef __APPLE__
363
364/*
365 * Structure to control non-portable Sockets extension to POSIX
366 */
367struct so_np_extensions {
368	u_int32_t	npx_flags;
369	u_int32_t	npx_mask;
370};
371
372#define SONPX_SETOPTSHUT	0x000000001     /* flag for allowing setsockopt after shutdown */
373
374
375#ifdef KERNEL_PRIVATE
376#define SONPX_MASK_VALID		(SONPX_SETOPTSHUT)
377#define IS_SO_TC_BACKGROUND(_tc_) ((_tc_) == SO_TC_BK || (_tc_) == SO_TC_BK_SYS)
378#endif /* KERNEL_PRIVATE */
379
380#endif
381#endif
382
383/*
384 * Level number for (get/set)sockopt() to apply to socket itself.
385 */
386#define	SOL_SOCKET	0xffff		/* options for socket level */
387
388
389/*
390 * Address families.
391 */
392#define	AF_UNSPEC	0		/* unspecified */
393#define	AF_UNIX		1		/* local to host (pipes) */
394#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
395#define	AF_LOCAL	AF_UNIX		/* backward compatibility */
396#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
397#define	AF_INET		2		/* internetwork: UDP, TCP, etc. */
398#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
399#define	AF_IMPLINK	3		/* arpanet imp addresses */
400#define	AF_PUP		4		/* pup protocols: e.g. BSP */
401#define	AF_CHAOS	5		/* mit CHAOS protocols */
402#define	AF_NS		6		/* XEROX NS protocols */
403#define	AF_ISO		7		/* ISO protocols */
404#define	AF_OSI		AF_ISO
405#define	AF_ECMA		8		/* European computer manufacturers */
406#define	AF_DATAKIT	9		/* datakit protocols */
407#define	AF_CCITT	10		/* CCITT protocols, X.25 etc */
408#define	AF_SNA		11		/* IBM SNA */
409#define AF_DECnet	12		/* DECnet */
410#define AF_DLI		13		/* DEC Direct data link interface */
411#define AF_LAT		14		/* LAT */
412#define	AF_HYLINK	15		/* NSC Hyperchannel */
413#define	AF_APPLETALK	16		/* Apple Talk */
414#define	AF_ROUTE	17		/* Internal Routing Protocol */
415#define	AF_LINK		18		/* Link layer interface */
416#define	pseudo_AF_XTP	19		/* eXpress Transfer Protocol (no AF) */
417#define	AF_COIP		20		/* connection-oriented IP, aka ST II */
418#define	AF_CNT		21		/* Computer Network Technology */
419#define pseudo_AF_RTIP	22		/* Help Identify RTIP packets */
420#define	AF_IPX		23		/* Novell Internet Protocol */
421#define	AF_SIP		24		/* Simple Internet Protocol */
422#define pseudo_AF_PIP	25		/* Help Identify PIP packets */
423#ifdef __APPLE__
424/*define pseudo_AF_BLUE	26	   Identify packets for Blue Box - Not used */
425#define AF_NDRV		27		/* Network Driver 'raw' access */
426#endif
427#define	AF_ISDN		28		/* Integrated Services Digital Network*/
428#define	AF_E164		AF_ISDN		/* CCITT E.164 recommendation */
429#define	pseudo_AF_KEY	29		/* Internal key-management function */
430#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
431#define	AF_INET6	30		/* IPv6 */
432#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
433#define	AF_NATM		31		/* native ATM access */
434#ifdef __APPLE__
435#define AF_SYSTEM	32		/* Kernel event messages */
436#define AF_NETBIOS	33		/* NetBIOS */
437#define AF_PPP		34		/* PPP communication protocol */
438#else
439#define	AF_ATM		30		/* ATM */
440#endif
441#define pseudo_AF_HDRCMPLT 35		/* Used by BPF to not rewrite headers
442					 * in interface output routine
443					 */
444#ifdef PRIVATE
445#define AF_AFP	36			/* Used by AFP */
446#else
447#define AF_RESERVED_36	36		/* Reserved for internal usage */
448#endif
449
450#ifndef __APPLE__
451#define	AF_NETGRAPH	32		/* Netgraph sockets */
452#endif
453#define AF_IEEE80211    37              /* IEEE 802.11 protocol */
454#ifdef __APPLE__
455#define AF_UTUN		38
456#endif
457#define	AF_MAX		39
458#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
459
460/*
461 * [XSI] Structure used by kernel to store most addresses.
462 */
463struct sockaddr {
464	__uint8_t	sa_len;		/* total length */
465	sa_family_t	sa_family;	/* [XSI] address family */
466	char		sa_data[14];	/* [XSI] addr value (actually larger) */
467};
468
469#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
470#define	SOCK_MAXADDRLEN	255		/* longest possible addresses */
471
472/*
473 * Structure used by kernel to pass protocol
474 * information in raw sockets.
475 */
476struct sockproto {
477	__uint16_t	sp_family;		/* address family */
478	__uint16_t	sp_protocol;		/* protocol */
479};
480#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE)*/
481
482/*
483 * RFC 2553: protocol-independent placeholder for socket addresses
484 */
485#define	_SS_MAXSIZE	128
486#define	_SS_ALIGNSIZE	(sizeof(__int64_t))
487#define	_SS_PAD1SIZE	\
488		(_SS_ALIGNSIZE - sizeof(__uint8_t) - sizeof(sa_family_t))
489#define	_SS_PAD2SIZE	\
490		(_SS_MAXSIZE - sizeof(__uint8_t) - sizeof(sa_family_t) - \
491				_SS_PAD1SIZE - _SS_ALIGNSIZE)
492
493/*
494 * [XSI] sockaddr_storage
495 */
496struct sockaddr_storage {
497	__uint8_t	ss_len;		/* address length */
498	sa_family_t	ss_family;	/* [XSI] address family */
499	char			__ss_pad1[_SS_PAD1SIZE];
500	__int64_t	__ss_align;	/* force structure storage alignment */
501	char			__ss_pad2[_SS_PAD2SIZE];
502};
503
504/*
505 * Protocol families, same as address families for now.
506 */
507#define	PF_UNSPEC	AF_UNSPEC
508#define	PF_LOCAL	AF_LOCAL
509#define	PF_UNIX		PF_LOCAL	/* backward compatibility */
510#define	PF_INET		AF_INET
511#define	PF_IMPLINK	AF_IMPLINK
512#define	PF_PUP		AF_PUP
513#define	PF_CHAOS	AF_CHAOS
514#define	PF_NS		AF_NS
515#define	PF_ISO		AF_ISO
516#define	PF_OSI		AF_ISO
517#define	PF_ECMA		AF_ECMA
518#define	PF_DATAKIT	AF_DATAKIT
519#define	PF_CCITT	AF_CCITT
520#define	PF_SNA		AF_SNA
521#define PF_DECnet	AF_DECnet
522#define PF_DLI		AF_DLI
523#define PF_LAT		AF_LAT
524#define	PF_HYLINK	AF_HYLINK
525#define	PF_APPLETALK	AF_APPLETALK
526#define	PF_ROUTE	AF_ROUTE
527#define	PF_LINK		AF_LINK
528#define	PF_XTP		pseudo_AF_XTP	/* really just proto family, no AF */
529#define	PF_COIP		AF_COIP
530#define	PF_CNT		AF_CNT
531#define	PF_SIP		AF_SIP
532#define	PF_IPX		AF_IPX		/* same format as AF_NS */
533#define PF_RTIP		pseudo_AF_RTIP	/* same format as AF_INET */
534#define PF_PIP		pseudo_AF_PIP
535#ifdef __APPLE__
536#define PF_NDRV		AF_NDRV
537#endif
538#define	PF_ISDN		AF_ISDN
539#define	PF_KEY		pseudo_AF_KEY
540#define	PF_INET6	AF_INET6
541#define	PF_NATM		AF_NATM
542#ifdef __APPLE__
543#define PF_SYSTEM	AF_SYSTEM
544#define PF_NETBIOS	AF_NETBIOS
545#define PF_PPP		AF_PPP
546#ifdef PRIVATE
547#define PF_AFP   	AF_AFP
548#else
549#define PF_RESERVED_36  AF_RESERVED_36
550#endif
551
552#else
553#define	PF_ATM		AF_ATM
554#define	PF_NETGRAPH	AF_NETGRAPH
555#endif
556
557#ifdef __APPLE__
558#define PF_UTUN		AF_UTUN
559#endif
560#define	PF_MAX		AF_MAX
561
562/*
563 * These do not have socket-layer support:
564 */
565#define	PF_VLAN		((uint32_t)0x766c616e)	/* 'vlan' */
566#define PF_BOND		((uint32_t)0x626f6e64)	/* 'bond' */
567#ifdef KERNEL_PRIVATE
568#define PF_BRIDGE	((uint32_t)0x62726467)	/* 'brdg' */
569#endif /* KERNEL_PRIVATE */
570
571/*
572 * Definitions for network related sysctl, CTL_NET.
573 *
574 * Second level is protocol family.
575 * Third level is protocol number.
576 *
577 * Further levels are defined by the individual families below.
578 */
579#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
580#define NET_MAXID	AF_MAX
581#endif /* (_POSIX_C_SOURCE && !_DARWIN_C_SOURCE) */
582
583#ifdef KERNEL_PRIVATE
584#define CTL_NET_NAMES { \
585	{ 0, 0 }, \
586	{ "local", CTLTYPE_NODE }, \
587	{ "inet", CTLTYPE_NODE }, \
588	{ "implink", CTLTYPE_NODE }, \
589	{ "pup", CTLTYPE_NODE }, \
590	{ "chaos", CTLTYPE_NODE }, \
591	{ "xerox_ns", CTLTYPE_NODE }, \
592	{ "iso", CTLTYPE_NODE }, \
593	{ "emca", CTLTYPE_NODE }, \
594	{ "datakit", CTLTYPE_NODE }, \
595	{ "ccitt", CTLTYPE_NODE }, \
596	{ "ibm_sna", CTLTYPE_NODE }, \
597	{ "decnet", CTLTYPE_NODE }, \
598	{ "dec_dli", CTLTYPE_NODE }, \
599	{ "lat", CTLTYPE_NODE }, \
600	{ "hylink", CTLTYPE_NODE }, \
601	{ "appletalk", CTLTYPE_NODE }, \
602	{ "route", CTLTYPE_NODE }, \
603	{ "link_layer", CTLTYPE_NODE }, \
604	{ "xtp", CTLTYPE_NODE }, \
605	{ "coip", CTLTYPE_NODE }, \
606	{ "cnt", CTLTYPE_NODE }, \
607	{ "rtip", CTLTYPE_NODE }, \
608	{ "ipx", CTLTYPE_NODE }, \
609	{ "sip", CTLTYPE_NODE }, \
610	{ "pip", CTLTYPE_NODE }, \
611	{ 0, 0 }, \
612	{ "ndrv", CTLTYPE_NODE }, \
613	{ "isdn", CTLTYPE_NODE }, \
614	{ "key", CTLTYPE_NODE }, \
615	{ "inet6", CTLTYPE_NODE }, \
616	{ "natm", CTLTYPE_NODE }, \
617	{ "sys", CTLTYPE_NODE }, \
618	{ "netbios", CTLTYPE_NODE }, \
619	{ "ppp", CTLTYPE_NODE }, \
620	{ "hdrcomplete", CTLTYPE_NODE }, \
621}
622#endif /* KERNEL_PRIVATE */
623
624#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
625/*
626 * PF_ROUTE - Routing table
627 *
628 * Three additional levels are defined:
629 *	Fourth: address family, 0 is wildcard
630 *	Fifth: type of info, defined below
631 *	Sixth: flag(s) to mask with for NET_RT_FLAGS
632 */
633#define NET_RT_DUMP		1	/* dump; may limit to a.f. */
634#define NET_RT_FLAGS		2	/* by flags, e.g. RESOLVING */
635#define NET_RT_IFLIST		3	/* survey interface list */
636#define NET_RT_STAT		4	/* routing statistics */
637#define NET_RT_TRASH		5	/* routes not in table but not freed */
638#define NET_RT_IFLIST2		6	/* interface list with addresses */
639#define NET_RT_DUMP2		7	/* dump; may limit to a.f. */
640#ifdef PRIVATE
641#define	NET_RT_DUMPX		8	/* private */
642#define	NET_RT_DUMPX_FLAGS	9	/* private */
643#endif /* PRIVATE */
644#define	NET_RT_MAXID		10
645#endif /* (_POSIX_C_SOURCE && !_DARWIN_C_SOURCE) */
646
647#ifdef KERNEL_PRIVATE
648#define CTL_NET_RT_NAMES { \
649	{ 0, 0 }, \
650	{ "dump", CTLTYPE_STRUCT }, \
651	{ "flags", CTLTYPE_STRUCT }, \
652	{ "iflist", CTLTYPE_STRUCT }, \
653	{ "stat", CTLTYPE_STRUCT }, \
654	{ "trash", CTLTYPE_INT }, \
655	{ "iflist2", CTLTYPE_STRUCT }, \
656        { "dump2", CTLTYPE_STRUCT }, \
657        { "dumpx", CTLTYPE_STRUCT }, \
658        { "dumpx_flags", CTLTYPE_STRUCT }, \
659}
660
661#endif /* KERNEL_PRIVATE */
662
663/*
664 * Maximum queue length specifiable by listen.
665 */
666#define	SOMAXCONN	128
667
668/*
669 * [XSI] Message header for recvmsg and sendmsg calls.
670 * Used value-result for recvmsg, value only for sendmsg.
671 */
672struct msghdr {
673	void		*msg_name;	/* [XSI] optional address */
674	socklen_t	msg_namelen;	/* [XSI] size of address */
675	struct		iovec *msg_iov;	/* [XSI] scatter/gather array */
676	int		msg_iovlen;	/* [XSI] # elements in msg_iov */
677	void		*msg_control;	/* [XSI] ancillary data, see below */
678	socklen_t	msg_controllen;	/* [XSI] ancillary data buffer len */
679	int		msg_flags;	/* [XSI] flags on received message */
680};
681
682#ifdef KERNEL
683/*
684 * In-kernel representation of "struct msghdr" from
685 * userspace. Has enough precision for 32-bit or
686 * 64-bit clients, but does not need to be packed.
687 */
688
689struct user_msghdr {
690	user_addr_t	msg_name;		/* optional address */
691	socklen_t	msg_namelen;		/* size of address */
692	user_addr_t	msg_iov;		/* scatter/gather array */
693	int		msg_iovlen;		/* # elements in msg_iov */
694	user_addr_t	msg_control;		/* ancillary data, see below */
695	socklen_t	msg_controllen;		/* ancillary data buffer len */
696	int		msg_flags;		/* flags on received message */
697};
698
699/*
700 * LP64 user version of struct msghdr.
701 * WARNING - keep in sync with struct msghdr
702 */
703
704struct user64_msghdr {
705	user64_addr_t	msg_name;		/* optional address */
706	socklen_t	msg_namelen;		/* size of address */
707	user64_addr_t	msg_iov;		/* scatter/gather array */
708	int		msg_iovlen;		/* # elements in msg_iov */
709	user64_addr_t	msg_control;		/* ancillary data, see below */
710	socklen_t	msg_controllen;		/* ancillary data buffer len */
711	int		msg_flags;		/* flags on received message */
712};
713
714/*
715 * ILP32 user version of struct msghdr.
716 * WARNING - keep in sync with struct msghdr
717 */
718
719struct user32_msghdr {
720	user32_addr_t	msg_name;	/* optional address */
721	socklen_t	msg_namelen;	/* size of address */
722	user32_addr_t	msg_iov;	/* scatter/gather array */
723	int		msg_iovlen;	/* # elements in msg_iov */
724	user32_addr_t	msg_control;	/* ancillary data, see below */
725	socklen_t	msg_controllen;	/* ancillary data buffer len */
726	int		msg_flags;	/* flags on received message */
727};
728
729#endif // KERNEL
730
731#define	MSG_OOB		0x1		/* process out-of-band data */
732#define	MSG_PEEK	0x2		/* peek at incoming message */
733#define	MSG_DONTROUTE	0x4		/* send without using routing tables */
734#define	MSG_EOR		0x8		/* data completes record */
735#define	MSG_TRUNC	0x10		/* data discarded before delivery */
736#define	MSG_CTRUNC	0x20		/* control data lost before delivery */
737#define	MSG_WAITALL	0x40		/* wait for full request or error */
738#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
739#define	MSG_DONTWAIT	0x80		/* this message should be nonblocking */
740#define	MSG_EOF		0x100		/* data completes connection */
741#ifdef __APPLE__
742#ifndef PRIVATE
743#ifdef __APPLE_API_OBSOLETE
744#define MSG_WAITSTREAM  0x200           /* wait up to full request.. may return partial */
745#endif
746#else
747#define MSG_WAITSTREAM  0x200           /* wait up to full request.. may return partial */
748#endif
749#define MSG_FLUSH	0x400		/* Start of 'hold' seq; dump so_temp */
750#define MSG_HOLD	0x800		/* Hold frag in so_temp */
751#define MSG_SEND	0x1000		/* Send the packet in so_temp */
752#define MSG_HAVEMORE	0x2000		/* Data ready to be read */
753#define MSG_RCVMORE	0x4000		/* Data remains in current pkt */
754#endif
755#ifdef KERNEL_PRIVATE
756#define MSG_COMPAT      0x8000		/* deprecated */
757#endif /* KERNEL_PRIVATE */
758#define MSG_NEEDSA	0x10000		/* Fail receive if socket address cannot be allocated */
759#ifdef KERNEL_PRIVATE
760#define MSG_NBIO	0x20000		/* FIONBIO mode, used by fifofs */
761#endif
762#ifdef	KERNEL
763#define MSG_USEUPCALL	0x80000000 /* Inherit upcall in sock_accept */
764#endif
765#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
766
767/*
768 * Header for ancillary data objects in msg_control buffer.
769 * Used for additional information with/about a datagram
770 * not expressible by flags.  The format is a sequence
771 * of message elements headed by cmsghdr structures.
772 */
773struct cmsghdr {
774	socklen_t	cmsg_len;	/* [XSI] data byte count, including hdr */
775	int		cmsg_level;	/* [XSI] originating protocol */
776	int		cmsg_type;	/* [XSI] protocol-specific type */
777/* followed by	unsigned char  cmsg_data[]; */
778};
779
780#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
781#ifndef __APPLE__
782/*
783 * While we may have more groups than this, the cmsgcred struct must
784 * be able to fit in an mbuf, and NGROUPS_MAX is too large to allow
785 * this.
786*/
787#define CMGROUP_MAX 16
788
789/*
790 * Credentials structure, used to verify the identity of a peer
791 * process that has sent us a message. This is allocated by the
792 * peer process but filled in by the kernel. This prevents the
793 * peer from lying about its identity. (Note that cmcred_groups[0]
794 * is the effective GID.)
795 */
796struct cmsgcred {
797	pid_t	cmcred_pid;		/* PID of sending process */
798	uid_t	cmcred_uid;		/* real UID of sending process */
799	uid_t	cmcred_euid;		/* effective UID of sending process */
800	gid_t	cmcred_gid;		/* real GID of sending process */
801	short	cmcred_ngroups;		/* number or groups */
802	gid_t	cmcred_groups[CMGROUP_MAX];	/* groups */
803};
804#endif
805#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
806
807/* given pointer to struct cmsghdr, return pointer to data */
808#define	CMSG_DATA(cmsg)		((unsigned char *)(cmsg) + \
809				 __DARWIN_ALIGN32(sizeof(struct cmsghdr)))
810
811/*
812 * RFC 2292 requires to check msg_controllen, in case that the kernel returns
813 * an empty list for some reasons.
814 */
815#define CMSG_FIRSTHDR(mhdr) \
816        ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
817         (struct cmsghdr *)(mhdr)->msg_control : \
818         (struct cmsghdr *)0L)
819
820
821/*
822 * Given pointer to struct cmsghdr, return pointer to next cmsghdr
823 * RFC 2292 says that CMSG_NXTHDR(mhdr, NULL) is equivalent to CMSG_FIRSTHDR(mhdr)
824 */
825#define	CMSG_NXTHDR(mhdr, cmsg)						\
826	((char *)(cmsg) == (char *)0L ? CMSG_FIRSTHDR(mhdr) :		\
827	 ((((unsigned char *)(cmsg) +					\
828	    __DARWIN_ALIGN32((__uint32_t)(cmsg)->cmsg_len) +		\
829	    __DARWIN_ALIGN32(sizeof(struct cmsghdr))) >			\
830	    ((unsigned char *)(mhdr)->msg_control +			\
831	     (mhdr)->msg_controllen)) ?					\
832	  (struct cmsghdr *)0L /* NULL */ :				\
833	  (struct cmsghdr *)(void *)((unsigned char *)(cmsg) +		\
834	 		    __DARWIN_ALIGN32((__uint32_t)(cmsg)->cmsg_len))))
835
836#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
837/* RFC 2292 additions */
838#define	CMSG_SPACE(l)		(__DARWIN_ALIGN32(sizeof(struct cmsghdr)) + __DARWIN_ALIGN32(l))
839#define	CMSG_LEN(l)		(__DARWIN_ALIGN32(sizeof(struct cmsghdr)) + (l))
840
841#ifdef KERNEL
842#define	CMSG_ALIGN(n)	__DARWIN_ALIGN32(n)
843#endif
844#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
845
846/* "Socket"-level control message types: */
847#define	SCM_RIGHTS			0x01	/* access rights (array of int) */
848#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
849#define	SCM_TIMESTAMP			0x02	/* timestamp (struct timeval) */
850#define	SCM_CREDS			0x03	/* process creds (struct cmsgcred) */
851#define	SCM_TIMESTAMP_MONOTONIC		0x04	/* timestamp (uint64_t) */
852
853#ifdef KERNEL_PRIVATE
854/*
855 * 4.3 compat sockaddr (deprecated)
856 */
857struct osockaddr {
858	__uint16_t	sa_family;	/* address family */
859	char	sa_data[14];		/* up to 14 bytes of direct address */
860};
861
862/*
863 * 4.3-compat message header (deprecated)
864 */
865struct omsghdr {
866	void		*msg_name;		/* optional address */
867	socklen_t	msg_namelen;		/* size of address */
868	struct	iovec	*msg_iov;		/* scatter/gather array */
869	int		msg_iovlen;		/* # elements in msg_iov */
870	void		*msg_accrights;		/* access rights sent/rcvd */
871	int		msg_accrightslen;
872};
873
874#define	SA(s)	((struct sockaddr *)(void *)(s))
875#endif /* KERNEL_PRIVATE */
876#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
877
878/*
879 * howto arguments for shutdown(2), specified by Posix.1g.
880 */
881#define	SHUT_RD		0		/* shut down the reading side */
882#define	SHUT_WR		1		/* shut down the writing side */
883#define	SHUT_RDWR	2		/* shut down both sides */
884
885#if !defined(_POSIX_C_SOURCE)
886/*
887 * sendfile(2) header/trailer struct
888 */
889struct sf_hdtr {
890	struct iovec *headers;	/* pointer to an array of header struct iovec's */
891	int hdr_cnt;		/* number of header iovec's */
892	struct iovec *trailers;	/* pointer to an array of trailer struct iovec's */
893	int trl_cnt;		/* number of trailer iovec's */
894};
895
896#ifdef KERNEL
897
898/* In-kernel representation */
899struct user_sf_hdtr {
900	user_addr_t headers;	/* pointer to an array of header struct iovec's */
901	int hdr_cnt;		/* number of header iovec's */
902	user_addr_t trailers;	/* pointer to an array of trailer struct iovec's */
903	int trl_cnt;		/* number of trailer iovec's */
904};
905
906/* LP64 user version of struct sf_hdtr */
907struct user64_sf_hdtr {
908	user64_addr_t headers;	/* pointer to an array of header struct iovec's */
909	int hdr_cnt;		/* number of header iovec's */
910	user64_addr_t trailers;	/* pointer to an array of trailer struct iovec's */
911	int trl_cnt;		/* number of trailer iovec's */
912};
913
914/* ILP32 user version of struct sf_hdtr */
915struct user32_sf_hdtr {
916	user32_addr_t headers;	/* pointer to an array of header struct iovec's */
917	int hdr_cnt;		/* number of header iovec's */
918	user32_addr_t trailers;	/* pointer to an array of trailer struct iovec's */
919	int trl_cnt;		/* number of trailer iovec's */
920};
921
922#endif /* KERNEL */
923
924#endif	/* !_POSIX_C_SOURCE */
925
926#ifndef	KERNEL
927__BEGIN_DECLS
928int	accept(int, struct sockaddr * __restrict, socklen_t * __restrict)
929		__DARWIN_ALIAS_C(accept);
930int	bind(int, const struct sockaddr *, socklen_t) __DARWIN_ALIAS(bind);
931int	connect(int, const struct sockaddr *, socklen_t) __DARWIN_ALIAS_C( connect);
932int	getpeername(int, struct sockaddr * __restrict, socklen_t * __restrict)
933		__DARWIN_ALIAS(getpeername);
934int	getsockname(int, struct sockaddr * __restrict, socklen_t * __restrict)
935		__DARWIN_ALIAS(getsockname);
936int	getsockopt(int, int, int, void * __restrict, socklen_t * __restrict);
937int	listen(int, int) __DARWIN_ALIAS(listen);
938ssize_t	recv(int, void *, size_t, int) __DARWIN_ALIAS_C(recv);
939ssize_t	recvfrom(int, void *, size_t, int, struct sockaddr * __restrict,
940		socklen_t * __restrict) __DARWIN_ALIAS_C(recvfrom);
941ssize_t	recvmsg(int, struct msghdr *, int) __DARWIN_ALIAS_C(recvmsg);
942ssize_t	send(int, const void *, size_t, int) __DARWIN_ALIAS_C(send);
943ssize_t	sendmsg(int, const struct msghdr *, int) __DARWIN_ALIAS_C(sendmsg);
944ssize_t	sendto(int, const void *, size_t,
945		int, const struct sockaddr *, socklen_t) __DARWIN_ALIAS_C(sendto);
946int	setsockopt(int, int, int, const void *, socklen_t);
947int	shutdown(int, int);
948int	sockatmark(int) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
949int	socket(int, int, int);
950int	socketpair(int, int, int, int *) __DARWIN_ALIAS(socketpair);
951
952#if !defined(_POSIX_C_SOURCE)
953int	sendfile(int, int, off_t, off_t *, struct sf_hdtr *, int);
954#endif	/* !_POSIX_C_SOURCE */
955
956#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
957void	pfctlinput(int, struct sockaddr *);
958#endif	/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
959__END_DECLS
960#endif /* !KERNEL */
961
962#ifdef KERNEL
963#include <sys/kpi_socket.h>
964#endif
965
966#endif /* !_SYS_SOCKET_H_ */
967