Home | History | Annotate | Line # | Download | only in netinet6
ip6_input.c revision 1.186
      1 /*	$NetBSD: ip6_input.c,v 1.186 2018/01/29 10:57:13 maxv Exp $	*/
      2 /*	$KAME: ip6_input.c,v 1.188 2001/03/29 05:34:31 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1988, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. Neither the name of the University nor the names of its contributors
     46  *    may be used to endorse or promote products derived from this software
     47  *    without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60  *
     61  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: ip6_input.c,v 1.186 2018/01/29 10:57:13 maxv Exp $");
     66 
     67 #ifdef _KERNEL_OPT
     68 #include "opt_gateway.h"
     69 #include "opt_inet.h"
     70 #include "opt_inet6.h"
     71 #include "opt_ipsec.h"
     72 #include "opt_net_mpsafe.h"
     73 #endif
     74 
     75 #include <sys/param.h>
     76 #include <sys/systm.h>
     77 #include <sys/mbuf.h>
     78 #include <sys/domain.h>
     79 #include <sys/protosw.h>
     80 #include <sys/socket.h>
     81 #include <sys/socketvar.h>
     82 #include <sys/errno.h>
     83 #include <sys/time.h>
     84 #include <sys/kernel.h>
     85 #include <sys/syslog.h>
     86 #include <sys/proc.h>
     87 #include <sys/sysctl.h>
     88 #include <sys/cprng.h>
     89 #include <sys/percpu.h>
     90 
     91 #include <net/if.h>
     92 #include <net/if_types.h>
     93 #include <net/if_dl.h>
     94 #include <net/route.h>
     95 #include <net/pktqueue.h>
     96 #include <net/pfil.h>
     97 
     98 #include <netinet/in.h>
     99 #include <netinet/in_systm.h>
    100 #ifdef INET
    101 #include <netinet/ip.h>
    102 #include <netinet/ip_var.h>
    103 #include <netinet/ip_icmp.h>
    104 #endif /* INET */
    105 #include <netinet/ip6.h>
    106 #include <netinet/portalgo.h>
    107 #include <netinet6/in6_var.h>
    108 #include <netinet6/ip6_var.h>
    109 #include <netinet6/ip6_private.h>
    110 #include <netinet6/in6_pcb.h>
    111 #include <netinet/icmp6.h>
    112 #include <netinet6/scope6_var.h>
    113 #include <netinet6/in6_ifattach.h>
    114 #include <netinet6/nd6.h>
    115 
    116 #ifdef IPSEC
    117 #include <netipsec/ipsec.h>
    118 #include <netipsec/ipsec6.h>
    119 #include <netipsec/key.h>
    120 #endif /* IPSEC */
    121 
    122 #include <netinet6/ip6protosw.h>
    123 
    124 #include "faith.h"
    125 
    126 #include <net/net_osdep.h>
    127 
    128 extern struct domain inet6domain;
    129 
    130 u_char ip6_protox[IPPROTO_MAX];
    131 pktqueue_t *ip6_pktq __read_mostly;
    132 
    133 pfil_head_t *inet6_pfil_hook;
    134 
    135 percpu_t *ip6stat_percpu;
    136 
    137 percpu_t *ip6_forward_rt_percpu __cacheline_aligned;
    138 
    139 static void ip6_init2(void);
    140 static void ip6intr(void *);
    141 static struct m_tag *ip6_setdstifaddr(struct mbuf *, const struct in6_ifaddr *);
    142 
    143 static int ip6_process_hopopts(struct mbuf *, u_int8_t *, int, u_int32_t *,
    144 	u_int32_t *);
    145 static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
    146 static void sysctl_net_inet6_ip6_setup(struct sysctllog **);
    147 
    148 #ifdef NET_MPSAFE
    149 #define	SOFTNET_LOCK()		mutex_enter(softnet_lock)
    150 #define	SOFTNET_UNLOCK()	mutex_exit(softnet_lock)
    151 #else
    152 #define	SOFTNET_LOCK()		KASSERT(mutex_owned(softnet_lock))
    153 #define	SOFTNET_UNLOCK()	KASSERT(mutex_owned(softnet_lock))
    154 #endif
    155 
    156 /*
    157  * IP6 initialization: fill in IP6 protocol switch table.
    158  * All protocols not implemented in kernel go to raw IP6 protocol handler.
    159  */
    160 void
    161 ip6_init(void)
    162 {
    163 	const struct ip6protosw *pr;
    164 	int i;
    165 
    166 	in6_init();
    167 
    168 	sysctl_net_inet6_ip6_setup(NULL);
    169 	pr = (const struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
    170 	if (pr == 0)
    171 		panic("ip6_init");
    172 	for (i = 0; i < IPPROTO_MAX; i++)
    173 		ip6_protox[i] = pr - inet6sw;
    174 	for (pr = (const struct ip6protosw *)inet6domain.dom_protosw;
    175 	    pr < (const struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
    176 		if (pr->pr_domain->dom_family == PF_INET6 &&
    177 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
    178 			ip6_protox[pr->pr_protocol] = pr - inet6sw;
    179 
    180 	ip6_pktq = pktq_create(IFQ_MAXLEN, ip6intr, NULL);
    181 	KASSERT(ip6_pktq != NULL);
    182 
    183 	scope6_init();
    184 	addrsel_policy_init();
    185 	nd6_init();
    186 	frag6_init();
    187 	ip6_desync_factor = cprng_fast32() % MAX_TEMP_DESYNC_FACTOR;
    188 
    189 	ip6_init2();
    190 #ifdef GATEWAY
    191 	ip6flow_init(ip6_hashsize);
    192 #endif
    193 	/* Register our Packet Filter hook. */
    194 	inet6_pfil_hook = pfil_head_create(PFIL_TYPE_AF, (void *)AF_INET6);
    195 	KASSERT(inet6_pfil_hook != NULL);
    196 
    197 	ip6stat_percpu = percpu_alloc(sizeof(uint64_t) * IP6_NSTATS);
    198 	ip6_forward_rt_percpu = percpu_alloc(sizeof(struct route));
    199 }
    200 
    201 static void
    202 ip6_init2(void)
    203 {
    204 
    205 	/* timer for regeneration of temporary addresses randomize ID */
    206 	callout_init(&in6_tmpaddrtimer_ch, CALLOUT_MPSAFE);
    207 	callout_reset(&in6_tmpaddrtimer_ch,
    208 		      (ip6_temp_preferred_lifetime - ip6_desync_factor -
    209 		       ip6_temp_regen_advance) * hz,
    210 		      in6_tmpaddrtimer, NULL);
    211 }
    212 
    213 /*
    214  * IP6 input interrupt handling. Just pass the packet to ip6_input.
    215  */
    216 static void
    217 ip6intr(void *arg __unused)
    218 {
    219 	struct mbuf *m;
    220 
    221 	SOFTNET_LOCK_UNLESS_NET_MPSAFE();
    222 	while ((m = pktq_dequeue(ip6_pktq)) != NULL) {
    223 		struct psref psref;
    224 		struct ifnet *rcvif = m_get_rcvif_psref(m, &psref);
    225 
    226 		if (rcvif == NULL) {
    227 			m_freem(m);
    228 			continue;
    229 		}
    230 		/*
    231 		 * Drop the packet if IPv6 is disabled on the interface.
    232 		 */
    233 		if ((ND_IFINFO(rcvif)->flags & ND6_IFF_IFDISABLED)) {
    234 			m_put_rcvif_psref(rcvif, &psref);
    235 			m_freem(m);
    236 			continue;
    237 		}
    238 		ip6_input(m, rcvif);
    239 		m_put_rcvif_psref(rcvif, &psref);
    240 	}
    241 	SOFTNET_UNLOCK_UNLESS_NET_MPSAFE();
    242 }
    243 
    244 void
    245 ip6_input(struct mbuf *m, struct ifnet *rcvif)
    246 {
    247 	struct ip6_hdr *ip6;
    248 	int hit, off = sizeof(struct ip6_hdr), nest;
    249 	u_int32_t plen;
    250 	u_int32_t rtalert = ~0;
    251 	int nxt, ours = 0, rh_present = 0;
    252 	struct ifnet *deliverifp = NULL;
    253 	int srcrt = 0;
    254 	struct rtentry *rt = NULL;
    255 	union {
    256 		struct sockaddr		dst;
    257 		struct sockaddr_in6	dst6;
    258 	} u;
    259 	struct route *ro;
    260 
    261 	KASSERT(rcvif != NULL);
    262 
    263 	/*
    264 	 * make sure we don't have onion peering information into m_tag.
    265 	 */
    266 	ip6_delaux(m);
    267 
    268 	/*
    269 	 * mbuf statistics
    270 	 */
    271 	if (m->m_flags & M_EXT) {
    272 		if (m->m_next)
    273 			IP6_STATINC(IP6_STAT_MEXT2M);
    274 		else
    275 			IP6_STATINC(IP6_STAT_MEXT1);
    276 	} else {
    277 #define M2MMAX	32
    278 		if (m->m_next) {
    279 			if (m->m_flags & M_LOOP)
    280 			/*XXX*/	IP6_STATINC(IP6_STAT_M2M + lo0ifp->if_index);
    281 			else if (rcvif->if_index < M2MMAX)
    282 				IP6_STATINC(IP6_STAT_M2M + rcvif->if_index);
    283 			else
    284 				IP6_STATINC(IP6_STAT_M2M);
    285 		} else
    286 			IP6_STATINC(IP6_STAT_M1);
    287 #undef M2MMAX
    288 	}
    289 
    290 	in6_ifstat_inc(rcvif, ifs6_in_receive);
    291 	IP6_STATINC(IP6_STAT_TOTAL);
    292 
    293 	/*
    294 	 * If the IPv6 header is not aligned, slurp it up into a new
    295 	 * mbuf with space for link headers, in the event we forward
    296 	 * it.  Otherwise, if it is aligned, make sure the entire base
    297 	 * IPv6 header is in the first mbuf of the chain.
    298 	 */
    299 	if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
    300 		if ((m = m_copyup(m, sizeof(struct ip6_hdr),
    301 		    (max_linkhdr + 3) & ~3)) == NULL) {
    302 			/* XXXJRT new stat, please */
    303 			IP6_STATINC(IP6_STAT_TOOSMALL);
    304 			in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
    305 			return;
    306 		}
    307 	} else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
    308 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
    309 			IP6_STATINC(IP6_STAT_TOOSMALL);
    310 			in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
    311 			return;
    312 		}
    313 	}
    314 
    315 	ip6 = mtod(m, struct ip6_hdr *);
    316 
    317 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
    318 		IP6_STATINC(IP6_STAT_BADVERS);
    319 		in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
    320 		goto bad;
    321 	}
    322 
    323 	/*
    324 	 * Assume that we can create a fast-forward IP flow entry
    325 	 * based on this packet.
    326 	 */
    327 	m->m_flags |= M_CANFASTFWD;
    328 
    329 	/*
    330 	 * Run through list of hooks for input packets.  If there are any
    331 	 * filters which require that additional packets in the flow are
    332 	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
    333 	 * Note that filters must _never_ set this flag, as another filter
    334 	 * in the list may have previously cleared it.
    335 	 */
    336 	/*
    337 	 * let ipfilter look at packet on the wire,
    338 	 * not the decapsulated packet.
    339 	 */
    340 #if defined(IPSEC)
    341 	if (!ipsec_used || !ipsec_indone(m))
    342 #else
    343 	if (1)
    344 #endif
    345 	{
    346 		struct in6_addr odst;
    347 
    348 		odst = ip6->ip6_dst;
    349 		if (pfil_run_hooks(inet6_pfil_hook, &m, rcvif, PFIL_IN) != 0)
    350 			return;
    351 		if (m == NULL)
    352 			return;
    353 		ip6 = mtod(m, struct ip6_hdr *);
    354 		srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
    355 	}
    356 
    357 	IP6_STATINC(IP6_STAT_NXTHIST + ip6->ip6_nxt);
    358 
    359 #ifdef ALTQ
    360 	if (altq_input != NULL) {
    361 		SOFTNET_LOCK();
    362 		if ((*altq_input)(m, AF_INET6) == 0) {
    363 			SOFTNET_UNLOCK();
    364 			/* packet is dropped by traffic conditioner */
    365 			return;
    366 		}
    367 		SOFTNET_UNLOCK();
    368 	}
    369 #endif
    370 
    371 	/*
    372 	 * Check against address spoofing/corruption.
    373 	 */
    374 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
    375 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
    376 		/*
    377 		 * XXX: "badscope" is not very suitable for a multicast source.
    378 		 */
    379 		IP6_STATINC(IP6_STAT_BADSCOPE);
    380 		in6_ifstat_inc(rcvif, ifs6_in_addrerr);
    381 		goto bad;
    382 	}
    383 
    384 	/*
    385 	 * The following check is not documented in specs.  A malicious
    386 	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
    387 	 * and bypass security checks (act as if it was from 127.0.0.1 by using
    388 	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
    389 	 *
    390 	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
    391 	 * support IPv4-less kernel compilation, we cannot support SIIT
    392 	 * environment at all.  So, it makes more sense for us to reject any
    393 	 * malicious packets for non-SIIT environment, than try to do a
    394 	 * partial support for SIIT environment.
    395 	 */
    396 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    397 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    398 		IP6_STATINC(IP6_STAT_BADSCOPE);
    399 		in6_ifstat_inc(rcvif, ifs6_in_addrerr);
    400 		goto bad;
    401 	}
    402 
    403 #if 0
    404 	/*
    405 	 * Reject packets with IPv4 compatible addresses (auto tunnel).
    406 	 *
    407 	 * The code forbids auto tunnel relay case in RFC1933 (the check is
    408 	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
    409 	 * is revised to forbid relaying case.
    410 	 */
    411 	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
    412 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
    413 		IP6_STATINC(IP6_STAT_BADSCOPE);
    414 		in6_ifstat_inc(rcvif, ifs6_in_addrerr);
    415 		goto bad;
    416 	}
    417 #endif
    418 
    419 	/*
    420 	 * Disambiguate address scope zones (if there is ambiguity).
    421 	 * We first make sure that the original source or destination address
    422 	 * is not in our internal form for scoped addresses.  Such addresses
    423 	 * are not necessarily invalid spec-wise, but we cannot accept them due
    424 	 * to the usage conflict.
    425 	 * in6_setscope() then also checks and rejects the cases where src or
    426 	 * dst are the loopback address and the receiving interface
    427 	 * is not loopback.
    428 	 */
    429 	if (__predict_false(
    430 	    m_makewritable(&m, 0, sizeof(struct ip6_hdr), M_DONTWAIT)))
    431 		goto bad;
    432 	ip6 = mtod(m, struct ip6_hdr *);
    433 	if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
    434 		IP6_STATINC(IP6_STAT_BADSCOPE);	/* XXX */
    435 		goto bad;
    436 	}
    437 	if (in6_setscope(&ip6->ip6_src, rcvif, NULL) ||
    438 	    in6_setscope(&ip6->ip6_dst, rcvif, NULL)) {
    439 		IP6_STATINC(IP6_STAT_BADSCOPE);
    440 		goto bad;
    441 	}
    442 
    443 	ro = percpu_getref(ip6_forward_rt_percpu);
    444 
    445 	/*
    446 	 * Multicast check
    447 	 */
    448 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    449 		bool ingroup;
    450 
    451 		in6_ifstat_inc(rcvif, ifs6_in_mcast);
    452 		/*
    453 		 * See if we belong to the destination multicast group on the
    454 		 * arrival interface.
    455 		 */
    456 		ingroup = in6_multi_group(&ip6->ip6_dst, rcvif);
    457 		if (ingroup) {
    458 			ours = 1;
    459 		} else if (!ip6_mrouter) {
    460 			uint64_t *ip6s = IP6_STAT_GETREF();
    461 			ip6s[IP6_STAT_NOTMEMBER]++;
    462 			ip6s[IP6_STAT_CANTFORWARD]++;
    463 			IP6_STAT_PUTREF();
    464 			in6_ifstat_inc(rcvif, ifs6_in_discard);
    465 			goto bad_unref;
    466 		}
    467 		deliverifp = rcvif;
    468 		goto hbhcheck;
    469 	}
    470 
    471 	sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
    472 
    473 	/*
    474 	 * Unicast check
    475 	 */
    476 	rt = rtcache_lookup2(ro, &u.dst, 1, &hit);
    477 	if (hit)
    478 		IP6_STATINC(IP6_STAT_FORWARD_CACHEHIT);
    479 	else
    480 		IP6_STATINC(IP6_STAT_FORWARD_CACHEMISS);
    481 
    482 	/*
    483 	 * Accept the packet if the forwarding interface to the destination
    484 	 * (according to the routing table) is the loopback interface,
    485 	 * unless the associated route has a gateway.
    486 	 *
    487 	 * We don't explicitly match ip6_dst against an interface here. It
    488 	 * is already done in rtcache_lookup2: rt->rt_ifp->if_type will be
    489 	 * IFT_LOOP if the packet is for us.
    490 	 *
    491 	 * Note that this approach causes to accept a packet if there is a
    492 	 * route to the loopback interface for the destination of the packet.
    493 	 * But we think it's even useful in some situations, e.g. when using
    494 	 * a special daemon which wants to intercept the packet.
    495 	 */
    496 	if (rt != NULL &&
    497 	    (rt->rt_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
    498 	    rt->rt_ifp->if_type == IFT_LOOP) {
    499 		struct in6_ifaddr *ia6 = (struct in6_ifaddr *)rt->rt_ifa;
    500 		int addrok;
    501 
    502 		if (ia6->ia6_flags & IN6_IFF_ANYCAST)
    503 			m->m_flags |= M_ANYCAST6;
    504 		/*
    505 		 * packets to a tentative, duplicated, or somehow invalid
    506 		 * address must not be accepted.
    507 		 */
    508 		if (ia6->ia6_flags & IN6_IFF_NOTREADY)
    509 			addrok = 0;
    510 		else if (ia6->ia6_flags & IN6_IFF_DETACHED &&
    511 		    !IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src))
    512 		{
    513 			/* Allow internal traffic to DETACHED addresses */
    514 			struct sockaddr_in6 sin6;
    515 			int s;
    516 
    517 			memset(&sin6, 0, sizeof(sin6));
    518 			sin6.sin6_family = AF_INET6;
    519 			sin6.sin6_len = sizeof(sin6);
    520 			sin6.sin6_addr = ip6->ip6_src;
    521 			s = pserialize_read_enter();
    522 			addrok = (ifa_ifwithaddr(sin6tosa(&sin6)) != NULL);
    523 			pserialize_read_exit(s);
    524 		} else
    525 			addrok = 1;
    526 		if (addrok) {
    527 			/* this address is ready */
    528 			ours = 1;
    529 			deliverifp = ia6->ia_ifp;	/* correct? */
    530 			goto hbhcheck;
    531 		} else {
    532 			/* address is not ready, so discard the packet. */
    533 			char ip6bufs[INET6_ADDRSTRLEN];
    534 			char ip6bufd[INET6_ADDRSTRLEN];
    535 			nd6log(LOG_INFO, "packet to an unready address %s->%s\n",
    536 			    IN6_PRINT(ip6bufs, &ip6->ip6_src),
    537 			    IN6_PRINT(ip6bufd, &ip6->ip6_dst));
    538 
    539 			goto bad_unref;
    540 		}
    541 	}
    542 
    543 	/*
    544 	 * FAITH (Firewall Aided Internet Translator)
    545 	 */
    546 #if defined(NFAITH) && 0 < NFAITH
    547 	if (ip6_keepfaith) {
    548 		if (rt != NULL && rt->rt_ifp != NULL &&
    549 		    rt->rt_ifp->if_type == IFT_FAITH) {
    550 			/* XXX do we need more sanity checks? */
    551 			ours = 1;
    552 			deliverifp = rt->rt_ifp; /* faith */
    553 			goto hbhcheck;
    554 		}
    555 	}
    556 #endif
    557 
    558 	/*
    559 	 * Now there is no reason to process the packet if it's not our own
    560 	 * and we're not a router.
    561 	 */
    562 	if (!ip6_forwarding) {
    563 		IP6_STATINC(IP6_STAT_CANTFORWARD);
    564 		in6_ifstat_inc(rcvif, ifs6_in_discard);
    565 		goto bad_unref;
    566 	}
    567 
    568 hbhcheck:
    569 	/*
    570 	 * Record address information into m_tag, if we don't have one yet.
    571 	 * Note that we are unable to record it, if the address is not listed
    572 	 * as our interface address (e.g. multicast addresses, addresses
    573 	 * within FAITH prefixes and such).
    574 	 */
    575 	if (deliverifp && ip6_getdstifaddr(m) == NULL) {
    576 		struct in6_ifaddr *ia6;
    577 		int s = pserialize_read_enter();
    578 
    579 		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
    580 		/* Depends on ip6_setdstifaddr never sleep */
    581 		if (ia6 != NULL && ip6_setdstifaddr(m, ia6) == NULL) {
    582 			/*
    583 			 * XXX maybe we should drop the packet here,
    584 			 * as we could not provide enough information
    585 			 * to the upper layers.
    586 			 */
    587 		}
    588 		pserialize_read_exit(s);
    589 	}
    590 
    591 	/*
    592 	 * Process Hop-by-Hop options header if it's contained.
    593 	 * m may be modified in ip6_hopopts_input().
    594 	 * If a JumboPayload option is included, plen will also be modified.
    595 	 */
    596 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
    597 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
    598 		struct ip6_hbh *hbh;
    599 
    600 		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
    601 			/* m already freed */
    602 			in6_ifstat_inc(rcvif, ifs6_in_discard);
    603 			rtcache_unref(rt, ro);
    604 			percpu_putref(ip6_forward_rt_percpu);
    605 			return;
    606 		}
    607 
    608 		/* adjust pointer */
    609 		ip6 = mtod(m, struct ip6_hdr *);
    610 
    611 		/*
    612 		 * if the payload length field is 0 and the next header field
    613 		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
    614 		 * option MUST be included.
    615 		 */
    616 		if (ip6->ip6_plen == 0 && plen == 0) {
    617 			/*
    618 			 * Note that if a valid jumbo payload option is
    619 			 * contained, ip6_hopopts_input() must set a valid
    620 			 * (non-zero) payload length to the variable plen.
    621 			 */
    622 			IP6_STATINC(IP6_STAT_BADOPTIONS);
    623 			in6_ifstat_inc(rcvif, ifs6_in_discard);
    624 			in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
    625 			icmp6_error(m, ICMP6_PARAM_PROB,
    626 				    ICMP6_PARAMPROB_HEADER,
    627 				    (char *)&ip6->ip6_plen - (char *)ip6);
    628 			rtcache_unref(rt, ro);
    629 			percpu_putref(ip6_forward_rt_percpu);
    630 			return;
    631 		}
    632 		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
    633 			sizeof(struct ip6_hbh));
    634 		if (hbh == NULL) {
    635 			IP6_STATINC(IP6_STAT_TOOSHORT);
    636 			rtcache_unref(rt, ro);
    637 			percpu_putref(ip6_forward_rt_percpu);
    638 			return;
    639 		}
    640 		KASSERT(IP6_HDR_ALIGNED_P(hbh));
    641 		nxt = hbh->ip6h_nxt;
    642 
    643 		/*
    644 		 * accept the packet if a router alert option is included
    645 		 * and we act as an IPv6 router.
    646 		 */
    647 		if (rtalert != ~0 && ip6_forwarding)
    648 			ours = 1;
    649 	} else
    650 		nxt = ip6->ip6_nxt;
    651 
    652 	/*
    653 	 * Check that the amount of data in the buffers is at least much as
    654 	 * the IPv6 header would have us expect. Trim mbufs if longer than we
    655 	 * expect. Drop packet if shorter than we expect.
    656 	 */
    657 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
    658 		IP6_STATINC(IP6_STAT_TOOSHORT);
    659 		in6_ifstat_inc(rcvif, ifs6_in_truncated);
    660 		goto bad_unref;
    661 	}
    662 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
    663 		if (m->m_len == m->m_pkthdr.len) {
    664 			m->m_len = sizeof(struct ip6_hdr) + plen;
    665 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
    666 		} else
    667 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
    668 	}
    669 
    670 	/*
    671 	 * Forward if desirable.
    672 	 */
    673 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    674 		/*
    675 		 * If we are acting as a multicast router, all
    676 		 * incoming multicast packets are passed to the
    677 		 * kernel-level multicast forwarding function.
    678 		 * The packet is returned (relatively) intact; if
    679 		 * ip6_mforward() returns a non-zero value, the packet
    680 		 * must be discarded, else it may be accepted below.
    681 		 */
    682 		if (ip6_mrouter != NULL) {
    683 			int error;
    684 
    685 			SOFTNET_LOCK();
    686 			error = ip6_mforward(ip6, rcvif, m);
    687 			SOFTNET_UNLOCK();
    688 
    689 			if (error != 0) {
    690 				rtcache_unref(rt, ro);
    691 				percpu_putref(ip6_forward_rt_percpu);
    692 				IP6_STATINC(IP6_STAT_CANTFORWARD);
    693 				goto bad;
    694 			}
    695 		}
    696 		if (!ours)
    697 			goto bad_unref;
    698 	} else if (!ours) {
    699 		rtcache_unref(rt, ro);
    700 		percpu_putref(ip6_forward_rt_percpu);
    701 		ip6_forward(m, srcrt);
    702 		return;
    703 	}
    704 
    705 	ip6 = mtod(m, struct ip6_hdr *);
    706 
    707 	/*
    708 	 * Malicious party may be able to use IPv4 mapped addr to confuse
    709 	 * tcp/udp stack and bypass security checks (act as if it was from
    710 	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
    711 	 *
    712 	 * For SIIT end node behavior, you may want to disable the check.
    713 	 * However, you will  become vulnerable to attacks using IPv4 mapped
    714 	 * source.
    715 	 */
    716 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    717 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    718 		IP6_STATINC(IP6_STAT_BADSCOPE);
    719 		in6_ifstat_inc(rcvif, ifs6_in_addrerr);
    720 		goto bad_unref;
    721 	}
    722 
    723 #ifdef IFA_STATS
    724 	if (deliverifp != NULL) {
    725 		struct in6_ifaddr *ia6;
    726 		int s = pserialize_read_enter();
    727 		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
    728 		if (ia6)
    729 			ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
    730 		pserialize_read_exit(s);
    731 	}
    732 #endif
    733 	IP6_STATINC(IP6_STAT_DELIVERED);
    734 	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
    735 	nest = 0;
    736 
    737 	if (rt != NULL) {
    738 		rtcache_unref(rt, ro);
    739 		rt = NULL;
    740 	}
    741 	percpu_putref(ip6_forward_rt_percpu);
    742 
    743 	rh_present = 0;
    744 	while (nxt != IPPROTO_DONE) {
    745 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
    746 			IP6_STATINC(IP6_STAT_TOOMANYHDR);
    747 			in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
    748 			goto bad;
    749 		}
    750 
    751 		/*
    752 		 * protection against faulty packet - there should be
    753 		 * more sanity checks in header chain processing.
    754 		 */
    755 		if (m->m_pkthdr.len < off) {
    756 			IP6_STATINC(IP6_STAT_TOOSHORT);
    757 			in6_ifstat_inc(rcvif, ifs6_in_truncated);
    758 			goto bad;
    759 		}
    760 
    761 		if (nxt == IPPROTO_ROUTING) {
    762 			if (rh_present++) {
    763 				in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
    764 				IP6_STATINC(IP6_STAT_BADOPTIONS);
    765 				goto bad;
    766 			}
    767 		}
    768 
    769 #ifdef IPSEC
    770 		if (ipsec_used) {
    771 			/*
    772 			 * Enforce IPsec policy checking if we are seeing last
    773 			 * header. Note that we do not visit this with
    774 			 * protocols with pcb layer code - like udp/tcp/raw ip.
    775 			 */
    776 			if ((inet6sw[ip_protox[nxt]].pr_flags
    777 			    & PR_LASTHDR) != 0) {
    778 				int error;
    779 
    780 				error = ipsec6_input(m);
    781 				if (error)
    782 					goto bad;
    783 			}
    784 		}
    785 #endif
    786 
    787 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
    788 	}
    789 	return;
    790 
    791 bad_unref:
    792 	rtcache_unref(rt, ro);
    793 	percpu_putref(ip6_forward_rt_percpu);
    794 bad:
    795 	m_freem(m);
    796 	return;
    797 }
    798 
    799 /*
    800  * set/grab in6_ifaddr correspond to IPv6 destination address.
    801  */
    802 static struct m_tag *
    803 ip6_setdstifaddr(struct mbuf *m, const struct in6_ifaddr *ia)
    804 {
    805 	struct m_tag *mtag;
    806 	struct ip6aux *ip6a;
    807 
    808 	mtag = ip6_addaux(m);
    809 	if (mtag == NULL)
    810 		return NULL;
    811 
    812 	ip6a = (struct ip6aux *)(mtag + 1);
    813 	if (in6_setscope(&ip6a->ip6a_src, ia->ia_ifp, &ip6a->ip6a_scope_id)) {
    814 		IP6_STATINC(IP6_STAT_BADSCOPE);
    815 		return NULL;
    816 	}
    817 
    818 	ip6a->ip6a_src = ia->ia_addr.sin6_addr;
    819 	ip6a->ip6a_flags = ia->ia6_flags;
    820 	return mtag;
    821 }
    822 
    823 const struct ip6aux *
    824 ip6_getdstifaddr(struct mbuf *m)
    825 {
    826 	struct m_tag *mtag;
    827 
    828 	mtag = ip6_findaux(m);
    829 	if (mtag != NULL)
    830 		return (struct ip6aux *)(mtag + 1);
    831 	else
    832 		return NULL;
    833 }
    834 
    835 /*
    836  * Hop-by-Hop options header processing. If a valid jumbo payload option is
    837  * included, the real payload length will be stored in plenp.
    838  *
    839  * rtalertp - XXX: should be stored more smart way
    840  */
    841 int
    842 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
    843 	struct mbuf **mp, int *offp)
    844 {
    845 	struct mbuf *m = *mp;
    846 	int off = *offp, hbhlen;
    847 	struct ip6_hbh *hbh;
    848 
    849 	/* validation of the length of the header */
    850 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
    851 		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
    852 	if (hbh == NULL) {
    853 		IP6_STATINC(IP6_STAT_TOOSHORT);
    854 		return -1;
    855 	}
    856 	hbhlen = (hbh->ip6h_len + 1) << 3;
    857 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
    858 		hbhlen);
    859 	if (hbh == NULL) {
    860 		IP6_STATINC(IP6_STAT_TOOSHORT);
    861 		return -1;
    862 	}
    863 	KASSERT(IP6_HDR_ALIGNED_P(hbh));
    864 	off += hbhlen;
    865 	hbhlen -= sizeof(struct ip6_hbh);
    866 
    867 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
    868 				hbhlen, rtalertp, plenp) < 0)
    869 		return (-1);
    870 
    871 	*offp = off;
    872 	*mp = m;
    873 	return (0);
    874 }
    875 
    876 /*
    877  * Search header for all Hop-by-hop options and process each option.
    878  * This function is separate from ip6_hopopts_input() in order to
    879  * handle a case where the sending node itself process its hop-by-hop
    880  * options header. In such a case, the function is called from ip6_output().
    881  *
    882  * The function assumes that hbh header is located right after the IPv6 header
    883  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
    884  * opthead + hbhlen is located in continuous memory region.
    885  */
    886 static int
    887 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
    888 	u_int32_t *rtalertp, u_int32_t *plenp)
    889 {
    890 	struct ip6_hdr *ip6;
    891 	int optlen = 0;
    892 	u_int8_t *opt = opthead;
    893 	u_int16_t rtalert_val;
    894 	u_int32_t jumboplen;
    895 	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
    896 
    897 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
    898 		switch (*opt) {
    899 		case IP6OPT_PAD1:
    900 			optlen = 1;
    901 			break;
    902 		case IP6OPT_PADN:
    903 			if (hbhlen < IP6OPT_MINLEN) {
    904 				IP6_STATINC(IP6_STAT_TOOSMALL);
    905 				goto bad;
    906 			}
    907 			optlen = *(opt + 1) + 2;
    908 			break;
    909 		case IP6OPT_RTALERT:
    910 			/* XXX may need check for alignment */
    911 			if (hbhlen < IP6OPT_RTALERT_LEN) {
    912 				IP6_STATINC(IP6_STAT_TOOSMALL);
    913 				goto bad;
    914 			}
    915 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
    916 				/* XXX stat */
    917 				icmp6_error(m, ICMP6_PARAM_PROB,
    918 				    ICMP6_PARAMPROB_HEADER,
    919 				    erroff + opt + 1 - opthead);
    920 				return (-1);
    921 			}
    922 			optlen = IP6OPT_RTALERT_LEN;
    923 			memcpy((void *)&rtalert_val, (void *)(opt + 2), 2);
    924 			*rtalertp = ntohs(rtalert_val);
    925 			break;
    926 		case IP6OPT_JUMBO:
    927 			/* XXX may need check for alignment */
    928 			if (hbhlen < IP6OPT_JUMBO_LEN) {
    929 				IP6_STATINC(IP6_STAT_TOOSMALL);
    930 				goto bad;
    931 			}
    932 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
    933 				/* XXX stat */
    934 				icmp6_error(m, ICMP6_PARAM_PROB,
    935 				    ICMP6_PARAMPROB_HEADER,
    936 				    erroff + opt + 1 - opthead);
    937 				return (-1);
    938 			}
    939 			optlen = IP6OPT_JUMBO_LEN;
    940 
    941 			/*
    942 			 * IPv6 packets that have non 0 payload length
    943 			 * must not contain a jumbo payload option.
    944 			 */
    945 			ip6 = mtod(m, struct ip6_hdr *);
    946 			if (ip6->ip6_plen) {
    947 				IP6_STATINC(IP6_STAT_BADOPTIONS);
    948 				icmp6_error(m, ICMP6_PARAM_PROB,
    949 				    ICMP6_PARAMPROB_HEADER,
    950 				    erroff + opt - opthead);
    951 				return (-1);
    952 			}
    953 
    954 			/*
    955 			 * We may see jumbolen in unaligned location, so
    956 			 * we'd need to perform bcopy().
    957 			 */
    958 			memcpy(&jumboplen, opt + 2, sizeof(jumboplen));
    959 			jumboplen = (u_int32_t)htonl(jumboplen);
    960 
    961 #if 1
    962 			/*
    963 			 * if there are multiple jumbo payload options,
    964 			 * *plenp will be non-zero and the packet will be
    965 			 * rejected.
    966 			 * the behavior may need some debate in ipngwg -
    967 			 * multiple options does not make sense, however,
    968 			 * there's no explicit mention in specification.
    969 			 */
    970 			if (*plenp != 0) {
    971 				IP6_STATINC(IP6_STAT_BADOPTIONS);
    972 				icmp6_error(m, ICMP6_PARAM_PROB,
    973 				    ICMP6_PARAMPROB_HEADER,
    974 				    erroff + opt + 2 - opthead);
    975 				return (-1);
    976 			}
    977 #endif
    978 
    979 			/*
    980 			 * jumbo payload length must be larger than 65535.
    981 			 */
    982 			if (jumboplen <= IPV6_MAXPACKET) {
    983 				IP6_STATINC(IP6_STAT_BADOPTIONS);
    984 				icmp6_error(m, ICMP6_PARAM_PROB,
    985 				    ICMP6_PARAMPROB_HEADER,
    986 				    erroff + opt + 2 - opthead);
    987 				return (-1);
    988 			}
    989 			*plenp = jumboplen;
    990 
    991 			break;
    992 		default:		/* unknown option */
    993 			if (hbhlen < IP6OPT_MINLEN) {
    994 				IP6_STATINC(IP6_STAT_TOOSMALL);
    995 				goto bad;
    996 			}
    997 			optlen = ip6_unknown_opt(opt, m,
    998 			    erroff + opt - opthead);
    999 			if (optlen == -1)
   1000 				return (-1);
   1001 			optlen += 2;
   1002 			break;
   1003 		}
   1004 	}
   1005 
   1006 	return (0);
   1007 
   1008   bad:
   1009 	m_freem(m);
   1010 	return (-1);
   1011 }
   1012 
   1013 /*
   1014  * Unknown option processing.
   1015  * The third argument `off' is the offset from the IPv6 header to the option,
   1016  * which is necessary if the IPv6 header the and option header and IPv6 header
   1017  * is not continuous in order to return an ICMPv6 error.
   1018  */
   1019 int
   1020 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
   1021 {
   1022 	struct ip6_hdr *ip6;
   1023 
   1024 	switch (IP6OPT_TYPE(*optp)) {
   1025 	case IP6OPT_TYPE_SKIP: /* ignore the option */
   1026 		return ((int)*(optp + 1));
   1027 	case IP6OPT_TYPE_DISCARD:	/* silently discard */
   1028 		m_freem(m);
   1029 		return (-1);
   1030 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
   1031 		IP6_STATINC(IP6_STAT_BADOPTIONS);
   1032 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
   1033 		return (-1);
   1034 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
   1035 		IP6_STATINC(IP6_STAT_BADOPTIONS);
   1036 		ip6 = mtod(m, struct ip6_hdr *);
   1037 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
   1038 		    (m->m_flags & (M_BCAST|M_MCAST)))
   1039 			m_freem(m);
   1040 		else
   1041 			icmp6_error(m, ICMP6_PARAM_PROB,
   1042 				    ICMP6_PARAMPROB_OPTION, off);
   1043 		return (-1);
   1044 	}
   1045 
   1046 	m_freem(m);		/* XXX: NOTREACHED */
   1047 	return (-1);
   1048 }
   1049 
   1050 /*
   1051  * Create the "control" list for this pcb.
   1052  *
   1053  * The routine will be called from upper layer handlers like tcp6_input().
   1054  * Thus the routine assumes that the caller (tcp6_input) have already
   1055  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
   1056  * very first mbuf on the mbuf chain.
   1057  * We may want to add some infinite loop prevention or sanity checks for safety.
   1058  * (This applies only when you are using KAME mbuf chain restriction, i.e.
   1059  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
   1060  */
   1061 void
   1062 ip6_savecontrol(struct in6pcb *in6p, struct mbuf **mp,
   1063 	struct ip6_hdr *ip6, struct mbuf *m)
   1064 {
   1065 	struct socket *so = in6p->in6p_socket;
   1066 #ifdef RFC2292
   1067 #define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
   1068 #else
   1069 #define IS2292(x, y)	(y)
   1070 #endif
   1071 
   1072 	if (SOOPT_TIMESTAMP(so->so_options))
   1073 		mp = sbsavetimestamp(so->so_options, m, mp);
   1074 
   1075 	/* some OSes call this logic with IPv4 packet, for SO_TIMESTAMP */
   1076 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
   1077 		return;
   1078 
   1079 	/* RFC 2292 sec. 5 */
   1080 	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
   1081 		struct in6_pktinfo pi6;
   1082 
   1083 		memcpy(&pi6.ipi6_addr, &ip6->ip6_dst, sizeof(struct in6_addr));
   1084 		in6_clearscope(&pi6.ipi6_addr);	/* XXX */
   1085 		pi6.ipi6_ifindex = m->m_pkthdr.rcvif_index;
   1086 		*mp = sbcreatecontrol(&pi6, sizeof(pi6),
   1087 		    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
   1088 		if (*mp)
   1089 			mp = &(*mp)->m_next;
   1090 	}
   1091 
   1092 	if (in6p->in6p_flags & IN6P_HOPLIMIT) {
   1093 		int hlim = ip6->ip6_hlim & 0xff;
   1094 
   1095 		*mp = sbcreatecontrol(&hlim, sizeof(hlim),
   1096 		    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
   1097 		if (*mp)
   1098 			mp = &(*mp)->m_next;
   1099 	}
   1100 
   1101 	if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
   1102 		u_int32_t flowinfo;
   1103 		int tclass;
   1104 
   1105 		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
   1106 		flowinfo >>= 20;
   1107 
   1108 		tclass = flowinfo & 0xff;
   1109 		*mp = sbcreatecontrol(&tclass, sizeof(tclass),
   1110 		    IPV6_TCLASS, IPPROTO_IPV6);
   1111 
   1112 		if (*mp)
   1113 			mp = &(*mp)->m_next;
   1114 	}
   1115 
   1116 	/*
   1117 	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
   1118 	 * privilege for the option (see ip6_ctloutput), but it might be too
   1119 	 * strict, since there might be some hop-by-hop options which can be
   1120 	 * returned to normal user.
   1121 	 * See also RFC3542 section 8 (or RFC2292 section 6).
   1122 	 */
   1123 	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
   1124 		/*
   1125 		 * Check if a hop-by-hop options header is contatined in the
   1126 		 * received packet, and if so, store the options as ancillary
   1127 		 * data. Note that a hop-by-hop options header must be
   1128 		 * just after the IPv6 header, which fact is assured through
   1129 		 * the IPv6 input processing.
   1130 		 */
   1131 		struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
   1132 		if (xip6->ip6_nxt == IPPROTO_HOPOPTS) {
   1133 			struct ip6_hbh *hbh;
   1134 			int hbhlen;
   1135 			struct mbuf *ext;
   1136 
   1137 			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
   1138 			    xip6->ip6_nxt);
   1139 			if (ext == NULL) {
   1140 				IP6_STATINC(IP6_STAT_TOOSHORT);
   1141 				return;
   1142 			}
   1143 			hbh = mtod(ext, struct ip6_hbh *);
   1144 			hbhlen = (hbh->ip6h_len + 1) << 3;
   1145 			if (hbhlen != ext->m_len) {
   1146 				m_freem(ext);
   1147 				IP6_STATINC(IP6_STAT_TOOSHORT);
   1148 				return;
   1149 			}
   1150 
   1151 			/*
   1152 			 * XXX: We copy whole the header even if a jumbo
   1153 			 * payload option is included, which option is to
   1154 			 * be removed before returning in the RFC 2292.
   1155 			 * Note: this constraint is removed in RFC3542.
   1156 			 */
   1157 			*mp = sbcreatecontrol(hbh, hbhlen,
   1158 			    IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
   1159 			    IPPROTO_IPV6);
   1160 			if (*mp)
   1161 				mp = &(*mp)->m_next;
   1162 			m_freem(ext);
   1163 		}
   1164 	}
   1165 
   1166 	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
   1167 	if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
   1168 		struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
   1169 		int nxt = xip6->ip6_nxt, off = sizeof(struct ip6_hdr);
   1170 
   1171 		/*
   1172 		 * Search for destination options headers or routing
   1173 		 * header(s) through the header chain, and stores each
   1174 		 * header as ancillary data.
   1175 		 * Note that the order of the headers remains in
   1176 		 * the chain of ancillary data.
   1177 		 */
   1178 		for (;;) {	/* is explicit loop prevention necessary? */
   1179 			struct ip6_ext *ip6e = NULL;
   1180 			int elen;
   1181 			struct mbuf *ext = NULL;
   1182 
   1183 			/*
   1184 			 * if it is not an extension header, don't try to
   1185 			 * pull it from the chain.
   1186 			 */
   1187 			switch (nxt) {
   1188 			case IPPROTO_DSTOPTS:
   1189 			case IPPROTO_ROUTING:
   1190 			case IPPROTO_HOPOPTS:
   1191 			case IPPROTO_AH: /* is it possible? */
   1192 				break;
   1193 			default:
   1194 				goto loopend;
   1195 			}
   1196 
   1197 			ext = ip6_pullexthdr(m, off, nxt);
   1198 			if (ext == NULL) {
   1199 				IP6_STATINC(IP6_STAT_TOOSHORT);
   1200 				return;
   1201 			}
   1202 			ip6e = mtod(ext, struct ip6_ext *);
   1203 			if (nxt == IPPROTO_AH)
   1204 				elen = (ip6e->ip6e_len + 2) << 2;
   1205 			else
   1206 				elen = (ip6e->ip6e_len + 1) << 3;
   1207 			if (elen != ext->m_len) {
   1208 				m_freem(ext);
   1209 				IP6_STATINC(IP6_STAT_TOOSHORT);
   1210 				return;
   1211 			}
   1212 			KASSERT(IP6_HDR_ALIGNED_P(ip6e));
   1213 
   1214 			switch (nxt) {
   1215 			case IPPROTO_DSTOPTS:
   1216 				if (!(in6p->in6p_flags & IN6P_DSTOPTS))
   1217 					break;
   1218 
   1219 				*mp = sbcreatecontrol(ip6e, elen,
   1220 				    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
   1221 				    IPPROTO_IPV6);
   1222 				if (*mp)
   1223 					mp = &(*mp)->m_next;
   1224 				break;
   1225 
   1226 			case IPPROTO_ROUTING:
   1227 				if (!(in6p->in6p_flags & IN6P_RTHDR))
   1228 					break;
   1229 
   1230 				*mp = sbcreatecontrol(ip6e, elen,
   1231 				    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
   1232 				    IPPROTO_IPV6);
   1233 				if (*mp)
   1234 					mp = &(*mp)->m_next;
   1235 				break;
   1236 
   1237 			case IPPROTO_HOPOPTS:
   1238 			case IPPROTO_AH: /* is it possible? */
   1239 				break;
   1240 
   1241 			default:
   1242 				/*
   1243 			 	 * other cases have been filtered in the above.
   1244 				 * none will visit this case.  here we supply
   1245 				 * the code just in case (nxt overwritten or
   1246 				 * other cases).
   1247 				 */
   1248 				m_freem(ext);
   1249 				goto loopend;
   1250 
   1251 			}
   1252 
   1253 			/* proceed with the next header. */
   1254 			off += elen;
   1255 			nxt = ip6e->ip6e_nxt;
   1256 			ip6e = NULL;
   1257 			m_freem(ext);
   1258 			ext = NULL;
   1259 		}
   1260 	  loopend:
   1261 	  	;
   1262 	}
   1263 }
   1264 #undef IS2292
   1265 
   1266 
   1267 void
   1268 ip6_notify_pmtu(struct in6pcb *in6p, const struct sockaddr_in6 *dst,
   1269     uint32_t *mtu)
   1270 {
   1271 	struct socket *so;
   1272 	struct mbuf *m_mtu;
   1273 	struct ip6_mtuinfo mtuctl;
   1274 
   1275 	so = in6p->in6p_socket;
   1276 
   1277 	if (mtu == NULL)
   1278 		return;
   1279 
   1280 	KASSERT(so != NULL);
   1281 
   1282 	memset(&mtuctl, 0, sizeof(mtuctl));	/* zero-clear for safety */
   1283 	mtuctl.ip6m_mtu = *mtu;
   1284 	mtuctl.ip6m_addr = *dst;
   1285 	if (sa6_recoverscope(&mtuctl.ip6m_addr))
   1286 		return;
   1287 
   1288 	if ((m_mtu = sbcreatecontrol(&mtuctl, sizeof(mtuctl),
   1289 	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
   1290 		return;
   1291 
   1292 	if (sbappendaddr(&so->so_rcv, (const struct sockaddr *)dst, NULL, m_mtu)
   1293 	    == 0) {
   1294 		m_freem(m_mtu);
   1295 		/* XXX: should count statistics */
   1296 	} else
   1297 		sorwakeup(so);
   1298 
   1299 	return;
   1300 }
   1301 
   1302 /*
   1303  * pull single extension header from mbuf chain.  returns single mbuf that
   1304  * contains the result, or NULL on error.
   1305  */
   1306 static struct mbuf *
   1307 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
   1308 {
   1309 	struct ip6_ext ip6e;
   1310 	size_t elen;
   1311 	struct mbuf *n;
   1312 
   1313 #ifdef DIAGNOSTIC
   1314 	switch (nxt) {
   1315 	case IPPROTO_DSTOPTS:
   1316 	case IPPROTO_ROUTING:
   1317 	case IPPROTO_HOPOPTS:
   1318 	case IPPROTO_AH: /* is it possible? */
   1319 		break;
   1320 	default:
   1321 		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
   1322 	}
   1323 #endif
   1324 
   1325 	m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
   1326 	if (nxt == IPPROTO_AH)
   1327 		elen = (ip6e.ip6e_len + 2) << 2;
   1328 	else
   1329 		elen = (ip6e.ip6e_len + 1) << 3;
   1330 
   1331 	MGET(n, M_DONTWAIT, MT_DATA);
   1332 	if (n && elen >= MLEN) {
   1333 		MCLGET(n, M_DONTWAIT);
   1334 		if ((n->m_flags & M_EXT) == 0) {
   1335 			m_free(n);
   1336 			n = NULL;
   1337 		}
   1338 	}
   1339 	if (!n)
   1340 		return NULL;
   1341 
   1342 	n->m_len = 0;
   1343 	if (elen >= M_TRAILINGSPACE(n)) {
   1344 		m_free(n);
   1345 		return NULL;
   1346 	}
   1347 
   1348 	m_copydata(m, off, elen, mtod(n, void *));
   1349 	n->m_len = elen;
   1350 	return n;
   1351 }
   1352 
   1353 /*
   1354  * Get pointer to the previous header followed by the header
   1355  * currently processed.
   1356  * XXX: This function supposes that
   1357  *	M includes all headers,
   1358  *	the next header field and the header length field of each header
   1359  *	are valid, and
   1360  *	the sum of each header length equals to OFF.
   1361  * Because of these assumptions, this function must be called very
   1362  * carefully. Moreover, it will not be used in the near future when
   1363  * we develop `neater' mechanism to process extension headers.
   1364  */
   1365 u_int8_t *
   1366 ip6_get_prevhdr(struct mbuf *m, int off)
   1367 {
   1368 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   1369 
   1370 	if (off == sizeof(struct ip6_hdr))
   1371 		return (&ip6->ip6_nxt);
   1372 	else {
   1373 		int len, nxt;
   1374 		struct ip6_ext *ip6e = NULL;
   1375 
   1376 		nxt = ip6->ip6_nxt;
   1377 		len = sizeof(struct ip6_hdr);
   1378 		while (len < off) {
   1379 			ip6e = (struct ip6_ext *)(mtod(m, char *) + len);
   1380 
   1381 			switch (nxt) {
   1382 			case IPPROTO_FRAGMENT:
   1383 				len += sizeof(struct ip6_frag);
   1384 				break;
   1385 			case IPPROTO_AH:
   1386 				len += (ip6e->ip6e_len + 2) << 2;
   1387 				break;
   1388 			default:
   1389 				len += (ip6e->ip6e_len + 1) << 3;
   1390 				break;
   1391 			}
   1392 			nxt = ip6e->ip6e_nxt;
   1393 		}
   1394 		if (ip6e)
   1395 			return (&ip6e->ip6e_nxt);
   1396 		else
   1397 			return NULL;
   1398 	}
   1399 }
   1400 
   1401 /*
   1402  * get next header offset.  m will be retained.
   1403  */
   1404 int
   1405 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
   1406 {
   1407 	struct ip6_hdr ip6;
   1408 	struct ip6_ext ip6e;
   1409 	struct ip6_frag fh;
   1410 
   1411 	/* just in case */
   1412 	if (m == NULL)
   1413 		panic("ip6_nexthdr: m == NULL");
   1414 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
   1415 		return -1;
   1416 
   1417 	switch (proto) {
   1418 	case IPPROTO_IPV6:
   1419 		/* do not chase beyond intermediate IPv6 headers */
   1420 		if (off != 0)
   1421 			return -1;
   1422 		if (m->m_pkthdr.len < off + sizeof(ip6))
   1423 			return -1;
   1424 		m_copydata(m, off, sizeof(ip6), (void *)&ip6);
   1425 		if (nxtp)
   1426 			*nxtp = ip6.ip6_nxt;
   1427 		off += sizeof(ip6);
   1428 		return off;
   1429 
   1430 	case IPPROTO_FRAGMENT:
   1431 		/*
   1432 		 * terminate parsing if it is not the first fragment,
   1433 		 * it does not make sense to parse through it.
   1434 		 */
   1435 		if (m->m_pkthdr.len < off + sizeof(fh))
   1436 			return -1;
   1437 		m_copydata(m, off, sizeof(fh), (void *)&fh);
   1438 		if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0)
   1439 			return -1;
   1440 		if (nxtp)
   1441 			*nxtp = fh.ip6f_nxt;
   1442 		off += sizeof(struct ip6_frag);
   1443 		return off;
   1444 
   1445 	case IPPROTO_AH:
   1446 		if (m->m_pkthdr.len < off + sizeof(ip6e))
   1447 			return -1;
   1448 		m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
   1449 		if (nxtp)
   1450 			*nxtp = ip6e.ip6e_nxt;
   1451 		off += (ip6e.ip6e_len + 2) << 2;
   1452 		if (m->m_pkthdr.len < off)
   1453 			return -1;
   1454 		return off;
   1455 
   1456 	case IPPROTO_HOPOPTS:
   1457 	case IPPROTO_ROUTING:
   1458 	case IPPROTO_DSTOPTS:
   1459 		if (m->m_pkthdr.len < off + sizeof(ip6e))
   1460 			return -1;
   1461 		m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
   1462 		if (nxtp)
   1463 			*nxtp = ip6e.ip6e_nxt;
   1464 		off += (ip6e.ip6e_len + 1) << 3;
   1465 		if (m->m_pkthdr.len < off)
   1466 			return -1;
   1467 		return off;
   1468 
   1469 	case IPPROTO_NONE:
   1470 	case IPPROTO_ESP:
   1471 	case IPPROTO_IPCOMP:
   1472 		/* give up */
   1473 		return -1;
   1474 
   1475 	default:
   1476 		return -1;
   1477 	}
   1478 }
   1479 
   1480 /*
   1481  * get offset for the last header in the chain.  m will be kept untainted.
   1482  */
   1483 int
   1484 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
   1485 {
   1486 	int newoff;
   1487 	int nxt;
   1488 
   1489 	if (!nxtp) {
   1490 		nxt = -1;
   1491 		nxtp = &nxt;
   1492 	}
   1493 	for (;;) {
   1494 		newoff = ip6_nexthdr(m, off, proto, nxtp);
   1495 		if (newoff < 0)
   1496 			return off;
   1497 		else if (newoff < off)
   1498 			return -1;	/* invalid */
   1499 		else if (newoff == off)
   1500 			return newoff;
   1501 
   1502 		off = newoff;
   1503 		proto = *nxtp;
   1504 	}
   1505 }
   1506 
   1507 struct m_tag *
   1508 ip6_addaux(struct mbuf *m)
   1509 {
   1510 	struct m_tag *mtag;
   1511 
   1512 	mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
   1513 	if (!mtag) {
   1514 		mtag = m_tag_get(PACKET_TAG_INET6, sizeof(struct ip6aux),
   1515 		    M_NOWAIT);
   1516 		if (mtag) {
   1517 			m_tag_prepend(m, mtag);
   1518 			memset(mtag + 1, 0, sizeof(struct ip6aux));
   1519 		}
   1520 	}
   1521 	return mtag;
   1522 }
   1523 
   1524 struct m_tag *
   1525 ip6_findaux(struct mbuf *m)
   1526 {
   1527 	struct m_tag *mtag;
   1528 
   1529 	mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
   1530 	return mtag;
   1531 }
   1532 
   1533 void
   1534 ip6_delaux(struct mbuf *m)
   1535 {
   1536 	struct m_tag *mtag;
   1537 
   1538 	mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
   1539 	if (mtag)
   1540 		m_tag_delete(m, mtag);
   1541 }
   1542 
   1543 /*
   1544  * System control for IP6
   1545  */
   1546 
   1547 const u_char inet6ctlerrmap[PRC_NCMDS] = {
   1548 	0,		0,		0,		0,
   1549 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
   1550 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
   1551 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
   1552 	0,		0,		0,		0,
   1553 	ENOPROTOOPT
   1554 };
   1555 
   1556 extern int sysctl_net_inet6_addrctlpolicy(SYSCTLFN_ARGS);
   1557 
   1558 static int
   1559 sysctl_net_inet6_ip6_stats(SYSCTLFN_ARGS)
   1560 {
   1561 
   1562 	return (NETSTAT_SYSCTL(ip6stat_percpu, IP6_NSTATS));
   1563 }
   1564 
   1565 static void
   1566 sysctl_net_inet6_ip6_setup(struct sysctllog **clog)
   1567 {
   1568 #ifdef RFC2292
   1569 #define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
   1570 #else
   1571 #define IS2292(x, y)	(y)
   1572 #endif
   1573 
   1574 	sysctl_createv(clog, 0, NULL, NULL,
   1575 		       CTLFLAG_PERMANENT,
   1576 		       CTLTYPE_NODE, "inet6",
   1577 		       SYSCTL_DESCR("PF_INET6 related settings"),
   1578 		       NULL, 0, NULL, 0,
   1579 		       CTL_NET, PF_INET6, CTL_EOL);
   1580 	sysctl_createv(clog, 0, NULL, NULL,
   1581 		       CTLFLAG_PERMANENT,
   1582 		       CTLTYPE_NODE, "ip6",
   1583 		       SYSCTL_DESCR("IPv6 related settings"),
   1584 		       NULL, 0, NULL, 0,
   1585 		       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_EOL);
   1586 
   1587 	sysctl_createv(clog, 0, NULL, NULL,
   1588 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1589 		       CTLTYPE_INT, "forwarding",
   1590 		       SYSCTL_DESCR("Enable forwarding of INET6 datagrams"),
   1591 		       NULL, 0, &ip6_forwarding, 0,
   1592 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1593 		       IPV6CTL_FORWARDING, CTL_EOL);
   1594 	sysctl_createv(clog, 0, NULL, NULL,
   1595 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1596 		       CTLTYPE_INT, "redirect",
   1597 		       SYSCTL_DESCR("Enable sending of ICMPv6 redirect messages"),
   1598 		       NULL, 0, &ip6_sendredirects, 0,
   1599 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1600 		       IPV6CTL_SENDREDIRECTS, CTL_EOL);
   1601 	sysctl_createv(clog, 0, NULL, NULL,
   1602 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1603 		       CTLTYPE_INT, "hlim",
   1604 		       SYSCTL_DESCR("Hop limit for an INET6 datagram"),
   1605 		       NULL, 0, &ip6_defhlim, 0,
   1606 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1607 		       IPV6CTL_DEFHLIM, CTL_EOL);
   1608 #ifdef notyet
   1609 	sysctl_createv(clog, 0, NULL, NULL,
   1610 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1611 		       CTLTYPE_INT, "mtu", NULL,
   1612 		       NULL, 0, &, 0,
   1613 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1614 		       IPV6CTL_DEFMTU, CTL_EOL);
   1615 #endif
   1616 #ifdef __no_idea__
   1617 	sysctl_createv(clog, 0, NULL, NULL,
   1618 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1619 		       CTLTYPE_INT, "forwsrcrt", NULL,
   1620 		       NULL, 0, &?, 0,
   1621 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1622 		       IPV6CTL_FORWSRCRT, CTL_EOL);
   1623 	sysctl_createv(clog, 0, NULL, NULL,
   1624 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1625 		       CTLTYPE_STRUCT, "mrtstats", NULL,
   1626 		       NULL, 0, &?, sizeof(?),
   1627 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1628 		       IPV6CTL_MRTSTATS, CTL_EOL);
   1629 	sysctl_createv(clog, 0, NULL, NULL,
   1630 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1631 		       CTLTYPE_?, "mrtproto", NULL,
   1632 		       NULL, 0, &?, sizeof(?),
   1633 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1634 		       IPV6CTL_MRTPROTO, CTL_EOL);
   1635 #endif
   1636 	sysctl_createv(clog, 0, NULL, NULL,
   1637 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1638 		       CTLTYPE_INT, "maxfragpackets",
   1639 		       SYSCTL_DESCR("Maximum number of fragments to buffer "
   1640 				    "for reassembly"),
   1641 		       NULL, 0, &ip6_maxfragpackets, 0,
   1642 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1643 		       IPV6CTL_MAXFRAGPACKETS, CTL_EOL);
   1644 #ifdef __no_idea__
   1645 	sysctl_createv(clog, 0, NULL, NULL,
   1646 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1647 		       CTLTYPE_INT, "sourcecheck", NULL,
   1648 		       NULL, 0, &?, 0,
   1649 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1650 		       IPV6CTL_SOURCECHECK, CTL_EOL);
   1651 	sysctl_createv(clog, 0, NULL, NULL,
   1652 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1653 		       CTLTYPE_INT, "sourcecheck_logint", NULL,
   1654 		       NULL, 0, &?, 0,
   1655 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1656 		       IPV6CTL_SOURCECHECK_LOGINT, CTL_EOL);
   1657 #endif
   1658 	sysctl_createv(clog, 0, NULL, NULL,
   1659 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1660 		       CTLTYPE_INT, "accept_rtadv",
   1661 		       SYSCTL_DESCR("Accept router advertisements"),
   1662 		       NULL, 0, &ip6_accept_rtadv, 0,
   1663 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1664 		       IPV6CTL_ACCEPT_RTADV, CTL_EOL);
   1665 	sysctl_createv(clog, 0, NULL, NULL,
   1666 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1667 		       CTLTYPE_INT, "rtadv_maxroutes",
   1668 		       SYSCTL_DESCR("Maximum number of routes accepted via router advertisements"),
   1669 		       NULL, 0, &ip6_rtadv_maxroutes, 0,
   1670 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1671 		       IPV6CTL_RTADV_MAXROUTES, CTL_EOL);
   1672 	sysctl_createv(clog, 0, NULL, NULL,
   1673 		       CTLFLAG_PERMANENT,
   1674 		       CTLTYPE_INT, "rtadv_numroutes",
   1675 		       SYSCTL_DESCR("Current number of routes accepted via router advertisements"),
   1676 		       NULL, 0, &nd6_numroutes, 0,
   1677 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1678 		       IPV6CTL_RTADV_NUMROUTES, CTL_EOL);
   1679 	sysctl_createv(clog, 0, NULL, NULL,
   1680 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1681 		       CTLTYPE_INT, "keepfaith",
   1682 		       SYSCTL_DESCR("Activate faith interface"),
   1683 		       NULL, 0, &ip6_keepfaith, 0,
   1684 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1685 		       IPV6CTL_KEEPFAITH, CTL_EOL);
   1686 	sysctl_createv(clog, 0, NULL, NULL,
   1687 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1688 		       CTLTYPE_INT, "log_interval",
   1689 		       SYSCTL_DESCR("Minumum interval between logging "
   1690 				    "unroutable packets"),
   1691 		       NULL, 0, &ip6_log_interval, 0,
   1692 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1693 		       IPV6CTL_LOG_INTERVAL, CTL_EOL);
   1694 	sysctl_createv(clog, 0, NULL, NULL,
   1695 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1696 		       CTLTYPE_INT, "hdrnestlimit",
   1697 		       SYSCTL_DESCR("Maximum number of nested IPv6 headers"),
   1698 		       NULL, 0, &ip6_hdrnestlimit, 0,
   1699 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1700 		       IPV6CTL_HDRNESTLIMIT, CTL_EOL);
   1701 	sysctl_createv(clog, 0, NULL, NULL,
   1702 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1703 		       CTLTYPE_INT, "dad_count",
   1704 		       SYSCTL_DESCR("Number of Duplicate Address Detection "
   1705 				    "probes to send"),
   1706 		       NULL, 0, &ip6_dad_count, 0,
   1707 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1708 		       IPV6CTL_DAD_COUNT, CTL_EOL);
   1709 	sysctl_createv(clog, 0, NULL, NULL,
   1710 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1711 		       CTLTYPE_INT, "auto_flowlabel",
   1712 		       SYSCTL_DESCR("Assign random IPv6 flow labels"),
   1713 		       NULL, 0, &ip6_auto_flowlabel, 0,
   1714 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1715 		       IPV6CTL_AUTO_FLOWLABEL, CTL_EOL);
   1716 	sysctl_createv(clog, 0, NULL, NULL,
   1717 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1718 		       CTLTYPE_INT, "defmcasthlim",
   1719 		       SYSCTL_DESCR("Default multicast hop limit"),
   1720 		       NULL, 0, &ip6_defmcasthlim, 0,
   1721 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1722 		       IPV6CTL_DEFMCASTHLIM, CTL_EOL);
   1723 	sysctl_createv(clog, 0, NULL, NULL,
   1724 		       CTLFLAG_PERMANENT,
   1725 		       CTLTYPE_STRING, "kame_version",
   1726 		       SYSCTL_DESCR("KAME Version"),
   1727 		       NULL, 0, __UNCONST(__KAME_VERSION), 0,
   1728 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1729 		       IPV6CTL_KAME_VERSION, CTL_EOL);
   1730 	sysctl_createv(clog, 0, NULL, NULL,
   1731 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1732 		       CTLTYPE_INT, "use_deprecated",
   1733 		       SYSCTL_DESCR("Allow use of deprecated addresses as "
   1734 				    "source addresses"),
   1735 		       NULL, 0, &ip6_use_deprecated, 0,
   1736 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1737 		       IPV6CTL_USE_DEPRECATED, CTL_EOL);
   1738 	sysctl_createv(clog, 0, NULL, NULL,
   1739 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1740 		       CTLTYPE_INT, "rr_prune", NULL,
   1741 		       NULL, 0, &ip6_rr_prune, 0,
   1742 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1743 		       IPV6CTL_RR_PRUNE, CTL_EOL);
   1744 	sysctl_createv(clog, 0, NULL, NULL,
   1745 		       CTLFLAG_PERMANENT
   1746 #ifndef INET6_BINDV6ONLY
   1747 		       |CTLFLAG_READWRITE,
   1748 #endif
   1749 		       CTLTYPE_INT, "v6only",
   1750 		       SYSCTL_DESCR("Disallow PF_INET6 sockets from connecting "
   1751 				    "to PF_INET sockets"),
   1752 		       NULL, 0, &ip6_v6only, 0,
   1753 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1754 		       IPV6CTL_V6ONLY, CTL_EOL);
   1755 	sysctl_createv(clog, 0, NULL, NULL,
   1756 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1757 		       CTLTYPE_INT, "anonportmin",
   1758 		       SYSCTL_DESCR("Lowest ephemeral port number to assign"),
   1759 		       sysctl_net_inet_ip_ports, 0, &ip6_anonportmin, 0,
   1760 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1761 		       IPV6CTL_ANONPORTMIN, CTL_EOL);
   1762 	sysctl_createv(clog, 0, NULL, NULL,
   1763 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1764 		       CTLTYPE_INT, "anonportmax",
   1765 		       SYSCTL_DESCR("Highest ephemeral port number to assign"),
   1766 		       sysctl_net_inet_ip_ports, 0, &ip6_anonportmax, 0,
   1767 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1768 		       IPV6CTL_ANONPORTMAX, CTL_EOL);
   1769 #ifndef IPNOPRIVPORTS
   1770 	sysctl_createv(clog, 0, NULL, NULL,
   1771 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1772 		       CTLTYPE_INT, "lowportmin",
   1773 		       SYSCTL_DESCR("Lowest privileged ephemeral port number "
   1774 				    "to assign"),
   1775 		       sysctl_net_inet_ip_ports, 0, &ip6_lowportmin, 0,
   1776 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1777 		       IPV6CTL_LOWPORTMIN, CTL_EOL);
   1778 	sysctl_createv(clog, 0, NULL, NULL,
   1779 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1780 		       CTLTYPE_INT, "lowportmax",
   1781 		       SYSCTL_DESCR("Highest privileged ephemeral port number "
   1782 				    "to assign"),
   1783 		       sysctl_net_inet_ip_ports, 0, &ip6_lowportmax, 0,
   1784 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1785 		       IPV6CTL_LOWPORTMAX, CTL_EOL);
   1786 #endif /* IPNOPRIVPORTS */
   1787 	sysctl_createv(clog, 0, NULL, NULL,
   1788 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1789 		       CTLTYPE_INT, "auto_linklocal",
   1790 		       SYSCTL_DESCR("Default value of per-interface flag for "
   1791 		                    "adding an IPv6 link-local address to "
   1792 				    "interfaces when attached"),
   1793 		       NULL, 0, &ip6_auto_linklocal, 0,
   1794 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1795 		       IPV6CTL_AUTO_LINKLOCAL, CTL_EOL);
   1796 	sysctl_createv(clog, 0, NULL, NULL,
   1797 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   1798 		       CTLTYPE_STRUCT, "addctlpolicy",
   1799 		       SYSCTL_DESCR("Return the current address control"
   1800 			   " policy"),
   1801 		       sysctl_net_inet6_addrctlpolicy, 0, NULL, 0,
   1802 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1803 		       IPV6CTL_ADDRCTLPOLICY, CTL_EOL);
   1804 	sysctl_createv(clog, 0, NULL, NULL,
   1805 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1806 		       CTLTYPE_INT, "use_tempaddr",
   1807 		       SYSCTL_DESCR("Use temporary address"),
   1808 		       NULL, 0, &ip6_use_tempaddr, 0,
   1809 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1810 		       CTL_CREATE, CTL_EOL);
   1811 	sysctl_createv(clog, 0, NULL, NULL,
   1812 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1813 		       CTLTYPE_INT, "prefer_tempaddr",
   1814 		       SYSCTL_DESCR("Prefer temporary address as source "
   1815 		                    "address"),
   1816 		       NULL, 0, &ip6_prefer_tempaddr, 0,
   1817 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1818 		       CTL_CREATE, CTL_EOL);
   1819 	sysctl_createv(clog, 0, NULL, NULL,
   1820 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1821 		       CTLTYPE_INT, "temppltime",
   1822 		       SYSCTL_DESCR("preferred lifetime of a temporary address"),
   1823 		       NULL, 0, &ip6_temp_preferred_lifetime, 0,
   1824 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1825 		       CTL_CREATE, CTL_EOL);
   1826 	sysctl_createv(clog, 0, NULL, NULL,
   1827 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1828 		       CTLTYPE_INT, "tempvltime",
   1829 		       SYSCTL_DESCR("valid lifetime of a temporary address"),
   1830 		       NULL, 0, &ip6_temp_valid_lifetime, 0,
   1831 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1832 		       CTL_CREATE, CTL_EOL);
   1833 	sysctl_createv(clog, 0, NULL, NULL,
   1834 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1835 		       CTLTYPE_INT, "maxfrags",
   1836 		       SYSCTL_DESCR("Maximum fragments in reassembly queue"),
   1837 		       NULL, 0, &ip6_maxfrags, 0,
   1838 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1839 		       IPV6CTL_MAXFRAGS, CTL_EOL);
   1840 	sysctl_createv(clog, 0, NULL, NULL,
   1841 		       CTLFLAG_PERMANENT,
   1842 		       CTLTYPE_STRUCT, "stats",
   1843 		       SYSCTL_DESCR("IPv6 statistics"),
   1844 		       sysctl_net_inet6_ip6_stats, 0, NULL, 0,
   1845 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1846 		       IPV6CTL_STATS, CTL_EOL);
   1847 	sysctl_createv(clog, 0, NULL, NULL,
   1848 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1849 		       CTLTYPE_INT, "use_defaultzone",
   1850 		       SYSCTL_DESCR("Whether to use the default scope zones"),
   1851 		       NULL, 0, &ip6_use_defzone, 0,
   1852 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1853 		       IPV6CTL_USE_DEFAULTZONE, CTL_EOL);
   1854 	sysctl_createv(clog, 0, NULL, NULL,
   1855 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1856 		       CTLTYPE_INT, "mcast_pmtu",
   1857 		       SYSCTL_DESCR("Enable pMTU discovery for multicast packet"),
   1858 		       NULL, 0, &ip6_mcast_pmtu, 0,
   1859 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1860 		       CTL_CREATE, CTL_EOL);
   1861 	/* anonportalgo RFC6056 subtree */
   1862 	const struct sysctlnode *portalgo_node;
   1863 	sysctl_createv(clog, 0, NULL, &portalgo_node,
   1864 		       CTLFLAG_PERMANENT,
   1865 		       CTLTYPE_NODE, "anonportalgo",
   1866 		       SYSCTL_DESCR("Anonymous port algorithm selection (RFC 6056)"),
   1867 	    	       NULL, 0, NULL, 0,
   1868 		       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_CREATE, CTL_EOL);
   1869 	sysctl_createv(clog, 0, &portalgo_node, NULL,
   1870 		       CTLFLAG_PERMANENT,
   1871 		       CTLTYPE_STRING, "available",
   1872 		       SYSCTL_DESCR("available algorithms"),
   1873 		       sysctl_portalgo_available, 0, NULL, PORTALGO_MAXLEN,
   1874 		       CTL_CREATE, CTL_EOL);
   1875 	sysctl_createv(clog, 0, &portalgo_node, NULL,
   1876 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1877 		       CTLTYPE_STRING, "selected",
   1878 		       SYSCTL_DESCR("selected algorithm"),
   1879 	               sysctl_portalgo_selected6, 0, NULL, PORTALGO_MAXLEN,
   1880 		       CTL_CREATE, CTL_EOL);
   1881 	sysctl_createv(clog, 0, &portalgo_node, NULL,
   1882 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1883 		       CTLTYPE_STRUCT, "reserve",
   1884 		       SYSCTL_DESCR("bitmap of reserved ports"),
   1885 		       sysctl_portalgo_reserve6, 0, NULL, 0,
   1886 		       CTL_CREATE, CTL_EOL);
   1887 	sysctl_createv(clog, 0, NULL, NULL,
   1888 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1889 		       CTLTYPE_INT, "neighborgcthresh",
   1890 		       SYSCTL_DESCR("Maximum number of entries in neighbor"
   1891 			" cache"),
   1892 		       NULL, 1, &ip6_neighborgcthresh, 0,
   1893 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1894 		       CTL_CREATE, CTL_EOL);
   1895 	sysctl_createv(clog, 0, NULL, NULL,
   1896 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1897 		       CTLTYPE_INT, "maxifprefixes",
   1898 		       SYSCTL_DESCR("Maximum number of prefixes created by"
   1899 			   " route advertisement per interface"),
   1900 		       NULL, 1, &ip6_maxifprefixes, 0,
   1901 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1902 		       CTL_CREATE, CTL_EOL);
   1903 	sysctl_createv(clog, 0, NULL, NULL,
   1904 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1905 		       CTLTYPE_INT, "maxifdefrouters",
   1906 		       SYSCTL_DESCR("Maximum number of default routers created"
   1907 			   " by route advertisement per interface"),
   1908 		       NULL, 1, &ip6_maxifdefrouters, 0,
   1909 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1910 		       CTL_CREATE, CTL_EOL);
   1911 	sysctl_createv(clog, 0, NULL, NULL,
   1912 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1913 		       CTLTYPE_INT, "maxdynroutes",
   1914 		       SYSCTL_DESCR("Maximum number of routes created via"
   1915 			   " redirect"),
   1916 		       NULL, 1, &ip6_maxdynroutes, 0,
   1917 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1918 		       CTL_CREATE, CTL_EOL);
   1919 }
   1920 
   1921 void
   1922 ip6_statinc(u_int stat)
   1923 {
   1924 
   1925 	KASSERT(stat < IP6_NSTATS);
   1926 	IP6_STATINC(stat);
   1927 }
   1928