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