xref: /illumos-gate/usr/src/uts/common/sys/socket.h (revision 22f5594a529d50114d839d4ddecc2c499731a3d7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved	*/
28 
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 #ifndef	_SYS_SOCKET_H
40 #define	_SYS_SOCKET_H
41 
42 #pragma ident	"%Z%%M%	%I%	%E% SMI"
43 
44 #include <sys/types.h>
45 #include <sys/uio.h>
46 #include <sys/feature_tests.h>
47 #include <sys/socket_impl.h>
48 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
49 #ifndef	_KERNEL
50 #include <sys/netconfig.h>
51 #endif	/* !_KERNEL */
52 #include <netinet/in.h>
53 #endif	/* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
54 
55 #ifdef	__cplusplus
56 extern "C" {
57 #endif
58 
59 #ifndef	_SOCKLEN_T
60 #define	_SOCKLEN_T
61 
62 /*
63  * The socklen definitions are reproduced in netinet/in.h for the inet6_
64  * functions.  Exposing all of sys/socket.h via netinet/in.h breaks existing
65  * applications and is not required by austin.
66  */
67 #if defined(_XPG4_2) && !defined(_XPG5) && !defined(_LP64)
68 typedef	size_t		socklen_t;
69 #else
70 typedef	uint32_t	socklen_t;
71 #endif	/* defined(_XPG4_2) && !defined(_XPG5) && !defined(_LP64) */
72 
73 #if defined(_XPG4_2) || defined(_BOOT)
74 typedef	socklen_t	*_RESTRICT_KYWD Psocklen_t;
75 #else
76 typedef	void		*_RESTRICT_KYWD Psocklen_t;
77 #endif	/* defined(_XPG4_2) || defined(_BOOT) */
78 
79 #endif	/* _SOCKLEN_T */
80 
81 /*
82  * Definitions related to sockets: types, address families, options.
83  */
84 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
85 #ifndef	NC_TPI_CLTS
86 #define	NC_TPI_CLTS	1		/* must agree with netconfig.h */
87 #define	NC_TPI_COTS	2		/* must agree with netconfig.h */
88 #define	NC_TPI_COTS_ORD	3		/* must agree with netconfig.h */
89 #define	NC_TPI_RAW	4		/* must agree with netconfig.h */
90 #endif	/* !NC_TPI_CLTS */
91 #endif	/* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
92 
93 /*
94  * Types
95  */
96 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
97 #define	SOCK_STREAM	NC_TPI_COTS	/* stream socket */
98 #define	SOCK_DGRAM	NC_TPI_CLTS	/* datagram socket */
99 #define	SOCK_RAW	NC_TPI_RAW	/* raw-protocol interface */
100 #else
101 #define	SOCK_STREAM	2		/* stream socket */
102 #define	SOCK_DGRAM	1		/* datagram socket */
103 #define	SOCK_RAW	4		/* raw-protocol interface */
104 #endif	/* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
105 #define	SOCK_RDM	5		/* reliably-delivered message */
106 #define	SOCK_SEQPACKET	6		/* sequenced packet stream */
107 
108 /*
109  * Option flags per-socket.
110  */
111 #define	SO_DEBUG	0x0001		/* turn on debugging info recording */
112 #define	SO_ACCEPTCONN	0x0002		/* socket has had listen() */
113 #define	SO_REUSEADDR	0x0004		/* allow local address reuse */
114 #define	SO_KEEPALIVE	0x0008		/* keep connections alive */
115 #define	SO_DONTROUTE	0x0010		/* just use interface addresses */
116 #define	SO_BROADCAST	0x0020		/* permit sending of broadcast msgs */
117 #define	SO_USELOOPBACK	0x0040		/* bypass hardware when possible */
118 #define	SO_LINGER	0x0080		/* linger on close if data present */
119 #define	SO_OOBINLINE	0x0100		/* leave received OOB data in line */
120 #define	SO_DGRAM_ERRIND	0x0200		/* Application wants delayed error */
121 #define	SO_RECVUCRED	0x0400		/* Application wants ucred of sender */
122 
123 #ifdef _KERNEL
124 #define	SO_SND_COPYAVOID 0x0800		/* Internal: use zero-copy */
125 #endif /* _KERNEL */
126 
127 /*
128  * N.B.: The following definition is present only for compatibility
129  * with release 3.0.  It will disappear in later releases.
130  */
131 #define	SO_DONTLINGER	(~SO_LINGER)	/* ~SO_LINGER */
132 
133 /*
134  * Additional options, not kept in so_options.
135  */
136 #define	SO_SNDBUF	0x1001		/* send buffer size */
137 #define	SO_RCVBUF	0x1002		/* receive buffer size */
138 #define	SO_SNDLOWAT	0x1003		/* send low-water mark */
139 #define	SO_RCVLOWAT	0x1004		/* receive low-water mark */
140 #define	SO_SNDTIMEO	0x1005		/* send timeout */
141 #define	SO_RCVTIMEO	0x1006		/* receive timeout */
142 #define	SO_ERROR	0x1007		/* get error status and clear */
143 #define	SO_TYPE		0x1008		/* get socket type */
144 #define	SO_PROTOTYPE	0x1009		/* get/set protocol type */
145 #define	SO_ANON_MLP	0x100a		/* create MLP on anonymous bind */
146 #define	SO_MAC_EXEMPT	0x100b		/* allow dominated unlabeled peers */
147 #define	SO_DOMAIN	0x100c		/* get socket domain */
148 
149 /* "Socket"-level control message types: */
150 #define	SCM_RIGHTS	0x1010		/* access rights (array of int) */
151 
152 #define	SO_SECATTR	0x1011		/* socket's security attributes */
153 #define	SCM_UCRED	0x1012		/* sender's ucred */
154 #define	SO_TIMESTAMP	0x1013		/* socket-level timestamp option */
155 #define	SCM_TIMESTAMP	SO_TIMESTAMP	/* socket control message timestamp */
156 
157 #define	SO_ALLZONES	0x1014		/* bind in all zones */
158 #define	SO_EXCLBIND	0x1015		/* exclusive binding */
159 
160 #ifdef	_KERNEL
161 #define	SO_SRCADDR	0x2001		/* Internal: AF_UNIX source address */
162 #define	SO_FILEP	0x2002		/* Internal: AF_UNIX file pointer */
163 #define	SO_UNIX_CLOSE	0x2003		/* Internal: AF_UNIX peer closed */
164 #endif	/* _KERNEL */
165 
166 #ifdef	_KERNEL
167 /*
168  * new socket open flags to identify socket and acceptor streams
169  */
170 #define	SO_ACCEPTOR	0x20000		/* acceptor socket */
171 #define	SO_SOCKSTR	0x40000		/* normal socket stream */
172 #endif	/* _KERNEL */
173 
174 /*
175  * Structure used for manipulating linger option.
176  */
177 struct	linger {
178 	int	l_onoff;		/* option on/off */
179 	int	l_linger;		/* linger time */
180 };
181 
182 /*
183  * Level number for (get/set)sockopt() to apply to socket itself.
184  */
185 #define	SOL_SOCKET	0xffff		/* options for socket level */
186 
187 /*
188  * Address families.
189  *
190  * Some of these constant names are copied for the DTrace IP provider in
191  * usr/src/lib/libdtrace/common/{ip.d.in, ip.sed.in}, which should be kept
192  * in sync.
193  */
194 #define	AF_UNSPEC	0		/* unspecified */
195 #define	AF_UNIX		1		/* local to host (pipes, portals) */
196 #define	AF_INET		2		/* internetwork: UDP, TCP, etc. */
197 #define	AF_IMPLINK	3		/* arpanet imp addresses */
198 #define	AF_PUP		4		/* pup protocols: e.g. BSP */
199 #define	AF_CHAOS	5		/* mit CHAOS protocols */
200 #define	AF_NS		6		/* XEROX NS protocols */
201 #define	AF_NBS		7		/* nbs protocols */
202 #define	AF_ECMA		8		/* european computer manufacturers */
203 #define	AF_DATAKIT	9		/* datakit protocols */
204 #define	AF_CCITT	10		/* CCITT protocols, X.25 etc */
205 #define	AF_SNA		11		/* IBM SNA */
206 #define	AF_DECnet	12		/* DECnet */
207 #define	AF_DLI		13		/* Direct data link interface */
208 #define	AF_LAT		14		/* LAT */
209 #define	AF_HYLINK	15		/* NSC Hyperchannel */
210 #define	AF_APPLETALK	16		/* Apple Talk */
211 #define	AF_NIT		17		/* Network Interface Tap */
212 #define	AF_802		18		/* IEEE 802.2, also ISO 8802 */
213 #define	AF_OSI		19		/* umbrella for all families used */
214 #define	AF_X25		20		/* CCITT X.25 in particular */
215 #define	AF_OSINET	21		/* AFI = 47, IDI = 4 */
216 #define	AF_GOSIP	22		/* U.S. Government OSI */
217 #define	AF_IPX		23		/* Novell Internet Protocol */
218 #define	AF_ROUTE	24		/* Internal Routing Protocol */
219 #define	AF_LINK		25		/* Link-layer interface */
220 #define	AF_INET6	26		/* Internet Protocol, Version 6 */
221 #define	AF_KEY		27		/* Security Association DB socket */
222 #define	AF_NCA		28		/* NCA socket */
223 #define	AF_POLICY	29		/* Security Policy DB socket */
224 #define	AF_INET_OFFLOAD	30		/* Sun private; do not use */
225 
226 #define	AF_MAX		30
227 
228 /*
229  * Protocol families, same as address families for now.
230  */
231 #define	PF_UNSPEC	AF_UNSPEC
232 #define	PF_UNIX		AF_UNIX
233 #define	PF_INET		AF_INET
234 #define	PF_IMPLINK	AF_IMPLINK
235 #define	PF_PUP		AF_PUP
236 #define	PF_CHAOS	AF_CHAOS
237 #define	PF_NS		AF_NS
238 #define	PF_NBS		AF_NBS
239 #define	PF_ECMA		AF_ECMA
240 #define	PF_DATAKIT	AF_DATAKIT
241 #define	PF_CCITT	AF_CCITT
242 #define	PF_SNA		AF_SNA
243 #define	PF_DECnet	AF_DECnet
244 #define	PF_DLI		AF_DLI
245 #define	PF_LAT		AF_LAT
246 #define	PF_HYLINK	AF_HYLINK
247 #define	PF_APPLETALK	AF_APPLETALK
248 #define	PF_NIT		AF_NIT
249 #define	PF_802		AF_802
250 #define	PF_OSI		AF_OSI
251 #define	PF_X25		AF_X25
252 #define	PF_OSINET	AF_OSINET
253 #define	PF_GOSIP	AF_GOSIP
254 #define	PF_IPX		AF_IPX
255 #define	PF_ROUTE	AF_ROUTE
256 #define	PF_LINK		AF_LINK
257 #define	PF_INET6	AF_INET6
258 #define	PF_KEY		AF_KEY
259 #define	PF_NCA		AF_NCA
260 #define	PF_POLICY	AF_POLICY
261 #define	PF_INET_OFFLOAD	AF_INET_OFFLOAD	/* Sun private; do not use */
262 
263 #define	PF_MAX		AF_MAX
264 
265 /*
266  * Maximum queue length specifiable by listen.
267  */
268 #define	SOMAXCONN	128
269 
270 /*
271  * Message header for recvmsg and sendmsg calls.
272  */
273 struct msghdr {
274 	void		*msg_name;		/* optional address */
275 	socklen_t	msg_namelen;		/* size of address */
276 	struct iovec	*msg_iov;		/* scatter/gather array */
277 	int		msg_iovlen;		/* # elements in msg_iov */
278 
279 #if defined(_XPG4_2) || defined(_KERNEL)
280 	void		*msg_control;		/* ancillary data */
281 	socklen_t	msg_controllen;		/* ancillary data buffer len */
282 	int		msg_flags;		/* flags on received message */
283 #else
284 	caddr_t		msg_accrights;	/* access rights sent/received */
285 	int		msg_accrightslen;
286 #endif	/* defined(_XPG4_2) || defined(_KERNEL) */
287 };
288 
289 #if	defined(_KERNEL)
290 
291 /*
292  *	N.B.:  we assume that omsghdr and nmsghdr are isomorphic, with
293  *	the sole exception that nmsghdr has the additional msg_flags
294  *	field at the end.
295  */
296 struct omsghdr {
297 	void		*msg_name;	/* optional address */
298 	socklen_t	msg_namelen;	/* size of address */
299 	struct	iovec	*msg_iov;	/* scatter/gather array */
300 	int		msg_iovlen;	/* # elements in msg_iov */
301 	caddr_t		msg_accrights;	/* access rights sent/received */
302 	int		msg_accrightslen;
303 };
304 
305 #define	nmsghdr		msghdr
306 
307 #if defined(_SYSCALL32)
308 
309 struct omsghdr32 {
310 	caddr32_t	msg_name;	/* optional address */
311 	uint32_t	msg_namelen;	/* size of address */
312 	caddr32_t	msg_iov;	/* scatter/gather array */
313 	int32_t		msg_iovlen;	/* # elements in msg_iov */
314 	caddr32_t	msg_accrights;	/* access rights sent/received */
315 	uint32_t	msg_accrightslen;
316 };
317 
318 struct msghdr32 {
319 	caddr32_t	msg_name;	/* optional address */
320 	uint32_t	msg_namelen;	/* size of address */
321 	caddr32_t	msg_iov;	/* scatter/gather array */
322 	int32_t		msg_iovlen;	/* # elements in msg_iov */
323 	caddr32_t	msg_control;	/* ancillary data */
324 	uint32_t	msg_controllen;	/* ancillary data buffer len */
325 	int32_t		msg_flags;	/* flags on received message */
326 };
327 
328 #define	nmsghdr32	msghdr32
329 
330 #endif	/* _SYSCALL32 */
331 #endif	/* _KERNEL */
332 
333 #define	MSG_OOB		0x1		/* process out-of-band data */
334 #define	MSG_PEEK	0x2		/* peek at incoming message */
335 #define	MSG_DONTROUTE	0x4		/* send without using routing tables */
336 /* Added for XPGv2 compliance */
337 #define	MSG_EOR		0x8		/* Terminates a record */
338 #define	MSG_CTRUNC	0x10		/* Control data truncated */
339 #define	MSG_TRUNC	0x20		/* Normal data truncated */
340 #define	MSG_WAITALL	0x40		/* Wait for complete recv or error */
341 /* End of XPGv2 compliance */
342 #define	MSG_DONTWAIT	0x80		/* Don't block for this recv */
343 #define	MSG_NOTIFICATION 0x100		/* Notification, not data */
344 #define	MSG_XPG4_2	0x8000		/* Private: XPG4.2 flag */
345 
346 #define	MSG_MAXIOVLEN	16
347 
348 /* Added for XPGv2 compliance */
349 #define	SHUT_RD		0
350 #define	SHUT_WR		1
351 #define	SHUT_RDWR	2
352 
353 struct cmsghdr {
354 	socklen_t	cmsg_len;	/* data byte count, including hdr */
355 	int		cmsg_level;	/* originating protocol */
356 	int		cmsg_type;	/* protocol-specific type */
357 };
358 
359 #if defined(_XPG4_2) || defined(_KERNEL)
360 #if defined(__sparc)
361 /* To maintain backward compatibility, alignment needs to be 8 on sparc. */
362 #define	_CMSG_HDR_ALIGNMENT	8
363 #else
364 /* for __i386 (and other future architectures) */
365 #define	_CMSG_HDR_ALIGNMENT	4
366 #endif	/* defined(__sparc) */
367 #endif	/* defined(_XPG4_2) || defined(_KERNEL) */
368 
369 #if defined(_XPG4_2)
370 /*
371  * The cmsg headers (and macros dealing with them) were made available as
372  * part of UNIX95 and hence need to be protected with a _XPG4_2 define.
373  */
374 #define	_CMSG_DATA_ALIGNMENT	(sizeof (int))
375 #define	_CMSG_HDR_ALIGN(x)	(((uintptr_t)(x) + _CMSG_HDR_ALIGNMENT - 1) & \
376 				    ~(_CMSG_HDR_ALIGNMENT - 1))
377 #define	_CMSG_DATA_ALIGN(x)	(((uintptr_t)(x) + _CMSG_DATA_ALIGNMENT - 1) & \
378 				    ~(_CMSG_DATA_ALIGNMENT - 1))
379 #define	CMSG_DATA(c)							\
380 	((unsigned char *)_CMSG_DATA_ALIGN((struct cmsghdr *)(c) + 1))
381 
382 #define	CMSG_FIRSTHDR(m)						\
383 	(((m)->msg_controllen < sizeof (struct cmsghdr)) ?		\
384 	    (struct cmsghdr *)0 : (struct cmsghdr *)((m)->msg_control))
385 
386 #define	CMSG_NXTHDR(m, c)						\
387 	(((c) == 0) ? CMSG_FIRSTHDR(m) :			\
388 	((((uintptr_t)_CMSG_HDR_ALIGN((char *)(c) +			\
389 	((struct cmsghdr *)(c))->cmsg_len) + sizeof (struct cmsghdr)) >	\
390 	(((uintptr_t)((struct msghdr *)(m))->msg_control) +		\
391 	((uintptr_t)((struct msghdr *)(m))->msg_controllen))) ?		\
392 	((struct cmsghdr *)0) :						\
393 	((struct cmsghdr *)_CMSG_HDR_ALIGN((char *)(c) +		\
394 	    ((struct cmsghdr *)(c))->cmsg_len))))
395 
396 /* Amount of space + padding needed for a message of length l */
397 #define	CMSG_SPACE(l)							\
398 	((unsigned int)_CMSG_HDR_ALIGN(sizeof (struct cmsghdr) + (l)))
399 
400 /* Value to be used in cmsg_len, does not include trailing padding */
401 #define	CMSG_LEN(l)							\
402 	((unsigned int)_CMSG_DATA_ALIGN(sizeof (struct cmsghdr)) + (l))
403 
404 #endif	/* _XPG4_2 */
405 
406 #ifdef	_XPG4_2
407 #ifdef	__PRAGMA_REDEFINE_EXTNAME
408 #pragma redefine_extname bind __xnet_bind
409 #pragma redefine_extname connect __xnet_connect
410 #pragma redefine_extname recvmsg __xnet_recvmsg
411 #pragma redefine_extname sendmsg __xnet_sendmsg
412 #pragma redefine_extname sendto __xnet_sendto
413 #pragma redefine_extname socket __xnet_socket
414 #pragma redefine_extname socketpair __xnet_socketpair
415 #pragma redefine_extname getsockopt __xnet_getsockopt
416 #else	/* __PRAGMA_REDEFINE_EXTNAME */
417 #define	bind	__xnet_bind
418 #define	connect	__xnet_connect
419 #define	recvmsg	__xnet_recvmsg
420 #define	sendmsg	__xnet_sendmsg
421 #define	sendto	__xnet_sendto
422 #define	socket	__xnet_socket
423 #define	socketpair	__xnet_socketpair
424 #define	getsockopt	__xnet_getsockopt
425 #endif	/* __PRAGMA_REDEFINE_EXTNAME */
426 
427 #endif	/* _XPG4_2 */
428 
429 #if defined(_XPG4_2) && !defined(_XPG5)
430 #ifdef	__PRAGMA_REDEFINE_EXTNAME
431 #pragma redefine_extname listen __xnet_listen
432 #else	/* __PRAGMA_REDEFINE_EXTNAME */
433 #define	listen	__xnet_listen
434 #endif	/* __PRAGMA_REDEFINE_EXTNAME */
435 #endif /* (_XPG4_2) && !defined(_XPG5) */
436 
437 #if !defined(_KERNEL) || defined(_BOOT)
438 #ifdef	__STDC__
439 extern int accept(int, struct sockaddr *_RESTRICT_KYWD, Psocklen_t);
440 extern int bind(int, const struct sockaddr *, socklen_t);
441 extern int connect(int, const struct sockaddr *, socklen_t);
442 extern int getpeername(int, struct sockaddr *_RESTRICT_KYWD, Psocklen_t);
443 extern int getsockname(int, struct sockaddr *_RESTRICT_KYWD, Psocklen_t);
444 extern int getsockopt(int, int, int, void *_RESTRICT_KYWD, Psocklen_t);
445 extern int listen(int, int);	/* XXX - fixme???  where do I go */
446 extern int socketpair(int, int, int, int *);
447 extern ssize_t recv(int, void *, size_t, int);
448 extern ssize_t recvfrom(int, void *_RESTRICT_KYWD, size_t, int,
449 	struct sockaddr *_RESTRICT_KYWD, Psocklen_t);
450 extern ssize_t recvmsg(int, struct msghdr *, int);
451 extern ssize_t send(int, const void *, size_t, int);
452 extern ssize_t sendmsg(int, const struct msghdr *, int);
453 extern ssize_t sendto(int, const void *, size_t, int, const struct sockaddr *,
454 	socklen_t);
455 extern int setsockopt(int, int, int, const void *, socklen_t);
456 extern int shutdown(int, int);
457 extern int socket(int, int, int);
458 
459 #if !defined(_XPG4_2) || defined(_XPG6) || defined(__EXTENSIONS__)
460 extern int sockatmark(int);
461 #endif /* !defined(_XPG4_2) || defined(_XPG6) || defined(__EXTENSIONS__) */
462 #else	/* __STDC__ */
463 extern int accept();
464 extern int bind();
465 extern int connect();
466 extern int getpeername();
467 extern int getsockname();
468 extern int getsockopt();
469 extern int listen();
470 extern int recv();
471 extern int recvfrom();
472 extern int send();
473 extern int sendto();
474 extern int setsockopt();
475 extern int sockatmark();
476 extern int socket();
477 extern int recvmsg();
478 extern int sendmsg();
479 extern int shutdown();
480 extern int socketpair();
481 #endif	/* __STDC__ */
482 #endif	/* !defined(_KERNEL) || defined(_BOOT) */
483 
484 #ifdef	__cplusplus
485 }
486 #endif
487 
488 #endif	/* _SYS_SOCKET_H */
489