ip6_input.c revision 126444
162587Sitojun/*	$FreeBSD: head/sys/netinet6/ip6_input.c 126444 2004-03-01 15:34:29Z ume $	*/
295023Ssuz/*	$KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 jinmei Exp $	*/
362587Sitojun
453541Sshin/*
553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
653541Sshin * All rights reserved.
753541Sshin *
853541Sshin * Redistribution and use in source and binary forms, with or without
953541Sshin * modification, are permitted provided that the following conditions
1053541Sshin * are met:
1153541Sshin * 1. Redistributions of source code must retain the above copyright
1253541Sshin *    notice, this list of conditions and the following disclaimer.
1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1453541Sshin *    notice, this list of conditions and the following disclaimer in the
1553541Sshin *    documentation and/or other materials provided with the distribution.
1653541Sshin * 3. Neither the name of the project nor the names of its contributors
1753541Sshin *    may be used to endorse or promote products derived from this software
1853541Sshin *    without specific prior written permission.
1953541Sshin *
2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2353541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3053541Sshin * SUCH DAMAGE.
3153541Sshin */
3253541Sshin
3353541Sshin/*
3453541Sshin * Copyright (c) 1982, 1986, 1988, 1993
3553541Sshin *	The Regents of the University of California.  All rights reserved.
3653541Sshin *
3753541Sshin * Redistribution and use in source and binary forms, with or without
3853541Sshin * modification, are permitted provided that the following conditions
3953541Sshin * are met:
4053541Sshin * 1. Redistributions of source code must retain the above copyright
4153541Sshin *    notice, this list of conditions and the following disclaimer.
4253541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4353541Sshin *    notice, this list of conditions and the following disclaimer in the
4453541Sshin *    documentation and/or other materials provided with the distribution.
4553541Sshin * 3. All advertising materials mentioning features or use of this software
4653541Sshin *    must display the following acknowledgement:
4753541Sshin *	This product includes software developed by the University of
4853541Sshin *	California, Berkeley and its contributors.
4953541Sshin * 4. Neither the name of the University nor the names of its contributors
5053541Sshin *    may be used to endorse or promote products derived from this software
5153541Sshin *    without specific prior written permission.
5253541Sshin *
5353541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5453541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5553541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5653541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5753541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5853541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5953541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6053541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6153541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6253541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6353541Sshin * SUCH DAMAGE.
6453541Sshin *
6553541Sshin *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
6653541Sshin */
6753541Sshin
6862587Sitojun#include "opt_ip6fw.h"
6962587Sitojun#include "opt_inet.h"
7062587Sitojun#include "opt_inet6.h"
7155009Sshin#include "opt_ipsec.h"
7264060Sdarrenr#include "opt_pfil_hooks.h"
73120649Sume#include "opt_random_ip_id.h"
7455009Sshin
7553541Sshin#include <sys/param.h>
7653541Sshin#include <sys/systm.h>
7778064Sume#include <sys/malloc.h>
7853541Sshin#include <sys/mbuf.h>
7983366Sjulian#include <sys/proc.h>
8053541Sshin#include <sys/domain.h>
8153541Sshin#include <sys/protosw.h>
8253541Sshin#include <sys/socket.h>
8353541Sshin#include <sys/socketvar.h>
8453541Sshin#include <sys/errno.h>
8553541Sshin#include <sys/time.h>
8653541Sshin#include <sys/kernel.h>
8753541Sshin#include <sys/syslog.h>
8853541Sshin
8953541Sshin#include <net/if.h>
9053541Sshin#include <net/if_types.h>
9153541Sshin#include <net/if_dl.h>
9253541Sshin#include <net/route.h>
9353541Sshin#include <net/netisr.h>
9464060Sdarrenr#ifdef PFIL_HOOKS
9564060Sdarrenr#include <net/pfil.h>
9664060Sdarrenr#endif
9753541Sshin
9853541Sshin#include <netinet/in.h>
9953541Sshin#include <netinet/in_systm.h>
10062587Sitojun#ifdef INET
10153541Sshin#include <netinet/ip.h>
10253541Sshin#include <netinet/ip_icmp.h>
10395023Ssuz#endif /* INET */
10462587Sitojun#include <netinet/ip6.h>
10553541Sshin#include <netinet6/in6_var.h>
10653541Sshin#include <netinet6/ip6_var.h>
10762587Sitojun#include <netinet/in_pcb.h>
10862587Sitojun#include <netinet/icmp6.h>
109121161Sume#include <netinet6/scope6_var.h>
11053541Sshin#include <netinet6/in6_ifattach.h>
11153541Sshin#include <netinet6/nd6.h>
11253541Sshin#include <netinet6/in6_prefix.h>
11353541Sshin
11478064Sume#ifdef IPSEC
11578064Sume#include <netinet6/ipsec.h>
11678064Sume#ifdef INET6
11778064Sume#include <netinet6/ipsec6.h>
11878064Sume#endif
11978064Sume#endif
12078064Sume
121105199Ssam#ifdef FAST_IPSEC
122105199Ssam#include <netipsec/ipsec.h>
123105199Ssam#include <netipsec/ipsec6.h>
124105199Ssam#define	IPSEC
125105199Ssam#endif /* FAST_IPSEC */
126105199Ssam
12753541Sshin#include <netinet6/ip6_fw.h>
12853541Sshin
12953541Sshin#include <netinet6/ip6protosw.h>
13053541Sshin
13153541Sshin#include <net/net_osdep.h>
13253541Sshin
13362587Sitojunextern struct domain inet6domain;
13453541Sshin
13562587Sitojunu_char ip6_protox[IPPROTO_MAX];
136111888Sjlemonstatic struct ifqueue ip6intrq;
13762587Sitojunstatic int ip6qmaxlen = IFQ_MAXLEN;
13862587Sitojunstruct in6_ifaddr *in6_ifaddr;
13953541Sshin
14078064Sumeextern struct callout in6_tmpaddrtimer_ch;
14178064Sume
14262587Sitojunint ip6_forward_srcrt;			/* XXX */
14362587Sitojunint ip6_sourcecheck;			/* XXX */
14462587Sitojunint ip6_sourcecheck_interval;		/* XXX */
14553541Sshin
14678064Sumeint ip6_ours_check_algorithm;
14778064Sume
148120386Ssam#ifdef PFIL_HOOKS
149120386Ssamstruct pfil_head inet6_pfil_hook;
150120386Ssam#endif
15178064Sume
15253541Sshin/* firewall hooks */
15362587Sitojunip6_fw_chk_t *ip6_fw_chk_ptr;
15462587Sitojunip6_fw_ctl_t *ip6_fw_ctl_ptr;
15566303Sumeint ip6_fw_enable = 1;
15653541Sshin
15762587Sitojunstruct ip6stat ip6stat;
15853541Sshin
15962587Sitojunstatic void ip6_init2 __P((void *));
160121673Sumestatic struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *));
16162587Sitojunstatic int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
16262587Sitojun#ifdef PULLDOWN_TEST
16362587Sitojunstatic struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int));
16453541Sshin#endif
16553541Sshin
16653541Sshin/*
16753541Sshin * IP6 initialization: fill in IP6 protocol switch table.
16853541Sshin * All protocols not implemented in kernel go to raw IP6 protocol handler.
16953541Sshin */
17053541Sshinvoid
17153541Sshinip6_init()
17253541Sshin{
17378064Sume	struct ip6protosw *pr;
17478064Sume	int i;
17553541Sshin
17678064Sume#ifdef DIAGNOSTIC
17778064Sume	if (sizeof(struct protosw) != sizeof(struct ip6protosw))
17878064Sume		panic("sizeof(protosw) != sizeof(ip6protosw)");
17978064Sume#endif
18053541Sshin	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
18153541Sshin	if (pr == 0)
18253541Sshin		panic("ip6_init");
18353541Sshin	for (i = 0; i < IPPROTO_MAX; i++)
18453541Sshin		ip6_protox[i] = pr - inet6sw;
18553541Sshin	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
18653541Sshin	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
18753541Sshin		if (pr->pr_domain->dom_family == PF_INET6 &&
18853541Sshin		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
18953541Sshin			ip6_protox[pr->pr_protocol] = pr - inet6sw;
190120386Ssam#ifdef PFIL_HOOKS
191120386Ssam	inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
192120386Ssam	inet6_pfil_hook.ph_af = AF_INET6;
193120386Ssam	if ((i = pfil_head_register(&inet6_pfil_hook)) != 0)
194120386Ssam		printf("%s: WARNING: unable to register pfil hook, "
195120386Ssam			"error %d\n", __func__, i);
196120386Ssam#endif /* PFIL_HOOKS */
19753541Sshin	ip6intrq.ifq_maxlen = ip6qmaxlen;
19893818Sjhb	mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF);
199122320Ssam	netisr_register(NETISR_IPV6, ip6_input, &ip6intrq, 0);
200121161Sume	scope6_init();
201121742Sume	addrsel_policy_init();
20253541Sshin	nd6_init();
20353541Sshin	frag6_init();
204120649Sume#ifndef RANDOM_IP_ID
205120648Sume	ip6_flow_seq = arc4random();
206120649Sume#endif
207120648Sume	ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
20853541Sshin}
20953541Sshin
21053541Sshinstatic void
21153541Sshinip6_init2(dummy)
21253541Sshin	void *dummy;
21353541Sshin{
21453541Sshin
21553541Sshin	/* nd6_timer_init */
21678064Sume	callout_init(&nd6_timer_ch, 0);
21778064Sume	callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
21878064Sume
21953541Sshin	/* router renumbering prefix list maintenance */
22078064Sume	callout_init(&in6_rr_timer_ch, 0);
22178064Sume	callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL);
22278064Sume
22378064Sume	/* timer for regeneranation of temporary addresses randomize ID */
224121806Sume	callout_init(&in6_tmpaddrtimer_ch, 0);
22578064Sume	callout_reset(&in6_tmpaddrtimer_ch,
22678064Sume		      (ip6_temp_preferred_lifetime - ip6_desync_factor -
22778064Sume		       ip6_temp_regen_advance) * hz,
22878064Sume		      in6_tmpaddrtimer, NULL);
22953541Sshin}
23053541Sshin
23153541Sshin/* cheat */
23255009Sshin/* This must be after route_init(), which is now SI_ORDER_THIRD */
23355009SshinSYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
23453541Sshin
23553541Sshinextern struct	route_in6 ip6_forward_rt;
23653541Sshin
23753541Sshinvoid
23853541Sshinip6_input(m)
23953541Sshin	struct mbuf *m;
24053541Sshin{
24153541Sshin	struct ip6_hdr *ip6;
24253541Sshin	int off = sizeof(struct ip6_hdr), nest;
24353541Sshin	u_int32_t plen;
24453541Sshin	u_int32_t rtalert = ~0;
24553541Sshin	int nxt, ours = 0;
24653541Sshin	struct ifnet *deliverifp = NULL;
247126444Sume	struct sockaddr_in6 sa6;
248126444Sume	u_int32_t srczone, dstzone;
249121143Ssam#ifdef PFIL_HOOKS
250121143Ssam	struct in6_addr odst;
251121143Ssam#endif
252121143Ssam	int srcrt = 0;
253121144Ssam
254122320Ssam	GIANT_REQUIRED;			/* XXX for now */
25553541Sshin#ifdef IPSEC
25653541Sshin	/*
25753541Sshin	 * should the inner packet be considered authentic?
25853541Sshin	 * see comment in ah4_input().
25953541Sshin	 */
26053541Sshin	if (m) {
26153541Sshin		m->m_flags &= ~M_AUTHIPHDR;
26253541Sshin		m->m_flags &= ~M_AUTHIPDGM;
26353541Sshin	}
26453541Sshin#endif
26553541Sshin
26653541Sshin	/*
267121630Sume	 * make sure we don't have onion peering information into m_tag.
26878064Sume	 */
26978064Sume	ip6_delaux(m);
27078064Sume
27178064Sume	/*
27295023Ssuz	 * mbuf statistics
27353541Sshin	 */
27453541Sshin	if (m->m_flags & M_EXT) {
27553541Sshin		if (m->m_next)
27653541Sshin			ip6stat.ip6s_mext2m++;
27753541Sshin		else
27853541Sshin			ip6stat.ip6s_mext1++;
27953541Sshin	} else {
28078064Sume#define M2MMAX	(sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
28153541Sshin		if (m->m_next) {
28253541Sshin			if (m->m_flags & M_LOOP) {
283120913Sume				ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
28478064Sume			} else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
28553541Sshin				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
28653541Sshin			else
28753541Sshin				ip6stat.ip6s_m2m[0]++;
28853541Sshin		} else
28953541Sshin			ip6stat.ip6s_m1++;
29078064Sume#undef M2MMAX
29153541Sshin	}
29253541Sshin
29353541Sshin	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
29453541Sshin	ip6stat.ip6s_total++;
29553541Sshin
29662587Sitojun#ifndef PULLDOWN_TEST
29774336Skuriyama	/*
29874336Skuriyama	 * L2 bridge code and some other code can return mbuf chain
29974336Skuriyama	 * that does not conform to KAME requirement.  too bad.
30074336Skuriyama	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
30174336Skuriyama	 */
30274336Skuriyama	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
30374336Skuriyama		struct mbuf *n;
30474336Skuriyama
305111119Simp		MGETHDR(n, M_DONTWAIT, MT_HEADER);
30677003Sume		if (n)
307108466Ssam			M_MOVE_PKTHDR(n, m);
308108825Ssam		if (n && n->m_pkthdr.len > MHLEN) {
309111119Simp			MCLGET(n, M_DONTWAIT);
31074336Skuriyama			if ((n->m_flags & M_EXT) == 0) {
31174336Skuriyama				m_freem(n);
31274336Skuriyama				n = NULL;
31374336Skuriyama			}
31474336Skuriyama		}
31595023Ssuz		if (n == NULL) {
31677003Sume			m_freem(m);
317120913Sume			return;	/* ENOBUFS */
31877003Sume		}
31974336Skuriyama
320108825Ssam		m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
321108825Ssam		n->m_len = n->m_pkthdr.len;
32274336Skuriyama		m_freem(m);
32374336Skuriyama		m = n;
32474336Skuriyama	}
325120913Sume	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
32662587Sitojun#endif
32753541Sshin
32853541Sshin	if (m->m_len < sizeof(struct ip6_hdr)) {
32953541Sshin		struct ifnet *inifp;
33053541Sshin		inifp = m->m_pkthdr.rcvif;
331120913Sume		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
33253541Sshin			ip6stat.ip6s_toosmall++;
33353541Sshin			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
33453541Sshin			return;
33553541Sshin		}
33653541Sshin	}
33753541Sshin
33853541Sshin	ip6 = mtod(m, struct ip6_hdr *);
33953541Sshin
34053541Sshin	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
34153541Sshin		ip6stat.ip6s_badvers++;
34253541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
34353541Sshin		goto bad;
34453541Sshin	}
34553541Sshin
34664060Sdarrenr#ifdef PFIL_HOOKS
34764060Sdarrenr	/*
348120386Ssam	 * Run through list of hooks for input packets.
349121144Ssam	 *
350121144Ssam	 * NB: Beware of the destination address changing
351121144Ssam	 *     (e.g. by NAT rewriting).  When this happens,
352121144Ssam	 *     tell ip6_forward to do the right thing.
35364060Sdarrenr	 */
354121144Ssam	odst = ip6->ip6_dst;
355120386Ssam	if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN))
356120386Ssam		return;
357120386Ssam	if (m == NULL)			/* consumed by filter */
358120386Ssam		return;
359120386Ssam	ip6 = mtod(m, struct ip6_hdr *);
360121144Ssam	srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
36164060Sdarrenr#endif /* PFIL_HOOKS */
36264060Sdarrenr
36353541Sshin	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
36453541Sshin
36553541Sshin	/*
36653541Sshin	 * Check with the firewall...
36753541Sshin	 */
36866303Sume	if (ip6_fw_enable && ip6_fw_chk_ptr) {
36953541Sshin		u_short port = 0;
37053541Sshin		/* If ipfw says divert, we have to just drop packet */
37153541Sshin		/* use port as a dummy argument */
37253541Sshin		if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
37353541Sshin			m_freem(m);
37453541Sshin			m = NULL;
37553541Sshin		}
37653541Sshin		if (!m)
37753541Sshin			return;
37853541Sshin	}
37953541Sshin
38053541Sshin	/*
38178064Sume	 * Check against address spoofing/corruption.
38253541Sshin	 */
38353541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
38453541Sshin	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
38578064Sume		/*
38678064Sume		 * XXX: "badscope" is not very suitable for a multicast source.
38778064Sume		 */
38853541Sshin		ip6stat.ip6s_badscope++;
38953541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
39053541Sshin		goto bad;
39153541Sshin	}
392126444Sume	if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
393126444Sume	    !(m->m_flags & M_LOOP)) {
394126444Sume		/*
395126444Sume		 * In this case, the packet should come from the loopback
396126444Sume		 * interface.  However, we cannot just check the if_flags,
397126444Sume		 * because ip6_mloopback() passes the "actual" interface
398126444Sume		 * as the outgoing/incoming interface.
399126444Sume		 */
40078064Sume		ip6stat.ip6s_badscope++;
40178064Sume		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
40278064Sume		goto bad;
40378064Sume	}
40495023Ssuz
40562587Sitojun	/*
40678064Sume	 * The following check is not documented in specs.  A malicious
40778064Sume	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
40878064Sume	 * and bypass security checks (act as if it was from 127.0.0.1 by using
409120913Sume	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
41078064Sume	 *
41178064Sume	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
41278064Sume	 * support IPv4-less kernel compilation, we cannot support SIIT
41378064Sume	 * environment at all.  So, it makes more sense for us to reject any
41478064Sume	 * malicious packets for non-SIIT environment, than try to do a
415120913Sume	 * partial support for SIIT environment.
41662587Sitojun	 */
41778064Sume	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
41878064Sume	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
41978064Sume		ip6stat.ip6s_badscope++;
42078064Sume		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
42178064Sume		goto bad;
42278064Sume	}
42362587Sitojun#if 0
42462587Sitojun	/*
42562587Sitojun	 * Reject packets with IPv4 compatible addresses (auto tunnel).
42662587Sitojun	 *
42762587Sitojun	 * The code forbids auto tunnel relay case in RFC1933 (the check is
42862587Sitojun	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
42962587Sitojun	 * is revised to forbid relaying case.
43062587Sitojun	 */
43162587Sitojun	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
43262587Sitojun	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
43362587Sitojun		ip6stat.ip6s_badscope++;
43462587Sitojun		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
43562587Sitojun		goto bad;
43662587Sitojun	}
43762587Sitojun#endif
43875246Sume
439126444Sume	/*
440126444Sume	 * Drop packets if the link ID portion is already filled.
441126444Sume	 * XXX: this is technically not a good behavior.  But, we internally
442126444Sume	 * use the field to disambiguate link-local addresses, so we cannot
443126444Sume	 * be generous against those a bit strange addresses.
444126444Sume	 */
44578064Sume	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
44678064Sume		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
44778064Sume		    ip6->ip6_src.s6_addr16[1]) {
44853541Sshin			ip6stat.ip6s_badscope++;
44953541Sshin			goto bad;
45053541Sshin		}
451121315Sume		if ((IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) ||
452121315Sume		     IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) &&
45378064Sume		    ip6->ip6_dst.s6_addr16[1]) {
45478064Sume			ip6stat.ip6s_badscope++;
45578064Sume			goto bad;
45678064Sume		}
45753541Sshin	}
45853541Sshin
459126444Sume	/*
460126444Sume	 * construct source and destination address structures with
461126444Sume	 * disambiguating their scope zones (if there is ambiguity).
462126444Sume	 * XXX: sin6_family and sin6_len will NOT be referred to, but we fill
463126444Sume	 * in these fields just in case.
464126444Sume	 */
465126444Sume	if (in6_addr2zoneid(m->m_pkthdr.rcvif, &ip6->ip6_src, &srczone) ||
466126444Sume	    in6_addr2zoneid(m->m_pkthdr.rcvif, &ip6->ip6_dst, &dstzone)) {
467126444Sume		/*
468126444Sume		 * Note that these generic checks cover cases that src or
469126444Sume		 * dst are the loopback address and the receiving interface
470126444Sume		 * is not loopback.
471126444Sume		 */
472126444Sume		ip6stat.ip6s_badscope++;
473126444Sume		goto bad;
474126444Sume	}
47553541Sshin
476126444Sume	bzero(&sa6, sizeof(sa6));
477126444Sume	sa6.sin6_family = AF_INET6;
478126444Sume	sa6.sin6_len = sizeof(struct sockaddr_in6);
479126444Sume
480126444Sume	sa6.sin6_addr = ip6->ip6_src;
481126444Sume	sa6.sin6_scope_id = srczone;
482126444Sume	if (in6_embedscope(&ip6->ip6_src, &sa6, NULL, NULL)) {
483126444Sume		/* XXX: should not happen */
484126444Sume		ip6stat.ip6s_badscope++;
485126444Sume		goto bad;
486126444Sume	}
487126444Sume
488126444Sume	sa6.sin6_addr = ip6->ip6_dst;
489126444Sume	sa6.sin6_scope_id = dstzone;
490126444Sume	if (in6_embedscope(&ip6->ip6_dst, &sa6, NULL, NULL)) {
491126444Sume		/* XXX: should not happen */
492126444Sume		ip6stat.ip6s_badscope++;
493126444Sume		goto bad;
494126444Sume	}
495126444Sume
49653541Sshin	/*
49753541Sshin	 * Multicast check
49853541Sshin	 */
49953541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
500120913Sume	  	struct in6_multi *in6m = 0;
50153541Sshin
50253541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
50353541Sshin		/*
50453541Sshin		 * See if we belong to the destination multicast group on the
50553541Sshin		 * arrival interface.
50653541Sshin		 */
50753541Sshin		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
50853541Sshin		if (in6m)
50953541Sshin			ours = 1;
51056723Sshin		else if (!ip6_mrouter) {
51153541Sshin			ip6stat.ip6s_notmember++;
51253541Sshin			ip6stat.ip6s_cantforward++;
51353541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
51453541Sshin			goto bad;
51553541Sshin		}
51653541Sshin		deliverifp = m->m_pkthdr.rcvif;
51753541Sshin		goto hbhcheck;
51853541Sshin	}
51953541Sshin
52053541Sshin	/*
52153541Sshin	 *  Unicast check
52253541Sshin	 */
52362587Sitojun	if (ip6_forward_rt.ro_rt != NULL &&
524120913Sume	    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
52562587Sitojun	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
526120913Sume	    &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
52762587Sitojun		ip6stat.ip6s_forward_cachehit++;
52862587Sitojun	else {
52978064Sume		struct sockaddr_in6 *dst6;
53078064Sume
53153541Sshin		if (ip6_forward_rt.ro_rt) {
53262587Sitojun			/* route is down or destination is different */
53362587Sitojun			ip6stat.ip6s_forward_cachemiss++;
53453541Sshin			RTFREE(ip6_forward_rt.ro_rt);
53553541Sshin			ip6_forward_rt.ro_rt = 0;
53653541Sshin		}
53762587Sitojun
53853541Sshin		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
53978064Sume		dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
54078064Sume		dst6->sin6_len = sizeof(struct sockaddr_in6);
54178064Sume		dst6->sin6_family = AF_INET6;
54278064Sume		dst6->sin6_addr = ip6->ip6_dst;
54353541Sshin
544122921Sandre		rtalloc((struct route *)&ip6_forward_rt);
54553541Sshin	}
54653541Sshin
54753541Sshin#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
54853541Sshin
54953541Sshin	/*
55053541Sshin	 * Accept the packet if the forwarding interface to the destination
55153541Sshin	 * according to the routing table is the loopback interface,
55253541Sshin	 * unless the associated route has a gateway.
55353541Sshin	 * Note that this approach causes to accept a packet if there is a
55453541Sshin	 * route to the loopback interface for the destination of the packet.
55553541Sshin	 * But we think it's even useful in some situations, e.g. when using
55653541Sshin	 * a special daemon which wants to intercept the packet.
55778064Sume	 *
55878064Sume	 * XXX: some OSes automatically make a cloned route for the destination
55978064Sume	 * of an outgoing packet.  If the outgoing interface of the packet
56078064Sume	 * is a loopback one, the kernel would consider the packet to be
56178064Sume	 * accepted, even if we have no such address assinged on the interface.
56278064Sume	 * We check the cloned flag of the route entry to reject such cases,
56378064Sume	 * assuming that route entries for our own addresses are not made by
56478064Sume	 * cloning (it should be true because in6_addloop explicitly installs
56578064Sume	 * the host route).  However, we might have to do an explicit check
56678064Sume	 * while it would be less efficient.  Or, should we rather install a
56778064Sume	 * reject route for such a case?
56853541Sshin	 */
56953541Sshin	if (ip6_forward_rt.ro_rt &&
57053541Sshin	    (ip6_forward_rt.ro_rt->rt_flags &
57153541Sshin	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
57278064Sume#ifdef RTF_WASCLONED
57378064Sume	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
57478064Sume#endif
57578064Sume#ifdef RTF_CLONED
57678064Sume	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
57778064Sume#endif
57862587Sitojun#if 0
57953541Sshin	    /*
58062587Sitojun	     * The check below is redundant since the comparison of
58162587Sitojun	     * the destination and the key of the rtentry has
58262587Sitojun	     * already done through looking up the routing table.
58353541Sshin	     */
58462587Sitojun	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
585120913Sume	    &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
58662587Sitojun#endif
58753541Sshin	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
58853541Sshin		struct in6_ifaddr *ia6 =
58953541Sshin			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
59078064Sume
59162587Sitojun		/*
592121630Sume		 * record address information into m_tag.
59378064Sume		 */
59478064Sume		(void)ip6_setdstifaddr(m, ia6);
59578064Sume
59678064Sume		/*
59762587Sitojun		 * packets to a tentative, duplicated, or somehow invalid
59862587Sitojun		 * address must not be accepted.
59962587Sitojun		 */
60053541Sshin		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
60162587Sitojun			/* this address is ready */
60253541Sshin			ours = 1;
60353541Sshin			deliverifp = ia6->ia_ifp;	/* correct? */
60467334Sjoe			/* Count the packet in the ip address stats */
60567334Sjoe			ia6->ia_ifa.if_ipackets++;
60667334Sjoe			ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
60753541Sshin			goto hbhcheck;
60853541Sshin		} else {
60962587Sitojun			/* address is not ready, so discard the packet. */
61078064Sume			nd6log((LOG_INFO,
61178064Sume			    "ip6_input: packet to an unready address %s->%s\n",
61262587Sitojun			    ip6_sprintf(&ip6->ip6_src),
61378064Sume			    ip6_sprintf(&ip6->ip6_dst)));
61462587Sitojun
61562587Sitojun			goto bad;
61653541Sshin		}
61753541Sshin	}
61853541Sshin
61953541Sshin	/*
620120913Sume	 * FAITH (Firewall Aided Internet Translator)
62153541Sshin	 */
62253541Sshin	if (ip6_keepfaith) {
62353541Sshin		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
62453541Sshin		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
62553541Sshin			/* XXX do we need more sanity checks? */
62653541Sshin			ours = 1;
62795023Ssuz			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
62853541Sshin			goto hbhcheck;
62953541Sshin		}
63053541Sshin	}
63153541Sshin
63253541Sshin	/*
63353541Sshin	 * Now there is no reason to process the packet if it's not our own
63453541Sshin	 * and we're not a router.
63553541Sshin	 */
63653541Sshin	if (!ip6_forwarding) {
63753541Sshin		ip6stat.ip6s_cantforward++;
63853541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
63953541Sshin		goto bad;
64053541Sshin	}
64153541Sshin
64253541Sshin  hbhcheck:
64353541Sshin	/*
644121630Sume	 * record address information into m_tag, if we don't have one yet.
64578064Sume	 * note that we are unable to record it, if the address is not listed
64678064Sume	 * as our interface address (e.g. multicast addresses, addresses
64778064Sume	 * within FAITH prefixes and such).
64878064Sume	 */
64978064Sume	if (deliverifp && !ip6_getdstifaddr(m)) {
65078064Sume		struct in6_ifaddr *ia6;
65178064Sume
65278064Sume		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
65378064Sume		if (ia6) {
65478064Sume			if (!ip6_setdstifaddr(m, ia6)) {
65578064Sume				/*
65678064Sume				 * XXX maybe we should drop the packet here,
65778064Sume				 * as we could not provide enough information
65878064Sume				 * to the upper layers.
65978064Sume				 */
66078064Sume			}
66178064Sume		}
66278064Sume	}
66378064Sume
66478064Sume	/*
66553541Sshin	 * Process Hop-by-Hop options header if it's contained.
66653541Sshin	 * m may be modified in ip6_hopopts_input().
66753541Sshin	 * If a JumboPayload option is included, plen will also be modified.
66853541Sshin	 */
66953541Sshin	plen = (u_int32_t)ntohs(ip6->ip6_plen);
67053541Sshin	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
67162587Sitojun		struct ip6_hbh *hbh;
67262587Sitojun
67353541Sshin		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
67462587Sitojun#if 0	/*touches NULL pointer*/
67553541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
67662587Sitojun#endif
67753541Sshin			return;	/* m have already been freed */
67853541Sshin		}
67962587Sitojun
68053541Sshin		/* adjust pointer */
68153541Sshin		ip6 = mtod(m, struct ip6_hdr *);
68253541Sshin
68353541Sshin		/*
68495023Ssuz		 * if the payload length field is 0 and the next header field
68562587Sitojun		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
68662587Sitojun		 * option MUST be included.
68762587Sitojun		 */
68862587Sitojun		if (ip6->ip6_plen == 0 && plen == 0) {
68962587Sitojun			/*
69062587Sitojun			 * Note that if a valid jumbo payload option is
691120913Sume			 * contained, ip6_hopopts_input() must set a valid
692120913Sume			 * (non-zero) payload length to the variable plen.
69362587Sitojun			 */
69462587Sitojun			ip6stat.ip6s_badoptions++;
69562587Sitojun			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
69662587Sitojun			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
69762587Sitojun			icmp6_error(m, ICMP6_PARAM_PROB,
69862587Sitojun				    ICMP6_PARAMPROB_HEADER,
69962587Sitojun				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
70062587Sitojun			return;
70162587Sitojun		}
70262587Sitojun#ifndef PULLDOWN_TEST
70362587Sitojun		/* ip6_hopopts_input() ensures that mbuf is contiguous */
70462587Sitojun		hbh = (struct ip6_hbh *)(ip6 + 1);
70562587Sitojun#else
70662587Sitojun		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
70762587Sitojun			sizeof(struct ip6_hbh));
70862587Sitojun		if (hbh == NULL) {
70962587Sitojun			ip6stat.ip6s_tooshort++;
71062587Sitojun			return;
71162587Sitojun		}
71262587Sitojun#endif
71362587Sitojun		nxt = hbh->ip6h_nxt;
71462587Sitojun
71562587Sitojun		/*
71653541Sshin		 * accept the packet if a router alert option is included
71753541Sshin		 * and we act as an IPv6 router.
71853541Sshin		 */
71953541Sshin		if (rtalert != ~0 && ip6_forwarding)
72053541Sshin			ours = 1;
72153541Sshin	} else
72253541Sshin		nxt = ip6->ip6_nxt;
72353541Sshin
72453541Sshin	/*
72553541Sshin	 * Check that the amount of data in the buffers
72653541Sshin	 * is as at least much as the IPv6 header would have us expect.
72753541Sshin	 * Trim mbufs if longer than we expect.
72853541Sshin	 * Drop packet if shorter than we expect.
72953541Sshin	 */
73053541Sshin	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
73153541Sshin		ip6stat.ip6s_tooshort++;
73253541Sshin		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
73353541Sshin		goto bad;
73453541Sshin	}
73553541Sshin	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
73653541Sshin		if (m->m_len == m->m_pkthdr.len) {
73753541Sshin			m->m_len = sizeof(struct ip6_hdr) + plen;
73853541Sshin			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
73953541Sshin		} else
74053541Sshin			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
74153541Sshin	}
74253541Sshin
74353541Sshin	/*
74453541Sshin	 * Forward if desirable.
74553541Sshin	 */
74653541Sshin	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
74756723Sshin		/*
74856723Sshin		 * If we are acting as a multicast router, all
74956723Sshin		 * incoming multicast packets are passed to the
75056723Sshin		 * kernel-level multicast forwarding function.
75156723Sshin		 * The packet is returned (relatively) intact; if
75256723Sshin		 * ip6_mforward() returns a non-zero value, the packet
75356723Sshin		 * must be discarded, else it may be accepted below.
75456723Sshin		 */
75556723Sshin		if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
75656723Sshin			ip6stat.ip6s_cantforward++;
75756723Sshin			m_freem(m);
75856723Sshin			return;
75956723Sshin		}
76053541Sshin		if (!ours) {
76153541Sshin			m_freem(m);
76253541Sshin			return;
76353541Sshin		}
76453541Sshin	} else if (!ours) {
765121143Ssam		ip6_forward(m, srcrt);
76653541Sshin		return;
767121673Sume	}
76853541Sshin
76962587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
77062587Sitojun
77153541Sshin	/*
77262587Sitojun	 * Malicious party may be able to use IPv4 mapped addr to confuse
77362587Sitojun	 * tcp/udp stack and bypass security checks (act as if it was from
77462587Sitojun	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
77562587Sitojun	 *
77662587Sitojun	 * For SIIT end node behavior, you may want to disable the check.
77762587Sitojun	 * However, you will  become vulnerable to attacks using IPv4 mapped
77862587Sitojun	 * source.
77962587Sitojun	 */
78062587Sitojun	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
78162587Sitojun	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
78262587Sitojun		ip6stat.ip6s_badscope++;
78362587Sitojun		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
78462587Sitojun		goto bad;
78562587Sitojun	}
78662587Sitojun
78762587Sitojun	/*
78853541Sshin	 * Tell launch routine the next header
78953541Sshin	 */
79053541Sshin	ip6stat.ip6s_delivered++;
79153541Sshin	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
79253541Sshin	nest = 0;
79378064Sume
79453541Sshin	while (nxt != IPPROTO_DONE) {
79553541Sshin		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
79653541Sshin			ip6stat.ip6s_toomanyhdr++;
79753541Sshin			goto bad;
79853541Sshin		}
79953541Sshin
80053541Sshin		/*
80153541Sshin		 * protection against faulty packet - there should be
80253541Sshin		 * more sanity checks in header chain processing.
80353541Sshin		 */
80453541Sshin		if (m->m_pkthdr.len < off) {
80553541Sshin			ip6stat.ip6s_tooshort++;
80653541Sshin			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
80753541Sshin			goto bad;
80853541Sshin		}
80953541Sshin
81078064Sume#ifdef IPSEC
81178064Sume		/*
81278064Sume		 * enforce IPsec policy checking if we are seeing last header.
81378064Sume		 * note that we do not visit this with protocols with pcb layer
81478064Sume		 * code - like udp/tcp/raw ip.
81578064Sume		 */
81678064Sume		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
81778064Sume		    ipsec6_in_reject(m, NULL)) {
81878064Sume			ipsec6stat.in_polvio++;
81978064Sume			goto bad;
82078064Sume		}
82178064Sume#endif
82253541Sshin		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
82353541Sshin	}
82453541Sshin	return;
82553541Sshin bad:
82653541Sshin	m_freem(m);
82753541Sshin}
82853541Sshin
82953541Sshin/*
83078064Sume * set/grab in6_ifaddr correspond to IPv6 destination address.
83178064Sume * XXX backward compatibility wrapper
83278064Sume */
833121673Sumestatic struct ip6aux *
83478064Sumeip6_setdstifaddr(m, ia6)
83578064Sume	struct mbuf *m;
83678064Sume	struct in6_ifaddr *ia6;
83778064Sume{
838121673Sume	struct ip6aux *ip6a;
83978064Sume
840121673Sume	ip6a = ip6_addaux(m);
841121673Sume	if (ip6a)
842121673Sume		ip6a->ip6a_dstia6 = ia6;
843121673Sume	return ip6a;	/* NULL if failed to set */
84478064Sume}
84578064Sume
84678064Sumestruct in6_ifaddr *
84778064Sumeip6_getdstifaddr(m)
84878064Sume	struct mbuf *m;
84978064Sume{
850121673Sume	struct ip6aux *ip6a;
85178064Sume
852121673Sume	ip6a = ip6_findaux(m);
853121673Sume	if (ip6a)
854121673Sume		return ip6a->ip6a_dstia6;
85578064Sume	else
85678064Sume		return NULL;
85778064Sume}
85878064Sume
85978064Sume/*
86053541Sshin * Hop-by-Hop options header processing. If a valid jumbo payload option is
86153541Sshin * included, the real payload length will be stored in plenp.
86253541Sshin */
86353541Sshinstatic int
86453541Sshinip6_hopopts_input(plenp, rtalertp, mp, offp)
86553541Sshin	u_int32_t *plenp;
86653541Sshin	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
86753541Sshin	struct mbuf **mp;
86853541Sshin	int *offp;
86953541Sshin{
87078064Sume	struct mbuf *m = *mp;
87153541Sshin	int off = *offp, hbhlen;
87253541Sshin	struct ip6_hbh *hbh;
87353541Sshin	u_int8_t *opt;
87453541Sshin
87553541Sshin	/* validation of the length of the header */
87662587Sitojun#ifndef PULLDOWN_TEST
87753541Sshin	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
87853541Sshin	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
87953541Sshin	hbhlen = (hbh->ip6h_len + 1) << 3;
88053541Sshin
88153541Sshin	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
88253541Sshin	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
88362587Sitojun#else
88462587Sitojun	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
88562587Sitojun		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
88662587Sitojun	if (hbh == NULL) {
88762587Sitojun		ip6stat.ip6s_tooshort++;
88862587Sitojun		return -1;
88962587Sitojun	}
89062587Sitojun	hbhlen = (hbh->ip6h_len + 1) << 3;
89162587Sitojun	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
89262587Sitojun		hbhlen);
89362587Sitojun	if (hbh == NULL) {
89462587Sitojun		ip6stat.ip6s_tooshort++;
89562587Sitojun		return -1;
89662587Sitojun	}
89762587Sitojun#endif
89853541Sshin	off += hbhlen;
89953541Sshin	hbhlen -= sizeof(struct ip6_hbh);
90053541Sshin	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
90153541Sshin
90253541Sshin	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
90353541Sshin				hbhlen, rtalertp, plenp) < 0)
904120856Sume		return (-1);
90553541Sshin
90653541Sshin	*offp = off;
90753541Sshin	*mp = m;
908120856Sume	return (0);
90953541Sshin}
91053541Sshin
91153541Sshin/*
91253541Sshin * Search header for all Hop-by-hop options and process each option.
91353541Sshin * This function is separate from ip6_hopopts_input() in order to
91453541Sshin * handle a case where the sending node itself process its hop-by-hop
91553541Sshin * options header. In such a case, the function is called from ip6_output().
91678064Sume *
91778064Sume * The function assumes that hbh header is located right after the IPv6 header
91878064Sume * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
91978064Sume * opthead + hbhlen is located in continuous memory region.
92053541Sshin */
92153541Sshinint
92253541Sshinip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
92353541Sshin	struct mbuf *m;
92453541Sshin	u_int8_t *opthead;
92553541Sshin	int hbhlen;
92653541Sshin	u_int32_t *rtalertp;
92753541Sshin	u_int32_t *plenp;
92853541Sshin{
92953541Sshin	struct ip6_hdr *ip6;
93053541Sshin	int optlen = 0;
93153541Sshin	u_int8_t *opt = opthead;
93253541Sshin	u_int16_t rtalert_val;
93362587Sitojun	u_int32_t jumboplen;
93478064Sume	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
93553541Sshin
93653541Sshin	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
93778064Sume		switch (*opt) {
93878064Sume		case IP6OPT_PAD1:
93978064Sume			optlen = 1;
94078064Sume			break;
94178064Sume		case IP6OPT_PADN:
94278064Sume			if (hbhlen < IP6OPT_MINLEN) {
94378064Sume				ip6stat.ip6s_toosmall++;
94478064Sume				goto bad;
94578064Sume			}
94678064Sume			optlen = *(opt + 1) + 2;
94778064Sume			break;
948121472Sume		case IP6OPT_ROUTER_ALERT:
94962587Sitojun			/* XXX may need check for alignment */
95078064Sume			if (hbhlen < IP6OPT_RTALERT_LEN) {
95178064Sume				ip6stat.ip6s_toosmall++;
95278064Sume				goto bad;
95378064Sume			}
95478064Sume			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
95578064Sume				/* XXX stat */
95678064Sume				icmp6_error(m, ICMP6_PARAM_PROB,
957120913Sume				    ICMP6_PARAMPROB_HEADER,
958120913Sume				    erroff + opt + 1 - opthead);
959120856Sume				return (-1);
96078064Sume			}
96178064Sume			optlen = IP6OPT_RTALERT_LEN;
96278064Sume			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
96378064Sume			*rtalertp = ntohs(rtalert_val);
96478064Sume			break;
96578064Sume		case IP6OPT_JUMBO:
96678064Sume			/* XXX may need check for alignment */
96762587Sitojun			if (hbhlen < IP6OPT_JUMBO_LEN) {
96862587Sitojun				ip6stat.ip6s_toosmall++;
96962587Sitojun				goto bad;
97062587Sitojun			}
97178064Sume			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
97278064Sume				/* XXX stat */
97378064Sume				icmp6_error(m, ICMP6_PARAM_PROB,
974120913Sume				    ICMP6_PARAMPROB_HEADER,
975120913Sume				    erroff + opt + 1 - opthead);
976120856Sume				return (-1);
97778064Sume			}
97862587Sitojun			optlen = IP6OPT_JUMBO_LEN;
97953541Sshin
98062587Sitojun			/*
98162587Sitojun			 * IPv6 packets that have non 0 payload length
98278064Sume			 * must not contain a jumbo payload option.
98362587Sitojun			 */
98462587Sitojun			ip6 = mtod(m, struct ip6_hdr *);
98562587Sitojun			if (ip6->ip6_plen) {
98662587Sitojun				ip6stat.ip6s_badoptions++;
98762587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
988120913Sume				    ICMP6_PARAMPROB_HEADER,
989120913Sume				    erroff + opt - opthead);
990120856Sume				return (-1);
99162587Sitojun			}
99253541Sshin
99362587Sitojun			/*
99462587Sitojun			 * We may see jumbolen in unaligned location, so
99562587Sitojun			 * we'd need to perform bcopy().
99662587Sitojun			 */
99762587Sitojun			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
99862587Sitojun			jumboplen = (u_int32_t)htonl(jumboplen);
99962587Sitojun
100062587Sitojun#if 1
100162587Sitojun			/*
100262587Sitojun			 * if there are multiple jumbo payload options,
100362587Sitojun			 * *plenp will be non-zero and the packet will be
100462587Sitojun			 * rejected.
100562587Sitojun			 * the behavior may need some debate in ipngwg -
100662587Sitojun			 * multiple options does not make sense, however,
100762587Sitojun			 * there's no explicit mention in specification.
100862587Sitojun			 */
100962587Sitojun			if (*plenp != 0) {
101062587Sitojun				ip6stat.ip6s_badoptions++;
101162587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
1012120913Sume				    ICMP6_PARAMPROB_HEADER,
1013120913Sume				    erroff + opt + 2 - opthead);
1014120856Sume				return (-1);
101562587Sitojun			}
101662587Sitojun#endif
101762587Sitojun
101862587Sitojun			/*
101962587Sitojun			 * jumbo payload length must be larger than 65535.
102062587Sitojun			 */
102162587Sitojun			if (jumboplen <= IPV6_MAXPACKET) {
102262587Sitojun				ip6stat.ip6s_badoptions++;
102362587Sitojun				icmp6_error(m, ICMP6_PARAM_PROB,
1024120913Sume				    ICMP6_PARAMPROB_HEADER,
1025120913Sume				    erroff + opt + 2 - opthead);
1026120856Sume				return (-1);
102762587Sitojun			}
102862587Sitojun			*plenp = jumboplen;
102962587Sitojun
103062587Sitojun			break;
103178064Sume		default:		/* unknown option */
103278064Sume			if (hbhlen < IP6OPT_MINLEN) {
103378064Sume				ip6stat.ip6s_toosmall++;
103478064Sume				goto bad;
103578064Sume			}
103678064Sume			optlen = ip6_unknown_opt(opt, m,
103778064Sume			    erroff + opt - opthead);
103878064Sume			if (optlen == -1)
1039120856Sume				return (-1);
104078064Sume			optlen += 2;
104178064Sume			break;
104253541Sshin		}
104353541Sshin	}
104453541Sshin
1045120856Sume	return (0);
104653541Sshin
104753541Sshin  bad:
104853541Sshin	m_freem(m);
1049120856Sume	return (-1);
105053541Sshin}
105153541Sshin
105253541Sshin/*
105353541Sshin * Unknown option processing.
105453541Sshin * The third argument `off' is the offset from the IPv6 header to the option,
105553541Sshin * which is necessary if the IPv6 header the and option header and IPv6 header
105653541Sshin * is not continuous in order to return an ICMPv6 error.
105753541Sshin */
105853541Sshinint
105953541Sshinip6_unknown_opt(optp, m, off)
106053541Sshin	u_int8_t *optp;
106153541Sshin	struct mbuf *m;
106253541Sshin	int off;
106353541Sshin{
106453541Sshin	struct ip6_hdr *ip6;
106553541Sshin
106678064Sume	switch (IP6OPT_TYPE(*optp)) {
106778064Sume	case IP6OPT_TYPE_SKIP: /* ignore the option */
1068120856Sume		return ((int)*(optp + 1));
106978064Sume	case IP6OPT_TYPE_DISCARD:	/* silently discard */
107078064Sume		m_freem(m);
1071120856Sume		return (-1);
107278064Sume	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
107378064Sume		ip6stat.ip6s_badoptions++;
107478064Sume		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1075120856Sume		return (-1);
107678064Sume	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
107778064Sume		ip6stat.ip6s_badoptions++;
107878064Sume		ip6 = mtod(m, struct ip6_hdr *);
107978064Sume		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
108078064Sume		    (m->m_flags & (M_BCAST|M_MCAST)))
108178064Sume			m_freem(m);
108278064Sume		else
108378064Sume			icmp6_error(m, ICMP6_PARAM_PROB,
108478064Sume				    ICMP6_PARAMPROB_OPTION, off);
1085120856Sume		return (-1);
108653541Sshin	}
108753541Sshin
108853541Sshin	m_freem(m);		/* XXX: NOTREACHED */
1089120856Sume	return (-1);
109053541Sshin}
109153541Sshin
109253541Sshin/*
109362587Sitojun * Create the "control" list for this pcb.
109478064Sume * The function will not modify mbuf chain at all.
109562587Sitojun *
109678064Sume * with KAME mbuf chain restriction:
109762587Sitojun * The routine will be called from upper layer handlers like tcp6_input().
109862587Sitojun * Thus the routine assumes that the caller (tcp6_input) have already
109962587Sitojun * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
110062587Sitojun * very first mbuf on the mbuf chain.
110153541Sshin */
110253541Sshinvoid
1103121674Sumeip6_savecontrol(in6p, m, mp)
110478064Sume	struct inpcb *in6p;
1105121674Sume	struct mbuf *m, **mp;
110653541Sshin{
1107121472Sume#define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
110893593Sjhb	struct thread *td = curthread;	/* XXX */
110978064Sume	int privileged = 0;
1110121674Sume	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
111153541Sshin
111293593Sjhb	if (td && !suser(td))
1113120913Sume		privileged++;
111453541Sshin
111578064Sume#ifdef SO_TIMESTAMP
111678064Sume	if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
111753541Sshin		struct timeval tv;
111853541Sshin
111953541Sshin		microtime(&tv);
112053541Sshin		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1121120913Sume		    SCM_TIMESTAMP, SOL_SOCKET);
1122121472Sume		if (*mp)
112353541Sshin			mp = &(*mp)->m_next;
112497658Stanimura	}
112562587Sitojun#endif
112662587Sitojun
1127121631Sume	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
1128121631Sume		return;
1129121631Sume
113053541Sshin	/* RFC 2292 sec. 5 */
113178064Sume	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
113253541Sshin		struct in6_pktinfo pi6;
1133120913Sume
113453541Sshin		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1135121315Sume		in6_clearscope(&pi6.ipi6_addr);	/* XXX */
1136120913Sume		pi6.ipi6_ifindex =
1137120913Sume		    (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1138120913Sume
113953541Sshin		*mp = sbcreatecontrol((caddr_t) &pi6,
1140120913Sume		    sizeof(struct in6_pktinfo),
1141121472Sume		    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1142121472Sume		if (*mp)
114353541Sshin			mp = &(*mp)->m_next;
114453541Sshin	}
114578064Sume
114678064Sume	if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
114753541Sshin		int hlim = ip6->ip6_hlim & 0xff;
1148120913Sume
1149120913Sume		*mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1150121472Sume		    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1151121472Sume		if (*mp)
115253541Sshin			mp = &(*mp)->m_next;
115353541Sshin	}
115453541Sshin
1155121472Sume	if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1156121472Sume		u_int32_t flowinfo;
1157121472Sume		int tclass;
1158121472Sume
1159121472Sume		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1160121472Sume		flowinfo >>= 20;
1161121472Sume
1162121472Sume		tclass = flowinfo & 0xff;
1163121472Sume		*mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass),
1164121472Sume		    IPV6_TCLASS, IPPROTO_IPV6);
1165121472Sume		if (*mp)
1166121472Sume			mp = &(*mp)->m_next;
1167121472Sume	}
1168121472Sume
116953541Sshin	/*
117053541Sshin	 * IPV6_HOPOPTS socket option. We require super-user privilege
117153541Sshin	 * for the option, but it might be too strict, since there might
117253541Sshin	 * be some hop-by-hop options which can be returned to normal user.
117353541Sshin	 * See RFC 2292 section 6.
117453541Sshin	 */
1175121472Sume	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1176121472Sume#ifdef DIAGNOSTIC
1177121472Sume		if (!privileged)
1178121472Sume			panic("IN6P_HOPOPTS is set for unprivileged socket");
1179121472Sume#endif
118053541Sshin		/*
118153541Sshin		 * Check if a hop-by-hop options header is contatined in the
118253541Sshin		 * received packet, and if so, store the options as ancillary
118353541Sshin		 * data. Note that a hop-by-hop options header must be
1184120913Sume		 * just after the IPv6 header, which is assured through the
1185120913Sume		 * IPv6 input processing.
118653541Sshin		 */
118753541Sshin		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
118862587Sitojun			struct ip6_hbh *hbh;
118978064Sume			int hbhlen = 0;
119078064Sume#ifdef PULLDOWN_TEST
119178064Sume			struct mbuf *ext;
119278064Sume#endif
119353541Sshin
119462587Sitojun#ifndef PULLDOWN_TEST
119562587Sitojun			hbh = (struct ip6_hbh *)(ip6 + 1);
119662587Sitojun			hbhlen = (hbh->ip6h_len + 1) << 3;
119762587Sitojun#else
119878064Sume			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
119978064Sume			    ip6->ip6_nxt);
120078064Sume			if (ext == NULL) {
120162587Sitojun				ip6stat.ip6s_tooshort++;
120262587Sitojun				return;
120362587Sitojun			}
120478064Sume			hbh = mtod(ext, struct ip6_hbh *);
120562587Sitojun			hbhlen = (hbh->ip6h_len + 1) << 3;
120678064Sume			if (hbhlen != ext->m_len) {
120778064Sume				m_freem(ext);
120862587Sitojun				ip6stat.ip6s_tooshort++;
120962587Sitojun				return;
121062587Sitojun			}
121162587Sitojun#endif
121262587Sitojun
121353541Sshin			/*
1214120913Sume			 * XXX: We copy the whole header even if a
1215120913Sume			 * jumbo payload option is included, the option which
1216120913Sume			 * is to be removed before returning according to
1217120913Sume			 * RFC2292.
121878064Sume			 * Note: this constraint is removed in 2292bis.
121953541Sshin			 */
122062587Sitojun			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1221121472Sume			    IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1222121472Sume			    IPPROTO_IPV6);
1223121472Sume			if (*mp)
122453541Sshin				mp = &(*mp)->m_next;
122578064Sume#ifdef PULLDOWN_TEST
122678064Sume			m_freem(ext);
122778064Sume#endif
122853541Sshin		}
122953541Sshin	}
123053541Sshin
1231121472Sume	if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
123278064Sume		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
123353541Sshin
123453541Sshin		/*
123553541Sshin		 * Search for destination options headers or routing
123653541Sshin		 * header(s) through the header chain, and stores each
123753541Sshin		 * header as ancillary data.
123853541Sshin		 * Note that the order of the headers remains in
123953541Sshin		 * the chain of ancillary data.
124053541Sshin		 */
124178064Sume		while (1) {	/* is explicit loop prevention necessary? */
124278064Sume			struct ip6_ext *ip6e = NULL;
124362587Sitojun			int elen;
124478064Sume#ifdef PULLDOWN_TEST
124578064Sume			struct mbuf *ext = NULL;
124678064Sume#endif
124753541Sshin
124878064Sume			/*
124978064Sume			 * if it is not an extension header, don't try to
125078064Sume			 * pull it from the chain.
125178064Sume			 */
125278064Sume			switch (nxt) {
125378064Sume			case IPPROTO_DSTOPTS:
125478064Sume			case IPPROTO_ROUTING:
125578064Sume			case IPPROTO_HOPOPTS:
125678064Sume			case IPPROTO_AH: /* is it possible? */
125778064Sume				break;
125878064Sume			default:
125978064Sume				goto loopend;
126078064Sume			}
126178064Sume
126262587Sitojun#ifndef PULLDOWN_TEST
126378064Sume			if (off + sizeof(*ip6e) > m->m_len)
126478064Sume				goto loopend;
126562587Sitojun			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
126662587Sitojun			if (nxt == IPPROTO_AH)
126762587Sitojun				elen = (ip6e->ip6e_len + 2) << 2;
126862587Sitojun			else
126962587Sitojun				elen = (ip6e->ip6e_len + 1) << 3;
127078064Sume			if (off + elen > m->m_len)
127178064Sume				goto loopend;
127262587Sitojun#else
127378064Sume			ext = ip6_pullexthdr(m, off, nxt);
127478064Sume			if (ext == NULL) {
127562587Sitojun				ip6stat.ip6s_tooshort++;
127662587Sitojun				return;
127762587Sitojun			}
127878064Sume			ip6e = mtod(ext, struct ip6_ext *);
127962587Sitojun			if (nxt == IPPROTO_AH)
128062587Sitojun				elen = (ip6e->ip6e_len + 2) << 2;
128162587Sitojun			else
128262587Sitojun				elen = (ip6e->ip6e_len + 1) << 3;
128378064Sume			if (elen != ext->m_len) {
128478064Sume				m_freem(ext);
128562587Sitojun				ip6stat.ip6s_tooshort++;
128662587Sitojun				return;
128762587Sitojun			}
128862587Sitojun#endif
128962587Sitojun
129078064Sume			switch (nxt) {
129178064Sume			case IPPROTO_DSTOPTS:
1292120913Sume				if (!(in6p->in6p_flags & IN6P_DSTOPTS))
129378064Sume					break;
129453541Sshin
129578064Sume				/*
129678064Sume				 * We also require super-user privilege for
1297120913Sume				 * the option.  See comments on IN6_HOPOPTS.
129878064Sume				 */
129978064Sume				if (!privileged)
130078064Sume					break;
130153541Sshin
130278064Sume				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1303121472Sume				    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1304120913Sume				    IPPROTO_IPV6);
130578064Sume				if (*mp)
130678064Sume					mp = &(*mp)->m_next;
130778064Sume				break;
130878064Sume			case IPPROTO_ROUTING:
130978064Sume				if (!in6p->in6p_flags & IN6P_RTHDR)
131078064Sume					break;
131153541Sshin
131278064Sume				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1313121472Sume				    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1314120913Sume				    IPPROTO_IPV6);
131578064Sume				if (*mp)
131678064Sume					mp = &(*mp)->m_next;
131778064Sume				break;
131878064Sume			case IPPROTO_HOPOPTS:
131978064Sume			case IPPROTO_AH: /* is it possible? */
132078064Sume				break;
132153541Sshin
132278064Sume			default:
132378064Sume				/*
132478064Sume			 	 * other cases have been filtered in the above.
132578064Sume				 * none will visit this case.  here we supply
132678064Sume				 * the code just in case (nxt overwritten or
132778064Sume				 * other cases).
132878064Sume				 */
132978064Sume#ifdef PULLDOWN_TEST
133078064Sume				m_freem(ext);
133178064Sume#endif
133278064Sume				goto loopend;
133353541Sshin
133453541Sshin			}
133553541Sshin
133653541Sshin			/* proceed with the next header. */
133762587Sitojun			off += elen;
133853541Sshin			nxt = ip6e->ip6e_nxt;
133978064Sume			ip6e = NULL;
134078064Sume#ifdef PULLDOWN_TEST
134178064Sume			m_freem(ext);
134278064Sume			ext = NULL;
134378064Sume#endif
134453541Sshin		}
134553541Sshin	  loopend:
134678064Sume		;
134753541Sshin	}
134878064Sume
1349121472Sume#undef IS2292
135078064Sume}
135178064Sume
1352125776Sumevoid
1353125776Sumeip6_notify_pmtu(in6p, dst, mtu)
1354125776Sume	struct inpcb *in6p;
1355125776Sume	struct sockaddr_in6 *dst;
1356125776Sume	u_int32_t *mtu;
1357125776Sume{
1358125776Sume	struct socket *so;
1359125776Sume	struct mbuf *m_mtu;
1360125776Sume	struct ip6_mtuinfo mtuctl;
1361125776Sume
1362125776Sume	so =  in6p->inp_socket;
1363125776Sume
1364125776Sume	if (mtu == NULL)
1365125776Sume		return;
1366125776Sume
1367125776Sume#ifdef DIAGNOSTIC
1368125776Sume	if (so == NULL)		/* I believe this is impossible */
1369125776Sume		panic("ip6_notify_pmtu: socket is NULL");
1370125776Sume#endif
1371125776Sume
1372125776Sume	bzero(&mtuctl, sizeof(mtuctl));	/* zero-clear for safety */
1373125776Sume	mtuctl.ip6m_mtu = *mtu;
1374125776Sume	mtuctl.ip6m_addr = *dst;
1375125776Sume	in6_recoverscope(&mtuctl.ip6m_addr, &mtuctl.ip6m_addr.sin6_addr, NULL);
1376125776Sume
1377125776Sume	if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1378125776Sume	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1379125776Sume		return;
1380125776Sume
1381125776Sume	if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1382125776Sume	    == 0) {
1383125776Sume		m_freem(m_mtu);
1384125776Sume		/* XXX: should count statistics */
1385125776Sume	} else
1386125776Sume		sorwakeup(so);
1387125776Sume
1388125776Sume	return;
1389125776Sume}
1390125776Sume
139178064Sume#ifdef PULLDOWN_TEST
139278064Sume/*
139378064Sume * pull single extension header from mbuf chain.  returns single mbuf that
139478064Sume * contains the result, or NULL on error.
139578064Sume */
139678064Sumestatic struct mbuf *
139778064Sumeip6_pullexthdr(m, off, nxt)
139878064Sume	struct mbuf *m;
139978064Sume	size_t off;
140078064Sume	int nxt;
140178064Sume{
140278064Sume	struct ip6_ext ip6e;
140378064Sume	size_t elen;
140478064Sume	struct mbuf *n;
140578064Sume
140678064Sume#ifdef DIAGNOSTIC
140778064Sume	switch (nxt) {
140878064Sume	case IPPROTO_DSTOPTS:
140978064Sume	case IPPROTO_ROUTING:
141078064Sume	case IPPROTO_HOPOPTS:
141178064Sume	case IPPROTO_AH: /* is it possible? */
141278064Sume		break;
141378064Sume	default:
141478064Sume		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
141553541Sshin	}
141678064Sume#endif
141778064Sume
141878064Sume	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
141978064Sume	if (nxt == IPPROTO_AH)
142078064Sume		elen = (ip6e.ip6e_len + 2) << 2;
142178064Sume	else
142278064Sume		elen = (ip6e.ip6e_len + 1) << 3;
142378064Sume
1424111119Simp	MGET(n, M_DONTWAIT, MT_DATA);
142578064Sume	if (n && elen >= MLEN) {
1426111119Simp		MCLGET(n, M_DONTWAIT);
142778064Sume		if ((n->m_flags & M_EXT) == 0) {
142878064Sume			m_free(n);
142978064Sume			n = NULL;
143078064Sume		}
143153541Sshin	}
143278064Sume	if (!n)
143378064Sume		return NULL;
143462587Sitojun
143578064Sume	n->m_len = 0;
143678064Sume	if (elen >= M_TRAILINGSPACE(n)) {
143778064Sume		m_free(n);
143878064Sume		return NULL;
143978064Sume	}
144078064Sume
144178064Sume	m_copydata(m, off, elen, mtod(n, caddr_t));
144278064Sume	n->m_len = elen;
144378064Sume	return n;
144453541Sshin}
144578064Sume#endif
144653541Sshin
144753541Sshin/*
144853541Sshin * Get pointer to the previous header followed by the header
144953541Sshin * currently processed.
145053541Sshin * XXX: This function supposes that
145153541Sshin *	M includes all headers,
145253541Sshin *	the next header field and the header length field of each header
145353541Sshin *	are valid, and
145453541Sshin *	the sum of each header length equals to OFF.
145553541Sshin * Because of these assumptions, this function must be called very
145653541Sshin * carefully. Moreover, it will not be used in the near future when
145753541Sshin * we develop `neater' mechanism to process extension headers.
145853541Sshin */
145953541Sshinchar *
146053541Sshinip6_get_prevhdr(m, off)
146153541Sshin	struct mbuf *m;
146253541Sshin	int off;
146353541Sshin{
146453541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
146553541Sshin
146653541Sshin	if (off == sizeof(struct ip6_hdr))
1467120856Sume		return (&ip6->ip6_nxt);
146853541Sshin	else {
146953541Sshin		int len, nxt;
147053541Sshin		struct ip6_ext *ip6e = NULL;
147153541Sshin
147253541Sshin		nxt = ip6->ip6_nxt;
147353541Sshin		len = sizeof(struct ip6_hdr);
147453541Sshin		while (len < off) {
147553541Sshin			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
147653541Sshin
147778064Sume			switch (nxt) {
147853541Sshin			case IPPROTO_FRAGMENT:
147953541Sshin				len += sizeof(struct ip6_frag);
148053541Sshin				break;
148153541Sshin			case IPPROTO_AH:
148253541Sshin				len += (ip6e->ip6e_len + 2) << 2;
148353541Sshin				break;
148453541Sshin			default:
148553541Sshin				len += (ip6e->ip6e_len + 1) << 3;
148653541Sshin				break;
148753541Sshin			}
148853541Sshin			nxt = ip6e->ip6e_nxt;
148953541Sshin		}
149053541Sshin		if (ip6e)
1491120856Sume			return (&ip6e->ip6e_nxt);
149253541Sshin		else
149353541Sshin			return NULL;
149453541Sshin	}
149553541Sshin}
149653541Sshin
149753541Sshin/*
149862587Sitojun * get next header offset.  m will be retained.
149962587Sitojun */
150062587Sitojunint
150162587Sitojunip6_nexthdr(m, off, proto, nxtp)
150262587Sitojun	struct mbuf *m;
150362587Sitojun	int off;
150462587Sitojun	int proto;
150562587Sitojun	int *nxtp;
150662587Sitojun{
150762587Sitojun	struct ip6_hdr ip6;
150862587Sitojun	struct ip6_ext ip6e;
150962587Sitojun	struct ip6_frag fh;
151062587Sitojun
151162587Sitojun	/* just in case */
151262587Sitojun	if (m == NULL)
151362587Sitojun		panic("ip6_nexthdr: m == NULL");
151462587Sitojun	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
151562587Sitojun		return -1;
151662587Sitojun
151762587Sitojun	switch (proto) {
151862587Sitojun	case IPPROTO_IPV6:
151962587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6))
152062587Sitojun			return -1;
152162587Sitojun		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
152262587Sitojun		if (nxtp)
152362587Sitojun			*nxtp = ip6.ip6_nxt;
152462587Sitojun		off += sizeof(ip6);
152562587Sitojun		return off;
152662587Sitojun
152762587Sitojun	case IPPROTO_FRAGMENT:
152862587Sitojun		/*
152962587Sitojun		 * terminate parsing if it is not the first fragment,
153062587Sitojun		 * it does not make sense to parse through it.
153162587Sitojun		 */
153262587Sitojun		if (m->m_pkthdr.len < off + sizeof(fh))
153362587Sitojun			return -1;
153462587Sitojun		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1535120978Sume		/* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1536120978Sume		if (fh.ip6f_offlg & IP6F_OFF_MASK)
153762587Sitojun			return -1;
153862587Sitojun		if (nxtp)
153962587Sitojun			*nxtp = fh.ip6f_nxt;
154062587Sitojun		off += sizeof(struct ip6_frag);
154162587Sitojun		return off;
154262587Sitojun
154362587Sitojun	case IPPROTO_AH:
154462587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6e))
154562587Sitojun			return -1;
154662587Sitojun		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
154762587Sitojun		if (nxtp)
154862587Sitojun			*nxtp = ip6e.ip6e_nxt;
154962587Sitojun		off += (ip6e.ip6e_len + 2) << 2;
155062587Sitojun		return off;
155162587Sitojun
155262587Sitojun	case IPPROTO_HOPOPTS:
155362587Sitojun	case IPPROTO_ROUTING:
155462587Sitojun	case IPPROTO_DSTOPTS:
155562587Sitojun		if (m->m_pkthdr.len < off + sizeof(ip6e))
155662587Sitojun			return -1;
155762587Sitojun		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
155862587Sitojun		if (nxtp)
155962587Sitojun			*nxtp = ip6e.ip6e_nxt;
156062587Sitojun		off += (ip6e.ip6e_len + 1) << 3;
156162587Sitojun		return off;
156262587Sitojun
156362587Sitojun	case IPPROTO_NONE:
156462587Sitojun	case IPPROTO_ESP:
156562587Sitojun	case IPPROTO_IPCOMP:
156662587Sitojun		/* give up */
156762587Sitojun		return -1;
156862587Sitojun
156962587Sitojun	default:
157062587Sitojun		return -1;
157162587Sitojun	}
157262587Sitojun
157362587Sitojun	return -1;
157462587Sitojun}
157562587Sitojun
157662587Sitojun/*
157762587Sitojun * get offset for the last header in the chain.  m will be kept untainted.
157862587Sitojun */
157962587Sitojunint
158062587Sitojunip6_lasthdr(m, off, proto, nxtp)
158162587Sitojun	struct mbuf *m;
158262587Sitojun	int off;
158362587Sitojun	int proto;
158462587Sitojun	int *nxtp;
158562587Sitojun{
158662587Sitojun	int newoff;
158762587Sitojun	int nxt;
158862587Sitojun
158962587Sitojun	if (!nxtp) {
159062587Sitojun		nxt = -1;
159162587Sitojun		nxtp = &nxt;
159262587Sitojun	}
159362587Sitojun	while (1) {
159462587Sitojun		newoff = ip6_nexthdr(m, off, proto, nxtp);
159562587Sitojun		if (newoff < 0)
159662587Sitojun			return off;
159762587Sitojun		else if (newoff < off)
159862587Sitojun			return -1;	/* invalid */
159962587Sitojun		else if (newoff == off)
160062587Sitojun			return newoff;
160162587Sitojun
160262587Sitojun		off = newoff;
160362587Sitojun		proto = *nxtp;
160462587Sitojun	}
160562587Sitojun}
160662587Sitojun
1607121673Sumestruct ip6aux *
160878064Sumeip6_addaux(m)
160978064Sume	struct mbuf *m;
161078064Sume{
1611120913Sume	struct m_tag *mtag;
1612120913Sume
1613120913Sume	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1614120913Sume	if (!mtag) {
1615120913Sume		mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux),
1616120913Sume		    M_NOWAIT);
1617121630Sume		if (mtag) {
1618120913Sume			m_tag_prepend(m, mtag);
1619121630Sume			bzero(mtag + 1, sizeof(struct ip6aux));
1620121630Sume		}
162178064Sume	}
1622121673Sume	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
162378064Sume}
162478064Sume
1625121673Sumestruct ip6aux *
162678064Sumeip6_findaux(m)
162778064Sume	struct mbuf *m;
162878064Sume{
1629120913Sume	struct m_tag *mtag;
1630120913Sume
1631120913Sume	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1632121673Sume	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
163378064Sume}
163478064Sume
163578064Sumevoid
163678064Sumeip6_delaux(m)
163778064Sume	struct mbuf *m;
163878064Sume{
1639120913Sume	struct m_tag *mtag;
1640120913Sume
1641120913Sume	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1642120913Sume	if (mtag)
1643120913Sume		m_tag_delete(m, mtag);
164478064Sume}
164578064Sume
164662587Sitojun/*
164753541Sshin * System control for IP6
164853541Sshin */
164953541Sshin
165053541Sshinu_char	inet6ctlerrmap[PRC_NCMDS] = {
165153541Sshin	0,		0,		0,		0,
165253541Sshin	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
165353541Sshin	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
165453541Sshin	EMSGSIZE,	EHOSTUNREACH,	0,		0,
165553541Sshin	0,		0,		0,		0,
165653541Sshin	ENOPROTOOPT
165753541Sshin};
1658