ip6_input.c revision 112130
162587Sitojun/*	$FreeBSD: head/sys/netinet6/ip6_input.c 112130 2003-03-12 06:08:48Z sam $	*/
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>
9364060Sdarrenr#ifdef PFIL_HOOKS
9464060Sdarrenr#include <net/pfil.h>
9564060Sdarrenr#endif
9653541Sshin
9753541Sshin#include <netinet/in.h>
9853541Sshin#include <netinet/in_systm.h>
9962587Sitojun#ifdef INET
10053541Sshin#include <netinet/ip.h>
10153541Sshin#include <netinet/ip_icmp.h>
10295023Ssuz#endif /* INET */
10362587Sitojun#include <netinet/ip6.h>
10453541Sshin#include <netinet6/in6_var.h>
10553541Sshin#include <netinet6/ip6_var.h>
10662587Sitojun#include <netinet/in_pcb.h>
10762587Sitojun#include <netinet/icmp6.h>
10853541Sshin#include <netinet6/in6_ifattach.h>
10953541Sshin#include <netinet6/nd6.h>
11053541Sshin#include <netinet6/in6_prefix.h>
11153541Sshin
11278064Sume#ifdef IPSEC
11378064Sume#include <netinet6/ipsec.h>
11478064Sume#ifdef INET6
11578064Sume#include <netinet6/ipsec6.h>
11678064Sume#endif
11778064Sume#endif
11878064Sume
119105199Ssam#ifdef FAST_IPSEC
120105199Ssam#include <netipsec/ipsec.h>
121105199Ssam#include <netipsec/ipsec6.h>
122105199Ssam#define	IPSEC
123105199Ssam#endif /* FAST_IPSEC */
124105199Ssam
12553541Sshin#include <netinet6/ip6_fw.h>
12653541Sshin
12753541Sshin#include <netinet6/ip6protosw.h>
12853541Sshin
12953541Sshin#include <net/net_osdep.h>
13053541Sshin
13162587Sitojunextern struct domain inet6domain;
13253541Sshin
13362587Sitojunu_char ip6_protox[IPPROTO_MAX];
134111888Sjlemonstatic struct ifqueue ip6intrq;
13562587Sitojunstatic int ip6qmaxlen = IFQ_MAXLEN;
13662587Sitojunstruct in6_ifaddr *in6_ifaddr;
13753541Sshin
13878064Sumeextern struct callout in6_tmpaddrtimer_ch;
13978064Sume
14062587Sitojunint ip6_forward_srcrt;			/* XXX */
14162587Sitojunint ip6_sourcecheck;			/* XXX */
14262587Sitojunint ip6_sourcecheck_interval;		/* XXX */
14353541Sshin
14478064Sumeint ip6_ours_check_algorithm;
14578064Sume
14678064Sume
14753541Sshin/* firewall hooks */
14862587Sitojunip6_fw_chk_t *ip6_fw_chk_ptr;
14962587Sitojunip6_fw_ctl_t *ip6_fw_ctl_ptr;
15066303Sumeint ip6_fw_enable = 1;
15153541Sshin
15262587Sitojunstruct ip6stat ip6stat;
15353541Sshin
15462587Sitojunstatic void ip6_init2 __P((void *));
155105194Ssamstatic struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *));
15662587Sitojunstatic int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
15762587Sitojun#ifdef PULLDOWN_TEST
15862587Sitojunstatic struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int));
15953541Sshin#endif
16053541Sshin
16178064Sume
16253541Sshin/*
16353541Sshin * IP6 initialization: fill in IP6 protocol switch table.
16453541Sshin * All protocols not implemented in kernel go to raw IP6 protocol handler.
16553541Sshin */
16653541Sshinvoid
16753541Sshinip6_init()
16853541Sshin{
16978064Sume	struct ip6protosw *pr;
17078064Sume	int i;
17153541Sshin	struct timeval tv;
17253541Sshin
17378064Sume#ifdef DIAGNOSTIC
17478064Sume	if (sizeof(struct protosw) != sizeof(struct ip6protosw))
17578064Sume		panic("sizeof(protosw) != sizeof(ip6protosw)");
17678064Sume#endif
17753541Sshin	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
17853541Sshin	if (pr == 0)
17953541Sshin		panic("ip6_init");
18053541Sshin	for (i = 0; i < IPPROTO_MAX; i++)
18153541Sshin		ip6_protox[i] = pr - inet6sw;
18253541Sshin	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
18353541Sshin	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
18453541Sshin		if (pr->pr_domain->dom_family == PF_INET6 &&
18553541Sshin		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
18653541Sshin			ip6_protox[pr->pr_protocol] = pr - inet6sw;
18753541Sshin	ip6intrq.ifq_maxlen = ip6qmaxlen;
18893818Sjhb	mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF);
189111888Sjlemon	netisr_register(NETISR_IPV6, ip6_input, &ip6intrq);
19053541Sshin	nd6_init();
19153541Sshin	frag6_init();
19253541Sshin	/*
19353541Sshin	 * in many cases, random() here does NOT return random number
19453541Sshin	 * as initialization during bootstrap time occur in fixed order.
19553541Sshin	 */
19653541Sshin	microtime(&tv);
19753541Sshin	ip6_flow_seq = random() ^ tv.tv_usec;
19878064Sume	microtime(&tv);
19978064Sume	ip6_desync_factor = (random() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR;
20053541Sshin}
20153541Sshin
20253541Sshinstatic void
20353541Sshinip6_init2(dummy)
20453541Sshin	void *dummy;
20553541Sshin{
20653541Sshin
20753541Sshin	/*
20853541Sshin	 * to route local address of p2p link to loopback,
20953541Sshin	 * assign loopback address first.
21053541Sshin	 */
21162587Sitojun	in6_ifattach(&loif[0], NULL);
21253541Sshin
21353541Sshin	/* nd6_timer_init */
21478064Sume	callout_init(&nd6_timer_ch, 0);
21578064Sume	callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
21678064Sume
21753541Sshin	/* router renumbering prefix list maintenance */
21878064Sume	callout_init(&in6_rr_timer_ch, 0);
21978064Sume	callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL);
22078064Sume
22178064Sume	/* timer for regeneranation of temporary addresses randomize ID */
22278064Sume	callout_reset(&in6_tmpaddrtimer_ch,
22378064Sume		      (ip6_temp_preferred_lifetime - ip6_desync_factor -
22478064Sume		       ip6_temp_regen_advance) * hz,
22578064Sume		      in6_tmpaddrtimer, NULL);
22653541Sshin}
22753541Sshin
22853541Sshin/* cheat */
22955009Sshin/* This must be after route_init(), which is now SI_ORDER_THIRD */
23055009SshinSYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
23153541Sshin
23253541Sshinextern struct	route_in6 ip6_forward_rt;
23353541Sshin
23453541Sshinvoid
23553541Sshinip6_input(m)
23653541Sshin	struct mbuf *m;
23753541Sshin{
23853541Sshin	struct ip6_hdr *ip6;
23953541Sshin	int off = sizeof(struct ip6_hdr), nest;
24053541Sshin	u_int32_t plen;
24153541Sshin	u_int32_t rtalert = ~0;
24253541Sshin	int nxt, ours = 0;
24353541Sshin	struct ifnet *deliverifp = NULL;
24464060Sdarrenr#ifdef  PFIL_HOOKS
24564060Sdarrenr	struct packet_filter_hook *pfh;
24664060Sdarrenr	struct mbuf *m0;
24764060Sdarrenr	int rv;
24864060Sdarrenr#endif  /* PFIL_HOOKS */
24953541Sshin
25053541Sshin#ifdef IPSEC
25153541Sshin	/*
25253541Sshin	 * should the inner packet be considered authentic?
25353541Sshin	 * see comment in ah4_input().
25453541Sshin	 */
25553541Sshin	if (m) {
25653541Sshin		m->m_flags &= ~M_AUTHIPHDR;
25753541Sshin		m->m_flags &= ~M_AUTHIPDGM;
25853541Sshin	}
25953541Sshin#endif
26053541Sshin
26153541Sshin	/*
26278064Sume	 * make sure we don't have onion peering information into m_aux.
26378064Sume	 */
26478064Sume	ip6_delaux(m);
26578064Sume
26678064Sume	/*
26795023Ssuz	 * mbuf statistics
26853541Sshin	 */
26953541Sshin	if (m->m_flags & M_EXT) {
27053541Sshin		if (m->m_next)
27153541Sshin			ip6stat.ip6s_mext2m++;
27253541Sshin		else
27353541Sshin			ip6stat.ip6s_mext1++;
27453541Sshin	} else {
27578064Sume#define M2MMAX	(sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
27653541Sshin		if (m->m_next) {
27753541Sshin			if (m->m_flags & M_LOOP) {
27895023Ssuz				ip6stat.ip6s_m2m[loif[0].if_index]++;	/* XXX */
27978064Sume			} else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
28053541Sshin				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
28153541Sshin			else
28253541Sshin				ip6stat.ip6s_m2m[0]++;
28353541Sshin		} else
28453541Sshin			ip6stat.ip6s_m1++;
28578064Sume#undef M2MMAX
28653541Sshin	}
28753541Sshin
28853541Sshin	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
28953541Sshin	ip6stat.ip6s_total++;
29053541Sshin
29162587Sitojun#ifndef PULLDOWN_TEST
29274336Skuriyama	/*
29374336Skuriyama	 * L2 bridge code and some other code can return mbuf chain
29474336Skuriyama	 * that does not conform to KAME requirement.  too bad.
29574336Skuriyama	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
29674336Skuriyama	 */
29774336Skuriyama	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
29874336Skuriyama		struct mbuf *n;
29974336Skuriyama
300111119Simp		MGETHDR(n, M_DONTWAIT, MT_HEADER);
30177003Sume		if (n)
302108466Ssam			M_MOVE_PKTHDR(n, m);
303108825Ssam		if (n && n->m_pkthdr.len > MHLEN) {
304111119Simp			MCLGET(n, M_DONTWAIT);
30574336Skuriyama			if ((n->m_flags & M_EXT) == 0) {
30674336Skuriyama				m_freem(n);
30774336Skuriyama				n = NULL;
30874336Skuriyama			}
30974336Skuriyama		}
31095023Ssuz		if (n == NULL) {
31177003Sume			m_freem(m);
31274336Skuriyama			return;	/*ENOBUFS*/
31377003Sume		}
31474336Skuriyama
315108825Ssam		m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
316108825Ssam		n->m_len = n->m_pkthdr.len;
31774336Skuriyama		m_freem(m);
31874336Skuriyama		m = n;
31974336Skuriyama	}
32053541Sshin	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
32162587Sitojun#endif
32253541Sshin
32353541Sshin	if (m->m_len < sizeof(struct ip6_hdr)) {
32453541Sshin		struct ifnet *inifp;
32553541Sshin		inifp = m->m_pkthdr.rcvif;
32653541Sshin		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
32753541Sshin			ip6stat.ip6s_toosmall++;
32853541Sshin			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
32953541Sshin			return;
33053541Sshin		}
33153541Sshin	}
33253541Sshin
33353541Sshin	ip6 = mtod(m, struct ip6_hdr *);
33453541Sshin
33553541Sshin	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
33653541Sshin		ip6stat.ip6s_badvers++;
33753541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
33853541Sshin		goto bad;
33953541Sshin	}
34053541Sshin
34164060Sdarrenr#ifdef PFIL_HOOKS
34264060Sdarrenr	/*
34364060Sdarrenr	 * Run through list of hooks for input packets.  If there are any
34464060Sdarrenr	 * filters which require that additional packets in the flow are
34564060Sdarrenr	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
34664060Sdarrenr	 * Note that filters must _never_ set this flag, as another filter
34764060Sdarrenr	 * in the list may have previously cleared it.
34864060Sdarrenr	 */
34964060Sdarrenr	m0 = m;
35064558Sume	pfh = pfil_hook_get(PFIL_IN, &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
35164060Sdarrenr	for (; pfh; pfh = pfh->pfil_link.tqe_next)
35264060Sdarrenr		if (pfh->pfil_func) {
35364060Sdarrenr			rv = pfh->pfil_func(ip6, sizeof(*ip6),
35464060Sdarrenr					    m->m_pkthdr.rcvif, 0, &m0);
35564060Sdarrenr			if (rv)
35664060Sdarrenr				return;
35764060Sdarrenr			m = m0;
35864060Sdarrenr			if (m == NULL)
35964060Sdarrenr				return;
36064060Sdarrenr			ip6 = mtod(m, struct ip6_hdr *);
36164060Sdarrenr		}
36264060Sdarrenr#endif /* PFIL_HOOKS */
36364060Sdarrenr
36453541Sshin	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
36553541Sshin
36653541Sshin	/*
36753541Sshin	 * Check with the firewall...
36853541Sshin	 */
36966303Sume	if (ip6_fw_enable && ip6_fw_chk_ptr) {
37053541Sshin		u_short port = 0;
37153541Sshin		/* If ipfw says divert, we have to just drop packet */
37253541Sshin		/* use port as a dummy argument */
37353541Sshin		if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
37453541Sshin			m_freem(m);
37553541Sshin			m = NULL;
37653541Sshin		}
37753541Sshin		if (!m)
37853541Sshin			return;
37953541Sshin	}
38053541Sshin
38153541Sshin	/*
38278064Sume	 * Check against address spoofing/corruption.
38353541Sshin	 */
38453541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
38553541Sshin	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
38678064Sume		/*
38778064Sume		 * XXX: "badscope" is not very suitable for a multicast source.
38878064Sume		 */
38953541Sshin		ip6stat.ip6s_badscope++;
39053541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
39153541Sshin		goto bad;
39253541Sshin	}
39378064Sume	if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
39478064Sume	     IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) &&
39578064Sume	    (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
39678064Sume		ip6stat.ip6s_badscope++;
39778064Sume		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
39878064Sume		goto bad;
39978064Sume	}
40095023Ssuz
40162587Sitojun	/*
40278064Sume	 * The following check is not documented in specs.  A malicious
40378064Sume	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
40478064Sume	 * and bypass security checks (act as if it was from 127.0.0.1 by using
40578064Sume	 * IPv6 src ::ffff:127.0.0.1).	Be cautious.
40678064Sume	 *
40778064Sume	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
40878064Sume	 * support IPv4-less kernel compilation, we cannot support SIIT
40978064Sume	 * environment at all.  So, it makes more sense for us to reject any
41078064Sume	 * malicious packets for non-SIIT environment, than try to do a
41178064Sume	 * partical support for SIIT environment.
41262587Sitojun	 */
41378064Sume	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
41478064Sume	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
41578064Sume		ip6stat.ip6s_badscope++;
41678064Sume		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
41778064Sume		goto bad;
41878064Sume	}
41962587Sitojun#if 0
42062587Sitojun	/*
42162587Sitojun	 * Reject packets with IPv4 compatible addresses (auto tunnel).
42262587Sitojun	 *
42362587Sitojun	 * The code forbids auto tunnel relay case in RFC1933 (the check is
42462587Sitojun	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
42562587Sitojun	 * is revised to forbid relaying case.
42662587Sitojun	 */
42762587Sitojun	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
42862587Sitojun	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
42962587Sitojun		ip6stat.ip6s_badscope++;
43062587Sitojun		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
43162587Sitojun		goto bad;
43262587Sitojun	}
43362587Sitojun#endif
43475246Sume
43578064Sume	/* drop packets if interface ID portion is already filled */
43678064Sume	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
43778064Sume		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
43878064Sume		    ip6->ip6_src.s6_addr16[1]) {
43953541Sshin			ip6stat.ip6s_badscope++;
44053541Sshin			goto bad;
44153541Sshin		}
44278064Sume		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
44378064Sume		    ip6->ip6_dst.s6_addr16[1]) {
44478064Sume			ip6stat.ip6s_badscope++;
44578064Sume			goto bad;
44678064Sume		}
44753541Sshin	}
44853541Sshin
44978064Sume	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
45078064Sume		ip6->ip6_src.s6_addr16[1]
45178064Sume			= htons(m->m_pkthdr.rcvif->if_index);
45278064Sume	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
45378064Sume		ip6->ip6_dst.s6_addr16[1]
45478064Sume			= htons(m->m_pkthdr.rcvif->if_index);
45553541Sshin
45678064Sume#if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */
45753541Sshin	/*
45878064Sume	 * We use rt->rt_ifp to determine if the address is ours or not.
45978064Sume	 * If rt_ifp is lo0, the address is ours.
46078064Sume	 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0,
46178064Sume	 * so any address under fe80::%lo0/64 will be mistakenly considered
46278064Sume	 * local.  The special case is supplied to handle the case properly
46378064Sume	 * by actually looking at interface addresses
46478064Sume	 * (using in6ifa_ifpwithaddr).
46553541Sshin	 */
46678064Sume	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 &&
46778064Sume	    IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
46878064Sume		if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) {
46978064Sume			icmp6_error(m, ICMP6_DST_UNREACH,
47078064Sume			    ICMP6_DST_UNREACH_ADDR, 0);
47178064Sume			/* m is already freed */
47278064Sume			return;
47378064Sume		}
47475246Sume
47578064Sume		ours = 1;
47678064Sume		deliverifp = m->m_pkthdr.rcvif;
47778064Sume		goto hbhcheck;
47878064Sume	}
47975719Sume#endif
48053541Sshin
48153541Sshin	/*
48253541Sshin	 * Multicast check
48353541Sshin	 */
48453541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
48553541Sshin	  	struct	in6_multi *in6m = 0;
48653541Sshin
48753541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
48853541Sshin		/*
48953541Sshin		 * See if we belong to the destination multicast group on the
49053541Sshin		 * arrival interface.
49153541Sshin		 */
49253541Sshin		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
49353541Sshin		if (in6m)
49453541Sshin			ours = 1;
49556723Sshin		else if (!ip6_mrouter) {
49653541Sshin			ip6stat.ip6s_notmember++;
49753541Sshin			ip6stat.ip6s_cantforward++;
49853541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
49953541Sshin			goto bad;
50053541Sshin		}
50153541Sshin		deliverifp = m->m_pkthdr.rcvif;
50253541Sshin		goto hbhcheck;
50353541Sshin	}
50453541Sshin
50553541Sshin	/*
50653541Sshin	 *  Unicast check
50753541Sshin	 */
50878064Sume	switch (ip6_ours_check_algorithm) {
50978064Sume	default:
51078064Sume		/*
51178064Sume		 * XXX: I intentionally broke our indentation rule here,
51278064Sume		 *      since this switch-case is just for measurement and
51378064Sume		 *      therefore should soon be removed.
51478064Sume		 */
51562587Sitojun	if (ip6_forward_rt.ro_rt != NULL &&
51662587Sitojun	    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
51762587Sitojun	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
51878064Sume			       &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
51962587Sitojun		ip6stat.ip6s_forward_cachehit++;
52062587Sitojun	else {
52178064Sume		struct sockaddr_in6 *dst6;
52278064Sume
52353541Sshin		if (ip6_forward_rt.ro_rt) {
52462587Sitojun			/* route is down or destination is different */
52562587Sitojun			ip6stat.ip6s_forward_cachemiss++;
52653541Sshin			RTFREE(ip6_forward_rt.ro_rt);
52753541Sshin			ip6_forward_rt.ro_rt = 0;
52853541Sshin		}
52962587Sitojun
53053541Sshin		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
53178064Sume		dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
53278064Sume		dst6->sin6_len = sizeof(struct sockaddr_in6);
53378064Sume		dst6->sin6_family = AF_INET6;
53478064Sume		dst6->sin6_addr = ip6->ip6_dst;
53562587Sitojun#ifdef SCOPEDROUTING
53662587Sitojun		ip6_forward_rt.ro_dst.sin6_scope_id =
53762587Sitojun			in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_dst);
53862587Sitojun#endif
53953541Sshin
54053541Sshin		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
54153541Sshin	}
54253541Sshin
54353541Sshin#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
54453541Sshin
54553541Sshin	/*
54653541Sshin	 * Accept the packet if the forwarding interface to the destination
54753541Sshin	 * according to the routing table is the loopback interface,
54853541Sshin	 * unless the associated route has a gateway.
54953541Sshin	 * Note that this approach causes to accept a packet if there is a
55053541Sshin	 * route to the loopback interface for the destination of the packet.
55153541Sshin	 * But we think it's even useful in some situations, e.g. when using
55253541Sshin	 * a special daemon which wants to intercept the packet.
55378064Sume	 *
55478064Sume	 * XXX: some OSes automatically make a cloned route for the destination
55578064Sume	 * of an outgoing packet.  If the outgoing interface of the packet
55678064Sume	 * is a loopback one, the kernel would consider the packet to be
55778064Sume	 * accepted, even if we have no such address assinged on the interface.
55878064Sume	 * We check the cloned flag of the route entry to reject such cases,
55978064Sume	 * assuming that route entries for our own addresses are not made by
56078064Sume	 * cloning (it should be true because in6_addloop explicitly installs
56178064Sume	 * the host route).  However, we might have to do an explicit check
56278064Sume	 * while it would be less efficient.  Or, should we rather install a
56378064Sume	 * reject route for such a case?
56453541Sshin	 */
56553541Sshin	if (ip6_forward_rt.ro_rt &&
56653541Sshin	    (ip6_forward_rt.ro_rt->rt_flags &
56753541Sshin	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
56878064Sume#ifdef RTF_WASCLONED
56978064Sume	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
57078064Sume#endif
57178064Sume#ifdef RTF_CLONED
57278064Sume	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
57378064Sume#endif
57462587Sitojun#if 0
57553541Sshin	    /*
57662587Sitojun	     * The check below is redundant since the comparison of
57762587Sitojun	     * the destination and the key of the rtentry has
57862587Sitojun	     * already done through looking up the routing table.
57953541Sshin	     */
58062587Sitojun	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
58178064Sume				&rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
58262587Sitojun#endif
58353541Sshin	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
58453541Sshin		struct in6_ifaddr *ia6 =
58553541Sshin			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
58678064Sume
58762587Sitojun		/*
58878064Sume		 * record address information into m_aux.
58978064Sume		 */
59078064Sume		(void)ip6_setdstifaddr(m, ia6);
59178064Sume
59278064Sume		/*
59362587Sitojun		 * packets to a tentative, duplicated, or somehow invalid
59462587Sitojun		 * address must not be accepted.
59562587Sitojun		 */
59653541Sshin		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
59762587Sitojun			/* this address is ready */
59853541Sshin			ours = 1;
59953541Sshin			deliverifp = ia6->ia_ifp;	/* correct? */
60067334Sjoe			/* Count the packet in the ip address stats */
60167334Sjoe			ia6->ia_ifa.if_ipackets++;
60267334Sjoe			ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
60353541Sshin			goto hbhcheck;
60453541Sshin		} else {
60562587Sitojun			/* address is not ready, so discard the packet. */
60678064Sume			nd6log((LOG_INFO,
60778064Sume			    "ip6_input: packet to an unready address %s->%s\n",
60862587Sitojun			    ip6_sprintf(&ip6->ip6_src),
60978064Sume			    ip6_sprintf(&ip6->ip6_dst)));
61062587Sitojun
61162587Sitojun			goto bad;
61253541Sshin		}
61353541Sshin	}
61478064Sume	} /* XXX indentation (see above) */
61553541Sshin
61653541Sshin	/*
61753541Sshin	 * FAITH(Firewall Aided Internet Translator)
61853541Sshin	 */
61953541Sshin	if (ip6_keepfaith) {
62053541Sshin		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
62153541Sshin		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
62253541Sshin			/* XXX do we need more sanity checks? */
62353541Sshin			ours = 1;
62495023Ssuz			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
62553541Sshin			goto hbhcheck;
62653541Sshin		}
62753541Sshin	}
62853541Sshin
62953541Sshin	/*
63053541Sshin	 * Now there is no reason to process the packet if it's not our own
63153541Sshin	 * and we're not a router.
63253541Sshin	 */
63353541Sshin	if (!ip6_forwarding) {
63453541Sshin		ip6stat.ip6s_cantforward++;
63553541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
63653541Sshin		goto bad;
63753541Sshin	}
63853541Sshin
63953541Sshin  hbhcheck:
64053541Sshin	/*
64178064Sume	 * record address information into m_aux, if we don't have one yet.
64278064Sume	 * note that we are unable to record it, if the address is not listed
64378064Sume	 * as our interface address (e.g. multicast addresses, addresses
64478064Sume	 * within FAITH prefixes and such).
64578064Sume	 */
64678064Sume	if (deliverifp && !ip6_getdstifaddr(m)) {
64778064Sume		struct in6_ifaddr *ia6;
64878064Sume
64978064Sume		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
65078064Sume		if (ia6) {
65178064Sume			if (!ip6_setdstifaddr(m, ia6)) {
65278064Sume				/*
65378064Sume				 * XXX maybe we should drop the packet here,
65478064Sume				 * as we could not provide enough information
65578064Sume				 * to the upper layers.
65678064Sume				 */
65778064Sume			}
65878064Sume		}
65978064Sume	}
66078064Sume
66178064Sume	/*
66253541Sshin	 * Process Hop-by-Hop options header if it's contained.
66353541Sshin	 * m may be modified in ip6_hopopts_input().
66453541Sshin	 * If a JumboPayload option is included, plen will also be modified.
66553541Sshin	 */
66653541Sshin	plen = (u_int32_t)ntohs(ip6->ip6_plen);
66753541Sshin	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
66862587Sitojun		struct ip6_hbh *hbh;
66962587Sitojun
67053541Sshin		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
67162587Sitojun#if 0	/*touches NULL pointer*/
67253541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
67362587Sitojun#endif
67453541Sshin			return;	/* m have already been freed */
67553541Sshin		}
67662587Sitojun
67753541Sshin		/* adjust pointer */
67853541Sshin		ip6 = mtod(m, struct ip6_hdr *);
67953541Sshin
68053541Sshin		/*
68195023Ssuz		 * if the payload length field is 0 and the next header field
68262587Sitojun		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
68362587Sitojun		 * option MUST be included.
68462587Sitojun		 */
68562587Sitojun		if (ip6->ip6_plen == 0 && plen == 0) {
68662587Sitojun			/*
68762587Sitojun			 * Note that if a valid jumbo payload option is
68862587Sitojun			 * contained, ip6_hoptops_input() must set a valid
68962587Sitojun			 * (non-zero) payload length to the variable plen.
69062587Sitojun			 */
69162587Sitojun			ip6stat.ip6s_badoptions++;
69262587Sitojun			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
69362587Sitojun			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
69462587Sitojun			icmp6_error(m, ICMP6_PARAM_PROB,
69562587Sitojun				    ICMP6_PARAMPROB_HEADER,
69662587Sitojun				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
69762587Sitojun			return;
69862587Sitojun		}
69962587Sitojun#ifndef PULLDOWN_TEST
70062587Sitojun		/* ip6_hopopts_input() ensures that mbuf is contiguous */
70162587Sitojun		hbh = (struct ip6_hbh *)(ip6 + 1);
70262587Sitojun#else
70362587Sitojun		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
70462587Sitojun			sizeof(struct ip6_hbh));
70562587Sitojun		if (hbh == NULL) {
70662587Sitojun			ip6stat.ip6s_tooshort++;
70762587Sitojun			return;
70862587Sitojun		}
70962587Sitojun#endif
71062587Sitojun		nxt = hbh->ip6h_nxt;
71162587Sitojun
71262587Sitojun		/*
71353541Sshin		 * accept the packet if a router alert option is included
71453541Sshin		 * and we act as an IPv6 router.
71553541Sshin		 */
71653541Sshin		if (rtalert != ~0 && ip6_forwarding)
71753541Sshin			ours = 1;
71853541Sshin	} else
71953541Sshin		nxt = ip6->ip6_nxt;
72053541Sshin
72153541Sshin	/*
72253541Sshin	 * Check that the amount of data in the buffers
72353541Sshin	 * is as at least much as the IPv6 header would have us expect.
72453541Sshin	 * Trim mbufs if longer than we expect.
72553541Sshin	 * Drop packet if shorter than we expect.
72653541Sshin	 */
72753541Sshin	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
72853541Sshin		ip6stat.ip6s_tooshort++;
72953541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
73053541Sshin		goto bad;
73153541Sshin	}
73253541Sshin	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
73353541Sshin		if (m->m_len == m->m_pkthdr.len) {
73453541Sshin			m->m_len = sizeof(struct ip6_hdr) + plen;
73553541Sshin			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
73653541Sshin		} else
73753541Sshin			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
73853541Sshin	}
73953541Sshin
74053541Sshin	/*
74153541Sshin	 * Forward if desirable.
74253541Sshin	 */
74353541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
74456723Sshin		/*
74556723Sshin		 * If we are acting as a multicast router, all
74656723Sshin		 * incoming multicast packets are passed to the
74756723Sshin		 * kernel-level multicast forwarding function.
74856723Sshin		 * The packet is returned (relatively) intact; if
74956723Sshin		 * ip6_mforward() returns a non-zero value, the packet
75056723Sshin		 * must be discarded, else it may be accepted below.
75156723Sshin		 */
75256723Sshin		if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
75356723Sshin			ip6stat.ip6s_cantforward++;
75456723Sshin			m_freem(m);
75556723Sshin			return;
75656723Sshin		}
75753541Sshin		if (!ours) {
75853541Sshin			m_freem(m);
75953541Sshin			return;
76053541Sshin		}
76153541Sshin	} else if (!ours) {
76253541Sshin		ip6_forward(m, 0);
76353541Sshin		return;
76453541Sshin	}
76553541Sshin
76662587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
76762587Sitojun
76853541Sshin	/*
76962587Sitojun	 * Malicious party may be able to use IPv4 mapped addr to confuse
77062587Sitojun	 * tcp/udp stack and bypass security checks (act as if it was from
77162587Sitojun	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
77262587Sitojun	 *
77362587Sitojun	 * For SIIT end node behavior, you may want to disable the check.
77462587Sitojun	 * However, you will  become vulnerable to attacks using IPv4 mapped
77562587Sitojun	 * source.
77662587Sitojun	 */
77762587Sitojun	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
77862587Sitojun	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
77962587Sitojun		ip6stat.ip6s_badscope++;
78062587Sitojun		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
78162587Sitojun		goto bad;
78262587Sitojun	}
78362587Sitojun
78462587Sitojun	/*
78553541Sshin	 * Tell launch routine the next header
78653541Sshin	 */
78753541Sshin	ip6stat.ip6s_delivered++;
78853541Sshin	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
78953541Sshin	nest = 0;
79078064Sume
79153541Sshin	while (nxt != IPPROTO_DONE) {
79253541Sshin		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
79353541Sshin			ip6stat.ip6s_toomanyhdr++;
79453541Sshin			goto bad;
79553541Sshin		}
79653541Sshin
79753541Sshin		/*
79853541Sshin		 * protection against faulty packet - there should be
79953541Sshin		 * more sanity checks in header chain processing.
80053541Sshin		 */
80153541Sshin		if (m->m_pkthdr.len < off) {
80253541Sshin			ip6stat.ip6s_tooshort++;
80353541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
80453541Sshin			goto bad;
80553541Sshin		}
80653541Sshin
80778064Sume#if 0
80878064Sume		/*
80978064Sume		 * do we need to do it for every header?  yeah, other
81078064Sume		 * functions can play with it (like re-allocate and copy).
81178064Sume		 */
81278064Sume		mhist = ip6_addaux(m);
81378064Sume		if (mhist && M_TRAILINGSPACE(mhist) >= sizeof(nxt)) {
81478064Sume			hist = mtod(mhist, caddr_t) + mhist->m_len;
81578064Sume			bcopy(&nxt, hist, sizeof(nxt));
81678064Sume			mhist->m_len += sizeof(nxt);
81778064Sume		} else {
81878064Sume			ip6stat.ip6s_toomanyhdr++;
81978064Sume			goto bad;
82078064Sume		}
82178064Sume#endif
82278064Sume
82378064Sume#ifdef IPSEC
82478064Sume		/*
82578064Sume		 * enforce IPsec policy checking if we are seeing last header.
82678064Sume		 * note that we do not visit this with protocols with pcb layer
82778064Sume		 * code - like udp/tcp/raw ip.
82878064Sume		 */
82978064Sume		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
83078064Sume		    ipsec6_in_reject(m, NULL)) {
83178064Sume			ipsec6stat.in_polvio++;
83278064Sume			goto bad;
83378064Sume		}
83478064Sume#endif
83578064Sume
83653541Sshin		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
83753541Sshin	}
83853541Sshin	return;
83953541Sshin bad:
84053541Sshin	m_freem(m);
84153541Sshin}
84253541Sshin
84353541Sshin/*
84478064Sume * set/grab in6_ifaddr correspond to IPv6 destination address.
84578064Sume * XXX backward compatibility wrapper
84678064Sume */
847105194Ssamstatic struct ip6aux *
84878064Sumeip6_setdstifaddr(m, ia6)
84978064Sume	struct mbuf *m;
85078064Sume	struct in6_ifaddr *ia6;
85178064Sume{
852105194Ssam	struct ip6aux *n;
85378064Sume
85478064Sume	n = ip6_addaux(m);
85578064Sume	if (n)
856105194Ssam		n->ip6a_dstia6 = ia6;
85778064Sume	return n;	/* NULL if failed to set */
85878064Sume}
85978064Sume
86078064Sumestruct in6_ifaddr *
86178064Sumeip6_getdstifaddr(m)
86278064Sume	struct mbuf *m;
86378064Sume{
864105194Ssam	struct ip6aux *n;
86578064Sume
86678064Sume	n = ip6_findaux(m);
86778064Sume	if (n)
868105194Ssam		return n->ip6a_dstia6;
86978064Sume	else
87078064Sume		return NULL;
87178064Sume}
87278064Sume
87378064Sume/*
87453541Sshin * Hop-by-Hop options header processing. If a valid jumbo payload option is
87553541Sshin * included, the real payload length will be stored in plenp.
87653541Sshin */
87753541Sshinstatic int
87853541Sshinip6_hopopts_input(plenp, rtalertp, mp, offp)
87953541Sshin	u_int32_t *plenp;
88053541Sshin	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
88153541Sshin	struct mbuf **mp;
88253541Sshin	int *offp;
88353541Sshin{
88478064Sume	struct mbuf *m = *mp;
88553541Sshin	int off = *offp, hbhlen;
88653541Sshin	struct ip6_hbh *hbh;
88753541Sshin	u_int8_t *opt;
88853541Sshin
88953541Sshin	/* validation of the length of the header */
89062587Sitojun#ifndef PULLDOWN_TEST
89153541Sshin	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
89253541Sshin	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
89353541Sshin	hbhlen = (hbh->ip6h_len + 1) << 3;
89453541Sshin
89553541Sshin	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
89653541Sshin	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
89762587Sitojun#else
89862587Sitojun	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
89962587Sitojun		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
90062587Sitojun	if (hbh == NULL) {
90162587Sitojun		ip6stat.ip6s_tooshort++;
90262587Sitojun		return -1;
90362587Sitojun	}
90462587Sitojun	hbhlen = (hbh->ip6h_len + 1) << 3;
90562587Sitojun	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
90662587Sitojun		hbhlen);
90762587Sitojun	if (hbh == NULL) {
90862587Sitojun		ip6stat.ip6s_tooshort++;
90962587Sitojun		return -1;
91062587Sitojun	}
91162587Sitojun#endif
91253541Sshin	off += hbhlen;
91353541Sshin	hbhlen -= sizeof(struct ip6_hbh);
91453541Sshin	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
91553541Sshin
91653541Sshin	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
91753541Sshin				hbhlen, rtalertp, plenp) < 0)
91853541Sshin		return(-1);
91953541Sshin
92053541Sshin	*offp = off;
92153541Sshin	*mp = m;
92253541Sshin	return(0);
92353541Sshin}
92453541Sshin
92553541Sshin/*
92653541Sshin * Search header for all Hop-by-hop options and process each option.
92753541Sshin * This function is separate from ip6_hopopts_input() in order to
92853541Sshin * handle a case where the sending node itself process its hop-by-hop
92953541Sshin * options header. In such a case, the function is called from ip6_output().
93078064Sume *
93178064Sume * The function assumes that hbh header is located right after the IPv6 header
93278064Sume * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
93378064Sume * opthead + hbhlen is located in continuous memory region.
93453541Sshin */
93553541Sshinint
93653541Sshinip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
93753541Sshin	struct mbuf *m;
93853541Sshin	u_int8_t *opthead;
93953541Sshin	int hbhlen;
94053541Sshin	u_int32_t *rtalertp;
94153541Sshin	u_int32_t *plenp;
94253541Sshin{
94353541Sshin	struct ip6_hdr *ip6;
94453541Sshin	int optlen = 0;
94553541Sshin	u_int8_t *opt = opthead;
94653541Sshin	u_int16_t rtalert_val;
94762587Sitojun	u_int32_t jumboplen;
94878064Sume	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
94953541Sshin
95053541Sshin	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
95178064Sume		switch (*opt) {
95278064Sume		case IP6OPT_PAD1:
95378064Sume			optlen = 1;
95478064Sume			break;
95578064Sume		case IP6OPT_PADN:
95678064Sume			if (hbhlen < IP6OPT_MINLEN) {
95778064Sume				ip6stat.ip6s_toosmall++;
95878064Sume				goto bad;
95978064Sume			}
96078064Sume			optlen = *(opt + 1) + 2;
96178064Sume			break;
96278064Sume		case IP6OPT_RTALERT:
96362587Sitojun			/* XXX may need check for alignment */
96478064Sume			if (hbhlen < IP6OPT_RTALERT_LEN) {
96578064Sume				ip6stat.ip6s_toosmall++;
96678064Sume				goto bad;
96778064Sume			}
96878064Sume			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
96978064Sume				/* XXX stat */
97078064Sume				icmp6_error(m, ICMP6_PARAM_PROB,
97178064Sume					    ICMP6_PARAMPROB_HEADER,
97278064Sume					    erroff + opt + 1 - opthead);
97378064Sume				return(-1);
97478064Sume			}
97578064Sume			optlen = IP6OPT_RTALERT_LEN;
97678064Sume			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
97778064Sume			*rtalertp = ntohs(rtalert_val);
97878064Sume			break;
97978064Sume		case IP6OPT_JUMBO:
98078064Sume			/* XXX may need check for alignment */
98162587Sitojun			if (hbhlen < IP6OPT_JUMBO_LEN) {
98262587Sitojun				ip6stat.ip6s_toosmall++;
98362587Sitojun				goto bad;
98462587Sitojun			}
98578064Sume			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
98678064Sume				/* XXX stat */
98778064Sume				icmp6_error(m, ICMP6_PARAM_PROB,
98878064Sume					    ICMP6_PARAMPROB_HEADER,
98978064Sume					    erroff + opt + 1 - opthead);
99078064Sume				return(-1);
99178064Sume			}
99262587Sitojun			optlen = IP6OPT_JUMBO_LEN;
99353541Sshin
99462587Sitojun			/*
99562587Sitojun			 * IPv6 packets that have non 0 payload length
99678064Sume			 * must not contain a jumbo payload option.
99762587Sitojun			 */
99862587Sitojun			ip6 = mtod(m, struct ip6_hdr *);
99962587Sitojun			if (ip6->ip6_plen) {
100062587Sitojun				ip6stat.ip6s_badoptions++;
100162587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
100262587Sitojun					    ICMP6_PARAMPROB_HEADER,
100378064Sume					    erroff + opt - opthead);
100462587Sitojun				return(-1);
100562587Sitojun			}
100653541Sshin
100762587Sitojun			/*
100862587Sitojun			 * We may see jumbolen in unaligned location, so
100962587Sitojun			 * we'd need to perform bcopy().
101062587Sitojun			 */
101162587Sitojun			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
101262587Sitojun			jumboplen = (u_int32_t)htonl(jumboplen);
101362587Sitojun
101462587Sitojun#if 1
101562587Sitojun			/*
101662587Sitojun			 * if there are multiple jumbo payload options,
101762587Sitojun			 * *plenp will be non-zero and the packet will be
101862587Sitojun			 * rejected.
101962587Sitojun			 * the behavior may need some debate in ipngwg -
102062587Sitojun			 * multiple options does not make sense, however,
102162587Sitojun			 * there's no explicit mention in specification.
102262587Sitojun			 */
102362587Sitojun			if (*plenp != 0) {
102462587Sitojun				ip6stat.ip6s_badoptions++;
102562587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
102662587Sitojun					    ICMP6_PARAMPROB_HEADER,
102778064Sume					    erroff + opt + 2 - opthead);
102862587Sitojun				return(-1);
102962587Sitojun			}
103062587Sitojun#endif
103162587Sitojun
103262587Sitojun			/*
103362587Sitojun			 * jumbo payload length must be larger than 65535.
103462587Sitojun			 */
103562587Sitojun			if (jumboplen <= IPV6_MAXPACKET) {
103662587Sitojun				ip6stat.ip6s_badoptions++;
103762587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
103862587Sitojun					    ICMP6_PARAMPROB_HEADER,
103978064Sume					    erroff + opt + 2 - opthead);
104062587Sitojun				return(-1);
104162587Sitojun			}
104262587Sitojun			*plenp = jumboplen;
104362587Sitojun
104462587Sitojun			break;
104578064Sume		default:		/* unknown option */
104678064Sume			if (hbhlen < IP6OPT_MINLEN) {
104778064Sume				ip6stat.ip6s_toosmall++;
104878064Sume				goto bad;
104978064Sume			}
105078064Sume			optlen = ip6_unknown_opt(opt, m,
105178064Sume			    erroff + opt - opthead);
105278064Sume			if (optlen == -1)
105378064Sume				return(-1);
105478064Sume			optlen += 2;
105578064Sume			break;
105653541Sshin		}
105753541Sshin	}
105853541Sshin
105953541Sshin	return(0);
106053541Sshin
106153541Sshin  bad:
106253541Sshin	m_freem(m);
106353541Sshin	return(-1);
106453541Sshin}
106553541Sshin
106653541Sshin/*
106753541Sshin * Unknown option processing.
106853541Sshin * The third argument `off' is the offset from the IPv6 header to the option,
106953541Sshin * which is necessary if the IPv6 header the and option header and IPv6 header
107053541Sshin * is not continuous in order to return an ICMPv6 error.
107153541Sshin */
107253541Sshinint
107353541Sshinip6_unknown_opt(optp, m, off)
107453541Sshin	u_int8_t *optp;
107553541Sshin	struct mbuf *m;
107653541Sshin	int off;
107753541Sshin{
107853541Sshin	struct ip6_hdr *ip6;
107953541Sshin
108078064Sume	switch (IP6OPT_TYPE(*optp)) {
108178064Sume	case IP6OPT_TYPE_SKIP: /* ignore the option */
108278064Sume		return((int)*(optp + 1));
108378064Sume	case IP6OPT_TYPE_DISCARD:	/* silently discard */
108478064Sume		m_freem(m);
108578064Sume		return(-1);
108678064Sume	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
108778064Sume		ip6stat.ip6s_badoptions++;
108878064Sume		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
108978064Sume		return(-1);
109078064Sume	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
109178064Sume		ip6stat.ip6s_badoptions++;
109278064Sume		ip6 = mtod(m, struct ip6_hdr *);
109378064Sume		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
109478064Sume		    (m->m_flags & (M_BCAST|M_MCAST)))
109578064Sume			m_freem(m);
109678064Sume		else
109778064Sume			icmp6_error(m, ICMP6_PARAM_PROB,
109878064Sume				    ICMP6_PARAMPROB_OPTION, off);
109978064Sume		return(-1);
110053541Sshin	}
110153541Sshin
110253541Sshin	m_freem(m);		/* XXX: NOTREACHED */
110353541Sshin	return(-1);
110453541Sshin}
110553541Sshin
110653541Sshin/*
110762587Sitojun * Create the "control" list for this pcb.
110878064Sume * The function will not modify mbuf chain at all.
110962587Sitojun *
111078064Sume * with KAME mbuf chain restriction:
111162587Sitojun * The routine will be called from upper layer handlers like tcp6_input().
111262587Sitojun * Thus the routine assumes that the caller (tcp6_input) have already
111362587Sitojun * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
111462587Sitojun * very first mbuf on the mbuf chain.
111553541Sshin */
111653541Sshinvoid
111753541Sshinip6_savecontrol(in6p, mp, ip6, m)
111878064Sume	struct inpcb *in6p;
111978064Sume	struct mbuf **mp;
112078064Sume	struct ip6_hdr *ip6;
112178064Sume	struct mbuf *m;
112253541Sshin{
112397676Sume#if __FreeBSD_version >= 500000
112493593Sjhb	struct thread *td = curthread;	/* XXX */
112593593Sjhb#else
112693593Sjhb	struct proc *td = curproc;	/* XXX */
112793593Sjhb#endif
112878064Sume	int privileged = 0;
112978064Sume	int rthdr_exist = 0;
113053541Sshin
113178064Sume
113293593Sjhb	if (td && !suser(td))
113378064Sume 		privileged++;
113453541Sshin
113578064Sume#ifdef SO_TIMESTAMP
113678064Sume	if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
113753541Sshin		struct timeval tv;
113853541Sshin
113953541Sshin		microtime(&tv);
114053541Sshin		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
114178064Sume				      SCM_TIMESTAMP, SOL_SOCKET);
114278064Sume		if (*mp) {
114353541Sshin			mp = &(*mp)->m_next;
114478064Sume		}
114597658Stanimura	}
114662587Sitojun#endif
114762587Sitojun
114853541Sshin	/* RFC 2292 sec. 5 */
114978064Sume	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
115053541Sshin		struct in6_pktinfo pi6;
115153541Sshin		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
115253541Sshin		if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
115353541Sshin			pi6.ipi6_addr.s6_addr16[1] = 0;
115453541Sshin		pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
115553541Sshin					? m->m_pkthdr.rcvif->if_index
115653541Sshin					: 0;
115753541Sshin		*mp = sbcreatecontrol((caddr_t) &pi6,
115853541Sshin			sizeof(struct in6_pktinfo), IPV6_PKTINFO,
115953541Sshin			IPPROTO_IPV6);
116053541Sshin		if (*mp)
116153541Sshin			mp = &(*mp)->m_next;
116253541Sshin	}
116378064Sume
116478064Sume	if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
116553541Sshin		int hlim = ip6->ip6_hlim & 0xff;
116653541Sshin		*mp = sbcreatecontrol((caddr_t) &hlim,
116753541Sshin			sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
116853541Sshin		if (*mp)
116953541Sshin			mp = &(*mp)->m_next;
117053541Sshin	}
117153541Sshin
117253541Sshin	/*
117353541Sshin	 * IPV6_HOPOPTS socket option. We require super-user privilege
117453541Sshin	 * for the option, but it might be too strict, since there might
117553541Sshin	 * be some hop-by-hop options which can be returned to normal user.
117653541Sshin	 * See RFC 2292 section 6.
117753541Sshin	 */
117878064Sume	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) {
117953541Sshin		/*
118053541Sshin		 * Check if a hop-by-hop options header is contatined in the
118153541Sshin		 * received packet, and if so, store the options as ancillary
118253541Sshin		 * data. Note that a hop-by-hop options header must be
118353541Sshin		 * just after the IPv6 header, which fact is assured through
118453541Sshin		 * the IPv6 input processing.
118553541Sshin		 */
118653541Sshin		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
118753541Sshin		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
118862587Sitojun			struct ip6_hbh *hbh;
118978064Sume			int hbhlen = 0;
119078064Sume#ifdef PULLDOWN_TEST
119178064Sume			struct mbuf *ext;
119278064Sume#endif
119353541Sshin
119462587Sitojun#ifndef PULLDOWN_TEST
119562587Sitojun			hbh = (struct ip6_hbh *)(ip6 + 1);
119662587Sitojun			hbhlen = (hbh->ip6h_len + 1) << 3;
119762587Sitojun#else
119878064Sume			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
119978064Sume			    ip6->ip6_nxt);
120078064Sume			if (ext == NULL) {
120162587Sitojun				ip6stat.ip6s_tooshort++;
120262587Sitojun				return;
120362587Sitojun			}
120478064Sume			hbh = mtod(ext, struct ip6_hbh *);
120562587Sitojun			hbhlen = (hbh->ip6h_len + 1) << 3;
120678064Sume			if (hbhlen != ext->m_len) {
120778064Sume				m_freem(ext);
120862587Sitojun				ip6stat.ip6s_tooshort++;
120962587Sitojun				return;
121062587Sitojun			}
121162587Sitojun#endif
121262587Sitojun
121353541Sshin			/*
121453541Sshin			 * XXX: We copy whole the header even if a jumbo
121553541Sshin			 * payload option is included, which option is to
121653541Sshin			 * be removed before returning in the RFC 2292.
121778064Sume			 * Note: this constraint is removed in 2292bis.
121853541Sshin			 */
121962587Sitojun			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
122053541Sshin					      IPV6_HOPOPTS, IPPROTO_IPV6);
122153541Sshin			if (*mp)
122253541Sshin				mp = &(*mp)->m_next;
122378064Sume#ifdef PULLDOWN_TEST
122478064Sume			m_freem(ext);
122578064Sume#endif
122653541Sshin		}
122753541Sshin	}
122853541Sshin
122953541Sshin	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
123078064Sume	if ((in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) {
123178064Sume		int proto, off, nxt;
123278064Sume
123378064Sume		/*
123478064Sume		 * go through the header chain to see if a routing header is
123578064Sume		 * contained in the packet. We need this information to store
123678064Sume		 * destination options headers (if any) properly.
123778064Sume		 * XXX: performance issue. We should record this info when
123878064Sume		 * processing extension headers in incoming routine.
123978064Sume		 * (todo) use m_aux?
124078064Sume		 */
124178064Sume		proto = IPPROTO_IPV6;
124278064Sume		off = 0;
124378064Sume		nxt = -1;
124478064Sume		while (1) {
124578064Sume			int newoff;
124678064Sume
124778064Sume			newoff = ip6_nexthdr(m, off, proto, &nxt);
124878064Sume			if (newoff < 0)
124978064Sume				break;
125078064Sume			if (newoff < off) /* invalid, check for safety */
125178064Sume				break;
125278064Sume			if ((proto = nxt) == IPPROTO_ROUTING) {
125378064Sume				rthdr_exist = 1;
125478064Sume				break;
125578064Sume			}
125678064Sume			off = newoff;
125778064Sume		}
125878064Sume	}
125978064Sume
126078064Sume	if ((in6p->in6p_flags &
126178064Sume	     (IN6P_RTHDR | IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) {
126253541Sshin		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
126378064Sume		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
126453541Sshin
126553541Sshin		/*
126653541Sshin		 * Search for destination options headers or routing
126753541Sshin		 * header(s) through the header chain, and stores each
126853541Sshin		 * header as ancillary data.
126953541Sshin		 * Note that the order of the headers remains in
127053541Sshin		 * the chain of ancillary data.
127153541Sshin		 */
127278064Sume		while (1) {	/* is explicit loop prevention necessary? */
127378064Sume			struct ip6_ext *ip6e = NULL;
127462587Sitojun			int elen;
127578064Sume#ifdef PULLDOWN_TEST
127678064Sume			struct mbuf *ext = NULL;
127778064Sume#endif
127853541Sshin
127978064Sume			/*
128078064Sume			 * if it is not an extension header, don't try to
128178064Sume			 * pull it from the chain.
128278064Sume			 */
128378064Sume			switch (nxt) {
128478064Sume			case IPPROTO_DSTOPTS:
128578064Sume			case IPPROTO_ROUTING:
128678064Sume			case IPPROTO_HOPOPTS:
128778064Sume			case IPPROTO_AH: /* is it possible? */
128878064Sume				break;
128978064Sume			default:
129078064Sume				goto loopend;
129178064Sume			}
129278064Sume
129362587Sitojun#ifndef PULLDOWN_TEST
129478064Sume			if (off + sizeof(*ip6e) > m->m_len)
129578064Sume				goto loopend;
129662587Sitojun			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
129762587Sitojun			if (nxt == IPPROTO_AH)
129862587Sitojun				elen = (ip6e->ip6e_len + 2) << 2;
129962587Sitojun			else
130062587Sitojun				elen = (ip6e->ip6e_len + 1) << 3;
130178064Sume			if (off + elen > m->m_len)
130278064Sume				goto loopend;
130362587Sitojun#else
130478064Sume			ext = ip6_pullexthdr(m, off, nxt);
130578064Sume			if (ext == NULL) {
130662587Sitojun				ip6stat.ip6s_tooshort++;
130762587Sitojun				return;
130862587Sitojun			}
130978064Sume			ip6e = mtod(ext, struct ip6_ext *);
131062587Sitojun			if (nxt == IPPROTO_AH)
131162587Sitojun				elen = (ip6e->ip6e_len + 2) << 2;
131262587Sitojun			else
131362587Sitojun				elen = (ip6e->ip6e_len + 1) << 3;
131478064Sume			if (elen != ext->m_len) {
131578064Sume				m_freem(ext);
131662587Sitojun				ip6stat.ip6s_tooshort++;
131762587Sitojun				return;
131862587Sitojun			}
131962587Sitojun#endif
132062587Sitojun
132178064Sume			switch (nxt) {
132278064Sume			case IPPROTO_DSTOPTS:
132378064Sume				if ((in6p->in6p_flags & IN6P_DSTOPTS) == 0)
132478064Sume					break;
132553541Sshin
132678064Sume				/*
132778064Sume				 * We also require super-user privilege for
132878064Sume				 * the option.
132978064Sume				 * See the comments on IN6_HOPOPTS.
133078064Sume				 */
133178064Sume				if (!privileged)
133278064Sume					break;
133353541Sshin
133478064Sume				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
133578064Sume						      IPV6_DSTOPTS,
133678064Sume						      IPPROTO_IPV6);
133778064Sume				if (*mp)
133878064Sume					mp = &(*mp)->m_next;
133978064Sume				break;
134078064Sume			case IPPROTO_ROUTING:
134178064Sume				if (!in6p->in6p_flags & IN6P_RTHDR)
134278064Sume					break;
134353541Sshin
134478064Sume				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
134578064Sume						      IPV6_RTHDR,
134678064Sume						      IPPROTO_IPV6);
134778064Sume				if (*mp)
134878064Sume					mp = &(*mp)->m_next;
134978064Sume				break;
135078064Sume			case IPPROTO_HOPOPTS:
135178064Sume			case IPPROTO_AH: /* is it possible? */
135278064Sume				break;
135353541Sshin
135478064Sume			default:
135578064Sume				/*
135678064Sume			 	 * other cases have been filtered in the above.
135778064Sume				 * none will visit this case.  here we supply
135878064Sume				 * the code just in case (nxt overwritten or
135978064Sume				 * other cases).
136078064Sume				 */
136178064Sume#ifdef PULLDOWN_TEST
136278064Sume				m_freem(ext);
136378064Sume#endif
136478064Sume				goto loopend;
136553541Sshin
136653541Sshin			}
136753541Sshin
136853541Sshin			/* proceed with the next header. */
136962587Sitojun			off += elen;
137053541Sshin			nxt = ip6e->ip6e_nxt;
137178064Sume			ip6e = NULL;
137278064Sume#ifdef PULLDOWN_TEST
137378064Sume			m_freem(ext);
137478064Sume			ext = NULL;
137578064Sume#endif
137653541Sshin		}
137753541Sshin	  loopend:
137878064Sume		;
137953541Sshin	}
138078064Sume
138178064Sume}
138278064Sume
138378064Sume#ifdef PULLDOWN_TEST
138478064Sume/*
138578064Sume * pull single extension header from mbuf chain.  returns single mbuf that
138678064Sume * contains the result, or NULL on error.
138778064Sume */
138878064Sumestatic struct mbuf *
138978064Sumeip6_pullexthdr(m, off, nxt)
139078064Sume	struct mbuf *m;
139178064Sume	size_t off;
139278064Sume	int nxt;
139378064Sume{
139478064Sume	struct ip6_ext ip6e;
139578064Sume	size_t elen;
139678064Sume	struct mbuf *n;
139778064Sume
139878064Sume#ifdef DIAGNOSTIC
139978064Sume	switch (nxt) {
140078064Sume	case IPPROTO_DSTOPTS:
140178064Sume	case IPPROTO_ROUTING:
140278064Sume	case IPPROTO_HOPOPTS:
140378064Sume	case IPPROTO_AH: /* is it possible? */
140478064Sume		break;
140578064Sume	default:
140678064Sume		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
140753541Sshin	}
140878064Sume#endif
140978064Sume
141078064Sume	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
141178064Sume	if (nxt == IPPROTO_AH)
141278064Sume		elen = (ip6e.ip6e_len + 2) << 2;
141378064Sume	else
141478064Sume		elen = (ip6e.ip6e_len + 1) << 3;
141578064Sume
1416111119Simp	MGET(n, M_DONTWAIT, MT_DATA);
141778064Sume	if (n && elen >= MLEN) {
1418111119Simp		MCLGET(n, M_DONTWAIT);
141978064Sume		if ((n->m_flags & M_EXT) == 0) {
142078064Sume			m_free(n);
142178064Sume			n = NULL;
142278064Sume		}
142353541Sshin	}
142478064Sume	if (!n)
142578064Sume		return NULL;
142662587Sitojun
142778064Sume	n->m_len = 0;
142878064Sume	if (elen >= M_TRAILINGSPACE(n)) {
142978064Sume		m_free(n);
143078064Sume		return NULL;
143178064Sume	}
143278064Sume
143378064Sume	m_copydata(m, off, elen, mtod(n, caddr_t));
143478064Sume	n->m_len = elen;
143578064Sume	return n;
143653541Sshin}
143778064Sume#endif
143853541Sshin
143953541Sshin/*
144053541Sshin * Get pointer to the previous header followed by the header
144153541Sshin * currently processed.
144253541Sshin * XXX: This function supposes that
144353541Sshin *	M includes all headers,
144453541Sshin *	the next header field and the header length field of each header
144553541Sshin *	are valid, and
144653541Sshin *	the sum of each header length equals to OFF.
144753541Sshin * Because of these assumptions, this function must be called very
144853541Sshin * carefully. Moreover, it will not be used in the near future when
144953541Sshin * we develop `neater' mechanism to process extension headers.
145053541Sshin */
145153541Sshinchar *
145253541Sshinip6_get_prevhdr(m, off)
145353541Sshin	struct mbuf *m;
145453541Sshin	int off;
145553541Sshin{
145653541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
145753541Sshin
145853541Sshin	if (off == sizeof(struct ip6_hdr))
145953541Sshin		return(&ip6->ip6_nxt);
146053541Sshin	else {
146153541Sshin		int len, nxt;
146253541Sshin		struct ip6_ext *ip6e = NULL;
146353541Sshin
146453541Sshin		nxt = ip6->ip6_nxt;
146553541Sshin		len = sizeof(struct ip6_hdr);
146653541Sshin		while (len < off) {
146753541Sshin			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
146853541Sshin
146978064Sume			switch (nxt) {
147053541Sshin			case IPPROTO_FRAGMENT:
147153541Sshin				len += sizeof(struct ip6_frag);
147253541Sshin				break;
147353541Sshin			case IPPROTO_AH:
147453541Sshin				len += (ip6e->ip6e_len + 2) << 2;
147553541Sshin				break;
147653541Sshin			default:
147753541Sshin				len += (ip6e->ip6e_len + 1) << 3;
147853541Sshin				break;
147953541Sshin			}
148053541Sshin			nxt = ip6e->ip6e_nxt;
148153541Sshin		}
148253541Sshin		if (ip6e)
148353541Sshin			return(&ip6e->ip6e_nxt);
148453541Sshin		else
148553541Sshin			return NULL;
148653541Sshin	}
148753541Sshin}
148853541Sshin
148953541Sshin/*
149062587Sitojun * get next header offset.  m will be retained.
149162587Sitojun */
149262587Sitojunint
149362587Sitojunip6_nexthdr(m, off, proto, nxtp)
149462587Sitojun	struct mbuf *m;
149562587Sitojun	int off;
149662587Sitojun	int proto;
149762587Sitojun	int *nxtp;
149862587Sitojun{
149962587Sitojun	struct ip6_hdr ip6;
150062587Sitojun	struct ip6_ext ip6e;
150162587Sitojun	struct ip6_frag fh;
150262587Sitojun
150362587Sitojun	/* just in case */
150462587Sitojun	if (m == NULL)
150562587Sitojun		panic("ip6_nexthdr: m == NULL");
150662587Sitojun	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
150762587Sitojun		return -1;
150862587Sitojun
150962587Sitojun	switch (proto) {
151062587Sitojun	case IPPROTO_IPV6:
151162587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6))
151262587Sitojun			return -1;
151362587Sitojun		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
151462587Sitojun		if (nxtp)
151562587Sitojun			*nxtp = ip6.ip6_nxt;
151662587Sitojun		off += sizeof(ip6);
151762587Sitojun		return off;
151862587Sitojun
151962587Sitojun	case IPPROTO_FRAGMENT:
152062587Sitojun		/*
152162587Sitojun		 * terminate parsing if it is not the first fragment,
152262587Sitojun		 * it does not make sense to parse through it.
152362587Sitojun		 */
152462587Sitojun		if (m->m_pkthdr.len < off + sizeof(fh))
152562587Sitojun			return -1;
152662587Sitojun		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
152762587Sitojun		if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
152862587Sitojun			return -1;
152962587Sitojun		if (nxtp)
153062587Sitojun			*nxtp = fh.ip6f_nxt;
153162587Sitojun		off += sizeof(struct ip6_frag);
153262587Sitojun		return off;
153362587Sitojun
153462587Sitojun	case IPPROTO_AH:
153562587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6e))
153662587Sitojun			return -1;
153762587Sitojun		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
153862587Sitojun		if (nxtp)
153962587Sitojun			*nxtp = ip6e.ip6e_nxt;
154062587Sitojun		off += (ip6e.ip6e_len + 2) << 2;
154162587Sitojun		return off;
154262587Sitojun
154362587Sitojun	case IPPROTO_HOPOPTS:
154462587Sitojun	case IPPROTO_ROUTING:
154562587Sitojun	case IPPROTO_DSTOPTS:
154662587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6e))
154762587Sitojun			return -1;
154862587Sitojun		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
154962587Sitojun		if (nxtp)
155062587Sitojun			*nxtp = ip6e.ip6e_nxt;
155162587Sitojun		off += (ip6e.ip6e_len + 1) << 3;
155262587Sitojun		return off;
155362587Sitojun
155462587Sitojun	case IPPROTO_NONE:
155562587Sitojun	case IPPROTO_ESP:
155662587Sitojun	case IPPROTO_IPCOMP:
155762587Sitojun		/* give up */
155862587Sitojun		return -1;
155962587Sitojun
156062587Sitojun	default:
156162587Sitojun		return -1;
156262587Sitojun	}
156362587Sitojun
156462587Sitojun	return -1;
156562587Sitojun}
156662587Sitojun
156762587Sitojun/*
156862587Sitojun * get offset for the last header in the chain.  m will be kept untainted.
156962587Sitojun */
157062587Sitojunint
157162587Sitojunip6_lasthdr(m, off, proto, nxtp)
157262587Sitojun	struct mbuf *m;
157362587Sitojun	int off;
157462587Sitojun	int proto;
157562587Sitojun	int *nxtp;
157662587Sitojun{
157762587Sitojun	int newoff;
157862587Sitojun	int nxt;
157962587Sitojun
158062587Sitojun	if (!nxtp) {
158162587Sitojun		nxt = -1;
158262587Sitojun		nxtp = &nxt;
158362587Sitojun	}
158462587Sitojun	while (1) {
158562587Sitojun		newoff = ip6_nexthdr(m, off, proto, nxtp);
158662587Sitojun		if (newoff < 0)
158762587Sitojun			return off;
158862587Sitojun		else if (newoff < off)
158962587Sitojun			return -1;	/* invalid */
159062587Sitojun		else if (newoff == off)
159162587Sitojun			return newoff;
159262587Sitojun
159362587Sitojun		off = newoff;
159462587Sitojun		proto = *nxtp;
159562587Sitojun	}
159662587Sitojun}
159762587Sitojun
1598105194Ssamstruct ip6aux *
159978064Sumeip6_addaux(m)
160078064Sume	struct mbuf *m;
160178064Sume{
1602105194Ssam	struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1603105194Ssam	if (!tag) {
1604105194Ssam		tag = m_tag_get(PACKET_TAG_IPV6_INPUT,
1605105194Ssam				sizeof (struct ip6aux),
1606112130Ssam				M_NOWAIT);
1607105194Ssam		if (tag)
1608105194Ssam			m_tag_prepend(m, tag);
160978064Sume	}
1610105194Ssam	if (tag)
1611105194Ssam		bzero(tag+1, sizeof (struct ip6aux));
1612105194Ssam	return tag ? (struct ip6aux*)(tag+1) : NULL;
161378064Sume}
161478064Sume
1615105194Ssamstruct ip6aux *
161678064Sumeip6_findaux(m)
161778064Sume	struct mbuf *m;
161878064Sume{
1619105194Ssam	struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1620105194Ssam	return tag ? (struct ip6aux*)(tag+1) : NULL;
162178064Sume}
162278064Sume
162378064Sumevoid
162478064Sumeip6_delaux(m)
162578064Sume	struct mbuf *m;
162678064Sume{
1627105194Ssam	struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1628105194Ssam	if (tag)
1629105194Ssam		m_tag_delete(m, tag);
163078064Sume}
163178064Sume
163262587Sitojun/*
163353541Sshin * System control for IP6
163453541Sshin */
163553541Sshin
163653541Sshinu_char	inet6ctlerrmap[PRC_NCMDS] = {
163753541Sshin	0,		0,		0,		0,
163853541Sshin	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
163953541Sshin	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
164053541Sshin	EMSGSIZE,	EHOSTUNREACH,	0,		0,
164153541Sshin	0,		0,		0,		0,
164253541Sshin	ENOPROTOOPT
164353541Sshin};
1644