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