icmp6.c revision 196019
1139826Simp/*-
253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
353541Sshin * All rights reserved.
453541Sshin *
553541Sshin * Redistribution and use in source and binary forms, with or without
653541Sshin * modification, are permitted provided that the following conditions
753541Sshin * are met:
853541Sshin * 1. Redistributions of source code must retain the above copyright
953541Sshin *    notice, this list of conditions and the following disclaimer.
1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1153541Sshin *    notice, this list of conditions and the following disclaimer in the
1253541Sshin *    documentation and/or other materials provided with the distribution.
1353541Sshin * 3. Neither the name of the project nor the names of its contributors
1453541Sshin *    may be used to endorse or promote products derived from this software
1553541Sshin *    without specific prior written permission.
1653541Sshin *
1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2053541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2753541Sshin * SUCH DAMAGE.
28174510Sobrien *
29174510Sobrien *	$KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $
3053541Sshin */
3153541Sshin
32139826Simp/*-
3353541Sshin * Copyright (c) 1982, 1986, 1988, 1993
3453541Sshin *	The Regents of the University of California.  All rights reserved.
3553541Sshin *
3653541Sshin * Redistribution and use in source and binary forms, with or without
3753541Sshin * modification, are permitted provided that the following conditions
3853541Sshin * are met:
3953541Sshin * 1. Redistributions of source code must retain the above copyright
4053541Sshin *    notice, this list of conditions and the following disclaimer.
4153541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4253541Sshin *    notice, this list of conditions and the following disclaimer in the
4353541Sshin *    documentation and/or other materials provided with the distribution.
4453541Sshin * 4. Neither the name of the University nor the names of its contributors
4553541Sshin *    may be used to endorse or promote products derived from this software
4653541Sshin *    without specific prior written permission.
4753541Sshin *
4853541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4953541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5053541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5153541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5253541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5353541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5453541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5553541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5653541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5753541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5853541Sshin * SUCH DAMAGE.
5953541Sshin *
6053541Sshin *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
6153541Sshin */
6253541Sshin
63174510Sobrien#include <sys/cdefs.h>
64174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/icmp6.c 196019 2009-08-01 19:26:27Z rwatson $");
65174510Sobrien
6662587Sitojun#include "opt_inet.h"
6762587Sitojun#include "opt_inet6.h"
6855009Sshin#include "opt_ipsec.h"
6953541Sshin
7053541Sshin#include <sys/param.h>
7195759Stanimura#include <sys/domain.h>
72193066Sjamie#include <sys/jail.h>
7395759Stanimura#include <sys/kernel.h>
7495759Stanimura#include <sys/lock.h>
7578064Sume#include <sys/malloc.h>
7653541Sshin#include <sys/mbuf.h>
77196019Srwatson#include <sys/proc.h>
7853541Sshin#include <sys/protosw.h>
7995759Stanimura#include <sys/signalvar.h>
8053541Sshin#include <sys/socket.h>
8153541Sshin#include <sys/socketvar.h>
8295759Stanimura#include <sys/sx.h>
8395759Stanimura#include <sys/syslog.h>
8495759Stanimura#include <sys/systm.h>
8553541Sshin#include <sys/time.h>
8653541Sshin
8753541Sshin#include <net/if.h>
8853541Sshin#include <net/if_dl.h>
89186119Sqingli#include <net/if_llatbl.h>
9053541Sshin#include <net/if_types.h>
9195759Stanimura#include <net/route.h>
92185571Sbz#include <net/vnet.h>
9353541Sshin
9453541Sshin#include <netinet/in.h>
9595759Stanimura#include <netinet/in_pcb.h>
9653541Sshin#include <netinet/in_var.h>
9762587Sitojun#include <netinet/ip6.h>
9895759Stanimura#include <netinet/icmp6.h>
99122922Sandre#include <netinet/tcp_var.h>
100185571Sbz
10195759Stanimura#include <netinet6/in6_ifattach.h>
10295759Stanimura#include <netinet6/in6_pcb.h>
10395759Stanimura#include <netinet6/ip6protosw.h>
10453541Sshin#include <netinet6/ip6_var.h>
105148385Sume#include <netinet6/scope6_var.h>
10653541Sshin#include <netinet6/mld6_var.h>
10753541Sshin#include <netinet6/nd6.h>
10853541Sshin
109171167Sgnn#ifdef IPSEC
110105199Ssam#include <netipsec/ipsec.h>
111105199Ssam#include <netipsec/key.h>
112105199Ssam#endif
113105199Ssam
11462587Sitojunextern struct domain inet6domain;
115185348Szec
116195699SrwatsonVNET_DECLARE(struct inpcbinfo, ripcbinfo);
117195699SrwatsonVNET_DECLARE(struct inpcbhead, ripcb);
118195699SrwatsonVNET_DECLARE(int, icmp6errppslim);
119195699SrwatsonVNET_DECLARE(int, icmp6_nodeinfo);
12053541Sshin
121195727Srwatson#define	V_ripcbinfo			VNET(ripcbinfo)
122195727Srwatson#define	V_ripcb				VNET(ripcb)
123195727Srwatson#define	V_icmp6errppslim		VNET(icmp6errppslim)
124195727Srwatson#define	V_icmp6_nodeinfo		VNET(icmp6_nodeinfo)
125185088Szec
126195699SrwatsonVNET_DEFINE(struct icmp6stat, icmp6stat);
127195699Srwatsonstatic VNET_DEFINE(int, icmp6errpps_count);
128195699Srwatsonstatic VNET_DEFINE(struct timeval, icmp6errppslim_last);
129195699Srwatson
130195727Srwatson#define	V_icmp6errpps_count	VNET(icmp6errpps_count)
131195727Srwatson#define	V_icmp6errppslim_last	VNET(icmp6errppslim_last)
132195699Srwatson
133175162Sobrienstatic void icmp6_errcount(struct icmp6errstat *, int, int);
134175162Sobrienstatic int icmp6_rip6_input(struct mbuf **, int);
135175162Sobrienstatic int icmp6_ratelimit(const struct in6_addr *, const int, const int);
13662587Sitojunstatic const char *icmp6_redirect_diag __P((struct in6_addr *,
13762587Sitojun	struct in6_addr *, struct in6_addr *));
138175162Sobrienstatic struct mbuf *ni6_input(struct mbuf *, int);
139175162Sobrienstatic struct mbuf *ni6_nametodns(const char *, int, int);
140175162Sobrienstatic int ni6_dnsmatch(const char *, int, const char *, int);
14162587Sitojunstatic int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
142148892Sume			  struct ifnet **, struct in6_addr *));
14362587Sitojunstatic int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
14462587Sitojun				struct ifnet *, int));
145175162Sobrienstatic int icmp6_notify_error(struct mbuf **, int, int, int);
14653541Sshin
14753541Sshin
14853541Sshinvoid
149171259Sdelphijicmp6_init(void)
15053541Sshin{
151171259Sdelphij
152185088Szec	V_icmp6errpps_count = 0;
15353541Sshin}
15453541Sshin
15562587Sitojunstatic void
156171259Sdelphijicmp6_errcount(struct icmp6errstat *stat, int type, int code)
15762587Sitojun{
15878064Sume	switch (type) {
15962587Sitojun	case ICMP6_DST_UNREACH:
16062587Sitojun		switch (code) {
16162587Sitojun		case ICMP6_DST_UNREACH_NOROUTE:
16262587Sitojun			stat->icp6errs_dst_unreach_noroute++;
16362587Sitojun			return;
16462587Sitojun		case ICMP6_DST_UNREACH_ADMIN:
16562587Sitojun			stat->icp6errs_dst_unreach_admin++;
16662587Sitojun			return;
16762587Sitojun		case ICMP6_DST_UNREACH_BEYONDSCOPE:
16862587Sitojun			stat->icp6errs_dst_unreach_beyondscope++;
16962587Sitojun			return;
17062587Sitojun		case ICMP6_DST_UNREACH_ADDR:
17162587Sitojun			stat->icp6errs_dst_unreach_addr++;
17262587Sitojun			return;
17362587Sitojun		case ICMP6_DST_UNREACH_NOPORT:
17462587Sitojun			stat->icp6errs_dst_unreach_noport++;
17562587Sitojun			return;
17662587Sitojun		}
17762587Sitojun		break;
17862587Sitojun	case ICMP6_PACKET_TOO_BIG:
17962587Sitojun		stat->icp6errs_packet_too_big++;
18062587Sitojun		return;
18162587Sitojun	case ICMP6_TIME_EXCEEDED:
18278064Sume		switch (code) {
18362587Sitojun		case ICMP6_TIME_EXCEED_TRANSIT:
18462587Sitojun			stat->icp6errs_time_exceed_transit++;
18562587Sitojun			return;
18662587Sitojun		case ICMP6_TIME_EXCEED_REASSEMBLY:
18762587Sitojun			stat->icp6errs_time_exceed_reassembly++;
18862587Sitojun			return;
18962587Sitojun		}
19062587Sitojun		break;
19162587Sitojun	case ICMP6_PARAM_PROB:
19278064Sume		switch (code) {
19362587Sitojun		case ICMP6_PARAMPROB_HEADER:
19462587Sitojun			stat->icp6errs_paramprob_header++;
19562587Sitojun			return;
19662587Sitojun		case ICMP6_PARAMPROB_NEXTHEADER:
19762587Sitojun			stat->icp6errs_paramprob_nextheader++;
19862587Sitojun			return;
19962587Sitojun		case ICMP6_PARAMPROB_OPTION:
20062587Sitojun			stat->icp6errs_paramprob_option++;
20162587Sitojun			return;
20262587Sitojun		}
20362587Sitojun		break;
20462587Sitojun	case ND_REDIRECT:
20562587Sitojun		stat->icp6errs_redirect++;
20662587Sitojun		return;
20762587Sitojun	}
20862587Sitojun	stat->icp6errs_unknown++;
20962587Sitojun}
21062587Sitojun
21153541Sshin/*
212148385Sume * A wrapper function for icmp6_error() necessary when the erroneous packet
213148385Sume * may not contain enough scope zone information.
214148385Sume */
215148385Sumevoid
216171259Sdelphijicmp6_error2(struct mbuf *m, int type, int code, int param,
217171259Sdelphij    struct ifnet *ifp)
218148385Sume{
219148385Sume	struct ip6_hdr *ip6;
220148385Sume
221148385Sume	if (ifp == NULL)
222148385Sume		return;
223148385Sume
224148385Sume#ifndef PULLDOWN_TEST
225148385Sume	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
226148385Sume#else
227148385Sume	if (m->m_len < sizeof(struct ip6_hdr)) {
228148385Sume		m = m_pullup(m, sizeof(struct ip6_hdr));
229148385Sume		if (m == NULL)
230148385Sume			return;
231148385Sume	}
232148385Sume#endif
233148385Sume
234148385Sume	ip6 = mtod(m, struct ip6_hdr *);
235148385Sume
236148385Sume	if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
237148385Sume		return;
238148385Sume	if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
239148385Sume		return;
240148385Sume
241148385Sume	icmp6_error(m, type, code, param);
242148385Sume}
243148385Sume
244148385Sume/*
24553541Sshin * Generate an error packet of type error in response to bad IP6 packet.
24653541Sshin */
24753541Sshinvoid
248171259Sdelphijicmp6_error(struct mbuf *m, int type, int code, int param)
24953541Sshin{
25053541Sshin	struct ip6_hdr *oip6, *nip6;
25153541Sshin	struct icmp6_hdr *icmp6;
25262587Sitojun	u_int preplen;
25353541Sshin	int off;
25462587Sitojun	int nxt;
25553541Sshin
256190964Srwatson	ICMP6STAT_INC(icp6s_error);
25753541Sshin
25862587Sitojun	/* count per-type-code statistics */
259181803Sbz	icmp6_errcount(&V_icmp6stat.icp6s_outerrhist, type, code);
26062587Sitojun
26162587Sitojun#ifdef M_DECRYPTED	/*not openbsd*/
26262587Sitojun	if (m->m_flags & M_DECRYPTED) {
263190964Srwatson		ICMP6STAT_INC(icp6s_canterror);
26453541Sshin		goto freeit;
26562587Sitojun	}
26662587Sitojun#endif
26753541Sshin
26862587Sitojun#ifndef PULLDOWN_TEST
26962587Sitojun	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
27062587Sitojun#else
27162587Sitojun	if (m->m_len < sizeof(struct ip6_hdr)) {
27262587Sitojun		m = m_pullup(m, sizeof(struct ip6_hdr));
27362587Sitojun		if (m == NULL)
27462587Sitojun			return;
27562587Sitojun	}
27662587Sitojun#endif
27753541Sshin	oip6 = mtod(m, struct ip6_hdr *);
27853541Sshin
27953541Sshin	/*
280121472Sume	 * If the destination address of the erroneous packet is a multicast
281121472Sume	 * address, or the packet was sent using link-layer multicast,
282121472Sume	 * we should basically suppress sending an error (RFC 2463, Section
283121472Sume	 * 2.4).
284121472Sume	 * We have two exceptions (the item e.2 in that section):
285121472Sume	 * - the Pakcet Too Big message can be sent for path MTU discovery.
286121472Sume	 * - the Parameter Problem Message that can be allowed an icmp6 error
287121472Sume	 *   in the option type field.  This check has been done in
288121472Sume	 *   ip6_unknown_opt(), so we can just check the type and code.
28953541Sshin	 */
29053541Sshin	if ((m->m_flags & (M_BCAST|M_MCAST) ||
29153541Sshin	     IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
29253541Sshin	    (type != ICMP6_PACKET_TOO_BIG &&
29353541Sshin	     (type != ICMP6_PARAM_PROB ||
29453541Sshin	      code != ICMP6_PARAMPROB_OPTION)))
29553541Sshin		goto freeit;
29653541Sshin
297121472Sume	/*
298121472Sume	 * RFC 2463, 2.4 (e.5): source address check.
299121472Sume	 * XXX: the case of anycast source?
300121472Sume	 */
30153541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
30253541Sshin	    IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
30353541Sshin		goto freeit;
30453541Sshin
30553541Sshin	/*
30662587Sitojun	 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
30762587Sitojun	 * don't do it.
30853541Sshin	 */
30962587Sitojun	nxt = -1;
31062587Sitojun	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
31162587Sitojun	if (off >= 0 && nxt == IPPROTO_ICMPV6) {
31253541Sshin		struct icmp6_hdr *icp;
31353541Sshin
31462587Sitojun#ifndef PULLDOWN_TEST
31562587Sitojun		IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
31662587Sitojun		icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
31762587Sitojun#else
31862587Sitojun		IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
31962587Sitojun			sizeof(*icp));
32062587Sitojun		if (icp == NULL) {
321190964Srwatson			ICMP6STAT_INC(icp6s_tooshort);
32262587Sitojun			return;
32362587Sitojun		}
32462587Sitojun#endif
32562587Sitojun		if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
32662587Sitojun		    icp->icmp6_type == ND_REDIRECT) {
32753541Sshin			/*
32862587Sitojun			 * ICMPv6 error
32962587Sitojun			 * Special case: for redirect (which is
33062587Sitojun			 * informational) we must not send icmp6 error.
33153541Sshin			 */
332190964Srwatson			ICMP6STAT_INC(icp6s_canterror);
33362587Sitojun			goto freeit;
33462587Sitojun		} else {
33562587Sitojun			/* ICMPv6 informational - send the error */
33653541Sshin		}
33762587Sitojun	} else {
33862587Sitojun		/* non-ICMPv6 - send the error */
33953541Sshin	}
34053541Sshin
34153541Sshin	oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
34253541Sshin
34353541Sshin	/* Finally, do rate limitation check. */
34453541Sshin	if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
345190964Srwatson		ICMP6STAT_INC(icp6s_toofreq);
34653541Sshin		goto freeit;
34753541Sshin	}
34853541Sshin
34953541Sshin	/*
35053541Sshin	 * OK, ICMP6 can be generated.
35153541Sshin	 */
35253541Sshin
35353541Sshin	if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
35453541Sshin		m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
35553541Sshin
35662587Sitojun	preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
357111119Simp	M_PREPEND(m, preplen, M_DONTWAIT);
35862587Sitojun	if (m && m->m_len < preplen)
35962587Sitojun		m = m_pullup(m, preplen);
36053541Sshin	if (m == NULL) {
36178064Sume		nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
36253541Sshin		return;
36353541Sshin	}
36453541Sshin
36553541Sshin	nip6 = mtod(m, struct ip6_hdr *);
36653541Sshin	nip6->ip6_src  = oip6->ip6_src;
36753541Sshin	nip6->ip6_dst  = oip6->ip6_dst;
36853541Sshin
369121315Sume	in6_clearscope(&oip6->ip6_src);
370121315Sume	in6_clearscope(&oip6->ip6_dst);
37153541Sshin
37253541Sshin	icmp6 = (struct icmp6_hdr *)(nip6 + 1);
37353541Sshin	icmp6->icmp6_type = type;
37453541Sshin	icmp6->icmp6_code = code;
37553541Sshin	icmp6->icmp6_pptr = htonl((u_int32_t)param);
37653541Sshin
37778064Sume	/*
37878064Sume	 * icmp6_reflect() is designed to be in the input path.
379148987Sume	 * icmp6_error() can be called from both input and output path,
38078064Sume	 * and if we are in output path rcvif could contain bogus value.
38178064Sume	 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
38278064Sume	 * information in ip header (nip6).
38378064Sume	 */
38478064Sume	m->m_pkthdr.rcvif = NULL;
38578064Sume
386190964Srwatson	ICMP6STAT_INC(icp6s_outhist[type]);
38795023Ssuz	icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
38862587Sitojun
38962587Sitojun	return;
39062587Sitojun
39162587Sitojun  freeit:
39262587Sitojun	/*
393148987Sume	 * If we can't tell whether or not we can generate ICMP6, free it.
39462587Sitojun	 */
39562587Sitojun	m_freem(m);
39653541Sshin}
39753541Sshin
39853541Sshin/*
39953541Sshin * Process a received ICMP6 message.
40053541Sshin */
40153541Sshinint
402171259Sdelphijicmp6_input(struct mbuf **mp, int *offp, int proto)
40353541Sshin{
40453541Sshin	struct mbuf *m = *mp, *n;
405192923Sbms	struct ifnet *ifp;
40653541Sshin	struct ip6_hdr *ip6, *nip6;
40753541Sshin	struct icmp6_hdr *icmp6, *nicmp6;
40853541Sshin	int off = *offp;
40953541Sshin	int icmp6len = m->m_pkthdr.len - *offp;
41053541Sshin	int code, sum, noff;
411165118Sbz	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
41253541Sshin
413192923Sbms	ifp = m->m_pkthdr.rcvif;
414192923Sbms
41562587Sitojun#ifndef PULLDOWN_TEST
41653541Sshin	IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
41795023Ssuz	/* m might change if M_LOOP.  So, call mtod after this */
41862587Sitojun#endif
41953541Sshin
42053541Sshin	/*
42153541Sshin	 * Locate icmp6 structure in mbuf, and check
42253541Sshin	 * that not corrupted and of at least minimum length
42353541Sshin	 */
42453541Sshin
42553541Sshin	ip6 = mtod(m, struct ip6_hdr *);
42653541Sshin	if (icmp6len < sizeof(struct icmp6_hdr)) {
427190964Srwatson		ICMP6STAT_INC(icp6s_tooshort);
42853541Sshin		goto freeit;
42953541Sshin	}
43053541Sshin
43153541Sshin	/*
432191672Sbms	 * Check multicast group membership.
433191672Sbms	 * Note: SSM filters are not applied for ICMPv6 traffic.
434191672Sbms	 */
435191672Sbms	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
436192923Sbms		struct in6_multi	*inm;
437191672Sbms
438191672Sbms		inm = in6m_lookup(ifp, &ip6->ip6_dst);
439191672Sbms		if (inm == NULL) {
440191672Sbms			IP6STAT_INC(ip6s_notmember);
441191672Sbms			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
442191672Sbms			goto freeit;
443191672Sbms		}
444191672Sbms	}
445191672Sbms
446191672Sbms	/*
44753541Sshin	 * calculate the checksum
44853541Sshin	 */
44962587Sitojun#ifndef PULLDOWN_TEST
45053541Sshin	icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
45162587Sitojun#else
45262587Sitojun	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
45362587Sitojun	if (icmp6 == NULL) {
454190964Srwatson		ICMP6STAT_INC(icp6s_tooshort);
45562587Sitojun		return IPPROTO_DONE;
45662587Sitojun	}
45762587Sitojun#endif
45853541Sshin	code = icmp6->icmp6_code;
45953541Sshin
46053541Sshin	if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
46178064Sume		nd6log((LOG_ERR,
46253541Sshin		    "ICMP6 checksum error(%d|%x) %s\n",
463165118Sbz		    icmp6->icmp6_type, sum,
464165118Sbz		    ip6_sprintf(ip6bufs, &ip6->ip6_src)));
465190964Srwatson		ICMP6STAT_INC(icp6s_checksum);
46653541Sshin		goto freeit;
46753541Sshin	}
46853541Sshin
46983934Sbrooks	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
47053541Sshin		/*
47153541Sshin		 * Deliver very specific ICMP6 type only.
472148987Sume		 * This is important to deliver TOOBIG.  Otherwise PMTUD
47353541Sshin		 * will not work.
47453541Sshin		 */
47553541Sshin		switch (icmp6->icmp6_type) {
47653541Sshin		case ICMP6_DST_UNREACH:
47753541Sshin		case ICMP6_PACKET_TOO_BIG:
47853541Sshin		case ICMP6_TIME_EXCEEDED:
47953541Sshin			break;
48053541Sshin		default:
48153541Sshin			goto freeit;
48253541Sshin		}
48353541Sshin	}
48453541Sshin
485190964Srwatson	ICMP6STAT_INC(icp6s_inhist[icmp6->icmp6_type]);
486192923Sbms	icmp6_ifstat_inc(ifp, ifs6_in_msg);
48753541Sshin	if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
488192923Sbms		icmp6_ifstat_inc(ifp, ifs6_in_error);
48953541Sshin
49053541Sshin	switch (icmp6->icmp6_type) {
49153541Sshin	case ICMP6_DST_UNREACH:
492192923Sbms		icmp6_ifstat_inc(ifp, ifs6_in_dstunreach);
49353541Sshin		switch (code) {
49453541Sshin		case ICMP6_DST_UNREACH_NOROUTE:
49553541Sshin			code = PRC_UNREACH_NET;
49653541Sshin			break;
49753541Sshin		case ICMP6_DST_UNREACH_ADMIN:
498192923Sbms			icmp6_ifstat_inc(ifp, ifs6_in_adminprohib);
49962587Sitojun			code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
50062587Sitojun			break;
50153541Sshin		case ICMP6_DST_UNREACH_ADDR:
50262587Sitojun			code = PRC_HOSTDEAD;
50353541Sshin			break;
50462587Sitojun		case ICMP6_DST_UNREACH_BEYONDSCOPE:
50562587Sitojun			/* I mean "source address was incorrect." */
50662587Sitojun			code = PRC_PARAMPROB;
50762587Sitojun			break;
50853541Sshin		case ICMP6_DST_UNREACH_NOPORT:
50953541Sshin			code = PRC_UNREACH_PORT;
51053541Sshin			break;
51153541Sshin		default:
51253541Sshin			goto badcode;
51353541Sshin		}
51453541Sshin		goto deliver;
51553541Sshin		break;
51653541Sshin
51753541Sshin	case ICMP6_PACKET_TOO_BIG:
518192923Sbms		icmp6_ifstat_inc(ifp, ifs6_in_pkttoobig);
51953541Sshin
520126194Sume		/* validation is made in icmp6_mtudisc_update */
521126194Sume
52253541Sshin		code = PRC_MSGSIZE;
52353541Sshin
52462587Sitojun		/*
52562587Sitojun		 * Updating the path MTU will be done after examining
52662587Sitojun		 * intermediate extension headers.
52762587Sitojun		 */
52853541Sshin		goto deliver;
52953541Sshin		break;
53053541Sshin
53153541Sshin	case ICMP6_TIME_EXCEEDED:
532192923Sbms		icmp6_ifstat_inc(ifp, ifs6_in_timeexceed);
53353541Sshin		switch (code) {
53453541Sshin		case ICMP6_TIME_EXCEED_TRANSIT:
535151465Ssuz			code = PRC_TIMXCEED_INTRANS;
536151465Ssuz			break;
53753541Sshin		case ICMP6_TIME_EXCEED_REASSEMBLY:
538151465Ssuz			code = PRC_TIMXCEED_REASS;
53953541Sshin			break;
54053541Sshin		default:
54153541Sshin			goto badcode;
54253541Sshin		}
54353541Sshin		goto deliver;
54453541Sshin		break;
54553541Sshin
54653541Sshin	case ICMP6_PARAM_PROB:
547192923Sbms		icmp6_ifstat_inc(ifp, ifs6_in_paramprob);
54853541Sshin		switch (code) {
54953541Sshin		case ICMP6_PARAMPROB_NEXTHEADER:
55053541Sshin			code = PRC_UNREACH_PROTOCOL;
55153541Sshin			break;
55253541Sshin		case ICMP6_PARAMPROB_HEADER:
55353541Sshin		case ICMP6_PARAMPROB_OPTION:
55453541Sshin			code = PRC_PARAMPROB;
55553541Sshin			break;
55653541Sshin		default:
55753541Sshin			goto badcode;
55853541Sshin		}
55953541Sshin		goto deliver;
56053541Sshin		break;
56153541Sshin
56253541Sshin	case ICMP6_ECHO_REQUEST:
563192923Sbms		icmp6_ifstat_inc(ifp, ifs6_in_echo);
56453541Sshin		if (code != 0)
56553541Sshin			goto badcode;
56653541Sshin		if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
56753541Sshin			/* Give up remote */
56853541Sshin			break;
56953541Sshin		}
57062587Sitojun		if ((n->m_flags & M_EXT) != 0
57162587Sitojun		 || n->m_len < off + sizeof(struct icmp6_hdr)) {
57253541Sshin			struct mbuf *n0 = n;
57362587Sitojun			const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
574108741Ssam			int n0len;
57553541Sshin
576111119Simp			MGETHDR(n, M_DONTWAIT, n0->m_type);
577111070Ssam			n0len = n0->m_pkthdr.len;	/* save for use below */
578111070Ssam			if (n)
579111070Ssam				M_MOVE_PKTHDR(n, n0);
58062587Sitojun			if (n && maxlen >= MHLEN) {
581111119Simp				MCLGET(n, M_DONTWAIT);
58262587Sitojun				if ((n->m_flags & M_EXT) == 0) {
58362587Sitojun					m_free(n);
58462587Sitojun					n = NULL;
58562587Sitojun				}
58662587Sitojun			}
58753541Sshin			if (n == NULL) {
58853541Sshin				/* Give up remote */
58953541Sshin				m_freem(n0);
59053541Sshin				break;
59153541Sshin			}
59253541Sshin			/*
59353541Sshin			 * Copy IPv6 and ICMPv6 only.
59453541Sshin			 */
59553541Sshin			nip6 = mtod(n, struct ip6_hdr *);
59653541Sshin			bcopy(ip6, nip6, sizeof(struct ip6_hdr));
59753541Sshin			nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
59853541Sshin			bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
59962587Sitojun			noff = sizeof(struct ip6_hdr);
600108741Ssam			/* new mbuf contains only ipv6+icmpv6 headers */
601108741Ssam			n->m_len = noff + sizeof(struct icmp6_hdr);
60253541Sshin			/*
603120891Sume			 * Adjust mbuf.  ip6_plen will be adjusted in
60462587Sitojun			 * ip6_output().
60553541Sshin			 */
60662587Sitojun			m_adj(n0, off + sizeof(struct icmp6_hdr));
607108741Ssam			/* recalculate complete packet size */
608108741Ssam			n->m_pkthdr.len = n0len + (noff - off);
60962587Sitojun			n->m_next = n0;
61053541Sshin		} else {
61153541Sshin			nip6 = mtod(n, struct ip6_hdr *);
612166046Sume			IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off,
613166046Sume			    sizeof(*nicmp6));
61453541Sshin			noff = off;
61553541Sshin		}
61653541Sshin		nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
61753541Sshin		nicmp6->icmp6_code = 0;
61853541Sshin		if (n) {
619190964Srwatson			ICMP6STAT_INC(icp6s_reflect);
620190964Srwatson			ICMP6STAT_INC(icp6s_outhist[ICMP6_ECHO_REPLY]);
62153541Sshin			icmp6_reflect(n, noff);
62253541Sshin		}
62353541Sshin		break;
62453541Sshin
62553541Sshin	case ICMP6_ECHO_REPLY:
626192923Sbms		icmp6_ifstat_inc(ifp, ifs6_in_echoreply);
62753541Sshin		if (code != 0)
62853541Sshin			goto badcode;
62953541Sshin		break;
63053541Sshin
63196116Sume	case MLD_LISTENER_QUERY:
63296116Sume	case MLD_LISTENER_REPORT:
633191672Sbms	case MLD_LISTENER_DONE:
634191672Sbms	case MLDV2_LISTENER_REPORT:
635191672Sbms		/*
636192923Sbms		 * Drop MLD traffic which is not link-local, has a hop limit
637192923Sbms		 * of greater than 1 hop, or which does not have the
638192923Sbms		 * IPv6 HBH Router Alert option.
639192923Sbms		 * As IPv6 HBH options are stripped in ip6_input() we must
640192923Sbms		 * check an mbuf header flag.
641191672Sbms		 * XXX Should we also sanity check that these messages
642191672Sbms		 * were directed to a link-local multicast prefix?
643191672Sbms		 */
644192923Sbms		if ((ip6->ip6_hlim != 1) || (m->m_flags & M_RTALERT_MLD) == 0)
64562587Sitojun			goto freeit;
646191672Sbms		if (mld_input(m, off, icmp6len) != 0)
647191672Sbms			return (IPPROTO_DONE);
64853541Sshin		/* m stays. */
64953541Sshin		break;
65053541Sshin
65153541Sshin	case ICMP6_WRUREQUEST:	/* ICMP6_FQDN_QUERY */
65253541Sshin	    {
65353541Sshin		enum { WRU, FQDN } mode;
65453541Sshin
655181803Sbz		if (!V_icmp6_nodeinfo)
65662587Sitojun			break;
65762587Sitojun
65853541Sshin		if (icmp6len == sizeof(struct icmp6_hdr) + 4)
65953541Sshin			mode = WRU;
66062587Sitojun		else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
66153541Sshin			mode = FQDN;
66253541Sshin		else
66353541Sshin			goto badlen;
66453541Sshin
66553541Sshin		if (mode == FQDN) {
66662587Sitojun#ifndef PULLDOWN_TEST
66753541Sshin			IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
668120891Sume			    IPPROTO_DONE);
66962587Sitojun#endif
67062587Sitojun			n = m_copy(m, 0, M_COPYALL);
67162587Sitojun			if (n)
67262587Sitojun				n = ni6_input(n, off);
67362587Sitojun			/* XXX meaningless if n == NULL */
67453541Sshin			noff = sizeof(struct ip6_hdr);
67553541Sshin		} else {
676193066Sjamie			struct prison *pr;
67753541Sshin			u_char *p;
678193066Sjamie			int maxlen, maxhlen, hlen;
67953541Sshin
680169664Sjinmei			/*
681169664Sjinmei			 * XXX: this combination of flags is pointless,
682169664Sjinmei			 * but should we keep this for compatibility?
683169664Sjinmei			 */
684181803Sbz			if ((V_icmp6_nodeinfo & 5) != 5)
68578064Sume				break;
68678064Sume
68762587Sitojun			if (code != 0)
68862587Sitojun				goto badcode;
68962587Sitojun			maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
69062587Sitojun			if (maxlen >= MCLBYTES) {
69162587Sitojun				/* Give up remote */
69262587Sitojun				break;
69362587Sitojun			}
694111119Simp			MGETHDR(n, M_DONTWAIT, m->m_type);
69562587Sitojun			if (n && maxlen > MHLEN) {
696111119Simp				MCLGET(n, M_DONTWAIT);
69762587Sitojun				if ((n->m_flags & M_EXT) == 0) {
69862587Sitojun					m_free(n);
69962587Sitojun					n = NULL;
70062587Sitojun				}
70162587Sitojun			}
702166619Sbms			if (n && !m_dup_pkthdr(n, m, M_DONTWAIT)) {
703108466Ssam				/*
704108466Ssam				 * Previous code did a blind M_COPY_PKTHDR
705108466Ssam				 * and said "just for rcvif".  If true, then
706108466Ssam				 * we could tolerate the dup failing (due to
707108466Ssam				 * the deep copy of the tag chain).  For now
708108466Ssam				 * be conservative and just fail.
709108466Ssam				 */
710108466Ssam				m_free(n);
711108466Ssam				n = NULL;
712108466Ssam			}
71353541Sshin			if (n == NULL) {
71453541Sshin				/* Give up remote */
71553541Sshin				break;
71653541Sshin			}
71778064Sume			n->m_pkthdr.rcvif = NULL;
71862587Sitojun			n->m_len = 0;
71962587Sitojun			maxhlen = M_TRAILINGSPACE(n) - maxlen;
720193066Sjamie			pr = curthread->td_ucred->cr_prison;
721193066Sjamie			mtx_lock(&pr->pr_mtx);
722194118Sjamie			hlen = strlen(pr->pr_hostname);
723193066Sjamie			if (maxhlen > hlen)
724193066Sjamie				maxhlen = hlen;
72553541Sshin			/*
72653541Sshin			 * Copy IPv6 and ICMPv6 only.
72753541Sshin			 */
72853541Sshin			nip6 = mtod(n, struct ip6_hdr *);
72953541Sshin			bcopy(ip6, nip6, sizeof(struct ip6_hdr));
73053541Sshin			nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
73153541Sshin			bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
73253541Sshin			p = (u_char *)(nicmp6 + 1);
73353541Sshin			bzero(p, 4);
734194118Sjamie			/* meaningless TTL */
735194118Sjamie			bcopy(pr->pr_hostname, p + 4, maxhlen);
736193066Sjamie			mtx_unlock(&pr->pr_mtx);
73753541Sshin			noff = sizeof(struct ip6_hdr);
73853541Sshin			n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
73962587Sitojun				sizeof(struct icmp6_hdr) + 4 + maxhlen;
74053541Sshin			nicmp6->icmp6_type = ICMP6_WRUREPLY;
74153541Sshin			nicmp6->icmp6_code = 0;
74253541Sshin		}
74353541Sshin		if (n) {
744190964Srwatson			ICMP6STAT_INC(icp6s_reflect);
745190964Srwatson			ICMP6STAT_INC(icp6s_outhist[ICMP6_WRUREPLY]);
74653541Sshin			icmp6_reflect(n, noff);
74753541Sshin		}
74853541Sshin		break;
74953541Sshin	    }
75053541Sshin
75153541Sshin	case ICMP6_WRUREPLY:
75253541Sshin		if (code != 0)
75353541Sshin			goto badcode;
75453541Sshin		break;
75553541Sshin
75653541Sshin	case ND_ROUTER_SOLICIT:
757192923Sbms		icmp6_ifstat_inc(ifp, ifs6_in_routersolicit);
75853541Sshin		if (code != 0)
75953541Sshin			goto badcode;
76053541Sshin		if (icmp6len < sizeof(struct nd_router_solicit))
76153541Sshin			goto badlen;
762111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
76362587Sitojun			/* give up local */
76462587Sitojun			nd6_rs_input(m, off, icmp6len);
76562587Sitojun			m = NULL;
76662587Sitojun			goto freeit;
76762587Sitojun		}
76862587Sitojun		nd6_rs_input(n, off, icmp6len);
76953541Sshin		/* m stays. */
77053541Sshin		break;
77153541Sshin
77253541Sshin	case ND_ROUTER_ADVERT:
773192923Sbms		icmp6_ifstat_inc(ifp, ifs6_in_routeradvert);
77453541Sshin		if (code != 0)
77553541Sshin			goto badcode;
77653541Sshin		if (icmp6len < sizeof(struct nd_router_advert))
77753541Sshin			goto badlen;
778111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
77962587Sitojun			/* give up local */
78062587Sitojun			nd6_ra_input(m, off, icmp6len);
78162587Sitojun			m = NULL;
78262587Sitojun			goto freeit;
78362587Sitojun		}
78462587Sitojun		nd6_ra_input(n, off, icmp6len);
78553541Sshin		/* m stays. */
78653541Sshin		break;
78753541Sshin
78853541Sshin	case ND_NEIGHBOR_SOLICIT:
789192923Sbms		icmp6_ifstat_inc(ifp, ifs6_in_neighborsolicit);
79053541Sshin		if (code != 0)
79153541Sshin			goto badcode;
79253541Sshin		if (icmp6len < sizeof(struct nd_neighbor_solicit))
79353541Sshin			goto badlen;
794111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
79562587Sitojun			/* give up local */
79662587Sitojun			nd6_ns_input(m, off, icmp6len);
79762587Sitojun			m = NULL;
79862587Sitojun			goto freeit;
79962587Sitojun		}
80062587Sitojun		nd6_ns_input(n, off, icmp6len);
80153541Sshin		/* m stays. */
80253541Sshin		break;
80353541Sshin
80453541Sshin	case ND_NEIGHBOR_ADVERT:
805192923Sbms		icmp6_ifstat_inc(ifp, ifs6_in_neighboradvert);
80653541Sshin		if (code != 0)
80753541Sshin			goto badcode;
80853541Sshin		if (icmp6len < sizeof(struct nd_neighbor_advert))
80953541Sshin			goto badlen;
810111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
81162587Sitojun			/* give up local */
81262587Sitojun			nd6_na_input(m, off, icmp6len);
81362587Sitojun			m = NULL;
81462587Sitojun			goto freeit;
81562587Sitojun		}
81662587Sitojun		nd6_na_input(n, off, icmp6len);
81753541Sshin		/* m stays. */
81853541Sshin		break;
81953541Sshin
82053541Sshin	case ND_REDIRECT:
821192923Sbms		icmp6_ifstat_inc(ifp, ifs6_in_redirect);
82253541Sshin		if (code != 0)
82353541Sshin			goto badcode;
82453541Sshin		if (icmp6len < sizeof(struct nd_redirect))
82553541Sshin			goto badlen;
826111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
82762587Sitojun			/* give up local */
82862587Sitojun			icmp6_redirect_input(m, off);
82962587Sitojun			m = NULL;
83062587Sitojun			goto freeit;
83162587Sitojun		}
83262587Sitojun		icmp6_redirect_input(n, off);
83353541Sshin		/* m stays. */
83453541Sshin		break;
83553541Sshin
83653541Sshin	case ICMP6_ROUTER_RENUMBERING:
83753541Sshin		if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
83853541Sshin		    code != ICMP6_ROUTER_RENUMBERING_RESULT)
83953541Sshin			goto badcode;
84053541Sshin		if (icmp6len < sizeof(struct icmp6_router_renum))
84153541Sshin			goto badlen;
84253541Sshin		break;
84353541Sshin
84453541Sshin	default:
84578064Sume		nd6log((LOG_DEBUG,
84678064Sume		    "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
847165118Sbz		    icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src),
848165118Sbz		    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
849192923Sbms		    ifp ? ifp->if_index : 0));
85053541Sshin		if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
85153541Sshin			/* ICMPv6 error: MUST deliver it by spec... */
85253541Sshin			code = PRC_NCMDS;
85353541Sshin			/* deliver */
85453541Sshin		} else {
85553541Sshin			/* ICMPv6 informational: MUST not deliver */
85653541Sshin			break;
85753541Sshin		}
85853541Sshin	deliver:
859142681Sume		if (icmp6_notify_error(&m, off, icmp6len, code)) {
86078064Sume			/* In this case, m should've been freed. */
861120856Sume			return (IPPROTO_DONE);
86253541Sshin		}
86378064Sume		break;
86478064Sume
86578064Sume	badcode:
866190964Srwatson		ICMP6STAT_INC(icp6s_badcode);
86778064Sume		break;
86878064Sume
86978064Sume	badlen:
870190964Srwatson		ICMP6STAT_INC(icp6s_badlen);
87178064Sume		break;
87278064Sume	}
87378064Sume
87478064Sume	/* deliver the packet to appropriate sockets */
87578064Sume	icmp6_rip6_input(&m, *offp);
87678064Sume
87778064Sume	return IPPROTO_DONE;
87878064Sume
87978064Sume freeit:
88078064Sume	m_freem(m);
88178064Sume	return IPPROTO_DONE;
88278064Sume}
88378064Sume
88478064Sumestatic int
885171259Sdelphijicmp6_notify_error(struct mbuf **mp, int off, int icmp6len, int code)
88678064Sume{
887142681Sume	struct mbuf *m = *mp;
88878064Sume	struct icmp6_hdr *icmp6;
88978064Sume	struct ip6_hdr *eip6;
89078064Sume	u_int32_t notifymtu;
89178064Sume	struct sockaddr_in6 icmp6src, icmp6dst;
89278064Sume
89378064Sume	if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
894190964Srwatson		ICMP6STAT_INC(icp6s_tooshort);
89578064Sume		goto freeit;
89678064Sume	}
89762587Sitojun#ifndef PULLDOWN_TEST
89878064Sume	IP6_EXTHDR_CHECK(m, off,
899120891Sume	    sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1);
90078064Sume	icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
90162587Sitojun#else
90278064Sume	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
903120891Sume	    sizeof(*icmp6) + sizeof(struct ip6_hdr));
90478064Sume	if (icmp6 == NULL) {
905190964Srwatson		ICMP6STAT_INC(icp6s_tooshort);
906120856Sume		return (-1);
90778064Sume	}
90862587Sitojun#endif
90978064Sume	eip6 = (struct ip6_hdr *)(icmp6 + 1);
91053541Sshin
91178064Sume	/* Detect the upper level protocol */
91278064Sume	{
913175162Sobrien		void (*ctlfunc)(int, struct sockaddr *, void *);
91453541Sshin		u_int8_t nxt = eip6->ip6_nxt;
91553541Sshin		int eoff = off + sizeof(struct icmp6_hdr) +
916120891Sume		    sizeof(struct ip6_hdr);
91753541Sshin		struct ip6ctlparam ip6cp;
91862587Sitojun		struct in6_addr *finaldst = NULL;
91962587Sitojun		int icmp6type = icmp6->icmp6_type;
92062587Sitojun		struct ip6_frag *fh;
92162587Sitojun		struct ip6_rthdr *rth;
92262587Sitojun		struct ip6_rthdr0 *rth0;
92362587Sitojun		int rthlen;
92453541Sshin
92595023Ssuz		while (1) { /* XXX: should avoid infinite loop explicitly? */
92653541Sshin			struct ip6_ext *eh;
92753541Sshin
92878064Sume			switch (nxt) {
92953541Sshin			case IPPROTO_HOPOPTS:
93053541Sshin			case IPPROTO_DSTOPTS:
93153541Sshin			case IPPROTO_AH:
93262587Sitojun#ifndef PULLDOWN_TEST
933120891Sume				IP6_EXTHDR_CHECK(m, 0,
934120891Sume				    eoff + sizeof(struct ip6_ext), -1);
935120892Sume				eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff);
93662587Sitojun#else
93762587Sitojun				IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
938120891Sume				    eoff, sizeof(*eh));
93962587Sitojun				if (eh == NULL) {
940190964Srwatson					ICMP6STAT_INC(icp6s_tooshort);
941120856Sume					return (-1);
94262587Sitojun				}
94362587Sitojun#endif
944120891Sume
94553541Sshin				if (nxt == IPPROTO_AH)
94653541Sshin					eoff += (eh->ip6e_len + 2) << 2;
94753541Sshin				else
94853541Sshin					eoff += (eh->ip6e_len + 1) << 3;
94953541Sshin				nxt = eh->ip6e_nxt;
95053541Sshin				break;
95162587Sitojun			case IPPROTO_ROUTING:
95262587Sitojun				/*
95362587Sitojun				 * When the erroneous packet contains a
95462587Sitojun				 * routing header, we should examine the
95562587Sitojun				 * header to determine the final destination.
95662587Sitojun				 * Otherwise, we can't properly update
95762587Sitojun				 * information that depends on the final
95862587Sitojun				 * destination (e.g. path MTU).
95962587Sitojun				 */
96062587Sitojun#ifndef PULLDOWN_TEST
961120891Sume				IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1);
962120891Sume				rth = (struct ip6_rthdr *)
963120891Sume				    (mtod(m, caddr_t) + eoff);
96462587Sitojun#else
96562587Sitojun				IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
966120891Sume				    eoff, sizeof(*rth));
96762587Sitojun				if (rth == NULL) {
968190964Srwatson					ICMP6STAT_INC(icp6s_tooshort);
969120856Sume					return (-1);
97062587Sitojun				}
97162587Sitojun#endif
97262587Sitojun				rthlen = (rth->ip6r_len + 1) << 3;
97362587Sitojun				/*
97462587Sitojun				 * XXX: currently there is no
97562587Sitojun				 * officially defined type other
97662587Sitojun				 * than type-0.
97762587Sitojun				 * Note that if the segment left field
97862587Sitojun				 * is 0, all intermediate hops must
97962587Sitojun				 * have been passed.
98062587Sitojun				 */
98162587Sitojun				if (rth->ip6r_segleft &&
98262587Sitojun				    rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
98362587Sitojun					int hops;
98462587Sitojun
98562587Sitojun#ifndef PULLDOWN_TEST
986120891Sume					IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1);
987120891Sume					rth0 = (struct ip6_rthdr0 *)
988120891Sume					    (mtod(m, caddr_t) + eoff);
98962587Sitojun#else
99062587Sitojun					IP6_EXTHDR_GET(rth0,
991120891Sume					    struct ip6_rthdr0 *, m,
992120891Sume					    eoff, rthlen);
99362587Sitojun					if (rth0 == NULL) {
994190964Srwatson						ICMP6STAT_INC(icp6s_tooshort);
995120856Sume						return (-1);
99662587Sitojun					}
99762587Sitojun#endif
99862587Sitojun					/* just ignore a bogus header */
99962587Sitojun					if ((rth0->ip6r0_len % 2) == 0 &&
100062587Sitojun					    (hops = rth0->ip6r0_len/2))
100162587Sitojun						finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
100262587Sitojun				}
100362587Sitojun				eoff += rthlen;
100462587Sitojun				nxt = rth->ip6r_nxt;
100562587Sitojun				break;
100662587Sitojun			case IPPROTO_FRAGMENT:
100762587Sitojun#ifndef PULLDOWN_TEST
100862587Sitojun				IP6_EXTHDR_CHECK(m, 0, eoff +
1009120891Sume				    sizeof(struct ip6_frag), -1);
1010120891Sume				fh = (struct ip6_frag *)(mtod(m, caddr_t) +
1011120891Sume				    eoff);
101262587Sitojun#else
101362587Sitojun				IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
1014120891Sume				    eoff, sizeof(*fh));
101562587Sitojun				if (fh == NULL) {
1016190964Srwatson					ICMP6STAT_INC(icp6s_tooshort);
1017120856Sume					return (-1);
101862587Sitojun				}
101962587Sitojun#endif
102062587Sitojun				/*
102162587Sitojun				 * Data after a fragment header is meaningless
102262587Sitojun				 * unless it is the first fragment, but
102362587Sitojun				 * we'll go to the notify label for path MTU
102462587Sitojun				 * discovery.
102562587Sitojun				 */
102662587Sitojun				if (fh->ip6f_offlg & IP6F_OFF_MASK)
102762587Sitojun					goto notify;
102862587Sitojun
102962587Sitojun				eoff += sizeof(struct ip6_frag);
103062587Sitojun				nxt = fh->ip6f_nxt;
103162587Sitojun				break;
103253541Sshin			default:
103362587Sitojun				/*
103462587Sitojun				 * This case includes ESP and the No Next
103595023Ssuz				 * Header.  In such cases going to the notify
103662587Sitojun				 * label does not have any meaning
103762587Sitojun				 * (i.e. ctlfunc will be NULL), but we go
103862587Sitojun				 * anyway since we might have to update
103962587Sitojun				 * path MTU information.
104062587Sitojun				 */
104153541Sshin				goto notify;
104253541Sshin			}
104353541Sshin		}
104478064Sume	  notify:
104562587Sitojun#ifndef PULLDOWN_TEST
104653541Sshin		icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
104762587Sitojun#else
104862587Sitojun		IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1049120891Sume		    sizeof(*icmp6) + sizeof(struct ip6_hdr));
105062587Sitojun		if (icmp6 == NULL) {
1051190964Srwatson			ICMP6STAT_INC(icp6s_tooshort);
1052120856Sume			return (-1);
105362587Sitojun		}
105462587Sitojun#endif
105578064Sume
1056120891Sume		/*
1057120891Sume		 * retrieve parameters from the inner IPv6 header, and convert
1058120891Sume		 * them into sockaddr structures.
1059120891Sume		 * XXX: there is no guarantee that the source or destination
1060120891Sume		 * addresses of the inner packet are in the same scope as
1061120891Sume		 * the addresses of the icmp packet.  But there is no other
1062120891Sume		 * way to determine the zone.
1063120891Sume		 */
106478064Sume		eip6 = (struct ip6_hdr *)(icmp6 + 1);
1065120891Sume
106678064Sume		bzero(&icmp6dst, sizeof(icmp6dst));
106778064Sume		icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
106878064Sume		icmp6dst.sin6_family = AF_INET6;
106978064Sume		if (finaldst == NULL)
107078064Sume			icmp6dst.sin6_addr = eip6->ip6_dst;
107178064Sume		else
107278064Sume			icmp6dst.sin6_addr = *finaldst;
1073148385Sume		if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
1074121315Sume			goto freeit;
107578064Sume		bzero(&icmp6src, sizeof(icmp6src));
107678064Sume		icmp6src.sin6_len = sizeof(struct sockaddr_in6);
107778064Sume		icmp6src.sin6_family = AF_INET6;
107878064Sume		icmp6src.sin6_addr = eip6->ip6_src;
1079148385Sume		if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
1080121315Sume			goto freeit;
1081148385Sume		icmp6src.sin6_flowinfo =
1082148385Sume		    (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
108378064Sume
108478064Sume		if (finaldst == NULL)
108578064Sume			finaldst = &eip6->ip6_dst;
108678064Sume		ip6cp.ip6c_m = m;
108778064Sume		ip6cp.ip6c_icmp6 = icmp6;
108878064Sume		ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
108978064Sume		ip6cp.ip6c_off = eoff;
109078064Sume		ip6cp.ip6c_finaldst = finaldst;
109178064Sume		ip6cp.ip6c_src = &icmp6src;
109278064Sume		ip6cp.ip6c_nxt = nxt;
109378064Sume
109462587Sitojun		if (icmp6type == ICMP6_PACKET_TOO_BIG) {
109578064Sume			notifymtu = ntohl(icmp6->icmp6_mtu);
109678064Sume			ip6cp.ip6c_cmdarg = (void *)&notifymtu;
109778064Sume			icmp6_mtudisc_update(&ip6cp, 1);	/*XXX*/
109862587Sitojun		}
109962587Sitojun
1100175162Sobrien		ctlfunc = (void (*)(int, struct sockaddr *, void *))
1101120891Sume		    (inet6sw[ip6_protox[nxt]].pr_ctlinput);
110253541Sshin		if (ctlfunc) {
110378064Sume			(void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1104120891Sume			    &ip6cp);
110553541Sshin		}
110653541Sshin	}
1107142681Sume	*mp = m;
1108120856Sume	return (0);
110953541Sshin
111078064Sume  freeit:
111153541Sshin	m_freem(m);
1112120856Sume	return (-1);
111353541Sshin}
111453541Sshin
111578064Sumevoid
1116171259Sdelphijicmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
111762587Sitojun{
111878064Sume	struct in6_addr *dst = ip6cp->ip6c_finaldst;
111978064Sume	struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
112078064Sume	struct mbuf *m = ip6cp->ip6c_m;	/* will be necessary for scope issue */
112162587Sitojun	u_int mtu = ntohl(icmp6->icmp6_mtu);
1122122922Sandre	struct in_conninfo inc;
112362587Sitojun
1124121472Sume#if 0
1125121472Sume	/*
1126121472Sume	 * RFC2460 section 5, last paragraph.
1127121472Sume	 * even though minimum link MTU for IPv6 is IPV6_MMTU,
1128121472Sume	 * we may see ICMPv6 too big with mtu < IPV6_MMTU
1129121472Sume	 * due to packet translator in the middle.
1130121472Sume	 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for
1131121472Sume	 * special handling.
1132121472Sume	 */
1133121472Sume	if (mtu < IPV6_MMTU)
1134121472Sume		return;
1135121472Sume#endif
1136121472Sume
1137121472Sume	/*
1138121472Sume	 * we reject ICMPv6 too big with abnormally small value.
1139121472Sume	 * XXX what is the good definition of "abnormally small"?
1140121472Sume	 */
1141121472Sume	if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
1142121472Sume		return;
1143121472Sume
114478064Sume	if (!validated)
114578064Sume		return;
114678064Sume
1147182740Ssimon	/*
1148182740Ssimon	 * In case the suggested mtu is less than IPV6_MMTU, we
1149182740Ssimon	 * only need to remember that it was for above mentioned
1150182740Ssimon	 * "alwaysfrag" case.
1151182740Ssimon	 * Try to be as close to the spec as possible.
1152182740Ssimon	 */
1153182740Ssimon	if (mtu < IPV6_MMTU)
1154182740Ssimon		mtu = IPV6_MMTU - 8;
1155182740Ssimon
1156122922Sandre	bzero(&inc, sizeof(inc));
1157186222Sbz	inc.inc_flags |= INC_ISIPV6;
1158122922Sandre	inc.inc6_faddr = *dst;
1159148385Sume	if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL))
1160148385Sume		return;
116162587Sitojun
1162162084Sandre	if (mtu < tcp_maxmtu6(&inc, NULL)) {
1163122922Sandre		tcp_hc_updatemtu(&inc, mtu);
1164190964Srwatson		ICMP6STAT_INC(icp6s_pmtuchg);
116562587Sitojun	}
116662587Sitojun}
116762587Sitojun
116853541Sshin/*
116978064Sume * Process a Node Information Query packet, based on
117078064Sume * draft-ietf-ipngwg-icmp-name-lookups-07.
1171120891Sume *
117262587Sitojun * Spec incompatibilities:
117362587Sitojun * - IPv6 Subject address handling
117462587Sitojun * - IPv4 Subject address handling support missing
117562587Sitojun * - Proxy reply (answer even if it's not for me)
117662587Sitojun * - joins NI group address at in6_ifattach() time only, does not cope
117762587Sitojun *   with hostname changes by sethostname(3)
117853541Sshin */
117953541Sshinstatic struct mbuf *
1180171259Sdelphijni6_input(struct mbuf *m, int off)
118153541Sshin{
118262587Sitojun	struct icmp6_nodeinfo *ni6, *nni6;
118353541Sshin	struct mbuf *n = NULL;
1184193066Sjamie	struct prison *pr;
118562587Sitojun	u_int16_t qtype;
118662587Sitojun	int subjlen;
118753541Sshin	int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
118853541Sshin	struct ni_reply_fqdn *fqdn;
118953541Sshin	int addrs;		/* for NI_QTYPE_NODEADDR */
119053541Sshin	struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1191148385Sume	struct in6_addr in6_subj; /* subject address */
119262587Sitojun	struct ip6_hdr *ip6;
119362587Sitojun	int oldfqdn = 0;	/* if 1, return pascal string (03 draft) */
119478064Sume	char *subj = NULL;
119578064Sume	struct in6_ifaddr *ia6 = NULL;
119653541Sshin
119762587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
119862587Sitojun#ifndef PULLDOWN_TEST
119962587Sitojun	ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
120062587Sitojun#else
120162587Sitojun	IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
120262587Sitojun	if (ni6 == NULL) {
120362587Sitojun		/* m is already reclaimed */
1204120891Sume		return (NULL);
120562587Sitojun	}
120662587Sitojun#endif
120753541Sshin
120862587Sitojun	/*
1209169664Sjinmei	 * Validate IPv6 source address.
1210169664Sjinmei	 * The default configuration MUST be to refuse answering queries from
1211169664Sjinmei	 * global-scope addresses according to RFC4602.
1212169664Sjinmei	 * Notes:
1213169664Sjinmei	 *  - it's not very clear what "refuse" means; this implementation
1214169664Sjinmei	 *    simply drops it.
1215169664Sjinmei	 *  - it's not very easy to identify global-scope (unicast) addresses
1216169664Sjinmei	 *    since there are many prefixes for them.  It should be safer
1217169664Sjinmei	 *    and in practice sufficient to check "all" but loopback and
1218169664Sjinmei	 *    link-local (note that site-local unicast was deprecated and
1219169664Sjinmei	 *    ULA is defined as global scope-wise)
1220169664Sjinmei	 */
1221181803Sbz	if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 &&
1222169664Sjinmei	    !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) &&
1223169664Sjinmei	    !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
1224169664Sjinmei		goto bad;
1225169664Sjinmei
1226169664Sjinmei	/*
122762587Sitojun	 * Validate IPv6 destination address.
122862587Sitojun	 *
122978064Sume	 * The Responder must discard the Query without further processing
123078064Sume	 * unless it is one of the Responder's unicast or anycast addresses, or
123178064Sume	 * a link-local scope multicast address which the Responder has joined.
1232169664Sjinmei	 * [RFC4602, Section 5.]
123362587Sitojun	 */
1234121630Sume	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1235121630Sume		if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1236121630Sume			goto bad;
1237121630Sume		/* else it's a link-local multicast, fine */
1238121630Sume	} else {		/* unicast or anycast */
1239121630Sume		if ((ia6 = ip6_getdstifaddr(m)) == NULL)
1240121630Sume			goto bad; /* XXX impossible */
1241121630Sume
1242121630Sume		if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1243181803Sbz		    !(V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) {
1244194760Srwatson			ifa_free(&ia6->ia_ifa);
124578064Sume			nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
124678064Sume				"a temporary address in %s:%d",
124778064Sume			       __FILE__, __LINE__));
124878064Sume			goto bad;
124978064Sume		}
1250194760Srwatson		ifa_free(&ia6->ia_ifa);
1251121630Sume	}
125262587Sitojun
125378064Sume	/* validate query Subject field. */
125462587Sitojun	qtype = ntohs(ni6->ni_qtype);
125562587Sitojun	subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
125662587Sitojun	switch (qtype) {
125762587Sitojun	case NI_QTYPE_NOOP:
125862587Sitojun	case NI_QTYPE_SUPTYPES:
125978064Sume		/* 07 draft */
126078064Sume		if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
126178064Sume			break;
126278064Sume		/* FALLTHROUGH */
126362587Sitojun	case NI_QTYPE_FQDN:
126462587Sitojun	case NI_QTYPE_NODEADDR:
1265151475Ssuz	case NI_QTYPE_IPV4ADDR:
126662587Sitojun		switch (ni6->ni_code) {
126762587Sitojun		case ICMP6_NI_SUBJ_IPV6:
126862587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0
126962587Sitojun		case 0:
127062587Sitojun#endif
127162587Sitojun			/*
127262587Sitojun			 * backward compatibility - try to accept 03 draft
127362587Sitojun			 * format, where no Subject is present.
127462587Sitojun			 */
127578064Sume			if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
127678064Sume			    subjlen == 0) {
127762587Sitojun				oldfqdn++;
127862587Sitojun				break;
127962587Sitojun			}
128078064Sume#if ICMP6_NI_SUBJ_IPV6 != 0
128178064Sume			if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
128278064Sume				goto bad;
128378064Sume#endif
128462587Sitojun
1285121630Sume			if (subjlen != sizeof(struct in6_addr))
128662587Sitojun				goto bad;
128762587Sitojun
128862587Sitojun			/*
128962587Sitojun			 * Validate Subject address.
129062587Sitojun			 *
129178064Sume			 * Not sure what exactly "address belongs to the node"
129278064Sume			 * means in the spec, is it just unicast, or what?
129362587Sitojun			 *
129462587Sitojun			 * At this moment we consider Subject address as
129562587Sitojun			 * "belong to the node" if the Subject address equals
129662587Sitojun			 * to the IPv6 destination address; validation for
129762587Sitojun			 * IPv6 destination address should have done enough
129862587Sitojun			 * check for us.
129962587Sitojun			 *
130062587Sitojun			 * We do not do proxy at this moment.
130162587Sitojun			 */
130262587Sitojun			/* m_pulldown instead of copy? */
130362587Sitojun			m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1304148385Sume			    subjlen, (caddr_t)&in6_subj);
1305148385Sume			if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1306121315Sume				goto bad;
1307121630Sume
1308148385Sume			subj = (char *)&in6_subj;
1309148385Sume			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
131062587Sitojun				break;
131178064Sume
131262587Sitojun			/*
131362587Sitojun			 * XXX if we are to allow other cases, we should really
131462587Sitojun			 * be careful about scope here.
131562587Sitojun			 * basically, we should disallow queries toward IPv6
1316120891Sume			 * destination X with subject Y,
1317120891Sume			 * if scope(X) > scope(Y).
131862587Sitojun			 * if we allow scope(X) > scope(Y), it will result in
131962587Sitojun			 * information leakage across scope boundary.
132062587Sitojun			 */
132162587Sitojun			goto bad;
132262587Sitojun
132362587Sitojun		case ICMP6_NI_SUBJ_FQDN:
132462587Sitojun			/*
132562587Sitojun			 * Validate Subject name with gethostname(3).
132662587Sitojun			 *
132762587Sitojun			 * The behavior may need some debate, since:
132862587Sitojun			 * - we are not sure if the node has FQDN as
132962587Sitojun			 *   hostname (returned by gethostname(3)).
133062587Sitojun			 * - the code does wildcard match for truncated names.
133162587Sitojun			 *   however, we are not sure if we want to perform
133262587Sitojun			 *   wildcard match, if gethostname(3) side has
133362587Sitojun			 *   truncated hostname.
133462587Sitojun			 */
1335193066Sjamie			pr = curthread->td_ucred->cr_prison;
1336193066Sjamie			mtx_lock(&pr->pr_mtx);
1337194118Sjamie			n = ni6_nametodns(pr->pr_hostname,
1338194118Sjamie			    strlen(pr->pr_hostname), 0);
1339193066Sjamie			mtx_unlock(&pr->pr_mtx);
134062587Sitojun			if (!n || n->m_next || n->m_len == 0)
134162587Sitojun				goto bad;
134262587Sitojun			IP6_EXTHDR_GET(subj, char *, m,
134362587Sitojun			    off + sizeof(struct icmp6_nodeinfo), subjlen);
134462587Sitojun			if (subj == NULL)
134562587Sitojun				goto bad;
134662587Sitojun			if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1347120891Sume			    n->m_len)) {
134862587Sitojun				goto bad;
134962587Sitojun			}
135062587Sitojun			m_freem(n);
135162587Sitojun			n = NULL;
135262587Sitojun			break;
135362587Sitojun
135478064Sume		case ICMP6_NI_SUBJ_IPV4:	/* XXX: to be implemented? */
135562587Sitojun		default:
135662587Sitojun			goto bad;
135762587Sitojun		}
135862587Sitojun		break;
135978064Sume	}
136062587Sitojun
136178064Sume	/* refuse based on configuration.  XXX ICMP6_NI_REFUSED? */
136278064Sume	switch (qtype) {
136378064Sume	case NI_QTYPE_FQDN:
1364181803Sbz		if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0)
136578064Sume			goto bad;
136678064Sume		break;
136778064Sume	case NI_QTYPE_NODEADDR:
1368151475Ssuz	case NI_QTYPE_IPV4ADDR:
1369181803Sbz		if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0)
137078064Sume			goto bad;
137178064Sume		break;
137278064Sume	}
137378064Sume
137478064Sume	/* guess reply length */
137578064Sume	switch (qtype) {
137678064Sume	case NI_QTYPE_NOOP:
137778064Sume		break;		/* no reply data */
137878064Sume	case NI_QTYPE_SUPTYPES:
137978064Sume		replylen += sizeof(u_int32_t);
138078064Sume		break;
138178064Sume	case NI_QTYPE_FQDN:
138278064Sume		/* XXX will append an mbuf */
138378064Sume		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
138478064Sume		break;
138578064Sume	case NI_QTYPE_NODEADDR:
1386148892Sume		addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj);
138778064Sume		if ((replylen += addrs * (sizeof(struct in6_addr) +
1388120891Sume		    sizeof(u_int32_t))) > MCLBYTES)
138978064Sume			replylen = MCLBYTES; /* XXX: will truncate pkt later */
139078064Sume		break;
1391151475Ssuz	case NI_QTYPE_IPV4ADDR:
1392151475Ssuz		/* unsupported - should respond with unknown Qtype? */
1393151475Ssuz		break;
139462587Sitojun	default:
139578064Sume		/*
139678064Sume		 * XXX: We must return a reply with the ICMP6 code
1397120891Sume		 * `unknown Qtype' in this case.  However we regard the case
139878064Sume		 * as an FQDN query for backward compatibility.
139978064Sume		 * Older versions set a random value to this field,
140078064Sume		 * so it rarely varies in the defined qtypes.
140178064Sume		 * But the mechanism is not reliable...
140278064Sume		 * maybe we should obsolete older versions.
140378064Sume		 */
140478064Sume		qtype = NI_QTYPE_FQDN;
140578064Sume		/* XXX will append an mbuf */
140678064Sume		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
140778064Sume		oldfqdn++;
140878064Sume		break;
140962587Sitojun	}
141062587Sitojun
141178064Sume	/* allocate an mbuf to reply. */
1412111119Simp	MGETHDR(n, M_DONTWAIT, m->m_type);
141362587Sitojun	if (n == NULL) {
141462587Sitojun		m_freem(m);
1415120856Sume		return (NULL);
141662587Sitojun	}
1417108466Ssam	M_MOVE_PKTHDR(n, m); /* just for recvif */
141853541Sshin	if (replylen > MHLEN) {
141962587Sitojun		if (replylen > MCLBYTES) {
142078064Sume			/*
142178064Sume			 * XXX: should we try to allocate more? But MCLBYTES
142278064Sume			 * is probably much larger than IPV6_MMTU...
142378064Sume			 */
142453541Sshin			goto bad;
142562587Sitojun		}
1426111119Simp		MCLGET(n, M_DONTWAIT);
142753541Sshin		if ((n->m_flags & M_EXT) == 0) {
142853541Sshin			goto bad;
142953541Sshin		}
143053541Sshin	}
143153541Sshin	n->m_pkthdr.len = n->m_len = replylen;
143253541Sshin
143353541Sshin	/* copy mbuf header and IPv6 + Node Information base headers */
143453541Sshin	bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
143553541Sshin	nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
143662587Sitojun	bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
143753541Sshin
143853541Sshin	/* qtype dependent procedure */
143953541Sshin	switch (qtype) {
144062587Sitojun	case NI_QTYPE_NOOP:
144178064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
144262587Sitojun		nni6->ni_flags = 0;
144362587Sitojun		break;
144462587Sitojun	case NI_QTYPE_SUPTYPES:
144578064Sume	{
144678064Sume		u_int32_t v;
144778064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
144878064Sume		nni6->ni_flags = htons(0x0000);	/* raw bitmap */
144978064Sume		/* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
145078064Sume		v = (u_int32_t)htonl(0x0000000f);
145178064Sume		bcopy(&v, nni6 + 1, sizeof(u_int32_t));
145262587Sitojun		break;
145378064Sume	}
145462587Sitojun	case NI_QTYPE_FQDN:
145578064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
145662587Sitojun		fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1457120891Sume		    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
145862587Sitojun		nni6->ni_flags = 0; /* XXX: meaningless TTL */
145962587Sitojun		fqdn->ni_fqdn_ttl = 0;	/* ditto. */
146062587Sitojun		/*
1461193066Sjamie		 * XXX do we really have FQDN in hostname?
146262587Sitojun		 */
1463193066Sjamie		pr = curthread->td_ucred->cr_prison;
1464193066Sjamie		mtx_lock(&pr->pr_mtx);
1465194118Sjamie		n->m_next = ni6_nametodns(pr->pr_hostname,
1466194118Sjamie		    strlen(pr->pr_hostname), oldfqdn);
1467193066Sjamie		mtx_unlock(&pr->pr_mtx);
146862587Sitojun		if (n->m_next == NULL)
146962587Sitojun			goto bad;
147062587Sitojun		/* XXX we assume that n->m_next is not a chain */
147162587Sitojun		if (n->m_next->m_next != NULL)
147262587Sitojun			goto bad;
147362587Sitojun		n->m_pkthdr.len += n->m_next->m_len;
147462587Sitojun		break;
147562587Sitojun	case NI_QTYPE_NODEADDR:
147662587Sitojun	{
147762587Sitojun		int lenlim, copied;
147853541Sshin
147978064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
148078064Sume		n->m_pkthdr.len = n->m_len =
148178064Sume		    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
148278064Sume		lenlim = M_TRAILINGSPACE(n);
148362587Sitojun		copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
148462587Sitojun		/* XXX: reset mbuf length */
148562587Sitojun		n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1486120891Sume		    sizeof(struct icmp6_nodeinfo) + copied;
148762587Sitojun		break;
148853541Sshin	}
148962587Sitojun	default:
149062587Sitojun		break;		/* XXX impossible! */
149162587Sitojun	}
149253541Sshin
149353541Sshin	nni6->ni_type = ICMP6_NI_REPLY;
149462587Sitojun	m_freem(m);
1495120856Sume	return (n);
149653541Sshin
149753541Sshin  bad:
149862587Sitojun	m_freem(m);
149953541Sshin	if (n)
150053541Sshin		m_freem(n);
1501120856Sume	return (NULL);
150253541Sshin}
150353541Sshin
150453541Sshin/*
150562587Sitojun * make a mbuf with DNS-encoded string.  no compression support.
150662587Sitojun *
150762587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be
150862587Sitojun * treated as truncated name (two \0 at the end).  this is a wild guess.
1509171259Sdelphij *
1510171259Sdelphij * old - return pascal string if non-zero
151162587Sitojun */
151262587Sitojunstatic struct mbuf *
1513171259Sdelphijni6_nametodns(const char *name, int namelen, int old)
151462587Sitojun{
151562587Sitojun	struct mbuf *m;
151662587Sitojun	char *cp, *ep;
151762587Sitojun	const char *p, *q;
151862587Sitojun	int i, len, nterm;
151962587Sitojun
152062587Sitojun	if (old)
152162587Sitojun		len = namelen + 1;
152262587Sitojun	else
152362587Sitojun		len = MCLBYTES;
152462587Sitojun
152562587Sitojun	/* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1526111119Simp	MGET(m, M_DONTWAIT, MT_DATA);
152762587Sitojun	if (m && len > MLEN) {
1528111119Simp		MCLGET(m, M_DONTWAIT);
152962587Sitojun		if ((m->m_flags & M_EXT) == 0)
153062587Sitojun			goto fail;
153162587Sitojun	}
153262587Sitojun	if (!m)
153362587Sitojun		goto fail;
153462587Sitojun	m->m_next = NULL;
153562587Sitojun
153662587Sitojun	if (old) {
153762587Sitojun		m->m_len = len;
153862587Sitojun		*mtod(m, char *) = namelen;
153962587Sitojun		bcopy(name, mtod(m, char *) + 1, namelen);
154062587Sitojun		return m;
154162587Sitojun	} else {
154262587Sitojun		m->m_len = 0;
154362587Sitojun		cp = mtod(m, char *);
154462587Sitojun		ep = mtod(m, char *) + M_TRAILINGSPACE(m);
154562587Sitojun
154662587Sitojun		/* if not certain about my name, return empty buffer */
154762587Sitojun		if (namelen == 0)
154862587Sitojun			return m;
154962587Sitojun
155062587Sitojun		/*
155162587Sitojun		 * guess if it looks like shortened hostname, or FQDN.
155262587Sitojun		 * shortened hostname needs two trailing "\0".
155362587Sitojun		 */
155462587Sitojun		i = 0;
155562587Sitojun		for (p = name; p < name + namelen; p++) {
155662587Sitojun			if (*p && *p == '.')
155762587Sitojun				i++;
155862587Sitojun		}
155962587Sitojun		if (i < 2)
156062587Sitojun			nterm = 2;
156162587Sitojun		else
156262587Sitojun			nterm = 1;
156362587Sitojun
156462587Sitojun		p = name;
156562587Sitojun		while (cp < ep && p < name + namelen) {
156662587Sitojun			i = 0;
156762587Sitojun			for (q = p; q < name + namelen && *q && *q != '.'; q++)
156862587Sitojun				i++;
156962587Sitojun			/* result does not fit into mbuf */
157062587Sitojun			if (cp + i + 1 >= ep)
157162587Sitojun				goto fail;
157278704Sume			/*
157378704Sume			 * DNS label length restriction, RFC1035 page 8.
157478704Sume			 * "i == 0" case is included here to avoid returning
157578704Sume			 * 0-length label on "foo..bar".
157678704Sume			 */
157778704Sume			if (i <= 0 || i >= 64)
157862587Sitojun				goto fail;
157962587Sitojun			*cp++ = i;
158062587Sitojun			bcopy(p, cp, i);
158162587Sitojun			cp += i;
158262587Sitojun			p = q;
158362587Sitojun			if (p < name + namelen && *p == '.')
158462587Sitojun				p++;
158562587Sitojun		}
158662587Sitojun		/* termination */
158762587Sitojun		if (cp + nterm >= ep)
158862587Sitojun			goto fail;
158962587Sitojun		while (nterm-- > 0)
159062587Sitojun			*cp++ = '\0';
159162587Sitojun		m->m_len = cp - mtod(m, char *);
159262587Sitojun		return m;
159362587Sitojun	}
159462587Sitojun
159562587Sitojun	panic("should not reach here");
159695023Ssuz	/* NOTREACHED */
159762587Sitojun
159862587Sitojun fail:
159962587Sitojun	if (m)
160062587Sitojun		m_freem(m);
160162587Sitojun	return NULL;
160262587Sitojun}
160362587Sitojun
160462587Sitojun/*
160562587Sitojun * check if two DNS-encoded string matches.  takes care of truncated
160662587Sitojun * form (with \0\0 at the end).  no compression support.
160778064Sume * XXX upper/lowercase match (see RFC2065)
160862587Sitojun */
160962587Sitojunstatic int
1610171259Sdelphijni6_dnsmatch(const char *a, int alen, const char *b, int blen)
161162587Sitojun{
161262587Sitojun	const char *a0, *b0;
161362587Sitojun	int l;
161462587Sitojun
161562587Sitojun	/* simplest case - need validation? */
161662587Sitojun	if (alen == blen && bcmp(a, b, alen) == 0)
161762587Sitojun		return 1;
161862587Sitojun
161962587Sitojun	a0 = a;
162062587Sitojun	b0 = b;
162162587Sitojun
162262587Sitojun	/* termination is mandatory */
162362587Sitojun	if (alen < 2 || blen < 2)
162462587Sitojun		return 0;
162562587Sitojun	if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
162662587Sitojun		return 0;
162762587Sitojun	alen--;
162862587Sitojun	blen--;
162962587Sitojun
163062587Sitojun	while (a - a0 < alen && b - b0 < blen) {
163162587Sitojun		if (a - a0 + 1 > alen || b - b0 + 1 > blen)
163262587Sitojun			return 0;
163362587Sitojun
163462587Sitojun		if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
163562587Sitojun			return 0;
163662587Sitojun		/* we don't support compression yet */
163762587Sitojun		if (a[0] >= 64 || b[0] >= 64)
163862587Sitojun			return 0;
163962587Sitojun
164062587Sitojun		/* truncated case */
164162587Sitojun		if (a[0] == 0 && a - a0 == alen - 1)
164262587Sitojun			return 1;
164362587Sitojun		if (b[0] == 0 && b - b0 == blen - 1)
164462587Sitojun			return 1;
164562587Sitojun		if (a[0] == 0 || b[0] == 0)
164662587Sitojun			return 0;
164762587Sitojun
164862587Sitojun		if (a[0] != b[0])
164962587Sitojun			return 0;
165062587Sitojun		l = a[0];
165162587Sitojun		if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
165262587Sitojun			return 0;
165362587Sitojun		if (bcmp(a + 1, b + 1, l) != 0)
165462587Sitojun			return 0;
165562587Sitojun
165662587Sitojun		a += 1 + l;
165762587Sitojun		b += 1 + l;
165862587Sitojun	}
165962587Sitojun
166062587Sitojun	if (a - a0 == alen && b - b0 == blen)
166162587Sitojun		return 1;
166262587Sitojun	else
166362587Sitojun		return 0;
166462587Sitojun}
166562587Sitojun
166662587Sitojun/*
166753541Sshin * calculate the number of addresses to be returned in the node info reply.
166853541Sshin */
166953541Sshinstatic int
1670171259Sdelphijni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
1671171259Sdelphij    struct in6_addr *subj)
167253541Sshin{
167378064Sume	struct ifnet *ifp;
167478064Sume	struct in6_ifaddr *ifa6;
167578064Sume	struct ifaddr *ifa;
167653541Sshin	int addrs = 0, addrsofif, iffound = 0;
167778064Sume	int niflags = ni6->ni_flags;
167853541Sshin
167978064Sume	if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
168078064Sume		switch (ni6->ni_code) {
168178064Sume		case ICMP6_NI_SUBJ_IPV6:
168278064Sume			if (subj == NULL) /* must be impossible... */
1683120856Sume				return (0);
168478064Sume			break;
168578064Sume		default:
168678064Sume			/*
168778064Sume			 * XXX: we only support IPv6 subject address for
168878064Sume			 * this Qtype.
168978064Sume			 */
1690120856Sume			return (0);
169178064Sume		}
169278064Sume	}
169378064Sume
1694108172Shsu	IFNET_RLOCK();
1695181803Sbz	for (ifp = TAILQ_FIRST(&V_ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
169653541Sshin		addrsofif = 0;
1697191317Srwatson		IF_ADDR_LOCK(ifp);
1698191340Srwatson		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
169953541Sshin			if (ifa->ifa_addr->sa_family != AF_INET6)
170053541Sshin				continue;
170153541Sshin			ifa6 = (struct in6_ifaddr *)ifa;
170253541Sshin
170378064Sume			if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1704148892Sume			    IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr))
170553541Sshin				iffound = 1;
170653541Sshin
170762587Sitojun			/*
170862587Sitojun			 * IPv4-mapped addresses can only be returned by a
170962587Sitojun			 * Node Information proxy, since they represent
171062587Sitojun			 * addresses of IPv4-only nodes, which perforce do
171162587Sitojun			 * not implement this protocol.
171278064Sume			 * [icmp-name-lookups-07, Section 5.4]
171362587Sitojun			 * So we don't support NI_NODEADDR_FLAG_COMPAT in
171462587Sitojun			 * this function at this moment.
171562587Sitojun			 */
171662587Sitojun
171753541Sshin			/* What do we have to do about ::1? */
171878064Sume			switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
171978064Sume			case IPV6_ADDR_SCOPE_LINKLOCAL:
172078064Sume				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
172178064Sume					continue;
172253541Sshin				break;
172378064Sume			case IPV6_ADDR_SCOPE_SITELOCAL:
172478064Sume				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
172578064Sume					continue;
172653541Sshin				break;
172778064Sume			case IPV6_ADDR_SCOPE_GLOBAL:
172878064Sume				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
172978064Sume					continue;
173078064Sume				break;
173178064Sume			default:
173278064Sume				continue;
173353541Sshin			}
173478064Sume
173578064Sume			/*
173678064Sume			 * check if anycast is okay.
173795023Ssuz			 * XXX: just experimental.  not in the spec.
173878064Sume			 */
173978064Sume			if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
174078064Sume			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
174178064Sume				continue; /* we need only unicast addresses */
174278064Sume			if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1743181803Sbz			    (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
174478064Sume				continue;
174578064Sume			}
174678064Sume			addrsofif++; /* count the address */
174753541Sshin		}
1748191317Srwatson		IF_ADDR_UNLOCK(ifp);
174953541Sshin		if (iffound) {
175053541Sshin			*ifpp = ifp;
1751108172Shsu			IFNET_RUNLOCK();
1752120856Sume			return (addrsofif);
175353541Sshin		}
175453541Sshin
175553541Sshin		addrs += addrsofif;
175653541Sshin	}
1757108172Shsu	IFNET_RUNLOCK();
175853541Sshin
1759120856Sume	return (addrs);
176053541Sshin}
176153541Sshin
176253541Sshinstatic int
1763171259Sdelphijni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1764171259Sdelphij    struct ifnet *ifp0, int resid)
176553541Sshin{
1766181803Sbz	struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&V_ifnet);
176778064Sume	struct in6_ifaddr *ifa6;
176878064Sume	struct ifaddr *ifa;
176978064Sume	struct ifnet *ifp_dep = NULL;
177078064Sume	int copied = 0, allow_deprecated = 0;
177153541Sshin	u_char *cp = (u_char *)(nni6 + 1);
177278064Sume	int niflags = ni6->ni_flags;
177378064Sume	u_int32_t ltime;
177453541Sshin
177578064Sume	if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1776120856Sume		return (0);	/* needless to copy */
1777120891Sume
1778108172Shsu	IFNET_RLOCK();
177978064Sume  again:
1780120892Sume
1781120891Sume	for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1782191317Srwatson		IF_ADDR_LOCK(ifp);
1783191340Srwatson		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
178453541Sshin			if (ifa->ifa_addr->sa_family != AF_INET6)
178553541Sshin				continue;
178653541Sshin			ifa6 = (struct in6_ifaddr *)ifa;
178753541Sshin
178878064Sume			if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
178978064Sume			    allow_deprecated == 0) {
179078064Sume				/*
179178064Sume				 * prefererred address should be put before
179278064Sume				 * deprecated addresses.
179378064Sume				 */
179478064Sume
179578064Sume				/* record the interface for later search */
179678064Sume				if (ifp_dep == NULL)
179778064Sume					ifp_dep = ifp;
179878064Sume
179978064Sume				continue;
1800120891Sume			} else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1801120891Sume			    allow_deprecated != 0)
180278064Sume				continue; /* we now collect deprecated addrs */
180353541Sshin
180453541Sshin			/* What do we have to do about ::1? */
180578064Sume			switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
180678064Sume			case IPV6_ADDR_SCOPE_LINKLOCAL:
180778064Sume				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
180878064Sume					continue;
180953541Sshin				break;
181078064Sume			case IPV6_ADDR_SCOPE_SITELOCAL:
181178064Sume				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
181278064Sume					continue;
181353541Sshin				break;
181478064Sume			case IPV6_ADDR_SCOPE_GLOBAL:
181578064Sume				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
181678064Sume					continue;
181778064Sume				break;
181878064Sume			default:
181978064Sume				continue;
182053541Sshin			}
182153541Sshin
182278064Sume			/*
182378064Sume			 * check if anycast is okay.
1824120891Sume			 * XXX: just experimental.  not in the spec.
182578064Sume			 */
182678064Sume			if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
182778064Sume			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
182878064Sume				continue;
182978064Sume			if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1830181803Sbz			    (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
183178064Sume				continue;
183253541Sshin			}
183378064Sume
183478064Sume			/* now we can copy the address */
183578064Sume			if (resid < sizeof(struct in6_addr) +
183678064Sume			    sizeof(u_int32_t)) {
1837191317Srwatson				IF_ADDR_UNLOCK(ifp);
183878064Sume				/*
183978064Sume				 * We give up much more copy.
184078064Sume				 * Set the truncate flag and return.
184178064Sume				 */
1842120891Sume				nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1843108172Shsu				IFNET_RUNLOCK();
1844120856Sume				return (copied);
184578064Sume			}
184678064Sume
184778064Sume			/*
184878064Sume			 * Set the TTL of the address.
184978064Sume			 * The TTL value should be one of the following
185078064Sume			 * according to the specification:
185178064Sume			 *
185278064Sume			 * 1. The remaining lifetime of a DHCP lease on the
185378064Sume			 *    address, or
185478064Sume			 * 2. The remaining Valid Lifetime of a prefix from
185578064Sume			 *    which the address was derived through Stateless
185678064Sume			 *    Autoconfiguration.
185778064Sume			 *
185878064Sume			 * Note that we currently do not support stateful
185978064Sume			 * address configuration by DHCPv6, so the former
186078064Sume			 * case can't happen.
186178064Sume			 */
186278064Sume			if (ifa6->ia6_lifetime.ia6t_expire == 0)
186378064Sume				ltime = ND6_INFINITE_LIFETIME;
186478064Sume			else {
186578064Sume				if (ifa6->ia6_lifetime.ia6t_expire >
186678064Sume				    time_second)
186778064Sume					ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
186878064Sume				else
186978064Sume					ltime = 0;
187078064Sume			}
1871120891Sume
187278064Sume			bcopy(&ltime, cp, sizeof(u_int32_t));
187378064Sume			cp += sizeof(u_int32_t);
187478064Sume
187578064Sume			/* copy the address itself */
187678064Sume			bcopy(&ifa6->ia_addr.sin6_addr, cp,
1877120891Sume			    sizeof(struct in6_addr));
1878121315Sume			in6_clearscope((struct in6_addr *)cp); /* XXX */
187978064Sume			cp += sizeof(struct in6_addr);
1880120891Sume
188178064Sume			resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1882120891Sume			copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
188353541Sshin		}
1884191317Srwatson		IF_ADDR_UNLOCK(ifp);
188553541Sshin		if (ifp0)	/* we need search only on the specified IF */
188653541Sshin			break;
188753541Sshin	}
188853541Sshin
188978064Sume	if (allow_deprecated == 0 && ifp_dep != NULL) {
189078064Sume		ifp = ifp_dep;
189178064Sume		allow_deprecated = 1;
189278064Sume
189378064Sume		goto again;
189478064Sume	}
189578064Sume
1896108172Shsu	IFNET_RUNLOCK();
1897108172Shsu
1898120856Sume	return (copied);
189953541Sshin}
190053541Sshin
190153541Sshin/*
190253541Sshin * XXX almost dup'ed code with rip6_input.
190353541Sshin */
190453541Sshinstatic int
1905171259Sdelphijicmp6_rip6_input(struct mbuf **mp, int off)
190653541Sshin{
190753541Sshin	struct mbuf *m = *mp;
190878064Sume	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1909186141Sbz	struct inpcb *in6p;
1910186141Sbz	struct inpcb *last = NULL;
1911121315Sume	struct sockaddr_in6 fromsa;
191253541Sshin	struct icmp6_hdr *icmp6;
191353541Sshin	struct mbuf *opts = NULL;
191453541Sshin
191562587Sitojun#ifndef PULLDOWN_TEST
191653541Sshin	/* this is assumed to be safe. */
191753541Sshin	icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
191862587Sitojun#else
191962587Sitojun	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
192062587Sitojun	if (icmp6 == NULL) {
192162587Sitojun		/* m is already reclaimed */
1922120891Sume		return (IPPROTO_DONE);
192362587Sitojun	}
192462587Sitojun#endif
192553541Sshin
1926148385Sume	/*
1927148385Sume	 * XXX: the address may have embedded scope zone ID, which should be
1928148385Sume	 * hidden from applications.
1929148385Sume	 */
1930121315Sume	bzero(&fromsa, sizeof(fromsa));
1931148385Sume	fromsa.sin6_family = AF_INET6;
1932121315Sume	fromsa.sin6_len = sizeof(struct sockaddr_in6);
1933148385Sume	fromsa.sin6_addr = ip6->ip6_src;
1934148385Sume	if (sa6_recoverscope(&fromsa)) {
1935148385Sume		m_freem(m);
1936148385Sume		return (IPPROTO_DONE);
1937148385Sume	}
193853541Sshin
1939181803Sbz	INP_INFO_RLOCK(&V_ripcbinfo);
1940181803Sbz	LIST_FOREACH(in6p, &V_ripcb, inp_list) {
1941180850Smav		if ((in6p->inp_vflag & INP_IPV6) == 0)
194253541Sshin			continue;
1943186141Sbz		if (in6p->inp_ip_p != IPPROTO_ICMPV6)
1944180850Smav			continue;
194553541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
194653541Sshin		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1947180850Smav			continue;
194853541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
194953541Sshin		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1950180850Smav			continue;
1951180932Smav		INP_RLOCK(in6p);
1952180965Srwatson		if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1953180965Srwatson		    in6p->in6p_icmp6filt)) {
1954180932Smav			INP_RUNLOCK(in6p);
1955180850Smav			continue;
1956180932Smav		}
1957186163Skmacy		if (last != NULL) {
1958121809Sume			struct	mbuf *n = NULL;
1959121809Sume
1960121809Sume			/*
1961121809Sume			 * Recent network drivers tend to allocate a single
1962121809Sume			 * mbuf cluster, rather than to make a couple of
1963121809Sume			 * mbufs without clusters.  Also, since the IPv6 code
1964121809Sume			 * path tries to avoid m_pullup(), it is highly
1965121809Sume			 * probable that we still have an mbuf cluster here
1966121809Sume			 * even though the necessary length can be stored in an
1967121809Sume			 * mbuf's internal buffer.
1968121809Sume			 * Meanwhile, the default size of the receive socket
1969121809Sume			 * buffer for raw sockets is not so large.  This means
1970121809Sume			 * the possibility of packet loss is relatively higher
1971121809Sume			 * than before.  To avoid this scenario, we copy the
1972121809Sume			 * received data to a separate mbuf that does not use
1973121809Sume			 * a cluster, if possible.
1974121809Sume			 * XXX: it is better to copy the data after stripping
1975121809Sume			 * intermediate headers.
1976121809Sume			 */
1977121809Sume			if ((m->m_flags & M_EXT) && m->m_next == NULL &&
1978121809Sume			    m->m_len <= MHLEN) {
1979121809Sume				MGET(n, M_DONTWAIT, m->m_type);
1980121809Sume				if (n != NULL) {
1981145065Sgnn					if (m_dup_pkthdr(n, m, M_NOWAIT)) {
1982171260Sdelphij						bcopy(m->m_data, n->m_data,
1983145065Sgnn						      m->m_len);
1984145065Sgnn						n->m_len = m->m_len;
1985145065Sgnn					} else {
1986145065Sgnn						m_free(n);
1987145065Sgnn						n = NULL;
1988145065Sgnn					}
1989121809Sume				}
1990121809Sume			}
1991121809Sume			if (n != NULL ||
1992121809Sume			    (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1993186223Sbz				if (last->inp_flags & INP_CONTROLOPTS)
1994121674Sume					ip6_savecontrol(last, n, &opts);
199553541Sshin				/* strip intermediate headers */
199653541Sshin				m_adj(n, off);
1997186141Sbz				SOCKBUF_LOCK(&last->inp_socket->so_rcv);
1998160591Srwatson				if (sbappendaddr_locked(
1999186141Sbz				    &last->inp_socket->so_rcv,
2000121315Sume				    (struct sockaddr *)&fromsa, n, opts)
2001120891Sume				    == 0) {
200253541Sshin					/* should notify about lost packet */
200353541Sshin					m_freem(n);
200478064Sume					if (opts) {
200553541Sshin						m_freem(opts);
200678064Sume					}
2007160591Srwatson					SOCKBUF_UNLOCK(
2008186141Sbz					    &last->inp_socket->so_rcv);
200997658Stanimura				} else
2010186141Sbz					sorwakeup_locked(last->inp_socket);
201153541Sshin				opts = NULL;
201253541Sshin			}
2013178378Srwatson			INP_RUNLOCK(last);
201453541Sshin		}
201553541Sshin		last = in6p;
201653541Sshin	}
2017181803Sbz	INP_INFO_RUNLOCK(&V_ripcbinfo);
2018186163Skmacy	if (last != NULL) {
2019186223Sbz		if (last->inp_flags & INP_CONTROLOPTS)
2020121674Sume			ip6_savecontrol(last, m, &opts);
202153541Sshin		/* strip intermediate headers */
202253541Sshin		m_adj(m, off);
2023121809Sume
2024121809Sume		/* avoid using mbuf clusters if possible (see above) */
2025121809Sume		if ((m->m_flags & M_EXT) && m->m_next == NULL &&
2026121809Sume		    m->m_len <= MHLEN) {
2027121809Sume			struct mbuf *n;
2028121809Sume
2029121809Sume			MGET(n, M_DONTWAIT, m->m_type);
2030121809Sume			if (n != NULL) {
2031145065Sgnn				if (m_dup_pkthdr(n, m, M_NOWAIT)) {
2032145065Sgnn					bcopy(m->m_data, n->m_data, m->m_len);
2033145065Sgnn					n->m_len = m->m_len;
2034171260Sdelphij
2035145065Sgnn					m_freem(m);
2036145065Sgnn					m = n;
2037145065Sgnn				} else {
2038145065Sgnn					m_freem(n);
2039145065Sgnn					n = NULL;
2040145065Sgnn				}
2041121809Sume			}
2042121809Sume		}
2043186141Sbz		SOCKBUF_LOCK(&last->inp_socket->so_rcv);
2044186141Sbz		if (sbappendaddr_locked(&last->inp_socket->so_rcv,
2045121315Sume		    (struct sockaddr *)&fromsa, m, opts) == 0) {
204653541Sshin			m_freem(m);
204753541Sshin			if (opts)
204853541Sshin				m_freem(opts);
2049186141Sbz			SOCKBUF_UNLOCK(&last->inp_socket->so_rcv);
205097658Stanimura		} else
2051186141Sbz			sorwakeup_locked(last->inp_socket);
2052178378Srwatson		INP_RUNLOCK(last);
205353541Sshin	} else {
205453541Sshin		m_freem(m);
2055191672Sbms		IP6STAT_DEC(ip6s_delivered);
205653541Sshin	}
205753541Sshin	return IPPROTO_DONE;
205853541Sshin}
205953541Sshin
206053541Sshin/*
206153541Sshin * Reflect the ip6 packet back to the source.
206262587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf.
206353541Sshin */
206453541Sshinvoid
2065171259Sdelphijicmp6_reflect(struct mbuf *m, size_t off)
206653541Sshin{
206762587Sitojun	struct ip6_hdr *ip6;
206853541Sshin	struct icmp6_hdr *icmp6;
2069194760Srwatson	struct in6_ifaddr *ia = NULL;
207062587Sitojun	int plen;
207153541Sshin	int type, code;
207253541Sshin	struct ifnet *outif = NULL;
2073194777Sbz	struct in6_addr origdst, src, *srcp = NULL;
207453541Sshin
207562587Sitojun	/* too short to reflect */
207662587Sitojun	if (off < sizeof(struct ip6_hdr)) {
207778064Sume		nd6log((LOG_DEBUG,
207878064Sume		    "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
207978064Sume		    (u_long)off, (u_long)sizeof(struct ip6_hdr),
208078064Sume		    __FILE__, __LINE__));
208162587Sitojun		goto bad;
208262587Sitojun	}
208362587Sitojun
208453541Sshin	/*
208553541Sshin	 * If there are extra headers between IPv6 and ICMPv6, strip
208653541Sshin	 * off that header first.
208753541Sshin	 */
208862587Sitojun#ifdef DIAGNOSTIC
208962587Sitojun	if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
209062587Sitojun		panic("assumption failed in icmp6_reflect");
209162587Sitojun#endif
209262587Sitojun	if (off > sizeof(struct ip6_hdr)) {
209362587Sitojun		size_t l;
209462587Sitojun		struct ip6_hdr nip6;
209553541Sshin
209662587Sitojun		l = off - sizeof(struct ip6_hdr);
209762587Sitojun		m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
209862587Sitojun		m_adj(m, l);
209962587Sitojun		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
210062587Sitojun		if (m->m_len < l) {
210162587Sitojun			if ((m = m_pullup(m, l)) == NULL)
210262587Sitojun				return;
210353541Sshin		}
210462587Sitojun		bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
210562587Sitojun	} else /* off == sizeof(struct ip6_hdr) */ {
210662587Sitojun		size_t l;
210762587Sitojun		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
210862587Sitojun		if (m->m_len < l) {
210962587Sitojun			if ((m = m_pullup(m, l)) == NULL)
211062587Sitojun				return;
211153541Sshin		}
211253541Sshin	}
211362587Sitojun	plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
211462587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
211562587Sitojun	ip6->ip6_nxt = IPPROTO_ICMPV6;
211653541Sshin	icmp6 = (struct icmp6_hdr *)(ip6 + 1);
211753541Sshin	type = icmp6->icmp6_type; /* keep type for statistics */
211853541Sshin	code = icmp6->icmp6_code; /* ditto. */
211953541Sshin
2120148385Sume	origdst = ip6->ip6_dst;
212153541Sshin	/*
212253541Sshin	 * ip6_input() drops a packet if its src is multicast.
212353541Sshin	 * So, the src is never multicast.
212453541Sshin	 */
212553541Sshin	ip6->ip6_dst = ip6->ip6_src;
212653541Sshin
212753541Sshin	/*
2128120891Sume	 * If the incoming packet was addressed directly to us (i.e. unicast),
212953541Sshin	 * use dst as the src for the reply.
2130120891Sume	 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
213162587Sitojun	 * (for example) when we encounter an error while forwarding procedure
213262587Sitojun	 * destined to a duplicated address of ours.
2133148385Sume	 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2134148385Sume	 * procedure of an outgoing packet of our own, in which case we need
2135148385Sume	 * to search in the ifaddr list.
213653541Sshin	 */
2137148385Sume	if (!IN6_IS_ADDR_MULTICAST(&origdst)) {
2138148385Sume		if ((ia = ip6_getdstifaddr(m))) {
2139148385Sume			if (!(ia->ia6_flags &
2140148385Sume			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)))
2141194777Sbz				srcp = &ia->ia_addr.sin6_addr;
2142148385Sume		} else {
2143148385Sume			struct sockaddr_in6 d;
2144148385Sume
2145148385Sume			bzero(&d, sizeof(d));
2146148385Sume			d.sin6_family = AF_INET6;
2147148385Sume			d.sin6_len = sizeof(d);
2148148385Sume			d.sin6_addr = origdst;
2149148385Sume			ia = (struct in6_ifaddr *)
2150148385Sume			    ifa_ifwithaddr((struct sockaddr *)&d);
2151148385Sume			if (ia &&
2152148385Sume			    !(ia->ia6_flags &
2153148385Sume			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) {
2154194777Sbz				srcp = &ia->ia_addr.sin6_addr;
2155148385Sume			}
215653541Sshin		}
215753541Sshin	}
215853541Sshin
2159194777Sbz	if (srcp == NULL) {
216078064Sume		int e;
2161148385Sume		struct sockaddr_in6 sin6;
216278064Sume		struct route_in6 ro;
216378064Sume
216453541Sshin		/*
216562587Sitojun		 * This case matches to multicasts, our anycast, or unicasts
216695023Ssuz		 * that we do not own.  Select a source address based on the
216778064Sume		 * source address of the erroneous packet.
216853541Sshin		 */
2169148385Sume		bzero(&sin6, sizeof(sin6));
2170148385Sume		sin6.sin6_family = AF_INET6;
2171148385Sume		sin6.sin6_len = sizeof(sin6);
2172148385Sume		sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */
2173148385Sume
217478064Sume		bzero(&ro, sizeof(ro));
2175194777Sbz		e = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &src);
217678064Sume		if (ro.ro_rt)
217778064Sume			RTFREE(ro.ro_rt); /* XXX: we could use this */
2178194777Sbz		if (e) {
2179165118Sbz			char ip6buf[INET6_ADDRSTRLEN];
218078064Sume			nd6log((LOG_DEBUG,
218178064Sume			    "icmp6_reflect: source can't be determined: "
218278064Sume			    "dst=%s, error=%d\n",
2183165118Sbz			    ip6_sprintf(ip6buf, &sin6.sin6_addr), e));
218478064Sume			goto bad;
218578064Sume		}
2186194777Sbz		srcp = &src;
218778064Sume	}
218853541Sshin
2189194777Sbz	ip6->ip6_src = *srcp;
219053541Sshin	ip6->ip6_flow = 0;
219162587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
219262587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
219353541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
2194121472Sume	if (outif)
2195121472Sume		ip6->ip6_hlim = ND_IFINFO(outif)->chlim;
2196121472Sume	else if (m->m_pkthdr.rcvif) {
219753541Sshin		/* XXX: This may not be the outgoing interface */
2198121161Sume		ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
219978064Sume	} else
2200181803Sbz		ip6->ip6_hlim = V_ip6_defhlim;
220153541Sshin
220253541Sshin	icmp6->icmp6_cksum = 0;
220353541Sshin	icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2204120891Sume	    sizeof(struct ip6_hdr), plen);
220553541Sshin
220653541Sshin	/*
220778064Sume	 * XXX option handling
220853541Sshin	 */
220953541Sshin
221053541Sshin	m->m_flags &= ~(M_BCAST|M_MCAST);
221153541Sshin
2212105194Ssam	ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
221353541Sshin	if (outif)
221453541Sshin		icmp6_ifoutstat_inc(outif, type, code);
221553541Sshin
2216194760Srwatson	if (ia != NULL)
2217194760Srwatson		ifa_free(&ia->ia_ifa);
221853541Sshin	return;
221953541Sshin
222053541Sshin bad:
2221194760Srwatson	if (ia != NULL)
2222194760Srwatson		ifa_free(&ia->ia_ifa);
222353541Sshin	m_freem(m);
222453541Sshin	return;
222553541Sshin}
222653541Sshin
222753541Sshinvoid
2228171259Sdelphijicmp6_fasttimo(void)
222953541Sshin{
223062587Sitojun
2231191672Sbms	mld_fasttimo();
223253541Sshin}
223353541Sshin
2234191672Sbmsvoid
2235191672Sbmsicmp6_slowtimo(void)
2236191672Sbms{
2237191672Sbms
2238191672Sbms	mld_slowtimo();
2239191672Sbms}
2240191672Sbms
224153541Sshinstatic const char *
2242171259Sdelphijicmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
2243171259Sdelphij    struct in6_addr *tgt6)
224453541Sshin{
224553541Sshin	static char buf[1024];
2246165118Sbz	char ip6bufs[INET6_ADDRSTRLEN];
2247165118Sbz	char ip6bufd[INET6_ADDRSTRLEN];
2248165118Sbz	char ip6buft[INET6_ADDRSTRLEN];
224953541Sshin	snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2250165118Sbz	    ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6),
2251165118Sbz	    ip6_sprintf(ip6buft, tgt6));
225253541Sshin	return buf;
225353541Sshin}
225453541Sshin
225553541Sshinvoid
2256171259Sdelphijicmp6_redirect_input(struct mbuf *m, int off)
225753541Sshin{
2258171133Sgnn	struct ifnet *ifp;
225953541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
226062587Sitojun	struct nd_redirect *nd_rd;
226153541Sshin	int icmp6len = ntohs(ip6->ip6_plen);
226253541Sshin	char *lladdr = NULL;
226353541Sshin	int lladdrlen = 0;
226453541Sshin	u_char *redirhdr = NULL;
226553541Sshin	int redirhdrlen = 0;
226653541Sshin	struct rtentry *rt = NULL;
226753541Sshin	int is_router;
226853541Sshin	int is_onlink;
226953541Sshin	struct in6_addr src6 = ip6->ip6_src;
227062587Sitojun	struct in6_addr redtgt6;
227162587Sitojun	struct in6_addr reddst6;
227253541Sshin	union nd_opts ndopts;
2273165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
227453541Sshin
2275171133Sgnn	if (!m)
227653541Sshin		return;
227753541Sshin
2278171133Sgnn	ifp = m->m_pkthdr.rcvif;
2279171133Sgnn
2280171133Sgnn	if (!ifp)
2281171133Sgnn		return;
2282171133Sgnn
228353541Sshin	/* XXX if we are router, we don't update route by icmp6 redirect */
2284181803Sbz	if (V_ip6_forwarding)
228562587Sitojun		goto freeit;
2286181803Sbz	if (!V_icmp6_rediraccept)
228762587Sitojun		goto freeit;
228862587Sitojun
228962587Sitojun#ifndef PULLDOWN_TEST
229062587Sitojun	IP6_EXTHDR_CHECK(m, off, icmp6len,);
229162587Sitojun	nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
229262587Sitojun#else
229362587Sitojun	IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
229462587Sitojun	if (nd_rd == NULL) {
2295190964Srwatson		ICMP6STAT_INC(icp6s_tooshort);
229653541Sshin		return;
229762587Sitojun	}
229862587Sitojun#endif
229962587Sitojun	redtgt6 = nd_rd->nd_rd_target;
230062587Sitojun	reddst6 = nd_rd->nd_rd_dst;
230153541Sshin
2302148385Sume	if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) ||
2303148385Sume	    in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) {
2304148385Sume		goto freeit;
2305148385Sume	}
230653541Sshin
230753541Sshin	/* validation */
230853541Sshin	if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
230978064Sume		nd6log((LOG_ERR,
2310120891Sume		    "ICMP6 redirect sent from %s rejected; "
2311120891Sume		    "must be from linklocal\n",
2312165118Sbz		    ip6_sprintf(ip6buf, &src6)));
231378064Sume		goto bad;
231453541Sshin	}
231553541Sshin	if (ip6->ip6_hlim != 255) {
231678064Sume		nd6log((LOG_ERR,
2317120891Sume		    "ICMP6 redirect sent from %s rejected; "
2318120891Sume		    "hlim=%d (must be 255)\n",
2319165118Sbz		    ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim));
232078064Sume		goto bad;
232153541Sshin	}
232253541Sshin    {
232353541Sshin	/* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
232453541Sshin	struct sockaddr_in6 sin6;
232553541Sshin	struct in6_addr *gw6;
232653541Sshin
232753541Sshin	bzero(&sin6, sizeof(sin6));
232853541Sshin	sin6.sin6_family = AF_INET6;
232953541Sshin	sin6.sin6_len = sizeof(struct sockaddr_in6);
233053541Sshin	bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
233153541Sshin	rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
233253541Sshin	if (rt) {
233365895Sume		if (rt->rt_gateway == NULL ||
233465895Sume		    rt->rt_gateway->sa_family != AF_INET6) {
233578064Sume			nd6log((LOG_ERR,
233665895Sume			    "ICMP6 redirect rejected; no route "
233765895Sume			    "with inet6 gateway found for redirect dst: %s\n",
233878064Sume			    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2339120727Ssam			RTFREE_LOCKED(rt);
234078064Sume			goto bad;
234165895Sume		}
234265895Sume
234353541Sshin		gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
234453541Sshin		if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
234578064Sume			nd6log((LOG_ERR,
2346120891Sume			    "ICMP6 redirect rejected; "
2347120891Sume			    "not equal to gw-for-src=%s (must be same): "
2348120891Sume			    "%s\n",
2349165118Sbz			    ip6_sprintf(ip6buf, gw6),
2350120891Sume			    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2351120727Ssam			RTFREE_LOCKED(rt);
235278064Sume			goto bad;
235353541Sshin		}
235453541Sshin	} else {
235578064Sume		nd6log((LOG_ERR,
2356120891Sume		    "ICMP6 redirect rejected; "
2357120891Sume		    "no route found for redirect dst: %s\n",
2358120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
235978064Sume		goto bad;
236053541Sshin	}
2361120727Ssam	RTFREE_LOCKED(rt);
236253541Sshin	rt = NULL;
236353541Sshin    }
236453541Sshin	if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
236578064Sume		nd6log((LOG_ERR,
2366120891Sume		    "ICMP6 redirect rejected; "
2367120891Sume		    "redirect dst must be unicast: %s\n",
2368120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
236978064Sume		goto bad;
237053541Sshin	}
237153541Sshin
237253541Sshin	is_router = is_onlink = 0;
237353541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
237453541Sshin		is_router = 1;	/* router case */
237553541Sshin	if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
237653541Sshin		is_onlink = 1;	/* on-link destination case */
237753541Sshin	if (!is_router && !is_onlink) {
237878064Sume		nd6log((LOG_ERR,
2379120891Sume		    "ICMP6 redirect rejected; "
2380120891Sume		    "neither router case nor onlink case: %s\n",
2381120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
238278064Sume		goto bad;
238353541Sshin	}
238453541Sshin	/* validation passed */
238553541Sshin
238653541Sshin	icmp6len -= sizeof(*nd_rd);
238753541Sshin	nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
238853541Sshin	if (nd6_options(&ndopts) < 0) {
238978064Sume		nd6log((LOG_INFO, "icmp6_redirect_input: "
2390120891Sume		    "invalid ND option, rejected: %s\n",
2391120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
239278064Sume		/* nd6_options have incremented stats */
239362587Sitojun		goto freeit;
239453541Sshin	}
239553541Sshin
239653541Sshin	if (ndopts.nd_opts_tgt_lladdr) {
239753541Sshin		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
239853541Sshin		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
239953541Sshin	}
240053541Sshin
240153541Sshin	if (ndopts.nd_opts_rh) {
240253541Sshin		redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
240353541Sshin		redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
240453541Sshin	}
240553541Sshin
240653541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
240778064Sume		nd6log((LOG_INFO,
2408120891Sume		    "icmp6_redirect_input: lladdrlen mismatch for %s "
2409120891Sume		    "(if %d, icmp6 packet %d): %s\n",
2410165118Sbz		    ip6_sprintf(ip6buf, &redtgt6),
2411165118Sbz		    ifp->if_addrlen, lladdrlen - 2,
2412120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
241378064Sume		goto bad;
241453541Sshin	}
241553541Sshin
241653541Sshin	/* RFC 2461 8.3 */
241753541Sshin	nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2418120891Sume	    is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
241953541Sshin
242095023Ssuz	if (!is_onlink) {	/* better router case.  perform rtredirect. */
242153541Sshin		/* perform rtredirect */
242253541Sshin		struct sockaddr_in6 sdst;
242353541Sshin		struct sockaddr_in6 sgw;
242453541Sshin		struct sockaddr_in6 ssrc;
242553541Sshin
242653541Sshin		bzero(&sdst, sizeof(sdst));
242753541Sshin		bzero(&sgw, sizeof(sgw));
242853541Sshin		bzero(&ssrc, sizeof(ssrc));
242953541Sshin		sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
243053541Sshin		sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
243153541Sshin			sizeof(struct sockaddr_in6);
243253541Sshin		bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
243353541Sshin		bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
243453541Sshin		bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
243553541Sshin		rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2436120891Sume		    (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2437120891Sume		    (struct sockaddr *)&ssrc);
243853541Sshin	}
243953541Sshin	/* finally update cached route in each socket via pfctlinput */
244053541Sshin    {
244153541Sshin	struct sockaddr_in6 sdst;
244253541Sshin
244353541Sshin	bzero(&sdst, sizeof(sdst));
244453541Sshin	sdst.sin6_family = AF_INET6;
244553541Sshin	sdst.sin6_len = sizeof(struct sockaddr_in6);
244653541Sshin	bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
244753541Sshin	pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2448171167Sgnn#ifdef IPSEC
244953541Sshin	key_sa_routechange((struct sockaddr *)&sdst);
2450171167Sgnn#endif /* IPSEC */
245153541Sshin    }
245262587Sitojun
245362587Sitojun freeit:
245462587Sitojun	m_freem(m);
245578064Sume	return;
245678064Sume
245778064Sume bad:
2458190964Srwatson	ICMP6STAT_INC(icp6s_badredirect);
245978064Sume	m_freem(m);
246053541Sshin}
246153541Sshin
246253541Sshinvoid
2463171259Sdelphijicmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
246453541Sshin{
246553541Sshin	struct ifnet *ifp;	/* my outgoing interface */
246653541Sshin	struct in6_addr *ifp_ll6;
246753541Sshin	struct in6_addr *router_ll6;
246853541Sshin	struct ip6_hdr *sip6;	/* m0 as struct ip6_hdr */
246953541Sshin	struct mbuf *m = NULL;	/* newly allocated one */
247053541Sshin	struct ip6_hdr *ip6;	/* m as struct ip6_hdr */
247153541Sshin	struct nd_redirect *nd_rd;
2472186157Skmacy	struct llentry *ln = NULL;
247353541Sshin	size_t maxlen;
247453541Sshin	u_char *p;
247553541Sshin	struct ifnet *outif = NULL;
247662587Sitojun	struct sockaddr_in6 src_sa;
247753541Sshin
2478181803Sbz	icmp6_errcount(&V_icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
247962587Sitojun
248053541Sshin	/* if we are not router, we don't send icmp6 redirect */
2481181803Sbz	if (!V_ip6_forwarding)
248253541Sshin		goto fail;
248353541Sshin
248453541Sshin	/* sanity check */
248553541Sshin	if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
248653541Sshin		goto fail;
248753541Sshin
248853541Sshin	/*
248953541Sshin	 * Address check:
249053541Sshin	 *  the source address must identify a neighbor, and
249153541Sshin	 *  the destination address must not be a multicast address
249253541Sshin	 *  [RFC 2461, sec 8.2]
249353541Sshin	 */
249453541Sshin	sip6 = mtod(m0, struct ip6_hdr *);
249562587Sitojun	bzero(&src_sa, sizeof(src_sa));
249662587Sitojun	src_sa.sin6_family = AF_INET6;
249762587Sitojun	src_sa.sin6_len = sizeof(src_sa);
249862587Sitojun	src_sa.sin6_addr = sip6->ip6_src;
249962587Sitojun	if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
250053541Sshin		goto fail;
250153541Sshin	if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
250253541Sshin		goto fail;	/* what should we do here? */
250353541Sshin
250453541Sshin	/* rate limit */
250553541Sshin	if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
250653541Sshin		goto fail;
250753541Sshin
250853541Sshin	/*
250953541Sshin	 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
251053541Sshin	 * we almost always ask for an mbuf cluster for simplicity.
251153541Sshin	 * (MHLEN < IPV6_MMTU is almost always true)
251253541Sshin	 */
251362587Sitojun#if IPV6_MMTU >= MCLBYTES
251462587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES
251562587Sitojun#endif
2516111119Simp	MGETHDR(m, M_DONTWAIT, MT_HEADER);
251762587Sitojun	if (m && IPV6_MMTU >= MHLEN)
2518111119Simp		MCLGET(m, M_DONTWAIT);
251953541Sshin	if (!m)
252053541Sshin		goto fail;
252178064Sume	m->m_pkthdr.rcvif = NULL;
252278064Sume	m->m_len = 0;
252378064Sume	maxlen = M_TRAILINGSPACE(m);
252453541Sshin	maxlen = min(IPV6_MMTU, maxlen);
252553541Sshin	/* just for safety */
252662587Sitojun	if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
252762587Sitojun	    ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
252853541Sshin		goto fail;
252962587Sitojun	}
253053541Sshin
253153541Sshin	{
253253541Sshin		/* get ip6 linklocal address for ifp(my outgoing interface). */
253362587Sitojun		struct in6_ifaddr *ia;
253462587Sitojun		if ((ia = in6ifa_ifpforlinklocal(ifp,
253562587Sitojun						 IN6_IFF_NOTREADY|
253662587Sitojun						 IN6_IFF_ANYCAST)) == NULL)
253753541Sshin			goto fail;
253853541Sshin		ifp_ll6 = &ia->ia_addr.sin6_addr;
2539194760Srwatson		/* XXXRW: reference released prematurely. */
2540194760Srwatson		ifa_free(&ia->ia_ifa);
254153541Sshin	}
254253541Sshin
254353541Sshin	/* get ip6 linklocal address for the router. */
254453541Sshin	if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
254553541Sshin		struct sockaddr_in6 *sin6;
254653541Sshin		sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
254753541Sshin		router_ll6 = &sin6->sin6_addr;
254853541Sshin		if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
254953541Sshin			router_ll6 = (struct in6_addr *)NULL;
255053541Sshin	} else
255153541Sshin		router_ll6 = (struct in6_addr *)NULL;
255253541Sshin
255353541Sshin	/* ip6 */
255453541Sshin	ip6 = mtod(m, struct ip6_hdr *);
255553541Sshin	ip6->ip6_flow = 0;
255662587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
255762587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
255853541Sshin	/* ip6->ip6_plen will be set later */
255953541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
256053541Sshin	ip6->ip6_hlim = 255;
256153541Sshin	/* ip6->ip6_src must be linklocal addr for my outgoing if. */
256253541Sshin	bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
256353541Sshin	bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
256453541Sshin
256553541Sshin	/* ND Redirect */
256653541Sshin	nd_rd = (struct nd_redirect *)(ip6 + 1);
256753541Sshin	nd_rd->nd_rd_type = ND_REDIRECT;
256853541Sshin	nd_rd->nd_rd_code = 0;
256953541Sshin	nd_rd->nd_rd_reserved = 0;
257053541Sshin	if (rt->rt_flags & RTF_GATEWAY) {
257153541Sshin		/*
257253541Sshin		 * nd_rd->nd_rd_target must be a link-local address in
257353541Sshin		 * better router cases.
257453541Sshin		 */
257553541Sshin		if (!router_ll6)
257653541Sshin			goto fail;
257753541Sshin		bcopy(router_ll6, &nd_rd->nd_rd_target,
2578120892Sume		    sizeof(nd_rd->nd_rd_target));
257953541Sshin		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2580120892Sume		    sizeof(nd_rd->nd_rd_dst));
258153541Sshin	} else {
258253541Sshin		/* make sure redtgt == reddst */
258353541Sshin		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2584120892Sume		    sizeof(nd_rd->nd_rd_target));
258553541Sshin		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2586120892Sume		    sizeof(nd_rd->nd_rd_dst));
258753541Sshin	}
258853541Sshin
258953541Sshin	p = (u_char *)(nd_rd + 1);
259053541Sshin
259153541Sshin	if (!router_ll6)
259253541Sshin		goto nolladdropt;
259353541Sshin
2594120892Sume	{
2595120892Sume		/* target lladdr option */
2596120892Sume		int len;
2597120892Sume		struct nd_opt_hdr *nd_opt;
2598120892Sume		char *lladdr;
259953541Sshin
2600186119Sqingli		IF_AFDATA_LOCK(ifp);
2601186119Sqingli		ln = nd6_lookup(router_ll6, 0, ifp);
2602186119Sqingli		IF_AFDATA_UNLOCK(ifp);
2603186157Skmacy		if (ln == NULL)
2604120892Sume			goto nolladdropt;
2605186119Sqingli
2606120892Sume		len = sizeof(*nd_opt) + ifp->if_addrlen;
2607120892Sume		len = (len + 7) & ~7;	/* round by 8 */
2608120892Sume		/* safety check */
2609186157Skmacy		if (len + (p - (u_char *)ip6) > maxlen)
2610120892Sume			goto nolladdropt;
2611186119Sqingli
2612186119Sqingli		if (ln->la_flags & LLE_VALID) {
2613120892Sume			nd_opt = (struct nd_opt_hdr *)p;
2614120892Sume			nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2615120892Sume			nd_opt->nd_opt_len = len >> 3;
2616120892Sume			lladdr = (char *)(nd_opt + 1);
2617186119Sqingli			bcopy(&ln->ll_addr, lladdr, ifp->if_addrlen);
2618120892Sume			p += len;
2619120892Sume		}
2620186157Skmacy	}
2621186157Skmacynolladdropt:
2622186157Skmacy	if (ln != NULL)
2623186119Sqingli		LLE_RUNLOCK(ln);
2624186157Skmacy
262553541Sshin	m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
262653541Sshin
262753541Sshin	/* just to be safe */
262862587Sitojun#ifdef M_DECRYPTED	/*not openbsd*/
262953541Sshin	if (m0->m_flags & M_DECRYPTED)
263053541Sshin		goto noredhdropt;
263162587Sitojun#endif
263262587Sitojun	if (p - (u_char *)ip6 > maxlen)
263362587Sitojun		goto noredhdropt;
263453541Sshin
2635120891Sume	{
2636120891Sume		/* redirected header option */
2637120891Sume		int len;
2638120891Sume		struct nd_opt_rd_hdr *nd_opt_rh;
263953541Sshin
2640120891Sume		/*
2641120891Sume		 * compute the maximum size for icmp6 redirect header option.
2642120891Sume		 * XXX room for auth header?
2643120891Sume		 */
2644120891Sume		len = maxlen - (p - (u_char *)ip6);
2645120891Sume		len &= ~7;
264653541Sshin
2647120891Sume		/* This is just for simplicity. */
2648120891Sume		if (m0->m_pkthdr.len != m0->m_len) {
2649120891Sume			if (m0->m_next) {
2650120891Sume				m_freem(m0->m_next);
2651120891Sume				m0->m_next = NULL;
2652120891Sume			}
2653120891Sume			m0->m_pkthdr.len = m0->m_len;
265453541Sshin		}
265553541Sshin
2656120891Sume		/*
2657120891Sume		 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2658120891Sume		 * about padding/truncate rule for the original IP packet.
2659120891Sume		 * From the discussion on IPv6imp in Feb 1999,
2660120891Sume		 * the consensus was:
2661120891Sume		 * - "attach as much as possible" is the goal
2662120891Sume		 * - pad if not aligned (original size can be guessed by
2663120891Sume		 *   original ip6 header)
2664120891Sume		 * Following code adds the padding if it is simple enough,
2665120891Sume		 * and truncates if not.
2666120891Sume		 */
2667120891Sume		if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2668120891Sume			panic("assumption failed in %s:%d", __FILE__,
2669120891Sume			    __LINE__);
267053541Sshin
2671120891Sume		if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2672120891Sume			/* not enough room, truncate */
2673120891Sume			m0->m_pkthdr.len = m0->m_len = len -
2674120891Sume			    sizeof(*nd_opt_rh);
2675120891Sume		} else {
2676120891Sume			/* enough room, pad or truncate */
2677120891Sume			size_t extra;
267853541Sshin
2679120891Sume			extra = m0->m_pkthdr.len % 8;
2680120891Sume			if (extra) {
2681120891Sume				/* pad if easy enough, truncate if not */
2682120891Sume				if (8 - extra <= M_TRAILINGSPACE(m0)) {
2683120891Sume					/* pad */
2684120891Sume					m0->m_len += (8 - extra);
2685120891Sume					m0->m_pkthdr.len += (8 - extra);
2686120891Sume				} else {
2687120891Sume					/* truncate */
2688120891Sume					m0->m_pkthdr.len -= extra;
2689120891Sume					m0->m_len -= extra;
2690120891Sume				}
269153541Sshin			}
2692120891Sume			len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2693120891Sume			m0->m_pkthdr.len = m0->m_len = len -
2694120891Sume			    sizeof(*nd_opt_rh);
269553541Sshin		}
269653541Sshin
2697120891Sume		nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2698120891Sume		bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2699120891Sume		nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2700120891Sume		nd_opt_rh->nd_opt_rh_len = len >> 3;
2701120891Sume		p += sizeof(*nd_opt_rh);
2702120891Sume		m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
270353541Sshin
2704120891Sume		/* connect m0 to m */
2705120891Sume		m_tag_delete_chain(m0, NULL);
2706120891Sume		m0->m_flags &= ~M_PKTHDR;
2707120891Sume		m->m_next = m0;
2708120891Sume		m->m_pkthdr.len = m->m_len + m0->m_len;
2709120891Sume		m0 = NULL;
2710120891Sume	}
271153541Sshinnoredhdropt:;
2712112781Ssuz	if (m0) {
2713112781Ssuz		m_freem(m0);
2714112781Ssuz		m0 = NULL;
2715112781Ssuz	}
271653541Sshin
2717121315Sume	/* XXX: clear embedded link IDs in the inner header */
2718121315Sume	in6_clearscope(&sip6->ip6_src);
2719121315Sume	in6_clearscope(&sip6->ip6_dst);
2720121315Sume	in6_clearscope(&nd_rd->nd_rd_target);
2721121315Sume	in6_clearscope(&nd_rd->nd_rd_dst);
272253541Sshin
272353541Sshin	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
272453541Sshin
272553541Sshin	nd_rd->nd_rd_cksum = 0;
2726120891Sume	nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2727120891Sume	    sizeof(*ip6), ntohs(ip6->ip6_plen));
272853541Sshin
272953541Sshin	/* send the packet to outside... */
2730105194Ssam	ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
273153541Sshin	if (outif) {
273253541Sshin		icmp6_ifstat_inc(outif, ifs6_out_msg);
273353541Sshin		icmp6_ifstat_inc(outif, ifs6_out_redirect);
273453541Sshin	}
2735190964Srwatson	ICMP6STAT_INC(icp6s_outhist[ND_REDIRECT]);
273653541Sshin
273753541Sshin	return;
273853541Sshin
273953541Sshinfail:
274053541Sshin	if (m)
274153541Sshin		m_freem(m);
274253541Sshin	if (m0)
274353541Sshin		m_freem(m0);
274453541Sshin}
274553541Sshin
274653541Sshin/*
274753541Sshin * ICMPv6 socket option processing.
274853541Sshin */
274953541Sshinint
2750171259Sdelphijicmp6_ctloutput(struct socket *so, struct sockopt *sopt)
275153541Sshin{
275253541Sshin	int error = 0;
275353541Sshin	int optlen;
275478064Sume	struct inpcb *inp = sotoinpcb(so);
275553541Sshin	int level, op, optname;
275653541Sshin
275753541Sshin	if (sopt) {
275853541Sshin		level = sopt->sopt_level;
275953541Sshin		op = sopt->sopt_dir;
276053541Sshin		optname = sopt->sopt_name;
276153541Sshin		optlen = sopt->sopt_valsize;
276253541Sshin	} else
276353541Sshin		level = op = optname = optlen = 0;
276478064Sume
276553541Sshin	if (level != IPPROTO_ICMPV6) {
276653541Sshin		return EINVAL;
276753541Sshin	}
276853541Sshin
276978064Sume	switch (op) {
277053541Sshin	case PRCO_SETOPT:
277153541Sshin		switch (optname) {
277253541Sshin		case ICMP6_FILTER:
277353541Sshin		    {
2774180968Srwatson			struct icmp6_filter ic6f;
277553541Sshin
2776180968Srwatson			if (optlen != sizeof(ic6f)) {
277753541Sshin				error = EMSGSIZE;
277853541Sshin				break;
277953541Sshin			}
2780180968Srwatson			error = sooptcopyin(sopt, &ic6f, optlen, optlen);
2781180968Srwatson			if (error == 0) {
2782180968Srwatson				INP_WLOCK(inp);
2783180968Srwatson				*inp->in6p_icmp6filt = ic6f;
2784180968Srwatson				INP_WUNLOCK(inp);
2785180968Srwatson			}
278653541Sshin			break;
278753541Sshin		    }
278853541Sshin
278953541Sshin		default:
279053541Sshin			error = ENOPROTOOPT;
279153541Sshin			break;
279253541Sshin		}
279353541Sshin		break;
279453541Sshin
279553541Sshin	case PRCO_GETOPT:
279653541Sshin		switch (optname) {
279753541Sshin		case ICMP6_FILTER:
279853541Sshin		    {
2799180968Srwatson			struct icmp6_filter ic6f;
2800180968Srwatson
2801180968Srwatson			INP_RLOCK(inp);
2802180968Srwatson			ic6f = *inp->in6p_icmp6filt;
2803180968Srwatson			INP_RUNLOCK(inp);
2804180968Srwatson			error = sooptcopyout(sopt, &ic6f, sizeof(ic6f));
280553541Sshin			break;
280653541Sshin		    }
280753541Sshin
280853541Sshin		default:
280953541Sshin			error = ENOPROTOOPT;
281053541Sshin			break;
281153541Sshin		}
281253541Sshin		break;
281353541Sshin	}
281453541Sshin
2815120856Sume	return (error);
281653541Sshin}
281753541Sshin
281853541Sshin/*
281953541Sshin * Perform rate limit check.
282053541Sshin * Returns 0 if it is okay to send the icmp6 packet.
282153541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
282253541Sshin * limitation.
282353541Sshin *
282453541Sshin * XXX per-destination/type check necessary?
2825171259Sdelphij *
2826171259Sdelphij * dst - not used at this moment
2827171259Sdelphij * type - not used at this moment
2828171259Sdelphij * code - not used at this moment
282953541Sshin */
283053541Sshinstatic int
2831171259Sdelphijicmp6_ratelimit(const struct in6_addr *dst, const int type,
2832171259Sdelphij    const int code)
283353541Sshin{
283462587Sitojun	int ret;
283553541Sshin
283695023Ssuz	ret = 0;	/* okay to send */
283753541Sshin
283862587Sitojun	/* PPS limit */
2839181803Sbz	if (!ppsratecheck(&V_icmp6errppslim_last, &V_icmp6errpps_count,
2840181803Sbz	    V_icmp6errppslim)) {
284153541Sshin		/* The packet is subject to rate limit */
284262587Sitojun		ret++;
284353541Sshin	}
284453541Sshin
284562587Sitojun	return ret;
284653541Sshin}
2847