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