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