ip6_input.c revision 97658
162587Sitojun/*	$FreeBSD: head/sys/netinet6/ip6_input.c 97658 2002-05-31 11:52:35Z tanimura $	*/
295023Ssuz/*	$KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $	*/
362587Sitojun
453541Sshin/*
553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
653541Sshin * All rights reserved.
753541Sshin *
853541Sshin * Redistribution and use in source and binary forms, with or without
953541Sshin * modification, are permitted provided that the following conditions
1053541Sshin * are met:
1153541Sshin * 1. Redistributions of source code must retain the above copyright
1253541Sshin *    notice, this list of conditions and the following disclaimer.
1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1453541Sshin *    notice, this list of conditions and the following disclaimer in the
1553541Sshin *    documentation and/or other materials provided with the distribution.
1653541Sshin * 3. Neither the name of the project nor the names of its contributors
1753541Sshin *    may be used to endorse or promote products derived from this software
1853541Sshin *    without specific prior written permission.
1953541Sshin *
2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2353541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3053541Sshin * SUCH DAMAGE.
3153541Sshin */
3253541Sshin
3353541Sshin/*
3453541Sshin * Copyright (c) 1982, 1986, 1988, 1993
3553541Sshin *	The Regents of the University of California.  All rights reserved.
3653541Sshin *
3753541Sshin * Redistribution and use in source and binary forms, with or without
3853541Sshin * modification, are permitted provided that the following conditions
3953541Sshin * are met:
4053541Sshin * 1. Redistributions of source code must retain the above copyright
4153541Sshin *    notice, this list of conditions and the following disclaimer.
4253541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4353541Sshin *    notice, this list of conditions and the following disclaimer in the
4453541Sshin *    documentation and/or other materials provided with the distribution.
4553541Sshin * 3. All advertising materials mentioning features or use of this software
4653541Sshin *    must display the following acknowledgement:
4753541Sshin *	This product includes software developed by the University of
4853541Sshin *	California, Berkeley and its contributors.
4953541Sshin * 4. Neither the name of the University nor the names of its contributors
5053541Sshin *    may be used to endorse or promote products derived from this software
5153541Sshin *    without specific prior written permission.
5253541Sshin *
5353541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5453541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5553541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5653541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5753541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5853541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5953541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6053541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6153541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6253541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6353541Sshin * SUCH DAMAGE.
6453541Sshin *
6553541Sshin *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
6653541Sshin */
6753541Sshin
6862587Sitojun#include "opt_ip6fw.h"
6962587Sitojun#include "opt_inet.h"
7062587Sitojun#include "opt_inet6.h"
7155009Sshin#include "opt_ipsec.h"
7264060Sdarrenr#include "opt_pfil_hooks.h"
7355009Sshin
7453541Sshin#include <sys/param.h>
7553541Sshin#include <sys/systm.h>
7678064Sume#include <sys/malloc.h>
7753541Sshin#include <sys/mbuf.h>
7883366Sjulian#include <sys/proc.h>
7953541Sshin#include <sys/domain.h>
8053541Sshin#include <sys/protosw.h>
8153541Sshin#include <sys/socket.h>
8253541Sshin#include <sys/socketvar.h>
8353541Sshin#include <sys/errno.h>
8453541Sshin#include <sys/time.h>
8553541Sshin#include <sys/kernel.h>
8653541Sshin#include <sys/syslog.h>
8753541Sshin
8853541Sshin#include <net/if.h>
8953541Sshin#include <net/if_types.h>
9053541Sshin#include <net/if_dl.h>
9153541Sshin#include <net/route.h>
9253541Sshin#include <net/netisr.h>
9356555Sbrian#include <net/intrq.h>
9464060Sdarrenr#ifdef PFIL_HOOKS
9564060Sdarrenr#include <net/pfil.h>
9664060Sdarrenr#endif
9753541Sshin
9853541Sshin#include <netinet/in.h>
9953541Sshin#include <netinet/in_systm.h>
10062587Sitojun#ifdef INET
10153541Sshin#include <netinet/ip.h>
10253541Sshin#include <netinet/ip_icmp.h>
10395023Ssuz#endif /* INET */
10462587Sitojun#include <netinet/ip6.h>
10553541Sshin#include <netinet6/in6_var.h>
10653541Sshin#include <netinet6/ip6_var.h>
10762587Sitojun#include <netinet/in_pcb.h>
10862587Sitojun#include <netinet/icmp6.h>
10953541Sshin#include <netinet6/in6_ifattach.h>
11053541Sshin#include <netinet6/nd6.h>
11153541Sshin#include <netinet6/in6_prefix.h>
11253541Sshin
11378064Sume#ifdef IPSEC
11478064Sume#include <netinet6/ipsec.h>
11578064Sume#ifdef INET6
11678064Sume#include <netinet6/ipsec6.h>
11778064Sume#endif
11878064Sume#endif
11978064Sume
12053541Sshin#include <netinet6/ip6_fw.h>
12153541Sshin
12253541Sshin#include <netinet6/ip6protosw.h>
12353541Sshin
12453541Sshin#include <net/net_osdep.h>
12553541Sshin
12662587Sitojunextern struct domain inet6domain;
12753541Sshin
12862587Sitojunu_char ip6_protox[IPPROTO_MAX];
12962587Sitojunstatic int ip6qmaxlen = IFQ_MAXLEN;
13062587Sitojunstruct in6_ifaddr *in6_ifaddr;
13153541Sshin
13278064Sumeextern struct callout in6_tmpaddrtimer_ch;
13378064Sume
13462587Sitojunint ip6_forward_srcrt;			/* XXX */
13562587Sitojunint ip6_sourcecheck;			/* XXX */
13662587Sitojunint ip6_sourcecheck_interval;		/* XXX */
13753541Sshin
13878064Sumeint ip6_ours_check_algorithm;
13978064Sume
14078064Sume
14153541Sshin/* firewall hooks */
14262587Sitojunip6_fw_chk_t *ip6_fw_chk_ptr;
14362587Sitojunip6_fw_ctl_t *ip6_fw_ctl_ptr;
14466303Sumeint ip6_fw_enable = 1;
14553541Sshin
14662587Sitojunstruct ip6stat ip6stat;
14753541Sshin
14862587Sitojunstatic void ip6_init2 __P((void *));
14978064Sumestatic struct mbuf *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *));
15062587Sitojunstatic int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
15162587Sitojun#ifdef PULLDOWN_TEST
15262587Sitojunstatic struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int));
15353541Sshin#endif
15453541Sshin
15578064Sume
15653541Sshin/*
15753541Sshin * IP6 initialization: fill in IP6 protocol switch table.
15853541Sshin * All protocols not implemented in kernel go to raw IP6 protocol handler.
15953541Sshin */
16053541Sshinvoid
16153541Sshinip6_init()
16253541Sshin{
16378064Sume	struct ip6protosw *pr;
16478064Sume	int i;
16553541Sshin	struct timeval tv;
16653541Sshin
16778064Sume#ifdef DIAGNOSTIC
16878064Sume	if (sizeof(struct protosw) != sizeof(struct ip6protosw))
16978064Sume		panic("sizeof(protosw) != sizeof(ip6protosw)");
17078064Sume#endif
17153541Sshin	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
17253541Sshin	if (pr == 0)
17353541Sshin		panic("ip6_init");
17453541Sshin	for (i = 0; i < IPPROTO_MAX; i++)
17553541Sshin		ip6_protox[i] = pr - inet6sw;
17653541Sshin	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
17753541Sshin	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
17853541Sshin		if (pr->pr_domain->dom_family == PF_INET6 &&
17953541Sshin		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
18053541Sshin			ip6_protox[pr->pr_protocol] = pr - inet6sw;
18153541Sshin	ip6intrq.ifq_maxlen = ip6qmaxlen;
18293818Sjhb	mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF);
18389069Smsmith	ip6intrq_present = 1;
18457178Speter	register_netisr(NETISR_IPV6, ip6intr);
18553541Sshin	nd6_init();
18653541Sshin	frag6_init();
18753541Sshin	/*
18853541Sshin	 * in many cases, random() here does NOT return random number
18953541Sshin	 * as initialization during bootstrap time occur in fixed order.
19053541Sshin	 */
19153541Sshin	microtime(&tv);
19253541Sshin	ip6_flow_seq = random() ^ tv.tv_usec;
19378064Sume	microtime(&tv);
19478064Sume	ip6_desync_factor = (random() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR;
19553541Sshin}
19653541Sshin
19753541Sshinstatic void
19853541Sshinip6_init2(dummy)
19953541Sshin	void *dummy;
20053541Sshin{
20153541Sshin
20253541Sshin	/*
20353541Sshin	 * to route local address of p2p link to loopback,
20453541Sshin	 * assign loopback address first.
20553541Sshin	 */
20662587Sitojun	in6_ifattach(&loif[0], NULL);
20753541Sshin
20853541Sshin	/* nd6_timer_init */
20978064Sume	callout_init(&nd6_timer_ch, 0);
21078064Sume	callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
21178064Sume
21253541Sshin	/* router renumbering prefix list maintenance */
21378064Sume	callout_init(&in6_rr_timer_ch, 0);
21478064Sume	callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL);
21578064Sume
21678064Sume	/* timer for regeneranation of temporary addresses randomize ID */
21778064Sume	callout_reset(&in6_tmpaddrtimer_ch,
21878064Sume		      (ip6_temp_preferred_lifetime - ip6_desync_factor -
21978064Sume		       ip6_temp_regen_advance) * hz,
22078064Sume		      in6_tmpaddrtimer, NULL);
22153541Sshin}
22253541Sshin
22353541Sshin/* cheat */
22455009Sshin/* This must be after route_init(), which is now SI_ORDER_THIRD */
22555009SshinSYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
22653541Sshin
22753541Sshin/*
22853541Sshin * IP6 input interrupt handling. Just pass the packet to ip6_input.
22953541Sshin */
23053541Sshinvoid
23153541Sshinip6intr()
23253541Sshin{
23353541Sshin	int s;
23453541Sshin	struct mbuf *m;
23553541Sshin
23653541Sshin	for (;;) {
23753541Sshin		s = splimp();
23853541Sshin		IF_DEQUEUE(&ip6intrq, m);
23953541Sshin		splx(s);
24053541Sshin		if (m == 0)
24153541Sshin			return;
24253541Sshin		ip6_input(m);
24353541Sshin	}
24453541Sshin}
24553541Sshin
24653541Sshinextern struct	route_in6 ip6_forward_rt;
24753541Sshin
24853541Sshinvoid
24953541Sshinip6_input(m)
25053541Sshin	struct mbuf *m;
25153541Sshin{
25253541Sshin	struct ip6_hdr *ip6;
25353541Sshin	int off = sizeof(struct ip6_hdr), nest;
25453541Sshin	u_int32_t plen;
25553541Sshin	u_int32_t rtalert = ~0;
25653541Sshin	int nxt, ours = 0;
25753541Sshin	struct ifnet *deliverifp = NULL;
25864060Sdarrenr#ifdef  PFIL_HOOKS
25964060Sdarrenr	struct packet_filter_hook *pfh;
26064060Sdarrenr	struct mbuf *m0;
26164060Sdarrenr	int rv;
26264060Sdarrenr#endif  /* PFIL_HOOKS */
26353541Sshin
26453541Sshin#ifdef IPSEC
26553541Sshin	/*
26653541Sshin	 * should the inner packet be considered authentic?
26753541Sshin	 * see comment in ah4_input().
26853541Sshin	 */
26953541Sshin	if (m) {
27053541Sshin		m->m_flags &= ~M_AUTHIPHDR;
27153541Sshin		m->m_flags &= ~M_AUTHIPDGM;
27253541Sshin	}
27353541Sshin#endif
27453541Sshin
27553541Sshin	/*
27678064Sume	 * make sure we don't have onion peering information into m_aux.
27778064Sume	 */
27878064Sume	ip6_delaux(m);
27978064Sume
28078064Sume	/*
28195023Ssuz	 * mbuf statistics
28253541Sshin	 */
28353541Sshin	if (m->m_flags & M_EXT) {
28453541Sshin		if (m->m_next)
28553541Sshin			ip6stat.ip6s_mext2m++;
28653541Sshin		else
28753541Sshin			ip6stat.ip6s_mext1++;
28853541Sshin	} else {
28978064Sume#define M2MMAX	(sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
29053541Sshin		if (m->m_next) {
29153541Sshin			if (m->m_flags & M_LOOP) {
29295023Ssuz				ip6stat.ip6s_m2m[loif[0].if_index]++;	/* XXX */
29378064Sume			} else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
29453541Sshin				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
29553541Sshin			else
29653541Sshin				ip6stat.ip6s_m2m[0]++;
29753541Sshin		} else
29853541Sshin			ip6stat.ip6s_m1++;
29978064Sume#undef M2MMAX
30053541Sshin	}
30153541Sshin
30253541Sshin	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
30353541Sshin	ip6stat.ip6s_total++;
30453541Sshin
30562587Sitojun#ifndef PULLDOWN_TEST
30674336Skuriyama	/*
30774336Skuriyama	 * L2 bridge code and some other code can return mbuf chain
30874336Skuriyama	 * that does not conform to KAME requirement.  too bad.
30974336Skuriyama	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
31074336Skuriyama	 */
31174336Skuriyama	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
31274336Skuriyama		struct mbuf *n;
31374336Skuriyama
31474336Skuriyama		MGETHDR(n, M_DONTWAIT, MT_HEADER);
31577003Sume		if (n)
31677003Sume			M_COPY_PKTHDR(n, m);
31774336Skuriyama		if (n && m->m_pkthdr.len > MHLEN) {
31874336Skuriyama			MCLGET(n, M_DONTWAIT);
31974336Skuriyama			if ((n->m_flags & M_EXT) == 0) {
32074336Skuriyama				m_freem(n);
32174336Skuriyama				n = NULL;
32274336Skuriyama			}
32374336Skuriyama		}
32495023Ssuz		if (n == NULL) {
32577003Sume			m_freem(m);
32674336Skuriyama			return;	/*ENOBUFS*/
32777003Sume		}
32874336Skuriyama
32974336Skuriyama		m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
33074336Skuriyama		n->m_len = m->m_pkthdr.len;
33174336Skuriyama		m_freem(m);
33274336Skuriyama		m = n;
33374336Skuriyama	}
33453541Sshin	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
33562587Sitojun#endif
33653541Sshin
33753541Sshin	if (m->m_len < sizeof(struct ip6_hdr)) {
33853541Sshin		struct ifnet *inifp;
33953541Sshin		inifp = m->m_pkthdr.rcvif;
34053541Sshin		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
34153541Sshin			ip6stat.ip6s_toosmall++;
34253541Sshin			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
34353541Sshin			return;
34453541Sshin		}
34553541Sshin	}
34653541Sshin
34753541Sshin	ip6 = mtod(m, struct ip6_hdr *);
34853541Sshin
34953541Sshin	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
35053541Sshin		ip6stat.ip6s_badvers++;
35153541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
35253541Sshin		goto bad;
35353541Sshin	}
35453541Sshin
35564060Sdarrenr#ifdef PFIL_HOOKS
35664060Sdarrenr	/*
35764060Sdarrenr	 * Run through list of hooks for input packets.  If there are any
35864060Sdarrenr	 * filters which require that additional packets in the flow are
35964060Sdarrenr	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
36064060Sdarrenr	 * Note that filters must _never_ set this flag, as another filter
36164060Sdarrenr	 * in the list may have previously cleared it.
36264060Sdarrenr	 */
36364060Sdarrenr	m0 = m;
36464558Sume	pfh = pfil_hook_get(PFIL_IN, &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
36564060Sdarrenr	for (; pfh; pfh = pfh->pfil_link.tqe_next)
36664060Sdarrenr		if (pfh->pfil_func) {
36764060Sdarrenr			rv = pfh->pfil_func(ip6, sizeof(*ip6),
36864060Sdarrenr					    m->m_pkthdr.rcvif, 0, &m0);
36964060Sdarrenr			if (rv)
37064060Sdarrenr				return;
37164060Sdarrenr			m = m0;
37264060Sdarrenr			if (m == NULL)
37364060Sdarrenr				return;
37464060Sdarrenr			ip6 = mtod(m, struct ip6_hdr *);
37564060Sdarrenr		}
37664060Sdarrenr#endif /* PFIL_HOOKS */
37764060Sdarrenr
37853541Sshin	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
37953541Sshin
38053541Sshin	/*
38153541Sshin	 * Check with the firewall...
38253541Sshin	 */
38366303Sume	if (ip6_fw_enable && ip6_fw_chk_ptr) {
38453541Sshin		u_short port = 0;
38553541Sshin		/* If ipfw says divert, we have to just drop packet */
38653541Sshin		/* use port as a dummy argument */
38753541Sshin		if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
38853541Sshin			m_freem(m);
38953541Sshin			m = NULL;
39053541Sshin		}
39153541Sshin		if (!m)
39253541Sshin			return;
39353541Sshin	}
39453541Sshin
39553541Sshin	/*
39678064Sume	 * Check against address spoofing/corruption.
39753541Sshin	 */
39853541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
39953541Sshin	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
40078064Sume		/*
40178064Sume		 * XXX: "badscope" is not very suitable for a multicast source.
40278064Sume		 */
40353541Sshin		ip6stat.ip6s_badscope++;
40453541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
40553541Sshin		goto bad;
40653541Sshin	}
40778064Sume	if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
40878064Sume	     IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) &&
40978064Sume	    (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
41078064Sume		ip6stat.ip6s_badscope++;
41178064Sume		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
41278064Sume		goto bad;
41378064Sume	}
41495023Ssuz
41562587Sitojun	/*
41678064Sume	 * The following check is not documented in specs.  A malicious
41778064Sume	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
41878064Sume	 * and bypass security checks (act as if it was from 127.0.0.1 by using
41978064Sume	 * IPv6 src ::ffff:127.0.0.1).	Be cautious.
42078064Sume	 *
42178064Sume	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
42278064Sume	 * support IPv4-less kernel compilation, we cannot support SIIT
42378064Sume	 * environment at all.  So, it makes more sense for us to reject any
42478064Sume	 * malicious packets for non-SIIT environment, than try to do a
42578064Sume	 * partical support for SIIT environment.
42662587Sitojun	 */
42778064Sume	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
42878064Sume	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
42978064Sume		ip6stat.ip6s_badscope++;
43078064Sume		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
43178064Sume		goto bad;
43278064Sume	}
43362587Sitojun#if 0
43462587Sitojun	/*
43562587Sitojun	 * Reject packets with IPv4 compatible addresses (auto tunnel).
43662587Sitojun	 *
43762587Sitojun	 * The code forbids auto tunnel relay case in RFC1933 (the check is
43862587Sitojun	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
43962587Sitojun	 * is revised to forbid relaying case.
44062587Sitojun	 */
44162587Sitojun	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
44262587Sitojun	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
44362587Sitojun		ip6stat.ip6s_badscope++;
44462587Sitojun		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
44562587Sitojun		goto bad;
44662587Sitojun	}
44762587Sitojun#endif
44875246Sume
44978064Sume	/* drop packets if interface ID portion is already filled */
45078064Sume	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
45178064Sume		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
45278064Sume		    ip6->ip6_src.s6_addr16[1]) {
45353541Sshin			ip6stat.ip6s_badscope++;
45453541Sshin			goto bad;
45553541Sshin		}
45678064Sume		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
45778064Sume		    ip6->ip6_dst.s6_addr16[1]) {
45878064Sume			ip6stat.ip6s_badscope++;
45978064Sume			goto bad;
46078064Sume		}
46153541Sshin	}
46253541Sshin
46378064Sume	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
46478064Sume		ip6->ip6_src.s6_addr16[1]
46578064Sume			= htons(m->m_pkthdr.rcvif->if_index);
46678064Sume	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
46778064Sume		ip6->ip6_dst.s6_addr16[1]
46878064Sume			= htons(m->m_pkthdr.rcvif->if_index);
46953541Sshin
47078064Sume#if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */
47153541Sshin	/*
47278064Sume	 * We use rt->rt_ifp to determine if the address is ours or not.
47378064Sume	 * If rt_ifp is lo0, the address is ours.
47478064Sume	 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0,
47578064Sume	 * so any address under fe80::%lo0/64 will be mistakenly considered
47678064Sume	 * local.  The special case is supplied to handle the case properly
47778064Sume	 * by actually looking at interface addresses
47878064Sume	 * (using in6ifa_ifpwithaddr).
47953541Sshin	 */
48078064Sume	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 &&
48178064Sume	    IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
48278064Sume		if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) {
48378064Sume			icmp6_error(m, ICMP6_DST_UNREACH,
48478064Sume			    ICMP6_DST_UNREACH_ADDR, 0);
48578064Sume			/* m is already freed */
48678064Sume			return;
48778064Sume		}
48875246Sume
48978064Sume		ours = 1;
49078064Sume		deliverifp = m->m_pkthdr.rcvif;
49178064Sume		goto hbhcheck;
49278064Sume	}
49375719Sume#endif
49453541Sshin
49553541Sshin	/*
49653541Sshin	 * Multicast check
49753541Sshin	 */
49853541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
49953541Sshin	  	struct	in6_multi *in6m = 0;
50053541Sshin
50153541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
50253541Sshin		/*
50353541Sshin		 * See if we belong to the destination multicast group on the
50453541Sshin		 * arrival interface.
50553541Sshin		 */
50653541Sshin		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
50753541Sshin		if (in6m)
50853541Sshin			ours = 1;
50956723Sshin		else if (!ip6_mrouter) {
51053541Sshin			ip6stat.ip6s_notmember++;
51153541Sshin			ip6stat.ip6s_cantforward++;
51253541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
51353541Sshin			goto bad;
51453541Sshin		}
51553541Sshin		deliverifp = m->m_pkthdr.rcvif;
51653541Sshin		goto hbhcheck;
51753541Sshin	}
51853541Sshin
51953541Sshin	/*
52053541Sshin	 *  Unicast check
52153541Sshin	 */
52278064Sume	switch (ip6_ours_check_algorithm) {
52378064Sume	default:
52478064Sume		/*
52578064Sume		 * XXX: I intentionally broke our indentation rule here,
52678064Sume		 *      since this switch-case is just for measurement and
52778064Sume		 *      therefore should soon be removed.
52878064Sume		 */
52962587Sitojun	if (ip6_forward_rt.ro_rt != NULL &&
53062587Sitojun	    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
53162587Sitojun	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
53278064Sume			       &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
53362587Sitojun		ip6stat.ip6s_forward_cachehit++;
53462587Sitojun	else {
53578064Sume		struct sockaddr_in6 *dst6;
53678064Sume
53753541Sshin		if (ip6_forward_rt.ro_rt) {
53862587Sitojun			/* route is down or destination is different */
53962587Sitojun			ip6stat.ip6s_forward_cachemiss++;
54053541Sshin			RTFREE(ip6_forward_rt.ro_rt);
54153541Sshin			ip6_forward_rt.ro_rt = 0;
54253541Sshin		}
54362587Sitojun
54453541Sshin		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
54578064Sume		dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
54678064Sume		dst6->sin6_len = sizeof(struct sockaddr_in6);
54778064Sume		dst6->sin6_family = AF_INET6;
54878064Sume		dst6->sin6_addr = ip6->ip6_dst;
54962587Sitojun#ifdef SCOPEDROUTING
55062587Sitojun		ip6_forward_rt.ro_dst.sin6_scope_id =
55162587Sitojun			in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_dst);
55262587Sitojun#endif
55353541Sshin
55453541Sshin		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
55553541Sshin	}
55653541Sshin
55753541Sshin#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
55853541Sshin
55953541Sshin	/*
56053541Sshin	 * Accept the packet if the forwarding interface to the destination
56153541Sshin	 * according to the routing table is the loopback interface,
56253541Sshin	 * unless the associated route has a gateway.
56353541Sshin	 * Note that this approach causes to accept a packet if there is a
56453541Sshin	 * route to the loopback interface for the destination of the packet.
56553541Sshin	 * But we think it's even useful in some situations, e.g. when using
56653541Sshin	 * a special daemon which wants to intercept the packet.
56778064Sume	 *
56878064Sume	 * XXX: some OSes automatically make a cloned route for the destination
56978064Sume	 * of an outgoing packet.  If the outgoing interface of the packet
57078064Sume	 * is a loopback one, the kernel would consider the packet to be
57178064Sume	 * accepted, even if we have no such address assinged on the interface.
57278064Sume	 * We check the cloned flag of the route entry to reject such cases,
57378064Sume	 * assuming that route entries for our own addresses are not made by
57478064Sume	 * cloning (it should be true because in6_addloop explicitly installs
57578064Sume	 * the host route).  However, we might have to do an explicit check
57678064Sume	 * while it would be less efficient.  Or, should we rather install a
57778064Sume	 * reject route for such a case?
57853541Sshin	 */
57953541Sshin	if (ip6_forward_rt.ro_rt &&
58053541Sshin	    (ip6_forward_rt.ro_rt->rt_flags &
58153541Sshin	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
58278064Sume#ifdef RTF_WASCLONED
58378064Sume	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
58478064Sume#endif
58578064Sume#ifdef RTF_CLONED
58678064Sume	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
58778064Sume#endif
58862587Sitojun#if 0
58953541Sshin	    /*
59062587Sitojun	     * The check below is redundant since the comparison of
59162587Sitojun	     * the destination and the key of the rtentry has
59262587Sitojun	     * already done through looking up the routing table.
59353541Sshin	     */
59462587Sitojun	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
59578064Sume				&rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
59662587Sitojun#endif
59753541Sshin	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
59853541Sshin		struct in6_ifaddr *ia6 =
59953541Sshin			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
60078064Sume
60162587Sitojun		/*
60278064Sume		 * record address information into m_aux.
60378064Sume		 */
60478064Sume		(void)ip6_setdstifaddr(m, ia6);
60578064Sume
60678064Sume		/*
60762587Sitojun		 * packets to a tentative, duplicated, or somehow invalid
60862587Sitojun		 * address must not be accepted.
60962587Sitojun		 */
61053541Sshin		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
61162587Sitojun			/* this address is ready */
61253541Sshin			ours = 1;
61353541Sshin			deliverifp = ia6->ia_ifp;	/* correct? */
61467334Sjoe			/* Count the packet in the ip address stats */
61567334Sjoe			ia6->ia_ifa.if_ipackets++;
61667334Sjoe			ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
61753541Sshin			goto hbhcheck;
61853541Sshin		} else {
61962587Sitojun			/* address is not ready, so discard the packet. */
62078064Sume			nd6log((LOG_INFO,
62178064Sume			    "ip6_input: packet to an unready address %s->%s\n",
62262587Sitojun			    ip6_sprintf(&ip6->ip6_src),
62378064Sume			    ip6_sprintf(&ip6->ip6_dst)));
62462587Sitojun
62562587Sitojun			goto bad;
62653541Sshin		}
62753541Sshin	}
62878064Sume	} /* XXX indentation (see above) */
62953541Sshin
63053541Sshin	/*
63153541Sshin	 * FAITH(Firewall Aided Internet Translator)
63253541Sshin	 */
63353541Sshin	if (ip6_keepfaith) {
63453541Sshin		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
63553541Sshin		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
63653541Sshin			/* XXX do we need more sanity checks? */
63753541Sshin			ours = 1;
63895023Ssuz			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
63953541Sshin			goto hbhcheck;
64053541Sshin		}
64153541Sshin	}
64253541Sshin
64353541Sshin	/*
64453541Sshin	 * Now there is no reason to process the packet if it's not our own
64553541Sshin	 * and we're not a router.
64653541Sshin	 */
64753541Sshin	if (!ip6_forwarding) {
64853541Sshin		ip6stat.ip6s_cantforward++;
64953541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
65053541Sshin		goto bad;
65153541Sshin	}
65253541Sshin
65353541Sshin  hbhcheck:
65453541Sshin	/*
65578064Sume	 * record address information into m_aux, if we don't have one yet.
65678064Sume	 * note that we are unable to record it, if the address is not listed
65778064Sume	 * as our interface address (e.g. multicast addresses, addresses
65878064Sume	 * within FAITH prefixes and such).
65978064Sume	 */
66078064Sume	if (deliverifp && !ip6_getdstifaddr(m)) {
66178064Sume		struct in6_ifaddr *ia6;
66278064Sume
66378064Sume		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
66478064Sume		if (ia6) {
66578064Sume			if (!ip6_setdstifaddr(m, ia6)) {
66678064Sume				/*
66778064Sume				 * XXX maybe we should drop the packet here,
66878064Sume				 * as we could not provide enough information
66978064Sume				 * to the upper layers.
67078064Sume				 */
67178064Sume			}
67278064Sume		}
67378064Sume	}
67478064Sume
67578064Sume	/*
67653541Sshin	 * Process Hop-by-Hop options header if it's contained.
67753541Sshin	 * m may be modified in ip6_hopopts_input().
67853541Sshin	 * If a JumboPayload option is included, plen will also be modified.
67953541Sshin	 */
68053541Sshin	plen = (u_int32_t)ntohs(ip6->ip6_plen);
68153541Sshin	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
68262587Sitojun		struct ip6_hbh *hbh;
68362587Sitojun
68453541Sshin		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
68562587Sitojun#if 0	/*touches NULL pointer*/
68653541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
68762587Sitojun#endif
68853541Sshin			return;	/* m have already been freed */
68953541Sshin		}
69062587Sitojun
69153541Sshin		/* adjust pointer */
69253541Sshin		ip6 = mtod(m, struct ip6_hdr *);
69353541Sshin
69453541Sshin		/*
69595023Ssuz		 * if the payload length field is 0 and the next header field
69662587Sitojun		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
69762587Sitojun		 * option MUST be included.
69862587Sitojun		 */
69962587Sitojun		if (ip6->ip6_plen == 0 && plen == 0) {
70062587Sitojun			/*
70162587Sitojun			 * Note that if a valid jumbo payload option is
70262587Sitojun			 * contained, ip6_hoptops_input() must set a valid
70362587Sitojun			 * (non-zero) payload length to the variable plen.
70462587Sitojun			 */
70562587Sitojun			ip6stat.ip6s_badoptions++;
70662587Sitojun			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
70762587Sitojun			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
70862587Sitojun			icmp6_error(m, ICMP6_PARAM_PROB,
70962587Sitojun				    ICMP6_PARAMPROB_HEADER,
71062587Sitojun				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
71162587Sitojun			return;
71262587Sitojun		}
71362587Sitojun#ifndef PULLDOWN_TEST
71462587Sitojun		/* ip6_hopopts_input() ensures that mbuf is contiguous */
71562587Sitojun		hbh = (struct ip6_hbh *)(ip6 + 1);
71662587Sitojun#else
71762587Sitojun		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
71862587Sitojun			sizeof(struct ip6_hbh));
71962587Sitojun		if (hbh == NULL) {
72062587Sitojun			ip6stat.ip6s_tooshort++;
72162587Sitojun			return;
72262587Sitojun		}
72362587Sitojun#endif
72462587Sitojun		nxt = hbh->ip6h_nxt;
72562587Sitojun
72662587Sitojun		/*
72753541Sshin		 * accept the packet if a router alert option is included
72853541Sshin		 * and we act as an IPv6 router.
72953541Sshin		 */
73053541Sshin		if (rtalert != ~0 && ip6_forwarding)
73153541Sshin			ours = 1;
73253541Sshin	} else
73353541Sshin		nxt = ip6->ip6_nxt;
73453541Sshin
73553541Sshin	/*
73653541Sshin	 * Check that the amount of data in the buffers
73753541Sshin	 * is as at least much as the IPv6 header would have us expect.
73853541Sshin	 * Trim mbufs if longer than we expect.
73953541Sshin	 * Drop packet if shorter than we expect.
74053541Sshin	 */
74153541Sshin	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
74253541Sshin		ip6stat.ip6s_tooshort++;
74353541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
74453541Sshin		goto bad;
74553541Sshin	}
74653541Sshin	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
74753541Sshin		if (m->m_len == m->m_pkthdr.len) {
74853541Sshin			m->m_len = sizeof(struct ip6_hdr) + plen;
74953541Sshin			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
75053541Sshin		} else
75153541Sshin			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
75253541Sshin	}
75353541Sshin
75453541Sshin	/*
75553541Sshin	 * Forward if desirable.
75653541Sshin	 */
75753541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
75856723Sshin		/*
75956723Sshin		 * If we are acting as a multicast router, all
76056723Sshin		 * incoming multicast packets are passed to the
76156723Sshin		 * kernel-level multicast forwarding function.
76256723Sshin		 * The packet is returned (relatively) intact; if
76356723Sshin		 * ip6_mforward() returns a non-zero value, the packet
76456723Sshin		 * must be discarded, else it may be accepted below.
76556723Sshin		 */
76656723Sshin		if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
76756723Sshin			ip6stat.ip6s_cantforward++;
76856723Sshin			m_freem(m);
76956723Sshin			return;
77056723Sshin		}
77153541Sshin		if (!ours) {
77253541Sshin			m_freem(m);
77353541Sshin			return;
77453541Sshin		}
77553541Sshin	} else if (!ours) {
77653541Sshin		ip6_forward(m, 0);
77753541Sshin		return;
77853541Sshin	}
77953541Sshin
78062587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
78162587Sitojun
78253541Sshin	/*
78362587Sitojun	 * Malicious party may be able to use IPv4 mapped addr to confuse
78462587Sitojun	 * tcp/udp stack and bypass security checks (act as if it was from
78562587Sitojun	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
78662587Sitojun	 *
78762587Sitojun	 * For SIIT end node behavior, you may want to disable the check.
78862587Sitojun	 * However, you will  become vulnerable to attacks using IPv4 mapped
78962587Sitojun	 * source.
79062587Sitojun	 */
79162587Sitojun	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
79262587Sitojun	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
79362587Sitojun		ip6stat.ip6s_badscope++;
79462587Sitojun		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
79562587Sitojun		goto bad;
79662587Sitojun	}
79762587Sitojun
79862587Sitojun	/*
79953541Sshin	 * Tell launch routine the next header
80053541Sshin	 */
80153541Sshin	ip6stat.ip6s_delivered++;
80253541Sshin	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
80353541Sshin	nest = 0;
80478064Sume
80553541Sshin	while (nxt != IPPROTO_DONE) {
80653541Sshin		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
80753541Sshin			ip6stat.ip6s_toomanyhdr++;
80853541Sshin			goto bad;
80953541Sshin		}
81053541Sshin
81153541Sshin		/*
81253541Sshin		 * protection against faulty packet - there should be
81353541Sshin		 * more sanity checks in header chain processing.
81453541Sshin		 */
81553541Sshin		if (m->m_pkthdr.len < off) {
81653541Sshin			ip6stat.ip6s_tooshort++;
81753541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
81853541Sshin			goto bad;
81953541Sshin		}
82053541Sshin
82178064Sume#if 0
82278064Sume		/*
82378064Sume		 * do we need to do it for every header?  yeah, other
82478064Sume		 * functions can play with it (like re-allocate and copy).
82578064Sume		 */
82678064Sume		mhist = ip6_addaux(m);
82778064Sume		if (mhist && M_TRAILINGSPACE(mhist) >= sizeof(nxt)) {
82878064Sume			hist = mtod(mhist, caddr_t) + mhist->m_len;
82978064Sume			bcopy(&nxt, hist, sizeof(nxt));
83078064Sume			mhist->m_len += sizeof(nxt);
83178064Sume		} else {
83278064Sume			ip6stat.ip6s_toomanyhdr++;
83378064Sume			goto bad;
83478064Sume		}
83578064Sume#endif
83678064Sume
83778064Sume#ifdef IPSEC
83878064Sume		/*
83978064Sume		 * enforce IPsec policy checking if we are seeing last header.
84078064Sume		 * note that we do not visit this with protocols with pcb layer
84178064Sume		 * code - like udp/tcp/raw ip.
84278064Sume		 */
84378064Sume		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
84478064Sume		    ipsec6_in_reject(m, NULL)) {
84578064Sume			ipsec6stat.in_polvio++;
84678064Sume			goto bad;
84778064Sume		}
84878064Sume#endif
84978064Sume
85053541Sshin		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
85153541Sshin	}
85253541Sshin	return;
85353541Sshin bad:
85453541Sshin	m_freem(m);
85553541Sshin}
85653541Sshin
85753541Sshin/*
85878064Sume * set/grab in6_ifaddr correspond to IPv6 destination address.
85978064Sume * XXX backward compatibility wrapper
86078064Sume */
86178064Sumestatic struct mbuf *
86278064Sumeip6_setdstifaddr(m, ia6)
86378064Sume	struct mbuf *m;
86478064Sume	struct in6_ifaddr *ia6;
86578064Sume{
86678064Sume	struct mbuf *n;
86778064Sume
86878064Sume	n = ip6_addaux(m);
86978064Sume	if (n)
87078064Sume		mtod(n, struct ip6aux *)->ip6a_dstia6 = ia6;
87178064Sume	return n;	/* NULL if failed to set */
87278064Sume}
87378064Sume
87478064Sumestruct in6_ifaddr *
87578064Sumeip6_getdstifaddr(m)
87678064Sume	struct mbuf *m;
87778064Sume{
87878064Sume	struct mbuf *n;
87978064Sume
88078064Sume	n = ip6_findaux(m);
88178064Sume	if (n)
88278064Sume		return mtod(n, struct ip6aux *)->ip6a_dstia6;
88378064Sume	else
88478064Sume		return NULL;
88578064Sume}
88678064Sume
88778064Sume/*
88853541Sshin * Hop-by-Hop options header processing. If a valid jumbo payload option is
88953541Sshin * included, the real payload length will be stored in plenp.
89053541Sshin */
89153541Sshinstatic int
89253541Sshinip6_hopopts_input(plenp, rtalertp, mp, offp)
89353541Sshin	u_int32_t *plenp;
89453541Sshin	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
89553541Sshin	struct mbuf **mp;
89653541Sshin	int *offp;
89753541Sshin{
89878064Sume	struct mbuf *m = *mp;
89953541Sshin	int off = *offp, hbhlen;
90053541Sshin	struct ip6_hbh *hbh;
90153541Sshin	u_int8_t *opt;
90253541Sshin
90353541Sshin	/* validation of the length of the header */
90462587Sitojun#ifndef PULLDOWN_TEST
90553541Sshin	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
90653541Sshin	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
90753541Sshin	hbhlen = (hbh->ip6h_len + 1) << 3;
90853541Sshin
90953541Sshin	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
91053541Sshin	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
91162587Sitojun#else
91262587Sitojun	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
91362587Sitojun		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
91462587Sitojun	if (hbh == NULL) {
91562587Sitojun		ip6stat.ip6s_tooshort++;
91662587Sitojun		return -1;
91762587Sitojun	}
91862587Sitojun	hbhlen = (hbh->ip6h_len + 1) << 3;
91962587Sitojun	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
92062587Sitojun		hbhlen);
92162587Sitojun	if (hbh == NULL) {
92262587Sitojun		ip6stat.ip6s_tooshort++;
92362587Sitojun		return -1;
92462587Sitojun	}
92562587Sitojun#endif
92653541Sshin	off += hbhlen;
92753541Sshin	hbhlen -= sizeof(struct ip6_hbh);
92853541Sshin	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
92953541Sshin
93053541Sshin	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
93153541Sshin				hbhlen, rtalertp, plenp) < 0)
93253541Sshin		return(-1);
93353541Sshin
93453541Sshin	*offp = off;
93553541Sshin	*mp = m;
93653541Sshin	return(0);
93753541Sshin}
93853541Sshin
93953541Sshin/*
94053541Sshin * Search header for all Hop-by-hop options and process each option.
94153541Sshin * This function is separate from ip6_hopopts_input() in order to
94253541Sshin * handle a case where the sending node itself process its hop-by-hop
94353541Sshin * options header. In such a case, the function is called from ip6_output().
94478064Sume *
94578064Sume * The function assumes that hbh header is located right after the IPv6 header
94678064Sume * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
94778064Sume * opthead + hbhlen is located in continuous memory region.
94853541Sshin */
94953541Sshinint
95053541Sshinip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
95153541Sshin	struct mbuf *m;
95253541Sshin	u_int8_t *opthead;
95353541Sshin	int hbhlen;
95453541Sshin	u_int32_t *rtalertp;
95553541Sshin	u_int32_t *plenp;
95653541Sshin{
95753541Sshin	struct ip6_hdr *ip6;
95853541Sshin	int optlen = 0;
95953541Sshin	u_int8_t *opt = opthead;
96053541Sshin	u_int16_t rtalert_val;
96162587Sitojun	u_int32_t jumboplen;
96278064Sume	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
96353541Sshin
96453541Sshin	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
96578064Sume		switch (*opt) {
96678064Sume		case IP6OPT_PAD1:
96778064Sume			optlen = 1;
96878064Sume			break;
96978064Sume		case IP6OPT_PADN:
97078064Sume			if (hbhlen < IP6OPT_MINLEN) {
97178064Sume				ip6stat.ip6s_toosmall++;
97278064Sume				goto bad;
97378064Sume			}
97478064Sume			optlen = *(opt + 1) + 2;
97578064Sume			break;
97678064Sume		case IP6OPT_RTALERT:
97762587Sitojun			/* XXX may need check for alignment */
97878064Sume			if (hbhlen < IP6OPT_RTALERT_LEN) {
97978064Sume				ip6stat.ip6s_toosmall++;
98078064Sume				goto bad;
98178064Sume			}
98278064Sume			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
98378064Sume				/* XXX stat */
98478064Sume				icmp6_error(m, ICMP6_PARAM_PROB,
98578064Sume					    ICMP6_PARAMPROB_HEADER,
98678064Sume					    erroff + opt + 1 - opthead);
98778064Sume				return(-1);
98878064Sume			}
98978064Sume			optlen = IP6OPT_RTALERT_LEN;
99078064Sume			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
99178064Sume			*rtalertp = ntohs(rtalert_val);
99278064Sume			break;
99378064Sume		case IP6OPT_JUMBO:
99478064Sume			/* XXX may need check for alignment */
99562587Sitojun			if (hbhlen < IP6OPT_JUMBO_LEN) {
99662587Sitojun				ip6stat.ip6s_toosmall++;
99762587Sitojun				goto bad;
99862587Sitojun			}
99978064Sume			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
100078064Sume				/* XXX stat */
100178064Sume				icmp6_error(m, ICMP6_PARAM_PROB,
100278064Sume					    ICMP6_PARAMPROB_HEADER,
100378064Sume					    erroff + opt + 1 - opthead);
100478064Sume				return(-1);
100578064Sume			}
100662587Sitojun			optlen = IP6OPT_JUMBO_LEN;
100753541Sshin
100862587Sitojun			/*
100962587Sitojun			 * IPv6 packets that have non 0 payload length
101078064Sume			 * must not contain a jumbo payload option.
101162587Sitojun			 */
101262587Sitojun			ip6 = mtod(m, struct ip6_hdr *);
101362587Sitojun			if (ip6->ip6_plen) {
101462587Sitojun				ip6stat.ip6s_badoptions++;
101562587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
101662587Sitojun					    ICMP6_PARAMPROB_HEADER,
101778064Sume					    erroff + opt - opthead);
101862587Sitojun				return(-1);
101962587Sitojun			}
102053541Sshin
102162587Sitojun			/*
102262587Sitojun			 * We may see jumbolen in unaligned location, so
102362587Sitojun			 * we'd need to perform bcopy().
102462587Sitojun			 */
102562587Sitojun			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
102662587Sitojun			jumboplen = (u_int32_t)htonl(jumboplen);
102762587Sitojun
102862587Sitojun#if 1
102962587Sitojun			/*
103062587Sitojun			 * if there are multiple jumbo payload options,
103162587Sitojun			 * *plenp will be non-zero and the packet will be
103262587Sitojun			 * rejected.
103362587Sitojun			 * the behavior may need some debate in ipngwg -
103462587Sitojun			 * multiple options does not make sense, however,
103562587Sitojun			 * there's no explicit mention in specification.
103662587Sitojun			 */
103762587Sitojun			if (*plenp != 0) {
103862587Sitojun				ip6stat.ip6s_badoptions++;
103962587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
104062587Sitojun					    ICMP6_PARAMPROB_HEADER,
104178064Sume					    erroff + opt + 2 - opthead);
104262587Sitojun				return(-1);
104362587Sitojun			}
104462587Sitojun#endif
104562587Sitojun
104662587Sitojun			/*
104762587Sitojun			 * jumbo payload length must be larger than 65535.
104862587Sitojun			 */
104962587Sitojun			if (jumboplen <= IPV6_MAXPACKET) {
105062587Sitojun				ip6stat.ip6s_badoptions++;
105162587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
105262587Sitojun					    ICMP6_PARAMPROB_HEADER,
105378064Sume					    erroff + opt + 2 - opthead);
105462587Sitojun				return(-1);
105562587Sitojun			}
105662587Sitojun			*plenp = jumboplen;
105762587Sitojun
105862587Sitojun			break;
105978064Sume		default:		/* unknown option */
106078064Sume			if (hbhlen < IP6OPT_MINLEN) {
106178064Sume				ip6stat.ip6s_toosmall++;
106278064Sume				goto bad;
106378064Sume			}
106478064Sume			optlen = ip6_unknown_opt(opt, m,
106578064Sume			    erroff + opt - opthead);
106678064Sume			if (optlen == -1)
106778064Sume				return(-1);
106878064Sume			optlen += 2;
106978064Sume			break;
107053541Sshin		}
107153541Sshin	}
107253541Sshin
107353541Sshin	return(0);
107453541Sshin
107553541Sshin  bad:
107653541Sshin	m_freem(m);
107753541Sshin	return(-1);
107853541Sshin}
107953541Sshin
108053541Sshin/*
108153541Sshin * Unknown option processing.
108253541Sshin * The third argument `off' is the offset from the IPv6 header to the option,
108353541Sshin * which is necessary if the IPv6 header the and option header and IPv6 header
108453541Sshin * is not continuous in order to return an ICMPv6 error.
108553541Sshin */
108653541Sshinint
108753541Sshinip6_unknown_opt(optp, m, off)
108853541Sshin	u_int8_t *optp;
108953541Sshin	struct mbuf *m;
109053541Sshin	int off;
109153541Sshin{
109253541Sshin	struct ip6_hdr *ip6;
109353541Sshin
109478064Sume	switch (IP6OPT_TYPE(*optp)) {
109578064Sume	case IP6OPT_TYPE_SKIP: /* ignore the option */
109678064Sume		return((int)*(optp + 1));
109778064Sume	case IP6OPT_TYPE_DISCARD:	/* silently discard */
109878064Sume		m_freem(m);
109978064Sume		return(-1);
110078064Sume	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
110178064Sume		ip6stat.ip6s_badoptions++;
110278064Sume		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
110378064Sume		return(-1);
110478064Sume	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
110578064Sume		ip6stat.ip6s_badoptions++;
110678064Sume		ip6 = mtod(m, struct ip6_hdr *);
110778064Sume		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
110878064Sume		    (m->m_flags & (M_BCAST|M_MCAST)))
110978064Sume			m_freem(m);
111078064Sume		else
111178064Sume			icmp6_error(m, ICMP6_PARAM_PROB,
111278064Sume				    ICMP6_PARAMPROB_OPTION, off);
111378064Sume		return(-1);
111453541Sshin	}
111553541Sshin
111653541Sshin	m_freem(m);		/* XXX: NOTREACHED */
111753541Sshin	return(-1);
111853541Sshin}
111953541Sshin
112053541Sshin/*
112162587Sitojun * Create the "control" list for this pcb.
112278064Sume * The function will not modify mbuf chain at all.
112362587Sitojun *
112478064Sume * with KAME mbuf chain restriction:
112562587Sitojun * The routine will be called from upper layer handlers like tcp6_input().
112662587Sitojun * Thus the routine assumes that the caller (tcp6_input) have already
112762587Sitojun * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
112862587Sitojun * very first mbuf on the mbuf chain.
112953541Sshin */
113053541Sshinvoid
113153541Sshinip6_savecontrol(in6p, mp, ip6, m)
113278064Sume	struct inpcb *in6p;
113378064Sume	struct mbuf **mp;
113478064Sume	struct ip6_hdr *ip6;
113578064Sume	struct mbuf *m;
113653541Sshin{
113793593Sjhb#if __FreeBSD__ >= 5
113893593Sjhb	struct thread *td = curthread;	/* XXX */
113993593Sjhb#else
114093593Sjhb	struct proc *td = curproc;	/* XXX */
114193593Sjhb#endif
114278064Sume	int privileged = 0;
114378064Sume	int rthdr_exist = 0;
114453541Sshin
114578064Sume
114693593Sjhb	if (td && !suser(td))
114778064Sume 		privileged++;
114853541Sshin
114978064Sume#ifdef SO_TIMESTAMP
115078064Sume	if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
115153541Sshin		struct timeval tv;
115253541Sshin
115353541Sshin		microtime(&tv);
115453541Sshin		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
115578064Sume				      SCM_TIMESTAMP, SOL_SOCKET);
115678064Sume		if (*mp) {
115753541Sshin			mp = &(*mp)->m_next;
115878064Sume		}
115997658Stanimura	}
116062587Sitojun#endif
116162587Sitojun
116253541Sshin	/* RFC 2292 sec. 5 */
116378064Sume	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
116453541Sshin		struct in6_pktinfo pi6;
116553541Sshin		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
116653541Sshin		if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
116753541Sshin			pi6.ipi6_addr.s6_addr16[1] = 0;
116853541Sshin		pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
116953541Sshin					? m->m_pkthdr.rcvif->if_index
117053541Sshin					: 0;
117153541Sshin		*mp = sbcreatecontrol((caddr_t) &pi6,
117253541Sshin			sizeof(struct in6_pktinfo), IPV6_PKTINFO,
117353541Sshin			IPPROTO_IPV6);
117453541Sshin		if (*mp)
117553541Sshin			mp = &(*mp)->m_next;
117653541Sshin	}
117778064Sume
117878064Sume	if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
117953541Sshin		int hlim = ip6->ip6_hlim & 0xff;
118053541Sshin		*mp = sbcreatecontrol((caddr_t) &hlim,
118153541Sshin			sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
118253541Sshin		if (*mp)
118353541Sshin			mp = &(*mp)->m_next;
118453541Sshin	}
118553541Sshin
118653541Sshin	/*
118753541Sshin	 * IPV6_HOPOPTS socket option. We require super-user privilege
118853541Sshin	 * for the option, but it might be too strict, since there might
118953541Sshin	 * be some hop-by-hop options which can be returned to normal user.
119053541Sshin	 * See RFC 2292 section 6.
119153541Sshin	 */
119278064Sume	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) {
119353541Sshin		/*
119453541Sshin		 * Check if a hop-by-hop options header is contatined in the
119553541Sshin		 * received packet, and if so, store the options as ancillary
119653541Sshin		 * data. Note that a hop-by-hop options header must be
119753541Sshin		 * just after the IPv6 header, which fact is assured through
119853541Sshin		 * the IPv6 input processing.
119953541Sshin		 */
120053541Sshin		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
120153541Sshin		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
120262587Sitojun			struct ip6_hbh *hbh;
120378064Sume			int hbhlen = 0;
120478064Sume#ifdef PULLDOWN_TEST
120578064Sume			struct mbuf *ext;
120678064Sume#endif
120753541Sshin
120862587Sitojun#ifndef PULLDOWN_TEST
120962587Sitojun			hbh = (struct ip6_hbh *)(ip6 + 1);
121062587Sitojun			hbhlen = (hbh->ip6h_len + 1) << 3;
121162587Sitojun#else
121278064Sume			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
121378064Sume			    ip6->ip6_nxt);
121478064Sume			if (ext == NULL) {
121562587Sitojun				ip6stat.ip6s_tooshort++;
121662587Sitojun				return;
121762587Sitojun			}
121878064Sume			hbh = mtod(ext, struct ip6_hbh *);
121962587Sitojun			hbhlen = (hbh->ip6h_len + 1) << 3;
122078064Sume			if (hbhlen != ext->m_len) {
122178064Sume				m_freem(ext);
122262587Sitojun				ip6stat.ip6s_tooshort++;
122362587Sitojun				return;
122462587Sitojun			}
122562587Sitojun#endif
122662587Sitojun
122753541Sshin			/*
122853541Sshin			 * XXX: We copy whole the header even if a jumbo
122953541Sshin			 * payload option is included, which option is to
123053541Sshin			 * be removed before returning in the RFC 2292.
123178064Sume			 * Note: this constraint is removed in 2292bis.
123253541Sshin			 */
123362587Sitojun			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
123453541Sshin					      IPV6_HOPOPTS, IPPROTO_IPV6);
123553541Sshin			if (*mp)
123653541Sshin				mp = &(*mp)->m_next;
123778064Sume#ifdef PULLDOWN_TEST
123878064Sume			m_freem(ext);
123978064Sume#endif
124053541Sshin		}
124153541Sshin	}
124253541Sshin
124353541Sshin	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
124478064Sume	if ((in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) {
124578064Sume		int proto, off, nxt;
124678064Sume
124778064Sume		/*
124878064Sume		 * go through the header chain to see if a routing header is
124978064Sume		 * contained in the packet. We need this information to store
125078064Sume		 * destination options headers (if any) properly.
125178064Sume		 * XXX: performance issue. We should record this info when
125278064Sume		 * processing extension headers in incoming routine.
125378064Sume		 * (todo) use m_aux?
125478064Sume		 */
125578064Sume		proto = IPPROTO_IPV6;
125678064Sume		off = 0;
125778064Sume		nxt = -1;
125878064Sume		while (1) {
125978064Sume			int newoff;
126078064Sume
126178064Sume			newoff = ip6_nexthdr(m, off, proto, &nxt);
126278064Sume			if (newoff < 0)
126378064Sume				break;
126478064Sume			if (newoff < off) /* invalid, check for safety */
126578064Sume				break;
126678064Sume			if ((proto = nxt) == IPPROTO_ROUTING) {
126778064Sume				rthdr_exist = 1;
126878064Sume				break;
126978064Sume			}
127078064Sume			off = newoff;
127178064Sume		}
127278064Sume	}
127378064Sume
127478064Sume	if ((in6p->in6p_flags &
127578064Sume	     (IN6P_RTHDR | IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) {
127653541Sshin		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
127778064Sume		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
127853541Sshin
127953541Sshin		/*
128053541Sshin		 * Search for destination options headers or routing
128153541Sshin		 * header(s) through the header chain, and stores each
128253541Sshin		 * header as ancillary data.
128353541Sshin		 * Note that the order of the headers remains in
128453541Sshin		 * the chain of ancillary data.
128553541Sshin		 */
128678064Sume		while (1) {	/* is explicit loop prevention necessary? */
128778064Sume			struct ip6_ext *ip6e = NULL;
128862587Sitojun			int elen;
128978064Sume#ifdef PULLDOWN_TEST
129078064Sume			struct mbuf *ext = NULL;
129178064Sume#endif
129253541Sshin
129378064Sume			/*
129478064Sume			 * if it is not an extension header, don't try to
129578064Sume			 * pull it from the chain.
129678064Sume			 */
129778064Sume			switch (nxt) {
129878064Sume			case IPPROTO_DSTOPTS:
129978064Sume			case IPPROTO_ROUTING:
130078064Sume			case IPPROTO_HOPOPTS:
130178064Sume			case IPPROTO_AH: /* is it possible? */
130278064Sume				break;
130378064Sume			default:
130478064Sume				goto loopend;
130578064Sume			}
130678064Sume
130762587Sitojun#ifndef PULLDOWN_TEST
130878064Sume			if (off + sizeof(*ip6e) > m->m_len)
130978064Sume				goto loopend;
131062587Sitojun			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
131162587Sitojun			if (nxt == IPPROTO_AH)
131262587Sitojun				elen = (ip6e->ip6e_len + 2) << 2;
131362587Sitojun			else
131462587Sitojun				elen = (ip6e->ip6e_len + 1) << 3;
131578064Sume			if (off + elen > m->m_len)
131678064Sume				goto loopend;
131762587Sitojun#else
131878064Sume			ext = ip6_pullexthdr(m, off, nxt);
131978064Sume			if (ext == NULL) {
132062587Sitojun				ip6stat.ip6s_tooshort++;
132162587Sitojun				return;
132262587Sitojun			}
132378064Sume			ip6e = mtod(ext, struct ip6_ext *);
132462587Sitojun			if (nxt == IPPROTO_AH)
132562587Sitojun				elen = (ip6e->ip6e_len + 2) << 2;
132662587Sitojun			else
132762587Sitojun				elen = (ip6e->ip6e_len + 1) << 3;
132878064Sume			if (elen != ext->m_len) {
132978064Sume				m_freem(ext);
133062587Sitojun				ip6stat.ip6s_tooshort++;
133162587Sitojun				return;
133262587Sitojun			}
133362587Sitojun#endif
133462587Sitojun
133578064Sume			switch (nxt) {
133678064Sume			case IPPROTO_DSTOPTS:
133778064Sume				if ((in6p->in6p_flags & IN6P_DSTOPTS) == 0)
133878064Sume					break;
133953541Sshin
134078064Sume				/*
134178064Sume				 * We also require super-user privilege for
134278064Sume				 * the option.
134378064Sume				 * See the comments on IN6_HOPOPTS.
134478064Sume				 */
134578064Sume				if (!privileged)
134678064Sume					break;
134753541Sshin
134878064Sume				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
134978064Sume						      IPV6_DSTOPTS,
135078064Sume						      IPPROTO_IPV6);
135178064Sume				if (*mp)
135278064Sume					mp = &(*mp)->m_next;
135378064Sume				break;
135478064Sume			case IPPROTO_ROUTING:
135578064Sume				if (!in6p->in6p_flags & IN6P_RTHDR)
135678064Sume					break;
135753541Sshin
135878064Sume				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
135978064Sume						      IPV6_RTHDR,
136078064Sume						      IPPROTO_IPV6);
136178064Sume				if (*mp)
136278064Sume					mp = &(*mp)->m_next;
136378064Sume				break;
136478064Sume			case IPPROTO_HOPOPTS:
136578064Sume			case IPPROTO_AH: /* is it possible? */
136678064Sume				break;
136753541Sshin
136878064Sume			default:
136978064Sume				/*
137078064Sume			 	 * other cases have been filtered in the above.
137178064Sume				 * none will visit this case.  here we supply
137278064Sume				 * the code just in case (nxt overwritten or
137378064Sume				 * other cases).
137478064Sume				 */
137578064Sume#ifdef PULLDOWN_TEST
137678064Sume				m_freem(ext);
137778064Sume#endif
137878064Sume				goto loopend;
137953541Sshin
138053541Sshin			}
138153541Sshin
138253541Sshin			/* proceed with the next header. */
138362587Sitojun			off += elen;
138453541Sshin			nxt = ip6e->ip6e_nxt;
138578064Sume			ip6e = NULL;
138678064Sume#ifdef PULLDOWN_TEST
138778064Sume			m_freem(ext);
138878064Sume			ext = NULL;
138978064Sume#endif
139053541Sshin		}
139153541Sshin	  loopend:
139278064Sume		;
139353541Sshin	}
139478064Sume
139578064Sume}
139678064Sume
139778064Sume#ifdef PULLDOWN_TEST
139878064Sume/*
139978064Sume * pull single extension header from mbuf chain.  returns single mbuf that
140078064Sume * contains the result, or NULL on error.
140178064Sume */
140278064Sumestatic struct mbuf *
140378064Sumeip6_pullexthdr(m, off, nxt)
140478064Sume	struct mbuf *m;
140578064Sume	size_t off;
140678064Sume	int nxt;
140778064Sume{
140878064Sume	struct ip6_ext ip6e;
140978064Sume	size_t elen;
141078064Sume	struct mbuf *n;
141178064Sume
141278064Sume#ifdef DIAGNOSTIC
141378064Sume	switch (nxt) {
141478064Sume	case IPPROTO_DSTOPTS:
141578064Sume	case IPPROTO_ROUTING:
141678064Sume	case IPPROTO_HOPOPTS:
141778064Sume	case IPPROTO_AH: /* is it possible? */
141878064Sume		break;
141978064Sume	default:
142078064Sume		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
142153541Sshin	}
142278064Sume#endif
142378064Sume
142478064Sume	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
142578064Sume	if (nxt == IPPROTO_AH)
142678064Sume		elen = (ip6e.ip6e_len + 2) << 2;
142778064Sume	else
142878064Sume		elen = (ip6e.ip6e_len + 1) << 3;
142978064Sume
143078064Sume	MGET(n, M_DONTWAIT, MT_DATA);
143178064Sume	if (n && elen >= MLEN) {
143278064Sume		MCLGET(n, M_DONTWAIT);
143378064Sume		if ((n->m_flags & M_EXT) == 0) {
143478064Sume			m_free(n);
143578064Sume			n = NULL;
143678064Sume		}
143753541Sshin	}
143878064Sume	if (!n)
143978064Sume		return NULL;
144062587Sitojun
144178064Sume	n->m_len = 0;
144278064Sume	if (elen >= M_TRAILINGSPACE(n)) {
144378064Sume		m_free(n);
144478064Sume		return NULL;
144578064Sume	}
144678064Sume
144778064Sume	m_copydata(m, off, elen, mtod(n, caddr_t));
144878064Sume	n->m_len = elen;
144978064Sume	return n;
145053541Sshin}
145178064Sume#endif
145253541Sshin
145353541Sshin/*
145453541Sshin * Get pointer to the previous header followed by the header
145553541Sshin * currently processed.
145653541Sshin * XXX: This function supposes that
145753541Sshin *	M includes all headers,
145853541Sshin *	the next header field and the header length field of each header
145953541Sshin *	are valid, and
146053541Sshin *	the sum of each header length equals to OFF.
146153541Sshin * Because of these assumptions, this function must be called very
146253541Sshin * carefully. Moreover, it will not be used in the near future when
146353541Sshin * we develop `neater' mechanism to process extension headers.
146453541Sshin */
146553541Sshinchar *
146653541Sshinip6_get_prevhdr(m, off)
146753541Sshin	struct mbuf *m;
146853541Sshin	int off;
146953541Sshin{
147053541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
147153541Sshin
147253541Sshin	if (off == sizeof(struct ip6_hdr))
147353541Sshin		return(&ip6->ip6_nxt);
147453541Sshin	else {
147553541Sshin		int len, nxt;
147653541Sshin		struct ip6_ext *ip6e = NULL;
147753541Sshin
147853541Sshin		nxt = ip6->ip6_nxt;
147953541Sshin		len = sizeof(struct ip6_hdr);
148053541Sshin		while (len < off) {
148153541Sshin			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
148253541Sshin
148378064Sume			switch (nxt) {
148453541Sshin			case IPPROTO_FRAGMENT:
148553541Sshin				len += sizeof(struct ip6_frag);
148653541Sshin				break;
148753541Sshin			case IPPROTO_AH:
148853541Sshin				len += (ip6e->ip6e_len + 2) << 2;
148953541Sshin				break;
149053541Sshin			default:
149153541Sshin				len += (ip6e->ip6e_len + 1) << 3;
149253541Sshin				break;
149353541Sshin			}
149453541Sshin			nxt = ip6e->ip6e_nxt;
149553541Sshin		}
149653541Sshin		if (ip6e)
149753541Sshin			return(&ip6e->ip6e_nxt);
149853541Sshin		else
149953541Sshin			return NULL;
150053541Sshin	}
150153541Sshin}
150253541Sshin
150353541Sshin/*
150462587Sitojun * get next header offset.  m will be retained.
150562587Sitojun */
150662587Sitojunint
150762587Sitojunip6_nexthdr(m, off, proto, nxtp)
150862587Sitojun	struct mbuf *m;
150962587Sitojun	int off;
151062587Sitojun	int proto;
151162587Sitojun	int *nxtp;
151262587Sitojun{
151362587Sitojun	struct ip6_hdr ip6;
151462587Sitojun	struct ip6_ext ip6e;
151562587Sitojun	struct ip6_frag fh;
151662587Sitojun
151762587Sitojun	/* just in case */
151862587Sitojun	if (m == NULL)
151962587Sitojun		panic("ip6_nexthdr: m == NULL");
152062587Sitojun	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
152162587Sitojun		return -1;
152262587Sitojun
152362587Sitojun	switch (proto) {
152462587Sitojun	case IPPROTO_IPV6:
152562587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6))
152662587Sitojun			return -1;
152762587Sitojun		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
152862587Sitojun		if (nxtp)
152962587Sitojun			*nxtp = ip6.ip6_nxt;
153062587Sitojun		off += sizeof(ip6);
153162587Sitojun		return off;
153262587Sitojun
153362587Sitojun	case IPPROTO_FRAGMENT:
153462587Sitojun		/*
153562587Sitojun		 * terminate parsing if it is not the first fragment,
153662587Sitojun		 * it does not make sense to parse through it.
153762587Sitojun		 */
153862587Sitojun		if (m->m_pkthdr.len < off + sizeof(fh))
153962587Sitojun			return -1;
154062587Sitojun		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
154162587Sitojun		if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
154262587Sitojun			return -1;
154362587Sitojun		if (nxtp)
154462587Sitojun			*nxtp = fh.ip6f_nxt;
154562587Sitojun		off += sizeof(struct ip6_frag);
154662587Sitojun		return off;
154762587Sitojun
154862587Sitojun	case IPPROTO_AH:
154962587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6e))
155062587Sitojun			return -1;
155162587Sitojun		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
155262587Sitojun		if (nxtp)
155362587Sitojun			*nxtp = ip6e.ip6e_nxt;
155462587Sitojun		off += (ip6e.ip6e_len + 2) << 2;
155562587Sitojun		return off;
155662587Sitojun
155762587Sitojun	case IPPROTO_HOPOPTS:
155862587Sitojun	case IPPROTO_ROUTING:
155962587Sitojun	case IPPROTO_DSTOPTS:
156062587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6e))
156162587Sitojun			return -1;
156262587Sitojun		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
156362587Sitojun		if (nxtp)
156462587Sitojun			*nxtp = ip6e.ip6e_nxt;
156562587Sitojun		off += (ip6e.ip6e_len + 1) << 3;
156662587Sitojun		return off;
156762587Sitojun
156862587Sitojun	case IPPROTO_NONE:
156962587Sitojun	case IPPROTO_ESP:
157062587Sitojun	case IPPROTO_IPCOMP:
157162587Sitojun		/* give up */
157262587Sitojun		return -1;
157362587Sitojun
157462587Sitojun	default:
157562587Sitojun		return -1;
157662587Sitojun	}
157762587Sitojun
157862587Sitojun	return -1;
157962587Sitojun}
158062587Sitojun
158162587Sitojun/*
158262587Sitojun * get offset for the last header in the chain.  m will be kept untainted.
158362587Sitojun */
158462587Sitojunint
158562587Sitojunip6_lasthdr(m, off, proto, nxtp)
158662587Sitojun	struct mbuf *m;
158762587Sitojun	int off;
158862587Sitojun	int proto;
158962587Sitojun	int *nxtp;
159062587Sitojun{
159162587Sitojun	int newoff;
159262587Sitojun	int nxt;
159362587Sitojun
159462587Sitojun	if (!nxtp) {
159562587Sitojun		nxt = -1;
159662587Sitojun		nxtp = &nxt;
159762587Sitojun	}
159862587Sitojun	while (1) {
159962587Sitojun		newoff = ip6_nexthdr(m, off, proto, nxtp);
160062587Sitojun		if (newoff < 0)
160162587Sitojun			return off;
160262587Sitojun		else if (newoff < off)
160362587Sitojun			return -1;	/* invalid */
160462587Sitojun		else if (newoff == off)
160562587Sitojun			return newoff;
160662587Sitojun
160762587Sitojun		off = newoff;
160862587Sitojun		proto = *nxtp;
160962587Sitojun	}
161062587Sitojun}
161162587Sitojun
161278064Sumestruct mbuf *
161378064Sumeip6_addaux(m)
161478064Sume	struct mbuf *m;
161578064Sume{
161678064Sume	struct mbuf *n;
161778064Sume
161878064Sume#ifdef DIAGNOSTIC
161978064Sume	if (sizeof(struct ip6aux) > MHLEN)
162078064Sume		panic("assumption failed on sizeof(ip6aux)");
162178064Sume#endif
162278064Sume	n = m_aux_find(m, AF_INET6, -1);
162378064Sume	if (n) {
162478064Sume		if (n->m_len < sizeof(struct ip6aux)) {
162578064Sume			printf("conflicting use of ip6aux");
162678064Sume			return NULL;
162778064Sume		}
162878064Sume	} else {
162978064Sume		n = m_aux_add(m, AF_INET6, -1);
163078064Sume		n->m_len = sizeof(struct ip6aux);
163178064Sume		bzero(mtod(n, caddr_t), n->m_len);
163278064Sume	}
163378064Sume	return n;
163478064Sume}
163578064Sume
163678064Sumestruct mbuf *
163778064Sumeip6_findaux(m)
163878064Sume	struct mbuf *m;
163978064Sume{
164078064Sume	struct mbuf *n;
164178064Sume
164278064Sume	n = m_aux_find(m, AF_INET6, -1);
164378064Sume	if (n && n->m_len < sizeof(struct ip6aux)) {
164478064Sume		printf("conflicting use of ip6aux");
164578064Sume		n = NULL;
164678064Sume	}
164778064Sume	return n;
164878064Sume}
164978064Sume
165078064Sumevoid
165178064Sumeip6_delaux(m)
165278064Sume	struct mbuf *m;
165378064Sume{
165478064Sume	struct mbuf *n;
165578064Sume
165678064Sume	n = m_aux_find(m, AF_INET6, -1);
165778064Sume	if (n)
165878064Sume		m_aux_delete(m, n);
165978064Sume}
166078064Sume
166162587Sitojun/*
166253541Sshin * System control for IP6
166353541Sshin */
166453541Sshin
166553541Sshinu_char	inet6ctlerrmap[PRC_NCMDS] = {
166653541Sshin	0,		0,		0,		0,
166753541Sshin	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
166853541Sshin	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
166953541Sshin	EMSGSIZE,	EHOSTUNREACH,	0,		0,
167053541Sshin	0,		0,		0,		0,
167153541Sshin	ENOPROTOOPT
167253541Sshin};
1673