ip6_input.c revision 166938
162587Sitojun/*	$FreeBSD: head/sys/netinet6/ip6_input.c 166938 2007-02-24 11:38:47Z bms $	*/
295023Ssuz/*	$KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $	*/
362587Sitojun
4139826Simp/*-
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
33139826Simp/*-
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 * 4. Neither the name of the University nor the names of its contributors
4653541Sshin *    may be used to endorse or promote products derived from this software
4753541Sshin *    without specific prior written permission.
4853541Sshin *
4953541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5053541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5153541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5253541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5353541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5453541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5553541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5653541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5753541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5853541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5953541Sshin * SUCH DAMAGE.
6053541Sshin *
6153541Sshin *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
6253541Sshin */
6353541Sshin
6462587Sitojun#include "opt_inet.h"
6562587Sitojun#include "opt_inet6.h"
6655009Sshin#include "opt_ipsec.h"
6755009Sshin
6853541Sshin#include <sys/param.h>
6953541Sshin#include <sys/systm.h>
7078064Sume#include <sys/malloc.h>
7153541Sshin#include <sys/mbuf.h>
7283366Sjulian#include <sys/proc.h>
7353541Sshin#include <sys/domain.h>
7453541Sshin#include <sys/protosw.h>
7553541Sshin#include <sys/socket.h>
7653541Sshin#include <sys/socketvar.h>
7753541Sshin#include <sys/errno.h>
7853541Sshin#include <sys/time.h>
7953541Sshin#include <sys/kernel.h>
8053541Sshin#include <sys/syslog.h>
8153541Sshin
8253541Sshin#include <net/if.h>
8353541Sshin#include <net/if_types.h>
8453541Sshin#include <net/if_dl.h>
8553541Sshin#include <net/route.h>
8653541Sshin#include <net/netisr.h>
8764060Sdarrenr#include <net/pfil.h>
8853541Sshin
8953541Sshin#include <netinet/in.h>
9053541Sshin#include <netinet/in_systm.h>
9162587Sitojun#ifdef INET
9253541Sshin#include <netinet/ip.h>
9353541Sshin#include <netinet/ip_icmp.h>
9495023Ssuz#endif /* INET */
9562587Sitojun#include <netinet/ip6.h>
9653541Sshin#include <netinet6/in6_var.h>
9753541Sshin#include <netinet6/ip6_var.h>
9862587Sitojun#include <netinet/in_pcb.h>
9962587Sitojun#include <netinet/icmp6.h>
100121161Sume#include <netinet6/scope6_var.h>
10153541Sshin#include <netinet6/in6_ifattach.h>
10253541Sshin#include <netinet6/nd6.h>
10353541Sshin
10478064Sume#ifdef IPSEC
10578064Sume#include <netinet6/ipsec.h>
10678064Sume#ifdef INET6
10778064Sume#include <netinet6/ipsec6.h>
10878064Sume#endif
10978064Sume#endif
11078064Sume
111105199Ssam#ifdef FAST_IPSEC
112105199Ssam#include <netipsec/ipsec.h>
113105199Ssam#include <netipsec/ipsec6.h>
114105199Ssam#define	IPSEC
115105199Ssam#endif /* FAST_IPSEC */
116105199Ssam
11753541Sshin#include <netinet6/ip6protosw.h>
11853541Sshin
11962587Sitojunextern struct domain inet6domain;
12053541Sshin
12162587Sitojunu_char ip6_protox[IPPROTO_MAX];
122111888Sjlemonstatic struct ifqueue ip6intrq;
12362587Sitojunstatic int ip6qmaxlen = IFQ_MAXLEN;
12462587Sitojunstruct in6_ifaddr *in6_ifaddr;
12553541Sshin
12678064Sumeextern struct callout in6_tmpaddrtimer_ch;
12778064Sume
12862587Sitojunint ip6_forward_srcrt;			/* XXX */
12962587Sitojunint ip6_sourcecheck;			/* XXX */
13062587Sitojunint ip6_sourcecheck_interval;		/* XXX */
13153541Sshin
13278064Sumeint ip6_ours_check_algorithm;
13378064Sume
134120386Ssamstruct pfil_head inet6_pfil_hook;
13578064Sume
13662587Sitojunstruct ip6stat ip6stat;
13753541Sshin
13862587Sitojunstatic void ip6_init2 __P((void *));
139121673Sumestatic struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *));
14062587Sitojunstatic int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
14162587Sitojun#ifdef PULLDOWN_TEST
14262587Sitojunstatic struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int));
14353541Sshin#endif
14453541Sshin
14553541Sshin/*
14653541Sshin * IP6 initialization: fill in IP6 protocol switch table.
14753541Sshin * All protocols not implemented in kernel go to raw IP6 protocol handler.
14853541Sshin */
14953541Sshinvoid
15053541Sshinip6_init()
15153541Sshin{
15278064Sume	struct ip6protosw *pr;
15378064Sume	int i;
15453541Sshin
15578064Sume#ifdef DIAGNOSTIC
15678064Sume	if (sizeof(struct protosw) != sizeof(struct ip6protosw))
15778064Sume		panic("sizeof(protosw) != sizeof(ip6protosw)");
15878064Sume#endif
15953541Sshin	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
16053541Sshin	if (pr == 0)
16153541Sshin		panic("ip6_init");
162136689Sandre
163136689Sandre	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
16453541Sshin	for (i = 0; i < IPPROTO_MAX; i++)
16553541Sshin		ip6_protox[i] = pr - inet6sw;
166136689Sandre	/*
167136689Sandre	 * Cycle through IP protocols and put them into the appropriate place
168136689Sandre	 * in ip6_protox[].
169136689Sandre	 */
17053541Sshin	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
17153541Sshin	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
17253541Sshin		if (pr->pr_domain->dom_family == PF_INET6 &&
173136689Sandre		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
174136689Sandre			/* Be careful to only index valid IP protocols. */
175143675Ssam			if (pr->pr_protocol < IPPROTO_MAX)
176136689Sandre				ip6_protox[pr->pr_protocol] = pr - inet6sw;
177136689Sandre		}
178134383Sandre
179134383Sandre	/* Initialize packet filter hooks. */
180120386Ssam	inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
181120386Ssam	inet6_pfil_hook.ph_af = AF_INET6;
182120386Ssam	if ((i = pfil_head_register(&inet6_pfil_hook)) != 0)
183120386Ssam		printf("%s: WARNING: unable to register pfil hook, "
184120386Ssam			"error %d\n", __func__, i);
185134383Sandre
18653541Sshin	ip6intrq.ifq_maxlen = ip6qmaxlen;
18793818Sjhb	mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF);
188122320Ssam	netisr_register(NETISR_IPV6, ip6_input, &ip6intrq, 0);
189121161Sume	scope6_init();
190121742Sume	addrsel_policy_init();
19153541Sshin	nd6_init();
19253541Sshin	frag6_init();
193120648Sume	ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
19453541Sshin}
19553541Sshin
19653541Sshinstatic void
19753541Sshinip6_init2(dummy)
19853541Sshin	void *dummy;
19953541Sshin{
20053541Sshin
20153541Sshin	/* nd6_timer_init */
20278064Sume	callout_init(&nd6_timer_ch, 0);
20378064Sume	callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
20478064Sume
20578064Sume	/* timer for regeneranation of temporary addresses randomize ID */
206121806Sume	callout_init(&in6_tmpaddrtimer_ch, 0);
20778064Sume	callout_reset(&in6_tmpaddrtimer_ch,
20878064Sume		      (ip6_temp_preferred_lifetime - ip6_desync_factor -
20978064Sume		       ip6_temp_regen_advance) * hz,
21078064Sume		      in6_tmpaddrtimer, NULL);
21153541Sshin}
21253541Sshin
21353541Sshin/* cheat */
21455009Sshin/* This must be after route_init(), which is now SI_ORDER_THIRD */
21555009SshinSYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
21653541Sshin
21753541Sshinextern struct	route_in6 ip6_forward_rt;
21853541Sshin
21953541Sshinvoid
22053541Sshinip6_input(m)
22153541Sshin	struct mbuf *m;
22253541Sshin{
22353541Sshin	struct ip6_hdr *ip6;
22453541Sshin	int off = sizeof(struct ip6_hdr), nest;
22553541Sshin	u_int32_t plen;
22653541Sshin	u_int32_t rtalert = ~0;
22753541Sshin	int nxt, ours = 0;
22853541Sshin	struct ifnet *deliverifp = NULL;
229121143Ssam	struct in6_addr odst;
230121143Ssam	int srcrt = 0;
231121144Ssam
232122320Ssam	GIANT_REQUIRED;			/* XXX for now */
23353541Sshin#ifdef IPSEC
23453541Sshin	/*
23553541Sshin	 * should the inner packet be considered authentic?
23653541Sshin	 * see comment in ah4_input().
23753541Sshin	 */
23853541Sshin	if (m) {
23953541Sshin		m->m_flags &= ~M_AUTHIPHDR;
24053541Sshin		m->m_flags &= ~M_AUTHIPDGM;
24153541Sshin	}
24253541Sshin#endif
24353541Sshin
24453541Sshin	/*
245121630Sume	 * make sure we don't have onion peering information into m_tag.
24678064Sume	 */
24778064Sume	ip6_delaux(m);
24878064Sume
24978064Sume	/*
25095023Ssuz	 * mbuf statistics
25153541Sshin	 */
25253541Sshin	if (m->m_flags & M_EXT) {
25353541Sshin		if (m->m_next)
25453541Sshin			ip6stat.ip6s_mext2m++;
25553541Sshin		else
25653541Sshin			ip6stat.ip6s_mext1++;
25753541Sshin	} else {
25878064Sume#define M2MMAX	(sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
25953541Sshin		if (m->m_next) {
26053541Sshin			if (m->m_flags & M_LOOP) {
261120913Sume				ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
26278064Sume			} else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
26353541Sshin				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
26453541Sshin			else
26553541Sshin				ip6stat.ip6s_m2m[0]++;
26653541Sshin		} else
26753541Sshin			ip6stat.ip6s_m1++;
26878064Sume#undef M2MMAX
26953541Sshin	}
27053541Sshin
271151474Ssuz	/* drop the packet if IPv6 operation is disabled on the IF */
272151474Ssuz	if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) {
273151474Ssuz		m_freem(m);
274151474Ssuz		return;
275151474Ssuz	}
276151474Ssuz
27753541Sshin	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
27853541Sshin	ip6stat.ip6s_total++;
27953541Sshin
28062587Sitojun#ifndef PULLDOWN_TEST
28174336Skuriyama	/*
28274336Skuriyama	 * L2 bridge code and some other code can return mbuf chain
28374336Skuriyama	 * that does not conform to KAME requirement.  too bad.
28474336Skuriyama	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
28574336Skuriyama	 */
28674336Skuriyama	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
28774336Skuriyama		struct mbuf *n;
28874336Skuriyama
289111119Simp		MGETHDR(n, M_DONTWAIT, MT_HEADER);
29077003Sume		if (n)
291108466Ssam			M_MOVE_PKTHDR(n, m);
292108825Ssam		if (n && n->m_pkthdr.len > MHLEN) {
293111119Simp			MCLGET(n, M_DONTWAIT);
29474336Skuriyama			if ((n->m_flags & M_EXT) == 0) {
29574336Skuriyama				m_freem(n);
29674336Skuriyama				n = NULL;
29774336Skuriyama			}
29874336Skuriyama		}
29995023Ssuz		if (n == NULL) {
30077003Sume			m_freem(m);
301120913Sume			return;	/* ENOBUFS */
30277003Sume		}
30374336Skuriyama
304108825Ssam		m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
305108825Ssam		n->m_len = n->m_pkthdr.len;
30674336Skuriyama		m_freem(m);
30774336Skuriyama		m = n;
30874336Skuriyama	}
309120913Sume	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
31062587Sitojun#endif
31153541Sshin
31253541Sshin	if (m->m_len < sizeof(struct ip6_hdr)) {
31353541Sshin		struct ifnet *inifp;
31453541Sshin		inifp = m->m_pkthdr.rcvif;
315120913Sume		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
31653541Sshin			ip6stat.ip6s_toosmall++;
31753541Sshin			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
31853541Sshin			return;
31953541Sshin		}
32053541Sshin	}
32153541Sshin
32253541Sshin	ip6 = mtod(m, struct ip6_hdr *);
32353541Sshin
32453541Sshin	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
32553541Sshin		ip6stat.ip6s_badvers++;
32653541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
32753541Sshin		goto bad;
32853541Sshin	}
32953541Sshin
33053541Sshin	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
33153541Sshin
33253541Sshin	/*
33378064Sume	 * Check against address spoofing/corruption.
33453541Sshin	 */
33553541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
33653541Sshin	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
33778064Sume		/*
33878064Sume		 * XXX: "badscope" is not very suitable for a multicast source.
33978064Sume		 */
34053541Sshin		ip6stat.ip6s_badscope++;
34153541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
34253541Sshin		goto bad;
34353541Sshin	}
344126444Sume	if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
345126444Sume	    !(m->m_flags & M_LOOP)) {
346126444Sume		/*
347126444Sume		 * In this case, the packet should come from the loopback
348126444Sume		 * interface.  However, we cannot just check the if_flags,
349126444Sume		 * because ip6_mloopback() passes the "actual" interface
350126444Sume		 * as the outgoing/incoming interface.
351126444Sume		 */
35278064Sume		ip6stat.ip6s_badscope++;
35378064Sume		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
35478064Sume		goto bad;
35578064Sume	}
35695023Ssuz
357130416Smlaier#ifdef ALTQ
358130416Smlaier	if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
359130416Smlaier		/* packet is dropped by traffic conditioner */
360130416Smlaier		return;
361130416Smlaier	}
362130416Smlaier#endif
36362587Sitojun	/*
36478064Sume	 * The following check is not documented in specs.  A malicious
36578064Sume	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
36678064Sume	 * and bypass security checks (act as if it was from 127.0.0.1 by using
367120913Sume	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
36878064Sume	 *
36978064Sume	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
37078064Sume	 * support IPv4-less kernel compilation, we cannot support SIIT
37178064Sume	 * environment at all.  So, it makes more sense for us to reject any
37278064Sume	 * malicious packets for non-SIIT environment, than try to do a
373120913Sume	 * partial support for SIIT environment.
37462587Sitojun	 */
37578064Sume	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
37678064Sume	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
37778064Sume		ip6stat.ip6s_badscope++;
37878064Sume		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
37978064Sume		goto bad;
38078064Sume	}
38162587Sitojun#if 0
38262587Sitojun	/*
38362587Sitojun	 * Reject packets with IPv4 compatible addresses (auto tunnel).
38462587Sitojun	 *
38562587Sitojun	 * The code forbids auto tunnel relay case in RFC1933 (the check is
38662587Sitojun	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
38762587Sitojun	 * is revised to forbid relaying case.
38862587Sitojun	 */
38962587Sitojun	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
39062587Sitojun	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
39162587Sitojun		ip6stat.ip6s_badscope++;
39262587Sitojun		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
39362587Sitojun		goto bad;
39462587Sitojun	}
39562587Sitojun#endif
39675246Sume
397126444Sume	/*
398126508Smlaier	 * Run through list of hooks for input packets.
399126508Smlaier	 *
400126508Smlaier	 * NB: Beware of the destination address changing
401126508Smlaier	 *     (e.g. by NAT rewriting).  When this happens,
402126508Smlaier	 *     tell ip6_forward to do the right thing.
403126508Smlaier	 */
404126508Smlaier	odst = ip6->ip6_dst;
405134383Sandre
406134383Sandre	/* Jump over all PFIL processing if hooks are not active. */
407155201Scsjp	if (!PFIL_HOOKED(&inet6_pfil_hook))
408134383Sandre		goto passin;
409134383Sandre
410135920Smlaier	if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL))
411126508Smlaier		return;
412126508Smlaier	if (m == NULL)			/* consumed by filter */
413126508Smlaier		return;
414126508Smlaier	ip6 = mtod(m, struct ip6_hdr *);
415126508Smlaier	srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
416126508Smlaier
417134383Sandrepassin:
418126508Smlaier	/*
419154804Sume	 * Disambiguate address scope zones (if there is ambiguity).
420154804Sume	 * We first make sure that the original source or destination address
421154804Sume	 * is not in our internal form for scoped addresses.  Such addresses
422154804Sume	 * are not necessarily invalid spec-wise, but we cannot accept them due
423154804Sume	 * to the usage conflict.
424154804Sume	 * in6_setscope() then also checks and rejects the cases where src or
425154804Sume	 * dst are the loopback address and the receiving interface
426154804Sume	 * is not loopback.
427154804Sume	 */
428154804Sume	if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
429154804Sume		ip6stat.ip6s_badscope++; /* XXX */
430154804Sume		goto bad;
431154804Sume	}
432154804Sume	if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) ||
433154804Sume	    in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) {
434154804Sume		ip6stat.ip6s_badscope++;
435154804Sume		goto bad;
436154804Sume	}
437154804Sume
438154804Sume	/*
43953541Sshin	 * Multicast check
44053541Sshin	 */
44153541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
442120913Sume	  	struct in6_multi *in6m = 0;
44353541Sshin
44453541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
44553541Sshin		/*
44653541Sshin		 * See if we belong to the destination multicast group on the
44753541Sshin		 * arrival interface.
44853541Sshin		 */
44953541Sshin		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
45053541Sshin		if (in6m)
45153541Sshin			ours = 1;
45256723Sshin		else if (!ip6_mrouter) {
45353541Sshin			ip6stat.ip6s_notmember++;
45453541Sshin			ip6stat.ip6s_cantforward++;
45553541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
45653541Sshin			goto bad;
45753541Sshin		}
45853541Sshin		deliverifp = m->m_pkthdr.rcvif;
45953541Sshin		goto hbhcheck;
46053541Sshin	}
46153541Sshin
46253541Sshin	/*
46353541Sshin	 *  Unicast check
46453541Sshin	 */
46562587Sitojun	if (ip6_forward_rt.ro_rt != NULL &&
466120913Sume	    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
46762587Sitojun	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
468120913Sume	    &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
46962587Sitojun		ip6stat.ip6s_forward_cachehit++;
47062587Sitojun	else {
47178064Sume		struct sockaddr_in6 *dst6;
47278064Sume
47353541Sshin		if (ip6_forward_rt.ro_rt) {
47462587Sitojun			/* route is down or destination is different */
47562587Sitojun			ip6stat.ip6s_forward_cachemiss++;
47653541Sshin			RTFREE(ip6_forward_rt.ro_rt);
47753541Sshin			ip6_forward_rt.ro_rt = 0;
47853541Sshin		}
47962587Sitojun
48053541Sshin		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
48178064Sume		dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
48278064Sume		dst6->sin6_len = sizeof(struct sockaddr_in6);
48378064Sume		dst6->sin6_family = AF_INET6;
48478064Sume		dst6->sin6_addr = ip6->ip6_dst;
48553541Sshin
486122921Sandre		rtalloc((struct route *)&ip6_forward_rt);
48753541Sshin	}
48853541Sshin
48953541Sshin#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
49053541Sshin
49153541Sshin	/*
49253541Sshin	 * Accept the packet if the forwarding interface to the destination
49353541Sshin	 * according to the routing table is the loopback interface,
49453541Sshin	 * unless the associated route has a gateway.
49553541Sshin	 * Note that this approach causes to accept a packet if there is a
49653541Sshin	 * route to the loopback interface for the destination of the packet.
49753541Sshin	 * But we think it's even useful in some situations, e.g. when using
49853541Sshin	 * a special daemon which wants to intercept the packet.
49978064Sume	 *
50078064Sume	 * XXX: some OSes automatically make a cloned route for the destination
50178064Sume	 * of an outgoing packet.  If the outgoing interface of the packet
50278064Sume	 * is a loopback one, the kernel would consider the packet to be
50378064Sume	 * accepted, even if we have no such address assinged on the interface.
50478064Sume	 * We check the cloned flag of the route entry to reject such cases,
50578064Sume	 * assuming that route entries for our own addresses are not made by
50678064Sume	 * cloning (it should be true because in6_addloop explicitly installs
50778064Sume	 * the host route).  However, we might have to do an explicit check
50878064Sume	 * while it would be less efficient.  Or, should we rather install a
50978064Sume	 * reject route for such a case?
51053541Sshin	 */
51153541Sshin	if (ip6_forward_rt.ro_rt &&
51253541Sshin	    (ip6_forward_rt.ro_rt->rt_flags &
51353541Sshin	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
51478064Sume#ifdef RTF_WASCLONED
51578064Sume	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
51678064Sume#endif
51778064Sume#ifdef RTF_CLONED
51878064Sume	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
51978064Sume#endif
52062587Sitojun#if 0
52153541Sshin	    /*
52262587Sitojun	     * The check below is redundant since the comparison of
52362587Sitojun	     * the destination and the key of the rtentry has
52462587Sitojun	     * already done through looking up the routing table.
52553541Sshin	     */
52662587Sitojun	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
527120913Sume	    &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
52862587Sitojun#endif
52953541Sshin	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
53053541Sshin		struct in6_ifaddr *ia6 =
53153541Sshin			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
53278064Sume
53362587Sitojun		/*
534121630Sume		 * record address information into m_tag.
53578064Sume		 */
53678064Sume		(void)ip6_setdstifaddr(m, ia6);
53778064Sume
53878064Sume		/*
53962587Sitojun		 * packets to a tentative, duplicated, or somehow invalid
54062587Sitojun		 * address must not be accepted.
54162587Sitojun		 */
54253541Sshin		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
54362587Sitojun			/* this address is ready */
54453541Sshin			ours = 1;
54553541Sshin			deliverifp = ia6->ia_ifp;	/* correct? */
54667334Sjoe			/* Count the packet in the ip address stats */
54767334Sjoe			ia6->ia_ifa.if_ipackets++;
54867334Sjoe			ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
54953541Sshin			goto hbhcheck;
55053541Sshin		} else {
551165118Sbz			char ip6bufs[INET6_ADDRSTRLEN];
552165118Sbz			char ip6bufd[INET6_ADDRSTRLEN];
55362587Sitojun			/* address is not ready, so discard the packet. */
55478064Sume			nd6log((LOG_INFO,
55578064Sume			    "ip6_input: packet to an unready address %s->%s\n",
556165118Sbz			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
557165118Sbz			    ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
55862587Sitojun
55962587Sitojun			goto bad;
56053541Sshin		}
56153541Sshin	}
56253541Sshin
56353541Sshin	/*
564120913Sume	 * FAITH (Firewall Aided Internet Translator)
56553541Sshin	 */
56653541Sshin	if (ip6_keepfaith) {
56753541Sshin		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
56853541Sshin		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
56953541Sshin			/* XXX do we need more sanity checks? */
57053541Sshin			ours = 1;
57195023Ssuz			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
57253541Sshin			goto hbhcheck;
57353541Sshin		}
57453541Sshin	}
57553541Sshin
57653541Sshin	/*
57753541Sshin	 * Now there is no reason to process the packet if it's not our own
57853541Sshin	 * and we're not a router.
57953541Sshin	 */
58053541Sshin	if (!ip6_forwarding) {
58153541Sshin		ip6stat.ip6s_cantforward++;
58253541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
58353541Sshin		goto bad;
58453541Sshin	}
58553541Sshin
58653541Sshin  hbhcheck:
58753541Sshin	/*
588121630Sume	 * record address information into m_tag, if we don't have one yet.
58978064Sume	 * note that we are unable to record it, if the address is not listed
59078064Sume	 * as our interface address (e.g. multicast addresses, addresses
59178064Sume	 * within FAITH prefixes and such).
59278064Sume	 */
59378064Sume	if (deliverifp && !ip6_getdstifaddr(m)) {
59478064Sume		struct in6_ifaddr *ia6;
59578064Sume
59678064Sume		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
59778064Sume		if (ia6) {
59878064Sume			if (!ip6_setdstifaddr(m, ia6)) {
59978064Sume				/*
60078064Sume				 * XXX maybe we should drop the packet here,
60178064Sume				 * as we could not provide enough information
60278064Sume				 * to the upper layers.
60378064Sume				 */
60478064Sume			}
60578064Sume		}
60678064Sume	}
60778064Sume
60878064Sume	/*
60953541Sshin	 * Process Hop-by-Hop options header if it's contained.
61053541Sshin	 * m may be modified in ip6_hopopts_input().
61153541Sshin	 * If a JumboPayload option is included, plen will also be modified.
61253541Sshin	 */
61353541Sshin	plen = (u_int32_t)ntohs(ip6->ip6_plen);
61453541Sshin	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
61562587Sitojun		struct ip6_hbh *hbh;
61662587Sitojun
61753541Sshin		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
61862587Sitojun#if 0	/*touches NULL pointer*/
61953541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
62062587Sitojun#endif
62153541Sshin			return;	/* m have already been freed */
62253541Sshin		}
62362587Sitojun
62453541Sshin		/* adjust pointer */
62553541Sshin		ip6 = mtod(m, struct ip6_hdr *);
62653541Sshin
62753541Sshin		/*
62895023Ssuz		 * if the payload length field is 0 and the next header field
62962587Sitojun		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
63062587Sitojun		 * option MUST be included.
63162587Sitojun		 */
63262587Sitojun		if (ip6->ip6_plen == 0 && plen == 0) {
63362587Sitojun			/*
63462587Sitojun			 * Note that if a valid jumbo payload option is
635120913Sume			 * contained, ip6_hopopts_input() must set a valid
636120913Sume			 * (non-zero) payload length to the variable plen.
63762587Sitojun			 */
63862587Sitojun			ip6stat.ip6s_badoptions++;
63962587Sitojun			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
64062587Sitojun			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
64162587Sitojun			icmp6_error(m, ICMP6_PARAM_PROB,
64262587Sitojun				    ICMP6_PARAMPROB_HEADER,
64362587Sitojun				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
64462587Sitojun			return;
64562587Sitojun		}
64662587Sitojun#ifndef PULLDOWN_TEST
64762587Sitojun		/* ip6_hopopts_input() ensures that mbuf is contiguous */
64862587Sitojun		hbh = (struct ip6_hbh *)(ip6 + 1);
64962587Sitojun#else
65062587Sitojun		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
65162587Sitojun			sizeof(struct ip6_hbh));
65262587Sitojun		if (hbh == NULL) {
65362587Sitojun			ip6stat.ip6s_tooshort++;
65462587Sitojun			return;
65562587Sitojun		}
65662587Sitojun#endif
65762587Sitojun		nxt = hbh->ip6h_nxt;
65862587Sitojun
65962587Sitojun		/*
66053541Sshin		 * accept the packet if a router alert option is included
66153541Sshin		 * and we act as an IPv6 router.
66253541Sshin		 */
66353541Sshin		if (rtalert != ~0 && ip6_forwarding)
66453541Sshin			ours = 1;
66553541Sshin	} else
66653541Sshin		nxt = ip6->ip6_nxt;
66753541Sshin
66853541Sshin	/*
66953541Sshin	 * Check that the amount of data in the buffers
67053541Sshin	 * is as at least much as the IPv6 header would have us expect.
67153541Sshin	 * Trim mbufs if longer than we expect.
67253541Sshin	 * Drop packet if shorter than we expect.
67353541Sshin	 */
67453541Sshin	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
67553541Sshin		ip6stat.ip6s_tooshort++;
67653541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
67753541Sshin		goto bad;
67853541Sshin	}
67953541Sshin	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
68053541Sshin		if (m->m_len == m->m_pkthdr.len) {
68153541Sshin			m->m_len = sizeof(struct ip6_hdr) + plen;
68253541Sshin			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
68353541Sshin		} else
68453541Sshin			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
68553541Sshin	}
68653541Sshin
68753541Sshin	/*
68853541Sshin	 * Forward if desirable.
68953541Sshin	 */
69053541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
69156723Sshin		/*
69256723Sshin		 * If we are acting as a multicast router, all
69356723Sshin		 * incoming multicast packets are passed to the
69456723Sshin		 * kernel-level multicast forwarding function.
69556723Sshin		 * The packet is returned (relatively) intact; if
69656723Sshin		 * ip6_mforward() returns a non-zero value, the packet
69756723Sshin		 * must be discarded, else it may be accepted below.
69856723Sshin		 */
699166938Sbms		if (ip6_mrouter && ip6_mforward &&
700166938Sbms		    ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
70156723Sshin			ip6stat.ip6s_cantforward++;
70256723Sshin			m_freem(m);
70356723Sshin			return;
70456723Sshin		}
70553541Sshin		if (!ours) {
70653541Sshin			m_freem(m);
70753541Sshin			return;
70853541Sshin		}
70953541Sshin	} else if (!ours) {
710121143Ssam		ip6_forward(m, srcrt);
71153541Sshin		return;
712121673Sume	}
71353541Sshin
71462587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
71562587Sitojun
71653541Sshin	/*
71762587Sitojun	 * Malicious party may be able to use IPv4 mapped addr to confuse
71862587Sitojun	 * tcp/udp stack and bypass security checks (act as if it was from
71962587Sitojun	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
72062587Sitojun	 *
72162587Sitojun	 * For SIIT end node behavior, you may want to disable the check.
72262587Sitojun	 * However, you will  become vulnerable to attacks using IPv4 mapped
72362587Sitojun	 * source.
72462587Sitojun	 */
72562587Sitojun	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
72662587Sitojun	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
72762587Sitojun		ip6stat.ip6s_badscope++;
72862587Sitojun		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
72962587Sitojun		goto bad;
73062587Sitojun	}
73162587Sitojun
73262587Sitojun	/*
73353541Sshin	 * Tell launch routine the next header
73453541Sshin	 */
73553541Sshin	ip6stat.ip6s_delivered++;
73653541Sshin	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
73753541Sshin	nest = 0;
73878064Sume
73953541Sshin	while (nxt != IPPROTO_DONE) {
74053541Sshin		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
74153541Sshin			ip6stat.ip6s_toomanyhdr++;
74253541Sshin			goto bad;
74353541Sshin		}
74453541Sshin
74553541Sshin		/*
74653541Sshin		 * protection against faulty packet - there should be
74753541Sshin		 * more sanity checks in header chain processing.
74853541Sshin		 */
74953541Sshin		if (m->m_pkthdr.len < off) {
75053541Sshin			ip6stat.ip6s_tooshort++;
75153541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
75253541Sshin			goto bad;
75353541Sshin		}
75453541Sshin
75578064Sume#ifdef IPSEC
75678064Sume		/*
75778064Sume		 * enforce IPsec policy checking if we are seeing last header.
75878064Sume		 * note that we do not visit this with protocols with pcb layer
75978064Sume		 * code - like udp/tcp/raw ip.
76078064Sume		 */
76178064Sume		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
76278064Sume		    ipsec6_in_reject(m, NULL)) {
76378064Sume			ipsec6stat.in_polvio++;
76478064Sume			goto bad;
76578064Sume		}
76678064Sume#endif
76753541Sshin		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
76853541Sshin	}
76953541Sshin	return;
77053541Sshin bad:
77153541Sshin	m_freem(m);
77253541Sshin}
77353541Sshin
77453541Sshin/*
77578064Sume * set/grab in6_ifaddr correspond to IPv6 destination address.
77678064Sume * XXX backward compatibility wrapper
77778064Sume */
778121673Sumestatic struct ip6aux *
77978064Sumeip6_setdstifaddr(m, ia6)
78078064Sume	struct mbuf *m;
78178064Sume	struct in6_ifaddr *ia6;
78278064Sume{
783121673Sume	struct ip6aux *ip6a;
78478064Sume
785121673Sume	ip6a = ip6_addaux(m);
786121673Sume	if (ip6a)
787121673Sume		ip6a->ip6a_dstia6 = ia6;
788121673Sume	return ip6a;	/* NULL if failed to set */
78978064Sume}
79078064Sume
79178064Sumestruct in6_ifaddr *
79278064Sumeip6_getdstifaddr(m)
79378064Sume	struct mbuf *m;
79478064Sume{
795121673Sume	struct ip6aux *ip6a;
79678064Sume
797121673Sume	ip6a = ip6_findaux(m);
798121673Sume	if (ip6a)
799121673Sume		return ip6a->ip6a_dstia6;
80078064Sume	else
80178064Sume		return NULL;
80278064Sume}
80378064Sume
80478064Sume/*
80553541Sshin * Hop-by-Hop options header processing. If a valid jumbo payload option is
80653541Sshin * included, the real payload length will be stored in plenp.
80753541Sshin */
80853541Sshinstatic int
80953541Sshinip6_hopopts_input(plenp, rtalertp, mp, offp)
81053541Sshin	u_int32_t *plenp;
81153541Sshin	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
81253541Sshin	struct mbuf **mp;
81353541Sshin	int *offp;
81453541Sshin{
81578064Sume	struct mbuf *m = *mp;
81653541Sshin	int off = *offp, hbhlen;
81753541Sshin	struct ip6_hbh *hbh;
81853541Sshin	u_int8_t *opt;
81953541Sshin
82053541Sshin	/* validation of the length of the header */
82162587Sitojun#ifndef PULLDOWN_TEST
82253541Sshin	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
82353541Sshin	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
82453541Sshin	hbhlen = (hbh->ip6h_len + 1) << 3;
82553541Sshin
82653541Sshin	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
82753541Sshin	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
82862587Sitojun#else
82962587Sitojun	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
83062587Sitojun		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
83162587Sitojun	if (hbh == NULL) {
83262587Sitojun		ip6stat.ip6s_tooshort++;
83362587Sitojun		return -1;
83462587Sitojun	}
83562587Sitojun	hbhlen = (hbh->ip6h_len + 1) << 3;
83662587Sitojun	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
83762587Sitojun		hbhlen);
83862587Sitojun	if (hbh == NULL) {
83962587Sitojun		ip6stat.ip6s_tooshort++;
84062587Sitojun		return -1;
84162587Sitojun	}
84262587Sitojun#endif
84353541Sshin	off += hbhlen;
84453541Sshin	hbhlen -= sizeof(struct ip6_hbh);
84553541Sshin	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
84653541Sshin
84753541Sshin	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
84853541Sshin				hbhlen, rtalertp, plenp) < 0)
849120856Sume		return (-1);
85053541Sshin
85153541Sshin	*offp = off;
85253541Sshin	*mp = m;
853120856Sume	return (0);
85453541Sshin}
85553541Sshin
85653541Sshin/*
85753541Sshin * Search header for all Hop-by-hop options and process each option.
85853541Sshin * This function is separate from ip6_hopopts_input() in order to
85953541Sshin * handle a case where the sending node itself process its hop-by-hop
86053541Sshin * options header. In such a case, the function is called from ip6_output().
86178064Sume *
86278064Sume * The function assumes that hbh header is located right after the IPv6 header
86378064Sume * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
86478064Sume * opthead + hbhlen is located in continuous memory region.
86553541Sshin */
86653541Sshinint
86753541Sshinip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
86853541Sshin	struct mbuf *m;
86953541Sshin	u_int8_t *opthead;
87053541Sshin	int hbhlen;
87153541Sshin	u_int32_t *rtalertp;
87253541Sshin	u_int32_t *plenp;
87353541Sshin{
87453541Sshin	struct ip6_hdr *ip6;
87553541Sshin	int optlen = 0;
87653541Sshin	u_int8_t *opt = opthead;
87753541Sshin	u_int16_t rtalert_val;
87862587Sitojun	u_int32_t jumboplen;
87978064Sume	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
88053541Sshin
88153541Sshin	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
88278064Sume		switch (*opt) {
88378064Sume		case IP6OPT_PAD1:
88478064Sume			optlen = 1;
88578064Sume			break;
88678064Sume		case IP6OPT_PADN:
88778064Sume			if (hbhlen < IP6OPT_MINLEN) {
88878064Sume				ip6stat.ip6s_toosmall++;
88978064Sume				goto bad;
89078064Sume			}
89178064Sume			optlen = *(opt + 1) + 2;
89278064Sume			break;
893121472Sume		case IP6OPT_ROUTER_ALERT:
89462587Sitojun			/* XXX may need check for alignment */
89578064Sume			if (hbhlen < IP6OPT_RTALERT_LEN) {
89678064Sume				ip6stat.ip6s_toosmall++;
89778064Sume				goto bad;
89878064Sume			}
89978064Sume			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
90078064Sume				/* XXX stat */
90178064Sume				icmp6_error(m, ICMP6_PARAM_PROB,
902120913Sume				    ICMP6_PARAMPROB_HEADER,
903120913Sume				    erroff + opt + 1 - opthead);
904120856Sume				return (-1);
90578064Sume			}
90678064Sume			optlen = IP6OPT_RTALERT_LEN;
90778064Sume			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
90878064Sume			*rtalertp = ntohs(rtalert_val);
90978064Sume			break;
91078064Sume		case IP6OPT_JUMBO:
91178064Sume			/* XXX may need check for alignment */
91262587Sitojun			if (hbhlen < IP6OPT_JUMBO_LEN) {
91362587Sitojun				ip6stat.ip6s_toosmall++;
91462587Sitojun				goto bad;
91562587Sitojun			}
91678064Sume			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
91778064Sume				/* XXX stat */
91878064Sume				icmp6_error(m, ICMP6_PARAM_PROB,
919120913Sume				    ICMP6_PARAMPROB_HEADER,
920120913Sume				    erroff + opt + 1 - opthead);
921120856Sume				return (-1);
92278064Sume			}
92362587Sitojun			optlen = IP6OPT_JUMBO_LEN;
92453541Sshin
92562587Sitojun			/*
92662587Sitojun			 * IPv6 packets that have non 0 payload length
92778064Sume			 * must not contain a jumbo payload option.
92862587Sitojun			 */
92962587Sitojun			ip6 = mtod(m, struct ip6_hdr *);
93062587Sitojun			if (ip6->ip6_plen) {
93162587Sitojun				ip6stat.ip6s_badoptions++;
93262587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
933120913Sume				    ICMP6_PARAMPROB_HEADER,
934120913Sume				    erroff + opt - opthead);
935120856Sume				return (-1);
93662587Sitojun			}
93753541Sshin
93862587Sitojun			/*
93962587Sitojun			 * We may see jumbolen in unaligned location, so
94062587Sitojun			 * we'd need to perform bcopy().
94162587Sitojun			 */
94262587Sitojun			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
94362587Sitojun			jumboplen = (u_int32_t)htonl(jumboplen);
94462587Sitojun
94562587Sitojun#if 1
94662587Sitojun			/*
94762587Sitojun			 * if there are multiple jumbo payload options,
94862587Sitojun			 * *plenp will be non-zero and the packet will be
94962587Sitojun			 * rejected.
95062587Sitojun			 * the behavior may need some debate in ipngwg -
95162587Sitojun			 * multiple options does not make sense, however,
95262587Sitojun			 * there's no explicit mention in specification.
95362587Sitojun			 */
95462587Sitojun			if (*plenp != 0) {
95562587Sitojun				ip6stat.ip6s_badoptions++;
95662587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
957120913Sume				    ICMP6_PARAMPROB_HEADER,
958120913Sume				    erroff + opt + 2 - opthead);
959120856Sume				return (-1);
96062587Sitojun			}
96162587Sitojun#endif
96262587Sitojun
96362587Sitojun			/*
96462587Sitojun			 * jumbo payload length must be larger than 65535.
96562587Sitojun			 */
96662587Sitojun			if (jumboplen <= IPV6_MAXPACKET) {
96762587Sitojun				ip6stat.ip6s_badoptions++;
96862587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
969120913Sume				    ICMP6_PARAMPROB_HEADER,
970120913Sume				    erroff + opt + 2 - opthead);
971120856Sume				return (-1);
97262587Sitojun			}
97362587Sitojun			*plenp = jumboplen;
97462587Sitojun
97562587Sitojun			break;
97678064Sume		default:		/* unknown option */
97778064Sume			if (hbhlen < IP6OPT_MINLEN) {
97878064Sume				ip6stat.ip6s_toosmall++;
97978064Sume				goto bad;
98078064Sume			}
98178064Sume			optlen = ip6_unknown_opt(opt, m,
98278064Sume			    erroff + opt - opthead);
98378064Sume			if (optlen == -1)
984120856Sume				return (-1);
98578064Sume			optlen += 2;
98678064Sume			break;
98753541Sshin		}
98853541Sshin	}
98953541Sshin
990120856Sume	return (0);
99153541Sshin
99253541Sshin  bad:
99353541Sshin	m_freem(m);
994120856Sume	return (-1);
99553541Sshin}
99653541Sshin
99753541Sshin/*
99853541Sshin * Unknown option processing.
99953541Sshin * The third argument `off' is the offset from the IPv6 header to the option,
100053541Sshin * which is necessary if the IPv6 header the and option header and IPv6 header
100153541Sshin * is not continuous in order to return an ICMPv6 error.
100253541Sshin */
100353541Sshinint
100453541Sshinip6_unknown_opt(optp, m, off)
100553541Sshin	u_int8_t *optp;
100653541Sshin	struct mbuf *m;
100753541Sshin	int off;
100853541Sshin{
100953541Sshin	struct ip6_hdr *ip6;
101053541Sshin
101178064Sume	switch (IP6OPT_TYPE(*optp)) {
101278064Sume	case IP6OPT_TYPE_SKIP: /* ignore the option */
1013120856Sume		return ((int)*(optp + 1));
101478064Sume	case IP6OPT_TYPE_DISCARD:	/* silently discard */
101578064Sume		m_freem(m);
1016120856Sume		return (-1);
101778064Sume	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
101878064Sume		ip6stat.ip6s_badoptions++;
101978064Sume		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1020120856Sume		return (-1);
102178064Sume	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
102278064Sume		ip6stat.ip6s_badoptions++;
102378064Sume		ip6 = mtod(m, struct ip6_hdr *);
102478064Sume		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
102578064Sume		    (m->m_flags & (M_BCAST|M_MCAST)))
102678064Sume			m_freem(m);
102778064Sume		else
102878064Sume			icmp6_error(m, ICMP6_PARAM_PROB,
102978064Sume				    ICMP6_PARAMPROB_OPTION, off);
1030120856Sume		return (-1);
103153541Sshin	}
103253541Sshin
103353541Sshin	m_freem(m);		/* XXX: NOTREACHED */
1034120856Sume	return (-1);
103553541Sshin}
103653541Sshin
103753541Sshin/*
103862587Sitojun * Create the "control" list for this pcb.
103978064Sume * The function will not modify mbuf chain at all.
104062587Sitojun *
104178064Sume * with KAME mbuf chain restriction:
104262587Sitojun * The routine will be called from upper layer handlers like tcp6_input().
104362587Sitojun * Thus the routine assumes that the caller (tcp6_input) have already
104462587Sitojun * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
104562587Sitojun * very first mbuf on the mbuf chain.
104653541Sshin */
104753541Sshinvoid
1048121674Sumeip6_savecontrol(in6p, m, mp)
104978064Sume	struct inpcb *in6p;
1050121674Sume	struct mbuf *m, **mp;
105153541Sshin{
1052121472Sume#define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1053121674Sume	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
105453541Sshin
105578064Sume#ifdef SO_TIMESTAMP
105678064Sume	if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
105753541Sshin		struct timeval tv;
105853541Sshin
105953541Sshin		microtime(&tv);
106053541Sshin		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1061120913Sume		    SCM_TIMESTAMP, SOL_SOCKET);
1062121472Sume		if (*mp)
106353541Sshin			mp = &(*mp)->m_next;
106497658Stanimura	}
106562587Sitojun#endif
106662587Sitojun
1067121631Sume	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
1068121631Sume		return;
1069121631Sume
107053541Sshin	/* RFC 2292 sec. 5 */
107178064Sume	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
107253541Sshin		struct in6_pktinfo pi6;
1073120913Sume
107453541Sshin		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1075121315Sume		in6_clearscope(&pi6.ipi6_addr);	/* XXX */
1076120913Sume		pi6.ipi6_ifindex =
1077120913Sume		    (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1078120913Sume
107953541Sshin		*mp = sbcreatecontrol((caddr_t) &pi6,
1080120913Sume		    sizeof(struct in6_pktinfo),
1081121472Sume		    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1082121472Sume		if (*mp)
108353541Sshin			mp = &(*mp)->m_next;
108453541Sshin	}
108578064Sume
108678064Sume	if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
108753541Sshin		int hlim = ip6->ip6_hlim & 0xff;
1088120913Sume
1089120913Sume		*mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1090121472Sume		    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1091121472Sume		if (*mp)
109253541Sshin			mp = &(*mp)->m_next;
109353541Sshin	}
109453541Sshin
1095121472Sume	if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1096121472Sume		u_int32_t flowinfo;
1097121472Sume		int tclass;
1098121472Sume
1099121472Sume		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1100121472Sume		flowinfo >>= 20;
1101121472Sume
1102121472Sume		tclass = flowinfo & 0xff;
1103121472Sume		*mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass),
1104121472Sume		    IPV6_TCLASS, IPPROTO_IPV6);
1105121472Sume		if (*mp)
1106121472Sume			mp = &(*mp)->m_next;
1107121472Sume	}
1108121472Sume
110953541Sshin	/*
1110130002Sume	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
1111130002Sume	 * privilege for the option (see ip6_ctloutput), but it might be too
1112130002Sume	 * strict, since there might be some hop-by-hop options which can be
1113130002Sume	 * returned to normal user.
1114130002Sume	 * See also RFC 2292 section 6 (or RFC 3542 section 8).
111553541Sshin	 */
1116121472Sume	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
111753541Sshin		/*
111853541Sshin		 * Check if a hop-by-hop options header is contatined in the
111953541Sshin		 * received packet, and if so, store the options as ancillary
112053541Sshin		 * data. Note that a hop-by-hop options header must be
1121120913Sume		 * just after the IPv6 header, which is assured through the
1122120913Sume		 * IPv6 input processing.
112353541Sshin		 */
112453541Sshin		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
112562587Sitojun			struct ip6_hbh *hbh;
112678064Sume			int hbhlen = 0;
112778064Sume#ifdef PULLDOWN_TEST
112878064Sume			struct mbuf *ext;
112978064Sume#endif
113053541Sshin
113162587Sitojun#ifndef PULLDOWN_TEST
113262587Sitojun			hbh = (struct ip6_hbh *)(ip6 + 1);
113362587Sitojun			hbhlen = (hbh->ip6h_len + 1) << 3;
113462587Sitojun#else
113578064Sume			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
113678064Sume			    ip6->ip6_nxt);
113778064Sume			if (ext == NULL) {
113862587Sitojun				ip6stat.ip6s_tooshort++;
113962587Sitojun				return;
114062587Sitojun			}
114178064Sume			hbh = mtod(ext, struct ip6_hbh *);
114262587Sitojun			hbhlen = (hbh->ip6h_len + 1) << 3;
114378064Sume			if (hbhlen != ext->m_len) {
114478064Sume				m_freem(ext);
114562587Sitojun				ip6stat.ip6s_tooshort++;
114662587Sitojun				return;
114762587Sitojun			}
114862587Sitojun#endif
114962587Sitojun
115053541Sshin			/*
1151120913Sume			 * XXX: We copy the whole header even if a
1152120913Sume			 * jumbo payload option is included, the option which
1153120913Sume			 * is to be removed before returning according to
1154120913Sume			 * RFC2292.
1155148169Sume			 * Note: this constraint is removed in RFC3542
115653541Sshin			 */
115762587Sitojun			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1158121472Sume			    IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1159121472Sume			    IPPROTO_IPV6);
1160121472Sume			if (*mp)
116153541Sshin				mp = &(*mp)->m_next;
116278064Sume#ifdef PULLDOWN_TEST
116378064Sume			m_freem(ext);
116478064Sume#endif
116553541Sshin		}
116653541Sshin	}
116753541Sshin
1168121472Sume	if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
116978064Sume		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
117053541Sshin
117153541Sshin		/*
117253541Sshin		 * Search for destination options headers or routing
117353541Sshin		 * header(s) through the header chain, and stores each
117453541Sshin		 * header as ancillary data.
117553541Sshin		 * Note that the order of the headers remains in
117653541Sshin		 * the chain of ancillary data.
117753541Sshin		 */
117878064Sume		while (1) {	/* is explicit loop prevention necessary? */
117978064Sume			struct ip6_ext *ip6e = NULL;
118062587Sitojun			int elen;
118178064Sume#ifdef PULLDOWN_TEST
118278064Sume			struct mbuf *ext = NULL;
118378064Sume#endif
118453541Sshin
118578064Sume			/*
118678064Sume			 * if it is not an extension header, don't try to
118778064Sume			 * pull it from the chain.
118878064Sume			 */
118978064Sume			switch (nxt) {
119078064Sume			case IPPROTO_DSTOPTS:
119178064Sume			case IPPROTO_ROUTING:
119278064Sume			case IPPROTO_HOPOPTS:
119378064Sume			case IPPROTO_AH: /* is it possible? */
119478064Sume				break;
119578064Sume			default:
119678064Sume				goto loopend;
119778064Sume			}
119878064Sume
119962587Sitojun#ifndef PULLDOWN_TEST
120078064Sume			if (off + sizeof(*ip6e) > m->m_len)
120178064Sume				goto loopend;
120262587Sitojun			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
120362587Sitojun			if (nxt == IPPROTO_AH)
120462587Sitojun				elen = (ip6e->ip6e_len + 2) << 2;
120562587Sitojun			else
120662587Sitojun				elen = (ip6e->ip6e_len + 1) << 3;
120778064Sume			if (off + elen > m->m_len)
120878064Sume				goto loopend;
120962587Sitojun#else
121078064Sume			ext = ip6_pullexthdr(m, off, nxt);
121178064Sume			if (ext == NULL) {
121262587Sitojun				ip6stat.ip6s_tooshort++;
121362587Sitojun				return;
121462587Sitojun			}
121578064Sume			ip6e = mtod(ext, struct ip6_ext *);
121662587Sitojun			if (nxt == IPPROTO_AH)
121762587Sitojun				elen = (ip6e->ip6e_len + 2) << 2;
121862587Sitojun			else
121962587Sitojun				elen = (ip6e->ip6e_len + 1) << 3;
122078064Sume			if (elen != ext->m_len) {
122178064Sume				m_freem(ext);
122262587Sitojun				ip6stat.ip6s_tooshort++;
122362587Sitojun				return;
122462587Sitojun			}
122562587Sitojun#endif
122662587Sitojun
122778064Sume			switch (nxt) {
122878064Sume			case IPPROTO_DSTOPTS:
1229120913Sume				if (!(in6p->in6p_flags & IN6P_DSTOPTS))
123078064Sume					break;
123153541Sshin
123278064Sume				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1233121472Sume				    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1234120913Sume				    IPPROTO_IPV6);
123578064Sume				if (*mp)
123678064Sume					mp = &(*mp)->m_next;
123778064Sume				break;
123878064Sume			case IPPROTO_ROUTING:
123978064Sume				if (!in6p->in6p_flags & IN6P_RTHDR)
124078064Sume					break;
124153541Sshin
124278064Sume				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1243121472Sume				    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1244120913Sume				    IPPROTO_IPV6);
124578064Sume				if (*mp)
124678064Sume					mp = &(*mp)->m_next;
124778064Sume				break;
124878064Sume			case IPPROTO_HOPOPTS:
124978064Sume			case IPPROTO_AH: /* is it possible? */
125078064Sume				break;
125153541Sshin
125278064Sume			default:
125378064Sume				/*
125478064Sume			 	 * other cases have been filtered in the above.
125578064Sume				 * none will visit this case.  here we supply
125678064Sume				 * the code just in case (nxt overwritten or
125778064Sume				 * other cases).
125878064Sume				 */
125978064Sume#ifdef PULLDOWN_TEST
126078064Sume				m_freem(ext);
126178064Sume#endif
126278064Sume				goto loopend;
126353541Sshin
126453541Sshin			}
126553541Sshin
126653541Sshin			/* proceed with the next header. */
126762587Sitojun			off += elen;
126853541Sshin			nxt = ip6e->ip6e_nxt;
126978064Sume			ip6e = NULL;
127078064Sume#ifdef PULLDOWN_TEST
127178064Sume			m_freem(ext);
127278064Sume			ext = NULL;
127378064Sume#endif
127453541Sshin		}
127553541Sshin	  loopend:
127678064Sume		;
127753541Sshin	}
127878064Sume
1279121472Sume#undef IS2292
128078064Sume}
128178064Sume
1282125776Sumevoid
1283125776Sumeip6_notify_pmtu(in6p, dst, mtu)
1284125776Sume	struct inpcb *in6p;
1285125776Sume	struct sockaddr_in6 *dst;
1286125776Sume	u_int32_t *mtu;
1287125776Sume{
1288125776Sume	struct socket *so;
1289125776Sume	struct mbuf *m_mtu;
1290125776Sume	struct ip6_mtuinfo mtuctl;
1291125776Sume
1292125776Sume	so =  in6p->inp_socket;
1293125776Sume
1294125776Sume	if (mtu == NULL)
1295125776Sume		return;
1296125776Sume
1297125776Sume#ifdef DIAGNOSTIC
1298125776Sume	if (so == NULL)		/* I believe this is impossible */
1299125776Sume		panic("ip6_notify_pmtu: socket is NULL");
1300125776Sume#endif
1301125776Sume
1302125776Sume	bzero(&mtuctl, sizeof(mtuctl));	/* zero-clear for safety */
1303125776Sume	mtuctl.ip6m_mtu = *mtu;
1304125776Sume	mtuctl.ip6m_addr = *dst;
1305148385Sume	if (sa6_recoverscope(&mtuctl.ip6m_addr))
1306148385Sume		return;
1307125776Sume
1308125776Sume	if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1309125776Sume	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1310125776Sume		return;
1311125776Sume
1312125776Sume	if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1313125776Sume	    == 0) {
1314125776Sume		m_freem(m_mtu);
1315125776Sume		/* XXX: should count statistics */
1316125776Sume	} else
1317125776Sume		sorwakeup(so);
1318125776Sume
1319125776Sume	return;
1320125776Sume}
1321125776Sume
132278064Sume#ifdef PULLDOWN_TEST
132378064Sume/*
132478064Sume * pull single extension header from mbuf chain.  returns single mbuf that
132578064Sume * contains the result, or NULL on error.
132678064Sume */
132778064Sumestatic struct mbuf *
132878064Sumeip6_pullexthdr(m, off, nxt)
132978064Sume	struct mbuf *m;
133078064Sume	size_t off;
133178064Sume	int nxt;
133278064Sume{
133378064Sume	struct ip6_ext ip6e;
133478064Sume	size_t elen;
133578064Sume	struct mbuf *n;
133678064Sume
133778064Sume#ifdef DIAGNOSTIC
133878064Sume	switch (nxt) {
133978064Sume	case IPPROTO_DSTOPTS:
134078064Sume	case IPPROTO_ROUTING:
134178064Sume	case IPPROTO_HOPOPTS:
134278064Sume	case IPPROTO_AH: /* is it possible? */
134378064Sume		break;
134478064Sume	default:
134578064Sume		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
134653541Sshin	}
134778064Sume#endif
134878064Sume
134978064Sume	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
135078064Sume	if (nxt == IPPROTO_AH)
135178064Sume		elen = (ip6e.ip6e_len + 2) << 2;
135278064Sume	else
135378064Sume		elen = (ip6e.ip6e_len + 1) << 3;
135478064Sume
1355111119Simp	MGET(n, M_DONTWAIT, MT_DATA);
135678064Sume	if (n && elen >= MLEN) {
1357111119Simp		MCLGET(n, M_DONTWAIT);
135878064Sume		if ((n->m_flags & M_EXT) == 0) {
135978064Sume			m_free(n);
136078064Sume			n = NULL;
136178064Sume		}
136253541Sshin	}
136378064Sume	if (!n)
136478064Sume		return NULL;
136562587Sitojun
136678064Sume	n->m_len = 0;
136778064Sume	if (elen >= M_TRAILINGSPACE(n)) {
136878064Sume		m_free(n);
136978064Sume		return NULL;
137078064Sume	}
137178064Sume
137278064Sume	m_copydata(m, off, elen, mtod(n, caddr_t));
137378064Sume	n->m_len = elen;
137478064Sume	return n;
137553541Sshin}
137678064Sume#endif
137753541Sshin
137853541Sshin/*
137953541Sshin * Get pointer to the previous header followed by the header
138053541Sshin * currently processed.
138153541Sshin * XXX: This function supposes that
138253541Sshin *	M includes all headers,
138353541Sshin *	the next header field and the header length field of each header
138453541Sshin *	are valid, and
138553541Sshin *	the sum of each header length equals to OFF.
138653541Sshin * Because of these assumptions, this function must be called very
138753541Sshin * carefully. Moreover, it will not be used in the near future when
138853541Sshin * we develop `neater' mechanism to process extension headers.
138953541Sshin */
139053541Sshinchar *
139153541Sshinip6_get_prevhdr(m, off)
139253541Sshin	struct mbuf *m;
139353541Sshin	int off;
139453541Sshin{
139553541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
139653541Sshin
139753541Sshin	if (off == sizeof(struct ip6_hdr))
1398120856Sume		return (&ip6->ip6_nxt);
139953541Sshin	else {
140053541Sshin		int len, nxt;
140153541Sshin		struct ip6_ext *ip6e = NULL;
140253541Sshin
140353541Sshin		nxt = ip6->ip6_nxt;
140453541Sshin		len = sizeof(struct ip6_hdr);
140553541Sshin		while (len < off) {
140653541Sshin			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
140753541Sshin
140878064Sume			switch (nxt) {
140953541Sshin			case IPPROTO_FRAGMENT:
141053541Sshin				len += sizeof(struct ip6_frag);
141153541Sshin				break;
141253541Sshin			case IPPROTO_AH:
141353541Sshin				len += (ip6e->ip6e_len + 2) << 2;
141453541Sshin				break;
141553541Sshin			default:
141653541Sshin				len += (ip6e->ip6e_len + 1) << 3;
141753541Sshin				break;
141853541Sshin			}
141953541Sshin			nxt = ip6e->ip6e_nxt;
142053541Sshin		}
142153541Sshin		if (ip6e)
1422120856Sume			return (&ip6e->ip6e_nxt);
142353541Sshin		else
142453541Sshin			return NULL;
142553541Sshin	}
142653541Sshin}
142753541Sshin
142853541Sshin/*
142962587Sitojun * get next header offset.  m will be retained.
143062587Sitojun */
143162587Sitojunint
143262587Sitojunip6_nexthdr(m, off, proto, nxtp)
143362587Sitojun	struct mbuf *m;
143462587Sitojun	int off;
143562587Sitojun	int proto;
143662587Sitojun	int *nxtp;
143762587Sitojun{
143862587Sitojun	struct ip6_hdr ip6;
143962587Sitojun	struct ip6_ext ip6e;
144062587Sitojun	struct ip6_frag fh;
144162587Sitojun
144262587Sitojun	/* just in case */
144362587Sitojun	if (m == NULL)
144462587Sitojun		panic("ip6_nexthdr: m == NULL");
144562587Sitojun	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
144662587Sitojun		return -1;
144762587Sitojun
144862587Sitojun	switch (proto) {
144962587Sitojun	case IPPROTO_IPV6:
145062587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6))
145162587Sitojun			return -1;
145262587Sitojun		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
145362587Sitojun		if (nxtp)
145462587Sitojun			*nxtp = ip6.ip6_nxt;
145562587Sitojun		off += sizeof(ip6);
145662587Sitojun		return off;
145762587Sitojun
145862587Sitojun	case IPPROTO_FRAGMENT:
145962587Sitojun		/*
146062587Sitojun		 * terminate parsing if it is not the first fragment,
146162587Sitojun		 * it does not make sense to parse through it.
146262587Sitojun		 */
146362587Sitojun		if (m->m_pkthdr.len < off + sizeof(fh))
146462587Sitojun			return -1;
146562587Sitojun		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1466120978Sume		/* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1467120978Sume		if (fh.ip6f_offlg & IP6F_OFF_MASK)
146862587Sitojun			return -1;
146962587Sitojun		if (nxtp)
147062587Sitojun			*nxtp = fh.ip6f_nxt;
147162587Sitojun		off += sizeof(struct ip6_frag);
147262587Sitojun		return off;
147362587Sitojun
147462587Sitojun	case IPPROTO_AH:
147562587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6e))
147662587Sitojun			return -1;
147762587Sitojun		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
147862587Sitojun		if (nxtp)
147962587Sitojun			*nxtp = ip6e.ip6e_nxt;
148062587Sitojun		off += (ip6e.ip6e_len + 2) << 2;
148162587Sitojun		return off;
148262587Sitojun
148362587Sitojun	case IPPROTO_HOPOPTS:
148462587Sitojun	case IPPROTO_ROUTING:
148562587Sitojun	case IPPROTO_DSTOPTS:
148662587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6e))
148762587Sitojun			return -1;
148862587Sitojun		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
148962587Sitojun		if (nxtp)
149062587Sitojun			*nxtp = ip6e.ip6e_nxt;
149162587Sitojun		off += (ip6e.ip6e_len + 1) << 3;
149262587Sitojun		return off;
149362587Sitojun
149462587Sitojun	case IPPROTO_NONE:
149562587Sitojun	case IPPROTO_ESP:
149662587Sitojun	case IPPROTO_IPCOMP:
149762587Sitojun		/* give up */
149862587Sitojun		return -1;
149962587Sitojun
150062587Sitojun	default:
150162587Sitojun		return -1;
150262587Sitojun	}
150362587Sitojun
150462587Sitojun	return -1;
150562587Sitojun}
150662587Sitojun
150762587Sitojun/*
150862587Sitojun * get offset for the last header in the chain.  m will be kept untainted.
150962587Sitojun */
151062587Sitojunint
151162587Sitojunip6_lasthdr(m, off, proto, nxtp)
151262587Sitojun	struct mbuf *m;
151362587Sitojun	int off;
151462587Sitojun	int proto;
151562587Sitojun	int *nxtp;
151662587Sitojun{
151762587Sitojun	int newoff;
151862587Sitojun	int nxt;
151962587Sitojun
152062587Sitojun	if (!nxtp) {
152162587Sitojun		nxt = -1;
152262587Sitojun		nxtp = &nxt;
152362587Sitojun	}
152462587Sitojun	while (1) {
152562587Sitojun		newoff = ip6_nexthdr(m, off, proto, nxtp);
152662587Sitojun		if (newoff < 0)
152762587Sitojun			return off;
152862587Sitojun		else if (newoff < off)
152962587Sitojun			return -1;	/* invalid */
153062587Sitojun		else if (newoff == off)
153162587Sitojun			return newoff;
153262587Sitojun
153362587Sitojun		off = newoff;
153462587Sitojun		proto = *nxtp;
153562587Sitojun	}
153662587Sitojun}
153762587Sitojun
1538121673Sumestruct ip6aux *
153978064Sumeip6_addaux(m)
154078064Sume	struct mbuf *m;
154178064Sume{
1542120913Sume	struct m_tag *mtag;
1543120913Sume
1544120913Sume	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1545120913Sume	if (!mtag) {
1546120913Sume		mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux),
1547120913Sume		    M_NOWAIT);
1548121630Sume		if (mtag) {
1549120913Sume			m_tag_prepend(m, mtag);
1550121630Sume			bzero(mtag + 1, sizeof(struct ip6aux));
1551121630Sume		}
155278064Sume	}
1553121673Sume	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
155478064Sume}
155578064Sume
1556121673Sumestruct ip6aux *
155778064Sumeip6_findaux(m)
155878064Sume	struct mbuf *m;
155978064Sume{
1560120913Sume	struct m_tag *mtag;
1561120913Sume
1562120913Sume	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1563121673Sume	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
156478064Sume}
156578064Sume
156678064Sumevoid
156778064Sumeip6_delaux(m)
156878064Sume	struct mbuf *m;
156978064Sume{
1570120913Sume	struct m_tag *mtag;
1571120913Sume
1572120913Sume	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1573120913Sume	if (mtag)
1574120913Sume		m_tag_delete(m, mtag);
157578064Sume}
157678064Sume
157762587Sitojun/*
157853541Sshin * System control for IP6
157953541Sshin */
158053541Sshin
158153541Sshinu_char	inet6ctlerrmap[PRC_NCMDS] = {
158253541Sshin	0,		0,		0,		0,
158353541Sshin	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
158453541Sshin	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
158553541Sshin	EMSGSIZE,	EHOSTUNREACH,	0,		0,
158653541Sshin	0,		0,		0,		0,
158753541Sshin	ENOPROTOOPT
158853541Sshin};
1589