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