xref: /illumos-gate/usr/src/uts/common/netinet/in.h (revision d1aea6f139360e9e7f1504facb24f8521047b15c)
1 /*
2  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  *
5  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
6  */
7 /*
8  * Copyright (c) 1982, 1986 Regents of the University of California.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms are permitted
12  * provided that this notice is preserved and that due credit is given
13  * to the University of California at Berkeley. The name of the University
14  * may not be used to endorse or promote products derived from this
15  * software without specific prior written permission. This software
16  * is provided ``as is'' without express or implied warranty.
17  */
18 
19 /*
20  * Constants and structures defined by the internet system,
21  * according to following documents
22  *
23  * Internet ASSIGNED NUMBERS (RFC1700) and its successors:
24  *	http://www.iana.org/assignments/protocol-numbers
25  *	http://www.iana.org/assignments/port-numbers
26  * Basic Socket Interface Extensions for IPv6 (RFC2133 and its successors)
27  *
28  */
29 
30 #ifndef _NETINET_IN_H
31 #define	_NETINET_IN_H
32 
33 #include <sys/feature_tests.h>
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 #include <sys/types.h>
40 
41 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
42 #include <sys/socket_impl.h>
43 #endif	/* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
44 
45 #ifndef _SOCKLEN_T
46 #define	_SOCKLEN_T
47 
48 /*
49  * The socklen definitions are reproduced here from sys/socket.h so as to
50  * not introduce that namespace into existing users of netinet/in.h.
51  */
52 #if defined(_XPG4_2) && !defined(_XPG5) && !defined(_LP64)
53 typedef	size_t		socklen_t;
54 #else
55 typedef	uint32_t	socklen_t;
56 #endif	/* defined(_XPG4_2) && !defined(_XPG5) && !defined(_LP64) */
57 
58 #if defined(_XPG4_2) || defined(_BOOT)
59 typedef	socklen_t	*Psocklen_t;
60 #else
61 typedef	void		*Psocklen_t;
62 #endif	/* defined(_XPG4_2) || defined(_BOOT) */
63 
64 #endif /* _SOCKLEN_T */
65 
66 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
67 #include <sys/stream.h>
68 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
69 /*
70  * Symbols such as htonl() are required to be exposed through this file,
71  * per XNS Issue 5. This is achieved by inclusion of <sys/byteorder.h>
72  */
73 #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5)
74 #include <sys/byteorder.h>
75 #endif
76 
77 #ifndef _IN_PORT_T
78 #define	_IN_PORT_T
79 typedef	uint16_t	in_port_t;
80 #endif
81 
82 /*
83  * Note: IPv4 address data structures usage conventions.
84  * The "in_addr_t" type below (required by Unix standards)
85  * is NOT a typedef of "struct in_addr" and violates the usual
86  * conventions where "struct <name>" and <name>_t are corresponding
87  * typedefs.
88  * To minimize confusion, kernel data structures/usage prefers use
89  * of "ipaddr_t" as atomic uint32_t type and avoid using "in_addr_t"
90  * The user level APIs continue to follow the historic popular
91  * practice of using "struct in_addr".
92  */
93 #ifndef _IN_ADDR_T
94 #define	_IN_ADDR_T
95 typedef	uint32_t	in_addr_t;
96 #endif
97 
98 #ifndef _IPADDR_T
99 #define	_IPADDR_T
100 typedef uint32_t ipaddr_t;
101 #endif
102 
103 #if !defined(_XPG4_2) || defined(_XPG6) || defined(__EXTENSIONS__)
104 
105 struct in6_addr {
106 	union {
107 		/*
108 		 * Note: Static initalizers of "union" type assume
109 		 * the constant on the RHS is the type of the first member
110 		 * of union.
111 		 * To make static initializers (and efficient usage) work,
112 		 * the order of members exposed to user and kernel view of
113 		 * this data structure is different.
114 		 * User environment sees specified uint8_t type as first
115 		 * member whereas kernel sees most efficient type as
116 		 * first member.
117 		 */
118 #ifdef _KERNEL
119 		uint32_t	_S6_u32[4];	/* IPv6 address */
120 		uint16_t	_S6_u16[8];	/* IPv6 address */
121 		uint8_t		_S6_u8[16];	/* IPv6 address */
122 #else
123 		uint8_t		_S6_u8[16];	/* IPv6 address */
124 		uint16_t	_S6_u16[8];	/* IPv6 address */
125 		uint32_t	_S6_u32[4];	/* IPv6 address */
126 #endif
127 		uint32_t	__S6_align;	/* Align on 32 bit boundary */
128 	} _S6_un;
129 };
130 #define	s6_addr		_S6_un._S6_u8
131 
132 #ifdef _KERNEL
133 #define	s6_addr8	_S6_un._S6_u8
134 #define	s6_addr16	_S6_un._S6_u16
135 #define	s6_addr32	_S6_un._S6_u32
136 #endif
137 
138 typedef struct in6_addr in6_addr_t;
139 
140 #endif /* !defined(_XPG4_2) || defined(_XPG6) || defined(__EXTENSIONS__) */
141 
142 #ifndef _SA_FAMILY_T
143 #define	_SA_FAMILY_T
144 typedef	uint16_t	sa_family_t;
145 #endif
146 
147 /*
148  * Protocols
149  *
150  * Some of these constant names are copied for the DTrace IP provider in
151  * usr/src/lib/libdtrace/common/{ip.d.in, ip.sed.in}, which should be kept
152  * in sync.
153  */
154 #define	IPPROTO_IP		0		/* dummy for IP */
155 #define	IPPROTO_HOPOPTS		0		/* Hop by hop header for IPv6 */
156 #define	IPPROTO_ICMP		1		/* control message protocol */
157 #define	IPPROTO_IGMP		2		/* group control protocol */
158 #define	IPPROTO_GGP		3		/* gateway^2 (deprecated) */
159 #define	IPPROTO_ENCAP		4		/* IP in IP encapsulation */
160 #define	IPPROTO_TCP		6		/* tcp */
161 #define	IPPROTO_EGP		8		/* exterior gateway protocol */
162 #define	IPPROTO_PUP		12		/* pup */
163 #define	IPPROTO_UDP		17		/* user datagram protocol */
164 #define	IPPROTO_IDP		22		/* xns idp */
165 #define	IPPROTO_IPV6		41		/* IPv6 encapsulated in IP */
166 #define	IPPROTO_ROUTING		43		/* Routing header for IPv6 */
167 #define	IPPROTO_FRAGMENT	44		/* Fragment header for IPv6 */
168 #define	IPPROTO_RSVP		46		/* rsvp */
169 #define	IPPROTO_ESP		50		/* IPsec Encap. Sec. Payload */
170 #define	IPPROTO_AH		51		/* IPsec Authentication Hdr. */
171 #define	IPPROTO_ICMPV6		58		/* ICMP for IPv6 */
172 #define	IPPROTO_NONE		59		/* No next header for IPv6 */
173 #define	IPPROTO_DSTOPTS		60		/* Destination options */
174 #define	IPPROTO_HELLO		63		/* "hello" routing protocol */
175 #define	IPPROTO_ND		77		/* UNOFFICIAL net disk proto */
176 #define	IPPROTO_EON		80		/* ISO clnp */
177 #define	IPPROTO_OSPF		89		/* OSPF */
178 #define	IPPROTO_PIM		103		/* PIM routing protocol */
179 #define	IPPROTO_SCTP		132		/* Stream Control */
180 						/* Transmission Protocol */
181 
182 #define	IPPROTO_RAW		255		/* raw IP packet */
183 #define	IPPROTO_MAX		256
184 
185 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
186 #define	PROTO_SDP		257		/* Sockets Direct Protocol */
187 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
188 
189 /*
190  * Port/socket numbers: network standard functions
191  *
192  * Entries should exist here for each port number compiled into an ON
193  * component, such as snoop.
194  */
195 #define	IPPORT_ECHO		7
196 #define	IPPORT_DISCARD		9
197 #define	IPPORT_SYSTAT		11
198 #define	IPPORT_DAYTIME		13
199 #define	IPPORT_NETSTAT		15
200 #define	IPPORT_CHARGEN		19
201 #define	IPPORT_FTP		21
202 #define	IPPORT_TELNET		23
203 #define	IPPORT_SMTP		25
204 #define	IPPORT_TIMESERVER	37
205 #define	IPPORT_NAMESERVER	42
206 #define	IPPORT_WHOIS		43
207 #define	IPPORT_DOMAIN		53
208 #define	IPPORT_MDNS		5353
209 #define	IPPORT_MTP		57
210 
211 /*
212  * Port/socket numbers: host specific functions
213  */
214 #define	IPPORT_BOOTPS		67
215 #define	IPPORT_BOOTPC		68
216 #define	IPPORT_TFTP		69
217 #define	IPPORT_RJE		77
218 #define	IPPORT_FINGER		79
219 #define	IPPORT_HTTP		80
220 #define	IPPORT_HTTP_ALT		8080
221 #define	IPPORT_TTYLINK		87
222 #define	IPPORT_SUPDUP		95
223 #define	IPPORT_NTP		123
224 #define	IPPORT_NETBIOS_NS	137
225 #define	IPPORT_NETBIOS_DGM	138
226 #define	IPPORT_NETBIOS_SSN	139
227 #define	IPPORT_LDAP		389
228 #define	IPPORT_SLP		427
229 #define	IPPORT_MIP		434
230 #define	IPPORT_SMB		445		/* a.k.a. microsoft-ds */
231 
232 /*
233  * Internet Key Exchange (IKE) ports
234  */
235 #define	IPPORT_IKE		500
236 #define	IPPORT_IKE_NATT		4500
237 
238 /*
239  * UNIX TCP sockets
240  */
241 #define	IPPORT_EXECSERVER	512
242 #define	IPPORT_LOGINSERVER	513
243 #define	IPPORT_CMDSERVER	514
244 #define	IPPORT_PRINTER		515
245 #define	IPPORT_EFSSERVER	520
246 
247 /*
248  * UNIX UDP sockets
249  */
250 #define	IPPORT_BIFFUDP		512
251 #define	IPPORT_WHOSERVER	513
252 #define	IPPORT_SYSLOG		514
253 #define	IPPORT_TALK		517
254 #define	IPPORT_ROUTESERVER	520
255 #define	IPPORT_RIPNG		521
256 
257 /*
258  * DHCPv6 UDP ports
259  */
260 #define	IPPORT_DHCPV6C		546
261 #define	IPPORT_DHCPV6S		547
262 
263 #define	IPPORT_SOCKS		1080
264 
265 /*
266  * Ports < IPPORT_RESERVED are reserved for
267  * privileged processes (e.g. root).
268  * Ports > IPPORT_USERRESERVED are reserved
269  * for servers, not necessarily privileged.
270  */
271 #define	IPPORT_RESERVED		1024
272 #define	IPPORT_USERRESERVED	5000
273 
274 /*
275  * Link numbers
276  */
277 #define	IMPLINK_IP		155
278 #define	IMPLINK_LOWEXPER	156
279 #define	IMPLINK_HIGHEXPER	158
280 
281 /*
282  * IPv4 Internet address
283  *	This definition contains obsolete fields for compatibility
284  *	with SunOS 3.x and 4.2bsd.  The presence of subnets renders
285  *	divisions into fixed fields misleading at best.  New code
286  *	should use only the s_addr field.
287  */
288 
289 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
290 #define	_S_un_b	S_un_b
291 #define	_S_un_w	S_un_w
292 #define	_S_addr	S_addr
293 #define	_S_un	S_un
294 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
295 
296 struct in_addr {
297 	union {
298 		struct { uint8_t s_b1, s_b2, s_b3, s_b4; } _S_un_b;
299 		struct { uint16_t s_w1, s_w2; } _S_un_w;
300 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
301 		uint32_t _S_addr;
302 #else
303 		in_addr_t _S_addr;
304 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
305 	} _S_un;
306 #define	s_addr	_S_un._S_addr		/* should be used for all code */
307 #define	s_host	_S_un._S_un_b.s_b2	/* OBSOLETE: host on imp */
308 #define	s_net	_S_un._S_un_b.s_b1	/* OBSOLETE: network */
309 #define	s_imp	_S_un._S_un_w.s_w2	/* OBSOLETE: imp */
310 #define	s_impno	_S_un._S_un_b.s_b4	/* OBSOLETE: imp # */
311 #define	s_lh	_S_un._S_un_b.s_b3	/* OBSOLETE: logical host */
312 };
313 
314 /*
315  * Definitions of bits in internet address integers.
316  * On subnets, the decomposition of addresses to host and net parts
317  * is done according to subnet mask, not the masks here.
318  *
319  * Note that with the introduction of CIDR, IN_CLASSA, IN_CLASSB,
320  * IN_CLASSC, IN_CLASSD and IN_CLASSE macros have become "de-facto
321  * obsolete". IN_MULTICAST macro should be used to test if a address
322  * is a multicast address.
323  */
324 #define	IN_CLASSA(i)		(((i) & 0x80000000U) == 0)
325 #define	IN_CLASSA_NET		0xff000000U
326 #define	IN_CLASSA_NSHIFT	24
327 #define	IN_CLASSA_HOST		0x00ffffffU
328 #define	IN_CLASSA_MAX		128
329 
330 #define	IN_CLASSB(i)		(((i) & 0xc0000000U) == 0x80000000U)
331 #define	IN_CLASSB_NET		0xffff0000U
332 #define	IN_CLASSB_NSHIFT	16
333 #define	IN_CLASSB_HOST		0x0000ffffU
334 #define	IN_CLASSB_MAX		65536
335 
336 #define	IN_CLASSC(i)		(((i) & 0xe0000000U) == 0xc0000000U)
337 #define	IN_CLASSC_NET		0xffffff00U
338 #define	IN_CLASSC_NSHIFT	8
339 #define	IN_CLASSC_HOST		0x000000ffU
340 
341 #define	IN_CLASSD(i)		(((i) & 0xf0000000U) == 0xe0000000U)
342 #define	IN_CLASSD_NET		0xf0000000U	/* These aren't really  */
343 #define	IN_CLASSD_NSHIFT	28		/* net and host fields, but */
344 #define	IN_CLASSD_HOST		0x0fffffffU	/* routing needn't know */
345 
346 #define	IN_CLASSE(i)		(((i) & 0xf0000000U) == 0xf0000000U)
347 #define	IN_CLASSE_NET		0xffffffffU
348 
349 #define	IN_MULTICAST(i)		IN_CLASSD(i)
350 
351 /*
352  * We have removed CLASS E checks from the kernel
353  * But we preserve these defines for userland in order
354  * to avoid compile  breakage of some 3rd party piece of software
355  */
356 #ifndef _KERNEL
357 #define	IN_EXPERIMENTAL(i)	(((i) & 0xe0000000U) == 0xe0000000U)
358 #define	IN_BADCLASS(i)		(((i) & 0xf0000000U) == 0xf0000000U)
359 #endif
360 
361 #define	INADDR_ANY		0x00000000U
362 #define	INADDR_LOOPBACK		0x7F000001U
363 #define	INADDR_BROADCAST	0xffffffffU	/* must be masked */
364 #define	INADDR_NONE		0xffffffffU
365 
366 #define	INADDR_UNSPEC_GROUP	0xe0000000U	/* 224.0.0.0   */
367 #define	INADDR_ALLHOSTS_GROUP	0xe0000001U	/* 224.0.0.1   */
368 #define	INADDR_ALLRTRS_GROUP	0xe0000002U	/* 224.0.0.2   */
369 #define	INADDR_ALLRPTS_GROUP	0xe0000016U	/* 224.0.0.22, IGMPv3 */
370 #define	INADDR_MAX_LOCAL_GROUP	0xe00000ffU	/* 224.0.0.255 */
371 
372 /* Scoped IPv4 prefixes (in host byte-order) */
373 #define	IN_AUTOCONF_NET		0xa9fe0000U	/* 169.254/16 */
374 #define	IN_AUTOCONF_MASK	0xffff0000U
375 #define	IN_PRIVATE8_NET		0x0a000000U	/* 10/8 */
376 #define	IN_PRIVATE8_MASK	0xff000000U
377 #define	IN_PRIVATE12_NET	0xac100000U	/* 172.16/12 */
378 #define	IN_PRIVATE12_MASK	0xfff00000U
379 #define	IN_PRIVATE16_NET	0xc0a80000U	/* 192.168/16 */
380 #define	IN_PRIVATE16_MASK	0xffff0000U
381 
382 /* RFC 3927 IPv4 link local address (i in host byte-order) */
383 #define	IN_LINKLOCAL(i)		(((i) & IN_AUTOCONF_MASK) == IN_AUTOCONF_NET)
384 
385 /* Well known 6to4 Relay Router Anycast address defined in RFC 3068 */
386 #if !defined(_XPG4_2) || !defined(__EXTENSIONS__)
387 #define	INADDR_6TO4RRANYCAST	0xc0586301U 	/* 192.88.99.1 */
388 #endif	/* !defined(_XPG4_2) || !defined(__EXTENSIONS__) */
389 
390 #define	IN_LOOPBACKNET		127			/* official! */
391 
392 /*
393  * Define a macro to stuff the loopback address into an Internet address
394  */
395 #if !defined(_XPG4_2) || !defined(__EXTENSIONS__)
396 #define	IN_SET_LOOPBACK_ADDR(a) \
397 	{ (a)->sin_addr.s_addr  = htonl(INADDR_LOOPBACK); \
398 	(a)->sin_family = AF_INET; }
399 #endif /* !defined(_XPG4_2) || !defined(__EXTENSIONS__) */
400 
401 /*
402  * IPv4 Socket address.
403  */
404 struct sockaddr_in {
405 	sa_family_t	sin_family;
406 	in_port_t	sin_port;
407 	struct	in_addr sin_addr;
408 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
409 	char		sin_zero[8];
410 #else
411 	unsigned char	sin_zero[8];
412 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
413 };
414 
415 #if !defined(_XPG4_2) || defined(_XPG6) || defined(__EXTENSIONS__)
416 /*
417  * IPv6 socket address.
418  */
419 struct sockaddr_in6 {
420 	sa_family_t	sin6_family;
421 	in_port_t	sin6_port;
422 	uint32_t	sin6_flowinfo;
423 	struct in6_addr	sin6_addr;
424 	uint32_t	sin6_scope_id;  /* Depends on scope of sin6_addr */
425 	uint32_t	__sin6_src_id;	/* Impl. specific - UDP replies */
426 };
427 
428 /*
429  * Macros for accessing the traffic class and flow label fields from
430  * sin6_flowinfo.
431  * These are designed to be applied to a 32-bit value.
432  */
433 #ifdef _BIG_ENDIAN
434 
435 /* masks */
436 #define	IPV6_FLOWINFO_FLOWLABEL			0x000fffffU
437 #define	IPV6_FLOWINFO_TCLASS			0x0ff00000U
438 
439 #else /* _BIG_ENDIAN */
440 
441 /* masks */
442 #define	IPV6_FLOWINFO_FLOWLABEL			0xffff0f00U
443 #define	IPV6_FLOWINFO_TCLASS			0x0000f00fU
444 
445 #endif	/* _BIG_ENDIAN */
446 
447 /*
448  * Note: Macros IN6ADDR_ANY_INIT and IN6ADDR_LOOPBACK_INIT are for
449  * use as RHS of Static initializers of "struct in6_addr" (or in6_addr_t)
450  * only. They need to be different for User/Kernel versions because union
451  * component data structure is defined differently (it is identical at
452  * binary representation level).
453  *
454  * const struct in6_addr IN6ADDR_ANY_INIT;
455  * const struct in6_addr IN6ADDR_LOOPBACK_INIT;
456  */
457 
458 
459 #ifdef _KERNEL
460 #define	IN6ADDR_ANY_INIT		{ 0, 0, 0, 0 }
461 
462 #ifdef _BIG_ENDIAN
463 #define	IN6ADDR_LOOPBACK_INIT		{ 0, 0, 0, 0x00000001U }
464 #else /* _BIG_ENDIAN */
465 #define	IN6ADDR_LOOPBACK_INIT		{ 0, 0, 0, 0x01000000U }
466 #endif /* _BIG_ENDIAN */
467 
468 #else
469 
470 #define	IN6ADDR_ANY_INIT	    {	0, 0, 0, 0,	\
471 					0, 0, 0, 0,	\
472 					0, 0, 0, 0, 	\
473 					0, 0, 0, 0 }
474 
475 #define	IN6ADDR_LOOPBACK_INIT	    {	0, 0, 0, 0,	\
476 					0, 0, 0, 0,	\
477 					0, 0, 0, 0,	\
478 					0, 0, 0, 0x1U }
479 #endif /* _KERNEL */
480 
481 /*
482  * RFC 2553 specifies the following macros. Their type is defined
483  * as "int" in the RFC but they only have boolean significance
484  * (zero or non-zero). For the purposes of our comment notation,
485  * we assume a hypothetical type "bool" defined as follows to
486  * write the prototypes assumed for macros in our comments better.
487  *
488  * typedef int bool;
489  */
490 
491 /*
492  * IN6 macros used to test for special IPv6 addresses
493  * (Mostly from spec)
494  *
495  * bool  IN6_IS_ADDR_UNSPECIFIED (const struct in6_addr *);
496  * bool  IN6_IS_ADDR_LOOPBACK    (const struct in6_addr *);
497  * bool  IN6_IS_ADDR_MULTICAST   (const struct in6_addr *);
498  * bool  IN6_IS_ADDR_LINKLOCAL   (const struct in6_addr *);
499  * bool  IN6_IS_ADDR_SITELOCAL   (const struct in6_addr *);
500  * bool  IN6_IS_ADDR_V4MAPPED    (const struct in6_addr *);
501  * bool  IN6_IS_ADDR_V4MAPPED_ANY(const struct in6_addr *); -- Not from RFC2553
502  * bool  IN6_IS_ADDR_V4COMPAT    (const struct in6_addr *);
503  * bool  IN6_IS_ADDR_MC_RESERVED (const struct in6_addr *); -- Not from RFC2553
504  * bool  IN6_IS_ADDR_MC_NODELOCAL(const struct in6_addr *);
505  * bool  IN6_IS_ADDR_MC_LINKLOCAL(const struct in6_addr *);
506  * bool  IN6_IS_ADDR_MC_SITELOCAL(const struct in6_addr *);
507  * bool  IN6_IS_ADDR_MC_ORGLOCAL (const struct in6_addr *);
508  * bool  IN6_IS_ADDR_MC_GLOBAL   (const struct in6_addr *);
509  * bool  IN6_IS_ADDR_6TO4	 (const struct in6_addr *); -- Not from RFC2553
510  * bool  IN6_ARE_6TO4_PREFIX_EQUAL(const struct in6_addr *,
511  *	     const struct in6_addr *);			    -- Not from RFC2553
512  * bool  IN6_IS_ADDR_LINKSCOPE	 (const struct in6addr  *); -- Not from RFC2553
513  */
514 
515 #define	IN6_IS_ADDR_UNSPECIFIED(addr) \
516 	(((addr)->_S6_un._S6_u32[3] == 0) && \
517 	((addr)->_S6_un._S6_u32[2] == 0) && \
518 	((addr)->_S6_un._S6_u32[1] == 0) && \
519 	((addr)->_S6_un._S6_u32[0] == 0))
520 
521 #ifdef _BIG_ENDIAN
522 #define	IN6_IS_ADDR_LOOPBACK(addr) \
523 	(((addr)->_S6_un._S6_u32[3] == 0x00000001) && \
524 	((addr)->_S6_un._S6_u32[2] == 0) && \
525 	((addr)->_S6_un._S6_u32[1] == 0) && \
526 	((addr)->_S6_un._S6_u32[0] == 0))
527 #else /* _BIG_ENDIAN */
528 #define	IN6_IS_ADDR_LOOPBACK(addr) \
529 	(((addr)->_S6_un._S6_u32[3] == 0x01000000) && \
530 	((addr)->_S6_un._S6_u32[2] == 0) && \
531 	((addr)->_S6_un._S6_u32[1] == 0) && \
532 	((addr)->_S6_un._S6_u32[0] == 0))
533 #endif /* _BIG_ENDIAN */
534 
535 #ifdef _BIG_ENDIAN
536 #define	IN6_IS_ADDR_MULTICAST(addr) \
537 	(((addr)->_S6_un._S6_u32[0] & 0xff000000) == 0xff000000)
538 #else /* _BIG_ENDIAN */
539 #define	IN6_IS_ADDR_MULTICAST(addr) \
540 	(((addr)->_S6_un._S6_u32[0] & 0x000000ff) == 0x000000ff)
541 #endif /* _BIG_ENDIAN */
542 
543 #ifdef _BIG_ENDIAN
544 #define	IN6_IS_ADDR_LINKLOCAL(addr) \
545 	(((addr)->_S6_un._S6_u32[0] & 0xffc00000) == 0xfe800000)
546 #else /* _BIG_ENDIAN */
547 #define	IN6_IS_ADDR_LINKLOCAL(addr) \
548 	(((addr)->_S6_un._S6_u32[0] & 0x0000c0ff) == 0x000080fe)
549 #endif /* _BIG_ENDIAN */
550 
551 #ifdef _BIG_ENDIAN
552 #define	IN6_IS_ADDR_SITELOCAL(addr) \
553 	(((addr)->_S6_un._S6_u32[0] & 0xffc00000) == 0xfec00000)
554 #else /* _BIG_ENDIAN */
555 #define	IN6_IS_ADDR_SITELOCAL(addr) \
556 	(((addr)->_S6_un._S6_u32[0] & 0x0000c0ff) == 0x0000c0fe)
557 #endif /* _BIG_ENDIAN */
558 
559 #ifdef _BIG_ENDIAN
560 #define	IN6_IS_ADDR_V4MAPPED(addr) \
561 	(((addr)->_S6_un._S6_u32[2] == 0x0000ffff) && \
562 	((addr)->_S6_un._S6_u32[1] == 0) && \
563 	((addr)->_S6_un._S6_u32[0] == 0))
564 #else  /* _BIG_ENDIAN */
565 #define	IN6_IS_ADDR_V4MAPPED(addr) \
566 	(((addr)->_S6_un._S6_u32[2] == 0xffff0000U) && \
567 	((addr)->_S6_un._S6_u32[1] == 0) && \
568 	((addr)->_S6_un._S6_u32[0] == 0))
569 #endif /* _BIG_ENDIAN */
570 
571 /*
572  * IN6_IS_ADDR_V4MAPPED - A IPv4 mapped INADDR_ANY
573  * Note: This macro is currently NOT defined in RFC2553 specification
574  * and not a standard macro that portable applications should use.
575  */
576 #ifdef _BIG_ENDIAN
577 #define	IN6_IS_ADDR_V4MAPPED_ANY(addr) \
578 	(((addr)->_S6_un._S6_u32[3] == 0) && \
579 	((addr)->_S6_un._S6_u32[2] == 0x0000ffff) && \
580 	((addr)->_S6_un._S6_u32[1] == 0) && \
581 	((addr)->_S6_un._S6_u32[0] == 0))
582 #else  /* _BIG_ENDIAN */
583 #define	IN6_IS_ADDR_V4MAPPED_ANY(addr) \
584 	(((addr)->_S6_un._S6_u32[3] == 0) && \
585 	((addr)->_S6_un._S6_u32[2] == 0xffff0000U) && \
586 	((addr)->_S6_un._S6_u32[1] == 0) && \
587 	((addr)->_S6_un._S6_u32[0] == 0))
588 #endif /* _BIG_ENDIAN */
589 
590 /* Exclude loopback and unspecified address */
591 #ifdef _BIG_ENDIAN
592 #define	IN6_IS_ADDR_V4COMPAT(addr) \
593 	(((addr)->_S6_un._S6_u32[2] == 0) && \
594 	((addr)->_S6_un._S6_u32[1] == 0) && \
595 	((addr)->_S6_un._S6_u32[0] == 0) && \
596 	!((addr)->_S6_un._S6_u32[3] == 0) && \
597 	!((addr)->_S6_un._S6_u32[3] == 0x00000001))
598 
599 #else /* _BIG_ENDIAN */
600 #define	IN6_IS_ADDR_V4COMPAT(addr) \
601 	(((addr)->_S6_un._S6_u32[2] == 0) && \
602 	((addr)->_S6_un._S6_u32[1] == 0) && \
603 	((addr)->_S6_un._S6_u32[0] == 0) && \
604 	!((addr)->_S6_un._S6_u32[3] == 0) && \
605 	!((addr)->_S6_un._S6_u32[3] == 0x01000000))
606 #endif /* _BIG_ENDIAN */
607 
608 /*
609  * Note:
610  * IN6_IS_ADDR_MC_RESERVED macro is currently NOT defined in RFC2553
611  * specification and not a standard macro that portable applications
612  * should use.
613  */
614 #ifdef _BIG_ENDIAN
615 #define	IN6_IS_ADDR_MC_RESERVED(addr) \
616 	(((addr)->_S6_un._S6_u32[0] & 0xff0f0000) == 0xff000000)
617 
618 #else  /* _BIG_ENDIAN */
619 #define	IN6_IS_ADDR_MC_RESERVED(addr) \
620 	(((addr)->_S6_un._S6_u32[0] & 0x00000fff) == 0x000000ff)
621 #endif /* _BIG_ENDIAN */
622 
623 #ifdef _BIG_ENDIAN
624 #define	IN6_IS_ADDR_MC_NODELOCAL(addr) \
625 	(((addr)->_S6_un._S6_u32[0] & 0xff0f0000) == 0xff010000)
626 #else  /* _BIG_ENDIAN */
627 #define	IN6_IS_ADDR_MC_NODELOCAL(addr) \
628 	(((addr)->_S6_un._S6_u32[0] & 0x00000fff) == 0x000001ff)
629 #endif /* _BIG_ENDIAN */
630 
631 #ifdef _BIG_ENDIAN
632 #define	IN6_IS_ADDR_MC_LINKLOCAL(addr) \
633 	(((addr)->_S6_un._S6_u32[0] & 0xff0f0000) == 0xff020000)
634 #else  /* _BIG_ENDIAN */
635 #define	IN6_IS_ADDR_MC_LINKLOCAL(addr) \
636 	(((addr)->_S6_un._S6_u32[0] & 0x00000fff) == 0x000002ff)
637 #endif /* _BIG_ENDIAN */
638 
639 #ifdef _BIG_ENDIAN
640 #define	IN6_IS_ADDR_MC_SITELOCAL(addr) \
641 	(((addr)->_S6_un._S6_u32[0] & 0xff0f0000) == 0xff050000)
642 #else  /* _BIG_ENDIAN */
643 #define	IN6_IS_ADDR_MC_SITELOCAL(addr) \
644 	(((addr)->_S6_un._S6_u32[0] & 0x00000fff) == 0x000005ff)
645 #endif /* _BIG_ENDIAN */
646 
647 #ifdef _BIG_ENDIAN
648 #define	IN6_IS_ADDR_MC_ORGLOCAL(addr) \
649 	(((addr)->_S6_un._S6_u32[0] & 0xff0f0000) == 0xff080000)
650 #else  /* _BIG_ENDIAN */
651 #define	IN6_IS_ADDR_MC_ORGLOCAL(addr) \
652 	(((addr)->_S6_un._S6_u32[0] & 0x00000fff) == 0x000008ff)
653 #endif /* _BIG_ENDIAN */
654 
655 #ifdef _BIG_ENDIAN
656 #define	IN6_IS_ADDR_MC_GLOBAL(addr) \
657 	(((addr)->_S6_un._S6_u32[0] & 0xff0f0000) == 0xff0e0000)
658 #else /* _BIG_ENDIAN */
659 #define	IN6_IS_ADDR_MC_GLOBAL(addr) \
660 	(((addr)->_S6_un._S6_u32[0] & 0x00000fff) == 0x00000eff)
661 #endif /* _BIG_ENDIAN */
662 
663 /*
664  * The IN6_IS_ADDR_MC_SOLICITEDNODE macro is not defined in any standard or
665  * RFC, and shouldn't be used by portable applications.  It is used to see
666  * if an address is a solicited-node multicast address, which is prefixed
667  * with ff02:0:0:0:0:1:ff00::/104.
668  */
669 #ifdef _BIG_ENDIAN
670 #define	IN6_IS_ADDR_MC_SOLICITEDNODE(addr)			\
671 	(((addr)->_S6_un._S6_u32[0] == 0xff020000) &&		\
672 	((addr)->_S6_un._S6_u32[1] == 0x00000000) &&		\
673 	((addr)->_S6_un._S6_u32[2] == 0x00000001) &&		\
674 	(((addr)->_S6_un._S6_u32[3] & 0xff000000) == 0xff000000))
675 #else
676 #define	IN6_IS_ADDR_MC_SOLICITEDNODE(addr)			\
677 	(((addr)->_S6_un._S6_u32[0] == 0x000002ff) &&		\
678 	((addr)->_S6_un._S6_u32[1] == 0x00000000) &&		\
679 	((addr)->_S6_un._S6_u32[2] == 0x01000000) &&		\
680 	(((addr)->_S6_un._S6_u32[3] & 0x000000ff) == 0x000000ff))
681 #endif
682 
683 /*
684  * Macros to a) test for 6to4 IPv6 address, and b) to test if two
685  * 6to4 addresses have the same /48 prefix, and, hence, are from the
686  * same 6to4 site.
687  */
688 
689 #ifdef _BIG_ENDIAN
690 #define	IN6_IS_ADDR_6TO4(addr) \
691 	(((addr)->_S6_un._S6_u32[0] & 0xffff0000) == 0x20020000)
692 #else /* _BIG_ENDIAN */
693 #define	IN6_IS_ADDR_6TO4(addr) \
694 	(((addr)->_S6_un._S6_u32[0] & 0x0000ffff) == 0x00000220)
695 #endif /* _BIG_ENDIAN */
696 
697 #define	IN6_ARE_6TO4_PREFIX_EQUAL(addr1, addr2) \
698 	(((addr1)->_S6_un._S6_u32[0] == (addr2)->_S6_un._S6_u32[0]) && \
699 	((addr1)->_S6_un._S6_u8[4] == (addr2)->_S6_un._S6_u8[4]) && \
700 	((addr1)->_S6_un._S6_u8[5] == (addr2)->_S6_un._S6_u8[5]))
701 
702 /*
703  * IN6_IS_ADDR_LINKSCOPE
704  * Identifies an address as being either link-local, link-local multicast or
705  * node-local multicast.  All types of addresses are considered to be unique
706  * within the scope of a given link.
707  */
708 #define	IN6_IS_ADDR_LINKSCOPE(addr) \
709 	(IN6_IS_ADDR_LINKLOCAL(addr) || IN6_IS_ADDR_MC_LINKLOCAL(addr) || \
710 	IN6_IS_ADDR_MC_NODELOCAL(addr))
711 
712 /*
713  * Useful utility macros for operations with IPv6 addresses
714  * Note: These macros are NOT defined in the RFC2553 or any other
715  * standard specification and are not standard macros that portable
716  * applications should use.
717  */
718 
719 /*
720  * IN6_V4MAPPED_TO_INADDR
721  * IN6_V4MAPPED_TO_IPADDR
722  *	Assign a IPv4-Mapped IPv6 address to an IPv4 address.
723  *	Note: These macros are NOT defined in RFC2553 or any other standard
724  *	specification and are not macros that portable applications should
725  *	use.
726  *
727  * void IN6_V4MAPPED_TO_INADDR(const in6_addr_t *v6, struct in_addr *v4);
728  * void IN6_V4MAPPED_TO_IPADDR(const in6_addr_t *v6, ipaddr_t v4);
729  *
730  */
731 #define	IN6_V4MAPPED_TO_INADDR(v6, v4) \
732 	((v4)->s_addr = (v6)->_S6_un._S6_u32[3])
733 #define	IN6_V4MAPPED_TO_IPADDR(v6, v4) \
734 	((v4) = (v6)->_S6_un._S6_u32[3])
735 
736 /*
737  * IN6_INADDR_TO_V4MAPPED
738  * IN6_IPADDR_TO_V4MAPPED
739  *	Assign a IPv4 address address to an IPv6 address as a IPv4-mapped
740  *	address.
741  *	Note: These macros are NOT defined in RFC2553 or any other standard
742  *	specification and are not macros that portable applications should
743  *	use.
744  *
745  * void IN6_INADDR_TO_V4MAPPED(const struct in_addr *v4, in6_addr_t *v6);
746  * void IN6_IPADDR_TO_V4MAPPED(const ipaddr_t v4, in6_addr_t *v6);
747  *
748  */
749 #ifdef _BIG_ENDIAN
750 #define	IN6_INADDR_TO_V4MAPPED(v4, v6) \
751 	((v6)->_S6_un._S6_u32[3] = (v4)->s_addr, \
752 	(v6)->_S6_un._S6_u32[2] = 0x0000ffff, \
753 	(v6)->_S6_un._S6_u32[1] = 0, \
754 	(v6)->_S6_un._S6_u32[0] = 0)
755 #define	IN6_IPADDR_TO_V4MAPPED(v4, v6) \
756 	((v6)->_S6_un._S6_u32[3] = (v4), \
757 	(v6)->_S6_un._S6_u32[2] = 0x0000ffff, \
758 	(v6)->_S6_un._S6_u32[1] = 0, \
759 	(v6)->_S6_un._S6_u32[0] = 0)
760 #else /* _BIG_ENDIAN */
761 #define	IN6_INADDR_TO_V4MAPPED(v4, v6) \
762 	((v6)->_S6_un._S6_u32[3] = (v4)->s_addr, \
763 	(v6)->_S6_un._S6_u32[2] = 0xffff0000U, \
764 	(v6)->_S6_un._S6_u32[1] = 0, \
765 	(v6)->_S6_un._S6_u32[0] = 0)
766 #define	IN6_IPADDR_TO_V4MAPPED(v4, v6) \
767 	((v6)->_S6_un._S6_u32[3] = (v4), \
768 	(v6)->_S6_un._S6_u32[2] = 0xffff0000U, \
769 	(v6)->_S6_un._S6_u32[1] = 0, \
770 	(v6)->_S6_un._S6_u32[0] = 0)
771 #endif /* _BIG_ENDIAN */
772 
773 /*
774  * IN6_6TO4_TO_V4ADDR
775  *	Extract the embedded IPv4 address from the prefix to a 6to4 IPv6
776  *      address.
777  *	Note: This macro is NOT defined in RFC2553 or any other standard
778  *	specification and is not a macro that portable applications should
779  *	use.
780  *	Note: we don't use the IPADDR form of the macro because we need
781  *	to do a bytewise copy; the V4ADDR in the 6to4 address is not
782  *	32-bit aligned.
783  *
784  * void IN6_6TO4_TO_V4ADDR(const in6_addr_t *v6, struct in_addr *v4);
785  *
786  */
787 #define	IN6_6TO4_TO_V4ADDR(v6, v4) \
788 	((v4)->_S_un._S_un_b.s_b1 = (v6)->_S6_un._S6_u8[2], \
789 	(v4)->_S_un._S_un_b.s_b2 = (v6)->_S6_un._S6_u8[3],  \
790 	(v4)->_S_un._S_un_b.s_b3 = (v6)->_S6_un._S6_u8[4],  \
791 	(v4)->_S_un._S_un_b.s_b4 = (v6)->_S6_un._S6_u8[5])
792 
793 /*
794  * IN6_V4ADDR_TO_6TO4
795  *	Given an IPv4 address and an IPv6 address for output, a 6to4 address
796  *	will be created from the IPv4 Address.
797  *	Note:  This method for creating 6to4 addresses is not standardized
798  *	outside of Solaris.  The newly created 6to4 address will be of the form
799  *	2002:<V4ADDR>:<SUBNETID>::<HOSTID>, where SUBNETID will equal 0 and
800  *	HOSTID will equal 1.
801  *
802  * void IN6_V4ADDR_TO_6TO4(const struct in_addr *v4, in6_addr_t *v6)
803  *
804  */
805 #ifdef _BIG_ENDIAN
806 #define	IN6_V4ADDR_TO_6TO4(v4, v6) \
807 	((v6)->_S6_un._S6_u8[0] = 0x20, \
808 	(v6)->_S6_un._S6_u8[1] = 0x02, \
809 	(v6)->_S6_un._S6_u8[2] = (v4)->_S_un._S_un_b.s_b1, \
810 	(v6)->_S6_un._S6_u8[3] = (v4)->_S_un._S_un_b.s_b2, \
811 	(v6)->_S6_un._S6_u8[4] = (v4)->_S_un._S_un_b.s_b3, \
812 	(v6)->_S6_un._S6_u8[5] = (v4)->_S_un._S_un_b.s_b4, \
813 	(v6)->_S6_un._S6_u8[6] = 0, \
814 	(v6)->_S6_un._S6_u8[7] = 0, \
815 	(v6)->_S6_un._S6_u32[2] = 0, \
816 	(v6)->_S6_un._S6_u32[3] = 0x00000001U)
817 #else
818 #define	IN6_V4ADDR_TO_6TO4(v4, v6) \
819 	((v6)->_S6_un._S6_u8[0] = 0x20, \
820 	(v6)->_S6_un._S6_u8[1] = 0x02, \
821 	(v6)->_S6_un._S6_u8[2] = (v4)->_S_un._S_un_b.s_b1, \
822 	(v6)->_S6_un._S6_u8[3] = (v4)->_S_un._S_un_b.s_b2, \
823 	(v6)->_S6_un._S6_u8[4] = (v4)->_S_un._S_un_b.s_b3, \
824 	(v6)->_S6_un._S6_u8[5] = (v4)->_S_un._S_un_b.s_b4, \
825 	(v6)->_S6_un._S6_u8[6] = 0, \
826 	(v6)->_S6_un._S6_u8[7] = 0, \
827 	(v6)->_S6_un._S6_u32[2] = 0, \
828 	(v6)->_S6_un._S6_u32[3] = 0x01000000U)
829 #endif /* _BIG_ENDIAN */
830 
831 /*
832  * IN6_ARE_ADDR_EQUAL (defined in RFC2292)
833  *	 Compares if IPv6 addresses are equal.
834  * Note: Compares in order of high likelyhood of a miss so we minimize
835  * compares. (Current heuristic order, compare in reverse order of
836  * uint32_t units)
837  *
838  * bool  IN6_ARE_ADDR_EQUAL(const struct in6_addr *,
839  *			    const struct in6_addr *);
840  */
841 #define	IN6_ARE_ADDR_EQUAL(addr1, addr2) \
842 	(((addr1)->_S6_un._S6_u32[3] == (addr2)->_S6_un._S6_u32[3]) && \
843 	((addr1)->_S6_un._S6_u32[2] == (addr2)->_S6_un._S6_u32[2]) && \
844 	((addr1)->_S6_un._S6_u32[1] == (addr2)->_S6_un._S6_u32[1]) && \
845 	((addr1)->_S6_un._S6_u32[0] == (addr2)->_S6_un._S6_u32[0]))
846 
847 /*
848  * IN6_ARE_PREFIXEDADDR_EQUAL (not defined in RFCs)
849  *	Compares if prefixed parts of IPv6 addresses are equal.
850  *
851  * uint32_t IN6_MASK_FROM_PREFIX(int, int);
852  * bool     IN6_ARE_PREFIXEDADDR_EQUAL(const struct in6_addr *,
853  *				       const struct in6_addr *,
854  *				       int);
855  */
856 #define	IN6_MASK_FROM_PREFIX(qoctet, prefix) \
857 	((((qoctet) + 1) * 32 < (prefix)) ? 0xFFFFFFFFu : \
858 	((((qoctet) * 32) >= (prefix)) ? 0x00000000u : \
859 	0xFFFFFFFFu << (((qoctet) + 1) * 32 - (prefix))))
860 
861 #define	IN6_ARE_PREFIXEDADDR_EQUAL(addr1, addr2, prefix) \
862 	(((ntohl((addr1)->_S6_un._S6_u32[0]) & \
863 	IN6_MASK_FROM_PREFIX(0, prefix)) == \
864 	(ntohl((addr2)->_S6_un._S6_u32[0]) & \
865 	IN6_MASK_FROM_PREFIX(0, prefix))) && \
866 	((ntohl((addr1)->_S6_un._S6_u32[1]) & \
867 	IN6_MASK_FROM_PREFIX(1, prefix)) == \
868 	(ntohl((addr2)->_S6_un._S6_u32[1]) & \
869 	IN6_MASK_FROM_PREFIX(1, prefix))) && \
870 	((ntohl((addr1)->_S6_un._S6_u32[2]) & \
871 	IN6_MASK_FROM_PREFIX(2, prefix)) == \
872 	(ntohl((addr2)->_S6_un._S6_u32[2]) & \
873 	IN6_MASK_FROM_PREFIX(2, prefix))) && \
874 	((ntohl((addr1)->_S6_un._S6_u32[3]) & \
875 	IN6_MASK_FROM_PREFIX(3, prefix)) == \
876 	(ntohl((addr2)->_S6_un._S6_u32[3]) & \
877 	IN6_MASK_FROM_PREFIX(3, prefix))))
878 
879 #endif /* !defined(_XPG4_2) || defined(_XPG6) || defined(__EXTENSIONS__) */
880 
881 
882 /*
883  * Options for use with [gs]etsockopt at the IP level.
884  *
885  * Note: Some of the IP_ namespace has conflict with and
886  * and is exposed through <xti.h>. (It also requires exposing
887  * options not implemented). The options with potential
888  * for conflicts use #ifndef guards.
889  */
890 #ifndef IP_OPTIONS
891 #define	IP_OPTIONS	1	/* set/get IP per-packet options   */
892 #endif
893 
894 #define	IP_HDRINCL	2	/* int; header is included with data (raw) */
895 
896 #ifndef IP_TOS
897 #define	IP_TOS		3	/* int; IP type of service and precedence */
898 #endif
899 
900 #ifndef IP_TTL
901 #define	IP_TTL		4	/* int; IP time to live */
902 #endif
903 
904 #define	IP_RECVOPTS	0x5	/* int; receive all IP options w/datagram */
905 #define	IP_RECVRETOPTS	0x6	/* int; receive IP options for response */
906 #define	IP_RECVDSTADDR	0x7	/* int; receive IP dst addr w/datagram */
907 #define	IP_RETOPTS	0x8	/* ip_opts; set/get IP per-packet options */
908 #define	IP_RECVIF	0x9	/* int; receive the inbound interface index */
909 #define	IP_RECVSLLA	0xa	/* sockaddr_dl; get source link layer address */
910 #define	IP_RECVTTL	0xb	/* uint8_t; get TTL for inbound packet */
911 
912 #define	IP_MULTICAST_IF		0x10	/* set/get IP multicast interface  */
913 #define	IP_MULTICAST_TTL	0x11	/* set/get IP multicast timetolive */
914 #define	IP_MULTICAST_LOOP	0x12	/* set/get IP multicast loopback   */
915 #define	IP_ADD_MEMBERSHIP	0x13	/* add	an IP group membership	   */
916 #define	IP_DROP_MEMBERSHIP	0x14	/* drop an IP group membership	   */
917 #define	IP_BLOCK_SOURCE		0x15	/* block   mcast pkts from source  */
918 #define	IP_UNBLOCK_SOURCE	0x16	/* unblock mcast pkts from source  */
919 #define	IP_ADD_SOURCE_MEMBERSHIP  0x17	/* add  mcast group/source pair	   */
920 #define	IP_DROP_SOURCE_MEMBERSHIP 0x18	/* drop mcast group/source pair	   */
921 #define	IP_NEXTHOP		0x19	/* send directly to next hop	   */
922 /*
923  * IP_PKTINFO and IP_RECVPKTINFO have same value. Size of argument passed in
924  * is used to differentiate b/w the two.
925  */
926 #define	IP_PKTINFO		0x1a	/* specify src address and/or index */
927 #define	IP_RECVPKTINFO		0x1a	/* recv dest/matched addr and index */
928 #define	IP_DONTFRAG		0x1b	/* don't fragment packets */
929 
930 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
931 /*
932  * Different preferences that can be requested from IPSEC protocols.
933  */
934 #define	IP_SEC_OPT		0x22	/* Used to set IPSEC options */
935 #define	IPSEC_PREF_NEVER	0x01
936 #define	IPSEC_PREF_REQUIRED	0x02
937 #define	IPSEC_PREF_UNIQUE	0x04
938 /*
939  * This can be used with the setsockopt() call to set per socket security
940  * options. When the application uses per-socket API, we will reflect
941  * the request on both outbound and inbound packets.
942  */
943 
944 typedef struct ipsec_req {
945 	uint_t 		ipsr_ah_req;		/* AH request */
946 	uint_t 		ipsr_esp_req;		/* ESP request */
947 	uint_t		ipsr_self_encap_req;	/* Self-Encap request */
948 	uint8_t		ipsr_auth_alg;		/* Auth algs for AH */
949 	uint8_t		ipsr_esp_alg;		/* Encr algs for ESP */
950 	uint8_t		ipsr_esp_auth_alg;	/* Auth algs for ESP */
951 } ipsec_req_t;
952 
953 /*
954  * MCAST_* options are protocol-independent.  The actual definitions
955  * are with the v6 options below; this comment is here to note the
956  * namespace usage.
957  *
958  * #define	MCAST_JOIN_GROUP	0x29
959  * #define	MCAST_LEAVE_GROUP	0x2a
960  * #define	MCAST_BLOCK_SOURCE	0x2b
961  * #define	MCAST_UNBLOCK_SOURCE	0x2c
962  * #define	MCAST_JOIN_SOURCE_GROUP	0x2d
963  * #define	MCAST_LEAVE_SOURCE_GROUP 0x2e
964  */
965 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
966 
967 /*
968  * SunOS private (potentially not portable) IP_ option names
969  */
970 #define	IP_BOUND_IF		0x41	/* bind socket to an ifindex	   */
971 #define	IP_UNSPEC_SRC		0x42	/* use unspecified source address  */
972 #define	IP_BROADCAST_TTL	0x43	/* use specific TTL for broadcast  */
973 /* can be reused		0x44 */
974 #define	IP_DHCPINIT_IF		0x45	/* accept all unicast DHCP traffic */
975 
976 /*
977  * Option values and names (when !_XPG5) shared with <xti_inet.h>
978  */
979 #ifndef IP_REUSEADDR
980 #define	IP_REUSEADDR		0x104
981 #endif
982 
983 #ifndef IP_DONTROUTE
984 #define	IP_DONTROUTE		0x105
985 #endif
986 
987 #ifndef IP_BROADCAST
988 #define	IP_BROADCAST		0x106
989 #endif
990 
991 /*
992  * The following option values are reserved by <xti_inet.h>
993  *
994  * T_IP_OPTIONS	0x107	 -  IP per-packet options
995  * T_IP_TOS	0x108	 -  IP per packet type of service
996  */
997 
998 /*
999  * Default value constants for multicast attributes controlled by
1000  * IP*_MULTICAST_LOOP and IP*_MULTICAST_{TTL,HOPS} options.
1001  */
1002 #define	IP_DEFAULT_MULTICAST_TTL  1	/* normally limit m'casts to 1 hop */
1003 #define	IP_DEFAULT_MULTICAST_LOOP 1	/* normally hear sends if a member */
1004 
1005 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
1006 /*
1007  * Argument structure for IP_ADD_MEMBERSHIP and IP_DROP_MEMBERSHIP.
1008  */
1009 struct ip_mreq {
1010 	struct in_addr	imr_multiaddr;	/* IP multicast address of group */
1011 	struct in_addr	imr_interface;	/* local IP address of interface */
1012 };
1013 
1014 /*
1015  * Argument structure for IP_BLOCK_SOURCE, IP_UNBLOCK_SOURCE,
1016  * IP_ADD_SOURCE_MEMBERSHIP, and IP_DROP_SOURCE_MEMBERSHIP.
1017  */
1018 struct ip_mreq_source {
1019 	struct in_addr	imr_multiaddr;	/* IP address of group */
1020 	struct in_addr	imr_sourceaddr;	/* IP address of source */
1021 	struct in_addr	imr_interface;	/* IP address of interface */
1022 };
1023 
1024 /*
1025  * Argument structure for IPV6_JOIN_GROUP and IPV6_LEAVE_GROUP on
1026  * IPv6 addresses.
1027  */
1028 struct ipv6_mreq {
1029 	struct in6_addr	ipv6mr_multiaddr;	/* IPv6 multicast addr */
1030 	unsigned int	ipv6mr_interface;	/* interface index */
1031 };
1032 
1033 /*
1034  * Use #pragma pack() construct to force 32-bit alignment on amd64.
1035  * This is needed to keep the structure size and offsets consistent
1036  * between a 32-bit app and the 64-bit amd64 kernel in structures
1037  * where 64-bit alignment would create gaps (in this case, structures
1038  * which have a uint32_t followed by a struct sockaddr_storage).
1039  */
1040 #if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
1041 #pragma pack(4)
1042 #endif
1043 
1044 /*
1045  * Argument structure for MCAST_JOIN_GROUP and MCAST_LEAVE_GROUP.
1046  */
1047 struct group_req {
1048 	uint32_t		gr_interface;	/* interface index */
1049 	struct sockaddr_storage	gr_group;	/* group address */
1050 };
1051 
1052 /*
1053  * Argument structure for MCAST_BLOCK_SOURCE, MCAST_UNBLOCK_SOURCE,
1054  * MCAST_JOIN_SOURCE_GROUP, MCAST_LEAVE_SOURCE_GROUP.
1055  */
1056 struct group_source_req {
1057 	uint32_t		gsr_interface;	/* interface index */
1058 	struct sockaddr_storage	gsr_group;	/* group address */
1059 	struct sockaddr_storage	gsr_source;	/* source address */
1060 };
1061 
1062 /*
1063  * Argument for SIOC[GS]MSFILTER ioctls
1064  */
1065 struct group_filter {
1066 	uint32_t		gf_interface;	/* interface index */
1067 	struct sockaddr_storage	gf_group;	/* multicast address */
1068 	uint32_t		gf_fmode;	/* filter mode */
1069 	uint32_t		gf_numsrc;	/* number of sources */
1070 	struct sockaddr_storage	gf_slist[1];	/* source address */
1071 };
1072 
1073 #if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
1074 #pragma pack()
1075 #endif
1076 
1077 #define	GROUP_FILTER_SIZE(numsrc) \
1078 	(sizeof (struct group_filter) - sizeof (struct sockaddr_storage) \
1079 	+ (numsrc) * sizeof (struct sockaddr_storage))
1080 
1081 /*
1082  * Argument for SIOC[GS]IPMSFILTER ioctls (IPv4-specific)
1083  */
1084 struct ip_msfilter {
1085 	struct in_addr	imsf_multiaddr;	/* IP multicast address of group */
1086 	struct in_addr	imsf_interface;	/* local IP address of interface */
1087 	uint32_t	imsf_fmode;	/* filter mode */
1088 	uint32_t	imsf_numsrc;	/* number of sources in src_list */
1089 	struct in_addr	imsf_slist[1];	/* start of source list */
1090 };
1091 
1092 #define	IP_MSFILTER_SIZE(numsrc) \
1093 	(sizeof (struct ip_msfilter) - sizeof (struct in_addr) \
1094 	+ (numsrc) * sizeof (struct in_addr))
1095 
1096 /*
1097  * Multicast source filter manipulation functions in libsocket;
1098  * defined in RFC 3678.
1099  */
1100 int setsourcefilter(int, uint32_t, struct sockaddr *, socklen_t, uint32_t,
1101 			uint_t, struct sockaddr_storage *);
1102 
1103 int getsourcefilter(int, uint32_t, struct sockaddr *, socklen_t, uint32_t *,
1104 			uint_t *, struct sockaddr_storage *);
1105 
1106 int setipv4sourcefilter(int, struct in_addr, struct in_addr, uint32_t,
1107 			uint32_t, struct in_addr *);
1108 
1109 int getipv4sourcefilter(int, struct in_addr, struct in_addr, uint32_t *,
1110 			uint32_t *, struct in_addr *);
1111 
1112 /*
1113  * Definitions needed for [gs]etsourcefilter(), [gs]etipv4sourcefilter()
1114  */
1115 #define	MCAST_INCLUDE	1
1116 #define	MCAST_EXCLUDE	2
1117 
1118 /*
1119  * Argument struct for IP_PKTINFO option
1120  */
1121 typedef struct in_pktinfo {
1122 	unsigned int		ipi_ifindex;	/* send/recv interface index */
1123 	struct in_addr		ipi_spec_dst;	/* matched source address */
1124 	struct in_addr		ipi_addr;	/* src/dst address in IP hdr */
1125 } in_pktinfo_t;
1126 
1127 /*
1128  * Argument struct for IPV6_PKTINFO option
1129  */
1130 struct in6_pktinfo {
1131 	struct in6_addr		ipi6_addr;	/* src/dst IPv6 address */
1132 	unsigned int		ipi6_ifindex;	/* send/recv interface index */
1133 };
1134 
1135 /*
1136  * Argument struct for IPV6_MTUINFO option
1137  */
1138 struct ip6_mtuinfo {
1139 	struct sockaddr_in6	ip6m_addr; /* dst address including zone ID */
1140 	uint32_t		ip6m_mtu;  /* path MTU in host byte order */
1141 };
1142 
1143 /*
1144  * IPv6 routing header types
1145  */
1146 #define	IPV6_RTHDR_TYPE_0	0
1147 
1148 extern socklen_t inet6_rth_space(int type, int segments);
1149 extern void *inet6_rth_init(void *bp, socklen_t bp_len, int type, int segments);
1150 extern int inet6_rth_add(void *bp, const struct in6_addr *addr);
1151 extern int inet6_rth_reverse(const void *in, void *out);
1152 extern int inet6_rth_segments(const void *bp);
1153 extern struct in6_addr *inet6_rth_getaddr(const void *bp, int index);
1154 
1155 extern int inet6_opt_init(void *extbuf, socklen_t extlen);
1156 extern int inet6_opt_append(void *extbuf, socklen_t extlen, int offset,
1157 	uint8_t type, socklen_t len, uint_t align, void **databufp);
1158 extern int inet6_opt_finish(void *extbuf, socklen_t extlen, int offset);
1159 extern int inet6_opt_set_val(void *databuf, int offset, void *val,
1160 	socklen_t vallen);
1161 extern int inet6_opt_next(void *extbuf, socklen_t extlen, int offset,
1162 	uint8_t *typep, socklen_t *lenp, void **databufp);
1163 extern int inet6_opt_find(void *extbufp, socklen_t extlen, int offset,
1164 	uint8_t type, socklen_t *lenp, void **databufp);
1165 extern int inet6_opt_get_val(void *databuf, int offset, void *val,
1166 	socklen_t vallen);
1167 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
1168 
1169 /*
1170  * Argument structure for IP_ADD_PROXY_ADDR.
1171  * Note that this is an unstable, experimental interface. It may change
1172  * later. Don't use it unless you know what it is.
1173  */
1174 typedef struct {
1175 	struct in_addr	in_prefix_addr;
1176 	unsigned int	in_prefix_len;
1177 } in_prefix_t;
1178 
1179 
1180 #if !defined(_XPG4_2) || defined(_XPG6) || defined(__EXTENSIONS__)
1181 /*
1182  * IPv6 options
1183  */
1184 #define	IPV6_UNICAST_HOPS	0x5	/* hop limit value for unicast */
1185 					/* packets. */
1186 					/* argument type: uint_t */
1187 #define	IPV6_MULTICAST_IF	0x6	/* outgoing interface for */
1188 					/* multicast packets. */
1189 					/* argument type: struct in6_addr */
1190 #define	IPV6_MULTICAST_HOPS	0x7	/* hop limit value to use for */
1191 					/* multicast packets. */
1192 					/* argument type: uint_t */
1193 #define	IPV6_MULTICAST_LOOP	0x8	/* enable/disable delivery of */
1194 					/* multicast packets on same socket. */
1195 					/* argument type: uint_t */
1196 #define	IPV6_JOIN_GROUP		0x9	/* join an IPv6 multicast group. */
1197 					/* argument type: struct ipv6_mreq */
1198 #define	IPV6_LEAVE_GROUP	0xa	/* leave an IPv6 multicast group */
1199 					/* argument type: struct ipv6_mreq */
1200 
1201 /*
1202  * Other XPG6 constants.
1203  */
1204 #define	INET_ADDRSTRLEN		16	/* max len IPv4 addr in ascii dotted */
1205 					/* decimal notation. */
1206 #define	INET6_ADDRSTRLEN	46	/* max len of IPv6 addr in ascii */
1207 					/* standard colon-hex notation. */
1208 
1209 #endif /* !defined(_XPG4_2) || defined(_XPG6) || defined(__EXTENSIONS__) */
1210 
1211 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
1212 
1213 /*
1214  * IPV6_ADD_MEMBERSHIP and IPV6_DROP_MEMBERSHIP are being kept
1215  * for backward compatibility. They have the same meaning as IPV6_JOIN_GROUP
1216  * and IPV6_LEAVE_GROUP respectively.
1217  */
1218 #define	IPV6_ADD_MEMBERSHIP	0x9	/* join an IPv6 multicast group. */
1219 					/* argument type: struct ipv6_mreq */
1220 #define	IPV6_DROP_MEMBERSHIP	0xa	/* leave an IPv6 multicast group */
1221 					/* argument type: struct ipv6_mreq */
1222 
1223 #define	IPV6_PKTINFO		0xb	/* addr plus interface index */
1224 					/* arg type: "struct in6_pktingo" - */
1225 #define	IPV6_HOPLIMIT		0xc	/* hoplimit for datagram */
1226 #define	IPV6_NEXTHOP		0xd	/* next hop address  */
1227 #define	IPV6_HOPOPTS		0xe	/* hop by hop options */
1228 #define	IPV6_DSTOPTS		0xf	/* destination options - after */
1229 					/* the routing header */
1230 #define	IPV6_RTHDR		0x10	/* routing header  */
1231 #define	IPV6_RTHDRDSTOPTS	0x11	/* destination options - before */
1232 					/* the routing header */
1233 #define	IPV6_RECVPKTINFO	0x12	/* enable/disable IPV6_PKTINFO */
1234 #define	IPV6_RECVHOPLIMIT	0x13	/* enable/disable IPV6_HOPLIMIT */
1235 #define	IPV6_RECVHOPOPTS	0x14	/* enable/disable IPV6_HOPOPTS */
1236 
1237 /*
1238  * This options exists for backwards compatability and should no longer be
1239  * used.  Use IPV6_RECVDSTOPTS instead.
1240  */
1241 #define	_OLD_IPV6_RECVDSTOPTS	0x15
1242 
1243 #define	IPV6_RECVRTHDR		0x16	/* enable/disable IPV6_RTHDR */
1244 
1245 /*
1246  * enable/disable IPV6_RTHDRDSTOPTS.  Now obsolete.  IPV6_RECVDSTOPTS enables
1247  * the receipt of both headers.
1248  */
1249 #define	IPV6_RECVRTHDRDSTOPTS	0x17
1250 
1251 #define	IPV6_CHECKSUM		0x18	/* Control checksum on raw sockets */
1252 #define	IPV6_RECVTCLASS		0x19	/* enable/disable IPV6_CLASS */
1253 #define	IPV6_USE_MIN_MTU	0x20	/* send packets with minimum MTU */
1254 #define	IPV6_DONTFRAG		0x21	/* don't fragment packets */
1255 #define	IPV6_SEC_OPT		0x22	/* Used to set IPSEC options */
1256 #define	IPV6_SRC_PREFERENCES	0x23	/* Control socket's src addr select */
1257 #define	IPV6_RECVPATHMTU	0x24	/* receive PMTU info */
1258 #define	IPV6_PATHMTU		0x25	/* get the PMTU */
1259 #define	IPV6_TCLASS		0x26	/* traffic class */
1260 #define	IPV6_V6ONLY		0x27	/* v6 only socket option */
1261 
1262 /*
1263  * enable/disable receipt of both both IPV6_DSTOPTS headers.
1264  */
1265 #define	IPV6_RECVDSTOPTS	0x28
1266 
1267 /*
1268  * protocol-independent multicast membership options.
1269  */
1270 #define	MCAST_JOIN_GROUP	0x29	/* join group for all sources */
1271 #define	MCAST_LEAVE_GROUP	0x2a	/* leave group */
1272 #define	MCAST_BLOCK_SOURCE	0x2b	/* block specified source */
1273 #define	MCAST_UNBLOCK_SOURCE	0x2c	/* unblock specified source */
1274 #define	MCAST_JOIN_SOURCE_GROUP	0x2d	/* join group for specified source */
1275 #define	MCAST_LEAVE_SOURCE_GROUP 0x2e	/* leave source/group pair */
1276 
1277 /* 32Bit field for IPV6_SRC_PREFERENCES */
1278 #define	IPV6_PREFER_SRC_HOME		0x00000001
1279 #define	IPV6_PREFER_SRC_COA		0x00000002
1280 #define	IPV6_PREFER_SRC_PUBLIC		0x00000004
1281 #define	IPV6_PREFER_SRC_TMP		0x00000008
1282 #define	IPV6_PREFER_SRC_NONCGA		0x00000010
1283 #define	IPV6_PREFER_SRC_CGA		0x00000020
1284 
1285 #define	IPV6_PREFER_SRC_MIPMASK	(IPV6_PREFER_SRC_HOME | IPV6_PREFER_SRC_COA)
1286 #define	IPV6_PREFER_SRC_MIPDEFAULT	IPV6_PREFER_SRC_HOME
1287 #define	IPV6_PREFER_SRC_TMPMASK	(IPV6_PREFER_SRC_PUBLIC | IPV6_PREFER_SRC_TMP)
1288 #define	IPV6_PREFER_SRC_TMPDEFAULT	IPV6_PREFER_SRC_PUBLIC
1289 #define	IPV6_PREFER_SRC_CGAMASK	(IPV6_PREFER_SRC_NONCGA | IPV6_PREFER_SRC_CGA)
1290 #define	IPV6_PREFER_SRC_CGADEFAULT	IPV6_PREFER_SRC_NONCGA
1291 
1292 #define	IPV6_PREFER_SRC_MASK (IPV6_PREFER_SRC_MIPMASK |\
1293 	IPV6_PREFER_SRC_TMPMASK | IPV6_PREFER_SRC_CGAMASK)
1294 
1295 #define	IPV6_PREFER_SRC_DEFAULT	(IPV6_PREFER_SRC_MIPDEFAULT |\
1296 	IPV6_PREFER_SRC_TMPDEFAULT | IPV6_PREFER_SRC_CGADEFAULT)
1297 
1298 /*
1299  * SunOS private (potentially not portable) IPV6_ option names
1300  */
1301 #define	IPV6_BOUND_IF		0x41	/* bind to an ifindex */
1302 #define	IPV6_UNSPEC_SRC		0x42	/* source of packets set to */
1303 					/* unspecified (all zeros) */
1304 
1305 /*
1306  * Miscellaneous IPv6 constants.
1307  */
1308 #define	IPV6_PAD1_OPT		0	/* pad byte in IPv6 extension hdrs */
1309 
1310 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
1311 
1312 /*
1313  * Extern declarations for pre-defined global const variables
1314  */
1315 #if !defined(_XPG4_2) || defined(_XPG6) || defined(__EXTENSIONS__)
1316 #ifndef _KERNEL
1317 #ifdef __STDC__
1318 extern const struct in6_addr in6addr_any;
1319 extern const struct in6_addr in6addr_loopback;
1320 #else
1321 extern struct in6_addr in6addr_any;
1322 extern struct in6_addr in6addr_loopback;
1323 #endif
1324 #endif
1325 #endif /* !defined(_XPG4_2) || defined(_XPG6) || defined(__EXTENSIONS__) */
1326 
1327 #ifdef	__cplusplus
1328 }
1329 #endif
1330 
1331 #endif	/* _NETINET_IN_H */
1332