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