1 /**	$MirOS: src/sys/sys/socket.h,v 1.3 2005/07/07 14:39:27 tg Exp $ */
2 /*	$OpenBSD: socket.h,v 1.52 2005/05/27 04:55:28 mcbride Exp $	*/
3 /*	$NetBSD: socket.h,v 1.14 1996/02/09 18:25:36 christos Exp $	*/
4 
5 /*
6  * Copyright (c) 1982, 1985, 1986, 1988, 1993, 1994
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)socket.h	8.4 (Berkeley) 2/21/94
34  */
35 
36 #ifndef _SYS_SOCKET_H_
37 #define	_SYS_SOCKET_H_
38 
39 /*
40  * needed for ALIGNBYTES
41  */
42 #include <machine/param.h>
43 
44 /*
45  * Definitions related to sockets: types, address families, options.
46  */
47 
48 /*
49  * Types
50  */
51 #define	SOCK_STREAM	1		/* stream socket */
52 #define	SOCK_DGRAM	2		/* datagram socket */
53 #define	SOCK_RAW	3		/* raw-protocol interface */
54 #define	SOCK_RDM	4		/* reliably-delivered message */
55 #define	SOCK_SEQPACKET	5		/* sequenced packet stream */
56 
57 /*
58  * Option flags per-socket.
59  */
60 #define	SO_DEBUG	0x0001		/* turn on debugging info recording */
61 #define	SO_ACCEPTCONN	0x0002		/* socket has had listen() */
62 #define	SO_REUSEADDR	0x0004		/* allow local address reuse */
63 #define	SO_KEEPALIVE	0x0008		/* keep connections alive */
64 #define	SO_DONTROUTE	0x0010		/* just use interface addresses */
65 #define	SO_BROADCAST	0x0020		/* permit sending of broadcast msgs */
66 #define	SO_USELOOPBACK	0x0040		/* bypass hardware when possible */
67 #define	SO_LINGER	0x0080		/* linger on close if data present */
68 #define	SO_OOBINLINE	0x0100		/* leave received OOB data in line */
69 #define	SO_REUSEPORT	0x0200		/* allow local address & port reuse */
70 #define SO_JUMBO	0x0400		/* try to use jumbograms */
71 
72 /*
73  * Additional options, not kept in so_options.
74  */
75 #define	SO_SNDBUF	0x1001		/* send buffer size */
76 #define	SO_RCVBUF	0x1002		/* receive buffer size */
77 #define	SO_SNDLOWAT	0x1003		/* send low-water mark */
78 #define	SO_RCVLOWAT	0x1004		/* receive low-water mark */
79 #define	SO_SNDTIMEO	0x1005		/* send timeout */
80 #define	SO_RCVTIMEO	0x1006		/* receive timeout */
81 #define	SO_ERROR	0x1007		/* get error status and clear */
82 #define	SO_TYPE		0x1008		/* get socket type */
83 #define	SO_NETPROC	0x1020		/* multiplex; network processing */
84 
85 /*
86  * Structure used for manipulating linger option.
87  */
88 struct	linger {
89 	int	l_onoff;		/* option on/off */
90 	int	l_linger;		/* linger time */
91 };
92 
93 /*
94  * Level number for (get/set)sockopt() to apply to socket itself.
95  */
96 #define	SOL_SOCKET	0xffff		/* options for socket level */
97 
98 /*
99  * Address families.
100  */
101 #define	AF_UNSPEC	0		/* unspecified */
102 #define	AF_LOCAL	1		/* local to host (pipes, portals) */
103 #define	AF_UNIX		AF_LOCAL	/* backward compatibility */
104 #define	AF_INET		2		/* internetwork: UDP, TCP, etc. */
105 #define	AF_IMPLINK	3		/* arpanet imp addresses */
106 #define	AF_PUP		4		/* pup protocols: e.g. BSP */
107 #define	AF_CHAOS	5		/* mit CHAOS protocols */
108 #define	AF_NS		6		/* XEROX NS protocols */
109 #define	AF_ISO		7		/* ISO protocols */
110 #define	AF_OSI		AF_ISO
111 #define	AF_ECMA		8		/* european computer manufacturers */
112 #define	AF_DATAKIT	9		/* datakit protocols */
113 #define	AF_CCITT	10		/* CCITT protocols, X.25 etc */
114 #define	AF_SNA		11		/* IBM SNA */
115 #define AF_DECnet	12		/* DECnet */
116 #define AF_DLI		13		/* DEC Direct data link interface */
117 #define AF_LAT		14		/* LAT */
118 #define	AF_HYLINK	15		/* NSC Hyperchannel */
119 #define	AF_APPLETALK	16		/* Apple Talk */
120 #define	AF_ROUTE	17		/* Internal Routing Protocol */
121 #define	AF_LINK		18		/* Link layer interface */
122 #define	pseudo_AF_XTP	19		/* eXpress Transfer Protocol (no AF) */
123 #define	AF_COIP		20		/* connection-oriented IP, aka ST II */
124 #define	AF_CNT		21		/* Computer Network Technology */
125 #define pseudo_AF_RTIP	22		/* Help Identify RTIP packets */
126 #define	AF_IPX		23		/* Novell Internet Protocol */
127 #define	AF_INET6	24		/* IPv6 */
128 #define pseudo_AF_PIP	25		/* Help Identify PIP packets */
129 #define AF_ISDN		26		/* Integrated Services Digital Network*/
130 #define AF_E164		AF_ISDN		/* CCITT E.164 recommendation */
131 #define AF_NATM		27		/* native ATM access */
132 #define	AF_ENCAP	28
133 #define	AF_SIP		29		/* Simple Internet Protocol */
134 #define AF_KEY		30
135 #define pseudo_AF_HDRCMPLT 31		/* Used by BPF to not rewrite headers
136 					   in interface output routine */
137 #define	AF_BLUETOOTH	32		/* Bluetooth */
138 
139 #define	AF_MAX		33
140 
141 /*
142  * Structure used by kernel to store most
143  * addresses.
144  */
145 struct sockaddr {
146 	u_int8_t    sa_len;		/* total length */
147 	sa_family_t sa_family;		/* address family */
148 	char	    sa_data[14];	/* actually longer; address value */
149 };
150 
151 /*
152  * Sockaddr type which can hold any sockaddr type available
153  * in the system.
154  *
155  * Note: __ss_{len,family} is defined in RFC2553.  During RFC2553 discussion
156  * the field name went back and forth between ss_len and __ss_len,
157  * and RFC2553 specifies it to be __ss_len.  openbsd picked ss_len.
158  * For maximum portability, userland programmer would need to
159  * (1) make the code never touch ss_len portion (cast it into sockaddr and
160  * touch sa_len), or (2) add "-Dss_len=__ss_len" into CFLAGS to unify all
161  * occurences (including header file) to __ss_len.
162  */
163 struct sockaddr_storage {
164 	u_int8_t	ss_len;		/* total length */
165 	sa_family_t	ss_family;	/* address family */
166 	unsigned char	__ss_pad1[6];	/* align to quad */
167 	u_int64_t	__ss_pad2;	/* force alignment for stupid compilers */
168 	unsigned char	__ss_pad3[240];	/* pad to a total of 256 bytes */
169 };
170 
171 /*
172  * Structure used by kernel to pass protocol
173  * information in raw sockets.
174  */
175 struct sockproto {
176 	unsigned short	sp_family;	/* address family */
177 	unsigned short	sp_protocol;	/* protocol */
178 };
179 
180 /*
181  * Protocol families, same as address families for now.
182  */
183 #define	PF_UNSPEC	AF_UNSPEC
184 #define	PF_LOCAL	AF_LOCAL
185 #define	PF_UNIX		PF_LOCAL	/* backward compatibility */
186 #define	PF_INET		AF_INET
187 #define	PF_IMPLINK	AF_IMPLINK
188 #define	PF_PUP		AF_PUP
189 #define	PF_CHAOS	AF_CHAOS
190 #define	PF_NS		AF_NS
191 #define	PF_ISO		AF_ISO
192 #define	PF_OSI		AF_ISO
193 #define	PF_ECMA		AF_ECMA
194 #define	PF_DATAKIT	AF_DATAKIT
195 #define	PF_CCITT	AF_CCITT
196 #define	PF_SNA		AF_SNA
197 #define PF_DECnet	AF_DECnet
198 #define PF_DLI		AF_DLI
199 #define PF_LAT		AF_LAT
200 #define	PF_HYLINK	AF_HYLINK
201 #define	PF_APPLETALK	AF_APPLETALK
202 #define	PF_ROUTE	AF_ROUTE
203 #define	PF_LINK		AF_LINK
204 #define	PF_XTP		pseudo_AF_XTP	/* really just proto family, no AF */
205 #define	PF_COIP		AF_COIP
206 #define	PF_CNT		AF_CNT
207 #define	PF_IPX		AF_IPX		/* same format as AF_NS */
208 #define PF_INET6	AF_INET6
209 #define PF_RTIP		pseudo_AF_RTIP	/* same format as AF_INET */
210 #define PF_PIP		pseudo_AF_PIP
211 #define PF_ISDN		AF_ISDN
212 #define PF_NATM		AF_NATM
213 #define PF_ENCAP	AF_ENCAP
214 #define	PF_SIP		AF_SIP
215 #define PF_KEY		AF_KEY
216 #define PF_BPF		pseudo_AF_HDRCMPLT
217 #define	PF_BLUETOOTH	AF_BLUETOOTH
218 #define	PF_MAX		AF_MAX
219 
220 /*
221  * These are the valid values for the "how" field used by shutdown(2).
222  */
223 #define	SHUT_RD		0
224 #define	SHUT_WR		1
225 #define	SHUT_RDWR	2
226 
227 /*
228  * Socket credentials.
229  */
230 struct sockcred {
231 	uid_t	sc_uid;			/* real user id */
232 	uid_t	sc_euid;		/* effective user id */
233 	gid_t	sc_gid;			/* real group id */
234 	gid_t	sc_egid;		/* effective group id */
235 	int	sc_ngroups;		/* number of supplemental groups */
236 	gid_t	sc_groups[1];		/* variable length */
237 };
238 
239 /*
240  * Compute size of a sockcred structure with groups.
241  */
242 #define SOCKCREDSIZE(ngrps) \
243 	(sizeof(struct sockcred) + (sizeof(gid_t) * ((ngrps) - 1)))
244 
245 /*
246  * Definitions for network related sysctl, CTL_NET.
247  *
248  * Second level is protocol family.
249  * Third level is protocol number.
250  *
251  * Further levels are defined by the individual families below.
252  */
253 #define NET_MAXID	AF_MAX
254 
255 #define CTL_NET_NAMES { \
256 	{ 0, 0 }, \
257 	{ "unix", CTLTYPE_NODE }, \
258 	{ "inet", CTLTYPE_NODE }, \
259 	{ "implink", CTLTYPE_NODE }, \
260 	{ "pup", CTLTYPE_NODE }, \
261 	{ "chaos", CTLTYPE_NODE }, \
262 	{ "xerox_ns", CTLTYPE_NODE }, \
263 	{ "iso", CTLTYPE_NODE }, \
264 	{ "emca", CTLTYPE_NODE }, \
265 	{ "datakit", CTLTYPE_NODE }, \
266 	{ "ccitt", CTLTYPE_NODE }, \
267 	{ "ibm_sna", CTLTYPE_NODE }, \
268 	{ "decnet", CTLTYPE_NODE }, \
269 	{ "dec_dli", CTLTYPE_NODE }, \
270 	{ "lat", CTLTYPE_NODE }, \
271 	{ "hylink", CTLTYPE_NODE }, \
272 	{ "appletalk", CTLTYPE_NODE }, \
273 	{ "route", CTLTYPE_NODE }, \
274 	{ "link_layer", CTLTYPE_NODE }, \
275 	{ "xtp", CTLTYPE_NODE }, \
276 	{ "coip", CTLTYPE_NODE }, \
277 	{ "cnt", CTLTYPE_NODE }, \
278 	{ "rtip", CTLTYPE_NODE }, \
279 	{ "ipx", CTLTYPE_NODE }, \
280 	{ "inet6", CTLTYPE_NODE }, \
281 	{ "pip", CTLTYPE_NODE }, \
282 	{ "isdn", CTLTYPE_NODE }, \
283 	{ "natm", CTLTYPE_NODE }, \
284 	{ "encap", CTLTYPE_NODE }, \
285 	{ "sip", CTLTYPE_NODE }, \
286 	{ "key", CTLTYPE_NODE }, \
287 	{ "bpf", CTLTYPE_NODE }, \
288 }
289 
290 /*
291  * PF_ROUTE - Routing table
292  *
293  * Three additional levels are defined:
294  *	Fourth: address family, 0 is wildcard
295  *	Fifth: type of info, defined below
296  *	Sixth: flag(s) to mask with for NET_RT_FLAGS
297  */
298 #define NET_RT_DUMP	1		/* dump; may limit to a.f. */
299 #define NET_RT_FLAGS	2		/* by flags, e.g. RESOLVING */
300 #define NET_RT_IFLIST	3		/* survey interface list */
301 #define	NET_RT_MAXID	4
302 
303 #define CTL_NET_RT_NAMES { \
304 	{ 0, 0 }, \
305 	{ "dump", CTLTYPE_STRUCT }, \
306 	{ "flags", CTLTYPE_STRUCT }, \
307 	{ "iflist", CTLTYPE_STRUCT }, \
308 }
309 
310 /*
311  * PF_KEY - Key Management
312  */
313 #define NET_KEY_SADB_DUMP	1	/* return SADB */
314 #define NET_KEY_SPD_DUMP	2	/* return SPD */
315 #define NET_KEY_MAXID		3
316 
317 #define CTL_NET_KEY_NAMES { \
318 	{ 0, 0 }, \
319 	{ "sadb_dump", CTLTYPE_STRUCT }, \
320 	{ "spd_dump", CTLTYPE_STRUCT }, \
321 }
322 
323 /*
324  * PF_BPF  not really a family, but connected under CTL_NET
325  */
326 #define NET_BPF_BUFSIZE		1		/* default buffer size */
327 #define NET_BPF_MAXBUFSIZE	2		/* maximum buffer size */
328 #define NET_BPF_MAXID		3
329 
330 #define CTL_NET_BPF_NAMES { \
331 	{ 0, 0 }, \
332 	{ "bufsize", CTLTYPE_INT }, \
333 	{ "maxbufsize", CTLTYPE_INT }, \
334 }
335 
336 /*
337  * Maximum queue length specifiable by listen(2).
338  */
339 #define	SOMAXCONN	128
340 
341 /*
342  * Message header for recvmsg and sendmsg calls.
343  * Used value-result for recvmsg, value only for sendmsg.
344  */
345 struct msghdr {
346 	void		*msg_name;	/* optional address */
347 	socklen_t	msg_namelen;	/* size of address */
348 	struct		iovec *msg_iov;	/* scatter/gather array */
349 	unsigned int	msg_iovlen;	/* # elements in msg_iov */
350 	void		*msg_control;	/* ancillary data, see below */
351 	socklen_t	msg_controllen;	/* ancillary data buffer len */
352 	int		msg_flags;	/* flags on received message */
353 };
354 
355 #define	MSG_OOB		0x1		/* process out-of-band data */
356 #define	MSG_PEEK	0x2		/* peek at incoming message */
357 #define	MSG_DONTROUTE	0x4		/* send without using routing tables */
358 #define	MSG_EOR		0x8		/* data completes record */
359 #define	MSG_TRUNC	0x10		/* data discarded before delivery */
360 #define	MSG_CTRUNC	0x20		/* control data lost before delivery */
361 #define	MSG_WAITALL	0x40		/* wait for full request or error */
362 #define	MSG_DONTWAIT	0x80		/* this message should be nonblocking */
363 #define	MSG_BCAST	0x100		/* this message rec'd as broadcast */
364 #define	MSG_MCAST	0x200		/* this message rec'd as multicast */
365 
366 /*
367  * Header for ancillary data objects in msg_control buffer.
368  * Used for additional information with/about a datagram
369  * not expressible by flags.  The format is a sequence
370  * of message elements headed by cmsghdr structures.
371  */
372 struct cmsghdr {
373 	socklen_t	cmsg_len;	/* data byte count, including hdr */
374 	int		cmsg_level;	/* originating protocol */
375 	int		cmsg_type;	/* protocol-specific type */
376 /* followed by	u_char  cmsg_data[]; */
377 };
378 
379 /* given pointer to struct cmsghdr, return pointer to data */
380 #define	CMSG_DATA(cmsg) \
381 	((u_char *)(cmsg) + __CMSG_ALIGN(sizeof(struct cmsghdr)))
382 
383 /* given pointer to struct cmsghdr, return pointer to next cmsghdr */
384 #define	CMSG_NXTHDR(mhdr, cmsg)	\
385 	(((caddr_t)(cmsg) + __CMSG_ALIGN((cmsg)->cmsg_len) + \
386 			    __CMSG_ALIGN(sizeof(struct cmsghdr)) > \
387 	    ((caddr_t)(mhdr)->msg_control) + (mhdr)->msg_controllen) ? \
388 	    (struct cmsghdr *)NULL : \
389 	    (struct cmsghdr *)((caddr_t)(cmsg) + __CMSG_ALIGN((cmsg)->cmsg_len)))
390 
391 /*
392  * RFC 2292 requires to check msg_controllen, in case that the kernel returns
393  * an empty list for some reasons.
394  */
395 #define	CMSG_FIRSTHDR(mhdr) \
396 	((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
397 	 (struct cmsghdr *)(mhdr)->msg_control : \
398 	 (struct cmsghdr *)NULL)
399 
400 /* Round len up to next alignment boundary */
401 #define	__CMSG_ALIGN(len)	ALIGN(len)
402 #ifdef _KERNEL
403 #define CMSG_ALIGN(n)		__CMSG_ALIGN(n)
404 #endif
405 
406 /* Length of the contents of a control message of length len */
407 #define	CMSG_LEN(len)	(__CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
408 
409 /* Length of the space taken up by a padded control message of length len */
410 #define	CMSG_SPACE(len)	(__CMSG_ALIGN(sizeof(struct cmsghdr)) + __CMSG_ALIGN(len))
411 
412 /* "Socket"-level control message types: */
413 #define	SCM_RIGHTS	0x01		/* access rights (array of int) */
414 #define SCM_CREDS	0x02		/* credentials (struct sockcred) */
415 
416 /*
417  * 4.3 compat sockaddr, move to compat file later
418  */
419 struct osockaddr {
420 	unsigned short	sa_family;	/* address family */
421 	char		sa_data[14];	/* up to 14 bytes of direct address */
422 };
423 
424 /*
425  * 4.3-compat message header (move to compat file later).
426  */
427 struct omsghdr {
428 	caddr_t	msg_name;		/* optional address */
429 	int	msg_namelen;		/* size of address */
430 	struct	iovec *msg_iov;		/* scatter/gather array */
431 	int	msg_iovlen;		/* # elements in msg_iov */
432 	caddr_t	msg_accrights;		/* access rights sent/received */
433 	int	msg_accrightslen;
434 };
435 
436 #define SA_LEN(x) ((x)->sa_len)
437 
438 #ifndef	_KERNEL
439 
440 #include <sys/cdefs.h>
441 
442 __BEGIN_DECLS
443 int	accept(int, struct sockaddr *, socklen_t *);
444 int	bind(int, const struct sockaddr *, socklen_t);
445 int	connect(int, const struct sockaddr *, socklen_t);
446 int	getpeereid(int, uid_t *, gid_t *);
447 int	getpeername(int, struct sockaddr *, socklen_t *);
448 int	getsockname(int, struct sockaddr *, socklen_t *);
449 int	getsockopt(int, int, int, void *, socklen_t *);
450 int	listen(int, int);
451 ssize_t	recv(int, void *, size_t, int);
452 ssize_t	recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
453 ssize_t	recvmsg(int, struct msghdr *, int);
454 ssize_t	send(int, const void *, size_t, int);
455 ssize_t	sendto(int, const void *,
456 	    size_t, int, const struct sockaddr *, socklen_t);
457 ssize_t	sendmsg(int, const struct msghdr *, int);
458 int	setsockopt(int, int, int, const void *, socklen_t);
459 int	shutdown(int, int);
460 int	socket(int, int, int);
461 int	socketpair(int, int, int, int *);
462 __END_DECLS
463 #else
464 # if defined(COMPAT_OPENBSD) || defined(COMPAT_LINUX)
465 #  define COMPAT_OLDSOCK
466 #  define MSG_COMPAT	0x8000
467 # endif
468 
469 void	pfctlinput(int, struct sockaddr *);
470 #endif /* !_KERNEL */
471 
472 #endif /* !_SYS_SOCKET_H_ */
473