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