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