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