icmp6.c revision 142681
162587Sitojun/*	$FreeBSD: head/sys/netinet6/icmp6.c 142681 2005-02-27 18:57:10Z ume $	*/
278064Sume/*	$KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $	*/
362587Sitojun
4139826Simp/*-
553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
653541Sshin * All rights reserved.
753541Sshin *
853541Sshin * Redistribution and use in source and binary forms, with or without
953541Sshin * modification, are permitted provided that the following conditions
1053541Sshin * are met:
1153541Sshin * 1. Redistributions of source code must retain the above copyright
1253541Sshin *    notice, this list of conditions and the following disclaimer.
1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1453541Sshin *    notice, this list of conditions and the following disclaimer in the
1553541Sshin *    documentation and/or other materials provided with the distribution.
1653541Sshin * 3. Neither the name of the project nor the names of its contributors
1753541Sshin *    may be used to endorse or promote products derived from this software
1853541Sshin *    without specific prior written permission.
1953541Sshin *
2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2353541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3053541Sshin * SUCH DAMAGE.
3153541Sshin */
3253541Sshin
33139826Simp/*-
3453541Sshin * Copyright (c) 1982, 1986, 1988, 1993
3553541Sshin *	The Regents of the University of California.  All rights reserved.
3653541Sshin *
3753541Sshin * Redistribution and use in source and binary forms, with or without
3853541Sshin * modification, are permitted provided that the following conditions
3953541Sshin * are met:
4053541Sshin * 1. Redistributions of source code must retain the above copyright
4153541Sshin *    notice, this list of conditions and the following disclaimer.
4253541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4353541Sshin *    notice, this list of conditions and the following disclaimer in the
4453541Sshin *    documentation and/or other materials provided with the distribution.
4553541Sshin * 4. Neither the name of the University nor the names of its contributors
4653541Sshin *    may be used to endorse or promote products derived from this software
4753541Sshin *    without specific prior written permission.
4853541Sshin *
4953541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5053541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5153541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5253541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5353541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5453541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5553541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5653541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5753541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5853541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5953541Sshin * SUCH DAMAGE.
6053541Sshin *
6153541Sshin *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
6253541Sshin */
6353541Sshin
6462587Sitojun#include "opt_inet.h"
6562587Sitojun#include "opt_inet6.h"
6655009Sshin#include "opt_ipsec.h"
6753541Sshin
6853541Sshin#include <sys/param.h>
6995759Stanimura#include <sys/domain.h>
7095759Stanimura#include <sys/kernel.h>
7195759Stanimura#include <sys/lock.h>
7278064Sume#include <sys/malloc.h>
7353541Sshin#include <sys/mbuf.h>
7453541Sshin#include <sys/protosw.h>
7595759Stanimura#include <sys/signalvar.h>
7653541Sshin#include <sys/socket.h>
7753541Sshin#include <sys/socketvar.h>
7895759Stanimura#include <sys/sx.h>
7995759Stanimura#include <sys/syslog.h>
8095759Stanimura#include <sys/systm.h>
8153541Sshin#include <sys/time.h>
8253541Sshin
8353541Sshin#include <net/if.h>
8453541Sshin#include <net/if_dl.h>
8553541Sshin#include <net/if_types.h>
8695759Stanimura#include <net/route.h>
8753541Sshin
8853541Sshin#include <netinet/in.h>
8995759Stanimura#include <netinet/in_pcb.h>
9053541Sshin#include <netinet/in_var.h>
9162587Sitojun#include <netinet/ip6.h>
9295759Stanimura#include <netinet/icmp6.h>
93122922Sandre#include <netinet/tcp_var.h>
9495759Stanimura#include <netinet6/in6_ifattach.h>
9595759Stanimura#include <netinet6/in6_pcb.h>
9695759Stanimura#include <netinet6/ip6protosw.h>
9753541Sshin#include <netinet6/ip6_var.h>
9853541Sshin#include <netinet6/mld6_var.h>
9953541Sshin#include <netinet6/nd6.h>
10053541Sshin
10153541Sshin#ifdef IPSEC
10253541Sshin#include <netinet6/ipsec.h>
10353541Sshin#include <netkey/key.h>
10455009Sshin#endif
10553541Sshin
106105199Ssam#ifdef FAST_IPSEC
107105199Ssam#include <netipsec/ipsec.h>
108105199Ssam#include <netipsec/key.h>
109105199Ssam#endif
110105199Ssam
11153541Sshin#include <net/net_osdep.h>
11253541Sshin
11362587Sitojunextern struct domain inet6domain;
11453541Sshin
11562587Sitojunstruct icmp6stat icmp6stat;
11653541Sshin
117141553Srwatsonextern struct inpcbinfo ripcbinfo;
11862587Sitojunextern struct inpcbhead ripcb;
11962587Sitojunextern int icmp6errppslim;
12062587Sitojunstatic int icmp6errpps_count = 0;
12178064Sumestatic struct timeval icmp6errppslim_last;
12262587Sitojunextern int icmp6_nodeinfo;
12353541Sshin
12462587Sitojunstatic void icmp6_errcount __P((struct icmp6errstat *, int, int));
12562587Sitojunstatic int icmp6_rip6_input __P((struct mbuf **, int));
12662587Sitojunstatic int icmp6_ratelimit __P((const struct in6_addr *, const int, const int));
12762587Sitojunstatic const char *icmp6_redirect_diag __P((struct in6_addr *,
12862587Sitojun	struct in6_addr *, struct in6_addr *));
12962587Sitojunstatic struct mbuf *ni6_input __P((struct mbuf *, int));
13062587Sitojunstatic struct mbuf *ni6_nametodns __P((const char *, int, int));
13162587Sitojunstatic int ni6_dnsmatch __P((const char *, int, const char *, int));
13262587Sitojunstatic int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
13378064Sume			  struct ifnet **, char *));
13462587Sitojunstatic int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
13562587Sitojun				struct ifnet *, int));
136142681Sumestatic int icmp6_notify_error __P((struct mbuf **, int, int, int));
13753541Sshin
13853541Sshin#ifdef COMPAT_RFC1885
13962587Sitojunstatic struct route_in6 icmp6_reflect_rt;
14053541Sshin#endif
14153541Sshin
14278064Sume
14353541Sshinvoid
14453541Sshinicmp6_init()
14553541Sshin{
14653541Sshin	mld6_init();
14753541Sshin}
14853541Sshin
14962587Sitojunstatic void
15062587Sitojunicmp6_errcount(stat, type, code)
15162587Sitojun	struct icmp6errstat *stat;
15262587Sitojun	int type, code;
15362587Sitojun{
15478064Sume	switch (type) {
15562587Sitojun	case ICMP6_DST_UNREACH:
15662587Sitojun		switch (code) {
15762587Sitojun		case ICMP6_DST_UNREACH_NOROUTE:
15862587Sitojun			stat->icp6errs_dst_unreach_noroute++;
15962587Sitojun			return;
16062587Sitojun		case ICMP6_DST_UNREACH_ADMIN:
16162587Sitojun			stat->icp6errs_dst_unreach_admin++;
16262587Sitojun			return;
16362587Sitojun		case ICMP6_DST_UNREACH_BEYONDSCOPE:
16462587Sitojun			stat->icp6errs_dst_unreach_beyondscope++;
16562587Sitojun			return;
16662587Sitojun		case ICMP6_DST_UNREACH_ADDR:
16762587Sitojun			stat->icp6errs_dst_unreach_addr++;
16862587Sitojun			return;
16962587Sitojun		case ICMP6_DST_UNREACH_NOPORT:
17062587Sitojun			stat->icp6errs_dst_unreach_noport++;
17162587Sitojun			return;
17262587Sitojun		}
17362587Sitojun		break;
17462587Sitojun	case ICMP6_PACKET_TOO_BIG:
17562587Sitojun		stat->icp6errs_packet_too_big++;
17662587Sitojun		return;
17762587Sitojun	case ICMP6_TIME_EXCEEDED:
17878064Sume		switch (code) {
17962587Sitojun		case ICMP6_TIME_EXCEED_TRANSIT:
18062587Sitojun			stat->icp6errs_time_exceed_transit++;
18162587Sitojun			return;
18262587Sitojun		case ICMP6_TIME_EXCEED_REASSEMBLY:
18362587Sitojun			stat->icp6errs_time_exceed_reassembly++;
18462587Sitojun			return;
18562587Sitojun		}
18662587Sitojun		break;
18762587Sitojun	case ICMP6_PARAM_PROB:
18878064Sume		switch (code) {
18962587Sitojun		case ICMP6_PARAMPROB_HEADER:
19062587Sitojun			stat->icp6errs_paramprob_header++;
19162587Sitojun			return;
19262587Sitojun		case ICMP6_PARAMPROB_NEXTHEADER:
19362587Sitojun			stat->icp6errs_paramprob_nextheader++;
19462587Sitojun			return;
19562587Sitojun		case ICMP6_PARAMPROB_OPTION:
19662587Sitojun			stat->icp6errs_paramprob_option++;
19762587Sitojun			return;
19862587Sitojun		}
19962587Sitojun		break;
20062587Sitojun	case ND_REDIRECT:
20162587Sitojun		stat->icp6errs_redirect++;
20262587Sitojun		return;
20362587Sitojun	}
20462587Sitojun	stat->icp6errs_unknown++;
20562587Sitojun}
20662587Sitojun
20753541Sshin/*
20853541Sshin * Generate an error packet of type error in response to bad IP6 packet.
20953541Sshin */
21053541Sshinvoid
21153541Sshinicmp6_error(m, type, code, param)
21253541Sshin	struct mbuf *m;
21353541Sshin	int type, code, param;
21453541Sshin{
21553541Sshin	struct ip6_hdr *oip6, *nip6;
21653541Sshin	struct icmp6_hdr *icmp6;
21762587Sitojun	u_int preplen;
21853541Sshin	int off;
21962587Sitojun	int nxt;
22053541Sshin
22153541Sshin	icmp6stat.icp6s_error++;
22253541Sshin
22362587Sitojun	/* count per-type-code statistics */
22462587Sitojun	icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
22562587Sitojun
22662587Sitojun#ifdef M_DECRYPTED	/*not openbsd*/
22762587Sitojun	if (m->m_flags & M_DECRYPTED) {
22862587Sitojun		icmp6stat.icp6s_canterror++;
22953541Sshin		goto freeit;
23062587Sitojun	}
23162587Sitojun#endif
23253541Sshin
23362587Sitojun#ifndef PULLDOWN_TEST
23462587Sitojun	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
23562587Sitojun#else
23662587Sitojun	if (m->m_len < sizeof(struct ip6_hdr)) {
23762587Sitojun		m = m_pullup(m, sizeof(struct ip6_hdr));
23862587Sitojun		if (m == NULL)
23962587Sitojun			return;
24062587Sitojun	}
24162587Sitojun#endif
24253541Sshin	oip6 = mtod(m, struct ip6_hdr *);
24353541Sshin
24453541Sshin	/*
245121472Sume	 * If the destination address of the erroneous packet is a multicast
246121472Sume	 * address, or the packet was sent using link-layer multicast,
247121472Sume	 * we should basically suppress sending an error (RFC 2463, Section
248121472Sume	 * 2.4).
249121472Sume	 * We have two exceptions (the item e.2 in that section):
250121472Sume	 * - the Pakcet Too Big message can be sent for path MTU discovery.
251121472Sume	 * - the Parameter Problem Message that can be allowed an icmp6 error
252121472Sume	 *   in the option type field.  This check has been done in
253121472Sume	 *   ip6_unknown_opt(), so we can just check the type and code.
25453541Sshin	 */
25553541Sshin	if ((m->m_flags & (M_BCAST|M_MCAST) ||
25653541Sshin	     IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
25753541Sshin	    (type != ICMP6_PACKET_TOO_BIG &&
25853541Sshin	     (type != ICMP6_PARAM_PROB ||
25953541Sshin	      code != ICMP6_PARAMPROB_OPTION)))
26053541Sshin		goto freeit;
26153541Sshin
262121472Sume	/*
263121472Sume	 * RFC 2463, 2.4 (e.5): source address check.
264121472Sume	 * XXX: the case of anycast source?
265121472Sume	 */
26653541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
26753541Sshin	    IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
26853541Sshin		goto freeit;
26953541Sshin
27053541Sshin	/*
27162587Sitojun	 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
27262587Sitojun	 * don't do it.
27353541Sshin	 */
27462587Sitojun	nxt = -1;
27562587Sitojun	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
27662587Sitojun	if (off >= 0 && nxt == IPPROTO_ICMPV6) {
27753541Sshin		struct icmp6_hdr *icp;
27853541Sshin
27962587Sitojun#ifndef PULLDOWN_TEST
28062587Sitojun		IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
28162587Sitojun		icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
28262587Sitojun#else
28362587Sitojun		IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
28462587Sitojun			sizeof(*icp));
28562587Sitojun		if (icp == NULL) {
28662587Sitojun			icmp6stat.icp6s_tooshort++;
28762587Sitojun			return;
28862587Sitojun		}
28962587Sitojun#endif
29062587Sitojun		if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
29162587Sitojun		    icp->icmp6_type == ND_REDIRECT) {
29253541Sshin			/*
29362587Sitojun			 * ICMPv6 error
29462587Sitojun			 * Special case: for redirect (which is
29562587Sitojun			 * informational) we must not send icmp6 error.
29653541Sshin			 */
29762587Sitojun			icmp6stat.icp6s_canterror++;
29862587Sitojun			goto freeit;
29962587Sitojun		} else {
30062587Sitojun			/* ICMPv6 informational - send the error */
30153541Sshin		}
30262587Sitojun	} else {
30362587Sitojun		/* non-ICMPv6 - send the error */
30453541Sshin	}
30553541Sshin
30653541Sshin	oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
30753541Sshin
30853541Sshin	/* Finally, do rate limitation check. */
30953541Sshin	if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
31053541Sshin		icmp6stat.icp6s_toofreq++;
31153541Sshin		goto freeit;
31253541Sshin	}
31353541Sshin
31453541Sshin	/*
31553541Sshin	 * OK, ICMP6 can be generated.
31653541Sshin	 */
31753541Sshin
31853541Sshin	if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
31953541Sshin		m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
32053541Sshin
32162587Sitojun	preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
322111119Simp	M_PREPEND(m, preplen, M_DONTWAIT);
32362587Sitojun	if (m && m->m_len < preplen)
32462587Sitojun		m = m_pullup(m, preplen);
32553541Sshin	if (m == NULL) {
32678064Sume		nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
32753541Sshin		return;
32853541Sshin	}
32953541Sshin
33053541Sshin	nip6 = mtod(m, struct ip6_hdr *);
33153541Sshin	nip6->ip6_src  = oip6->ip6_src;
33253541Sshin	nip6->ip6_dst  = oip6->ip6_dst;
33353541Sshin
334121315Sume	in6_clearscope(&oip6->ip6_src);
335121315Sume	in6_clearscope(&oip6->ip6_dst);
33653541Sshin
33753541Sshin	icmp6 = (struct icmp6_hdr *)(nip6 + 1);
33853541Sshin	icmp6->icmp6_type = type;
33953541Sshin	icmp6->icmp6_code = code;
34053541Sshin	icmp6->icmp6_pptr = htonl((u_int32_t)param);
34153541Sshin
34278064Sume	/*
34378064Sume	 * icmp6_reflect() is designed to be in the input path.
34478064Sume	 * icmp6_error() can be called from both input and outut path,
34578064Sume	 * and if we are in output path rcvif could contain bogus value.
34678064Sume	 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
34778064Sume	 * information in ip header (nip6).
34878064Sume	 */
34978064Sume	m->m_pkthdr.rcvif = NULL;
35078064Sume
35153541Sshin	icmp6stat.icp6s_outhist[type]++;
35295023Ssuz	icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
35362587Sitojun
35462587Sitojun	return;
35562587Sitojun
35662587Sitojun  freeit:
35762587Sitojun	/*
35862587Sitojun	 * If we can't tell wheter or not we can generate ICMP6, free it.
35962587Sitojun	 */
36062587Sitojun	m_freem(m);
36153541Sshin}
36253541Sshin
36353541Sshin/*
36453541Sshin * Process a received ICMP6 message.
36553541Sshin */
36653541Sshinint
36753541Sshinicmp6_input(mp, offp, proto)
36853541Sshin	struct mbuf **mp;
36953541Sshin	int *offp, proto;
37053541Sshin{
37153541Sshin	struct mbuf *m = *mp, *n;
37253541Sshin	struct ip6_hdr *ip6, *nip6;
37353541Sshin	struct icmp6_hdr *icmp6, *nicmp6;
37453541Sshin	int off = *offp;
37553541Sshin	int icmp6len = m->m_pkthdr.len - *offp;
37653541Sshin	int code, sum, noff;
37753541Sshin
37862587Sitojun#ifndef PULLDOWN_TEST
37953541Sshin	IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
38095023Ssuz	/* m might change if M_LOOP.  So, call mtod after this */
38162587Sitojun#endif
38253541Sshin
38353541Sshin	/*
38453541Sshin	 * Locate icmp6 structure in mbuf, and check
38553541Sshin	 * that not corrupted and of at least minimum length
38653541Sshin	 */
38753541Sshin
38853541Sshin	ip6 = mtod(m, struct ip6_hdr *);
38953541Sshin	if (icmp6len < sizeof(struct icmp6_hdr)) {
39053541Sshin		icmp6stat.icp6s_tooshort++;
39153541Sshin		goto freeit;
39253541Sshin	}
39353541Sshin
39453541Sshin	/*
39553541Sshin	 * calculate the checksum
39653541Sshin	 */
39762587Sitojun#ifndef PULLDOWN_TEST
39853541Sshin	icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
39962587Sitojun#else
40062587Sitojun	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
40162587Sitojun	if (icmp6 == NULL) {
40262587Sitojun		icmp6stat.icp6s_tooshort++;
40362587Sitojun		return IPPROTO_DONE;
40462587Sitojun	}
40562587Sitojun#endif
40653541Sshin	code = icmp6->icmp6_code;
40753541Sshin
40853541Sshin	if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
40978064Sume		nd6log((LOG_ERR,
41053541Sshin		    "ICMP6 checksum error(%d|%x) %s\n",
41178064Sume		    icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
41253541Sshin		icmp6stat.icp6s_checksum++;
41353541Sshin		goto freeit;
41453541Sshin	}
41553541Sshin
41683934Sbrooks	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
41753541Sshin		/*
41853541Sshin		 * Deliver very specific ICMP6 type only.
41953541Sshin		 * This is important to deilver TOOBIG.  Otherwise PMTUD
42053541Sshin		 * will not work.
42153541Sshin		 */
42253541Sshin		switch (icmp6->icmp6_type) {
42353541Sshin		case ICMP6_DST_UNREACH:
42453541Sshin		case ICMP6_PACKET_TOO_BIG:
42553541Sshin		case ICMP6_TIME_EXCEEDED:
42653541Sshin			break;
42753541Sshin		default:
42853541Sshin			goto freeit;
42953541Sshin		}
43053541Sshin	}
43153541Sshin
43253541Sshin	icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
43353541Sshin	icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
43453541Sshin	if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
43553541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
43653541Sshin
43753541Sshin	switch (icmp6->icmp6_type) {
43853541Sshin	case ICMP6_DST_UNREACH:
43953541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
44053541Sshin		switch (code) {
44153541Sshin		case ICMP6_DST_UNREACH_NOROUTE:
44253541Sshin			code = PRC_UNREACH_NET;
44353541Sshin			break;
44453541Sshin		case ICMP6_DST_UNREACH_ADMIN:
44553541Sshin			icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
44662587Sitojun			code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
44762587Sitojun			break;
44853541Sshin		case ICMP6_DST_UNREACH_ADDR:
44962587Sitojun			code = PRC_HOSTDEAD;
45053541Sshin			break;
45162587Sitojun#ifdef COMPAT_RFC1885
45253541Sshin		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
45353541Sshin			code = PRC_UNREACH_SRCFAIL;
45453541Sshin			break;
45562587Sitojun#else
45662587Sitojun		case ICMP6_DST_UNREACH_BEYONDSCOPE:
45762587Sitojun			/* I mean "source address was incorrect." */
45862587Sitojun			code = PRC_PARAMPROB;
45962587Sitojun			break;
46062587Sitojun#endif
46153541Sshin		case ICMP6_DST_UNREACH_NOPORT:
46253541Sshin			code = PRC_UNREACH_PORT;
46353541Sshin			break;
46453541Sshin		default:
46553541Sshin			goto badcode;
46653541Sshin		}
46753541Sshin		goto deliver;
46853541Sshin		break;
46953541Sshin
47053541Sshin	case ICMP6_PACKET_TOO_BIG:
47153541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
47253541Sshin		if (code != 0)
47353541Sshin			goto badcode;
47453541Sshin
475126194Sume		/* validation is made in icmp6_mtudisc_update */
476126194Sume
47753541Sshin		code = PRC_MSGSIZE;
47853541Sshin
47962587Sitojun		/*
48062587Sitojun		 * Updating the path MTU will be done after examining
48162587Sitojun		 * intermediate extension headers.
48262587Sitojun		 */
48353541Sshin		goto deliver;
48453541Sshin		break;
48553541Sshin
48653541Sshin	case ICMP6_TIME_EXCEEDED:
48753541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
48853541Sshin		switch (code) {
48953541Sshin		case ICMP6_TIME_EXCEED_TRANSIT:
49053541Sshin		case ICMP6_TIME_EXCEED_REASSEMBLY:
49153541Sshin			code += PRC_TIMXCEED_INTRANS;
49253541Sshin			break;
49353541Sshin		default:
49453541Sshin			goto badcode;
49553541Sshin		}
49653541Sshin		goto deliver;
49753541Sshin		break;
49853541Sshin
49953541Sshin	case ICMP6_PARAM_PROB:
50053541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
50153541Sshin		switch (code) {
50253541Sshin		case ICMP6_PARAMPROB_NEXTHEADER:
50353541Sshin			code = PRC_UNREACH_PROTOCOL;
50453541Sshin			break;
50553541Sshin		case ICMP6_PARAMPROB_HEADER:
50653541Sshin		case ICMP6_PARAMPROB_OPTION:
50753541Sshin			code = PRC_PARAMPROB;
50853541Sshin			break;
50953541Sshin		default:
51053541Sshin			goto badcode;
51153541Sshin		}
51253541Sshin		goto deliver;
51353541Sshin		break;
51453541Sshin
51553541Sshin	case ICMP6_ECHO_REQUEST:
51653541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
51753541Sshin		if (code != 0)
51853541Sshin			goto badcode;
51953541Sshin		if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
52053541Sshin			/* Give up remote */
52153541Sshin			break;
52253541Sshin		}
52362587Sitojun		if ((n->m_flags & M_EXT) != 0
52462587Sitojun		 || n->m_len < off + sizeof(struct icmp6_hdr)) {
52553541Sshin			struct mbuf *n0 = n;
52662587Sitojun			const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
527108741Ssam			int n0len;
52853541Sshin
52953541Sshin			/*
530120891Sume			 * Prepare an internal mbuf.  m_pullup() doesn't
53153541Sshin			 * always copy the length we specified.
53253541Sshin			 */
53362587Sitojun			if (maxlen >= MCLBYTES) {
53462587Sitojun				/* Give up remote */
53562587Sitojun				m_freem(n0);
53662587Sitojun				break;
53762587Sitojun			}
538111119Simp			MGETHDR(n, M_DONTWAIT, n0->m_type);
539111070Ssam			n0len = n0->m_pkthdr.len;	/* save for use below */
540111070Ssam			if (n)
541111070Ssam				M_MOVE_PKTHDR(n, n0);
54262587Sitojun			if (n && maxlen >= MHLEN) {
543111119Simp				MCLGET(n, M_DONTWAIT);
54462587Sitojun				if ((n->m_flags & M_EXT) == 0) {
54562587Sitojun					m_free(n);
54662587Sitojun					n = NULL;
54762587Sitojun				}
54862587Sitojun			}
54953541Sshin			if (n == NULL) {
55053541Sshin				/* Give up remote */
55153541Sshin				m_freem(n0);
55253541Sshin				break;
55353541Sshin			}
55453541Sshin			/*
55553541Sshin			 * Copy IPv6 and ICMPv6 only.
55653541Sshin			 */
55753541Sshin			nip6 = mtod(n, struct ip6_hdr *);
55853541Sshin			bcopy(ip6, nip6, sizeof(struct ip6_hdr));
55953541Sshin			nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
56053541Sshin			bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
56162587Sitojun			noff = sizeof(struct ip6_hdr);
562108741Ssam			/* new mbuf contains only ipv6+icmpv6 headers */
563108741Ssam			n->m_len = noff + sizeof(struct icmp6_hdr);
56453541Sshin			/*
565120891Sume			 * Adjust mbuf.  ip6_plen will be adjusted in
56662587Sitojun			 * ip6_output().
56753541Sshin			 */
56862587Sitojun			m_adj(n0, off + sizeof(struct icmp6_hdr));
569108741Ssam			/* recalculate complete packet size */
570108741Ssam			n->m_pkthdr.len = n0len + (noff - off);
57162587Sitojun			n->m_next = n0;
57253541Sshin		} else {
57353541Sshin			nip6 = mtod(n, struct ip6_hdr *);
57453541Sshin			nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off);
57553541Sshin			noff = off;
57653541Sshin		}
57753541Sshin		nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
57853541Sshin		nicmp6->icmp6_code = 0;
57953541Sshin		if (n) {
58053541Sshin			icmp6stat.icp6s_reflect++;
58153541Sshin			icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
58253541Sshin			icmp6_reflect(n, noff);
58353541Sshin		}
58453541Sshin		break;
58553541Sshin
58653541Sshin	case ICMP6_ECHO_REPLY:
58753541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
58853541Sshin		if (code != 0)
58953541Sshin			goto badcode;
59053541Sshin		break;
59153541Sshin
59296116Sume	case MLD_LISTENER_QUERY:
59396116Sume	case MLD_LISTENER_REPORT:
59496116Sume		if (icmp6len < sizeof(struct mld_hdr))
59553541Sshin			goto badlen;
59696116Sume		if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
59753541Sshin			icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
59853541Sshin		else
59953541Sshin			icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
600111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
60162587Sitojun			/* give up local */
60262587Sitojun			mld6_input(m, off);
60362587Sitojun			m = NULL;
60462587Sitojun			goto freeit;
60562587Sitojun		}
60662587Sitojun		mld6_input(n, off);
60753541Sshin		/* m stays. */
60853541Sshin		break;
60953541Sshin
61096116Sume	case MLD_LISTENER_DONE:
61153541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
61296116Sume		if (icmp6len < sizeof(struct mld_hdr))	/* necessary? */
61353541Sshin			goto badlen;
61453541Sshin		break;		/* nothing to be done in kernel */
61553541Sshin
61696116Sume	case MLD_MTRACE_RESP:
61796116Sume	case MLD_MTRACE:
618120891Sume		/* XXX: these two are experimental.  not officially defined. */
61953541Sshin		/* XXX: per-interface statistics? */
62062587Sitojun		break;		/* just pass it to applications */
62153541Sshin
62253541Sshin	case ICMP6_WRUREQUEST:	/* ICMP6_FQDN_QUERY */
62353541Sshin	    {
62453541Sshin		enum { WRU, FQDN } mode;
62553541Sshin
62662587Sitojun		if (!icmp6_nodeinfo)
62762587Sitojun			break;
62862587Sitojun
62953541Sshin		if (icmp6len == sizeof(struct icmp6_hdr) + 4)
63053541Sshin			mode = WRU;
63162587Sitojun		else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
63253541Sshin			mode = FQDN;
63353541Sshin		else
63453541Sshin			goto badlen;
63553541Sshin
63653541Sshin#define hostnamelen	strlen(hostname)
63753541Sshin		if (mode == FQDN) {
63862587Sitojun#ifndef PULLDOWN_TEST
63953541Sshin			IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
640120891Sume			    IPPROTO_DONE);
64162587Sitojun#endif
64262587Sitojun			n = m_copy(m, 0, M_COPYALL);
64362587Sitojun			if (n)
64462587Sitojun				n = ni6_input(n, off);
64562587Sitojun			/* XXX meaningless if n == NULL */
64653541Sshin			noff = sizeof(struct ip6_hdr);
64753541Sshin		} else {
64853541Sshin			u_char *p;
64962587Sitojun			int maxlen, maxhlen;
65053541Sshin
651120891Sume			if ((icmp6_nodeinfo & 5) != 5)
65278064Sume				break;
65378064Sume
65462587Sitojun			if (code != 0)
65562587Sitojun				goto badcode;
65662587Sitojun			maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
65762587Sitojun			if (maxlen >= MCLBYTES) {
65862587Sitojun				/* Give up remote */
65962587Sitojun				break;
66062587Sitojun			}
661111119Simp			MGETHDR(n, M_DONTWAIT, m->m_type);
66262587Sitojun			if (n && maxlen > MHLEN) {
663111119Simp				MCLGET(n, M_DONTWAIT);
66462587Sitojun				if ((n->m_flags & M_EXT) == 0) {
66562587Sitojun					m_free(n);
66662587Sitojun					n = NULL;
66762587Sitojun				}
66862587Sitojun			}
669111119Simp			if (!m_dup_pkthdr(n, m, M_DONTWAIT)) {
670108466Ssam				/*
671108466Ssam				 * Previous code did a blind M_COPY_PKTHDR
672108466Ssam				 * and said "just for rcvif".  If true, then
673108466Ssam				 * we could tolerate the dup failing (due to
674108466Ssam				 * the deep copy of the tag chain).  For now
675108466Ssam				 * be conservative and just fail.
676108466Ssam				 */
677108466Ssam				m_free(n);
678108466Ssam				n = NULL;
679108466Ssam			}
68053541Sshin			if (n == NULL) {
68153541Sshin				/* Give up remote */
68253541Sshin				break;
68353541Sshin			}
68478064Sume			n->m_pkthdr.rcvif = NULL;
68562587Sitojun			n->m_len = 0;
68662587Sitojun			maxhlen = M_TRAILINGSPACE(n) - maxlen;
68762587Sitojun			if (maxhlen > hostnamelen)
68862587Sitojun				maxhlen = hostnamelen;
68953541Sshin			/*
69053541Sshin			 * Copy IPv6 and ICMPv6 only.
69153541Sshin			 */
69253541Sshin			nip6 = mtod(n, struct ip6_hdr *);
69353541Sshin			bcopy(ip6, nip6, sizeof(struct ip6_hdr));
69453541Sshin			nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
69553541Sshin			bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
69653541Sshin			p = (u_char *)(nicmp6 + 1);
69753541Sshin			bzero(p, 4);
69895023Ssuz			bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
69953541Sshin			noff = sizeof(struct ip6_hdr);
70053541Sshin			n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
70162587Sitojun				sizeof(struct icmp6_hdr) + 4 + maxhlen;
70253541Sshin			nicmp6->icmp6_type = ICMP6_WRUREPLY;
70353541Sshin			nicmp6->icmp6_code = 0;
70453541Sshin		}
70553541Sshin#undef hostnamelen
70653541Sshin		if (n) {
70753541Sshin			icmp6stat.icp6s_reflect++;
70853541Sshin			icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
70953541Sshin			icmp6_reflect(n, noff);
71053541Sshin		}
71153541Sshin		break;
71253541Sshin	    }
71353541Sshin
71453541Sshin	case ICMP6_WRUREPLY:
71553541Sshin		if (code != 0)
71653541Sshin			goto badcode;
71753541Sshin		break;
71853541Sshin
71953541Sshin	case ND_ROUTER_SOLICIT:
72053541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
72153541Sshin		if (code != 0)
72253541Sshin			goto badcode;
72353541Sshin		if (icmp6len < sizeof(struct nd_router_solicit))
72453541Sshin			goto badlen;
725111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
72662587Sitojun			/* give up local */
72762587Sitojun			nd6_rs_input(m, off, icmp6len);
72862587Sitojun			m = NULL;
72962587Sitojun			goto freeit;
73062587Sitojun		}
73162587Sitojun		nd6_rs_input(n, off, icmp6len);
73253541Sshin		/* m stays. */
73353541Sshin		break;
73453541Sshin
73553541Sshin	case ND_ROUTER_ADVERT:
73653541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
73753541Sshin		if (code != 0)
73853541Sshin			goto badcode;
73953541Sshin		if (icmp6len < sizeof(struct nd_router_advert))
74053541Sshin			goto badlen;
741111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
74262587Sitojun			/* give up local */
74362587Sitojun			nd6_ra_input(m, off, icmp6len);
74462587Sitojun			m = NULL;
74562587Sitojun			goto freeit;
74662587Sitojun		}
74762587Sitojun		nd6_ra_input(n, off, icmp6len);
74853541Sshin		/* m stays. */
74953541Sshin		break;
75053541Sshin
75153541Sshin	case ND_NEIGHBOR_SOLICIT:
75253541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
75353541Sshin		if (code != 0)
75453541Sshin			goto badcode;
75553541Sshin		if (icmp6len < sizeof(struct nd_neighbor_solicit))
75653541Sshin			goto badlen;
757111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
75862587Sitojun			/* give up local */
75962587Sitojun			nd6_ns_input(m, off, icmp6len);
76062587Sitojun			m = NULL;
76162587Sitojun			goto freeit;
76262587Sitojun		}
76362587Sitojun		nd6_ns_input(n, off, icmp6len);
76453541Sshin		/* m stays. */
76553541Sshin		break;
76653541Sshin
76753541Sshin	case ND_NEIGHBOR_ADVERT:
76853541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
76953541Sshin		if (code != 0)
77053541Sshin			goto badcode;
77153541Sshin		if (icmp6len < sizeof(struct nd_neighbor_advert))
77253541Sshin			goto badlen;
773111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
77462587Sitojun			/* give up local */
77562587Sitojun			nd6_na_input(m, off, icmp6len);
77662587Sitojun			m = NULL;
77762587Sitojun			goto freeit;
77862587Sitojun		}
77962587Sitojun		nd6_na_input(n, off, icmp6len);
78053541Sshin		/* m stays. */
78153541Sshin		break;
78253541Sshin
78353541Sshin	case ND_REDIRECT:
78453541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
78553541Sshin		if (code != 0)
78653541Sshin			goto badcode;
78753541Sshin		if (icmp6len < sizeof(struct nd_redirect))
78853541Sshin			goto badlen;
789111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
79062587Sitojun			/* give up local */
79162587Sitojun			icmp6_redirect_input(m, off);
79262587Sitojun			m = NULL;
79362587Sitojun			goto freeit;
79462587Sitojun		}
79562587Sitojun		icmp6_redirect_input(n, off);
79653541Sshin		/* m stays. */
79753541Sshin		break;
79853541Sshin
79953541Sshin	case ICMP6_ROUTER_RENUMBERING:
80053541Sshin		if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
80153541Sshin		    code != ICMP6_ROUTER_RENUMBERING_RESULT)
80253541Sshin			goto badcode;
80353541Sshin		if (icmp6len < sizeof(struct icmp6_router_renum))
80453541Sshin			goto badlen;
80553541Sshin		break;
80653541Sshin
80753541Sshin	default:
80878064Sume		nd6log((LOG_DEBUG,
80978064Sume		    "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
81078064Sume		    icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
81178064Sume		    ip6_sprintf(&ip6->ip6_dst),
81278064Sume		    m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
81353541Sshin		if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
81453541Sshin			/* ICMPv6 error: MUST deliver it by spec... */
81553541Sshin			code = PRC_NCMDS;
81653541Sshin			/* deliver */
81753541Sshin		} else {
81853541Sshin			/* ICMPv6 informational: MUST not deliver */
81953541Sshin			break;
82053541Sshin		}
82153541Sshin	deliver:
822142681Sume		if (icmp6_notify_error(&m, off, icmp6len, code)) {
82378064Sume			/* In this case, m should've been freed. */
824120856Sume			return (IPPROTO_DONE);
82553541Sshin		}
82678064Sume		break;
82778064Sume
82878064Sume	badcode:
82978064Sume		icmp6stat.icp6s_badcode++;
83078064Sume		break;
83178064Sume
83278064Sume	badlen:
83378064Sume		icmp6stat.icp6s_badlen++;
83478064Sume		break;
83578064Sume	}
83678064Sume
83778064Sume	/* deliver the packet to appropriate sockets */
83878064Sume	icmp6_rip6_input(&m, *offp);
83978064Sume
84078064Sume	return IPPROTO_DONE;
84178064Sume
84278064Sume freeit:
84378064Sume	m_freem(m);
84478064Sume	return IPPROTO_DONE;
84578064Sume}
84678064Sume
84778064Sumestatic int
848142681Sumeicmp6_notify_error(mp, off, icmp6len, code)
849142681Sume	struct mbuf **mp;
850113799Sobrien	int off, icmp6len, code;
85178064Sume{
852142681Sume	struct mbuf *m = *mp;
85378064Sume	struct icmp6_hdr *icmp6;
85478064Sume	struct ip6_hdr *eip6;
85578064Sume	u_int32_t notifymtu;
85678064Sume	struct sockaddr_in6 icmp6src, icmp6dst;
85778064Sume
85878064Sume	if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
85978064Sume		icmp6stat.icp6s_tooshort++;
86078064Sume		goto freeit;
86178064Sume	}
86262587Sitojun#ifndef PULLDOWN_TEST
86378064Sume	IP6_EXTHDR_CHECK(m, off,
864120891Sume	    sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1);
86578064Sume	icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
86662587Sitojun#else
86778064Sume	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
868120891Sume	    sizeof(*icmp6) + sizeof(struct ip6_hdr));
86978064Sume	if (icmp6 == NULL) {
87078064Sume		icmp6stat.icp6s_tooshort++;
871120856Sume		return (-1);
87278064Sume	}
87362587Sitojun#endif
87478064Sume	eip6 = (struct ip6_hdr *)(icmp6 + 1);
87553541Sshin
87678064Sume	/* Detect the upper level protocol */
87778064Sume	{
87853541Sshin		void (*ctlfunc) __P((int, struct sockaddr *, void *));
87953541Sshin		u_int8_t nxt = eip6->ip6_nxt;
88053541Sshin		int eoff = off + sizeof(struct icmp6_hdr) +
881120891Sume		    sizeof(struct ip6_hdr);
88253541Sshin		struct ip6ctlparam ip6cp;
88362587Sitojun		struct in6_addr *finaldst = NULL;
88462587Sitojun		int icmp6type = icmp6->icmp6_type;
88562587Sitojun		struct ip6_frag *fh;
88662587Sitojun		struct ip6_rthdr *rth;
88762587Sitojun		struct ip6_rthdr0 *rth0;
88862587Sitojun		int rthlen;
88953541Sshin
89095023Ssuz		while (1) { /* XXX: should avoid infinite loop explicitly? */
89153541Sshin			struct ip6_ext *eh;
89253541Sshin
89378064Sume			switch (nxt) {
89453541Sshin			case IPPROTO_HOPOPTS:
89553541Sshin			case IPPROTO_DSTOPTS:
89653541Sshin			case IPPROTO_AH:
89762587Sitojun#ifndef PULLDOWN_TEST
898120891Sume				IP6_EXTHDR_CHECK(m, 0,
899120891Sume				    eoff + sizeof(struct ip6_ext), -1);
900120892Sume				eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff);
90162587Sitojun#else
90262587Sitojun				IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
903120891Sume				    eoff, sizeof(*eh));
90462587Sitojun				if (eh == NULL) {
90562587Sitojun					icmp6stat.icp6s_tooshort++;
906120856Sume					return (-1);
90762587Sitojun				}
90862587Sitojun#endif
909120891Sume
91053541Sshin				if (nxt == IPPROTO_AH)
91153541Sshin					eoff += (eh->ip6e_len + 2) << 2;
91253541Sshin				else
91353541Sshin					eoff += (eh->ip6e_len + 1) << 3;
91453541Sshin				nxt = eh->ip6e_nxt;
91553541Sshin				break;
91662587Sitojun			case IPPROTO_ROUTING:
91762587Sitojun				/*
91862587Sitojun				 * When the erroneous packet contains a
91962587Sitojun				 * routing header, we should examine the
92062587Sitojun				 * header to determine the final destination.
92162587Sitojun				 * Otherwise, we can't properly update
92262587Sitojun				 * information that depends on the final
92362587Sitojun				 * destination (e.g. path MTU).
92462587Sitojun				 */
92562587Sitojun#ifndef PULLDOWN_TEST
926120891Sume				IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1);
927120891Sume				rth = (struct ip6_rthdr *)
928120891Sume				    (mtod(m, caddr_t) + eoff);
92962587Sitojun#else
93062587Sitojun				IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
931120891Sume				    eoff, sizeof(*rth));
93262587Sitojun				if (rth == NULL) {
93362587Sitojun					icmp6stat.icp6s_tooshort++;
934120856Sume					return (-1);
93562587Sitojun				}
93662587Sitojun#endif
93762587Sitojun				rthlen = (rth->ip6r_len + 1) << 3;
93862587Sitojun				/*
93962587Sitojun				 * XXX: currently there is no
94062587Sitojun				 * officially defined type other
94162587Sitojun				 * than type-0.
94262587Sitojun				 * Note that if the segment left field
94362587Sitojun				 * is 0, all intermediate hops must
94462587Sitojun				 * have been passed.
94562587Sitojun				 */
94662587Sitojun				if (rth->ip6r_segleft &&
94762587Sitojun				    rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
94862587Sitojun					int hops;
94962587Sitojun
95062587Sitojun#ifndef PULLDOWN_TEST
951120891Sume					IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1);
952120891Sume					rth0 = (struct ip6_rthdr0 *)
953120891Sume					    (mtod(m, caddr_t) + eoff);
95462587Sitojun#else
95562587Sitojun					IP6_EXTHDR_GET(rth0,
956120891Sume					    struct ip6_rthdr0 *, m,
957120891Sume					    eoff, rthlen);
95862587Sitojun					if (rth0 == NULL) {
95962587Sitojun						icmp6stat.icp6s_tooshort++;
960120856Sume						return (-1);
96162587Sitojun					}
96262587Sitojun#endif
96362587Sitojun					/* just ignore a bogus header */
96462587Sitojun					if ((rth0->ip6r0_len % 2) == 0 &&
96562587Sitojun					    (hops = rth0->ip6r0_len/2))
96662587Sitojun						finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
96762587Sitojun				}
96862587Sitojun				eoff += rthlen;
96962587Sitojun				nxt = rth->ip6r_nxt;
97062587Sitojun				break;
97162587Sitojun			case IPPROTO_FRAGMENT:
97262587Sitojun#ifndef PULLDOWN_TEST
97362587Sitojun				IP6_EXTHDR_CHECK(m, 0, eoff +
974120891Sume				    sizeof(struct ip6_frag), -1);
975120891Sume				fh = (struct ip6_frag *)(mtod(m, caddr_t) +
976120891Sume				    eoff);
97762587Sitojun#else
97862587Sitojun				IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
979120891Sume				    eoff, sizeof(*fh));
98062587Sitojun				if (fh == NULL) {
98162587Sitojun					icmp6stat.icp6s_tooshort++;
982120856Sume					return (-1);
98362587Sitojun				}
98462587Sitojun#endif
98562587Sitojun				/*
98662587Sitojun				 * Data after a fragment header is meaningless
98762587Sitojun				 * unless it is the first fragment, but
98862587Sitojun				 * we'll go to the notify label for path MTU
98962587Sitojun				 * discovery.
99062587Sitojun				 */
99162587Sitojun				if (fh->ip6f_offlg & IP6F_OFF_MASK)
99262587Sitojun					goto notify;
99362587Sitojun
99462587Sitojun				eoff += sizeof(struct ip6_frag);
99562587Sitojun				nxt = fh->ip6f_nxt;
99662587Sitojun				break;
99753541Sshin			default:
99862587Sitojun				/*
99962587Sitojun				 * This case includes ESP and the No Next
100095023Ssuz				 * Header.  In such cases going to the notify
100162587Sitojun				 * label does not have any meaning
100262587Sitojun				 * (i.e. ctlfunc will be NULL), but we go
100362587Sitojun				 * anyway since we might have to update
100462587Sitojun				 * path MTU information.
100562587Sitojun				 */
100653541Sshin				goto notify;
100753541Sshin			}
100853541Sshin		}
100978064Sume	  notify:
101062587Sitojun#ifndef PULLDOWN_TEST
101153541Sshin		icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
101262587Sitojun#else
101362587Sitojun		IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1014120891Sume		    sizeof(*icmp6) + sizeof(struct ip6_hdr));
101562587Sitojun		if (icmp6 == NULL) {
101662587Sitojun			icmp6stat.icp6s_tooshort++;
1017120856Sume			return (-1);
101862587Sitojun		}
101962587Sitojun#endif
102078064Sume
1021120891Sume		/*
1022120891Sume		 * retrieve parameters from the inner IPv6 header, and convert
1023120891Sume		 * them into sockaddr structures.
1024120891Sume		 * XXX: there is no guarantee that the source or destination
1025120891Sume		 * addresses of the inner packet are in the same scope as
1026120891Sume		 * the addresses of the icmp packet.  But there is no other
1027120891Sume		 * way to determine the zone.
1028120891Sume		 */
102978064Sume		eip6 = (struct ip6_hdr *)(icmp6 + 1);
1030120891Sume
103178064Sume		bzero(&icmp6dst, sizeof(icmp6dst));
103278064Sume		icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
103378064Sume		icmp6dst.sin6_family = AF_INET6;
103478064Sume		if (finaldst == NULL)
103578064Sume			icmp6dst.sin6_addr = eip6->ip6_dst;
103678064Sume		else
103778064Sume			icmp6dst.sin6_addr = *finaldst;
1038121315Sume		if (in6_addr2zoneid(m->m_pkthdr.rcvif, &icmp6dst.sin6_addr,
1039121315Sume		    &icmp6dst.sin6_scope_id))
1040121315Sume			goto freeit;
104178064Sume		if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst,
104278064Sume				   NULL, NULL)) {
1043142679Sume			/* should be impossible */
104478064Sume			nd6log((LOG_DEBUG,
104578064Sume			    "icmp6_notify_error: in6_embedscope failed\n"));
104678064Sume			goto freeit;
104778064Sume		}
104878064Sume
104978064Sume		/*
105078064Sume		 * retrieve parameters from the inner IPv6 header, and convert
105178064Sume		 * them into sockaddr structures.
105278064Sume		 */
105378064Sume		bzero(&icmp6src, sizeof(icmp6src));
105478064Sume		icmp6src.sin6_len = sizeof(struct sockaddr_in6);
105578064Sume		icmp6src.sin6_family = AF_INET6;
105678064Sume		icmp6src.sin6_addr = eip6->ip6_src;
1057121315Sume		if (in6_addr2zoneid(m->m_pkthdr.rcvif, &icmp6src.sin6_addr,
1058121315Sume		    &icmp6src.sin6_scope_id)) {
1059121315Sume			goto freeit;
1060121315Sume		}
106178064Sume		if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src,
106278064Sume				   NULL, NULL)) {
1063142679Sume			/* should be impossible */
106478064Sume			nd6log((LOG_DEBUG,
106578064Sume			    "icmp6_notify_error: in6_embedscope failed\n"));
106678064Sume			goto freeit;
106778064Sume		}
1068120891Sume		icmp6src.sin6_flowinfo = (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
106978064Sume
107078064Sume		if (finaldst == NULL)
107178064Sume			finaldst = &eip6->ip6_dst;
107278064Sume		ip6cp.ip6c_m = m;
107378064Sume		ip6cp.ip6c_icmp6 = icmp6;
107478064Sume		ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
107578064Sume		ip6cp.ip6c_off = eoff;
107678064Sume		ip6cp.ip6c_finaldst = finaldst;
107778064Sume		ip6cp.ip6c_src = &icmp6src;
107878064Sume		ip6cp.ip6c_nxt = nxt;
107978064Sume
108062587Sitojun		if (icmp6type == ICMP6_PACKET_TOO_BIG) {
108178064Sume			notifymtu = ntohl(icmp6->icmp6_mtu);
108278064Sume			ip6cp.ip6c_cmdarg = (void *)&notifymtu;
108378064Sume			icmp6_mtudisc_update(&ip6cp, 1);	/*XXX*/
108462587Sitojun		}
108562587Sitojun
108653541Sshin		ctlfunc = (void (*) __P((int, struct sockaddr *, void *)))
1087120891Sume		    (inet6sw[ip6_protox[nxt]].pr_ctlinput);
108853541Sshin		if (ctlfunc) {
108978064Sume			(void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1090120891Sume			    &ip6cp);
109153541Sshin		}
109253541Sshin	}
1093142681Sume	*mp = m;
1094120856Sume	return (0);
109553541Sshin
109678064Sume  freeit:
109753541Sshin	m_freem(m);
1098120856Sume	return (-1);
109953541Sshin}
110053541Sshin
110178064Sumevoid
110278064Sumeicmp6_mtudisc_update(ip6cp, validated)
110378064Sume	struct ip6ctlparam *ip6cp;
110478064Sume	int validated;
110562587Sitojun{
110678064Sume	struct in6_addr *dst = ip6cp->ip6c_finaldst;
110778064Sume	struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
110878064Sume	struct mbuf *m = ip6cp->ip6c_m;	/* will be necessary for scope issue */
110962587Sitojun	u_int mtu = ntohl(icmp6->icmp6_mtu);
1110122922Sandre	struct in_conninfo inc;
111162587Sitojun
1112121472Sume#if 0
1113121472Sume	/*
1114121472Sume	 * RFC2460 section 5, last paragraph.
1115121472Sume	 * even though minimum link MTU for IPv6 is IPV6_MMTU,
1116121472Sume	 * we may see ICMPv6 too big with mtu < IPV6_MMTU
1117121472Sume	 * due to packet translator in the middle.
1118121472Sume	 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for
1119121472Sume	 * special handling.
1120121472Sume	 */
1121121472Sume	if (mtu < IPV6_MMTU)
1122121472Sume		return;
1123121472Sume#endif
1124121472Sume
1125121472Sume	/*
1126121472Sume	 * we reject ICMPv6 too big with abnormally small value.
1127121472Sume	 * XXX what is the good definition of "abnormally small"?
1128121472Sume	 */
1129121472Sume	if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
1130121472Sume		return;
1131121472Sume
113278064Sume	if (!validated)
113378064Sume		return;
113478064Sume
1135122922Sandre	bzero(&inc, sizeof(inc));
1136122922Sandre	inc.inc_flags = 1; /* IPv6 */
1137122922Sandre	inc.inc6_faddr = *dst;
113878064Sume	/* XXX normally, this won't happen */
113978064Sume	if (IN6_IS_ADDR_LINKLOCAL(dst)) {
1140122922Sandre		inc.inc6_faddr.s6_addr16[1] =
114178064Sume		    htons(m->m_pkthdr.rcvif->if_index);
114278064Sume	}
114362587Sitojun
1144126194Sume	if (mtu < tcp_maxmtu6(&inc)) {
1145122922Sandre		tcp_hc_updatemtu(&inc, mtu);
1146122922Sandre		icmp6stat.icp6s_pmtuchg++;
114762587Sitojun	}
114862587Sitojun}
114962587Sitojun
115053541Sshin/*
115178064Sume * Process a Node Information Query packet, based on
115278064Sume * draft-ietf-ipngwg-icmp-name-lookups-07.
1153120891Sume *
115462587Sitojun * Spec incompatibilities:
115562587Sitojun * - IPv6 Subject address handling
115662587Sitojun * - IPv4 Subject address handling support missing
115762587Sitojun * - Proxy reply (answer even if it's not for me)
115862587Sitojun * - joins NI group address at in6_ifattach() time only, does not cope
115962587Sitojun *   with hostname changes by sethostname(3)
116053541Sshin */
116162587Sitojun#define hostnamelen	strlen(hostname)
116253541Sshinstatic struct mbuf *
116353541Sshinni6_input(m, off)
116453541Sshin	struct mbuf *m;
116553541Sshin	int off;
116653541Sshin{
116762587Sitojun	struct icmp6_nodeinfo *ni6, *nni6;
116853541Sshin	struct mbuf *n = NULL;
116962587Sitojun	u_int16_t qtype;
117062587Sitojun	int subjlen;
117153541Sshin	int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
117253541Sshin	struct ni_reply_fqdn *fqdn;
117353541Sshin	int addrs;		/* for NI_QTYPE_NODEADDR */
117453541Sshin	struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1175121630Sume	struct sockaddr_in6 sin6_sbj; /* subject address */
117662587Sitojun	struct ip6_hdr *ip6;
117762587Sitojun	int oldfqdn = 0;	/* if 1, return pascal string (03 draft) */
117878064Sume	char *subj = NULL;
117978064Sume	struct in6_ifaddr *ia6 = NULL;
118053541Sshin
118162587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
118262587Sitojun#ifndef PULLDOWN_TEST
118362587Sitojun	ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
118462587Sitojun#else
118562587Sitojun	IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
118662587Sitojun	if (ni6 == NULL) {
118762587Sitojun		/* m is already reclaimed */
1188120891Sume		return (NULL);
118962587Sitojun	}
119062587Sitojun#endif
119153541Sshin
119262587Sitojun	/*
119362587Sitojun	 * Validate IPv6 destination address.
119462587Sitojun	 *
119578064Sume	 * The Responder must discard the Query without further processing
119678064Sume	 * unless it is one of the Responder's unicast or anycast addresses, or
119778064Sume	 * a link-local scope multicast address which the Responder has joined.
1198121630Sume	 * [icmp-name-lookups-08, Section 4.]
119962587Sitojun	 */
1200121630Sume	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1201121630Sume		if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1202121630Sume			goto bad;
1203121630Sume		/* else it's a link-local multicast, fine */
1204121630Sume	} else {		/* unicast or anycast */
1205121630Sume		if ((ia6 = ip6_getdstifaddr(m)) == NULL)
1206121630Sume			goto bad; /* XXX impossible */
1207121630Sume
1208121630Sume		if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1209121630Sume		    !(icmp6_nodeinfo & 4)) {
121078064Sume			nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
121178064Sume				"a temporary address in %s:%d",
121278064Sume			       __FILE__, __LINE__));
121378064Sume			goto bad;
121478064Sume		}
1215121630Sume	}
121662587Sitojun
121778064Sume	/* validate query Subject field. */
121862587Sitojun	qtype = ntohs(ni6->ni_qtype);
121962587Sitojun	subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
122062587Sitojun	switch (qtype) {
122162587Sitojun	case NI_QTYPE_NOOP:
122262587Sitojun	case NI_QTYPE_SUPTYPES:
122378064Sume		/* 07 draft */
122478064Sume		if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
122578064Sume			break;
122678064Sume		/* FALLTHROUGH */
122762587Sitojun	case NI_QTYPE_FQDN:
122862587Sitojun	case NI_QTYPE_NODEADDR:
122962587Sitojun		switch (ni6->ni_code) {
123062587Sitojun		case ICMP6_NI_SUBJ_IPV6:
123162587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0
123262587Sitojun		case 0:
123362587Sitojun#endif
123462587Sitojun			/*
123562587Sitojun			 * backward compatibility - try to accept 03 draft
123662587Sitojun			 * format, where no Subject is present.
123762587Sitojun			 */
123878064Sume			if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
123978064Sume			    subjlen == 0) {
124062587Sitojun				oldfqdn++;
124162587Sitojun				break;
124262587Sitojun			}
124378064Sume#if ICMP6_NI_SUBJ_IPV6 != 0
124478064Sume			if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
124578064Sume				goto bad;
124678064Sume#endif
124762587Sitojun
1248121630Sume			if (subjlen != sizeof(struct in6_addr))
124962587Sitojun				goto bad;
125062587Sitojun
125162587Sitojun			/*
125262587Sitojun			 * Validate Subject address.
125362587Sitojun			 *
125478064Sume			 * Not sure what exactly "address belongs to the node"
125578064Sume			 * means in the spec, is it just unicast, or what?
125662587Sitojun			 *
125762587Sitojun			 * At this moment we consider Subject address as
125862587Sitojun			 * "belong to the node" if the Subject address equals
125962587Sitojun			 * to the IPv6 destination address; validation for
126062587Sitojun			 * IPv6 destination address should have done enough
126162587Sitojun			 * check for us.
126262587Sitojun			 *
126362587Sitojun			 * We do not do proxy at this moment.
126462587Sitojun			 */
126562587Sitojun			/* m_pulldown instead of copy? */
1266121630Sume			bzero(&sin6_sbj, sizeof(sin6_sbj));
1267121630Sume			sin6_sbj.sin6_family = AF_INET6;
1268121630Sume			sin6_sbj.sin6_len = sizeof(sin6_sbj);
126962587Sitojun			m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1270121630Sume			    subjlen, (caddr_t)&sin6_sbj.sin6_addr);
1271121315Sume			if (in6_addr2zoneid(m->m_pkthdr.rcvif,
1272121630Sume			    &sin6_sbj.sin6_addr, &sin6_sbj.sin6_scope_id)) {
1273121315Sume				goto bad;
1274121315Sume			}
1275121630Sume			if (in6_embedscope(&sin6_sbj.sin6_addr, &sin6_sbj,
1276121630Sume			    NULL, NULL))
1277121630Sume				goto bad; /* XXX should not happen */
1278121630Sume
1279121630Sume			subj = (char *)&sin6_sbj;
1280140588Sume			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1281140588Sume			    &sin6_sbj.sin6_addr))
128262587Sitojun				break;
128378064Sume
128462587Sitojun			/*
128562587Sitojun			 * XXX if we are to allow other cases, we should really
128662587Sitojun			 * be careful about scope here.
128762587Sitojun			 * basically, we should disallow queries toward IPv6
1288120891Sume			 * destination X with subject Y,
1289120891Sume			 * if scope(X) > scope(Y).
129062587Sitojun			 * if we allow scope(X) > scope(Y), it will result in
129162587Sitojun			 * information leakage across scope boundary.
129262587Sitojun			 */
129362587Sitojun			goto bad;
129462587Sitojun
129562587Sitojun		case ICMP6_NI_SUBJ_FQDN:
129662587Sitojun			/*
129762587Sitojun			 * Validate Subject name with gethostname(3).
129862587Sitojun			 *
129962587Sitojun			 * The behavior may need some debate, since:
130062587Sitojun			 * - we are not sure if the node has FQDN as
130162587Sitojun			 *   hostname (returned by gethostname(3)).
130262587Sitojun			 * - the code does wildcard match for truncated names.
130362587Sitojun			 *   however, we are not sure if we want to perform
130462587Sitojun			 *   wildcard match, if gethostname(3) side has
130562587Sitojun			 *   truncated hostname.
130662587Sitojun			 */
130762587Sitojun			n = ni6_nametodns(hostname, hostnamelen, 0);
130862587Sitojun			if (!n || n->m_next || n->m_len == 0)
130962587Sitojun				goto bad;
131062587Sitojun			IP6_EXTHDR_GET(subj, char *, m,
131162587Sitojun			    off + sizeof(struct icmp6_nodeinfo), subjlen);
131262587Sitojun			if (subj == NULL)
131362587Sitojun				goto bad;
131462587Sitojun			if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1315120891Sume			    n->m_len)) {
131662587Sitojun				goto bad;
131762587Sitojun			}
131862587Sitojun			m_freem(n);
131962587Sitojun			n = NULL;
132062587Sitojun			break;
132162587Sitojun
132278064Sume		case ICMP6_NI_SUBJ_IPV4:	/* XXX: to be implemented? */
132362587Sitojun		default:
132462587Sitojun			goto bad;
132562587Sitojun		}
132662587Sitojun		break;
132778064Sume	}
132862587Sitojun
132978064Sume	/* refuse based on configuration.  XXX ICMP6_NI_REFUSED? */
133078064Sume	switch (qtype) {
133178064Sume	case NI_QTYPE_FQDN:
133278064Sume		if ((icmp6_nodeinfo & 1) == 0)
133378064Sume			goto bad;
133478064Sume		break;
133578064Sume	case NI_QTYPE_NODEADDR:
133678064Sume		if ((icmp6_nodeinfo & 2) == 0)
133778064Sume			goto bad;
133878064Sume		break;
133978064Sume	}
134078064Sume
134178064Sume	/* guess reply length */
134278064Sume	switch (qtype) {
134378064Sume	case NI_QTYPE_NOOP:
134478064Sume		break;		/* no reply data */
134578064Sume	case NI_QTYPE_SUPTYPES:
134678064Sume		replylen += sizeof(u_int32_t);
134778064Sume		break;
134878064Sume	case NI_QTYPE_FQDN:
134978064Sume		/* XXX will append an mbuf */
135078064Sume		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
135178064Sume		break;
135278064Sume	case NI_QTYPE_NODEADDR:
135378064Sume		addrs = ni6_addrs(ni6, m, &ifp, subj);
135478064Sume		if ((replylen += addrs * (sizeof(struct in6_addr) +
1355120891Sume		    sizeof(u_int32_t))) > MCLBYTES)
135678064Sume			replylen = MCLBYTES; /* XXX: will truncate pkt later */
135778064Sume		break;
135862587Sitojun	default:
135978064Sume		/*
136078064Sume		 * XXX: We must return a reply with the ICMP6 code
1361120891Sume		 * `unknown Qtype' in this case.  However we regard the case
136278064Sume		 * as an FQDN query for backward compatibility.
136378064Sume		 * Older versions set a random value to this field,
136478064Sume		 * so it rarely varies in the defined qtypes.
136578064Sume		 * But the mechanism is not reliable...
136678064Sume		 * maybe we should obsolete older versions.
136778064Sume		 */
136878064Sume		qtype = NI_QTYPE_FQDN;
136978064Sume		/* XXX will append an mbuf */
137078064Sume		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
137178064Sume		oldfqdn++;
137278064Sume		break;
137362587Sitojun	}
137462587Sitojun
137578064Sume	/* allocate an mbuf to reply. */
1376111119Simp	MGETHDR(n, M_DONTWAIT, m->m_type);
137762587Sitojun	if (n == NULL) {
137862587Sitojun		m_freem(m);
1379120856Sume		return (NULL);
138062587Sitojun	}
1381108466Ssam	M_MOVE_PKTHDR(n, m); /* just for recvif */
138253541Sshin	if (replylen > MHLEN) {
138362587Sitojun		if (replylen > MCLBYTES) {
138478064Sume			/*
138578064Sume			 * XXX: should we try to allocate more? But MCLBYTES
138678064Sume			 * is probably much larger than IPV6_MMTU...
138778064Sume			 */
138853541Sshin			goto bad;
138962587Sitojun		}
1390111119Simp		MCLGET(n, M_DONTWAIT);
139153541Sshin		if ((n->m_flags & M_EXT) == 0) {
139253541Sshin			goto bad;
139353541Sshin		}
139453541Sshin	}
139553541Sshin	n->m_pkthdr.len = n->m_len = replylen;
139653541Sshin
139753541Sshin	/* copy mbuf header and IPv6 + Node Information base headers */
139853541Sshin	bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
139953541Sshin	nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
140062587Sitojun	bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
140153541Sshin
140253541Sshin	/* qtype dependent procedure */
140353541Sshin	switch (qtype) {
140462587Sitojun	case NI_QTYPE_NOOP:
140578064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
140662587Sitojun		nni6->ni_flags = 0;
140762587Sitojun		break;
140862587Sitojun	case NI_QTYPE_SUPTYPES:
140978064Sume	{
141078064Sume		u_int32_t v;
141178064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
141278064Sume		nni6->ni_flags = htons(0x0000);	/* raw bitmap */
141378064Sume		/* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
141478064Sume		v = (u_int32_t)htonl(0x0000000f);
141578064Sume		bcopy(&v, nni6 + 1, sizeof(u_int32_t));
141662587Sitojun		break;
141778064Sume	}
141862587Sitojun	case NI_QTYPE_FQDN:
141978064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
142062587Sitojun		fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1421120891Sume		    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
142262587Sitojun		nni6->ni_flags = 0; /* XXX: meaningless TTL */
142362587Sitojun		fqdn->ni_fqdn_ttl = 0;	/* ditto. */
142462587Sitojun		/*
142562587Sitojun		 * XXX do we really have FQDN in variable "hostname"?
142662587Sitojun		 */
142762587Sitojun		n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
142862587Sitojun		if (n->m_next == NULL)
142962587Sitojun			goto bad;
143062587Sitojun		/* XXX we assume that n->m_next is not a chain */
143162587Sitojun		if (n->m_next->m_next != NULL)
143262587Sitojun			goto bad;
143362587Sitojun		n->m_pkthdr.len += n->m_next->m_len;
143462587Sitojun		break;
143562587Sitojun	case NI_QTYPE_NODEADDR:
143662587Sitojun	{
143762587Sitojun		int lenlim, copied;
143853541Sshin
143978064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
144078064Sume		n->m_pkthdr.len = n->m_len =
144178064Sume		    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
144278064Sume		lenlim = M_TRAILINGSPACE(n);
144362587Sitojun		copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
144462587Sitojun		/* XXX: reset mbuf length */
144562587Sitojun		n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1446120891Sume		    sizeof(struct icmp6_nodeinfo) + copied;
144762587Sitojun		break;
144853541Sshin	}
144962587Sitojun	default:
145062587Sitojun		break;		/* XXX impossible! */
145162587Sitojun	}
145253541Sshin
145353541Sshin	nni6->ni_type = ICMP6_NI_REPLY;
145462587Sitojun	m_freem(m);
1455120856Sume	return (n);
145653541Sshin
145753541Sshin  bad:
145862587Sitojun	m_freem(m);
145953541Sshin	if (n)
146053541Sshin		m_freem(n);
1461120856Sume	return (NULL);
146253541Sshin}
146353541Sshin#undef hostnamelen
146453541Sshin
146553541Sshin/*
146662587Sitojun * make a mbuf with DNS-encoded string.  no compression support.
146762587Sitojun *
146862587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be
146962587Sitojun * treated as truncated name (two \0 at the end).  this is a wild guess.
147062587Sitojun */
147162587Sitojunstatic struct mbuf *
147262587Sitojunni6_nametodns(name, namelen, old)
147362587Sitojun	const char *name;
147462587Sitojun	int namelen;
147562587Sitojun	int old;	/* return pascal string if non-zero */
147662587Sitojun{
147762587Sitojun	struct mbuf *m;
147862587Sitojun	char *cp, *ep;
147962587Sitojun	const char *p, *q;
148062587Sitojun	int i, len, nterm;
148162587Sitojun
148262587Sitojun	if (old)
148362587Sitojun		len = namelen + 1;
148462587Sitojun	else
148562587Sitojun		len = MCLBYTES;
148662587Sitojun
148762587Sitojun	/* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1488111119Simp	MGET(m, M_DONTWAIT, MT_DATA);
148962587Sitojun	if (m && len > MLEN) {
1490111119Simp		MCLGET(m, M_DONTWAIT);
149162587Sitojun		if ((m->m_flags & M_EXT) == 0)
149262587Sitojun			goto fail;
149362587Sitojun	}
149462587Sitojun	if (!m)
149562587Sitojun		goto fail;
149662587Sitojun	m->m_next = NULL;
149762587Sitojun
149862587Sitojun	if (old) {
149962587Sitojun		m->m_len = len;
150062587Sitojun		*mtod(m, char *) = namelen;
150162587Sitojun		bcopy(name, mtod(m, char *) + 1, namelen);
150262587Sitojun		return m;
150362587Sitojun	} else {
150462587Sitojun		m->m_len = 0;
150562587Sitojun		cp = mtod(m, char *);
150662587Sitojun		ep = mtod(m, char *) + M_TRAILINGSPACE(m);
150762587Sitojun
150862587Sitojun		/* if not certain about my name, return empty buffer */
150962587Sitojun		if (namelen == 0)
151062587Sitojun			return m;
151162587Sitojun
151262587Sitojun		/*
151362587Sitojun		 * guess if it looks like shortened hostname, or FQDN.
151462587Sitojun		 * shortened hostname needs two trailing "\0".
151562587Sitojun		 */
151662587Sitojun		i = 0;
151762587Sitojun		for (p = name; p < name + namelen; p++) {
151862587Sitojun			if (*p && *p == '.')
151962587Sitojun				i++;
152062587Sitojun		}
152162587Sitojun		if (i < 2)
152262587Sitojun			nterm = 2;
152362587Sitojun		else
152462587Sitojun			nterm = 1;
152562587Sitojun
152662587Sitojun		p = name;
152762587Sitojun		while (cp < ep && p < name + namelen) {
152862587Sitojun			i = 0;
152962587Sitojun			for (q = p; q < name + namelen && *q && *q != '.'; q++)
153062587Sitojun				i++;
153162587Sitojun			/* result does not fit into mbuf */
153262587Sitojun			if (cp + i + 1 >= ep)
153362587Sitojun				goto fail;
153478704Sume			/*
153578704Sume			 * DNS label length restriction, RFC1035 page 8.
153678704Sume			 * "i == 0" case is included here to avoid returning
153778704Sume			 * 0-length label on "foo..bar".
153878704Sume			 */
153978704Sume			if (i <= 0 || i >= 64)
154062587Sitojun				goto fail;
154162587Sitojun			*cp++ = i;
154262587Sitojun			bcopy(p, cp, i);
154362587Sitojun			cp += i;
154462587Sitojun			p = q;
154562587Sitojun			if (p < name + namelen && *p == '.')
154662587Sitojun				p++;
154762587Sitojun		}
154862587Sitojun		/* termination */
154962587Sitojun		if (cp + nterm >= ep)
155062587Sitojun			goto fail;
155162587Sitojun		while (nterm-- > 0)
155262587Sitojun			*cp++ = '\0';
155362587Sitojun		m->m_len = cp - mtod(m, char *);
155462587Sitojun		return m;
155562587Sitojun	}
155662587Sitojun
155762587Sitojun	panic("should not reach here");
155895023Ssuz	/* NOTREACHED */
155962587Sitojun
156062587Sitojun fail:
156162587Sitojun	if (m)
156262587Sitojun		m_freem(m);
156362587Sitojun	return NULL;
156462587Sitojun}
156562587Sitojun
156662587Sitojun/*
156762587Sitojun * check if two DNS-encoded string matches.  takes care of truncated
156862587Sitojun * form (with \0\0 at the end).  no compression support.
156978064Sume * XXX upper/lowercase match (see RFC2065)
157062587Sitojun */
157162587Sitojunstatic int
157262587Sitojunni6_dnsmatch(a, alen, b, blen)
157362587Sitojun	const char *a;
157462587Sitojun	int alen;
157562587Sitojun	const char *b;
157662587Sitojun	int blen;
157762587Sitojun{
157862587Sitojun	const char *a0, *b0;
157962587Sitojun	int l;
158062587Sitojun
158162587Sitojun	/* simplest case - need validation? */
158262587Sitojun	if (alen == blen && bcmp(a, b, alen) == 0)
158362587Sitojun		return 1;
158462587Sitojun
158562587Sitojun	a0 = a;
158662587Sitojun	b0 = b;
158762587Sitojun
158862587Sitojun	/* termination is mandatory */
158962587Sitojun	if (alen < 2 || blen < 2)
159062587Sitojun		return 0;
159162587Sitojun	if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
159262587Sitojun		return 0;
159362587Sitojun	alen--;
159462587Sitojun	blen--;
159562587Sitojun
159662587Sitojun	while (a - a0 < alen && b - b0 < blen) {
159762587Sitojun		if (a - a0 + 1 > alen || b - b0 + 1 > blen)
159862587Sitojun			return 0;
159962587Sitojun
160062587Sitojun		if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
160162587Sitojun			return 0;
160262587Sitojun		/* we don't support compression yet */
160362587Sitojun		if (a[0] >= 64 || b[0] >= 64)
160462587Sitojun			return 0;
160562587Sitojun
160662587Sitojun		/* truncated case */
160762587Sitojun		if (a[0] == 0 && a - a0 == alen - 1)
160862587Sitojun			return 1;
160962587Sitojun		if (b[0] == 0 && b - b0 == blen - 1)
161062587Sitojun			return 1;
161162587Sitojun		if (a[0] == 0 || b[0] == 0)
161262587Sitojun			return 0;
161362587Sitojun
161462587Sitojun		if (a[0] != b[0])
161562587Sitojun			return 0;
161662587Sitojun		l = a[0];
161762587Sitojun		if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
161862587Sitojun			return 0;
161962587Sitojun		if (bcmp(a + 1, b + 1, l) != 0)
162062587Sitojun			return 0;
162162587Sitojun
162262587Sitojun		a += 1 + l;
162362587Sitojun		b += 1 + l;
162462587Sitojun	}
162562587Sitojun
162662587Sitojun	if (a - a0 == alen && b - b0 == blen)
162762587Sitojun		return 1;
162862587Sitojun	else
162962587Sitojun		return 0;
163062587Sitojun}
163162587Sitojun
163262587Sitojun/*
163353541Sshin * calculate the number of addresses to be returned in the node info reply.
163453541Sshin */
163553541Sshinstatic int
163678064Sumeni6_addrs(ni6, m, ifpp, subj)
163753541Sshin	struct icmp6_nodeinfo *ni6;
163853541Sshin	struct mbuf *m;
163953541Sshin	struct ifnet **ifpp;
164078064Sume	char *subj;
164153541Sshin{
164278064Sume	struct ifnet *ifp;
164378064Sume	struct in6_ifaddr *ifa6;
164478064Sume	struct ifaddr *ifa;
164578064Sume	struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
164653541Sshin	int addrs = 0, addrsofif, iffound = 0;
164778064Sume	int niflags = ni6->ni_flags;
164853541Sshin
164978064Sume	if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
165078064Sume		switch (ni6->ni_code) {
165178064Sume		case ICMP6_NI_SUBJ_IPV6:
165278064Sume			if (subj == NULL) /* must be impossible... */
1653120856Sume				return (0);
165478064Sume			subj_ip6 = (struct sockaddr_in6 *)subj;
165578064Sume			break;
165678064Sume		default:
165778064Sume			/*
165878064Sume			 * XXX: we only support IPv6 subject address for
165978064Sume			 * this Qtype.
166078064Sume			 */
1661120856Sume			return (0);
166278064Sume		}
166378064Sume	}
166478064Sume
1665108172Shsu	IFNET_RLOCK();
1666120891Sume	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
166753541Sshin		addrsofif = 0;
1668120891Sume		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
166953541Sshin			if (ifa->ifa_addr->sa_family != AF_INET6)
167053541Sshin				continue;
167153541Sshin			ifa6 = (struct in6_ifaddr *)ifa;
167253541Sshin
167378064Sume			if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
167478064Sume			    IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1675120891Sume			    &ifa6->ia_addr.sin6_addr))
167653541Sshin				iffound = 1;
167753541Sshin
167862587Sitojun			/*
167962587Sitojun			 * IPv4-mapped addresses can only be returned by a
168062587Sitojun			 * Node Information proxy, since they represent
168162587Sitojun			 * addresses of IPv4-only nodes, which perforce do
168262587Sitojun			 * not implement this protocol.
168378064Sume			 * [icmp-name-lookups-07, Section 5.4]
168462587Sitojun			 * So we don't support NI_NODEADDR_FLAG_COMPAT in
168562587Sitojun			 * this function at this moment.
168662587Sitojun			 */
168762587Sitojun
168853541Sshin			/* What do we have to do about ::1? */
168978064Sume			switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
169078064Sume			case IPV6_ADDR_SCOPE_LINKLOCAL:
169178064Sume				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
169278064Sume					continue;
169353541Sshin				break;
169478064Sume			case IPV6_ADDR_SCOPE_SITELOCAL:
169578064Sume				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
169678064Sume					continue;
169753541Sshin				break;
169878064Sume			case IPV6_ADDR_SCOPE_GLOBAL:
169978064Sume				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
170078064Sume					continue;
170178064Sume				break;
170278064Sume			default:
170378064Sume				continue;
170453541Sshin			}
170578064Sume
170678064Sume			/*
170778064Sume			 * check if anycast is okay.
170895023Ssuz			 * XXX: just experimental.  not in the spec.
170978064Sume			 */
171078064Sume			if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
171178064Sume			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
171278064Sume				continue; /* we need only unicast addresses */
171378064Sume			if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
171478064Sume			    (icmp6_nodeinfo & 4) == 0) {
171578064Sume				continue;
171678064Sume			}
171778064Sume			addrsofif++; /* count the address */
171853541Sshin		}
171953541Sshin		if (iffound) {
172053541Sshin			*ifpp = ifp;
1721108172Shsu			IFNET_RUNLOCK();
1722120856Sume			return (addrsofif);
172353541Sshin		}
172453541Sshin
172553541Sshin		addrs += addrsofif;
172653541Sshin	}
1727108172Shsu	IFNET_RUNLOCK();
172853541Sshin
1729120856Sume	return (addrs);
173053541Sshin}
173153541Sshin
173253541Sshinstatic int
173353541Sshinni6_store_addrs(ni6, nni6, ifp0, resid)
173453541Sshin	struct icmp6_nodeinfo *ni6, *nni6;
173553541Sshin	struct ifnet *ifp0;
173653541Sshin	int resid;
173753541Sshin{
173878064Sume	struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
173978064Sume	struct in6_ifaddr *ifa6;
174078064Sume	struct ifaddr *ifa;
174178064Sume	struct ifnet *ifp_dep = NULL;
174278064Sume	int copied = 0, allow_deprecated = 0;
174353541Sshin	u_char *cp = (u_char *)(nni6 + 1);
174478064Sume	int niflags = ni6->ni_flags;
174578064Sume	u_int32_t ltime;
174653541Sshin
174778064Sume	if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1748120856Sume		return (0);	/* needless to copy */
1749120891Sume
1750108172Shsu	IFNET_RLOCK();
175178064Sume  again:
1752120892Sume
1753120891Sume	for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
175462587Sitojun		for (ifa = ifp->if_addrlist.tqh_first; ifa;
1755120891Sume		     ifa = ifa->ifa_list.tqe_next) {
175653541Sshin			if (ifa->ifa_addr->sa_family != AF_INET6)
175753541Sshin				continue;
175853541Sshin			ifa6 = (struct in6_ifaddr *)ifa;
175953541Sshin
176078064Sume			if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
176178064Sume			    allow_deprecated == 0) {
176278064Sume				/*
176378064Sume				 * prefererred address should be put before
176478064Sume				 * deprecated addresses.
176578064Sume				 */
176678064Sume
176778064Sume				/* record the interface for later search */
176878064Sume				if (ifp_dep == NULL)
176978064Sume					ifp_dep = ifp;
177078064Sume
177178064Sume				continue;
1772120891Sume			} else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1773120891Sume			    allow_deprecated != 0)
177478064Sume				continue; /* we now collect deprecated addrs */
177553541Sshin
177653541Sshin			/* What do we have to do about ::1? */
177778064Sume			switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
177878064Sume			case IPV6_ADDR_SCOPE_LINKLOCAL:
177978064Sume				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
178078064Sume					continue;
178153541Sshin				break;
178278064Sume			case IPV6_ADDR_SCOPE_SITELOCAL:
178378064Sume				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
178478064Sume					continue;
178553541Sshin				break;
178678064Sume			case IPV6_ADDR_SCOPE_GLOBAL:
178778064Sume				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
178878064Sume					continue;
178978064Sume				break;
179078064Sume			default:
179178064Sume				continue;
179253541Sshin			}
179353541Sshin
179478064Sume			/*
179578064Sume			 * check if anycast is okay.
1796120891Sume			 * XXX: just experimental.  not in the spec.
179778064Sume			 */
179878064Sume			if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
179978064Sume			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
180078064Sume				continue;
180178064Sume			if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
180278064Sume			    (icmp6_nodeinfo & 4) == 0) {
180378064Sume				continue;
180453541Sshin			}
180578064Sume
180678064Sume			/* now we can copy the address */
180778064Sume			if (resid < sizeof(struct in6_addr) +
180878064Sume			    sizeof(u_int32_t)) {
180978064Sume				/*
181078064Sume				 * We give up much more copy.
181178064Sume				 * Set the truncate flag and return.
181278064Sume				 */
1813120891Sume				nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1814108172Shsu				IFNET_RUNLOCK();
1815120856Sume				return (copied);
181678064Sume			}
181778064Sume
181878064Sume			/*
181978064Sume			 * Set the TTL of the address.
182078064Sume			 * The TTL value should be one of the following
182178064Sume			 * according to the specification:
182278064Sume			 *
182378064Sume			 * 1. The remaining lifetime of a DHCP lease on the
182478064Sume			 *    address, or
182578064Sume			 * 2. The remaining Valid Lifetime of a prefix from
182678064Sume			 *    which the address was derived through Stateless
182778064Sume			 *    Autoconfiguration.
182878064Sume			 *
182978064Sume			 * Note that we currently do not support stateful
183078064Sume			 * address configuration by DHCPv6, so the former
183178064Sume			 * case can't happen.
183278064Sume			 */
183378064Sume			if (ifa6->ia6_lifetime.ia6t_expire == 0)
183478064Sume				ltime = ND6_INFINITE_LIFETIME;
183578064Sume			else {
183678064Sume				if (ifa6->ia6_lifetime.ia6t_expire >
183778064Sume				    time_second)
183878064Sume					ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
183978064Sume				else
184078064Sume					ltime = 0;
184178064Sume			}
1842120891Sume
184378064Sume			bcopy(&ltime, cp, sizeof(u_int32_t));
184478064Sume			cp += sizeof(u_int32_t);
184578064Sume
184678064Sume			/* copy the address itself */
184778064Sume			bcopy(&ifa6->ia_addr.sin6_addr, cp,
1848120891Sume			    sizeof(struct in6_addr));
1849121315Sume			in6_clearscope((struct in6_addr *)cp); /* XXX */
185078064Sume			cp += sizeof(struct in6_addr);
1851120891Sume
185278064Sume			resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1853120891Sume			copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
185453541Sshin		}
185553541Sshin		if (ifp0)	/* we need search only on the specified IF */
185653541Sshin			break;
185753541Sshin	}
185853541Sshin
185978064Sume	if (allow_deprecated == 0 && ifp_dep != NULL) {
186078064Sume		ifp = ifp_dep;
186178064Sume		allow_deprecated = 1;
186278064Sume
186378064Sume		goto again;
186478064Sume	}
186578064Sume
1866108172Shsu	IFNET_RUNLOCK();
1867108172Shsu
1868120856Sume	return (copied);
186953541Sshin}
187053541Sshin
187153541Sshin/*
187253541Sshin * XXX almost dup'ed code with rip6_input.
187353541Sshin */
187453541Sshinstatic int
187553541Sshinicmp6_rip6_input(mp, off)
187653541Sshin	struct	mbuf **mp;
187753541Sshin	int	off;
187853541Sshin{
187953541Sshin	struct mbuf *m = *mp;
188078064Sume	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
188178064Sume	struct in6pcb *in6p;
188253541Sshin	struct in6pcb *last = NULL;
1883121315Sume	struct sockaddr_in6 fromsa;
188453541Sshin	struct icmp6_hdr *icmp6;
188553541Sshin	struct mbuf *opts = NULL;
188653541Sshin
188762587Sitojun#ifndef PULLDOWN_TEST
188853541Sshin	/* this is assumed to be safe. */
188953541Sshin	icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
189062587Sitojun#else
189162587Sitojun	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
189262587Sitojun	if (icmp6 == NULL) {
189362587Sitojun		/* m is already reclaimed */
1894120891Sume		return (IPPROTO_DONE);
189562587Sitojun	}
189662587Sitojun#endif
189753541Sshin
1898121315Sume	bzero(&fromsa, sizeof(fromsa));
1899121315Sume	fromsa.sin6_len = sizeof(struct sockaddr_in6);
1900121315Sume	fromsa.sin6_family = AF_INET6;
190162587Sitojun	/* KAME hack: recover scopeid */
1902121315Sume	(void)in6_recoverscope(&fromsa, &ip6->ip6_src, m->m_pkthdr.rcvif);
190353541Sshin
1904141545Srwatson	INP_INFO_RLOCK(&ripcbinfo);
1905120891Sume	LIST_FOREACH(in6p, &ripcb, inp_list) {
1906141545Srwatson		INP_LOCK(in6p);
1907141545Srwatson		if ((in6p->inp_vflag & INP_IPV6) == 0) {
1908141545Srwatson	docontinue:
1909141545Srwatson			INP_UNLOCK(in6p);
191053541Sshin			continue;
1911141545Srwatson		}
191253541Sshin		if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
1913141545Srwatson			goto docontinue;
191453541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
191553541Sshin		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1916141545Srwatson			goto docontinue;
191753541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
191853541Sshin		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1919141545Srwatson			goto docontinue;
192053541Sshin		if (in6p->in6p_icmp6filt
192153541Sshin		    && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
192253541Sshin				 in6p->in6p_icmp6filt))
1923141545Srwatson			goto docontinue;
192453541Sshin		if (last) {
1925121809Sume			struct	mbuf *n = NULL;
1926121809Sume
1927121809Sume			/*
1928121809Sume			 * Recent network drivers tend to allocate a single
1929121809Sume			 * mbuf cluster, rather than to make a couple of
1930121809Sume			 * mbufs without clusters.  Also, since the IPv6 code
1931121809Sume			 * path tries to avoid m_pullup(), it is highly
1932121809Sume			 * probable that we still have an mbuf cluster here
1933121809Sume			 * even though the necessary length can be stored in an
1934121809Sume			 * mbuf's internal buffer.
1935121809Sume			 * Meanwhile, the default size of the receive socket
1936121809Sume			 * buffer for raw sockets is not so large.  This means
1937121809Sume			 * the possibility of packet loss is relatively higher
1938121809Sume			 * than before.  To avoid this scenario, we copy the
1939121809Sume			 * received data to a separate mbuf that does not use
1940121809Sume			 * a cluster, if possible.
1941121809Sume			 * XXX: it is better to copy the data after stripping
1942121809Sume			 * intermediate headers.
1943121809Sume			 */
1944121809Sume			if ((m->m_flags & M_EXT) && m->m_next == NULL &&
1945121809Sume			    m->m_len <= MHLEN) {
1946121809Sume				MGET(n, M_DONTWAIT, m->m_type);
1947121809Sume				if (n != NULL) {
1948121809Sume					m_dup_pkthdr(n, m, M_NOWAIT);
1949121809Sume					bcopy(m->m_data, n->m_data, m->m_len);
1950121809Sume					n->m_len = m->m_len;
1951121809Sume				}
1952121809Sume			}
1953121809Sume			if (n != NULL ||
1954121809Sume			    (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
195553541Sshin				if (last->in6p_flags & IN6P_CONTROLOPTS)
1956121674Sume					ip6_savecontrol(last, n, &opts);
195753541Sshin				/* strip intermediate headers */
195853541Sshin				m_adj(n, off);
195953541Sshin				if (sbappendaddr(&last->in6p_socket->so_rcv,
1960121315Sume				    (struct sockaddr *)&fromsa, n, opts)
1961120891Sume				    == 0) {
196253541Sshin					/* should notify about lost packet */
196353541Sshin					m_freem(n);
196478064Sume					if (opts) {
196553541Sshin						m_freem(opts);
196678064Sume					}
196797658Stanimura				} else
196853541Sshin					sorwakeup(last->in6p_socket);
196953541Sshin				opts = NULL;
197053541Sshin			}
1971141545Srwatson			INP_UNLOCK(last);
197253541Sshin		}
197353541Sshin		last = in6p;
197453541Sshin	}
197553541Sshin	if (last) {
197653541Sshin		if (last->in6p_flags & IN6P_CONTROLOPTS)
1977121674Sume			ip6_savecontrol(last, m, &opts);
197853541Sshin		/* strip intermediate headers */
197953541Sshin		m_adj(m, off);
1980121809Sume
1981121809Sume		/* avoid using mbuf clusters if possible (see above) */
1982121809Sume		if ((m->m_flags & M_EXT) && m->m_next == NULL &&
1983121809Sume		    m->m_len <= MHLEN) {
1984121809Sume			struct mbuf *n;
1985121809Sume
1986121809Sume			MGET(n, M_DONTWAIT, m->m_type);
1987121809Sume			if (n != NULL) {
1988121809Sume				m_dup_pkthdr(n, m, M_NOWAIT);
1989121809Sume				bcopy(m->m_data, n->m_data, m->m_len);
1990121809Sume				n->m_len = m->m_len;
1991121809Sume
1992121809Sume				m_freem(m);
1993121809Sume				m = n;
1994121809Sume			}
1995121809Sume		}
199653541Sshin		if (sbappendaddr(&last->in6p_socket->so_rcv,
1997121315Sume		    (struct sockaddr *)&fromsa, m, opts) == 0) {
199853541Sshin			m_freem(m);
199953541Sshin			if (opts)
200053541Sshin				m_freem(opts);
200197658Stanimura		} else
200253541Sshin			sorwakeup(last->in6p_socket);
2003141545Srwatson		INP_UNLOCK(last);
200453541Sshin	} else {
200553541Sshin		m_freem(m);
200653541Sshin		ip6stat.ip6s_delivered--;
200753541Sshin	}
2008141545Srwatson	INP_INFO_RUNLOCK(&ripcbinfo);
200953541Sshin	return IPPROTO_DONE;
201053541Sshin}
201153541Sshin
201253541Sshin/*
201353541Sshin * Reflect the ip6 packet back to the source.
201462587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf.
201553541Sshin */
201653541Sshinvoid
201753541Sshinicmp6_reflect(m, off)
201853541Sshin	struct	mbuf *m;
201953541Sshin	size_t off;
202053541Sshin{
202162587Sitojun	struct ip6_hdr *ip6;
202253541Sshin	struct icmp6_hdr *icmp6;
202353541Sshin	struct in6_ifaddr *ia;
202453541Sshin	struct in6_addr t, *src = 0;
202562587Sitojun	int plen;
202653541Sshin	int type, code;
202753541Sshin	struct ifnet *outif = NULL;
202878064Sume	struct sockaddr_in6 sa6_src, sa6_dst;
202953541Sshin#ifdef COMPAT_RFC1885
203053541Sshin	int mtu = IPV6_MMTU;
203153541Sshin	struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst;
203253541Sshin#endif
203353541Sshin
203462587Sitojun	/* too short to reflect */
203562587Sitojun	if (off < sizeof(struct ip6_hdr)) {
203678064Sume		nd6log((LOG_DEBUG,
203778064Sume		    "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
203878064Sume		    (u_long)off, (u_long)sizeof(struct ip6_hdr),
203978064Sume		    __FILE__, __LINE__));
204062587Sitojun		goto bad;
204162587Sitojun	}
204262587Sitojun
204353541Sshin	/*
204453541Sshin	 * If there are extra headers between IPv6 and ICMPv6, strip
204553541Sshin	 * off that header first.
204653541Sshin	 */
204762587Sitojun#ifdef DIAGNOSTIC
204862587Sitojun	if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
204962587Sitojun		panic("assumption failed in icmp6_reflect");
205062587Sitojun#endif
205162587Sitojun	if (off > sizeof(struct ip6_hdr)) {
205262587Sitojun		size_t l;
205362587Sitojun		struct ip6_hdr nip6;
205453541Sshin
205562587Sitojun		l = off - sizeof(struct ip6_hdr);
205662587Sitojun		m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
205762587Sitojun		m_adj(m, l);
205862587Sitojun		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
205962587Sitojun		if (m->m_len < l) {
206062587Sitojun			if ((m = m_pullup(m, l)) == NULL)
206162587Sitojun				return;
206253541Sshin		}
206362587Sitojun		bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
206462587Sitojun	} else /* off == sizeof(struct ip6_hdr) */ {
206562587Sitojun		size_t l;
206662587Sitojun		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
206762587Sitojun		if (m->m_len < l) {
206862587Sitojun			if ((m = m_pullup(m, l)) == NULL)
206962587Sitojun				return;
207053541Sshin		}
207153541Sshin	}
207262587Sitojun	plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
207362587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
207462587Sitojun	ip6->ip6_nxt = IPPROTO_ICMPV6;
207553541Sshin	icmp6 = (struct icmp6_hdr *)(ip6 + 1);
207653541Sshin	type = icmp6->icmp6_type; /* keep type for statistics */
207753541Sshin	code = icmp6->icmp6_code; /* ditto. */
207853541Sshin
207953541Sshin	t = ip6->ip6_dst;
208053541Sshin	/*
208153541Sshin	 * ip6_input() drops a packet if its src is multicast.
208253541Sshin	 * So, the src is never multicast.
208353541Sshin	 */
208453541Sshin	ip6->ip6_dst = ip6->ip6_src;
208553541Sshin
208678064Sume	/*
208778064Sume	 * XXX: make sure to embed scope zone information, using
208878064Sume	 * already embedded IDs or the received interface (if any).
208978064Sume	 * Note that rcvif may be NULL.
209078064Sume	 * TODO: scoped routing case (XXX).
209178064Sume	 */
209278064Sume	bzero(&sa6_src, sizeof(sa6_src));
209378064Sume	sa6_src.sin6_family = AF_INET6;
209478064Sume	sa6_src.sin6_len = sizeof(sa6_src);
209578064Sume	sa6_src.sin6_addr = ip6->ip6_dst;
209678064Sume	in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
209778064Sume	in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL);
209878064Sume	bzero(&sa6_dst, sizeof(sa6_dst));
209978064Sume	sa6_dst.sin6_family = AF_INET6;
210078064Sume	sa6_dst.sin6_len = sizeof(sa6_dst);
210178064Sume	sa6_dst.sin6_addr = t;
210278064Sume	in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
210378064Sume	in6_embedscope(&t, &sa6_dst, NULL, NULL);
210453541Sshin
210553541Sshin#ifdef COMPAT_RFC1885
210653541Sshin	/*
210753541Sshin	 * xxx guess MTU
210853541Sshin	 * RFC 1885 requires that echo reply should be truncated if it
210953541Sshin	 * does not fit in with (return) path MTU, but the description was
211053541Sshin	 * removed in the new spec.
211153541Sshin	 */
211253541Sshin	if (icmp6_reflect_rt.ro_rt == 0 ||
211353541Sshin	    ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) {
211453541Sshin		if (icmp6_reflect_rt.ro_rt) {
211553541Sshin			RTFREE(icmp6_reflect_rt.ro_rt);
211653541Sshin			icmp6_reflect_rt.ro_rt = 0;
211753541Sshin		}
211853541Sshin		bzero(sin6, sizeof(*sin6));
211953541Sshin		sin6->sin6_family = PF_INET6;
212053541Sshin		sin6->sin6_len = sizeof(struct sockaddr_in6);
212153541Sshin		sin6->sin6_addr = ip6->ip6_dst;
212253541Sshin
2123122921Sandre		rtalloc((struct route *)&icmp6_reflect_rt.ro_rt);
212453541Sshin	}
212553541Sshin
212653541Sshin	if (icmp6_reflect_rt.ro_rt == 0)
212753541Sshin		goto bad;
212853541Sshin
212953541Sshin	if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST)
213053541Sshin	    && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu)
213153541Sshin		mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu;
213253541Sshin
213353541Sshin	if (mtu < m->m_pkthdr.len) {
213453541Sshin		plen -= (m->m_pkthdr.len - mtu);
213553541Sshin		m_adj(m, mtu - m->m_pkthdr.len);
213653541Sshin	}
213753541Sshin#endif
213853541Sshin	/*
2139120891Sume	 * If the incoming packet was addressed directly to us (i.e. unicast),
214053541Sshin	 * use dst as the src for the reply.
2141120891Sume	 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
214262587Sitojun	 * (for example) when we encounter an error while forwarding procedure
214362587Sitojun	 * destined to a duplicated address of ours.
214453541Sshin	 */
214553541Sshin	for (ia = in6_ifaddr; ia; ia = ia->ia_next)
214653541Sshin		if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
214762587Sitojun		    (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
214853541Sshin			src = &t;
214953541Sshin			break;
215053541Sshin		}
215153541Sshin	if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
215253541Sshin		/*
215353541Sshin		 * This is the case if the dst is our link-local address
215495023Ssuz		 * and the sender is also ourselves.
215553541Sshin		 */
215653541Sshin		src = &t;
215753541Sshin	}
215853541Sshin
215978064Sume	if (src == 0) {
216078064Sume		int e;
216178064Sume		struct route_in6 ro;
216278064Sume
216353541Sshin		/*
216462587Sitojun		 * This case matches to multicasts, our anycast, or unicasts
216595023Ssuz		 * that we do not own.  Select a source address based on the
216678064Sume		 * source address of the erroneous packet.
216753541Sshin		 */
216878064Sume		bzero(&ro, sizeof(ro));
216978064Sume		src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e);
217078064Sume		if (ro.ro_rt)
217178064Sume			RTFREE(ro.ro_rt); /* XXX: we could use this */
217278064Sume		if (src == NULL) {
217378064Sume			nd6log((LOG_DEBUG,
217478064Sume			    "icmp6_reflect: source can't be determined: "
217578064Sume			    "dst=%s, error=%d\n",
217678064Sume			    ip6_sprintf(&sa6_src.sin6_addr), e));
217778064Sume			goto bad;
217878064Sume		}
217978064Sume	}
218053541Sshin
218153541Sshin	ip6->ip6_src = *src;
218253541Sshin
218353541Sshin	ip6->ip6_flow = 0;
218462587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
218562587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
218653541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
2187121472Sume	if (outif)
2188121472Sume		ip6->ip6_hlim = ND_IFINFO(outif)->chlim;
2189121472Sume	else if (m->m_pkthdr.rcvif) {
219053541Sshin		/* XXX: This may not be the outgoing interface */
2191121161Sume		ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
219278064Sume	} else
219378064Sume		ip6->ip6_hlim = ip6_defhlim;
219453541Sshin
219553541Sshin	icmp6->icmp6_cksum = 0;
219653541Sshin	icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2197120891Sume	    sizeof(struct ip6_hdr), plen);
219853541Sshin
219953541Sshin	/*
220078064Sume	 * XXX option handling
220153541Sshin	 */
220253541Sshin
220353541Sshin	m->m_flags &= ~(M_BCAST|M_MCAST);
220453541Sshin
220553541Sshin#ifdef COMPAT_RFC1885
2206105194Ssam	ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL);
220753541Sshin#else
2208105194Ssam	ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
220953541Sshin#endif
221053541Sshin	if (outif)
221153541Sshin		icmp6_ifoutstat_inc(outif, type, code);
221253541Sshin
221353541Sshin	return;
221453541Sshin
221553541Sshin bad:
221653541Sshin	m_freem(m);
221753541Sshin	return;
221853541Sshin}
221953541Sshin
222053541Sshinvoid
222153541Sshinicmp6_fasttimo()
222253541Sshin{
222362587Sitojun
222453541Sshin	mld6_fasttimeo();
222553541Sshin}
222653541Sshin
222753541Sshinstatic const char *
222853541Sshinicmp6_redirect_diag(src6, dst6, tgt6)
222953541Sshin	struct in6_addr *src6;
223053541Sshin	struct in6_addr *dst6;
223153541Sshin	struct in6_addr *tgt6;
223253541Sshin{
223353541Sshin	static char buf[1024];
223453541Sshin	snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2235120891Sume	    ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
223653541Sshin	return buf;
223753541Sshin}
223853541Sshin
223953541Sshinvoid
224053541Sshinicmp6_redirect_input(m, off)
224178064Sume	struct mbuf *m;
224253541Sshin	int off;
224353541Sshin{
224453541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
224553541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
224662587Sitojun	struct nd_redirect *nd_rd;
224753541Sshin	int icmp6len = ntohs(ip6->ip6_plen);
224853541Sshin	char *lladdr = NULL;
224953541Sshin	int lladdrlen = 0;
225053541Sshin	u_char *redirhdr = NULL;
225153541Sshin	int redirhdrlen = 0;
225253541Sshin	struct rtentry *rt = NULL;
225353541Sshin	int is_router;
225453541Sshin	int is_onlink;
225553541Sshin	struct in6_addr src6 = ip6->ip6_src;
225662587Sitojun	struct in6_addr redtgt6;
225762587Sitojun	struct in6_addr reddst6;
225853541Sshin	union nd_opts ndopts;
225953541Sshin
226053541Sshin	if (!m || !ifp)
226153541Sshin		return;
226253541Sshin
226353541Sshin	/* XXX if we are router, we don't update route by icmp6 redirect */
226453541Sshin	if (ip6_forwarding)
226562587Sitojun		goto freeit;
226653541Sshin	if (!icmp6_rediraccept)
226762587Sitojun		goto freeit;
226862587Sitojun
226962587Sitojun#ifndef PULLDOWN_TEST
227062587Sitojun	IP6_EXTHDR_CHECK(m, off, icmp6len,);
227162587Sitojun	nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
227262587Sitojun#else
227362587Sitojun	IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
227462587Sitojun	if (nd_rd == NULL) {
227562587Sitojun		icmp6stat.icp6s_tooshort++;
227653541Sshin		return;
227762587Sitojun	}
227862587Sitojun#endif
227962587Sitojun	redtgt6 = nd_rd->nd_rd_target;
228062587Sitojun	reddst6 = nd_rd->nd_rd_dst;
228153541Sshin
228253541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
228353541Sshin		redtgt6.s6_addr16[1] = htons(ifp->if_index);
228453541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
228553541Sshin		reddst6.s6_addr16[1] = htons(ifp->if_index);
228653541Sshin
228753541Sshin	/* validation */
228853541Sshin	if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
228978064Sume		nd6log((LOG_ERR,
2290120891Sume		    "ICMP6 redirect sent from %s rejected; "
2291120891Sume		    "must be from linklocal\n",
2292120891Sume		    ip6_sprintf(&src6)));
229378064Sume		goto bad;
229453541Sshin	}
229553541Sshin	if (ip6->ip6_hlim != 255) {
229678064Sume		nd6log((LOG_ERR,
2297120891Sume		    "ICMP6 redirect sent from %s rejected; "
2298120891Sume		    "hlim=%d (must be 255)\n",
2299120891Sume		    ip6_sprintf(&src6), ip6->ip6_hlim));
230078064Sume		goto bad;
230153541Sshin	}
230253541Sshin    {
230353541Sshin	/* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
230453541Sshin	struct sockaddr_in6 sin6;
230553541Sshin	struct in6_addr *gw6;
230653541Sshin
230753541Sshin	bzero(&sin6, sizeof(sin6));
230853541Sshin	sin6.sin6_family = AF_INET6;
230953541Sshin	sin6.sin6_len = sizeof(struct sockaddr_in6);
231053541Sshin	bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
231153541Sshin	rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
231253541Sshin	if (rt) {
231365895Sume		if (rt->rt_gateway == NULL ||
231465895Sume		    rt->rt_gateway->sa_family != AF_INET6) {
231578064Sume			nd6log((LOG_ERR,
231665895Sume			    "ICMP6 redirect rejected; no route "
231765895Sume			    "with inet6 gateway found for redirect dst: %s\n",
231878064Sume			    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2319120727Ssam			RTFREE_LOCKED(rt);
232078064Sume			goto bad;
232165895Sume		}
232265895Sume
232353541Sshin		gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
232453541Sshin		if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
232578064Sume			nd6log((LOG_ERR,
2326120891Sume			    "ICMP6 redirect rejected; "
2327120891Sume			    "not equal to gw-for-src=%s (must be same): "
2328120891Sume			    "%s\n",
2329120891Sume			    ip6_sprintf(gw6),
2330120891Sume			    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2331120727Ssam			RTFREE_LOCKED(rt);
233278064Sume			goto bad;
233353541Sshin		}
233453541Sshin	} else {
233578064Sume		nd6log((LOG_ERR,
2336120891Sume		    "ICMP6 redirect rejected; "
2337120891Sume		    "no route found for redirect dst: %s\n",
2338120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
233978064Sume		goto bad;
234053541Sshin	}
2341120727Ssam	RTFREE_LOCKED(rt);
234253541Sshin	rt = NULL;
234353541Sshin    }
234453541Sshin	if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
234578064Sume		nd6log((LOG_ERR,
2346120891Sume		    "ICMP6 redirect rejected; "
2347120891Sume		    "redirect dst must be unicast: %s\n",
2348120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
234978064Sume		goto bad;
235053541Sshin	}
235153541Sshin
235253541Sshin	is_router = is_onlink = 0;
235353541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
235453541Sshin		is_router = 1;	/* router case */
235553541Sshin	if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
235653541Sshin		is_onlink = 1;	/* on-link destination case */
235753541Sshin	if (!is_router && !is_onlink) {
235878064Sume		nd6log((LOG_ERR,
2359120891Sume		    "ICMP6 redirect rejected; "
2360120891Sume		    "neither router case nor onlink case: %s\n",
2361120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
236278064Sume		goto bad;
236353541Sshin	}
236453541Sshin	/* validation passed */
236553541Sshin
236653541Sshin	icmp6len -= sizeof(*nd_rd);
236753541Sshin	nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
236853541Sshin	if (nd6_options(&ndopts) < 0) {
236978064Sume		nd6log((LOG_INFO, "icmp6_redirect_input: "
2370120891Sume		    "invalid ND option, rejected: %s\n",
2371120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
237278064Sume		/* nd6_options have incremented stats */
237362587Sitojun		goto freeit;
237453541Sshin	}
237553541Sshin
237653541Sshin	if (ndopts.nd_opts_tgt_lladdr) {
237753541Sshin		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
237853541Sshin		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
237953541Sshin	}
238053541Sshin
238153541Sshin	if (ndopts.nd_opts_rh) {
238253541Sshin		redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
238353541Sshin		redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
238453541Sshin	}
238553541Sshin
238653541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
238778064Sume		nd6log((LOG_INFO,
2388120891Sume		    "icmp6_redirect_input: lladdrlen mismatch for %s "
2389120891Sume		    "(if %d, icmp6 packet %d): %s\n",
2390120891Sume		    ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2391120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
239278064Sume		goto bad;
239353541Sshin	}
239453541Sshin
239553541Sshin	/* RFC 2461 8.3 */
239653541Sshin	nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2397120891Sume	    is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
239853541Sshin
239995023Ssuz	if (!is_onlink) {	/* better router case.  perform rtredirect. */
240053541Sshin		/* perform rtredirect */
240153541Sshin		struct sockaddr_in6 sdst;
240253541Sshin		struct sockaddr_in6 sgw;
240353541Sshin		struct sockaddr_in6 ssrc;
240453541Sshin
240553541Sshin		bzero(&sdst, sizeof(sdst));
240653541Sshin		bzero(&sgw, sizeof(sgw));
240753541Sshin		bzero(&ssrc, sizeof(ssrc));
240853541Sshin		sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
240953541Sshin		sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
241053541Sshin			sizeof(struct sockaddr_in6);
241153541Sshin		bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
241253541Sshin		bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
241353541Sshin		bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
241453541Sshin		rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2415120891Sume		    (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2416120891Sume		    (struct sockaddr *)&ssrc);
241753541Sshin	}
241853541Sshin	/* finally update cached route in each socket via pfctlinput */
241953541Sshin    {
242053541Sshin	struct sockaddr_in6 sdst;
242153541Sshin
242253541Sshin	bzero(&sdst, sizeof(sdst));
242353541Sshin	sdst.sin6_family = AF_INET6;
242453541Sshin	sdst.sin6_len = sizeof(struct sockaddr_in6);
242553541Sshin	bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
242653541Sshin	pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2427122062Sume#if defined(IPSEC) || defined(FAST_IPSEC)
242853541Sshin	key_sa_routechange((struct sockaddr *)&sdst);
242953541Sshin#endif
243053541Sshin    }
243162587Sitojun
243262587Sitojun freeit:
243362587Sitojun	m_freem(m);
243478064Sume	return;
243578064Sume
243678064Sume bad:
243778064Sume	icmp6stat.icp6s_badredirect++;
243878064Sume	m_freem(m);
243953541Sshin}
244053541Sshin
244153541Sshinvoid
244253541Sshinicmp6_redirect_output(m0, rt)
244353541Sshin	struct mbuf *m0;
244453541Sshin	struct rtentry *rt;
244553541Sshin{
244653541Sshin	struct ifnet *ifp;	/* my outgoing interface */
244753541Sshin	struct in6_addr *ifp_ll6;
244853541Sshin	struct in6_addr *router_ll6;
244953541Sshin	struct ip6_hdr *sip6;	/* m0 as struct ip6_hdr */
245053541Sshin	struct mbuf *m = NULL;	/* newly allocated one */
245153541Sshin	struct ip6_hdr *ip6;	/* m as struct ip6_hdr */
245253541Sshin	struct nd_redirect *nd_rd;
245353541Sshin	size_t maxlen;
245453541Sshin	u_char *p;
245553541Sshin	struct ifnet *outif = NULL;
245662587Sitojun	struct sockaddr_in6 src_sa;
245753541Sshin
245862587Sitojun	icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
245962587Sitojun
246053541Sshin	/* if we are not router, we don't send icmp6 redirect */
246153541Sshin	if (!ip6_forwarding || ip6_accept_rtadv)
246253541Sshin		goto fail;
246353541Sshin
246453541Sshin	/* sanity check */
246553541Sshin	if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
246653541Sshin		goto fail;
246753541Sshin
246853541Sshin	/*
246953541Sshin	 * Address check:
247053541Sshin	 *  the source address must identify a neighbor, and
247153541Sshin	 *  the destination address must not be a multicast address
247253541Sshin	 *  [RFC 2461, sec 8.2]
247353541Sshin	 */
247453541Sshin	sip6 = mtod(m0, struct ip6_hdr *);
247562587Sitojun	bzero(&src_sa, sizeof(src_sa));
247662587Sitojun	src_sa.sin6_family = AF_INET6;
247762587Sitojun	src_sa.sin6_len = sizeof(src_sa);
247862587Sitojun	src_sa.sin6_addr = sip6->ip6_src;
247962587Sitojun	/* we don't currently use sin6_scope_id, but eventually use it */
2480121315Sume	if (in6_addr2zoneid(ifp, &sip6->ip6_src, &src_sa.sin6_scope_id))
2481121315Sume		goto fail;
248262587Sitojun	if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
248353541Sshin		goto fail;
248453541Sshin	if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
248553541Sshin		goto fail;	/* what should we do here? */
248653541Sshin
248753541Sshin	/* rate limit */
248853541Sshin	if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
248953541Sshin		goto fail;
249053541Sshin
249153541Sshin	/*
249253541Sshin	 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
249353541Sshin	 * we almost always ask for an mbuf cluster for simplicity.
249453541Sshin	 * (MHLEN < IPV6_MMTU is almost always true)
249553541Sshin	 */
249662587Sitojun#if IPV6_MMTU >= MCLBYTES
249762587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES
249862587Sitojun#endif
2499111119Simp	MGETHDR(m, M_DONTWAIT, MT_HEADER);
250062587Sitojun	if (m && IPV6_MMTU >= MHLEN)
2501111119Simp		MCLGET(m, M_DONTWAIT);
250253541Sshin	if (!m)
250353541Sshin		goto fail;
250478064Sume	m->m_pkthdr.rcvif = NULL;
250578064Sume	m->m_len = 0;
250678064Sume	maxlen = M_TRAILINGSPACE(m);
250753541Sshin	maxlen = min(IPV6_MMTU, maxlen);
250853541Sshin	/* just for safety */
250962587Sitojun	if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
251062587Sitojun	    ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
251153541Sshin		goto fail;
251262587Sitojun	}
251353541Sshin
251453541Sshin	{
251553541Sshin		/* get ip6 linklocal address for ifp(my outgoing interface). */
251662587Sitojun		struct in6_ifaddr *ia;
251762587Sitojun		if ((ia = in6ifa_ifpforlinklocal(ifp,
251862587Sitojun						 IN6_IFF_NOTREADY|
251962587Sitojun						 IN6_IFF_ANYCAST)) == NULL)
252053541Sshin			goto fail;
252153541Sshin		ifp_ll6 = &ia->ia_addr.sin6_addr;
252253541Sshin	}
252353541Sshin
252453541Sshin	/* get ip6 linklocal address for the router. */
252553541Sshin	if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
252653541Sshin		struct sockaddr_in6 *sin6;
252753541Sshin		sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
252853541Sshin		router_ll6 = &sin6->sin6_addr;
252953541Sshin		if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
253053541Sshin			router_ll6 = (struct in6_addr *)NULL;
253153541Sshin	} else
253253541Sshin		router_ll6 = (struct in6_addr *)NULL;
253353541Sshin
253453541Sshin	/* ip6 */
253553541Sshin	ip6 = mtod(m, struct ip6_hdr *);
253653541Sshin	ip6->ip6_flow = 0;
253762587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
253862587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
253953541Sshin	/* ip6->ip6_plen will be set later */
254053541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
254153541Sshin	ip6->ip6_hlim = 255;
254253541Sshin	/* ip6->ip6_src must be linklocal addr for my outgoing if. */
254353541Sshin	bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
254453541Sshin	bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
254553541Sshin
254653541Sshin	/* ND Redirect */
254753541Sshin	nd_rd = (struct nd_redirect *)(ip6 + 1);
254853541Sshin	nd_rd->nd_rd_type = ND_REDIRECT;
254953541Sshin	nd_rd->nd_rd_code = 0;
255053541Sshin	nd_rd->nd_rd_reserved = 0;
255153541Sshin	if (rt->rt_flags & RTF_GATEWAY) {
255253541Sshin		/*
255353541Sshin		 * nd_rd->nd_rd_target must be a link-local address in
255453541Sshin		 * better router cases.
255553541Sshin		 */
255653541Sshin		if (!router_ll6)
255753541Sshin			goto fail;
255853541Sshin		bcopy(router_ll6, &nd_rd->nd_rd_target,
2559120892Sume		    sizeof(nd_rd->nd_rd_target));
256053541Sshin		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2561120892Sume		    sizeof(nd_rd->nd_rd_dst));
256253541Sshin	} else {
256353541Sshin		/* make sure redtgt == reddst */
256453541Sshin		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2565120892Sume		    sizeof(nd_rd->nd_rd_target));
256653541Sshin		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2567120892Sume		    sizeof(nd_rd->nd_rd_dst));
256853541Sshin	}
256953541Sshin
257053541Sshin	p = (u_char *)(nd_rd + 1);
257153541Sshin
257253541Sshin	if (!router_ll6)
257353541Sshin		goto nolladdropt;
257453541Sshin
2575120892Sume	{
2576120892Sume		/* target lladdr option */
2577120892Sume		struct rtentry *rt_router = NULL;
2578120892Sume		int len;
2579120892Sume		struct sockaddr_dl *sdl;
2580120892Sume		struct nd_opt_hdr *nd_opt;
2581120892Sume		char *lladdr;
258253541Sshin
2583120892Sume		rt_router = nd6_lookup(router_ll6, 0, ifp);
2584120892Sume		if (!rt_router)
2585120892Sume			goto nolladdropt;
2586120892Sume		len = sizeof(*nd_opt) + ifp->if_addrlen;
2587120892Sume		len = (len + 7) & ~7;	/* round by 8 */
2588120892Sume		/* safety check */
2589120892Sume		if (len + (p - (u_char *)ip6) > maxlen)
2590120892Sume			goto nolladdropt;
2591120892Sume		if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2592120892Sume		    (rt_router->rt_flags & RTF_LLINFO) &&
2593120892Sume		    (rt_router->rt_gateway->sa_family == AF_LINK) &&
2594120892Sume		    (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2595120892Sume		    sdl->sdl_alen) {
2596120892Sume			nd_opt = (struct nd_opt_hdr *)p;
2597120892Sume			nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2598120892Sume			nd_opt->nd_opt_len = len >> 3;
2599120892Sume			lladdr = (char *)(nd_opt + 1);
2600120892Sume			bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2601120892Sume			p += len;
2602120892Sume		}
2603120893Sume	}
260453541Sshinnolladdropt:;
260553541Sshin
260653541Sshin	m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
260753541Sshin
260853541Sshin	/* just to be safe */
260962587Sitojun#ifdef M_DECRYPTED	/*not openbsd*/
261053541Sshin	if (m0->m_flags & M_DECRYPTED)
261153541Sshin		goto noredhdropt;
261262587Sitojun#endif
261362587Sitojun	if (p - (u_char *)ip6 > maxlen)
261462587Sitojun		goto noredhdropt;
261553541Sshin
2616120891Sume	{
2617120891Sume		/* redirected header option */
2618120891Sume		int len;
2619120891Sume		struct nd_opt_rd_hdr *nd_opt_rh;
262053541Sshin
2621120891Sume		/*
2622120891Sume		 * compute the maximum size for icmp6 redirect header option.
2623120891Sume		 * XXX room for auth header?
2624120891Sume		 */
2625120891Sume		len = maxlen - (p - (u_char *)ip6);
2626120891Sume		len &= ~7;
262753541Sshin
2628120891Sume		/* This is just for simplicity. */
2629120891Sume		if (m0->m_pkthdr.len != m0->m_len) {
2630120891Sume			if (m0->m_next) {
2631120891Sume				m_freem(m0->m_next);
2632120891Sume				m0->m_next = NULL;
2633120891Sume			}
2634120891Sume			m0->m_pkthdr.len = m0->m_len;
263553541Sshin		}
263653541Sshin
2637120891Sume		/*
2638120891Sume		 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2639120891Sume		 * about padding/truncate rule for the original IP packet.
2640120891Sume		 * From the discussion on IPv6imp in Feb 1999,
2641120891Sume		 * the consensus was:
2642120891Sume		 * - "attach as much as possible" is the goal
2643120891Sume		 * - pad if not aligned (original size can be guessed by
2644120891Sume		 *   original ip6 header)
2645120891Sume		 * Following code adds the padding if it is simple enough,
2646120891Sume		 * and truncates if not.
2647120891Sume		 */
2648120891Sume		if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2649120891Sume			panic("assumption failed in %s:%d", __FILE__,
2650120891Sume			    __LINE__);
265153541Sshin
2652120891Sume		if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2653120891Sume			/* not enough room, truncate */
2654120891Sume			m0->m_pkthdr.len = m0->m_len = len -
2655120891Sume			    sizeof(*nd_opt_rh);
2656120891Sume		} else {
2657120891Sume			/* enough room, pad or truncate */
2658120891Sume			size_t extra;
265953541Sshin
2660120891Sume			extra = m0->m_pkthdr.len % 8;
2661120891Sume			if (extra) {
2662120891Sume				/* pad if easy enough, truncate if not */
2663120891Sume				if (8 - extra <= M_TRAILINGSPACE(m0)) {
2664120891Sume					/* pad */
2665120891Sume					m0->m_len += (8 - extra);
2666120891Sume					m0->m_pkthdr.len += (8 - extra);
2667120891Sume				} else {
2668120891Sume					/* truncate */
2669120891Sume					m0->m_pkthdr.len -= extra;
2670120891Sume					m0->m_len -= extra;
2671120891Sume				}
267253541Sshin			}
2673120891Sume			len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2674120891Sume			m0->m_pkthdr.len = m0->m_len = len -
2675120891Sume			    sizeof(*nd_opt_rh);
267653541Sshin		}
267753541Sshin
2678120891Sume		nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2679120891Sume		bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2680120891Sume		nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2681120891Sume		nd_opt_rh->nd_opt_rh_len = len >> 3;
2682120891Sume		p += sizeof(*nd_opt_rh);
2683120891Sume		m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
268453541Sshin
2685120891Sume		/* connect m0 to m */
2686120891Sume		m_tag_delete_chain(m0, NULL);
2687120891Sume		m0->m_flags &= ~M_PKTHDR;
2688120891Sume		m->m_next = m0;
2689120891Sume		m->m_pkthdr.len = m->m_len + m0->m_len;
2690120891Sume		m0 = NULL;
2691120891Sume	}
269253541Sshinnoredhdropt:;
2693112781Ssuz	if (m0) {
2694112781Ssuz		m_freem(m0);
2695112781Ssuz		m0 = NULL;
2696112781Ssuz	}
269753541Sshin
2698121315Sume	/* XXX: clear embedded link IDs in the inner header */
2699121315Sume	in6_clearscope(&sip6->ip6_src);
2700121315Sume	in6_clearscope(&sip6->ip6_dst);
2701121315Sume	in6_clearscope(&nd_rd->nd_rd_target);
2702121315Sume	in6_clearscope(&nd_rd->nd_rd_dst);
270353541Sshin
270453541Sshin	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
270553541Sshin
270653541Sshin	nd_rd->nd_rd_cksum = 0;
2707120891Sume	nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2708120891Sume	    sizeof(*ip6), ntohs(ip6->ip6_plen));
270953541Sshin
271053541Sshin	/* send the packet to outside... */
2711105194Ssam	ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
271253541Sshin	if (outif) {
271353541Sshin		icmp6_ifstat_inc(outif, ifs6_out_msg);
271453541Sshin		icmp6_ifstat_inc(outif, ifs6_out_redirect);
271553541Sshin	}
271653541Sshin	icmp6stat.icp6s_outhist[ND_REDIRECT]++;
271753541Sshin
271853541Sshin	return;
271953541Sshin
272053541Sshinfail:
272153541Sshin	if (m)
272253541Sshin		m_freem(m);
272353541Sshin	if (m0)
272453541Sshin		m_freem(m0);
272553541Sshin}
272653541Sshin
272753541Sshin/*
272853541Sshin * ICMPv6 socket option processing.
272953541Sshin */
273053541Sshinint
273153541Sshinicmp6_ctloutput(so, sopt)
273253541Sshin	struct socket *so;
273353541Sshin	struct sockopt *sopt;
273453541Sshin{
273553541Sshin	int error = 0;
273653541Sshin	int optlen;
273778064Sume	struct inpcb *inp = sotoinpcb(so);
273853541Sshin	int level, op, optname;
273953541Sshin
274053541Sshin	if (sopt) {
274153541Sshin		level = sopt->sopt_level;
274253541Sshin		op = sopt->sopt_dir;
274353541Sshin		optname = sopt->sopt_name;
274453541Sshin		optlen = sopt->sopt_valsize;
274553541Sshin	} else
274653541Sshin		level = op = optname = optlen = 0;
274778064Sume
274853541Sshin	if (level != IPPROTO_ICMPV6) {
274953541Sshin		return EINVAL;
275053541Sshin	}
275153541Sshin
275278064Sume	switch (op) {
275353541Sshin	case PRCO_SETOPT:
275453541Sshin		switch (optname) {
275553541Sshin		case ICMP6_FILTER:
275653541Sshin		    {
275753541Sshin			struct icmp6_filter *p;
275853541Sshin
275953541Sshin			if (optlen != sizeof(*p)) {
276053541Sshin				error = EMSGSIZE;
276153541Sshin				break;
276253541Sshin			}
276353541Sshin			if (inp->in6p_icmp6filt == NULL) {
276453541Sshin				error = EINVAL;
276553541Sshin				break;
276653541Sshin			}
276753541Sshin			error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen,
276853541Sshin				optlen);
276953541Sshin			break;
277053541Sshin		    }
277153541Sshin
277253541Sshin		default:
277353541Sshin			error = ENOPROTOOPT;
277453541Sshin			break;
277553541Sshin		}
277653541Sshin		break;
277753541Sshin
277853541Sshin	case PRCO_GETOPT:
277953541Sshin		switch (optname) {
278053541Sshin		case ICMP6_FILTER:
278153541Sshin		    {
278253541Sshin			if (inp->in6p_icmp6filt == NULL) {
278353541Sshin				error = EINVAL;
278453541Sshin				break;
278553541Sshin			}
278653541Sshin			error = sooptcopyout(sopt, inp->in6p_icmp6filt,
278753541Sshin				sizeof(struct icmp6_filter));
278853541Sshin			break;
278953541Sshin		    }
279053541Sshin
279153541Sshin		default:
279253541Sshin			error = ENOPROTOOPT;
279353541Sshin			break;
279453541Sshin		}
279553541Sshin		break;
279653541Sshin	}
279753541Sshin
2798120856Sume	return (error);
279953541Sshin}
280053541Sshin
280153541Sshin/*
280253541Sshin * Perform rate limit check.
280353541Sshin * Returns 0 if it is okay to send the icmp6 packet.
280453541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
280553541Sshin * limitation.
280653541Sshin *
280753541Sshin * XXX per-destination/type check necessary?
280853541Sshin */
280953541Sshinstatic int
281053541Sshinicmp6_ratelimit(dst, type, code)
281153541Sshin	const struct in6_addr *dst;	/* not used at this moment */
281253541Sshin	const int type;			/* not used at this moment */
281353541Sshin	const int code;			/* not used at this moment */
281453541Sshin{
281562587Sitojun	int ret;
281653541Sshin
281795023Ssuz	ret = 0;	/* okay to send */
281853541Sshin
281962587Sitojun	/* PPS limit */
282078064Sume	if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
282178064Sume	    icmp6errppslim)) {
282253541Sshin		/* The packet is subject to rate limit */
282362587Sitojun		ret++;
282453541Sshin	}
282553541Sshin
282662587Sitojun	return ret;
282753541Sshin}
2828