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