Home | History | Annotate | Line # | Download | only in netinet6
ip6_input.c revision 1.39
      1 /*	$NetBSD: ip6_input.c,v 1.39 2001/03/21 19:12:56 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 	/*
    307 	 * let ipfilter look at packet on the wire,
    308 	 * not the decapsulated packet.
    309 	 */
    310 #ifdef IPSEC
    311 	if (!ipsec_gethist(m, NULL))
    312 #else
    313 	if (1)
    314 #endif
    315 	{
    316 		if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif,
    317 				   PFIL_IN) != 0)
    318 			return;
    319 		if (m == NULL)
    320 			return;
    321 		ip6 = mtod(m, struct ip6_hdr *);
    322 	}
    323 #endif /* PFIL_HOOKS */
    324 
    325 
    326 	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
    327 
    328 #ifdef ALTQ
    329 	/* XXX Temporary until ALTQ is changed to use a pfil hook */
    330 	if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
    331 		/* packet is dropped by traffic conditioner */
    332 		return;
    333 	}
    334 #endif
    335 
    336 	/*
    337 	 * Scope check
    338 	 */
    339 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
    340 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
    341 		ip6stat.ip6s_badscope++;
    342 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    343 		goto bad;
    344 	}
    345 	/*
    346 	 * The following check is not documented in the spec.  Malicious party
    347 	 * may be able to use IPv4 mapped addr to confuse tcp/udp stack and
    348 	 * bypass security checks (act as if it was from 127.0.0.1 by using
    349 	 * IPv6 src ::ffff:127.0.0.1).	Be cautious.
    350 	 *
    351 	 * This check chokes if we are in SIIT cloud.  As none of BSDs support
    352 	 * IPv4-less kernel compilation, we cannot support SIIT environment
    353 	 * at all.  So, it makes more sense for us to reject any malicious
    354 	 * packets for non-SIIT environment, than try to do a partical support
    355 	 * for SIIT environment.
    356 	 */
    357 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    358 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    359 		ip6stat.ip6s_badscope++;
    360 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    361 		goto bad;
    362 	}
    363 #if 0
    364 	/*
    365 	 * Reject packets with IPv4 compatible addresses (auto tunnel).
    366 	 *
    367 	 * The code forbids auto tunnel relay case in RFC1933 (the check is
    368 	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
    369 	 * is revised to forbid relaying case.
    370 	 */
    371 	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
    372 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
    373 		ip6stat.ip6s_badscope++;
    374 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    375 		goto bad;
    376 	}
    377 #endif
    378 
    379 	if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
    380 	    IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
    381 		if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
    382 			ours = 1;
    383 			deliverifp = m->m_pkthdr.rcvif;
    384 			goto hbhcheck;
    385 		} else {
    386 			ip6stat.ip6s_badscope++;
    387 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    388 			goto bad;
    389 		}
    390 	}
    391 
    392 	/* drop packets if interface ID portion is already filled */
    393 	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
    394 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
    395 		    ip6->ip6_src.s6_addr16[1]) {
    396 			ip6stat.ip6s_badscope++;
    397 			goto bad;
    398 		}
    399 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
    400 		    ip6->ip6_dst.s6_addr16[1]) {
    401 			ip6stat.ip6s_badscope++;
    402 			goto bad;
    403 		}
    404 	}
    405 
    406 #ifndef FAKE_LOOPBACK_IF
    407 	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0)
    408 #else
    409 	if (1)
    410 #endif
    411 	{
    412 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
    413 			ip6->ip6_src.s6_addr16[1]
    414 				= htons(m->m_pkthdr.rcvif->if_index);
    415 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
    416 			ip6->ip6_dst.s6_addr16[1]
    417 				= htons(m->m_pkthdr.rcvif->if_index);
    418 	}
    419 
    420 	/*
    421 	 * XXX we need this since we do not have "goto ours" hack route
    422 	 * for some of our ifaddrs on loopback interface.
    423 	 * we should correct it by changing in6_ifattach to install
    424 	 * "goto ours" hack route.
    425 	 */
    426 	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0) {
    427 		if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
    428 			ours = 1;
    429 			deliverifp = m->m_pkthdr.rcvif;
    430 			goto hbhcheck;
    431 		}
    432 	}
    433 
    434 	/*
    435 	 * Multicast check
    436 	 */
    437 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    438 	  	struct	in6_multi *in6m = 0;
    439 
    440 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
    441 		/*
    442 		 * See if we belong to the destination multicast group on the
    443 		 * arrival interface.
    444 		 */
    445 		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
    446 		if (in6m)
    447 			ours = 1;
    448 		else if (!ip6_mrouter) {
    449 			ip6stat.ip6s_notmember++;
    450 			ip6stat.ip6s_cantforward++;
    451 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
    452 			goto bad;
    453 		}
    454 		deliverifp = m->m_pkthdr.rcvif;
    455 		goto hbhcheck;
    456 	}
    457 
    458 	/*
    459 	 *  Unicast check
    460 	 */
    461 	if (ip6_forward_rt.ro_rt != NULL &&
    462 	    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
    463 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
    464 			       &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
    465 		ip6stat.ip6s_forward_cachehit++;
    466 	else {
    467 		struct sockaddr_in6 *dst6;
    468 
    469 		if (ip6_forward_rt.ro_rt) {
    470 			/* route is down or destination is different */
    471 			ip6stat.ip6s_forward_cachemiss++;
    472 			RTFREE(ip6_forward_rt.ro_rt);
    473 			ip6_forward_rt.ro_rt = 0;
    474 		}
    475 
    476 		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
    477 		dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
    478 		dst6->sin6_len = sizeof(struct sockaddr_in6);
    479 		dst6->sin6_family = AF_INET6;
    480 		dst6->sin6_addr = ip6->ip6_dst;
    481 
    482 		rtalloc((struct route *)&ip6_forward_rt);
    483 	}
    484 
    485 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
    486 
    487 	/*
    488 	 * Accept the packet if the forwarding interface to the destination
    489 	 * according to the routing table is the loopback interface,
    490 	 * unless the associated route has a gateway.
    491 	 * Note that this approach causes to accept a packet if there is a
    492 	 * route to the loopback interface for the destination of the packet.
    493 	 * But we think it's even useful in some situations, e.g. when using
    494 	 * a special daemon which wants to intercept the packet.
    495 	 */
    496 	if (ip6_forward_rt.ro_rt &&
    497 	    (ip6_forward_rt.ro_rt->rt_flags &
    498 	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
    499 #if 0
    500 	    /*
    501 	     * The check below is redundant since the comparison of
    502 	     * the destination and the key of the rtentry has
    503 	     * already done through looking up the routing table.
    504 	     */
    505 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
    506 			       &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) &&
    507 #endif
    508 	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
    509 		struct in6_ifaddr *ia6 =
    510 			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
    511 		if (ia6->ia6_flags & IN6_IFF_ANYCAST)
    512 			m->m_flags |= M_ANYCAST6;
    513 		/*
    514 		 * packets to a tentative, duplicated, or somehow invalid
    515 		 * address must not be accepted.
    516 		 */
    517 		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
    518 			/* this address is ready */
    519 			ours = 1;
    520 			deliverifp = ia6->ia_ifp;	/* correct? */
    521 			goto hbhcheck;
    522 		} else {
    523 			/* address is not ready, so discard the packet. */
    524 			nd6log((LOG_INFO,
    525 			    "ip6_input: packet to an unready address %s->%s\n",
    526 			    ip6_sprintf(&ip6->ip6_src),
    527 			    ip6_sprintf(&ip6->ip6_dst)));
    528 
    529 			goto bad;
    530 		}
    531 	}
    532 
    533 	/*
    534 	 * FAITH(Firewall Aided Internet Translator)
    535 	 */
    536 #if defined(NFAITH) && 0 < NFAITH
    537 	if (ip6_keepfaith) {
    538 		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
    539 		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
    540 			/* XXX do we need more sanity checks? */
    541 			ours = 1;
    542 			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /*faith*/
    543 			goto hbhcheck;
    544 		}
    545 	}
    546 #endif
    547 
    548 #if 0
    549     {
    550 	/*
    551 	 * Last resort: check in6_ifaddr for incoming interface.
    552 	 * The code is here until I update the "goto ours hack" code above
    553 	 * working right.
    554 	 */
    555 	struct ifaddr *ifa;
    556 	for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
    557 	     ifa;
    558 	     ifa = ifa->ifa_list.tqe_next) {
    559 		if (ifa->ifa_addr == NULL)
    560 			continue;	/* just for safety */
    561 		if (ifa->ifa_addr->sa_family != AF_INET6)
    562 			continue;
    563 		if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
    564 			ours = 1;
    565 			deliverifp = ifa->ifa_ifp;
    566 			goto hbhcheck;
    567 		}
    568 	}
    569     }
    570 #endif
    571 
    572 	/*
    573 	 * Now there is no reason to process the packet if it's not our own
    574 	 * and we're not a router.
    575 	 */
    576 	if (!ip6_forwarding) {
    577 		ip6stat.ip6s_cantforward++;
    578 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
    579 		goto bad;
    580 	}
    581 
    582   hbhcheck:
    583 	/*
    584 	 * Process Hop-by-Hop options header if it's contained.
    585 	 * m may be modified in ip6_hopopts_input().
    586 	 * If a JumboPayload option is included, plen will also be modified.
    587 	 */
    588 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
    589 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
    590 		struct ip6_hbh *hbh;
    591 
    592 		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
    593 #if 0	/*touches NULL pointer*/
    594 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
    595 #endif
    596 			return;	/* m have already been freed */
    597 		}
    598 
    599 		/* adjust pointer */
    600 		ip6 = mtod(m, struct ip6_hdr *);
    601 
    602 		/*
    603 		 * if the payload length field is 0 and the next header field
    604 		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
    605 		 * option MUST be included.
    606 		 */
    607 		if (ip6->ip6_plen == 0 && plen == 0) {
    608 			/*
    609 			 * Note that if a valid jumbo payload option is
    610 			 * contained, ip6_hoptops_input() must set a valid
    611 			 * (non-zero) payload length to the variable plen.
    612 			 */
    613 			ip6stat.ip6s_badoptions++;
    614 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
    615 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
    616 			icmp6_error(m, ICMP6_PARAM_PROB,
    617 				    ICMP6_PARAMPROB_HEADER,
    618 				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
    619 			return;
    620 		}
    621 #ifndef PULLDOWN_TEST
    622 		/* ip6_hopopts_input() ensures that mbuf is contiguous */
    623 		hbh = (struct ip6_hbh *)(ip6 + 1);
    624 #else
    625 		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
    626 			sizeof(struct ip6_hbh));
    627 		if (hbh == NULL) {
    628 			ip6stat.ip6s_tooshort++;
    629 			return;
    630 		}
    631 #endif
    632 		nxt = hbh->ip6h_nxt;
    633 
    634 		/*
    635 		 * accept the packet if a router alert option is included
    636 		 * and we act as an IPv6 router.
    637 		 */
    638 		if (rtalert != ~0 && ip6_forwarding)
    639 			ours = 1;
    640 	} else
    641 		nxt = ip6->ip6_nxt;
    642 
    643 	/*
    644 	 * Check that the amount of data in the buffers
    645 	 * is as at least much as the IPv6 header would have us expect.
    646 	 * Trim mbufs if longer than we expect.
    647 	 * Drop packet if shorter than we expect.
    648 	 */
    649 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
    650 		ip6stat.ip6s_tooshort++;
    651 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
    652 		goto bad;
    653 	}
    654 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
    655 		if (m->m_len == m->m_pkthdr.len) {
    656 			m->m_len = sizeof(struct ip6_hdr) + plen;
    657 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
    658 		} else
    659 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
    660 	}
    661 
    662 	/*
    663 	 * Forward if desirable.
    664 	 */
    665 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    666 		/*
    667 		 * If we are acting as a multicast router, all
    668 		 * incoming multicast packets are passed to the
    669 		 * kernel-level multicast forwarding function.
    670 		 * The packet is returned (relatively) intact; if
    671 		 * ip6_mforward() returns a non-zero value, the packet
    672 		 * must be discarded, else it may be accepted below.
    673 		 */
    674 		if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
    675 			ip6stat.ip6s_cantforward++;
    676 			m_freem(m);
    677 			return;
    678 		}
    679 		if (!ours) {
    680 			m_freem(m);
    681 			return;
    682 		}
    683 	} else if (!ours) {
    684 		ip6_forward(m, 0);
    685 		return;
    686 	}
    687 
    688 	ip6 = mtod(m, struct ip6_hdr *);
    689 
    690 	/*
    691 	 * Malicious party may be able to use IPv4 mapped addr to confuse
    692 	 * tcp/udp stack and bypass security checks (act as if it was from
    693 	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
    694 	 *
    695 	 * For SIIT end node behavior, you may want to disable the check.
    696 	 * However, you will  become vulnerable to attacks using IPv4 mapped
    697 	 * source.
    698 	 */
    699 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    700 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    701 		ip6stat.ip6s_badscope++;
    702 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
    703 		goto bad;
    704 	}
    705 
    706 	/*
    707 	 * Tell launch routine the next header
    708 	 */
    709 #ifdef IFA_STATS
    710 	if (deliverifp != NULL) {
    711 		struct in6_ifaddr *ia6;
    712 		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
    713 		if (ia6)
    714 			ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
    715 	}
    716 #endif
    717 	ip6stat.ip6s_delivered++;
    718 	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
    719 	nest = 0;
    720 	while (nxt != IPPROTO_DONE) {
    721 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
    722 			ip6stat.ip6s_toomanyhdr++;
    723 			goto bad;
    724 		}
    725 
    726 		/*
    727 		 * protection against faulty packet - there should be
    728 		 * more sanity checks in header chain processing.
    729 		 */
    730 		if (m->m_pkthdr.len < off) {
    731 			ip6stat.ip6s_tooshort++;
    732 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
    733 			goto bad;
    734 		}
    735 
    736 #ifdef IPSEC
    737 		/*
    738 		 * enforce IPsec policy checking if we are seeing last header.
    739 		 * note that we do not visit this with protocols with pcb layer
    740 		 * code - like udp/tcp/raw ip.
    741 		 */
    742 		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
    743 		    ipsec6_in_reject(m, NULL)) {
    744 			ipsec6stat.in_polvio++;
    745 			goto bad;
    746 		}
    747 #endif
    748 
    749 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
    750 	}
    751 	return;
    752  bad:
    753 	m_freem(m);
    754 }
    755 
    756 /*
    757  * Hop-by-Hop options header processing. If a valid jumbo payload option is
    758  * included, the real payload length will be stored in plenp.
    759  */
    760 static int
    761 ip6_hopopts_input(plenp, rtalertp, mp, offp)
    762 	u_int32_t *plenp;
    763 	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
    764 	struct mbuf **mp;
    765 	int *offp;
    766 {
    767 	struct mbuf *m = *mp;
    768 	int off = *offp, hbhlen;
    769 	struct ip6_hbh *hbh;
    770 	u_int8_t *opt;
    771 
    772 	/* validation of the length of the header */
    773 #ifndef PULLDOWN_TEST
    774 	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
    775 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
    776 	hbhlen = (hbh->ip6h_len + 1) << 3;
    777 
    778 	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
    779 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
    780 #else
    781 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
    782 		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
    783 	if (hbh == NULL) {
    784 		ip6stat.ip6s_tooshort++;
    785 		return -1;
    786 	}
    787 	hbhlen = (hbh->ip6h_len + 1) << 3;
    788 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
    789 		hbhlen);
    790 	if (hbh == NULL) {
    791 		ip6stat.ip6s_tooshort++;
    792 		return -1;
    793 	}
    794 #endif
    795 	off += hbhlen;
    796 	hbhlen -= sizeof(struct ip6_hbh);
    797 	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
    798 
    799 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
    800 				hbhlen, rtalertp, plenp) < 0)
    801 		return(-1);
    802 
    803 	*offp = off;
    804 	*mp = m;
    805 	return(0);
    806 }
    807 
    808 /*
    809  * Search header for all Hop-by-hop options and process each option.
    810  * This function is separate from ip6_hopopts_input() in order to
    811  * handle a case where the sending node itself process its hop-by-hop
    812  * options header. In such a case, the function is called from ip6_output().
    813  */
    814 int
    815 ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
    816 	struct mbuf *m;
    817 	u_int8_t *opthead;
    818 	int hbhlen;
    819 	u_int32_t *rtalertp;
    820 	u_int32_t *plenp;
    821 {
    822 	struct ip6_hdr *ip6;
    823 	int optlen = 0;
    824 	u_int8_t *opt = opthead;
    825 	u_int16_t rtalert_val;
    826 	u_int32_t jumboplen;
    827 
    828 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
    829 		switch (*opt) {
    830 		case IP6OPT_PAD1:
    831 			optlen = 1;
    832 			break;
    833 		case IP6OPT_PADN:
    834 			if (hbhlen < IP6OPT_MINLEN) {
    835 				ip6stat.ip6s_toosmall++;
    836 				goto bad;
    837 			}
    838 			optlen = *(opt + 1) + 2;
    839 			break;
    840 		case IP6OPT_RTALERT:
    841 			/* XXX may need check for alignment */
    842 			if (hbhlen < IP6OPT_RTALERT_LEN) {
    843 				ip6stat.ip6s_toosmall++;
    844 				goto bad;
    845 			}
    846 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
    847 				 /* XXX: should we discard the packet? */
    848 				log(LOG_ERR, "length of router alert opt is inconsitent(%d)",
    849 				    *(opt + 1));
    850 			}
    851 			optlen = IP6OPT_RTALERT_LEN;
    852 			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
    853 			*rtalertp = ntohs(rtalert_val);
    854 			break;
    855 		case IP6OPT_JUMBO:
    856 			/* XXX may need check for alignment */
    857 			if (hbhlen < IP6OPT_JUMBO_LEN) {
    858 				ip6stat.ip6s_toosmall++;
    859 				goto bad;
    860 			}
    861 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
    862 				/* XXX: should we discard the packet? */
    863 				log(LOG_ERR, "length of jumbopayload opt "
    864 				    "is inconsistent(%d)\n",
    865 				    *(opt + 1));
    866 			}
    867 			optlen = IP6OPT_JUMBO_LEN;
    868 
    869 			/*
    870 			 * IPv6 packets that have non 0 payload length
    871 			 * must not contain a jumbo payload option.
    872 			 */
    873 			ip6 = mtod(m, struct ip6_hdr *);
    874 			if (ip6->ip6_plen) {
    875 				ip6stat.ip6s_badoptions++;
    876 				icmp6_error(m, ICMP6_PARAM_PROB,
    877 					    ICMP6_PARAMPROB_HEADER,
    878 					    sizeof(struct ip6_hdr) +
    879 					    sizeof(struct ip6_hbh) +
    880 					    opt - opthead);
    881 				return(-1);
    882 			}
    883 
    884 			/*
    885 			 * We may see jumbolen in unaligned location, so
    886 			 * we'd need to perform bcopy().
    887 			 */
    888 			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
    889 			jumboplen = (u_int32_t)htonl(jumboplen);
    890 
    891 #if 1
    892 			/*
    893 			 * if there are multiple jumbo payload options,
    894 			 * *plenp will be non-zero and the packet will be
    895 			 * rejected.
    896 			 * the behavior may need some debate in ipngwg -
    897 			 * multiple options does not make sense, however,
    898 			 * there's no explicit mention in specification.
    899 			 */
    900 			if (*plenp != 0) {
    901 				ip6stat.ip6s_badoptions++;
    902 				icmp6_error(m, ICMP6_PARAM_PROB,
    903 					    ICMP6_PARAMPROB_HEADER,
    904 					    sizeof(struct ip6_hdr) +
    905 					    sizeof(struct ip6_hbh) +
    906 					    opt + 2 - opthead);
    907 				return(-1);
    908 			}
    909 #endif
    910 
    911 			/*
    912 			 * jumbo payload length must be larger than 65535.
    913 			 */
    914 			if (jumboplen <= IPV6_MAXPACKET) {
    915 				ip6stat.ip6s_badoptions++;
    916 				icmp6_error(m, ICMP6_PARAM_PROB,
    917 					    ICMP6_PARAMPROB_HEADER,
    918 					    sizeof(struct ip6_hdr) +
    919 					    sizeof(struct ip6_hbh) +
    920 					    opt + 2 - opthead);
    921 				return(-1);
    922 			}
    923 			*plenp = jumboplen;
    924 
    925 			break;
    926 		default:		/* unknown option */
    927 			if (hbhlen < IP6OPT_MINLEN) {
    928 				ip6stat.ip6s_toosmall++;
    929 				goto bad;
    930 			}
    931 			if ((optlen = ip6_unknown_opt(opt, m,
    932 						      sizeof(struct ip6_hdr) +
    933 						      sizeof(struct ip6_hbh) +
    934 						      opt - opthead)) == -1)
    935 				return(-1);
    936 			optlen += 2;
    937 			break;
    938 		}
    939 	}
    940 
    941 	return(0);
    942 
    943   bad:
    944 	m_freem(m);
    945 	return(-1);
    946 }
    947 
    948 /*
    949  * Unknown option processing.
    950  * The third argument `off' is the offset from the IPv6 header to the option,
    951  * which is necessary if the IPv6 header the and option header and IPv6 header
    952  * is not continuous in order to return an ICMPv6 error.
    953  */
    954 int
    955 ip6_unknown_opt(optp, m, off)
    956 	u_int8_t *optp;
    957 	struct mbuf *m;
    958 	int off;
    959 {
    960 	struct ip6_hdr *ip6;
    961 
    962 	switch (IP6OPT_TYPE(*optp)) {
    963 	case IP6OPT_TYPE_SKIP: /* ignore the option */
    964 		return((int)*(optp + 1));
    965 	case IP6OPT_TYPE_DISCARD:	/* silently discard */
    966 		m_freem(m);
    967 		return(-1);
    968 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
    969 		ip6stat.ip6s_badoptions++;
    970 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
    971 		return(-1);
    972 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
    973 		ip6stat.ip6s_badoptions++;
    974 		ip6 = mtod(m, struct ip6_hdr *);
    975 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
    976 		    (m->m_flags & (M_BCAST|M_MCAST)))
    977 			m_freem(m);
    978 		else
    979 			icmp6_error(m, ICMP6_PARAM_PROB,
    980 				    ICMP6_PARAMPROB_OPTION, off);
    981 		return(-1);
    982 	}
    983 
    984 	m_freem(m);		/* XXX: NOTREACHED */
    985 	return(-1);
    986 }
    987 
    988 /*
    989  * Create the "control" list for this pcb.
    990  *
    991  * The routine will be called from upper layer handlers like tcp6_input().
    992  * Thus the routine assumes that the caller (tcp6_input) have already
    993  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
    994  * very first mbuf on the mbuf chain.
    995  * We may want to add some infinite loop prevention or sanity checks for safety.
    996  * (This applies only when you are using KAME mbuf chain restriction, i.e.
    997  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
    998  */
    999 void
   1000 ip6_savecontrol(in6p, mp, ip6, m)
   1001 	struct in6pcb *in6p;
   1002 	struct mbuf **mp;
   1003 	struct ip6_hdr *ip6;
   1004 	struct mbuf *m;
   1005 {
   1006 	struct proc *p = curproc;	/* XXX */
   1007 	int privileged;
   1008 
   1009 	privileged = 0;
   1010 	if (p && !suser(p->p_ucred, &p->p_acflag))
   1011 		privileged++;
   1012 
   1013 #ifdef SO_TIMESTAMP
   1014 	if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
   1015 		struct timeval tv;
   1016 
   1017 		microtime(&tv);
   1018 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
   1019 			SCM_TIMESTAMP, SOL_SOCKET);
   1020 		if (*mp)
   1021 			mp = &(*mp)->m_next;
   1022 	}
   1023 #endif
   1024 	if (in6p->in6p_flags & IN6P_RECVDSTADDR) {
   1025 		*mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst,
   1026 			sizeof(struct in6_addr), IPV6_RECVDSTADDR,
   1027 			IPPROTO_IPV6);
   1028 		if (*mp)
   1029 			mp = &(*mp)->m_next;
   1030 	}
   1031 
   1032 #ifdef noyet
   1033 	/* options were tossed above */
   1034 	if (in6p->in6p_flags & IN6P_RECVOPTS)
   1035 		/* broken */
   1036 	/* ip6_srcroute doesn't do what we want here, need to fix */
   1037 	if (in6p->in6p_flags & IPV6P_RECVRETOPTS)
   1038 		/* broken */
   1039 #endif
   1040 
   1041 	/* RFC 2292 sec. 5 */
   1042 	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
   1043 		struct in6_pktinfo pi6;
   1044 		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
   1045 		if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
   1046 			pi6.ipi6_addr.s6_addr16[1] = 0;
   1047 		pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
   1048 					? m->m_pkthdr.rcvif->if_index
   1049 					: 0;
   1050 		*mp = sbcreatecontrol((caddr_t) &pi6,
   1051 			sizeof(struct in6_pktinfo), IPV6_PKTINFO,
   1052 			IPPROTO_IPV6);
   1053 		if (*mp)
   1054 			mp = &(*mp)->m_next;
   1055 	}
   1056 	if (in6p->in6p_flags & IN6P_HOPLIMIT) {
   1057 		int hlim = ip6->ip6_hlim & 0xff;
   1058 		*mp = sbcreatecontrol((caddr_t) &hlim,
   1059 			sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
   1060 		if (*mp)
   1061 			mp = &(*mp)->m_next;
   1062 	}
   1063 	/* IN6P_NEXTHOP - for outgoing packet only */
   1064 
   1065 	/*
   1066 	 * IPV6_HOPOPTS socket option. We require super-user privilege
   1067 	 * for the option, but it might be too strict, since there might
   1068 	 * be some hop-by-hop options which can be returned to normal user.
   1069 	 * See RFC 2292 section 6.
   1070 	 */
   1071 	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) {
   1072 		/*
   1073 		 * Check if a hop-by-hop options header is contatined in the
   1074 		 * received packet, and if so, store the options as ancillary
   1075 		 * data. Note that a hop-by-hop options header must be
   1076 		 * just after the IPv6 header, which fact is assured through
   1077 		 * the IPv6 input processing.
   1078 		 */
   1079 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   1080 		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
   1081 			struct ip6_hbh *hbh;
   1082 			int hbhlen;
   1083 
   1084 #ifndef PULLDOWN_TEST
   1085 			hbh = (struct ip6_hbh *)(ip6 + 1);
   1086 			hbhlen = (hbh->ip6h_len + 1) << 3;
   1087 #else
   1088 			IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
   1089 				sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
   1090 			if (hbh == NULL) {
   1091 				ip6stat.ip6s_tooshort++;
   1092 				return;
   1093 			}
   1094 			hbhlen = (hbh->ip6h_len + 1) << 3;
   1095 			IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
   1096 				sizeof(struct ip6_hdr), hbhlen);
   1097 			if (hbh == NULL) {
   1098 				ip6stat.ip6s_tooshort++;
   1099 				return;
   1100 			}
   1101 #endif
   1102 
   1103 			/*
   1104 			 * XXX: We copy whole the header even if a jumbo
   1105 			 * payload option is included, which option is to
   1106 			 * be removed before returning in the RFC 2292.
   1107 			 * But it's too painful operation...
   1108 			 */
   1109 			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
   1110 					      IPV6_HOPOPTS, IPPROTO_IPV6);
   1111 			if (*mp)
   1112 				mp = &(*mp)->m_next;
   1113 		}
   1114 	}
   1115 
   1116 	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
   1117 	if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
   1118 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   1119 		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);;
   1120 
   1121 		/*
   1122 		 * Search for destination options headers or routing
   1123 		 * header(s) through the header chain, and stores each
   1124 		 * header as ancillary data.
   1125 		 * Note that the order of the headers remains in
   1126 		 * the chain of ancillary data.
   1127 		 */
   1128 		while (1) {	/* is explicit loop prevention necessary? */
   1129 			struct ip6_ext *ip6e;
   1130 			int elen;
   1131 
   1132 #ifndef PULLDOWN_TEST
   1133 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
   1134 			if (nxt == IPPROTO_AH)
   1135 				elen = (ip6e->ip6e_len + 2) << 2;
   1136 			else
   1137 				elen = (ip6e->ip6e_len + 1) << 3;
   1138 #else
   1139 			IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off,
   1140 				sizeof(struct ip6_ext));
   1141 			if (ip6e == NULL) {
   1142 				ip6stat.ip6s_tooshort++;
   1143 				return;
   1144 			}
   1145 			if (nxt == IPPROTO_AH)
   1146 				elen = (ip6e->ip6e_len + 2) << 2;
   1147 			else
   1148 				elen = (ip6e->ip6e_len + 1) << 3;
   1149 			IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off, elen);
   1150 			if (ip6e == NULL) {
   1151 				ip6stat.ip6s_tooshort++;
   1152 				return;
   1153 			}
   1154 #endif
   1155 
   1156 			switch (nxt) {
   1157 		        case IPPROTO_DSTOPTS:
   1158 				if (!in6p->in6p_flags & IN6P_DSTOPTS)
   1159 					break;
   1160 
   1161 				/*
   1162 				 * We also require super-user privilege for
   1163 				 * the option.
   1164 				 * See the comments on IN6_HOPOPTS.
   1165 				 */
   1166 				if (!privileged)
   1167 					break;
   1168 
   1169 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
   1170 						      IPV6_DSTOPTS,
   1171 						      IPPROTO_IPV6);
   1172 				if (*mp)
   1173 					mp = &(*mp)->m_next;
   1174 				break;
   1175 
   1176 			case IPPROTO_ROUTING:
   1177 				if (!in6p->in6p_flags & IN6P_RTHDR)
   1178 					break;
   1179 
   1180 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
   1181 						      IPV6_RTHDR,
   1182 						      IPPROTO_IPV6);
   1183 				if (*mp)
   1184 					mp = &(*mp)->m_next;
   1185 				break;
   1186 
   1187 			case IPPROTO_UDP:
   1188 			case IPPROTO_TCP:
   1189 			case IPPROTO_ICMPV6:
   1190 			default:
   1191 				/*
   1192 				 * stop search if we encounter an upper
   1193 				 * layer protocol headers.
   1194 				 */
   1195 				goto loopend;
   1196 
   1197 			case IPPROTO_HOPOPTS:
   1198 			case IPPROTO_AH: /* is it possible? */
   1199 				break;
   1200 			}
   1201 
   1202 			/* proceed with the next header. */
   1203 			off += elen;
   1204 			nxt = ip6e->ip6e_nxt;
   1205 		}
   1206 	  loopend:
   1207 		;
   1208 	}
   1209 	if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
   1210 		/* to be done */
   1211 	}
   1212 	if ((in6p->in6p_flags & IN6P_DSTOPTS) && privileged) {
   1213 		/* to be done */
   1214 	}
   1215 	/* IN6P_RTHDR - to be done */
   1216 
   1217 }
   1218 
   1219 /*
   1220  * Get pointer to the previous header followed by the header
   1221  * currently processed.
   1222  * XXX: This function supposes that
   1223  *	M includes all headers,
   1224  *	the next header field and the header length field of each header
   1225  *	are valid, and
   1226  *	the sum of each header length equals to OFF.
   1227  * Because of these assumptions, this function must be called very
   1228  * carefully. Moreover, it will not be used in the near future when
   1229  * we develop `neater' mechanism to process extension headers.
   1230  */
   1231 char *
   1232 ip6_get_prevhdr(m, off)
   1233 	struct mbuf *m;
   1234 	int off;
   1235 {
   1236 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   1237 
   1238 	if (off == sizeof(struct ip6_hdr))
   1239 		return(&ip6->ip6_nxt);
   1240 	else {
   1241 		int len, nxt;
   1242 		struct ip6_ext *ip6e = NULL;
   1243 
   1244 		nxt = ip6->ip6_nxt;
   1245 		len = sizeof(struct ip6_hdr);
   1246 		while (len < off) {
   1247 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
   1248 
   1249 			switch (nxt) {
   1250 			case IPPROTO_FRAGMENT:
   1251 				len += sizeof(struct ip6_frag);
   1252 				break;
   1253 			case IPPROTO_AH:
   1254 				len += (ip6e->ip6e_len + 2) << 2;
   1255 				break;
   1256 			default:
   1257 				len += (ip6e->ip6e_len + 1) << 3;
   1258 				break;
   1259 			}
   1260 			nxt = ip6e->ip6e_nxt;
   1261 		}
   1262 		if (ip6e)
   1263 			return(&ip6e->ip6e_nxt);
   1264 		else
   1265 			return NULL;
   1266 	}
   1267 }
   1268 
   1269 /*
   1270  * get next header offset.  m will be retained.
   1271  */
   1272 int
   1273 ip6_nexthdr(m, off, proto, nxtp)
   1274 	struct mbuf *m;
   1275 	int off;
   1276 	int proto;
   1277 	int *nxtp;
   1278 {
   1279 	struct ip6_hdr ip6;
   1280 	struct ip6_ext ip6e;
   1281 	struct ip6_frag fh;
   1282 
   1283 	/* just in case */
   1284 	if (m == NULL)
   1285 		panic("ip6_nexthdr: m == NULL");
   1286 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
   1287 		return -1;
   1288 
   1289 	switch (proto) {
   1290 	case IPPROTO_IPV6:
   1291 		if (m->m_pkthdr.len < off + sizeof(ip6))
   1292 			return -1;
   1293 		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
   1294 		if (nxtp)
   1295 			*nxtp = ip6.ip6_nxt;
   1296 		off += sizeof(ip6);
   1297 		return off;
   1298 
   1299 	case IPPROTO_FRAGMENT:
   1300 		/*
   1301 		 * terminate parsing if it is not the first fragment,
   1302 		 * it does not make sense to parse through it.
   1303 		 */
   1304 		if (m->m_pkthdr.len < off + sizeof(fh))
   1305 			return -1;
   1306 		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
   1307 		if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
   1308 			return -1;
   1309 		if (nxtp)
   1310 			*nxtp = fh.ip6f_nxt;
   1311 		off += sizeof(struct ip6_frag);
   1312 		return off;
   1313 
   1314 	case IPPROTO_AH:
   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 + 2) << 2;
   1321 		return off;
   1322 
   1323 	case IPPROTO_HOPOPTS:
   1324 	case IPPROTO_ROUTING:
   1325 	case IPPROTO_DSTOPTS:
   1326 		if (m->m_pkthdr.len < off + sizeof(ip6e))
   1327 			return -1;
   1328 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
   1329 		if (nxtp)
   1330 			*nxtp = ip6e.ip6e_nxt;
   1331 		off += (ip6e.ip6e_len + 1) << 3;
   1332 		return off;
   1333 
   1334 	case IPPROTO_NONE:
   1335 	case IPPROTO_ESP:
   1336 	case IPPROTO_IPCOMP:
   1337 		/* give up */
   1338 		return -1;
   1339 
   1340 	default:
   1341 		return -1;
   1342 	}
   1343 
   1344 	return -1;
   1345 }
   1346 
   1347 /*
   1348  * get offset for the last header in the chain.  m will be kept untainted.
   1349  */
   1350 int
   1351 ip6_lasthdr(m, off, proto, nxtp)
   1352 	struct mbuf *m;
   1353 	int off;
   1354 	int proto;
   1355 	int *nxtp;
   1356 {
   1357 	int newoff;
   1358 	int nxt;
   1359 
   1360 	if (!nxtp) {
   1361 		nxt = -1;
   1362 		nxtp = &nxt;
   1363 	}
   1364 	while (1) {
   1365 		newoff = ip6_nexthdr(m, off, proto, nxtp);
   1366 		if (newoff < 0)
   1367 			return off;
   1368 		else if (newoff < off)
   1369 			return -1;	/* invalid */
   1370 		else if (newoff == off)
   1371 			return newoff;
   1372 
   1373 		off = newoff;
   1374 		proto = *nxtp;
   1375 	}
   1376 }
   1377 
   1378 /*
   1379  * System control for IP6
   1380  */
   1381 
   1382 u_char	inet6ctlerrmap[PRC_NCMDS] = {
   1383 	0,		0,		0,		0,
   1384 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
   1385 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
   1386 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
   1387 	0,		0,		0,		0,
   1388 	ENOPROTOOPT
   1389 };
   1390 
   1391 #include <uvm/uvm_extern.h>
   1392 #include <sys/sysctl.h>
   1393 
   1394 int
   1395 ip6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
   1396 	int *name;
   1397 	u_int namelen;
   1398 	void *oldp;
   1399 	size_t *oldlenp;
   1400 	void *newp;
   1401 	size_t newlen;
   1402 {
   1403 	int old, error;
   1404 
   1405 	/* All sysctl names at this level are terminal. */
   1406 	if (namelen != 1)
   1407 		return ENOTDIR;
   1408 
   1409 	switch (name[0]) {
   1410 
   1411 	case IPV6CTL_FORWARDING:
   1412 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1413 				  &ip6_forwarding);
   1414 	case IPV6CTL_SENDREDIRECTS:
   1415 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1416 				&ip6_sendredirects);
   1417 	case IPV6CTL_DEFHLIM:
   1418 		return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_defhlim);
   1419 	case IPV6CTL_MAXFRAGPACKETS:
   1420 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1421 				&ip6_maxfragpackets);
   1422 	case IPV6CTL_ACCEPT_RTADV:
   1423 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1424 				&ip6_accept_rtadv);
   1425 	case IPV6CTL_KEEPFAITH:
   1426 		return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_keepfaith);
   1427 	case IPV6CTL_LOG_INTERVAL:
   1428 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1429 				&ip6_log_interval);
   1430 	case IPV6CTL_HDRNESTLIMIT:
   1431 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1432 				&ip6_hdrnestlimit);
   1433 	case IPV6CTL_DAD_COUNT:
   1434 		return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_dad_count);
   1435 	case IPV6CTL_AUTO_FLOWLABEL:
   1436 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1437 				&ip6_auto_flowlabel);
   1438 	case IPV6CTL_DEFMCASTHLIM:
   1439 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1440 				&ip6_defmcasthlim);
   1441 	case IPV6CTL_GIF_HLIM:
   1442 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1443 				&ip6_gif_hlim);
   1444 	case IPV6CTL_KAME_VERSION:
   1445 		return sysctl_rdstring(oldp, oldlenp, newp, __KAME_VERSION);
   1446 	case IPV6CTL_USE_DEPRECATED:
   1447 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1448 				&ip6_use_deprecated);
   1449 	case IPV6CTL_RR_PRUNE:
   1450 		return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_rr_prune);
   1451 #ifndef INET6_BINDV6ONLY
   1452 	case IPV6CTL_BINDV6ONLY:
   1453 		return sysctl_int(oldp, oldlenp, newp, newlen,
   1454 				&ip6_bindv6only);
   1455 #endif
   1456 	case IPV6CTL_ANONPORTMIN:
   1457 		old = ip6_anonportmin;
   1458 		error = sysctl_int(oldp, oldlenp, newp, newlen,
   1459 		    &ip6_anonportmin);
   1460 		if (ip6_anonportmin >= ip6_anonportmax || ip6_anonportmin < 0 ||
   1461 		    ip6_anonportmin > 65535
   1462 #ifndef IPNOPRIVPORTS
   1463 		    || ip6_anonportmin < IPV6PORT_RESERVED
   1464 #endif
   1465 		    ) {
   1466 			ip6_anonportmin = old;
   1467 			return (EINVAL);
   1468 		}
   1469 		return (error);
   1470 	case IPV6CTL_ANONPORTMAX:
   1471 		old = ip6_anonportmax;
   1472 		error = sysctl_int(oldp, oldlenp, newp, newlen,
   1473 		    &ip6_anonportmax);
   1474 		if (ip6_anonportmin >= ip6_anonportmax || ip6_anonportmax < 0 ||
   1475 		    ip6_anonportmax > 65535
   1476 #ifndef IPNOPRIVPORTS
   1477 		    || ip6_anonportmax < IPV6PORT_RESERVED
   1478 #endif
   1479 		    ) {
   1480 			ip6_anonportmax = old;
   1481 			return (EINVAL);
   1482 		}
   1483 		return (error);
   1484 #ifndef IPNOPRIVPORTS
   1485 	case IPV6CTL_LOWPORTMIN:
   1486 		old = ip6_lowportmin;
   1487 		error = sysctl_int(oldp, oldlenp, newp, newlen,
   1488 		    &ip6_lowportmin);
   1489 		if (ip6_lowportmin >= ip6_lowportmax ||
   1490 		    ip6_lowportmin > IPV6PORT_RESERVEDMAX ||
   1491 		    ip6_lowportmin < IPV6PORT_RESERVEDMIN) {
   1492 			ip6_lowportmin = old;
   1493 			return (EINVAL);
   1494 		}
   1495 		return (error);
   1496 	case IPV6CTL_LOWPORTMAX:
   1497 		old = ip6_lowportmax;
   1498 		error = sysctl_int(oldp, oldlenp, newp, newlen,
   1499 		    &ip6_lowportmax);
   1500 		if (ip6_lowportmin >= ip6_lowportmax ||
   1501 		    ip6_lowportmax > IPV6PORT_RESERVEDMAX ||
   1502 		    ip6_lowportmax < IPV6PORT_RESERVEDMIN) {
   1503 			ip6_lowportmax = old;
   1504 			return (EINVAL);
   1505 		}
   1506 		return (error);
   1507 #endif
   1508 	default:
   1509 		return EOPNOTSUPP;
   1510 	}
   1511 	/* NOTREACHED */
   1512 }
   1513