ip6_input.c revision 134383
162587Sitojun/*	$FreeBSD: head/sys/netinet6/ip6_input.c 134383 2004-08-27 15:16:24Z andre $	*/
295023Ssuz/*	$KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $	*/
362587Sitojun
453541Sshin/*
553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
653541Sshin * All rights reserved.
753541Sshin *
853541Sshin * Redistribution and use in source and binary forms, with or without
953541Sshin * modification, are permitted provided that the following conditions
1053541Sshin * are met:
1153541Sshin * 1. Redistributions of source code must retain the above copyright
1253541Sshin *    notice, this list of conditions and the following disclaimer.
1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1453541Sshin *    notice, this list of conditions and the following disclaimer in the
1553541Sshin *    documentation and/or other materials provided with the distribution.
1653541Sshin * 3. Neither the name of the project nor the names of its contributors
1753541Sshin *    may be used to endorse or promote products derived from this software
1853541Sshin *    without specific prior written permission.
1953541Sshin *
2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2353541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3053541Sshin * SUCH DAMAGE.
3153541Sshin */
3253541Sshin
3353541Sshin/*
3453541Sshin * Copyright (c) 1982, 1986, 1988, 1993
3553541Sshin *	The Regents of the University of California.  All rights reserved.
3653541Sshin *
3753541Sshin * Redistribution and use in source and binary forms, with or without
3853541Sshin * modification, are permitted provided that the following conditions
3953541Sshin * are met:
4053541Sshin * 1. Redistributions of source code must retain the above copyright
4153541Sshin *    notice, this list of conditions and the following disclaimer.
4253541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4353541Sshin *    notice, this list of conditions and the following disclaimer in the
4453541Sshin *    documentation and/or other materials provided with the distribution.
4553541Sshin * 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_ip6fw.h"
6562587Sitojun#include "opt_inet.h"
6662587Sitojun#include "opt_inet6.h"
6755009Sshin#include "opt_ipsec.h"
6855009Sshin
6953541Sshin#include <sys/param.h>
7053541Sshin#include <sys/systm.h>
7178064Sume#include <sys/malloc.h>
7253541Sshin#include <sys/mbuf.h>
7383366Sjulian#include <sys/proc.h>
7453541Sshin#include <sys/domain.h>
7553541Sshin#include <sys/protosw.h>
7653541Sshin#include <sys/socket.h>
7753541Sshin#include <sys/socketvar.h>
7853541Sshin#include <sys/errno.h>
7953541Sshin#include <sys/time.h>
8053541Sshin#include <sys/kernel.h>
8153541Sshin#include <sys/syslog.h>
8253541Sshin
8353541Sshin#include <net/if.h>
8453541Sshin#include <net/if_types.h>
8553541Sshin#include <net/if_dl.h>
8653541Sshin#include <net/route.h>
8753541Sshin#include <net/netisr.h>
8864060Sdarrenr#include <net/pfil.h>
8953541Sshin
9053541Sshin#include <netinet/in.h>
9153541Sshin#include <netinet/in_systm.h>
9262587Sitojun#ifdef INET
9353541Sshin#include <netinet/ip.h>
9453541Sshin#include <netinet/ip_icmp.h>
9595023Ssuz#endif /* INET */
9662587Sitojun#include <netinet/ip6.h>
9753541Sshin#include <netinet6/in6_var.h>
9853541Sshin#include <netinet6/ip6_var.h>
9962587Sitojun#include <netinet/in_pcb.h>
10062587Sitojun#include <netinet/icmp6.h>
101121161Sume#include <netinet6/scope6_var.h>
10253541Sshin#include <netinet6/in6_ifattach.h>
10353541Sshin#include <netinet6/nd6.h>
10453541Sshin
10578064Sume#ifdef IPSEC
10678064Sume#include <netinet6/ipsec.h>
10778064Sume#ifdef INET6
10878064Sume#include <netinet6/ipsec6.h>
10978064Sume#endif
11078064Sume#endif
11178064Sume
112105199Ssam#ifdef FAST_IPSEC
113105199Ssam#include <netipsec/ipsec.h>
114105199Ssam#include <netipsec/ipsec6.h>
115105199Ssam#define	IPSEC
116105199Ssam#endif /* FAST_IPSEC */
117105199Ssam
11853541Sshin#include <netinet6/ip6_fw.h>
11953541Sshin
12053541Sshin#include <netinet6/ip6protosw.h>
12153541Sshin
12253541Sshin#include <net/net_osdep.h>
12353541Sshin
12462587Sitojunextern struct domain inet6domain;
12553541Sshin
12662587Sitojunu_char ip6_protox[IPPROTO_MAX];
127111888Sjlemonstatic struct ifqueue ip6intrq;
12862587Sitojunstatic int ip6qmaxlen = IFQ_MAXLEN;
12962587Sitojunstruct in6_ifaddr *in6_ifaddr;
13053541Sshin
13178064Sumeextern struct callout in6_tmpaddrtimer_ch;
13278064Sume
13362587Sitojunint ip6_forward_srcrt;			/* XXX */
13462587Sitojunint ip6_sourcecheck;			/* XXX */
13562587Sitojunint ip6_sourcecheck_interval;		/* XXX */
13653541Sshin
13778064Sumeint ip6_ours_check_algorithm;
13878064Sume
139120386Ssamstruct pfil_head inet6_pfil_hook;
14078064Sume
14153541Sshin/* firewall hooks */
14262587Sitojunip6_fw_chk_t *ip6_fw_chk_ptr;
14362587Sitojunip6_fw_ctl_t *ip6_fw_ctl_ptr;
14466303Sumeint ip6_fw_enable = 1;
14553541Sshin
14662587Sitojunstruct ip6stat ip6stat;
14753541Sshin
14862587Sitojunstatic void ip6_init2 __P((void *));
149121673Sumestatic struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *));
15062587Sitojunstatic int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
15162587Sitojun#ifdef PULLDOWN_TEST
15262587Sitojunstatic struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int));
15353541Sshin#endif
15453541Sshin
15553541Sshin/*
15653541Sshin * IP6 initialization: fill in IP6 protocol switch table.
15753541Sshin * All protocols not implemented in kernel go to raw IP6 protocol handler.
15853541Sshin */
15953541Sshinvoid
16053541Sshinip6_init()
16153541Sshin{
16278064Sume	struct ip6protosw *pr;
16378064Sume	int i;
16453541Sshin
16578064Sume#ifdef DIAGNOSTIC
16678064Sume	if (sizeof(struct protosw) != sizeof(struct ip6protosw))
16778064Sume		panic("sizeof(protosw) != sizeof(ip6protosw)");
16878064Sume#endif
16953541Sshin	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
17053541Sshin	if (pr == 0)
17153541Sshin		panic("ip6_init");
17253541Sshin	for (i = 0; i < IPPROTO_MAX; i++)
17353541Sshin		ip6_protox[i] = pr - inet6sw;
17453541Sshin	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
17553541Sshin	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
17653541Sshin		if (pr->pr_domain->dom_family == PF_INET6 &&
17753541Sshin		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
17853541Sshin			ip6_protox[pr->pr_protocol] = pr - inet6sw;
179134383Sandre
180134383Sandre	/* Initialize packet filter hooks. */
181120386Ssam	inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
182120386Ssam	inet6_pfil_hook.ph_af = AF_INET6;
183120386Ssam	if ((i = pfil_head_register(&inet6_pfil_hook)) != 0)
184120386Ssam		printf("%s: WARNING: unable to register pfil hook, "
185120386Ssam			"error %d\n", __func__, i);
186134383Sandre
18753541Sshin	ip6intrq.ifq_maxlen = ip6qmaxlen;
18893818Sjhb	mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF);
189122320Ssam	netisr_register(NETISR_IPV6, ip6_input, &ip6intrq, 0);
190121161Sume	scope6_init();
191121742Sume	addrsel_policy_init();
19253541Sshin	nd6_init();
19353541Sshin	frag6_init();
194120648Sume	ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
19553541Sshin}
19653541Sshin
19753541Sshinstatic void
19853541Sshinip6_init2(dummy)
19953541Sshin	void *dummy;
20053541Sshin{
20153541Sshin
20253541Sshin	/* nd6_timer_init */
20378064Sume	callout_init(&nd6_timer_ch, 0);
20478064Sume	callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
20578064Sume
20678064Sume	/* timer for regeneranation of temporary addresses randomize ID */
207121806Sume	callout_init(&in6_tmpaddrtimer_ch, 0);
20878064Sume	callout_reset(&in6_tmpaddrtimer_ch,
20978064Sume		      (ip6_temp_preferred_lifetime - ip6_desync_factor -
21078064Sume		       ip6_temp_regen_advance) * hz,
21178064Sume		      in6_tmpaddrtimer, NULL);
21253541Sshin}
21353541Sshin
21453541Sshin/* cheat */
21555009Sshin/* This must be after route_init(), which is now SI_ORDER_THIRD */
21655009SshinSYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
21753541Sshin
21853541Sshinextern struct	route_in6 ip6_forward_rt;
21953541Sshin
22053541Sshinvoid
22153541Sshinip6_input(m)
22253541Sshin	struct mbuf *m;
22353541Sshin{
22453541Sshin	struct ip6_hdr *ip6;
22553541Sshin	int off = sizeof(struct ip6_hdr), nest;
22653541Sshin	u_int32_t plen;
22753541Sshin	u_int32_t rtalert = ~0;
22853541Sshin	int nxt, ours = 0;
22953541Sshin	struct ifnet *deliverifp = NULL;
230126444Sume	struct sockaddr_in6 sa6;
231126444Sume	u_int32_t srczone, dstzone;
232121143Ssam	struct in6_addr odst;
233121143Ssam	int srcrt = 0;
234121144Ssam
235122320Ssam	GIANT_REQUIRED;			/* XXX for now */
23653541Sshin#ifdef IPSEC
23753541Sshin	/*
23853541Sshin	 * should the inner packet be considered authentic?
23953541Sshin	 * see comment in ah4_input().
24053541Sshin	 */
24153541Sshin	if (m) {
24253541Sshin		m->m_flags &= ~M_AUTHIPHDR;
24353541Sshin		m->m_flags &= ~M_AUTHIPDGM;
24453541Sshin	}
24553541Sshin#endif
24653541Sshin
24753541Sshin	/*
248121630Sume	 * make sure we don't have onion peering information into m_tag.
24978064Sume	 */
25078064Sume	ip6_delaux(m);
25178064Sume
25278064Sume	/*
25395023Ssuz	 * mbuf statistics
25453541Sshin	 */
25553541Sshin	if (m->m_flags & M_EXT) {
25653541Sshin		if (m->m_next)
25753541Sshin			ip6stat.ip6s_mext2m++;
25853541Sshin		else
25953541Sshin			ip6stat.ip6s_mext1++;
26053541Sshin	} else {
26178064Sume#define M2MMAX	(sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
26253541Sshin		if (m->m_next) {
26353541Sshin			if (m->m_flags & M_LOOP) {
264120913Sume				ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
26578064Sume			} else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
26653541Sshin				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
26753541Sshin			else
26853541Sshin				ip6stat.ip6s_m2m[0]++;
26953541Sshin		} else
27053541Sshin			ip6stat.ip6s_m1++;
27178064Sume#undef M2MMAX
27253541Sshin	}
27353541Sshin
27453541Sshin	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
27553541Sshin	ip6stat.ip6s_total++;
27653541Sshin
27762587Sitojun#ifndef PULLDOWN_TEST
27874336Skuriyama	/*
27974336Skuriyama	 * L2 bridge code and some other code can return mbuf chain
28074336Skuriyama	 * that does not conform to KAME requirement.  too bad.
28174336Skuriyama	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
28274336Skuriyama	 */
28374336Skuriyama	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
28474336Skuriyama		struct mbuf *n;
28574336Skuriyama
286111119Simp		MGETHDR(n, M_DONTWAIT, MT_HEADER);
28777003Sume		if (n)
288108466Ssam			M_MOVE_PKTHDR(n, m);
289108825Ssam		if (n && n->m_pkthdr.len > MHLEN) {
290111119Simp			MCLGET(n, M_DONTWAIT);
29174336Skuriyama			if ((n->m_flags & M_EXT) == 0) {
29274336Skuriyama				m_freem(n);
29374336Skuriyama				n = NULL;
29474336Skuriyama			}
29574336Skuriyama		}
29695023Ssuz		if (n == NULL) {
29777003Sume			m_freem(m);
298120913Sume			return;	/* ENOBUFS */
29977003Sume		}
30074336Skuriyama
301108825Ssam		m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
302108825Ssam		n->m_len = n->m_pkthdr.len;
30374336Skuriyama		m_freem(m);
30474336Skuriyama		m = n;
30574336Skuriyama	}
306120913Sume	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
30762587Sitojun#endif
30853541Sshin
30953541Sshin	if (m->m_len < sizeof(struct ip6_hdr)) {
31053541Sshin		struct ifnet *inifp;
31153541Sshin		inifp = m->m_pkthdr.rcvif;
312120913Sume		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
31353541Sshin			ip6stat.ip6s_toosmall++;
31453541Sshin			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
31553541Sshin			return;
31653541Sshin		}
31753541Sshin	}
31853541Sshin
31953541Sshin	ip6 = mtod(m, struct ip6_hdr *);
32053541Sshin
32153541Sshin	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
32253541Sshin		ip6stat.ip6s_badvers++;
32353541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
32453541Sshin		goto bad;
32553541Sshin	}
32653541Sshin
32753541Sshin	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
32853541Sshin
32953541Sshin	/*
33078064Sume	 * Check against address spoofing/corruption.
33153541Sshin	 */
33253541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
33353541Sshin	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
33478064Sume		/*
33578064Sume		 * XXX: "badscope" is not very suitable for a multicast source.
33678064Sume		 */
33753541Sshin		ip6stat.ip6s_badscope++;
33853541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
33953541Sshin		goto bad;
34053541Sshin	}
341126444Sume	if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
342126444Sume	    !(m->m_flags & M_LOOP)) {
343126444Sume		/*
344126444Sume		 * In this case, the packet should come from the loopback
345126444Sume		 * interface.  However, we cannot just check the if_flags,
346126444Sume		 * because ip6_mloopback() passes the "actual" interface
347126444Sume		 * as the outgoing/incoming interface.
348126444Sume		 */
34978064Sume		ip6stat.ip6s_badscope++;
35078064Sume		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
35178064Sume		goto bad;
35278064Sume	}
35395023Ssuz
354130416Smlaier#ifdef ALTQ
355130416Smlaier	if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
356130416Smlaier		/* packet is dropped by traffic conditioner */
357130416Smlaier		return;
358130416Smlaier	}
359130416Smlaier#endif
36062587Sitojun	/*
36178064Sume	 * The following check is not documented in specs.  A malicious
36278064Sume	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
36378064Sume	 * and bypass security checks (act as if it was from 127.0.0.1 by using
364120913Sume	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
36578064Sume	 *
36678064Sume	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
36778064Sume	 * support IPv4-less kernel compilation, we cannot support SIIT
36878064Sume	 * environment at all.  So, it makes more sense for us to reject any
36978064Sume	 * malicious packets for non-SIIT environment, than try to do a
370120913Sume	 * partial support for SIIT environment.
37162587Sitojun	 */
37278064Sume	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
37378064Sume	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
37478064Sume		ip6stat.ip6s_badscope++;
37578064Sume		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
37678064Sume		goto bad;
37778064Sume	}
37862587Sitojun#if 0
37962587Sitojun	/*
38062587Sitojun	 * Reject packets with IPv4 compatible addresses (auto tunnel).
38162587Sitojun	 *
38262587Sitojun	 * The code forbids auto tunnel relay case in RFC1933 (the check is
38362587Sitojun	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
38462587Sitojun	 * is revised to forbid relaying case.
38562587Sitojun	 */
38662587Sitojun	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
38762587Sitojun	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
38862587Sitojun		ip6stat.ip6s_badscope++;
38962587Sitojun		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
39062587Sitojun		goto bad;
39162587Sitojun	}
39262587Sitojun#endif
39375246Sume
394126444Sume	/*
395126444Sume	 * Drop packets if the link ID portion is already filled.
396126444Sume	 * XXX: this is technically not a good behavior.  But, we internally
397126444Sume	 * use the field to disambiguate link-local addresses, so we cannot
398126444Sume	 * be generous against those a bit strange addresses.
399126444Sume	 */
40078064Sume	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
40178064Sume		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
40278064Sume		    ip6->ip6_src.s6_addr16[1]) {
40353541Sshin			ip6stat.ip6s_badscope++;
40453541Sshin			goto bad;
40553541Sshin		}
406121315Sume		if ((IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) ||
407121315Sume		     IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) &&
40878064Sume		    ip6->ip6_dst.s6_addr16[1]) {
40978064Sume			ip6stat.ip6s_badscope++;
41078064Sume			goto bad;
41178064Sume		}
41253541Sshin	}
41353541Sshin
414126444Sume	/*
415126508Smlaier	 * Run through list of hooks for input packets.
416126508Smlaier	 *
417126508Smlaier	 * NB: Beware of the destination address changing
418126508Smlaier	 *     (e.g. by NAT rewriting).  When this happens,
419126508Smlaier	 *     tell ip6_forward to do the right thing.
420126508Smlaier	 */
421126508Smlaier	odst = ip6->ip6_dst;
422134383Sandre
423134383Sandre	/* Jump over all PFIL processing if hooks are not active. */
424134383Sandre	if (inet6_pfil_hook.ph_busy_count == -1)
425134383Sandre		goto passin;
426134383Sandre
427126508Smlaier	if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN))
428126508Smlaier		return;
429126508Smlaier	if (m == NULL)			/* consumed by filter */
430126508Smlaier		return;
431126508Smlaier	ip6 = mtod(m, struct ip6_hdr *);
432126508Smlaier	srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
433126508Smlaier
434134383Sandrepassin:
435126508Smlaier	/*
436126508Smlaier	 * Check with the firewall...
437126508Smlaier	 */
438126508Smlaier	if (ip6_fw_enable && ip6_fw_chk_ptr) {
439126508Smlaier		u_short port = 0;
440126508Smlaier		/* If ipfw says divert, we have to just drop packet */
441126508Smlaier		/* use port as a dummy argument */
442126508Smlaier		if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
443126508Smlaier			m_freem(m);
444126508Smlaier			m = NULL;
445126508Smlaier		}
446126508Smlaier		if (!m)
447126508Smlaier			return;
448126508Smlaier	}
449126508Smlaier
450126508Smlaier	/*
451126444Sume	 * construct source and destination address structures with
452126444Sume	 * disambiguating their scope zones (if there is ambiguity).
453126444Sume	 * XXX: sin6_family and sin6_len will NOT be referred to, but we fill
454126444Sume	 * in these fields just in case.
455126444Sume	 */
456126444Sume	if (in6_addr2zoneid(m->m_pkthdr.rcvif, &ip6->ip6_src, &srczone) ||
457126444Sume	    in6_addr2zoneid(m->m_pkthdr.rcvif, &ip6->ip6_dst, &dstzone)) {
458126444Sume		/*
459126444Sume		 * Note that these generic checks cover cases that src or
460126444Sume		 * dst are the loopback address and the receiving interface
461126444Sume		 * is not loopback.
462126444Sume		 */
463126444Sume		ip6stat.ip6s_badscope++;
464126444Sume		goto bad;
465126444Sume	}
46653541Sshin
467126444Sume	bzero(&sa6, sizeof(sa6));
468126444Sume	sa6.sin6_family = AF_INET6;
469126444Sume	sa6.sin6_len = sizeof(struct sockaddr_in6);
470126444Sume
471126444Sume	sa6.sin6_addr = ip6->ip6_src;
472126444Sume	sa6.sin6_scope_id = srczone;
473126444Sume	if (in6_embedscope(&ip6->ip6_src, &sa6, NULL, NULL)) {
474126444Sume		/* XXX: should not happen */
475126444Sume		ip6stat.ip6s_badscope++;
476126444Sume		goto bad;
477126444Sume	}
478126444Sume
479126444Sume	sa6.sin6_addr = ip6->ip6_dst;
480126444Sume	sa6.sin6_scope_id = dstzone;
481126444Sume	if (in6_embedscope(&ip6->ip6_dst, &sa6, NULL, NULL)) {
482126444Sume		/* XXX: should not happen */
483126444Sume		ip6stat.ip6s_badscope++;
484126444Sume		goto bad;
485126444Sume	}
486126444Sume
487126489Sume	/* XXX: ff01::%ifN awareness is not merged, yet. */
488126489Sume	if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_src))
489126489Sume		ip6->ip6_src.s6_addr16[1] = 0;
490126489Sume	if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst))
491126489Sume		ip6->ip6_dst.s6_addr16[1] = 0;
492126489Sume
49353541Sshin	/*
49453541Sshin	 * Multicast check
49553541Sshin	 */
49653541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
497120913Sume	  	struct in6_multi *in6m = 0;
49853541Sshin
49953541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
50053541Sshin		/*
50153541Sshin		 * See if we belong to the destination multicast group on the
50253541Sshin		 * arrival interface.
50353541Sshin		 */
50453541Sshin		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
50553541Sshin		if (in6m)
50653541Sshin			ours = 1;
50756723Sshin		else if (!ip6_mrouter) {
50853541Sshin			ip6stat.ip6s_notmember++;
50953541Sshin			ip6stat.ip6s_cantforward++;
51053541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
51153541Sshin			goto bad;
51253541Sshin		}
51353541Sshin		deliverifp = m->m_pkthdr.rcvif;
51453541Sshin		goto hbhcheck;
51553541Sshin	}
51653541Sshin
51753541Sshin	/*
51853541Sshin	 *  Unicast check
51953541Sshin	 */
52062587Sitojun	if (ip6_forward_rt.ro_rt != NULL &&
521120913Sume	    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
52262587Sitojun	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
523120913Sume	    &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
52462587Sitojun		ip6stat.ip6s_forward_cachehit++;
52562587Sitojun	else {
52678064Sume		struct sockaddr_in6 *dst6;
52778064Sume
52853541Sshin		if (ip6_forward_rt.ro_rt) {
52962587Sitojun			/* route is down or destination is different */
53062587Sitojun			ip6stat.ip6s_forward_cachemiss++;
53153541Sshin			RTFREE(ip6_forward_rt.ro_rt);
53253541Sshin			ip6_forward_rt.ro_rt = 0;
53353541Sshin		}
53462587Sitojun
53553541Sshin		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
53678064Sume		dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
53778064Sume		dst6->sin6_len = sizeof(struct sockaddr_in6);
53878064Sume		dst6->sin6_family = AF_INET6;
53978064Sume		dst6->sin6_addr = ip6->ip6_dst;
54053541Sshin
541122921Sandre		rtalloc((struct route *)&ip6_forward_rt);
54253541Sshin	}
54353541Sshin
54453541Sshin#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
54553541Sshin
54653541Sshin	/*
54753541Sshin	 * Accept the packet if the forwarding interface to the destination
54853541Sshin	 * according to the routing table is the loopback interface,
54953541Sshin	 * unless the associated route has a gateway.
55053541Sshin	 * Note that this approach causes to accept a packet if there is a
55153541Sshin	 * route to the loopback interface for the destination of the packet.
55253541Sshin	 * But we think it's even useful in some situations, e.g. when using
55353541Sshin	 * a special daemon which wants to intercept the packet.
55478064Sume	 *
55578064Sume	 * XXX: some OSes automatically make a cloned route for the destination
55678064Sume	 * of an outgoing packet.  If the outgoing interface of the packet
55778064Sume	 * is a loopback one, the kernel would consider the packet to be
55878064Sume	 * accepted, even if we have no such address assinged on the interface.
55978064Sume	 * We check the cloned flag of the route entry to reject such cases,
56078064Sume	 * assuming that route entries for our own addresses are not made by
56178064Sume	 * cloning (it should be true because in6_addloop explicitly installs
56278064Sume	 * the host route).  However, we might have to do an explicit check
56378064Sume	 * while it would be less efficient.  Or, should we rather install a
56478064Sume	 * reject route for such a case?
56553541Sshin	 */
56653541Sshin	if (ip6_forward_rt.ro_rt &&
56753541Sshin	    (ip6_forward_rt.ro_rt->rt_flags &
56853541Sshin	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
56978064Sume#ifdef RTF_WASCLONED
57078064Sume	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
57178064Sume#endif
57278064Sume#ifdef RTF_CLONED
57378064Sume	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
57478064Sume#endif
57562587Sitojun#if 0
57653541Sshin	    /*
57762587Sitojun	     * The check below is redundant since the comparison of
57862587Sitojun	     * the destination and the key of the rtentry has
57962587Sitojun	     * already done through looking up the routing table.
58053541Sshin	     */
58162587Sitojun	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
582120913Sume	    &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
58362587Sitojun#endif
58453541Sshin	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
58553541Sshin		struct in6_ifaddr *ia6 =
58653541Sshin			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
58778064Sume
58862587Sitojun		/*
589121630Sume		 * record address information into m_tag.
59078064Sume		 */
59178064Sume		(void)ip6_setdstifaddr(m, ia6);
59278064Sume
59378064Sume		/*
59462587Sitojun		 * packets to a tentative, duplicated, or somehow invalid
59562587Sitojun		 * address must not be accepted.
59662587Sitojun		 */
59753541Sshin		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
59862587Sitojun			/* this address is ready */
59953541Sshin			ours = 1;
60053541Sshin			deliverifp = ia6->ia_ifp;	/* correct? */
60167334Sjoe			/* Count the packet in the ip address stats */
60267334Sjoe			ia6->ia_ifa.if_ipackets++;
60367334Sjoe			ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
60453541Sshin			goto hbhcheck;
60553541Sshin		} else {
60662587Sitojun			/* address is not ready, so discard the packet. */
60778064Sume			nd6log((LOG_INFO,
60878064Sume			    "ip6_input: packet to an unready address %s->%s\n",
60962587Sitojun			    ip6_sprintf(&ip6->ip6_src),
61078064Sume			    ip6_sprintf(&ip6->ip6_dst)));
61162587Sitojun
61262587Sitojun			goto bad;
61353541Sshin		}
61453541Sshin	}
61553541Sshin
61653541Sshin	/*
617120913Sume	 * FAITH (Firewall Aided Internet Translator)
61853541Sshin	 */
61953541Sshin	if (ip6_keepfaith) {
62053541Sshin		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
62153541Sshin		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
62253541Sshin			/* XXX do we need more sanity checks? */
62353541Sshin			ours = 1;
62495023Ssuz			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
62553541Sshin			goto hbhcheck;
62653541Sshin		}
62753541Sshin	}
62853541Sshin
62953541Sshin	/*
63053541Sshin	 * Now there is no reason to process the packet if it's not our own
63153541Sshin	 * and we're not a router.
63253541Sshin	 */
63353541Sshin	if (!ip6_forwarding) {
63453541Sshin		ip6stat.ip6s_cantforward++;
63553541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
63653541Sshin		goto bad;
63753541Sshin	}
63853541Sshin
63953541Sshin  hbhcheck:
64053541Sshin	/*
641121630Sume	 * record address information into m_tag, if we don't have one yet.
64278064Sume	 * note that we are unable to record it, if the address is not listed
64378064Sume	 * as our interface address (e.g. multicast addresses, addresses
64478064Sume	 * within FAITH prefixes and such).
64578064Sume	 */
64678064Sume	if (deliverifp && !ip6_getdstifaddr(m)) {
64778064Sume		struct in6_ifaddr *ia6;
64878064Sume
64978064Sume		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
65078064Sume		if (ia6) {
65178064Sume			if (!ip6_setdstifaddr(m, ia6)) {
65278064Sume				/*
65378064Sume				 * XXX maybe we should drop the packet here,
65478064Sume				 * as we could not provide enough information
65578064Sume				 * to the upper layers.
65678064Sume				 */
65778064Sume			}
65878064Sume		}
65978064Sume	}
66078064Sume
66178064Sume	/*
66253541Sshin	 * Process Hop-by-Hop options header if it's contained.
66353541Sshin	 * m may be modified in ip6_hopopts_input().
66453541Sshin	 * If a JumboPayload option is included, plen will also be modified.
66553541Sshin	 */
66653541Sshin	plen = (u_int32_t)ntohs(ip6->ip6_plen);
66753541Sshin	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
66862587Sitojun		struct ip6_hbh *hbh;
66962587Sitojun
67053541Sshin		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
67162587Sitojun#if 0	/*touches NULL pointer*/
67253541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
67362587Sitojun#endif
67453541Sshin			return;	/* m have already been freed */
67553541Sshin		}
67662587Sitojun
67753541Sshin		/* adjust pointer */
67853541Sshin		ip6 = mtod(m, struct ip6_hdr *);
67953541Sshin
68053541Sshin		/*
68195023Ssuz		 * if the payload length field is 0 and the next header field
68262587Sitojun		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
68362587Sitojun		 * option MUST be included.
68462587Sitojun		 */
68562587Sitojun		if (ip6->ip6_plen == 0 && plen == 0) {
68662587Sitojun			/*
68762587Sitojun			 * Note that if a valid jumbo payload option is
688120913Sume			 * contained, ip6_hopopts_input() must set a valid
689120913Sume			 * (non-zero) payload length to the variable plen.
69062587Sitojun			 */
69162587Sitojun			ip6stat.ip6s_badoptions++;
69262587Sitojun			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
69362587Sitojun			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
69462587Sitojun			icmp6_error(m, ICMP6_PARAM_PROB,
69562587Sitojun				    ICMP6_PARAMPROB_HEADER,
69662587Sitojun				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
69762587Sitojun			return;
69862587Sitojun		}
69962587Sitojun#ifndef PULLDOWN_TEST
70062587Sitojun		/* ip6_hopopts_input() ensures that mbuf is contiguous */
70162587Sitojun		hbh = (struct ip6_hbh *)(ip6 + 1);
70262587Sitojun#else
70362587Sitojun		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
70462587Sitojun			sizeof(struct ip6_hbh));
70562587Sitojun		if (hbh == NULL) {
70662587Sitojun			ip6stat.ip6s_tooshort++;
70762587Sitojun			return;
70862587Sitojun		}
70962587Sitojun#endif
71062587Sitojun		nxt = hbh->ip6h_nxt;
71162587Sitojun
71262587Sitojun		/*
71353541Sshin		 * accept the packet if a router alert option is included
71453541Sshin		 * and we act as an IPv6 router.
71553541Sshin		 */
71653541Sshin		if (rtalert != ~0 && ip6_forwarding)
71753541Sshin			ours = 1;
71853541Sshin	} else
71953541Sshin		nxt = ip6->ip6_nxt;
72053541Sshin
72153541Sshin	/*
72253541Sshin	 * Check that the amount of data in the buffers
72353541Sshin	 * is as at least much as the IPv6 header would have us expect.
72453541Sshin	 * Trim mbufs if longer than we expect.
72553541Sshin	 * Drop packet if shorter than we expect.
72653541Sshin	 */
72753541Sshin	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
72853541Sshin		ip6stat.ip6s_tooshort++;
72953541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
73053541Sshin		goto bad;
73153541Sshin	}
73253541Sshin	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
73353541Sshin		if (m->m_len == m->m_pkthdr.len) {
73453541Sshin			m->m_len = sizeof(struct ip6_hdr) + plen;
73553541Sshin			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
73653541Sshin		} else
73753541Sshin			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
73853541Sshin	}
73953541Sshin
74053541Sshin	/*
74153541Sshin	 * Forward if desirable.
74253541Sshin	 */
74353541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
74456723Sshin		/*
74556723Sshin		 * If we are acting as a multicast router, all
74656723Sshin		 * incoming multicast packets are passed to the
74756723Sshin		 * kernel-level multicast forwarding function.
74856723Sshin		 * The packet is returned (relatively) intact; if
74956723Sshin		 * ip6_mforward() returns a non-zero value, the packet
75056723Sshin		 * must be discarded, else it may be accepted below.
75156723Sshin		 */
75256723Sshin		if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
75356723Sshin			ip6stat.ip6s_cantforward++;
75456723Sshin			m_freem(m);
75556723Sshin			return;
75656723Sshin		}
75753541Sshin		if (!ours) {
75853541Sshin			m_freem(m);
75953541Sshin			return;
76053541Sshin		}
76153541Sshin	} else if (!ours) {
762121143Ssam		ip6_forward(m, srcrt);
76353541Sshin		return;
764121673Sume	}
76553541Sshin
76662587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
76762587Sitojun
76853541Sshin	/*
76962587Sitojun	 * Malicious party may be able to use IPv4 mapped addr to confuse
77062587Sitojun	 * tcp/udp stack and bypass security checks (act as if it was from
77162587Sitojun	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
77262587Sitojun	 *
77362587Sitojun	 * For SIIT end node behavior, you may want to disable the check.
77462587Sitojun	 * However, you will  become vulnerable to attacks using IPv4 mapped
77562587Sitojun	 * source.
77662587Sitojun	 */
77762587Sitojun	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
77862587Sitojun	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
77962587Sitojun		ip6stat.ip6s_badscope++;
78062587Sitojun		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
78162587Sitojun		goto bad;
78262587Sitojun	}
78362587Sitojun
78462587Sitojun	/*
78553541Sshin	 * Tell launch routine the next header
78653541Sshin	 */
78753541Sshin	ip6stat.ip6s_delivered++;
78853541Sshin	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
78953541Sshin	nest = 0;
79078064Sume
79153541Sshin	while (nxt != IPPROTO_DONE) {
79253541Sshin		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
79353541Sshin			ip6stat.ip6s_toomanyhdr++;
79453541Sshin			goto bad;
79553541Sshin		}
79653541Sshin
79753541Sshin		/*
79853541Sshin		 * protection against faulty packet - there should be
79953541Sshin		 * more sanity checks in header chain processing.
80053541Sshin		 */
80153541Sshin		if (m->m_pkthdr.len < off) {
80253541Sshin			ip6stat.ip6s_tooshort++;
80353541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
80453541Sshin			goto bad;
80553541Sshin		}
80653541Sshin
80778064Sume#ifdef IPSEC
80878064Sume		/*
80978064Sume		 * enforce IPsec policy checking if we are seeing last header.
81078064Sume		 * note that we do not visit this with protocols with pcb layer
81178064Sume		 * code - like udp/tcp/raw ip.
81278064Sume		 */
81378064Sume		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
81478064Sume		    ipsec6_in_reject(m, NULL)) {
81578064Sume			ipsec6stat.in_polvio++;
81678064Sume			goto bad;
81778064Sume		}
81878064Sume#endif
81953541Sshin		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
82053541Sshin	}
82153541Sshin	return;
82253541Sshin bad:
82353541Sshin	m_freem(m);
82453541Sshin}
82553541Sshin
82653541Sshin/*
82778064Sume * set/grab in6_ifaddr correspond to IPv6 destination address.
82878064Sume * XXX backward compatibility wrapper
82978064Sume */
830121673Sumestatic struct ip6aux *
83178064Sumeip6_setdstifaddr(m, ia6)
83278064Sume	struct mbuf *m;
83378064Sume	struct in6_ifaddr *ia6;
83478064Sume{
835121673Sume	struct ip6aux *ip6a;
83678064Sume
837121673Sume	ip6a = ip6_addaux(m);
838121673Sume	if (ip6a)
839121673Sume		ip6a->ip6a_dstia6 = ia6;
840121673Sume	return ip6a;	/* NULL if failed to set */
84178064Sume}
84278064Sume
84378064Sumestruct in6_ifaddr *
84478064Sumeip6_getdstifaddr(m)
84578064Sume	struct mbuf *m;
84678064Sume{
847121673Sume	struct ip6aux *ip6a;
84878064Sume
849121673Sume	ip6a = ip6_findaux(m);
850121673Sume	if (ip6a)
851121673Sume		return ip6a->ip6a_dstia6;
85278064Sume	else
85378064Sume		return NULL;
85478064Sume}
85578064Sume
85678064Sume/*
85753541Sshin * Hop-by-Hop options header processing. If a valid jumbo payload option is
85853541Sshin * included, the real payload length will be stored in plenp.
85953541Sshin */
86053541Sshinstatic int
86153541Sshinip6_hopopts_input(plenp, rtalertp, mp, offp)
86253541Sshin	u_int32_t *plenp;
86353541Sshin	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
86453541Sshin	struct mbuf **mp;
86553541Sshin	int *offp;
86653541Sshin{
86778064Sume	struct mbuf *m = *mp;
86853541Sshin	int off = *offp, hbhlen;
86953541Sshin	struct ip6_hbh *hbh;
87053541Sshin	u_int8_t *opt;
87153541Sshin
87253541Sshin	/* validation of the length of the header */
87362587Sitojun#ifndef PULLDOWN_TEST
87453541Sshin	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
87553541Sshin	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
87653541Sshin	hbhlen = (hbh->ip6h_len + 1) << 3;
87753541Sshin
87853541Sshin	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
87953541Sshin	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
88062587Sitojun#else
88162587Sitojun	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
88262587Sitojun		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
88362587Sitojun	if (hbh == NULL) {
88462587Sitojun		ip6stat.ip6s_tooshort++;
88562587Sitojun		return -1;
88662587Sitojun	}
88762587Sitojun	hbhlen = (hbh->ip6h_len + 1) << 3;
88862587Sitojun	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
88962587Sitojun		hbhlen);
89062587Sitojun	if (hbh == NULL) {
89162587Sitojun		ip6stat.ip6s_tooshort++;
89262587Sitojun		return -1;
89362587Sitojun	}
89462587Sitojun#endif
89553541Sshin	off += hbhlen;
89653541Sshin	hbhlen -= sizeof(struct ip6_hbh);
89753541Sshin	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
89853541Sshin
89953541Sshin	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
90053541Sshin				hbhlen, rtalertp, plenp) < 0)
901120856Sume		return (-1);
90253541Sshin
90353541Sshin	*offp = off;
90453541Sshin	*mp = m;
905120856Sume	return (0);
90653541Sshin}
90753541Sshin
90853541Sshin/*
90953541Sshin * Search header for all Hop-by-hop options and process each option.
91053541Sshin * This function is separate from ip6_hopopts_input() in order to
91153541Sshin * handle a case where the sending node itself process its hop-by-hop
91253541Sshin * options header. In such a case, the function is called from ip6_output().
91378064Sume *
91478064Sume * The function assumes that hbh header is located right after the IPv6 header
91578064Sume * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
91678064Sume * opthead + hbhlen is located in continuous memory region.
91753541Sshin */
91853541Sshinint
91953541Sshinip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
92053541Sshin	struct mbuf *m;
92153541Sshin	u_int8_t *opthead;
92253541Sshin	int hbhlen;
92353541Sshin	u_int32_t *rtalertp;
92453541Sshin	u_int32_t *plenp;
92553541Sshin{
92653541Sshin	struct ip6_hdr *ip6;
92753541Sshin	int optlen = 0;
92853541Sshin	u_int8_t *opt = opthead;
92953541Sshin	u_int16_t rtalert_val;
93062587Sitojun	u_int32_t jumboplen;
93178064Sume	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
93253541Sshin
93353541Sshin	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
93478064Sume		switch (*opt) {
93578064Sume		case IP6OPT_PAD1:
93678064Sume			optlen = 1;
93778064Sume			break;
93878064Sume		case IP6OPT_PADN:
93978064Sume			if (hbhlen < IP6OPT_MINLEN) {
94078064Sume				ip6stat.ip6s_toosmall++;
94178064Sume				goto bad;
94278064Sume			}
94378064Sume			optlen = *(opt + 1) + 2;
94478064Sume			break;
945121472Sume		case IP6OPT_ROUTER_ALERT:
94662587Sitojun			/* XXX may need check for alignment */
94778064Sume			if (hbhlen < IP6OPT_RTALERT_LEN) {
94878064Sume				ip6stat.ip6s_toosmall++;
94978064Sume				goto bad;
95078064Sume			}
95178064Sume			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
95278064Sume				/* XXX stat */
95378064Sume				icmp6_error(m, ICMP6_PARAM_PROB,
954120913Sume				    ICMP6_PARAMPROB_HEADER,
955120913Sume				    erroff + opt + 1 - opthead);
956120856Sume				return (-1);
95778064Sume			}
95878064Sume			optlen = IP6OPT_RTALERT_LEN;
95978064Sume			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
96078064Sume			*rtalertp = ntohs(rtalert_val);
96178064Sume			break;
96278064Sume		case IP6OPT_JUMBO:
96378064Sume			/* XXX may need check for alignment */
96462587Sitojun			if (hbhlen < IP6OPT_JUMBO_LEN) {
96562587Sitojun				ip6stat.ip6s_toosmall++;
96662587Sitojun				goto bad;
96762587Sitojun			}
96878064Sume			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
96978064Sume				/* XXX stat */
97078064Sume				icmp6_error(m, ICMP6_PARAM_PROB,
971120913Sume				    ICMP6_PARAMPROB_HEADER,
972120913Sume				    erroff + opt + 1 - opthead);
973120856Sume				return (-1);
97478064Sume			}
97562587Sitojun			optlen = IP6OPT_JUMBO_LEN;
97653541Sshin
97762587Sitojun			/*
97862587Sitojun			 * IPv6 packets that have non 0 payload length
97978064Sume			 * must not contain a jumbo payload option.
98062587Sitojun			 */
98162587Sitojun			ip6 = mtod(m, struct ip6_hdr *);
98262587Sitojun			if (ip6->ip6_plen) {
98362587Sitojun				ip6stat.ip6s_badoptions++;
98462587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
985120913Sume				    ICMP6_PARAMPROB_HEADER,
986120913Sume				    erroff + opt - opthead);
987120856Sume				return (-1);
98862587Sitojun			}
98953541Sshin
99062587Sitojun			/*
99162587Sitojun			 * We may see jumbolen in unaligned location, so
99262587Sitojun			 * we'd need to perform bcopy().
99362587Sitojun			 */
99462587Sitojun			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
99562587Sitojun			jumboplen = (u_int32_t)htonl(jumboplen);
99662587Sitojun
99762587Sitojun#if 1
99862587Sitojun			/*
99962587Sitojun			 * if there are multiple jumbo payload options,
100062587Sitojun			 * *plenp will be non-zero and the packet will be
100162587Sitojun			 * rejected.
100262587Sitojun			 * the behavior may need some debate in ipngwg -
100362587Sitojun			 * multiple options does not make sense, however,
100462587Sitojun			 * there's no explicit mention in specification.
100562587Sitojun			 */
100662587Sitojun			if (*plenp != 0) {
100762587Sitojun				ip6stat.ip6s_badoptions++;
100862587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
1009120913Sume				    ICMP6_PARAMPROB_HEADER,
1010120913Sume				    erroff + opt + 2 - opthead);
1011120856Sume				return (-1);
101262587Sitojun			}
101362587Sitojun#endif
101462587Sitojun
101562587Sitojun			/*
101662587Sitojun			 * jumbo payload length must be larger than 65535.
101762587Sitojun			 */
101862587Sitojun			if (jumboplen <= IPV6_MAXPACKET) {
101962587Sitojun				ip6stat.ip6s_badoptions++;
102062587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
1021120913Sume				    ICMP6_PARAMPROB_HEADER,
1022120913Sume				    erroff + opt + 2 - opthead);
1023120856Sume				return (-1);
102462587Sitojun			}
102562587Sitojun			*plenp = jumboplen;
102662587Sitojun
102762587Sitojun			break;
102878064Sume		default:		/* unknown option */
102978064Sume			if (hbhlen < IP6OPT_MINLEN) {
103078064Sume				ip6stat.ip6s_toosmall++;
103178064Sume				goto bad;
103278064Sume			}
103378064Sume			optlen = ip6_unknown_opt(opt, m,
103478064Sume			    erroff + opt - opthead);
103578064Sume			if (optlen == -1)
1036120856Sume				return (-1);
103778064Sume			optlen += 2;
103878064Sume			break;
103953541Sshin		}
104053541Sshin	}
104153541Sshin
1042120856Sume	return (0);
104353541Sshin
104453541Sshin  bad:
104553541Sshin	m_freem(m);
1046120856Sume	return (-1);
104753541Sshin}
104853541Sshin
104953541Sshin/*
105053541Sshin * Unknown option processing.
105153541Sshin * The third argument `off' is the offset from the IPv6 header to the option,
105253541Sshin * which is necessary if the IPv6 header the and option header and IPv6 header
105353541Sshin * is not continuous in order to return an ICMPv6 error.
105453541Sshin */
105553541Sshinint
105653541Sshinip6_unknown_opt(optp, m, off)
105753541Sshin	u_int8_t *optp;
105853541Sshin	struct mbuf *m;
105953541Sshin	int off;
106053541Sshin{
106153541Sshin	struct ip6_hdr *ip6;
106253541Sshin
106378064Sume	switch (IP6OPT_TYPE(*optp)) {
106478064Sume	case IP6OPT_TYPE_SKIP: /* ignore the option */
1065120856Sume		return ((int)*(optp + 1));
106678064Sume	case IP6OPT_TYPE_DISCARD:	/* silently discard */
106778064Sume		m_freem(m);
1068120856Sume		return (-1);
106978064Sume	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
107078064Sume		ip6stat.ip6s_badoptions++;
107178064Sume		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1072120856Sume		return (-1);
107378064Sume	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
107478064Sume		ip6stat.ip6s_badoptions++;
107578064Sume		ip6 = mtod(m, struct ip6_hdr *);
107678064Sume		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
107778064Sume		    (m->m_flags & (M_BCAST|M_MCAST)))
107878064Sume			m_freem(m);
107978064Sume		else
108078064Sume			icmp6_error(m, ICMP6_PARAM_PROB,
108178064Sume				    ICMP6_PARAMPROB_OPTION, off);
1082120856Sume		return (-1);
108353541Sshin	}
108453541Sshin
108553541Sshin	m_freem(m);		/* XXX: NOTREACHED */
1086120856Sume	return (-1);
108753541Sshin}
108853541Sshin
108953541Sshin/*
109062587Sitojun * Create the "control" list for this pcb.
109178064Sume * The function will not modify mbuf chain at all.
109262587Sitojun *
109378064Sume * with KAME mbuf chain restriction:
109462587Sitojun * The routine will be called from upper layer handlers like tcp6_input().
109562587Sitojun * Thus the routine assumes that the caller (tcp6_input) have already
109662587Sitojun * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
109762587Sitojun * very first mbuf on the mbuf chain.
109853541Sshin */
109953541Sshinvoid
1100121674Sumeip6_savecontrol(in6p, m, mp)
110178064Sume	struct inpcb *in6p;
1102121674Sume	struct mbuf *m, **mp;
110353541Sshin{
1104121472Sume#define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1105121674Sume	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
110653541Sshin
110778064Sume#ifdef SO_TIMESTAMP
110878064Sume	if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
110953541Sshin		struct timeval tv;
111053541Sshin
111153541Sshin		microtime(&tv);
111253541Sshin		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1113120913Sume		    SCM_TIMESTAMP, SOL_SOCKET);
1114121472Sume		if (*mp)
111553541Sshin			mp = &(*mp)->m_next;
111697658Stanimura	}
111762587Sitojun#endif
111862587Sitojun
1119121631Sume	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
1120121631Sume		return;
1121121631Sume
112253541Sshin	/* RFC 2292 sec. 5 */
112378064Sume	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
112453541Sshin		struct in6_pktinfo pi6;
1125120913Sume
112653541Sshin		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1127121315Sume		in6_clearscope(&pi6.ipi6_addr);	/* XXX */
1128120913Sume		pi6.ipi6_ifindex =
1129120913Sume		    (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1130120913Sume
113153541Sshin		*mp = sbcreatecontrol((caddr_t) &pi6,
1132120913Sume		    sizeof(struct in6_pktinfo),
1133121472Sume		    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1134121472Sume		if (*mp)
113553541Sshin			mp = &(*mp)->m_next;
113653541Sshin	}
113778064Sume
113878064Sume	if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
113953541Sshin		int hlim = ip6->ip6_hlim & 0xff;
1140120913Sume
1141120913Sume		*mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1142121472Sume		    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1143121472Sume		if (*mp)
114453541Sshin			mp = &(*mp)->m_next;
114553541Sshin	}
114653541Sshin
1147121472Sume	if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1148121472Sume		u_int32_t flowinfo;
1149121472Sume		int tclass;
1150121472Sume
1151121472Sume		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1152121472Sume		flowinfo >>= 20;
1153121472Sume
1154121472Sume		tclass = flowinfo & 0xff;
1155121472Sume		*mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass),
1156121472Sume		    IPV6_TCLASS, IPPROTO_IPV6);
1157121472Sume		if (*mp)
1158121472Sume			mp = &(*mp)->m_next;
1159121472Sume	}
1160121472Sume
116153541Sshin	/*
1162130002Sume	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
1163130002Sume	 * privilege for the option (see ip6_ctloutput), but it might be too
1164130002Sume	 * strict, since there might be some hop-by-hop options which can be
1165130002Sume	 * returned to normal user.
1166130002Sume	 * See also RFC 2292 section 6 (or RFC 3542 section 8).
116753541Sshin	 */
1168121472Sume	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
116953541Sshin		/*
117053541Sshin		 * Check if a hop-by-hop options header is contatined in the
117153541Sshin		 * received packet, and if so, store the options as ancillary
117253541Sshin		 * data. Note that a hop-by-hop options header must be
1173120913Sume		 * just after the IPv6 header, which is assured through the
1174120913Sume		 * IPv6 input processing.
117553541Sshin		 */
117653541Sshin		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
117762587Sitojun			struct ip6_hbh *hbh;
117878064Sume			int hbhlen = 0;
117978064Sume#ifdef PULLDOWN_TEST
118078064Sume			struct mbuf *ext;
118178064Sume#endif
118253541Sshin
118362587Sitojun#ifndef PULLDOWN_TEST
118462587Sitojun			hbh = (struct ip6_hbh *)(ip6 + 1);
118562587Sitojun			hbhlen = (hbh->ip6h_len + 1) << 3;
118662587Sitojun#else
118778064Sume			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
118878064Sume			    ip6->ip6_nxt);
118978064Sume			if (ext == NULL) {
119062587Sitojun				ip6stat.ip6s_tooshort++;
119162587Sitojun				return;
119262587Sitojun			}
119378064Sume			hbh = mtod(ext, struct ip6_hbh *);
119462587Sitojun			hbhlen = (hbh->ip6h_len + 1) << 3;
119578064Sume			if (hbhlen != ext->m_len) {
119678064Sume				m_freem(ext);
119762587Sitojun				ip6stat.ip6s_tooshort++;
119862587Sitojun				return;
119962587Sitojun			}
120062587Sitojun#endif
120162587Sitojun
120253541Sshin			/*
1203120913Sume			 * XXX: We copy the whole header even if a
1204120913Sume			 * jumbo payload option is included, the option which
1205120913Sume			 * is to be removed before returning according to
1206120913Sume			 * RFC2292.
120778064Sume			 * Note: this constraint is removed in 2292bis.
120853541Sshin			 */
120962587Sitojun			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1210121472Sume			    IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1211121472Sume			    IPPROTO_IPV6);
1212121472Sume			if (*mp)
121353541Sshin				mp = &(*mp)->m_next;
121478064Sume#ifdef PULLDOWN_TEST
121578064Sume			m_freem(ext);
121678064Sume#endif
121753541Sshin		}
121853541Sshin	}
121953541Sshin
1220121472Sume	if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
122178064Sume		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
122253541Sshin
122353541Sshin		/*
122453541Sshin		 * Search for destination options headers or routing
122553541Sshin		 * header(s) through the header chain, and stores each
122653541Sshin		 * header as ancillary data.
122753541Sshin		 * Note that the order of the headers remains in
122853541Sshin		 * the chain of ancillary data.
122953541Sshin		 */
123078064Sume		while (1) {	/* is explicit loop prevention necessary? */
123178064Sume			struct ip6_ext *ip6e = NULL;
123262587Sitojun			int elen;
123378064Sume#ifdef PULLDOWN_TEST
123478064Sume			struct mbuf *ext = NULL;
123578064Sume#endif
123653541Sshin
123778064Sume			/*
123878064Sume			 * if it is not an extension header, don't try to
123978064Sume			 * pull it from the chain.
124078064Sume			 */
124178064Sume			switch (nxt) {
124278064Sume			case IPPROTO_DSTOPTS:
124378064Sume			case IPPROTO_ROUTING:
124478064Sume			case IPPROTO_HOPOPTS:
124578064Sume			case IPPROTO_AH: /* is it possible? */
124678064Sume				break;
124778064Sume			default:
124878064Sume				goto loopend;
124978064Sume			}
125078064Sume
125162587Sitojun#ifndef PULLDOWN_TEST
125278064Sume			if (off + sizeof(*ip6e) > m->m_len)
125378064Sume				goto loopend;
125462587Sitojun			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
125562587Sitojun			if (nxt == IPPROTO_AH)
125662587Sitojun				elen = (ip6e->ip6e_len + 2) << 2;
125762587Sitojun			else
125862587Sitojun				elen = (ip6e->ip6e_len + 1) << 3;
125978064Sume			if (off + elen > m->m_len)
126078064Sume				goto loopend;
126162587Sitojun#else
126278064Sume			ext = ip6_pullexthdr(m, off, nxt);
126378064Sume			if (ext == NULL) {
126462587Sitojun				ip6stat.ip6s_tooshort++;
126562587Sitojun				return;
126662587Sitojun			}
126778064Sume			ip6e = mtod(ext, struct ip6_ext *);
126862587Sitojun			if (nxt == IPPROTO_AH)
126962587Sitojun				elen = (ip6e->ip6e_len + 2) << 2;
127062587Sitojun			else
127162587Sitojun				elen = (ip6e->ip6e_len + 1) << 3;
127278064Sume			if (elen != ext->m_len) {
127378064Sume				m_freem(ext);
127462587Sitojun				ip6stat.ip6s_tooshort++;
127562587Sitojun				return;
127662587Sitojun			}
127762587Sitojun#endif
127862587Sitojun
127978064Sume			switch (nxt) {
128078064Sume			case IPPROTO_DSTOPTS:
1281120913Sume				if (!(in6p->in6p_flags & IN6P_DSTOPTS))
128278064Sume					break;
128353541Sshin
128478064Sume				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1285121472Sume				    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1286120913Sume				    IPPROTO_IPV6);
128778064Sume				if (*mp)
128878064Sume					mp = &(*mp)->m_next;
128978064Sume				break;
129078064Sume			case IPPROTO_ROUTING:
129178064Sume				if (!in6p->in6p_flags & IN6P_RTHDR)
129278064Sume					break;
129353541Sshin
129478064Sume				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1295121472Sume				    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1296120913Sume				    IPPROTO_IPV6);
129778064Sume				if (*mp)
129878064Sume					mp = &(*mp)->m_next;
129978064Sume				break;
130078064Sume			case IPPROTO_HOPOPTS:
130178064Sume			case IPPROTO_AH: /* is it possible? */
130278064Sume				break;
130353541Sshin
130478064Sume			default:
130578064Sume				/*
130678064Sume			 	 * other cases have been filtered in the above.
130778064Sume				 * none will visit this case.  here we supply
130878064Sume				 * the code just in case (nxt overwritten or
130978064Sume				 * other cases).
131078064Sume				 */
131178064Sume#ifdef PULLDOWN_TEST
131278064Sume				m_freem(ext);
131378064Sume#endif
131478064Sume				goto loopend;
131553541Sshin
131653541Sshin			}
131753541Sshin
131853541Sshin			/* proceed with the next header. */
131962587Sitojun			off += elen;
132053541Sshin			nxt = ip6e->ip6e_nxt;
132178064Sume			ip6e = NULL;
132278064Sume#ifdef PULLDOWN_TEST
132378064Sume			m_freem(ext);
132478064Sume			ext = NULL;
132578064Sume#endif
132653541Sshin		}
132753541Sshin	  loopend:
132878064Sume		;
132953541Sshin	}
133078064Sume
1331121472Sume#undef IS2292
133278064Sume}
133378064Sume
1334125776Sumevoid
1335125776Sumeip6_notify_pmtu(in6p, dst, mtu)
1336125776Sume	struct inpcb *in6p;
1337125776Sume	struct sockaddr_in6 *dst;
1338125776Sume	u_int32_t *mtu;
1339125776Sume{
1340125776Sume	struct socket *so;
1341125776Sume	struct mbuf *m_mtu;
1342125776Sume	struct ip6_mtuinfo mtuctl;
1343125776Sume
1344125776Sume	so =  in6p->inp_socket;
1345125776Sume
1346125776Sume	if (mtu == NULL)
1347125776Sume		return;
1348125776Sume
1349125776Sume#ifdef DIAGNOSTIC
1350125776Sume	if (so == NULL)		/* I believe this is impossible */
1351125776Sume		panic("ip6_notify_pmtu: socket is NULL");
1352125776Sume#endif
1353125776Sume
1354125776Sume	bzero(&mtuctl, sizeof(mtuctl));	/* zero-clear for safety */
1355125776Sume	mtuctl.ip6m_mtu = *mtu;
1356125776Sume	mtuctl.ip6m_addr = *dst;
1357125776Sume	in6_recoverscope(&mtuctl.ip6m_addr, &mtuctl.ip6m_addr.sin6_addr, NULL);
1358125776Sume
1359125776Sume	if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1360125776Sume	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1361125776Sume		return;
1362125776Sume
1363125776Sume	if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1364125776Sume	    == 0) {
1365125776Sume		m_freem(m_mtu);
1366125776Sume		/* XXX: should count statistics */
1367125776Sume	} else
1368125776Sume		sorwakeup(so);
1369125776Sume
1370125776Sume	return;
1371125776Sume}
1372125776Sume
137378064Sume#ifdef PULLDOWN_TEST
137478064Sume/*
137578064Sume * pull single extension header from mbuf chain.  returns single mbuf that
137678064Sume * contains the result, or NULL on error.
137778064Sume */
137878064Sumestatic struct mbuf *
137978064Sumeip6_pullexthdr(m, off, nxt)
138078064Sume	struct mbuf *m;
138178064Sume	size_t off;
138278064Sume	int nxt;
138378064Sume{
138478064Sume	struct ip6_ext ip6e;
138578064Sume	size_t elen;
138678064Sume	struct mbuf *n;
138778064Sume
138878064Sume#ifdef DIAGNOSTIC
138978064Sume	switch (nxt) {
139078064Sume	case IPPROTO_DSTOPTS:
139178064Sume	case IPPROTO_ROUTING:
139278064Sume	case IPPROTO_HOPOPTS:
139378064Sume	case IPPROTO_AH: /* is it possible? */
139478064Sume		break;
139578064Sume	default:
139678064Sume		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
139753541Sshin	}
139878064Sume#endif
139978064Sume
140078064Sume	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
140178064Sume	if (nxt == IPPROTO_AH)
140278064Sume		elen = (ip6e.ip6e_len + 2) << 2;
140378064Sume	else
140478064Sume		elen = (ip6e.ip6e_len + 1) << 3;
140578064Sume
1406111119Simp	MGET(n, M_DONTWAIT, MT_DATA);
140778064Sume	if (n && elen >= MLEN) {
1408111119Simp		MCLGET(n, M_DONTWAIT);
140978064Sume		if ((n->m_flags & M_EXT) == 0) {
141078064Sume			m_free(n);
141178064Sume			n = NULL;
141278064Sume		}
141353541Sshin	}
141478064Sume	if (!n)
141578064Sume		return NULL;
141662587Sitojun
141778064Sume	n->m_len = 0;
141878064Sume	if (elen >= M_TRAILINGSPACE(n)) {
141978064Sume		m_free(n);
142078064Sume		return NULL;
142178064Sume	}
142278064Sume
142378064Sume	m_copydata(m, off, elen, mtod(n, caddr_t));
142478064Sume	n->m_len = elen;
142578064Sume	return n;
142653541Sshin}
142778064Sume#endif
142853541Sshin
142953541Sshin/*
143053541Sshin * Get pointer to the previous header followed by the header
143153541Sshin * currently processed.
143253541Sshin * XXX: This function supposes that
143353541Sshin *	M includes all headers,
143453541Sshin *	the next header field and the header length field of each header
143553541Sshin *	are valid, and
143653541Sshin *	the sum of each header length equals to OFF.
143753541Sshin * Because of these assumptions, this function must be called very
143853541Sshin * carefully. Moreover, it will not be used in the near future when
143953541Sshin * we develop `neater' mechanism to process extension headers.
144053541Sshin */
144153541Sshinchar *
144253541Sshinip6_get_prevhdr(m, off)
144353541Sshin	struct mbuf *m;
144453541Sshin	int off;
144553541Sshin{
144653541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
144753541Sshin
144853541Sshin	if (off == sizeof(struct ip6_hdr))
1449120856Sume		return (&ip6->ip6_nxt);
145053541Sshin	else {
145153541Sshin		int len, nxt;
145253541Sshin		struct ip6_ext *ip6e = NULL;
145353541Sshin
145453541Sshin		nxt = ip6->ip6_nxt;
145553541Sshin		len = sizeof(struct ip6_hdr);
145653541Sshin		while (len < off) {
145753541Sshin			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
145853541Sshin
145978064Sume			switch (nxt) {
146053541Sshin			case IPPROTO_FRAGMENT:
146153541Sshin				len += sizeof(struct ip6_frag);
146253541Sshin				break;
146353541Sshin			case IPPROTO_AH:
146453541Sshin				len += (ip6e->ip6e_len + 2) << 2;
146553541Sshin				break;
146653541Sshin			default:
146753541Sshin				len += (ip6e->ip6e_len + 1) << 3;
146853541Sshin				break;
146953541Sshin			}
147053541Sshin			nxt = ip6e->ip6e_nxt;
147153541Sshin		}
147253541Sshin		if (ip6e)
1473120856Sume			return (&ip6e->ip6e_nxt);
147453541Sshin		else
147553541Sshin			return NULL;
147653541Sshin	}
147753541Sshin}
147853541Sshin
147953541Sshin/*
148062587Sitojun * get next header offset.  m will be retained.
148162587Sitojun */
148262587Sitojunint
148362587Sitojunip6_nexthdr(m, off, proto, nxtp)
148462587Sitojun	struct mbuf *m;
148562587Sitojun	int off;
148662587Sitojun	int proto;
148762587Sitojun	int *nxtp;
148862587Sitojun{
148962587Sitojun	struct ip6_hdr ip6;
149062587Sitojun	struct ip6_ext ip6e;
149162587Sitojun	struct ip6_frag fh;
149262587Sitojun
149362587Sitojun	/* just in case */
149462587Sitojun	if (m == NULL)
149562587Sitojun		panic("ip6_nexthdr: m == NULL");
149662587Sitojun	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
149762587Sitojun		return -1;
149862587Sitojun
149962587Sitojun	switch (proto) {
150062587Sitojun	case IPPROTO_IPV6:
150162587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6))
150262587Sitojun			return -1;
150362587Sitojun		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
150462587Sitojun		if (nxtp)
150562587Sitojun			*nxtp = ip6.ip6_nxt;
150662587Sitojun		off += sizeof(ip6);
150762587Sitojun		return off;
150862587Sitojun
150962587Sitojun	case IPPROTO_FRAGMENT:
151062587Sitojun		/*
151162587Sitojun		 * terminate parsing if it is not the first fragment,
151262587Sitojun		 * it does not make sense to parse through it.
151362587Sitojun		 */
151462587Sitojun		if (m->m_pkthdr.len < off + sizeof(fh))
151562587Sitojun			return -1;
151662587Sitojun		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1517120978Sume		/* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1518120978Sume		if (fh.ip6f_offlg & IP6F_OFF_MASK)
151962587Sitojun			return -1;
152062587Sitojun		if (nxtp)
152162587Sitojun			*nxtp = fh.ip6f_nxt;
152262587Sitojun		off += sizeof(struct ip6_frag);
152362587Sitojun		return off;
152462587Sitojun
152562587Sitojun	case IPPROTO_AH:
152662587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6e))
152762587Sitojun			return -1;
152862587Sitojun		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
152962587Sitojun		if (nxtp)
153062587Sitojun			*nxtp = ip6e.ip6e_nxt;
153162587Sitojun		off += (ip6e.ip6e_len + 2) << 2;
153262587Sitojun		return off;
153362587Sitojun
153462587Sitojun	case IPPROTO_HOPOPTS:
153562587Sitojun	case IPPROTO_ROUTING:
153662587Sitojun	case IPPROTO_DSTOPTS:
153762587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6e))
153862587Sitojun			return -1;
153962587Sitojun		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
154062587Sitojun		if (nxtp)
154162587Sitojun			*nxtp = ip6e.ip6e_nxt;
154262587Sitojun		off += (ip6e.ip6e_len + 1) << 3;
154362587Sitojun		return off;
154462587Sitojun
154562587Sitojun	case IPPROTO_NONE:
154662587Sitojun	case IPPROTO_ESP:
154762587Sitojun	case IPPROTO_IPCOMP:
154862587Sitojun		/* give up */
154962587Sitojun		return -1;
155062587Sitojun
155162587Sitojun	default:
155262587Sitojun		return -1;
155362587Sitojun	}
155462587Sitojun
155562587Sitojun	return -1;
155662587Sitojun}
155762587Sitojun
155862587Sitojun/*
155962587Sitojun * get offset for the last header in the chain.  m will be kept untainted.
156062587Sitojun */
156162587Sitojunint
156262587Sitojunip6_lasthdr(m, off, proto, nxtp)
156362587Sitojun	struct mbuf *m;
156462587Sitojun	int off;
156562587Sitojun	int proto;
156662587Sitojun	int *nxtp;
156762587Sitojun{
156862587Sitojun	int newoff;
156962587Sitojun	int nxt;
157062587Sitojun
157162587Sitojun	if (!nxtp) {
157262587Sitojun		nxt = -1;
157362587Sitojun		nxtp = &nxt;
157462587Sitojun	}
157562587Sitojun	while (1) {
157662587Sitojun		newoff = ip6_nexthdr(m, off, proto, nxtp);
157762587Sitojun		if (newoff < 0)
157862587Sitojun			return off;
157962587Sitojun		else if (newoff < off)
158062587Sitojun			return -1;	/* invalid */
158162587Sitojun		else if (newoff == off)
158262587Sitojun			return newoff;
158362587Sitojun
158462587Sitojun		off = newoff;
158562587Sitojun		proto = *nxtp;
158662587Sitojun	}
158762587Sitojun}
158862587Sitojun
1589121673Sumestruct ip6aux *
159078064Sumeip6_addaux(m)
159178064Sume	struct mbuf *m;
159278064Sume{
1593120913Sume	struct m_tag *mtag;
1594120913Sume
1595120913Sume	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1596120913Sume	if (!mtag) {
1597120913Sume		mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux),
1598120913Sume		    M_NOWAIT);
1599121630Sume		if (mtag) {
1600120913Sume			m_tag_prepend(m, mtag);
1601121630Sume			bzero(mtag + 1, sizeof(struct ip6aux));
1602121630Sume		}
160378064Sume	}
1604121673Sume	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
160578064Sume}
160678064Sume
1607121673Sumestruct ip6aux *
160878064Sumeip6_findaux(m)
160978064Sume	struct mbuf *m;
161078064Sume{
1611120913Sume	struct m_tag *mtag;
1612120913Sume
1613120913Sume	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1614121673Sume	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
161578064Sume}
161678064Sume
161778064Sumevoid
161878064Sumeip6_delaux(m)
161978064Sume	struct mbuf *m;
162078064Sume{
1621120913Sume	struct m_tag *mtag;
1622120913Sume
1623120913Sume	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1624120913Sume	if (mtag)
1625120913Sume		m_tag_delete(m, mtag);
162678064Sume}
162778064Sume
162862587Sitojun/*
162953541Sshin * System control for IP6
163053541Sshin */
163153541Sshin
163253541Sshinu_char	inet6ctlerrmap[PRC_NCMDS] = {
163353541Sshin	0,		0,		0,		0,
163453541Sshin	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
163553541Sshin	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
163653541Sshin	EMSGSIZE,	EHOSTUNREACH,	0,		0,
163753541Sshin	0,		0,		0,		0,
163853541Sshin	ENOPROTOOPT
163953541Sshin};
1640