ip6_input.c revision 175162
1139826Simp/*-
253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
353541Sshin * All rights reserved.
453541Sshin *
553541Sshin * Redistribution and use in source and binary forms, with or without
653541Sshin * modification, are permitted provided that the following conditions
753541Sshin * are met:
853541Sshin * 1. Redistributions of source code must retain the above copyright
953541Sshin *    notice, this list of conditions and the following disclaimer.
1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1153541Sshin *    notice, this list of conditions and the following disclaimer in the
1253541Sshin *    documentation and/or other materials provided with the distribution.
1353541Sshin * 3. Neither the name of the project nor the names of its contributors
1453541Sshin *    may be used to endorse or promote products derived from this software
1553541Sshin *    without specific prior written permission.
1653541Sshin *
1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2053541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2753541Sshin * SUCH DAMAGE.
28174510Sobrien *
29174510Sobrien *	$KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $
3053541Sshin */
3153541Sshin
32139826Simp/*-
3353541Sshin * Copyright (c) 1982, 1986, 1988, 1993
3453541Sshin *	The Regents of the University of California.  All rights reserved.
3553541Sshin *
3653541Sshin * Redistribution and use in source and binary forms, with or without
3753541Sshin * modification, are permitted provided that the following conditions
3853541Sshin * are met:
3953541Sshin * 1. Redistributions of source code must retain the above copyright
4053541Sshin *    notice, this list of conditions and the following disclaimer.
4153541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4253541Sshin *    notice, this list of conditions and the following disclaimer in the
4353541Sshin *    documentation and/or other materials provided with the distribution.
4453541Sshin * 4. Neither the name of the University nor the names of its contributors
4553541Sshin *    may be used to endorse or promote products derived from this software
4653541Sshin *    without specific prior written permission.
4753541Sshin *
4853541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4953541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5053541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5153541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5253541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5353541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5453541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5553541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5653541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5753541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5853541Sshin * SUCH DAMAGE.
5953541Sshin *
6053541Sshin *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
6153541Sshin */
6253541Sshin
63174510Sobrien#include <sys/cdefs.h>
64174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/ip6_input.c 175162 2008-01-08 19:08:58Z obrien $");
65174510Sobrien
6662587Sitojun#include "opt_inet.h"
6762587Sitojun#include "opt_inet6.h"
6855009Sshin#include "opt_ipsec.h"
6955009Sshin
7053541Sshin#include <sys/param.h>
7153541Sshin#include <sys/systm.h>
7278064Sume#include <sys/malloc.h>
7353541Sshin#include <sys/mbuf.h>
7483366Sjulian#include <sys/proc.h>
7553541Sshin#include <sys/domain.h>
7653541Sshin#include <sys/protosw.h>
7753541Sshin#include <sys/socket.h>
7853541Sshin#include <sys/socketvar.h>
7953541Sshin#include <sys/errno.h>
8053541Sshin#include <sys/time.h>
8153541Sshin#include <sys/kernel.h>
8253541Sshin#include <sys/syslog.h>
8353541Sshin
8453541Sshin#include <net/if.h>
8553541Sshin#include <net/if_types.h>
8653541Sshin#include <net/if_dl.h>
8753541Sshin#include <net/route.h>
8853541Sshin#include <net/netisr.h>
8964060Sdarrenr#include <net/pfil.h>
9053541Sshin
9153541Sshin#include <netinet/in.h>
9253541Sshin#include <netinet/in_systm.h>
9362587Sitojun#ifdef INET
9453541Sshin#include <netinet/ip.h>
9553541Sshin#include <netinet/ip_icmp.h>
9695023Ssuz#endif /* INET */
9762587Sitojun#include <netinet/ip6.h>
9853541Sshin#include <netinet6/in6_var.h>
9953541Sshin#include <netinet6/ip6_var.h>
10062587Sitojun#include <netinet/in_pcb.h>
10162587Sitojun#include <netinet/icmp6.h>
102121161Sume#include <netinet6/scope6_var.h>
10353541Sshin#include <netinet6/in6_ifattach.h>
10453541Sshin#include <netinet6/nd6.h>
10553541Sshin
106171167Sgnn#ifdef IPSEC
107105199Ssam#include <netipsec/ipsec.h>
108171133Sgnn#include <netinet6/ip6_ipsec.h>
109105199Ssam#include <netipsec/ipsec6.h>
110171167Sgnn#endif /* IPSEC */
111105199Ssam
11253541Sshin#include <netinet6/ip6protosw.h>
11353541Sshin
11462587Sitojunextern struct domain inet6domain;
11553541Sshin
11662587Sitojunu_char ip6_protox[IPPROTO_MAX];
117111888Sjlemonstatic struct ifqueue ip6intrq;
11862587Sitojunstatic int ip6qmaxlen = IFQ_MAXLEN;
11962587Sitojunstruct in6_ifaddr *in6_ifaddr;
12053541Sshin
12178064Sumeextern struct callout in6_tmpaddrtimer_ch;
12278064Sume
12362587Sitojunint ip6_forward_srcrt;			/* XXX */
12462587Sitojunint ip6_sourcecheck;			/* XXX */
12562587Sitojunint ip6_sourcecheck_interval;		/* XXX */
12653541Sshin
12778064Sumeint ip6_ours_check_algorithm;
12878064Sume
129120386Ssamstruct pfil_head inet6_pfil_hook;
13078064Sume
13162587Sitojunstruct ip6stat ip6stat;
13253541Sshin
133175162Sobrienstatic void ip6_init2(void *);
134175162Sobrienstatic struct ip6aux *ip6_setdstifaddr(struct mbuf *, struct in6_ifaddr *);
135175162Sobrienstatic int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
13662587Sitojun#ifdef PULLDOWN_TEST
137175162Sobrienstatic struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
13853541Sshin#endif
13953541Sshin
14053541Sshin/*
14153541Sshin * IP6 initialization: fill in IP6 protocol switch table.
14253541Sshin * All protocols not implemented in kernel go to raw IP6 protocol handler.
14353541Sshin */
14453541Sshinvoid
145171259Sdelphijip6_init(void)
14653541Sshin{
14778064Sume	struct ip6protosw *pr;
14878064Sume	int i;
14953541Sshin
15078064Sume#ifdef DIAGNOSTIC
15178064Sume	if (sizeof(struct protosw) != sizeof(struct ip6protosw))
15278064Sume		panic("sizeof(protosw) != sizeof(ip6protosw)");
15378064Sume#endif
15453541Sshin	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
15553541Sshin	if (pr == 0)
15653541Sshin		panic("ip6_init");
157136689Sandre
158136689Sandre	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
15953541Sshin	for (i = 0; i < IPPROTO_MAX; i++)
16053541Sshin		ip6_protox[i] = pr - inet6sw;
161136689Sandre	/*
162136689Sandre	 * Cycle through IP protocols and put them into the appropriate place
163136689Sandre	 * in ip6_protox[].
164136689Sandre	 */
16553541Sshin	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
16653541Sshin	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
16753541Sshin		if (pr->pr_domain->dom_family == PF_INET6 &&
168136689Sandre		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
169136689Sandre			/* Be careful to only index valid IP protocols. */
170143675Ssam			if (pr->pr_protocol < IPPROTO_MAX)
171136689Sandre				ip6_protox[pr->pr_protocol] = pr - inet6sw;
172136689Sandre		}
173134383Sandre
174134383Sandre	/* Initialize packet filter hooks. */
175120386Ssam	inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
176120386Ssam	inet6_pfil_hook.ph_af = AF_INET6;
177120386Ssam	if ((i = pfil_head_register(&inet6_pfil_hook)) != 0)
178120386Ssam		printf("%s: WARNING: unable to register pfil hook, "
179120386Ssam			"error %d\n", __func__, i);
180134383Sandre
18153541Sshin	ip6intrq.ifq_maxlen = ip6qmaxlen;
18293818Sjhb	mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF);
183122320Ssam	netisr_register(NETISR_IPV6, ip6_input, &ip6intrq, 0);
184121161Sume	scope6_init();
185121742Sume	addrsel_policy_init();
18653541Sshin	nd6_init();
18753541Sshin	frag6_init();
188120648Sume	ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
18953541Sshin}
19053541Sshin
19153541Sshinstatic void
192171259Sdelphijip6_init2(void *dummy)
19353541Sshin{
19453541Sshin
19553541Sshin	/* nd6_timer_init */
19678064Sume	callout_init(&nd6_timer_ch, 0);
19778064Sume	callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
19878064Sume
19978064Sume	/* timer for regeneranation of temporary addresses randomize ID */
200121806Sume	callout_init(&in6_tmpaddrtimer_ch, 0);
20178064Sume	callout_reset(&in6_tmpaddrtimer_ch,
20278064Sume		      (ip6_temp_preferred_lifetime - ip6_desync_factor -
20378064Sume		       ip6_temp_regen_advance) * hz,
20478064Sume		      in6_tmpaddrtimer, NULL);
20553541Sshin}
20653541Sshin
20753541Sshin/* cheat */
20855009Sshin/* This must be after route_init(), which is now SI_ORDER_THIRD */
20955009SshinSYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
21053541Sshin
21153541Sshinextern struct	route_in6 ip6_forward_rt;
21253541Sshin
21353541Sshinvoid
214171259Sdelphijip6_input(struct mbuf *m)
21553541Sshin{
21653541Sshin	struct ip6_hdr *ip6;
21753541Sshin	int off = sizeof(struct ip6_hdr), nest;
21853541Sshin	u_int32_t plen;
21953541Sshin	u_int32_t rtalert = ~0;
22053541Sshin	int nxt, ours = 0;
22153541Sshin	struct ifnet *deliverifp = NULL;
222121143Ssam	struct in6_addr odst;
223121143Ssam	int srcrt = 0;
224121144Ssam
225122320Ssam	GIANT_REQUIRED;			/* XXX for now */
226171133Sgnn
227171167Sgnn#ifdef IPSEC
22853541Sshin	/*
22953541Sshin	 * should the inner packet be considered authentic?
23053541Sshin	 * see comment in ah4_input().
231171133Sgnn	 * NB: m cannot be NULL when passed to the input routine
23253541Sshin	 */
23353541Sshin
234171133Sgnn	m->m_flags &= ~M_AUTHIPHDR;
235171133Sgnn	m->m_flags &= ~M_AUTHIPDGM;
236171133Sgnn
237171167Sgnn#endif /* IPSEC */
238171133Sgnn
23953541Sshin	/*
240121630Sume	 * make sure we don't have onion peering information into m_tag.
24178064Sume	 */
24278064Sume	ip6_delaux(m);
24378064Sume
24478064Sume	/*
24595023Ssuz	 * mbuf statistics
24653541Sshin	 */
24753541Sshin	if (m->m_flags & M_EXT) {
24853541Sshin		if (m->m_next)
24953541Sshin			ip6stat.ip6s_mext2m++;
25053541Sshin		else
25153541Sshin			ip6stat.ip6s_mext1++;
25253541Sshin	} else {
25378064Sume#define M2MMAX	(sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
25453541Sshin		if (m->m_next) {
25553541Sshin			if (m->m_flags & M_LOOP) {
256120913Sume				ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
25778064Sume			} else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
25853541Sshin				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
25953541Sshin			else
26053541Sshin				ip6stat.ip6s_m2m[0]++;
26153541Sshin		} else
26253541Sshin			ip6stat.ip6s_m1++;
26378064Sume#undef M2MMAX
26453541Sshin	}
26553541Sshin
266151474Ssuz	/* drop the packet if IPv6 operation is disabled on the IF */
267151474Ssuz	if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) {
268151474Ssuz		m_freem(m);
269151474Ssuz		return;
270151474Ssuz	}
271151474Ssuz
27253541Sshin	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
27353541Sshin	ip6stat.ip6s_total++;
27453541Sshin
27562587Sitojun#ifndef PULLDOWN_TEST
27674336Skuriyama	/*
27774336Skuriyama	 * L2 bridge code and some other code can return mbuf chain
27874336Skuriyama	 * that does not conform to KAME requirement.  too bad.
27974336Skuriyama	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
28074336Skuriyama	 */
28174336Skuriyama	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
28274336Skuriyama		struct mbuf *n;
28374336Skuriyama
284111119Simp		MGETHDR(n, M_DONTWAIT, MT_HEADER);
28577003Sume		if (n)
286108466Ssam			M_MOVE_PKTHDR(n, m);
287108825Ssam		if (n && n->m_pkthdr.len > MHLEN) {
288111119Simp			MCLGET(n, M_DONTWAIT);
28974336Skuriyama			if ((n->m_flags & M_EXT) == 0) {
29074336Skuriyama				m_freem(n);
29174336Skuriyama				n = NULL;
29274336Skuriyama			}
29374336Skuriyama		}
29495023Ssuz		if (n == NULL) {
29577003Sume			m_freem(m);
296120913Sume			return;	/* ENOBUFS */
29777003Sume		}
29874336Skuriyama
299108825Ssam		m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
300108825Ssam		n->m_len = n->m_pkthdr.len;
30174336Skuriyama		m_freem(m);
30274336Skuriyama		m = n;
30374336Skuriyama	}
304120913Sume	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
30562587Sitojun#endif
30653541Sshin
30753541Sshin	if (m->m_len < sizeof(struct ip6_hdr)) {
30853541Sshin		struct ifnet *inifp;
30953541Sshin		inifp = m->m_pkthdr.rcvif;
310120913Sume		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
31153541Sshin			ip6stat.ip6s_toosmall++;
31253541Sshin			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
31353541Sshin			return;
31453541Sshin		}
31553541Sshin	}
31653541Sshin
31753541Sshin	ip6 = mtod(m, struct ip6_hdr *);
31853541Sshin
31953541Sshin	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
32053541Sshin		ip6stat.ip6s_badvers++;
32153541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
32253541Sshin		goto bad;
32353541Sshin	}
32453541Sshin
32553541Sshin	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
32653541Sshin
32753541Sshin	/*
32878064Sume	 * Check against address spoofing/corruption.
32953541Sshin	 */
33053541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
33153541Sshin	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
33278064Sume		/*
33378064Sume		 * XXX: "badscope" is not very suitable for a multicast source.
33478064Sume		 */
33553541Sshin		ip6stat.ip6s_badscope++;
33653541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
33753541Sshin		goto bad;
33853541Sshin	}
339126444Sume	if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
340126444Sume	    !(m->m_flags & M_LOOP)) {
341126444Sume		/*
342126444Sume		 * In this case, the packet should come from the loopback
343126444Sume		 * interface.  However, we cannot just check the if_flags,
344126444Sume		 * because ip6_mloopback() passes the "actual" interface
345126444Sume		 * as the outgoing/incoming interface.
346126444Sume		 */
34778064Sume		ip6stat.ip6s_badscope++;
34878064Sume		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
34978064Sume		goto bad;
35078064Sume	}
35195023Ssuz
352130416Smlaier#ifdef ALTQ
353130416Smlaier	if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
354130416Smlaier		/* packet is dropped by traffic conditioner */
355130416Smlaier		return;
356130416Smlaier	}
357130416Smlaier#endif
35862587Sitojun	/*
35978064Sume	 * The following check is not documented in specs.  A malicious
36078064Sume	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
36178064Sume	 * and bypass security checks (act as if it was from 127.0.0.1 by using
362120913Sume	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
36378064Sume	 *
36478064Sume	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
36578064Sume	 * support IPv4-less kernel compilation, we cannot support SIIT
36678064Sume	 * environment at all.  So, it makes more sense for us to reject any
36778064Sume	 * malicious packets for non-SIIT environment, than try to do a
368120913Sume	 * partial support for SIIT environment.
36962587Sitojun	 */
37078064Sume	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
37178064Sume	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
37278064Sume		ip6stat.ip6s_badscope++;
37378064Sume		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
37478064Sume		goto bad;
37578064Sume	}
37662587Sitojun#if 0
37762587Sitojun	/*
37862587Sitojun	 * Reject packets with IPv4 compatible addresses (auto tunnel).
37962587Sitojun	 *
38062587Sitojun	 * The code forbids auto tunnel relay case in RFC1933 (the check is
38162587Sitojun	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
38262587Sitojun	 * is revised to forbid relaying case.
38362587Sitojun	 */
38462587Sitojun	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
38562587Sitojun	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
38662587Sitojun		ip6stat.ip6s_badscope++;
38762587Sitojun		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
38862587Sitojun		goto bad;
38962587Sitojun	}
39062587Sitojun#endif
39175246Sume
392126444Sume	/*
393126508Smlaier	 * Run through list of hooks for input packets.
394126508Smlaier	 *
395126508Smlaier	 * NB: Beware of the destination address changing
396126508Smlaier	 *     (e.g. by NAT rewriting).  When this happens,
397126508Smlaier	 *     tell ip6_forward to do the right thing.
398126508Smlaier	 */
399126508Smlaier	odst = ip6->ip6_dst;
400134383Sandre
401134383Sandre	/* Jump over all PFIL processing if hooks are not active. */
402155201Scsjp	if (!PFIL_HOOKED(&inet6_pfil_hook))
403134383Sandre		goto passin;
404134383Sandre
405135920Smlaier	if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN, NULL))
406126508Smlaier		return;
407126508Smlaier	if (m == NULL)			/* consumed by filter */
408126508Smlaier		return;
409126508Smlaier	ip6 = mtod(m, struct ip6_hdr *);
410126508Smlaier	srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
411126508Smlaier
412134383Sandrepassin:
413126508Smlaier	/*
414154804Sume	 * Disambiguate address scope zones (if there is ambiguity).
415154804Sume	 * We first make sure that the original source or destination address
416154804Sume	 * is not in our internal form for scoped addresses.  Such addresses
417154804Sume	 * are not necessarily invalid spec-wise, but we cannot accept them due
418154804Sume	 * to the usage conflict.
419154804Sume	 * in6_setscope() then also checks and rejects the cases where src or
420154804Sume	 * dst are the loopback address and the receiving interface
421154804Sume	 * is not loopback.
422154804Sume	 */
423154804Sume	if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
424154804Sume		ip6stat.ip6s_badscope++; /* XXX */
425154804Sume		goto bad;
426154804Sume	}
427154804Sume	if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) ||
428154804Sume	    in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) {
429154804Sume		ip6stat.ip6s_badscope++;
430154804Sume		goto bad;
431154804Sume	}
432154804Sume
433154804Sume	/*
43453541Sshin	 * Multicast check
43553541Sshin	 */
43653541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
437171260Sdelphij		struct in6_multi *in6m = 0;
43853541Sshin
43953541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
44053541Sshin		/*
44153541Sshin		 * See if we belong to the destination multicast group on the
44253541Sshin		 * arrival interface.
44353541Sshin		 */
44453541Sshin		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
44553541Sshin		if (in6m)
44653541Sshin			ours = 1;
44756723Sshin		else if (!ip6_mrouter) {
44853541Sshin			ip6stat.ip6s_notmember++;
44953541Sshin			ip6stat.ip6s_cantforward++;
45053541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
45153541Sshin			goto bad;
45253541Sshin		}
45353541Sshin		deliverifp = m->m_pkthdr.rcvif;
45453541Sshin		goto hbhcheck;
45553541Sshin	}
45653541Sshin
45753541Sshin	/*
45853541Sshin	 *  Unicast check
45953541Sshin	 */
46062587Sitojun	if (ip6_forward_rt.ro_rt != NULL &&
461120913Sume	    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
46262587Sitojun	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
463120913Sume	    &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
46462587Sitojun		ip6stat.ip6s_forward_cachehit++;
46562587Sitojun	else {
46678064Sume		struct sockaddr_in6 *dst6;
46778064Sume
46853541Sshin		if (ip6_forward_rt.ro_rt) {
46962587Sitojun			/* route is down or destination is different */
47062587Sitojun			ip6stat.ip6s_forward_cachemiss++;
47153541Sshin			RTFREE(ip6_forward_rt.ro_rt);
47253541Sshin			ip6_forward_rt.ro_rt = 0;
47353541Sshin		}
47462587Sitojun
47553541Sshin		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
47678064Sume		dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
47778064Sume		dst6->sin6_len = sizeof(struct sockaddr_in6);
47878064Sume		dst6->sin6_family = AF_INET6;
47978064Sume		dst6->sin6_addr = ip6->ip6_dst;
48053541Sshin
481122921Sandre		rtalloc((struct route *)&ip6_forward_rt);
48253541Sshin	}
48353541Sshin
48453541Sshin#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
48553541Sshin
48653541Sshin	/*
48753541Sshin	 * Accept the packet if the forwarding interface to the destination
48853541Sshin	 * according to the routing table is the loopback interface,
48953541Sshin	 * unless the associated route has a gateway.
49053541Sshin	 * Note that this approach causes to accept a packet if there is a
49153541Sshin	 * route to the loopback interface for the destination of the packet.
49253541Sshin	 * But we think it's even useful in some situations, e.g. when using
49353541Sshin	 * a special daemon which wants to intercept the packet.
49478064Sume	 *
49578064Sume	 * XXX: some OSes automatically make a cloned route for the destination
49678064Sume	 * of an outgoing packet.  If the outgoing interface of the packet
49778064Sume	 * is a loopback one, the kernel would consider the packet to be
49878064Sume	 * accepted, even if we have no such address assinged on the interface.
49978064Sume	 * We check the cloned flag of the route entry to reject such cases,
50078064Sume	 * assuming that route entries for our own addresses are not made by
50178064Sume	 * cloning (it should be true because in6_addloop explicitly installs
50278064Sume	 * the host route).  However, we might have to do an explicit check
50378064Sume	 * while it would be less efficient.  Or, should we rather install a
50478064Sume	 * reject route for such a case?
50553541Sshin	 */
50653541Sshin	if (ip6_forward_rt.ro_rt &&
50753541Sshin	    (ip6_forward_rt.ro_rt->rt_flags &
50853541Sshin	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
50978064Sume#ifdef RTF_WASCLONED
51078064Sume	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
51178064Sume#endif
51278064Sume#ifdef RTF_CLONED
51378064Sume	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
51478064Sume#endif
51562587Sitojun#if 0
51653541Sshin	    /*
51762587Sitojun	     * The check below is redundant since the comparison of
51862587Sitojun	     * the destination and the key of the rtentry has
51962587Sitojun	     * already done through looking up the routing table.
52053541Sshin	     */
52162587Sitojun	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
522120913Sume	    &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
52362587Sitojun#endif
52453541Sshin	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
52553541Sshin		struct in6_ifaddr *ia6 =
52653541Sshin			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
52778064Sume
52862587Sitojun		/*
529121630Sume		 * record address information into m_tag.
53078064Sume		 */
53178064Sume		(void)ip6_setdstifaddr(m, ia6);
53278064Sume
53378064Sume		/*
53462587Sitojun		 * packets to a tentative, duplicated, or somehow invalid
53562587Sitojun		 * address must not be accepted.
53662587Sitojun		 */
53753541Sshin		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
53862587Sitojun			/* this address is ready */
53953541Sshin			ours = 1;
54053541Sshin			deliverifp = ia6->ia_ifp;	/* correct? */
54167334Sjoe			/* Count the packet in the ip address stats */
54267334Sjoe			ia6->ia_ifa.if_ipackets++;
54367334Sjoe			ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
54453541Sshin			goto hbhcheck;
54553541Sshin		} else {
546165118Sbz			char ip6bufs[INET6_ADDRSTRLEN];
547165118Sbz			char ip6bufd[INET6_ADDRSTRLEN];
54862587Sitojun			/* address is not ready, so discard the packet. */
54978064Sume			nd6log((LOG_INFO,
55078064Sume			    "ip6_input: packet to an unready address %s->%s\n",
551165118Sbz			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
552165118Sbz			    ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
55362587Sitojun
55462587Sitojun			goto bad;
55553541Sshin		}
55653541Sshin	}
55753541Sshin
55853541Sshin	/*
559120913Sume	 * FAITH (Firewall Aided Internet Translator)
56053541Sshin	 */
56153541Sshin	if (ip6_keepfaith) {
56253541Sshin		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
56353541Sshin		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
56453541Sshin			/* XXX do we need more sanity checks? */
56553541Sshin			ours = 1;
56695023Ssuz			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
56753541Sshin			goto hbhcheck;
56853541Sshin		}
56953541Sshin	}
57053541Sshin
57153541Sshin	/*
57253541Sshin	 * Now there is no reason to process the packet if it's not our own
57353541Sshin	 * and we're not a router.
57453541Sshin	 */
57553541Sshin	if (!ip6_forwarding) {
57653541Sshin		ip6stat.ip6s_cantforward++;
57753541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
57853541Sshin		goto bad;
57953541Sshin	}
58053541Sshin
58153541Sshin  hbhcheck:
58253541Sshin	/*
583121630Sume	 * record address information into m_tag, if we don't have one yet.
58478064Sume	 * note that we are unable to record it, if the address is not listed
58578064Sume	 * as our interface address (e.g. multicast addresses, addresses
58678064Sume	 * within FAITH prefixes and such).
58778064Sume	 */
58878064Sume	if (deliverifp && !ip6_getdstifaddr(m)) {
58978064Sume		struct in6_ifaddr *ia6;
59078064Sume
59178064Sume		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
59278064Sume		if (ia6) {
59378064Sume			if (!ip6_setdstifaddr(m, ia6)) {
59478064Sume				/*
59578064Sume				 * XXX maybe we should drop the packet here,
59678064Sume				 * as we could not provide enough information
59778064Sume				 * to the upper layers.
59878064Sume				 */
59978064Sume			}
60078064Sume		}
60178064Sume	}
60278064Sume
60378064Sume	/*
60453541Sshin	 * Process Hop-by-Hop options header if it's contained.
60553541Sshin	 * m may be modified in ip6_hopopts_input().
60653541Sshin	 * If a JumboPayload option is included, plen will also be modified.
60753541Sshin	 */
60853541Sshin	plen = (u_int32_t)ntohs(ip6->ip6_plen);
60953541Sshin	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
61062587Sitojun		struct ip6_hbh *hbh;
61162587Sitojun
61253541Sshin		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
61362587Sitojun#if 0	/*touches NULL pointer*/
61453541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
61562587Sitojun#endif
61653541Sshin			return;	/* m have already been freed */
61753541Sshin		}
61862587Sitojun
61953541Sshin		/* adjust pointer */
62053541Sshin		ip6 = mtod(m, struct ip6_hdr *);
62153541Sshin
62253541Sshin		/*
62395023Ssuz		 * if the payload length field is 0 and the next header field
62462587Sitojun		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
62562587Sitojun		 * option MUST be included.
62662587Sitojun		 */
62762587Sitojun		if (ip6->ip6_plen == 0 && plen == 0) {
62862587Sitojun			/*
62962587Sitojun			 * Note that if a valid jumbo payload option is
630120913Sume			 * contained, ip6_hopopts_input() must set a valid
631120913Sume			 * (non-zero) payload length to the variable plen.
63262587Sitojun			 */
63362587Sitojun			ip6stat.ip6s_badoptions++;
63462587Sitojun			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
63562587Sitojun			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
63662587Sitojun			icmp6_error(m, ICMP6_PARAM_PROB,
63762587Sitojun				    ICMP6_PARAMPROB_HEADER,
63862587Sitojun				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
63962587Sitojun			return;
64062587Sitojun		}
64162587Sitojun#ifndef PULLDOWN_TEST
64262587Sitojun		/* ip6_hopopts_input() ensures that mbuf is contiguous */
64362587Sitojun		hbh = (struct ip6_hbh *)(ip6 + 1);
64462587Sitojun#else
64562587Sitojun		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
64662587Sitojun			sizeof(struct ip6_hbh));
64762587Sitojun		if (hbh == NULL) {
64862587Sitojun			ip6stat.ip6s_tooshort++;
64962587Sitojun			return;
65062587Sitojun		}
65162587Sitojun#endif
65262587Sitojun		nxt = hbh->ip6h_nxt;
65362587Sitojun
65462587Sitojun		/*
655169557Sjinmei		 * If we are acting as a router and the packet contains a
656169557Sjinmei		 * router alert option, see if we know the option value.
657169557Sjinmei		 * Currently, we only support the option value for MLD, in which
658169557Sjinmei		 * case we should pass the packet to the multicast routing
659169557Sjinmei		 * daemon.
66053541Sshin		 */
661169557Sjinmei		if (rtalert != ~0 && ip6_forwarding) {
662169557Sjinmei			switch (rtalert) {
663169557Sjinmei			case IP6OPT_RTALERT_MLD:
664169557Sjinmei				ours = 1;
665169557Sjinmei				break;
666169557Sjinmei			default:
667169557Sjinmei				/*
668169557Sjinmei				 * RFC2711 requires unrecognized values must be
669169557Sjinmei				 * silently ignored.
670169557Sjinmei				 */
671169557Sjinmei				break;
672169557Sjinmei			}
673169557Sjinmei		}
67453541Sshin	} else
67553541Sshin		nxt = ip6->ip6_nxt;
67653541Sshin
67753541Sshin	/*
67853541Sshin	 * Check that the amount of data in the buffers
67953541Sshin	 * is as at least much as the IPv6 header would have us expect.
68053541Sshin	 * Trim mbufs if longer than we expect.
68153541Sshin	 * Drop packet if shorter than we expect.
68253541Sshin	 */
68353541Sshin	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
68453541Sshin		ip6stat.ip6s_tooshort++;
68553541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
68653541Sshin		goto bad;
68753541Sshin	}
68853541Sshin	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
68953541Sshin		if (m->m_len == m->m_pkthdr.len) {
69053541Sshin			m->m_len = sizeof(struct ip6_hdr) + plen;
69153541Sshin			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
69253541Sshin		} else
69353541Sshin			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
69453541Sshin	}
69553541Sshin
69653541Sshin	/*
69753541Sshin	 * Forward if desirable.
69853541Sshin	 */
69953541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
70056723Sshin		/*
70156723Sshin		 * If we are acting as a multicast router, all
70256723Sshin		 * incoming multicast packets are passed to the
70356723Sshin		 * kernel-level multicast forwarding function.
70456723Sshin		 * The packet is returned (relatively) intact; if
70556723Sshin		 * ip6_mforward() returns a non-zero value, the packet
70656723Sshin		 * must be discarded, else it may be accepted below.
70756723Sshin		 */
708166938Sbms		if (ip6_mrouter && ip6_mforward &&
709166938Sbms		    ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
71056723Sshin			ip6stat.ip6s_cantforward++;
71156723Sshin			m_freem(m);
71256723Sshin			return;
71356723Sshin		}
71453541Sshin		if (!ours) {
71553541Sshin			m_freem(m);
71653541Sshin			return;
71753541Sshin		}
71853541Sshin	} else if (!ours) {
719121143Ssam		ip6_forward(m, srcrt);
72053541Sshin		return;
721121673Sume	}
72253541Sshin
72362587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
72462587Sitojun
72553541Sshin	/*
72662587Sitojun	 * Malicious party may be able to use IPv4 mapped addr to confuse
72762587Sitojun	 * tcp/udp stack and bypass security checks (act as if it was from
72862587Sitojun	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
72962587Sitojun	 *
73062587Sitojun	 * For SIIT end node behavior, you may want to disable the check.
73162587Sitojun	 * However, you will  become vulnerable to attacks using IPv4 mapped
73262587Sitojun	 * source.
73362587Sitojun	 */
73462587Sitojun	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
73562587Sitojun	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
73662587Sitojun		ip6stat.ip6s_badscope++;
73762587Sitojun		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
73862587Sitojun		goto bad;
73962587Sitojun	}
74062587Sitojun
74162587Sitojun	/*
74253541Sshin	 * Tell launch routine the next header
74353541Sshin	 */
74453541Sshin	ip6stat.ip6s_delivered++;
74553541Sshin	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
74653541Sshin	nest = 0;
74778064Sume
74853541Sshin	while (nxt != IPPROTO_DONE) {
74953541Sshin		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
75053541Sshin			ip6stat.ip6s_toomanyhdr++;
75153541Sshin			goto bad;
75253541Sshin		}
75353541Sshin
75453541Sshin		/*
75553541Sshin		 * protection against faulty packet - there should be
75653541Sshin		 * more sanity checks in header chain processing.
75753541Sshin		 */
75853541Sshin		if (m->m_pkthdr.len < off) {
75953541Sshin			ip6stat.ip6s_tooshort++;
76053541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
76153541Sshin			goto bad;
76253541Sshin		}
76353541Sshin
764171167Sgnn#ifdef IPSEC
76578064Sume		/*
76678064Sume		 * enforce IPsec policy checking if we are seeing last header.
76778064Sume		 * note that we do not visit this with protocols with pcb layer
76878064Sume		 * code - like udp/tcp/raw ip.
76978064Sume		 */
770171133Sgnn		if (ip6_ipsec_input(m, nxt))
77178064Sume			goto bad;
772171167Sgnn#endif /* IPSEC */
77353541Sshin		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
77453541Sshin	}
77553541Sshin	return;
77653541Sshin bad:
77753541Sshin	m_freem(m);
77853541Sshin}
77953541Sshin
78053541Sshin/*
78178064Sume * set/grab in6_ifaddr correspond to IPv6 destination address.
78278064Sume * XXX backward compatibility wrapper
78378064Sume */
784121673Sumestatic struct ip6aux *
785171259Sdelphijip6_setdstifaddr(struct mbuf *m, struct in6_ifaddr *ia6)
78678064Sume{
787121673Sume	struct ip6aux *ip6a;
78878064Sume
789121673Sume	ip6a = ip6_addaux(m);
790121673Sume	if (ip6a)
791121673Sume		ip6a->ip6a_dstia6 = ia6;
792121673Sume	return ip6a;	/* NULL if failed to set */
79378064Sume}
79478064Sume
79578064Sumestruct in6_ifaddr *
796171259Sdelphijip6_getdstifaddr(struct mbuf *m)
79778064Sume{
798121673Sume	struct ip6aux *ip6a;
79978064Sume
800121673Sume	ip6a = ip6_findaux(m);
801121673Sume	if (ip6a)
802121673Sume		return ip6a->ip6a_dstia6;
80378064Sume	else
80478064Sume		return NULL;
80578064Sume}
80678064Sume
80778064Sume/*
80853541Sshin * Hop-by-Hop options header processing. If a valid jumbo payload option is
80953541Sshin * included, the real payload length will be stored in plenp.
810171259Sdelphij *
811171259Sdelphij * rtalertp - XXX: should be stored more smart way
81253541Sshin */
81353541Sshinstatic int
814171259Sdelphijip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
815171259Sdelphij    struct mbuf **mp, int *offp)
81653541Sshin{
81778064Sume	struct mbuf *m = *mp;
81853541Sshin	int off = *offp, hbhlen;
81953541Sshin	struct ip6_hbh *hbh;
82053541Sshin	u_int8_t *opt;
82153541Sshin
82253541Sshin	/* validation of the length of the header */
82362587Sitojun#ifndef PULLDOWN_TEST
82453541Sshin	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
82553541Sshin	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
82653541Sshin	hbhlen = (hbh->ip6h_len + 1) << 3;
82753541Sshin
82853541Sshin	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
82953541Sshin	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
83062587Sitojun#else
83162587Sitojun	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
83262587Sitojun		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
83362587Sitojun	if (hbh == NULL) {
83462587Sitojun		ip6stat.ip6s_tooshort++;
83562587Sitojun		return -1;
83662587Sitojun	}
83762587Sitojun	hbhlen = (hbh->ip6h_len + 1) << 3;
83862587Sitojun	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
83962587Sitojun		hbhlen);
84062587Sitojun	if (hbh == NULL) {
84162587Sitojun		ip6stat.ip6s_tooshort++;
84262587Sitojun		return -1;
84362587Sitojun	}
84462587Sitojun#endif
84553541Sshin	off += hbhlen;
84653541Sshin	hbhlen -= sizeof(struct ip6_hbh);
84753541Sshin	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
84853541Sshin
84953541Sshin	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
85053541Sshin				hbhlen, rtalertp, plenp) < 0)
851120856Sume		return (-1);
85253541Sshin
85353541Sshin	*offp = off;
85453541Sshin	*mp = m;
855120856Sume	return (0);
85653541Sshin}
85753541Sshin
85853541Sshin/*
85953541Sshin * Search header for all Hop-by-hop options and process each option.
86053541Sshin * This function is separate from ip6_hopopts_input() in order to
86153541Sshin * handle a case where the sending node itself process its hop-by-hop
86253541Sshin * options header. In such a case, the function is called from ip6_output().
86378064Sume *
86478064Sume * The function assumes that hbh header is located right after the IPv6 header
86578064Sume * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
86678064Sume * opthead + hbhlen is located in continuous memory region.
86753541Sshin */
86853541Sshinint
869171259Sdelphijip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
870171259Sdelphij    u_int32_t *rtalertp, u_int32_t *plenp)
87153541Sshin{
87253541Sshin	struct ip6_hdr *ip6;
87353541Sshin	int optlen = 0;
87453541Sshin	u_int8_t *opt = opthead;
87553541Sshin	u_int16_t rtalert_val;
87662587Sitojun	u_int32_t jumboplen;
87778064Sume	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
87853541Sshin
87953541Sshin	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
88078064Sume		switch (*opt) {
88178064Sume		case IP6OPT_PAD1:
88278064Sume			optlen = 1;
88378064Sume			break;
88478064Sume		case IP6OPT_PADN:
88578064Sume			if (hbhlen < IP6OPT_MINLEN) {
88678064Sume				ip6stat.ip6s_toosmall++;
88778064Sume				goto bad;
88878064Sume			}
88978064Sume			optlen = *(opt + 1) + 2;
89078064Sume			break;
891121472Sume		case IP6OPT_ROUTER_ALERT:
89262587Sitojun			/* XXX may need check for alignment */
89378064Sume			if (hbhlen < IP6OPT_RTALERT_LEN) {
89478064Sume				ip6stat.ip6s_toosmall++;
89578064Sume				goto bad;
89678064Sume			}
89778064Sume			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
89878064Sume				/* XXX stat */
89978064Sume				icmp6_error(m, ICMP6_PARAM_PROB,
900120913Sume				    ICMP6_PARAMPROB_HEADER,
901120913Sume				    erroff + opt + 1 - opthead);
902120856Sume				return (-1);
90378064Sume			}
90478064Sume			optlen = IP6OPT_RTALERT_LEN;
90578064Sume			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
90678064Sume			*rtalertp = ntohs(rtalert_val);
90778064Sume			break;
90878064Sume		case IP6OPT_JUMBO:
90978064Sume			/* XXX may need check for alignment */
91062587Sitojun			if (hbhlen < IP6OPT_JUMBO_LEN) {
91162587Sitojun				ip6stat.ip6s_toosmall++;
91262587Sitojun				goto bad;
91362587Sitojun			}
91478064Sume			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
91578064Sume				/* XXX stat */
91678064Sume				icmp6_error(m, ICMP6_PARAM_PROB,
917120913Sume				    ICMP6_PARAMPROB_HEADER,
918120913Sume				    erroff + opt + 1 - opthead);
919120856Sume				return (-1);
92078064Sume			}
92162587Sitojun			optlen = IP6OPT_JUMBO_LEN;
92253541Sshin
92362587Sitojun			/*
92462587Sitojun			 * IPv6 packets that have non 0 payload length
92578064Sume			 * must not contain a jumbo payload option.
92662587Sitojun			 */
92762587Sitojun			ip6 = mtod(m, struct ip6_hdr *);
92862587Sitojun			if (ip6->ip6_plen) {
92962587Sitojun				ip6stat.ip6s_badoptions++;
93062587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
931120913Sume				    ICMP6_PARAMPROB_HEADER,
932120913Sume				    erroff + opt - opthead);
933120856Sume				return (-1);
93462587Sitojun			}
93553541Sshin
93662587Sitojun			/*
93762587Sitojun			 * We may see jumbolen in unaligned location, so
93862587Sitojun			 * we'd need to perform bcopy().
93962587Sitojun			 */
94062587Sitojun			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
94162587Sitojun			jumboplen = (u_int32_t)htonl(jumboplen);
94262587Sitojun
94362587Sitojun#if 1
94462587Sitojun			/*
94562587Sitojun			 * if there are multiple jumbo payload options,
94662587Sitojun			 * *plenp will be non-zero and the packet will be
94762587Sitojun			 * rejected.
94862587Sitojun			 * the behavior may need some debate in ipngwg -
94962587Sitojun			 * multiple options does not make sense, however,
95062587Sitojun			 * there's no explicit mention in specification.
95162587Sitojun			 */
95262587Sitojun			if (*plenp != 0) {
95362587Sitojun				ip6stat.ip6s_badoptions++;
95462587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
955120913Sume				    ICMP6_PARAMPROB_HEADER,
956120913Sume				    erroff + opt + 2 - opthead);
957120856Sume				return (-1);
95862587Sitojun			}
95962587Sitojun#endif
96062587Sitojun
96162587Sitojun			/*
96262587Sitojun			 * jumbo payload length must be larger than 65535.
96362587Sitojun			 */
96462587Sitojun			if (jumboplen <= IPV6_MAXPACKET) {
96562587Sitojun				ip6stat.ip6s_badoptions++;
96662587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
967120913Sume				    ICMP6_PARAMPROB_HEADER,
968120913Sume				    erroff + opt + 2 - opthead);
969120856Sume				return (-1);
97062587Sitojun			}
97162587Sitojun			*plenp = jumboplen;
97262587Sitojun
97362587Sitojun			break;
97478064Sume		default:		/* unknown option */
97578064Sume			if (hbhlen < IP6OPT_MINLEN) {
97678064Sume				ip6stat.ip6s_toosmall++;
97778064Sume				goto bad;
97878064Sume			}
97978064Sume			optlen = ip6_unknown_opt(opt, m,
98078064Sume			    erroff + opt - opthead);
98178064Sume			if (optlen == -1)
982120856Sume				return (-1);
98378064Sume			optlen += 2;
98478064Sume			break;
98553541Sshin		}
98653541Sshin	}
98753541Sshin
988120856Sume	return (0);
98953541Sshin
99053541Sshin  bad:
99153541Sshin	m_freem(m);
992120856Sume	return (-1);
99353541Sshin}
99453541Sshin
99553541Sshin/*
99653541Sshin * Unknown option processing.
99753541Sshin * The third argument `off' is the offset from the IPv6 header to the option,
99853541Sshin * which is necessary if the IPv6 header the and option header and IPv6 header
99953541Sshin * is not continuous in order to return an ICMPv6 error.
100053541Sshin */
100153541Sshinint
1002171259Sdelphijip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
100353541Sshin{
100453541Sshin	struct ip6_hdr *ip6;
100553541Sshin
100678064Sume	switch (IP6OPT_TYPE(*optp)) {
100778064Sume	case IP6OPT_TYPE_SKIP: /* ignore the option */
1008120856Sume		return ((int)*(optp + 1));
100978064Sume	case IP6OPT_TYPE_DISCARD:	/* silently discard */
101078064Sume		m_freem(m);
1011120856Sume		return (-1);
101278064Sume	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
101378064Sume		ip6stat.ip6s_badoptions++;
101478064Sume		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1015120856Sume		return (-1);
101678064Sume	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
101778064Sume		ip6stat.ip6s_badoptions++;
101878064Sume		ip6 = mtod(m, struct ip6_hdr *);
101978064Sume		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
102078064Sume		    (m->m_flags & (M_BCAST|M_MCAST)))
102178064Sume			m_freem(m);
102278064Sume		else
102378064Sume			icmp6_error(m, ICMP6_PARAM_PROB,
102478064Sume				    ICMP6_PARAMPROB_OPTION, off);
1025120856Sume		return (-1);
102653541Sshin	}
102753541Sshin
102853541Sshin	m_freem(m);		/* XXX: NOTREACHED */
1029120856Sume	return (-1);
103053541Sshin}
103153541Sshin
103253541Sshin/*
103362587Sitojun * Create the "control" list for this pcb.
103478064Sume * The function will not modify mbuf chain at all.
103562587Sitojun *
103678064Sume * with KAME mbuf chain restriction:
103762587Sitojun * The routine will be called from upper layer handlers like tcp6_input().
103862587Sitojun * Thus the routine assumes that the caller (tcp6_input) have already
103962587Sitojun * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
104062587Sitojun * very first mbuf on the mbuf chain.
104153541Sshin */
104253541Sshinvoid
1043171259Sdelphijip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
104453541Sshin{
1045121472Sume#define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1046121674Sume	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
104753541Sshin
104878064Sume#ifdef SO_TIMESTAMP
104978064Sume	if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
105053541Sshin		struct timeval tv;
105153541Sshin
105253541Sshin		microtime(&tv);
105353541Sshin		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1054120913Sume		    SCM_TIMESTAMP, SOL_SOCKET);
1055121472Sume		if (*mp)
105653541Sshin			mp = &(*mp)->m_next;
105797658Stanimura	}
105862587Sitojun#endif
105962587Sitojun
1060121631Sume	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
1061121631Sume		return;
1062121631Sume
106353541Sshin	/* RFC 2292 sec. 5 */
106478064Sume	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
106553541Sshin		struct in6_pktinfo pi6;
1066120913Sume
106753541Sshin		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1068121315Sume		in6_clearscope(&pi6.ipi6_addr);	/* XXX */
1069120913Sume		pi6.ipi6_ifindex =
1070120913Sume		    (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1071120913Sume
107253541Sshin		*mp = sbcreatecontrol((caddr_t) &pi6,
1073120913Sume		    sizeof(struct in6_pktinfo),
1074121472Sume		    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1075121472Sume		if (*mp)
107653541Sshin			mp = &(*mp)->m_next;
107753541Sshin	}
107878064Sume
107978064Sume	if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
108053541Sshin		int hlim = ip6->ip6_hlim & 0xff;
1081120913Sume
1082120913Sume		*mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1083121472Sume		    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1084121472Sume		if (*mp)
108553541Sshin			mp = &(*mp)->m_next;
108653541Sshin	}
108753541Sshin
1088121472Sume	if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1089121472Sume		u_int32_t flowinfo;
1090121472Sume		int tclass;
1091121472Sume
1092121472Sume		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1093121472Sume		flowinfo >>= 20;
1094121472Sume
1095121472Sume		tclass = flowinfo & 0xff;
1096121472Sume		*mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass),
1097121472Sume		    IPV6_TCLASS, IPPROTO_IPV6);
1098121472Sume		if (*mp)
1099121472Sume			mp = &(*mp)->m_next;
1100121472Sume	}
1101121472Sume
110253541Sshin	/*
1103130002Sume	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
1104130002Sume	 * privilege for the option (see ip6_ctloutput), but it might be too
1105130002Sume	 * strict, since there might be some hop-by-hop options which can be
1106130002Sume	 * returned to normal user.
1107130002Sume	 * See also RFC 2292 section 6 (or RFC 3542 section 8).
110853541Sshin	 */
1109121472Sume	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
111053541Sshin		/*
111153541Sshin		 * Check if a hop-by-hop options header is contatined in the
111253541Sshin		 * received packet, and if so, store the options as ancillary
111353541Sshin		 * data. Note that a hop-by-hop options header must be
1114120913Sume		 * just after the IPv6 header, which is assured through the
1115120913Sume		 * IPv6 input processing.
111653541Sshin		 */
111753541Sshin		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
111862587Sitojun			struct ip6_hbh *hbh;
111978064Sume			int hbhlen = 0;
112078064Sume#ifdef PULLDOWN_TEST
112178064Sume			struct mbuf *ext;
112278064Sume#endif
112353541Sshin
112462587Sitojun#ifndef PULLDOWN_TEST
112562587Sitojun			hbh = (struct ip6_hbh *)(ip6 + 1);
112662587Sitojun			hbhlen = (hbh->ip6h_len + 1) << 3;
112762587Sitojun#else
112878064Sume			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
112978064Sume			    ip6->ip6_nxt);
113078064Sume			if (ext == NULL) {
113162587Sitojun				ip6stat.ip6s_tooshort++;
113262587Sitojun				return;
113362587Sitojun			}
113478064Sume			hbh = mtod(ext, struct ip6_hbh *);
113562587Sitojun			hbhlen = (hbh->ip6h_len + 1) << 3;
113678064Sume			if (hbhlen != ext->m_len) {
113778064Sume				m_freem(ext);
113862587Sitojun				ip6stat.ip6s_tooshort++;
113962587Sitojun				return;
114062587Sitojun			}
114162587Sitojun#endif
114262587Sitojun
114353541Sshin			/*
1144120913Sume			 * XXX: We copy the whole header even if a
1145120913Sume			 * jumbo payload option is included, the option which
1146120913Sume			 * is to be removed before returning according to
1147120913Sume			 * RFC2292.
1148148169Sume			 * Note: this constraint is removed in RFC3542
114953541Sshin			 */
115062587Sitojun			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1151121472Sume			    IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1152121472Sume			    IPPROTO_IPV6);
1153121472Sume			if (*mp)
115453541Sshin				mp = &(*mp)->m_next;
115578064Sume#ifdef PULLDOWN_TEST
115678064Sume			m_freem(ext);
115778064Sume#endif
115853541Sshin		}
115953541Sshin	}
116053541Sshin
1161121472Sume	if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
116278064Sume		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
116353541Sshin
116453541Sshin		/*
116553541Sshin		 * Search for destination options headers or routing
116653541Sshin		 * header(s) through the header chain, and stores each
116753541Sshin		 * header as ancillary data.
116853541Sshin		 * Note that the order of the headers remains in
116953541Sshin		 * the chain of ancillary data.
117053541Sshin		 */
117178064Sume		while (1) {	/* is explicit loop prevention necessary? */
117278064Sume			struct ip6_ext *ip6e = NULL;
117362587Sitojun			int elen;
117478064Sume#ifdef PULLDOWN_TEST
117578064Sume			struct mbuf *ext = NULL;
117678064Sume#endif
117753541Sshin
117878064Sume			/*
117978064Sume			 * if it is not an extension header, don't try to
118078064Sume			 * pull it from the chain.
118178064Sume			 */
118278064Sume			switch (nxt) {
118378064Sume			case IPPROTO_DSTOPTS:
118478064Sume			case IPPROTO_ROUTING:
118578064Sume			case IPPROTO_HOPOPTS:
118678064Sume			case IPPROTO_AH: /* is it possible? */
118778064Sume				break;
118878064Sume			default:
118978064Sume				goto loopend;
119078064Sume			}
119178064Sume
119262587Sitojun#ifndef PULLDOWN_TEST
119378064Sume			if (off + sizeof(*ip6e) > m->m_len)
119478064Sume				goto loopend;
119562587Sitojun			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
119662587Sitojun			if (nxt == IPPROTO_AH)
119762587Sitojun				elen = (ip6e->ip6e_len + 2) << 2;
119862587Sitojun			else
119962587Sitojun				elen = (ip6e->ip6e_len + 1) << 3;
120078064Sume			if (off + elen > m->m_len)
120178064Sume				goto loopend;
120262587Sitojun#else
120378064Sume			ext = ip6_pullexthdr(m, off, nxt);
120478064Sume			if (ext == NULL) {
120562587Sitojun				ip6stat.ip6s_tooshort++;
120662587Sitojun				return;
120762587Sitojun			}
120878064Sume			ip6e = mtod(ext, struct ip6_ext *);
120962587Sitojun			if (nxt == IPPROTO_AH)
121062587Sitojun				elen = (ip6e->ip6e_len + 2) << 2;
121162587Sitojun			else
121262587Sitojun				elen = (ip6e->ip6e_len + 1) << 3;
121378064Sume			if (elen != ext->m_len) {
121478064Sume				m_freem(ext);
121562587Sitojun				ip6stat.ip6s_tooshort++;
121662587Sitojun				return;
121762587Sitojun			}
121862587Sitojun#endif
121962587Sitojun
122078064Sume			switch (nxt) {
122178064Sume			case IPPROTO_DSTOPTS:
1222120913Sume				if (!(in6p->in6p_flags & IN6P_DSTOPTS))
122378064Sume					break;
122453541Sshin
122578064Sume				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1226121472Sume				    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1227120913Sume				    IPPROTO_IPV6);
122878064Sume				if (*mp)
122978064Sume					mp = &(*mp)->m_next;
123078064Sume				break;
123178064Sume			case IPPROTO_ROUTING:
123278064Sume				if (!in6p->in6p_flags & IN6P_RTHDR)
123378064Sume					break;
123453541Sshin
123578064Sume				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1236121472Sume				    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1237120913Sume				    IPPROTO_IPV6);
123878064Sume				if (*mp)
123978064Sume					mp = &(*mp)->m_next;
124078064Sume				break;
124178064Sume			case IPPROTO_HOPOPTS:
124278064Sume			case IPPROTO_AH: /* is it possible? */
124378064Sume				break;
124453541Sshin
124578064Sume			default:
124678064Sume				/*
1247171260Sdelphij				 * other cases have been filtered in the above.
124878064Sume				 * none will visit this case.  here we supply
124978064Sume				 * the code just in case (nxt overwritten or
125078064Sume				 * other cases).
125178064Sume				 */
125278064Sume#ifdef PULLDOWN_TEST
125378064Sume				m_freem(ext);
125478064Sume#endif
125578064Sume				goto loopend;
125653541Sshin
125753541Sshin			}
125853541Sshin
125953541Sshin			/* proceed with the next header. */
126062587Sitojun			off += elen;
126153541Sshin			nxt = ip6e->ip6e_nxt;
126278064Sume			ip6e = NULL;
126378064Sume#ifdef PULLDOWN_TEST
126478064Sume			m_freem(ext);
126578064Sume			ext = NULL;
126678064Sume#endif
126753541Sshin		}
126853541Sshin	  loopend:
126978064Sume		;
127053541Sshin	}
127178064Sume
1272121472Sume#undef IS2292
127378064Sume}
127478064Sume
1275125776Sumevoid
1276171259Sdelphijip6_notify_pmtu(struct inpcb *in6p, struct sockaddr_in6 *dst, u_int32_t *mtu)
1277125776Sume{
1278125776Sume	struct socket *so;
1279125776Sume	struct mbuf *m_mtu;
1280125776Sume	struct ip6_mtuinfo mtuctl;
1281125776Sume
1282125776Sume	so =  in6p->inp_socket;
1283125776Sume
1284125776Sume	if (mtu == NULL)
1285125776Sume		return;
1286125776Sume
1287125776Sume#ifdef DIAGNOSTIC
1288125776Sume	if (so == NULL)		/* I believe this is impossible */
1289125776Sume		panic("ip6_notify_pmtu: socket is NULL");
1290125776Sume#endif
1291125776Sume
1292125776Sume	bzero(&mtuctl, sizeof(mtuctl));	/* zero-clear for safety */
1293125776Sume	mtuctl.ip6m_mtu = *mtu;
1294125776Sume	mtuctl.ip6m_addr = *dst;
1295148385Sume	if (sa6_recoverscope(&mtuctl.ip6m_addr))
1296148385Sume		return;
1297125776Sume
1298125776Sume	if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1299125776Sume	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1300125776Sume		return;
1301125776Sume
1302125776Sume	if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1303125776Sume	    == 0) {
1304125776Sume		m_freem(m_mtu);
1305125776Sume		/* XXX: should count statistics */
1306125776Sume	} else
1307125776Sume		sorwakeup(so);
1308125776Sume
1309125776Sume	return;
1310125776Sume}
1311125776Sume
131278064Sume#ifdef PULLDOWN_TEST
131378064Sume/*
131478064Sume * pull single extension header from mbuf chain.  returns single mbuf that
131578064Sume * contains the result, or NULL on error.
131678064Sume */
131778064Sumestatic struct mbuf *
1318171259Sdelphijip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
131978064Sume{
132078064Sume	struct ip6_ext ip6e;
132178064Sume	size_t elen;
132278064Sume	struct mbuf *n;
132378064Sume
132478064Sume#ifdef DIAGNOSTIC
132578064Sume	switch (nxt) {
132678064Sume	case IPPROTO_DSTOPTS:
132778064Sume	case IPPROTO_ROUTING:
132878064Sume	case IPPROTO_HOPOPTS:
132978064Sume	case IPPROTO_AH: /* is it possible? */
133078064Sume		break;
133178064Sume	default:
133278064Sume		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
133353541Sshin	}
133478064Sume#endif
133578064Sume
133678064Sume	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
133778064Sume	if (nxt == IPPROTO_AH)
133878064Sume		elen = (ip6e.ip6e_len + 2) << 2;
133978064Sume	else
134078064Sume		elen = (ip6e.ip6e_len + 1) << 3;
134178064Sume
1342111119Simp	MGET(n, M_DONTWAIT, MT_DATA);
134378064Sume	if (n && elen >= MLEN) {
1344111119Simp		MCLGET(n, M_DONTWAIT);
134578064Sume		if ((n->m_flags & M_EXT) == 0) {
134678064Sume			m_free(n);
134778064Sume			n = NULL;
134878064Sume		}
134953541Sshin	}
135078064Sume	if (!n)
135178064Sume		return NULL;
135262587Sitojun
135378064Sume	n->m_len = 0;
135478064Sume	if (elen >= M_TRAILINGSPACE(n)) {
135578064Sume		m_free(n);
135678064Sume		return NULL;
135778064Sume	}
135878064Sume
135978064Sume	m_copydata(m, off, elen, mtod(n, caddr_t));
136078064Sume	n->m_len = elen;
136178064Sume	return n;
136253541Sshin}
136378064Sume#endif
136453541Sshin
136553541Sshin/*
136653541Sshin * Get pointer to the previous header followed by the header
136753541Sshin * currently processed.
136853541Sshin * XXX: This function supposes that
136953541Sshin *	M includes all headers,
137053541Sshin *	the next header field and the header length field of each header
137153541Sshin *	are valid, and
137253541Sshin *	the sum of each header length equals to OFF.
137353541Sshin * Because of these assumptions, this function must be called very
137453541Sshin * carefully. Moreover, it will not be used in the near future when
137553541Sshin * we develop `neater' mechanism to process extension headers.
137653541Sshin */
137753541Sshinchar *
1378171259Sdelphijip6_get_prevhdr(struct mbuf *m, int off)
137953541Sshin{
138053541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
138153541Sshin
138253541Sshin	if (off == sizeof(struct ip6_hdr))
1383120856Sume		return (&ip6->ip6_nxt);
138453541Sshin	else {
138553541Sshin		int len, nxt;
138653541Sshin		struct ip6_ext *ip6e = NULL;
138753541Sshin
138853541Sshin		nxt = ip6->ip6_nxt;
138953541Sshin		len = sizeof(struct ip6_hdr);
139053541Sshin		while (len < off) {
139153541Sshin			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
139253541Sshin
139378064Sume			switch (nxt) {
139453541Sshin			case IPPROTO_FRAGMENT:
139553541Sshin				len += sizeof(struct ip6_frag);
139653541Sshin				break;
139753541Sshin			case IPPROTO_AH:
139853541Sshin				len += (ip6e->ip6e_len + 2) << 2;
139953541Sshin				break;
140053541Sshin			default:
140153541Sshin				len += (ip6e->ip6e_len + 1) << 3;
140253541Sshin				break;
140353541Sshin			}
140453541Sshin			nxt = ip6e->ip6e_nxt;
140553541Sshin		}
140653541Sshin		if (ip6e)
1407120856Sume			return (&ip6e->ip6e_nxt);
140853541Sshin		else
140953541Sshin			return NULL;
141053541Sshin	}
141153541Sshin}
141253541Sshin
141353541Sshin/*
141462587Sitojun * get next header offset.  m will be retained.
141562587Sitojun */
141662587Sitojunint
1417171259Sdelphijip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
141862587Sitojun{
141962587Sitojun	struct ip6_hdr ip6;
142062587Sitojun	struct ip6_ext ip6e;
142162587Sitojun	struct ip6_frag fh;
142262587Sitojun
142362587Sitojun	/* just in case */
142462587Sitojun	if (m == NULL)
142562587Sitojun		panic("ip6_nexthdr: m == NULL");
142662587Sitojun	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
142762587Sitojun		return -1;
142862587Sitojun
142962587Sitojun	switch (proto) {
143062587Sitojun	case IPPROTO_IPV6:
143162587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6))
143262587Sitojun			return -1;
143362587Sitojun		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
143462587Sitojun		if (nxtp)
143562587Sitojun			*nxtp = ip6.ip6_nxt;
143662587Sitojun		off += sizeof(ip6);
143762587Sitojun		return off;
143862587Sitojun
143962587Sitojun	case IPPROTO_FRAGMENT:
144062587Sitojun		/*
144162587Sitojun		 * terminate parsing if it is not the first fragment,
144262587Sitojun		 * it does not make sense to parse through it.
144362587Sitojun		 */
144462587Sitojun		if (m->m_pkthdr.len < off + sizeof(fh))
144562587Sitojun			return -1;
144662587Sitojun		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1447120978Sume		/* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1448120978Sume		if (fh.ip6f_offlg & IP6F_OFF_MASK)
144962587Sitojun			return -1;
145062587Sitojun		if (nxtp)
145162587Sitojun			*nxtp = fh.ip6f_nxt;
145262587Sitojun		off += sizeof(struct ip6_frag);
145362587Sitojun		return off;
145462587Sitojun
145562587Sitojun	case IPPROTO_AH:
145662587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6e))
145762587Sitojun			return -1;
145862587Sitojun		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
145962587Sitojun		if (nxtp)
146062587Sitojun			*nxtp = ip6e.ip6e_nxt;
146162587Sitojun		off += (ip6e.ip6e_len + 2) << 2;
146262587Sitojun		return off;
146362587Sitojun
146462587Sitojun	case IPPROTO_HOPOPTS:
146562587Sitojun	case IPPROTO_ROUTING:
146662587Sitojun	case IPPROTO_DSTOPTS:
146762587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6e))
146862587Sitojun			return -1;
146962587Sitojun		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
147062587Sitojun		if (nxtp)
147162587Sitojun			*nxtp = ip6e.ip6e_nxt;
147262587Sitojun		off += (ip6e.ip6e_len + 1) << 3;
147362587Sitojun		return off;
147462587Sitojun
147562587Sitojun	case IPPROTO_NONE:
147662587Sitojun	case IPPROTO_ESP:
147762587Sitojun	case IPPROTO_IPCOMP:
147862587Sitojun		/* give up */
147962587Sitojun		return -1;
148062587Sitojun
148162587Sitojun	default:
148262587Sitojun		return -1;
148362587Sitojun	}
148462587Sitojun
148562587Sitojun	return -1;
148662587Sitojun}
148762587Sitojun
148862587Sitojun/*
148962587Sitojun * get offset for the last header in the chain.  m will be kept untainted.
149062587Sitojun */
149162587Sitojunint
1492171259Sdelphijip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
149362587Sitojun{
149462587Sitojun	int newoff;
149562587Sitojun	int nxt;
149662587Sitojun
149762587Sitojun	if (!nxtp) {
149862587Sitojun		nxt = -1;
149962587Sitojun		nxtp = &nxt;
150062587Sitojun	}
150162587Sitojun	while (1) {
150262587Sitojun		newoff = ip6_nexthdr(m, off, proto, nxtp);
150362587Sitojun		if (newoff < 0)
150462587Sitojun			return off;
150562587Sitojun		else if (newoff < off)
150662587Sitojun			return -1;	/* invalid */
150762587Sitojun		else if (newoff == off)
150862587Sitojun			return newoff;
150962587Sitojun
151062587Sitojun		off = newoff;
151162587Sitojun		proto = *nxtp;
151262587Sitojun	}
151362587Sitojun}
151462587Sitojun
1515121673Sumestruct ip6aux *
1516171259Sdelphijip6_addaux(struct mbuf *m)
151778064Sume{
1518120913Sume	struct m_tag *mtag;
1519120913Sume
1520120913Sume	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1521120913Sume	if (!mtag) {
1522120913Sume		mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux),
1523120913Sume		    M_NOWAIT);
1524121630Sume		if (mtag) {
1525120913Sume			m_tag_prepend(m, mtag);
1526121630Sume			bzero(mtag + 1, sizeof(struct ip6aux));
1527121630Sume		}
152878064Sume	}
1529121673Sume	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
153078064Sume}
153178064Sume
1532121673Sumestruct ip6aux *
1533171259Sdelphijip6_findaux(struct mbuf *m)
153478064Sume{
1535120913Sume	struct m_tag *mtag;
1536120913Sume
1537120913Sume	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1538121673Sume	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
153978064Sume}
154078064Sume
154178064Sumevoid
1542171259Sdelphijip6_delaux(struct mbuf *m)
154378064Sume{
1544120913Sume	struct m_tag *mtag;
1545120913Sume
1546120913Sume	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1547120913Sume	if (mtag)
1548120913Sume		m_tag_delete(m, mtag);
154978064Sume}
155078064Sume
155162587Sitojun/*
155253541Sshin * System control for IP6
155353541Sshin */
155453541Sshin
155553541Sshinu_char	inet6ctlerrmap[PRC_NCMDS] = {
155653541Sshin	0,		0,		0,		0,
155753541Sshin	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
155853541Sshin	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
155953541Sshin	EMSGSIZE,	EHOSTUNREACH,	0,		0,
156053541Sshin	0,		0,		0,		0,
156153541Sshin	ENOPROTOOPT
156253541Sshin};
1563