1 /*	$NetBSD: socket.h,v 1.119 2016/04/06 19:45:46 roy Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1985, 1986, 1988, 1993, 1994
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)socket.h	8.6 (Berkeley) 5/3/95
61  */
62 
63 #ifndef _SYS_SOCKET_H_
64 #define	_SYS_SOCKET_H_
65 
66 #include <sys/featuretest.h>
67 
68 /*
69  * Definitions related to sockets: types, address families, options.
70  */
71 
72 /*
73  * Data types.
74  */
75 #include <sys/ansi.h>
76 
77 #ifndef sa_family_t
78 typedef __sa_family_t	sa_family_t;
79 #define sa_family_t	__sa_family_t
80 #endif
81 
82 #ifndef socklen_t
83 typedef __socklen_t	socklen_t;
84 #define socklen_t	__socklen_t
85 #endif
86 
87 #include <machine/ansi.h>
88 
89 #ifdef	_BSD_SIZE_T_
90 typedef	_BSD_SIZE_T_	size_t;
91 #undef	_BSD_SIZE_T_
92 #endif
93 
94 #ifdef	_BSD_SSIZE_T_
95 typedef	_BSD_SSIZE_T_	ssize_t;
96 #undef	_BSD_SSIZE_T_
97 #endif
98 
99 #include <sys/uio.h>
100 #include <sys/sigtypes.h>
101 
102 /*
103  * Socket types.
104  */
105 #define	SOCK_STREAM	1		/* stream socket */
106 #define	SOCK_DGRAM	2		/* datagram socket */
107 #define	SOCK_RAW	3		/* raw-protocol interface */
108 #define	SOCK_RDM	4		/* reliably-delivered message */
109 #define	SOCK_SEQPACKET	5		/* sequenced packet stream */
110 #define	SOCK_CONN_DGRAM	6		/* connection-orientated datagram */
111 #define	SOCK_DCCP	SOCK_CONN_DGRAM
112 
113 #define	SOCK_CLOEXEC	0x10000000	/* set close on exec on socket */
114 #define	SOCK_NONBLOCK	0x20000000	/* set non blocking i/o socket */
115 #define	SOCK_NOSIGPIPE	0x40000000	/* don't send sigpipe */
116 #define	SOCK_FLAGS_MASK	0xf0000000	/* flags mask */
117 
118 /*
119  * Option flags per-socket.
120  */
121 #define	SO_DEBUG	0x0001		/* turn on debugging info recording */
122 #define	SO_ACCEPTCONN	0x0002		/* socket has had listen() */
123 #define	SO_REUSEADDR	0x0004		/* allow local address reuse */
124 #define	SO_KEEPALIVE	0x0008		/* keep connections alive */
125 #define	SO_DONTROUTE	0x0010		/* just use interface addresses */
126 #define	SO_BROADCAST	0x0020		/* permit sending of broadcast msgs */
127 #define	SO_USELOOPBACK	0x0040		/* bypass hardware when possible */
128 #define	SO_LINGER	0x0080		/* linger on close if data present */
129 #define	SO_OOBINLINE	0x0100		/* leave received OOB data in line */
130 #define	SO_REUSEPORT	0x0200		/* allow local address & port reuse */
131 /* 	SO_OTIMESTAMP	0x0400		*/
132 #define	SO_NOSIGPIPE	0x0800		/* no SIGPIPE from EPIPE */
133 #define	SO_ACCEPTFILTER	0x1000		/* there is an accept filter */
134 #define	SO_TIMESTAMP	0x2000		/* timestamp received dgram traffic */
135 
136 
137 /*
138  * Additional options, not kept in so_options.
139  */
140 #define SO_SNDBUF	0x1001		/* send buffer size */
141 #define SO_RCVBUF	0x1002		/* receive buffer size */
142 #define SO_SNDLOWAT	0x1003		/* send low-water mark */
143 #define SO_RCVLOWAT	0x1004		/* receive low-water mark */
144 /* SO_OSNDTIMEO		0x1005 */
145 /* SO_ORCVTIMEO		0x1006 */
146 #define	SO_ERROR	0x1007		/* get error status and clear */
147 #define	SO_TYPE		0x1008		/* get socket type */
148 #define	SO_OVERFLOWED	0x1009		/* datagrams: return packets dropped */
149 
150 #define	SO_NOHEADER	0x100a		/* user supplies no header to kernel;
151 					 * kernel removes header and supplies
152 					 * payload
153 					 */
154 #define SO_SNDTIMEO	0x100b		/* send timeout */
155 #define SO_RCVTIMEO	0x100c		/* receive timeout */
156 /*
157  * Structure used for manipulating linger option.
158  */
159 struct	linger {
160 	int	l_onoff;		/* option on/off */
161 	int	l_linger;		/* linger time in seconds */
162 };
163 
164 struct	accept_filter_arg {
165 	char	af_name[16];
166 	char	af_arg[256-16];
167 };
168 
169 /*
170  * Level number for (get/set)sockopt() to apply to socket itself.
171  */
172 #define	SOL_SOCKET	0xffff		/* options for socket level */
173 
174 /*
175  * Address families.
176  */
177 #define	AF_UNSPEC	0		/* unspecified */
178 #define	AF_LOCAL	1		/* local to host */
179 #define	AF_UNIX		AF_LOCAL	/* backward compatibility */
180 #define	AF_INET		2		/* internetwork: UDP, TCP, etc. */
181 #define	AF_IMPLINK	3		/* arpanet imp addresses */
182 #define	AF_PUP		4		/* pup protocols: e.g. BSP */
183 #define	AF_CHAOS	5		/* mit CHAOS protocols */
184 #define	AF_NS		6		/* XEROX NS protocols */
185 #define	AF_ISO		7		/* ISO protocols */
186 #define	AF_OSI		AF_ISO
187 #define	AF_ECMA		8		/* european computer manufacturers */
188 #define	AF_DATAKIT	9		/* datakit protocols */
189 #define	AF_CCITT	10		/* CCITT protocols, X.25 etc */
190 #define	AF_SNA		11		/* IBM SNA */
191 #define AF_DECnet	12		/* DECnet */
192 #define AF_DLI		13		/* DEC Direct data link interface */
193 #define AF_LAT		14		/* LAT */
194 #define	AF_HYLINK	15		/* NSC Hyperchannel */
195 #define	AF_APPLETALK	16		/* Apple Talk */
196 #define	AF_OROUTE	17		/* Internal Routing Protocol */
197 #define	AF_LINK		18		/* Link layer interface */
198 #if defined(_NETBSD_SOURCE)
199 #define	pseudo_AF_XTP	19		/* eXpress Transfer Protocol (no AF) */
200 #endif
201 #define	AF_COIP		20		/* connection-oriented IP, aka ST II */
202 #define	AF_CNT		21		/* Computer Network Technology */
203 #if defined(_NETBSD_SOURCE)
204 #define pseudo_AF_RTIP	22		/* Help Identify RTIP packets */
205 #endif
206 #define	AF_IPX		23		/* Novell Internet Protocol */
207 #define	AF_INET6	24		/* IP version 6 */
208 #if defined(_NETBSD_SOURCE)
209 #define pseudo_AF_PIP	25		/* Help Identify PIP packets */
210 #endif
211 #define AF_ISDN		26		/* Integrated Services Digital Network*/
212 #define AF_E164		AF_ISDN		/* CCITT E.164 recommendation */
213 #define AF_NATM		27		/* native ATM access */
214 #define AF_ARP		28		/* (rev.) addr. res. prot. (RFC 826) */
215 #if defined(_NETBSD_SOURCE)
216 #define pseudo_AF_KEY	29		/* Internal key management protocol  */
217 #define	pseudo_AF_HDRCMPLT 30		/* Used by BPF to not rewrite hdrs
218 					   in interface output routine */
219 #endif
220 #define AF_BLUETOOTH	31		/* Bluetooth: HCI, SCO, L2CAP, RFCOMM */
221 #define	AF_IEEE80211	32		/* IEEE80211 */
222 #define	AF_MPLS		33		/* MultiProtocol Label Switching */
223 #define	AF_ROUTE	34		/* Internal Routing Protocol */
224 #define	AF_MAX		35
225 
226 /*
227  * Structure used by kernel to store most
228  * addresses.
229  */
230 struct sockaddr {
231 	__uint8_t	sa_len;		/* total length */
232 	sa_family_t	sa_family;	/* address family */
233 	char		sa_data[14];	/* actually longer; address value */
234 };
235 
236 #if defined(_KERNEL)
237 /*
238  * Structure used by kernel to pass protocol
239  * information in raw sockets.
240  */
241 struct sockproto {
242 	u_short	sp_family;		/* address family */
243 	u_short	sp_protocol;		/* protocol */
244 };
245 
246 /*
247  * we make the entire struct at least UCHAR_MAX + 1 in size since existing
248  * use of sockaddr_un permits a path up to 253 bytes + '\0'.
249  * sizeof(sb_len) + sizeof(sb_family) + 253 + '\0'
250  */
251 #define _SB_DATASIZE	254
252 struct sockaddr_big {
253     union {
254 	struct {
255 	    __uint8_t	sb_len;
256 	    sa_family_t	sb_family;
257 	    char	sb_data[_SB_DATASIZE];
258 	};
259 	uint64_t dummy; /* solicit natural alignment */
260     };
261 };
262 
263 #endif /* _KERNEL */
264 
265 #if 1
266 /*
267  * RFC 2553: protocol-independent placeholder for socket addresses
268  */
269 #define _SS_MAXSIZE	128
270 #define _SS_ALIGNSIZE	(sizeof(__int64_t))
271 #define _SS_PAD1SIZE	(_SS_ALIGNSIZE - 2)
272 #define _SS_PAD2SIZE	(_SS_MAXSIZE - 2 - \
273 				_SS_PAD1SIZE - _SS_ALIGNSIZE)
274 
275 #if (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
276 struct sockaddr_storage {
277 	__uint8_t	ss_len;		/* address length */
278 	sa_family_t	ss_family;	/* address family */
279 	char		__ss_pad1[_SS_PAD1SIZE];
280 	__int64_t     __ss_align;/* force desired structure storage alignment */
281 	char		__ss_pad2[_SS_PAD2SIZE];
282 };
283 #define	sstosa(__ss)	((struct sockaddr *)(__ss))
284 #define	sstocsa(__ss)	((const struct sockaddr *)(__ss))
285 #endif /* _XOPEN_SOURCE >= 500 || _NETBSD_SOURCE */
286 #endif /* 1 */
287 
288 /*
289  * Protocol families, same as address families for now.
290  */
291 #define	PF_UNSPEC	AF_UNSPEC
292 #define	PF_LOCAL	AF_LOCAL
293 #define	PF_UNIX		PF_LOCAL	/* backward compatibility */
294 #define	PF_INET		AF_INET
295 #define	PF_IMPLINK	AF_IMPLINK
296 #define	PF_PUP		AF_PUP
297 #define	PF_CHAOS	AF_CHAOS
298 #define	PF_NS		AF_NS
299 #define	PF_ISO		AF_ISO
300 #define	PF_OSI		AF_ISO
301 #define	PF_ECMA		AF_ECMA
302 #define	PF_DATAKIT	AF_DATAKIT
303 #define	PF_CCITT	AF_CCITT
304 #define	PF_SNA		AF_SNA
305 #define PF_DECnet	AF_DECnet
306 #define PF_DLI		AF_DLI
307 #define PF_LAT		AF_LAT
308 #define	PF_HYLINK	AF_HYLINK
309 #define	PF_APPLETALK	AF_APPLETALK
310 #define	PF_OROUTE	AF_OROUTE
311 #define	PF_LINK		AF_LINK
312 #if defined(_NETBSD_SOURCE)
313 #define	PF_XTP		pseudo_AF_XTP	/* really just proto family, no AF */
314 #endif
315 #define	PF_COIP		AF_COIP
316 #define	PF_CNT		AF_CNT
317 #define	PF_INET6	AF_INET6
318 #define	PF_IPX		AF_IPX		/* same format as AF_NS */
319 #if defined(_NETBSD_SOURCE)
320 #define PF_RTIP		pseudo_AF_RTIP	/* same format as AF_INET */
321 #define PF_PIP		pseudo_AF_PIP
322 #endif
323 #define PF_ISDN		AF_ISDN		/* same as E164 */
324 #define PF_E164		AF_E164
325 #define PF_NATM		AF_NATM
326 #define PF_ARP		AF_ARP
327 #if defined(_NETBSD_SOURCE)
328 #define PF_KEY 		pseudo_AF_KEY	/* like PF_ROUTE, only for key mgmt */
329 #endif
330 #define PF_BLUETOOTH	AF_BLUETOOTH
331 #define	PF_MPLS		AF_MPLS
332 #define	PF_ROUTE	AF_ROUTE
333 
334 #define	PF_MAX		AF_MAX
335 
336 #if defined(_NETBSD_SOURCE)
337 
338 #ifndef pid_t
339 typedef __pid_t		pid_t;		/* process id */
340 #define pid_t		__pid_t
341 #endif
342 
343 #ifndef	gid_t
344 typedef	__gid_t		gid_t;		/* group id */
345 #define	gid_t		__gid_t
346 #endif
347 
348 #ifndef	uid_t
349 typedef	__uid_t		uid_t;		/* user id */
350 #define	uid_t		__uid_t
351 #endif
352 
353 /*
354  * Socket credentials.
355  */
356 struct sockcred {
357 	pid_t	sc_pid;			/* process id */
358 	uid_t	sc_uid;			/* real user id */
359 	uid_t	sc_euid;		/* effective user id */
360 	gid_t	sc_gid;			/* real group id */
361 	gid_t	sc_egid;		/* effective group id */
362 	int	sc_ngroups;		/* number of supplemental groups */
363 	gid_t	sc_groups[1];		/* variable length */
364 };
365 
366 /*
367  * Compute size of a sockcred structure with groups.
368  */
369 #define	SOCKCREDSIZE(ngrps) \
370 	(/*CONSTCOND*/sizeof(struct sockcred) + (sizeof(gid_t) * \
371 	    ((ngrps) ? ((ngrps) - 1) : 0)))
372 #endif /* _NETBSD_SOURCE */
373 
374 
375 #if defined(_NETBSD_SOURCE)
376 /*
377  * Definitions for network related sysctl, CTL_NET.
378  *
379  * Second level is protocol family.
380  * Third level is protocol number.
381  *
382  * Further levels are defined by the individual families below.
383  */
384 #define NET_MAXID	AF_MAX
385 
386 #define CTL_NET_NAMES { \
387 	{ 0, 0 }, \
388 	{ "local", CTLTYPE_NODE }, \
389 	{ "inet", CTLTYPE_NODE }, \
390 	{ "implink", CTLTYPE_NODE }, \
391 	{ "pup", CTLTYPE_NODE }, \
392 	{ "chaos", CTLTYPE_NODE }, \
393 	{ "xerox_ns", CTLTYPE_NODE }, \
394 	{ "iso", CTLTYPE_NODE }, \
395 	{ "emca", CTLTYPE_NODE }, \
396 	{ "datakit", CTLTYPE_NODE }, \
397 	{ "ccitt", CTLTYPE_NODE }, \
398 	{ "ibm_sna", CTLTYPE_NODE }, \
399 	{ "decnet", CTLTYPE_NODE }, \
400 	{ "dec_dli", CTLTYPE_NODE }, \
401 	{ "lat", CTLTYPE_NODE }, \
402 	{ "hylink", CTLTYPE_NODE }, \
403 	{ "appletalk", CTLTYPE_NODE }, \
404 	{ "oroute", CTLTYPE_NODE }, \
405 	{ "link_layer", CTLTYPE_NODE }, \
406 	{ "xtp", CTLTYPE_NODE }, \
407 	{ "coip", CTLTYPE_NODE }, \
408 	{ "cnt", CTLTYPE_NODE }, \
409 	{ "rtip", CTLTYPE_NODE }, \
410 	{ "ipx", CTLTYPE_NODE }, \
411 	{ "inet6", CTLTYPE_NODE }, \
412 	{ "pip", CTLTYPE_NODE }, \
413 	{ "isdn", CTLTYPE_NODE }, \
414 	{ "natm", CTLTYPE_NODE }, \
415 	{ "arp", CTLTYPE_NODE }, \
416 	{ "key", CTLTYPE_NODE }, \
417 	{ "ieee80211", CTLTYPE_NODE }, \
418 	{ "mlps", CTLTYPE_NODE }, \
419 	{ "route", CTLTYPE_NODE }, \
420 }
421 
422 struct kinfo_pcb {
423 	__uint64_t	ki_pcbaddr;	/* PTR: pcb addr */
424 	__uint64_t	ki_ppcbaddr;	/* PTR: ppcb addr */
425 	__uint64_t	ki_sockaddr;	/* PTR: socket addr */
426 
427 	__uint32_t	ki_family;	/* INT: protocol family */
428 	__uint32_t	ki_type;	/* INT: socket type */
429 	__uint32_t	ki_protocol;	/* INT: protocol */
430 	__uint32_t	ki_pflags;	/* INT: generic protocol flags */
431 
432 	__uint32_t	ki_sostate;	/* INT: socket state */
433 	__uint32_t	ki_prstate;	/* INT: protocol state */
434 	__int32_t	ki_tstate;	/* INT: tcp state */
435 	__uint32_t	ki_tflags;	/* INT: tcp flags */
436 
437 	__uint64_t	ki_rcvq;	/* U_LONG: receive queue len */
438 	__uint64_t	ki_sndq;	/* U_LONG: send queue len */
439 
440 	union {
441 		struct sockaddr	_kis_src; /* STRUCT: local address */
442 		char _kis_pad[256 + 8];		/* pad to max addr length */
443 	} ki_s;
444 	union {
445 		struct sockaddr	_kid_dst; /* STRUCT: remote address */
446 		char _kid_pad[256 + 8];		/* pad to max addr length */
447 	} ki_d;
448 
449 	__uint64_t	ki_inode;	/* INO_T: fake inode number */
450 	__uint64_t	ki_vnode;	/* PTR: if associated with file */
451 	__uint64_t	ki_conn;	/* PTR: control block of peer */
452 	__uint64_t	ki_refs;	/* PTR: referencing socket */
453 	__uint64_t	ki_nextref;	/* PTR: link in refs list */
454 };
455 
456 #define ki_src ki_s._kis_src
457 #define ki_dst ki_d._kid_dst
458 #define ki_spad ki_s._kis_pad
459 #define ki_dpad ki_d._kid_pad
460 
461 #define PCB_SLOP		20
462 #define PCB_ALL			0
463 
464 #endif /* _NETBSD_SOURCE */
465 
466 #if defined(_NETBSD_SOURCE)
467 /*
468  * PF_ROUTE - Routing table
469  *
470  * Three additional levels are defined:
471  *	Fourth: address family, 0 is wildcard
472  *	Fifth: type of info, defined below
473  *	Sixth: flag(s) to mask with for NET_RT_FLAGS
474  */
475 #define NET_RT_DUMP	1		/* dump; may limit to a.f. */
476 #define NET_RT_FLAGS	2		/* by flags, e.g. RESOLVING */
477 #define NET_RT_OOIFLIST	3		/* old NET_RT_IFLIST (pre 1.5) */
478 #define NET_RT_OIFLIST	4		/* survey interface list */
479 #define	NET_RT_IFLIST	5
480 #define	NET_RT_MAXID	6
481 
482 #define CTL_NET_RT_NAMES { \
483 	{ 0, 0 }, \
484 	{ "dump", CTLTYPE_STRUCT }, \
485 	{ "flags", CTLTYPE_STRUCT }, \
486 	{ 0, 0 }, \
487 	{ 0, 0 }, \
488 	{ "iflist", CTLTYPE_STRUCT }, \
489 }
490 #endif /* _NETBSD_SOURCE */
491 
492 /*
493  * Maximum queue length specifiable by listen(2).
494  */
495 #ifndef SOMAXCONN
496 #define	SOMAXCONN	128
497 #endif
498 
499 #include <sys/cdefs.h>
500 
501 /*
502  * Message header for recvmsg and sendmsg calls.
503  * Used value-result for recvmsg, value only for sendmsg.
504  */
505 struct msghdr {
506 	void		*msg_name;	/* optional address */
507 	socklen_t	msg_namelen;	/* size of address */
508 	struct iovec	*msg_iov;	/* scatter/gather array */
509 	int		msg_iovlen;	/* # elements in msg_iov */
510 	void		*msg_control;	/* ancillary data, see below */
511 	socklen_t	msg_controllen;	/* ancillary data buffer len */
512 	int		msg_flags;	/* flags on received message */
513 };
514 
515 #define	MSG_OOB		0x0001		/* process out-of-band data */
516 #define	MSG_PEEK	0x0002		/* peek at incoming message */
517 #define	MSG_DONTROUTE	0x0004		/* send without using routing tables */
518 #define	MSG_EOR		0x0008		/* data completes record */
519 #define	MSG_TRUNC	0x0010		/* data discarded before delivery */
520 #define	MSG_CTRUNC	0x0020		/* control data lost before delivery */
521 #define	MSG_WAITALL	0x0040		/* wait for full request or error */
522 #define	MSG_DONTWAIT	0x0080		/* this message should be nonblocking */
523 #define	MSG_BCAST	0x0100		/* this message was rcvd using link-level brdcst */
524 #define	MSG_MCAST	0x0200		/* this message was rcvd using link-level mcast */
525 #define	MSG_NOSIGNAL	0x0400		/* do not generate SIGPIPE on EOF */
526 #if defined(_NETBSD_SOURCE)
527 #define	MSG_CMSG_CLOEXEC 0x0800		/* close on exec receiving fd */
528 #define	MSG_NBIO	0x1000		/* use non-blocking I/O */
529 #define	MSG_WAITFORONE	0x2000		/* recvmmsg() wait for one message */
530 #define	MSG_NOTIFICATION 0x4000		/* SCTP notification */
531 
532 struct mmsghdr {
533 	struct msghdr msg_hdr;
534 	unsigned int msg_len;
535 };
536 #endif
537 
538 /* Extra flags used internally only */
539 #define	MSG_USERFLAGS	0x0ffffff
540 #define MSG_NAMEMBUF	0x1000000	/* msg_name is an mbuf */
541 #define MSG_CONTROLMBUF	0x2000000	/* msg_control is an mbuf */
542 #define MSG_IOVUSRSPACE	0x4000000	/* msg_iov is in user space */
543 #define MSG_LENUSRSPACE	0x8000000	/* address length is in user space */
544 
545 /*
546  * Header for ancillary data objects in msg_control buffer.
547  * Used for additional information with/about a datagram
548  * not expressible by flags.  The format is a sequence
549  * of message elements headed by cmsghdr structures.
550  */
551 struct cmsghdr {
552 	socklen_t	cmsg_len;	/* data byte count, including hdr */
553 	int		cmsg_level;	/* originating protocol */
554 	int		cmsg_type;	/* protocol-specific type */
555 /* followed by	u_char  cmsg_data[]; */
556 };
557 
558 /*
559  * Alignment requirement for CMSG struct manipulation.
560  * This basically behaves the same as ALIGN() ARCH/include/param.h.
561  * We declare it separately for two reasons:
562  * (1) avoid dependency between machine/param.h, and (2) to sync with kernel's
563  * idea of ALIGNBYTES at runtime.
564  * without (2), we can't guarantee binary compatibility in case of future
565  * changes in ALIGNBYTES.
566  */
567 #define __CMSG_ALIGN(n)	(((n) + __ALIGNBYTES) & ~__ALIGNBYTES)
568 
569 #ifdef _KERNEL
570 #define CMSG_ALIGN(n)	__CMSG_ALIGN(n)
571 #endif
572 
573 #define __CMSG_ASIZE	__CMSG_ALIGN(sizeof(struct cmsghdr))
574 #define __CMSG_MSGNEXT(cmsg) \
575     (__CASTV(char *, cmsg) + __CMSG_ALIGN((cmsg)->cmsg_len))
576 #define __CMSG_MSGEND(mhdr) \
577     (__CASTV(char *, (mhdr)->msg_control) + (mhdr)->msg_controllen)
578 
579 /* given pointer to struct cmsghdr, return pointer to data */
580 #define	CMSG_DATA(cmsg) (__CASTV(u_char *, cmsg) + __CMSG_ASIZE)
581 #define	CCMSG_DATA(cmsg) (__CASTCV(const u_char *, cmsg) + __CMSG_ASIZE)
582 
583 /* given pointer to struct cmsghdr, return pointer to next cmsghdr */
584 #define	CMSG_NXTHDR(mhdr, cmsg)	\
585     __CASTV(struct cmsghdr *,  \
586 	__CMSG_MSGNEXT(cmsg) + __CMSG_ASIZE > __CMSG_MSGEND(mhdr) ? 0 : \
587 	__CMSG_MSGNEXT(cmsg))
588 
589 /*
590  * RFC 2292 requires to check msg_controllen, in case that the kernel returns
591  * an empty list for some reasons.
592  */
593 #define	CMSG_FIRSTHDR(mhdr) \
594     __CASTV(struct cmsghdr *, \
595 	(mhdr)->msg_controllen < sizeof(struct cmsghdr) ? 0 : \
596 	(mhdr)->msg_control)
597 
598 #define CMSG_SPACE(l)	(__CMSG_ASIZE + __CMSG_ALIGN(l))
599 #define CMSG_LEN(l)	(__CMSG_ASIZE + (l))
600 
601 /* "Socket"-level control message types: */
602 #define	SCM_RIGHTS	0x01		/* access rights (array of int) */
603 #if defined(_NETBSD_SOURCE)
604 /*			0x02		   timestamp (struct timeval50) */
605 /*			0x04		   credentials (struct sockcred70) */
606 #define	SCM_TIMESTAMP	0x08		/* timestamp (struct timeval) */
607 #define	SCM_CREDS	0x10		/* credentials (struct sockcred) */
608 #endif
609 
610 /*
611  * Types of socket shutdown(2).
612  */
613 #define	SHUT_RD		0		/* Disallow further receives. */
614 #define	SHUT_WR		1		/* Disallow further sends. */
615 #define	SHUT_RDWR	2		/* Disallow further sends/receives. */
616 
617 #ifdef	_KERNEL
618 static inline socklen_t
sockaddr_getlen(const struct sockaddr * sa)619 sockaddr_getlen(const struct sockaddr *sa)
620 {
621 	return sa->sa_len;
622 }
623 
624 __BEGIN_DECLS
625 struct sockaddr *sockaddr_copy(struct sockaddr *, socklen_t,
626     const struct sockaddr *);
627 struct sockaddr *sockaddr_externalize(struct sockaddr *, socklen_t,
628     const struct sockaddr *);
629 struct sockaddr *sockaddr_alloc(sa_family_t, socklen_t, int);
630 const void *sockaddr_const_addr(const struct sockaddr *, socklen_t *);
631 void *sockaddr_addr(struct sockaddr *, socklen_t *);
632 const struct sockaddr *sockaddr_any(const struct sockaddr *);
633 const struct sockaddr *sockaddr_any_by_family(int);
634 const void *sockaddr_anyaddr(const struct sockaddr *, socklen_t *);
635 int sockaddr_cmp(const struct sockaddr *, const struct sockaddr *);
636 struct sockaddr *sockaddr_dup(const struct sockaddr *, int);
637 int sockaddr_format(const struct sockaddr *, char *, size_t);
638 void sockaddr_free(struct sockaddr *);
639 __END_DECLS
640 #endif /* _KERNEL */
641 
642 #ifndef	_KERNEL
643 
644 __BEGIN_DECLS
645 int	accept(int, struct sockaddr * __restrict, socklen_t * __restrict);
646 int	bind(int, const struct sockaddr *, socklen_t);
647 int	connect(int, const struct sockaddr *, socklen_t);
648 int	getpeername(int, struct sockaddr * __restrict, socklen_t * __restrict);
649 int	getsockname(int, struct sockaddr * __restrict, socklen_t * __restrict);
650 int	getsockopt(int, int, int, void *__restrict, socklen_t * __restrict);
651 int	listen(int, int);
652 int	paccept(int, struct sockaddr * __restrict, socklen_t * __restrict,
653 	const sigset_t * __restrict, int);
654 ssize_t	recv(int, void *, size_t, int);
655 ssize_t	recvfrom(int, void *__restrict, size_t, int,
656 	    struct sockaddr * __restrict, socklen_t * __restrict);
657 ssize_t	recvmsg(int, struct msghdr *, int);
658 ssize_t	send(int, const void *, size_t, int);
659 ssize_t	sendto(int, const void *,
660 	    size_t, int, const struct sockaddr *, socklen_t);
661 ssize_t	sendmsg(int, const struct msghdr *, int);
662 int	setsockopt(int, int, int, const void *, socklen_t);
663 int	shutdown(int, int);
664 int	sockatmark(int);
665 int	socket(int, int, int)
666 #if !defined(__LIBC12_SOURCE__) && !defined(_STANDALONE)
667 __RENAME(__socket30)
668 #endif
669 			     ;
670 int	socketpair(int, int, int, int *);
671 
672 #if defined(_NETBSD_SOURCE)
673 int	sendmmsg(int, struct mmsghdr *, unsigned int, unsigned int);
674 struct timespec;
675 int	recvmmsg(int, struct mmsghdr *, unsigned int, unsigned int,
676     struct timespec *);
677 #endif
678 __END_DECLS
679 #endif /* !_KERNEL */
680 
681 #endif /* !_SYS_SOCKET_H_ */
682