icmp6.c revision 120891
162587Sitojun/*	$FreeBSD: head/sys/netinet6/icmp6.c 120891 2003-10-07 17:46:18Z ume $	*/
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			/*
557120891Sume			 * 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			/*
592120891Sume			 * 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:
645120891Sume		/* XXX: these two are experimental.  not officially defined. */
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),
667120891Sume			    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
678120891Sume			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. */
851120856Sume			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;
877113799Sobrien	int off, icmp6len, code;
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,
890120891Sume	    sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1);
89178064Sume	icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
89262587Sitojun#else
89378064Sume	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
894120891Sume	    sizeof(*icmp6) + sizeof(struct ip6_hdr));
89578064Sume	if (icmp6 == NULL) {
89678064Sume		icmp6stat.icp6s_tooshort++;
897120856Sume		return (-1);
89878064Sume	}
89962587Sitojun#endif
90078064Sume	eip6 = (struct ip6_hdr *)(icmp6 + 1);
90153541Sshin
90278064Sume	/* Detect the upper level protocol */
90378064Sume	{
90453541Sshin		void (*ctlfunc) __P((int, struct sockaddr *, void *));
90553541Sshin		u_int8_t nxt = eip6->ip6_nxt;
90653541Sshin		int eoff = off + sizeof(struct icmp6_hdr) +
907120891Sume		    sizeof(struct ip6_hdr);
90853541Sshin		struct ip6ctlparam ip6cp;
90962587Sitojun		struct in6_addr *finaldst = NULL;
91062587Sitojun		int icmp6type = icmp6->icmp6_type;
91162587Sitojun		struct ip6_frag *fh;
91262587Sitojun		struct ip6_rthdr *rth;
91362587Sitojun		struct ip6_rthdr0 *rth0;
91462587Sitojun		int rthlen;
91553541Sshin
91695023Ssuz		while (1) { /* XXX: should avoid infinite loop explicitly? */
91753541Sshin			struct ip6_ext *eh;
91853541Sshin
91978064Sume			switch (nxt) {
92053541Sshin			case IPPROTO_HOPOPTS:
92153541Sshin			case IPPROTO_DSTOPTS:
92253541Sshin			case IPPROTO_AH:
92362587Sitojun#ifndef PULLDOWN_TEST
924120891Sume				IP6_EXTHDR_CHECK(m, 0,
925120891Sume				    eoff + sizeof(struct ip6_ext), -1);
92653541Sshin				eh = (struct ip6_ext *)(mtod(m, caddr_t)
92753541Sshin							+ eoff);
92862587Sitojun#else
92962587Sitojun				IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
930120891Sume				    eoff, sizeof(*eh));
93162587Sitojun				if (eh == NULL) {
93262587Sitojun					icmp6stat.icp6s_tooshort++;
933120856Sume					return (-1);
93462587Sitojun				}
93562587Sitojun#endif
936120891Sume
93753541Sshin				if (nxt == IPPROTO_AH)
93853541Sshin					eoff += (eh->ip6e_len + 2) << 2;
93953541Sshin				else
94053541Sshin					eoff += (eh->ip6e_len + 1) << 3;
94153541Sshin				nxt = eh->ip6e_nxt;
94253541Sshin				break;
94362587Sitojun			case IPPROTO_ROUTING:
94462587Sitojun				/*
94562587Sitojun				 * When the erroneous packet contains a
94662587Sitojun				 * routing header, we should examine the
94762587Sitojun				 * header to determine the final destination.
94862587Sitojun				 * Otherwise, we can't properly update
94962587Sitojun				 * information that depends on the final
95062587Sitojun				 * destination (e.g. path MTU).
95162587Sitojun				 */
95262587Sitojun#ifndef PULLDOWN_TEST
953120891Sume				IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1);
954120891Sume				rth = (struct ip6_rthdr *)
955120891Sume				    (mtod(m, caddr_t) + eoff);
95662587Sitojun#else
95762587Sitojun				IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
958120891Sume				    eoff, sizeof(*rth));
95962587Sitojun				if (rth == NULL) {
96062587Sitojun					icmp6stat.icp6s_tooshort++;
961120856Sume					return (-1);
96262587Sitojun				}
96362587Sitojun#endif
96462587Sitojun				rthlen = (rth->ip6r_len + 1) << 3;
96562587Sitojun				/*
96662587Sitojun				 * XXX: currently there is no
96762587Sitojun				 * officially defined type other
96862587Sitojun				 * than type-0.
96962587Sitojun				 * Note that if the segment left field
97062587Sitojun				 * is 0, all intermediate hops must
97162587Sitojun				 * have been passed.
97262587Sitojun				 */
97362587Sitojun				if (rth->ip6r_segleft &&
97462587Sitojun				    rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
97562587Sitojun					int hops;
97662587Sitojun
97762587Sitojun#ifndef PULLDOWN_TEST
978120891Sume					IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1);
979120891Sume					rth0 = (struct ip6_rthdr0 *)
980120891Sume					    (mtod(m, caddr_t) + eoff);
98162587Sitojun#else
98262587Sitojun					IP6_EXTHDR_GET(rth0,
983120891Sume					    struct ip6_rthdr0 *, m,
984120891Sume					    eoff, rthlen);
98562587Sitojun					if (rth0 == NULL) {
98662587Sitojun						icmp6stat.icp6s_tooshort++;
987120856Sume						return (-1);
98862587Sitojun					}
98962587Sitojun#endif
99062587Sitojun					/* just ignore a bogus header */
99162587Sitojun					if ((rth0->ip6r0_len % 2) == 0 &&
99262587Sitojun					    (hops = rth0->ip6r0_len/2))
99362587Sitojun						finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
99462587Sitojun				}
99562587Sitojun				eoff += rthlen;
99662587Sitojun				nxt = rth->ip6r_nxt;
99762587Sitojun				break;
99862587Sitojun			case IPPROTO_FRAGMENT:
99962587Sitojun#ifndef PULLDOWN_TEST
100062587Sitojun				IP6_EXTHDR_CHECK(m, 0, eoff +
1001120891Sume				    sizeof(struct ip6_frag), -1);
1002120891Sume				fh = (struct ip6_frag *)(mtod(m, caddr_t) +
1003120891Sume				    eoff);
100462587Sitojun#else
100562587Sitojun				IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
1006120891Sume				    eoff, sizeof(*fh));
100762587Sitojun				if (fh == NULL) {
100862587Sitojun					icmp6stat.icp6s_tooshort++;
1009120856Sume					return (-1);
101062587Sitojun				}
101162587Sitojun#endif
101262587Sitojun				/*
101362587Sitojun				 * Data after a fragment header is meaningless
101462587Sitojun				 * unless it is the first fragment, but
101562587Sitojun				 * we'll go to the notify label for path MTU
101662587Sitojun				 * discovery.
101762587Sitojun				 */
101862587Sitojun				if (fh->ip6f_offlg & IP6F_OFF_MASK)
101962587Sitojun					goto notify;
102062587Sitojun
102162587Sitojun				eoff += sizeof(struct ip6_frag);
102262587Sitojun				nxt = fh->ip6f_nxt;
102362587Sitojun				break;
102453541Sshin			default:
102562587Sitojun				/*
102662587Sitojun				 * This case includes ESP and the No Next
102795023Ssuz				 * Header.  In such cases going to the notify
102862587Sitojun				 * label does not have any meaning
102962587Sitojun				 * (i.e. ctlfunc will be NULL), but we go
103062587Sitojun				 * anyway since we might have to update
103162587Sitojun				 * path MTU information.
103262587Sitojun				 */
103353541Sshin				goto notify;
103453541Sshin			}
103553541Sshin		}
103678064Sume	  notify:
103762587Sitojun#ifndef PULLDOWN_TEST
103853541Sshin		icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
103962587Sitojun#else
104062587Sitojun		IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1041120891Sume		    sizeof(*icmp6) + sizeof(struct ip6_hdr));
104262587Sitojun		if (icmp6 == NULL) {
104362587Sitojun			icmp6stat.icp6s_tooshort++;
1044120856Sume			return (-1);
104562587Sitojun		}
104662587Sitojun#endif
104778064Sume
1048120891Sume		/*
1049120891Sume		 * retrieve parameters from the inner IPv6 header, and convert
1050120891Sume		 * them into sockaddr structures.
1051120891Sume		 * XXX: there is no guarantee that the source or destination
1052120891Sume		 * addresses of the inner packet are in the same scope as
1053120891Sume		 * the addresses of the icmp packet.  But there is no other
1054120891Sume		 * way to determine the zone.
1055120891Sume		 */
105678064Sume		eip6 = (struct ip6_hdr *)(icmp6 + 1);
1057120891Sume
105878064Sume		bzero(&icmp6dst, sizeof(icmp6dst));
105978064Sume		icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
106078064Sume		icmp6dst.sin6_family = AF_INET6;
106178064Sume		if (finaldst == NULL)
106278064Sume			icmp6dst.sin6_addr = eip6->ip6_dst;
106378064Sume		else
106478064Sume			icmp6dst.sin6_addr = *finaldst;
106578064Sume		icmp6dst.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
106678064Sume							  &icmp6dst.sin6_addr);
106778064Sume#ifndef SCOPEDROUTING
106878064Sume		if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst,
106978064Sume				   NULL, NULL)) {
107078064Sume			/* should be impossbile */
107178064Sume			nd6log((LOG_DEBUG,
107278064Sume			    "icmp6_notify_error: in6_embedscope failed\n"));
107378064Sume			goto freeit;
107478064Sume		}
107578064Sume#endif
107678064Sume
107778064Sume		/*
107878064Sume		 * retrieve parameters from the inner IPv6 header, and convert
107978064Sume		 * them into sockaddr structures.
108078064Sume		 */
108178064Sume		bzero(&icmp6src, sizeof(icmp6src));
108278064Sume		icmp6src.sin6_len = sizeof(struct sockaddr_in6);
108378064Sume		icmp6src.sin6_family = AF_INET6;
108478064Sume		icmp6src.sin6_addr = eip6->ip6_src;
108578064Sume		icmp6src.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
108678064Sume							  &icmp6src.sin6_addr);
108778064Sume#ifndef SCOPEDROUTING
108878064Sume		if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src,
108978064Sume				   NULL, NULL)) {
109078064Sume			/* should be impossbile */
109178064Sume			nd6log((LOG_DEBUG,
109278064Sume			    "icmp6_notify_error: in6_embedscope failed\n"));
109378064Sume			goto freeit;
109478064Sume		}
109578064Sume#endif
1096120891Sume		icmp6src.sin6_flowinfo = (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
109778064Sume
109878064Sume		if (finaldst == NULL)
109978064Sume			finaldst = &eip6->ip6_dst;
110078064Sume		ip6cp.ip6c_m = m;
110178064Sume		ip6cp.ip6c_icmp6 = icmp6;
110278064Sume		ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
110378064Sume		ip6cp.ip6c_off = eoff;
110478064Sume		ip6cp.ip6c_finaldst = finaldst;
110578064Sume		ip6cp.ip6c_src = &icmp6src;
110678064Sume		ip6cp.ip6c_nxt = nxt;
110778064Sume
110862587Sitojun		if (icmp6type == ICMP6_PACKET_TOO_BIG) {
110978064Sume			notifymtu = ntohl(icmp6->icmp6_mtu);
111078064Sume			ip6cp.ip6c_cmdarg = (void *)&notifymtu;
111178064Sume			icmp6_mtudisc_update(&ip6cp, 1);	/*XXX*/
111262587Sitojun		}
111362587Sitojun
111453541Sshin		ctlfunc = (void (*) __P((int, struct sockaddr *, void *)))
1115120891Sume		    (inet6sw[ip6_protox[nxt]].pr_ctlinput);
111653541Sshin		if (ctlfunc) {
111778064Sume			(void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1118120891Sume			    &ip6cp);
111953541Sshin		}
112053541Sshin	}
1121120856Sume	return (0);
112253541Sshin
112378064Sume  freeit:
112453541Sshin	m_freem(m);
1125120856Sume	return (-1);
112653541Sshin}
112753541Sshin
112878064Sumevoid
112978064Sumeicmp6_mtudisc_update(ip6cp, validated)
113078064Sume	struct ip6ctlparam *ip6cp;
113178064Sume	int validated;
113262587Sitojun{
113378064Sume	struct in6_addr *dst = ip6cp->ip6c_finaldst;
113478064Sume	struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
113578064Sume	struct mbuf *m = ip6cp->ip6c_m;	/* will be necessary for scope issue */
113662587Sitojun	u_int mtu = ntohl(icmp6->icmp6_mtu);
113762587Sitojun	struct rtentry *rt = NULL;
113862587Sitojun	struct sockaddr_in6 sin6;
113962587Sitojun
114078064Sume	if (!validated)
114178064Sume		return;
114278064Sume
114362587Sitojun	bzero(&sin6, sizeof(sin6));
114462587Sitojun	sin6.sin6_family = PF_INET6;
114562587Sitojun	sin6.sin6_len = sizeof(struct sockaddr_in6);
114662587Sitojun	sin6.sin6_addr = *dst;
114778064Sume	/* XXX normally, this won't happen */
114878064Sume	if (IN6_IS_ADDR_LINKLOCAL(dst)) {
114978064Sume		sin6.sin6_addr.s6_addr16[1] =
115078064Sume		    htons(m->m_pkthdr.rcvif->if_index);
115178064Sume	}
115262587Sitojun	/* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
115362587Sitojun	rt = rtalloc1((struct sockaddr *)&sin6, 0,
115462587Sitojun		      RTF_CLONING | RTF_PRCLONING);
115562587Sitojun
1156120891Sume	if (rt && (rt->rt_flags & RTF_HOST) &&
1157120891Sume	    !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
115862587Sitojun		if (mtu < IPV6_MMTU) {
115962587Sitojun				/* xxx */
116062587Sitojun			rt->rt_rmx.rmx_locks |= RTV_MTU;
116162587Sitojun		} else if (mtu < rt->rt_ifp->if_mtu &&
116262587Sitojun			   rt->rt_rmx.rmx_mtu > mtu) {
116378064Sume			icmp6stat.icp6s_pmtuchg++;
116462587Sitojun			rt->rt_rmx.rmx_mtu = mtu;
116562587Sitojun		}
116662587Sitojun	}
1167120727Ssam	if (rt)
1168120727Ssam		rtfree(rt);
116962587Sitojun}
117062587Sitojun
117153541Sshin/*
117278064Sume * Process a Node Information Query packet, based on
117378064Sume * draft-ietf-ipngwg-icmp-name-lookups-07.
1174120891Sume *
117562587Sitojun * Spec incompatibilities:
117662587Sitojun * - IPv6 Subject address handling
117762587Sitojun * - IPv4 Subject address handling support missing
117862587Sitojun * - Proxy reply (answer even if it's not for me)
117962587Sitojun * - joins NI group address at in6_ifattach() time only, does not cope
118062587Sitojun *   with hostname changes by sethostname(3)
118153541Sshin */
118262587Sitojun#define hostnamelen	strlen(hostname)
118353541Sshinstatic struct mbuf *
118453541Sshinni6_input(m, off)
118553541Sshin	struct mbuf *m;
118653541Sshin	int off;
118753541Sshin{
118862587Sitojun	struct icmp6_nodeinfo *ni6, *nni6;
118953541Sshin	struct mbuf *n = NULL;
119062587Sitojun	u_int16_t qtype;
119162587Sitojun	int subjlen;
119253541Sshin	int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
119353541Sshin	struct ni_reply_fqdn *fqdn;
119453541Sshin	int addrs;		/* for NI_QTYPE_NODEADDR */
119553541Sshin	struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
119678064Sume	struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */
119778064Sume	struct sockaddr_in6 sin6_d; /* XXX: we should retrieve this from m_aux */
119862587Sitojun	struct ip6_hdr *ip6;
119962587Sitojun	int oldfqdn = 0;	/* if 1, return pascal string (03 draft) */
120078064Sume	char *subj = NULL;
120178064Sume	struct in6_ifaddr *ia6 = NULL;
120253541Sshin
120362587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
120462587Sitojun#ifndef PULLDOWN_TEST
120562587Sitojun	ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
120662587Sitojun#else
120762587Sitojun	IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
120862587Sitojun	if (ni6 == NULL) {
120962587Sitojun		/* m is already reclaimed */
1210120891Sume		return (NULL);
121162587Sitojun	}
121262587Sitojun#endif
121353541Sshin
121462587Sitojun	/*
121562587Sitojun	 * Validate IPv6 destination address.
121662587Sitojun	 *
121778064Sume	 * The Responder must discard the Query without further processing
121878064Sume	 * unless it is one of the Responder's unicast or anycast addresses, or
121978064Sume	 * a link-local scope multicast address which the Responder has joined.
122078064Sume	 * [icmp-name-lookups-07, Section 4.]
122162587Sitojun	 */
122262587Sitojun	bzero(&sin6, sizeof(sin6));
122362587Sitojun	sin6.sin6_family = AF_INET6;
122462587Sitojun	sin6.sin6_len = sizeof(struct sockaddr_in6);
122562587Sitojun	bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
122662587Sitojun	/* XXX scopeid */
122778064Sume	if ((ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)&sin6)) != NULL) {
122878064Sume		/* unicast/anycast, fine */
122978064Sume		if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
123078064Sume		    (icmp6_nodeinfo & 4) == 0) {
123178064Sume			nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
123278064Sume				"a temporary address in %s:%d",
123378064Sume			       __FILE__, __LINE__));
123478064Sume			goto bad;
123578064Sume		}
123678064Sume	} else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
123778064Sume		; /* link-local multicast, fine */
123862587Sitojun	else
123962587Sitojun		goto bad;
124062587Sitojun
124178064Sume	/* validate query Subject field. */
124262587Sitojun	qtype = ntohs(ni6->ni_qtype);
124362587Sitojun	subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
124462587Sitojun	switch (qtype) {
124562587Sitojun	case NI_QTYPE_NOOP:
124662587Sitojun	case NI_QTYPE_SUPTYPES:
124778064Sume		/* 07 draft */
124878064Sume		if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
124978064Sume			break;
125078064Sume		/* FALLTHROUGH */
125162587Sitojun	case NI_QTYPE_FQDN:
125262587Sitojun	case NI_QTYPE_NODEADDR:
125362587Sitojun		switch (ni6->ni_code) {
125462587Sitojun		case ICMP6_NI_SUBJ_IPV6:
125562587Sitojun#if ICMP6_NI_SUBJ_IPV6 != 0
125662587Sitojun		case 0:
125762587Sitojun#endif
125862587Sitojun			/*
125962587Sitojun			 * backward compatibility - try to accept 03 draft
126062587Sitojun			 * format, where no Subject is present.
126162587Sitojun			 */
126278064Sume			if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
126378064Sume			    subjlen == 0) {
126462587Sitojun				oldfqdn++;
126562587Sitojun				break;
126662587Sitojun			}
126778064Sume#if ICMP6_NI_SUBJ_IPV6 != 0
126878064Sume			if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
126978064Sume				goto bad;
127078064Sume#endif
127162587Sitojun
127262587Sitojun			if (subjlen != sizeof(sin6.sin6_addr))
127362587Sitojun				goto bad;
127462587Sitojun
127562587Sitojun			/*
127662587Sitojun			 * Validate Subject address.
127762587Sitojun			 *
127878064Sume			 * Not sure what exactly "address belongs to the node"
127978064Sume			 * means in the spec, is it just unicast, or what?
128062587Sitojun			 *
128162587Sitojun			 * At this moment we consider Subject address as
128262587Sitojun			 * "belong to the node" if the Subject address equals
128362587Sitojun			 * to the IPv6 destination address; validation for
128462587Sitojun			 * IPv6 destination address should have done enough
128562587Sitojun			 * check for us.
128662587Sitojun			 *
128762587Sitojun			 * We do not do proxy at this moment.
128862587Sitojun			 */
128962587Sitojun			/* m_pulldown instead of copy? */
129062587Sitojun			m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
129162587Sitojun			    subjlen, (caddr_t)&sin6.sin6_addr);
129278064Sume			sin6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
129378064Sume							      &sin6.sin6_addr);
129478064Sume#ifndef SCOPEDROUTING
129578064Sume			in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL);
129662587Sitojun#endif
129778064Sume			bzero(&sin6_d, sizeof(sin6_d));
129878064Sume			sin6_d.sin6_family = AF_INET6; /* not used, actually */
129978064Sume			sin6_d.sin6_len = sizeof(sin6_d); /* ditto */
130078064Sume			sin6_d.sin6_addr = ip6->ip6_dst;
130178064Sume			sin6_d.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif,
130278064Sume								&ip6->ip6_dst);
130378064Sume#ifndef SCOPEDROUTING
130478064Sume			in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL);
130578064Sume#endif
130678064Sume			subj = (char *)&sin6;
130778064Sume			if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d))
130862587Sitojun				break;
130978064Sume
131062587Sitojun			/*
131162587Sitojun			 * XXX if we are to allow other cases, we should really
131262587Sitojun			 * be careful about scope here.
131362587Sitojun			 * basically, we should disallow queries toward IPv6
1314120891Sume			 * destination X with subject Y,
1315120891Sume			 * if scope(X) > scope(Y).
131662587Sitojun			 * if we allow scope(X) > scope(Y), it will result in
131762587Sitojun			 * information leakage across scope boundary.
131862587Sitojun			 */
131962587Sitojun			goto bad;
132062587Sitojun
132162587Sitojun		case ICMP6_NI_SUBJ_FQDN:
132262587Sitojun			/*
132362587Sitojun			 * Validate Subject name with gethostname(3).
132462587Sitojun			 *
132562587Sitojun			 * The behavior may need some debate, since:
132662587Sitojun			 * - we are not sure if the node has FQDN as
132762587Sitojun			 *   hostname (returned by gethostname(3)).
132862587Sitojun			 * - the code does wildcard match for truncated names.
132962587Sitojun			 *   however, we are not sure if we want to perform
133062587Sitojun			 *   wildcard match, if gethostname(3) side has
133162587Sitojun			 *   truncated hostname.
133262587Sitojun			 */
133362587Sitojun			n = ni6_nametodns(hostname, hostnamelen, 0);
133462587Sitojun			if (!n || n->m_next || n->m_len == 0)
133562587Sitojun				goto bad;
133662587Sitojun			IP6_EXTHDR_GET(subj, char *, m,
133762587Sitojun			    off + sizeof(struct icmp6_nodeinfo), subjlen);
133862587Sitojun			if (subj == NULL)
133962587Sitojun				goto bad;
134062587Sitojun			if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1341120891Sume			    n->m_len)) {
134262587Sitojun				goto bad;
134362587Sitojun			}
134462587Sitojun			m_freem(n);
134562587Sitojun			n = NULL;
134662587Sitojun			break;
134762587Sitojun
134878064Sume		case ICMP6_NI_SUBJ_IPV4:	/* XXX: to be implemented? */
134962587Sitojun		default:
135062587Sitojun			goto bad;
135162587Sitojun		}
135262587Sitojun		break;
135378064Sume	}
135462587Sitojun
135578064Sume	/* refuse based on configuration.  XXX ICMP6_NI_REFUSED? */
135678064Sume	switch (qtype) {
135778064Sume	case NI_QTYPE_FQDN:
135878064Sume		if ((icmp6_nodeinfo & 1) == 0)
135978064Sume			goto bad;
136078064Sume		break;
136178064Sume	case NI_QTYPE_NODEADDR:
136278064Sume		if ((icmp6_nodeinfo & 2) == 0)
136378064Sume			goto bad;
136478064Sume		break;
136578064Sume	}
136678064Sume
136778064Sume	/* guess reply length */
136878064Sume	switch (qtype) {
136978064Sume	case NI_QTYPE_NOOP:
137078064Sume		break;		/* no reply data */
137178064Sume	case NI_QTYPE_SUPTYPES:
137278064Sume		replylen += sizeof(u_int32_t);
137378064Sume		break;
137478064Sume	case NI_QTYPE_FQDN:
137578064Sume		/* XXX will append an mbuf */
137678064Sume		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
137778064Sume		break;
137878064Sume	case NI_QTYPE_NODEADDR:
137978064Sume		addrs = ni6_addrs(ni6, m, &ifp, subj);
138078064Sume		if ((replylen += addrs * (sizeof(struct in6_addr) +
1381120891Sume		    sizeof(u_int32_t))) > MCLBYTES)
138278064Sume			replylen = MCLBYTES; /* XXX: will truncate pkt later */
138378064Sume		break;
138462587Sitojun	default:
138578064Sume		/*
138678064Sume		 * XXX: We must return a reply with the ICMP6 code
1387120891Sume		 * `unknown Qtype' in this case.  However we regard the case
138878064Sume		 * as an FQDN query for backward compatibility.
138978064Sume		 * Older versions set a random value to this field,
139078064Sume		 * so it rarely varies in the defined qtypes.
139178064Sume		 * But the mechanism is not reliable...
139278064Sume		 * maybe we should obsolete older versions.
139378064Sume		 */
139478064Sume		qtype = NI_QTYPE_FQDN;
139578064Sume		/* XXX will append an mbuf */
139678064Sume		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
139778064Sume		oldfqdn++;
139878064Sume		break;
139962587Sitojun	}
140062587Sitojun
140178064Sume	/* allocate an mbuf to reply. */
1402111119Simp	MGETHDR(n, M_DONTWAIT, m->m_type);
140362587Sitojun	if (n == NULL) {
140462587Sitojun		m_freem(m);
1405120856Sume		return (NULL);
140662587Sitojun	}
1407108466Ssam	M_MOVE_PKTHDR(n, m); /* just for recvif */
140853541Sshin	if (replylen > MHLEN) {
140962587Sitojun		if (replylen > MCLBYTES) {
141078064Sume			/*
141178064Sume			 * XXX: should we try to allocate more? But MCLBYTES
141278064Sume			 * is probably much larger than IPV6_MMTU...
141378064Sume			 */
141453541Sshin			goto bad;
141562587Sitojun		}
1416111119Simp		MCLGET(n, M_DONTWAIT);
141753541Sshin		if ((n->m_flags & M_EXT) == 0) {
141853541Sshin			goto bad;
141953541Sshin		}
142053541Sshin	}
142153541Sshin	n->m_pkthdr.len = n->m_len = replylen;
142253541Sshin
142353541Sshin	/* copy mbuf header and IPv6 + Node Information base headers */
142453541Sshin	bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
142553541Sshin	nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
142662587Sitojun	bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
142753541Sshin
142853541Sshin	/* qtype dependent procedure */
142953541Sshin	switch (qtype) {
143062587Sitojun	case NI_QTYPE_NOOP:
143178064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
143262587Sitojun		nni6->ni_flags = 0;
143362587Sitojun		break;
143462587Sitojun	case NI_QTYPE_SUPTYPES:
143578064Sume	{
143678064Sume		u_int32_t v;
143778064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
143878064Sume		nni6->ni_flags = htons(0x0000);	/* raw bitmap */
143978064Sume		/* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
144078064Sume		v = (u_int32_t)htonl(0x0000000f);
144178064Sume		bcopy(&v, nni6 + 1, sizeof(u_int32_t));
144262587Sitojun		break;
144378064Sume	}
144462587Sitojun	case NI_QTYPE_FQDN:
144578064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
144662587Sitojun		fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1447120891Sume		    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
144862587Sitojun		nni6->ni_flags = 0; /* XXX: meaningless TTL */
144962587Sitojun		fqdn->ni_fqdn_ttl = 0;	/* ditto. */
145062587Sitojun		/*
145162587Sitojun		 * XXX do we really have FQDN in variable "hostname"?
145262587Sitojun		 */
145362587Sitojun		n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
145462587Sitojun		if (n->m_next == NULL)
145562587Sitojun			goto bad;
145662587Sitojun		/* XXX we assume that n->m_next is not a chain */
145762587Sitojun		if (n->m_next->m_next != NULL)
145862587Sitojun			goto bad;
145962587Sitojun		n->m_pkthdr.len += n->m_next->m_len;
146062587Sitojun		break;
146162587Sitojun	case NI_QTYPE_NODEADDR:
146262587Sitojun	{
146362587Sitojun		int lenlim, copied;
146453541Sshin
146578064Sume		nni6->ni_code = ICMP6_NI_SUCCESS;
146678064Sume		n->m_pkthdr.len = n->m_len =
146778064Sume		    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
146878064Sume		lenlim = M_TRAILINGSPACE(n);
146962587Sitojun		copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
147062587Sitojun		/* XXX: reset mbuf length */
147162587Sitojun		n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1472120891Sume		    sizeof(struct icmp6_nodeinfo) + copied;
147362587Sitojun		break;
147453541Sshin	}
147562587Sitojun	default:
147662587Sitojun		break;		/* XXX impossible! */
147762587Sitojun	}
147853541Sshin
147953541Sshin	nni6->ni_type = ICMP6_NI_REPLY;
148062587Sitojun	m_freem(m);
1481120856Sume	return (n);
148253541Sshin
148353541Sshin  bad:
148462587Sitojun	m_freem(m);
148553541Sshin	if (n)
148653541Sshin		m_freem(n);
1487120856Sume	return (NULL);
148853541Sshin}
148953541Sshin#undef hostnamelen
149053541Sshin
149153541Sshin/*
149262587Sitojun * make a mbuf with DNS-encoded string.  no compression support.
149362587Sitojun *
149462587Sitojun * XXX names with less than 2 dots (like "foo" or "foo.section") will be
149562587Sitojun * treated as truncated name (two \0 at the end).  this is a wild guess.
149662587Sitojun */
149762587Sitojunstatic struct mbuf *
149862587Sitojunni6_nametodns(name, namelen, old)
149962587Sitojun	const char *name;
150062587Sitojun	int namelen;
150162587Sitojun	int old;	/* return pascal string if non-zero */
150262587Sitojun{
150362587Sitojun	struct mbuf *m;
150462587Sitojun	char *cp, *ep;
150562587Sitojun	const char *p, *q;
150662587Sitojun	int i, len, nterm;
150762587Sitojun
150862587Sitojun	if (old)
150962587Sitojun		len = namelen + 1;
151062587Sitojun	else
151162587Sitojun		len = MCLBYTES;
151262587Sitojun
151362587Sitojun	/* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1514111119Simp	MGET(m, M_DONTWAIT, MT_DATA);
151562587Sitojun	if (m && len > MLEN) {
1516111119Simp		MCLGET(m, M_DONTWAIT);
151762587Sitojun		if ((m->m_flags & M_EXT) == 0)
151862587Sitojun			goto fail;
151962587Sitojun	}
152062587Sitojun	if (!m)
152162587Sitojun		goto fail;
152262587Sitojun	m->m_next = NULL;
152362587Sitojun
152462587Sitojun	if (old) {
152562587Sitojun		m->m_len = len;
152662587Sitojun		*mtod(m, char *) = namelen;
152762587Sitojun		bcopy(name, mtod(m, char *) + 1, namelen);
152862587Sitojun		return m;
152962587Sitojun	} else {
153062587Sitojun		m->m_len = 0;
153162587Sitojun		cp = mtod(m, char *);
153262587Sitojun		ep = mtod(m, char *) + M_TRAILINGSPACE(m);
153362587Sitojun
153462587Sitojun		/* if not certain about my name, return empty buffer */
153562587Sitojun		if (namelen == 0)
153662587Sitojun			return m;
153762587Sitojun
153862587Sitojun		/*
153962587Sitojun		 * guess if it looks like shortened hostname, or FQDN.
154062587Sitojun		 * shortened hostname needs two trailing "\0".
154162587Sitojun		 */
154262587Sitojun		i = 0;
154362587Sitojun		for (p = name; p < name + namelen; p++) {
154462587Sitojun			if (*p && *p == '.')
154562587Sitojun				i++;
154662587Sitojun		}
154762587Sitojun		if (i < 2)
154862587Sitojun			nterm = 2;
154962587Sitojun		else
155062587Sitojun			nterm = 1;
155162587Sitojun
155262587Sitojun		p = name;
155362587Sitojun		while (cp < ep && p < name + namelen) {
155462587Sitojun			i = 0;
155562587Sitojun			for (q = p; q < name + namelen && *q && *q != '.'; q++)
155662587Sitojun				i++;
155762587Sitojun			/* result does not fit into mbuf */
155862587Sitojun			if (cp + i + 1 >= ep)
155962587Sitojun				goto fail;
156078704Sume			/*
156178704Sume			 * DNS label length restriction, RFC1035 page 8.
156278704Sume			 * "i == 0" case is included here to avoid returning
156378704Sume			 * 0-length label on "foo..bar".
156478704Sume			 */
156578704Sume			if (i <= 0 || i >= 64)
156662587Sitojun				goto fail;
156762587Sitojun			*cp++ = i;
156862587Sitojun			bcopy(p, cp, i);
156962587Sitojun			cp += i;
157062587Sitojun			p = q;
157162587Sitojun			if (p < name + namelen && *p == '.')
157262587Sitojun				p++;
157362587Sitojun		}
157462587Sitojun		/* termination */
157562587Sitojun		if (cp + nterm >= ep)
157662587Sitojun			goto fail;
157762587Sitojun		while (nterm-- > 0)
157862587Sitojun			*cp++ = '\0';
157962587Sitojun		m->m_len = cp - mtod(m, char *);
158062587Sitojun		return m;
158162587Sitojun	}
158262587Sitojun
158362587Sitojun	panic("should not reach here");
158495023Ssuz	/* NOTREACHED */
158562587Sitojun
158662587Sitojun fail:
158762587Sitojun	if (m)
158862587Sitojun		m_freem(m);
158962587Sitojun	return NULL;
159062587Sitojun}
159162587Sitojun
159262587Sitojun/*
159362587Sitojun * check if two DNS-encoded string matches.  takes care of truncated
159462587Sitojun * form (with \0\0 at the end).  no compression support.
159578064Sume * XXX upper/lowercase match (see RFC2065)
159662587Sitojun */
159762587Sitojunstatic int
159862587Sitojunni6_dnsmatch(a, alen, b, blen)
159962587Sitojun	const char *a;
160062587Sitojun	int alen;
160162587Sitojun	const char *b;
160262587Sitojun	int blen;
160362587Sitojun{
160462587Sitojun	const char *a0, *b0;
160562587Sitojun	int l;
160662587Sitojun
160762587Sitojun	/* simplest case - need validation? */
160862587Sitojun	if (alen == blen && bcmp(a, b, alen) == 0)
160962587Sitojun		return 1;
161062587Sitojun
161162587Sitojun	a0 = a;
161262587Sitojun	b0 = b;
161362587Sitojun
161462587Sitojun	/* termination is mandatory */
161562587Sitojun	if (alen < 2 || blen < 2)
161662587Sitojun		return 0;
161762587Sitojun	if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
161862587Sitojun		return 0;
161962587Sitojun	alen--;
162062587Sitojun	blen--;
162162587Sitojun
162262587Sitojun	while (a - a0 < alen && b - b0 < blen) {
162362587Sitojun		if (a - a0 + 1 > alen || b - b0 + 1 > blen)
162462587Sitojun			return 0;
162562587Sitojun
162662587Sitojun		if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
162762587Sitojun			return 0;
162862587Sitojun		/* we don't support compression yet */
162962587Sitojun		if (a[0] >= 64 || b[0] >= 64)
163062587Sitojun			return 0;
163162587Sitojun
163262587Sitojun		/* truncated case */
163362587Sitojun		if (a[0] == 0 && a - a0 == alen - 1)
163462587Sitojun			return 1;
163562587Sitojun		if (b[0] == 0 && b - b0 == blen - 1)
163662587Sitojun			return 1;
163762587Sitojun		if (a[0] == 0 || b[0] == 0)
163862587Sitojun			return 0;
163962587Sitojun
164062587Sitojun		if (a[0] != b[0])
164162587Sitojun			return 0;
164262587Sitojun		l = a[0];
164362587Sitojun		if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
164462587Sitojun			return 0;
164562587Sitojun		if (bcmp(a + 1, b + 1, l) != 0)
164662587Sitojun			return 0;
164762587Sitojun
164862587Sitojun		a += 1 + l;
164962587Sitojun		b += 1 + l;
165062587Sitojun	}
165162587Sitojun
165262587Sitojun	if (a - a0 == alen && b - b0 == blen)
165362587Sitojun		return 1;
165462587Sitojun	else
165562587Sitojun		return 0;
165662587Sitojun}
165762587Sitojun
165862587Sitojun/*
165953541Sshin * calculate the number of addresses to be returned in the node info reply.
166053541Sshin */
166153541Sshinstatic int
166278064Sumeni6_addrs(ni6, m, ifpp, subj)
166353541Sshin	struct icmp6_nodeinfo *ni6;
166453541Sshin	struct mbuf *m;
166553541Sshin	struct ifnet **ifpp;
166678064Sume	char *subj;
166753541Sshin{
166878064Sume	struct ifnet *ifp;
166978064Sume	struct in6_ifaddr *ifa6;
167078064Sume	struct ifaddr *ifa;
167178064Sume	struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
167253541Sshin	int addrs = 0, addrsofif, iffound = 0;
167378064Sume	int niflags = ni6->ni_flags;
167453541Sshin
167578064Sume	if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
167678064Sume		switch (ni6->ni_code) {
167778064Sume		case ICMP6_NI_SUBJ_IPV6:
167878064Sume			if (subj == NULL) /* must be impossible... */
1679120856Sume				return (0);
168078064Sume			subj_ip6 = (struct sockaddr_in6 *)subj;
168178064Sume			break;
168278064Sume		default:
168378064Sume			/*
168478064Sume			 * XXX: we only support IPv6 subject address for
168578064Sume			 * this Qtype.
168678064Sume			 */
1687120856Sume			return (0);
168878064Sume		}
168978064Sume	}
169078064Sume
1691108172Shsu	IFNET_RLOCK();
1692120891Sume	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
169353541Sshin		addrsofif = 0;
1694120891Sume		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
169553541Sshin			if (ifa->ifa_addr->sa_family != AF_INET6)
169653541Sshin				continue;
169753541Sshin			ifa6 = (struct in6_ifaddr *)ifa;
169853541Sshin
169978064Sume			if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
170078064Sume			    IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1701120891Sume			    &ifa6->ia_addr.sin6_addr))
170253541Sshin				iffound = 1;
170353541Sshin
170462587Sitojun			/*
170562587Sitojun			 * IPv4-mapped addresses can only be returned by a
170662587Sitojun			 * Node Information proxy, since they represent
170762587Sitojun			 * addresses of IPv4-only nodes, which perforce do
170862587Sitojun			 * not implement this protocol.
170978064Sume			 * [icmp-name-lookups-07, Section 5.4]
171062587Sitojun			 * So we don't support NI_NODEADDR_FLAG_COMPAT in
171162587Sitojun			 * this function at this moment.
171262587Sitojun			 */
171362587Sitojun
171453541Sshin			/* What do we have to do about ::1? */
171578064Sume			switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
171678064Sume			case IPV6_ADDR_SCOPE_LINKLOCAL:
171778064Sume				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
171878064Sume					continue;
171953541Sshin				break;
172078064Sume			case IPV6_ADDR_SCOPE_SITELOCAL:
172178064Sume				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
172278064Sume					continue;
172353541Sshin				break;
172478064Sume			case IPV6_ADDR_SCOPE_GLOBAL:
172578064Sume				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
172678064Sume					continue;
172778064Sume				break;
172878064Sume			default:
172978064Sume				continue;
173053541Sshin			}
173178064Sume
173278064Sume			/*
173378064Sume			 * check if anycast is okay.
173495023Ssuz			 * XXX: just experimental.  not in the spec.
173578064Sume			 */
173678064Sume			if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
173778064Sume			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
173878064Sume				continue; /* we need only unicast addresses */
173978064Sume			if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
174078064Sume			    (icmp6_nodeinfo & 4) == 0) {
174178064Sume				continue;
174278064Sume			}
174378064Sume			addrsofif++; /* count the address */
174453541Sshin		}
174553541Sshin		if (iffound) {
174653541Sshin			*ifpp = ifp;
1747108172Shsu			IFNET_RUNLOCK();
1748120856Sume			return (addrsofif);
174953541Sshin		}
175053541Sshin
175153541Sshin		addrs += addrsofif;
175253541Sshin	}
1753108172Shsu	IFNET_RUNLOCK();
175453541Sshin
1755120856Sume	return (addrs);
175653541Sshin}
175753541Sshin
175853541Sshinstatic int
175953541Sshinni6_store_addrs(ni6, nni6, ifp0, resid)
176053541Sshin	struct icmp6_nodeinfo *ni6, *nni6;
176153541Sshin	struct ifnet *ifp0;
176253541Sshin	int resid;
176353541Sshin{
176478064Sume	struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
176578064Sume	struct in6_ifaddr *ifa6;
176678064Sume	struct ifaddr *ifa;
176778064Sume	struct ifnet *ifp_dep = NULL;
176878064Sume	int copied = 0, allow_deprecated = 0;
176953541Sshin	u_char *cp = (u_char *)(nni6 + 1);
177078064Sume	int niflags = ni6->ni_flags;
177178064Sume	u_int32_t ltime;
177253541Sshin
177378064Sume	if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1774120856Sume		return (0);	/* needless to copy */
1775120891Sume
1776108172Shsu	IFNET_RLOCK();
177778064Sume  again:
1778120891Sume	for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
177962587Sitojun		for (ifa = ifp->if_addrlist.tqh_first; ifa;
1780120891Sume		     ifa = ifa->ifa_list.tqe_next) {
178153541Sshin			if (ifa->ifa_addr->sa_family != AF_INET6)
178253541Sshin				continue;
178353541Sshin			ifa6 = (struct in6_ifaddr *)ifa;
178453541Sshin
178578064Sume			if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
178678064Sume			    allow_deprecated == 0) {
178778064Sume				/*
178878064Sume				 * prefererred address should be put before
178978064Sume				 * deprecated addresses.
179078064Sume				 */
179178064Sume
179278064Sume				/* record the interface for later search */
179378064Sume				if (ifp_dep == NULL)
179478064Sume					ifp_dep = ifp;
179578064Sume
179678064Sume				continue;
1797120891Sume			} else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1798120891Sume			    allow_deprecated != 0)
179978064Sume				continue; /* we now collect deprecated addrs */
180053541Sshin
180153541Sshin			/* What do we have to do about ::1? */
180278064Sume			switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
180378064Sume			case IPV6_ADDR_SCOPE_LINKLOCAL:
180478064Sume				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
180578064Sume					continue;
180653541Sshin				break;
180778064Sume			case IPV6_ADDR_SCOPE_SITELOCAL:
180878064Sume				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
180978064Sume					continue;
181053541Sshin				break;
181178064Sume			case IPV6_ADDR_SCOPE_GLOBAL:
181278064Sume				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
181378064Sume					continue;
181478064Sume				break;
181578064Sume			default:
181678064Sume				continue;
181753541Sshin			}
181853541Sshin
181978064Sume			/*
182078064Sume			 * check if anycast is okay.
1821120891Sume			 * XXX: just experimental.  not in the spec.
182278064Sume			 */
182378064Sume			if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
182478064Sume			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
182578064Sume				continue;
182678064Sume			if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
182778064Sume			    (icmp6_nodeinfo & 4) == 0) {
182878064Sume				continue;
182953541Sshin			}
183078064Sume
183178064Sume			/* now we can copy the address */
183278064Sume			if (resid < sizeof(struct in6_addr) +
183378064Sume			    sizeof(u_int32_t)) {
183478064Sume				/*
183578064Sume				 * We give up much more copy.
183678064Sume				 * Set the truncate flag and return.
183778064Sume				 */
1838120891Sume				nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1839108172Shsu				IFNET_RUNLOCK();
1840120856Sume				return (copied);
184178064Sume			}
184278064Sume
184378064Sume			/*
184478064Sume			 * Set the TTL of the address.
184578064Sume			 * The TTL value should be one of the following
184678064Sume			 * according to the specification:
184778064Sume			 *
184878064Sume			 * 1. The remaining lifetime of a DHCP lease on the
184978064Sume			 *    address, or
185078064Sume			 * 2. The remaining Valid Lifetime of a prefix from
185178064Sume			 *    which the address was derived through Stateless
185278064Sume			 *    Autoconfiguration.
185378064Sume			 *
185478064Sume			 * Note that we currently do not support stateful
185578064Sume			 * address configuration by DHCPv6, so the former
185678064Sume			 * case can't happen.
185778064Sume			 */
185878064Sume			if (ifa6->ia6_lifetime.ia6t_expire == 0)
185978064Sume				ltime = ND6_INFINITE_LIFETIME;
186078064Sume			else {
186178064Sume				if (ifa6->ia6_lifetime.ia6t_expire >
186278064Sume				    time_second)
186378064Sume					ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
186478064Sume				else
186578064Sume					ltime = 0;
186678064Sume			}
1867120891Sume
186878064Sume			bcopy(&ltime, cp, sizeof(u_int32_t));
186978064Sume			cp += sizeof(u_int32_t);
187078064Sume
187178064Sume			/* copy the address itself */
187278064Sume			bcopy(&ifa6->ia_addr.sin6_addr, cp,
1873120891Sume			    sizeof(struct in6_addr));
187478064Sume			/* XXX: KAME link-local hack; remove ifindex */
187578064Sume			if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
187678064Sume				((struct in6_addr *)cp)->s6_addr16[1] = 0;
187778064Sume			cp += sizeof(struct in6_addr);
1878120891Sume
187978064Sume			resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1880120891Sume			copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
188153541Sshin		}
188253541Sshin		if (ifp0)	/* we need search only on the specified IF */
188353541Sshin			break;
188453541Sshin	}
188553541Sshin
188678064Sume	if (allow_deprecated == 0 && ifp_dep != NULL) {
188778064Sume		ifp = ifp_dep;
188878064Sume		allow_deprecated = 1;
188978064Sume
189078064Sume		goto again;
189178064Sume	}
189278064Sume
1893108172Shsu	IFNET_RUNLOCK();
1894108172Shsu
1895120856Sume	return (copied);
189653541Sshin}
189753541Sshin
189853541Sshin/*
189953541Sshin * XXX almost dup'ed code with rip6_input.
190053541Sshin */
190153541Sshinstatic int
190253541Sshinicmp6_rip6_input(mp, off)
190353541Sshin	struct	mbuf **mp;
190453541Sshin	int	off;
190553541Sshin{
190653541Sshin	struct mbuf *m = *mp;
190778064Sume	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
190878064Sume	struct in6pcb *in6p;
190953541Sshin	struct in6pcb *last = NULL;
191053541Sshin	struct sockaddr_in6 rip6src;
191153541Sshin	struct icmp6_hdr *icmp6;
191253541Sshin	struct mbuf *opts = NULL;
191353541Sshin
191462587Sitojun#ifndef PULLDOWN_TEST
191553541Sshin	/* this is assumed to be safe. */
191653541Sshin	icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
191762587Sitojun#else
191862587Sitojun	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
191962587Sitojun	if (icmp6 == NULL) {
192062587Sitojun		/* m is already reclaimed */
1921120891Sume		return (IPPROTO_DONE);
192262587Sitojun	}
192362587Sitojun#endif
192453541Sshin
192553541Sshin	bzero(&rip6src, sizeof(rip6src));
192653541Sshin	rip6src.sin6_len = sizeof(struct sockaddr_in6);
192753541Sshin	rip6src.sin6_family = AF_INET6;
192862587Sitojun	/* KAME hack: recover scopeid */
192962587Sitojun	(void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
193053541Sshin
1931120891Sume	LIST_FOREACH(in6p, &ripcb, inp_list) {
193278064Sume		if ((in6p->inp_vflag & INP_IPV6) == 0)
193353541Sshin			continue;
193478064Sume#ifdef HAVE_NRL_INPCB
193578064Sume		if (!(in6p->in6p_flags & INP_IPV6))
193678064Sume			continue;
193778064Sume#endif
193853541Sshin		if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
193953541Sshin			continue;
194053541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
194153541Sshin		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
194253541Sshin			continue;
194353541Sshin		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
194453541Sshin		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
194553541Sshin			continue;
194653541Sshin		if (in6p->in6p_icmp6filt
194753541Sshin		    && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
194853541Sshin				 in6p->in6p_icmp6filt))
194953541Sshin			continue;
195053541Sshin		if (last) {
195153541Sshin			struct	mbuf *n;
195253541Sshin			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
195353541Sshin				if (last->in6p_flags & IN6P_CONTROLOPTS)
195453541Sshin					ip6_savecontrol(last, &opts, ip6, n);
195553541Sshin				/* strip intermediate headers */
195653541Sshin				m_adj(n, off);
195753541Sshin				if (sbappendaddr(&last->in6p_socket->so_rcv,
1958120891Sume				    (struct sockaddr *)&rip6src, n, opts)
1959120891Sume				    == 0) {
196053541Sshin					/* should notify about lost packet */
196153541Sshin					m_freem(n);
196278064Sume					if (opts) {
196353541Sshin						m_freem(opts);
196478064Sume					}
196597658Stanimura				} else
196653541Sshin					sorwakeup(last->in6p_socket);
196753541Sshin				opts = NULL;
196853541Sshin			}
196953541Sshin		}
197053541Sshin		last = in6p;
197153541Sshin	}
197253541Sshin	if (last) {
197353541Sshin		if (last->in6p_flags & IN6P_CONTROLOPTS)
197453541Sshin			ip6_savecontrol(last, &opts, ip6, m);
197553541Sshin		/* strip intermediate headers */
197653541Sshin		m_adj(m, off);
197753541Sshin		if (sbappendaddr(&last->in6p_socket->so_rcv,
1978120891Sume		    (struct sockaddr *)&rip6src, m, opts) == 0) {
197953541Sshin			m_freem(m);
198053541Sshin			if (opts)
198153541Sshin				m_freem(opts);
198297658Stanimura		} else
198353541Sshin			sorwakeup(last->in6p_socket);
198453541Sshin	} else {
198553541Sshin		m_freem(m);
198653541Sshin		ip6stat.ip6s_delivered--;
198753541Sshin	}
198853541Sshin	return IPPROTO_DONE;
198953541Sshin}
199053541Sshin
199153541Sshin/*
199253541Sshin * Reflect the ip6 packet back to the source.
199362587Sitojun * OFF points to the icmp6 header, counted from the top of the mbuf.
199453541Sshin */
199553541Sshinvoid
199653541Sshinicmp6_reflect(m, off)
199753541Sshin	struct	mbuf *m;
199853541Sshin	size_t off;
199953541Sshin{
200062587Sitojun	struct ip6_hdr *ip6;
200153541Sshin	struct icmp6_hdr *icmp6;
200253541Sshin	struct in6_ifaddr *ia;
200353541Sshin	struct in6_addr t, *src = 0;
200462587Sitojun	int plen;
200553541Sshin	int type, code;
200653541Sshin	struct ifnet *outif = NULL;
200778064Sume	struct sockaddr_in6 sa6_src, sa6_dst;
200853541Sshin#ifdef COMPAT_RFC1885
200953541Sshin	int mtu = IPV6_MMTU;
201053541Sshin	struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst;
201153541Sshin#endif
201253541Sshin
201362587Sitojun	/* too short to reflect */
201462587Sitojun	if (off < sizeof(struct ip6_hdr)) {
201578064Sume		nd6log((LOG_DEBUG,
201678064Sume		    "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
201778064Sume		    (u_long)off, (u_long)sizeof(struct ip6_hdr),
201878064Sume		    __FILE__, __LINE__));
201962587Sitojun		goto bad;
202062587Sitojun	}
202162587Sitojun
202253541Sshin	/*
202353541Sshin	 * If there are extra headers between IPv6 and ICMPv6, strip
202453541Sshin	 * off that header first.
202553541Sshin	 */
202662587Sitojun#ifdef DIAGNOSTIC
202762587Sitojun	if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
202862587Sitojun		panic("assumption failed in icmp6_reflect");
202962587Sitojun#endif
203062587Sitojun	if (off > sizeof(struct ip6_hdr)) {
203162587Sitojun		size_t l;
203262587Sitojun		struct ip6_hdr nip6;
203353541Sshin
203462587Sitojun		l = off - sizeof(struct ip6_hdr);
203562587Sitojun		m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
203662587Sitojun		m_adj(m, l);
203762587Sitojun		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
203862587Sitojun		if (m->m_len < l) {
203962587Sitojun			if ((m = m_pullup(m, l)) == NULL)
204062587Sitojun				return;
204153541Sshin		}
204262587Sitojun		bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
204362587Sitojun	} else /* off == sizeof(struct ip6_hdr) */ {
204462587Sitojun		size_t l;
204562587Sitojun		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
204662587Sitojun		if (m->m_len < l) {
204762587Sitojun			if ((m = m_pullup(m, l)) == NULL)
204862587Sitojun				return;
204953541Sshin		}
205053541Sshin	}
205162587Sitojun	plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
205262587Sitojun	ip6 = mtod(m, struct ip6_hdr *);
205362587Sitojun	ip6->ip6_nxt = IPPROTO_ICMPV6;
205453541Sshin	icmp6 = (struct icmp6_hdr *)(ip6 + 1);
205553541Sshin	type = icmp6->icmp6_type; /* keep type for statistics */
205653541Sshin	code = icmp6->icmp6_code; /* ditto. */
205753541Sshin
205853541Sshin	t = ip6->ip6_dst;
205953541Sshin	/*
206053541Sshin	 * ip6_input() drops a packet if its src is multicast.
206153541Sshin	 * So, the src is never multicast.
206253541Sshin	 */
206353541Sshin	ip6->ip6_dst = ip6->ip6_src;
206453541Sshin
206578064Sume	/*
206678064Sume	 * XXX: make sure to embed scope zone information, using
206778064Sume	 * already embedded IDs or the received interface (if any).
206878064Sume	 * Note that rcvif may be NULL.
206978064Sume	 * TODO: scoped routing case (XXX).
207078064Sume	 */
207178064Sume	bzero(&sa6_src, sizeof(sa6_src));
207278064Sume	sa6_src.sin6_family = AF_INET6;
207378064Sume	sa6_src.sin6_len = sizeof(sa6_src);
207478064Sume	sa6_src.sin6_addr = ip6->ip6_dst;
207578064Sume	in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
207678064Sume	in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL);
207778064Sume	bzero(&sa6_dst, sizeof(sa6_dst));
207878064Sume	sa6_dst.sin6_family = AF_INET6;
207978064Sume	sa6_dst.sin6_len = sizeof(sa6_dst);
208078064Sume	sa6_dst.sin6_addr = t;
208178064Sume	in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
208278064Sume	in6_embedscope(&t, &sa6_dst, NULL, NULL);
208353541Sshin
208453541Sshin#ifdef COMPAT_RFC1885
208553541Sshin	/*
208653541Sshin	 * xxx guess MTU
208753541Sshin	 * RFC 1885 requires that echo reply should be truncated if it
208853541Sshin	 * does not fit in with (return) path MTU, but the description was
208953541Sshin	 * removed in the new spec.
209053541Sshin	 */
209153541Sshin	if (icmp6_reflect_rt.ro_rt == 0 ||
209253541Sshin	    ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) {
209353541Sshin		if (icmp6_reflect_rt.ro_rt) {
209453541Sshin			RTFREE(icmp6_reflect_rt.ro_rt);
209553541Sshin			icmp6_reflect_rt.ro_rt = 0;
209653541Sshin		}
209753541Sshin		bzero(sin6, sizeof(*sin6));
209853541Sshin		sin6->sin6_family = PF_INET6;
209953541Sshin		sin6->sin6_len = sizeof(struct sockaddr_in6);
210053541Sshin		sin6->sin6_addr = ip6->ip6_dst;
210153541Sshin
210253541Sshin		rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt,
210353541Sshin			    RTF_PRCLONING);
210453541Sshin	}
210553541Sshin
210653541Sshin	if (icmp6_reflect_rt.ro_rt == 0)
210753541Sshin		goto bad;
210853541Sshin
210953541Sshin	if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST)
211053541Sshin	    && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu)
211153541Sshin		mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu;
211253541Sshin
211353541Sshin	if (mtu < m->m_pkthdr.len) {
211453541Sshin		plen -= (m->m_pkthdr.len - mtu);
211553541Sshin		m_adj(m, mtu - m->m_pkthdr.len);
211653541Sshin	}
211753541Sshin#endif
211853541Sshin	/*
2119120891Sume	 * If the incoming packet was addressed directly to us (i.e. unicast),
212053541Sshin	 * use dst as the src for the reply.
2121120891Sume	 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
212262587Sitojun	 * (for example) when we encounter an error while forwarding procedure
212362587Sitojun	 * destined to a duplicated address of ours.
212453541Sshin	 */
212553541Sshin	for (ia = in6_ifaddr; ia; ia = ia->ia_next)
212653541Sshin		if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
212762587Sitojun		    (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
212853541Sshin			src = &t;
212953541Sshin			break;
213053541Sshin		}
213153541Sshin	if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
213253541Sshin		/*
213353541Sshin		 * This is the case if the dst is our link-local address
213495023Ssuz		 * and the sender is also ourselves.
213553541Sshin		 */
213653541Sshin		src = &t;
213753541Sshin	}
213853541Sshin
213978064Sume	if (src == 0) {
214078064Sume		int e;
214178064Sume		struct route_in6 ro;
214278064Sume
214353541Sshin		/*
214462587Sitojun		 * This case matches to multicasts, our anycast, or unicasts
214595023Ssuz		 * that we do not own.  Select a source address based on the
214678064Sume		 * source address of the erroneous packet.
214753541Sshin		 */
214878064Sume		bzero(&ro, sizeof(ro));
214978064Sume		src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e);
215078064Sume		if (ro.ro_rt)
215178064Sume			RTFREE(ro.ro_rt); /* XXX: we could use this */
215278064Sume		if (src == NULL) {
215378064Sume			nd6log((LOG_DEBUG,
215478064Sume			    "icmp6_reflect: source can't be determined: "
215578064Sume			    "dst=%s, error=%d\n",
215678064Sume			    ip6_sprintf(&sa6_src.sin6_addr), e));
215778064Sume			goto bad;
215878064Sume		}
215978064Sume	}
216053541Sshin
216153541Sshin	ip6->ip6_src = *src;
216253541Sshin
216353541Sshin	ip6->ip6_flow = 0;
216462587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
216562587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
216653541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
216753541Sshin	if (m->m_pkthdr.rcvif) {
216853541Sshin		/* XXX: This may not be the outgoing interface */
216953541Sshin		ip6->ip6_hlim = nd_ifinfo[m->m_pkthdr.rcvif->if_index].chlim;
217078064Sume	} else
217178064Sume		ip6->ip6_hlim = ip6_defhlim;
217253541Sshin
217353541Sshin	icmp6->icmp6_cksum = 0;
217453541Sshin	icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2175120891Sume	    sizeof(struct ip6_hdr), plen);
217653541Sshin
217753541Sshin	/*
217878064Sume	 * XXX option handling
217953541Sshin	 */
218053541Sshin
218153541Sshin	m->m_flags &= ~(M_BCAST|M_MCAST);
218253541Sshin
218353541Sshin#ifdef COMPAT_RFC1885
2184105194Ssam	ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL);
218553541Sshin#else
2186105194Ssam	ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
218753541Sshin#endif
218853541Sshin	if (outif)
218953541Sshin		icmp6_ifoutstat_inc(outif, type, code);
219053541Sshin
219153541Sshin	return;
219253541Sshin
219353541Sshin bad:
219453541Sshin	m_freem(m);
219553541Sshin	return;
219653541Sshin}
219753541Sshin
219853541Sshinvoid
219953541Sshinicmp6_fasttimo()
220053541Sshin{
220162587Sitojun
220253541Sshin	mld6_fasttimeo();
220353541Sshin}
220453541Sshin
220553541Sshinstatic const char *
220653541Sshinicmp6_redirect_diag(src6, dst6, tgt6)
220753541Sshin	struct in6_addr *src6;
220853541Sshin	struct in6_addr *dst6;
220953541Sshin	struct in6_addr *tgt6;
221053541Sshin{
221153541Sshin	static char buf[1024];
221253541Sshin	snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2213120891Sume	    ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
221453541Sshin	return buf;
221553541Sshin}
221653541Sshin
221753541Sshinvoid
221853541Sshinicmp6_redirect_input(m, off)
221978064Sume	struct mbuf *m;
222053541Sshin	int off;
222153541Sshin{
222253541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
222353541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
222462587Sitojun	struct nd_redirect *nd_rd;
222553541Sshin	int icmp6len = ntohs(ip6->ip6_plen);
222653541Sshin	char *lladdr = NULL;
222753541Sshin	int lladdrlen = 0;
222853541Sshin	u_char *redirhdr = NULL;
222953541Sshin	int redirhdrlen = 0;
223053541Sshin	struct rtentry *rt = NULL;
223153541Sshin	int is_router;
223253541Sshin	int is_onlink;
223353541Sshin	struct in6_addr src6 = ip6->ip6_src;
223462587Sitojun	struct in6_addr redtgt6;
223562587Sitojun	struct in6_addr reddst6;
223653541Sshin	union nd_opts ndopts;
223753541Sshin
223853541Sshin	if (!m || !ifp)
223953541Sshin		return;
224053541Sshin
224153541Sshin	/* XXX if we are router, we don't update route by icmp6 redirect */
224253541Sshin	if (ip6_forwarding)
224362587Sitojun		goto freeit;
224453541Sshin	if (!icmp6_rediraccept)
224562587Sitojun		goto freeit;
224662587Sitojun
224762587Sitojun#ifndef PULLDOWN_TEST
224862587Sitojun	IP6_EXTHDR_CHECK(m, off, icmp6len,);
224962587Sitojun	nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
225062587Sitojun#else
225162587Sitojun	IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
225262587Sitojun	if (nd_rd == NULL) {
225362587Sitojun		icmp6stat.icp6s_tooshort++;
225453541Sshin		return;
225562587Sitojun	}
225662587Sitojun#endif
225762587Sitojun	redtgt6 = nd_rd->nd_rd_target;
225862587Sitojun	reddst6 = nd_rd->nd_rd_dst;
225953541Sshin
226053541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
226153541Sshin		redtgt6.s6_addr16[1] = htons(ifp->if_index);
226253541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
226353541Sshin		reddst6.s6_addr16[1] = htons(ifp->if_index);
226453541Sshin
226553541Sshin	/* validation */
226653541Sshin	if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
226778064Sume		nd6log((LOG_ERR,
2268120891Sume		    "ICMP6 redirect sent from %s rejected; "
2269120891Sume		    "must be from linklocal\n",
2270120891Sume		    ip6_sprintf(&src6)));
227178064Sume		goto bad;
227253541Sshin	}
227353541Sshin	if (ip6->ip6_hlim != 255) {
227478064Sume		nd6log((LOG_ERR,
2275120891Sume		    "ICMP6 redirect sent from %s rejected; "
2276120891Sume		    "hlim=%d (must be 255)\n",
2277120891Sume		    ip6_sprintf(&src6), ip6->ip6_hlim));
227878064Sume		goto bad;
227953541Sshin	}
228053541Sshin    {
228153541Sshin	/* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
228253541Sshin	struct sockaddr_in6 sin6;
228353541Sshin	struct in6_addr *gw6;
228453541Sshin
228553541Sshin	bzero(&sin6, sizeof(sin6));
228653541Sshin	sin6.sin6_family = AF_INET6;
228753541Sshin	sin6.sin6_len = sizeof(struct sockaddr_in6);
228853541Sshin	bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
228953541Sshin	rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
229053541Sshin	if (rt) {
229165895Sume		if (rt->rt_gateway == NULL ||
229265895Sume		    rt->rt_gateway->sa_family != AF_INET6) {
229378064Sume			nd6log((LOG_ERR,
229465895Sume			    "ICMP6 redirect rejected; no route "
229565895Sume			    "with inet6 gateway found for redirect dst: %s\n",
229678064Sume			    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2297120727Ssam			RTFREE_LOCKED(rt);
229878064Sume			goto bad;
229965895Sume		}
230065895Sume
230153541Sshin		gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
230253541Sshin		if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
230378064Sume			nd6log((LOG_ERR,
2304120891Sume			    "ICMP6 redirect rejected; "
2305120891Sume			    "not equal to gw-for-src=%s (must be same): "
2306120891Sume			    "%s\n",
2307120891Sume			    ip6_sprintf(gw6),
2308120891Sume			    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2309120727Ssam			RTFREE_LOCKED(rt);
231078064Sume			goto bad;
231153541Sshin		}
231253541Sshin	} else {
231378064Sume		nd6log((LOG_ERR,
2314120891Sume		    "ICMP6 redirect rejected; "
2315120891Sume		    "no route found for redirect dst: %s\n",
2316120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
231778064Sume		goto bad;
231853541Sshin	}
2319120727Ssam	RTFREE_LOCKED(rt);
232053541Sshin	rt = NULL;
232153541Sshin    }
232253541Sshin	if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
232378064Sume		nd6log((LOG_ERR,
2324120891Sume		    "ICMP6 redirect rejected; "
2325120891Sume		    "redirect dst must be unicast: %s\n",
2326120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
232778064Sume		goto bad;
232853541Sshin	}
232953541Sshin
233053541Sshin	is_router = is_onlink = 0;
233153541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
233253541Sshin		is_router = 1;	/* router case */
233353541Sshin	if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
233453541Sshin		is_onlink = 1;	/* on-link destination case */
233553541Sshin	if (!is_router && !is_onlink) {
233678064Sume		nd6log((LOG_ERR,
2337120891Sume		    "ICMP6 redirect rejected; "
2338120891Sume		    "neither router case nor onlink case: %s\n",
2339120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
234078064Sume		goto bad;
234153541Sshin	}
234253541Sshin	/* validation passed */
234353541Sshin
234453541Sshin	icmp6len -= sizeof(*nd_rd);
234553541Sshin	nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
234653541Sshin	if (nd6_options(&ndopts) < 0) {
234778064Sume		nd6log((LOG_INFO, "icmp6_redirect_input: "
2348120891Sume		    "invalid ND option, rejected: %s\n",
2349120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
235078064Sume		/* nd6_options have incremented stats */
235162587Sitojun		goto freeit;
235253541Sshin	}
235353541Sshin
235453541Sshin	if (ndopts.nd_opts_tgt_lladdr) {
235553541Sshin		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
235653541Sshin		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
235753541Sshin	}
235853541Sshin
235953541Sshin	if (ndopts.nd_opts_rh) {
236053541Sshin		redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
236153541Sshin		redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
236253541Sshin	}
236353541Sshin
236453541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
236578064Sume		nd6log((LOG_INFO,
2366120891Sume		    "icmp6_redirect_input: lladdrlen mismatch for %s "
2367120891Sume		    "(if %d, icmp6 packet %d): %s\n",
2368120891Sume		    ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2369120891Sume		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
237078064Sume		goto bad;
237153541Sshin	}
237253541Sshin
237353541Sshin	/* RFC 2461 8.3 */
237453541Sshin	nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2375120891Sume	    is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
237653541Sshin
237795023Ssuz	if (!is_onlink) {	/* better router case.  perform rtredirect. */
237853541Sshin		/* perform rtredirect */
237953541Sshin		struct sockaddr_in6 sdst;
238053541Sshin		struct sockaddr_in6 sgw;
238153541Sshin		struct sockaddr_in6 ssrc;
238253541Sshin
238353541Sshin		bzero(&sdst, sizeof(sdst));
238453541Sshin		bzero(&sgw, sizeof(sgw));
238553541Sshin		bzero(&ssrc, sizeof(ssrc));
238653541Sshin		sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
238753541Sshin		sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
238853541Sshin			sizeof(struct sockaddr_in6);
238953541Sshin		bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
239053541Sshin		bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
239153541Sshin		bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
239253541Sshin		rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2393120891Sume		    (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2394120891Sume		    (struct sockaddr *)&ssrc);
239553541Sshin	}
239653541Sshin	/* finally update cached route in each socket via pfctlinput */
239753541Sshin    {
239853541Sshin	struct sockaddr_in6 sdst;
239953541Sshin
240053541Sshin	bzero(&sdst, sizeof(sdst));
240153541Sshin	sdst.sin6_family = AF_INET6;
240253541Sshin	sdst.sin6_len = sizeof(struct sockaddr_in6);
240353541Sshin	bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
240453541Sshin	pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
240553541Sshin#ifdef IPSEC
240653541Sshin	key_sa_routechange((struct sockaddr *)&sdst);
240753541Sshin#endif
240853541Sshin    }
240962587Sitojun
241062587Sitojun freeit:
241162587Sitojun	m_freem(m);
241278064Sume	return;
241378064Sume
241478064Sume bad:
241578064Sume	icmp6stat.icp6s_badredirect++;
241678064Sume	m_freem(m);
241753541Sshin}
241853541Sshin
241953541Sshinvoid
242053541Sshinicmp6_redirect_output(m0, rt)
242153541Sshin	struct mbuf *m0;
242253541Sshin	struct rtentry *rt;
242353541Sshin{
242453541Sshin	struct ifnet *ifp;	/* my outgoing interface */
242553541Sshin	struct in6_addr *ifp_ll6;
242653541Sshin	struct in6_addr *router_ll6;
242753541Sshin	struct ip6_hdr *sip6;	/* m0 as struct ip6_hdr */
242853541Sshin	struct mbuf *m = NULL;	/* newly allocated one */
242953541Sshin	struct ip6_hdr *ip6;	/* m as struct ip6_hdr */
243053541Sshin	struct nd_redirect *nd_rd;
243153541Sshin	size_t maxlen;
243253541Sshin	u_char *p;
243353541Sshin	struct ifnet *outif = NULL;
243462587Sitojun	struct sockaddr_in6 src_sa;
243553541Sshin
243662587Sitojun	icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
243762587Sitojun
243853541Sshin	/* if we are not router, we don't send icmp6 redirect */
243953541Sshin	if (!ip6_forwarding || ip6_accept_rtadv)
244053541Sshin		goto fail;
244153541Sshin
244253541Sshin	/* sanity check */
244353541Sshin	if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
244453541Sshin		goto fail;
244553541Sshin
244653541Sshin	/*
244753541Sshin	 * Address check:
244853541Sshin	 *  the source address must identify a neighbor, and
244953541Sshin	 *  the destination address must not be a multicast address
245053541Sshin	 *  [RFC 2461, sec 8.2]
245153541Sshin	 */
245253541Sshin	sip6 = mtod(m0, struct ip6_hdr *);
245362587Sitojun	bzero(&src_sa, sizeof(src_sa));
245462587Sitojun	src_sa.sin6_family = AF_INET6;
245562587Sitojun	src_sa.sin6_len = sizeof(src_sa);
245662587Sitojun	src_sa.sin6_addr = sip6->ip6_src;
245762587Sitojun	/* we don't currently use sin6_scope_id, but eventually use it */
245862587Sitojun	src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src);
245962587Sitojun	if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
246053541Sshin		goto fail;
246153541Sshin	if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
246253541Sshin		goto fail;	/* what should we do here? */
246353541Sshin
246453541Sshin	/* rate limit */
246553541Sshin	if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
246653541Sshin		goto fail;
246753541Sshin
246853541Sshin	/*
246953541Sshin	 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
247053541Sshin	 * we almost always ask for an mbuf cluster for simplicity.
247153541Sshin	 * (MHLEN < IPV6_MMTU is almost always true)
247253541Sshin	 */
247362587Sitojun#if IPV6_MMTU >= MCLBYTES
247462587Sitojun# error assumption failed about IPV6_MMTU and MCLBYTES
247562587Sitojun#endif
2476111119Simp	MGETHDR(m, M_DONTWAIT, MT_HEADER);
247762587Sitojun	if (m && IPV6_MMTU >= MHLEN)
2478111119Simp		MCLGET(m, M_DONTWAIT);
247953541Sshin	if (!m)
248053541Sshin		goto fail;
248178064Sume	m->m_pkthdr.rcvif = NULL;
248278064Sume	m->m_len = 0;
248378064Sume	maxlen = M_TRAILINGSPACE(m);
248453541Sshin	maxlen = min(IPV6_MMTU, maxlen);
248553541Sshin	/* just for safety */
248662587Sitojun	if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
248762587Sitojun	    ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
248853541Sshin		goto fail;
248962587Sitojun	}
249053541Sshin
249153541Sshin	{
249253541Sshin		/* get ip6 linklocal address for ifp(my outgoing interface). */
249362587Sitojun		struct in6_ifaddr *ia;
249462587Sitojun		if ((ia = in6ifa_ifpforlinklocal(ifp,
249562587Sitojun						 IN6_IFF_NOTREADY|
249662587Sitojun						 IN6_IFF_ANYCAST)) == NULL)
249753541Sshin			goto fail;
249853541Sshin		ifp_ll6 = &ia->ia_addr.sin6_addr;
249953541Sshin	}
250053541Sshin
250153541Sshin	/* get ip6 linklocal address for the router. */
250253541Sshin	if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
250353541Sshin		struct sockaddr_in6 *sin6;
250453541Sshin		sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
250553541Sshin		router_ll6 = &sin6->sin6_addr;
250653541Sshin		if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
250753541Sshin			router_ll6 = (struct in6_addr *)NULL;
250853541Sshin	} else
250953541Sshin		router_ll6 = (struct in6_addr *)NULL;
251053541Sshin
251153541Sshin	/* ip6 */
251253541Sshin	ip6 = mtod(m, struct ip6_hdr *);
251353541Sshin	ip6->ip6_flow = 0;
251462587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
251562587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
251653541Sshin	/* ip6->ip6_plen will be set later */
251753541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
251853541Sshin	ip6->ip6_hlim = 255;
251953541Sshin	/* ip6->ip6_src must be linklocal addr for my outgoing if. */
252053541Sshin	bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
252153541Sshin	bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
252253541Sshin
252353541Sshin	/* ND Redirect */
252453541Sshin	nd_rd = (struct nd_redirect *)(ip6 + 1);
252553541Sshin	nd_rd->nd_rd_type = ND_REDIRECT;
252653541Sshin	nd_rd->nd_rd_code = 0;
252753541Sshin	nd_rd->nd_rd_reserved = 0;
252853541Sshin	if (rt->rt_flags & RTF_GATEWAY) {
252953541Sshin		/*
253053541Sshin		 * nd_rd->nd_rd_target must be a link-local address in
253153541Sshin		 * better router cases.
253253541Sshin		 */
253353541Sshin		if (!router_ll6)
253453541Sshin			goto fail;
253553541Sshin		bcopy(router_ll6, &nd_rd->nd_rd_target,
253653541Sshin		      sizeof(nd_rd->nd_rd_target));
253753541Sshin		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
253853541Sshin		      sizeof(nd_rd->nd_rd_dst));
253953541Sshin	} else {
254053541Sshin		/* make sure redtgt == reddst */
254153541Sshin		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
254253541Sshin		      sizeof(nd_rd->nd_rd_target));
254353541Sshin		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
254453541Sshin		      sizeof(nd_rd->nd_rd_dst));
254553541Sshin	}
254653541Sshin
254753541Sshin	p = (u_char *)(nd_rd + 1);
254853541Sshin
254953541Sshin	if (!router_ll6)
255053541Sshin		goto nolladdropt;
255153541Sshin
255253541Sshin    {
255353541Sshin	/* target lladdr option */
255453541Sshin	struct rtentry *rt_router = NULL;
255553541Sshin	int len;
255653541Sshin	struct sockaddr_dl *sdl;
255753541Sshin	struct nd_opt_hdr *nd_opt;
255853541Sshin	char *lladdr;
255953541Sshin
256053541Sshin	rt_router = nd6_lookup(router_ll6, 0, ifp);
256153541Sshin	if (!rt_router)
256253541Sshin		goto nolladdropt;
256362587Sitojun	len = sizeof(*nd_opt) + ifp->if_addrlen;
256495023Ssuz	len = (len + 7) & ~7;	/* round by 8 */
256562587Sitojun	/* safety check */
256662587Sitojun	if (len + (p - (u_char *)ip6) > maxlen)
256762587Sitojun		goto nolladdropt;
256862587Sitojun	if (!(rt_router->rt_flags & RTF_GATEWAY) &&
256962587Sitojun	    (rt_router->rt_flags & RTF_LLINFO) &&
257062587Sitojun	    (rt_router->rt_gateway->sa_family == AF_LINK) &&
257162587Sitojun	    (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
257262587Sitojun	    sdl->sdl_alen) {
257353541Sshin		nd_opt = (struct nd_opt_hdr *)p;
257453541Sshin		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
257553541Sshin		nd_opt->nd_opt_len = len >> 3;
257653541Sshin		lladdr = (char *)(nd_opt + 1);
257753541Sshin		bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
257862587Sitojun		p += len;
257953541Sshin	}
258053541Sshin    }
258153541Sshinnolladdropt:;
258253541Sshin
258353541Sshin	m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
258453541Sshin
258553541Sshin	/* just to be safe */
258662587Sitojun#ifdef M_DECRYPTED	/*not openbsd*/
258753541Sshin	if (m0->m_flags & M_DECRYPTED)
258853541Sshin		goto noredhdropt;
258962587Sitojun#endif
259062587Sitojun	if (p - (u_char *)ip6 > maxlen)
259162587Sitojun		goto noredhdropt;
259253541Sshin
2593120891Sume	{
2594120891Sume		/* redirected header option */
2595120891Sume		int len;
2596120891Sume		struct nd_opt_rd_hdr *nd_opt_rh;
259753541Sshin
2598120891Sume		/*
2599120891Sume		 * compute the maximum size for icmp6 redirect header option.
2600120891Sume		 * XXX room for auth header?
2601120891Sume		 */
2602120891Sume		len = maxlen - (p - (u_char *)ip6);
2603120891Sume		len &= ~7;
260453541Sshin
2605120891Sume		/* This is just for simplicity. */
2606120891Sume		if (m0->m_pkthdr.len != m0->m_len) {
2607120891Sume			if (m0->m_next) {
2608120891Sume				m_freem(m0->m_next);
2609120891Sume				m0->m_next = NULL;
2610120891Sume			}
2611120891Sume			m0->m_pkthdr.len = m0->m_len;
261253541Sshin		}
261353541Sshin
2614120891Sume		/*
2615120891Sume		 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2616120891Sume		 * about padding/truncate rule for the original IP packet.
2617120891Sume		 * From the discussion on IPv6imp in Feb 1999,
2618120891Sume		 * the consensus was:
2619120891Sume		 * - "attach as much as possible" is the goal
2620120891Sume		 * - pad if not aligned (original size can be guessed by
2621120891Sume		 *   original ip6 header)
2622120891Sume		 * Following code adds the padding if it is simple enough,
2623120891Sume		 * and truncates if not.
2624120891Sume		 */
2625120891Sume		if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2626120891Sume			panic("assumption failed in %s:%d", __FILE__,
2627120891Sume			    __LINE__);
262853541Sshin
2629120891Sume		if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2630120891Sume			/* not enough room, truncate */
2631120891Sume			m0->m_pkthdr.len = m0->m_len = len -
2632120891Sume			    sizeof(*nd_opt_rh);
2633120891Sume		} else {
2634120891Sume			/* enough room, pad or truncate */
2635120891Sume			size_t extra;
263653541Sshin
2637120891Sume			extra = m0->m_pkthdr.len % 8;
2638120891Sume			if (extra) {
2639120891Sume				/* pad if easy enough, truncate if not */
2640120891Sume				if (8 - extra <= M_TRAILINGSPACE(m0)) {
2641120891Sume					/* pad */
2642120891Sume					m0->m_len += (8 - extra);
2643120891Sume					m0->m_pkthdr.len += (8 - extra);
2644120891Sume				} else {
2645120891Sume					/* truncate */
2646120891Sume					m0->m_pkthdr.len -= extra;
2647120891Sume					m0->m_len -= extra;
2648120891Sume				}
264953541Sshin			}
2650120891Sume			len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2651120891Sume			m0->m_pkthdr.len = m0->m_len = len -
2652120891Sume			    sizeof(*nd_opt_rh);
265353541Sshin		}
265453541Sshin
2655120891Sume		nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2656120891Sume		bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2657120891Sume		nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2658120891Sume		nd_opt_rh->nd_opt_rh_len = len >> 3;
2659120891Sume		p += sizeof(*nd_opt_rh);
2660120891Sume		m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
266153541Sshin
2662120891Sume		/* connect m0 to m */
2663120891Sume		m_tag_delete_chain(m0, NULL);
2664120891Sume		m0->m_flags &= ~M_PKTHDR;
2665120891Sume		m->m_next = m0;
2666120891Sume		m->m_pkthdr.len = m->m_len + m0->m_len;
2667120891Sume		m0 = NULL;
2668120891Sume	}
266953541Sshinnoredhdropt:;
2670112781Ssuz	if (m0) {
2671112781Ssuz		m_freem(m0);
2672112781Ssuz		m0 = NULL;
2673112781Ssuz	}
267453541Sshin
267553541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
267653541Sshin		sip6->ip6_src.s6_addr16[1] = 0;
267753541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
267853541Sshin		sip6->ip6_dst.s6_addr16[1] = 0;
267962587Sitojun#if 0
268062587Sitojun	if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
268162587Sitojun		ip6->ip6_src.s6_addr16[1] = 0;
268262587Sitojun	if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
268362587Sitojun		ip6->ip6_dst.s6_addr16[1] = 0;
268462587Sitojun#endif
268553541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
268653541Sshin		nd_rd->nd_rd_target.s6_addr16[1] = 0;
268753541Sshin	if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
268853541Sshin		nd_rd->nd_rd_dst.s6_addr16[1] = 0;
268953541Sshin
269053541Sshin	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
269153541Sshin
269253541Sshin	nd_rd->nd_rd_cksum = 0;
2693120891Sume	nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2694120891Sume	    sizeof(*ip6), ntohs(ip6->ip6_plen));
269553541Sshin
269653541Sshin	/* send the packet to outside... */
2697105194Ssam	ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
269853541Sshin	if (outif) {
269953541Sshin		icmp6_ifstat_inc(outif, ifs6_out_msg);
270053541Sshin		icmp6_ifstat_inc(outif, ifs6_out_redirect);
270153541Sshin	}
270253541Sshin	icmp6stat.icp6s_outhist[ND_REDIRECT]++;
270353541Sshin
270453541Sshin	return;
270553541Sshin
270653541Sshinfail:
270753541Sshin	if (m)
270853541Sshin		m_freem(m);
270953541Sshin	if (m0)
271053541Sshin		m_freem(m0);
271153541Sshin}
271253541Sshin
271378064Sume#ifdef HAVE_NRL_INPCB
271478064Sume#define sotoin6pcb	sotoinpcb
271578064Sume#define in6pcb		inpcb
271678064Sume#define in6p_icmp6filt	inp_icmp6filt
271778064Sume#endif
271853541Sshin/*
271953541Sshin * ICMPv6 socket option processing.
272053541Sshin */
272153541Sshinint
272253541Sshinicmp6_ctloutput(so, sopt)
272353541Sshin	struct socket *so;
272453541Sshin	struct sockopt *sopt;
272553541Sshin{
272653541Sshin	int error = 0;
272753541Sshin	int optlen;
272878064Sume	struct inpcb *inp = sotoinpcb(so);
272953541Sshin	int level, op, optname;
273053541Sshin
273153541Sshin	if (sopt) {
273253541Sshin		level = sopt->sopt_level;
273353541Sshin		op = sopt->sopt_dir;
273453541Sshin		optname = sopt->sopt_name;
273553541Sshin		optlen = sopt->sopt_valsize;
273653541Sshin	} else
273753541Sshin		level = op = optname = optlen = 0;
273878064Sume
273953541Sshin	if (level != IPPROTO_ICMPV6) {
274053541Sshin		return EINVAL;
274153541Sshin	}
274253541Sshin
274378064Sume	switch (op) {
274453541Sshin	case PRCO_SETOPT:
274553541Sshin		switch (optname) {
274653541Sshin		case ICMP6_FILTER:
274753541Sshin		    {
274853541Sshin			struct icmp6_filter *p;
274953541Sshin
275053541Sshin			if (optlen != sizeof(*p)) {
275153541Sshin				error = EMSGSIZE;
275253541Sshin				break;
275353541Sshin			}
275453541Sshin			if (inp->in6p_icmp6filt == NULL) {
275553541Sshin				error = EINVAL;
275653541Sshin				break;
275753541Sshin			}
275853541Sshin			error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen,
275953541Sshin				optlen);
276053541Sshin			break;
276153541Sshin		    }
276253541Sshin
276353541Sshin		default:
276453541Sshin			error = ENOPROTOOPT;
276553541Sshin			break;
276653541Sshin		}
276753541Sshin		break;
276853541Sshin
276953541Sshin	case PRCO_GETOPT:
277053541Sshin		switch (optname) {
277153541Sshin		case ICMP6_FILTER:
277253541Sshin		    {
277353541Sshin			if (inp->in6p_icmp6filt == NULL) {
277453541Sshin				error = EINVAL;
277553541Sshin				break;
277653541Sshin			}
277753541Sshin			error = sooptcopyout(sopt, inp->in6p_icmp6filt,
277853541Sshin				sizeof(struct icmp6_filter));
277953541Sshin			break;
278053541Sshin		    }
278153541Sshin
278253541Sshin		default:
278353541Sshin			error = ENOPROTOOPT;
278453541Sshin			break;
278553541Sshin		}
278653541Sshin		break;
278753541Sshin	}
278853541Sshin
2789120856Sume	return (error);
279053541Sshin}
279178064Sume#ifdef HAVE_NRL_INPCB
279278064Sume#undef sotoin6pcb
279378064Sume#undef in6pcb
279478064Sume#undef in6p_icmp6filt
279578064Sume#endif
279653541Sshin
279778064Sume#ifndef HAVE_PPSRATECHECK
279878064Sume#ifndef timersub
279978064Sume#define	timersub(tvp, uvp, vvp)						\
280078064Sume	do {								\
280178064Sume		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
280278064Sume		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
280378064Sume		if ((vvp)->tv_usec < 0) {				\
280478064Sume			(vvp)->tv_sec--;				\
280578064Sume			(vvp)->tv_usec += 1000000;			\
280678064Sume		}							\
280778064Sume	} while (0)
280878064Sume#endif
280978064Sume
281053541Sshin/*
281178064Sume * ppsratecheck(): packets (or events) per second limitation.
281262587Sitojun */
281362587Sitojunstatic int
281478064Sumeppsratecheck(lasttime, curpps, maxpps)
281578064Sume	struct timeval *lasttime;
281678064Sume	int *curpps;
281778064Sume	int maxpps;	/* maximum pps allowed */
281862587Sitojun{
281978064Sume	struct timeval tv, delta;
282078064Sume	int s, rv;
282162587Sitojun
282278064Sume	s = splclock();
282378064Sume	microtime(&tv);
282478064Sume	splx(s);
282562587Sitojun
282678064Sume	timersub(&tv, lasttime, &delta);
282762587Sitojun
282878064Sume	/*
282995023Ssuz	 * Check for 0,0 so that the message will be seen at least once.
283095023Ssuz	 * If more than one second has passed since the last update of
283178064Sume	 * lasttime, reset the counter.
283278064Sume	 *
283395023Ssuz	 * We do increment *curpps even in *curpps < maxpps case, as some may
283478064Sume	 * try to use *curpps for stat purposes as well.
283578064Sume	 */
283678064Sume	if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
283778064Sume	    delta.tv_sec >= 1) {
283878064Sume		*lasttime = tv;
283978064Sume		*curpps = 0;
284078064Sume		rv = 1;
284178064Sume	} else if (maxpps < 0)
284278064Sume		rv = 1;
284378064Sume	else if (*curpps < maxpps)
284478064Sume		rv = 1;
284578064Sume	else
284678064Sume		rv = 0;
284762587Sitojun
284895023Ssuz#if 1 /* DIAGNOSTIC? */
284978064Sume	/* be careful about wrap-around */
285078064Sume	if (*curpps + 1 > *curpps)
285178064Sume		*curpps = *curpps + 1;
285278064Sume#else
285378064Sume	/*
285478064Sume	 * assume that there's not too many calls to this function.
285578064Sume	 * not sure if the assumption holds, as it depends on *caller's*
285678064Sume	 * behavior, not the behavior of this function.
285778064Sume	 * IMHO it is wrong to make assumption on the caller's behavior,
285878064Sume	 * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
285978064Sume	 */
286078064Sume	*curpps = *curpps + 1;
286178064Sume#endif
286278064Sume
286378064Sume	return (rv);
286462587Sitojun}
286562587Sitojun#endif
286662587Sitojun
286762587Sitojun/*
286853541Sshin * Perform rate limit check.
286953541Sshin * Returns 0 if it is okay to send the icmp6 packet.
287053541Sshin * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
287153541Sshin * limitation.
287253541Sshin *
287353541Sshin * XXX per-destination/type check necessary?
287453541Sshin */
287553541Sshinstatic int
287653541Sshinicmp6_ratelimit(dst, type, code)
287753541Sshin	const struct in6_addr *dst;	/* not used at this moment */
287853541Sshin	const int type;			/* not used at this moment */
287953541Sshin	const int code;			/* not used at this moment */
288053541Sshin{
288162587Sitojun	int ret;
288253541Sshin
288395023Ssuz	ret = 0;	/* okay to send */
288453541Sshin
288562587Sitojun	/* PPS limit */
288678064Sume	if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
288778064Sume	    icmp6errppslim)) {
288853541Sshin		/* The packet is subject to rate limit */
288962587Sitojun		ret++;
289053541Sshin	}
289153541Sshin
289262587Sitojun	return ret;
289353541Sshin}
2894