Home | History | Annotate | Line # | Download | only in netinet6
ip6_input.c revision 1.107
      1 /*	$NetBSD: ip6_input.c,v 1.107 2007/05/23 17:15:02 christos 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.107 2007/05/23 17:15:02 christos 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 struct callout 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 __P((void *));
    152 static struct m_tag *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *));
    153 
    154 static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
    155 static struct mbuf *ip6_pullexthdr __P((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()
    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);
    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);
    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()
    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_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
    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_key(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 	TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) {
    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)) {
    594 		struct in6_ifaddr *ia6;
    595 
    596 		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
    597 		if (ia6) {
    598 			if (!ip6_setdstifaddr(m, ia6)) {
    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 		}
    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(m->m_pkthdr.rcvif, ifs6_in_discard);
    620 #endif
    621 			return;	/* m have already been freed */
    622 		}
    623 
    624 		/* adjust pointer */
    625 		ip6 = mtod(m, struct ip6_hdr *);
    626 
    627 		/*
    628 		 * if the payload length field is 0 and the next header field
    629 		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
    630 		 * option MUST be included.
    631 		 */
    632 		if (ip6->ip6_plen == 0 && plen == 0) {
    633 			/*
    634 			 * Note that if a valid jumbo payload option is
    635 			 * contained, ip6_hopopts_input() must set a valid
    636 			 * (non-zero) payload length to the variable plen.
    637 			 */
    638 			ip6stat.ip6s_badoptions++;
    639 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
    640 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
    641 			icmp6_error(m, ICMP6_PARAM_PROB,
    642 				    ICMP6_PARAMPROB_HEADER,
    643 				    (char *)&ip6->ip6_plen - (char *)ip6);
    644 			return;
    645 		}
    646 		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
    647 			sizeof(struct ip6_hbh));
    648 		if (hbh == NULL) {
    649 			ip6stat.ip6s_tooshort++;
    650 			return;
    651 		}
    652 		KASSERT(IP6_HDR_ALIGNED_P(hbh));
    653 		nxt = hbh->ip6h_nxt;
    654 
    655 		/*
    656 		 * accept the packet if a router alert option is included
    657 		 * and we act as an IPv6 router.
    658 		 */
    659 		if (rtalert != ~0 && ip6_forwarding)
    660 			ours = 1;
    661 	} else
    662 		nxt = ip6->ip6_nxt;
    663 
    664 	/*
    665 	 * Check that the amount of data in the buffers
    666 	 * is as at least much as the IPv6 header would have us expect.
    667 	 * Trim mbufs if longer than we expect.
    668 	 * Drop packet if shorter than we expect.
    669 	 */
    670 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
    671 		ip6stat.ip6s_tooshort++;
    672 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
    673 		goto bad;
    674 	}
    675 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
    676 		if (m->m_len == m->m_pkthdr.len) {
    677 			m->m_len = sizeof(struct ip6_hdr) + plen;
    678 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
    679 		} else
    680 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
    681 	}
    682 
    683 	/*
    684 	 * Forward if desirable.
    685 	 */
    686 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    687 		/*
    688 		 * If we are acting as a multicast router, all
    689 		 * incoming multicast packets are passed to the
    690 		 * kernel-level multicast forwarding function.
    691 		 * The packet is returned (relatively) intact; if
    692 		 * ip6_mforward() returns a non-zero value, the packet
    693 		 * must be discarded, else it may be accepted below.
    694 		 */
    695 		if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
    696 			ip6stat.ip6s_cantforward++;
    697 			m_freem(m);
    698 			return;
    699 		}
    700 		if (!ours) {
    701 			m_freem(m);
    702 			return;
    703 		}
    704 	} else if (!ours) {
    705 		ip6_forward(m, srcrt);
    706 		return;
    707 	}
    708 
    709 	ip6 = mtod(m, struct ip6_hdr *);
    710 
    711 	/*
    712 	 * Malicious party may be able to use IPv4 mapped addr to confuse
    713 	 * tcp/udp stack and bypass security checks (act as if it was from
    714 	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
    715 	 *
    716 	 * For SIIT end node behavior, you may want to disable the check.
    717 	 * However, you will  become vulnerable to attacks using IPv4 mapped
    718 	 * source.
    719 	 */
    720 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    721 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    722 		ip6stat.ip6s_badscope++;
    723 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    724 		goto bad;
    725 	}
    726 
    727 	/*
    728 	 * Tell launch routine the next header
    729 	 */
    730 #ifdef IFA_STATS
    731 	if (deliverifp != NULL) {
    732 		struct in6_ifaddr *ia6;
    733 		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
    734 		if (ia6)
    735 			ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
    736 	}
    737 #endif
    738 	ip6stat.ip6s_delivered++;
    739 	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
    740 	nest = 0;
    741 
    742 	rh_present = 0;
    743 	while (nxt != IPPROTO_DONE) {
    744 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
    745 			ip6stat.ip6s_toomanyhdr++;
    746 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
    747 			goto bad;
    748 		}
    749 
    750 		/*
    751 		 * protection against faulty packet - there should be
    752 		 * more sanity checks in header chain processing.
    753 		 */
    754 		if (m->m_pkthdr.len < off) {
    755 			ip6stat.ip6s_tooshort++;
    756 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
    757 			goto bad;
    758 		}
    759 
    760 		if (nxt == IPPROTO_ROUTING) {
    761 			if (rh_present++) {
    762 				in6_ifstat_inc(m->m_pkthdr.rcvif,
    763 				    ifs6_in_hdrerr);
    764 				ip6stat.ip6s_badoptions++;
    765 				goto bad;
    766 			}
    767 		}
    768 
    769 #ifdef IPSEC
    770 		/*
    771 		 * enforce IPsec policy checking if we are seeing last header.
    772 		 * note that we do not visit this with protocols with pcb layer
    773 		 * code - like udp/tcp/raw ip.
    774 		 */
    775 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
    776 		    ipsec6_in_reject(m, NULL)) {
    777 			ipsec6stat.in_polvio++;
    778 			goto bad;
    779 		}
    780 #endif
    781 #ifdef FAST_IPSEC
    782 	/*
    783 	 * enforce IPsec policy checking if we are seeing last header.
    784 	 * note that we do not visit this with protocols with pcb layer
    785 	 * code - like udp/tcp/raw ip.
    786 	 */
    787 	if ((inet6sw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0) {
    788 		/*
    789 		 * Check if the packet has already had IPsec processing
    790 		 * done.  If so, then just pass it along.  This tag gets
    791 		 * set during AH, ESP, etc. input handling, before the
    792 		 * packet is returned to the ip input queue for delivery.
    793 		 */
    794 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
    795 		s = splsoftnet();
    796 		if (mtag != NULL) {
    797 			tdbi = (struct tdb_ident *)(mtag + 1);
    798 			sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
    799 		} else {
    800 			sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
    801 									IP_FORWARDING, &error);
    802 		}
    803 		if (sp != NULL) {
    804 			/*
    805 			 * Check security policy against packet attributes.
    806 			 */
    807 			error = ipsec_in_reject(sp, m);
    808 			KEY_FREESP(&sp);
    809 		} else {
    810 			/* XXX error stat??? */
    811 			error = EINVAL;
    812 			DPRINTF(("ip6_input: no SP, packet discarded\n"));/*XXX*/
    813 			goto bad;
    814 		}
    815 		splx(s);
    816 		if (error)
    817 			goto bad;
    818 	}
    819 #endif /* FAST_IPSEC */
    820 
    821 
    822 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
    823 	}
    824 	return;
    825  bad:
    826 	m_freem(m);
    827 }
    828 
    829 /*
    830  * set/grab in6_ifaddr correspond to IPv6 destination address.
    831  */
    832 static struct m_tag *
    833 ip6_setdstifaddr(struct mbuf *m, struct in6_ifaddr *ia6)
    834 {
    835 	struct m_tag *mtag;
    836 
    837 	mtag = ip6_addaux(m);
    838 	if (mtag)
    839 		((struct ip6aux *)(mtag + 1))->ip6a_dstia6 = ia6;
    840 	return mtag;	/* NULL if failed to set */
    841 }
    842 
    843 struct in6_ifaddr *
    844 ip6_getdstifaddr(struct mbuf *m)
    845 {
    846 	struct m_tag *mtag;
    847 
    848 	mtag = ip6_findaux(m);
    849 	if (mtag)
    850 		return ((struct ip6aux *)(mtag + 1))->ip6a_dstia6;
    851 	else
    852 		return NULL;
    853 }
    854 
    855 /*
    856  * Hop-by-Hop options header processing. If a valid jumbo payload option is
    857  * included, the real payload length will be stored in plenp.
    858  *
    859  * rtalertp - XXX: should be stored more smart way
    860  */
    861 static int
    862 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
    863 	struct mbuf **mp, int *offp)
    864 {
    865 	struct mbuf *m = *mp;
    866 	int off = *offp, hbhlen;
    867 	struct ip6_hbh *hbh;
    868 
    869 	/* validation of the length of the header */
    870 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
    871 		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
    872 	if (hbh == NULL) {
    873 		ip6stat.ip6s_tooshort++;
    874 		return -1;
    875 	}
    876 	hbhlen = (hbh->ip6h_len + 1) << 3;
    877 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
    878 		hbhlen);
    879 	if (hbh == NULL) {
    880 		ip6stat.ip6s_tooshort++;
    881 		return -1;
    882 	}
    883 	KASSERT(IP6_HDR_ALIGNED_P(hbh));
    884 	off += hbhlen;
    885 	hbhlen -= sizeof(struct ip6_hbh);
    886 
    887 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
    888 				hbhlen, rtalertp, plenp) < 0)
    889 		return (-1);
    890 
    891 	*offp = off;
    892 	*mp = m;
    893 	return (0);
    894 }
    895 
    896 /*
    897  * Search header for all Hop-by-hop options and process each option.
    898  * This function is separate from ip6_hopopts_input() in order to
    899  * handle a case where the sending node itself process its hop-by-hop
    900  * options header. In such a case, the function is called from ip6_output().
    901  *
    902  * The function assumes that hbh header is located right after the IPv6 header
    903  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
    904  * opthead + hbhlen is located in continuous memory region.
    905  */
    906 int
    907 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
    908 	u_int32_t *rtalertp, u_int32_t *plenp)
    909 {
    910 	struct ip6_hdr *ip6;
    911 	int optlen = 0;
    912 	u_int8_t *opt = opthead;
    913 	u_int16_t rtalert_val;
    914 	u_int32_t jumboplen;
    915 	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
    916 
    917 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
    918 		switch (*opt) {
    919 		case IP6OPT_PAD1:
    920 			optlen = 1;
    921 			break;
    922 		case IP6OPT_PADN:
    923 			if (hbhlen < IP6OPT_MINLEN) {
    924 				ip6stat.ip6s_toosmall++;
    925 				goto bad;
    926 			}
    927 			optlen = *(opt + 1) + 2;
    928 			break;
    929 		case IP6OPT_RTALERT:
    930 			/* XXX may need check for alignment */
    931 			if (hbhlen < IP6OPT_RTALERT_LEN) {
    932 				ip6stat.ip6s_toosmall++;
    933 				goto bad;
    934 			}
    935 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
    936 				/* XXX stat */
    937 				icmp6_error(m, ICMP6_PARAM_PROB,
    938 				    ICMP6_PARAMPROB_HEADER,
    939 				    erroff + opt + 1 - opthead);
    940 				return (-1);
    941 			}
    942 			optlen = IP6OPT_RTALERT_LEN;
    943 			bcopy((void *)(opt + 2), (void *)&rtalert_val, 2);
    944 			*rtalertp = ntohs(rtalert_val);
    945 			break;
    946 		case IP6OPT_JUMBO:
    947 			/* XXX may need check for alignment */
    948 			if (hbhlen < IP6OPT_JUMBO_LEN) {
    949 				ip6stat.ip6s_toosmall++;
    950 				goto bad;
    951 			}
    952 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
    953 				/* XXX stat */
    954 				icmp6_error(m, ICMP6_PARAM_PROB,
    955 				    ICMP6_PARAMPROB_HEADER,
    956 				    erroff + opt + 1 - opthead);
    957 				return (-1);
    958 			}
    959 			optlen = IP6OPT_JUMBO_LEN;
    960 
    961 			/*
    962 			 * IPv6 packets that have non 0 payload length
    963 			 * must not contain a jumbo payload option.
    964 			 */
    965 			ip6 = mtod(m, struct ip6_hdr *);
    966 			if (ip6->ip6_plen) {
    967 				ip6stat.ip6s_badoptions++;
    968 				icmp6_error(m, ICMP6_PARAM_PROB,
    969 				    ICMP6_PARAMPROB_HEADER,
    970 				    erroff + opt - opthead);
    971 				return (-1);
    972 			}
    973 
    974 			/*
    975 			 * We may see jumbolen in unaligned location, so
    976 			 * we'd need to perform bcopy().
    977 			 */
    978 			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
    979 			jumboplen = (u_int32_t)htonl(jumboplen);
    980 
    981 #if 1
    982 			/*
    983 			 * if there are multiple jumbo payload options,
    984 			 * *plenp will be non-zero and the packet will be
    985 			 * rejected.
    986 			 * the behavior may need some debate in ipngwg -
    987 			 * multiple options does not make sense, however,
    988 			 * there's no explicit mention in specification.
    989 			 */
    990 			if (*plenp != 0) {
    991 				ip6stat.ip6s_badoptions++;
    992 				icmp6_error(m, ICMP6_PARAM_PROB,
    993 				    ICMP6_PARAMPROB_HEADER,
    994 				    erroff + opt + 2 - opthead);
    995 				return (-1);
    996 			}
    997 #endif
    998 
    999 			/*
   1000 			 * jumbo payload length must be larger than 65535.
   1001 			 */
   1002 			if (jumboplen <= IPV6_MAXPACKET) {
   1003 				ip6stat.ip6s_badoptions++;
   1004 				icmp6_error(m, ICMP6_PARAM_PROB,
   1005 				    ICMP6_PARAMPROB_HEADER,
   1006 				    erroff + opt + 2 - opthead);
   1007 				return (-1);
   1008 			}
   1009 			*plenp = jumboplen;
   1010 
   1011 			break;
   1012 		default:		/* unknown option */
   1013 			if (hbhlen < IP6OPT_MINLEN) {
   1014 				ip6stat.ip6s_toosmall++;
   1015 				goto bad;
   1016 			}
   1017 			optlen = ip6_unknown_opt(opt, m,
   1018 			    erroff + opt - opthead);
   1019 			if (optlen == -1)
   1020 				return (-1);
   1021 			optlen += 2;
   1022 			break;
   1023 		}
   1024 	}
   1025 
   1026 	return (0);
   1027 
   1028   bad:
   1029 	m_freem(m);
   1030 	return (-1);
   1031 }
   1032 
   1033 /*
   1034  * Unknown option processing.
   1035  * The third argument `off' is the offset from the IPv6 header to the option,
   1036  * which is necessary if the IPv6 header the and option header and IPv6 header
   1037  * is not continuous in order to return an ICMPv6 error.
   1038  */
   1039 int
   1040 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
   1041 {
   1042 	struct ip6_hdr *ip6;
   1043 
   1044 	switch (IP6OPT_TYPE(*optp)) {
   1045 	case IP6OPT_TYPE_SKIP: /* ignore the option */
   1046 		return ((int)*(optp + 1));
   1047 	case IP6OPT_TYPE_DISCARD:	/* silently discard */
   1048 		m_freem(m);
   1049 		return (-1);
   1050 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
   1051 		ip6stat.ip6s_badoptions++;
   1052 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
   1053 		return (-1);
   1054 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
   1055 		ip6stat.ip6s_badoptions++;
   1056 		ip6 = mtod(m, struct ip6_hdr *);
   1057 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
   1058 		    (m->m_flags & (M_BCAST|M_MCAST)))
   1059 			m_freem(m);
   1060 		else
   1061 			icmp6_error(m, ICMP6_PARAM_PROB,
   1062 				    ICMP6_PARAMPROB_OPTION, off);
   1063 		return (-1);
   1064 	}
   1065 
   1066 	m_freem(m);		/* XXX: NOTREACHED */
   1067 	return (-1);
   1068 }
   1069 
   1070 /*
   1071  * Create the "control" list for this pcb.
   1072  *
   1073  * The routine will be called from upper layer handlers like tcp6_input().
   1074  * Thus the routine assumes that the caller (tcp6_input) have already
   1075  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
   1076  * very first mbuf on the mbuf chain.
   1077  * We may want to add some infinite loop prevention or sanity checks for safety.
   1078  * (This applies only when you are using KAME mbuf chain restriction, i.e.
   1079  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
   1080  */
   1081 void
   1082 ip6_savecontrol(struct in6pcb *in6p, struct mbuf **mp,
   1083 	struct ip6_hdr *ip6, struct mbuf *m)
   1084 {
   1085 #ifdef RFC2292
   1086 #define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
   1087 #else
   1088 #define IS2292(x, y)	(y)
   1089 #endif
   1090 
   1091 #ifdef SO_TIMESTAMP
   1092 	if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
   1093 		struct timeval tv;
   1094 
   1095 		microtime(&tv);
   1096 		*mp = sbcreatecontrol((void *) &tv, sizeof(tv),
   1097 		    SCM_TIMESTAMP, SOL_SOCKET);
   1098 		if (*mp)
   1099 			mp = &(*mp)->m_next;
   1100 	}
   1101 #endif
   1102 
   1103 	/* some OSes call this logic with IPv4 packet, for SO_TIMESTAMP */
   1104 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
   1105 		return;
   1106 
   1107 	/* RFC 2292 sec. 5 */
   1108 	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
   1109 		struct in6_pktinfo pi6;
   1110 
   1111 		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
   1112 		in6_clearscope(&pi6.ipi6_addr);	/* XXX */
   1113 		pi6.ipi6_ifindex = m->m_pkthdr.rcvif ?
   1114 		    m->m_pkthdr.rcvif->if_index : 0;
   1115 		*mp = sbcreatecontrol((void *) &pi6,
   1116 		    sizeof(struct in6_pktinfo),
   1117 		    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
   1118 		if (*mp)
   1119 			mp = &(*mp)->m_next;
   1120 	}
   1121 
   1122 	if (in6p->in6p_flags & IN6P_HOPLIMIT) {
   1123 		int hlim = ip6->ip6_hlim & 0xff;
   1124 
   1125 		*mp = sbcreatecontrol((void *) &hlim, sizeof(int),
   1126 		    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
   1127 		if (*mp)
   1128 			mp = &(*mp)->m_next;
   1129 	}
   1130 
   1131 	if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
   1132 		u_int32_t flowinfo;
   1133 		int tclass;
   1134 
   1135 		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
   1136 		flowinfo >>= 20;
   1137 
   1138 		tclass = flowinfo & 0xff;
   1139 		*mp = sbcreatecontrol((void *)&tclass, sizeof(tclass),
   1140 		    IPV6_TCLASS, IPPROTO_IPV6);
   1141 
   1142 		if (*mp)
   1143 			mp = &(*mp)->m_next;
   1144 	}
   1145 
   1146 	/*
   1147 	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
   1148 	 * privilege for the option (see ip6_ctloutput), but it might be too
   1149 	 * strict, since there might be some hop-by-hop options which can be
   1150 	 * returned to normal user.
   1151 	 * See also RFC3542 section 8 (or RFC2292 section 6).
   1152 	 */
   1153 	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
   1154 		/*
   1155 		 * Check if a hop-by-hop options header is contatined in the
   1156 		 * received packet, and if so, store the options as ancillary
   1157 		 * data. Note that a hop-by-hop options header must be
   1158 		 * just after the IPv6 header, which fact is assured through
   1159 		 * the IPv6 input processing.
   1160 		 */
   1161 		struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
   1162 		if (xip6->ip6_nxt == IPPROTO_HOPOPTS) {
   1163 			struct ip6_hbh *hbh;
   1164 			int hbhlen;
   1165 			struct mbuf *ext;
   1166 
   1167 			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
   1168 			    xip6->ip6_nxt);
   1169 			if (ext == NULL) {
   1170 				ip6stat.ip6s_tooshort++;
   1171 				return;
   1172 			}
   1173 			hbh = mtod(ext, struct ip6_hbh *);
   1174 			hbhlen = (hbh->ip6h_len + 1) << 3;
   1175 			if (hbhlen != ext->m_len) {
   1176 				m_freem(ext);
   1177 				ip6stat.ip6s_tooshort++;
   1178 				return;
   1179 			}
   1180 
   1181 			/*
   1182 			 * XXX: We copy whole the header even if a jumbo
   1183 			 * payload option is included, which option is to
   1184 			 * be removed before returning in the RFC 2292.
   1185 			 * Note: this constraint is removed in RFC3542.
   1186 			 */
   1187 			*mp = sbcreatecontrol((void *)hbh, hbhlen,
   1188 			    IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
   1189 			    IPPROTO_IPV6);
   1190 			if (*mp)
   1191 				mp = &(*mp)->m_next;
   1192 			m_freem(ext);
   1193 		}
   1194 	}
   1195 
   1196 	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
   1197 	if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
   1198 		struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
   1199 		int nxt = xip6->ip6_nxt, off = sizeof(struct ip6_hdr);
   1200 
   1201 		/*
   1202 		 * Search for destination options headers or routing
   1203 		 * header(s) through the header chain, and stores each
   1204 		 * header as ancillary data.
   1205 		 * Note that the order of the headers remains in
   1206 		 * the chain of ancillary data.
   1207 		 */
   1208 		for (;;) {	/* is explicit loop prevention necessary? */
   1209 			struct ip6_ext *ip6e = NULL;
   1210 			int elen;
   1211 			struct mbuf *ext = NULL;
   1212 
   1213 			/*
   1214 			 * if it is not an extension header, don't try to
   1215 			 * pull it from the chain.
   1216 			 */
   1217 			switch (nxt) {
   1218 			case IPPROTO_DSTOPTS:
   1219 			case IPPROTO_ROUTING:
   1220 			case IPPROTO_HOPOPTS:
   1221 			case IPPROTO_AH: /* is it possible? */
   1222 				break;
   1223 			default:
   1224 				goto loopend;
   1225 			}
   1226 
   1227 			ext = ip6_pullexthdr(m, off, nxt);
   1228 			if (ext == NULL) {
   1229 				ip6stat.ip6s_tooshort++;
   1230 				return;
   1231 			}
   1232 			ip6e = mtod(ext, struct ip6_ext *);
   1233 			if (nxt == IPPROTO_AH)
   1234 				elen = (ip6e->ip6e_len + 2) << 2;
   1235 			else
   1236 				elen = (ip6e->ip6e_len + 1) << 3;
   1237 			if (elen != ext->m_len) {
   1238 				m_freem(ext);
   1239 				ip6stat.ip6s_tooshort++;
   1240 				return;
   1241 			}
   1242 			KASSERT(IP6_HDR_ALIGNED_P(ip6e));
   1243 
   1244 			switch (nxt) {
   1245 			case IPPROTO_DSTOPTS:
   1246 				if (!in6p->in6p_flags & IN6P_DSTOPTS)
   1247 					break;
   1248 
   1249 				*mp = sbcreatecontrol((void *)ip6e, elen,
   1250 				    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
   1251 				    IPPROTO_IPV6);
   1252 				if (*mp)
   1253 					mp = &(*mp)->m_next;
   1254 				break;
   1255 
   1256 			case IPPROTO_ROUTING:
   1257 				if (!in6p->in6p_flags & IN6P_RTHDR)
   1258 					break;
   1259 
   1260 				*mp = sbcreatecontrol((void *)ip6e, elen,
   1261 				    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
   1262 				    IPPROTO_IPV6);
   1263 				if (*mp)
   1264 					mp = &(*mp)->m_next;
   1265 				break;
   1266 
   1267 			case IPPROTO_HOPOPTS:
   1268 			case IPPROTO_AH: /* is it possible? */
   1269 				break;
   1270 
   1271 			default:
   1272 				/*
   1273 			 	 * other cases have been filtered in the above.
   1274 				 * none will visit this case.  here we supply
   1275 				 * the code just in case (nxt overwritten or
   1276 				 * other cases).
   1277 				 */
   1278 				m_freem(ext);
   1279 				goto loopend;
   1280 
   1281 			}
   1282 
   1283 			/* proceed with the next header. */
   1284 			off += elen;
   1285 			nxt = ip6e->ip6e_nxt;
   1286 			ip6e = NULL;
   1287 			m_freem(ext);
   1288 			ext = NULL;
   1289 		}
   1290 	  loopend:
   1291 	  	;
   1292 	}
   1293 }
   1294 #undef IS2292
   1295 
   1296 
   1297 void
   1298 ip6_notify_pmtu(struct in6pcb *in6p, const struct sockaddr_in6 *dst,
   1299     uint32_t *mtu)
   1300 {
   1301 	struct socket *so;
   1302 	struct mbuf *m_mtu;
   1303 	struct ip6_mtuinfo mtuctl;
   1304 
   1305 	so = in6p->in6p_socket;
   1306 
   1307 	if (mtu == NULL)
   1308 		return;
   1309 
   1310 #ifdef DIAGNOSTIC
   1311 	if (so == NULL)		/* I believe this is impossible */
   1312 		panic("ip6_notify_pmtu: socket is NULL");
   1313 #endif
   1314 
   1315 	memset(&mtuctl, 0, sizeof(mtuctl));	/* zero-clear for safety */
   1316 	mtuctl.ip6m_mtu = *mtu;
   1317 	mtuctl.ip6m_addr = *dst;
   1318 	if (sa6_recoverscope(&mtuctl.ip6m_addr))
   1319 		return;
   1320 
   1321 	if ((m_mtu = sbcreatecontrol((void *)&mtuctl, sizeof(mtuctl),
   1322 	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
   1323 		return;
   1324 
   1325 	if (sbappendaddr(&so->so_rcv, (const struct sockaddr *)dst, NULL, m_mtu)
   1326 	    == 0) {
   1327 		m_freem(m_mtu);
   1328 		/* XXX: should count statistics */
   1329 	} else
   1330 		sorwakeup(so);
   1331 
   1332 	return;
   1333 }
   1334 
   1335 /*
   1336  * pull single extension header from mbuf chain.  returns single mbuf that
   1337  * contains the result, or NULL on error.
   1338  */
   1339 static struct mbuf *
   1340 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
   1341 {
   1342 	struct ip6_ext ip6e;
   1343 	size_t elen;
   1344 	struct mbuf *n;
   1345 
   1346 #ifdef DIAGNOSTIC
   1347 	switch (nxt) {
   1348 	case IPPROTO_DSTOPTS:
   1349 	case IPPROTO_ROUTING:
   1350 	case IPPROTO_HOPOPTS:
   1351 	case IPPROTO_AH: /* is it possible? */
   1352 		break;
   1353 	default:
   1354 		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
   1355 	}
   1356 #endif
   1357 
   1358 	m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
   1359 	if (nxt == IPPROTO_AH)
   1360 		elen = (ip6e.ip6e_len + 2) << 2;
   1361 	else
   1362 		elen = (ip6e.ip6e_len + 1) << 3;
   1363 
   1364 	MGET(n, M_DONTWAIT, MT_DATA);
   1365 	if (n && elen >= MLEN) {
   1366 		MCLGET(n, M_DONTWAIT);
   1367 		if ((n->m_flags & M_EXT) == 0) {
   1368 			m_free(n);
   1369 			n = NULL;
   1370 		}
   1371 	}
   1372 	if (!n)
   1373 		return NULL;
   1374 
   1375 	n->m_len = 0;
   1376 	if (elen >= M_TRAILINGSPACE(n)) {
   1377 		m_free(n);
   1378 		return NULL;
   1379 	}
   1380 
   1381 	m_copydata(m, off, elen, mtod(n, void *));
   1382 	n->m_len = elen;
   1383 	return n;
   1384 }
   1385 
   1386 /*
   1387  * Get pointer to the previous header followed by the header
   1388  * currently processed.
   1389  * XXX: This function supposes that
   1390  *	M includes all headers,
   1391  *	the next header field and the header length field of each header
   1392  *	are valid, and
   1393  *	the sum of each header length equals to OFF.
   1394  * Because of these assumptions, this function must be called very
   1395  * carefully. Moreover, it will not be used in the near future when
   1396  * we develop `neater' mechanism to process extension headers.
   1397  */
   1398 u_int8_t *
   1399 ip6_get_prevhdr(struct mbuf *m, int off)
   1400 {
   1401 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   1402 
   1403 	if (off == sizeof(struct ip6_hdr))
   1404 		return (&ip6->ip6_nxt);
   1405 	else {
   1406 		int len, nxt;
   1407 		struct ip6_ext *ip6e = NULL;
   1408 
   1409 		nxt = ip6->ip6_nxt;
   1410 		len = sizeof(struct ip6_hdr);
   1411 		while (len < off) {
   1412 			ip6e = (struct ip6_ext *)(mtod(m, char *) + len);
   1413 
   1414 			switch (nxt) {
   1415 			case IPPROTO_FRAGMENT:
   1416 				len += sizeof(struct ip6_frag);
   1417 				break;
   1418 			case IPPROTO_AH:
   1419 				len += (ip6e->ip6e_len + 2) << 2;
   1420 				break;
   1421 			default:
   1422 				len += (ip6e->ip6e_len + 1) << 3;
   1423 				break;
   1424 			}
   1425 			nxt = ip6e->ip6e_nxt;
   1426 		}
   1427 		if (ip6e)
   1428 			return (&ip6e->ip6e_nxt);
   1429 		else
   1430 			return NULL;
   1431 	}
   1432 }
   1433 
   1434 /*
   1435  * get next header offset.  m will be retained.
   1436  */
   1437 int
   1438 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
   1439 {
   1440 	struct ip6_hdr ip6;
   1441 	struct ip6_ext ip6e;
   1442 	struct ip6_frag fh;
   1443 
   1444 	/* just in case */
   1445 	if (m == NULL)
   1446 		panic("ip6_nexthdr: m == NULL");
   1447 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
   1448 		return -1;
   1449 
   1450 	switch (proto) {
   1451 	case IPPROTO_IPV6:
   1452 		/* do not chase beyond intermediate IPv6 headers */
   1453 		if (off != 0)
   1454 			return -1;
   1455 		if (m->m_pkthdr.len < off + sizeof(ip6))
   1456 			return -1;
   1457 		m_copydata(m, off, sizeof(ip6), (void *)&ip6);
   1458 		if (nxtp)
   1459 			*nxtp = ip6.ip6_nxt;
   1460 		off += sizeof(ip6);
   1461 		return off;
   1462 
   1463 	case IPPROTO_FRAGMENT:
   1464 		/*
   1465 		 * terminate parsing if it is not the first fragment,
   1466 		 * it does not make sense to parse through it.
   1467 		 */
   1468 		if (m->m_pkthdr.len < off + sizeof(fh))
   1469 			return -1;
   1470 		m_copydata(m, off, sizeof(fh), (void *)&fh);
   1471 		if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0)
   1472 			return -1;
   1473 		if (nxtp)
   1474 			*nxtp = fh.ip6f_nxt;
   1475 		off += sizeof(struct ip6_frag);
   1476 		return off;
   1477 
   1478 	case IPPROTO_AH:
   1479 		if (m->m_pkthdr.len < off + sizeof(ip6e))
   1480 			return -1;
   1481 		m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
   1482 		if (nxtp)
   1483 			*nxtp = ip6e.ip6e_nxt;
   1484 		off += (ip6e.ip6e_len + 2) << 2;
   1485 		if (m->m_pkthdr.len < off)
   1486 			return -1;
   1487 		return off;
   1488 
   1489 	case IPPROTO_HOPOPTS:
   1490 	case IPPROTO_ROUTING:
   1491 	case IPPROTO_DSTOPTS:
   1492 		if (m->m_pkthdr.len < off + sizeof(ip6e))
   1493 			return -1;
   1494 		m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
   1495 		if (nxtp)
   1496 			*nxtp = ip6e.ip6e_nxt;
   1497 		off += (ip6e.ip6e_len + 1) << 3;
   1498 		if (m->m_pkthdr.len < off)
   1499 			return -1;
   1500 		return off;
   1501 
   1502 	case IPPROTO_NONE:
   1503 	case IPPROTO_ESP:
   1504 	case IPPROTO_IPCOMP:
   1505 		/* give up */
   1506 		return -1;
   1507 
   1508 	default:
   1509 		return -1;
   1510 	}
   1511 }
   1512 
   1513 /*
   1514  * get offset for the last header in the chain.  m will be kept untainted.
   1515  */
   1516 int
   1517 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
   1518 {
   1519 	int newoff;
   1520 	int nxt;
   1521 
   1522 	if (!nxtp) {
   1523 		nxt = -1;
   1524 		nxtp = &nxt;
   1525 	}
   1526 	for (;;) {
   1527 		newoff = ip6_nexthdr(m, off, proto, nxtp);
   1528 		if (newoff < 0)
   1529 			return off;
   1530 		else if (newoff < off)
   1531 			return -1;	/* invalid */
   1532 		else if (newoff == off)
   1533 			return newoff;
   1534 
   1535 		off = newoff;
   1536 		proto = *nxtp;
   1537 	}
   1538 }
   1539 
   1540 struct m_tag *
   1541 ip6_addaux(struct mbuf *m)
   1542 {
   1543 	struct m_tag *mtag;
   1544 
   1545 	mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
   1546 	if (!mtag) {
   1547 		mtag = m_tag_get(PACKET_TAG_INET6, sizeof(struct ip6aux),
   1548 		    M_NOWAIT);
   1549 		if (mtag) {
   1550 			m_tag_prepend(m, mtag);
   1551 			bzero(mtag + 1, sizeof(struct ip6aux));
   1552 		}
   1553 	}
   1554 	return mtag;
   1555 }
   1556 
   1557 struct m_tag *
   1558 ip6_findaux(struct mbuf *m)
   1559 {
   1560 	struct m_tag *mtag;
   1561 
   1562 	mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
   1563 	return mtag;
   1564 }
   1565 
   1566 void
   1567 ip6_delaux(struct mbuf *m)
   1568 {
   1569 	struct m_tag *mtag;
   1570 
   1571 	mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
   1572 	if (mtag)
   1573 		m_tag_delete(m, mtag);
   1574 }
   1575 
   1576 #ifdef GATEWAY
   1577 /*
   1578  * sysctl helper routine for net.inet.ip6.maxflows. Since
   1579  * we could reduce this value, call ip6flow_reap();
   1580  */
   1581 static int
   1582 sysctl_net_inet6_ip6_maxflows(SYSCTLFN_ARGS)
   1583 {
   1584         int s;
   1585 
   1586         s = sysctl_lookup(SYSCTLFN_CALL(rnode));
   1587         if (s || newp == NULL)
   1588                 return (s);
   1589 
   1590         s = splsoftnet();
   1591         ip6flow_reap(0);
   1592         splx(s);
   1593 
   1594         return (0);
   1595 }
   1596 
   1597 static int
   1598 sysctl_net_inet6_ip6_hashsize(SYSCTLFN_ARGS)
   1599 {
   1600 	int error, tmp;
   1601 	struct sysctlnode node;
   1602 
   1603 	node = *rnode;
   1604 	tmp = ip6_hashsize;
   1605 	node.sysctl_data = &tmp;
   1606 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1607 	if (error || newp == NULL)
   1608 		return (error);
   1609 
   1610 	if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
   1611 		/*
   1612 		 * Can only fail due to malloc()
   1613 		 */
   1614 		if (ip6flow_invalidate_all(tmp))
   1615 			return ENOMEM;
   1616 	} else {
   1617 		/*
   1618 		 * EINVAL if not a power of 2
   1619 	         */
   1620 		return EINVAL;
   1621 	}
   1622 
   1623 	return (0);
   1624 }
   1625 #endif /* GATEWAY */
   1626 
   1627 /*
   1628  * System control for IP6
   1629  */
   1630 
   1631 u_char	inet6ctlerrmap[PRC_NCMDS] = {
   1632 	0,		0,		0,		0,
   1633 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
   1634 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
   1635 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
   1636 	0,		0,		0,		0,
   1637 	ENOPROTOOPT
   1638 };
   1639 
   1640 SYSCTL_SETUP(sysctl_net_inet6_ip6_setup, "sysctl net.inet6.ip6 subtree setup")
   1641 {
   1642 #ifdef RFC2292
   1643 #define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
   1644 #else
   1645 #define IS2292(x, y)	(y)
   1646 #endif
   1647 
   1648 	sysctl_createv(clog, 0, NULL, NULL,
   1649 		       CTLFLAG_PERMANENT,
   1650 		       CTLTYPE_NODE, "net", NULL,
   1651 		       NULL, 0, NULL, 0,
   1652 		       CTL_NET, CTL_EOL);
   1653 	sysctl_createv(clog, 0, NULL, NULL,
   1654 		       CTLFLAG_PERMANENT,
   1655 		       CTLTYPE_NODE, "inet6",
   1656 		       SYSCTL_DESCR("PF_INET6 related settings"),
   1657 		       NULL, 0, NULL, 0,
   1658 		       CTL_NET, PF_INET6, CTL_EOL);
   1659 	sysctl_createv(clog, 0, NULL, NULL,
   1660 		       CTLFLAG_PERMANENT,
   1661 		       CTLTYPE_NODE, "ip6",
   1662 		       SYSCTL_DESCR("IPv6 related settings"),
   1663 		       NULL, 0, NULL, 0,
   1664 		       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_EOL);
   1665 
   1666 	sysctl_createv(clog, 0, NULL, NULL,
   1667 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1668 		       CTLTYPE_INT, "forwarding",
   1669 		       SYSCTL_DESCR("Enable forwarding of INET6 datagrams"),
   1670 		       NULL, 0, &ip6_forwarding, 0,
   1671 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1672 		       IPV6CTL_FORWARDING, CTL_EOL);
   1673 	sysctl_createv(clog, 0, NULL, NULL,
   1674 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1675 		       CTLTYPE_INT, "redirect",
   1676 		       SYSCTL_DESCR("Enable sending of ICMPv6 redirect messages"),
   1677 		       NULL, 0, &ip6_sendredirects, 0,
   1678 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1679 		       IPV6CTL_SENDREDIRECTS, CTL_EOL);
   1680 	sysctl_createv(clog, 0, NULL, NULL,
   1681 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1682 		       CTLTYPE_INT, "hlim",
   1683 		       SYSCTL_DESCR("Hop limit for an INET6 datagram"),
   1684 		       NULL, 0, &ip6_defhlim, 0,
   1685 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1686 		       IPV6CTL_DEFHLIM, CTL_EOL);
   1687 #ifdef notyet
   1688 	sysctl_createv(clog, 0, NULL, NULL,
   1689 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1690 		       CTLTYPE_INT, "mtu", NULL,
   1691 		       NULL, 0, &, 0,
   1692 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1693 		       IPV6CTL_DEFMTU, CTL_EOL);
   1694 #endif
   1695 #ifdef __no_idea__
   1696 	sysctl_createv(clog, 0, NULL, NULL,
   1697 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1698 		       CTLTYPE_INT, "forwsrcrt", NULL,
   1699 		       NULL, 0, &?, 0,
   1700 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1701 		       IPV6CTL_FORWSRCRT, CTL_EOL);
   1702 	sysctl_createv(clog, 0, NULL, NULL,
   1703 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1704 		       CTLTYPE_STRUCT, "mrtstats", NULL,
   1705 		       NULL, 0, &?, sizeof(?),
   1706 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1707 		       IPV6CTL_MRTSTATS, CTL_EOL);
   1708 	sysctl_createv(clog, 0, NULL, NULL,
   1709 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1710 		       CTLTYPE_?, "mrtproto", NULL,
   1711 		       NULL, 0, &?, sizeof(?),
   1712 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1713 		       IPV6CTL_MRTPROTO, CTL_EOL);
   1714 #endif
   1715 	sysctl_createv(clog, 0, NULL, NULL,
   1716 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1717 		       CTLTYPE_INT, "maxfragpackets",
   1718 		       SYSCTL_DESCR("Maximum number of fragments to buffer "
   1719 				    "for reassembly"),
   1720 		       NULL, 0, &ip6_maxfragpackets, 0,
   1721 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1722 		       IPV6CTL_MAXFRAGPACKETS, CTL_EOL);
   1723 #ifdef __no_idea__
   1724 	sysctl_createv(clog, 0, NULL, NULL,
   1725 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1726 		       CTLTYPE_INT, "sourcecheck", NULL,
   1727 		       NULL, 0, &?, 0,
   1728 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1729 		       IPV6CTL_SOURCECHECK, CTL_EOL);
   1730 	sysctl_createv(clog, 0, NULL, NULL,
   1731 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1732 		       CTLTYPE_INT, "sourcecheck_logint", NULL,
   1733 		       NULL, 0, &?, 0,
   1734 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1735 		       IPV6CTL_SOURCECHECK_LOGINT, CTL_EOL);
   1736 #endif
   1737 	sysctl_createv(clog, 0, NULL, NULL,
   1738 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1739 		       CTLTYPE_INT, "accept_rtadv",
   1740 		       SYSCTL_DESCR("Accept router advertisements"),
   1741 		       NULL, 0, &ip6_accept_rtadv, 0,
   1742 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1743 		       IPV6CTL_ACCEPT_RTADV, CTL_EOL);
   1744 	sysctl_createv(clog, 0, NULL, NULL,
   1745 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1746 		       CTLTYPE_INT, "keepfaith",
   1747 		       SYSCTL_DESCR("Activate faith interface"),
   1748 		       NULL, 0, &ip6_keepfaith, 0,
   1749 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1750 		       IPV6CTL_KEEPFAITH, CTL_EOL);
   1751 	sysctl_createv(clog, 0, NULL, NULL,
   1752 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1753 		       CTLTYPE_INT, "log_interval",
   1754 		       SYSCTL_DESCR("Minumum interval between logging "
   1755 				    "unroutable packets"),
   1756 		       NULL, 0, &ip6_log_interval, 0,
   1757 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1758 		       IPV6CTL_LOG_INTERVAL, CTL_EOL);
   1759 	sysctl_createv(clog, 0, NULL, NULL,
   1760 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1761 		       CTLTYPE_INT, "hdrnestlimit",
   1762 		       SYSCTL_DESCR("Maximum number of nested IPv6 headers"),
   1763 		       NULL, 0, &ip6_hdrnestlimit, 0,
   1764 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1765 		       IPV6CTL_HDRNESTLIMIT, CTL_EOL);
   1766 	sysctl_createv(clog, 0, NULL, NULL,
   1767 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1768 		       CTLTYPE_INT, "dad_count",
   1769 		       SYSCTL_DESCR("Number of Duplicate Address Detection "
   1770 				    "probes to send"),
   1771 		       NULL, 0, &ip6_dad_count, 0,
   1772 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1773 		       IPV6CTL_DAD_COUNT, CTL_EOL);
   1774 	sysctl_createv(clog, 0, NULL, NULL,
   1775 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1776 		       CTLTYPE_INT, "auto_flowlabel",
   1777 		       SYSCTL_DESCR("Assign random IPv6 flow labels"),
   1778 		       NULL, 0, &ip6_auto_flowlabel, 0,
   1779 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1780 		       IPV6CTL_AUTO_FLOWLABEL, CTL_EOL);
   1781 	sysctl_createv(clog, 0, NULL, NULL,
   1782 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1783 		       CTLTYPE_INT, "defmcasthlim",
   1784 		       SYSCTL_DESCR("Default multicast hop limit"),
   1785 		       NULL, 0, &ip6_defmcasthlim, 0,
   1786 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1787 		       IPV6CTL_DEFMCASTHLIM, CTL_EOL);
   1788 #if NGIF > 0
   1789 	sysctl_createv(clog, 0, NULL, NULL,
   1790 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1791 		       CTLTYPE_INT, "gifhlim",
   1792 		       SYSCTL_DESCR("Default hop limit for a gif tunnel datagram"),
   1793 		       NULL, 0, &ip6_gif_hlim, 0,
   1794 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1795 		       IPV6CTL_GIF_HLIM, CTL_EOL);
   1796 #endif /* NGIF */
   1797 	sysctl_createv(clog, 0, NULL, NULL,
   1798 		       CTLFLAG_PERMANENT,
   1799 		       CTLTYPE_STRING, "kame_version",
   1800 		       SYSCTL_DESCR("KAME Version"),
   1801 		       NULL, 0, __UNCONST(__KAME_VERSION), 0,
   1802 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1803 		       IPV6CTL_KAME_VERSION, CTL_EOL);
   1804 	sysctl_createv(clog, 0, NULL, NULL,
   1805 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1806 		       CTLTYPE_INT, "use_deprecated",
   1807 		       SYSCTL_DESCR("Allow use of deprecated addresses as "
   1808 				    "source addresses"),
   1809 		       NULL, 0, &ip6_use_deprecated, 0,
   1810 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1811 		       IPV6CTL_USE_DEPRECATED, CTL_EOL);
   1812 	sysctl_createv(clog, 0, NULL, NULL,
   1813 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1814 		       CTLTYPE_INT, "rr_prune", NULL,
   1815 		       NULL, 0, &ip6_rr_prune, 0,
   1816 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1817 		       IPV6CTL_RR_PRUNE, CTL_EOL);
   1818 	sysctl_createv(clog, 0, NULL, NULL,
   1819 		       CTLFLAG_PERMANENT
   1820 #ifndef INET6_BINDV6ONLY
   1821 		       |CTLFLAG_READWRITE,
   1822 #endif
   1823 		       CTLTYPE_INT, "v6only",
   1824 		       SYSCTL_DESCR("Disallow PF_INET6 sockets from connecting "
   1825 				    "to PF_INET sockets"),
   1826 		       NULL, 0, &ip6_v6only, 0,
   1827 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1828 		       IPV6CTL_V6ONLY, CTL_EOL);
   1829 	sysctl_createv(clog, 0, NULL, NULL,
   1830 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1831 		       CTLTYPE_INT, "anonportmin",
   1832 		       SYSCTL_DESCR("Lowest ephemeral port number to assign"),
   1833 		       sysctl_net_inet_ip_ports, 0, &ip6_anonportmin, 0,
   1834 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1835 		       IPV6CTL_ANONPORTMIN, CTL_EOL);
   1836 	sysctl_createv(clog, 0, NULL, NULL,
   1837 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1838 		       CTLTYPE_INT, "anonportmax",
   1839 		       SYSCTL_DESCR("Highest ephemeral port number to assign"),
   1840 		       sysctl_net_inet_ip_ports, 0, &ip6_anonportmax, 0,
   1841 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1842 		       IPV6CTL_ANONPORTMAX, CTL_EOL);
   1843 #ifndef IPNOPRIVPORTS
   1844 	sysctl_createv(clog, 0, NULL, NULL,
   1845 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1846 		       CTLTYPE_INT, "lowportmin",
   1847 		       SYSCTL_DESCR("Lowest privileged ephemeral port number "
   1848 				    "to assign"),
   1849 		       sysctl_net_inet_ip_ports, 0, &ip6_lowportmin, 0,
   1850 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1851 		       IPV6CTL_LOWPORTMIN, CTL_EOL);
   1852 	sysctl_createv(clog, 0, NULL, NULL,
   1853 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1854 		       CTLTYPE_INT, "lowportmax",
   1855 		       SYSCTL_DESCR("Highest privileged ephemeral port number "
   1856 				    "to assign"),
   1857 		       sysctl_net_inet_ip_ports, 0, &ip6_lowportmax, 0,
   1858 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1859 		       IPV6CTL_LOWPORTMAX, CTL_EOL);
   1860 #endif /* IPNOPRIVPORTS */
   1861 	sysctl_createv(clog, 0, NULL, NULL,
   1862 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1863 		       CTLTYPE_INT, "use_tempaddr",
   1864 		       SYSCTL_DESCR("Use temporary address"),
   1865 		       NULL, 0, &ip6_use_tempaddr, 0,
   1866 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1867 		       CTL_CREATE, CTL_EOL);
   1868 	sysctl_createv(clog, 0, NULL, NULL,
   1869 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1870 		       CTLTYPE_INT, "temppltime",
   1871 		       SYSCTL_DESCR("preferred lifetime of a temporary address"),
   1872 		       NULL, 0, &ip6_temp_preferred_lifetime, 0,
   1873 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1874 		       CTL_CREATE, CTL_EOL);
   1875 	sysctl_createv(clog, 0, NULL, NULL,
   1876 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1877 		       CTLTYPE_INT, "tempvltime",
   1878 		       SYSCTL_DESCR("valid lifetime of a temporary address"),
   1879 		       NULL, 0, &ip6_temp_valid_lifetime, 0,
   1880 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1881 		       CTL_CREATE, CTL_EOL);
   1882 	sysctl_createv(clog, 0, NULL, NULL,
   1883 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1884 		       CTLTYPE_INT, "maxfrags",
   1885 		       SYSCTL_DESCR("Maximum fragments in reassembly queue"),
   1886 		       NULL, 0, &ip6_maxfrags, 0,
   1887 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1888 		       IPV6CTL_MAXFRAGS, CTL_EOL);
   1889 	sysctl_createv(clog, 0, NULL, NULL,
   1890 		       CTLFLAG_PERMANENT,
   1891 		       CTLTYPE_STRUCT, "stats",
   1892 		       SYSCTL_DESCR("IPv6 statistics"),
   1893 		       NULL, 0, &ip6stat, sizeof(ip6stat),
   1894 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1895 		       IPV6CTL_STATS, CTL_EOL);
   1896 	sysctl_createv(clog, 0, NULL, NULL,
   1897 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1898 		       CTLTYPE_INT, "use_defaultzone",
   1899 		       SYSCTL_DESCR("Whether to use the default scope zones"),
   1900 		       NULL, 0, &ip6_use_defzone, 0,
   1901 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1902 		       IPV6CTL_USE_DEFAULTZONE, CTL_EOL);
   1903 	sysctl_createv(clog, 0, NULL, NULL,
   1904 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1905 		       CTLTYPE_INT, "mcast_pmtu",
   1906 		       SYSCTL_DESCR("Enable pMTU discovery for multicast packet"),
   1907 		       NULL, 0, &ip6_mcast_pmtu, 0,
   1908 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1909 		       CTL_CREATE, CTL_EOL);
   1910 #ifdef GATEWAY
   1911 	sysctl_createv(clog, 0, NULL, NULL,
   1912 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1913 			CTLTYPE_INT, "maxflows",
   1914 			SYSCTL_DESCR("Number of flows for fast forwarding (IPv6)"),
   1915 			sysctl_net_inet6_ip6_maxflows, 0, &ip6_maxflows, 0,
   1916 			CTL_NET, PF_INET6, IPPROTO_IPV6,
   1917 			CTL_CREATE, CTL_EOL);
   1918 	sysctl_createv(clog, 0, NULL, NULL,
   1919 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1920 			CTLTYPE_INT, "hashsize",
   1921 			SYSCTL_DESCR("Size of hash table for fast forwarding (IPv6)"),
   1922 			sysctl_net_inet6_ip6_hashsize, 0, &ip6_hashsize, 0,
   1923 			CTL_NET, PF_INET6, IPPROTO_IPV6,
   1924 			CTL_CREATE, CTL_EOL);
   1925 #endif
   1926 }
   1927