Home | History | Annotate | Line # | Download | only in netinet6
ip6_input.c revision 1.75
      1  1.75   itojun /*	$NetBSD: ip6_input.c,v 1.75 2004/06/01 03:13:22 itojun 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.75   itojun __KERNEL_RCSID(0, "$NetBSD: ip6_input.c,v 1.75 2004/06/01 03:13:22 itojun Exp $");
     66   1.2   itojun 
     67   1.2   itojun #include "opt_inet.h"
     68   1.4  thorpej #include "opt_ipsec.h"
     69  1.15  darrenr #include "opt_pfil_hooks.h"
     70   1.2   itojun 
     71   1.2   itojun #include <sys/param.h>
     72   1.2   itojun #include <sys/systm.h>
     73   1.2   itojun #include <sys/malloc.h>
     74   1.2   itojun #include <sys/mbuf.h>
     75   1.2   itojun #include <sys/domain.h>
     76   1.2   itojun #include <sys/protosw.h>
     77   1.2   itojun #include <sys/socket.h>
     78   1.2   itojun #include <sys/socketvar.h>
     79   1.2   itojun #include <sys/errno.h>
     80   1.2   itojun #include <sys/time.h>
     81   1.2   itojun #include <sys/kernel.h>
     82   1.2   itojun #include <sys/syslog.h>
     83   1.2   itojun #include <sys/proc.h>
     84  1.46   simonb #include <sys/sysctl.h>
     85   1.2   itojun 
     86   1.2   itojun #include <net/if.h>
     87   1.2   itojun #include <net/if_types.h>
     88   1.2   itojun #include <net/if_dl.h>
     89   1.2   itojun #include <net/route.h>
     90   1.2   itojun #include <net/netisr.h>
     91  1.15  darrenr #ifdef PFIL_HOOKS
     92  1.15  darrenr #include <net/pfil.h>
     93  1.15  darrenr #endif
     94   1.2   itojun 
     95   1.2   itojun #include <netinet/in.h>
     96   1.9   itojun #include <netinet/in_systm.h>
     97   1.9   itojun #ifdef INET
     98   1.9   itojun #include <netinet/ip.h>
     99   1.9   itojun #include <netinet/ip_icmp.h>
    100  1.45   itojun #endif /* INET */
    101  1.14   itojun #include <netinet/ip6.h>
    102   1.2   itojun #include <netinet6/in6_var.h>
    103  1.11   itojun #include <netinet6/ip6_var.h>
    104   1.2   itojun #include <netinet6/in6_pcb.h>
    105  1.14   itojun #include <netinet/icmp6.h>
    106   1.2   itojun #include <netinet6/in6_ifattach.h>
    107   1.2   itojun #include <netinet6/nd6.h>
    108   1.2   itojun 
    109  1.37   itojun #ifdef IPSEC
    110  1.37   itojun #include <netinet6/ipsec.h>
    111  1.37   itojun #endif
    112  1.37   itojun 
    113   1.2   itojun #include <netinet6/ip6protosw.h>
    114   1.2   itojun 
    115   1.2   itojun /* we need it for NLOOP. */
    116   1.2   itojun #include "loop.h"
    117   1.2   itojun #include "faith.h"
    118   1.2   itojun #include "gif.h"
    119   1.2   itojun #include "bpfilter.h"
    120  1.50   itojun 
    121  1.50   itojun #if NGIF > 0
    122  1.50   itojun #include <netinet6/in6_gif.h>
    123  1.50   itojun #endif
    124   1.2   itojun 
    125   1.9   itojun #include <net/net_osdep.h>
    126   1.9   itojun 
    127   1.2   itojun extern struct domain inet6domain;
    128   1.2   itojun 
    129   1.2   itojun u_char ip6_protox[IPPROTO_MAX];
    130   1.2   itojun static int ip6qmaxlen = IFQ_MAXLEN;
    131   1.2   itojun struct in6_ifaddr *in6_ifaddr;
    132   1.2   itojun struct ifqueue ip6intrq;
    133   1.2   itojun 
    134   1.2   itojun extern struct ifnet loif[NLOOP];
    135   1.2   itojun int ip6_forward_srcrt;			/* XXX */
    136   1.2   itojun int ip6_sourcecheck;			/* XXX */
    137   1.2   itojun int ip6_sourcecheck_interval;		/* XXX */
    138   1.9   itojun 
    139  1.29  thorpej #ifdef PFIL_HOOKS
    140  1.29  thorpej struct pfil_head inet6_pfil_hook;
    141  1.29  thorpej #endif
    142  1.29  thorpej 
    143   1.2   itojun struct ip6stat ip6stat;
    144   1.2   itojun 
    145   1.2   itojun static void ip6_init2 __P((void *));
    146   1.2   itojun 
    147   1.5   itojun static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
    148  1.62   itojun static struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int));
    149   1.2   itojun 
    150   1.2   itojun /*
    151   1.2   itojun  * IP6 initialization: fill in IP6 protocol switch table.
    152   1.2   itojun  * All protocols not implemented in kernel go to raw IP6 protocol handler.
    153   1.2   itojun  */
    154   1.2   itojun void
    155   1.2   itojun ip6_init()
    156   1.2   itojun {
    157  1.35   itojun 	struct ip6protosw *pr;
    158  1.35   itojun 	int i;
    159   1.2   itojun 
    160   1.2   itojun 	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
    161   1.2   itojun 	if (pr == 0)
    162   1.2   itojun 		panic("ip6_init");
    163   1.2   itojun 	for (i = 0; i < IPPROTO_MAX; i++)
    164   1.2   itojun 		ip6_protox[i] = pr - inet6sw;
    165   1.2   itojun 	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
    166   1.2   itojun 	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
    167   1.2   itojun 		if (pr->pr_domain->dom_family == PF_INET6 &&
    168   1.2   itojun 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
    169   1.2   itojun 			ip6_protox[pr->pr_protocol] = pr - inet6sw;
    170   1.2   itojun 	ip6intrq.ifq_maxlen = ip6qmaxlen;
    171   1.2   itojun 	nd6_init();
    172   1.2   itojun 	frag6_init();
    173   1.2   itojun 
    174   1.2   itojun 	ip6_init2((void *)0);
    175  1.29  thorpej 
    176  1.29  thorpej #ifdef PFIL_HOOKS
    177  1.29  thorpej 	/* Register our Packet Filter hook. */
    178  1.33  thorpej 	inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
    179  1.33  thorpej 	inet6_pfil_hook.ph_af   = AF_INET6;
    180  1.29  thorpej 	i = pfil_head_register(&inet6_pfil_hook);
    181  1.29  thorpej 	if (i != 0)
    182  1.29  thorpej 		printf("ip6_init: WARNING: unable to register pfil hook, "
    183  1.29  thorpej 		    "error %d\n", i);
    184  1.29  thorpej #endif /* PFIL_HOOKS */
    185   1.2   itojun }
    186   1.2   itojun 
    187   1.2   itojun static void
    188   1.2   itojun ip6_init2(dummy)
    189   1.2   itojun 	void *dummy;
    190   1.2   itojun {
    191   1.2   itojun 
    192   1.2   itojun 	/* nd6_timer_init */
    193  1.19  thorpej 	callout_init(&nd6_timer_ch);
    194  1.19  thorpej 	callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
    195   1.2   itojun }
    196   1.2   itojun 
    197   1.2   itojun /*
    198   1.2   itojun  * IP6 input interrupt handling. Just pass the packet to ip6_input.
    199   1.2   itojun  */
    200   1.2   itojun void
    201   1.2   itojun ip6intr()
    202   1.2   itojun {
    203   1.2   itojun 	int s;
    204   1.2   itojun 	struct mbuf *m;
    205   1.2   itojun 
    206   1.2   itojun 	for (;;) {
    207  1.41  thorpej 		s = splnet();
    208   1.2   itojun 		IF_DEQUEUE(&ip6intrq, m);
    209   1.2   itojun 		splx(s);
    210   1.2   itojun 		if (m == 0)
    211   1.2   itojun 			return;
    212   1.2   itojun 		ip6_input(m);
    213   1.2   itojun 	}
    214   1.2   itojun }
    215   1.2   itojun 
    216   1.2   itojun extern struct	route_in6 ip6_forward_rt;
    217   1.2   itojun 
    218   1.2   itojun void
    219   1.2   itojun ip6_input(m)
    220   1.2   itojun 	struct mbuf *m;
    221   1.2   itojun {
    222   1.9   itojun 	struct ip6_hdr *ip6;
    223   1.2   itojun 	int off = sizeof(struct ip6_hdr), nest;
    224   1.2   itojun 	u_int32_t plen;
    225   1.5   itojun 	u_int32_t rtalert = ~0;
    226   1.2   itojun 	int nxt, ours = 0;
    227   1.9   itojun 	struct ifnet *deliverifp = NULL;
    228  1.64   itojun 	int srcrt = 0;
    229   1.2   itojun 
    230   1.2   itojun #ifdef IPSEC
    231   1.2   itojun 	/*
    232   1.2   itojun 	 * should the inner packet be considered authentic?
    233   1.2   itojun 	 * see comment in ah4_input().
    234   1.2   itojun 	 */
    235  1.49   itojun 	m->m_flags &= ~M_AUTHIPHDR;
    236  1.49   itojun 	m->m_flags &= ~M_AUTHIPDGM;
    237   1.2   itojun #endif
    238   1.9   itojun 
    239   1.2   itojun 	/*
    240  1.44   itojun 	 * mbuf statistics
    241   1.2   itojun 	 */
    242   1.2   itojun 	if (m->m_flags & M_EXT) {
    243   1.2   itojun 		if (m->m_next)
    244   1.2   itojun 			ip6stat.ip6s_mext2m++;
    245   1.2   itojun 		else
    246   1.2   itojun 			ip6stat.ip6s_mext1++;
    247   1.2   itojun 	} else {
    248  1.40   itojun #define M2MMAX	(sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
    249   1.2   itojun 		if (m->m_next) {
    250   1.9   itojun 			if (m->m_flags & M_LOOP) {
    251  1.44   itojun 				ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
    252  1.40   itojun 			} else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
    253   1.2   itojun 				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
    254   1.2   itojun 			else
    255   1.2   itojun 				ip6stat.ip6s_m2m[0]++;
    256   1.2   itojun 		} else
    257   1.2   itojun 			ip6stat.ip6s_m1++;
    258  1.40   itojun #undef M2MMAX
    259   1.2   itojun 	}
    260   1.2   itojun 
    261   1.9   itojun 	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
    262   1.9   itojun 	ip6stat.ip6s_total++;
    263   1.9   itojun 
    264  1.57  thorpej 	/*
    265  1.57  thorpej 	 * If the IPv6 header is not aligned, slurp it up into a new
    266  1.57  thorpej 	 * mbuf with space for link headers, in the event we forward
    267  1.57  thorpej 	 * it.  OTherwise, if it is aligned, make sure the entire base
    268  1.57  thorpej 	 * IPv6 header is in the first mbuf of the chain.
    269  1.57  thorpej 	 */
    270  1.57  thorpej 	if (IP6_HDR_ALIGNED_P(mtod(m, caddr_t)) == 0) {
    271  1.57  thorpej 		struct ifnet *inifp = m->m_pkthdr.rcvif;
    272  1.57  thorpej 		if ((m = m_copyup(m, sizeof(struct ip6_hdr),
    273  1.57  thorpej 				  (max_linkhdr + 3) & ~3)) == NULL) {
    274  1.57  thorpej 			/* XXXJRT new stat, please */
    275  1.57  thorpej 			ip6stat.ip6s_toosmall++;
    276  1.57  thorpej 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
    277  1.57  thorpej 			return;
    278  1.57  thorpej 		}
    279  1.57  thorpej 	} else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
    280  1.57  thorpej 		struct ifnet *inifp = m->m_pkthdr.rcvif;
    281  1.55   itojun 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
    282   1.9   itojun 			ip6stat.ip6s_toosmall++;
    283   1.9   itojun 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
    284   1.9   itojun 			return;
    285   1.9   itojun 		}
    286   1.2   itojun 	}
    287   1.2   itojun 
    288   1.2   itojun 	ip6 = mtod(m, struct ip6_hdr *);
    289   1.2   itojun 
    290   1.2   itojun 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
    291   1.2   itojun 		ip6stat.ip6s_badvers++;
    292   1.9   itojun 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
    293   1.2   itojun 		goto bad;
    294   1.2   itojun 	}
    295  1.15  darrenr 
    296  1.15  darrenr #ifdef PFIL_HOOKS
    297  1.15  darrenr 	/*
    298  1.15  darrenr 	 * Run through list of hooks for input packets.  If there are any
    299  1.15  darrenr 	 * filters which require that additional packets in the flow are
    300  1.15  darrenr 	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
    301  1.15  darrenr 	 * Note that filters must _never_ set this flag, as another filter
    302  1.15  darrenr 	 * in the list may have previously cleared it.
    303  1.15  darrenr 	 */
    304  1.39   itojun 	/*
    305  1.39   itojun 	 * let ipfilter look at packet on the wire,
    306  1.39   itojun 	 * not the decapsulated packet.
    307  1.39   itojun 	 */
    308  1.39   itojun #ifdef IPSEC
    309  1.42   itojun 	if (!ipsec_getnhist(m))
    310  1.39   itojun #else
    311  1.39   itojun 	if (1)
    312  1.39   itojun #endif
    313  1.39   itojun 	{
    314  1.64   itojun 		struct in6_addr odst;
    315  1.64   itojun 
    316  1.64   itojun 		odst = ip6->ip6_dst;
    317  1.39   itojun 		if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif,
    318  1.39   itojun 				   PFIL_IN) != 0)
    319  1.39   itojun 			return;
    320  1.39   itojun 		if (m == NULL)
    321  1.39   itojun 			return;
    322  1.39   itojun 		ip6 = mtod(m, struct ip6_hdr *);
    323  1.64   itojun 		srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
    324  1.39   itojun 	}
    325  1.15  darrenr #endif /* PFIL_HOOKS */
    326  1.15  darrenr 
    327   1.2   itojun 	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
    328   1.2   itojun 
    329  1.30  thorpej #ifdef ALTQ
    330  1.30  thorpej 	if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
    331  1.30  thorpej 		/* packet is dropped by traffic conditioner */
    332  1.30  thorpej 		return;
    333   1.9   itojun 	}
    334   1.9   itojun #endif
    335   1.9   itojun 
    336   1.2   itojun 	/*
    337  1.44   itojun 	 * Check against address spoofing/corruption.
    338   1.2   itojun 	 */
    339   1.2   itojun 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
    340   1.2   itojun 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
    341  1.55   itojun 		/*
    342  1.55   itojun 		 * XXX: "badscope" is not very suitable for a multicast source.
    343  1.55   itojun 		 */
    344   1.2   itojun 		ip6stat.ip6s_badscope++;
    345   1.9   itojun 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    346   1.2   itojun 		goto bad;
    347   1.2   itojun 	}
    348  1.13   itojun 	/*
    349  1.44   itojun 	 * The following check is not documented in specs.  A malicious
    350  1.44   itojun 	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
    351  1.44   itojun 	 * and bypass security checks (act as if it was from 127.0.0.1 by using
    352  1.44   itojun 	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
    353  1.35   itojun 	 *
    354  1.44   itojun 	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
    355  1.44   itojun 	 * support IPv4-less kernel compilation, we cannot support SIIT
    356  1.44   itojun 	 * environment at all.  So, it makes more sense for us to reject any
    357  1.44   itojun 	 * malicious packets for non-SIIT environment, than try to do a
    358  1.52      wiz 	 * partial support for SIIT environment.
    359  1.13   itojun 	 */
    360  1.13   itojun 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    361  1.13   itojun 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    362  1.13   itojun 		ip6stat.ip6s_badscope++;
    363  1.13   itojun 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    364  1.13   itojun 		goto bad;
    365  1.13   itojun 	}
    366  1.17   itojun #if 0
    367  1.13   itojun 	/*
    368  1.17   itojun 	 * Reject packets with IPv4 compatible addresses (auto tunnel).
    369  1.17   itojun 	 *
    370  1.17   itojun 	 * The code forbids auto tunnel relay case in RFC1933 (the check is
    371  1.17   itojun 	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
    372  1.17   itojun 	 * is revised to forbid relaying case.
    373  1.13   itojun 	 */
    374  1.13   itojun 	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
    375  1.13   itojun 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
    376  1.13   itojun 		ip6stat.ip6s_badscope++;
    377  1.13   itojun 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    378  1.13   itojun 		goto bad;
    379  1.13   itojun 	}
    380  1.13   itojun #endif
    381  1.35   itojun 
    382   1.2   itojun 	if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
    383   1.2   itojun 	    IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
    384   1.2   itojun 		if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
    385   1.2   itojun 			ours = 1;
    386   1.9   itojun 			deliverifp = m->m_pkthdr.rcvif;
    387   1.2   itojun 			goto hbhcheck;
    388   1.2   itojun 		} else {
    389   1.2   itojun 			ip6stat.ip6s_badscope++;
    390   1.9   itojun 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    391   1.2   itojun 			goto bad;
    392   1.2   itojun 		}
    393   1.2   itojun 	}
    394   1.2   itojun 
    395  1.38   itojun 	/* drop packets if interface ID portion is already filled */
    396  1.38   itojun 	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
    397  1.38   itojun 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
    398  1.38   itojun 		    ip6->ip6_src.s6_addr16[1]) {
    399  1.38   itojun 			ip6stat.ip6s_badscope++;
    400  1.38   itojun 			goto bad;
    401  1.38   itojun 		}
    402  1.38   itojun 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
    403  1.38   itojun 		    ip6->ip6_dst.s6_addr16[1]) {
    404  1.38   itojun 			ip6stat.ip6s_badscope++;
    405  1.38   itojun 			goto bad;
    406  1.38   itojun 		}
    407  1.38   itojun 	}
    408   1.2   itojun 
    409  1.40   itojun 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
    410  1.40   itojun 		ip6->ip6_src.s6_addr16[1]
    411  1.40   itojun 			= htons(m->m_pkthdr.rcvif->if_index);
    412  1.40   itojun 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
    413  1.40   itojun 		ip6->ip6_dst.s6_addr16[1]
    414  1.40   itojun 			= htons(m->m_pkthdr.rcvif->if_index);
    415  1.40   itojun 
    416  1.40   itojun 	/*
    417  1.40   itojun 	 * We use rt->rt_ifp to determine if the address is ours or not.
    418  1.40   itojun 	 * If rt_ifp is lo0, the address is ours.
    419  1.40   itojun 	 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0,
    420  1.40   itojun 	 * so any address under fe80::%lo0/64 will be mistakenly considered
    421  1.40   itojun 	 * local.  The special case is supplied to handle the case properly
    422  1.40   itojun 	 * by actually looking at interface addresses
    423  1.40   itojun 	 * (using in6ifa_ifpwithaddr).
    424  1.40   itojun 	 */
    425  1.40   itojun 	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 &&
    426  1.40   itojun 	    IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
    427  1.40   itojun 		if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) {
    428  1.40   itojun 			icmp6_error(m, ICMP6_DST_UNREACH,
    429  1.40   itojun 			    ICMP6_DST_UNREACH_ADDR, 0);
    430  1.40   itojun 			/* m is already freed */
    431  1.40   itojun 			return;
    432  1.21   itojun 		}
    433  1.40   itojun 
    434  1.40   itojun 		ours = 1;
    435  1.40   itojun 		deliverifp = m->m_pkthdr.rcvif;
    436  1.40   itojun 		goto hbhcheck;
    437   1.9   itojun 	}
    438   1.9   itojun 
    439   1.2   itojun 	/*
    440   1.2   itojun 	 * Multicast check
    441   1.2   itojun 	 */
    442   1.2   itojun 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    443   1.2   itojun 	  	struct	in6_multi *in6m = 0;
    444   1.9   itojun 
    445   1.9   itojun 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
    446   1.2   itojun 		/*
    447   1.2   itojun 		 * See if we belong to the destination multicast group on the
    448   1.2   itojun 		 * arrival interface.
    449   1.2   itojun 		 */
    450   1.2   itojun 		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
    451   1.2   itojun 		if (in6m)
    452   1.2   itojun 			ours = 1;
    453   1.2   itojun 		else if (!ip6_mrouter) {
    454   1.2   itojun 			ip6stat.ip6s_notmember++;
    455   1.2   itojun 			ip6stat.ip6s_cantforward++;
    456   1.9   itojun 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
    457   1.2   itojun 			goto bad;
    458   1.2   itojun 		}
    459   1.9   itojun 		deliverifp = m->m_pkthdr.rcvif;
    460   1.2   itojun 		goto hbhcheck;
    461   1.2   itojun 	}
    462   1.2   itojun 
    463   1.2   itojun 	/*
    464   1.2   itojun 	 *  Unicast check
    465   1.2   itojun 	 */
    466  1.22   itojun 	if (ip6_forward_rt.ro_rt != NULL &&
    467  1.55   itojun 	    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
    468  1.22   itojun 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
    469  1.72   itojun 	    &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
    470  1.25   itojun 		ip6stat.ip6s_forward_cachehit++;
    471  1.22   itojun 	else {
    472  1.35   itojun 		struct sockaddr_in6 *dst6;
    473  1.35   itojun 
    474   1.2   itojun 		if (ip6_forward_rt.ro_rt) {
    475  1.22   itojun 			/* route is down or destination is different */
    476  1.25   itojun 			ip6stat.ip6s_forward_cachemiss++;
    477   1.2   itojun 			RTFREE(ip6_forward_rt.ro_rt);
    478   1.2   itojun 			ip6_forward_rt.ro_rt = 0;
    479   1.2   itojun 		}
    480  1.22   itojun 
    481   1.2   itojun 		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
    482  1.35   itojun 		dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
    483  1.35   itojun 		dst6->sin6_len = sizeof(struct sockaddr_in6);
    484  1.35   itojun 		dst6->sin6_family = AF_INET6;
    485  1.35   itojun 		dst6->sin6_addr = ip6->ip6_dst;
    486   1.2   itojun 
    487   1.9   itojun 		rtalloc((struct route *)&ip6_forward_rt);
    488   1.2   itojun 	}
    489   1.2   itojun 
    490   1.2   itojun #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
    491   1.2   itojun 
    492   1.2   itojun 	/*
    493   1.2   itojun 	 * Accept the packet if the forwarding interface to the destination
    494   1.2   itojun 	 * according to the routing table is the loopback interface,
    495   1.2   itojun 	 * unless the associated route has a gateway.
    496   1.2   itojun 	 * Note that this approach causes to accept a packet if there is a
    497   1.2   itojun 	 * route to the loopback interface for the destination of the packet.
    498   1.2   itojun 	 * But we think it's even useful in some situations, e.g. when using
    499   1.2   itojun 	 * a special daemon which wants to intercept the packet.
    500   1.2   itojun 	 */
    501   1.2   itojun 	if (ip6_forward_rt.ro_rt &&
    502   1.2   itojun 	    (ip6_forward_rt.ro_rt->rt_flags &
    503   1.2   itojun 	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
    504  1.55   itojun 	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
    505   1.2   itojun #if 0
    506   1.2   itojun 	    /*
    507   1.2   itojun 	     * The check below is redundant since the comparison of
    508   1.2   itojun 	     * the destination and the key of the rtentry has
    509   1.2   itojun 	     * already done through looking up the routing table.
    510   1.2   itojun 	     */
    511   1.2   itojun 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
    512  1.55   itojun 	    &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) &&
    513   1.2   itojun #endif
    514   1.9   itojun 	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
    515   1.2   itojun 		struct in6_ifaddr *ia6 =
    516   1.2   itojun 			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
    517   1.2   itojun 		if (ia6->ia6_flags & IN6_IFF_ANYCAST)
    518   1.2   itojun 			m->m_flags |= M_ANYCAST6;
    519  1.24   itojun 		/*
    520  1.24   itojun 		 * packets to a tentative, duplicated, or somehow invalid
    521  1.24   itojun 		 * address must not be accepted.
    522  1.24   itojun 		 */
    523   1.2   itojun 		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
    524  1.24   itojun 			/* this address is ready */
    525   1.2   itojun 			ours = 1;
    526   1.9   itojun 			deliverifp = ia6->ia_ifp;	/* correct? */
    527   1.2   itojun 			goto hbhcheck;
    528   1.2   itojun 		} else {
    529  1.24   itojun 			/* address is not ready, so discard the packet. */
    530  1.34   itojun 			nd6log((LOG_INFO,
    531  1.27   itojun 			    "ip6_input: packet to an unready address %s->%s\n",
    532  1.24   itojun 			    ip6_sprintf(&ip6->ip6_src),
    533  1.34   itojun 			    ip6_sprintf(&ip6->ip6_dst)));
    534  1.24   itojun 
    535  1.24   itojun 			goto bad;
    536   1.2   itojun 		}
    537   1.2   itojun 	}
    538   1.2   itojun 
    539   1.2   itojun 	/*
    540  1.55   itojun 	 * FAITH (Firewall Aided Internet Translator)
    541   1.2   itojun 	 */
    542   1.2   itojun #if defined(NFAITH) && 0 < NFAITH
    543   1.2   itojun 	if (ip6_keepfaith) {
    544  1.71   itojun 		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp &&
    545  1.71   itojun 		    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
    546   1.2   itojun 			/* XXX do we need more sanity checks? */
    547   1.2   itojun 			ours = 1;
    548  1.45   itojun 			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
    549   1.9   itojun 			goto hbhcheck;
    550   1.9   itojun 		}
    551   1.9   itojun 	}
    552   1.9   itojun #endif
    553   1.9   itojun 
    554   1.9   itojun #if 0
    555   1.9   itojun     {
    556   1.9   itojun 	/*
    557   1.9   itojun 	 * Last resort: check in6_ifaddr for incoming interface.
    558   1.9   itojun 	 * The code is here until I update the "goto ours hack" code above
    559   1.9   itojun 	 * working right.
    560   1.9   itojun 	 */
    561   1.9   itojun 	struct ifaddr *ifa;
    562   1.9   itojun 	for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
    563   1.9   itojun 	     ifa;
    564   1.9   itojun 	     ifa = ifa->ifa_list.tqe_next) {
    565   1.9   itojun 		if (ifa->ifa_addr == NULL)
    566   1.9   itojun 			continue;	/* just for safety */
    567   1.9   itojun 		if (ifa->ifa_addr->sa_family != AF_INET6)
    568   1.9   itojun 			continue;
    569   1.9   itojun 		if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
    570   1.9   itojun 			ours = 1;
    571   1.9   itojun 			deliverifp = ifa->ifa_ifp;
    572   1.2   itojun 			goto hbhcheck;
    573   1.2   itojun 		}
    574   1.2   itojun 	}
    575   1.9   itojun     }
    576   1.2   itojun #endif
    577   1.2   itojun 
    578   1.2   itojun 	/*
    579   1.2   itojun 	 * Now there is no reason to process the packet if it's not our own
    580   1.2   itojun 	 * and we're not a router.
    581   1.2   itojun 	 */
    582   1.2   itojun 	if (!ip6_forwarding) {
    583   1.2   itojun 		ip6stat.ip6s_cantforward++;
    584   1.9   itojun 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
    585   1.2   itojun 		goto bad;
    586   1.2   itojun 	}
    587   1.2   itojun 
    588   1.2   itojun   hbhcheck:
    589   1.2   itojun 	/*
    590   1.2   itojun 	 * Process Hop-by-Hop options header if it's contained.
    591   1.2   itojun 	 * m may be modified in ip6_hopopts_input().
    592   1.2   itojun 	 * If a JumboPayload option is included, plen will also be modified.
    593   1.2   itojun 	 */
    594   1.2   itojun 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
    595   1.2   itojun 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
    596   1.9   itojun 		struct ip6_hbh *hbh;
    597   1.9   itojun 
    598   1.9   itojun 		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
    599   1.9   itojun #if 0	/*touches NULL pointer*/
    600   1.9   itojun 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
    601   1.9   itojun #endif
    602   1.2   itojun 			return;	/* m have already been freed */
    603   1.9   itojun 		}
    604  1.22   itojun 
    605   1.2   itojun 		/* adjust pointer */
    606   1.2   itojun 		ip6 = mtod(m, struct ip6_hdr *);
    607  1.22   itojun 
    608  1.22   itojun 		/*
    609  1.49   itojun 		 * if the payload length field is 0 and the next header field
    610  1.22   itojun 		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
    611  1.22   itojun 		 * option MUST be included.
    612  1.22   itojun 		 */
    613  1.22   itojun 		if (ip6->ip6_plen == 0 && plen == 0) {
    614  1.22   itojun 			/*
    615  1.22   itojun 			 * Note that if a valid jumbo payload option is
    616  1.22   itojun 			 * contained, ip6_hoptops_input() must set a valid
    617  1.55   itojun 			 * (non-zero) payload length to the variable plen.
    618  1.22   itojun 			 */
    619  1.22   itojun 			ip6stat.ip6s_badoptions++;
    620  1.22   itojun 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
    621  1.22   itojun 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
    622  1.22   itojun 			icmp6_error(m, ICMP6_PARAM_PROB,
    623  1.22   itojun 				    ICMP6_PARAMPROB_HEADER,
    624  1.22   itojun 				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
    625  1.22   itojun 			return;
    626  1.22   itojun 		}
    627   1.9   itojun 		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
    628   1.9   itojun 			sizeof(struct ip6_hbh));
    629   1.9   itojun 		if (hbh == NULL) {
    630   1.9   itojun 			ip6stat.ip6s_tooshort++;
    631   1.9   itojun 			return;
    632   1.9   itojun 		}
    633  1.57  thorpej 		KASSERT(IP6_HDR_ALIGNED_P(hbh));
    634   1.9   itojun 		nxt = hbh->ip6h_nxt;
    635   1.2   itojun 
    636   1.2   itojun 		/*
    637   1.2   itojun 		 * accept the packet if a router alert option is included
    638   1.2   itojun 		 * and we act as an IPv6 router.
    639   1.2   itojun 		 */
    640   1.5   itojun 		if (rtalert != ~0 && ip6_forwarding)
    641   1.2   itojun 			ours = 1;
    642   1.2   itojun 	} else
    643   1.2   itojun 		nxt = ip6->ip6_nxt;
    644   1.2   itojun 
    645   1.2   itojun 	/*
    646   1.2   itojun 	 * Check that the amount of data in the buffers
    647   1.2   itojun 	 * is as at least much as the IPv6 header would have us expect.
    648   1.2   itojun 	 * Trim mbufs if longer than we expect.
    649   1.2   itojun 	 * Drop packet if shorter than we expect.
    650   1.2   itojun 	 */
    651   1.2   itojun 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
    652   1.2   itojun 		ip6stat.ip6s_tooshort++;
    653   1.9   itojun 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
    654   1.2   itojun 		goto bad;
    655   1.2   itojun 	}
    656   1.2   itojun 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
    657   1.2   itojun 		if (m->m_len == m->m_pkthdr.len) {
    658   1.2   itojun 			m->m_len = sizeof(struct ip6_hdr) + plen;
    659   1.2   itojun 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
    660   1.2   itojun 		} else
    661   1.2   itojun 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
    662   1.2   itojun 	}
    663   1.2   itojun 
    664   1.2   itojun 	/*
    665   1.2   itojun 	 * Forward if desirable.
    666   1.2   itojun 	 */
    667   1.2   itojun 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    668   1.2   itojun 		/*
    669   1.2   itojun 		 * If we are acting as a multicast router, all
    670   1.2   itojun 		 * incoming multicast packets are passed to the
    671   1.2   itojun 		 * kernel-level multicast forwarding function.
    672   1.2   itojun 		 * The packet is returned (relatively) intact; if
    673   1.2   itojun 		 * ip6_mforward() returns a non-zero value, the packet
    674   1.2   itojun 		 * must be discarded, else it may be accepted below.
    675   1.2   itojun 		 */
    676   1.2   itojun 		if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
    677   1.2   itojun 			ip6stat.ip6s_cantforward++;
    678   1.2   itojun 			m_freem(m);
    679   1.2   itojun 			return;
    680   1.2   itojun 		}
    681   1.2   itojun 		if (!ours) {
    682   1.2   itojun 			m_freem(m);
    683   1.2   itojun 			return;
    684   1.2   itojun 		}
    685  1.22   itojun 	} else if (!ours) {
    686  1.64   itojun 		ip6_forward(m, srcrt);
    687   1.2   itojun 		return;
    688  1.56   itojun 	}
    689  1.25   itojun 
    690  1.25   itojun 	ip6 = mtod(m, struct ip6_hdr *);
    691  1.25   itojun 
    692  1.25   itojun 	/*
    693  1.25   itojun 	 * Malicious party may be able to use IPv4 mapped addr to confuse
    694  1.25   itojun 	 * tcp/udp stack and bypass security checks (act as if it was from
    695  1.25   itojun 	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
    696  1.25   itojun 	 *
    697  1.25   itojun 	 * For SIIT end node behavior, you may want to disable the check.
    698  1.25   itojun 	 * However, you will  become vulnerable to attacks using IPv4 mapped
    699  1.25   itojun 	 * source.
    700  1.25   itojun 	 */
    701  1.25   itojun 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    702  1.25   itojun 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    703  1.25   itojun 		ip6stat.ip6s_badscope++;
    704  1.25   itojun 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    705  1.25   itojun 		goto bad;
    706  1.25   itojun 	}
    707   1.2   itojun 
    708   1.2   itojun 	/*
    709   1.2   itojun 	 * Tell launch routine the next header
    710   1.2   itojun 	 */
    711  1.12   itojun #ifdef IFA_STATS
    712  1.28   itojun 	if (deliverifp != NULL) {
    713   1.9   itojun 		struct in6_ifaddr *ia6;
    714   1.9   itojun 		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
    715   1.9   itojun 		if (ia6)
    716   1.9   itojun 			ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
    717   1.9   itojun 	}
    718   1.9   itojun #endif
    719   1.2   itojun 	ip6stat.ip6s_delivered++;
    720   1.9   itojun 	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
    721   1.2   itojun 	nest = 0;
    722  1.40   itojun 
    723   1.2   itojun 	while (nxt != IPPROTO_DONE) {
    724   1.2   itojun 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
    725   1.2   itojun 			ip6stat.ip6s_toomanyhdr++;
    726   1.2   itojun 			goto bad;
    727   1.2   itojun 		}
    728   1.8   itojun 
    729   1.8   itojun 		/*
    730   1.8   itojun 		 * protection against faulty packet - there should be
    731   1.8   itojun 		 * more sanity checks in header chain processing.
    732   1.8   itojun 		 */
    733   1.8   itojun 		if (m->m_pkthdr.len < off) {
    734   1.8   itojun 			ip6stat.ip6s_tooshort++;
    735   1.9   itojun 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
    736   1.8   itojun 			goto bad;
    737   1.8   itojun 		}
    738   1.8   itojun 
    739  1.37   itojun #ifdef IPSEC
    740  1.37   itojun 		/*
    741  1.37   itojun 		 * enforce IPsec policy checking if we are seeing last header.
    742  1.37   itojun 		 * note that we do not visit this with protocols with pcb layer
    743  1.37   itojun 		 * code - like udp/tcp/raw ip.
    744  1.37   itojun 		 */
    745  1.37   itojun 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
    746  1.37   itojun 		    ipsec6_in_reject(m, NULL)) {
    747  1.37   itojun 			ipsec6stat.in_polvio++;
    748  1.37   itojun 			goto bad;
    749  1.37   itojun 		}
    750  1.37   itojun #endif
    751  1.56   itojun 
    752   1.2   itojun 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
    753   1.2   itojun 	}
    754   1.2   itojun 	return;
    755   1.2   itojun  bad:
    756   1.2   itojun 	m_freem(m);
    757   1.2   itojun }
    758   1.2   itojun 
    759   1.2   itojun /*
    760   1.2   itojun  * Hop-by-Hop options header processing. If a valid jumbo payload option is
    761   1.2   itojun  * included, the real payload length will be stored in plenp.
    762   1.2   itojun  */
    763   1.2   itojun static int
    764   1.2   itojun ip6_hopopts_input(plenp, rtalertp, mp, offp)
    765   1.2   itojun 	u_int32_t *plenp;
    766   1.5   itojun 	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
    767   1.2   itojun 	struct mbuf **mp;
    768   1.2   itojun 	int *offp;
    769   1.2   itojun {
    770  1.35   itojun 	struct mbuf *m = *mp;
    771   1.2   itojun 	int off = *offp, hbhlen;
    772   1.2   itojun 	struct ip6_hbh *hbh;
    773   1.2   itojun 
    774   1.2   itojun 	/* validation of the length of the header */
    775   1.9   itojun 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
    776   1.9   itojun 		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
    777   1.9   itojun 	if (hbh == NULL) {
    778   1.9   itojun 		ip6stat.ip6s_tooshort++;
    779   1.9   itojun 		return -1;
    780   1.9   itojun 	}
    781   1.9   itojun 	hbhlen = (hbh->ip6h_len + 1) << 3;
    782   1.9   itojun 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
    783   1.9   itojun 		hbhlen);
    784   1.9   itojun 	if (hbh == NULL) {
    785   1.9   itojun 		ip6stat.ip6s_tooshort++;
    786   1.9   itojun 		return -1;
    787   1.9   itojun 	}
    788  1.57  thorpej 	KASSERT(IP6_HDR_ALIGNED_P(hbh));
    789   1.2   itojun 	off += hbhlen;
    790   1.2   itojun 	hbhlen -= sizeof(struct ip6_hbh);
    791   1.2   itojun 
    792   1.2   itojun 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
    793   1.2   itojun 				hbhlen, rtalertp, plenp) < 0)
    794  1.58   itojun 		return (-1);
    795   1.2   itojun 
    796   1.2   itojun 	*offp = off;
    797   1.2   itojun 	*mp = m;
    798  1.58   itojun 	return (0);
    799   1.2   itojun }
    800   1.2   itojun 
    801   1.2   itojun /*
    802   1.2   itojun  * Search header for all Hop-by-hop options and process each option.
    803   1.2   itojun  * This function is separate from ip6_hopopts_input() in order to
    804   1.2   itojun  * handle a case where the sending node itself process its hop-by-hop
    805   1.2   itojun  * options header. In such a case, the function is called from ip6_output().
    806  1.55   itojun  *
    807  1.55   itojun  * The function assumes that hbh header is located right after the IPv6 header
    808  1.55   itojun  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
    809  1.55   itojun  * opthead + hbhlen is located in continuous memory region.
    810   1.2   itojun  */
    811   1.2   itojun int
    812   1.2   itojun ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
    813   1.2   itojun 	struct mbuf *m;
    814   1.2   itojun 	u_int8_t *opthead;
    815   1.2   itojun 	int hbhlen;
    816   1.5   itojun 	u_int32_t *rtalertp;
    817   1.2   itojun 	u_int32_t *plenp;
    818   1.2   itojun {
    819   1.2   itojun 	struct ip6_hdr *ip6;
    820   1.2   itojun 	int optlen = 0;
    821   1.2   itojun 	u_int8_t *opt = opthead;
    822   1.2   itojun 	u_int16_t rtalert_val;
    823  1.22   itojun 	u_int32_t jumboplen;
    824  1.55   itojun 	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
    825   1.2   itojun 
    826   1.2   itojun 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
    827  1.35   itojun 		switch (*opt) {
    828  1.35   itojun 		case IP6OPT_PAD1:
    829  1.35   itojun 			optlen = 1;
    830  1.35   itojun 			break;
    831  1.35   itojun 		case IP6OPT_PADN:
    832  1.35   itojun 			if (hbhlen < IP6OPT_MINLEN) {
    833  1.35   itojun 				ip6stat.ip6s_toosmall++;
    834  1.35   itojun 				goto bad;
    835  1.35   itojun 			}
    836  1.35   itojun 			optlen = *(opt + 1) + 2;
    837  1.35   itojun 			break;
    838  1.35   itojun 		case IP6OPT_RTALERT:
    839  1.35   itojun 			/* XXX may need check for alignment */
    840  1.35   itojun 			if (hbhlen < IP6OPT_RTALERT_LEN) {
    841  1.35   itojun 				ip6stat.ip6s_toosmall++;
    842  1.35   itojun 				goto bad;
    843  1.35   itojun 			}
    844  1.35   itojun 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
    845  1.55   itojun 				/* XXX stat */
    846  1.55   itojun 				icmp6_error(m, ICMP6_PARAM_PROB,
    847  1.55   itojun 				    ICMP6_PARAMPROB_HEADER,
    848  1.55   itojun 				    erroff + opt + 1 - opthead);
    849  1.55   itojun 				return (-1);
    850  1.35   itojun 			}
    851  1.35   itojun 			optlen = IP6OPT_RTALERT_LEN;
    852  1.35   itojun 			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
    853  1.35   itojun 			*rtalertp = ntohs(rtalert_val);
    854  1.35   itojun 			break;
    855  1.35   itojun 		case IP6OPT_JUMBO:
    856  1.22   itojun 			/* XXX may need check for alignment */
    857  1.22   itojun 			if (hbhlen < IP6OPT_JUMBO_LEN) {
    858  1.22   itojun 				ip6stat.ip6s_toosmall++;
    859  1.22   itojun 				goto bad;
    860  1.22   itojun 			}
    861  1.35   itojun 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
    862  1.55   itojun 				/* XXX stat */
    863  1.55   itojun 				icmp6_error(m, ICMP6_PARAM_PROB,
    864  1.55   itojun 				    ICMP6_PARAMPROB_HEADER,
    865  1.55   itojun 				    erroff + opt + 1 - opthead);
    866  1.55   itojun 				return (-1);
    867  1.35   itojun 			}
    868  1.22   itojun 			optlen = IP6OPT_JUMBO_LEN;
    869  1.22   itojun 
    870  1.22   itojun 			/*
    871  1.22   itojun 			 * IPv6 packets that have non 0 payload length
    872  1.35   itojun 			 * must not contain a jumbo payload option.
    873  1.22   itojun 			 */
    874  1.22   itojun 			ip6 = mtod(m, struct ip6_hdr *);
    875  1.22   itojun 			if (ip6->ip6_plen) {
    876  1.22   itojun 				ip6stat.ip6s_badoptions++;
    877  1.22   itojun 				icmp6_error(m, ICMP6_PARAM_PROB,
    878  1.55   itojun 				    ICMP6_PARAMPROB_HEADER,
    879  1.55   itojun 				    erroff + opt - opthead);
    880  1.55   itojun 				return (-1);
    881  1.22   itojun 			}
    882   1.2   itojun 
    883  1.22   itojun 			/*
    884  1.22   itojun 			 * We may see jumbolen in unaligned location, so
    885  1.22   itojun 			 * we'd need to perform bcopy().
    886  1.22   itojun 			 */
    887  1.22   itojun 			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
    888  1.22   itojun 			jumboplen = (u_int32_t)htonl(jumboplen);
    889  1.22   itojun 
    890  1.22   itojun #if 1
    891  1.22   itojun 			/*
    892  1.22   itojun 			 * if there are multiple jumbo payload options,
    893  1.22   itojun 			 * *plenp will be non-zero and the packet will be
    894  1.22   itojun 			 * rejected.
    895  1.22   itojun 			 * the behavior may need some debate in ipngwg -
    896  1.22   itojun 			 * multiple options does not make sense, however,
    897  1.22   itojun 			 * there's no explicit mention in specification.
    898  1.22   itojun 			 */
    899  1.22   itojun 			if (*plenp != 0) {
    900  1.22   itojun 				ip6stat.ip6s_badoptions++;
    901  1.22   itojun 				icmp6_error(m, ICMP6_PARAM_PROB,
    902  1.55   itojun 				    ICMP6_PARAMPROB_HEADER,
    903  1.55   itojun 				    erroff + opt + 2 - opthead);
    904  1.55   itojun 				return (-1);
    905  1.22   itojun 			}
    906   1.8   itojun #endif
    907   1.2   itojun 
    908  1.22   itojun 			/*
    909  1.22   itojun 			 * jumbo payload length must be larger than 65535.
    910  1.22   itojun 			 */
    911  1.22   itojun 			if (jumboplen <= IPV6_MAXPACKET) {
    912  1.22   itojun 				ip6stat.ip6s_badoptions++;
    913  1.22   itojun 				icmp6_error(m, ICMP6_PARAM_PROB,
    914  1.55   itojun 				    ICMP6_PARAMPROB_HEADER,
    915  1.55   itojun 				    erroff + opt + 2 - opthead);
    916  1.55   itojun 				return (-1);
    917  1.22   itojun 			}
    918  1.22   itojun 			*plenp = jumboplen;
    919  1.22   itojun 
    920  1.22   itojun 			break;
    921  1.35   itojun 		default:		/* unknown option */
    922  1.35   itojun 			if (hbhlen < IP6OPT_MINLEN) {
    923  1.35   itojun 				ip6stat.ip6s_toosmall++;
    924  1.35   itojun 				goto bad;
    925  1.35   itojun 			}
    926  1.55   itojun 			optlen = ip6_unknown_opt(opt, m,
    927  1.55   itojun 			    erroff + opt - opthead);
    928  1.55   itojun 			if (optlen == -1)
    929  1.55   itojun 				return (-1);
    930  1.35   itojun 			optlen += 2;
    931  1.35   itojun 			break;
    932   1.2   itojun 		}
    933   1.2   itojun 	}
    934   1.2   itojun 
    935  1.55   itojun 	return (0);
    936   1.2   itojun 
    937   1.2   itojun   bad:
    938   1.2   itojun 	m_freem(m);
    939  1.55   itojun 	return (-1);
    940   1.2   itojun }
    941   1.2   itojun 
    942   1.2   itojun /*
    943   1.2   itojun  * Unknown option processing.
    944   1.2   itojun  * The third argument `off' is the offset from the IPv6 header to the option,
    945   1.2   itojun  * which is necessary if the IPv6 header the and option header and IPv6 header
    946   1.2   itojun  * is not continuous in order to return an ICMPv6 error.
    947   1.2   itojun  */
    948   1.2   itojun int
    949   1.2   itojun ip6_unknown_opt(optp, m, off)
    950   1.2   itojun 	u_int8_t *optp;
    951   1.2   itojun 	struct mbuf *m;
    952   1.2   itojun 	int off;
    953   1.2   itojun {
    954   1.2   itojun 	struct ip6_hdr *ip6;
    955   1.2   itojun 
    956  1.35   itojun 	switch (IP6OPT_TYPE(*optp)) {
    957  1.35   itojun 	case IP6OPT_TYPE_SKIP: /* ignore the option */
    958  1.58   itojun 		return ((int)*(optp + 1));
    959  1.35   itojun 	case IP6OPT_TYPE_DISCARD:	/* silently discard */
    960  1.35   itojun 		m_freem(m);
    961  1.58   itojun 		return (-1);
    962  1.35   itojun 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
    963  1.35   itojun 		ip6stat.ip6s_badoptions++;
    964  1.35   itojun 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
    965  1.58   itojun 		return (-1);
    966  1.35   itojun 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
    967  1.35   itojun 		ip6stat.ip6s_badoptions++;
    968  1.35   itojun 		ip6 = mtod(m, struct ip6_hdr *);
    969  1.35   itojun 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
    970  1.35   itojun 		    (m->m_flags & (M_BCAST|M_MCAST)))
    971  1.35   itojun 			m_freem(m);
    972  1.35   itojun 		else
    973  1.35   itojun 			icmp6_error(m, ICMP6_PARAM_PROB,
    974  1.35   itojun 				    ICMP6_PARAMPROB_OPTION, off);
    975  1.58   itojun 		return (-1);
    976   1.2   itojun 	}
    977   1.2   itojun 
    978   1.2   itojun 	m_freem(m);		/* XXX: NOTREACHED */
    979  1.58   itojun 	return (-1);
    980   1.2   itojun }
    981   1.2   itojun 
    982   1.2   itojun /*
    983   1.9   itojun  * Create the "control" list for this pcb.
    984   1.9   itojun  *
    985   1.9   itojun  * The routine will be called from upper layer handlers like tcp6_input().
    986   1.9   itojun  * Thus the routine assumes that the caller (tcp6_input) have already
    987   1.9   itojun  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
    988   1.9   itojun  * very first mbuf on the mbuf chain.
    989   1.9   itojun  * We may want to add some infinite loop prevention or sanity checks for safety.
    990   1.9   itojun  * (This applies only when you are using KAME mbuf chain restriction, i.e.
    991   1.9   itojun  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
    992   1.2   itojun  */
    993   1.2   itojun void
    994   1.2   itojun ip6_savecontrol(in6p, mp, ip6, m)
    995  1.35   itojun 	struct in6pcb *in6p;
    996  1.35   itojun 	struct mbuf **mp;
    997  1.35   itojun 	struct ip6_hdr *ip6;
    998  1.35   itojun 	struct mbuf *m;
    999   1.2   itojun {
   1000   1.9   itojun 
   1001   1.9   itojun #ifdef SO_TIMESTAMP
   1002   1.2   itojun 	if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
   1003   1.2   itojun 		struct timeval tv;
   1004   1.2   itojun 
   1005   1.2   itojun 		microtime(&tv);
   1006   1.2   itojun 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
   1007  1.63   itojun 		    SCM_TIMESTAMP, SOL_SOCKET);
   1008   1.2   itojun 		if (*mp)
   1009   1.2   itojun 			mp = &(*mp)->m_next;
   1010   1.2   itojun 	}
   1011   1.9   itojun #endif
   1012   1.2   itojun 	if (in6p->in6p_flags & IN6P_RECVDSTADDR) {
   1013   1.2   itojun 		*mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst,
   1014  1.63   itojun 		    sizeof(struct in6_addr), IPV6_RECVDSTADDR, IPPROTO_IPV6);
   1015   1.2   itojun 		if (*mp)
   1016   1.2   itojun 			mp = &(*mp)->m_next;
   1017   1.2   itojun 	}
   1018   1.2   itojun 
   1019   1.2   itojun #ifdef noyet
   1020   1.2   itojun 	/* options were tossed above */
   1021   1.2   itojun 	if (in6p->in6p_flags & IN6P_RECVOPTS)
   1022   1.2   itojun 		/* broken */
   1023   1.2   itojun 	/* ip6_srcroute doesn't do what we want here, need to fix */
   1024   1.2   itojun 	if (in6p->in6p_flags & IPV6P_RECVRETOPTS)
   1025   1.2   itojun 		/* broken */
   1026   1.2   itojun #endif
   1027   1.2   itojun 
   1028   1.2   itojun 	/* RFC 2292 sec. 5 */
   1029  1.35   itojun 	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
   1030   1.2   itojun 		struct in6_pktinfo pi6;
   1031   1.2   itojun 		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
   1032   1.2   itojun 		if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
   1033   1.2   itojun 			pi6.ipi6_addr.s6_addr16[1] = 0;
   1034   1.2   itojun 		pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
   1035   1.2   itojun 					? m->m_pkthdr.rcvif->if_index
   1036   1.2   itojun 					: 0;
   1037   1.2   itojun 		*mp = sbcreatecontrol((caddr_t) &pi6,
   1038  1.63   itojun 		    sizeof(struct in6_pktinfo), IPV6_PKTINFO, IPPROTO_IPV6);
   1039   1.2   itojun 		if (*mp)
   1040   1.2   itojun 			mp = &(*mp)->m_next;
   1041   1.2   itojun 	}
   1042   1.2   itojun 	if (in6p->in6p_flags & IN6P_HOPLIMIT) {
   1043   1.2   itojun 		int hlim = ip6->ip6_hlim & 0xff;
   1044  1.63   itojun 		*mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
   1045  1.63   itojun 		    IPV6_HOPLIMIT, IPPROTO_IPV6);
   1046   1.2   itojun 		if (*mp)
   1047   1.2   itojun 			mp = &(*mp)->m_next;
   1048   1.2   itojun 	}
   1049   1.2   itojun 	/* IN6P_NEXTHOP - for outgoing packet only */
   1050   1.2   itojun 
   1051   1.2   itojun 	/*
   1052  1.75   itojun 	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
   1053  1.75   itojun 	 * privilege for the option (see ip6_ctloutput), but it might be too
   1054  1.75   itojun 	 * strict, since there might be some hop-by-hop options which can be
   1055  1.75   itojun 	 * returned to normal user.
   1056  1.75   itojun 	 * See also RFC 2292 section 6.
   1057   1.2   itojun 	 */
   1058  1.75   itojun 	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
   1059   1.2   itojun 		/*
   1060   1.2   itojun 		 * Check if a hop-by-hop options header is contatined in the
   1061   1.2   itojun 		 * received packet, and if so, store the options as ancillary
   1062   1.2   itojun 		 * data. Note that a hop-by-hop options header must be
   1063   1.2   itojun 		 * just after the IPv6 header, which fact is assured through
   1064   1.2   itojun 		 * the IPv6 input processing.
   1065   1.2   itojun 		 */
   1066   1.2   itojun 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   1067   1.2   itojun 		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
   1068   1.9   itojun 			struct ip6_hbh *hbh;
   1069   1.9   itojun 			int hbhlen;
   1070  1.62   itojun 			struct mbuf *ext;
   1071   1.9   itojun 
   1072  1.62   itojun 			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
   1073  1.62   itojun 			    ip6->ip6_nxt);
   1074  1.62   itojun 			if (ext == NULL) {
   1075   1.9   itojun 				ip6stat.ip6s_tooshort++;
   1076   1.9   itojun 				return;
   1077   1.9   itojun 			}
   1078  1.62   itojun 			hbh = mtod(ext, struct ip6_hbh *);
   1079   1.9   itojun 			hbhlen = (hbh->ip6h_len + 1) << 3;
   1080  1.62   itojun 			if (hbhlen != ext->m_len) {
   1081  1.62   itojun 				m_freem(ext);
   1082   1.9   itojun 				ip6stat.ip6s_tooshort++;
   1083   1.9   itojun 				return;
   1084   1.9   itojun 			}
   1085   1.2   itojun 
   1086   1.2   itojun 			/*
   1087   1.2   itojun 			 * XXX: We copy whole the header even if a jumbo
   1088   1.2   itojun 			 * payload option is included, which option is to
   1089   1.2   itojun 			 * be removed before returning in the RFC 2292.
   1090   1.2   itojun 			 * But it's too painful operation...
   1091   1.2   itojun 			 */
   1092   1.9   itojun 			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
   1093  1.63   itojun 			    IPV6_HOPOPTS, IPPROTO_IPV6);
   1094   1.2   itojun 			if (*mp)
   1095   1.2   itojun 				mp = &(*mp)->m_next;
   1096  1.62   itojun 			m_freem(ext);
   1097   1.2   itojun 		}
   1098   1.2   itojun 	}
   1099   1.2   itojun 
   1100   1.2   itojun 	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
   1101   1.2   itojun 	if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
   1102   1.2   itojun 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   1103  1.60   simonb 		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
   1104   1.2   itojun 
   1105   1.2   itojun 		/*
   1106   1.2   itojun 		 * Search for destination options headers or routing
   1107   1.2   itojun 		 * header(s) through the header chain, and stores each
   1108   1.2   itojun 		 * header as ancillary data.
   1109   1.2   itojun 		 * Note that the order of the headers remains in
   1110   1.2   itojun 		 * the chain of ancillary data.
   1111   1.2   itojun 		 */
   1112  1.35   itojun 		while (1) {	/* is explicit loop prevention necessary? */
   1113  1.62   itojun 			struct ip6_ext *ip6e = NULL;
   1114   1.9   itojun 			int elen;
   1115  1.62   itojun 			struct mbuf *ext = NULL;
   1116   1.9   itojun 
   1117  1.62   itojun 			/*
   1118  1.62   itojun 			 * if it is not an extension header, don't try to
   1119  1.62   itojun 			 * pull it from the chain.
   1120  1.62   itojun 			 */
   1121  1.62   itojun 			switch (nxt) {
   1122  1.62   itojun 			case IPPROTO_DSTOPTS:
   1123  1.62   itojun 			case IPPROTO_ROUTING:
   1124  1.62   itojun 			case IPPROTO_HOPOPTS:
   1125  1.62   itojun 			case IPPROTO_AH: /* is it possible? */
   1126  1.62   itojun 				break;
   1127  1.62   itojun 			default:
   1128  1.62   itojun 				goto loopend;
   1129  1.62   itojun 			}
   1130  1.62   itojun 
   1131  1.62   itojun 			ext = ip6_pullexthdr(m, off, nxt);
   1132  1.62   itojun 			if (ext == NULL) {
   1133   1.9   itojun 				ip6stat.ip6s_tooshort++;
   1134   1.9   itojun 				return;
   1135   1.9   itojun 			}
   1136  1.62   itojun 			ip6e = mtod(ext, struct ip6_ext *);
   1137   1.9   itojun 			if (nxt == IPPROTO_AH)
   1138   1.9   itojun 				elen = (ip6e->ip6e_len + 2) << 2;
   1139   1.9   itojun 			else
   1140   1.9   itojun 				elen = (ip6e->ip6e_len + 1) << 3;
   1141  1.62   itojun 			if (elen != ext->m_len) {
   1142  1.62   itojun 				m_freem(ext);
   1143   1.9   itojun 				ip6stat.ip6s_tooshort++;
   1144   1.9   itojun 				return;
   1145   1.9   itojun 			}
   1146  1.57  thorpej 			KASSERT(IP6_HDR_ALIGNED_P(ip6e));
   1147   1.2   itojun 
   1148  1.35   itojun 			switch (nxt) {
   1149  1.62   itojun 			case IPPROTO_DSTOPTS:
   1150  1.35   itojun 				if (!in6p->in6p_flags & IN6P_DSTOPTS)
   1151  1.35   itojun 					break;
   1152  1.35   itojun 
   1153  1.35   itojun 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
   1154  1.63   itojun 				    IPV6_DSTOPTS, IPPROTO_IPV6);
   1155  1.35   itojun 				if (*mp)
   1156  1.35   itojun 					mp = &(*mp)->m_next;
   1157  1.35   itojun 				break;
   1158  1.35   itojun 
   1159  1.35   itojun 			case IPPROTO_ROUTING:
   1160  1.35   itojun 				if (!in6p->in6p_flags & IN6P_RTHDR)
   1161  1.35   itojun 					break;
   1162  1.35   itojun 
   1163  1.35   itojun 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
   1164  1.63   itojun 				    IPV6_RTHDR, IPPROTO_IPV6);
   1165  1.35   itojun 				if (*mp)
   1166  1.35   itojun 					mp = &(*mp)->m_next;
   1167  1.35   itojun 				break;
   1168  1.35   itojun 
   1169  1.62   itojun 			case IPPROTO_HOPOPTS:
   1170  1.62   itojun 			case IPPROTO_AH: /* is it possible? */
   1171  1.62   itojun 				break;
   1172  1.62   itojun 
   1173  1.35   itojun 			default:
   1174  1.35   itojun 				/*
   1175  1.62   itojun 			 	 * other cases have been filtered in the above.
   1176  1.62   itojun 				 * none will visit this case.  here we supply
   1177  1.62   itojun 				 * the code just in case (nxt overwritten or
   1178  1.62   itojun 				 * other cases).
   1179  1.35   itojun 				 */
   1180  1.62   itojun 				m_freem(ext);
   1181  1.35   itojun 				goto loopend;
   1182  1.35   itojun 
   1183   1.2   itojun 			}
   1184   1.2   itojun 
   1185   1.2   itojun 			/* proceed with the next header. */
   1186   1.9   itojun 			off += elen;
   1187   1.2   itojun 			nxt = ip6e->ip6e_nxt;
   1188  1.62   itojun 			ip6e = NULL;
   1189  1.62   itojun 			m_freem(ext);
   1190  1.62   itojun 			ext = NULL;
   1191   1.2   itojun 		}
   1192   1.2   itojun 	  loopend:
   1193  1.62   itojun 	  	;
   1194   1.2   itojun 	}
   1195  1.62   itojun }
   1196  1.62   itojun 
   1197  1.62   itojun /*
   1198  1.62   itojun  * pull single extension header from mbuf chain.  returns single mbuf that
   1199  1.62   itojun  * contains the result, or NULL on error.
   1200  1.62   itojun  */
   1201  1.62   itojun static struct mbuf *
   1202  1.62   itojun ip6_pullexthdr(m, off, nxt)
   1203  1.62   itojun 	struct mbuf *m;
   1204  1.62   itojun 	size_t off;
   1205  1.62   itojun 	int nxt;
   1206  1.62   itojun {
   1207  1.62   itojun 	struct ip6_ext ip6e;
   1208  1.62   itojun 	size_t elen;
   1209  1.62   itojun 	struct mbuf *n;
   1210  1.62   itojun 
   1211  1.62   itojun #ifdef DIAGNOSTIC
   1212  1.62   itojun 	switch (nxt) {
   1213  1.62   itojun 	case IPPROTO_DSTOPTS:
   1214  1.62   itojun 	case IPPROTO_ROUTING:
   1215  1.62   itojun 	case IPPROTO_HOPOPTS:
   1216  1.62   itojun 	case IPPROTO_AH: /* is it possible? */
   1217  1.62   itojun 		break;
   1218  1.62   itojun 	default:
   1219  1.62   itojun 		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
   1220   1.2   itojun 	}
   1221  1.62   itojun #endif
   1222   1.2   itojun 
   1223  1.62   itojun 	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
   1224  1.62   itojun 	if (nxt == IPPROTO_AH)
   1225  1.62   itojun 		elen = (ip6e.ip6e_len + 2) << 2;
   1226  1.62   itojun 	else
   1227  1.62   itojun 		elen = (ip6e.ip6e_len + 1) << 3;
   1228  1.62   itojun 
   1229  1.62   itojun 	MGET(n, M_DONTWAIT, MT_DATA);
   1230  1.62   itojun 	if (n && elen >= MLEN) {
   1231  1.62   itojun 		MCLGET(n, M_DONTWAIT);
   1232  1.62   itojun 		if ((n->m_flags & M_EXT) == 0) {
   1233  1.62   itojun 			m_free(n);
   1234  1.62   itojun 			n = NULL;
   1235  1.62   itojun 		}
   1236  1.62   itojun 	}
   1237  1.62   itojun 	if (!n)
   1238  1.62   itojun 		return NULL;
   1239  1.62   itojun 
   1240  1.62   itojun 	n->m_len = 0;
   1241  1.62   itojun 	if (elen >= M_TRAILINGSPACE(n)) {
   1242  1.62   itojun 		m_free(n);
   1243  1.62   itojun 		return NULL;
   1244  1.62   itojun 	}
   1245  1.62   itojun 
   1246  1.62   itojun 	m_copydata(m, off, elen, mtod(n, caddr_t));
   1247  1.62   itojun 	n->m_len = elen;
   1248  1.62   itojun 	return n;
   1249   1.2   itojun }
   1250   1.2   itojun 
   1251   1.2   itojun /*
   1252   1.2   itojun  * Get pointer to the previous header followed by the header
   1253   1.2   itojun  * currently processed.
   1254   1.2   itojun  * XXX: This function supposes that
   1255   1.2   itojun  *	M includes all headers,
   1256   1.2   itojun  *	the next header field and the header length field of each header
   1257   1.2   itojun  *	are valid, and
   1258   1.2   itojun  *	the sum of each header length equals to OFF.
   1259   1.2   itojun  * Because of these assumptions, this function must be called very
   1260   1.2   itojun  * carefully. Moreover, it will not be used in the near future when
   1261   1.2   itojun  * we develop `neater' mechanism to process extension headers.
   1262   1.2   itojun  */
   1263  1.58   itojun u_int8_t *
   1264   1.2   itojun ip6_get_prevhdr(m, off)
   1265   1.2   itojun 	struct mbuf *m;
   1266   1.2   itojun 	int off;
   1267   1.2   itojun {
   1268   1.2   itojun 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   1269   1.2   itojun 
   1270   1.2   itojun 	if (off == sizeof(struct ip6_hdr))
   1271  1.58   itojun 		return (&ip6->ip6_nxt);
   1272   1.2   itojun 	else {
   1273   1.2   itojun 		int len, nxt;
   1274   1.2   itojun 		struct ip6_ext *ip6e = NULL;
   1275   1.2   itojun 
   1276   1.2   itojun 		nxt = ip6->ip6_nxt;
   1277   1.2   itojun 		len = sizeof(struct ip6_hdr);
   1278   1.2   itojun 		while (len < off) {
   1279   1.2   itojun 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
   1280   1.2   itojun 
   1281  1.35   itojun 			switch (nxt) {
   1282   1.2   itojun 			case IPPROTO_FRAGMENT:
   1283   1.2   itojun 				len += sizeof(struct ip6_frag);
   1284   1.2   itojun 				break;
   1285   1.2   itojun 			case IPPROTO_AH:
   1286   1.2   itojun 				len += (ip6e->ip6e_len + 2) << 2;
   1287   1.2   itojun 				break;
   1288   1.2   itojun 			default:
   1289   1.2   itojun 				len += (ip6e->ip6e_len + 1) << 3;
   1290   1.2   itojun 				break;
   1291   1.2   itojun 			}
   1292   1.2   itojun 			nxt = ip6e->ip6e_nxt;
   1293   1.2   itojun 		}
   1294   1.2   itojun 		if (ip6e)
   1295  1.58   itojun 			return (&ip6e->ip6e_nxt);
   1296   1.2   itojun 		else
   1297   1.2   itojun 			return NULL;
   1298  1.18   itojun 	}
   1299  1.18   itojun }
   1300  1.18   itojun 
   1301  1.18   itojun /*
   1302  1.18   itojun  * get next header offset.  m will be retained.
   1303  1.18   itojun  */
   1304  1.18   itojun int
   1305  1.18   itojun ip6_nexthdr(m, off, proto, nxtp)
   1306  1.18   itojun 	struct mbuf *m;
   1307  1.18   itojun 	int off;
   1308  1.18   itojun 	int proto;
   1309  1.18   itojun 	int *nxtp;
   1310  1.18   itojun {
   1311  1.18   itojun 	struct ip6_hdr ip6;
   1312  1.18   itojun 	struct ip6_ext ip6e;
   1313  1.18   itojun 	struct ip6_frag fh;
   1314  1.18   itojun 
   1315  1.18   itojun 	/* just in case */
   1316  1.18   itojun 	if (m == NULL)
   1317  1.18   itojun 		panic("ip6_nexthdr: m == NULL");
   1318  1.18   itojun 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
   1319  1.18   itojun 		return -1;
   1320  1.18   itojun 
   1321  1.18   itojun 	switch (proto) {
   1322  1.18   itojun 	case IPPROTO_IPV6:
   1323  1.18   itojun 		if (m->m_pkthdr.len < off + sizeof(ip6))
   1324  1.18   itojun 			return -1;
   1325  1.18   itojun 		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
   1326  1.18   itojun 		if (nxtp)
   1327  1.18   itojun 			*nxtp = ip6.ip6_nxt;
   1328  1.18   itojun 		off += sizeof(ip6);
   1329  1.18   itojun 		return off;
   1330  1.18   itojun 
   1331  1.18   itojun 	case IPPROTO_FRAGMENT:
   1332  1.18   itojun 		/*
   1333  1.18   itojun 		 * terminate parsing if it is not the first fragment,
   1334  1.18   itojun 		 * it does not make sense to parse through it.
   1335  1.18   itojun 		 */
   1336  1.18   itojun 		if (m->m_pkthdr.len < off + sizeof(fh))
   1337  1.18   itojun 			return -1;
   1338  1.18   itojun 		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
   1339  1.67   itojun 		if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0)
   1340  1.18   itojun 			return -1;
   1341  1.18   itojun 		if (nxtp)
   1342  1.18   itojun 			*nxtp = fh.ip6f_nxt;
   1343  1.18   itojun 		off += sizeof(struct ip6_frag);
   1344  1.18   itojun 		return off;
   1345  1.18   itojun 
   1346  1.18   itojun 	case IPPROTO_AH:
   1347  1.18   itojun 		if (m->m_pkthdr.len < off + sizeof(ip6e))
   1348  1.18   itojun 			return -1;
   1349  1.18   itojun 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
   1350  1.18   itojun 		if (nxtp)
   1351  1.18   itojun 			*nxtp = ip6e.ip6e_nxt;
   1352  1.18   itojun 		off += (ip6e.ip6e_len + 2) << 2;
   1353  1.47   itojun 		if (m->m_pkthdr.len < off)
   1354  1.47   itojun 			return -1;
   1355  1.18   itojun 		return off;
   1356  1.18   itojun 
   1357  1.18   itojun 	case IPPROTO_HOPOPTS:
   1358  1.18   itojun 	case IPPROTO_ROUTING:
   1359  1.18   itojun 	case IPPROTO_DSTOPTS:
   1360  1.18   itojun 		if (m->m_pkthdr.len < off + sizeof(ip6e))
   1361  1.18   itojun 			return -1;
   1362  1.18   itojun 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
   1363  1.18   itojun 		if (nxtp)
   1364  1.18   itojun 			*nxtp = ip6e.ip6e_nxt;
   1365  1.18   itojun 		off += (ip6e.ip6e_len + 1) << 3;
   1366  1.47   itojun 		if (m->m_pkthdr.len < off)
   1367  1.47   itojun 			return -1;
   1368  1.18   itojun 		return off;
   1369  1.18   itojun 
   1370  1.18   itojun 	case IPPROTO_NONE:
   1371  1.18   itojun 	case IPPROTO_ESP:
   1372  1.18   itojun 	case IPPROTO_IPCOMP:
   1373  1.18   itojun 		/* give up */
   1374  1.18   itojun 		return -1;
   1375  1.18   itojun 
   1376  1.18   itojun 	default:
   1377  1.18   itojun 		return -1;
   1378  1.18   itojun 	}
   1379  1.18   itojun }
   1380  1.18   itojun 
   1381  1.18   itojun /*
   1382  1.18   itojun  * get offset for the last header in the chain.  m will be kept untainted.
   1383  1.18   itojun  */
   1384  1.18   itojun int
   1385  1.18   itojun ip6_lasthdr(m, off, proto, nxtp)
   1386  1.18   itojun 	struct mbuf *m;
   1387  1.18   itojun 	int off;
   1388  1.18   itojun 	int proto;
   1389  1.18   itojun 	int *nxtp;
   1390  1.18   itojun {
   1391  1.18   itojun 	int newoff;
   1392  1.18   itojun 	int nxt;
   1393  1.18   itojun 
   1394  1.18   itojun 	if (!nxtp) {
   1395  1.18   itojun 		nxt = -1;
   1396  1.18   itojun 		nxtp = &nxt;
   1397  1.18   itojun 	}
   1398  1.18   itojun 	while (1) {
   1399  1.18   itojun 		newoff = ip6_nexthdr(m, off, proto, nxtp);
   1400  1.18   itojun 		if (newoff < 0)
   1401  1.18   itojun 			return off;
   1402  1.18   itojun 		else if (newoff < off)
   1403  1.18   itojun 			return -1;	/* invalid */
   1404  1.18   itojun 		else if (newoff == off)
   1405  1.18   itojun 			return newoff;
   1406  1.18   itojun 
   1407  1.18   itojun 		off = newoff;
   1408  1.18   itojun 		proto = *nxtp;
   1409   1.2   itojun 	}
   1410   1.2   itojun }
   1411   1.2   itojun 
   1412   1.2   itojun /*
   1413   1.2   itojun  * System control for IP6
   1414   1.2   itojun  */
   1415   1.2   itojun 
   1416   1.2   itojun u_char	inet6ctlerrmap[PRC_NCMDS] = {
   1417   1.2   itojun 	0,		0,		0,		0,
   1418   1.2   itojun 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
   1419   1.2   itojun 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
   1420   1.2   itojun 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
   1421   1.2   itojun 	0,		0,		0,		0,
   1422   1.2   itojun 	ENOPROTOOPT
   1423   1.2   itojun };
   1424   1.2   itojun 
   1425  1.70   atatat SYSCTL_SETUP(sysctl_net_inet6_ip6_setup, "sysctl net.inet6.ip6 subtree setup")
   1426   1.2   itojun {
   1427  1.26   itojun 
   1428  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1429  1.73   atatat 		       CTLFLAG_PERMANENT,
   1430  1.70   atatat 		       CTLTYPE_NODE, "net", NULL,
   1431  1.70   atatat 		       NULL, 0, NULL, 0,
   1432  1.70   atatat 		       CTL_NET, CTL_EOL);
   1433  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1434  1.73   atatat 		       CTLFLAG_PERMANENT,
   1435  1.74   atatat 		       CTLTYPE_NODE, "inet6",
   1436  1.74   atatat 		       SYSCTL_DESCR("PF_INET6 related settings"),
   1437  1.70   atatat 		       NULL, 0, NULL, 0,
   1438  1.70   atatat 		       CTL_NET, PF_INET6, CTL_EOL);
   1439  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1440  1.73   atatat 		       CTLFLAG_PERMANENT,
   1441  1.74   atatat 		       CTLTYPE_NODE, "ip6",
   1442  1.74   atatat 		       SYSCTL_DESCR("IPv6 related settings"),
   1443  1.70   atatat 		       NULL, 0, NULL, 0,
   1444  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_EOL);
   1445  1.70   atatat 
   1446  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1447  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1448  1.74   atatat 		       CTLTYPE_INT, "forwarding",
   1449  1.74   atatat 		       SYSCTL_DESCR("Enable forwarding of INET6 datagrams"),
   1450  1.70   atatat 		       NULL, 0, &ip6_forwarding, 0,
   1451  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1452  1.70   atatat 		       IPV6CTL_FORWARDING, CTL_EOL);
   1453  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1454  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1455  1.74   atatat 		       CTLTYPE_INT, "redirect",
   1456  1.74   atatat 		       SYSCTL_DESCR("Enable sending of ICMPv6 redirect messages"),
   1457  1.70   atatat 		       NULL, 0, &ip6_sendredirects, 0,
   1458  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1459  1.70   atatat 		       IPV6CTL_SENDREDIRECTS, CTL_EOL);
   1460  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1461  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1462  1.74   atatat 		       CTLTYPE_INT, "hlim",
   1463  1.74   atatat 		       SYSCTL_DESCR("Hop limit for an INET6 datagram"),
   1464  1.70   atatat 		       NULL, 0, &ip6_defhlim, 0,
   1465  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1466  1.70   atatat 		       IPV6CTL_DEFHLIM, CTL_EOL);
   1467  1.70   atatat #ifdef notyet
   1468  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1469  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1470  1.70   atatat 		       CTLTYPE_INT, "mtu", NULL,
   1471  1.70   atatat 		       NULL, 0, &, 0,
   1472  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1473  1.70   atatat 		       IPV6CTL_DEFMTU, CTL_EOL);
   1474  1.70   atatat #endif
   1475  1.70   atatat #ifdef __no_idea__
   1476  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1477  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1478  1.70   atatat 		       CTLTYPE_INT, "forwsrcrt", NULL,
   1479  1.70   atatat 		       NULL, 0, &?, 0,
   1480  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1481  1.70   atatat 		       IPV6CTL_FORWSRCRT, CTL_EOL);
   1482  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1483  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1484  1.70   atatat 		       CTLTYPE_STRUCT, "stats", NULL,
   1485  1.70   atatat 		       NULL, 0, &?, sizeof(?),
   1486  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1487  1.70   atatat 		       IPV6CTL_STATS, CTL_EOL);
   1488  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1489  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1490  1.70   atatat 		       CTLTYPE_STRUCT, "mrtstats", NULL,
   1491  1.70   atatat 		       NULL, 0, &?, sizeof(?),
   1492  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1493  1.70   atatat 		       IPV6CTL_MRTSTATS, CTL_EOL);
   1494  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1495  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1496  1.70   atatat 		       CTLTYPE_?, "mrtproto", NULL,
   1497  1.70   atatat 		       NULL, 0, &?, sizeof(?),
   1498  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1499  1.70   atatat 		       IPV6CTL_MRTPROTO, CTL_EOL);
   1500  1.70   atatat #endif
   1501  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1502  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1503  1.74   atatat 		       CTLTYPE_INT, "maxfragpackets",
   1504  1.74   atatat 		       SYSCTL_DESCR("Maximum number of fragments to buffer "
   1505  1.74   atatat 				    "for reassembly"),
   1506  1.70   atatat 		       NULL, 0, &ip6_maxfragpackets, 0,
   1507  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1508  1.70   atatat 		       IPV6CTL_MAXFRAGPACKETS, CTL_EOL);
   1509  1.70   atatat #ifdef __no_idea__
   1510  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1511  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1512  1.70   atatat 		       CTLTYPE_INT, "sourcecheck", NULL,
   1513  1.70   atatat 		       NULL, 0, &?, 0,
   1514  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1515  1.70   atatat 		       IPV6CTL_SOURCECHECK, CTL_EOL);
   1516  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1517  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1518  1.70   atatat 		       CTLTYPE_INT, "sourcecheck_logint", NULL,
   1519  1.70   atatat 		       NULL, 0, &?, 0,
   1520  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1521  1.70   atatat 		       IPV6CTL_SOURCECHECK_LOGINT, CTL_EOL);
   1522  1.70   atatat #endif
   1523  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1524  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1525  1.74   atatat 		       CTLTYPE_INT, "accept_rtadv",
   1526  1.74   atatat 		       SYSCTL_DESCR("Accept router advertisements"),
   1527  1.70   atatat 		       NULL, 0, &ip6_accept_rtadv, 0,
   1528  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1529  1.70   atatat 		       IPV6CTL_ACCEPT_RTADV, CTL_EOL);
   1530  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1531  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1532  1.74   atatat 		       CTLTYPE_INT, "keepfaith",
   1533  1.74   atatat 		       SYSCTL_DESCR("Activate faith interface"),
   1534  1.70   atatat 		       NULL, 0, &ip6_keepfaith, 0,
   1535  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1536  1.70   atatat 		       IPV6CTL_KEEPFAITH, CTL_EOL);
   1537  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1538  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1539  1.74   atatat 		       CTLTYPE_INT, "log_interval",
   1540  1.74   atatat 		       SYSCTL_DESCR("Minumum interval between logging "
   1541  1.74   atatat 				    "unroutable packets"),
   1542  1.70   atatat 		       NULL, 0, &ip6_log_interval, 0,
   1543  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1544  1.70   atatat 		       IPV6CTL_LOG_INTERVAL, CTL_EOL);
   1545  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1546  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1547  1.74   atatat 		       CTLTYPE_INT, "hdrnestlimit",
   1548  1.74   atatat 		       SYSCTL_DESCR("Maximum number of nested IPv6 headers"),
   1549  1.70   atatat 		       NULL, 0, &ip6_hdrnestlimit, 0,
   1550  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1551  1.70   atatat 		       IPV6CTL_HDRNESTLIMIT, CTL_EOL);
   1552  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1553  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1554  1.74   atatat 		       CTLTYPE_INT, "dad_count",
   1555  1.74   atatat 		       SYSCTL_DESCR("Number of Duplicate Address Detection "
   1556  1.74   atatat 				    "probes to send"),
   1557  1.70   atatat 		       NULL, 0, &ip6_dad_count, 0,
   1558  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1559  1.70   atatat 		       IPV6CTL_DAD_COUNT, CTL_EOL);
   1560  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1561  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1562  1.74   atatat 		       CTLTYPE_INT, "auto_flowlabel",
   1563  1.74   atatat 		       SYSCTL_DESCR("Assign random IPv6 flow labels"),
   1564  1.70   atatat 		       NULL, 0, &ip6_auto_flowlabel, 0,
   1565  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1566  1.70   atatat 		       IPV6CTL_AUTO_FLOWLABEL, CTL_EOL);
   1567  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1568  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1569  1.74   atatat 		       CTLTYPE_INT, "defmcasthlim",
   1570  1.74   atatat 		       SYSCTL_DESCR("Default multicast hop limit"),
   1571  1.70   atatat 		       NULL, 0, &ip6_defmcasthlim, 0,
   1572  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1573  1.70   atatat 		       IPV6CTL_DEFMCASTHLIM, CTL_EOL);
   1574  1.51   itojun #if NGIF > 0
   1575  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1576  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1577  1.74   atatat 		       CTLTYPE_INT, "gifhlim",
   1578  1.74   atatat 		       SYSCTL_DESCR("Default hop limit for a gif tunnel datagram"),
   1579  1.70   atatat 		       NULL, 0, &ip6_gif_hlim, 0,
   1580  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1581  1.70   atatat 		       IPV6CTL_GIF_HLIM, CTL_EOL);
   1582  1.70   atatat #endif /* NGIF */
   1583  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1584  1.73   atatat 		       CTLFLAG_PERMANENT,
   1585  1.74   atatat 		       CTLTYPE_STRING, "kame_version",
   1586  1.74   atatat 		       SYSCTL_DESCR("KAME Version"),
   1587  1.70   atatat 		       NULL, 0, __KAME_VERSION, 0,
   1588  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1589  1.70   atatat 		       IPV6CTL_KAME_VERSION, CTL_EOL);
   1590  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1591  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1592  1.74   atatat 		       CTLTYPE_INT, "use_deprecated",
   1593  1.74   atatat 		       SYSCTL_DESCR("Allow use of deprecated addresses as "
   1594  1.74   atatat 				    "source addresses"),
   1595  1.70   atatat 		       NULL, 0, &ip6_use_deprecated, 0,
   1596  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1597  1.70   atatat 		       IPV6CTL_USE_DEPRECATED, CTL_EOL);
   1598  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1599  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1600  1.70   atatat 		       CTLTYPE_INT, "rr_prune", NULL,
   1601  1.70   atatat 		       NULL, 0, &ip6_rr_prune, 0,
   1602  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1603  1.70   atatat 		       IPV6CTL_RR_PRUNE, CTL_EOL);
   1604  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1605  1.73   atatat 		       CTLFLAG_PERMANENT
   1606  1.70   atatat #ifndef INET6_BINDV6ONLY
   1607  1.73   atatat 		       |CTLFLAG_READWRITE,
   1608  1.70   atatat #endif
   1609  1.74   atatat 		       CTLTYPE_INT, "v6only",
   1610  1.74   atatat 		       SYSCTL_DESCR("Disallow PF_INET6 sockets from connecting "
   1611  1.74   atatat 				    "to PF_INET sockets"),
   1612  1.70   atatat 		       NULL, 0, &ip6_v6only, 0,
   1613  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1614  1.70   atatat 		       IPV6CTL_V6ONLY, CTL_EOL);
   1615  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1616  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1617  1.74   atatat 		       CTLTYPE_INT, "anonportmin",
   1618  1.74   atatat 		       SYSCTL_DESCR("Lowest ephemeral port number to assign"),
   1619  1.70   atatat 		       sysctl_net_inet_ip_ports, 0, &ip6_anonportmin, 0,
   1620  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1621  1.70   atatat 		       IPV6CTL_ANONPORTMIN, CTL_EOL);
   1622  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1623  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1624  1.74   atatat 		       CTLTYPE_INT, "anonportmax",
   1625  1.74   atatat 		       SYSCTL_DESCR("Highest ephemeral port number to assign"),
   1626  1.70   atatat 		       sysctl_net_inet_ip_ports, 0, &ip6_anonportmax, 0,
   1627  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1628  1.70   atatat 		       IPV6CTL_ANONPORTMAX, CTL_EOL);
   1629  1.26   itojun #ifndef IPNOPRIVPORTS
   1630  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1631  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1632  1.74   atatat 		       CTLTYPE_INT, "lowportmin",
   1633  1.74   atatat 		       SYSCTL_DESCR("Lowest privileged ephemeral port number "
   1634  1.74   atatat 				    "to assign"),
   1635  1.70   atatat 		       sysctl_net_inet_ip_ports, 0, &ip6_lowportmin, 0,
   1636  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1637  1.70   atatat 		       IPV6CTL_LOWPORTMIN, CTL_EOL);
   1638  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1639  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1640  1.74   atatat 		       CTLTYPE_INT, "lowportmax",
   1641  1.74   atatat 		       SYSCTL_DESCR("Highest privileged ephemeral port number "
   1642  1.74   atatat 				    "to assign"),
   1643  1.70   atatat 		       sysctl_net_inet_ip_ports, 0, &ip6_lowportmax, 0,
   1644  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1645  1.70   atatat 		       IPV6CTL_LOWPORTMAX, CTL_EOL);
   1646  1.70   atatat #endif /* IPNOPRIVPORTS */
   1647  1.73   atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1648  1.73   atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1649  1.74   atatat 		       CTLTYPE_INT, "maxfrags",
   1650  1.74   atatat 		       SYSCTL_DESCR("Maximum fragments in reassembly queue"),
   1651  1.70   atatat 		       NULL, 0, &ip6_maxfrags, 0,
   1652  1.70   atatat 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
   1653  1.70   atatat 		       IPV6CTL_MAXFRAGS, CTL_EOL);
   1654   1.2   itojun }
   1655