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