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