ip6_input.c revision 120856
162587Sitojun/*	$FreeBSD: head/sys/netinet6/ip6_input.c 120856 2003-10-06 14:02:09Z ume $	*/
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"
73120649Sume#include "opt_random_ip_id.h"
7455009Sshin
7553541Sshin#include <sys/param.h>
7653541Sshin#include <sys/systm.h>
7778064Sume#include <sys/malloc.h>
7853541Sshin#include <sys/mbuf.h>
7983366Sjulian#include <sys/proc.h>
8053541Sshin#include <sys/domain.h>
8153541Sshin#include <sys/protosw.h>
8253541Sshin#include <sys/socket.h>
8353541Sshin#include <sys/socketvar.h>
8453541Sshin#include <sys/errno.h>
8553541Sshin#include <sys/time.h>
8653541Sshin#include <sys/kernel.h>
8753541Sshin#include <sys/syslog.h>
8853541Sshin
8953541Sshin#include <net/if.h>
9053541Sshin#include <net/if_types.h>
9153541Sshin#include <net/if_dl.h>
9253541Sshin#include <net/route.h>
9353541Sshin#include <net/netisr.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
120105199Ssam#ifdef FAST_IPSEC
121105199Ssam#include <netipsec/ipsec.h>
122105199Ssam#include <netipsec/ipsec6.h>
123105199Ssam#define	IPSEC
124105199Ssam#endif /* FAST_IPSEC */
125105199Ssam
12653541Sshin#include <netinet6/ip6_fw.h>
12753541Sshin
12853541Sshin#include <netinet6/ip6protosw.h>
12953541Sshin
13053541Sshin#include <net/net_osdep.h>
13153541Sshin
13262587Sitojunextern struct domain inet6domain;
13353541Sshin
13462587Sitojunu_char ip6_protox[IPPROTO_MAX];
135111888Sjlemonstatic struct ifqueue ip6intrq;
13662587Sitojunstatic int ip6qmaxlen = IFQ_MAXLEN;
13762587Sitojunstruct in6_ifaddr *in6_ifaddr;
13853541Sshin
13978064Sumeextern struct callout in6_tmpaddrtimer_ch;
14078064Sume
14162587Sitojunint ip6_forward_srcrt;			/* XXX */
14262587Sitojunint ip6_sourcecheck;			/* XXX */
14362587Sitojunint ip6_sourcecheck_interval;		/* XXX */
14453541Sshin
14578064Sumeint ip6_ours_check_algorithm;
14678064Sume
147120386Ssam#ifdef PFIL_HOOKS
148120386Ssamstruct pfil_head inet6_pfil_hook;
149120386Ssam#endif
15078064Sume
15153541Sshin/* firewall hooks */
15262587Sitojunip6_fw_chk_t *ip6_fw_chk_ptr;
15362587Sitojunip6_fw_ctl_t *ip6_fw_ctl_ptr;
15466303Sumeint ip6_fw_enable = 1;
15553541Sshin
15662587Sitojunstruct ip6stat ip6stat;
15753541Sshin
15862587Sitojunstatic void ip6_init2 __P((void *));
159105194Ssamstatic struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *));
16062587Sitojunstatic int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
16162587Sitojun#ifdef PULLDOWN_TEST
16262587Sitojunstatic struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int));
16353541Sshin#endif
16453541Sshin
16578064Sume
16653541Sshin/*
16753541Sshin * IP6 initialization: fill in IP6 protocol switch table.
16853541Sshin * All protocols not implemented in kernel go to raw IP6 protocol handler.
16953541Sshin */
17053541Sshinvoid
17153541Sshinip6_init()
17253541Sshin{
17378064Sume	struct ip6protosw *pr;
17478064Sume	int i;
17553541Sshin
17678064Sume#ifdef DIAGNOSTIC
17778064Sume	if (sizeof(struct protosw) != sizeof(struct ip6protosw))
17878064Sume		panic("sizeof(protosw) != sizeof(ip6protosw)");
17978064Sume#endif
18053541Sshin	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
18153541Sshin	if (pr == 0)
18253541Sshin		panic("ip6_init");
18353541Sshin	for (i = 0; i < IPPROTO_MAX; i++)
18453541Sshin		ip6_protox[i] = pr - inet6sw;
18553541Sshin	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
18653541Sshin	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
18753541Sshin		if (pr->pr_domain->dom_family == PF_INET6 &&
18853541Sshin		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
18953541Sshin			ip6_protox[pr->pr_protocol] = pr - inet6sw;
190120386Ssam#ifdef PFIL_HOOKS
191120386Ssam	inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
192120386Ssam	inet6_pfil_hook.ph_af = AF_INET6;
193120386Ssam	if ((i = pfil_head_register(&inet6_pfil_hook)) != 0)
194120386Ssam		printf("%s: WARNING: unable to register pfil hook, "
195120386Ssam			"error %d\n", __func__, i);
196120386Ssam#endif /* PFIL_HOOKS */
19753541Sshin	ip6intrq.ifq_maxlen = ip6qmaxlen;
19893818Sjhb	mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF);
199111888Sjlemon	netisr_register(NETISR_IPV6, ip6_input, &ip6intrq);
20053541Sshin	nd6_init();
20153541Sshin	frag6_init();
202120649Sume#ifndef RANDOM_IP_ID
203120648Sume	ip6_flow_seq = arc4random();
204120649Sume#endif
205120648Sume	ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
20653541Sshin}
20753541Sshin
20853541Sshinstatic void
20953541Sshinip6_init2(dummy)
21053541Sshin	void *dummy;
21153541Sshin{
21253541Sshin
21353541Sshin	/*
21453541Sshin	 * to route local address of p2p link to loopback,
21553541Sshin	 * assign loopback address first.
21653541Sshin	 */
21762587Sitojun	in6_ifattach(&loif[0], NULL);
21853541Sshin
21953541Sshin	/* nd6_timer_init */
22078064Sume	callout_init(&nd6_timer_ch, 0);
22178064Sume	callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
22278064Sume
22353541Sshin	/* router renumbering prefix list maintenance */
22478064Sume	callout_init(&in6_rr_timer_ch, 0);
22578064Sume	callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL);
22678064Sume
22778064Sume	/* timer for regeneranation of temporary addresses randomize ID */
22878064Sume	callout_reset(&in6_tmpaddrtimer_ch,
22978064Sume		      (ip6_temp_preferred_lifetime - ip6_desync_factor -
23078064Sume		       ip6_temp_regen_advance) * hz,
23178064Sume		      in6_tmpaddrtimer, NULL);
23253541Sshin}
23353541Sshin
23453541Sshin/* cheat */
23555009Sshin/* This must be after route_init(), which is now SI_ORDER_THIRD */
23655009SshinSYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
23753541Sshin
23853541Sshinextern struct	route_in6 ip6_forward_rt;
23953541Sshin
24053541Sshinvoid
24153541Sshinip6_input(m)
24253541Sshin	struct mbuf *m;
24353541Sshin{
24453541Sshin	struct ip6_hdr *ip6;
24553541Sshin	int off = sizeof(struct ip6_hdr), nest;
24653541Sshin	u_int32_t plen;
24753541Sshin	u_int32_t rtalert = ~0;
24853541Sshin	int nxt, ours = 0;
24953541Sshin	struct ifnet *deliverifp = NULL;
25053541Sshin
25153541Sshin#ifdef IPSEC
25253541Sshin	/*
25353541Sshin	 * should the inner packet be considered authentic?
25453541Sshin	 * see comment in ah4_input().
25553541Sshin	 */
25653541Sshin	if (m) {
25753541Sshin		m->m_flags &= ~M_AUTHIPHDR;
25853541Sshin		m->m_flags &= ~M_AUTHIPDGM;
25953541Sshin	}
26053541Sshin#endif
26153541Sshin
26253541Sshin	/*
26378064Sume	 * make sure we don't have onion peering information into m_aux.
26478064Sume	 */
26578064Sume	ip6_delaux(m);
26678064Sume
26778064Sume	/*
26895023Ssuz	 * mbuf statistics
26953541Sshin	 */
27053541Sshin	if (m->m_flags & M_EXT) {
27153541Sshin		if (m->m_next)
27253541Sshin			ip6stat.ip6s_mext2m++;
27353541Sshin		else
27453541Sshin			ip6stat.ip6s_mext1++;
27553541Sshin	} else {
27678064Sume#define M2MMAX	(sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
27753541Sshin		if (m->m_next) {
27853541Sshin			if (m->m_flags & M_LOOP) {
27995023Ssuz				ip6stat.ip6s_m2m[loif[0].if_index]++;	/* XXX */
28078064Sume			} else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
28153541Sshin				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
28253541Sshin			else
28353541Sshin				ip6stat.ip6s_m2m[0]++;
28453541Sshin		} else
28553541Sshin			ip6stat.ip6s_m1++;
28678064Sume#undef M2MMAX
28753541Sshin	}
28853541Sshin
28953541Sshin	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
29053541Sshin	ip6stat.ip6s_total++;
29153541Sshin
29262587Sitojun#ifndef PULLDOWN_TEST
29374336Skuriyama	/*
29474336Skuriyama	 * L2 bridge code and some other code can return mbuf chain
29574336Skuriyama	 * that does not conform to KAME requirement.  too bad.
29674336Skuriyama	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
29774336Skuriyama	 */
29874336Skuriyama	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
29974336Skuriyama		struct mbuf *n;
30074336Skuriyama
301111119Simp		MGETHDR(n, M_DONTWAIT, MT_HEADER);
30277003Sume		if (n)
303108466Ssam			M_MOVE_PKTHDR(n, m);
304108825Ssam		if (n && n->m_pkthdr.len > MHLEN) {
305111119Simp			MCLGET(n, M_DONTWAIT);
30674336Skuriyama			if ((n->m_flags & M_EXT) == 0) {
30774336Skuriyama				m_freem(n);
30874336Skuriyama				n = NULL;
30974336Skuriyama			}
31074336Skuriyama		}
31195023Ssuz		if (n == NULL) {
31277003Sume			m_freem(m);
31374336Skuriyama			return;	/*ENOBUFS*/
31477003Sume		}
31574336Skuriyama
316108825Ssam		m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
317108825Ssam		n->m_len = n->m_pkthdr.len;
31874336Skuriyama		m_freem(m);
31974336Skuriyama		m = n;
32074336Skuriyama	}
32153541Sshin	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
32262587Sitojun#endif
32353541Sshin
32453541Sshin	if (m->m_len < sizeof(struct ip6_hdr)) {
32553541Sshin		struct ifnet *inifp;
32653541Sshin		inifp = m->m_pkthdr.rcvif;
32753541Sshin		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
32853541Sshin			ip6stat.ip6s_toosmall++;
32953541Sshin			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
33053541Sshin			return;
33153541Sshin		}
33253541Sshin	}
33353541Sshin
33453541Sshin	ip6 = mtod(m, struct ip6_hdr *);
33553541Sshin
33653541Sshin	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
33753541Sshin		ip6stat.ip6s_badvers++;
33853541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
33953541Sshin		goto bad;
34053541Sshin	}
34153541Sshin
34264060Sdarrenr#ifdef PFIL_HOOKS
34364060Sdarrenr	/*
344120386Ssam	 * Run through list of hooks for input packets.
34564060Sdarrenr	 */
346120386Ssam	if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN))
347120386Ssam		return;
348120386Ssam	if (m == NULL)			/* consumed by filter */
349120386Ssam		return;
350120386Ssam	ip6 = mtod(m, struct ip6_hdr *);
35164060Sdarrenr#endif /* PFIL_HOOKS */
35264060Sdarrenr
35353541Sshin	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
35453541Sshin
35553541Sshin	/*
35653541Sshin	 * Check with the firewall...
35753541Sshin	 */
35866303Sume	if (ip6_fw_enable && ip6_fw_chk_ptr) {
35953541Sshin		u_short port = 0;
36053541Sshin		/* If ipfw says divert, we have to just drop packet */
36153541Sshin		/* use port as a dummy argument */
36253541Sshin		if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
36353541Sshin			m_freem(m);
36453541Sshin			m = NULL;
36553541Sshin		}
36653541Sshin		if (!m)
36753541Sshin			return;
36853541Sshin	}
36953541Sshin
37053541Sshin	/*
37178064Sume	 * Check against address spoofing/corruption.
37253541Sshin	 */
37353541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
37453541Sshin	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
37578064Sume		/*
37678064Sume		 * XXX: "badscope" is not very suitable for a multicast source.
37778064Sume		 */
37853541Sshin		ip6stat.ip6s_badscope++;
37953541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
38053541Sshin		goto bad;
38153541Sshin	}
38278064Sume	if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
38378064Sume	     IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) &&
38478064Sume	    (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
38578064Sume		ip6stat.ip6s_badscope++;
38678064Sume		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
38778064Sume		goto bad;
38878064Sume	}
38995023Ssuz
39062587Sitojun	/*
39178064Sume	 * The following check is not documented in specs.  A malicious
39278064Sume	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
39378064Sume	 * and bypass security checks (act as if it was from 127.0.0.1 by using
39478064Sume	 * IPv6 src ::ffff:127.0.0.1).	Be cautious.
39578064Sume	 *
39678064Sume	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
39778064Sume	 * support IPv4-less kernel compilation, we cannot support SIIT
39878064Sume	 * environment at all.  So, it makes more sense for us to reject any
39978064Sume	 * malicious packets for non-SIIT environment, than try to do a
40078064Sume	 * partical support for SIIT environment.
40162587Sitojun	 */
40278064Sume	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
40378064Sume	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
40478064Sume		ip6stat.ip6s_badscope++;
40578064Sume		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
40678064Sume		goto bad;
40778064Sume	}
40862587Sitojun#if 0
40962587Sitojun	/*
41062587Sitojun	 * Reject packets with IPv4 compatible addresses (auto tunnel).
41162587Sitojun	 *
41262587Sitojun	 * The code forbids auto tunnel relay case in RFC1933 (the check is
41362587Sitojun	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
41462587Sitojun	 * is revised to forbid relaying case.
41562587Sitojun	 */
41662587Sitojun	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
41762587Sitojun	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
41862587Sitojun		ip6stat.ip6s_badscope++;
41962587Sitojun		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
42062587Sitojun		goto bad;
42162587Sitojun	}
42262587Sitojun#endif
42375246Sume
42478064Sume	/* drop packets if interface ID portion is already filled */
42578064Sume	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
42678064Sume		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
42778064Sume		    ip6->ip6_src.s6_addr16[1]) {
42853541Sshin			ip6stat.ip6s_badscope++;
42953541Sshin			goto bad;
43053541Sshin		}
43178064Sume		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
43278064Sume		    ip6->ip6_dst.s6_addr16[1]) {
43378064Sume			ip6stat.ip6s_badscope++;
43478064Sume			goto bad;
43578064Sume		}
43653541Sshin	}
43753541Sshin
43878064Sume	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
43978064Sume		ip6->ip6_src.s6_addr16[1]
44078064Sume			= htons(m->m_pkthdr.rcvif->if_index);
44178064Sume	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
44278064Sume		ip6->ip6_dst.s6_addr16[1]
44378064Sume			= htons(m->m_pkthdr.rcvif->if_index);
44453541Sshin
44578064Sume#if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */
44653541Sshin	/*
44778064Sume	 * We use rt->rt_ifp to determine if the address is ours or not.
44878064Sume	 * If rt_ifp is lo0, the address is ours.
44978064Sume	 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0,
45078064Sume	 * so any address under fe80::%lo0/64 will be mistakenly considered
45178064Sume	 * local.  The special case is supplied to handle the case properly
45278064Sume	 * by actually looking at interface addresses
45378064Sume	 * (using in6ifa_ifpwithaddr).
45453541Sshin	 */
45578064Sume	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 &&
45678064Sume	    IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
45778064Sume		if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) {
45878064Sume			icmp6_error(m, ICMP6_DST_UNREACH,
45978064Sume			    ICMP6_DST_UNREACH_ADDR, 0);
46078064Sume			/* m is already freed */
46178064Sume			return;
46278064Sume		}
46375246Sume
46478064Sume		ours = 1;
46578064Sume		deliverifp = m->m_pkthdr.rcvif;
46678064Sume		goto hbhcheck;
46778064Sume	}
46875719Sume#endif
46953541Sshin
47053541Sshin	/*
47153541Sshin	 * Multicast check
47253541Sshin	 */
47353541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
47453541Sshin	  	struct	in6_multi *in6m = 0;
47553541Sshin
47653541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
47753541Sshin		/*
47853541Sshin		 * See if we belong to the destination multicast group on the
47953541Sshin		 * arrival interface.
48053541Sshin		 */
48153541Sshin		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
48253541Sshin		if (in6m)
48353541Sshin			ours = 1;
48456723Sshin		else if (!ip6_mrouter) {
48553541Sshin			ip6stat.ip6s_notmember++;
48653541Sshin			ip6stat.ip6s_cantforward++;
48753541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
48853541Sshin			goto bad;
48953541Sshin		}
49053541Sshin		deliverifp = m->m_pkthdr.rcvif;
49153541Sshin		goto hbhcheck;
49253541Sshin	}
49353541Sshin
49453541Sshin	/*
49553541Sshin	 *  Unicast check
49653541Sshin	 */
49778064Sume	switch (ip6_ours_check_algorithm) {
49878064Sume	default:
49978064Sume		/*
50078064Sume		 * XXX: I intentionally broke our indentation rule here,
50178064Sume		 *      since this switch-case is just for measurement and
50278064Sume		 *      therefore should soon be removed.
50378064Sume		 */
50462587Sitojun	if (ip6_forward_rt.ro_rt != NULL &&
50562587Sitojun	    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
50662587Sitojun	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
50778064Sume			       &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
50862587Sitojun		ip6stat.ip6s_forward_cachehit++;
50962587Sitojun	else {
51078064Sume		struct sockaddr_in6 *dst6;
51178064Sume
51253541Sshin		if (ip6_forward_rt.ro_rt) {
51362587Sitojun			/* route is down or destination is different */
51462587Sitojun			ip6stat.ip6s_forward_cachemiss++;
51553541Sshin			RTFREE(ip6_forward_rt.ro_rt);
51653541Sshin			ip6_forward_rt.ro_rt = 0;
51753541Sshin		}
51862587Sitojun
51953541Sshin		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
52078064Sume		dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
52178064Sume		dst6->sin6_len = sizeof(struct sockaddr_in6);
52278064Sume		dst6->sin6_family = AF_INET6;
52378064Sume		dst6->sin6_addr = ip6->ip6_dst;
52462587Sitojun#ifdef SCOPEDROUTING
52562587Sitojun		ip6_forward_rt.ro_dst.sin6_scope_id =
52662587Sitojun			in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_dst);
52762587Sitojun#endif
52853541Sshin
52953541Sshin		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
53053541Sshin	}
53153541Sshin
53253541Sshin#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
53353541Sshin
53453541Sshin	/*
53553541Sshin	 * Accept the packet if the forwarding interface to the destination
53653541Sshin	 * according to the routing table is the loopback interface,
53753541Sshin	 * unless the associated route has a gateway.
53853541Sshin	 * Note that this approach causes to accept a packet if there is a
53953541Sshin	 * route to the loopback interface for the destination of the packet.
54053541Sshin	 * But we think it's even useful in some situations, e.g. when using
54153541Sshin	 * a special daemon which wants to intercept the packet.
54278064Sume	 *
54378064Sume	 * XXX: some OSes automatically make a cloned route for the destination
54478064Sume	 * of an outgoing packet.  If the outgoing interface of the packet
54578064Sume	 * is a loopback one, the kernel would consider the packet to be
54678064Sume	 * accepted, even if we have no such address assinged on the interface.
54778064Sume	 * We check the cloned flag of the route entry to reject such cases,
54878064Sume	 * assuming that route entries for our own addresses are not made by
54978064Sume	 * cloning (it should be true because in6_addloop explicitly installs
55078064Sume	 * the host route).  However, we might have to do an explicit check
55178064Sume	 * while it would be less efficient.  Or, should we rather install a
55278064Sume	 * reject route for such a case?
55353541Sshin	 */
55453541Sshin	if (ip6_forward_rt.ro_rt &&
55553541Sshin	    (ip6_forward_rt.ro_rt->rt_flags &
55653541Sshin	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
55778064Sume#ifdef RTF_WASCLONED
55878064Sume	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
55978064Sume#endif
56078064Sume#ifdef RTF_CLONED
56178064Sume	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
56278064Sume#endif
56362587Sitojun#if 0
56453541Sshin	    /*
56562587Sitojun	     * The check below is redundant since the comparison of
56662587Sitojun	     * the destination and the key of the rtentry has
56762587Sitojun	     * already done through looking up the routing table.
56853541Sshin	     */
56962587Sitojun	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
57078064Sume				&rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
57162587Sitojun#endif
57253541Sshin	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
57353541Sshin		struct in6_ifaddr *ia6 =
57453541Sshin			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
57578064Sume
57662587Sitojun		/*
57778064Sume		 * record address information into m_aux.
57878064Sume		 */
57978064Sume		(void)ip6_setdstifaddr(m, ia6);
58078064Sume
58178064Sume		/*
58262587Sitojun		 * packets to a tentative, duplicated, or somehow invalid
58362587Sitojun		 * address must not be accepted.
58462587Sitojun		 */
58553541Sshin		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
58662587Sitojun			/* this address is ready */
58753541Sshin			ours = 1;
58853541Sshin			deliverifp = ia6->ia_ifp;	/* correct? */
58967334Sjoe			/* Count the packet in the ip address stats */
59067334Sjoe			ia6->ia_ifa.if_ipackets++;
59167334Sjoe			ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
59253541Sshin			goto hbhcheck;
59353541Sshin		} else {
59462587Sitojun			/* address is not ready, so discard the packet. */
59578064Sume			nd6log((LOG_INFO,
59678064Sume			    "ip6_input: packet to an unready address %s->%s\n",
59762587Sitojun			    ip6_sprintf(&ip6->ip6_src),
59878064Sume			    ip6_sprintf(&ip6->ip6_dst)));
59962587Sitojun
60062587Sitojun			goto bad;
60153541Sshin		}
60253541Sshin	}
60378064Sume	} /* XXX indentation (see above) */
60453541Sshin
60553541Sshin	/*
60653541Sshin	 * FAITH(Firewall Aided Internet Translator)
60753541Sshin	 */
60853541Sshin	if (ip6_keepfaith) {
60953541Sshin		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
61053541Sshin		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
61153541Sshin			/* XXX do we need more sanity checks? */
61253541Sshin			ours = 1;
61395023Ssuz			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
61453541Sshin			goto hbhcheck;
61553541Sshin		}
61653541Sshin	}
61753541Sshin
61853541Sshin	/*
61953541Sshin	 * Now there is no reason to process the packet if it's not our own
62053541Sshin	 * and we're not a router.
62153541Sshin	 */
62253541Sshin	if (!ip6_forwarding) {
62353541Sshin		ip6stat.ip6s_cantforward++;
62453541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
62553541Sshin		goto bad;
62653541Sshin	}
62753541Sshin
62853541Sshin  hbhcheck:
62953541Sshin	/*
63078064Sume	 * record address information into m_aux, if we don't have one yet.
63178064Sume	 * note that we are unable to record it, if the address is not listed
63278064Sume	 * as our interface address (e.g. multicast addresses, addresses
63378064Sume	 * within FAITH prefixes and such).
63478064Sume	 */
63578064Sume	if (deliverifp && !ip6_getdstifaddr(m)) {
63678064Sume		struct in6_ifaddr *ia6;
63778064Sume
63878064Sume		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
63978064Sume		if (ia6) {
64078064Sume			if (!ip6_setdstifaddr(m, ia6)) {
64178064Sume				/*
64278064Sume				 * XXX maybe we should drop the packet here,
64378064Sume				 * as we could not provide enough information
64478064Sume				 * to the upper layers.
64578064Sume				 */
64678064Sume			}
64778064Sume		}
64878064Sume	}
64978064Sume
65078064Sume	/*
65153541Sshin	 * Process Hop-by-Hop options header if it's contained.
65253541Sshin	 * m may be modified in ip6_hopopts_input().
65353541Sshin	 * If a JumboPayload option is included, plen will also be modified.
65453541Sshin	 */
65553541Sshin	plen = (u_int32_t)ntohs(ip6->ip6_plen);
65653541Sshin	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
65762587Sitojun		struct ip6_hbh *hbh;
65862587Sitojun
65953541Sshin		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
66062587Sitojun#if 0	/*touches NULL pointer*/
66153541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
66262587Sitojun#endif
66353541Sshin			return;	/* m have already been freed */
66453541Sshin		}
66562587Sitojun
66653541Sshin		/* adjust pointer */
66753541Sshin		ip6 = mtod(m, struct ip6_hdr *);
66853541Sshin
66953541Sshin		/*
67095023Ssuz		 * if the payload length field is 0 and the next header field
67162587Sitojun		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
67262587Sitojun		 * option MUST be included.
67362587Sitojun		 */
67462587Sitojun		if (ip6->ip6_plen == 0 && plen == 0) {
67562587Sitojun			/*
67662587Sitojun			 * Note that if a valid jumbo payload option is
67762587Sitojun			 * contained, ip6_hoptops_input() must set a valid
67862587Sitojun			 * (non-zero) payload length to the variable plen.
67962587Sitojun			 */
68062587Sitojun			ip6stat.ip6s_badoptions++;
68162587Sitojun			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
68262587Sitojun			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
68362587Sitojun			icmp6_error(m, ICMP6_PARAM_PROB,
68462587Sitojun				    ICMP6_PARAMPROB_HEADER,
68562587Sitojun				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
68662587Sitojun			return;
68762587Sitojun		}
68862587Sitojun#ifndef PULLDOWN_TEST
68962587Sitojun		/* ip6_hopopts_input() ensures that mbuf is contiguous */
69062587Sitojun		hbh = (struct ip6_hbh *)(ip6 + 1);
69162587Sitojun#else
69262587Sitojun		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
69362587Sitojun			sizeof(struct ip6_hbh));
69462587Sitojun		if (hbh == NULL) {
69562587Sitojun			ip6stat.ip6s_tooshort++;
69662587Sitojun			return;
69762587Sitojun		}
69862587Sitojun#endif
69962587Sitojun		nxt = hbh->ip6h_nxt;
70062587Sitojun
70162587Sitojun		/*
70253541Sshin		 * accept the packet if a router alert option is included
70353541Sshin		 * and we act as an IPv6 router.
70453541Sshin		 */
70553541Sshin		if (rtalert != ~0 && ip6_forwarding)
70653541Sshin			ours = 1;
70753541Sshin	} else
70853541Sshin		nxt = ip6->ip6_nxt;
70953541Sshin
71053541Sshin	/*
71153541Sshin	 * Check that the amount of data in the buffers
71253541Sshin	 * is as at least much as the IPv6 header would have us expect.
71353541Sshin	 * Trim mbufs if longer than we expect.
71453541Sshin	 * Drop packet if shorter than we expect.
71553541Sshin	 */
71653541Sshin	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
71753541Sshin		ip6stat.ip6s_tooshort++;
71853541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
71953541Sshin		goto bad;
72053541Sshin	}
72153541Sshin	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
72253541Sshin		if (m->m_len == m->m_pkthdr.len) {
72353541Sshin			m->m_len = sizeof(struct ip6_hdr) + plen;
72453541Sshin			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
72553541Sshin		} else
72653541Sshin			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
72753541Sshin	}
72853541Sshin
72953541Sshin	/*
73053541Sshin	 * Forward if desirable.
73153541Sshin	 */
73253541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
73356723Sshin		/*
73456723Sshin		 * If we are acting as a multicast router, all
73556723Sshin		 * incoming multicast packets are passed to the
73656723Sshin		 * kernel-level multicast forwarding function.
73756723Sshin		 * The packet is returned (relatively) intact; if
73856723Sshin		 * ip6_mforward() returns a non-zero value, the packet
73956723Sshin		 * must be discarded, else it may be accepted below.
74056723Sshin		 */
74156723Sshin		if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
74256723Sshin			ip6stat.ip6s_cantforward++;
74356723Sshin			m_freem(m);
74456723Sshin			return;
74556723Sshin		}
74653541Sshin		if (!ours) {
74753541Sshin			m_freem(m);
74853541Sshin			return;
74953541Sshin		}
75053541Sshin	} else if (!ours) {
75153541Sshin		ip6_forward(m, 0);
75253541Sshin		return;
75353541Sshin	}
75453541Sshin
75562587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
75662587Sitojun
75753541Sshin	/*
75862587Sitojun	 * Malicious party may be able to use IPv4 mapped addr to confuse
75962587Sitojun	 * tcp/udp stack and bypass security checks (act as if it was from
76062587Sitojun	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
76162587Sitojun	 *
76262587Sitojun	 * For SIIT end node behavior, you may want to disable the check.
76362587Sitojun	 * However, you will  become vulnerable to attacks using IPv4 mapped
76462587Sitojun	 * source.
76562587Sitojun	 */
76662587Sitojun	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
76762587Sitojun	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
76862587Sitojun		ip6stat.ip6s_badscope++;
76962587Sitojun		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
77062587Sitojun		goto bad;
77162587Sitojun	}
77262587Sitojun
77362587Sitojun	/*
77453541Sshin	 * Tell launch routine the next header
77553541Sshin	 */
77653541Sshin	ip6stat.ip6s_delivered++;
77753541Sshin	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
77853541Sshin	nest = 0;
77978064Sume
78053541Sshin	while (nxt != IPPROTO_DONE) {
78153541Sshin		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
78253541Sshin			ip6stat.ip6s_toomanyhdr++;
78353541Sshin			goto bad;
78453541Sshin		}
78553541Sshin
78653541Sshin		/*
78753541Sshin		 * protection against faulty packet - there should be
78853541Sshin		 * more sanity checks in header chain processing.
78953541Sshin		 */
79053541Sshin		if (m->m_pkthdr.len < off) {
79153541Sshin			ip6stat.ip6s_tooshort++;
79253541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
79353541Sshin			goto bad;
79453541Sshin		}
79553541Sshin
79678064Sume#if 0
79778064Sume		/*
79878064Sume		 * do we need to do it for every header?  yeah, other
79978064Sume		 * functions can play with it (like re-allocate and copy).
80078064Sume		 */
80178064Sume		mhist = ip6_addaux(m);
80278064Sume		if (mhist && M_TRAILINGSPACE(mhist) >= sizeof(nxt)) {
80378064Sume			hist = mtod(mhist, caddr_t) + mhist->m_len;
80478064Sume			bcopy(&nxt, hist, sizeof(nxt));
80578064Sume			mhist->m_len += sizeof(nxt);
80678064Sume		} else {
80778064Sume			ip6stat.ip6s_toomanyhdr++;
80878064Sume			goto bad;
80978064Sume		}
81078064Sume#endif
81178064Sume
81278064Sume#ifdef IPSEC
81378064Sume		/*
81478064Sume		 * enforce IPsec policy checking if we are seeing last header.
81578064Sume		 * note that we do not visit this with protocols with pcb layer
81678064Sume		 * code - like udp/tcp/raw ip.
81778064Sume		 */
81878064Sume		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
81978064Sume		    ipsec6_in_reject(m, NULL)) {
82078064Sume			ipsec6stat.in_polvio++;
82178064Sume			goto bad;
82278064Sume		}
82378064Sume#endif
82478064Sume
82553541Sshin		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
82653541Sshin	}
82753541Sshin	return;
82853541Sshin bad:
82953541Sshin	m_freem(m);
83053541Sshin}
83153541Sshin
83253541Sshin/*
83378064Sume * set/grab in6_ifaddr correspond to IPv6 destination address.
83478064Sume * XXX backward compatibility wrapper
83578064Sume */
836105194Ssamstatic struct ip6aux *
83778064Sumeip6_setdstifaddr(m, ia6)
83878064Sume	struct mbuf *m;
83978064Sume	struct in6_ifaddr *ia6;
84078064Sume{
841105194Ssam	struct ip6aux *n;
84278064Sume
84378064Sume	n = ip6_addaux(m);
84478064Sume	if (n)
845105194Ssam		n->ip6a_dstia6 = ia6;
84678064Sume	return n;	/* NULL if failed to set */
84778064Sume}
84878064Sume
84978064Sumestruct in6_ifaddr *
85078064Sumeip6_getdstifaddr(m)
85178064Sume	struct mbuf *m;
85278064Sume{
853105194Ssam	struct ip6aux *n;
85478064Sume
85578064Sume	n = ip6_findaux(m);
85678064Sume	if (n)
857105194Ssam		return n->ip6a_dstia6;
85878064Sume	else
85978064Sume		return NULL;
86078064Sume}
86178064Sume
86278064Sume/*
86353541Sshin * Hop-by-Hop options header processing. If a valid jumbo payload option is
86453541Sshin * included, the real payload length will be stored in plenp.
86553541Sshin */
86653541Sshinstatic int
86753541Sshinip6_hopopts_input(plenp, rtalertp, mp, offp)
86853541Sshin	u_int32_t *plenp;
86953541Sshin	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
87053541Sshin	struct mbuf **mp;
87153541Sshin	int *offp;
87253541Sshin{
87378064Sume	struct mbuf *m = *mp;
87453541Sshin	int off = *offp, hbhlen;
87553541Sshin	struct ip6_hbh *hbh;
87653541Sshin	u_int8_t *opt;
87753541Sshin
87853541Sshin	/* validation of the length of the header */
87962587Sitojun#ifndef PULLDOWN_TEST
88053541Sshin	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
88153541Sshin	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
88253541Sshin	hbhlen = (hbh->ip6h_len + 1) << 3;
88353541Sshin
88453541Sshin	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
88553541Sshin	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
88662587Sitojun#else
88762587Sitojun	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
88862587Sitojun		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
88962587Sitojun	if (hbh == NULL) {
89062587Sitojun		ip6stat.ip6s_tooshort++;
89162587Sitojun		return -1;
89262587Sitojun	}
89362587Sitojun	hbhlen = (hbh->ip6h_len + 1) << 3;
89462587Sitojun	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
89562587Sitojun		hbhlen);
89662587Sitojun	if (hbh == NULL) {
89762587Sitojun		ip6stat.ip6s_tooshort++;
89862587Sitojun		return -1;
89962587Sitojun	}
90062587Sitojun#endif
90153541Sshin	off += hbhlen;
90253541Sshin	hbhlen -= sizeof(struct ip6_hbh);
90353541Sshin	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
90453541Sshin
90553541Sshin	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
90653541Sshin				hbhlen, rtalertp, plenp) < 0)
907120856Sume		return (-1);
90853541Sshin
90953541Sshin	*offp = off;
91053541Sshin	*mp = m;
911120856Sume	return (0);
91253541Sshin}
91353541Sshin
91453541Sshin/*
91553541Sshin * Search header for all Hop-by-hop options and process each option.
91653541Sshin * This function is separate from ip6_hopopts_input() in order to
91753541Sshin * handle a case where the sending node itself process its hop-by-hop
91853541Sshin * options header. In such a case, the function is called from ip6_output().
91978064Sume *
92078064Sume * The function assumes that hbh header is located right after the IPv6 header
92178064Sume * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
92278064Sume * opthead + hbhlen is located in continuous memory region.
92353541Sshin */
92453541Sshinint
92553541Sshinip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
92653541Sshin	struct mbuf *m;
92753541Sshin	u_int8_t *opthead;
92853541Sshin	int hbhlen;
92953541Sshin	u_int32_t *rtalertp;
93053541Sshin	u_int32_t *plenp;
93153541Sshin{
93253541Sshin	struct ip6_hdr *ip6;
93353541Sshin	int optlen = 0;
93453541Sshin	u_int8_t *opt = opthead;
93553541Sshin	u_int16_t rtalert_val;
93662587Sitojun	u_int32_t jumboplen;
93778064Sume	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
93853541Sshin
93953541Sshin	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
94078064Sume		switch (*opt) {
94178064Sume		case IP6OPT_PAD1:
94278064Sume			optlen = 1;
94378064Sume			break;
94478064Sume		case IP6OPT_PADN:
94578064Sume			if (hbhlen < IP6OPT_MINLEN) {
94678064Sume				ip6stat.ip6s_toosmall++;
94778064Sume				goto bad;
94878064Sume			}
94978064Sume			optlen = *(opt + 1) + 2;
95078064Sume			break;
95178064Sume		case IP6OPT_RTALERT:
95262587Sitojun			/* XXX may need check for alignment */
95378064Sume			if (hbhlen < IP6OPT_RTALERT_LEN) {
95478064Sume				ip6stat.ip6s_toosmall++;
95578064Sume				goto bad;
95678064Sume			}
95778064Sume			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
95878064Sume				/* XXX stat */
95978064Sume				icmp6_error(m, ICMP6_PARAM_PROB,
96078064Sume					    ICMP6_PARAMPROB_HEADER,
96178064Sume					    erroff + opt + 1 - opthead);
962120856Sume				return (-1);
96378064Sume			}
96478064Sume			optlen = IP6OPT_RTALERT_LEN;
96578064Sume			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
96678064Sume			*rtalertp = ntohs(rtalert_val);
96778064Sume			break;
96878064Sume		case IP6OPT_JUMBO:
96978064Sume			/* XXX may need check for alignment */
97062587Sitojun			if (hbhlen < IP6OPT_JUMBO_LEN) {
97162587Sitojun				ip6stat.ip6s_toosmall++;
97262587Sitojun				goto bad;
97362587Sitojun			}
97478064Sume			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
97578064Sume				/* XXX stat */
97678064Sume				icmp6_error(m, ICMP6_PARAM_PROB,
97778064Sume					    ICMP6_PARAMPROB_HEADER,
97878064Sume					    erroff + opt + 1 - opthead);
979120856Sume				return (-1);
98078064Sume			}
98162587Sitojun			optlen = IP6OPT_JUMBO_LEN;
98253541Sshin
98362587Sitojun			/*
98462587Sitojun			 * IPv6 packets that have non 0 payload length
98578064Sume			 * must not contain a jumbo payload option.
98662587Sitojun			 */
98762587Sitojun			ip6 = mtod(m, struct ip6_hdr *);
98862587Sitojun			if (ip6->ip6_plen) {
98962587Sitojun				ip6stat.ip6s_badoptions++;
99062587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
99162587Sitojun					    ICMP6_PARAMPROB_HEADER,
99278064Sume					    erroff + opt - opthead);
993120856Sume				return (-1);
99462587Sitojun			}
99553541Sshin
99662587Sitojun			/*
99762587Sitojun			 * We may see jumbolen in unaligned location, so
99862587Sitojun			 * we'd need to perform bcopy().
99962587Sitojun			 */
100062587Sitojun			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
100162587Sitojun			jumboplen = (u_int32_t)htonl(jumboplen);
100262587Sitojun
100362587Sitojun#if 1
100462587Sitojun			/*
100562587Sitojun			 * if there are multiple jumbo payload options,
100662587Sitojun			 * *plenp will be non-zero and the packet will be
100762587Sitojun			 * rejected.
100862587Sitojun			 * the behavior may need some debate in ipngwg -
100962587Sitojun			 * multiple options does not make sense, however,
101062587Sitojun			 * there's no explicit mention in specification.
101162587Sitojun			 */
101262587Sitojun			if (*plenp != 0) {
101362587Sitojun				ip6stat.ip6s_badoptions++;
101462587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
101562587Sitojun					    ICMP6_PARAMPROB_HEADER,
101678064Sume					    erroff + opt + 2 - opthead);
1017120856Sume				return (-1);
101862587Sitojun			}
101962587Sitojun#endif
102062587Sitojun
102162587Sitojun			/*
102262587Sitojun			 * jumbo payload length must be larger than 65535.
102362587Sitojun			 */
102462587Sitojun			if (jumboplen <= IPV6_MAXPACKET) {
102562587Sitojun				ip6stat.ip6s_badoptions++;
102662587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
102762587Sitojun					    ICMP6_PARAMPROB_HEADER,
102878064Sume					    erroff + opt + 2 - opthead);
1029120856Sume				return (-1);
103062587Sitojun			}
103162587Sitojun			*plenp = jumboplen;
103262587Sitojun
103362587Sitojun			break;
103478064Sume		default:		/* unknown option */
103578064Sume			if (hbhlen < IP6OPT_MINLEN) {
103678064Sume				ip6stat.ip6s_toosmall++;
103778064Sume				goto bad;
103878064Sume			}
103978064Sume			optlen = ip6_unknown_opt(opt, m,
104078064Sume			    erroff + opt - opthead);
104178064Sume			if (optlen == -1)
1042120856Sume				return (-1);
104378064Sume			optlen += 2;
104478064Sume			break;
104553541Sshin		}
104653541Sshin	}
104753541Sshin
1048120856Sume	return (0);
104953541Sshin
105053541Sshin  bad:
105153541Sshin	m_freem(m);
1052120856Sume	return (-1);
105353541Sshin}
105453541Sshin
105553541Sshin/*
105653541Sshin * Unknown option processing.
105753541Sshin * The third argument `off' is the offset from the IPv6 header to the option,
105853541Sshin * which is necessary if the IPv6 header the and option header and IPv6 header
105953541Sshin * is not continuous in order to return an ICMPv6 error.
106053541Sshin */
106153541Sshinint
106253541Sshinip6_unknown_opt(optp, m, off)
106353541Sshin	u_int8_t *optp;
106453541Sshin	struct mbuf *m;
106553541Sshin	int off;
106653541Sshin{
106753541Sshin	struct ip6_hdr *ip6;
106853541Sshin
106978064Sume	switch (IP6OPT_TYPE(*optp)) {
107078064Sume	case IP6OPT_TYPE_SKIP: /* ignore the option */
1071120856Sume		return ((int)*(optp + 1));
107278064Sume	case IP6OPT_TYPE_DISCARD:	/* silently discard */
107378064Sume		m_freem(m);
1074120856Sume		return (-1);
107578064Sume	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
107678064Sume		ip6stat.ip6s_badoptions++;
107778064Sume		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1078120856Sume		return (-1);
107978064Sume	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
108078064Sume		ip6stat.ip6s_badoptions++;
108178064Sume		ip6 = mtod(m, struct ip6_hdr *);
108278064Sume		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
108378064Sume		    (m->m_flags & (M_BCAST|M_MCAST)))
108478064Sume			m_freem(m);
108578064Sume		else
108678064Sume			icmp6_error(m, ICMP6_PARAM_PROB,
108778064Sume				    ICMP6_PARAMPROB_OPTION, off);
1088120856Sume		return (-1);
108953541Sshin	}
109053541Sshin
109153541Sshin	m_freem(m);		/* XXX: NOTREACHED */
1092120856Sume	return (-1);
109353541Sshin}
109453541Sshin
109553541Sshin/*
109662587Sitojun * Create the "control" list for this pcb.
109778064Sume * The function will not modify mbuf chain at all.
109862587Sitojun *
109978064Sume * with KAME mbuf chain restriction:
110062587Sitojun * The routine will be called from upper layer handlers like tcp6_input().
110162587Sitojun * Thus the routine assumes that the caller (tcp6_input) have already
110262587Sitojun * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
110362587Sitojun * very first mbuf on the mbuf chain.
110453541Sshin */
110553541Sshinvoid
110653541Sshinip6_savecontrol(in6p, mp, ip6, m)
110778064Sume	struct inpcb *in6p;
110878064Sume	struct mbuf **mp;
110978064Sume	struct ip6_hdr *ip6;
111078064Sume	struct mbuf *m;
111153541Sshin{
111297676Sume#if __FreeBSD_version >= 500000
111393593Sjhb	struct thread *td = curthread;	/* XXX */
111493593Sjhb#else
111593593Sjhb	struct proc *td = curproc;	/* XXX */
111693593Sjhb#endif
111778064Sume	int privileged = 0;
111878064Sume	int rthdr_exist = 0;
111953541Sshin
112078064Sume
112193593Sjhb	if (td && !suser(td))
112278064Sume 		privileged++;
112353541Sshin
112478064Sume#ifdef SO_TIMESTAMP
112578064Sume	if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
112653541Sshin		struct timeval tv;
112753541Sshin
112853541Sshin		microtime(&tv);
112953541Sshin		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
113078064Sume				      SCM_TIMESTAMP, SOL_SOCKET);
113178064Sume		if (*mp) {
113253541Sshin			mp = &(*mp)->m_next;
113378064Sume		}
113497658Stanimura	}
113562587Sitojun#endif
113662587Sitojun
113753541Sshin	/* RFC 2292 sec. 5 */
113878064Sume	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
113953541Sshin		struct in6_pktinfo pi6;
114053541Sshin		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
114153541Sshin		if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
114253541Sshin			pi6.ipi6_addr.s6_addr16[1] = 0;
114353541Sshin		pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
114453541Sshin					? m->m_pkthdr.rcvif->if_index
114553541Sshin					: 0;
114653541Sshin		*mp = sbcreatecontrol((caddr_t) &pi6,
114753541Sshin			sizeof(struct in6_pktinfo), IPV6_PKTINFO,
114853541Sshin			IPPROTO_IPV6);
114953541Sshin		if (*mp)
115053541Sshin			mp = &(*mp)->m_next;
115153541Sshin	}
115278064Sume
115378064Sume	if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
115453541Sshin		int hlim = ip6->ip6_hlim & 0xff;
115553541Sshin		*mp = sbcreatecontrol((caddr_t) &hlim,
115653541Sshin			sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
115753541Sshin		if (*mp)
115853541Sshin			mp = &(*mp)->m_next;
115953541Sshin	}
116053541Sshin
116153541Sshin	/*
116253541Sshin	 * IPV6_HOPOPTS socket option. We require super-user privilege
116353541Sshin	 * for the option, but it might be too strict, since there might
116453541Sshin	 * be some hop-by-hop options which can be returned to normal user.
116553541Sshin	 * See RFC 2292 section 6.
116653541Sshin	 */
116778064Sume	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) {
116853541Sshin		/*
116953541Sshin		 * Check if a hop-by-hop options header is contatined in the
117053541Sshin		 * received packet, and if so, store the options as ancillary
117153541Sshin		 * data. Note that a hop-by-hop options header must be
117253541Sshin		 * just after the IPv6 header, which fact is assured through
117353541Sshin		 * the IPv6 input processing.
117453541Sshin		 */
117553541Sshin		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
117653541Sshin		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
117762587Sitojun			struct ip6_hbh *hbh;
117878064Sume			int hbhlen = 0;
117978064Sume#ifdef PULLDOWN_TEST
118078064Sume			struct mbuf *ext;
118178064Sume#endif
118253541Sshin
118362587Sitojun#ifndef PULLDOWN_TEST
118462587Sitojun			hbh = (struct ip6_hbh *)(ip6 + 1);
118562587Sitojun			hbhlen = (hbh->ip6h_len + 1) << 3;
118662587Sitojun#else
118778064Sume			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
118878064Sume			    ip6->ip6_nxt);
118978064Sume			if (ext == NULL) {
119062587Sitojun				ip6stat.ip6s_tooshort++;
119162587Sitojun				return;
119262587Sitojun			}
119378064Sume			hbh = mtod(ext, struct ip6_hbh *);
119462587Sitojun			hbhlen = (hbh->ip6h_len + 1) << 3;
119578064Sume			if (hbhlen != ext->m_len) {
119678064Sume				m_freem(ext);
119762587Sitojun				ip6stat.ip6s_tooshort++;
119862587Sitojun				return;
119962587Sitojun			}
120062587Sitojun#endif
120162587Sitojun
120253541Sshin			/*
120353541Sshin			 * XXX: We copy whole the header even if a jumbo
120453541Sshin			 * payload option is included, which option is to
120553541Sshin			 * be removed before returning in the RFC 2292.
120678064Sume			 * Note: this constraint is removed in 2292bis.
120753541Sshin			 */
120862587Sitojun			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
120953541Sshin					      IPV6_HOPOPTS, IPPROTO_IPV6);
121053541Sshin			if (*mp)
121153541Sshin				mp = &(*mp)->m_next;
121278064Sume#ifdef PULLDOWN_TEST
121378064Sume			m_freem(ext);
121478064Sume#endif
121553541Sshin		}
121653541Sshin	}
121753541Sshin
121853541Sshin	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
121978064Sume	if ((in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) {
122078064Sume		int proto, off, nxt;
122178064Sume
122278064Sume		/*
122378064Sume		 * go through the header chain to see if a routing header is
122478064Sume		 * contained in the packet. We need this information to store
122578064Sume		 * destination options headers (if any) properly.
122678064Sume		 * XXX: performance issue. We should record this info when
122778064Sume		 * processing extension headers in incoming routine.
122878064Sume		 * (todo) use m_aux?
122978064Sume		 */
123078064Sume		proto = IPPROTO_IPV6;
123178064Sume		off = 0;
123278064Sume		nxt = -1;
123378064Sume		while (1) {
123478064Sume			int newoff;
123578064Sume
123678064Sume			newoff = ip6_nexthdr(m, off, proto, &nxt);
123778064Sume			if (newoff < 0)
123878064Sume				break;
123978064Sume			if (newoff < off) /* invalid, check for safety */
124078064Sume				break;
124178064Sume			if ((proto = nxt) == IPPROTO_ROUTING) {
124278064Sume				rthdr_exist = 1;
124378064Sume				break;
124478064Sume			}
124578064Sume			off = newoff;
124678064Sume		}
124778064Sume	}
124878064Sume
124978064Sume	if ((in6p->in6p_flags &
125078064Sume	     (IN6P_RTHDR | IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) {
125153541Sshin		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
125278064Sume		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
125353541Sshin
125453541Sshin		/*
125553541Sshin		 * Search for destination options headers or routing
125653541Sshin		 * header(s) through the header chain, and stores each
125753541Sshin		 * header as ancillary data.
125853541Sshin		 * Note that the order of the headers remains in
125953541Sshin		 * the chain of ancillary data.
126053541Sshin		 */
126178064Sume		while (1) {	/* is explicit loop prevention necessary? */
126278064Sume			struct ip6_ext *ip6e = NULL;
126362587Sitojun			int elen;
126478064Sume#ifdef PULLDOWN_TEST
126578064Sume			struct mbuf *ext = NULL;
126678064Sume#endif
126753541Sshin
126878064Sume			/*
126978064Sume			 * if it is not an extension header, don't try to
127078064Sume			 * pull it from the chain.
127178064Sume			 */
127278064Sume			switch (nxt) {
127378064Sume			case IPPROTO_DSTOPTS:
127478064Sume			case IPPROTO_ROUTING:
127578064Sume			case IPPROTO_HOPOPTS:
127678064Sume			case IPPROTO_AH: /* is it possible? */
127778064Sume				break;
127878064Sume			default:
127978064Sume				goto loopend;
128078064Sume			}
128178064Sume
128262587Sitojun#ifndef PULLDOWN_TEST
128378064Sume			if (off + sizeof(*ip6e) > m->m_len)
128478064Sume				goto loopend;
128562587Sitojun			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
128662587Sitojun			if (nxt == IPPROTO_AH)
128762587Sitojun				elen = (ip6e->ip6e_len + 2) << 2;
128862587Sitojun			else
128962587Sitojun				elen = (ip6e->ip6e_len + 1) << 3;
129078064Sume			if (off + elen > m->m_len)
129178064Sume				goto loopend;
129262587Sitojun#else
129378064Sume			ext = ip6_pullexthdr(m, off, nxt);
129478064Sume			if (ext == NULL) {
129562587Sitojun				ip6stat.ip6s_tooshort++;
129662587Sitojun				return;
129762587Sitojun			}
129878064Sume			ip6e = mtod(ext, struct ip6_ext *);
129962587Sitojun			if (nxt == IPPROTO_AH)
130062587Sitojun				elen = (ip6e->ip6e_len + 2) << 2;
130162587Sitojun			else
130262587Sitojun				elen = (ip6e->ip6e_len + 1) << 3;
130378064Sume			if (elen != ext->m_len) {
130478064Sume				m_freem(ext);
130562587Sitojun				ip6stat.ip6s_tooshort++;
130662587Sitojun				return;
130762587Sitojun			}
130862587Sitojun#endif
130962587Sitojun
131078064Sume			switch (nxt) {
131178064Sume			case IPPROTO_DSTOPTS:
131278064Sume				if ((in6p->in6p_flags & IN6P_DSTOPTS) == 0)
131378064Sume					break;
131453541Sshin
131578064Sume				/*
131678064Sume				 * We also require super-user privilege for
131778064Sume				 * the option.
131878064Sume				 * See the comments on IN6_HOPOPTS.
131978064Sume				 */
132078064Sume				if (!privileged)
132178064Sume					break;
132253541Sshin
132378064Sume				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
132478064Sume						      IPV6_DSTOPTS,
132578064Sume						      IPPROTO_IPV6);
132678064Sume				if (*mp)
132778064Sume					mp = &(*mp)->m_next;
132878064Sume				break;
132978064Sume			case IPPROTO_ROUTING:
133078064Sume				if (!in6p->in6p_flags & IN6P_RTHDR)
133178064Sume					break;
133253541Sshin
133378064Sume				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
133478064Sume						      IPV6_RTHDR,
133578064Sume						      IPPROTO_IPV6);
133678064Sume				if (*mp)
133778064Sume					mp = &(*mp)->m_next;
133878064Sume				break;
133978064Sume			case IPPROTO_HOPOPTS:
134078064Sume			case IPPROTO_AH: /* is it possible? */
134178064Sume				break;
134253541Sshin
134378064Sume			default:
134478064Sume				/*
134578064Sume			 	 * other cases have been filtered in the above.
134678064Sume				 * none will visit this case.  here we supply
134778064Sume				 * the code just in case (nxt overwritten or
134878064Sume				 * other cases).
134978064Sume				 */
135078064Sume#ifdef PULLDOWN_TEST
135178064Sume				m_freem(ext);
135278064Sume#endif
135378064Sume				goto loopend;
135453541Sshin
135553541Sshin			}
135653541Sshin
135753541Sshin			/* proceed with the next header. */
135862587Sitojun			off += elen;
135953541Sshin			nxt = ip6e->ip6e_nxt;
136078064Sume			ip6e = NULL;
136178064Sume#ifdef PULLDOWN_TEST
136278064Sume			m_freem(ext);
136378064Sume			ext = NULL;
136478064Sume#endif
136553541Sshin		}
136653541Sshin	  loopend:
136778064Sume		;
136853541Sshin	}
136978064Sume
137078064Sume}
137178064Sume
137278064Sume#ifdef PULLDOWN_TEST
137378064Sume/*
137478064Sume * pull single extension header from mbuf chain.  returns single mbuf that
137578064Sume * contains the result, or NULL on error.
137678064Sume */
137778064Sumestatic struct mbuf *
137878064Sumeip6_pullexthdr(m, off, nxt)
137978064Sume	struct mbuf *m;
138078064Sume	size_t off;
138178064Sume	int nxt;
138278064Sume{
138378064Sume	struct ip6_ext ip6e;
138478064Sume	size_t elen;
138578064Sume	struct mbuf *n;
138678064Sume
138778064Sume#ifdef DIAGNOSTIC
138878064Sume	switch (nxt) {
138978064Sume	case IPPROTO_DSTOPTS:
139078064Sume	case IPPROTO_ROUTING:
139178064Sume	case IPPROTO_HOPOPTS:
139278064Sume	case IPPROTO_AH: /* is it possible? */
139378064Sume		break;
139478064Sume	default:
139578064Sume		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
139653541Sshin	}
139778064Sume#endif
139878064Sume
139978064Sume	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
140078064Sume	if (nxt == IPPROTO_AH)
140178064Sume		elen = (ip6e.ip6e_len + 2) << 2;
140278064Sume	else
140378064Sume		elen = (ip6e.ip6e_len + 1) << 3;
140478064Sume
1405111119Simp	MGET(n, M_DONTWAIT, MT_DATA);
140678064Sume	if (n && elen >= MLEN) {
1407111119Simp		MCLGET(n, M_DONTWAIT);
140878064Sume		if ((n->m_flags & M_EXT) == 0) {
140978064Sume			m_free(n);
141078064Sume			n = NULL;
141178064Sume		}
141253541Sshin	}
141378064Sume	if (!n)
141478064Sume		return NULL;
141562587Sitojun
141678064Sume	n->m_len = 0;
141778064Sume	if (elen >= M_TRAILINGSPACE(n)) {
141878064Sume		m_free(n);
141978064Sume		return NULL;
142078064Sume	}
142178064Sume
142278064Sume	m_copydata(m, off, elen, mtod(n, caddr_t));
142378064Sume	n->m_len = elen;
142478064Sume	return n;
142553541Sshin}
142678064Sume#endif
142753541Sshin
142853541Sshin/*
142953541Sshin * Get pointer to the previous header followed by the header
143053541Sshin * currently processed.
143153541Sshin * XXX: This function supposes that
143253541Sshin *	M includes all headers,
143353541Sshin *	the next header field and the header length field of each header
143453541Sshin *	are valid, and
143553541Sshin *	the sum of each header length equals to OFF.
143653541Sshin * Because of these assumptions, this function must be called very
143753541Sshin * carefully. Moreover, it will not be used in the near future when
143853541Sshin * we develop `neater' mechanism to process extension headers.
143953541Sshin */
144053541Sshinchar *
144153541Sshinip6_get_prevhdr(m, off)
144253541Sshin	struct mbuf *m;
144353541Sshin	int off;
144453541Sshin{
144553541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
144653541Sshin
144753541Sshin	if (off == sizeof(struct ip6_hdr))
1448120856Sume		return (&ip6->ip6_nxt);
144953541Sshin	else {
145053541Sshin		int len, nxt;
145153541Sshin		struct ip6_ext *ip6e = NULL;
145253541Sshin
145353541Sshin		nxt = ip6->ip6_nxt;
145453541Sshin		len = sizeof(struct ip6_hdr);
145553541Sshin		while (len < off) {
145653541Sshin			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
145753541Sshin
145878064Sume			switch (nxt) {
145953541Sshin			case IPPROTO_FRAGMENT:
146053541Sshin				len += sizeof(struct ip6_frag);
146153541Sshin				break;
146253541Sshin			case IPPROTO_AH:
146353541Sshin				len += (ip6e->ip6e_len + 2) << 2;
146453541Sshin				break;
146553541Sshin			default:
146653541Sshin				len += (ip6e->ip6e_len + 1) << 3;
146753541Sshin				break;
146853541Sshin			}
146953541Sshin			nxt = ip6e->ip6e_nxt;
147053541Sshin		}
147153541Sshin		if (ip6e)
1472120856Sume			return (&ip6e->ip6e_nxt);
147353541Sshin		else
147453541Sshin			return NULL;
147553541Sshin	}
147653541Sshin}
147753541Sshin
147853541Sshin/*
147962587Sitojun * get next header offset.  m will be retained.
148062587Sitojun */
148162587Sitojunint
148262587Sitojunip6_nexthdr(m, off, proto, nxtp)
148362587Sitojun	struct mbuf *m;
148462587Sitojun	int off;
148562587Sitojun	int proto;
148662587Sitojun	int *nxtp;
148762587Sitojun{
148862587Sitojun	struct ip6_hdr ip6;
148962587Sitojun	struct ip6_ext ip6e;
149062587Sitojun	struct ip6_frag fh;
149162587Sitojun
149262587Sitojun	/* just in case */
149362587Sitojun	if (m == NULL)
149462587Sitojun		panic("ip6_nexthdr: m == NULL");
149562587Sitojun	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
149662587Sitojun		return -1;
149762587Sitojun
149862587Sitojun	switch (proto) {
149962587Sitojun	case IPPROTO_IPV6:
150062587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6))
150162587Sitojun			return -1;
150262587Sitojun		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
150362587Sitojun		if (nxtp)
150462587Sitojun			*nxtp = ip6.ip6_nxt;
150562587Sitojun		off += sizeof(ip6);
150662587Sitojun		return off;
150762587Sitojun
150862587Sitojun	case IPPROTO_FRAGMENT:
150962587Sitojun		/*
151062587Sitojun		 * terminate parsing if it is not the first fragment,
151162587Sitojun		 * it does not make sense to parse through it.
151262587Sitojun		 */
151362587Sitojun		if (m->m_pkthdr.len < off + sizeof(fh))
151462587Sitojun			return -1;
151562587Sitojun		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
151662587Sitojun		if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
151762587Sitojun			return -1;
151862587Sitojun		if (nxtp)
151962587Sitojun			*nxtp = fh.ip6f_nxt;
152062587Sitojun		off += sizeof(struct ip6_frag);
152162587Sitojun		return off;
152262587Sitojun
152362587Sitojun	case IPPROTO_AH:
152462587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6e))
152562587Sitojun			return -1;
152662587Sitojun		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
152762587Sitojun		if (nxtp)
152862587Sitojun			*nxtp = ip6e.ip6e_nxt;
152962587Sitojun		off += (ip6e.ip6e_len + 2) << 2;
153062587Sitojun		return off;
153162587Sitojun
153262587Sitojun	case IPPROTO_HOPOPTS:
153362587Sitojun	case IPPROTO_ROUTING:
153462587Sitojun	case IPPROTO_DSTOPTS:
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 + 1) << 3;
154162587Sitojun		return off;
154262587Sitojun
154362587Sitojun	case IPPROTO_NONE:
154462587Sitojun	case IPPROTO_ESP:
154562587Sitojun	case IPPROTO_IPCOMP:
154662587Sitojun		/* give up */
154762587Sitojun		return -1;
154862587Sitojun
154962587Sitojun	default:
155062587Sitojun		return -1;
155162587Sitojun	}
155262587Sitojun
155362587Sitojun	return -1;
155462587Sitojun}
155562587Sitojun
155662587Sitojun/*
155762587Sitojun * get offset for the last header in the chain.  m will be kept untainted.
155862587Sitojun */
155962587Sitojunint
156062587Sitojunip6_lasthdr(m, off, proto, nxtp)
156162587Sitojun	struct mbuf *m;
156262587Sitojun	int off;
156362587Sitojun	int proto;
156462587Sitojun	int *nxtp;
156562587Sitojun{
156662587Sitojun	int newoff;
156762587Sitojun	int nxt;
156862587Sitojun
156962587Sitojun	if (!nxtp) {
157062587Sitojun		nxt = -1;
157162587Sitojun		nxtp = &nxt;
157262587Sitojun	}
157362587Sitojun	while (1) {
157462587Sitojun		newoff = ip6_nexthdr(m, off, proto, nxtp);
157562587Sitojun		if (newoff < 0)
157662587Sitojun			return off;
157762587Sitojun		else if (newoff < off)
157862587Sitojun			return -1;	/* invalid */
157962587Sitojun		else if (newoff == off)
158062587Sitojun			return newoff;
158162587Sitojun
158262587Sitojun		off = newoff;
158362587Sitojun		proto = *nxtp;
158462587Sitojun	}
158562587Sitojun}
158662587Sitojun
1587105194Ssamstruct ip6aux *
158878064Sumeip6_addaux(m)
158978064Sume	struct mbuf *m;
159078064Sume{
1591105194Ssam	struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1592105194Ssam	if (!tag) {
1593105194Ssam		tag = m_tag_get(PACKET_TAG_IPV6_INPUT,
1594105194Ssam				sizeof (struct ip6aux),
1595112130Ssam				M_NOWAIT);
1596105194Ssam		if (tag)
1597105194Ssam			m_tag_prepend(m, tag);
159878064Sume	}
1599105194Ssam	if (tag)
1600105194Ssam		bzero(tag+1, sizeof (struct ip6aux));
1601105194Ssam	return tag ? (struct ip6aux*)(tag+1) : NULL;
160278064Sume}
160378064Sume
1604105194Ssamstruct ip6aux *
160578064Sumeip6_findaux(m)
160678064Sume	struct mbuf *m;
160778064Sume{
1608105194Ssam	struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1609105194Ssam	return tag ? (struct ip6aux*)(tag+1) : NULL;
161078064Sume}
161178064Sume
161278064Sumevoid
161378064Sumeip6_delaux(m)
161478064Sume	struct mbuf *m;
161578064Sume{
1616105194Ssam	struct m_tag *tag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1617105194Ssam	if (tag)
1618105194Ssam		m_tag_delete(m, tag);
161978064Sume}
162078064Sume
162162587Sitojun/*
162253541Sshin * System control for IP6
162353541Sshin */
162453541Sshin
162553541Sshinu_char	inet6ctlerrmap[PRC_NCMDS] = {
162653541Sshin	0,		0,		0,		0,
162753541Sshin	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
162853541Sshin	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
162953541Sshin	EMSGSIZE,	EHOSTUNREACH,	0,		0,
163053541Sshin	0,		0,		0,		0,
163153541Sshin	ENOPROTOOPT
163253541Sshin};
1633