in6.h revision 171260
162587Sitojun/*	$FreeBSD: head/sys/netinet6/in6.h 171260 2007-07-05 16:29:40Z delphij $	*/
278064Sume/*	$KAME: in6.h,v 1.89 2001/05/27 13:28:35 itojun Exp $	*/
362587Sitojun
4139826Simp/*-
552904Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
652904Sshin * All rights reserved.
753541Sshin *
852904Sshin * Redistribution and use in source and binary forms, with or without
952904Sshin * modification, are permitted provided that the following conditions
1052904Sshin * are met:
1152904Sshin * 1. Redistributions of source code must retain the above copyright
1252904Sshin *    notice, this list of conditions and the following disclaimer.
1352904Sshin * 2. Redistributions in binary form must reproduce the above copyright
1452904Sshin *    notice, this list of conditions and the following disclaimer in the
1552904Sshin *    documentation and/or other materials provided with the distribution.
1652904Sshin * 3. Neither the name of the project nor the names of its contributors
1752904Sshin *    may be used to endorse or promote products derived from this software
1852904Sshin *    without specific prior written permission.
1953541Sshin *
2052904Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2152904Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2252904Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2352904Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2452904Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2552904Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2652904Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2752904Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2852904Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2952904Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3052904Sshin * SUCH DAMAGE.
3152904Sshin */
3252904Sshin
33139826Simp/*-
3452904Sshin * Copyright (c) 1982, 1986, 1990, 1993
3552904Sshin *	The Regents of the University of California.  All rights reserved.
3652904Sshin *
3752904Sshin * Redistribution and use in source and binary forms, with or without
3852904Sshin * modification, are permitted provided that the following conditions
3952904Sshin * are met:
4052904Sshin * 1. Redistributions of source code must retain the above copyright
4152904Sshin *    notice, this list of conditions and the following disclaimer.
4252904Sshin * 2. Redistributions in binary form must reproduce the above copyright
4352904Sshin *    notice, this list of conditions and the following disclaimer in the
4452904Sshin *    documentation and/or other materials provided with the distribution.
4552904Sshin * 4. Neither the name of the University nor the names of its contributors
4652904Sshin *    may be used to endorse or promote products derived from this software
4752904Sshin *    without specific prior written permission.
4852904Sshin *
4952904Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5052904Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5152904Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5252904Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5352904Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5452904Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5552904Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5652904Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5752904Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5852904Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5952904Sshin * SUCH DAMAGE.
6052904Sshin *
6152904Sshin *	@(#)in.h	8.3 (Berkeley) 1/3/94
6252904Sshin */
6352904Sshin
6462587Sitojun#ifndef __KAME_NETINET_IN_H_INCLUDED_
6578064Sume#error "do not include netinet6/in6.h directly, include netinet/in.h.  see RFC2553"
6657120Sshin#endif
6757120Sshin
6862587Sitojun#ifndef _NETINET6_IN6_H_
6962587Sitojun#define _NETINET6_IN6_H_
7062587Sitojun
7152904Sshin/*
7252904Sshin * Identification of the network protocol stack
7378064Sume * for *BSD-current/release: http://www.kame.net/dev/cvsweb.cgi/kame/COVERAGE
7478064Sume * has the table of implementation/integration differences.
7552904Sshin */
7662587Sitojun#define __KAME__
77156865Ssuz#define __KAME_VERSION		"FreeBSD"
7852904Sshin
7952904Sshin/*
80157209Sdwmalone * IPv6 port allocation rules should mirror the IPv4 rules and are controlled
81157209Sdwmalone * by the the net.inet.ip.portrange sysctl tree. The following defines exist
82157209Sdwmalone * for compatibility with userland applications that need them.
8352904Sshin */
8497181Smike#if __BSD_VISIBLE
8552904Sshin#define	IPV6PORT_RESERVED	1024
8652904Sshin#define	IPV6PORT_ANONMIN	49152
8752904Sshin#define	IPV6PORT_ANONMAX	65535
8852904Sshin#define	IPV6PORT_RESERVEDMIN	600
8952904Sshin#define	IPV6PORT_RESERVEDMAX	(IPV6PORT_RESERVED-1)
9097181Smike#endif
9152904Sshin
9252904Sshin/*
9352904Sshin * IPv6 address
9452904Sshin */
9552904Sshinstruct in6_addr {
9652904Sshin	union {
9797181Smike		uint8_t		__u6_addr8[16];
9897181Smike		uint16_t	__u6_addr16[8];
9997181Smike		uint32_t	__u6_addr32[4];
10052904Sshin	} __u6_addr;			/* 128-bit IP6 address */
10152904Sshin};
10252904Sshin
10362587Sitojun#define s6_addr   __u6_addr.__u6_addr8
10495023Ssuz#ifdef _KERNEL	/* XXX nonstandard */
10562587Sitojun#define s6_addr8  __u6_addr.__u6_addr8
10662587Sitojun#define s6_addr16 __u6_addr.__u6_addr16
10762587Sitojun#define s6_addr32 __u6_addr.__u6_addr32
10852904Sshin#endif
10952904Sshin
11062587Sitojun#define INET6_ADDRSTRLEN	46
11152904Sshin
11252904Sshin/*
11397181Smike * XXX missing POSIX.1-2001 macro IPPROTO_IPV6.
11497181Smike */
11597181Smike
11697181Smike/*
11752904Sshin * Socket address for IPv6
11852904Sshin */
11997181Smike#if __BSD_VISIBLE
12062587Sitojun#define SIN6_LEN
12152904Sshin#endif
12297181Smike
12352904Sshinstruct sockaddr_in6 {
124100503Sume	uint8_t		sin6_len;	/* length of this struct */
12597181Smike	sa_family_t	sin6_family;	/* AF_INET6 */
12697181Smike	in_port_t	sin6_port;	/* Transport layer port # */
12797181Smike	uint32_t	sin6_flowinfo;	/* IP6 flow information */
12862587Sitojun	struct in6_addr	sin6_addr;	/* IP6 address */
12997181Smike	uint32_t	sin6_scope_id;	/* scope zone index */
13052904Sshin};
13152904Sshin
13252904Sshin/*
13352904Sshin * Local definition for masks
13452904Sshin */
13595023Ssuz#ifdef _KERNEL	/* XXX nonstandard */
13662587Sitojun#define IN6MASK0	{{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
13762587Sitojun#define IN6MASK32	{{{ 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, \
13852904Sshin			    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }}}
13962587Sitojun#define IN6MASK64	{{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
14052904Sshin			    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }}}
14162587Sitojun#define IN6MASK96	{{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
14252904Sshin			    0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }}}
14362587Sitojun#define IN6MASK128	{{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
14452904Sshin			    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
14552904Sshin#endif
14652904Sshin
14752904Sshin#ifdef _KERNEL
14878064Sumeextern const struct sockaddr_in6 sa6_any;
14978064Sume
15052904Sshinextern const struct in6_addr in6mask0;
15152904Sshinextern const struct in6_addr in6mask32;
15252904Sshinextern const struct in6_addr in6mask64;
15352904Sshinextern const struct in6_addr in6mask96;
15452904Sshinextern const struct in6_addr in6mask128;
15552904Sshin#endif /* _KERNEL */
15652904Sshin
15752904Sshin/*
15852904Sshin * Macros started with IPV6_ADDR is KAME local
15952904Sshin */
16095023Ssuz#ifdef _KERNEL	/* XXX nonstandard */
16197181Smike#if _BYTE_ORDER == _BIG_ENDIAN
16262587Sitojun#define IPV6_ADDR_INT32_ONE	1
16362587Sitojun#define IPV6_ADDR_INT32_TWO	2
16462587Sitojun#define IPV6_ADDR_INT32_MNL	0xff010000
16562587Sitojun#define IPV6_ADDR_INT32_MLL	0xff020000
16662587Sitojun#define IPV6_ADDR_INT32_SMP	0x0000ffff
16762587Sitojun#define IPV6_ADDR_INT16_ULL	0xfe80
16862587Sitojun#define IPV6_ADDR_INT16_USL	0xfec0
16962587Sitojun#define IPV6_ADDR_INT16_MLL	0xff02
17097181Smike#elif _BYTE_ORDER == _LITTLE_ENDIAN
17162587Sitojun#define IPV6_ADDR_INT32_ONE	0x01000000
17262587Sitojun#define IPV6_ADDR_INT32_TWO	0x02000000
17362587Sitojun#define IPV6_ADDR_INT32_MNL	0x000001ff
17462587Sitojun#define IPV6_ADDR_INT32_MLL	0x000002ff
17562587Sitojun#define IPV6_ADDR_INT32_SMP	0xffff0000
17662587Sitojun#define IPV6_ADDR_INT16_ULL	0x80fe
17762587Sitojun#define IPV6_ADDR_INT16_USL	0xc0fe
17862587Sitojun#define IPV6_ADDR_INT16_MLL	0x02ff
17952904Sshin#endif
18052904Sshin#endif
18152904Sshin
18252904Sshin/*
18352904Sshin * Definition of some useful macros to handle IP6 addresses
18452904Sshin */
18597181Smike#if __BSD_VISIBLE
18662587Sitojun#define IN6ADDR_ANY_INIT \
18752904Sshin	{{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
18852904Sshin	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }}}
18962587Sitojun#define IN6ADDR_LOOPBACK_INIT \
19052904Sshin	{{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
19152904Sshin	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }}}
19262587Sitojun#define IN6ADDR_NODELOCAL_ALLNODES_INIT \
19352904Sshin	{{{ 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
19452904Sshin	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }}}
195121315Sume#define IN6ADDR_INTFACELOCAL_ALLNODES_INIT \
196121315Sume	{{{ 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
197121315Sume	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }}}
19862587Sitojun#define IN6ADDR_LINKLOCAL_ALLNODES_INIT \
19952904Sshin	{{{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
20052904Sshin	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }}}
20162587Sitojun#define IN6ADDR_LINKLOCAL_ALLROUTERS_INIT \
20252904Sshin	{{{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
20352904Sshin	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }}}
20497181Smike#endif
20552904Sshin
20652904Sshinextern const struct in6_addr in6addr_any;
20752904Sshinextern const struct in6_addr in6addr_loopback;
20897181Smike#if __BSD_VISIBLE
20952904Sshinextern const struct in6_addr in6addr_nodelocal_allnodes;
21052904Sshinextern const struct in6_addr in6addr_linklocal_allnodes;
21152904Sshinextern const struct in6_addr in6addr_linklocal_allrouters;
21297181Smike#endif
21352904Sshin
21452904Sshin/*
21552904Sshin * Equality
21653877Sitojun * NOTE: Some of kernel programming environment (for example, openbsd/sparc)
21753877Sitojun * does not supply memcmp().  For userland memcmp() is preferred as it is
21853877Sitojun * in ANSI standard.
21952904Sshin */
22053877Sitojun#ifdef _KERNEL
22162587Sitojun#define IN6_ARE_ADDR_EQUAL(a, b)			\
22262587Sitojun    (bcmp(&(a)->s6_addr[0], &(b)->s6_addr[0], sizeof(struct in6_addr)) == 0)
22353877Sitojun#else
22497181Smike#if __BSD_VISIBLE
22562587Sitojun#define IN6_ARE_ADDR_EQUAL(a, b)			\
22662587Sitojun    (memcmp(&(a)->s6_addr[0], &(b)->s6_addr[0], sizeof(struct in6_addr)) == 0)
22753877Sitojun#endif
22897181Smike#endif
22952904Sshin
23052904Sshin/*
23152904Sshin * Unspecified
23252904Sshin */
23362587Sitojun#define IN6_IS_ADDR_UNSPECIFIED(a)	\
23478064Sume	((*(const u_int32_t *)(const void *)(&(a)->s6_addr[0]) == 0) &&	\
23578064Sume	 (*(const u_int32_t *)(const void *)(&(a)->s6_addr[4]) == 0) &&	\
23678064Sume	 (*(const u_int32_t *)(const void *)(&(a)->s6_addr[8]) == 0) &&	\
23778064Sume	 (*(const u_int32_t *)(const void *)(&(a)->s6_addr[12]) == 0))
23852904Sshin
23952904Sshin/*
24052904Sshin * Loopback
24152904Sshin */
24262587Sitojun#define IN6_IS_ADDR_LOOPBACK(a)		\
24378064Sume	((*(const u_int32_t *)(const void *)(&(a)->s6_addr[0]) == 0) &&	\
24478064Sume	 (*(const u_int32_t *)(const void *)(&(a)->s6_addr[4]) == 0) &&	\
24578064Sume	 (*(const u_int32_t *)(const void *)(&(a)->s6_addr[8]) == 0) &&	\
24678064Sume	 (*(const u_int32_t *)(const void *)(&(a)->s6_addr[12]) == ntohl(1)))
24752904Sshin
24852904Sshin/*
24952904Sshin * IPv4 compatible
25052904Sshin */
25162587Sitojun#define IN6_IS_ADDR_V4COMPAT(a)		\
25278064Sume	((*(const u_int32_t *)(const void *)(&(a)->s6_addr[0]) == 0) &&	\
25378064Sume	 (*(const u_int32_t *)(const void *)(&(a)->s6_addr[4]) == 0) &&	\
25478064Sume	 (*(const u_int32_t *)(const void *)(&(a)->s6_addr[8]) == 0) &&	\
25578064Sume	 (*(const u_int32_t *)(const void *)(&(a)->s6_addr[12]) != 0) &&	\
25678064Sume	 (*(const u_int32_t *)(const void *)(&(a)->s6_addr[12]) != ntohl(1)))
25752904Sshin
25852904Sshin/*
25952904Sshin * Mapped
26052904Sshin */
26162587Sitojun#define IN6_IS_ADDR_V4MAPPED(a)		      \
26278064Sume	((*(const u_int32_t *)(const void *)(&(a)->s6_addr[0]) == 0) &&	\
26378064Sume	 (*(const u_int32_t *)(const void *)(&(a)->s6_addr[4]) == 0) &&	\
26478064Sume	 (*(const u_int32_t *)(const void *)(&(a)->s6_addr[8]) == ntohl(0x0000ffff)))
26552904Sshin
26652904Sshin/*
26752904Sshin * KAME Scope Values
26852904Sshin */
26952904Sshin
27095023Ssuz#ifdef _KERNEL	/* XXX nonstandard */
27162587Sitojun#define IPV6_ADDR_SCOPE_NODELOCAL	0x01
272121315Sume#define IPV6_ADDR_SCOPE_INTFACELOCAL	0x01
27362587Sitojun#define IPV6_ADDR_SCOPE_LINKLOCAL	0x02
27462587Sitojun#define IPV6_ADDR_SCOPE_SITELOCAL	0x05
27562587Sitojun#define IPV6_ADDR_SCOPE_ORGLOCAL	0x08	/* just used in this file */
27662587Sitojun#define IPV6_ADDR_SCOPE_GLOBAL		0x0e
27752904Sshin#else
27862587Sitojun#define __IPV6_ADDR_SCOPE_NODELOCAL	0x01
279121315Sume#define __IPV6_ADDR_SCOPE_INTFACELOCAL	0x01
28062587Sitojun#define __IPV6_ADDR_SCOPE_LINKLOCAL	0x02
28162587Sitojun#define __IPV6_ADDR_SCOPE_SITELOCAL	0x05
28262587Sitojun#define __IPV6_ADDR_SCOPE_ORGLOCAL	0x08	/* just used in this file */
28362587Sitojun#define __IPV6_ADDR_SCOPE_GLOBAL	0x0e
28452904Sshin#endif
28552904Sshin
28652904Sshin/*
28752904Sshin * Unicast Scope
28852904Sshin * Note that we must check topmost 10 bits only, not 16 bits (see RFC2373).
28952904Sshin */
29062587Sitojun#define IN6_IS_ADDR_LINKLOCAL(a)	\
29152904Sshin	(((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0x80))
29262587Sitojun#define IN6_IS_ADDR_SITELOCAL(a)	\
29352904Sshin	(((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0xc0))
29452904Sshin
29552904Sshin/*
29652904Sshin * Multicast
29752904Sshin */
29852904Sshin#define IN6_IS_ADDR_MULTICAST(a)	((a)->s6_addr[0] == 0xff)
29952904Sshin
30095023Ssuz#ifdef _KERNEL	/* XXX nonstandard */
30162587Sitojun#define IPV6_ADDR_MC_SCOPE(a)		((a)->s6_addr[1] & 0x0f)
30252904Sshin#else
30362587Sitojun#define __IPV6_ADDR_MC_SCOPE(a)		((a)->s6_addr[1] & 0x0f)
30452904Sshin#endif
30552904Sshin
30652904Sshin/*
30752904Sshin * Multicast Scope
30852904Sshin */
30995023Ssuz#ifdef _KERNEL	/* refers nonstandard items */
31062587Sitojun#define IN6_IS_ADDR_MC_NODELOCAL(a)	\
31152904Sshin	(IN6_IS_ADDR_MULTICAST(a) &&	\
31252904Sshin	 (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_NODELOCAL))
313121315Sume#define IN6_IS_ADDR_MC_INTFACELOCAL(a)	\
314121315Sume	(IN6_IS_ADDR_MULTICAST(a) &&	\
315121315Sume	 (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_INTFACELOCAL))
31662587Sitojun#define IN6_IS_ADDR_MC_LINKLOCAL(a)	\
31752904Sshin	(IN6_IS_ADDR_MULTICAST(a) &&	\
31852904Sshin	 (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_LINKLOCAL))
31962587Sitojun#define IN6_IS_ADDR_MC_SITELOCAL(a)	\
320171260Sdelphij	(IN6_IS_ADDR_MULTICAST(a) &&	\
32152904Sshin	 (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_SITELOCAL))
32262587Sitojun#define IN6_IS_ADDR_MC_ORGLOCAL(a)	\
32352904Sshin	(IN6_IS_ADDR_MULTICAST(a) &&	\
32452904Sshin	 (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_ORGLOCAL))
32562587Sitojun#define IN6_IS_ADDR_MC_GLOBAL(a)	\
32652904Sshin	(IN6_IS_ADDR_MULTICAST(a) &&	\
32752904Sshin	 (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_GLOBAL))
32852904Sshin#else
32962587Sitojun#define IN6_IS_ADDR_MC_NODELOCAL(a)	\
33052904Sshin	(IN6_IS_ADDR_MULTICAST(a) &&	\
33152904Sshin	 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_NODELOCAL))
33262587Sitojun#define IN6_IS_ADDR_MC_LINKLOCAL(a)	\
33352904Sshin	(IN6_IS_ADDR_MULTICAST(a) &&	\
33452904Sshin	 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_LINKLOCAL))
33562587Sitojun#define IN6_IS_ADDR_MC_SITELOCAL(a)	\
336171260Sdelphij	(IN6_IS_ADDR_MULTICAST(a) &&	\
33752904Sshin	 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_SITELOCAL))
33862587Sitojun#define IN6_IS_ADDR_MC_ORGLOCAL(a)	\
33952904Sshin	(IN6_IS_ADDR_MULTICAST(a) &&	\
34052904Sshin	 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_ORGLOCAL))
34162587Sitojun#define IN6_IS_ADDR_MC_GLOBAL(a)	\
34252904Sshin	(IN6_IS_ADDR_MULTICAST(a) &&	\
34352904Sshin	 (__IPV6_ADDR_MC_SCOPE(a) == __IPV6_ADDR_SCOPE_GLOBAL))
34452904Sshin#endif
34552904Sshin
34695023Ssuz#ifdef _KERNEL	/* nonstandard */
34778064Sume/*
34852904Sshin * KAME Scope
34952904Sshin */
35062587Sitojun#define IN6_IS_SCOPE_LINKLOCAL(a)	\
35152904Sshin	((IN6_IS_ADDR_LINKLOCAL(a)) ||	\
35252904Sshin	 (IN6_IS_ADDR_MC_LINKLOCAL(a)))
353171173Smlaier#define	IN6_IS_SCOPE_EMBED(a)			\
354171173Smlaier	((IN6_IS_ADDR_LINKLOCAL(a)) ||		\
355171260Sdelphij	 (IN6_IS_ADDR_MC_LINKLOCAL(a)) ||	\
356171173Smlaier	 (IN6_IS_ADDR_MC_INTFACELOCAL(a)))
357171260Sdelphij
35878064Sume#define IFA6_IS_DEPRECATED(a) \
359151539Ssuz	((a)->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME && \
360151539Ssuz	 (u_int32_t)((time_second - (a)->ia6_updatetime)) > \
361151539Ssuz	 (a)->ia6_lifetime.ia6t_pltime)
36278064Sume#define IFA6_IS_INVALID(a) \
363151539Ssuz	((a)->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME && \
364151539Ssuz	 (u_int32_t)((time_second - (a)->ia6_updatetime)) > \
365151539Ssuz	 (a)->ia6_lifetime.ia6t_vltime)
36678064Sume#endif /* _KERNEL */
36778064Sume
36852904Sshin/*
36952904Sshin * IP6 route structure
37052904Sshin */
37197181Smike#if __BSD_VISIBLE
37252904Sshinstruct route_in6 {
37352904Sshin	struct	rtentry *ro_rt;
37452904Sshin	struct	sockaddr_in6 ro_dst;
37552904Sshin};
37652904Sshin#endif
37752904Sshin
37852904Sshin/*
37952904Sshin * Options for use with [gs]etsockopt at the IPV6 level.
38052904Sshin * First word of comment is data type; bool is stored in int.
38152904Sshin */
38252904Sshin/* no hdrincl */
38362587Sitojun#if 0 /* the followings are relic in IPv4 and hence are disabled */
38462587Sitojun#define IPV6_OPTIONS		1  /* buf/ip6_opts; set/get IP6 options */
38562587Sitojun#define IPV6_RECVOPTS		5  /* bool; receive all IP6 opts w/dgram */
38662587Sitojun#define IPV6_RECVRETOPTS	6  /* bool; receive IP6 opts for response */
38762587Sitojun#define IPV6_RECVDSTADDR	7  /* bool; receive IP6 dst addr w/dgram */
38862587Sitojun#define IPV6_RETOPTS		8  /* ip6_opts; set/get IP6 options */
38962587Sitojun#endif
39062587Sitojun#define IPV6_SOCKOPT_RESERVED1	3  /* reserved for future use */
391121472Sume#define IPV6_UNICAST_HOPS	4  /* int; IP6 hops */
392121472Sume#define IPV6_MULTICAST_IF	9  /* u_int; set/get IP6 multicast i/f  */
393121472Sume#define IPV6_MULTICAST_HOPS	10 /* int; set/get IP6 multicast hops */
394121472Sume#define IPV6_MULTICAST_LOOP	11 /* u_int; set/get IP6 multicast loopback */
395121472Sume#define IPV6_JOIN_GROUP		12 /* ip6_mreq; join a group membership */
396121472Sume#define IPV6_LEAVE_GROUP	13 /* ip6_mreq; leave a group membership */
39762587Sitojun#define IPV6_PORTRANGE		14 /* int; range to choose for unspec port */
39862587Sitojun#define ICMP6_FILTER		18 /* icmp6_filter; icmp6 filter */
39978064Sume/* RFC2292 options */
400121472Sume#ifdef _KERNEL
401121472Sume#define IPV6_2292PKTINFO	19 /* bool; send/recv if, src/dst addr */
402121472Sume#define IPV6_2292HOPLIMIT	20 /* bool; hop limit */
403121472Sume#define IPV6_2292NEXTHOP	21 /* bool; next hop addr */
404121472Sume#define IPV6_2292HOPOPTS	22 /* bool; hop-by-hop option */
405121472Sume#define IPV6_2292DSTOPTS	23 /* bool; destinaion option */
406121472Sume#define IPV6_2292RTHDR		24 /* bool; routing header */
407121472Sume#define IPV6_2292PKTOPTIONS	25 /* buf/cmsghdr; set/get IPv6 options */
408121472Sume#endif
40978064Sume
41062587Sitojun#define IPV6_CHECKSUM		26 /* int; checksum offset for raw socket */
411121472Sume#define IPV6_V6ONLY		27 /* bool; make AF_INET6 sockets v6 only */
41278064Sume#ifndef _KERNEL
41378064Sume#define IPV6_BINDV6ONLY		IPV6_V6ONLY
41478064Sume#endif
41552904Sshin
416171260Sdelphij#ifdef IPSEC
41762587Sitojun#define IPV6_IPSEC_POLICY	28 /* struct; get/set security policy */
418171167Sgnn#endif /* IPSEC */
419171133Sgnn
42062587Sitojun#define IPV6_FAITH		29 /* bool; accept FAITH'ed connections */
42152904Sshin
42295023Ssuz#if 1 /* IPV6FIREWALL */
42362587Sitojun#define IPV6_FW_ADD		30 /* add a firewall rule to chain */
42462587Sitojun#define IPV6_FW_DEL		31 /* delete a firewall rule from chain */
42562587Sitojun#define IPV6_FW_FLUSH		32 /* flush firewall rule chain */
42662587Sitojun#define IPV6_FW_ZERO		33 /* clear single/all firewall counter(s) */
42762587Sitojun#define IPV6_FW_GET		34 /* get entire firewall rule chain */
42878064Sume#endif
42978064Sume
430148169Sume/* new socket options introduced in RFC3542 */
431121472Sume#define IPV6_RTHDRDSTOPTS	35 /* ip6_dest; send dst option before rthdr */
432121472Sume
433121472Sume#define IPV6_RECVPKTINFO	36 /* bool; recv if, dst addr */
434121472Sume#define IPV6_RECVHOPLIMIT	37 /* bool; recv hop limit */
435121472Sume#define IPV6_RECVRTHDR		38 /* bool; recv routing header */
436121472Sume#define IPV6_RECVHOPOPTS	39 /* bool; recv hop-by-hop option */
437121472Sume#define IPV6_RECVDSTOPTS	40 /* bool; recv dst option after rthdr */
438121472Sume#ifdef _KERNEL
439121472Sume#define IPV6_RECVRTHDRDSTOPTS	41 /* bool; recv dst option before rthdr */
440121472Sume#endif
441121472Sume
442121472Sume#define IPV6_USE_MIN_MTU	42 /* bool; send packets at the minimum MTU */
443121472Sume#define IPV6_RECVPATHMTU	43 /* bool; notify an according MTU */
444121472Sume
445121472Sume#define IPV6_PATHMTU		44 /* mtuinfo; get the current path MTU (sopt),
446121472Sume				      4 bytes int; MTU notification (cmsg) */
447121472Sume#if 0 /*obsoleted during 2292bis -> 3542*/
448121472Sume#define IPV6_REACHCONF		45 /* no data; ND reachability confirm
449121472Sume				      (cmsg only/not in of RFC3542) */
450121472Sume#endif
451121472Sume
452148169Sume/* more new socket options introduced in RFC3542 */
453121472Sume#define IPV6_PKTINFO		46 /* in6_pktinfo; send if, src addr */
454121472Sume#define IPV6_HOPLIMIT		47 /* int; send hop limit */
455121472Sume#define IPV6_NEXTHOP		48 /* sockaddr; next hop addr */
456121472Sume#define IPV6_HOPOPTS		49 /* ip6_hbh; send hop-by-hop option */
457121472Sume#define IPV6_DSTOPTS		50 /* ip6_dest; send dst option befor rthdr */
458121472Sume#define IPV6_RTHDR		51 /* ip6_rthdr; send routing header */
459121472Sume#if 0
460121472Sume#define IPV6_PKTOPTIONS		52 /* buf/cmsghdr; set/get IPv6 options */
461148169Sume				   /* obsoleted by RFC3542 */
462121472Sume#endif
463121472Sume
464121472Sume#define IPV6_RECVTCLASS		57 /* bool; recv traffic class values */
465121472Sume
466121472Sume#define IPV6_AUTOFLOWLABEL	59 /* bool; attach flowlabel automagically */
467121472Sume
468121472Sume#define IPV6_TCLASS		61 /* int; send traffic class value */
469121472Sume#define IPV6_DONTFRAG		62 /* bool; disable IPv6 fragmentation */
470121472Sume
471121472Sume#define IPV6_PREFER_TEMPADDR	63 /* int; prefer temporary addresses as
472121472Sume				    * the source address.
473121472Sume				    */
474121472Sume
475170613Sbms/*
476170613Sbms * The following option is private; do not use it from user applications.
477170613Sbms * It is deliberately defined to the same value as IP_MSFILTER.
478170613Sbms */
479170613Sbms#define	IPV6_MSFILTER		74 /* struct __msfilterreq;
480170613Sbms				    * set/get multicast source filter list.
481170613Sbms				    */
482170613Sbms
48365124Sitojun/* to define items, should talk with KAME guys first, for *BSD compatibility */
48452904Sshin
48562587Sitojun#define IPV6_RTHDR_LOOSE     0 /* this hop need not be a neighbor. XXX old spec */
48662587Sitojun#define IPV6_RTHDR_STRICT    1 /* this hop must be a neighbor. XXX old spec */
48762587Sitojun#define IPV6_RTHDR_TYPE_0    0 /* IPv6 routing header type 0 */
48852904Sshin
48952904Sshin/*
49052904Sshin * Defaults and limits for options
49152904Sshin */
49295023Ssuz#define IPV6_DEFAULT_MULTICAST_HOPS 1	/* normally limit m'casts to 1 hop */
49395023Ssuz#define IPV6_DEFAULT_MULTICAST_LOOP 1	/* normally hear sends if a member */
49452904Sshin
49552904Sshin/*
49652904Sshin * Argument structure for IPV6_JOIN_GROUP and IPV6_LEAVE_GROUP.
49752904Sshin */
49852904Sshinstruct ipv6_mreq {
49962587Sitojun	struct in6_addr	ipv6mr_multiaddr;
50062587Sitojun	unsigned int	ipv6mr_interface;
50152904Sshin};
50252904Sshin
503170613Sbms#ifdef notyet
50452904Sshin/*
505170613Sbms * Argument structure for IPV6_ADD_SOURCE_MEMBERSHIP,
506170613Sbms * IPV6_DROP_SOURCE_MEMBERSHIP, IPV6_BLOCK_SOURCE, and IPV6_UNBLOCK_SOURCE.
507170613Sbms */
508170613Sbmsstruct ipv6_mreq_source {
509170613Sbms	struct in6_addr	ipv6mr_multiaddr;
510170613Sbms	struct in6_addr	ipv6mr_sourceaddr;
511170613Sbms	uint32_t	ipv6mr_interface;
512170613Sbms};
513170613Sbms#endif
514170613Sbms
515170613Sbms/*
51652904Sshin * IPV6_PKTINFO: Packet information(RFC2292 sec 5)
51752904Sshin */
51852904Sshinstruct in6_pktinfo {
51962587Sitojun	struct in6_addr	ipi6_addr;	/* src/dst IPv6 address */
52062587Sitojun	unsigned int	ipi6_ifindex;	/* send/recv interface index */
52152904Sshin};
52252904Sshin
52352904Sshin/*
524121472Sume * Control structure for IPV6_RECVPATHMTU socket option.
525121472Sume */
526121472Sumestruct ip6_mtuinfo {
527121472Sume	struct sockaddr_in6 ip6m_addr;	/* or sockaddr_storage? */
528121569Sume	uint32_t ip6m_mtu;
529121472Sume};
530121472Sume
531121472Sume/*
53252904Sshin * Argument for IPV6_PORTRANGE:
53352904Sshin * - which range to search when port is unspecified at bind() or connect()
53452904Sshin */
53552904Sshin#define	IPV6_PORTRANGE_DEFAULT	0	/* default range */
53652904Sshin#define	IPV6_PORTRANGE_HIGH	1	/* "high" - request firewall bypass */
53752904Sshin#define	IPV6_PORTRANGE_LOW	2	/* "low" - vouchsafe security */
53852904Sshin
539121472Sume#if __BSD_VISIBLE
54052904Sshin/*
54152904Sshin * Definitions for inet6 sysctl operations.
54252904Sshin *
54352904Sshin * Third level is protocol number.
54452904Sshin * Fourth level is desired variable within that protocol.
54552904Sshin */
54662587Sitojun#define IPV6PROTO_MAXID	(IPPROTO_PIM + 1)	/* don't list to IPV6PROTO_MAX */
54752904Sshin
54852904Sshin/*
54952904Sshin * Names for IP sysctl objects
55052904Sshin */
55162587Sitojun#define IPV6CTL_FORWARDING	1	/* act as router */
55262587Sitojun#define IPV6CTL_SENDREDIRECTS	2	/* may send redirects when forwarding*/
55362587Sitojun#define IPV6CTL_DEFHLIM		3	/* default Hop-Limit */
55452904Sshin#ifdef notyet
55562587Sitojun#define IPV6CTL_DEFMTU		4	/* default MTU */
55652904Sshin#endif
55762587Sitojun#define IPV6CTL_FORWSRCRT	5	/* forward source-routed dgrams */
55862587Sitojun#define IPV6CTL_STATS		6	/* stats */
55962587Sitojun#define IPV6CTL_MRTSTATS	7	/* multicast forwarding stats */
56062587Sitojun#define IPV6CTL_MRTPROTO	8	/* multicast routing protocol */
56162587Sitojun#define IPV6CTL_MAXFRAGPACKETS	9	/* max packets reassembly queue */
56262587Sitojun#define IPV6CTL_SOURCECHECK	10	/* verify source route and intf */
56362587Sitojun#define IPV6CTL_SOURCECHECK_LOGINT 11	/* minimume logging interval */
56462587Sitojun#define IPV6CTL_ACCEPT_RTADV	12
56562587Sitojun#define IPV6CTL_KEEPFAITH	13
56662587Sitojun#define IPV6CTL_LOG_INTERVAL	14
56762587Sitojun#define IPV6CTL_HDRNESTLIMIT	15
56862587Sitojun#define IPV6CTL_DAD_COUNT	16
56962587Sitojun#define IPV6CTL_AUTO_FLOWLABEL	17
57062587Sitojun#define IPV6CTL_DEFMCASTHLIM	18
57162587Sitojun#define IPV6CTL_GIF_HLIM	19	/* default HLIM for gif encap packet */
57262587Sitojun#define IPV6CTL_KAME_VERSION	20
57362587Sitojun#define IPV6CTL_USE_DEPRECATED	21	/* use deprecated addr (RFC2462 5.5.4) */
57462587Sitojun#define IPV6CTL_RR_PRUNE	22	/* walk timer for router renumbering */
57595023Ssuz#if 0	/* obsolete */
57662587Sitojun#define IPV6CTL_MAPPED_ADDR	23
57762604Sitojun#endif
57878064Sume#define IPV6CTL_V6ONLY		24
57962604Sitojun#define IPV6CTL_RTEXPIRE	25	/* cloned route expiration time */
58062604Sitojun#define IPV6CTL_RTMINEXPIRE	26	/* min value for expiration time */
58162604Sitojun#define IPV6CTL_RTMAXCACHE	27	/* trigger level for dynamic expire */
58278064Sume
58378064Sume#define IPV6CTL_USETEMPADDR	32	/* use temporary addresses (RFC3041) */
58478064Sume#define IPV6CTL_TEMPPLTIME	33	/* preferred lifetime for tmpaddrs */
58578064Sume#define IPV6CTL_TEMPVLTIME	34	/* valid lifetime for tmpaddrs */
58678064Sume#define IPV6CTL_AUTO_LINKLOCAL	35	/* automatic link-local addr assign */
58778064Sume#define IPV6CTL_RIP6STATS	36	/* raw_ip6 stats */
588122077Sume#define IPV6CTL_PREFER_TEMPADDR	37	/* prefer temporary addr as src */
589121742Sume#define IPV6CTL_ADDRCTLPOLICY	38	/* get/set address selection policy */
590148385Sume#define IPV6CTL_USE_DEFAULTZONE	39	/* use default scope zone */
591121742Sume
592121345Sume#define IPV6CTL_MAXFRAGS	41	/* max fragments */
593149033Sume#if 0
594149033Sume#define IPV6CTL_IFQ		42	/* ip6intrq node */
595149033Sume#define IPV6CTL_ISATAPRTR	43	/* isatap router */
596149033Sume#endif
597149033Sume#define IPV6CTL_MCAST_PMTU	44	/* enable pMTU discovery for multicast? */
598121345Sume
59952904Sshin/* New entries should be added here from current IPV6CTL_MAXID value. */
60065124Sitojun/* to define items, should talk with KAME guys first, for *BSD compatibility */
601148921Ssuz#define IPV6CTL_STEALTH		45
602148921Ssuz#define IPV6CTL_MAXID		46
603121472Sume#endif /* __BSD_VISIBLE */
60478064Sume
60553541Sshin/*
60653541Sshin * Redefinition of mbuf flags
60753541Sshin */
60878064Sume#define	M_AUTHIPHDR	M_PROTO2
60978064Sume#define	M_DECRYPTED	M_PROTO3
61078064Sume#define	M_LOOP		M_PROTO4
61178064Sume#define	M_AUTHIPDGM	M_PROTO5
61253541Sshin
61352904Sshin#ifdef _KERNEL
61478064Sumestruct cmsghdr;
61552904Sshin
61662587Sitojunint	in6_cksum __P((struct mbuf *, u_int8_t, u_int32_t, u_int32_t));
61752904Sshinint	in6_localaddr __P((struct in6_addr *));
61852904Sshinint	in6_addrscope __P((struct in6_addr *));
61952904Sshinstruct	in6_ifaddr *in6_ifawithifp __P((struct ifnet *, struct in6_addr *));
62078064Sumeextern void in6_if_up __P((struct ifnet *));
62178064Sumestruct sockaddr;
62292700Sdarrenrextern	u_char	ip6_protox[];
62352904Sshin
62452904Sshinvoid	in6_sin6_2_sin __P((struct sockaddr_in *sin,
62552904Sshin			    struct sockaddr_in6 *sin6));
62652904Sshinvoid	in6_sin_2_v4mapsin6 __P((struct sockaddr_in *sin,
62752904Sshin				 struct sockaddr_in6 *sin6));
62852904Sshinvoid	in6_sin6_2_sin_in_sock __P((struct sockaddr *nam));
62952904Sshinvoid	in6_sin_2_v4mapsin6_in_sock __P((struct sockaddr **nam));
630121742Sumeextern void addrsel_policy_init __P((void));
63152904Sshin
63278064Sume#define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
63378064Sume#define	sin6tosa(sin6)	((struct sockaddr *)(sin6))
63478064Sume#define	ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
63583934Sbrooks
63683934Sbrooksextern int	(*faithprefix_p)(struct in6_addr *);
63752904Sshin#endif /* _KERNEL */
63852904Sshin
639102227Smike#ifndef _SIZE_T_DECLARED
640102227Smiketypedef	__size_t	size_t;
641102227Smike#define	_SIZE_T_DECLARED
64297181Smike#endif
64397181Smike
644121498Sume#ifndef _SOCKLEN_T_DECLARED
645121498Sumetypedef	__socklen_t	socklen_t;
646121498Sume#define	_SOCKLEN_T_DECLARED
647121498Sume#endif
648121498Sume
649121472Sume#if __BSD_VISIBLE
650121472Sume
65152904Sshin__BEGIN_DECLS
65252904Sshinstruct cmsghdr;
65352904Sshin
65478064Sumeextern int inet6_option_space __P((int));
65578064Sumeextern int inet6_option_init __P((void *, struct cmsghdr **, int));
65697181Smikeextern int inet6_option_append __P((struct cmsghdr *, const uint8_t *,
65778064Sume	int, int));
658121499Sumeextern uint8_t *inet6_option_alloc __P((struct cmsghdr *, int, int, int));
659121499Sumeextern int inet6_option_next __P((const struct cmsghdr *, uint8_t **));
660121499Sumeextern int inet6_option_find __P((const struct cmsghdr *, uint8_t **, int));
66152904Sshin
66278064Sumeextern size_t inet6_rthdr_space __P((int, int));
66378064Sumeextern struct cmsghdr *inet6_rthdr_init __P((void *, int));
66478064Sumeextern int inet6_rthdr_add __P((struct cmsghdr *, const struct in6_addr *,
665121472Sume	unsigned int));
66678064Sumeextern int inet6_rthdr_lasthop __P((struct cmsghdr *, unsigned int));
66778064Sume#if 0 /* not implemented yet */
66878064Sumeextern int inet6_rthdr_reverse __P((const struct cmsghdr *, struct cmsghdr *));
66978064Sume#endif
67078064Sumeextern int inet6_rthdr_segments __P((const struct cmsghdr *));
67178064Sumeextern struct in6_addr *inet6_rthdr_getaddr __P((struct cmsghdr *, int));
67278064Sumeextern int inet6_rthdr_getflags __P((const struct cmsghdr *, int));
67378064Sume
674121472Sumeextern int inet6_opt_init __P((void *, socklen_t));
675121499Sumeextern int inet6_opt_append __P((void *, socklen_t, int, uint8_t, socklen_t,
676121499Sume	uint8_t, void **));
677121472Sumeextern int inet6_opt_finish __P((void *, socklen_t, int));
678121472Sumeextern int inet6_opt_set_val __P((void *, int, void *, socklen_t));
67978064Sume
680121499Sumeextern int inet6_opt_next __P((void *, socklen_t, int, uint8_t *, socklen_t *,
681121472Sume	void **));
682121499Sumeextern int inet6_opt_find __P((void *, socklen_t, int, uint8_t, socklen_t *,
683121472Sume	void **));
684121472Sumeextern int inet6_opt_get_val __P((void *, int, void *, socklen_t));
685121472Sumeextern socklen_t inet6_rth_space __P((int, int));
686121472Sumeextern void *inet6_rth_init __P((void *, socklen_t, int, int));
68778064Sumeextern int inet6_rth_add __P((void *, const struct in6_addr *));
68878064Sumeextern int inet6_rth_reverse __P((const void *, void *));
68978064Sumeextern int inet6_rth_segments __P((const void *));
69078064Sumeextern struct in6_addr *inet6_rth_getaddr __P((const void *, int));
69152904Sshin__END_DECLS
69252904Sshin
69397181Smike#endif /* __BSD_VISIBLE */
69497181Smike
69552904Sshin#endif /* !_NETINET6_IN6_H_ */
696