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