icmp6.c revision 112781
162587Sitojun/*	$FreeBSD: head/sys/netinet6/icmp6.c 112781 2003-03-29 08:31:28Z suz $	*/
278064Sume/*	$KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $	*/
362587Sitojun
453541Sshin/*
553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
653541Sshin * All rights reserved.
753541Sshin *
853541Sshin * Redistribution and use in source and binary forms, with or without
953541Sshin * modification, are permitted provided that the following conditions
1053541Sshin * are met:
1153541Sshin * 1. Redistributions of source code must retain the above copyright
1253541Sshin *    notice, this list of conditions and the following disclaimer.
1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1453541Sshin *    notice, this list of conditions and the following disclaimer in the
1553541Sshin *    documentation and/or other materials provided with the distribution.
1653541Sshin * 3. Neither the name of the project nor the names of its contributors
1753541Sshin *    may be used to endorse or promote products derived from this software
1853541Sshin *    without specific prior written permission.
1953541Sshin *
2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2353541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3053541Sshin * SUCH DAMAGE.
3153541Sshin */
3253541Sshin
3353541Sshin/*
3453541Sshin * Copyright (c) 1982, 1986, 1988, 1993
3553541Sshin *	The Regents of the University of California.  All rights reserved.
3653541Sshin *
3753541Sshin * Redistribution and use in source and binary forms, with or without
3853541Sshin * modification, are permitted provided that the following conditions
3953541Sshin * are met:
4053541Sshin * 1. Redistributions of source code must retain the above copyright
4153541Sshin *    notice, this list of conditions and the following disclaimer.
4253541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4353541Sshin *    notice, this list of conditions and the following disclaimer in the
4453541Sshin *    documentation and/or other materials provided with the distribution.
4553541Sshin * 3. All advertising materials mentioning features or use of this software
4653541Sshin *    must display the following acknowledgement:
4753541Sshin *	This product includes software developed by the University of
4853541Sshin *	California, Berkeley and its contributors.
4953541Sshin * 4. Neither the name of the University nor the names of its contributors
5053541Sshin *    may be used to endorse or promote products derived from this software
5153541Sshin *    without specific prior written permission.
5253541Sshin *
5353541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5453541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5553541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5653541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5753541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5853541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5953541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6053541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6153541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6253541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6353541Sshin * SUCH DAMAGE.
6453541Sshin *
6553541Sshin *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
6653541Sshin */
6753541Sshin
6862587Sitojun#include "opt_inet.h"
6962587Sitojun#include "opt_inet6.h"
7055009Sshin#include "opt_ipsec.h"
7153541Sshin
7253541Sshin#include <sys/param.h>
7395759Stanimura#include <sys/domain.h>
7495759Stanimura#include <sys/kernel.h>
7595759Stanimura#include <sys/lock.h>
7678064Sume#include <sys/malloc.h>
7753541Sshin#include <sys/mbuf.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>
8953541Sshin#include <net/if_types.h>
9095759Stanimura#include <net/route.h>
9153541Sshin
9253541Sshin#include <netinet/in.h>
9395759Stanimura#include <netinet/in_pcb.h>
9453541Sshin#include <netinet/in_var.h>
9562587Sitojun#include <netinet/ip6.h>
9695759Stanimura#include <netinet/icmp6.h>
9795759Stanimura#include <netinet6/in6_ifattach.h>
9895759Stanimura#include <netinet6/in6_pcb.h>
9995759Stanimura#include <netinet6/ip6protosw.h>
10053541Sshin#include <netinet6/ip6_var.h>
10153541Sshin#include <netinet6/mld6_var.h>
10253541Sshin#include <netinet6/nd6.h>
10353541Sshin
10453541Sshin#ifdef IPSEC
10553541Sshin#include <netinet6/ipsec.h>
10653541Sshin#include <netkey/key.h>
10755009Sshin#endif
10853541Sshin
109105199Ssam#ifdef FAST_IPSEC
110105199Ssam#include <netipsec/ipsec.h>
111105199Ssam#include <netipsec/key.h>
112105199Ssam#define	IPSEC
113105199Ssam#endif
114105199Ssam
11553541Sshin#include <net/net_osdep.h>
11653541Sshin
11778064Sume#ifdef HAVE_NRL_INPCB
11878064Sume/* inpcb members */
11978064Sume#define in6pcb		inpcb
12078064Sume#define in6p_laddr	inp_laddr6
12178064Sume#define in6p_faddr	inp_faddr6
12278064Sume#define in6p_icmp6filt	inp_icmp6filt
12378064Sume#define in6p_route	inp_route
12478064Sume#define in6p_socket	inp_socket
12578064Sume#define in6p_flags	inp_flags
12678064Sume#define in6p_moptions	inp_moptions6
12778064Sume#define in6p_outputopts	inp_outputopts6
12878064Sume#define in6p_ip6	inp_ipv6
12978064Sume#define in6p_flowinfo	inp_flowinfo
13078064Sume#define in6p_sp		inp_sp
13178064Sume#define in6p_next	inp_next
13278064Sume#define in6p_prev	inp_prev
13378064Sume/* macro names */
13478064Sume#define sotoin6pcb	sotoinpcb
13578064Sume/* function names */
13678064Sume#define in6_pcbdetach	in_pcbdetach
13778064Sume#define in6_rtchange	in_rtchange
13878064Sume
13978064Sume/*
14078064Sume * for KAME src sync over BSD*'s. XXX: FreeBSD (>=3) are VERY different from
14178064Sume * others...
14278064Sume */
14378064Sume#define in6p_ip6_nxt	inp_ipv6.ip6_nxt
14478064Sume#endif
14578064Sume
14662587Sitojunextern struct domain inet6domain;
14753541Sshin
14862587Sitojunstruct icmp6stat icmp6stat;
14953541Sshin
15062587Sitojunextern struct inpcbhead ripcb;
15162587Sitojunextern int icmp6errppslim;
15262587Sitojunstatic int icmp6errpps_count = 0;
15378064Sumestatic struct timeval icmp6errppslim_last;
15462587Sitojunextern int icmp6_nodeinfo;
15553541Sshin
15662587Sitojunstatic void icmp6_errcount __P((struct icmp6errstat *, int, int));
15762587Sitojunstatic int icmp6_rip6_input __P((struct mbuf **, int));
15862587Sitojunstatic int icmp6_ratelimit __P((const struct in6_addr *, const int, const int));
15962587Sitojunstatic const char *icmp6_redirect_diag __P((struct in6_addr *,
16062587Sitojun	struct in6_addr *, struct in6_addr *));
161108143Ssam#define	HAVE_PPSRATECHECK
16278064Sume#ifndef HAVE_PPSRATECHECK
16378064Sumestatic int ppsratecheck __P((struct timeval *, int *, int));
16462587Sitojun#endif
16562587Sitojunstatic struct mbuf *ni6_input __P((struct mbuf *, int));
16662587Sitojunstatic struct mbuf *ni6_nametodns __P((const char *, int, int));
16762587Sitojunstatic int ni6_dnsmatch __P((const char *, int, const char *, int));
16862587Sitojunstatic int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
16978064Sume			  struct ifnet **, char *));
17062587Sitojunstatic int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
17162587Sitojun				struct ifnet *, int));
17278064Sumestatic int icmp6_notify_error __P((struct mbuf *, int, int, int));
17353541Sshin
17453541Sshin#ifdef COMPAT_RFC1885
17562587Sitojunstatic struct route_in6 icmp6_reflect_rt;
17653541Sshin#endif
17753541Sshin
17878064Sume
17953541Sshinvoid
18053541Sshinicmp6_init()
18153541Sshin{
18253541Sshin	mld6_init();
18353541Sshin}
18453541Sshin
18562587Sitojunstatic void
18662587Sitojunicmp6_errcount(stat, type, code)
18762587Sitojun	struct icmp6errstat *stat;
18862587Sitojun	int type, code;
18962587Sitojun{
19078064Sume	switch (type) {
19162587Sitojun	case ICMP6_DST_UNREACH:
19262587Sitojun		switch (code) {
19362587Sitojun		case ICMP6_DST_UNREACH_NOROUTE:
19462587Sitojun			stat->icp6errs_dst_unreach_noroute++;
19562587Sitojun			return;
19662587Sitojun		case ICMP6_DST_UNREACH_ADMIN:
19762587Sitojun			stat->icp6errs_dst_unreach_admin++;
19862587Sitojun			return;
19962587Sitojun		case ICMP6_DST_UNREACH_BEYONDSCOPE:
20062587Sitojun			stat->icp6errs_dst_unreach_beyondscope++;
20162587Sitojun			return;
20262587Sitojun		case ICMP6_DST_UNREACH_ADDR:
20362587Sitojun			stat->icp6errs_dst_unreach_addr++;
20462587Sitojun			return;
20562587Sitojun		case ICMP6_DST_UNREACH_NOPORT:
20662587Sitojun			stat->icp6errs_dst_unreach_noport++;
20762587Sitojun			return;
20862587Sitojun		}
20962587Sitojun		break;
21062587Sitojun	case ICMP6_PACKET_TOO_BIG:
21162587Sitojun		stat->icp6errs_packet_too_big++;
21262587Sitojun		return;
21362587Sitojun	case ICMP6_TIME_EXCEEDED:
21478064Sume		switch (code) {
21562587Sitojun		case ICMP6_TIME_EXCEED_TRANSIT:
21662587Sitojun			stat->icp6errs_time_exceed_transit++;
21762587Sitojun			return;
21862587Sitojun		case ICMP6_TIME_EXCEED_REASSEMBLY:
21962587Sitojun			stat->icp6errs_time_exceed_reassembly++;
22062587Sitojun			return;
22162587Sitojun		}
22262587Sitojun		break;
22362587Sitojun	case ICMP6_PARAM_PROB:
22478064Sume		switch (code) {
22562587Sitojun		case ICMP6_PARAMPROB_HEADER:
22662587Sitojun			stat->icp6errs_paramprob_header++;
22762587Sitojun			return;
22862587Sitojun		case ICMP6_PARAMPROB_NEXTHEADER:
22962587Sitojun			stat->icp6errs_paramprob_nextheader++;
23062587Sitojun			return;
23162587Sitojun		case ICMP6_PARAMPROB_OPTION:
23262587Sitojun			stat->icp6errs_paramprob_option++;
23362587Sitojun			return;
23462587Sitojun		}
23562587Sitojun		break;
23662587Sitojun	case ND_REDIRECT:
23762587Sitojun		stat->icp6errs_redirect++;
23862587Sitojun		return;
23962587Sitojun	}
24062587Sitojun	stat->icp6errs_unknown++;
24162587Sitojun}
24262587Sitojun
24353541Sshin/*
24453541Sshin * Generate an error packet of type error in response to bad IP6 packet.
24553541Sshin */
24653541Sshinvoid
24753541Sshinicmp6_error(m, type, code, param)
24853541Sshin	struct mbuf *m;
24953541Sshin	int type, code, param;
25053541Sshin{
25153541Sshin	struct ip6_hdr *oip6, *nip6;
25253541Sshin	struct icmp6_hdr *icmp6;
25362587Sitojun	u_int preplen;
25453541Sshin	int off;
25562587Sitojun	int nxt;
25653541Sshin
25753541Sshin	icmp6stat.icp6s_error++;
25853541Sshin
25962587Sitojun	/* count per-type-code statistics */
26062587Sitojun	icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
26162587Sitojun
26262587Sitojun#ifdef M_DECRYPTED	/*not openbsd*/
26362587Sitojun	if (m->m_flags & M_DECRYPTED) {
26462587Sitojun		icmp6stat.icp6s_canterror++;
26553541Sshin		goto freeit;
26662587Sitojun	}
26762587Sitojun#endif
26853541Sshin
26962587Sitojun#ifndef PULLDOWN_TEST
27062587Sitojun	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
27162587Sitojun#else
27262587Sitojun	if (m->m_len < sizeof(struct ip6_hdr)) {
27362587Sitojun		m = m_pullup(m, sizeof(struct ip6_hdr));
27462587Sitojun		if (m == NULL)
27562587Sitojun			return;
27662587Sitojun	}
27762587Sitojun#endif
27853541Sshin	oip6 = mtod(m, struct ip6_hdr *);
27953541Sshin
28053541Sshin	/*
28153541Sshin	 * Multicast destination check. For unrecognized option errors,
28253541Sshin	 * this check has already done in ip6_unknown_opt(), so we can
28353541Sshin	 * check only for other errors.
28453541Sshin	 */
28553541Sshin	if ((m->m_flags & (M_BCAST|M_MCAST) ||
28653541Sshin	     IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
28753541Sshin	    (type != ICMP6_PACKET_TOO_BIG &&
28853541Sshin	     (type != ICMP6_PARAM_PROB ||
28953541Sshin	      code != ICMP6_PARAMPROB_OPTION)))
29053541Sshin		goto freeit;
29153541Sshin
29253541Sshin	/* Source address check. XXX: the case of anycast source? */
29353541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
29453541Sshin	    IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
29553541Sshin		goto freeit;
29653541Sshin
29753541Sshin	/*
29862587Sitojun	 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
29962587Sitojun	 * don't do it.
30053541Sshin	 */
30162587Sitojun	nxt = -1;
30262587Sitojun	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
30362587Sitojun	if (off >= 0 && nxt == IPPROTO_ICMPV6) {
30453541Sshin		struct icmp6_hdr *icp;
30553541Sshin
30662587Sitojun#ifndef PULLDOWN_TEST
30762587Sitojun		IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
30862587Sitojun		icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
30962587Sitojun#else
31062587Sitojun		IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
31162587Sitojun			sizeof(*icp));
31262587Sitojun		if (icp == NULL) {
31362587Sitojun			icmp6stat.icp6s_tooshort++;
31462587Sitojun			return;
31562587Sitojun		}
31662587Sitojun#endif
31762587Sitojun		if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
31862587Sitojun		    icp->icmp6_type == ND_REDIRECT) {
31953541Sshin			/*
32062587Sitojun			 * ICMPv6 error
32162587Sitojun			 * Special case: for redirect (which is
32262587Sitojun			 * informational) we must not send icmp6 error.
32353541Sshin			 */
32462587Sitojun			icmp6stat.icp6s_canterror++;
32562587Sitojun			goto freeit;
32662587Sitojun		} else {
32762587Sitojun			/* ICMPv6 informational - send the error */
32853541Sshin		}
32962587Sitojun	} else {
33062587Sitojun		/* non-ICMPv6 - send the error */
33153541Sshin	}
33253541Sshin
33353541Sshin	oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
33453541Sshin
33553541Sshin	/* Finally, do rate limitation check. */
33653541Sshin	if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
33753541Sshin		icmp6stat.icp6s_toofreq++;
33853541Sshin		goto freeit;
33953541Sshin	}
34053541Sshin
34153541Sshin	/*
34253541Sshin	 * OK, ICMP6 can be generated.
34353541Sshin	 */
34453541Sshin
34553541Sshin	if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
34653541Sshin		m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
34753541Sshin
34862587Sitojun	preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
349111119Simp	M_PREPEND(m, preplen, M_DONTWAIT);
35062587Sitojun	if (m && m->m_len < preplen)
35162587Sitojun		m = m_pullup(m, preplen);
35253541Sshin	if (m == NULL) {
35378064Sume		nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
35453541Sshin		return;
35553541Sshin	}
35653541Sshin
35753541Sshin	nip6 = mtod(m, struct ip6_hdr *);
35853541Sshin	nip6->ip6_src  = oip6->ip6_src;
35953541Sshin	nip6->ip6_dst  = oip6->ip6_dst;
36053541Sshin
36153541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
36253541Sshin		oip6->ip6_src.s6_addr16[1] = 0;
36353541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
36453541Sshin		oip6->ip6_dst.s6_addr16[1] = 0;
36553541Sshin
36653541Sshin	icmp6 = (struct icmp6_hdr *)(nip6 + 1);
36753541Sshin	icmp6->icmp6_type = type;
36853541Sshin	icmp6->icmp6_code = code;
36953541Sshin	icmp6->icmp6_pptr = htonl((u_int32_t)param);
37053541Sshin
37178064Sume	/*
37278064Sume	 * icmp6_reflect() is designed to be in the input path.
37378064Sume	 * icmp6_error() can be called from both input and outut path,
37478064Sume	 * and if we are in output path rcvif could contain bogus value.
37578064Sume	 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
37678064Sume	 * information in ip header (nip6).
37778064Sume	 */
37878064Sume	m->m_pkthdr.rcvif = NULL;
37978064Sume
38053541Sshin	icmp6stat.icp6s_outhist[type]++;
38195023Ssuz	icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
38262587Sitojun
38362587Sitojun	return;
38462587Sitojun
38562587Sitojun  freeit:
38662587Sitojun	/*
38762587Sitojun	 * If we can't tell wheter or not we can generate ICMP6, free it.
38862587Sitojun	 */
38962587Sitojun	m_freem(m);
39053541Sshin}
39153541Sshin
39253541Sshin/*
39353541Sshin * Process a received ICMP6 message.
39453541Sshin */
39553541Sshinint
39653541Sshinicmp6_input(mp, offp, proto)
39753541Sshin	struct mbuf **mp;
39853541Sshin	int *offp, proto;
39953541Sshin{
40053541Sshin	struct mbuf *m = *mp, *n;
40153541Sshin	struct ip6_hdr *ip6, *nip6;
40253541Sshin	struct icmp6_hdr *icmp6, *nicmp6;
40353541Sshin	int off = *offp;
40453541Sshin	int icmp6len = m->m_pkthdr.len - *offp;
40553541Sshin	int code, sum, noff;
40653541Sshin
40762587Sitojun#ifndef PULLDOWN_TEST
40853541Sshin	IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
40995023Ssuz	/* m might change if M_LOOP.  So, call mtod after this */
41062587Sitojun#endif
41153541Sshin
41253541Sshin	/*
41353541Sshin	 * Locate icmp6 structure in mbuf, and check
41453541Sshin	 * that not corrupted and of at least minimum length
41553541Sshin	 */
41653541Sshin
41753541Sshin	ip6 = mtod(m, struct ip6_hdr *);
41853541Sshin	if (icmp6len < sizeof(struct icmp6_hdr)) {
41953541Sshin		icmp6stat.icp6s_tooshort++;
42053541Sshin		goto freeit;
42153541Sshin	}
42253541Sshin
42353541Sshin	/*
42453541Sshin	 * calculate the checksum
42553541Sshin	 */
42662587Sitojun#ifndef PULLDOWN_TEST
42753541Sshin	icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
42862587Sitojun#else
42962587Sitojun	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
43062587Sitojun	if (icmp6 == NULL) {
43162587Sitojun		icmp6stat.icp6s_tooshort++;
43262587Sitojun		return IPPROTO_DONE;
43362587Sitojun	}
43462587Sitojun#endif
43553541Sshin	code = icmp6->icmp6_code;
43653541Sshin
43753541Sshin	if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
43878064Sume		nd6log((LOG_ERR,
43953541Sshin		    "ICMP6 checksum error(%d|%x) %s\n",
44078064Sume		    icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
44153541Sshin		icmp6stat.icp6s_checksum++;
44253541Sshin		goto freeit;
44353541Sshin	}
44453541Sshin
44583934Sbrooks	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
44653541Sshin		/*
44753541Sshin		 * Deliver very specific ICMP6 type only.
44853541Sshin		 * This is important to deilver TOOBIG.  Otherwise PMTUD
44953541Sshin		 * will not work.
45053541Sshin		 */
45153541Sshin		switch (icmp6->icmp6_type) {
45253541Sshin		case ICMP6_DST_UNREACH:
45353541Sshin		case ICMP6_PACKET_TOO_BIG:
45453541Sshin		case ICMP6_TIME_EXCEEDED:
45553541Sshin			break;
45653541Sshin		default:
45753541Sshin			goto freeit;
45853541Sshin		}
45953541Sshin	}
46053541Sshin
46153541Sshin	icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
46253541Sshin	icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
46353541Sshin	if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
46453541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
46553541Sshin
46653541Sshin	switch (icmp6->icmp6_type) {
46753541Sshin	case ICMP6_DST_UNREACH:
46853541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
46953541Sshin		switch (code) {
47053541Sshin		case ICMP6_DST_UNREACH_NOROUTE:
47153541Sshin			code = PRC_UNREACH_NET;
47253541Sshin			break;
47353541Sshin		case ICMP6_DST_UNREACH_ADMIN:
47453541Sshin			icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
47562587Sitojun			code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
47662587Sitojun			break;
47753541Sshin		case ICMP6_DST_UNREACH_ADDR:
47862587Sitojun			code = PRC_HOSTDEAD;
47953541Sshin			break;
48062587Sitojun#ifdef COMPAT_RFC1885
48153541Sshin		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
48253541Sshin			code = PRC_UNREACH_SRCFAIL;
48353541Sshin			break;
48462587Sitojun#else
48562587Sitojun		case ICMP6_DST_UNREACH_BEYONDSCOPE:
48662587Sitojun			/* I mean "source address was incorrect." */
48762587Sitojun			code = PRC_PARAMPROB;
48862587Sitojun			break;
48962587Sitojun#endif
49053541Sshin		case ICMP6_DST_UNREACH_NOPORT:
49153541Sshin			code = PRC_UNREACH_PORT;
49253541Sshin			break;
49353541Sshin		default:
49453541Sshin			goto badcode;
49553541Sshin		}
49653541Sshin		goto deliver;
49753541Sshin		break;
49853541Sshin
49953541Sshin	case ICMP6_PACKET_TOO_BIG:
50053541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
50153541Sshin		if (code != 0)
50253541Sshin			goto badcode;
50353541Sshin
50453541Sshin		code = PRC_MSGSIZE;
50553541Sshin
50662587Sitojun		/*
50762587Sitojun		 * Updating the path MTU will be done after examining
50862587Sitojun		 * intermediate extension headers.
50962587Sitojun		 */
51053541Sshin		goto deliver;
51153541Sshin		break;
51253541Sshin
51353541Sshin	case ICMP6_TIME_EXCEEDED:
51453541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
51553541Sshin		switch (code) {
51653541Sshin		case ICMP6_TIME_EXCEED_TRANSIT:
51753541Sshin		case ICMP6_TIME_EXCEED_REASSEMBLY:
51853541Sshin			code += PRC_TIMXCEED_INTRANS;
51953541Sshin			break;
52053541Sshin		default:
52153541Sshin			goto badcode;
52253541Sshin		}
52353541Sshin		goto deliver;
52453541Sshin		break;
52553541Sshin
52653541Sshin	case ICMP6_PARAM_PROB:
52753541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
52853541Sshin		switch (code) {
52953541Sshin		case ICMP6_PARAMPROB_NEXTHEADER:
53053541Sshin			code = PRC_UNREACH_PROTOCOL;
53153541Sshin			break;
53253541Sshin		case ICMP6_PARAMPROB_HEADER:
53353541Sshin		case ICMP6_PARAMPROB_OPTION:
53453541Sshin			code = PRC_PARAMPROB;
53553541Sshin			break;
53653541Sshin		default:
53753541Sshin			goto badcode;
53853541Sshin		}
53953541Sshin		goto deliver;
54053541Sshin		break;
54153541Sshin
54253541Sshin	case ICMP6_ECHO_REQUEST:
54353541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
54453541Sshin		if (code != 0)
54553541Sshin			goto badcode;
54653541Sshin		if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
54753541Sshin			/* Give up remote */
54853541Sshin			break;
54953541Sshin		}
55062587Sitojun		if ((n->m_flags & M_EXT) != 0
55162587Sitojun		 || n->m_len < off + sizeof(struct icmp6_hdr)) {
55253541Sshin			struct mbuf *n0 = n;
55362587Sitojun			const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
554108741Ssam			int n0len;
55553541Sshin
55653541Sshin			/*
55753541Sshin			 * Prepare an internal mbuf. m_pullup() doesn't
55853541Sshin			 * always copy the length we specified.
55953541Sshin			 */
56062587Sitojun			if (maxlen >= MCLBYTES) {
56162587Sitojun				/* Give up remote */
56262587Sitojun				m_freem(n0);
56362587Sitojun				break;
56462587Sitojun			}
565111119Simp			MGETHDR(n, M_DONTWAIT, n0->m_type);
566111070Ssam			n0len = n0->m_pkthdr.len;	/* save for use below */
567111070Ssam			if (n)
568111070Ssam				M_MOVE_PKTHDR(n, n0);
56962587Sitojun			if (n && maxlen >= MHLEN) {
570111119Simp				MCLGET(n, M_DONTWAIT);
57162587Sitojun				if ((n->m_flags & M_EXT) == 0) {
57262587Sitojun					m_free(n);
57362587Sitojun					n = NULL;
57462587Sitojun				}
57562587Sitojun			}
57653541Sshin			if (n == NULL) {
57753541Sshin				/* Give up remote */
57853541Sshin				m_freem(n0);
57953541Sshin				break;
58053541Sshin			}
58153541Sshin			/*
58253541Sshin			 * Copy IPv6 and ICMPv6 only.
58353541Sshin			 */
58453541Sshin			nip6 = mtod(n, struct ip6_hdr *);
58553541Sshin			bcopy(ip6, nip6, sizeof(struct ip6_hdr));
58653541Sshin			nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
58753541Sshin			bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
58862587Sitojun			noff = sizeof(struct ip6_hdr);
589108741Ssam			/* new mbuf contains only ipv6+icmpv6 headers */
590108741Ssam			n->m_len = noff + sizeof(struct icmp6_hdr);
59153541Sshin			/*
59262587Sitojun			 * Adjust mbuf. ip6_plen will be adjusted in
59362587Sitojun			 * ip6_output().
59453541Sshin			 */
59562587Sitojun			m_adj(n0, off + sizeof(struct icmp6_hdr));
596108741Ssam			/* recalculate complete packet size */
597108741Ssam			n->m_pkthdr.len = n0len + (noff - off);
59862587Sitojun			n->m_next = n0;
59953541Sshin		} else {
60053541Sshin			nip6 = mtod(n, struct ip6_hdr *);
60153541Sshin			nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off);
60253541Sshin			noff = off;
60353541Sshin		}
60453541Sshin		nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
60553541Sshin		nicmp6->icmp6_code = 0;
60653541Sshin		if (n) {
60753541Sshin			icmp6stat.icp6s_reflect++;
60853541Sshin			icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
60953541Sshin			icmp6_reflect(n, noff);
61053541Sshin		}
61153541Sshin		break;
61253541Sshin
61353541Sshin	case ICMP6_ECHO_REPLY:
61453541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
61553541Sshin		if (code != 0)
61653541Sshin			goto badcode;
61753541Sshin		break;
61853541Sshin
61996116Sume	case MLD_LISTENER_QUERY:
62096116Sume	case MLD_LISTENER_REPORT:
62196116Sume		if (icmp6len < sizeof(struct mld_hdr))
62253541Sshin			goto badlen;
62396116Sume		if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
62453541Sshin			icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
62553541Sshin		else
62653541Sshin			icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
627111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
62862587Sitojun			/* give up local */
62962587Sitojun			mld6_input(m, off);
63062587Sitojun			m = NULL;
63162587Sitojun			goto freeit;
63262587Sitojun		}
63362587Sitojun		mld6_input(n, off);
63453541Sshin		/* m stays. */
63553541Sshin		break;
63653541Sshin
63796116Sume	case MLD_LISTENER_DONE:
63853541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
63996116Sume		if (icmp6len < sizeof(struct mld_hdr))	/* necessary? */
64053541Sshin			goto badlen;
64153541Sshin		break;		/* nothing to be done in kernel */
64253541Sshin
64396116Sume	case MLD_MTRACE_RESP:
64496116Sume	case MLD_MTRACE:
64595023Ssuz		/* XXX: these two are experimental.  not officially defind. */
64653541Sshin		/* XXX: per-interface statistics? */
64762587Sitojun		break;		/* just pass it to applications */
64853541Sshin
64953541Sshin	case ICMP6_WRUREQUEST:	/* ICMP6_FQDN_QUERY */
65053541Sshin	    {
65153541Sshin		enum { WRU, FQDN } mode;
65253541Sshin
65362587Sitojun		if (!icmp6_nodeinfo)
65462587Sitojun			break;
65562587Sitojun
65653541Sshin		if (icmp6len == sizeof(struct icmp6_hdr) + 4)
65753541Sshin			mode = WRU;
65862587Sitojun		else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
65953541Sshin			mode = FQDN;
66053541Sshin		else
66153541Sshin			goto badlen;
66253541Sshin
66353541Sshin#define hostnamelen	strlen(hostname)
66453541Sshin		if (mode == FQDN) {
66562587Sitojun#ifndef PULLDOWN_TEST
66653541Sshin			IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
66753541Sshin					 IPPROTO_DONE);
66862587Sitojun#endif
66962587Sitojun			n = m_copy(m, 0, M_COPYALL);
67062587Sitojun			if (n)
67162587Sitojun				n = ni6_input(n, off);
67262587Sitojun			/* XXX meaningless if n == NULL */
67353541Sshin			noff = sizeof(struct ip6_hdr);
67453541Sshin		} else {
67553541Sshin			u_char *p;
67662587Sitojun			int maxlen, maxhlen;
67753541Sshin
67878064Sume			if ((icmp6_nodeinfo & 5) != 5)
67978064Sume				break;
68078064Sume
68162587Sitojun			if (code != 0)
68262587Sitojun				goto badcode;
68362587Sitojun			maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
68462587Sitojun			if (maxlen >= MCLBYTES) {
68562587Sitojun				/* Give up remote */
68662587Sitojun				break;
68762587Sitojun			}
688111119Simp			MGETHDR(n, M_DONTWAIT, m->m_type);
68962587Sitojun			if (n && maxlen > MHLEN) {
690111119Simp				MCLGET(n, M_DONTWAIT);
69162587Sitojun				if ((n->m_flags & M_EXT) == 0) {
69262587Sitojun					m_free(n);
69362587Sitojun					n = NULL;
69462587Sitojun				}
69562587Sitojun			}
696111119Simp			if (!m_dup_pkthdr(n, m, M_DONTWAIT)) {
697108466Ssam				/*
698108466Ssam				 * Previous code did a blind M_COPY_PKTHDR
699108466Ssam				 * and said "just for rcvif".  If true, then
700108466Ssam				 * we could tolerate the dup failing (due to
701108466Ssam				 * the deep copy of the tag chain).  For now
702108466Ssam				 * be conservative and just fail.
703108466Ssam				 */
704108466Ssam				m_free(n);
705108466Ssam				n = NULL;
706108466Ssam			}
70753541Sshin			if (n == NULL) {
70853541Sshin				/* Give up remote */
70953541Sshin				break;
71053541Sshin			}
71178064Sume			n->m_pkthdr.rcvif = NULL;
71262587Sitojun			n->m_len = 0;
71362587Sitojun			maxhlen = M_TRAILINGSPACE(n) - maxlen;
71462587Sitojun			if (maxhlen > hostnamelen)
71562587Sitojun				maxhlen = hostnamelen;
71653541Sshin			/*
71753541Sshin			 * Copy IPv6 and ICMPv6 only.
71853541Sshin			 */
71953541Sshin			nip6 = mtod(n, struct ip6_hdr *);
72053541Sshin			bcopy(ip6, nip6, sizeof(struct ip6_hdr));
72153541Sshin			nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
72253541Sshin			bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
72353541Sshin			p = (u_char *)(nicmp6 + 1);
72453541Sshin			bzero(p, 4);
72595023Ssuz			bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
72653541Sshin			noff = sizeof(struct ip6_hdr);
72753541Sshin			n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
72862587Sitojun				sizeof(struct icmp6_hdr) + 4 + maxhlen;
72953541Sshin			nicmp6->icmp6_type = ICMP6_WRUREPLY;
73053541Sshin			nicmp6->icmp6_code = 0;
73153541Sshin		}
73253541Sshin#undef hostnamelen
73353541Sshin		if (n) {
73453541Sshin			icmp6stat.icp6s_reflect++;
73553541Sshin			icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
73653541Sshin			icmp6_reflect(n, noff);
73753541Sshin		}
73853541Sshin		break;
73953541Sshin	    }
74053541Sshin
74153541Sshin	case ICMP6_WRUREPLY:
74253541Sshin		if (code != 0)
74353541Sshin			goto badcode;
74453541Sshin		break;
74553541Sshin
74653541Sshin	case ND_ROUTER_SOLICIT:
74753541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
74853541Sshin		if (code != 0)
74953541Sshin			goto badcode;
75053541Sshin		if (icmp6len < sizeof(struct nd_router_solicit))
75153541Sshin			goto badlen;
752111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
75362587Sitojun			/* give up local */
75462587Sitojun			nd6_rs_input(m, off, icmp6len);
75562587Sitojun			m = NULL;
75662587Sitojun			goto freeit;
75762587Sitojun		}
75862587Sitojun		nd6_rs_input(n, off, icmp6len);
75953541Sshin		/* m stays. */
76053541Sshin		break;
76153541Sshin
76253541Sshin	case ND_ROUTER_ADVERT:
76353541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
76453541Sshin		if (code != 0)
76553541Sshin			goto badcode;
76653541Sshin		if (icmp6len < sizeof(struct nd_router_advert))
76753541Sshin			goto badlen;
768111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
76962587Sitojun			/* give up local */
77062587Sitojun			nd6_ra_input(m, off, icmp6len);
77162587Sitojun			m = NULL;
77262587Sitojun			goto freeit;
77362587Sitojun		}
77462587Sitojun		nd6_ra_input(n, off, icmp6len);
77553541Sshin		/* m stays. */
77653541Sshin		break;
77753541Sshin
77853541Sshin	case ND_NEIGHBOR_SOLICIT:
77953541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
78053541Sshin		if (code != 0)
78153541Sshin			goto badcode;
78253541Sshin		if (icmp6len < sizeof(struct nd_neighbor_solicit))
78353541Sshin			goto badlen;
784111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
78562587Sitojun			/* give up local */
78662587Sitojun			nd6_ns_input(m, off, icmp6len);
78762587Sitojun			m = NULL;
78862587Sitojun			goto freeit;
78962587Sitojun		}
79062587Sitojun		nd6_ns_input(n, off, icmp6len);
79153541Sshin		/* m stays. */
79253541Sshin		break;
79353541Sshin
79453541Sshin	case ND_NEIGHBOR_ADVERT:
79553541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
79653541Sshin		if (code != 0)
79753541Sshin			goto badcode;
79853541Sshin		if (icmp6len < sizeof(struct nd_neighbor_advert))
79953541Sshin			goto badlen;
800111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
80162587Sitojun			/* give up local */
80262587Sitojun			nd6_na_input(m, off, icmp6len);
80362587Sitojun			m = NULL;
80462587Sitojun			goto freeit;
80562587Sitojun		}
80662587Sitojun		nd6_na_input(n, off, icmp6len);
80753541Sshin		/* m stays. */
80853541Sshin		break;
80953541Sshin
81053541Sshin	case ND_REDIRECT:
81153541Sshin		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
81253541Sshin		if (code != 0)
81353541Sshin			goto badcode;
81453541Sshin		if (icmp6len < sizeof(struct nd_redirect))
81553541Sshin			goto badlen;
816111119Simp		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
81762587Sitojun			/* give up local */
81862587Sitojun			icmp6_redirect_input(m, off);
81962587Sitojun			m = NULL;
82062587Sitojun			goto freeit;
82162587Sitojun		}
82262587Sitojun		icmp6_redirect_input(n, off);
82353541Sshin		/* m stays. */
82453541Sshin		break;
82553541Sshin
82653541Sshin	case ICMP6_ROUTER_RENUMBERING:
82753541Sshin		if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
82853541Sshin		    code != ICMP6_ROUTER_RENUMBERING_RESULT)
82953541Sshin			goto badcode;
83053541Sshin		if (icmp6len < sizeof(struct icmp6_router_renum))
83153541Sshin			goto badlen;
83253541Sshin		break;
83353541Sshin
83453541Sshin	default:
83578064Sume		nd6log((LOG_DEBUG,
83678064Sume		    "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
83778064Sume		    icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
83878064Sume		    ip6_sprintf(&ip6->ip6_dst),
83978064Sume		    m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
84053541Sshin		if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
84153541Sshin			/* ICMPv6 error: MUST deliver it by spec... */
84253541Sshin			code = PRC_NCMDS;
84353541Sshin			/* deliver */
84453541Sshin		} else {
84553541Sshin			/* ICMPv6 informational: MUST not deliver */
84653541Sshin			break;
84753541Sshin		}
84853541Sshin	deliver:
84978064Sume		if (icmp6_notify_error(m, off, icmp6len, code)) {
85078064Sume			/* In this case, m should've been freed. */
85178064Sume			return(IPPROTO_DONE);
85253541Sshin		}
85378064Sume		break;
85478064Sume
85578064Sume	badcode:
85678064Sume		icmp6stat.icp6s_badcode++;
85778064Sume		break;
85878064Sume
85978064Sume	badlen:
86078064Sume		icmp6stat.icp6s_badlen++;
86178064Sume		break;
86278064Sume	}
86378064Sume
86478064Sume	/* deliver the packet to appropriate sockets */
86578064Sume	icmp6_rip6_input(&m, *offp);
86678064Sume
86778064Sume	return IPPROTO_DONE;
86878064Sume
86978064Sume freeit:
87078064Sume	m_freem(m);
87178064Sume	return IPPROTO_DONE;
87278064Sume}
87378064Sume
87478064Sumestatic int
87578064Sumeicmp6_notify_error(m, off, icmp6len, code)
87678064Sume	struct mbuf *m;
87778064Sume	int off, icmp6len;
87878064Sume{
87978064Sume	struct icmp6_hdr *icmp6;
88078064Sume	struct ip6_hdr *eip6;
88178064Sume	u_int32_t notifymtu;
88278064Sume	struct sockaddr_in6 icmp6src, icmp6dst;
88378064Sume
88478064Sume	if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
88578064Sume		icmp6stat.icp6s_tooshort++;
88678064Sume		goto freeit;
88778064Sume	}
88862587Sitojun#ifndef PULLDOWN_TEST
88978064Sume	IP6_EXTHDR_CHECK(m, off,
89078064Sume			 sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr),
89178064Sume			 -1);
89278064Sume	icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
89362587Sitojun#else
89478064Sume	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
89578064Sume		       sizeof(*icmp6) + sizeof(struct ip6_hdr));
89678064Sume	if (icmp6 == NULL) {
89778064Sume		icmp6stat.icp6s_tooshort++;
89878064Sume		return(-1);
89978064Sume	}
90062587Sitojun#endif
90178064Sume	eip6 = (struct ip6_hdr *)(icmp6 + 1);
90253541Sshin
90378064Sume	/* Detect the upper level protocol */
90478064Sume	{
90553541Sshin		void (*ctlfunc) __P((int, struct sockaddr *, void *));
90653541Sshin		u_int8_t nxt = eip6->ip6_nxt;
90753541Sshin		int eoff = off + sizeof(struct icmp6_hdr) +
90853541Sshin			sizeof(struct ip6_hdr);
90953541Sshin		struct ip6ctlparam ip6cp;
91062587Sitojun		struct in6_addr *finaldst = NULL;
91162587Sitojun		int icmp6type = icmp6->icmp6_type;
91262587Sitojun		struct ip6_frag *fh;
91362587Sitojun		struct ip6_rthdr *rth;
91462587Sitojun		struct ip6_rthdr0 *rth0;
91562587Sitojun		int rthlen;
91653541Sshin
91795023Ssuz		while (1) { /* XXX: should avoid infinite loop explicitly? */
91853541Sshin			struct ip6_ext *eh;
91953541Sshin
92078064Sume			switch (nxt) {
92153541Sshin			case IPPROTO_HOPOPTS:
92253541Sshin			case IPPROTO_DSTOPTS:
92353541Sshin			case IPPROTO_AH:
92462587Sitojun#ifndef PULLDOWN_TEST
92553541Sshin				IP6_EXTHDR_CHECK(m, 0, eoff +
92653541Sshin						 sizeof(struct ip6_ext),
92778064Sume						 -1);
92853541Sshin				eh = (struct ip6_ext *)(mtod(m, caddr_t)
92953541Sshin							+ eoff);
93062587Sitojun#else
93162587Sitojun				IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
93278064Sume					       eoff, sizeof(*eh));
93362587Sitojun				if (eh == NULL) {
93462587Sitojun					icmp6stat.icp6s_tooshort++;
93578064Sume					return(-1);
93662587Sitojun				}
93762587Sitojun#endif
93862587Sitojun
93953541Sshin				if (nxt == IPPROTO_AH)
94053541Sshin					eoff += (eh->ip6e_len + 2) << 2;
94153541Sshin				else
94253541Sshin					eoff += (eh->ip6e_len + 1) << 3;
94353541Sshin				nxt = eh->ip6e_nxt;
94453541Sshin				break;
94562587Sitojun			case IPPROTO_ROUTING:
94662587Sitojun				/*
94762587Sitojun				 * When the erroneous packet contains a
94862587Sitojun				 * routing header, we should examine the
94962587Sitojun				 * header to determine the final destination.
95062587Sitojun				 * Otherwise, we can't properly update
95162587Sitojun				 * information that depends on the final
95262587Sitojun				 * destination (e.g. path MTU).
95362587Sitojun				 */
95462587Sitojun#ifndef PULLDOWN_TEST
95562587Sitojun				IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth),
95678064Sume						 -1);
95762587Sitojun				rth = (struct ip6_rthdr *)(mtod(m, caddr_t)
95862587Sitojun							   + eoff);
95962587Sitojun#else
96062587Sitojun				IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
96178064Sume					       eoff, sizeof(*rth));
96262587Sitojun				if (rth == NULL) {
96362587Sitojun					icmp6stat.icp6s_tooshort++;
96478064Sume					return(-1);
96562587Sitojun				}
96662587Sitojun#endif
96762587Sitojun				rthlen = (rth->ip6r_len + 1) << 3;
96862587Sitojun				/*
96962587Sitojun				 * XXX: currently there is no
97062587Sitojun				 * officially defined type other
97162587Sitojun				 * than type-0.
97262587Sitojun				 * Note that if the segment left field
97362587Sitojun				 * is 0, all intermediate hops must
97462587Sitojun				 * have been passed.
97562587Sitojun				 */
97662587Sitojun				if (rth->ip6r_segleft &&
97762587Sitojun				    rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
97862587Sitojun					int hops;
97962587Sitojun
98062587Sitojun#ifndef PULLDOWN_TEST
98162587Sitojun					IP6_EXTHDR_CHECK(m, 0, eoff + rthlen,
98278064Sume							 -1);
98362587Sitojun					rth0 = (struct ip6_rthdr0 *)(mtod(m, caddr_t) + eoff);
98462587Sitojun#else
98562587Sitojun					IP6_EXTHDR_GET(rth0,
98662587Sitojun						       struct ip6_rthdr0 *, m,
98762587Sitojun						       eoff, rthlen);
98862587Sitojun					if (rth0 == NULL) {
98962587Sitojun						icmp6stat.icp6s_tooshort++;
99078064Sume						return(-1);
99162587Sitojun					}
99262587Sitojun#endif
99362587Sitojun					/* just ignore a bogus header */
99462587Sitojun					if ((rth0->ip6r0_len % 2) == 0 &&
99562587Sitojun					    (hops = rth0->ip6r0_len/2))
99662587Sitojun						finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
99762587Sitojun				}
99862587Sitojun				eoff += rthlen;
99962587Sitojun				nxt = rth->ip6r_nxt;
100062587Sitojun				break;
100162587Sitojun			case IPPROTO_FRAGMENT:
100262587Sitojun#ifndef PULLDOWN_TEST
100362587Sitojun				IP6_EXTHDR_CHECK(m, 0, eoff +
100462587Sitojun						 sizeof(struct ip6_frag),
100578064Sume						 -1);
100662587Sitojun				fh = (struct ip6_frag *)(mtod(m, caddr_t)
100762587Sitojun							 + eoff);
100862587Sitojun#else
100962587Sitojun				IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
101078064Sume					       eoff, sizeof(*fh));
101162587Sitojun				if (fh == NULL) {
101262587Sitojun					icmp6stat.icp6s_tooshort++;
101378064Sume					return(-1);
101462587Sitojun				}
101562587Sitojun#endif
101662587Sitojun				/*
101762587Sitojun				 * Data after a fragment header is meaningless
101862587Sitojun				 * unless it is the first fragment, but
101962587Sitojun				 * we'll go to the notify label for path MTU
102062587Sitojun				 * discovery.
102162587Sitojun				 */
102262587Sitojun				if (fh->ip6f_offlg & IP6F_OFF_MASK)
102362587Sitojun					goto notify;
102462587Sitojun
102562587Sitojun				eoff += sizeof(struct ip6_frag);
102662587Sitojun				nxt = fh->ip6f_nxt;
102762587Sitojun				break;
102853541Sshin			default:
102962587Sitojun				/*
103062587Sitojun				 * This case includes ESP and the No Next
103195023Ssuz				 * Header.  In such cases going to the notify
103262587Sitojun				 * label does not have any meaning
103362587Sitojun				 * (i.e. ctlfunc will be NULL), but we go
103462587Sitojun				 * anyway since we might have to update
103562587Sitojun				 * path MTU information.
103662587Sitojun				 */
103753541Sshin				goto notify;
103853541Sshin			}
103953541Sshin		}
104078064Sume	  notify:
104162587Sitojun#ifndef PULLDOWN_TEST
104253541Sshin		icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
104362587Sitojun#else
104462587Sitojun		IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
104578064Sume			       sizeof(*icmp6) + sizeof(struct ip6_hdr));
104662587Sitojun		if (icmp6 == NULL) {
104762587Sitojun			icmp6stat.icp6s_tooshort++;
104878064Sume			return(-1);
104962587Sitojun		}
105062587Sitojun#endif
105178064Sume
105278064Sume		eip6 = (struct ip6_hdr *)(icmp6 + 1);
105378064Sume		bzero(&icmp6dst, sizeof(icmp6dst));
105478064Sume		icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
105578064Sume		icmp6dst.sin6_family = AF_INET6;
105678064Sume		if (finaldst == NULL)
105778064Sume			icmp6dst.sin6_addr = eip6->ip6_dst;
105878064Sume		else
105978064Sume			icmp6dst.sin6_addr = *finaldst;
106078064Sume		icmp6dst.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
106178064Sume							  &icmp6dst.sin6_addr);
106278064Sume#ifndef SCOPEDROUTING
106378064Sume		if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst,
106478064Sume				   NULL, NULL)) {
106578064Sume			/* should be impossbile */
106678064Sume			nd6log((LOG_DEBUG,
106778064Sume			    "icmp6_notify_error: in6_embedscope failed\n"));
106878064Sume			goto freeit;
106978064Sume		}
107078064Sume#endif
107178064Sume
107278064Sume		/*
107378064Sume		 * retrieve parameters from the inner IPv6 header, and convert
107478064Sume		 * them into sockaddr structures.
107578064Sume		 */
107678064Sume		bzero(&icmp6src, sizeof(icmp6src));
107778064Sume		icmp6src.sin6_len = sizeof(struct sockaddr_in6);
107878064Sume		icmp6src.sin6_family = AF_INET6;
107978064Sume		icmp6src.sin6_addr = eip6->ip6_src;
108078064Sume		icmp6src.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
108178064Sume							  &icmp6src.sin6_addr);
108278064Sume#ifndef SCOPEDROUTING
108378064Sume		if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src,
108478064Sume				   NULL, NULL)) {
108578064Sume			/* should be impossbile */
108678064Sume			nd6log((LOG_DEBUG,
108778064Sume			    "icmp6_notify_error: in6_embedscope failed\n"));
108878064Sume			goto freeit;
108978064Sume		}
109078064Sume#endif
109178064Sume		icmp6src.sin6_flowinfo =
109278064Sume			(eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
109378064Sume
109478064Sume		if (finaldst == NULL)
109578064Sume			finaldst = &eip6->ip6_dst;
109678064Sume		ip6cp.ip6c_m = m;
109778064Sume		ip6cp.ip6c_icmp6 = icmp6;
109878064Sume		ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
109978064Sume		ip6cp.ip6c_off = eoff;
110078064Sume		ip6cp.ip6c_finaldst = finaldst;
110178064Sume		ip6cp.ip6c_src = &icmp6src;
110278064Sume		ip6cp.ip6c_nxt = nxt;
110378064Sume
110462587Sitojun		if (icmp6type == ICMP6_PACKET_TOO_BIG) {
110578064Sume			notifymtu = ntohl(icmp6->icmp6_mtu);
110678064Sume			ip6cp.ip6c_cmdarg = (void *)&notifymtu;
110778064Sume			icmp6_mtudisc_update(&ip6cp, 1);	/*XXX*/
110862587Sitojun		}
110962587Sitojun
111053541Sshin		ctlfunc = (void (*) __P((int, struct sockaddr *, void *)))
111153541Sshin			(inet6sw[ip6_protox[nxt]].pr_ctlinput);
111253541Sshin		if (ctlfunc) {
111378064Sume			(void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
111478064Sume					  &ip6cp);
111553541Sshin		}
111653541Sshin	}
111778064Sume	return(0);
111853541Sshin
111978064Sume  freeit:
112053541Sshin	m_freem(m);
112178064Sume	return(-1);
112253541Sshin}
112353541Sshin
112478064Sumevoid
112578064Sumeicmp6_mtudisc_update(ip6cp, validated)
112678064Sume	struct ip6ctlparam *ip6cp;
112778064Sume	int validated;
112862587Sitojun{
112978064Sume	struct in6_addr *dst = ip6cp->ip6c_finaldst;
113078064Sume	struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
113178064Sume	struct mbuf *m = ip6cp->ip6c_m;	/* will be necessary for scope issue */
113262587Sitojun	u_int mtu = ntohl(icmp6->icmp6_mtu);
113362587Sitojun	struct rtentry *rt = NULL;
113462587Sitojun	struct sockaddr_in6 sin6;
113562587Sitojun
113678064Sume	if (!validated)
113778064Sume		return;
113878064Sume
113962587Sitojun	bzero(&sin6, sizeof(sin6));
114062587Sitojun	sin6.sin6_family = PF_INET6;
114162587Sitojun	sin6.sin6_len = sizeof(struct sockaddr_in6);
114262587Sitojun	sin6.sin6_addr = *dst;
114378064Sume	/* XXX normally, this won't happen */
114478064Sume	if (IN6_IS_ADDR_LINKLOCAL(dst)) {
114578064Sume		sin6.sin6_addr.s6_addr16[1] =
114678064Sume		    htons(m->m_pkthdr.rcvif->if_index);
114778064Sume	}
114862587Sitojun	/* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
114962587Sitojun	rt = rtalloc1((struct sockaddr *)&sin6, 0,
115062587Sitojun		      RTF_CLONING | RTF_PRCLONING);
115162587Sitojun
115262587Sitojun	if (rt && (rt->rt_flags & RTF_HOST)
115362587Sitojun	    && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
115462587Sitojun		if (mtu < IPV6_MMTU) {
115562587Sitojun				/* xxx */
115662587Sitojun			rt->rt_rmx.rmx_locks |= RTV_MTU;
115762587Sitojun		} else if (mtu < rt->rt_ifp->if_mtu &&
115862587Sitojun			   rt->rt_rmx.rmx_mtu > mtu) {
115978064Sume			icmp6stat.icp6s_pmtuchg++;
116062587Sitojun			rt->rt_rmx.rmx_mtu = mtu;
116162587Sitojun		}
116262587Sitojun	}
116395023Ssuz	if (rt) { /* XXX: need braces to avoid conflict with else in RTFREE. */
116462587Sitojun		RTFREE(rt);
116595023Ssuz	}
116662587Sitojun}
116762587Sitojun
116853541Sshin/*
116978064Sume * Process a Node Information Query packet, based on
117078064Sume * draft-ietf-ipngwg-icmp-name-lookups-07.
117162587Sitojun *
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 */
117962587Sitojun#define hostnamelen	strlen(hostname)
118053541Sshinstatic struct mbuf *
118153541Sshinni6_input(m, off)
118253541Sshin	struct mbuf *m;
118353541Sshin	int off;
118453541Sshin{
118562587Sitojun	struct icmp6_nodeinfo *ni6, *nni6;
118653541Sshin	struct mbuf *n = NULL;
118762587Sitojun	u_int16_t qtype;
118862587Sitojun	int subjlen;
118953541Sshin	int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
119053541Sshin	struct ni_reply_fqdn *fqdn;
119153541Sshin	int addrs;		/* for NI_QTYPE_NODEADDR */
119253541Sshin	struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
119378064Sume	struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */
119478064Sume	struct sockaddr_in6 sin6_d; /* XXX: we should retrieve this from m_aux */
119562587Sitojun	struct ip6_hdr *ip6;
119662587Sitojun	int oldfqdn = 0;	/* if 1, return pascal string (03 draft) */
119778064Sume	char *subj = NULL;
119878064Sume	struct in6_ifaddr *ia6 = NULL;
119953541Sshin
120062587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
120162587Sitojun#ifndef PULLDOWN_TEST
120262587Sitojun	ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
120362587Sitojun#else
120462587Sitojun	IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
120562587Sitojun	if (ni6 == NULL) {
120662587Sitojun		/* m is already reclaimed */
120762587Sitojun		return NULL;
120862587Sitojun	}
120962587Sitojun#endif
121053541Sshin
121162587Sitojun	/*
121262587Sitojun	 * Validate IPv6 destination address.
121362587Sitojun	 *
121478064Sume	 * The Responder must discard the Query without further processing
121578064Sume	 * unless it is one of the Responder's unicast or anycast addresses, or
121678064Sume	 * a link-local scope multicast address which the Responder has joined.
121778064Sume	 * [icmp-name-lookups-07, Section 4.]
121862587Sitojun	 */
121962587Sitojun	bzero(&sin6, sizeof(sin6));
122062587Sitojun	sin6.sin6_family = AF_INET6;
122162587Sitojun	sin6.sin6_len = sizeof(struct sockaddr_in6);
122262587Sitojun	bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
122362587Sitojun	/* XXX scopeid */
122478064Sume	if ((ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)&sin6)) != NULL) {
122578064Sume		/* unicast/anycast, fine */
122678064Sume		if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
122778064Sume		    (icmp6_nodeinfo & 4) == 0) {
122878064Sume			nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
122978064Sume				"a temporary address in %s:%d",
123078064Sume			       __FILE__, __LINE__));
123178064Sume			goto bad;
123278064Sume		}
123378064Sume	} else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
123478064Sume		; /* link-local multicast, fine */
123562587Sitojun	else
123662587Sitojun		goto bad;
123762587Sitojun
123878064Sume	/* validate query Subject field. */
123962587Sitojun	qtype = ntohs(ni6->ni_qtype);
124062587Sitojun	subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
124162587Sitojun	switch (qtype) {
124262587Sitojun	case NI_QTYPE_NOOP:
124362587Sitojun	case NI_QTYPE_SUPTYPES:
124478064Sume		/* 07 draft */
124578064Sume		if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
124678064Sume			break;
124778064Sume		/* FALLTHROUGH */
124862587Sitojun	case NI_QTYPE_FQDN:
124962587Sitojun	case NI_QTYPE_NODEADDR:
125062587Sitojun		switch (ni6->ni_code) {
125162587Sitojun		case ICMP6_NI_SUBJ_IPV6:
125262587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0
125362587Sitojun		case 0:
125462587Sitojun#endif
125562587Sitojun			/*
125662587Sitojun			 * backward compatibility - try to accept 03 draft
125762587Sitojun			 * format, where no Subject is present.
125862587Sitojun			 */
125978064Sume			if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
126078064Sume			    subjlen == 0) {
126162587Sitojun				oldfqdn++;
126262587Sitojun				break;
126362587Sitojun			}
126478064Sume#if ICMP6_NI_SUBJ_IPV6 != 0
126578064Sume			if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
126678064Sume				goto bad;
126778064Sume#endif
126862587Sitojun
126962587Sitojun			if (subjlen != sizeof(sin6.sin6_addr))
127062587Sitojun				goto bad;
127162587Sitojun
127262587Sitojun			/*
127362587Sitojun			 * Validate Subject address.
127462587Sitojun			 *
127578064Sume			 * Not sure what exactly "address belongs to the node"
127678064Sume			 * means in the spec, is it just unicast, or what?
127762587Sitojun			 *
127862587Sitojun			 * At this moment we consider Subject address as
127962587Sitojun			 * "belong to the node" if the Subject address equals
128062587Sitojun			 * to the IPv6 destination address; validation for
128162587Sitojun			 * IPv6 destination address should have done enough
128262587Sitojun			 * check for us.
128362587Sitojun			 *
128462587Sitojun			 * We do not do proxy at this moment.
128562587Sitojun			 */
128662587Sitojun			/* m_pulldown instead of copy? */
128762587Sitojun			m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
128862587Sitojun			    subjlen, (caddr_t)&sin6.sin6_addr);
128978064Sume			sin6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
129078064Sume							      &sin6.sin6_addr);
129178064Sume#ifndef SCOPEDROUTING
129278064Sume			in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL);
129362587Sitojun#endif
129478064Sume			bzero(&sin6_d, sizeof(sin6_d));
129578064Sume			sin6_d.sin6_family = AF_INET6; /* not used, actually */
129678064Sume			sin6_d.sin6_len = sizeof(sin6_d); /* ditto */
129778064Sume			sin6_d.sin6_addr = ip6->ip6_dst;
129878064Sume			sin6_d.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
129978064Sume								&ip6->ip6_dst);
130078064Sume#ifndef SCOPEDROUTING
130178064Sume			in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL);
130278064Sume#endif
130378064Sume			subj = (char *)&sin6;
130478064Sume			if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d))
130562587Sitojun				break;
130678064Sume
130762587Sitojun			/*
130862587Sitojun			 * XXX if we are to allow other cases, we should really
130962587Sitojun			 * be careful about scope here.
131062587Sitojun			 * basically, we should disallow queries toward IPv6
131162587Sitojun			 * destination X with subject Y, if scope(X) > scope(Y).
131262587Sitojun			 * if we allow scope(X) > scope(Y), it will result in
131362587Sitojun			 * information leakage across scope boundary.
131462587Sitojun			 */
131562587Sitojun			goto bad;
131662587Sitojun
131762587Sitojun		case ICMP6_NI_SUBJ_FQDN:
131862587Sitojun			/*
131962587Sitojun			 * Validate Subject name with gethostname(3).
132062587Sitojun			 *
132162587Sitojun			 * The behavior may need some debate, since:
132262587Sitojun			 * - we are not sure if the node has FQDN as
132362587Sitojun			 *   hostname (returned by gethostname(3)).
132462587Sitojun			 * - the code does wildcard match for truncated names.
132562587Sitojun			 *   however, we are not sure if we want to perform
132662587Sitojun			 *   wildcard match, if gethostname(3) side has
132762587Sitojun			 *   truncated hostname.
132862587Sitojun			 */
132962587Sitojun			n = ni6_nametodns(hostname, hostnamelen, 0);
133062587Sitojun			if (!n || n->m_next || n->m_len == 0)
133162587Sitojun				goto bad;
133262587Sitojun			IP6_EXTHDR_GET(subj, char *, m,
133362587Sitojun			    off + sizeof(struct icmp6_nodeinfo), subjlen);
133462587Sitojun			if (subj == NULL)
133562587Sitojun				goto bad;
133662587Sitojun			if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
133762587Sitojun					n->m_len)) {
133862587Sitojun				goto bad;
133962587Sitojun			}
134062587Sitojun			m_freem(n);
134162587Sitojun			n = NULL;
134262587Sitojun			break;
134362587Sitojun
134478064Sume		case ICMP6_NI_SUBJ_IPV4:	/* XXX: to be implemented? */
134562587Sitojun		default:
134662587Sitojun			goto bad;
134762587Sitojun		}
134862587Sitojun		break;
134978064Sume	}
135062587Sitojun
135178064Sume	/* refuse based on configuration.  XXX ICMP6_NI_REFUSED? */
135278064Sume	switch (qtype) {
135378064Sume	case NI_QTYPE_FQDN:
135478064Sume		if ((icmp6_nodeinfo & 1) == 0)
135578064Sume			goto bad;
135678064Sume		break;
135778064Sume	case NI_QTYPE_NODEADDR:
135878064Sume		if ((icmp6_nodeinfo & 2) == 0)
135978064Sume			goto bad;
136078064Sume		break;
136178064Sume	}
136278064Sume
136378064Sume	/* guess reply length */
136478064Sume	switch (qtype) {
136578064Sume	case NI_QTYPE_NOOP:
136678064Sume		break;		/* no reply data */
136778064Sume	case NI_QTYPE_SUPTYPES:
136878064Sume		replylen += sizeof(u_int32_t);
136978064Sume		break;
137078064Sume	case NI_QTYPE_FQDN:
137178064Sume		/* XXX will append an mbuf */
137278064Sume		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
137378064Sume		break;
137478064Sume	case NI_QTYPE_NODEADDR:
137578064Sume		addrs = ni6_addrs(ni6, m, &ifp, subj);
137678064Sume		if ((replylen += addrs * (sizeof(struct in6_addr) +
137778064Sume					  sizeof(u_int32_t))) > MCLBYTES)
137878064Sume			replylen = MCLBYTES; /* XXX: will truncate pkt later */
137978064Sume		break;
138062587Sitojun	default:
138178064Sume		/*
138278064Sume		 * XXX: We must return a reply with the ICMP6 code
138378064Sume		 * `unknown Qtype' in this case. However we regard the case
138478064Sume		 * as an FQDN query for backward compatibility.
138578064Sume		 * Older versions set a random value to this field,
138678064Sume		 * so it rarely varies in the defined qtypes.
138778064Sume		 * But the mechanism is not reliable...
138878064Sume		 * maybe we should obsolete older versions.
138978064Sume		 */
139078064Sume		qtype = NI_QTYPE_FQDN;
139178064Sume		/* XXX will append an mbuf */
139278064Sume		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
139378064Sume		oldfqdn++;
139478064Sume		break;
139562587Sitojun	}
139662587Sitojun
139778064Sume	/* allocate an mbuf to reply. */
1398111119Simp	MGETHDR(n, M_DONTWAIT, m->m_type);
139962587Sitojun	if (n == NULL) {
140062587Sitojun		m_freem(m);
140153541Sshin		return(NULL);
140262587Sitojun	}
1403108466Ssam	M_MOVE_PKTHDR(n, m); /* just for recvif */
140453541Sshin	if (replylen > MHLEN) {
140562587Sitojun		if (replylen > MCLBYTES) {
140678064Sume			/*
140778064Sume			 * XXX: should we try to allocate more? But MCLBYTES
140878064Sume			 * is probably much larger than IPV6_MMTU...
140978064Sume			 */
141053541Sshin			goto bad;
141162587Sitojun		}
1412111119Simp		MCLGET(n, M_DONTWAIT);
141353541Sshin		if ((n->m_flags & M_EXT) == 0) {
141453541Sshin			goto bad;
141553541Sshin		}
141653541Sshin	}
141753541Sshin	n->m_pkthdr.len = n->m_len = replylen;
141853541Sshin
141953541Sshin	/* copy mbuf header and IPv6 + Node Information base headers */
142053541Sshin	bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
142153541Sshin	nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
142262587Sitojun	bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
142353541Sshin
142453541Sshin	/* qtype dependent procedure */
142553541Sshin	switch (qtype) {
142662587Sitojun	case NI_QTYPE_NOOP:
142778064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
142862587Sitojun		nni6->ni_flags = 0;
142962587Sitojun		break;
143062587Sitojun	case NI_QTYPE_SUPTYPES:
143178064Sume	{
143278064Sume		u_int32_t v;
143378064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
143478064Sume		nni6->ni_flags = htons(0x0000);	/* raw bitmap */
143578064Sume		/* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
143678064Sume		v = (u_int32_t)htonl(0x0000000f);
143778064Sume		bcopy(&v, nni6 + 1, sizeof(u_int32_t));
143862587Sitojun		break;
143978064Sume	}
144062587Sitojun	case NI_QTYPE_FQDN:
144178064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
144262587Sitojun		fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
144362587Sitojun						sizeof(struct ip6_hdr) +
144462587Sitojun						sizeof(struct icmp6_nodeinfo));
144562587Sitojun		nni6->ni_flags = 0; /* XXX: meaningless TTL */
144662587Sitojun		fqdn->ni_fqdn_ttl = 0;	/* ditto. */
144762587Sitojun		/*
144862587Sitojun		 * XXX do we really have FQDN in variable "hostname"?
144962587Sitojun		 */
145062587Sitojun		n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
145162587Sitojun		if (n->m_next == NULL)
145262587Sitojun			goto bad;
145362587Sitojun		/* XXX we assume that n->m_next is not a chain */
145462587Sitojun		if (n->m_next->m_next != NULL)
145562587Sitojun			goto bad;
145662587Sitojun		n->m_pkthdr.len += n->m_next->m_len;
145762587Sitojun		break;
145862587Sitojun	case NI_QTYPE_NODEADDR:
145962587Sitojun	{
146062587Sitojun		int lenlim, copied;
146153541Sshin
146278064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
146378064Sume		n->m_pkthdr.len = n->m_len =
146478064Sume		    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
146578064Sume		lenlim = M_TRAILINGSPACE(n);
146662587Sitojun		copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
146762587Sitojun		/* XXX: reset mbuf length */
146862587Sitojun		n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
146962587Sitojun			sizeof(struct icmp6_nodeinfo) + copied;
147062587Sitojun		break;
147153541Sshin	}
147262587Sitojun	default:
147362587Sitojun		break;		/* XXX impossible! */
147462587Sitojun	}
147553541Sshin
147653541Sshin	nni6->ni_type = ICMP6_NI_REPLY;
147762587Sitojun	m_freem(m);
147853541Sshin	return(n);
147953541Sshin
148053541Sshin  bad:
148162587Sitojun	m_freem(m);
148253541Sshin	if (n)
148353541Sshin		m_freem(n);
148453541Sshin	return(NULL);
148553541Sshin}
148653541Sshin#undef hostnamelen
148753541Sshin
148853541Sshin/*
148962587Sitojun * make a mbuf with DNS-encoded string.  no compression support.
149062587Sitojun *
149162587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be
149262587Sitojun * treated as truncated name (two \0 at the end).  this is a wild guess.
149362587Sitojun */
149462587Sitojunstatic struct mbuf *
149562587Sitojunni6_nametodns(name, namelen, old)
149662587Sitojun	const char *name;
149762587Sitojun	int namelen;
149862587Sitojun	int old;	/* return pascal string if non-zero */
149962587Sitojun{
150062587Sitojun	struct mbuf *m;
150162587Sitojun	char *cp, *ep;
150262587Sitojun	const char *p, *q;
150362587Sitojun	int i, len, nterm;
150462587Sitojun
150562587Sitojun	if (old)
150662587Sitojun		len = namelen + 1;
150762587Sitojun	else
150862587Sitojun		len = MCLBYTES;
150962587Sitojun
151062587Sitojun	/* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1511111119Simp	MGET(m, M_DONTWAIT, MT_DATA);
151262587Sitojun	if (m && len > MLEN) {
1513111119Simp		MCLGET(m, M_DONTWAIT);
151462587Sitojun		if ((m->m_flags & M_EXT) == 0)
151562587Sitojun			goto fail;
151662587Sitojun	}
151762587Sitojun	if (!m)
151862587Sitojun		goto fail;
151962587Sitojun	m->m_next = NULL;
152062587Sitojun
152162587Sitojun	if (old) {
152262587Sitojun		m->m_len = len;
152362587Sitojun		*mtod(m, char *) = namelen;
152462587Sitojun		bcopy(name, mtod(m, char *) + 1, namelen);
152562587Sitojun		return m;
152662587Sitojun	} else {
152762587Sitojun		m->m_len = 0;
152862587Sitojun		cp = mtod(m, char *);
152962587Sitojun		ep = mtod(m, char *) + M_TRAILINGSPACE(m);
153062587Sitojun
153162587Sitojun		/* if not certain about my name, return empty buffer */
153262587Sitojun		if (namelen == 0)
153362587Sitojun			return m;
153462587Sitojun
153562587Sitojun		/*
153662587Sitojun		 * guess if it looks like shortened hostname, or FQDN.
153762587Sitojun		 * shortened hostname needs two trailing "\0".
153862587Sitojun		 */
153962587Sitojun		i = 0;
154062587Sitojun		for (p = name; p < name + namelen; p++) {
154162587Sitojun			if (*p && *p == '.')
154262587Sitojun				i++;
154362587Sitojun		}
154462587Sitojun		if (i < 2)
154562587Sitojun			nterm = 2;
154662587Sitojun		else
154762587Sitojun			nterm = 1;
154862587Sitojun
154962587Sitojun		p = name;
155062587Sitojun		while (cp < ep && p < name + namelen) {
155162587Sitojun			i = 0;
155262587Sitojun			for (q = p; q < name + namelen && *q && *q != '.'; q++)
155362587Sitojun				i++;
155462587Sitojun			/* result does not fit into mbuf */
155562587Sitojun			if (cp + i + 1 >= ep)
155662587Sitojun				goto fail;
155778704Sume			/*
155878704Sume			 * DNS label length restriction, RFC1035 page 8.
155978704Sume			 * "i == 0" case is included here to avoid returning
156078704Sume			 * 0-length label on "foo..bar".
156178704Sume			 */
156278704Sume			if (i <= 0 || i >= 64)
156362587Sitojun				goto fail;
156462587Sitojun			*cp++ = i;
156562587Sitojun			bcopy(p, cp, i);
156662587Sitojun			cp += i;
156762587Sitojun			p = q;
156862587Sitojun			if (p < name + namelen && *p == '.')
156962587Sitojun				p++;
157062587Sitojun		}
157162587Sitojun		/* termination */
157262587Sitojun		if (cp + nterm >= ep)
157362587Sitojun			goto fail;
157462587Sitojun		while (nterm-- > 0)
157562587Sitojun			*cp++ = '\0';
157662587Sitojun		m->m_len = cp - mtod(m, char *);
157762587Sitojun		return m;
157862587Sitojun	}
157962587Sitojun
158062587Sitojun	panic("should not reach here");
158195023Ssuz	/* NOTREACHED */
158262587Sitojun
158362587Sitojun fail:
158462587Sitojun	if (m)
158562587Sitojun		m_freem(m);
158662587Sitojun	return NULL;
158762587Sitojun}
158862587Sitojun
158962587Sitojun/*
159062587Sitojun * check if two DNS-encoded string matches.  takes care of truncated
159162587Sitojun * form (with \0\0 at the end).  no compression support.
159278064Sume * XXX upper/lowercase match (see RFC2065)
159362587Sitojun */
159462587Sitojunstatic int
159562587Sitojunni6_dnsmatch(a, alen, b, blen)
159662587Sitojun	const char *a;
159762587Sitojun	int alen;
159862587Sitojun	const char *b;
159962587Sitojun	int blen;
160062587Sitojun{
160162587Sitojun	const char *a0, *b0;
160262587Sitojun	int l;
160362587Sitojun
160462587Sitojun	/* simplest case - need validation? */
160562587Sitojun	if (alen == blen && bcmp(a, b, alen) == 0)
160662587Sitojun		return 1;
160762587Sitojun
160862587Sitojun	a0 = a;
160962587Sitojun	b0 = b;
161062587Sitojun
161162587Sitojun	/* termination is mandatory */
161262587Sitojun	if (alen < 2 || blen < 2)
161362587Sitojun		return 0;
161462587Sitojun	if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
161562587Sitojun		return 0;
161662587Sitojun	alen--;
161762587Sitojun	blen--;
161862587Sitojun
161962587Sitojun	while (a - a0 < alen && b - b0 < blen) {
162062587Sitojun		if (a - a0 + 1 > alen || b - b0 + 1 > blen)
162162587Sitojun			return 0;
162262587Sitojun
162362587Sitojun		if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
162462587Sitojun			return 0;
162562587Sitojun		/* we don't support compression yet */
162662587Sitojun		if (a[0] >= 64 || b[0] >= 64)
162762587Sitojun			return 0;
162862587Sitojun
162962587Sitojun		/* truncated case */
163062587Sitojun		if (a[0] == 0 && a - a0 == alen - 1)
163162587Sitojun			return 1;
163262587Sitojun		if (b[0] == 0 && b - b0 == blen - 1)
163362587Sitojun			return 1;
163462587Sitojun		if (a[0] == 0 || b[0] == 0)
163562587Sitojun			return 0;
163662587Sitojun
163762587Sitojun		if (a[0] != b[0])
163862587Sitojun			return 0;
163962587Sitojun		l = a[0];
164062587Sitojun		if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
164162587Sitojun			return 0;
164262587Sitojun		if (bcmp(a + 1, b + 1, l) != 0)
164362587Sitojun			return 0;
164462587Sitojun
164562587Sitojun		a += 1 + l;
164662587Sitojun		b += 1 + l;
164762587Sitojun	}
164862587Sitojun
164962587Sitojun	if (a - a0 == alen && b - b0 == blen)
165062587Sitojun		return 1;
165162587Sitojun	else
165262587Sitojun		return 0;
165362587Sitojun}
165462587Sitojun
165562587Sitojun/*
165653541Sshin * calculate the number of addresses to be returned in the node info reply.
165753541Sshin */
165853541Sshinstatic int
165978064Sumeni6_addrs(ni6, m, ifpp, subj)
166053541Sshin	struct icmp6_nodeinfo *ni6;
166153541Sshin	struct mbuf *m;
166253541Sshin	struct ifnet **ifpp;
166378064Sume	char *subj;
166453541Sshin{
166578064Sume	struct ifnet *ifp;
166678064Sume	struct in6_ifaddr *ifa6;
166778064Sume	struct ifaddr *ifa;
166878064Sume	struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
166953541Sshin	int addrs = 0, addrsofif, iffound = 0;
167078064Sume	int niflags = ni6->ni_flags;
167153541Sshin
167278064Sume	if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
167378064Sume		switch (ni6->ni_code) {
167478064Sume		case ICMP6_NI_SUBJ_IPV6:
167578064Sume			if (subj == NULL) /* must be impossible... */
167678064Sume				return(0);
167778064Sume			subj_ip6 = (struct sockaddr_in6 *)subj;
167878064Sume			break;
167978064Sume		default:
168078064Sume			/*
168178064Sume			 * XXX: we only support IPv6 subject address for
168278064Sume			 * this Qtype.
168378064Sume			 */
168478064Sume			return(0);
168578064Sume		}
168678064Sume	}
168778064Sume
1688108172Shsu	IFNET_RLOCK();
168953541Sshin	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
169053541Sshin	{
169153541Sshin		addrsofif = 0;
169279139Sume		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
169353541Sshin		{
169453541Sshin			if (ifa->ifa_addr->sa_family != AF_INET6)
169553541Sshin				continue;
169653541Sshin			ifa6 = (struct in6_ifaddr *)ifa;
169753541Sshin
169878064Sume			if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
169978064Sume			    IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
170053541Sshin					       &ifa6->ia_addr.sin6_addr))
170153541Sshin				iffound = 1;
170253541Sshin
170362587Sitojun			/*
170462587Sitojun			 * IPv4-mapped addresses can only be returned by a
170562587Sitojun			 * Node Information proxy, since they represent
170662587Sitojun			 * addresses of IPv4-only nodes, which perforce do
170762587Sitojun			 * not implement this protocol.
170878064Sume			 * [icmp-name-lookups-07, Section 5.4]
170962587Sitojun			 * So we don't support NI_NODEADDR_FLAG_COMPAT in
171062587Sitojun			 * this function at this moment.
171162587Sitojun			 */
171262587Sitojun
171353541Sshin			/* What do we have to do about ::1? */
171478064Sume			switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
171578064Sume			case IPV6_ADDR_SCOPE_LINKLOCAL:
171678064Sume				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
171778064Sume					continue;
171853541Sshin				break;
171978064Sume			case IPV6_ADDR_SCOPE_SITELOCAL:
172078064Sume				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
172178064Sume					continue;
172253541Sshin				break;
172378064Sume			case IPV6_ADDR_SCOPE_GLOBAL:
172478064Sume				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
172578064Sume					continue;
172678064Sume				break;
172778064Sume			default:
172878064Sume				continue;
172953541Sshin			}
173078064Sume
173178064Sume			/*
173278064Sume			 * check if anycast is okay.
173395023Ssuz			 * XXX: just experimental.  not in the spec.
173478064Sume			 */
173578064Sume			if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
173678064Sume			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
173778064Sume				continue; /* we need only unicast addresses */
173878064Sume			if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
173978064Sume			    (icmp6_nodeinfo & 4) == 0) {
174078064Sume				continue;
174178064Sume			}
174278064Sume			addrsofif++; /* count the address */
174353541Sshin		}
174453541Sshin		if (iffound) {
174553541Sshin			*ifpp = ifp;
1746108172Shsu			IFNET_RUNLOCK();
174753541Sshin			return(addrsofif);
174853541Sshin		}
174953541Sshin
175053541Sshin		addrs += addrsofif;
175153541Sshin	}
1752108172Shsu	IFNET_RUNLOCK();
175353541Sshin
175453541Sshin	return(addrs);
175553541Sshin}
175653541Sshin
175753541Sshinstatic int
175853541Sshinni6_store_addrs(ni6, nni6, ifp0, resid)
175953541Sshin	struct icmp6_nodeinfo *ni6, *nni6;
176053541Sshin	struct ifnet *ifp0;
176153541Sshin	int resid;
176253541Sshin{
176378064Sume	struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
176478064Sume	struct in6_ifaddr *ifa6;
176578064Sume	struct ifaddr *ifa;
176678064Sume	struct ifnet *ifp_dep = NULL;
176778064Sume	int copied = 0, allow_deprecated = 0;
176853541Sshin	u_char *cp = (u_char *)(nni6 + 1);
176978064Sume	int niflags = ni6->ni_flags;
177078064Sume	u_int32_t ltime;
177153541Sshin
177278064Sume	if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
177353541Sshin		return(0);	/* needless to copy */
1774108172Shsu
1775108172Shsu	IFNET_RLOCK();
177678064Sume  again:
177753541Sshin	for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
177853541Sshin	{
177962587Sitojun		for (ifa = ifp->if_addrlist.tqh_first; ifa;
178062587Sitojun		     ifa = ifa->ifa_list.tqe_next)
178153541Sshin		{
178253541Sshin			if (ifa->ifa_addr->sa_family != AF_INET6)
178353541Sshin				continue;
178453541Sshin			ifa6 = (struct in6_ifaddr *)ifa;
178553541Sshin
178678064Sume			if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
178778064Sume			    allow_deprecated == 0) {
178878064Sume				/*
178978064Sume				 * prefererred address should be put before
179078064Sume				 * deprecated addresses.
179178064Sume				 */
179278064Sume
179378064Sume				/* record the interface for later search */
179478064Sume				if (ifp_dep == NULL)
179578064Sume					ifp_dep = ifp;
179678064Sume
179778064Sume				continue;
179862587Sitojun			}
179978064Sume			else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
180078064Sume				 allow_deprecated != 0)
180178064Sume				continue; /* we now collect deprecated addrs */
180253541Sshin
180353541Sshin			/* What do we have to do about ::1? */
180478064Sume			switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
180578064Sume			case IPV6_ADDR_SCOPE_LINKLOCAL:
180678064Sume				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
180778064Sume					continue;
180853541Sshin				break;
180978064Sume			case IPV6_ADDR_SCOPE_SITELOCAL:
181078064Sume				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
181178064Sume					continue;
181253541Sshin				break;
181378064Sume			case IPV6_ADDR_SCOPE_GLOBAL:
181478064Sume				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
181578064Sume					continue;
181678064Sume				break;
181778064Sume			default:
181878064Sume				continue;
181953541Sshin			}
182053541Sshin
182178064Sume			/*
182278064Sume			 * check if anycast is okay.
182378064Sume			 * XXX: just experimental. not in the spec.
182478064Sume			 */
182578064Sume			if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
182678064Sume			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
182778064Sume				continue;
182878064Sume			if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
182978064Sume			    (icmp6_nodeinfo & 4) == 0) {
183078064Sume				continue;
183153541Sshin			}
183278064Sume
183378064Sume			/* now we can copy the address */
183478064Sume			if (resid < sizeof(struct in6_addr) +
183578064Sume			    sizeof(u_int32_t)) {
183678064Sume				/*
183778064Sume				 * We give up much more copy.
183878064Sume				 * Set the truncate flag and return.
183978064Sume				 */
184078064Sume				nni6->ni_flags |=
184178064Sume					NI_NODEADDR_FLAG_TRUNCATE;
1842108172Shsu				IFNET_RUNLOCK();
184378064Sume				return(copied);
184478064Sume			}
184578064Sume
184678064Sume			/*
184778064Sume			 * Set the TTL of the address.
184878064Sume			 * The TTL value should be one of the following
184978064Sume			 * according to the specification:
185078064Sume			 *
185178064Sume			 * 1. The remaining lifetime of a DHCP lease on the
185278064Sume			 *    address, or
185378064Sume			 * 2. The remaining Valid Lifetime of a prefix from
185478064Sume			 *    which the address was derived through Stateless
185578064Sume			 *    Autoconfiguration.
185678064Sume			 *
185778064Sume			 * Note that we currently do not support stateful
185878064Sume			 * address configuration by DHCPv6, so the former
185978064Sume			 * case can't happen.
186078064Sume			 */
186178064Sume			if (ifa6->ia6_lifetime.ia6t_expire == 0)
186278064Sume				ltime = ND6_INFINITE_LIFETIME;
186378064Sume			else {
186478064Sume				if (ifa6->ia6_lifetime.ia6t_expire >
186578064Sume				    time_second)
186678064Sume					ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
186778064Sume				else
186878064Sume					ltime = 0;
186978064Sume			}
187078064Sume
187178064Sume			bcopy(&ltime, cp, sizeof(u_int32_t));
187278064Sume			cp += sizeof(u_int32_t);
187378064Sume
187478064Sume			/* copy the address itself */
187578064Sume			bcopy(&ifa6->ia_addr.sin6_addr, cp,
187678064Sume			      sizeof(struct in6_addr));
187778064Sume			/* XXX: KAME link-local hack; remove ifindex */
187878064Sume			if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
187978064Sume				((struct in6_addr *)cp)->s6_addr16[1] = 0;
188078064Sume			cp += sizeof(struct in6_addr);
188178064Sume
188278064Sume			resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
188378064Sume			copied += (sizeof(struct in6_addr) +
188478064Sume				   sizeof(u_int32_t));
188553541Sshin		}
188653541Sshin		if (ifp0)	/* we need search only on the specified IF */
188753541Sshin			break;
188853541Sshin	}
188953541Sshin
189078064Sume	if (allow_deprecated == 0 && ifp_dep != NULL) {
189178064Sume		ifp = ifp_dep;
189278064Sume		allow_deprecated = 1;
189378064Sume
189478064Sume		goto again;
189578064Sume	}
189678064Sume
1897108172Shsu	IFNET_RUNLOCK();
1898108172Shsu
189953541Sshin	return(copied);
190053541Sshin}
190153541Sshin
190253541Sshin/*
190353541Sshin * XXX almost dup'ed code with rip6_input.
190453541Sshin */
190553541Sshinstatic int
190653541Sshinicmp6_rip6_input(mp, off)
190753541Sshin	struct	mbuf **mp;
190853541Sshin	int	off;
190953541Sshin{
191053541Sshin	struct mbuf *m = *mp;
191178064Sume	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
191278064Sume	struct in6pcb *in6p;
191353541Sshin	struct in6pcb *last = NULL;
191453541Sshin	struct sockaddr_in6 rip6src;
191553541Sshin	struct icmp6_hdr *icmp6;
191653541Sshin	struct mbuf *opts = NULL;
191753541Sshin
191862587Sitojun#ifndef PULLDOWN_TEST
191953541Sshin	/* this is assumed to be safe. */
192053541Sshin	icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
192162587Sitojun#else
192262587Sitojun	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
192362587Sitojun	if (icmp6 == NULL) {
192462587Sitojun		/* m is already reclaimed */
192562587Sitojun		return IPPROTO_DONE;
192662587Sitojun	}
192762587Sitojun#endif
192853541Sshin
192953541Sshin	bzero(&rip6src, sizeof(rip6src));
193053541Sshin	rip6src.sin6_len = sizeof(struct sockaddr_in6);
193153541Sshin	rip6src.sin6_family = AF_INET6;
193262587Sitojun	/* KAME hack: recover scopeid */
193362587Sitojun	(void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
193453541Sshin
193553541Sshin	LIST_FOREACH(in6p, &ripcb, inp_list)
193653541Sshin	{
193778064Sume		if ((in6p->inp_vflag & INP_IPV6) == 0)
193853541Sshin			continue;
193978064Sume#ifdef HAVE_NRL_INPCB
194078064Sume		if (!(in6p->in6p_flags & INP_IPV6))
194178064Sume			continue;
194278064Sume#endif
194353541Sshin		if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
194453541Sshin			continue;
194553541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
194653541Sshin		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
194753541Sshin			continue;
194853541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
194953541Sshin		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
195053541Sshin			continue;
195153541Sshin		if (in6p->in6p_icmp6filt
195253541Sshin		    && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
195353541Sshin				 in6p->in6p_icmp6filt))
195453541Sshin			continue;
195553541Sshin		if (last) {
195653541Sshin			struct	mbuf *n;
195753541Sshin			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
195853541Sshin				if (last->in6p_flags & IN6P_CONTROLOPTS)
195953541Sshin					ip6_savecontrol(last, &opts, ip6, n);
196053541Sshin				/* strip intermediate headers */
196153541Sshin				m_adj(n, off);
196253541Sshin				if (sbappendaddr(&last->in6p_socket->so_rcv,
196353541Sshin						 (struct sockaddr *)&rip6src,
196453541Sshin						 n, opts) == 0) {
196553541Sshin					/* should notify about lost packet */
196653541Sshin					m_freem(n);
196778064Sume					if (opts) {
196853541Sshin						m_freem(opts);
196978064Sume					}
197097658Stanimura				} else
197153541Sshin					sorwakeup(last->in6p_socket);
197253541Sshin				opts = NULL;
197353541Sshin			}
197453541Sshin		}
197553541Sshin		last = in6p;
197653541Sshin	}
197753541Sshin	if (last) {
197853541Sshin		if (last->in6p_flags & IN6P_CONTROLOPTS)
197953541Sshin			ip6_savecontrol(last, &opts, ip6, m);
198053541Sshin		/* strip intermediate headers */
198153541Sshin		m_adj(m, off);
198253541Sshin		if (sbappendaddr(&last->in6p_socket->so_rcv,
198378064Sume				 (struct sockaddr *)&rip6src, m, opts) == 0) {
198453541Sshin			m_freem(m);
198553541Sshin			if (opts)
198653541Sshin				m_freem(opts);
198797658Stanimura		} else
198853541Sshin			sorwakeup(last->in6p_socket);
198953541Sshin	} else {
199053541Sshin		m_freem(m);
199153541Sshin		ip6stat.ip6s_delivered--;
199253541Sshin	}
199353541Sshin	return IPPROTO_DONE;
199453541Sshin}
199553541Sshin
199653541Sshin/*
199753541Sshin * Reflect the ip6 packet back to the source.
199862587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf.
199953541Sshin */
200053541Sshinvoid
200153541Sshinicmp6_reflect(m, off)
200253541Sshin	struct	mbuf *m;
200353541Sshin	size_t off;
200453541Sshin{
200562587Sitojun	struct ip6_hdr *ip6;
200653541Sshin	struct icmp6_hdr *icmp6;
200753541Sshin	struct in6_ifaddr *ia;
200853541Sshin	struct in6_addr t, *src = 0;
200962587Sitojun	int plen;
201053541Sshin	int type, code;
201153541Sshin	struct ifnet *outif = NULL;
201278064Sume	struct sockaddr_in6 sa6_src, sa6_dst;
201353541Sshin#ifdef COMPAT_RFC1885
201453541Sshin	int mtu = IPV6_MMTU;
201553541Sshin	struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst;
201653541Sshin#endif
201753541Sshin
201862587Sitojun	/* too short to reflect */
201962587Sitojun	if (off < sizeof(struct ip6_hdr)) {
202078064Sume		nd6log((LOG_DEBUG,
202178064Sume		    "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
202278064Sume		    (u_long)off, (u_long)sizeof(struct ip6_hdr),
202378064Sume		    __FILE__, __LINE__));
202462587Sitojun		goto bad;
202562587Sitojun	}
202662587Sitojun
202753541Sshin	/*
202853541Sshin	 * If there are extra headers between IPv6 and ICMPv6, strip
202953541Sshin	 * off that header first.
203053541Sshin	 */
203162587Sitojun#ifdef DIAGNOSTIC
203262587Sitojun	if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
203362587Sitojun		panic("assumption failed in icmp6_reflect");
203462587Sitojun#endif
203562587Sitojun	if (off > sizeof(struct ip6_hdr)) {
203662587Sitojun		size_t l;
203762587Sitojun		struct ip6_hdr nip6;
203853541Sshin
203962587Sitojun		l = off - sizeof(struct ip6_hdr);
204062587Sitojun		m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
204162587Sitojun		m_adj(m, l);
204262587Sitojun		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
204362587Sitojun		if (m->m_len < l) {
204462587Sitojun			if ((m = m_pullup(m, l)) == NULL)
204562587Sitojun				return;
204653541Sshin		}
204762587Sitojun		bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
204862587Sitojun	} else /* off == sizeof(struct ip6_hdr) */ {
204962587Sitojun		size_t l;
205062587Sitojun		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
205162587Sitojun		if (m->m_len < l) {
205262587Sitojun			if ((m = m_pullup(m, l)) == NULL)
205362587Sitojun				return;
205453541Sshin		}
205553541Sshin	}
205662587Sitojun	plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
205762587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
205862587Sitojun	ip6->ip6_nxt = IPPROTO_ICMPV6;
205953541Sshin	icmp6 = (struct icmp6_hdr *)(ip6 + 1);
206053541Sshin	type = icmp6->icmp6_type; /* keep type for statistics */
206153541Sshin	code = icmp6->icmp6_code; /* ditto. */
206253541Sshin
206353541Sshin	t = ip6->ip6_dst;
206453541Sshin	/*
206553541Sshin	 * ip6_input() drops a packet if its src is multicast.
206653541Sshin	 * So, the src is never multicast.
206753541Sshin	 */
206853541Sshin	ip6->ip6_dst = ip6->ip6_src;
206953541Sshin
207078064Sume	/*
207178064Sume	 * XXX: make sure to embed scope zone information, using
207278064Sume	 * already embedded IDs or the received interface (if any).
207378064Sume	 * Note that rcvif may be NULL.
207478064Sume	 * TODO: scoped routing case (XXX).
207578064Sume	 */
207678064Sume	bzero(&sa6_src, sizeof(sa6_src));
207778064Sume	sa6_src.sin6_family = AF_INET6;
207878064Sume	sa6_src.sin6_len = sizeof(sa6_src);
207978064Sume	sa6_src.sin6_addr = ip6->ip6_dst;
208078064Sume	in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
208178064Sume	in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL);
208278064Sume	bzero(&sa6_dst, sizeof(sa6_dst));
208378064Sume	sa6_dst.sin6_family = AF_INET6;
208478064Sume	sa6_dst.sin6_len = sizeof(sa6_dst);
208578064Sume	sa6_dst.sin6_addr = t;
208678064Sume	in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
208778064Sume	in6_embedscope(&t, &sa6_dst, NULL, NULL);
208853541Sshin
208953541Sshin#ifdef COMPAT_RFC1885
209053541Sshin	/*
209153541Sshin	 * xxx guess MTU
209253541Sshin	 * RFC 1885 requires that echo reply should be truncated if it
209353541Sshin	 * does not fit in with (return) path MTU, but the description was
209453541Sshin	 * removed in the new spec.
209553541Sshin	 */
209653541Sshin	if (icmp6_reflect_rt.ro_rt == 0 ||
209753541Sshin	    ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) {
209853541Sshin		if (icmp6_reflect_rt.ro_rt) {
209953541Sshin			RTFREE(icmp6_reflect_rt.ro_rt);
210053541Sshin			icmp6_reflect_rt.ro_rt = 0;
210153541Sshin		}
210253541Sshin		bzero(sin6, sizeof(*sin6));
210353541Sshin		sin6->sin6_family = PF_INET6;
210453541Sshin		sin6->sin6_len = sizeof(struct sockaddr_in6);
210553541Sshin		sin6->sin6_addr = ip6->ip6_dst;
210653541Sshin
210753541Sshin		rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt,
210853541Sshin			    RTF_PRCLONING);
210953541Sshin	}
211053541Sshin
211153541Sshin	if (icmp6_reflect_rt.ro_rt == 0)
211253541Sshin		goto bad;
211353541Sshin
211453541Sshin	if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST)
211553541Sshin	    && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu)
211653541Sshin		mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu;
211753541Sshin
211853541Sshin	if (mtu < m->m_pkthdr.len) {
211953541Sshin		plen -= (m->m_pkthdr.len - mtu);
212053541Sshin		m_adj(m, mtu - m->m_pkthdr.len);
212153541Sshin	}
212253541Sshin#endif
212353541Sshin	/*
212453541Sshin	 * If the incoming packet was addressed directly to us(i.e. unicast),
212553541Sshin	 * use dst as the src for the reply.
212662587Sitojun	 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
212762587Sitojun	 * (for example) when we encounter an error while forwarding procedure
212862587Sitojun	 * destined to a duplicated address of ours.
212953541Sshin	 */
213053541Sshin	for (ia = in6_ifaddr; ia; ia = ia->ia_next)
213153541Sshin		if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
213262587Sitojun		    (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
213353541Sshin			src = &t;
213453541Sshin			break;
213553541Sshin		}
213653541Sshin	if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
213753541Sshin		/*
213853541Sshin		 * This is the case if the dst is our link-local address
213995023Ssuz		 * and the sender is also ourselves.
214053541Sshin		 */
214153541Sshin		src = &t;
214253541Sshin	}
214353541Sshin
214478064Sume	if (src == 0) {
214578064Sume		int e;
214678064Sume		struct route_in6 ro;
214778064Sume
214853541Sshin		/*
214962587Sitojun		 * This case matches to multicasts, our anycast, or unicasts
215095023Ssuz		 * that we do not own.  Select a source address based on the
215178064Sume		 * source address of the erroneous packet.
215253541Sshin		 */
215378064Sume		bzero(&ro, sizeof(ro));
215478064Sume		src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e);
215578064Sume		if (ro.ro_rt)
215678064Sume			RTFREE(ro.ro_rt); /* XXX: we could use this */
215778064Sume		if (src == NULL) {
215878064Sume			nd6log((LOG_DEBUG,
215978064Sume			    "icmp6_reflect: source can't be determined: "
216078064Sume			    "dst=%s, error=%d\n",
216178064Sume			    ip6_sprintf(&sa6_src.sin6_addr), e));
216278064Sume			goto bad;
216378064Sume		}
216478064Sume	}
216553541Sshin
216653541Sshin	ip6->ip6_src = *src;
216753541Sshin
216853541Sshin	ip6->ip6_flow = 0;
216962587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
217062587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
217153541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
217253541Sshin	if (m->m_pkthdr.rcvif) {
217353541Sshin		/* XXX: This may not be the outgoing interface */
217453541Sshin		ip6->ip6_hlim = nd_ifinfo[m->m_pkthdr.rcvif->if_index].chlim;
217578064Sume	} else
217678064Sume		ip6->ip6_hlim = ip6_defhlim;
217753541Sshin
217853541Sshin	icmp6->icmp6_cksum = 0;
217953541Sshin	icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
218053541Sshin					sizeof(struct ip6_hdr), plen);
218153541Sshin
218253541Sshin	/*
218378064Sume	 * XXX option handling
218453541Sshin	 */
218553541Sshin
218653541Sshin	m->m_flags &= ~(M_BCAST|M_MCAST);
218753541Sshin
218853541Sshin#ifdef COMPAT_RFC1885
2189105194Ssam	ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL);
219053541Sshin#else
2191105194Ssam	ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
219253541Sshin#endif
219353541Sshin	if (outif)
219453541Sshin		icmp6_ifoutstat_inc(outif, type, code);
219553541Sshin
219653541Sshin	return;
219753541Sshin
219853541Sshin bad:
219953541Sshin	m_freem(m);
220053541Sshin	return;
220153541Sshin}
220253541Sshin
220353541Sshinvoid
220453541Sshinicmp6_fasttimo()
220553541Sshin{
220662587Sitojun
220753541Sshin	mld6_fasttimeo();
220853541Sshin}
220953541Sshin
221053541Sshinstatic const char *
221153541Sshinicmp6_redirect_diag(src6, dst6, tgt6)
221253541Sshin	struct in6_addr *src6;
221353541Sshin	struct in6_addr *dst6;
221453541Sshin	struct in6_addr *tgt6;
221553541Sshin{
221653541Sshin	static char buf[1024];
221753541Sshin	snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
221853541Sshin		ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
221953541Sshin	return buf;
222053541Sshin}
222153541Sshin
222253541Sshinvoid
222353541Sshinicmp6_redirect_input(m, off)
222478064Sume	struct mbuf *m;
222553541Sshin	int off;
222653541Sshin{
222753541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
222853541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
222962587Sitojun	struct nd_redirect *nd_rd;
223053541Sshin	int icmp6len = ntohs(ip6->ip6_plen);
223153541Sshin	char *lladdr = NULL;
223253541Sshin	int lladdrlen = 0;
223353541Sshin	u_char *redirhdr = NULL;
223453541Sshin	int redirhdrlen = 0;
223553541Sshin	struct rtentry *rt = NULL;
223653541Sshin	int is_router;
223753541Sshin	int is_onlink;
223853541Sshin	struct in6_addr src6 = ip6->ip6_src;
223962587Sitojun	struct in6_addr redtgt6;
224062587Sitojun	struct in6_addr reddst6;
224153541Sshin	union nd_opts ndopts;
224253541Sshin
224353541Sshin	if (!m || !ifp)
224453541Sshin		return;
224553541Sshin
224653541Sshin	/* XXX if we are router, we don't update route by icmp6 redirect */
224753541Sshin	if (ip6_forwarding)
224862587Sitojun		goto freeit;
224953541Sshin	if (!icmp6_rediraccept)
225062587Sitojun		goto freeit;
225162587Sitojun
225262587Sitojun#ifndef PULLDOWN_TEST
225362587Sitojun	IP6_EXTHDR_CHECK(m, off, icmp6len,);
225462587Sitojun	nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
225562587Sitojun#else
225662587Sitojun	IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
225762587Sitojun	if (nd_rd == NULL) {
225862587Sitojun		icmp6stat.icp6s_tooshort++;
225953541Sshin		return;
226062587Sitojun	}
226162587Sitojun#endif
226262587Sitojun	redtgt6 = nd_rd->nd_rd_target;
226362587Sitojun	reddst6 = nd_rd->nd_rd_dst;
226453541Sshin
226553541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
226653541Sshin		redtgt6.s6_addr16[1] = htons(ifp->if_index);
226753541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
226853541Sshin		reddst6.s6_addr16[1] = htons(ifp->if_index);
226953541Sshin
227053541Sshin	/* validation */
227153541Sshin	if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
227278064Sume		nd6log((LOG_ERR,
227353541Sshin			"ICMP6 redirect sent from %s rejected; "
227478064Sume			"must be from linklocal\n", ip6_sprintf(&src6)));
227578064Sume		goto bad;
227653541Sshin	}
227753541Sshin	if (ip6->ip6_hlim != 255) {
227878064Sume		nd6log((LOG_ERR,
227953541Sshin			"ICMP6 redirect sent from %s rejected; "
228053541Sshin			"hlim=%d (must be 255)\n",
228178064Sume			ip6_sprintf(&src6), ip6->ip6_hlim));
228278064Sume		goto bad;
228353541Sshin	}
228453541Sshin    {
228553541Sshin	/* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
228653541Sshin	struct sockaddr_in6 sin6;
228753541Sshin	struct in6_addr *gw6;
228853541Sshin
228953541Sshin	bzero(&sin6, sizeof(sin6));
229053541Sshin	sin6.sin6_family = AF_INET6;
229153541Sshin	sin6.sin6_len = sizeof(struct sockaddr_in6);
229253541Sshin	bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
229353541Sshin	rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
229453541Sshin	if (rt) {
229565895Sume		if (rt->rt_gateway == NULL ||
229665895Sume		    rt->rt_gateway->sa_family != AF_INET6) {
229778064Sume			nd6log((LOG_ERR,
229865895Sume			    "ICMP6 redirect rejected; no route "
229965895Sume			    "with inet6 gateway found for redirect dst: %s\n",
230078064Sume			    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
230165895Sume			RTFREE(rt);
230278064Sume			goto bad;
230365895Sume		}
230465895Sume
230553541Sshin		gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
230653541Sshin		if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
230778064Sume			nd6log((LOG_ERR,
230853541Sshin				"ICMP6 redirect rejected; "
230953541Sshin				"not equal to gw-for-src=%s (must be same): "
231053541Sshin				"%s\n",
231153541Sshin				ip6_sprintf(gw6),
231278064Sume				icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
231353541Sshin			RTFREE(rt);
231478064Sume			goto bad;
231553541Sshin		}
231653541Sshin	} else {
231778064Sume		nd6log((LOG_ERR,
231853541Sshin			"ICMP6 redirect rejected; "
231953541Sshin			"no route found for redirect dst: %s\n",
232078064Sume			icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
232178064Sume		goto bad;
232253541Sshin	}
232353541Sshin	RTFREE(rt);
232453541Sshin	rt = NULL;
232553541Sshin    }
232653541Sshin	if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
232778064Sume		nd6log((LOG_ERR,
232853541Sshin			"ICMP6 redirect rejected; "
232953541Sshin			"redirect dst must be unicast: %s\n",
233078064Sume			icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
233178064Sume		goto bad;
233253541Sshin	}
233353541Sshin
233453541Sshin	is_router = is_onlink = 0;
233553541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
233653541Sshin		is_router = 1;	/* router case */
233753541Sshin	if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
233853541Sshin		is_onlink = 1;	/* on-link destination case */
233953541Sshin	if (!is_router && !is_onlink) {
234078064Sume		nd6log((LOG_ERR,
234153541Sshin			"ICMP6 redirect rejected; "
234253541Sshin			"neither router case nor onlink case: %s\n",
234378064Sume			icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
234478064Sume		goto bad;
234553541Sshin	}
234653541Sshin	/* validation passed */
234753541Sshin
234853541Sshin	icmp6len -= sizeof(*nd_rd);
234953541Sshin	nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
235053541Sshin	if (nd6_options(&ndopts) < 0) {
235178064Sume		nd6log((LOG_INFO, "icmp6_redirect_input: "
235253541Sshin			"invalid ND option, rejected: %s\n",
235378064Sume			icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
235478064Sume		/* nd6_options have incremented stats */
235562587Sitojun		goto freeit;
235653541Sshin	}
235753541Sshin
235853541Sshin	if (ndopts.nd_opts_tgt_lladdr) {
235953541Sshin		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
236053541Sshin		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
236153541Sshin	}
236253541Sshin
236353541Sshin	if (ndopts.nd_opts_rh) {
236453541Sshin		redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
236553541Sshin		redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
236653541Sshin	}
236753541Sshin
236853541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
236978064Sume		nd6log((LOG_INFO,
237053541Sshin			"icmp6_redirect_input: lladdrlen mismatch for %s "
237153541Sshin			"(if %d, icmp6 packet %d): %s\n",
237253541Sshin			ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
237378064Sume			icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
237478064Sume		goto bad;
237553541Sshin	}
237653541Sshin
237753541Sshin	/* RFC 2461 8.3 */
237853541Sshin	nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
237953541Sshin			 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
238053541Sshin
238195023Ssuz	if (!is_onlink) {	/* better router case.  perform rtredirect. */
238253541Sshin		/* perform rtredirect */
238353541Sshin		struct sockaddr_in6 sdst;
238453541Sshin		struct sockaddr_in6 sgw;
238553541Sshin		struct sockaddr_in6 ssrc;
238653541Sshin
238753541Sshin		bzero(&sdst, sizeof(sdst));
238853541Sshin		bzero(&sgw, sizeof(sgw));
238953541Sshin		bzero(&ssrc, sizeof(ssrc));
239053541Sshin		sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
239153541Sshin		sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
239253541Sshin			sizeof(struct sockaddr_in6);
239353541Sshin		bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
239453541Sshin		bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
239553541Sshin		bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
239653541Sshin		rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
239753541Sshin			   (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
239853541Sshin			   (struct sockaddr *)&ssrc,
239953541Sshin			   (struct rtentry **)NULL);
240053541Sshin	}
240153541Sshin	/* finally update cached route in each socket via pfctlinput */
240253541Sshin    {
240353541Sshin	struct sockaddr_in6 sdst;
240453541Sshin
240553541Sshin	bzero(&sdst, sizeof(sdst));
240653541Sshin	sdst.sin6_family = AF_INET6;
240753541Sshin	sdst.sin6_len = sizeof(struct sockaddr_in6);
240853541Sshin	bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
240953541Sshin	pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
241053541Sshin#ifdef IPSEC
241153541Sshin	key_sa_routechange((struct sockaddr *)&sdst);
241253541Sshin#endif
241353541Sshin    }
241462587Sitojun
241562587Sitojun freeit:
241662587Sitojun	m_freem(m);
241778064Sume	return;
241878064Sume
241978064Sume bad:
242078064Sume	icmp6stat.icp6s_badredirect++;
242178064Sume	m_freem(m);
242253541Sshin}
242353541Sshin
242453541Sshinvoid
242553541Sshinicmp6_redirect_output(m0, rt)
242653541Sshin	struct mbuf *m0;
242753541Sshin	struct rtentry *rt;
242853541Sshin{
242953541Sshin	struct ifnet *ifp;	/* my outgoing interface */
243053541Sshin	struct in6_addr *ifp_ll6;
243153541Sshin	struct in6_addr *router_ll6;
243253541Sshin	struct ip6_hdr *sip6;	/* m0 as struct ip6_hdr */
243353541Sshin	struct mbuf *m = NULL;	/* newly allocated one */
243453541Sshin	struct ip6_hdr *ip6;	/* m as struct ip6_hdr */
243553541Sshin	struct nd_redirect *nd_rd;
243653541Sshin	size_t maxlen;
243753541Sshin	u_char *p;
243853541Sshin	struct ifnet *outif = NULL;
243962587Sitojun	struct sockaddr_in6 src_sa;
244053541Sshin
244162587Sitojun	icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
244262587Sitojun
244353541Sshin	/* if we are not router, we don't send icmp6 redirect */
244453541Sshin	if (!ip6_forwarding || ip6_accept_rtadv)
244553541Sshin		goto fail;
244653541Sshin
244753541Sshin	/* sanity check */
244853541Sshin	if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
244953541Sshin		goto fail;
245053541Sshin
245153541Sshin	/*
245253541Sshin	 * Address check:
245353541Sshin	 *  the source address must identify a neighbor, and
245453541Sshin	 *  the destination address must not be a multicast address
245553541Sshin	 *  [RFC 2461, sec 8.2]
245653541Sshin	 */
245753541Sshin	sip6 = mtod(m0, struct ip6_hdr *);
245862587Sitojun	bzero(&src_sa, sizeof(src_sa));
245962587Sitojun	src_sa.sin6_family = AF_INET6;
246062587Sitojun	src_sa.sin6_len = sizeof(src_sa);
246162587Sitojun	src_sa.sin6_addr = sip6->ip6_src;
246262587Sitojun	/* we don't currently use sin6_scope_id, but eventually use it */
246362587Sitojun	src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src);
246462587Sitojun	if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
246553541Sshin		goto fail;
246653541Sshin	if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
246753541Sshin		goto fail;	/* what should we do here? */
246853541Sshin
246953541Sshin	/* rate limit */
247053541Sshin	if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
247153541Sshin		goto fail;
247253541Sshin
247353541Sshin	/*
247453541Sshin	 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
247553541Sshin	 * we almost always ask for an mbuf cluster for simplicity.
247653541Sshin	 * (MHLEN < IPV6_MMTU is almost always true)
247753541Sshin	 */
247862587Sitojun#if IPV6_MMTU >= MCLBYTES
247962587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES
248062587Sitojun#endif
2481111119Simp	MGETHDR(m, M_DONTWAIT, MT_HEADER);
248262587Sitojun	if (m && IPV6_MMTU >= MHLEN)
2483111119Simp		MCLGET(m, M_DONTWAIT);
248453541Sshin	if (!m)
248553541Sshin		goto fail;
248678064Sume	m->m_pkthdr.rcvif = NULL;
248778064Sume	m->m_len = 0;
248878064Sume	maxlen = M_TRAILINGSPACE(m);
248953541Sshin	maxlen = min(IPV6_MMTU, maxlen);
249053541Sshin	/* just for safety */
249162587Sitojun	if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
249262587Sitojun	    ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
249353541Sshin		goto fail;
249462587Sitojun	}
249553541Sshin
249653541Sshin	{
249753541Sshin		/* get ip6 linklocal address for ifp(my outgoing interface). */
249862587Sitojun		struct in6_ifaddr *ia;
249962587Sitojun		if ((ia = in6ifa_ifpforlinklocal(ifp,
250062587Sitojun						 IN6_IFF_NOTREADY|
250162587Sitojun						 IN6_IFF_ANYCAST)) == NULL)
250253541Sshin			goto fail;
250353541Sshin		ifp_ll6 = &ia->ia_addr.sin6_addr;
250453541Sshin	}
250553541Sshin
250653541Sshin	/* get ip6 linklocal address for the router. */
250753541Sshin	if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
250853541Sshin		struct sockaddr_in6 *sin6;
250953541Sshin		sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
251053541Sshin		router_ll6 = &sin6->sin6_addr;
251153541Sshin		if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
251253541Sshin			router_ll6 = (struct in6_addr *)NULL;
251353541Sshin	} else
251453541Sshin		router_ll6 = (struct in6_addr *)NULL;
251553541Sshin
251653541Sshin	/* ip6 */
251753541Sshin	ip6 = mtod(m, struct ip6_hdr *);
251853541Sshin	ip6->ip6_flow = 0;
251962587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
252062587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
252153541Sshin	/* ip6->ip6_plen will be set later */
252253541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
252353541Sshin	ip6->ip6_hlim = 255;
252453541Sshin	/* ip6->ip6_src must be linklocal addr for my outgoing if. */
252553541Sshin	bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
252653541Sshin	bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
252753541Sshin
252853541Sshin	/* ND Redirect */
252953541Sshin	nd_rd = (struct nd_redirect *)(ip6 + 1);
253053541Sshin	nd_rd->nd_rd_type = ND_REDIRECT;
253153541Sshin	nd_rd->nd_rd_code = 0;
253253541Sshin	nd_rd->nd_rd_reserved = 0;
253353541Sshin	if (rt->rt_flags & RTF_GATEWAY) {
253453541Sshin		/*
253553541Sshin		 * nd_rd->nd_rd_target must be a link-local address in
253653541Sshin		 * better router cases.
253753541Sshin		 */
253853541Sshin		if (!router_ll6)
253953541Sshin			goto fail;
254053541Sshin		bcopy(router_ll6, &nd_rd->nd_rd_target,
254153541Sshin		      sizeof(nd_rd->nd_rd_target));
254253541Sshin		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
254353541Sshin		      sizeof(nd_rd->nd_rd_dst));
254453541Sshin	} else {
254553541Sshin		/* make sure redtgt == reddst */
254653541Sshin		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
254753541Sshin		      sizeof(nd_rd->nd_rd_target));
254853541Sshin		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
254953541Sshin		      sizeof(nd_rd->nd_rd_dst));
255053541Sshin	}
255153541Sshin
255253541Sshin	p = (u_char *)(nd_rd + 1);
255353541Sshin
255453541Sshin	if (!router_ll6)
255553541Sshin		goto nolladdropt;
255653541Sshin
255753541Sshin    {
255853541Sshin	/* target lladdr option */
255953541Sshin	struct rtentry *rt_router = NULL;
256053541Sshin	int len;
256153541Sshin	struct sockaddr_dl *sdl;
256253541Sshin	struct nd_opt_hdr *nd_opt;
256353541Sshin	char *lladdr;
256453541Sshin
256553541Sshin	rt_router = nd6_lookup(router_ll6, 0, ifp);
256653541Sshin	if (!rt_router)
256753541Sshin		goto nolladdropt;
256862587Sitojun	len = sizeof(*nd_opt) + ifp->if_addrlen;
256995023Ssuz	len = (len + 7) & ~7;	/* round by 8 */
257062587Sitojun	/* safety check */
257162587Sitojun	if (len + (p - (u_char *)ip6) > maxlen)
257262587Sitojun		goto nolladdropt;
257362587Sitojun	if (!(rt_router->rt_flags & RTF_GATEWAY) &&
257462587Sitojun	    (rt_router->rt_flags & RTF_LLINFO) &&
257562587Sitojun	    (rt_router->rt_gateway->sa_family == AF_LINK) &&
257662587Sitojun	    (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
257762587Sitojun	    sdl->sdl_alen) {
257853541Sshin		nd_opt = (struct nd_opt_hdr *)p;
257953541Sshin		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
258053541Sshin		nd_opt->nd_opt_len = len >> 3;
258153541Sshin		lladdr = (char *)(nd_opt + 1);
258253541Sshin		bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
258362587Sitojun		p += len;
258453541Sshin	}
258553541Sshin    }
258653541Sshinnolladdropt:;
258753541Sshin
258853541Sshin	m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
258953541Sshin
259053541Sshin	/* just to be safe */
259162587Sitojun#ifdef M_DECRYPTED	/*not openbsd*/
259253541Sshin	if (m0->m_flags & M_DECRYPTED)
259353541Sshin		goto noredhdropt;
259462587Sitojun#endif
259562587Sitojun	if (p - (u_char *)ip6 > maxlen)
259662587Sitojun		goto noredhdropt;
259753541Sshin
259853541Sshin    {
259953541Sshin	/* redirected header option */
260053541Sshin	int len;
260153541Sshin	struct nd_opt_rd_hdr *nd_opt_rh;
260253541Sshin
260353541Sshin	/*
260453541Sshin	 * compute the maximum size for icmp6 redirect header option.
260553541Sshin	 * XXX room for auth header?
260653541Sshin	 */
260753541Sshin	len = maxlen - (p - (u_char *)ip6);
260853541Sshin	len &= ~7;
260953541Sshin
261053541Sshin	/* This is just for simplicity. */
261153541Sshin	if (m0->m_pkthdr.len != m0->m_len) {
261253541Sshin		if (m0->m_next) {
261353541Sshin			m_freem(m0->m_next);
261453541Sshin			m0->m_next = NULL;
261553541Sshin		}
261653541Sshin		m0->m_pkthdr.len = m0->m_len;
261753541Sshin	}
261853541Sshin
261953541Sshin	/*
262053541Sshin	 * Redirected header option spec (RFC2461 4.6.3) talks nothing
262153541Sshin	 * about padding/truncate rule for the original IP packet.
262253541Sshin	 * From the discussion on IPv6imp in Feb 1999, the consensus was:
262353541Sshin	 * - "attach as much as possible" is the goal
262453541Sshin	 * - pad if not aligned (original size can be guessed by original
262553541Sshin	 *   ip6 header)
262653541Sshin	 * Following code adds the padding if it is simple enough,
262753541Sshin	 * and truncates if not.
262853541Sshin	 */
262953541Sshin	if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
263053541Sshin		panic("assumption failed in %s:%d\n", __FILE__, __LINE__);
263153541Sshin
263253541Sshin	if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
263353541Sshin		/* not enough room, truncate */
263453541Sshin		m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
263553541Sshin	} else {
263653541Sshin		/* enough room, pad or truncate */
263753541Sshin		size_t extra;
263853541Sshin
263953541Sshin		extra = m0->m_pkthdr.len % 8;
264053541Sshin		if (extra) {
264153541Sshin			/* pad if easy enough, truncate if not */
264253541Sshin			if (8 - extra <= M_TRAILINGSPACE(m0)) {
264353541Sshin				/* pad */
264453541Sshin				m0->m_len += (8 - extra);
264553541Sshin				m0->m_pkthdr.len += (8 - extra);
264653541Sshin			} else {
264753541Sshin				/* truncate */
264853541Sshin				m0->m_pkthdr.len -= extra;
264953541Sshin				m0->m_len -= extra;
265053541Sshin			}
265153541Sshin		}
265253541Sshin		len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
265353541Sshin		m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
265453541Sshin	}
265553541Sshin
265653541Sshin	nd_opt_rh = (struct nd_opt_rd_hdr *)p;
265753541Sshin	bzero(nd_opt_rh, sizeof(*nd_opt_rh));
265853541Sshin	nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
265953541Sshin	nd_opt_rh->nd_opt_rh_len = len >> 3;
266053541Sshin	p += sizeof(*nd_opt_rh);
266153541Sshin	m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
266253541Sshin
266353541Sshin	/* connect m0 to m */
2664112781Ssuz	m_tag_delete_chain(m0, NULL);
2665112781Ssuz	m0->m_flags &= ~M_PKTHDR;
266653541Sshin	m->m_next = m0;
266753541Sshin	m->m_pkthdr.len = m->m_len + m0->m_len;
2668112781Ssuz	m0 = NULL;
266953541Sshin    }
267053541Sshinnoredhdropt:;
2671112781Ssuz	if (m0) {
2672112781Ssuz		m_freem(m0);
2673112781Ssuz		m0 = NULL;
2674112781Ssuz	}
267553541Sshin
267653541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
267753541Sshin		sip6->ip6_src.s6_addr16[1] = 0;
267853541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
267953541Sshin		sip6->ip6_dst.s6_addr16[1] = 0;
268062587Sitojun#if 0
268162587Sitojun	if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
268262587Sitojun		ip6->ip6_src.s6_addr16[1] = 0;
268362587Sitojun	if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
268462587Sitojun		ip6->ip6_dst.s6_addr16[1] = 0;
268562587Sitojun#endif
268653541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
268753541Sshin		nd_rd->nd_rd_target.s6_addr16[1] = 0;
268853541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
268953541Sshin		nd_rd->nd_rd_dst.s6_addr16[1] = 0;
269053541Sshin
269153541Sshin	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
269253541Sshin
269353541Sshin	nd_rd->nd_rd_cksum = 0;
269453541Sshin	nd_rd->nd_rd_cksum
269553541Sshin		= in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
269653541Sshin
269753541Sshin	/* send the packet to outside... */
2698105194Ssam	ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
269953541Sshin	if (outif) {
270053541Sshin		icmp6_ifstat_inc(outif, ifs6_out_msg);
270153541Sshin		icmp6_ifstat_inc(outif, ifs6_out_redirect);
270253541Sshin	}
270353541Sshin	icmp6stat.icp6s_outhist[ND_REDIRECT]++;
270453541Sshin
270553541Sshin	return;
270653541Sshin
270753541Sshinfail:
270853541Sshin	if (m)
270953541Sshin		m_freem(m);
271053541Sshin	if (m0)
271153541Sshin		m_freem(m0);
271253541Sshin}
271353541Sshin
271478064Sume#ifdef HAVE_NRL_INPCB
271578064Sume#define sotoin6pcb	sotoinpcb
271678064Sume#define in6pcb		inpcb
271778064Sume#define in6p_icmp6filt	inp_icmp6filt
271878064Sume#endif
271953541Sshin/*
272053541Sshin * ICMPv6 socket option processing.
272153541Sshin */
272253541Sshinint
272353541Sshinicmp6_ctloutput(so, sopt)
272453541Sshin	struct socket *so;
272553541Sshin	struct sockopt *sopt;
272653541Sshin{
272753541Sshin	int error = 0;
272853541Sshin	int optlen;
272978064Sume	struct inpcb *inp = sotoinpcb(so);
273053541Sshin	int level, op, optname;
273153541Sshin
273253541Sshin	if (sopt) {
273353541Sshin		level = sopt->sopt_level;
273453541Sshin		op = sopt->sopt_dir;
273553541Sshin		optname = sopt->sopt_name;
273653541Sshin		optlen = sopt->sopt_valsize;
273753541Sshin	} else
273853541Sshin		level = op = optname = optlen = 0;
273978064Sume
274053541Sshin	if (level != IPPROTO_ICMPV6) {
274153541Sshin		return EINVAL;
274253541Sshin	}
274353541Sshin
274478064Sume	switch (op) {
274553541Sshin	case PRCO_SETOPT:
274653541Sshin		switch (optname) {
274753541Sshin		case ICMP6_FILTER:
274853541Sshin		    {
274953541Sshin			struct icmp6_filter *p;
275053541Sshin
275153541Sshin			if (optlen != sizeof(*p)) {
275253541Sshin				error = EMSGSIZE;
275353541Sshin				break;
275453541Sshin			}
275553541Sshin			if (inp->in6p_icmp6filt == NULL) {
275653541Sshin				error = EINVAL;
275753541Sshin				break;
275853541Sshin			}
275953541Sshin			error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen,
276053541Sshin				optlen);
276153541Sshin			break;
276253541Sshin		    }
276353541Sshin
276453541Sshin		default:
276553541Sshin			error = ENOPROTOOPT;
276653541Sshin			break;
276753541Sshin		}
276853541Sshin		break;
276953541Sshin
277053541Sshin	case PRCO_GETOPT:
277153541Sshin		switch (optname) {
277253541Sshin		case ICMP6_FILTER:
277353541Sshin		    {
277453541Sshin			if (inp->in6p_icmp6filt == NULL) {
277553541Sshin				error = EINVAL;
277653541Sshin				break;
277753541Sshin			}
277853541Sshin			error = sooptcopyout(sopt, inp->in6p_icmp6filt,
277953541Sshin				sizeof(struct icmp6_filter));
278053541Sshin			break;
278153541Sshin		    }
278253541Sshin
278353541Sshin		default:
278453541Sshin			error = ENOPROTOOPT;
278553541Sshin			break;
278653541Sshin		}
278753541Sshin		break;
278853541Sshin	}
278953541Sshin
279053541Sshin	return(error);
279153541Sshin}
279278064Sume#ifdef HAVE_NRL_INPCB
279378064Sume#undef sotoin6pcb
279478064Sume#undef in6pcb
279578064Sume#undef in6p_icmp6filt
279678064Sume#endif
279753541Sshin
279878064Sume#ifndef HAVE_PPSRATECHECK
279978064Sume#ifndef timersub
280078064Sume#define	timersub(tvp, uvp, vvp)						\
280178064Sume	do {								\
280278064Sume		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
280378064Sume		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
280478064Sume		if ((vvp)->tv_usec < 0) {				\
280578064Sume			(vvp)->tv_sec--;				\
280678064Sume			(vvp)->tv_usec += 1000000;			\
280778064Sume		}							\
280878064Sume	} while (0)
280978064Sume#endif
281078064Sume
281153541Sshin/*
281278064Sume * ppsratecheck(): packets (or events) per second limitation.
281362587Sitojun */
281462587Sitojunstatic int
281578064Sumeppsratecheck(lasttime, curpps, maxpps)
281678064Sume	struct timeval *lasttime;
281778064Sume	int *curpps;
281878064Sume	int maxpps;	/* maximum pps allowed */
281962587Sitojun{
282078064Sume	struct timeval tv, delta;
282178064Sume	int s, rv;
282262587Sitojun
282378064Sume	s = splclock();
282478064Sume	microtime(&tv);
282578064Sume	splx(s);
282662587Sitojun
282778064Sume	timersub(&tv, lasttime, &delta);
282862587Sitojun
282978064Sume	/*
283095023Ssuz	 * Check for 0,0 so that the message will be seen at least once.
283195023Ssuz	 * If more than one second has passed since the last update of
283278064Sume	 * lasttime, reset the counter.
283378064Sume	 *
283495023Ssuz	 * We do increment *curpps even in *curpps < maxpps case, as some may
283578064Sume	 * try to use *curpps for stat purposes as well.
283678064Sume	 */
283778064Sume	if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
283878064Sume	    delta.tv_sec >= 1) {
283978064Sume		*lasttime = tv;
284078064Sume		*curpps = 0;
284178064Sume		rv = 1;
284278064Sume	} else if (maxpps < 0)
284378064Sume		rv = 1;
284478064Sume	else if (*curpps < maxpps)
284578064Sume		rv = 1;
284678064Sume	else
284778064Sume		rv = 0;
284862587Sitojun
284995023Ssuz#if 1 /* DIAGNOSTIC? */
285078064Sume	/* be careful about wrap-around */
285178064Sume	if (*curpps + 1 > *curpps)
285278064Sume		*curpps = *curpps + 1;
285378064Sume#else
285478064Sume	/*
285578064Sume	 * assume that there's not too many calls to this function.
285678064Sume	 * not sure if the assumption holds, as it depends on *caller's*
285778064Sume	 * behavior, not the behavior of this function.
285878064Sume	 * IMHO it is wrong to make assumption on the caller's behavior,
285978064Sume	 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
286078064Sume	 */
286178064Sume	*curpps = *curpps + 1;
286278064Sume#endif
286378064Sume
286478064Sume	return (rv);
286562587Sitojun}
286662587Sitojun#endif
286762587Sitojun
286862587Sitojun/*
286953541Sshin * Perform rate limit check.
287053541Sshin * Returns 0 if it is okay to send the icmp6 packet.
287153541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
287253541Sshin * limitation.
287353541Sshin *
287453541Sshin * XXX per-destination/type check necessary?
287553541Sshin */
287653541Sshinstatic int
287753541Sshinicmp6_ratelimit(dst, type, code)
287853541Sshin	const struct in6_addr *dst;	/* not used at this moment */
287953541Sshin	const int type;			/* not used at this moment */
288053541Sshin	const int code;			/* not used at this moment */
288153541Sshin{
288262587Sitojun	int ret;
288353541Sshin
288495023Ssuz	ret = 0;	/* okay to send */
288553541Sshin
288662587Sitojun	/* PPS limit */
288778064Sume	if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
288878064Sume	    icmp6errppslim)) {
288953541Sshin		/* The packet is subject to rate limit */
289062587Sitojun		ret++;
289153541Sshin	}
289253541Sshin
289362587Sitojun	return ret;
289453541Sshin}
2895