Home | History | Annotate | Line # | Download | only in netinet6
ip6_input.c revision 1.38
      1 /*	$NetBSD: ip6_input.c,v 1.38 2001/03/16 12:22:34 itojun Exp $	*/
      2 /*	$KAME: ip6_input.c,v 1.183 2001/03/01 15:15:23 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1988, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. All advertising materials mentioning features or use of this software
     46  *    must display the following acknowledgement:
     47  *	This product includes software developed by the University of
     48  *	California, Berkeley and its contributors.
     49  * 4. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
     66  */
     67 
     68 #include "opt_inet.h"
     69 #include "opt_ipsec.h"
     70 #include "opt_pfil_hooks.h"
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/malloc.h>
     75 #include <sys/mbuf.h>
     76 #include <sys/domain.h>
     77 #include <sys/protosw.h>
     78 #include <sys/socket.h>
     79 #include <sys/socketvar.h>
     80 #include <sys/errno.h>
     81 #include <sys/time.h>
     82 #include <sys/kernel.h>
     83 #include <sys/syslog.h>
     84 #include <sys/proc.h>
     85 
     86 #include <net/if.h>
     87 #include <net/if_types.h>
     88 #include <net/if_dl.h>
     89 #include <net/route.h>
     90 #include <net/netisr.h>
     91 #ifdef PFIL_HOOKS
     92 #include <net/pfil.h>
     93 #endif
     94 
     95 #include <netinet/in.h>
     96 #include <netinet/in_systm.h>
     97 #ifdef INET
     98 #include <netinet/ip.h>
     99 #include <netinet/ip_icmp.h>
    100 #endif /*INET*/
    101 #include <netinet/ip6.h>
    102 #include <netinet6/in6_var.h>
    103 #include <netinet6/ip6_var.h>
    104 #include <netinet6/in6_pcb.h>
    105 #include <netinet/icmp6.h>
    106 #include <netinet6/in6_ifattach.h>
    107 #include <netinet6/nd6.h>
    108 #include <netinet6/in6_prefix.h>
    109 
    110 #ifdef IPSEC
    111 #include <netinet6/ipsec.h>
    112 #endif
    113 
    114 #include <netinet6/ip6protosw.h>
    115 
    116 /* we need it for NLOOP. */
    117 #include "loop.h"
    118 #include "faith.h"
    119 
    120 #include "gif.h"
    121 #include "bpfilter.h"
    122 
    123 #include <net/net_osdep.h>
    124 
    125 extern struct domain inet6domain;
    126 
    127 u_char ip6_protox[IPPROTO_MAX];
    128 static int ip6qmaxlen = IFQ_MAXLEN;
    129 struct in6_ifaddr *in6_ifaddr;
    130 struct ifqueue ip6intrq;
    131 
    132 extern struct ifnet loif[NLOOP];
    133 int ip6_forward_srcrt;			/* XXX */
    134 int ip6_sourcecheck;			/* XXX */
    135 int ip6_sourcecheck_interval;		/* XXX */
    136 
    137 #ifdef PFIL_HOOKS
    138 struct pfil_head inet6_pfil_hook;
    139 #endif
    140 
    141 struct ip6stat ip6stat;
    142 
    143 static void ip6_init2 __P((void *));
    144 
    145 static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
    146 
    147 /*
    148  * IP6 initialization: fill in IP6 protocol switch table.
    149  * All protocols not implemented in kernel go to raw IP6 protocol handler.
    150  */
    151 void
    152 ip6_init()
    153 {
    154 	struct ip6protosw *pr;
    155 	int i;
    156 	struct timeval tv;
    157 
    158 	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
    159 	if (pr == 0)
    160 		panic("ip6_init");
    161 	for (i = 0; i < IPPROTO_MAX; i++)
    162 		ip6_protox[i] = pr - inet6sw;
    163 	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
    164 	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
    165 		if (pr->pr_domain->dom_family == PF_INET6 &&
    166 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
    167 			ip6_protox[pr->pr_protocol] = pr - inet6sw;
    168 	ip6intrq.ifq_maxlen = ip6qmaxlen;
    169 	nd6_init();
    170 	frag6_init();
    171 	/*
    172 	 * in many cases, random() here does NOT return random number
    173 	 * as initialization during bootstrap time occur in fixed order.
    174 	 */
    175 	microtime(&tv);
    176 	ip6_flow_seq = random() ^ tv.tv_usec;
    177 
    178 	ip6_init2((void *)0);
    179 
    180 #ifdef PFIL_HOOKS
    181 	/* Register our Packet Filter hook. */
    182 	inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
    183 	inet6_pfil_hook.ph_af   = AF_INET6;
    184 	i = pfil_head_register(&inet6_pfil_hook);
    185 	if (i != 0)
    186 		printf("ip6_init: WARNING: unable to register pfil hook, "
    187 		    "error %d\n", i);
    188 #endif /* PFIL_HOOKS */
    189 }
    190 
    191 static void
    192 ip6_init2(dummy)
    193 	void *dummy;
    194 {
    195 	/*
    196 	 * to route local address of p2p link to loopback,
    197 	 * assign loopback address first.
    198 	 */
    199 	in6_ifattach(&loif[0], NULL);
    200 
    201 	/* nd6_timer_init */
    202 	callout_init(&nd6_timer_ch);
    203 	callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
    204 	/* router renumbering prefix list maintenance */
    205 	callout_init(&in6_rr_timer_ch);
    206 	callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL);
    207 }
    208 
    209 /*
    210  * IP6 input interrupt handling. Just pass the packet to ip6_input.
    211  */
    212 void
    213 ip6intr()
    214 {
    215 	int s;
    216 	struct mbuf *m;
    217 
    218 	for (;;) {
    219 		s = splimp();
    220 		IF_DEQUEUE(&ip6intrq, m);
    221 		splx(s);
    222 		if (m == 0)
    223 			return;
    224 		ip6_input(m);
    225 	}
    226 }
    227 
    228 extern struct	route_in6 ip6_forward_rt;
    229 
    230 void
    231 ip6_input(m)
    232 	struct mbuf *m;
    233 {
    234 	struct ip6_hdr *ip6;
    235 	int off = sizeof(struct ip6_hdr), nest;
    236 	u_int32_t plen;
    237 	u_int32_t rtalert = ~0;
    238 	int nxt, ours = 0;
    239 	struct ifnet *deliverifp = NULL;
    240 
    241 #ifdef IPSEC
    242 	/*
    243 	 * should the inner packet be considered authentic?
    244 	 * see comment in ah4_input().
    245 	 */
    246 	if (m) {
    247 		m->m_flags &= ~M_AUTHIPHDR;
    248 		m->m_flags &= ~M_AUTHIPDGM;
    249 	}
    250 #endif
    251 
    252 	/*
    253 	 * mbuf statistics by kazu
    254 	 */
    255 	if (m->m_flags & M_EXT) {
    256 		if (m->m_next)
    257 			ip6stat.ip6s_mext2m++;
    258 		else
    259 			ip6stat.ip6s_mext1++;
    260 	} else {
    261 		if (m->m_next) {
    262 			if (m->m_flags & M_LOOP) {
    263 				ip6stat.ip6s_m2m[loif[0].if_index]++;	/*XXX*/
    264 			} else if (m->m_pkthdr.rcvif->if_index <= 31)
    265 				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
    266 			else
    267 				ip6stat.ip6s_m2m[0]++;
    268 		} else
    269 			ip6stat.ip6s_m1++;
    270 	}
    271 
    272 	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
    273 	ip6stat.ip6s_total++;
    274 
    275 #ifndef PULLDOWN_TEST
    276 	/* XXX is the line really necessary? */
    277 	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
    278 #endif
    279 
    280 	if (m->m_len < sizeof(struct ip6_hdr)) {
    281 		struct ifnet *inifp;
    282 		inifp = m->m_pkthdr.rcvif;
    283 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
    284 			ip6stat.ip6s_toosmall++;
    285 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
    286 			return;
    287 		}
    288 	}
    289 
    290 	ip6 = mtod(m, struct ip6_hdr *);
    291 
    292 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
    293 		ip6stat.ip6s_badvers++;
    294 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
    295 		goto bad;
    296 	}
    297 
    298 #ifdef PFIL_HOOKS
    299 	/*
    300 	 * Run through list of hooks for input packets.  If there are any
    301 	 * filters which require that additional packets in the flow are
    302 	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
    303 	 * Note that filters must _never_ set this flag, as another filter
    304 	 * in the list may have previously cleared it.
    305 	 */
    306 	if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif,
    307 			   PFIL_IN) != 0)
    308 		return;
    309 	if (m == NULL)
    310 		return;
    311 	ip6 = mtod(m, struct ip6_hdr *);
    312 #endif /* PFIL_HOOKS */
    313 
    314 
    315 	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
    316 
    317 #ifdef ALTQ
    318 	/* XXX Temporary until ALTQ is changed to use a pfil hook */
    319 	if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
    320 		/* packet is dropped by traffic conditioner */
    321 		return;
    322 	}
    323 #endif
    324 
    325 	/*
    326 	 * Scope check
    327 	 */
    328 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
    329 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
    330 		ip6stat.ip6s_badscope++;
    331 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    332 		goto bad;
    333 	}
    334 	/*
    335 	 * The following check is not documented in the spec.  Malicious party
    336 	 * may be able to use IPv4 mapped addr to confuse tcp/udp stack and
    337 	 * bypass security checks (act as if it was from 127.0.0.1 by using
    338 	 * IPv6 src ::ffff:127.0.0.1).	Be cautious.
    339 	 *
    340 	 * This check chokes if we are in SIIT cloud.  As none of BSDs support
    341 	 * IPv4-less kernel compilation, we cannot support SIIT environment
    342 	 * at all.  So, it makes more sense for us to reject any malicious
    343 	 * packets for non-SIIT environment, than try to do a partical support
    344 	 * for SIIT environment.
    345 	 */
    346 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    347 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    348 		ip6stat.ip6s_badscope++;
    349 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    350 		goto bad;
    351 	}
    352 #if 0
    353 	/*
    354 	 * Reject packets with IPv4 compatible addresses (auto tunnel).
    355 	 *
    356 	 * The code forbids auto tunnel relay case in RFC1933 (the check is
    357 	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
    358 	 * is revised to forbid relaying case.
    359 	 */
    360 	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
    361 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
    362 		ip6stat.ip6s_badscope++;
    363 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    364 		goto bad;
    365 	}
    366 #endif
    367 
    368 	if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
    369 	    IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
    370 		if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
    371 			ours = 1;
    372 			deliverifp = m->m_pkthdr.rcvif;
    373 			goto hbhcheck;
    374 		} else {
    375 			ip6stat.ip6s_badscope++;
    376 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    377 			goto bad;
    378 		}
    379 	}
    380 
    381 	/* drop packets if interface ID portion is already filled */
    382 	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
    383 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
    384 		    ip6->ip6_src.s6_addr16[1]) {
    385 			ip6stat.ip6s_badscope++;
    386 			goto bad;
    387 		}
    388 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
    389 		    ip6->ip6_dst.s6_addr16[1]) {
    390 			ip6stat.ip6s_badscope++;
    391 			goto bad;
    392 		}
    393 	}
    394 
    395 #ifndef FAKE_LOOPBACK_IF
    396 	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0)
    397 #else
    398 	if (1)
    399 #endif
    400 	{
    401 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
    402 			ip6->ip6_src.s6_addr16[1]
    403 				= htons(m->m_pkthdr.rcvif->if_index);
    404 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
    405 			ip6->ip6_dst.s6_addr16[1]
    406 				= htons(m->m_pkthdr.rcvif->if_index);
    407 	}
    408 
    409 	/*
    410 	 * XXX we need this since we do not have "goto ours" hack route
    411 	 * for some of our ifaddrs on loopback interface.
    412 	 * we should correct it by changing in6_ifattach to install
    413 	 * "goto ours" hack route.
    414 	 */
    415 	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0) {
    416 		if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
    417 			ours = 1;
    418 			deliverifp = m->m_pkthdr.rcvif;
    419 			goto hbhcheck;
    420 		}
    421 	}
    422 
    423 	/*
    424 	 * Multicast check
    425 	 */
    426 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    427 	  	struct	in6_multi *in6m = 0;
    428 
    429 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
    430 		/*
    431 		 * See if we belong to the destination multicast group on the
    432 		 * arrival interface.
    433 		 */
    434 		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
    435 		if (in6m)
    436 			ours = 1;
    437 		else if (!ip6_mrouter) {
    438 			ip6stat.ip6s_notmember++;
    439 			ip6stat.ip6s_cantforward++;
    440 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
    441 			goto bad;
    442 		}
    443 		deliverifp = m->m_pkthdr.rcvif;
    444 		goto hbhcheck;
    445 	}
    446 
    447 	/*
    448 	 *  Unicast check
    449 	 */
    450 	if (ip6_forward_rt.ro_rt != NULL &&
    451 	    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
    452 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
    453 			       &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
    454 		ip6stat.ip6s_forward_cachehit++;
    455 	else {
    456 		struct sockaddr_in6 *dst6;
    457 
    458 		if (ip6_forward_rt.ro_rt) {
    459 			/* route is down or destination is different */
    460 			ip6stat.ip6s_forward_cachemiss++;
    461 			RTFREE(ip6_forward_rt.ro_rt);
    462 			ip6_forward_rt.ro_rt = 0;
    463 		}
    464 
    465 		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
    466 		dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
    467 		dst6->sin6_len = sizeof(struct sockaddr_in6);
    468 		dst6->sin6_family = AF_INET6;
    469 		dst6->sin6_addr = ip6->ip6_dst;
    470 
    471 		rtalloc((struct route *)&ip6_forward_rt);
    472 	}
    473 
    474 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
    475 
    476 	/*
    477 	 * Accept the packet if the forwarding interface to the destination
    478 	 * according to the routing table is the loopback interface,
    479 	 * unless the associated route has a gateway.
    480 	 * Note that this approach causes to accept a packet if there is a
    481 	 * route to the loopback interface for the destination of the packet.
    482 	 * But we think it's even useful in some situations, e.g. when using
    483 	 * a special daemon which wants to intercept the packet.
    484 	 */
    485 	if (ip6_forward_rt.ro_rt &&
    486 	    (ip6_forward_rt.ro_rt->rt_flags &
    487 	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
    488 #if 0
    489 	    /*
    490 	     * The check below is redundant since the comparison of
    491 	     * the destination and the key of the rtentry has
    492 	     * already done through looking up the routing table.
    493 	     */
    494 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
    495 			       &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) &&
    496 #endif
    497 	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
    498 		struct in6_ifaddr *ia6 =
    499 			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
    500 		if (ia6->ia6_flags & IN6_IFF_ANYCAST)
    501 			m->m_flags |= M_ANYCAST6;
    502 		/*
    503 		 * packets to a tentative, duplicated, or somehow invalid
    504 		 * address must not be accepted.
    505 		 */
    506 		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
    507 			/* this address is ready */
    508 			ours = 1;
    509 			deliverifp = ia6->ia_ifp;	/* correct? */
    510 			goto hbhcheck;
    511 		} else {
    512 			/* address is not ready, so discard the packet. */
    513 			nd6log((LOG_INFO,
    514 			    "ip6_input: packet to an unready address %s->%s\n",
    515 			    ip6_sprintf(&ip6->ip6_src),
    516 			    ip6_sprintf(&ip6->ip6_dst)));
    517 
    518 			goto bad;
    519 		}
    520 	}
    521 
    522 	/*
    523 	 * FAITH(Firewall Aided Internet Translator)
    524 	 */
    525 #if defined(NFAITH) && 0 < NFAITH
    526 	if (ip6_keepfaith) {
    527 		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
    528 		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
    529 			/* XXX do we need more sanity checks? */
    530 			ours = 1;
    531 			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /*faith*/
    532 			goto hbhcheck;
    533 		}
    534 	}
    535 #endif
    536 
    537 #if 0
    538     {
    539 	/*
    540 	 * Last resort: check in6_ifaddr for incoming interface.
    541 	 * The code is here until I update the "goto ours hack" code above
    542 	 * working right.
    543 	 */
    544 	struct ifaddr *ifa;
    545 	for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
    546 	     ifa;
    547 	     ifa = ifa->ifa_list.tqe_next) {
    548 		if (ifa->ifa_addr == NULL)
    549 			continue;	/* just for safety */
    550 		if (ifa->ifa_addr->sa_family != AF_INET6)
    551 			continue;
    552 		if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
    553 			ours = 1;
    554 			deliverifp = ifa->ifa_ifp;
    555 			goto hbhcheck;
    556 		}
    557 	}
    558     }
    559 #endif
    560 
    561 	/*
    562 	 * Now there is no reason to process the packet if it's not our own
    563 	 * and we're not a router.
    564 	 */
    565 	if (!ip6_forwarding) {
    566 		ip6stat.ip6s_cantforward++;
    567 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
    568 		goto bad;
    569 	}
    570 
    571   hbhcheck:
    572 	/*
    573 	 * Process Hop-by-Hop options header if it's contained.
    574 	 * m may be modified in ip6_hopopts_input().
    575 	 * If a JumboPayload option is included, plen will also be modified.
    576 	 */
    577 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
    578 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
    579 		struct ip6_hbh *hbh;
    580 
    581 		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
    582 #if 0	/*touches NULL pointer*/
    583 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
    584 #endif
    585 			return;	/* m have already been freed */
    586 		}
    587 
    588 		/* adjust pointer */
    589 		ip6 = mtod(m, struct ip6_hdr *);
    590 
    591 		/*
    592 		 * if the payload length field is 0 and the next header field
    593 		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
    594 		 * option MUST be included.
    595 		 */
    596 		if (ip6->ip6_plen == 0 && plen == 0) {
    597 			/*
    598 			 * Note that if a valid jumbo payload option is
    599 			 * contained, ip6_hoptops_input() must set a valid
    600 			 * (non-zero) payload length to the variable plen.
    601 			 */
    602 			ip6stat.ip6s_badoptions++;
    603 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
    604 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
    605 			icmp6_error(m, ICMP6_PARAM_PROB,
    606 				    ICMP6_PARAMPROB_HEADER,
    607 				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
    608 			return;
    609 		}
    610 #ifndef PULLDOWN_TEST
    611 		/* ip6_hopopts_input() ensures that mbuf is contiguous */
    612 		hbh = (struct ip6_hbh *)(ip6 + 1);
    613 #else
    614 		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
    615 			sizeof(struct ip6_hbh));
    616 		if (hbh == NULL) {
    617 			ip6stat.ip6s_tooshort++;
    618 			return;
    619 		}
    620 #endif
    621 		nxt = hbh->ip6h_nxt;
    622 
    623 		/*
    624 		 * accept the packet if a router alert option is included
    625 		 * and we act as an IPv6 router.
    626 		 */
    627 		if (rtalert != ~0 && ip6_forwarding)
    628 			ours = 1;
    629 	} else
    630 		nxt = ip6->ip6_nxt;
    631 
    632 	/*
    633 	 * Check that the amount of data in the buffers
    634 	 * is as at least much as the IPv6 header would have us expect.
    635 	 * Trim mbufs if longer than we expect.
    636 	 * Drop packet if shorter than we expect.
    637 	 */
    638 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
    639 		ip6stat.ip6s_tooshort++;
    640 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
    641 		goto bad;
    642 	}
    643 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
    644 		if (m->m_len == m->m_pkthdr.len) {
    645 			m->m_len = sizeof(struct ip6_hdr) + plen;
    646 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
    647 		} else
    648 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
    649 	}
    650 
    651 	/*
    652 	 * Forward if desirable.
    653 	 */
    654 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    655 		/*
    656 		 * If we are acting as a multicast router, all
    657 		 * incoming multicast packets are passed to the
    658 		 * kernel-level multicast forwarding function.
    659 		 * The packet is returned (relatively) intact; if
    660 		 * ip6_mforward() returns a non-zero value, the packet
    661 		 * must be discarded, else it may be accepted below.
    662 		 */
    663 		if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
    664 			ip6stat.ip6s_cantforward++;
    665 			m_freem(m);
    666 			return;
    667 		}
    668 		if (!ours) {
    669 			m_freem(m);
    670 			return;
    671 		}
    672 	} else if (!ours) {
    673 		ip6_forward(m, 0);
    674 		return;
    675 	}
    676 
    677 	ip6 = mtod(m, struct ip6_hdr *);
    678 
    679 	/*
    680 	 * Malicious party may be able to use IPv4 mapped addr to confuse
    681 	 * tcp/udp stack and bypass security checks (act as if it was from
    682 	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
    683 	 *
    684 	 * For SIIT end node behavior, you may want to disable the check.
    685 	 * However, you will  become vulnerable to attacks using IPv4 mapped
    686 	 * source.
    687 	 */
    688 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    689 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    690 		ip6stat.ip6s_badscope++;
    691 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    692 		goto bad;
    693 	}
    694 
    695 	/*
    696 	 * Tell launch routine the next header
    697 	 */
    698 #ifdef IFA_STATS
    699 	if (deliverifp != NULL) {
    700 		struct in6_ifaddr *ia6;
    701 		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
    702 		if (ia6)
    703 			ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
    704 	}
    705 #endif
    706 	ip6stat.ip6s_delivered++;
    707 	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
    708 	nest = 0;
    709 	while (nxt != IPPROTO_DONE) {
    710 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
    711 			ip6stat.ip6s_toomanyhdr++;
    712 			goto bad;
    713 		}
    714 
    715 		/*
    716 		 * protection against faulty packet - there should be
    717 		 * more sanity checks in header chain processing.
    718 		 */
    719 		if (m->m_pkthdr.len < off) {
    720 			ip6stat.ip6s_tooshort++;
    721 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
    722 			goto bad;
    723 		}
    724 
    725 #ifdef IPSEC
    726 		/*
    727 		 * enforce IPsec policy checking if we are seeing last header.
    728 		 * note that we do not visit this with protocols with pcb layer
    729 		 * code - like udp/tcp/raw ip.
    730 		 */
    731 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
    732 		    ipsec6_in_reject(m, NULL)) {
    733 			ipsec6stat.in_polvio++;
    734 			goto bad;
    735 		}
    736 #endif
    737 
    738 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
    739 	}
    740 	return;
    741  bad:
    742 	m_freem(m);
    743 }
    744 
    745 /*
    746  * Hop-by-Hop options header processing. If a valid jumbo payload option is
    747  * included, the real payload length will be stored in plenp.
    748  */
    749 static int
    750 ip6_hopopts_input(plenp, rtalertp, mp, offp)
    751 	u_int32_t *plenp;
    752 	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
    753 	struct mbuf **mp;
    754 	int *offp;
    755 {
    756 	struct mbuf *m = *mp;
    757 	int off = *offp, hbhlen;
    758 	struct ip6_hbh *hbh;
    759 	u_int8_t *opt;
    760 
    761 	/* validation of the length of the header */
    762 #ifndef PULLDOWN_TEST
    763 	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
    764 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
    765 	hbhlen = (hbh->ip6h_len + 1) << 3;
    766 
    767 	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
    768 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
    769 #else
    770 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
    771 		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
    772 	if (hbh == NULL) {
    773 		ip6stat.ip6s_tooshort++;
    774 		return -1;
    775 	}
    776 	hbhlen = (hbh->ip6h_len + 1) << 3;
    777 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
    778 		hbhlen);
    779 	if (hbh == NULL) {
    780 		ip6stat.ip6s_tooshort++;
    781 		return -1;
    782 	}
    783 #endif
    784 	off += hbhlen;
    785 	hbhlen -= sizeof(struct ip6_hbh);
    786 	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
    787 
    788 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
    789 				hbhlen, rtalertp, plenp) < 0)
    790 		return(-1);
    791 
    792 	*offp = off;
    793 	*mp = m;
    794 	return(0);
    795 }
    796 
    797 /*
    798  * Search header for all Hop-by-hop options and process each option.
    799  * This function is separate from ip6_hopopts_input() in order to
    800  * handle a case where the sending node itself process its hop-by-hop
    801  * options header. In such a case, the function is called from ip6_output().
    802  */
    803 int
    804 ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
    805 	struct mbuf *m;
    806 	u_int8_t *opthead;
    807 	int hbhlen;
    808 	u_int32_t *rtalertp;
    809 	u_int32_t *plenp;
    810 {
    811 	struct ip6_hdr *ip6;
    812 	int optlen = 0;
    813 	u_int8_t *opt = opthead;
    814 	u_int16_t rtalert_val;
    815 	u_int32_t jumboplen;
    816 
    817 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
    818 		switch (*opt) {
    819 		case IP6OPT_PAD1:
    820 			optlen = 1;
    821 			break;
    822 		case IP6OPT_PADN:
    823 			if (hbhlen < IP6OPT_MINLEN) {
    824 				ip6stat.ip6s_toosmall++;
    825 				goto bad;
    826 			}
    827 			optlen = *(opt + 1) + 2;
    828 			break;
    829 		case IP6OPT_RTALERT:
    830 			/* XXX may need check for alignment */
    831 			if (hbhlen < IP6OPT_RTALERT_LEN) {
    832 				ip6stat.ip6s_toosmall++;
    833 				goto bad;
    834 			}
    835 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
    836 				 /* XXX: should we discard the packet? */
    837 				log(LOG_ERR, "length of router alert opt is inconsitent(%d)",
    838 				    *(opt + 1));
    839 			}
    840 			optlen = IP6OPT_RTALERT_LEN;
    841 			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
    842 			*rtalertp = ntohs(rtalert_val);
    843 			break;
    844 		case IP6OPT_JUMBO:
    845 			/* XXX may need check for alignment */
    846 			if (hbhlen < IP6OPT_JUMBO_LEN) {
    847 				ip6stat.ip6s_toosmall++;
    848 				goto bad;
    849 			}
    850 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
    851 				/* XXX: should we discard the packet? */
    852 				log(LOG_ERR, "length of jumbopayload opt "
    853 				    "is inconsistent(%d)\n",
    854 				    *(opt + 1));
    855 			}
    856 			optlen = IP6OPT_JUMBO_LEN;
    857 
    858 			/*
    859 			 * IPv6 packets that have non 0 payload length
    860 			 * must not contain a jumbo payload option.
    861 			 */
    862 			ip6 = mtod(m, struct ip6_hdr *);
    863 			if (ip6->ip6_plen) {
    864 				ip6stat.ip6s_badoptions++;
    865 				icmp6_error(m, ICMP6_PARAM_PROB,
    866 					    ICMP6_PARAMPROB_HEADER,
    867 					    sizeof(struct ip6_hdr) +
    868 					    sizeof(struct ip6_hbh) +
    869 					    opt - opthead);
    870 				return(-1);
    871 			}
    872 
    873 			/*
    874 			 * We may see jumbolen in unaligned location, so
    875 			 * we'd need to perform bcopy().
    876 			 */
    877 			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
    878 			jumboplen = (u_int32_t)htonl(jumboplen);
    879 
    880 #if 1
    881 			/*
    882 			 * if there are multiple jumbo payload options,
    883 			 * *plenp will be non-zero and the packet will be
    884 			 * rejected.
    885 			 * the behavior may need some debate in ipngwg -
    886 			 * multiple options does not make sense, however,
    887 			 * there's no explicit mention in specification.
    888 			 */
    889 			if (*plenp != 0) {
    890 				ip6stat.ip6s_badoptions++;
    891 				icmp6_error(m, ICMP6_PARAM_PROB,
    892 					    ICMP6_PARAMPROB_HEADER,
    893 					    sizeof(struct ip6_hdr) +
    894 					    sizeof(struct ip6_hbh) +
    895 					    opt + 2 - opthead);
    896 				return(-1);
    897 			}
    898 #endif
    899 
    900 			/*
    901 			 * jumbo payload length must be larger than 65535.
    902 			 */
    903 			if (jumboplen <= IPV6_MAXPACKET) {
    904 				ip6stat.ip6s_badoptions++;
    905 				icmp6_error(m, ICMP6_PARAM_PROB,
    906 					    ICMP6_PARAMPROB_HEADER,
    907 					    sizeof(struct ip6_hdr) +
    908 					    sizeof(struct ip6_hbh) +
    909 					    opt + 2 - opthead);
    910 				return(-1);
    911 			}
    912 			*plenp = jumboplen;
    913 
    914 			break;
    915 		default:		/* unknown option */
    916 			if (hbhlen < IP6OPT_MINLEN) {
    917 				ip6stat.ip6s_toosmall++;
    918 				goto bad;
    919 			}
    920 			if ((optlen = ip6_unknown_opt(opt, m,
    921 						      sizeof(struct ip6_hdr) +
    922 						      sizeof(struct ip6_hbh) +
    923 						      opt - opthead)) == -1)
    924 				return(-1);
    925 			optlen += 2;
    926 			break;
    927 		}
    928 	}
    929 
    930 	return(0);
    931 
    932   bad:
    933 	m_freem(m);
    934 	return(-1);
    935 }
    936 
    937 /*
    938  * Unknown option processing.
    939  * The third argument `off' is the offset from the IPv6 header to the option,
    940  * which is necessary if the IPv6 header the and option header and IPv6 header
    941  * is not continuous in order to return an ICMPv6 error.
    942  */
    943 int
    944 ip6_unknown_opt(optp, m, off)
    945 	u_int8_t *optp;
    946 	struct mbuf *m;
    947 	int off;
    948 {
    949 	struct ip6_hdr *ip6;
    950 
    951 	switch (IP6OPT_TYPE(*optp)) {
    952 	case IP6OPT_TYPE_SKIP: /* ignore the option */
    953 		return((int)*(optp + 1));
    954 	case IP6OPT_TYPE_DISCARD:	/* silently discard */
    955 		m_freem(m);
    956 		return(-1);
    957 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
    958 		ip6stat.ip6s_badoptions++;
    959 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
    960 		return(-1);
    961 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
    962 		ip6stat.ip6s_badoptions++;
    963 		ip6 = mtod(m, struct ip6_hdr *);
    964 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
    965 		    (m->m_flags & (M_BCAST|M_MCAST)))
    966 			m_freem(m);
    967 		else
    968 			icmp6_error(m, ICMP6_PARAM_PROB,
    969 				    ICMP6_PARAMPROB_OPTION, off);
    970 		return(-1);
    971 	}
    972 
    973 	m_freem(m);		/* XXX: NOTREACHED */
    974 	return(-1);
    975 }
    976 
    977 /*
    978  * Create the "control" list for this pcb.
    979  *
    980  * The routine will be called from upper layer handlers like tcp6_input().
    981  * Thus the routine assumes that the caller (tcp6_input) have already
    982  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
    983  * very first mbuf on the mbuf chain.
    984  * We may want to add some infinite loop prevention or sanity checks for safety.
    985  * (This applies only when you are using KAME mbuf chain restriction, i.e.
    986  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
    987  */
    988 void
    989 ip6_savecontrol(in6p, mp, ip6, m)
    990 	struct in6pcb *in6p;
    991 	struct mbuf **mp;
    992 	struct ip6_hdr *ip6;
    993 	struct mbuf *m;
    994 {
    995 	struct proc *p = curproc;	/* XXX */
    996 	int privileged;
    997 
    998 	privileged = 0;
    999 	if (p && !suser(p->p_ucred, &p->p_acflag))
   1000 		privileged++;
   1001 
   1002 #ifdef SO_TIMESTAMP
   1003 	if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
   1004 		struct timeval tv;
   1005 
   1006 		microtime(&tv);
   1007 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
   1008 			SCM_TIMESTAMP, SOL_SOCKET);
   1009 		if (*mp)
   1010 			mp = &(*mp)->m_next;
   1011 	}
   1012 #endif
   1013 	if (in6p->in6p_flags & IN6P_RECVDSTADDR) {
   1014 		*mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst,
   1015 			sizeof(struct in6_addr), IPV6_RECVDSTADDR,
   1016 			IPPROTO_IPV6);
   1017 		if (*mp)
   1018 			mp = &(*mp)->m_next;
   1019 	}
   1020 
   1021 #ifdef noyet
   1022 	/* options were tossed above */
   1023 	if (in6p->in6p_flags & IN6P_RECVOPTS)
   1024 		/* broken */
   1025 	/* ip6_srcroute doesn't do what we want here, need to fix */
   1026 	if (in6p->in6p_flags & IPV6P_RECVRETOPTS)
   1027 		/* broken */
   1028 #endif
   1029 
   1030 	/* RFC 2292 sec. 5 */
   1031 	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
   1032 		struct in6_pktinfo pi6;
   1033 		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
   1034 		if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
   1035 			pi6.ipi6_addr.s6_addr16[1] = 0;
   1036 		pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
   1037 					? m->m_pkthdr.rcvif->if_index
   1038 					: 0;
   1039 		*mp = sbcreatecontrol((caddr_t) &pi6,
   1040 			sizeof(struct in6_pktinfo), IPV6_PKTINFO,
   1041 			IPPROTO_IPV6);
   1042 		if (*mp)
   1043 			mp = &(*mp)->m_next;
   1044 	}
   1045 	if (in6p->in6p_flags & IN6P_HOPLIMIT) {
   1046 		int hlim = ip6->ip6_hlim & 0xff;
   1047 		*mp = sbcreatecontrol((caddr_t) &hlim,
   1048 			sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
   1049 		if (*mp)
   1050 			mp = &(*mp)->m_next;
   1051 	}
   1052 	/* IN6P_NEXTHOP - for outgoing packet only */
   1053 
   1054 	/*
   1055 	 * IPV6_HOPOPTS socket option. We require super-user privilege
   1056 	 * for the option, but it might be too strict, since there might
   1057 	 * be some hop-by-hop options which can be returned to normal user.
   1058 	 * See RFC 2292 section 6.
   1059 	 */
   1060 	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) {
   1061 		/*
   1062 		 * Check if a hop-by-hop options header is contatined in the
   1063 		 * received packet, and if so, store the options as ancillary
   1064 		 * data. Note that a hop-by-hop options header must be
   1065 		 * just after the IPv6 header, which fact is assured through
   1066 		 * the IPv6 input processing.
   1067 		 */
   1068 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   1069 		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
   1070 			struct ip6_hbh *hbh;
   1071 			int hbhlen;
   1072 
   1073 #ifndef PULLDOWN_TEST
   1074 			hbh = (struct ip6_hbh *)(ip6 + 1);
   1075 			hbhlen = (hbh->ip6h_len + 1) << 3;
   1076 #else
   1077 			IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
   1078 				sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
   1079 			if (hbh == NULL) {
   1080 				ip6stat.ip6s_tooshort++;
   1081 				return;
   1082 			}
   1083 			hbhlen = (hbh->ip6h_len + 1) << 3;
   1084 			IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
   1085 				sizeof(struct ip6_hdr), hbhlen);
   1086 			if (hbh == NULL) {
   1087 				ip6stat.ip6s_tooshort++;
   1088 				return;
   1089 			}
   1090 #endif
   1091 
   1092 			/*
   1093 			 * XXX: We copy whole the header even if a jumbo
   1094 			 * payload option is included, which option is to
   1095 			 * be removed before returning in the RFC 2292.
   1096 			 * But it's too painful operation...
   1097 			 */
   1098 			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
   1099 					      IPV6_HOPOPTS, IPPROTO_IPV6);
   1100 			if (*mp)
   1101 				mp = &(*mp)->m_next;
   1102 		}
   1103 	}
   1104 
   1105 	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
   1106 	if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
   1107 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   1108 		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);;
   1109 
   1110 		/*
   1111 		 * Search for destination options headers or routing
   1112 		 * header(s) through the header chain, and stores each
   1113 		 * header as ancillary data.
   1114 		 * Note that the order of the headers remains in
   1115 		 * the chain of ancillary data.
   1116 		 */
   1117 		while (1) {	/* is explicit loop prevention necessary? */
   1118 			struct ip6_ext *ip6e;
   1119 			int elen;
   1120 
   1121 #ifndef PULLDOWN_TEST
   1122 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
   1123 			if (nxt == IPPROTO_AH)
   1124 				elen = (ip6e->ip6e_len + 2) << 2;
   1125 			else
   1126 				elen = (ip6e->ip6e_len + 1) << 3;
   1127 #else
   1128 			IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off,
   1129 				sizeof(struct ip6_ext));
   1130 			if (ip6e == NULL) {
   1131 				ip6stat.ip6s_tooshort++;
   1132 				return;
   1133 			}
   1134 			if (nxt == IPPROTO_AH)
   1135 				elen = (ip6e->ip6e_len + 2) << 2;
   1136 			else
   1137 				elen = (ip6e->ip6e_len + 1) << 3;
   1138 			IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off, elen);
   1139 			if (ip6e == NULL) {
   1140 				ip6stat.ip6s_tooshort++;
   1141 				return;
   1142 			}
   1143 #endif
   1144 
   1145 			switch (nxt) {
   1146 		        case IPPROTO_DSTOPTS:
   1147 				if (!in6p->in6p_flags & IN6P_DSTOPTS)
   1148 					break;
   1149 
   1150 				/*
   1151 				 * We also require super-user privilege for
   1152 				 * the option.
   1153 				 * See the comments on IN6_HOPOPTS.
   1154 				 */
   1155 				if (!privileged)
   1156 					break;
   1157 
   1158 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
   1159 						      IPV6_DSTOPTS,
   1160 						      IPPROTO_IPV6);
   1161 				if (*mp)
   1162 					mp = &(*mp)->m_next;
   1163 				break;
   1164 
   1165 			case IPPROTO_ROUTING:
   1166 				if (!in6p->in6p_flags & IN6P_RTHDR)
   1167 					break;
   1168 
   1169 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
   1170 						      IPV6_RTHDR,
   1171 						      IPPROTO_IPV6);
   1172 				if (*mp)
   1173 					mp = &(*mp)->m_next;
   1174 				break;
   1175 
   1176 			case IPPROTO_UDP:
   1177 			case IPPROTO_TCP:
   1178 			case IPPROTO_ICMPV6:
   1179 			default:
   1180 				/*
   1181 				 * stop search if we encounter an upper
   1182 				 * layer protocol headers.
   1183 				 */
   1184 				goto loopend;
   1185 
   1186 			case IPPROTO_HOPOPTS:
   1187 			case IPPROTO_AH: /* is it possible? */
   1188 				break;
   1189 			}
   1190 
   1191 			/* proceed with the next header. */
   1192 			off += elen;
   1193 			nxt = ip6e->ip6e_nxt;
   1194 		}
   1195 	  loopend:
   1196 		;
   1197 	}
   1198 	if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
   1199 		/* to be done */
   1200 	}
   1201 	if ((in6p->in6p_flags & IN6P_DSTOPTS) && privileged) {
   1202 		/* to be done */
   1203 	}
   1204 	/* IN6P_RTHDR - to be done */
   1205 
   1206 }
   1207 
   1208 /*
   1209  * Get pointer to the previous header followed by the header
   1210  * currently processed.
   1211  * XXX: This function supposes that
   1212  *	M includes all headers,
   1213  *	the next header field and the header length field of each header
   1214  *	are valid, and
   1215  *	the sum of each header length equals to OFF.
   1216  * Because of these assumptions, this function must be called very
   1217  * carefully. Moreover, it will not be used in the near future when
   1218  * we develop `neater' mechanism to process extension headers.
   1219  */
   1220 char *
   1221 ip6_get_prevhdr(m, off)
   1222 	struct mbuf *m;
   1223 	int off;
   1224 {
   1225 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   1226 
   1227 	if (off == sizeof(struct ip6_hdr))
   1228 		return(&ip6->ip6_nxt);
   1229 	else {
   1230 		int len, nxt;
   1231 		struct ip6_ext *ip6e = NULL;
   1232 
   1233 		nxt = ip6->ip6_nxt;
   1234 		len = sizeof(struct ip6_hdr);
   1235 		while (len < off) {
   1236 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
   1237 
   1238 			switch (nxt) {
   1239 			case IPPROTO_FRAGMENT:
   1240 				len += sizeof(struct ip6_frag);
   1241 				break;
   1242 			case IPPROTO_AH:
   1243 				len += (ip6e->ip6e_len + 2) << 2;
   1244 				break;
   1245 			default:
   1246 				len += (ip6e->ip6e_len + 1) << 3;
   1247 				break;
   1248 			}
   1249 			nxt = ip6e->ip6e_nxt;
   1250 		}
   1251 		if (ip6e)
   1252 			return(&ip6e->ip6e_nxt);
   1253 		else
   1254 			return NULL;
   1255 	}
   1256 }
   1257 
   1258 /*
   1259  * get next header offset.  m will be retained.
   1260  */
   1261 int
   1262 ip6_nexthdr(m, off, proto, nxtp)
   1263 	struct mbuf *m;
   1264 	int off;
   1265 	int proto;
   1266 	int *nxtp;
   1267 {
   1268 	struct ip6_hdr ip6;
   1269 	struct ip6_ext ip6e;
   1270 	struct ip6_frag fh;
   1271 
   1272 	/* just in case */
   1273 	if (m == NULL)
   1274 		panic("ip6_nexthdr: m == NULL");
   1275 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
   1276 		return -1;
   1277 
   1278 	switch (proto) {
   1279 	case IPPROTO_IPV6:
   1280 		if (m->m_pkthdr.len < off + sizeof(ip6))
   1281 			return -1;
   1282 		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
   1283 		if (nxtp)
   1284 			*nxtp = ip6.ip6_nxt;
   1285 		off += sizeof(ip6);
   1286 		return off;
   1287 
   1288 	case IPPROTO_FRAGMENT:
   1289 		/*
   1290 		 * terminate parsing if it is not the first fragment,
   1291 		 * it does not make sense to parse through it.
   1292 		 */
   1293 		if (m->m_pkthdr.len < off + sizeof(fh))
   1294 			return -1;
   1295 		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
   1296 		if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
   1297 			return -1;
   1298 		if (nxtp)
   1299 			*nxtp = fh.ip6f_nxt;
   1300 		off += sizeof(struct ip6_frag);
   1301 		return off;
   1302 
   1303 	case IPPROTO_AH:
   1304 		if (m->m_pkthdr.len < off + sizeof(ip6e))
   1305 			return -1;
   1306 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
   1307 		if (nxtp)
   1308 			*nxtp = ip6e.ip6e_nxt;
   1309 		off += (ip6e.ip6e_len + 2) << 2;
   1310 		return off;
   1311 
   1312 	case IPPROTO_HOPOPTS:
   1313 	case IPPROTO_ROUTING:
   1314 	case IPPROTO_DSTOPTS:
   1315 		if (m->m_pkthdr.len < off + sizeof(ip6e))
   1316 			return -1;
   1317 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
   1318 		if (nxtp)
   1319 			*nxtp = ip6e.ip6e_nxt;
   1320 		off += (ip6e.ip6e_len + 1) << 3;
   1321 		return off;
   1322 
   1323 	case IPPROTO_NONE:
   1324 	case IPPROTO_ESP:
   1325 	case IPPROTO_IPCOMP:
   1326 		/* give up */
   1327 		return -1;
   1328 
   1329 	default:
   1330 		return -1;
   1331 	}
   1332 
   1333 	return -1;
   1334 }
   1335 
   1336 /*
   1337  * get offset for the last header in the chain.  m will be kept untainted.
   1338  */
   1339 int
   1340 ip6_lasthdr(m, off, proto, nxtp)
   1341 	struct mbuf *m;
   1342 	int off;
   1343 	int proto;
   1344 	int *nxtp;
   1345 {
   1346 	int newoff;
   1347 	int nxt;
   1348 
   1349 	if (!nxtp) {
   1350 		nxt = -1;
   1351 		nxtp = &nxt;
   1352 	}
   1353 	while (1) {
   1354 		newoff = ip6_nexthdr(m, off, proto, nxtp);
   1355 		if (newoff < 0)
   1356 			return off;
   1357 		else if (newoff < off)
   1358 			return -1;	/* invalid */
   1359 		else if (newoff == off)
   1360 			return newoff;
   1361 
   1362 		off = newoff;
   1363 		proto = *nxtp;
   1364 	}
   1365 }
   1366 
   1367 /*
   1368  * System control for IP6
   1369  */
   1370 
   1371 u_char	inet6ctlerrmap[PRC_NCMDS] = {
   1372 	0,		0,		0,		0,
   1373 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
   1374 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
   1375 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
   1376 	0,		0,		0,		0,
   1377 	ENOPROTOOPT
   1378 };
   1379 
   1380 #include <uvm/uvm_extern.h>
   1381 #include <sys/sysctl.h>
   1382 
   1383 int
   1384 ip6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
   1385 	int *name;
   1386 	u_int namelen;
   1387 	void *oldp;
   1388 	size_t *oldlenp;
   1389 	void *newp;
   1390 	size_t newlen;
   1391 {
   1392 	int old, error;
   1393 
   1394 	/* All sysctl names at this level are terminal. */
   1395 	if (namelen != 1)
   1396 		return ENOTDIR;
   1397 
   1398 	switch (name[0]) {
   1399 
   1400 	case IPV6CTL_FORWARDING:
   1401 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1402 				  &ip6_forwarding);
   1403 	case IPV6CTL_SENDREDIRECTS:
   1404 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1405 				&ip6_sendredirects);
   1406 	case IPV6CTL_DEFHLIM:
   1407 		return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_defhlim);
   1408 	case IPV6CTL_MAXFRAGPACKETS:
   1409 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1410 				&ip6_maxfragpackets);
   1411 	case IPV6CTL_ACCEPT_RTADV:
   1412 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1413 				&ip6_accept_rtadv);
   1414 	case IPV6CTL_KEEPFAITH:
   1415 		return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_keepfaith);
   1416 	case IPV6CTL_LOG_INTERVAL:
   1417 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1418 				&ip6_log_interval);
   1419 	case IPV6CTL_HDRNESTLIMIT:
   1420 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1421 				&ip6_hdrnestlimit);
   1422 	case IPV6CTL_DAD_COUNT:
   1423 		return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_dad_count);
   1424 	case IPV6CTL_AUTO_FLOWLABEL:
   1425 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1426 				&ip6_auto_flowlabel);
   1427 	case IPV6CTL_DEFMCASTHLIM:
   1428 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1429 				&ip6_defmcasthlim);
   1430 	case IPV6CTL_GIF_HLIM:
   1431 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1432 				&ip6_gif_hlim);
   1433 	case IPV6CTL_KAME_VERSION:
   1434 		return sysctl_rdstring(oldp, oldlenp, newp, __KAME_VERSION);
   1435 	case IPV6CTL_USE_DEPRECATED:
   1436 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1437 				&ip6_use_deprecated);
   1438 	case IPV6CTL_RR_PRUNE:
   1439 		return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_rr_prune);
   1440 #ifndef INET6_BINDV6ONLY
   1441 	case IPV6CTL_BINDV6ONLY:
   1442 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1443 				&ip6_bindv6only);
   1444 #endif
   1445 	case IPV6CTL_ANONPORTMIN:
   1446 		old = ip6_anonportmin;
   1447 		error = sysctl_int(oldp, oldlenp, newp, newlen,
   1448 		    &ip6_anonportmin);
   1449 		if (ip6_anonportmin >= ip6_anonportmax || ip6_anonportmin < 0 ||
   1450 		    ip6_anonportmin > 65535
   1451 #ifndef IPNOPRIVPORTS
   1452 		    || ip6_anonportmin < IPV6PORT_RESERVED
   1453 #endif
   1454 		    ) {
   1455 			ip6_anonportmin = old;
   1456 			return (EINVAL);
   1457 		}
   1458 		return (error);
   1459 	case IPV6CTL_ANONPORTMAX:
   1460 		old = ip6_anonportmax;
   1461 		error = sysctl_int(oldp, oldlenp, newp, newlen,
   1462 		    &ip6_anonportmax);
   1463 		if (ip6_anonportmin >= ip6_anonportmax || ip6_anonportmax < 0 ||
   1464 		    ip6_anonportmax > 65535
   1465 #ifndef IPNOPRIVPORTS
   1466 		    || ip6_anonportmax < IPV6PORT_RESERVED
   1467 #endif
   1468 		    ) {
   1469 			ip6_anonportmax = old;
   1470 			return (EINVAL);
   1471 		}
   1472 		return (error);
   1473 #ifndef IPNOPRIVPORTS
   1474 	case IPV6CTL_LOWPORTMIN:
   1475 		old = ip6_lowportmin;
   1476 		error = sysctl_int(oldp, oldlenp, newp, newlen,
   1477 		    &ip6_lowportmin);
   1478 		if (ip6_lowportmin >= ip6_lowportmax ||
   1479 		    ip6_lowportmin > IPV6PORT_RESERVEDMAX ||
   1480 		    ip6_lowportmin < IPV6PORT_RESERVEDMIN) {
   1481 			ip6_lowportmin = old;
   1482 			return (EINVAL);
   1483 		}
   1484 		return (error);
   1485 	case IPV6CTL_LOWPORTMAX:
   1486 		old = ip6_lowportmax;
   1487 		error = sysctl_int(oldp, oldlenp, newp, newlen,
   1488 		    &ip6_lowportmax);
   1489 		if (ip6_lowportmin >= ip6_lowportmax ||
   1490 		    ip6_lowportmax > IPV6PORT_RESERVEDMAX ||
   1491 		    ip6_lowportmax < IPV6PORT_RESERVEDMIN) {
   1492 			ip6_lowportmax = old;
   1493 			return (EINVAL);
   1494 		}
   1495 		return (error);
   1496 #endif
   1497 	default:
   1498 		return EOPNOTSUPP;
   1499 	}
   1500 	/* NOTREACHED */
   1501 }
   1502