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