Home | History | Annotate | Line # | Download | only in netinet6
ip6_input.c revision 1.6
      1  1.6   itojun /*	$NetBSD: ip6_input.c,v 1.6 1999/07/31 18:41:16 itojun Exp $	*/
      2  1.3  thorpej 
      3  1.2   itojun /*
      4  1.2   itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  1.2   itojun  * All rights reserved.
      6  1.2   itojun  *
      7  1.2   itojun  * Redistribution and use in source and binary forms, with or without
      8  1.2   itojun  * modification, are permitted provided that the following conditions
      9  1.2   itojun  * are met:
     10  1.2   itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.2   itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.2   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2   itojun  *    notice, this list of conditions and the following disclaimer in the
     14  1.2   itojun  *    documentation and/or other materials provided with the distribution.
     15  1.2   itojun  * 3. Neither the name of the project nor the names of its contributors
     16  1.2   itojun  *    may be used to endorse or promote products derived from this software
     17  1.2   itojun  *    without specific prior written permission.
     18  1.2   itojun  *
     19  1.2   itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  1.2   itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.2   itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.2   itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  1.2   itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.2   itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.2   itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.2   itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.2   itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.2   itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.2   itojun  * SUCH DAMAGE.
     30  1.2   itojun  */
     31  1.2   itojun 
     32  1.2   itojun /*
     33  1.2   itojun  * Copyright (c) 1982, 1986, 1988, 1993
     34  1.2   itojun  *	The Regents of the University of California.  All rights reserved.
     35  1.2   itojun  *
     36  1.2   itojun  * Redistribution and use in source and binary forms, with or without
     37  1.2   itojun  * modification, are permitted provided that the following conditions
     38  1.2   itojun  * are met:
     39  1.2   itojun  * 1. Redistributions of source code must retain the above copyright
     40  1.2   itojun  *    notice, this list of conditions and the following disclaimer.
     41  1.2   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     42  1.2   itojun  *    notice, this list of conditions and the following disclaimer in the
     43  1.2   itojun  *    documentation and/or other materials provided with the distribution.
     44  1.2   itojun  * 3. All advertising materials mentioning features or use of this software
     45  1.2   itojun  *    must display the following acknowledgement:
     46  1.2   itojun  *	This product includes software developed by the University of
     47  1.2   itojun  *	California, Berkeley and its contributors.
     48  1.2   itojun  * 4. Neither the name of the University nor the names of its contributors
     49  1.2   itojun  *    may be used to endorse or promote products derived from this software
     50  1.2   itojun  *    without specific prior written permission.
     51  1.2   itojun  *
     52  1.2   itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  1.2   itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  1.2   itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  1.2   itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  1.2   itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  1.2   itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  1.2   itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  1.2   itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  1.2   itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  1.2   itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  1.2   itojun  * SUCH DAMAGE.
     63  1.2   itojun  *
     64  1.2   itojun  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
     65  1.2   itojun  */
     66  1.2   itojun 
     67  1.2   itojun #ifdef __FreeBSD__
     68  1.2   itojun #include "opt_ip6fw.h"
     69  1.2   itojun #endif
     70  1.2   itojun #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
     71  1.2   itojun #include "opt_inet.h"
     72  1.6   itojun #ifdef __NetBSD__	/*XXX*/
     73  1.4  thorpej #include "opt_ipsec.h"
     74  1.6   itojun #endif
     75  1.2   itojun #endif
     76  1.2   itojun 
     77  1.2   itojun #include <sys/param.h>
     78  1.2   itojun #include <sys/systm.h>
     79  1.2   itojun #include <sys/malloc.h>
     80  1.2   itojun #include <sys/mbuf.h>
     81  1.2   itojun #include <sys/domain.h>
     82  1.2   itojun #include <sys/protosw.h>
     83  1.2   itojun #include <sys/socket.h>
     84  1.2   itojun #include <sys/socketvar.h>
     85  1.2   itojun #include <sys/errno.h>
     86  1.2   itojun #include <sys/time.h>
     87  1.2   itojun #include <sys/kernel.h>
     88  1.2   itojun #include <sys/syslog.h>
     89  1.2   itojun #ifdef __NetBSD__
     90  1.2   itojun #include <sys/proc.h>
     91  1.2   itojun #endif
     92  1.2   itojun 
     93  1.2   itojun #include <net/if.h>
     94  1.2   itojun #include <net/if_types.h>
     95  1.2   itojun #include <net/if_dl.h>
     96  1.2   itojun #include <net/route.h>
     97  1.2   itojun #include <net/netisr.h>
     98  1.2   itojun 
     99  1.2   itojun #include <netinet/in.h>
    100  1.2   itojun #include <netinet6/in6_var.h>
    101  1.2   itojun #include <netinet/in_systm.h>
    102  1.2   itojun #include <netinet6/ip6.h>
    103  1.2   itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
    104  1.2   itojun #include <netinet6/in6_pcb.h>
    105  1.2   itojun #else
    106  1.2   itojun #include <netinet/in_pcb.h>
    107  1.2   itojun #endif
    108  1.2   itojun #include <netinet6/ip6_var.h>
    109  1.2   itojun #include <netinet6/icmp6.h>
    110  1.2   itojun #include <netinet6/in6_ifattach.h>
    111  1.2   itojun #include <netinet6/nd6.h>
    112  1.2   itojun 
    113  1.2   itojun #ifdef INET
    114  1.2   itojun #include <netinet/ip.h>
    115  1.2   itojun #include <netinet/ip_icmp.h>
    116  1.2   itojun #endif /*INET*/
    117  1.2   itojun 
    118  1.2   itojun #include <netinet6/ip6protosw.h>
    119  1.2   itojun 
    120  1.2   itojun /* we need it for NLOOP. */
    121  1.2   itojun #include "loop.h"
    122  1.2   itojun #include "faith.h"
    123  1.2   itojun 
    124  1.2   itojun #include "gif.h"
    125  1.2   itojun #include "bpfilter.h"
    126  1.2   itojun 
    127  1.2   itojun extern struct domain inet6domain;
    128  1.2   itojun extern struct ip6protosw inet6sw[];
    129  1.2   itojun #ifdef __bsdi__
    130  1.2   itojun extern struct ifnet loif;
    131  1.2   itojun #endif
    132  1.2   itojun 
    133  1.2   itojun u_char ip6_protox[IPPROTO_MAX];
    134  1.2   itojun static int ip6qmaxlen = IFQ_MAXLEN;
    135  1.2   itojun struct in6_ifaddr *in6_ifaddr;
    136  1.2   itojun struct ifqueue ip6intrq;
    137  1.2   itojun 
    138  1.2   itojun #ifdef __NetBSD__
    139  1.2   itojun extern struct ifnet loif[NLOOP];
    140  1.2   itojun int ip6_forward_srcrt;			/* XXX */
    141  1.2   itojun int ip6_sourcecheck;			/* XXX */
    142  1.2   itojun int ip6_sourcecheck_interval;		/* XXX */
    143  1.2   itojun #endif
    144  1.2   itojun 
    145  1.2   itojun struct ip6stat ip6stat;
    146  1.2   itojun 
    147  1.2   itojun static void ip6_init2 __P((void *));
    148  1.2   itojun 
    149  1.5   itojun static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
    150  1.2   itojun 
    151  1.2   itojun /*
    152  1.2   itojun  * IP6 initialization: fill in IP6 protocol switch table.
    153  1.2   itojun  * All protocols not implemented in kernel go to raw IP6 protocol handler.
    154  1.2   itojun  */
    155  1.2   itojun void
    156  1.2   itojun ip6_init()
    157  1.2   itojun {
    158  1.2   itojun 	register struct ip6protosw *pr;
    159  1.2   itojun 	register int i;
    160  1.2   itojun 	struct timeval tv;
    161  1.2   itojun 
    162  1.2   itojun 	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
    163  1.2   itojun 	if (pr == 0)
    164  1.2   itojun 		panic("ip6_init");
    165  1.2   itojun 	for (i = 0; i < IPPROTO_MAX; i++)
    166  1.2   itojun 		ip6_protox[i] = pr - inet6sw;
    167  1.2   itojun 	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
    168  1.2   itojun 	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
    169  1.2   itojun 		if (pr->pr_domain->dom_family == PF_INET6 &&
    170  1.2   itojun 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
    171  1.2   itojun 			ip6_protox[pr->pr_protocol] = pr - inet6sw;
    172  1.2   itojun 	ip6intrq.ifq_maxlen = ip6qmaxlen;
    173  1.2   itojun 	nd6_init();
    174  1.2   itojun 	frag6_init();
    175  1.2   itojun 	/*
    176  1.2   itojun 	 * in many cases, random() here does NOT return random number
    177  1.2   itojun 	 * as initialization during bootstrap time occur in fixed order.
    178  1.2   itojun 	 */
    179  1.2   itojun 	microtime(&tv);
    180  1.2   itojun 	ip6_flow_seq = random() ^ tv.tv_usec;
    181  1.2   itojun 	in6_iflladdr = malloc(i, M_IFADDR, M_WAITOK);
    182  1.2   itojun 	bzero(in6_iflladdr, i);
    183  1.2   itojun 
    184  1.2   itojun 	ip6_init2((void *)0);
    185  1.2   itojun }
    186  1.2   itojun 
    187  1.2   itojun static void
    188  1.2   itojun ip6_init2(dummy)
    189  1.2   itojun 	void *dummy;
    190  1.2   itojun {
    191  1.2   itojun 	int i;
    192  1.2   itojun 
    193  1.2   itojun 	/*
    194  1.2   itojun 	 * to route local address of p2p link to loopback,
    195  1.2   itojun 	 * assign loopback address first.
    196  1.2   itojun 	 */
    197  1.2   itojun 	for (i = 0; i < NLOOP; i++)
    198  1.2   itojun 		in6_ifattach(&loif[i], IN6_IFT_LOOP, NULL, 0);
    199  1.2   itojun 
    200  1.2   itojun 	/* get EUI64 from somewhere, attach pseudo interfaces */
    201  1.2   itojun 	if (in6_ifattach_getifid(NULL) == 0)
    202  1.2   itojun 		in6_ifattach_p2p();
    203  1.2   itojun 
    204  1.2   itojun 	/* nd6_timer_init */
    205  1.2   itojun 	timeout(nd6_timer, (caddr_t)0, hz);
    206  1.2   itojun }
    207  1.2   itojun 
    208  1.2   itojun #ifdef __FreeBSD__
    209  1.2   itojun /* cheat */
    210  1.2   itojun SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_init2, NULL);
    211  1.2   itojun #endif
    212  1.2   itojun 
    213  1.2   itojun /*
    214  1.2   itojun  * IP6 input interrupt handling. Just pass the packet to ip6_input.
    215  1.2   itojun  */
    216  1.2   itojun void
    217  1.2   itojun ip6intr()
    218  1.2   itojun {
    219  1.2   itojun 	int s;
    220  1.2   itojun 	struct mbuf *m;
    221  1.2   itojun 
    222  1.2   itojun 	for (;;) {
    223  1.2   itojun 		s = splimp();
    224  1.2   itojun 		IF_DEQUEUE(&ip6intrq, m);
    225  1.2   itojun 		splx(s);
    226  1.2   itojun 		if (m == 0)
    227  1.2   itojun 			return;
    228  1.2   itojun 		ip6_input(m);
    229  1.2   itojun 	}
    230  1.2   itojun }
    231  1.2   itojun 
    232  1.2   itojun #ifdef __FreeBSD__
    233  1.2   itojun NETISR_SET(NETISR_IPV6, ip6intr);
    234  1.2   itojun #endif
    235  1.2   itojun 
    236  1.2   itojun extern struct	route_in6 ip6_forward_rt;
    237  1.2   itojun 
    238  1.2   itojun void
    239  1.2   itojun ip6_input(m)
    240  1.2   itojun 	struct mbuf *m;
    241  1.2   itojun {
    242  1.2   itojun 	register struct ip6_hdr *ip6;
    243  1.2   itojun 	int off = sizeof(struct ip6_hdr), nest;
    244  1.2   itojun 	u_int32_t plen;
    245  1.5   itojun 	u_int32_t rtalert = ~0;
    246  1.2   itojun 	int nxt, ours = 0;
    247  1.2   itojun 
    248  1.2   itojun #ifdef IPSEC
    249  1.2   itojun 	/*
    250  1.2   itojun 	 * should the inner packet be considered authentic?
    251  1.2   itojun 	 * see comment in ah4_input().
    252  1.2   itojun 	 */
    253  1.2   itojun 	if (m) {
    254  1.2   itojun 		m->m_flags &= ~M_AUTHIPHDR;
    255  1.2   itojun 		m->m_flags &= ~M_AUTHIPDGM;
    256  1.2   itojun 	}
    257  1.2   itojun #endif
    258  1.2   itojun 	/*
    259  1.2   itojun 	 * mbuf statistics by kazu
    260  1.2   itojun 	 */
    261  1.2   itojun 	if (m->m_flags & M_EXT) {
    262  1.2   itojun 		if (m->m_next)
    263  1.2   itojun 			ip6stat.ip6s_mext2m++;
    264  1.2   itojun 		else
    265  1.2   itojun 			ip6stat.ip6s_mext1++;
    266  1.2   itojun 	} else {
    267  1.2   itojun 		if (m->m_next) {
    268  1.2   itojun 			if (m->m_flags & M_LOOP)
    269  1.2   itojun 				ip6stat.ip6s_m2m[loif[0].if_index]++;	/*XXX*/
    270  1.2   itojun 			else if (m->m_pkthdr.rcvif->if_index <= 31)
    271  1.2   itojun 				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
    272  1.2   itojun 			else
    273  1.2   itojun 				ip6stat.ip6s_m2m[0]++;
    274  1.2   itojun 		} else
    275  1.2   itojun 			ip6stat.ip6s_m1++;
    276  1.2   itojun 	}
    277  1.2   itojun 
    278  1.2   itojun 	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
    279  1.2   itojun 
    280  1.2   itojun 	ip6stat.ip6s_total++;
    281  1.2   itojun 
    282  1.2   itojun 	if (m->m_len < sizeof(struct ip6_hdr) &&
    283  1.2   itojun 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
    284  1.2   itojun 		ip6stat.ip6s_toosmall++;
    285  1.2   itojun 		return;
    286  1.2   itojun 	}
    287  1.2   itojun 
    288  1.2   itojun 	ip6 = mtod(m, struct ip6_hdr *);
    289  1.2   itojun 
    290  1.2   itojun 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
    291  1.2   itojun 		ip6stat.ip6s_badvers++;
    292  1.2   itojun 		goto bad;
    293  1.2   itojun 	}
    294  1.2   itojun 
    295  1.2   itojun 	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
    296  1.2   itojun 
    297  1.2   itojun 	/*
    298  1.2   itojun 	 * Scope check
    299  1.2   itojun 	 */
    300  1.2   itojun 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
    301  1.2   itojun 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
    302  1.2   itojun 		ip6stat.ip6s_badscope++;
    303  1.2   itojun 		goto bad;
    304  1.2   itojun 	}
    305  1.2   itojun 	if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
    306  1.2   itojun 	    IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
    307  1.2   itojun 		if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
    308  1.2   itojun 			ours = 1;
    309  1.2   itojun 			goto hbhcheck;
    310  1.2   itojun 		} else {
    311  1.2   itojun 			ip6stat.ip6s_badscope++;
    312  1.2   itojun 			goto bad;
    313  1.2   itojun 		}
    314  1.2   itojun 	}
    315  1.2   itojun 
    316  1.2   itojun 	if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
    317  1.2   itojun 		if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
    318  1.2   itojun 			ours = 1;
    319  1.2   itojun 			goto hbhcheck;
    320  1.2   itojun 		}
    321  1.2   itojun 	} else {
    322  1.2   itojun 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
    323  1.2   itojun 			ip6->ip6_src.s6_addr16[1]
    324  1.2   itojun 				= htons(m->m_pkthdr.rcvif->if_index);
    325  1.2   itojun 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
    326  1.2   itojun 			ip6->ip6_dst.s6_addr16[1]
    327  1.2   itojun 				= htons(m->m_pkthdr.rcvif->if_index);
    328  1.2   itojun 	}
    329  1.2   itojun 
    330  1.2   itojun 	/*
    331  1.2   itojun 	 * Multicast check
    332  1.2   itojun 	 */
    333  1.2   itojun 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    334  1.2   itojun 	  	struct	in6_multi *in6m = 0;
    335  1.2   itojun 		/*
    336  1.2   itojun 		 * See if we belong to the destination multicast group on the
    337  1.2   itojun 		 * arrival interface.
    338  1.2   itojun 		 */
    339  1.2   itojun 		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
    340  1.2   itojun 		if (in6m)
    341  1.2   itojun 			ours = 1;
    342  1.2   itojun 		else if (!ip6_mrouter) {
    343  1.2   itojun 			ip6stat.ip6s_notmember++;
    344  1.2   itojun 			ip6stat.ip6s_cantforward++;
    345  1.2   itojun 			goto bad;
    346  1.2   itojun 		}
    347  1.2   itojun 		goto hbhcheck;
    348  1.2   itojun 	}
    349  1.2   itojun 
    350  1.2   itojun 	/*
    351  1.2   itojun 	 *  Unicast check
    352  1.2   itojun 	 */
    353  1.2   itojun 	if (ip6_forward_rt.ro_rt == 0 ||
    354  1.2   itojun 	    !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
    355  1.2   itojun 				&ip6_forward_rt.ro_dst.sin6_addr)) {
    356  1.2   itojun 		if (ip6_forward_rt.ro_rt) {
    357  1.2   itojun 			RTFREE(ip6_forward_rt.ro_rt);
    358  1.2   itojun 			ip6_forward_rt.ro_rt = 0;
    359  1.2   itojun 		}
    360  1.2   itojun 		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
    361  1.2   itojun 		ip6_forward_rt.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
    362  1.2   itojun 		ip6_forward_rt.ro_dst.sin6_family = AF_INET6;
    363  1.2   itojun 		ip6_forward_rt.ro_dst.sin6_addr = ip6->ip6_dst;
    364  1.2   itojun 
    365  1.2   itojun #if defined(__bsdi__) || defined(__NetBSD__)
    366  1.2   itojun 		rtalloc((struct route *)&ip6_forward_rt);
    367  1.2   itojun #endif
    368  1.2   itojun #ifdef __FreeBSD__
    369  1.2   itojun 		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
    370  1.2   itojun #endif
    371  1.2   itojun 	}
    372  1.2   itojun 
    373  1.2   itojun #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
    374  1.2   itojun 
    375  1.2   itojun 	/*
    376  1.2   itojun 	 * Accept the packet if the forwarding interface to the destination
    377  1.2   itojun 	 * according to the routing table is the loopback interface,
    378  1.2   itojun 	 * unless the associated route has a gateway.
    379  1.2   itojun 	 * Note that this approach causes to accept a packet if there is a
    380  1.2   itojun 	 * route to the loopback interface for the destination of the packet.
    381  1.2   itojun 	 * But we think it's even useful in some situations, e.g. when using
    382  1.2   itojun 	 * a special daemon which wants to intercept the packet.
    383  1.2   itojun 	 */
    384  1.2   itojun 	if (ip6_forward_rt.ro_rt &&
    385  1.2   itojun 	    (ip6_forward_rt.ro_rt->rt_flags &
    386  1.2   itojun 	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
    387  1.2   itojun #if 0
    388  1.2   itojun 	    /*
    389  1.2   itojun 	     * The check below is redundant since the comparison of
    390  1.2   itojun 	     * the destination and the key of the rtentry has
    391  1.2   itojun 	     * already done through looking up the routing table.
    392  1.2   itojun 	     */
    393  1.2   itojun 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
    394  1.2   itojun 			       &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) &&
    395  1.2   itojun #endif
    396  1.2   itojun #ifdef __bsdi__
    397  1.2   itojun 	    ip6_foward.rt.ro_rt->rt_ifp == &loif
    398  1.2   itojun #else
    399  1.2   itojun 	    ip6_forward_rt.ro_rt->rt_ifp == &loif[0]
    400  1.2   itojun #endif
    401  1.2   itojun 		) {
    402  1.2   itojun 		struct in6_ifaddr *ia6 =
    403  1.2   itojun 			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
    404  1.2   itojun 		/* packet to tentative address must not be received */
    405  1.2   itojun 		if (ia6->ia6_flags & IN6_IFF_ANYCAST)
    406  1.2   itojun 			m->m_flags |= M_ANYCAST6;
    407  1.2   itojun 		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
    408  1.2   itojun 			/* this interface is ready */
    409  1.2   itojun 			ours = 1;
    410  1.2   itojun 			goto hbhcheck;
    411  1.2   itojun 		} else {
    412  1.2   itojun 			/* this interface is not ready, fall through */
    413  1.2   itojun 		}
    414  1.2   itojun 	}
    415  1.2   itojun 
    416  1.2   itojun 	/*
    417  1.2   itojun 	 * FAITH(Firewall Aided Internet Translator)
    418  1.2   itojun 	 */
    419  1.2   itojun #if defined(NFAITH) && 0 < NFAITH
    420  1.2   itojun 	if (ip6_keepfaith) {
    421  1.2   itojun 		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
    422  1.2   itojun 		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
    423  1.2   itojun 			/* XXX do we need more sanity checks? */
    424  1.2   itojun 			ours = 1;
    425  1.2   itojun 			goto hbhcheck;
    426  1.2   itojun 		}
    427  1.2   itojun 	}
    428  1.2   itojun #endif
    429  1.2   itojun 
    430  1.2   itojun 	/*
    431  1.2   itojun 	 * Now there is no reason to process the packet if it's not our own
    432  1.2   itojun 	 * and we're not a router.
    433  1.2   itojun 	 */
    434  1.2   itojun 	if (!ip6_forwarding) {
    435  1.2   itojun 		ip6stat.ip6s_cantforward++;
    436  1.2   itojun 		goto bad;
    437  1.2   itojun 	}
    438  1.2   itojun 
    439  1.2   itojun   hbhcheck:
    440  1.2   itojun 	/*
    441  1.2   itojun 	 * Process Hop-by-Hop options header if it's contained.
    442  1.2   itojun 	 * m may be modified in ip6_hopopts_input().
    443  1.2   itojun 	 * If a JumboPayload option is included, plen will also be modified.
    444  1.2   itojun 	 */
    445  1.2   itojun 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
    446  1.2   itojun 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
    447  1.2   itojun 		if (ip6_hopopts_input(&plen, &rtalert, &m, &off))
    448  1.2   itojun 			return;	/* m have already been freed */
    449  1.2   itojun 		/* adjust pointer */
    450  1.2   itojun 		ip6 = mtod(m, struct ip6_hdr *);
    451  1.2   itojun 		nxt = ((struct ip6_hbh *)(ip6 + 1))->ip6h_nxt;
    452  1.2   itojun 
    453  1.2   itojun 		/*
    454  1.2   itojun 		 * accept the packet if a router alert option is included
    455  1.2   itojun 		 * and we act as an IPv6 router.
    456  1.2   itojun 		 */
    457  1.5   itojun 		if (rtalert != ~0 && ip6_forwarding)
    458  1.2   itojun 			ours = 1;
    459  1.2   itojun 	} else
    460  1.2   itojun 		nxt = ip6->ip6_nxt;
    461  1.2   itojun 
    462  1.2   itojun 	/*
    463  1.2   itojun 	 * Check that the amount of data in the buffers
    464  1.2   itojun 	 * is as at least much as the IPv6 header would have us expect.
    465  1.2   itojun 	 * Trim mbufs if longer than we expect.
    466  1.2   itojun 	 * Drop packet if shorter than we expect.
    467  1.2   itojun 	 */
    468  1.2   itojun 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
    469  1.2   itojun 		ip6stat.ip6s_tooshort++;
    470  1.2   itojun 		goto bad;
    471  1.2   itojun 	}
    472  1.2   itojun 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
    473  1.2   itojun 		if (m->m_len == m->m_pkthdr.len) {
    474  1.2   itojun 			m->m_len = sizeof(struct ip6_hdr) + plen;
    475  1.2   itojun 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
    476  1.2   itojun 		} else
    477  1.2   itojun 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
    478  1.2   itojun 	}
    479  1.2   itojun 
    480  1.2   itojun 	/*
    481  1.2   itojun 	 * Forward if desirable.
    482  1.2   itojun 	 */
    483  1.2   itojun 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    484  1.2   itojun 		/*
    485  1.2   itojun 		 * If we are acting as a multicast router, all
    486  1.2   itojun 		 * incoming multicast packets are passed to the
    487  1.2   itojun 		 * kernel-level multicast forwarding function.
    488  1.2   itojun 		 * The packet is returned (relatively) intact; if
    489  1.2   itojun 		 * ip6_mforward() returns a non-zero value, the packet
    490  1.2   itojun 		 * must be discarded, else it may be accepted below.
    491  1.2   itojun 		 */
    492  1.2   itojun 		if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
    493  1.2   itojun 			ip6stat.ip6s_cantforward++;
    494  1.2   itojun 			m_freem(m);
    495  1.2   itojun 			return;
    496  1.2   itojun 		}
    497  1.2   itojun 		if (!ours) {
    498  1.2   itojun 			m_freem(m);
    499  1.2   itojun 			return;
    500  1.2   itojun 		}
    501  1.2   itojun 	}
    502  1.2   itojun 	else if (!ours) {
    503  1.2   itojun 		ip6_forward(m, 0);
    504  1.2   itojun 		return;
    505  1.2   itojun 	}
    506  1.2   itojun 
    507  1.2   itojun 	/*
    508  1.2   itojun 	 * Tell launch routine the next header
    509  1.2   itojun 	 */
    510  1.2   itojun 	ip6stat.ip6s_delivered++;
    511  1.2   itojun 	nest = 0;
    512  1.2   itojun 	while (nxt != IPPROTO_DONE) {
    513  1.2   itojun 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
    514  1.2   itojun 			ip6stat.ip6s_toomanyhdr++;
    515  1.2   itojun 			goto bad;
    516  1.2   itojun 		}
    517  1.2   itojun 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
    518  1.2   itojun 	}
    519  1.2   itojun 	return;
    520  1.2   itojun  bad:
    521  1.2   itojun 	m_freem(m);
    522  1.2   itojun }
    523  1.2   itojun 
    524  1.2   itojun /*
    525  1.2   itojun  * Hop-by-Hop options header processing. If a valid jumbo payload option is
    526  1.2   itojun  * included, the real payload length will be stored in plenp.
    527  1.2   itojun  */
    528  1.2   itojun static int
    529  1.2   itojun ip6_hopopts_input(plenp, rtalertp, mp, offp)
    530  1.2   itojun 	u_int32_t *plenp;
    531  1.5   itojun 	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
    532  1.2   itojun 	struct mbuf **mp;
    533  1.2   itojun 	int *offp;
    534  1.2   itojun {
    535  1.2   itojun 	register struct mbuf *m = *mp;
    536  1.2   itojun 	int off = *offp, hbhlen;
    537  1.2   itojun 	struct ip6_hbh *hbh;
    538  1.2   itojun 	u_int8_t *opt;
    539  1.2   itojun 
    540  1.2   itojun 	/* validation of the length of the header */
    541  1.2   itojun 	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
    542  1.2   itojun 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
    543  1.2   itojun 	hbhlen = (hbh->ip6h_len + 1) << 3;
    544  1.2   itojun 
    545  1.2   itojun 	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
    546  1.2   itojun 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
    547  1.2   itojun 	off += hbhlen;
    548  1.2   itojun 	hbhlen -= sizeof(struct ip6_hbh);
    549  1.2   itojun 	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
    550  1.2   itojun 
    551  1.2   itojun 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
    552  1.2   itojun 				hbhlen, rtalertp, plenp) < 0)
    553  1.2   itojun 		return(-1);
    554  1.2   itojun 
    555  1.2   itojun 	*offp = off;
    556  1.2   itojun 	*mp = m;
    557  1.2   itojun 	return(0);
    558  1.2   itojun }
    559  1.2   itojun 
    560  1.2   itojun /*
    561  1.2   itojun  * Search header for all Hop-by-hop options and process each option.
    562  1.2   itojun  * This function is separate from ip6_hopopts_input() in order to
    563  1.2   itojun  * handle a case where the sending node itself process its hop-by-hop
    564  1.2   itojun  * options header. In such a case, the function is called from ip6_output().
    565  1.2   itojun  */
    566  1.2   itojun int
    567  1.2   itojun ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
    568  1.2   itojun 	struct mbuf *m;
    569  1.2   itojun 	u_int8_t *opthead;
    570  1.2   itojun 	int hbhlen;
    571  1.5   itojun 	u_int32_t *rtalertp;
    572  1.2   itojun 	u_int32_t *plenp;
    573  1.2   itojun {
    574  1.2   itojun 	struct ip6_hdr *ip6;
    575  1.2   itojun 	int optlen = 0;
    576  1.2   itojun 	u_int8_t *opt = opthead;
    577  1.2   itojun 	u_int16_t rtalert_val;
    578  1.2   itojun 
    579  1.2   itojun 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
    580  1.2   itojun 		switch(*opt) {
    581  1.2   itojun 		 case IP6OPT_PAD1:
    582  1.2   itojun 			 optlen = 1;
    583  1.2   itojun 			 break;
    584  1.2   itojun 		 case IP6OPT_PADN:
    585  1.2   itojun 			 if (hbhlen < IP6OPT_MINLEN) {
    586  1.2   itojun 				 ip6stat.ip6s_toosmall++;
    587  1.2   itojun 				 goto bad;
    588  1.2   itojun 			 }
    589  1.2   itojun 			 optlen = *(opt + 1) + 2;
    590  1.2   itojun 			 break;
    591  1.2   itojun 		 case IP6OPT_RTALERT:
    592  1.2   itojun 			 if (hbhlen < IP6OPT_RTALERT_LEN) {
    593  1.2   itojun 				 ip6stat.ip6s_toosmall++;
    594  1.2   itojun 				 goto bad;
    595  1.2   itojun 			 }
    596  1.2   itojun 			 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2)
    597  1.2   itojun 				  /* XXX: should we discard the packet? */
    598  1.2   itojun 				 log(LOG_ERR, "length of router alert opt is inconsitent(%d)",
    599  1.2   itojun 				     *(opt + 1));
    600  1.2   itojun 			 optlen = IP6OPT_RTALERT_LEN;
    601  1.2   itojun 			 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
    602  1.2   itojun 			 *rtalertp = ntohs(rtalert_val);
    603  1.2   itojun 			 break;
    604  1.2   itojun 		 case IP6OPT_JUMBO:
    605  1.2   itojun 			 if (hbhlen < IP6OPT_JUMBO_LEN) {
    606  1.2   itojun 				 ip6stat.ip6s_toosmall++;
    607  1.2   itojun 				 goto bad;
    608  1.2   itojun 			 }
    609  1.2   itojun 			 if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2)
    610  1.2   itojun 				  /* XXX: should we discard the packet? */
    611  1.2   itojun 				 log(LOG_ERR, "length of jumbopayload opt "
    612  1.2   itojun 				     "is inconsistent(%d)",
    613  1.2   itojun 				     *(opt + 1));
    614  1.2   itojun 			 optlen = IP6OPT_JUMBO_LEN;
    615  1.2   itojun 
    616  1.2   itojun 			 /*
    617  1.2   itojun 			  * We can simply cast because of the alignment
    618  1.2   itojun 			  * requirement of the jumbo payload option.
    619  1.2   itojun 			  */
    620  1.2   itojun 			 *plenp = ntohl(*(u_int32_t *)(opt + 2));
    621  1.2   itojun 			 if (*plenp <= IPV6_MAXPACKET) {
    622  1.2   itojun 				 /*
    623  1.2   itojun 				  * jumbo payload length must be larger
    624  1.2   itojun 				  * than 65535
    625  1.2   itojun 				  */
    626  1.2   itojun 				 ip6stat.ip6s_badoptions++;
    627  1.2   itojun 				 icmp6_error(m, ICMP6_PARAM_PROB,
    628  1.2   itojun 					     ICMP6_PARAMPROB_HEADER,
    629  1.2   itojun 					     sizeof(struct ip6_hdr) +
    630  1.2   itojun 					     sizeof(struct ip6_hbh) +
    631  1.2   itojun 					     opt + 2 - opthead);
    632  1.2   itojun 				 return(-1);
    633  1.2   itojun 			 }
    634  1.2   itojun 
    635  1.2   itojun 			 ip6 = mtod(m, struct ip6_hdr *);
    636  1.2   itojun 			 if (ip6->ip6_plen) {
    637  1.2   itojun 				 /*
    638  1.2   itojun 				  * IPv6 packets that have non 0 payload length
    639  1.2   itojun 				  * must not contain a jumbo paylod option.
    640  1.2   itojun 				  */
    641  1.2   itojun 				 ip6stat.ip6s_badoptions++;
    642  1.2   itojun 				 icmp6_error(m, ICMP6_PARAM_PROB,
    643  1.2   itojun 					     ICMP6_PARAMPROB_HEADER,
    644  1.2   itojun 					     sizeof(struct ip6_hdr) +
    645  1.2   itojun 					     sizeof(struct ip6_hbh) +
    646  1.2   itojun 					     opt - opthead);
    647  1.2   itojun 				 return(-1);
    648  1.2   itojun 			 }
    649  1.2   itojun 			 break;
    650  1.2   itojun 		 default:		/* unknown option */
    651  1.2   itojun 			 if (hbhlen < IP6OPT_MINLEN) {
    652  1.2   itojun 				 ip6stat.ip6s_toosmall++;
    653  1.2   itojun 				 goto bad;
    654  1.2   itojun 			 }
    655  1.2   itojun 			 if ((optlen = ip6_unknown_opt(opt, m,
    656  1.2   itojun 						       sizeof(struct ip6_hdr) +
    657  1.2   itojun 						       sizeof(struct ip6_hbh) +
    658  1.2   itojun 						       opt - opthead)) == -1)
    659  1.2   itojun 				 return(-1);
    660  1.2   itojun 			 optlen += 2;
    661  1.2   itojun 			 break;
    662  1.2   itojun 		}
    663  1.2   itojun 	}
    664  1.2   itojun 
    665  1.2   itojun 	return(0);
    666  1.2   itojun 
    667  1.2   itojun   bad:
    668  1.2   itojun 	m_freem(m);
    669  1.2   itojun 	return(-1);
    670  1.2   itojun }
    671  1.2   itojun 
    672  1.2   itojun /*
    673  1.2   itojun  * Unknown option processing.
    674  1.2   itojun  * The third argument `off' is the offset from the IPv6 header to the option,
    675  1.2   itojun  * which is necessary if the IPv6 header the and option header and IPv6 header
    676  1.2   itojun  * is not continuous in order to return an ICMPv6 error.
    677  1.2   itojun  */
    678  1.2   itojun int
    679  1.2   itojun ip6_unknown_opt(optp, m, off)
    680  1.2   itojun 	u_int8_t *optp;
    681  1.2   itojun 	struct mbuf *m;
    682  1.2   itojun 	int off;
    683  1.2   itojun {
    684  1.2   itojun 	struct ip6_hdr *ip6;
    685  1.2   itojun 
    686  1.2   itojun 	switch(IP6OPT_TYPE(*optp)) {
    687  1.2   itojun 	 case IP6OPT_TYPE_SKIP: /* ignore the option */
    688  1.2   itojun 		 return((int)*(optp + 1));
    689  1.2   itojun 	 case IP6OPT_TYPE_DISCARD:	/* silently discard */
    690  1.2   itojun 		 m_freem(m);
    691  1.2   itojun 		 return(-1);
    692  1.2   itojun 	 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
    693  1.2   itojun 		 ip6stat.ip6s_badoptions++;
    694  1.2   itojun 		 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
    695  1.2   itojun 		 return(-1);
    696  1.2   itojun 	 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
    697  1.2   itojun 		 ip6stat.ip6s_badoptions++;
    698  1.2   itojun 		 ip6 = mtod(m, struct ip6_hdr *);
    699  1.2   itojun 		 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
    700  1.2   itojun 		     (m->m_flags & (M_BCAST|M_MCAST)))
    701  1.2   itojun 			 m_freem(m);
    702  1.2   itojun 		 else
    703  1.2   itojun 			 icmp6_error(m, ICMP6_PARAM_PROB,
    704  1.2   itojun 				     ICMP6_PARAMPROB_OPTION, off);
    705  1.2   itojun 		 return(-1);
    706  1.2   itojun 	}
    707  1.2   itojun 
    708  1.2   itojun 	m_freem(m);		/* XXX: NOTREACHED */
    709  1.2   itojun 	return(-1);
    710  1.2   itojun }
    711  1.2   itojun 
    712  1.2   itojun /*
    713  1.2   itojun  * Create the "control" list for this pcb
    714  1.2   itojun  */
    715  1.2   itojun void
    716  1.2   itojun ip6_savecontrol(in6p, mp, ip6, m)
    717  1.2   itojun 	register struct in6pcb *in6p;
    718  1.2   itojun 	register struct mbuf **mp;
    719  1.2   itojun 	register struct ip6_hdr *ip6;
    720  1.2   itojun 	register struct mbuf *m;
    721  1.2   itojun {
    722  1.2   itojun #ifdef __NetBSD__
    723  1.2   itojun 	struct proc *p = curproc;	/* XXX */
    724  1.2   itojun #endif
    725  1.2   itojun #ifdef __bsdi__
    726  1.2   itojun # define sbcreatecontrol	so_cmsg
    727  1.2   itojun #endif
    728  1.2   itojun 
    729  1.2   itojun 	if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
    730  1.2   itojun 		struct timeval tv;
    731  1.2   itojun 
    732  1.2   itojun 		microtime(&tv);
    733  1.2   itojun 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
    734  1.2   itojun 			SCM_TIMESTAMP, SOL_SOCKET);
    735  1.2   itojun 		if (*mp)
    736  1.2   itojun 			mp = &(*mp)->m_next;
    737  1.2   itojun 	}
    738  1.2   itojun 	if (in6p->in6p_flags & IN6P_RECVDSTADDR) {
    739  1.2   itojun 		*mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst,
    740  1.2   itojun 			sizeof(struct in6_addr), IPV6_RECVDSTADDR,
    741  1.2   itojun 			IPPROTO_IPV6);
    742  1.2   itojun 		if (*mp)
    743  1.2   itojun 			mp = &(*mp)->m_next;
    744  1.2   itojun 	}
    745  1.2   itojun 
    746  1.2   itojun #ifdef noyet
    747  1.2   itojun 	/* options were tossed above */
    748  1.2   itojun 	if (in6p->in6p_flags & IN6P_RECVOPTS)
    749  1.2   itojun 		/* broken */
    750  1.2   itojun 	/* ip6_srcroute doesn't do what we want here, need to fix */
    751  1.2   itojun 	if (in6p->in6p_flags & IPV6P_RECVRETOPTS)
    752  1.2   itojun 		/* broken */
    753  1.2   itojun #endif
    754  1.2   itojun 
    755  1.2   itojun 	/* RFC 2292 sec. 5 */
    756  1.2   itojun 	if (in6p->in6p_flags & IN6P_PKTINFO) {
    757  1.2   itojun 		struct in6_pktinfo pi6;
    758  1.2   itojun 		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
    759  1.2   itojun 		if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
    760  1.2   itojun 			pi6.ipi6_addr.s6_addr16[1] = 0;
    761  1.2   itojun 		pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
    762  1.2   itojun 					? m->m_pkthdr.rcvif->if_index
    763  1.2   itojun 					: 0;
    764  1.2   itojun 		*mp = sbcreatecontrol((caddr_t) &pi6,
    765  1.2   itojun 			sizeof(struct in6_pktinfo), IPV6_PKTINFO,
    766  1.2   itojun 			IPPROTO_IPV6);
    767  1.2   itojun 		if (*mp)
    768  1.2   itojun 			mp = &(*mp)->m_next;
    769  1.2   itojun 	}
    770  1.2   itojun 	if (in6p->in6p_flags & IN6P_HOPLIMIT) {
    771  1.2   itojun 		int hlim = ip6->ip6_hlim & 0xff;
    772  1.2   itojun 		*mp = sbcreatecontrol((caddr_t) &hlim,
    773  1.2   itojun 			sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
    774  1.2   itojun 		if (*mp)
    775  1.2   itojun 			mp = &(*mp)->m_next;
    776  1.2   itojun 	}
    777  1.2   itojun 	/* IN6P_NEXTHOP - for outgoing packet only */
    778  1.2   itojun 
    779  1.2   itojun 	/*
    780  1.2   itojun 	 * IPV6_HOPOPTS socket option. We require super-user privilege
    781  1.2   itojun 	 * for the option, but it might be too strict, since there might
    782  1.2   itojun 	 * be some hop-by-hop options which can be returned to normal user.
    783  1.2   itojun 	 * See RFC 2292 section 6.
    784  1.2   itojun 	 */
    785  1.2   itojun 	if ((in6p->in6p_flags & IN6P_HOPOPTS) &&
    786  1.2   itojun 	    p && !suser(p->p_ucred, &p->p_acflag)) {
    787  1.2   itojun 		/*
    788  1.2   itojun 		 * Check if a hop-by-hop options header is contatined in the
    789  1.2   itojun 		 * received packet, and if so, store the options as ancillary
    790  1.2   itojun 		 * data. Note that a hop-by-hop options header must be
    791  1.2   itojun 		 * just after the IPv6 header, which fact is assured through
    792  1.2   itojun 		 * the IPv6 input processing.
    793  1.2   itojun 		 */
    794  1.2   itojun 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    795  1.2   itojun 		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
    796  1.2   itojun 			struct ip6_hbh *hbh = (struct ip6_hbh *)(ip6 + 1);
    797  1.2   itojun 
    798  1.2   itojun 			/*
    799  1.2   itojun 			 * XXX: We copy whole the header even if a jumbo
    800  1.2   itojun 			 * payload option is included, which option is to
    801  1.2   itojun 			 * be removed before returning in the RFC 2292.
    802  1.2   itojun 			 * But it's too painful operation...
    803  1.2   itojun 			 */
    804  1.2   itojun 			*mp = sbcreatecontrol((caddr_t)hbh,
    805  1.2   itojun 					      (hbh->ip6h_len + 1) << 3,
    806  1.2   itojun 					      IPV6_HOPOPTS, IPPROTO_IPV6);
    807  1.2   itojun 			if (*mp)
    808  1.2   itojun 				mp = &(*mp)->m_next;
    809  1.2   itojun 		}
    810  1.2   itojun 	}
    811  1.2   itojun 
    812  1.2   itojun 	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
    813  1.2   itojun 	if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
    814  1.2   itojun 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    815  1.2   itojun 		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);;
    816  1.2   itojun 
    817  1.2   itojun 		/*
    818  1.2   itojun 		 * Search for destination options headers or routing
    819  1.2   itojun 		 * header(s) through the header chain, and stores each
    820  1.2   itojun 		 * header as ancillary data.
    821  1.2   itojun 		 * Note that the order of the headers remains in
    822  1.2   itojun 		 * the chain of ancillary data.
    823  1.2   itojun 		 */
    824  1.2   itojun 		while(1) {	/* is explicit loop prevention necessary? */
    825  1.2   itojun 			struct ip6_ext *ip6e =
    826  1.2   itojun 				(struct ip6_ext *)(mtod(m, caddr_t) + off);
    827  1.2   itojun 
    828  1.2   itojun 			switch(nxt) {
    829  1.2   itojun 		         case IPPROTO_DSTOPTS:
    830  1.2   itojun 				 if (!in6p->in6p_flags & IN6P_DSTOPTS)
    831  1.2   itojun 					 break;
    832  1.2   itojun 
    833  1.2   itojun 				 /*
    834  1.2   itojun 				  * We also require super-user privilege for
    835  1.2   itojun 				  * the option.
    836  1.2   itojun 				  * See the comments on IN6_HOPOPTS.
    837  1.2   itojun 				  */
    838  1.2   itojun 				 if (!p || !suser(p->p_ucred, &p->p_acflag))
    839  1.2   itojun 					 break;
    840  1.2   itojun 
    841  1.2   itojun 				 *mp = sbcreatecontrol((caddr_t)ip6e,
    842  1.2   itojun 						       (ip6e->ip6e_len + 1) << 3,
    843  1.2   itojun 						       IPV6_DSTOPTS,
    844  1.2   itojun 						       IPPROTO_IPV6);
    845  1.2   itojun 				 if (*mp)
    846  1.2   itojun 					 mp = &(*mp)->m_next;
    847  1.2   itojun 				 break;
    848  1.2   itojun 
    849  1.2   itojun 			 case IPPROTO_ROUTING:
    850  1.2   itojun 				 if (!in6p->in6p_flags & IN6P_RTHDR)
    851  1.2   itojun 					 break;
    852  1.2   itojun 
    853  1.2   itojun 				 *mp = sbcreatecontrol((caddr_t)ip6e,
    854  1.2   itojun 						       (ip6e->ip6e_len + 1) << 3,
    855  1.2   itojun 						       IPV6_RTHDR,
    856  1.2   itojun 						       IPPROTO_IPV6);
    857  1.2   itojun 				 if (*mp)
    858  1.2   itojun 					 mp = &(*mp)->m_next;
    859  1.2   itojun 				 break;
    860  1.2   itojun 
    861  1.2   itojun 			 case IPPROTO_UDP:
    862  1.2   itojun 			 case IPPROTO_TCP:
    863  1.2   itojun 			 case IPPROTO_ICMPV6:
    864  1.2   itojun 			 default:
    865  1.2   itojun 				 /*
    866  1.2   itojun 				  * stop search if we encounter an upper
    867  1.2   itojun 				  * layer protocol headers.
    868  1.2   itojun 				  */
    869  1.2   itojun 				 goto loopend;
    870  1.2   itojun 
    871  1.2   itojun 			 case IPPROTO_HOPOPTS:
    872  1.2   itojun 			 case IPPROTO_AH: /* is it possible? */
    873  1.2   itojun 				 break;
    874  1.2   itojun 			}
    875  1.2   itojun 
    876  1.2   itojun 			/* proceed with the next header. */
    877  1.2   itojun 			if (nxt == IPPROTO_AH)
    878  1.2   itojun 				off += (ip6e->ip6e_len + 2) << 2;
    879  1.2   itojun 			else
    880  1.2   itojun 				off += (ip6e->ip6e_len + 1) << 3;
    881  1.2   itojun 			nxt = ip6e->ip6e_nxt;
    882  1.2   itojun 		}
    883  1.2   itojun 	  loopend:
    884  1.2   itojun 	}
    885  1.2   itojun 	if ((in6p->in6p_flags & IN6P_HOPOPTS)
    886  1.2   itojun 	 && p && !suser(p->p_ucred, &p->p_acflag)) {
    887  1.2   itojun 		/* to be done */
    888  1.2   itojun 	}
    889  1.2   itojun 	if ((in6p->in6p_flags & IN6P_DSTOPTS)
    890  1.2   itojun 	 && p && !suser(p->p_ucred, &p->p_acflag)) {
    891  1.2   itojun 		/* to be done */
    892  1.2   itojun 	}
    893  1.2   itojun 	/* IN6P_RTHDR - to be done */
    894  1.2   itojun 
    895  1.2   itojun #ifdef __bsdi__
    896  1.2   itojun # undef sbcreatecontrol
    897  1.2   itojun #endif
    898  1.2   itojun }
    899  1.2   itojun 
    900  1.2   itojun /*
    901  1.2   itojun  * Get pointer to the previous header followed by the header
    902  1.2   itojun  * currently processed.
    903  1.2   itojun  * XXX: This function supposes that
    904  1.2   itojun  *	M includes all headers,
    905  1.2   itojun  *	the next header field and the header length field of each header
    906  1.2   itojun  *	are valid, and
    907  1.2   itojun  *	the sum of each header length equals to OFF.
    908  1.2   itojun  * Because of these assumptions, this function must be called very
    909  1.2   itojun  * carefully. Moreover, it will not be used in the near future when
    910  1.2   itojun  * we develop `neater' mechanism to process extension headers.
    911  1.2   itojun  */
    912  1.2   itojun char *
    913  1.2   itojun ip6_get_prevhdr(m, off)
    914  1.2   itojun 	struct mbuf *m;
    915  1.2   itojun 	int off;
    916  1.2   itojun {
    917  1.2   itojun 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    918  1.2   itojun 
    919  1.2   itojun 	if (off == sizeof(struct ip6_hdr))
    920  1.2   itojun 		return(&ip6->ip6_nxt);
    921  1.2   itojun 	else {
    922  1.2   itojun 		int len, nxt;
    923  1.2   itojun 		struct ip6_ext *ip6e = NULL;
    924  1.2   itojun 
    925  1.2   itojun 		nxt = ip6->ip6_nxt;
    926  1.2   itojun 		len = sizeof(struct ip6_hdr);
    927  1.2   itojun 		while (len < off) {
    928  1.2   itojun 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
    929  1.2   itojun 
    930  1.2   itojun 			switch(nxt) {
    931  1.2   itojun 			case IPPROTO_FRAGMENT:
    932  1.2   itojun 				len += sizeof(struct ip6_frag);
    933  1.2   itojun 				break;
    934  1.2   itojun 			case IPPROTO_AH:
    935  1.2   itojun 				len += (ip6e->ip6e_len + 2) << 2;
    936  1.2   itojun 				break;
    937  1.2   itojun 			default:
    938  1.2   itojun 				len += (ip6e->ip6e_len + 1) << 3;
    939  1.2   itojun 				break;
    940  1.2   itojun 			}
    941  1.2   itojun 			nxt = ip6e->ip6e_nxt;
    942  1.2   itojun 		}
    943  1.2   itojun 		if (ip6e)
    944  1.2   itojun 			return(&ip6e->ip6e_nxt);
    945  1.2   itojun 		else
    946  1.2   itojun 			return NULL;
    947  1.2   itojun 	}
    948  1.2   itojun }
    949  1.2   itojun 
    950  1.2   itojun /*
    951  1.2   itojun  * System control for IP6
    952  1.2   itojun  */
    953  1.2   itojun 
    954  1.2   itojun u_char	inet6ctlerrmap[PRC_NCMDS] = {
    955  1.2   itojun 	0,		0,		0,		0,
    956  1.2   itojun 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
    957  1.2   itojun 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
    958  1.2   itojun 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
    959  1.2   itojun 	0,		0,		0,		0,
    960  1.2   itojun 	ENOPROTOOPT
    961  1.2   itojun };
    962  1.2   itojun 
    963  1.2   itojun #ifdef __NetBSD__
    964  1.2   itojun #include <vm/vm.h>
    965  1.2   itojun #include <sys/sysctl.h>
    966  1.2   itojun 
    967  1.2   itojun int
    968  1.2   itojun ip6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    969  1.2   itojun 	int *name;
    970  1.2   itojun 	u_int namelen;
    971  1.2   itojun 	void *oldp;
    972  1.2   itojun 	size_t *oldlenp;
    973  1.2   itojun 	void *newp;
    974  1.2   itojun 	size_t newlen;
    975  1.2   itojun {
    976  1.2   itojun 	/* All sysctl names at this level are terminal. */
    977  1.2   itojun 	if (namelen != 1)
    978  1.2   itojun 		return ENOTDIR;
    979  1.2   itojun 
    980  1.2   itojun 	switch (name[0]) {
    981  1.2   itojun 
    982  1.2   itojun 	case IPV6CTL_FORWARDING:
    983  1.2   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen,
    984  1.2   itojun 				  &ip6_forwarding);
    985  1.2   itojun 	case IPV6CTL_SENDREDIRECTS:
    986  1.2   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen,
    987  1.2   itojun 				&ip6_sendredirects);
    988  1.2   itojun 	case IPV6CTL_DEFHLIM:
    989  1.2   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_defhlim);
    990  1.2   itojun 	case IPV6CTL_MAXFRAGPACKETS:
    991  1.2   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen,
    992  1.2   itojun 				&ip6_maxfragpackets);
    993  1.2   itojun 	case IPV6CTL_ACCEPT_RTADV:
    994  1.2   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen,
    995  1.2   itojun 				&ip6_accept_rtadv);
    996  1.2   itojun 	case IPV6CTL_KEEPFAITH:
    997  1.2   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_keepfaith);
    998  1.2   itojun 	case IPV6CTL_LOG_INTERVAL:
    999  1.2   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1000  1.2   itojun 				&ip6_log_interval);
   1001  1.2   itojun 	case IPV6CTL_HDRNESTLIMIT:
   1002  1.2   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1003  1.2   itojun 				&ip6_hdrnestlimit);
   1004  1.2   itojun 	case IPV6CTL_DAD_COUNT:
   1005  1.2   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_dad_count);
   1006  1.2   itojun 	case IPV6CTL_AUTO_FLOWLABEL:
   1007  1.2   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1008  1.2   itojun 				&ip6_auto_flowlabel);
   1009  1.2   itojun 	case IPV6CTL_DEFMCASTHLIM:
   1010  1.2   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1011  1.2   itojun 				&ip6_defmcasthlim);
   1012  1.2   itojun 	case IPV6CTL_GIF_HLIM:
   1013  1.2   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1014  1.2   itojun 				&ip6_gif_hlim);
   1015  1.2   itojun 	case IPV6CTL_KAME_VERSION:
   1016  1.2   itojun 		return sysctl_rdstring(oldp, oldlenp, newp, __KAME_VERSION);
   1017  1.2   itojun 	default:
   1018  1.2   itojun 		return EOPNOTSUPP;
   1019  1.2   itojun 	}
   1020  1.2   itojun 	/* NOTREACHED */
   1021  1.2   itojun }
   1022  1.2   itojun #endif /* __NetBSD__ */
   1023