Home | History | Annotate | Line # | Download | only in netinet6
icmp6.c revision 1.207
      1 /*	$NetBSD: icmp6.c,v 1.207 2017/02/02 02:52:10 ozaki-r Exp $	*/
      2 /*	$KAME: icmp6.c,v 1.217 2001/06/20 15:03:29 jinmei Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1988, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. Neither the name of the University nor the names of its contributors
     46  *    may be used to endorse or promote products derived from this software
     47  *    without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60  *
     61  *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: icmp6.c,v 1.207 2017/02/02 02:52:10 ozaki-r Exp $");
     66 
     67 #ifdef _KERNEL_OPT
     68 #include "opt_inet.h"
     69 #include "opt_ipsec.h"
     70 #endif
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/kmem.h>
     75 #include <sys/mbuf.h>
     76 #include <sys/protosw.h>
     77 #include <sys/socket.h>
     78 #include <sys/socketvar.h>
     79 #include <sys/time.h>
     80 #include <sys/kernel.h>
     81 #include <sys/syslog.h>
     82 #include <sys/domain.h>
     83 #include <sys/sysctl.h>
     84 
     85 #include <net/if.h>
     86 #include <net/route.h>
     87 #include <net/if_dl.h>
     88 #include <net/if_types.h>
     89 
     90 #include <netinet/in.h>
     91 #include <netinet/in_var.h>
     92 #include <netinet/ip6.h>
     93 #include <netinet/wqinput.h>
     94 #include <netinet6/ip6_var.h>
     95 #include <netinet6/ip6_private.h>
     96 #include <netinet/icmp6.h>
     97 #include <netinet6/icmp6_private.h>
     98 #include <netinet6/mld6_var.h>
     99 #include <netinet6/in6_pcb.h>
    100 #include <netinet6/nd6.h>
    101 #include <netinet6/in6_ifattach.h>
    102 #include <netinet6/ip6protosw.h>
    103 #include <netinet6/scope6_var.h>
    104 
    105 #ifdef IPSEC
    106 #include <netipsec/ipsec.h>
    107 #include <netipsec/key.h>
    108 #endif
    109 
    110 
    111 #include "faith.h"
    112 #if defined(NFAITH) && 0 < NFAITH
    113 #include <net/if_faith.h>
    114 #endif
    115 
    116 #include <net/net_osdep.h>
    117 
    118 extern struct domain inet6domain;
    119 
    120 percpu_t *icmp6stat_percpu;
    121 
    122 extern struct inpcbtable raw6cbtable;
    123 extern int icmp6errppslim;
    124 static int icmp6errpps_count = 0;
    125 static struct timeval icmp6errppslim_last;
    126 extern int icmp6_nodeinfo;
    127 
    128 /*
    129  * List of callbacks to notify when Path MTU changes are made.
    130  */
    131 struct icmp6_mtudisc_callback {
    132 	LIST_ENTRY(icmp6_mtudisc_callback) mc_list;
    133 	void (*mc_func)(struct in6_addr *);
    134 };
    135 
    136 LIST_HEAD(, icmp6_mtudisc_callback) icmp6_mtudisc_callbacks =
    137     LIST_HEAD_INITIALIZER(&icmp6_mtudisc_callbacks);
    138 
    139 static struct rttimer_queue *icmp6_mtudisc_timeout_q = NULL;
    140 extern int pmtu_expire;
    141 
    142 /* XXX do these values make any sense? */
    143 static int icmp6_mtudisc_hiwat = 1280;
    144 static int icmp6_mtudisc_lowat = 256;
    145 
    146 /*
    147  * keep track of # of redirect routes.
    148  */
    149 static struct rttimer_queue *icmp6_redirect_timeout_q = NULL;
    150 
    151 /* XXX experimental, turned off */
    152 static int icmp6_redirect_hiwat = -1;
    153 static int icmp6_redirect_lowat = -1;
    154 
    155 static void icmp6_errcount(u_int, int, int);
    156 static int icmp6_rip6_input(struct mbuf **, int);
    157 static int icmp6_ratelimit(const struct in6_addr *, const int, const int);
    158 static const char *icmp6_redirect_diag(char *, size_t, struct in6_addr *,
    159 	struct in6_addr *, struct in6_addr *);
    160 static struct mbuf *ni6_input(struct mbuf *, int);
    161 static struct mbuf *ni6_nametodns(const char *, int, int);
    162 static int ni6_dnsmatch(const char *, int, const char *, int);
    163 static int ni6_addrs(struct icmp6_nodeinfo *, struct mbuf *,
    164 			  struct ifnet **, char *, struct psref *);
    165 static int ni6_store_addrs(struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
    166 				struct ifnet *, int);
    167 static int icmp6_notify_error(struct mbuf *, int, int, int);
    168 static struct rtentry *icmp6_mtudisc_clone(struct sockaddr *);
    169 static void icmp6_mtudisc_timeout(struct rtentry *, struct rttimer *);
    170 static void icmp6_redirect_timeout(struct rtentry *, struct rttimer *);
    171 static void sysctl_net_inet6_icmp6_setup(struct sysctllog **);
    172 
    173 /* workqueue-based pr_input */
    174 static struct wqinput *icmp6_wqinput;
    175 static void _icmp6_input(struct mbuf *m, int off, int proto);
    176 
    177 void
    178 icmp6_init(void)
    179 {
    180 
    181 	sysctl_net_inet6_icmp6_setup(NULL);
    182 	mld_init();
    183 	icmp6_mtudisc_timeout_q = rt_timer_queue_create(pmtu_expire);
    184 	icmp6_redirect_timeout_q = rt_timer_queue_create(icmp6_redirtimeout);
    185 
    186 	icmp6stat_percpu = percpu_alloc(sizeof(uint64_t) * ICMP6_NSTATS);
    187 
    188 	icmp6_wqinput = wqinput_create("icmp6", _icmp6_input);
    189 }
    190 
    191 static void
    192 icmp6_errcount(u_int base, int type, int code)
    193 {
    194 	switch (type) {
    195 	case ICMP6_DST_UNREACH:
    196 		switch (code) {
    197 		case ICMP6_DST_UNREACH_NOROUTE:
    198 			ICMP6_STATINC(base + ICMP6_ERRSTAT_DST_UNREACH_NOROUTE);
    199 			return;
    200 		case ICMP6_DST_UNREACH_ADMIN:
    201 			ICMP6_STATINC(base + ICMP6_ERRSTAT_DST_UNREACH_ADMIN);
    202 			return;
    203 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
    204 			ICMP6_STATINC(base +
    205 				      ICMP6_ERRSTAT_DST_UNREACH_BEYONDSCOPE);
    206 			return;
    207 		case ICMP6_DST_UNREACH_ADDR:
    208 			ICMP6_STATINC(base + ICMP6_ERRSTAT_DST_UNREACH_ADDR);
    209 			return;
    210 		case ICMP6_DST_UNREACH_NOPORT:
    211 			ICMP6_STATINC(base + ICMP6_ERRSTAT_DST_UNREACH_NOPORT);
    212 			return;
    213 		}
    214 		break;
    215 	case ICMP6_PACKET_TOO_BIG:
    216 		ICMP6_STATINC(base + ICMP6_ERRSTAT_PACKET_TOO_BIG);
    217 		return;
    218 	case ICMP6_TIME_EXCEEDED:
    219 		switch (code) {
    220 		case ICMP6_TIME_EXCEED_TRANSIT:
    221 			ICMP6_STATINC(base + ICMP6_ERRSTAT_TIME_EXCEED_TRANSIT);
    222 			return;
    223 		case ICMP6_TIME_EXCEED_REASSEMBLY:
    224 			ICMP6_STATINC(base +
    225 				      ICMP6_ERRSTAT_TIME_EXCEED_REASSEMBLY);
    226 			return;
    227 		}
    228 		break;
    229 	case ICMP6_PARAM_PROB:
    230 		switch (code) {
    231 		case ICMP6_PARAMPROB_HEADER:
    232 			ICMP6_STATINC(base + ICMP6_ERRSTAT_PARAMPROB_HEADER);
    233 			return;
    234 		case ICMP6_PARAMPROB_NEXTHEADER:
    235 			ICMP6_STATINC(base +
    236 				      ICMP6_ERRSTAT_PARAMPROB_NEXTHEADER);
    237 			return;
    238 		case ICMP6_PARAMPROB_OPTION:
    239 			ICMP6_STATINC(base + ICMP6_ERRSTAT_PARAMPROB_OPTION);
    240 			return;
    241 		}
    242 		break;
    243 	case ND_REDIRECT:
    244 		ICMP6_STATINC(base + ICMP6_ERRSTAT_REDIRECT);
    245 		return;
    246 	}
    247 	ICMP6_STATINC(base + ICMP6_ERRSTAT_UNKNOWN);
    248 }
    249 
    250 /*
    251  * Register a Path MTU Discovery callback.
    252  */
    253 void
    254 icmp6_mtudisc_callback_register(void (*func)(struct in6_addr *))
    255 {
    256 	struct icmp6_mtudisc_callback *mc;
    257 
    258 	for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL;
    259 	     mc = LIST_NEXT(mc, mc_list)) {
    260 		if (mc->mc_func == func)
    261 			return;
    262 	}
    263 
    264 	mc = kmem_alloc(sizeof(*mc), KM_SLEEP);
    265 	mc->mc_func = func;
    266 	LIST_INSERT_HEAD(&icmp6_mtudisc_callbacks, mc, mc_list);
    267 }
    268 
    269 /*
    270  * A wrapper function for icmp6_error() necessary when the erroneous packet
    271  * may not contain enough scope zone information.
    272  */
    273 void
    274 icmp6_error2(struct mbuf *m, int type, int code, int param,
    275 	struct ifnet *ifp)
    276 {
    277 	struct ip6_hdr *ip6;
    278 
    279 	if (ifp == NULL)
    280 		return;
    281 
    282 	if (m->m_len < sizeof(struct ip6_hdr)) {
    283 		m = m_pullup(m, sizeof(struct ip6_hdr));
    284 		if (m == NULL)
    285 			return;
    286 	}
    287 
    288 	ip6 = mtod(m, struct ip6_hdr *);
    289 
    290 	if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0)
    291 		return;
    292 	if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
    293 		return;
    294 
    295 	icmp6_error(m, type, code, param);
    296 }
    297 
    298 /*
    299  * Generate an error packet of type error in response to bad IP6 packet.
    300  */
    301 void
    302 icmp6_error(struct mbuf *m, int type, int code, int param)
    303 {
    304 	struct ip6_hdr *oip6, *nip6;
    305 	struct icmp6_hdr *icmp6;
    306 	u_int preplen;
    307 	int off;
    308 	int nxt;
    309 
    310 	ICMP6_STATINC(ICMP6_STAT_ERROR);
    311 
    312 	/* count per-type-code statistics */
    313 	icmp6_errcount(ICMP6_STAT_OUTERRHIST, type, code);
    314 
    315 	if (m->m_flags & M_DECRYPTED) {
    316 		ICMP6_STATINC(ICMP6_STAT_CANTERROR);
    317 		goto freeit;
    318 	}
    319 
    320 	if (M_UNWRITABLE(m, sizeof(struct ip6_hdr)) &&
    321 	    (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL)
    322 		return;
    323 	oip6 = mtod(m, struct ip6_hdr *);
    324 
    325 	/*
    326 	 * If the destination address of the erroneous packet is a multicast
    327 	 * address, or the packet was sent using link-layer multicast,
    328 	 * we should basically suppress sending an error (RFC 2463, Section
    329 	 * 2.4).
    330 	 * We have two exceptions (the item e.2 in that section):
    331 	 * - the Pakcet Too Big message can be sent for path MTU discovery.
    332 	 * - the Parameter Problem Message that can be allowed an icmp6 error
    333 	 *   in the option type field.  This check has been done in
    334 	 *   ip6_unknown_opt(), so we can just check the type and code.
    335 	 */
    336 	if ((m->m_flags & (M_BCAST|M_MCAST) ||
    337 	     IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
    338 	    (type != ICMP6_PACKET_TOO_BIG &&
    339 	     (type != ICMP6_PARAM_PROB ||
    340 	      code != ICMP6_PARAMPROB_OPTION)))
    341 		goto freeit;
    342 
    343 	/*
    344 	 * RFC 2463, 2.4 (e.5): source address check.
    345 	 * XXX: the case of anycast source?
    346 	 */
    347 	if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
    348 	    IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
    349 		goto freeit;
    350 
    351 	/*
    352 	 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
    353 	 * don't do it.
    354 	 */
    355 	nxt = -1;
    356 	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
    357 	if (off >= 0 && nxt == IPPROTO_ICMPV6) {
    358 		struct icmp6_hdr *icp;
    359 
    360 		IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
    361 			sizeof(*icp));
    362 		if (icp == NULL) {
    363 			ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    364 			return;
    365 		}
    366 		if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
    367 		    icp->icmp6_type == ND_REDIRECT) {
    368 			/*
    369 			 * ICMPv6 error
    370 			 * Special case: for redirect (which is
    371 			 * informational) we must not send icmp6 error.
    372 			 */
    373 			ICMP6_STATINC(ICMP6_STAT_CANTERROR);
    374 			goto freeit;
    375 		} else {
    376 			/* ICMPv6 informational - send the error */
    377 		}
    378 	}
    379 #if 0 /* controversial */
    380 	else if (off >= 0 && nxt == IPPROTO_ESP) {
    381 		/*
    382 		 * It could be ICMPv6 error inside ESP.  Take a safer side,
    383 		 * don't respond.
    384 		 */
    385 		ICMP6_STATINC(ICMP6_STAT_CANTERROR);
    386 		goto freeit;
    387 	}
    388 #endif
    389 	else {
    390 		/* non-ICMPv6 - send the error */
    391 	}
    392 
    393 	oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
    394 
    395 	/* Finally, do rate limitation check. */
    396 	if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
    397 		ICMP6_STATINC(ICMP6_STAT_TOOFREQ);
    398 		goto freeit;
    399 	}
    400 
    401 	/*
    402 	 * OK, ICMP6 can be generated.
    403 	 */
    404 
    405 	if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
    406 		m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
    407 
    408 	preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
    409 	M_PREPEND(m, preplen, M_DONTWAIT);
    410 	if (m && M_UNWRITABLE(m, preplen))
    411 		m = m_pullup(m, preplen);
    412 	if (m == NULL) {
    413 		nd6log(LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__);
    414 		return;
    415 	}
    416 
    417 	nip6 = mtod(m, struct ip6_hdr *);
    418 	nip6->ip6_src  = oip6->ip6_src;
    419 	nip6->ip6_dst  = oip6->ip6_dst;
    420 
    421 	in6_clearscope(&oip6->ip6_src);
    422 	in6_clearscope(&oip6->ip6_dst);
    423 
    424 	icmp6 = (struct icmp6_hdr *)(nip6 + 1);
    425 	icmp6->icmp6_type = type;
    426 	icmp6->icmp6_code = code;
    427 	icmp6->icmp6_pptr = htonl((u_int32_t)param);
    428 
    429 	/*
    430 	 * icmp6_reflect() is designed to be in the input path.
    431 	 * icmp6_error() can be called from both input and output path,
    432 	 * and if we are in output path rcvif could contain bogus value.
    433 	 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
    434 	 * information in ip header (nip6).
    435 	 */
    436 	m_reset_rcvif(m);
    437 
    438 	ICMP6_STATINC(ICMP6_STAT_OUTHIST + type);
    439 	icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
    440 
    441 	return;
    442 
    443   freeit:
    444 	/*
    445 	 * If we can't tell whether or not we can generate ICMP6, free it.
    446 	 */
    447 	m_freem(m);
    448 }
    449 
    450 /*
    451  * Process a received ICMP6 message.
    452  */
    453 static void
    454 _icmp6_input(struct mbuf *m, int off, int proto)
    455 {
    456 	struct mbuf *n;
    457 	struct ip6_hdr *ip6, *nip6;
    458 	struct icmp6_hdr *icmp6, *nicmp6;
    459 	int icmp6len = m->m_pkthdr.len - off;
    460 	int code, sum, noff;
    461 	struct ifnet *rcvif;
    462 	struct psref psref;
    463 	char ip6buf[INET6_ADDRSTRLEN], ip6buf2[INET6_ADDRSTRLEN];
    464 
    465 	rcvif = m_get_rcvif_psref(m, &psref);
    466 	if (__predict_false(rcvif == NULL))
    467 		goto freeit;
    468 
    469 #define ICMP6_MAXLEN (sizeof(*nip6) + sizeof(*nicmp6) + 4)
    470 	KASSERT(ICMP6_MAXLEN < MCLBYTES);
    471 	icmp6_ifstat_inc(rcvif, ifs6_in_msg);
    472 
    473 	/*
    474 	 * Locate icmp6 structure in mbuf, and check
    475 	 * that not corrupted and of at least minimum length
    476 	 */
    477 
    478 	if (icmp6len < sizeof(struct icmp6_hdr)) {
    479 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    480 		icmp6_ifstat_inc(rcvif, ifs6_in_error);
    481 		goto freeit;
    482 	}
    483 
    484 	ip6 = mtod(m, struct ip6_hdr *);
    485 	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
    486 	if (icmp6 == NULL) {
    487 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    488 		icmp6_ifstat_inc(rcvif, ifs6_in_error);
    489 		goto freeit;
    490 	}
    491 	/*
    492 	 * Enforce alignment requirements that are violated in
    493 	 * some cases, see kern/50766 for details.
    494 	 */
    495 	if (IP6_HDR_ALIGNED_P(icmp6) == 0) {
    496 		m = m_copyup(m, off + sizeof(struct icmp6_hdr), 0);
    497 		if (m == NULL) {
    498 			ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    499 			icmp6_ifstat_inc(rcvif, ifs6_in_error);
    500 			goto freeit;
    501 		}
    502 		ip6 = mtod(m, struct ip6_hdr *);
    503 		icmp6 = (struct icmp6_hdr *)(ip6 + 1);
    504 	}
    505 	KASSERT(IP6_HDR_ALIGNED_P(icmp6));
    506 
    507 	/*
    508 	 * calculate the checksum
    509 	 */
    510 	if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
    511 		nd6log(LOG_ERR, "ICMP6 checksum error(%d|%x) %s\n",
    512 		    icmp6->icmp6_type, sum, IN6_PRINT(ip6buf, &ip6->ip6_src));
    513 		ICMP6_STATINC(ICMP6_STAT_CHECKSUM);
    514 		icmp6_ifstat_inc(rcvif, ifs6_in_error);
    515 		goto freeit;
    516 	}
    517 
    518 #if defined(NFAITH) && 0 < NFAITH
    519 	if (faithprefix(&ip6->ip6_dst)) {
    520 		/*
    521 		 * Deliver very specific ICMP6 type only.
    522 		 * This is important to deliver TOOBIG.  Otherwise PMTUD
    523 		 * will not work.
    524 		 */
    525 		switch (icmp6->icmp6_type) {
    526 		case ICMP6_DST_UNREACH:
    527 		case ICMP6_PACKET_TOO_BIG:
    528 		case ICMP6_TIME_EXCEEDED:
    529 			break;
    530 		default:
    531 			goto freeit;
    532 		}
    533 	}
    534 #endif
    535 
    536 	code = icmp6->icmp6_code;
    537 	ICMP6_STATINC(ICMP6_STAT_INHIST + icmp6->icmp6_type);
    538 
    539 	switch (icmp6->icmp6_type) {
    540 	case ICMP6_DST_UNREACH:
    541 		icmp6_ifstat_inc(rcvif, ifs6_in_dstunreach);
    542 		switch (code) {
    543 		case ICMP6_DST_UNREACH_NOROUTE:
    544 			code = PRC_UNREACH_NET;
    545 			break;
    546 		case ICMP6_DST_UNREACH_ADMIN:
    547 			icmp6_ifstat_inc(rcvif, ifs6_in_adminprohib);
    548 			code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
    549 			break;
    550 		case ICMP6_DST_UNREACH_ADDR:
    551 			code = PRC_HOSTDEAD;
    552 			break;
    553 #ifdef COMPAT_RFC1885
    554 		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
    555 			code = PRC_UNREACH_SRCFAIL;
    556 			break;
    557 #else
    558 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
    559 			/* I mean "source address was incorrect." */
    560 			code = PRC_UNREACH_NET;
    561 			break;
    562 #endif
    563 		case ICMP6_DST_UNREACH_NOPORT:
    564 			code = PRC_UNREACH_PORT;
    565 			break;
    566 		default:
    567 			goto badcode;
    568 		}
    569 		goto deliver;
    570 
    571 	case ICMP6_PACKET_TOO_BIG:
    572 		icmp6_ifstat_inc(rcvif, ifs6_in_pkttoobig);
    573 
    574 		/*
    575 		 * MTU is checked in icmp6_mtudisc.
    576 		 */
    577 		code = PRC_MSGSIZE;
    578 
    579 		/*
    580 		 * Updating the path MTU will be done after examining
    581 		 * intermediate extension headers.
    582 		 */
    583 		goto deliver;
    584 
    585 	case ICMP6_TIME_EXCEEDED:
    586 		icmp6_ifstat_inc(rcvif, ifs6_in_timeexceed);
    587 		switch (code) {
    588 		case ICMP6_TIME_EXCEED_TRANSIT:
    589 			code = PRC_TIMXCEED_INTRANS;
    590 			break;
    591 		case ICMP6_TIME_EXCEED_REASSEMBLY:
    592 			code = PRC_TIMXCEED_REASS;
    593 			break;
    594 		default:
    595 			goto badcode;
    596 		}
    597 		goto deliver;
    598 
    599 	case ICMP6_PARAM_PROB:
    600 		icmp6_ifstat_inc(rcvif, ifs6_in_paramprob);
    601 		switch (code) {
    602 		case ICMP6_PARAMPROB_NEXTHEADER:
    603 			code = PRC_UNREACH_PROTOCOL;
    604 			break;
    605 		case ICMP6_PARAMPROB_HEADER:
    606 		case ICMP6_PARAMPROB_OPTION:
    607 			code = PRC_PARAMPROB;
    608 			break;
    609 		default:
    610 			goto badcode;
    611 		}
    612 		goto deliver;
    613 
    614 	case ICMP6_ECHO_REQUEST:
    615 		icmp6_ifstat_inc(rcvif, ifs6_in_echo);
    616 		if (code != 0)
    617 			goto badcode;
    618 		/*
    619 		 * Copy mbuf to send to two data paths: userland socket(s),
    620 		 * and to the querier (echo reply).
    621 		 * m: a copy for socket, n: a copy for querier
    622 		 *
    623 		 * If the first mbuf is shared, or the first mbuf is too short,
    624 		 * copy the first part of the data into a fresh mbuf.
    625 		 * Otherwise, we will wrongly overwrite both copies.
    626 		 */
    627 		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    628 			/* Give up local */
    629 			n = m;
    630 			m = NULL;
    631 		} else if (M_READONLY(n) ||
    632 		    n->m_len < off + sizeof(struct icmp6_hdr)) {
    633 			struct mbuf *n0 = n;
    634 
    635 			/*
    636 			 * Prepare an internal mbuf.  m_pullup() doesn't
    637 			 * always copy the length we specified.
    638 			 */
    639 			if ((n = m_dup(n0, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    640 				/* Give up local */
    641 				n = m;
    642 				m = NULL;
    643 			}
    644 			m_freem(n0);
    645 		}
    646 		IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off,
    647 		    sizeof(*nicmp6));
    648 		if (nicmp6 == NULL)
    649 			goto freeit;
    650 		nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
    651 		nicmp6->icmp6_code = 0;
    652 		if (n) {
    653 			uint64_t *icmp6s = ICMP6_STAT_GETREF();
    654 			icmp6s[ICMP6_STAT_REFLECT]++;
    655 			icmp6s[ICMP6_STAT_OUTHIST + ICMP6_ECHO_REPLY]++;
    656 			ICMP6_STAT_PUTREF();
    657 			icmp6_reflect(n, off);
    658 		}
    659 		if (!m)
    660 			goto freeit;
    661 		break;
    662 
    663 	case ICMP6_ECHO_REPLY:
    664 		icmp6_ifstat_inc(rcvif, ifs6_in_echoreply);
    665 		if (code != 0)
    666 			goto badcode;
    667 		break;
    668 
    669 	case MLD_LISTENER_QUERY:
    670 	case MLD_LISTENER_REPORT:
    671 		if (icmp6len < sizeof(struct mld_hdr))
    672 			goto badlen;
    673 		if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
    674 			icmp6_ifstat_inc(rcvif, ifs6_in_mldquery);
    675 		else
    676 			icmp6_ifstat_inc(rcvif, ifs6_in_mldreport);
    677 		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    678 			/* give up local */
    679 			mld_input(m, off);
    680 			m = NULL;
    681 			goto freeit;
    682 		}
    683 		mld_input(n, off);
    684 		/* m stays. */
    685 		break;
    686 
    687 	case MLD_LISTENER_DONE:
    688 		icmp6_ifstat_inc(rcvif, ifs6_in_mlddone);
    689 		if (icmp6len < sizeof(struct mld_hdr))	/* necessary? */
    690 			goto badlen;
    691 		break;		/* nothing to be done in kernel */
    692 
    693 	case MLD_MTRACE_RESP:
    694 	case MLD_MTRACE:
    695 		/* XXX: these two are experimental.  not officially defined. */
    696 		/* XXX: per-interface statistics? */
    697 		break;		/* just pass it to applications */
    698 
    699 	case ICMP6_WRUREQUEST:	/* ICMP6_FQDN_QUERY */
    700 	    {
    701 		enum { WRU, FQDN } mode;
    702 
    703 		if (!icmp6_nodeinfo)
    704 			break;
    705 
    706 		if (icmp6len == sizeof(struct icmp6_hdr) + 4)
    707 			mode = WRU;
    708 		else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
    709 			mode = FQDN;
    710 		else
    711 			goto badlen;
    712 
    713 		if (mode == FQDN) {
    714 			n = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
    715 			if (n)
    716 				n = ni6_input(n, off);
    717 			/* XXX meaningless if n == NULL */
    718 			noff = sizeof(struct ip6_hdr);
    719 		} else {
    720 			u_char *p;
    721 			int maxhlen;
    722 
    723 			if ((icmp6_nodeinfo & 5) != 5)
    724 				break;
    725 
    726 			if (code != 0)
    727 				goto badcode;
    728 			MGETHDR(n, M_DONTWAIT, m->m_type);
    729 			if (n && ICMP6_MAXLEN > MHLEN) {
    730 				MCLGET(n, M_DONTWAIT);
    731 				if ((n->m_flags & M_EXT) == 0) {
    732 					m_free(n);
    733 					n = NULL;
    734 				}
    735 			}
    736 			if (n == NULL) {
    737 				/* Give up remote */
    738 				break;
    739 			}
    740 			m_reset_rcvif(n);
    741 			n->m_len = 0;
    742 			maxhlen = M_TRAILINGSPACE(n) - ICMP6_MAXLEN;
    743 			if (maxhlen < 0)
    744 				break;
    745 			if (maxhlen > hostnamelen)
    746 				maxhlen = hostnamelen;
    747 			/*
    748 			 * Copy IPv6 and ICMPv6 only.
    749 			 */
    750 			nip6 = mtod(n, struct ip6_hdr *);
    751 			bcopy(ip6, nip6, sizeof(struct ip6_hdr));
    752 			nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
    753 			bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
    754 			p = (u_char *)(nicmp6 + 1);
    755 			memset(p, 0, 4);
    756 			bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
    757 			noff = sizeof(struct ip6_hdr);
    758 			M_COPY_PKTHDR(n, m); /* just for rcvif */
    759 			n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
    760 				sizeof(struct icmp6_hdr) + 4 + maxhlen;
    761 			nicmp6->icmp6_type = ICMP6_WRUREPLY;
    762 			nicmp6->icmp6_code = 0;
    763 		}
    764 #undef hostnamelen
    765 		if (n) {
    766 			uint64_t *icmp6s = ICMP6_STAT_GETREF();
    767 			icmp6s[ICMP6_STAT_REFLECT]++;
    768 			icmp6s[ICMP6_STAT_OUTHIST + ICMP6_WRUREPLY]++;
    769 			ICMP6_STAT_PUTREF();
    770 			icmp6_reflect(n, noff);
    771 		}
    772 		break;
    773 	    }
    774 
    775 	case ICMP6_WRUREPLY:
    776 		if (code != 0)
    777 			goto badcode;
    778 		break;
    779 
    780 	case ND_ROUTER_SOLICIT:
    781 		icmp6_ifstat_inc(rcvif, ifs6_in_routersolicit);
    782 		if (code != 0)
    783 			goto badcode;
    784 		if (icmp6len < sizeof(struct nd_router_solicit))
    785 			goto badlen;
    786 		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    787 			/* give up local */
    788 			nd6_rs_input(m, off, icmp6len);
    789 			m = NULL;
    790 			goto freeit;
    791 		}
    792 		nd6_rs_input(n, off, icmp6len);
    793 		/* m stays. */
    794 		break;
    795 
    796 	case ND_ROUTER_ADVERT:
    797 		icmp6_ifstat_inc(rcvif, ifs6_in_routeradvert);
    798 		if (code != 0)
    799 			goto badcode;
    800 		if (icmp6len < sizeof(struct nd_router_advert))
    801 			goto badlen;
    802 		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    803 			/* give up local */
    804 			nd6_ra_input(m, off, icmp6len);
    805 			m = NULL;
    806 			goto freeit;
    807 		}
    808 		nd6_ra_input(n, off, icmp6len);
    809 		/* m stays. */
    810 		break;
    811 
    812 	case ND_NEIGHBOR_SOLICIT:
    813 		icmp6_ifstat_inc(rcvif, ifs6_in_neighborsolicit);
    814 		if (code != 0)
    815 			goto badcode;
    816 		if (icmp6len < sizeof(struct nd_neighbor_solicit))
    817 			goto badlen;
    818 		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    819 			/* give up local */
    820 			nd6_ns_input(m, off, icmp6len);
    821 			m = NULL;
    822 			goto freeit;
    823 		}
    824 		nd6_ns_input(n, off, icmp6len);
    825 		/* m stays. */
    826 		break;
    827 
    828 	case ND_NEIGHBOR_ADVERT:
    829 		icmp6_ifstat_inc(rcvif, ifs6_in_neighboradvert);
    830 		if (code != 0)
    831 			goto badcode;
    832 		if (icmp6len < sizeof(struct nd_neighbor_advert))
    833 			goto badlen;
    834 		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    835 			/* give up local */
    836 			nd6_na_input(m, off, icmp6len);
    837 			m = NULL;
    838 			goto freeit;
    839 		}
    840 		nd6_na_input(n, off, icmp6len);
    841 		/* m stays. */
    842 		break;
    843 
    844 	case ND_REDIRECT:
    845 		icmp6_ifstat_inc(rcvif, ifs6_in_redirect);
    846 		if (code != 0)
    847 			goto badcode;
    848 		if (icmp6len < sizeof(struct nd_redirect))
    849 			goto badlen;
    850 		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
    851 			/* give up local */
    852 			icmp6_redirect_input(m, off);
    853 			m = NULL;
    854 			goto freeit;
    855 		}
    856 		icmp6_redirect_input(n, off);
    857 		/* m stays. */
    858 		break;
    859 
    860 	case ICMP6_ROUTER_RENUMBERING:
    861 		if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
    862 		    code != ICMP6_ROUTER_RENUMBERING_RESULT)
    863 			goto badcode;
    864 		if (icmp6len < sizeof(struct icmp6_router_renum))
    865 			goto badlen;
    866 		break;
    867 
    868 	default:
    869 		nd6log(LOG_DEBUG,
    870 		    "unknown type %d(src=%s, dst=%s, ifid=%d)\n",
    871 		    icmp6->icmp6_type,
    872 		    IN6_PRINT(ip6buf, &ip6->ip6_src),
    873 		    IN6_PRINT(ip6buf2, &ip6->ip6_dst),
    874 		    rcvif ? rcvif->if_index : 0);
    875 		if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
    876 			/* ICMPv6 error: MUST deliver it by spec... */
    877 			code = PRC_NCMDS;
    878 			/* deliver */
    879 		} else {
    880 			/* ICMPv6 informational: MUST not deliver */
    881 			break;
    882 		}
    883 	deliver:
    884 		if (icmp6_notify_error(m, off, icmp6len, code)) {
    885 			/* In this case, m should've been freed. */
    886 			m_put_rcvif_psref(rcvif, &psref);
    887 			return;
    888 		}
    889 		break;
    890 
    891 	badcode:
    892 		ICMP6_STATINC(ICMP6_STAT_BADCODE);
    893 		break;
    894 
    895 	badlen:
    896 		ICMP6_STATINC(ICMP6_STAT_BADLEN);
    897 		break;
    898 	}
    899 	m_put_rcvif_psref(rcvif, &psref);
    900 
    901 	/* deliver the packet to appropriate sockets */
    902 	icmp6_rip6_input(&m, off);
    903 
    904 	return;
    905 
    906  freeit:
    907 	m_put_rcvif_psref(rcvif, &psref);
    908 	m_freem(m);
    909 	return;
    910 }
    911 
    912 int
    913 icmp6_input(struct mbuf **mp, int *offp, int proto)
    914 {
    915 
    916 	wqinput_input(icmp6_wqinput, *mp, *offp, proto);
    917 
    918 	return IPPROTO_DONE;
    919 }
    920 
    921 static int
    922 icmp6_notify_error(struct mbuf *m, int off, int icmp6len, int code)
    923 {
    924 	struct icmp6_hdr *icmp6;
    925 	struct ip6_hdr *eip6;
    926 	u_int32_t notifymtu;
    927 	struct sockaddr_in6 icmp6src, icmp6dst;
    928 
    929 	if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
    930 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    931 		goto freeit;
    932 	}
    933 	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
    934 		       sizeof(*icmp6) + sizeof(struct ip6_hdr));
    935 	if (icmp6 == NULL) {
    936 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    937 		return (-1);
    938 	}
    939 	eip6 = (struct ip6_hdr *)(icmp6 + 1);
    940 
    941 	/* Detect the upper level protocol */
    942 	{
    943 		void (*ctlfunc)(int, struct sockaddr *, void *);
    944 		u_int8_t nxt = eip6->ip6_nxt;
    945 		int eoff = off + sizeof(struct icmp6_hdr) +
    946 			sizeof(struct ip6_hdr);
    947 		struct ip6ctlparam ip6cp;
    948 		struct in6_addr *finaldst = NULL;
    949 		int icmp6type = icmp6->icmp6_type;
    950 		struct ip6_frag *fh;
    951 		struct ip6_rthdr *rth;
    952 		struct ip6_rthdr0 *rth0;
    953 		int rthlen;
    954 		struct ifnet *rcvif;
    955 		int s;
    956 
    957 		while (1) { /* XXX: should avoid infinite loop explicitly? */
    958 			struct ip6_ext *eh;
    959 
    960 			switch (nxt) {
    961 			case IPPROTO_HOPOPTS:
    962 			case IPPROTO_DSTOPTS:
    963 			case IPPROTO_AH:
    964 				IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
    965 					       eoff, sizeof(*eh));
    966 				if (eh == NULL) {
    967 					ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    968 					return (-1);
    969 				}
    970 
    971 				if (nxt == IPPROTO_AH)
    972 					eoff += (eh->ip6e_len + 2) << 2;
    973 				else
    974 					eoff += (eh->ip6e_len + 1) << 3;
    975 				nxt = eh->ip6e_nxt;
    976 				break;
    977 			case IPPROTO_ROUTING:
    978 				/*
    979 				 * When the erroneous packet contains a
    980 				 * routing header, we should examine the
    981 				 * header to determine the final destination.
    982 				 * Otherwise, we can't properly update
    983 				 * information that depends on the final
    984 				 * destination (e.g. path MTU).
    985 				 */
    986 				IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
    987 					       eoff, sizeof(*rth));
    988 				if (rth == NULL) {
    989 					ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    990 					return (-1);
    991 				}
    992 				rthlen = (rth->ip6r_len + 1) << 3;
    993 				/*
    994 				 * XXX: currently there is no
    995 				 * officially defined type other
    996 				 * than type-0.
    997 				 * Note that if the segment left field
    998 				 * is 0, all intermediate hops must
    999 				 * have been passed.
   1000 				 */
   1001 				if (rth->ip6r_segleft &&
   1002 				    rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
   1003 					int hops;
   1004 
   1005 					IP6_EXTHDR_GET(rth0,
   1006 						       struct ip6_rthdr0 *, m,
   1007 						       eoff, rthlen);
   1008 					if (rth0 == NULL) {
   1009 						ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
   1010 						return (-1);
   1011 					}
   1012 					/* just ignore a bogus header */
   1013 					if ((rth0->ip6r0_len % 2) == 0 &&
   1014 					    (hops = rth0->ip6r0_len/2))
   1015 						finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
   1016 				}
   1017 				eoff += rthlen;
   1018 				nxt = rth->ip6r_nxt;
   1019 				break;
   1020 			case IPPROTO_FRAGMENT:
   1021 				IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
   1022 					       eoff, sizeof(*fh));
   1023 				if (fh == NULL) {
   1024 					ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
   1025 					return (-1);
   1026 				}
   1027 				/*
   1028 				 * Data after a fragment header is meaningless
   1029 				 * unless it is the first fragment, but
   1030 				 * we'll go to the notify label for path MTU
   1031 				 * discovery.
   1032 				 */
   1033 				if (fh->ip6f_offlg & IP6F_OFF_MASK)
   1034 					goto notify;
   1035 
   1036 				eoff += sizeof(struct ip6_frag);
   1037 				nxt = fh->ip6f_nxt;
   1038 				break;
   1039 			default:
   1040 				/*
   1041 				 * This case includes ESP and the No Next
   1042 				 * Header.  In such cases going to the notify
   1043 				 * label does not have any meaning
   1044 				 * (i.e. ctlfunc will be NULL), but we go
   1045 				 * anyway since we might have to update
   1046 				 * path MTU information.
   1047 				 */
   1048 				goto notify;
   1049 			}
   1050 		}
   1051 	  notify:
   1052 		IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
   1053 			       sizeof(*icmp6) + sizeof(struct ip6_hdr));
   1054 		if (icmp6 == NULL) {
   1055 			ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
   1056 			return (-1);
   1057 		}
   1058 
   1059 		/*
   1060 		 * retrieve parameters from the inner IPv6 header, and convert
   1061 		 * them into sockaddr structures.
   1062 		 * XXX: there is no guarantee that the source or destination
   1063 		 * addresses of the inner packet are in the same scope zone as
   1064 		 * the addresses of the icmp packet.  But there is no other
   1065 		 * way to determine the zone.
   1066 		 */
   1067 		eip6 = (struct ip6_hdr *)(icmp6 + 1);
   1068 
   1069 		rcvif = m_get_rcvif(m, &s);
   1070 		sockaddr_in6_init(&icmp6dst,
   1071 		    (finaldst == NULL) ? &eip6->ip6_dst : finaldst, 0, 0, 0);
   1072 		if (in6_setscope(&icmp6dst.sin6_addr, rcvif, NULL)) {
   1073 			m_put_rcvif(rcvif, &s);
   1074 			goto freeit;
   1075 		}
   1076 		sockaddr_in6_init(&icmp6src, &eip6->ip6_src, 0, 0, 0);
   1077 		if (in6_setscope(&icmp6src.sin6_addr, rcvif, NULL)) {
   1078 			m_put_rcvif(rcvif, &s);
   1079 			goto freeit;
   1080 		}
   1081 		m_put_rcvif(rcvif, &s);
   1082 
   1083 		icmp6src.sin6_flowinfo =
   1084 			(eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
   1085 
   1086 		if (finaldst == NULL)
   1087 			finaldst = &eip6->ip6_dst;
   1088 		ip6cp.ip6c_m = m;
   1089 		ip6cp.ip6c_icmp6 = icmp6;
   1090 		ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
   1091 		ip6cp.ip6c_off = eoff;
   1092 		ip6cp.ip6c_finaldst = finaldst;
   1093 		ip6cp.ip6c_src = &icmp6src;
   1094 		ip6cp.ip6c_nxt = nxt;
   1095 
   1096 		if (icmp6type == ICMP6_PACKET_TOO_BIG) {
   1097 			notifymtu = ntohl(icmp6->icmp6_mtu);
   1098 			ip6cp.ip6c_cmdarg = (void *)&notifymtu;
   1099 		}
   1100 
   1101 		ctlfunc = (void (*)(int, struct sockaddr *, void *))
   1102 			(inet6sw[ip6_protox[nxt]].pr_ctlinput);
   1103 		if (ctlfunc) {
   1104 			(void) (*ctlfunc)(code, sin6tosa(&icmp6dst),
   1105 					  &ip6cp);
   1106 		}
   1107 	}
   1108 	return (0);
   1109 
   1110   freeit:
   1111 	m_freem(m);
   1112 	return (-1);
   1113 }
   1114 
   1115 void
   1116 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
   1117 {
   1118 	unsigned long rtcount;
   1119 	struct icmp6_mtudisc_callback *mc;
   1120 	struct in6_addr *dst = ip6cp->ip6c_finaldst;
   1121 	struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
   1122 	struct mbuf *m = ip6cp->ip6c_m;	/* will be necessary for scope issue */
   1123 	u_int mtu = ntohl(icmp6->icmp6_mtu);
   1124 	struct rtentry *rt = NULL;
   1125 	struct sockaddr_in6 sin6;
   1126 	struct ifnet *rcvif;
   1127 	int s;
   1128 
   1129 	/*
   1130 	 * The MTU should not be less than the minimal IPv6 MTU except for the
   1131 	 * hack in ip6_output/ip6_setpmtu where we always include a frag header.
   1132 	 * In that one case, the MTU might be less than 1280.
   1133 	 */
   1134 	if (__predict_false(mtu < IPV6_MMTU - sizeof(struct ip6_frag))) {
   1135 		/* is the mtu even sane? */
   1136 		if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
   1137 			return;
   1138 		if (!validated)
   1139 			return;
   1140 		mtu = IPV6_MMTU - sizeof(struct ip6_frag);
   1141 	}
   1142 
   1143 	/*
   1144 	 * allow non-validated cases if memory is plenty, to make traffic
   1145 	 * from non-connected pcb happy.
   1146 	 */
   1147 	rtcount = rt_timer_count(icmp6_mtudisc_timeout_q);
   1148 	if (validated) {
   1149 		if (0 <= icmp6_mtudisc_hiwat && rtcount > icmp6_mtudisc_hiwat)
   1150 			return;
   1151 		else if (0 <= icmp6_mtudisc_lowat &&
   1152 		    rtcount > icmp6_mtudisc_lowat) {
   1153 			/*
   1154 			 * XXX nuke a victim, install the new one.
   1155 			 */
   1156 		}
   1157 	} else {
   1158 		if (0 <= icmp6_mtudisc_lowat && rtcount > icmp6_mtudisc_lowat)
   1159 			return;
   1160 	}
   1161 
   1162 	memset(&sin6, 0, sizeof(sin6));
   1163 	sin6.sin6_family = PF_INET6;
   1164 	sin6.sin6_len = sizeof(struct sockaddr_in6);
   1165 	sin6.sin6_addr = *dst;
   1166 	rcvif = m_get_rcvif(m, &s);
   1167 	if (in6_setscope(&sin6.sin6_addr, rcvif, NULL)) {
   1168 		m_put_rcvif(rcvif, &s);
   1169 		return;
   1170 	}
   1171 	m_put_rcvif(rcvif, &s);
   1172 
   1173 	rt = icmp6_mtudisc_clone(sin6tosa(&sin6));
   1174 
   1175 	if (rt && (rt->rt_flags & RTF_HOST) &&
   1176 	    !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
   1177 	    (rt->rt_rmx.rmx_mtu > mtu || rt->rt_rmx.rmx_mtu == 0)) {
   1178 		if (mtu < IN6_LINKMTU(rt->rt_ifp)) {
   1179 			ICMP6_STATINC(ICMP6_STAT_PMTUCHG);
   1180 			rt->rt_rmx.rmx_mtu = mtu;
   1181 		}
   1182 	}
   1183 	if (rt) {
   1184 		rt_unref(rt);
   1185 	}
   1186 
   1187 	/*
   1188 	 * Notify protocols that the MTU for this destination
   1189 	 * has changed.
   1190 	 */
   1191 	for (mc = LIST_FIRST(&icmp6_mtudisc_callbacks); mc != NULL;
   1192 	     mc = LIST_NEXT(mc, mc_list))
   1193 		(*mc->mc_func)(&sin6.sin6_addr);
   1194 }
   1195 
   1196 /*
   1197  * Process a Node Information Query packet, based on
   1198  * draft-ietf-ipngwg-icmp-name-lookups-07.
   1199  *
   1200  * Spec incompatibilities:
   1201  * - IPv6 Subject address handling
   1202  * - IPv4 Subject address handling support missing
   1203  * - Proxy reply (answer even if it's not for me)
   1204  * - joins NI group address at in6_ifattach() time only, does not cope
   1205  *   with hostname changes by sethostname(3)
   1206  */
   1207 static struct mbuf *
   1208 ni6_input(struct mbuf *m, int off)
   1209 {
   1210 	struct icmp6_nodeinfo *ni6, *nni6;
   1211 	struct mbuf *n = NULL;
   1212 	u_int16_t qtype;
   1213 	int subjlen;
   1214 	int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
   1215 	struct ni_reply_fqdn *fqdn;
   1216 	int addrs;		/* for NI_QTYPE_NODEADDR */
   1217 	struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
   1218 	struct sockaddr_in6 sin6; /* ip6_dst */
   1219 	struct in6_addr in6_subj; /* subject address */
   1220 	struct ip6_hdr *ip6;
   1221 	int oldfqdn = 0;	/* if 1, return pascal string (03 draft) */
   1222 	char *subj = NULL;
   1223 	struct ifnet *rcvif;
   1224 	int s, ss;
   1225 	struct ifaddr *ifa;
   1226 	struct psref psref;
   1227 
   1228 	ip6 = mtod(m, struct ip6_hdr *);
   1229 	IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
   1230 	if (ni6 == NULL) {
   1231 		/* m is already reclaimed */
   1232 		return NULL;
   1233 	}
   1234 
   1235 	/*
   1236 	 * Validate IPv6 destination address.
   1237 	 *
   1238 	 * The Responder must discard the Query without further processing
   1239 	 * unless it is one of the Responder's unicast or anycast addresses, or
   1240 	 * a link-local scope multicast address which the Responder has joined.
   1241 	 * [icmp-name-lookups-07, Section 4.]
   1242 	 */
   1243 	sockaddr_in6_init(&sin6, &ip6->ip6_dst, 0, 0, 0);
   1244 	/* XXX scopeid */
   1245 	ss = pserialize_read_enter();
   1246 	ifa = ifa_ifwithaddr(sin6tosa(&sin6));
   1247 	if (ifa != NULL)
   1248 		; /* unicast/anycast, fine */
   1249 	else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
   1250 		; /* link-local multicast, fine */
   1251 	else {
   1252 		pserialize_read_exit(ss);
   1253 		goto bad;
   1254 	}
   1255 	pserialize_read_exit(ss);
   1256 
   1257 	/* validate query Subject field. */
   1258 	qtype = ntohs(ni6->ni_qtype);
   1259 	subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
   1260 	switch (qtype) {
   1261 	case NI_QTYPE_NOOP:
   1262 	case NI_QTYPE_SUPTYPES:
   1263 		/* 07 draft */
   1264 		if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
   1265 			break;
   1266 		/* FALLTHROUGH */
   1267 	case NI_QTYPE_FQDN:
   1268 	case NI_QTYPE_NODEADDR:
   1269 	case NI_QTYPE_IPV4ADDR:
   1270 		switch (ni6->ni_code) {
   1271 		case ICMP6_NI_SUBJ_IPV6:
   1272 #if ICMP6_NI_SUBJ_IPV6 != 0
   1273 		case 0:
   1274 #endif
   1275 			/*
   1276 			 * backward compatibility - try to accept 03 draft
   1277 			 * format, where no Subject is present.
   1278 			 */
   1279 			if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
   1280 			    subjlen == 0) {
   1281 				oldfqdn++;
   1282 				break;
   1283 			}
   1284 #if ICMP6_NI_SUBJ_IPV6 != 0
   1285 			if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
   1286 				goto bad;
   1287 #endif
   1288 
   1289 			if (subjlen != sizeof(sin6.sin6_addr))
   1290 				goto bad;
   1291 
   1292 			/*
   1293 			 * Validate Subject address.
   1294 			 *
   1295 			 * Not sure what exactly "address belongs to the node"
   1296 			 * means in the spec, is it just unicast, or what?
   1297 			 *
   1298 			 * At this moment we consider Subject address as
   1299 			 * "belong to the node" if the Subject address equals
   1300 			 * to the IPv6 destination address; validation for
   1301 			 * IPv6 destination address should have done enough
   1302 			 * check for us.
   1303 			 *
   1304 			 * We do not do proxy at this moment.
   1305 			 */
   1306 			/* m_pulldown instead of copy? */
   1307 			m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
   1308 			    subjlen, (void *)&in6_subj);
   1309 			rcvif = m_get_rcvif(m, &s);
   1310 			if (in6_setscope(&in6_subj, rcvif, NULL)) {
   1311 				m_put_rcvif(rcvif, &s);
   1312 				goto bad;
   1313 			}
   1314 			m_put_rcvif(rcvif, &s);
   1315 
   1316 			subj = (char *)&in6_subj;
   1317 			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
   1318 				break;
   1319 
   1320 			/*
   1321 			 * XXX if we are to allow other cases, we should really
   1322 			 * be careful about scope here.
   1323 			 * basically, we should disallow queries toward IPv6
   1324 			 * destination X with subject Y, if scope(X) > scope(Y).
   1325 			 * if we allow scope(X) > scope(Y), it will result in
   1326 			 * information leakage across scope boundary.
   1327 			 */
   1328 			goto bad;
   1329 
   1330 		case ICMP6_NI_SUBJ_FQDN:
   1331 			/*
   1332 			 * Validate Subject name with gethostname(3).
   1333 			 *
   1334 			 * The behavior may need some debate, since:
   1335 			 * - we are not sure if the node has FQDN as
   1336 			 *   hostname (returned by gethostname(3)).
   1337 			 * - the code does wildcard match for truncated names.
   1338 			 *   however, we are not sure if we want to perform
   1339 			 *   wildcard match, if gethostname(3) side has
   1340 			 *   truncated hostname.
   1341 			 */
   1342 			n = ni6_nametodns(hostname, hostnamelen, 0);
   1343 			if (!n || n->m_next || n->m_len == 0)
   1344 				goto bad;
   1345 			IP6_EXTHDR_GET(subj, char *, m,
   1346 			    off + sizeof(struct icmp6_nodeinfo), subjlen);
   1347 			if (subj == NULL)
   1348 				goto bad;
   1349 			if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
   1350 					n->m_len)) {
   1351 				goto bad;
   1352 			}
   1353 			m_freem(n);
   1354 			n = NULL;
   1355 			break;
   1356 
   1357 		case ICMP6_NI_SUBJ_IPV4:	/* XXX: to be implemented? */
   1358 		default:
   1359 			goto bad;
   1360 		}
   1361 		break;
   1362 	}
   1363 
   1364 	/* refuse based on configuration.  XXX ICMP6_NI_REFUSED? */
   1365 	switch (qtype) {
   1366 	case NI_QTYPE_FQDN:
   1367 		if ((icmp6_nodeinfo & 1) == 0)
   1368 			goto bad;
   1369 		break;
   1370 	case NI_QTYPE_NODEADDR:
   1371 	case NI_QTYPE_IPV4ADDR:
   1372 		if ((icmp6_nodeinfo & 2) == 0)
   1373 			goto bad;
   1374 		break;
   1375 	}
   1376 
   1377 	/* guess reply length */
   1378 	switch (qtype) {
   1379 	case NI_QTYPE_NOOP:
   1380 		break;		/* no reply data */
   1381 	case NI_QTYPE_SUPTYPES:
   1382 		replylen += sizeof(u_int32_t);
   1383 		break;
   1384 	case NI_QTYPE_FQDN:
   1385 		/* XXX will append an mbuf */
   1386 		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
   1387 		break;
   1388 	case NI_QTYPE_NODEADDR:
   1389 		addrs = ni6_addrs(ni6, m, &ifp, subj, &psref);
   1390 		if ((replylen += addrs * (sizeof(struct in6_addr) +
   1391 					  sizeof(u_int32_t))) > MCLBYTES)
   1392 			replylen = MCLBYTES; /* XXX: will truncate pkt later */
   1393 		break;
   1394 	case NI_QTYPE_IPV4ADDR:
   1395 		/* unsupported - should respond with unknown Qtype? */
   1396 		goto bad;
   1397 	default:
   1398 		/*
   1399 		 * XXX: We must return a reply with the ICMP6 code
   1400 		 * `unknown Qtype' in this case.  However we regard the case
   1401 		 * as an FQDN query for backward compatibility.
   1402 		 * Older versions set a random value to this field,
   1403 		 * so it rarely varies in the defined qtypes.
   1404 		 * But the mechanism is not reliable...
   1405 		 * maybe we should obsolete older versions.
   1406 		 */
   1407 		qtype = NI_QTYPE_FQDN;
   1408 		/* XXX will append an mbuf */
   1409 		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
   1410 		oldfqdn++;
   1411 		break;
   1412 	}
   1413 
   1414 	/* allocate an mbuf to reply. */
   1415 	MGETHDR(n, M_DONTWAIT, m->m_type);
   1416 	if (n == NULL) {
   1417 		if_put(ifp, &psref);
   1418 		m_freem(m);
   1419 		return (NULL);
   1420 	}
   1421 	M_MOVE_PKTHDR(n, m); /* just for rcvif */
   1422 	if (replylen > MHLEN) {
   1423 		if (replylen > MCLBYTES) {
   1424 			/*
   1425 			 * XXX: should we try to allocate more? But MCLBYTES
   1426 			 * is probably much larger than IPV6_MMTU...
   1427 			 */
   1428 			goto bad;
   1429 		}
   1430 		MCLGET(n, M_DONTWAIT);
   1431 		if ((n->m_flags & M_EXT) == 0) {
   1432 			goto bad;
   1433 		}
   1434 	}
   1435 	n->m_pkthdr.len = n->m_len = replylen;
   1436 
   1437 	/* copy mbuf header and IPv6 + Node Information base headers */
   1438 	bcopy(mtod(m, void *), mtod(n, void *), sizeof(struct ip6_hdr));
   1439 	nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
   1440 	bcopy((void *)ni6, (void *)nni6, sizeof(struct icmp6_nodeinfo));
   1441 
   1442 	/* qtype dependent procedure */
   1443 	switch (qtype) {
   1444 	case NI_QTYPE_NOOP:
   1445 		nni6->ni_code = ICMP6_NI_SUCCESS;
   1446 		nni6->ni_flags = 0;
   1447 		break;
   1448 	case NI_QTYPE_SUPTYPES:
   1449 	{
   1450 		u_int32_t v;
   1451 		nni6->ni_code = ICMP6_NI_SUCCESS;
   1452 		nni6->ni_flags = htons(0x0000);	/* raw bitmap */
   1453 		/* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
   1454 		v = (u_int32_t)htonl(0x0000000f);
   1455 		bcopy(&v, nni6 + 1, sizeof(u_int32_t));
   1456 		break;
   1457 	}
   1458 	case NI_QTYPE_FQDN:
   1459 		nni6->ni_code = ICMP6_NI_SUCCESS;
   1460 		fqdn = (struct ni_reply_fqdn *)(mtod(n, char *) +
   1461 						sizeof(struct ip6_hdr) +
   1462 						sizeof(struct icmp6_nodeinfo));
   1463 		nni6->ni_flags = 0; /* XXX: meaningless TTL */
   1464 		fqdn->ni_fqdn_ttl = 0;	/* ditto. */
   1465 		/*
   1466 		 * XXX do we really have FQDN in variable "hostname"?
   1467 		 */
   1468 		n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
   1469 		if (n->m_next == NULL)
   1470 			goto bad;
   1471 		/* XXX we assume that n->m_next is not a chain */
   1472 		if (n->m_next->m_next != NULL)
   1473 			goto bad;
   1474 		n->m_pkthdr.len += n->m_next->m_len;
   1475 		break;
   1476 	case NI_QTYPE_NODEADDR:
   1477 	{
   1478 		int lenlim, copied;
   1479 
   1480 		nni6->ni_code = ICMP6_NI_SUCCESS;
   1481 		n->m_pkthdr.len = n->m_len =
   1482 		    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
   1483 		lenlim = M_TRAILINGSPACE(n);
   1484 		copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
   1485 		if_put(ifp, &psref);
   1486 		ifp = NULL;
   1487 		/* XXX: reset mbuf length */
   1488 		n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
   1489 			sizeof(struct icmp6_nodeinfo) + copied;
   1490 		break;
   1491 	}
   1492 	default:
   1493 		break;		/* XXX impossible! */
   1494 	}
   1495 
   1496 	nni6->ni_type = ICMP6_NI_REPLY;
   1497 	m_freem(m);
   1498 	return (n);
   1499 
   1500   bad:
   1501 	if_put(ifp, &psref);
   1502 	m_freem(m);
   1503 	if (n)
   1504 		m_freem(n);
   1505 	return (NULL);
   1506 }
   1507 #undef hostnamelen
   1508 
   1509 #define isupper(x) ('A' <= (x) && (x) <= 'Z')
   1510 #define isalpha(x) (('A' <= (x) && (x) <= 'Z') || ('a' <= (x) && (x) <= 'z'))
   1511 #define isalnum(x) (isalpha(x) || ('0' <= (x) && (x) <= '9'))
   1512 #define tolower(x) (isupper(x) ? (x) + 'a' - 'A' : (x))
   1513 
   1514 /*
   1515  * make a mbuf with DNS-encoded string.  no compression support.
   1516  *
   1517  * XXX names with less than 2 dots (like "foo" or "foo.section") will be
   1518  * treated as truncated name (two \0 at the end).  this is a wild guess.
   1519  *
   1520  * old - return pascal string if non-zero
   1521  */
   1522 static struct mbuf *
   1523 ni6_nametodns(const char *name, int namelen, int old)
   1524 {
   1525 	struct mbuf *m;
   1526 	char *cp, *ep;
   1527 	const char *p, *q;
   1528 	int i, len, nterm;
   1529 
   1530 	if (old)
   1531 		len = namelen + 1;
   1532 	else
   1533 		len = MCLBYTES;
   1534 
   1535 	/* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
   1536 	MGET(m, M_DONTWAIT, MT_DATA);
   1537 	if (m && len > MLEN) {
   1538 		MCLGET(m, M_DONTWAIT);
   1539 		if ((m->m_flags & M_EXT) == 0)
   1540 			goto fail;
   1541 	}
   1542 	if (!m)
   1543 		goto fail;
   1544 	m->m_next = NULL;
   1545 
   1546 	if (old) {
   1547 		m->m_len = len;
   1548 		*mtod(m, char *) = namelen;
   1549 		bcopy(name, mtod(m, char *) + 1, namelen);
   1550 		return m;
   1551 	} else {
   1552 		m->m_len = 0;
   1553 		cp = mtod(m, char *);
   1554 		ep = mtod(m, char *) + M_TRAILINGSPACE(m);
   1555 
   1556 		/* if not certain about my name, return empty buffer */
   1557 		if (namelen == 0)
   1558 			return m;
   1559 
   1560 		/*
   1561 		 * guess if it looks like shortened hostname, or FQDN.
   1562 		 * shortened hostname needs two trailing "\0".
   1563 		 */
   1564 		i = 0;
   1565 		for (p = name; p < name + namelen; p++) {
   1566 			if (*p == '.')
   1567 				i++;
   1568 		}
   1569 		if (i < 2)
   1570 			nterm = 2;
   1571 		else
   1572 			nterm = 1;
   1573 
   1574 		p = name;
   1575 		while (cp < ep && p < name + namelen) {
   1576 			i = 0;
   1577 			for (q = p; q < name + namelen && *q && *q != '.'; q++)
   1578 				i++;
   1579 			/* result does not fit into mbuf */
   1580 			if (cp + i + 1 >= ep)
   1581 				goto fail;
   1582 			/*
   1583 			 * DNS label length restriction, RFC1035 page 8.
   1584 			 * "i == 0" case is included here to avoid returning
   1585 			 * 0-length label on "foo..bar".
   1586 			 */
   1587 			if (i <= 0 || i >= 64)
   1588 				goto fail;
   1589 			*cp++ = i;
   1590 			if (!isalpha(p[0]) || !isalnum(p[i - 1]))
   1591 				goto fail;
   1592 			while (i > 0) {
   1593 				if (!isalnum(*p) && *p != '-')
   1594 					goto fail;
   1595 				if (isupper(*p)) {
   1596 					*cp++ = tolower(*p);
   1597 					p++;
   1598 				} else
   1599 					*cp++ = *p++;
   1600 				i--;
   1601 			}
   1602 			p = q;
   1603 			if (p < name + namelen && *p == '.')
   1604 				p++;
   1605 		}
   1606 		/* termination */
   1607 		if (cp + nterm >= ep)
   1608 			goto fail;
   1609 		while (nterm-- > 0)
   1610 			*cp++ = '\0';
   1611 		m->m_len = cp - mtod(m, char *);
   1612 		return m;
   1613 	}
   1614 
   1615 	panic("should not reach here");
   1616 	/* NOTREACHED */
   1617 
   1618  fail:
   1619 	if (m)
   1620 		m_freem(m);
   1621 	return NULL;
   1622 }
   1623 
   1624 /*
   1625  * check if two DNS-encoded string matches.  takes care of truncated
   1626  * form (with \0\0 at the end).  no compression support.
   1627  * XXX upper/lowercase match (see RFC2065)
   1628  */
   1629 static int
   1630 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
   1631 {
   1632 	const char *a0, *b0;
   1633 	int l;
   1634 
   1635 	/* simplest case - need validation? */
   1636 	if (alen == blen && memcmp(a, b, alen) == 0)
   1637 		return 1;
   1638 
   1639 	a0 = a;
   1640 	b0 = b;
   1641 
   1642 	/* termination is mandatory */
   1643 	if (alen < 2 || blen < 2)
   1644 		return 0;
   1645 	if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
   1646 		return 0;
   1647 	alen--;
   1648 	blen--;
   1649 
   1650 	while (a - a0 < alen && b - b0 < blen) {
   1651 		if (a - a0 + 1 > alen || b - b0 + 1 > blen)
   1652 			return 0;
   1653 
   1654 		if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
   1655 			return 0;
   1656 		/* we don't support compression yet */
   1657 		if (a[0] >= 64 || b[0] >= 64)
   1658 			return 0;
   1659 
   1660 		/* truncated case */
   1661 		if (a[0] == 0 && a - a0 == alen - 1)
   1662 			return 1;
   1663 		if (b[0] == 0 && b - b0 == blen - 1)
   1664 			return 1;
   1665 		if (a[0] == 0 || b[0] == 0)
   1666 			return 0;
   1667 
   1668 		if (a[0] != b[0])
   1669 			return 0;
   1670 		l = a[0];
   1671 		if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
   1672 			return 0;
   1673 		if (memcmp(a + 1, b + 1, l) != 0)
   1674 			return 0;
   1675 
   1676 		a += 1 + l;
   1677 		b += 1 + l;
   1678 	}
   1679 
   1680 	if (a - a0 == alen && b - b0 == blen)
   1681 		return 1;
   1682 	else
   1683 		return 0;
   1684 }
   1685 
   1686 /*
   1687  * calculate the number of addresses to be returned in the node info reply.
   1688  */
   1689 static int
   1690 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m,
   1691     struct ifnet **ifpp, char *subj, struct psref *psref)
   1692 {
   1693 	struct ifnet *ifp;
   1694 	struct in6_ifaddr *ia6;
   1695 	struct ifaddr *ifa;
   1696 	struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
   1697 	int addrs = 0, addrsofif, iffound = 0;
   1698 	int niflags = ni6->ni_flags;
   1699 	int s;
   1700 
   1701 	if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
   1702 		switch (ni6->ni_code) {
   1703 		case ICMP6_NI_SUBJ_IPV6:
   1704 			if (subj == NULL) /* must be impossible... */
   1705 				return (0);
   1706 			subj_ip6 = (struct sockaddr_in6 *)subj;
   1707 			break;
   1708 		default:
   1709 			/*
   1710 			 * XXX: we only support IPv6 subject address for
   1711 			 * this Qtype.
   1712 			 */
   1713 			return (0);
   1714 		}
   1715 	}
   1716 
   1717 	s = pserialize_read_enter();
   1718 	IFNET_READER_FOREACH(ifp) {
   1719 		addrsofif = 0;
   1720 		IFADDR_READER_FOREACH(ifa, ifp) {
   1721 			if (ifa->ifa_addr->sa_family != AF_INET6)
   1722 				continue;
   1723 			ia6 = (struct in6_ifaddr *)ifa;
   1724 
   1725 			if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
   1726 			    IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
   1727 					       &ia6->ia_addr.sin6_addr))
   1728 				iffound = 1;
   1729 
   1730 			/*
   1731 			 * IPv4-mapped addresses can only be returned by a
   1732 			 * Node Information proxy, since they represent
   1733 			 * addresses of IPv4-only nodes, which perforce do
   1734 			 * not implement this protocol.
   1735 			 * [icmp-name-lookups-07, Section 5.4]
   1736 			 * So we don't support NI_NODEADDR_FLAG_COMPAT in
   1737 			 * this function at this moment.
   1738 			 */
   1739 
   1740 			/* What do we have to do about ::1? */
   1741 			switch (in6_addrscope(&ia6->ia_addr.sin6_addr)) {
   1742 			case IPV6_ADDR_SCOPE_LINKLOCAL:
   1743 				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
   1744 					continue;
   1745 				break;
   1746 			case IPV6_ADDR_SCOPE_SITELOCAL:
   1747 				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
   1748 					continue;
   1749 				break;
   1750 			case IPV6_ADDR_SCOPE_GLOBAL:
   1751 				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
   1752 					continue;
   1753 				break;
   1754 			default:
   1755 				continue;
   1756 			}
   1757 
   1758 			/*
   1759 			 * check if anycast is okay.
   1760 			 * XXX: just experimental.  not in the spec.
   1761 			 */
   1762 			if ((ia6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
   1763 			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
   1764 				continue; /* we need only unicast addresses */
   1765 
   1766 			addrsofif++; /* count the address */
   1767 		}
   1768 		if (iffound) {
   1769 			if_acquire_NOMPSAFE(ifp, psref);
   1770 			pserialize_read_exit(s);
   1771 			*ifpp = ifp;
   1772 			return (addrsofif);
   1773 		}
   1774 
   1775 		addrs += addrsofif;
   1776 	}
   1777 	pserialize_read_exit(s);
   1778 
   1779 	return (addrs);
   1780 }
   1781 
   1782 static int
   1783 ni6_store_addrs(struct icmp6_nodeinfo *ni6,
   1784 	struct icmp6_nodeinfo *nni6, struct ifnet *ifp0,
   1785 	int resid)
   1786 {
   1787 	struct ifnet *ifp;
   1788 	struct in6_ifaddr *ia6;
   1789 	struct ifaddr *ifa;
   1790 	struct ifnet *ifp_dep = NULL;
   1791 	int copied = 0, allow_deprecated = 0;
   1792 	u_char *cp = (u_char *)(nni6 + 1);
   1793 	int niflags = ni6->ni_flags;
   1794 	u_int32_t ltime;
   1795 	int s;
   1796 
   1797 	if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
   1798 		return (0);	/* needless to copy */
   1799 
   1800 	s = pserialize_read_enter();
   1801 	ifp = ifp0 ? ifp0 : IFNET_READER_FIRST();
   1802   again:
   1803 
   1804 	for (; ifp; ifp = IFNET_READER_NEXT(ifp))
   1805 	{
   1806 		IFADDR_READER_FOREACH(ifa, ifp) {
   1807 			if (ifa->ifa_addr->sa_family != AF_INET6)
   1808 				continue;
   1809 			ia6 = (struct in6_ifaddr *)ifa;
   1810 
   1811 			if ((ia6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
   1812 			    allow_deprecated == 0) {
   1813 				/*
   1814 				 * prefererred address should be put before
   1815 				 * deprecated addresses.
   1816 				 */
   1817 
   1818 				/* record the interface for later search */
   1819 				if (ifp_dep == NULL)
   1820 					ifp_dep = ifp;
   1821 
   1822 				continue;
   1823 			}
   1824 			else if ((ia6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
   1825 				 allow_deprecated != 0)
   1826 				continue; /* we now collect deprecated addrs */
   1827 
   1828 			/* What do we have to do about ::1? */
   1829 			switch (in6_addrscope(&ia6->ia_addr.sin6_addr)) {
   1830 			case IPV6_ADDR_SCOPE_LINKLOCAL:
   1831 				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
   1832 					continue;
   1833 				break;
   1834 			case IPV6_ADDR_SCOPE_SITELOCAL:
   1835 				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
   1836 					continue;
   1837 				break;
   1838 			case IPV6_ADDR_SCOPE_GLOBAL:
   1839 				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
   1840 					continue;
   1841 				break;
   1842 			default:
   1843 				continue;
   1844 			}
   1845 
   1846 			/*
   1847 			 * check if anycast is okay.
   1848 			 * XXX: just experimental.  not in the spec.
   1849 			 */
   1850 			if ((ia6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
   1851 			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
   1852 				continue;
   1853 
   1854 			/* now we can copy the address */
   1855 			if (resid < sizeof(struct in6_addr) +
   1856 			    sizeof(u_int32_t)) {
   1857 				/*
   1858 				 * We give up much more copy.
   1859 				 * Set the truncate flag and return.
   1860 				 */
   1861 				nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
   1862 				goto out;
   1863 			}
   1864 
   1865 			/*
   1866 			 * Set the TTL of the address.
   1867 			 * The TTL value should be one of the following
   1868 			 * according to the specification:
   1869 			 *
   1870 			 * 1. The remaining lifetime of a DHCP lease on the
   1871 			 *    address, or
   1872 			 * 2. The remaining Valid Lifetime of a prefix from
   1873 			 *    which the address was derived through Stateless
   1874 			 *    Autoconfiguration.
   1875 			 *
   1876 			 * Note that we currently do not support stateful
   1877 			 * address configuration by DHCPv6, so the former
   1878 			 * case can't happen.
   1879 			 *
   1880 			 * TTL must be 2^31 > TTL >= 0.
   1881 			 */
   1882 			if (ia6->ia6_lifetime.ia6t_expire == 0)
   1883 				ltime = ND6_INFINITE_LIFETIME;
   1884 			else {
   1885 				if (ia6->ia6_lifetime.ia6t_expire >
   1886 				    time_uptime)
   1887 					ltime = ia6->ia6_lifetime.ia6t_expire -
   1888 					    time_uptime;
   1889 				else
   1890 					ltime = 0;
   1891 			}
   1892 			if (ltime > 0x7fffffff)
   1893 				ltime = 0x7fffffff;
   1894 			ltime = htonl(ltime);
   1895 
   1896 			bcopy(&ltime, cp, sizeof(u_int32_t));
   1897 			cp += sizeof(u_int32_t);
   1898 
   1899 			/* copy the address itself */
   1900 			bcopy(&ia6->ia_addr.sin6_addr, cp,
   1901 			      sizeof(struct in6_addr));
   1902 			in6_clearscope((struct in6_addr *)cp); /* XXX */
   1903 			cp += sizeof(struct in6_addr);
   1904 
   1905 			resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
   1906 			copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
   1907 		}
   1908 		if (ifp0)	/* we need search only on the specified IF */
   1909 			break;
   1910 	}
   1911 
   1912 	if (allow_deprecated == 0 && ifp_dep != NULL) {
   1913 		ifp = ifp_dep;
   1914 		allow_deprecated = 1;
   1915 
   1916 		goto again;
   1917 	}
   1918 out:
   1919 	pserialize_read_exit(s);
   1920 	return (copied);
   1921 }
   1922 
   1923 /*
   1924  * XXX almost dup'ed code with rip6_input.
   1925  */
   1926 static int
   1927 icmp6_rip6_input(struct mbuf **mp, int off)
   1928 {
   1929 	struct mbuf *m = *mp;
   1930 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   1931 	struct inpcb_hdr *inph;
   1932 	struct in6pcb *in6p;
   1933 	struct in6pcb *last = NULL;
   1934 	struct sockaddr_in6 rip6src;
   1935 	struct icmp6_hdr *icmp6;
   1936 	struct mbuf *opts = NULL;
   1937 
   1938 	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
   1939 	if (icmp6 == NULL) {
   1940 		/* m is already reclaimed */
   1941 		return IPPROTO_DONE;
   1942 	}
   1943 
   1944 	/*
   1945 	 * XXX: the address may have embedded scope zone ID, which should be
   1946 	 * hidden from applications.
   1947 	 */
   1948 	sockaddr_in6_init(&rip6src, &ip6->ip6_src, 0, 0, 0);
   1949 	if (sa6_recoverscope(&rip6src)) {
   1950 		m_freem(m);
   1951 		return (IPPROTO_DONE);
   1952 	}
   1953 
   1954 	TAILQ_FOREACH(inph, &raw6cbtable.inpt_queue, inph_queue) {
   1955 		in6p = (struct in6pcb *)inph;
   1956 		if (in6p->in6p_af != AF_INET6)
   1957 			continue;
   1958 		if (in6p->in6p_ip6.ip6_nxt != IPPROTO_ICMPV6)
   1959 			continue;
   1960 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
   1961 		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
   1962 			continue;
   1963 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
   1964 		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
   1965 			continue;
   1966 		if (in6p->in6p_icmp6filt
   1967 		    && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
   1968 				 in6p->in6p_icmp6filt))
   1969 			continue;
   1970 		if (last) {
   1971 			struct	mbuf *n;
   1972 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
   1973 				if (last->in6p_flags & IN6P_CONTROLOPTS)
   1974 					ip6_savecontrol(last, &opts, ip6, n);
   1975 				/* strip intermediate headers */
   1976 				m_adj(n, off);
   1977 				if (sbappendaddr(&last->in6p_socket->so_rcv,
   1978 				    sin6tosa(&rip6src), n, opts) == 0) {
   1979 					/* should notify about lost packet */
   1980 					m_freem(n);
   1981 					if (opts)
   1982 						m_freem(opts);
   1983 				} else
   1984 					sorwakeup(last->in6p_socket);
   1985 				opts = NULL;
   1986 			}
   1987 		}
   1988 		last = in6p;
   1989 	}
   1990 	if (last) {
   1991 		if (last->in6p_flags & IN6P_CONTROLOPTS)
   1992 			ip6_savecontrol(last, &opts, ip6, m);
   1993 		/* strip intermediate headers */
   1994 		m_adj(m, off);
   1995 		if (sbappendaddr(&last->in6p_socket->so_rcv,
   1996 		    sin6tosa(&rip6src), m, opts) == 0) {
   1997 			m_freem(m);
   1998 			if (opts)
   1999 				m_freem(opts);
   2000 		} else
   2001 			sorwakeup(last->in6p_socket);
   2002 	} else {
   2003 		m_freem(m);
   2004 		IP6_STATDEC(IP6_STAT_DELIVERED);
   2005 	}
   2006 	return IPPROTO_DONE;
   2007 }
   2008 
   2009 /*
   2010  * Reflect the ip6 packet back to the source.
   2011  * OFF points to the icmp6 header, counted from the top of the mbuf.
   2012  *
   2013  * Note: RFC 1885 required that an echo reply should be truncated if it
   2014  * did not fit in with (return) path MTU, and KAME code supported the
   2015  * behavior.  However, as a clarification after the RFC, this limitation
   2016  * was removed in a revised version of the spec, RFC 2463.  We had kept the
   2017  * old behavior, with a (non-default) ifdef block, while the new version of
   2018  * the spec was an internet-draft status, and even after the new RFC was
   2019  * published.  But it would rather make sense to clean the obsoleted part
   2020  * up, and to make the code simpler at this stage.
   2021  */
   2022 void
   2023 icmp6_reflect(struct mbuf *m, size_t off)
   2024 {
   2025 	struct ip6_hdr *ip6;
   2026 	struct icmp6_hdr *icmp6;
   2027 	const struct in6_ifaddr *ia;
   2028 	const struct ip6aux *ip6a;
   2029 	int plen;
   2030 	int type, code;
   2031 	struct ifnet *outif = NULL;
   2032 	struct in6_addr origdst;
   2033 	struct ifnet *rcvif;
   2034 	int s;
   2035 	bool ip6_src_filled = false;
   2036 
   2037 	/* too short to reflect */
   2038 	if (off < sizeof(struct ip6_hdr)) {
   2039 		nd6log(LOG_DEBUG,
   2040 		    "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
   2041 		    (u_long)off, (u_long)sizeof(struct ip6_hdr),
   2042 		    __FILE__, __LINE__);
   2043 		goto bad;
   2044 	}
   2045 
   2046 	/*
   2047 	 * If there are extra headers between IPv6 and ICMPv6, strip
   2048 	 * off that header first.
   2049 	 */
   2050 #ifdef DIAGNOSTIC
   2051 	if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
   2052 		panic("assumption failed in icmp6_reflect");
   2053 #endif
   2054 	if (off > sizeof(struct ip6_hdr)) {
   2055 		size_t l;
   2056 		struct ip6_hdr nip6;
   2057 
   2058 		l = off - sizeof(struct ip6_hdr);
   2059 		m_copydata(m, 0, sizeof(nip6), (void *)&nip6);
   2060 		m_adj(m, l);
   2061 		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
   2062 		if (m->m_len < l) {
   2063 			if ((m = m_pullup(m, l)) == NULL)
   2064 				return;
   2065 		}
   2066 		bcopy((void *)&nip6, mtod(m, void *), sizeof(nip6));
   2067 	} else /* off == sizeof(struct ip6_hdr) */ {
   2068 		size_t l;
   2069 		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
   2070 		if (m->m_len < l) {
   2071 			if ((m = m_pullup(m, l)) == NULL)
   2072 				return;
   2073 		}
   2074 	}
   2075 	plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
   2076 	ip6 = mtod(m, struct ip6_hdr *);
   2077 	ip6->ip6_nxt = IPPROTO_ICMPV6;
   2078 	icmp6 = (struct icmp6_hdr *)(ip6 + 1);
   2079 	type = icmp6->icmp6_type; /* keep type for statistics */
   2080 	code = icmp6->icmp6_code; /* ditto. */
   2081 
   2082 	origdst = ip6->ip6_dst;
   2083 	/*
   2084 	 * ip6_input() drops a packet if its src is multicast.
   2085 	 * So, the src is never multicast.
   2086 	 */
   2087 	ip6->ip6_dst = ip6->ip6_src;
   2088 
   2089 	/*
   2090 	 * If the incoming packet was addressed directly to us (i.e. unicast),
   2091 	 * use dst as the src for the reply.
   2092 	 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
   2093 	 * (for example) when we encounter an error while forwarding procedure
   2094 	 * destined to a duplicated address of ours.
   2095 	 * Note that ip6_getdstifaddr() may fail if we are in an error handling
   2096 	 * procedure of an outgoing packet of our own, in which case we need
   2097 	 * to search in the ifaddr list.
   2098 	 */
   2099 	if (IN6_IS_ADDR_MULTICAST(&origdst))
   2100 		;
   2101 	else if ((ip6a = ip6_getdstifaddr(m)) != NULL) {
   2102 		if ((ip6a->ip6a_flags &
   2103 		     (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
   2104 			ip6->ip6_src = ip6a->ip6a_src;
   2105 			ip6_src_filled = true;
   2106 		}
   2107 	} else {
   2108 		union {
   2109 			struct sockaddr_in6 sin6;
   2110 			struct sockaddr sa;
   2111 		} u;
   2112 		int _s;
   2113 		struct ifaddr *ifa;
   2114 
   2115 		sockaddr_in6_init(&u.sin6, &origdst, 0, 0, 0);
   2116 
   2117 		_s = pserialize_read_enter();
   2118 		ifa = ifa_ifwithaddr(&u.sa);
   2119 
   2120 		if (ifa != NULL) {
   2121 			ia = ifatoia6(ifa);
   2122 			if ((ia->ia6_flags &
   2123 				 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
   2124 				ip6->ip6_src = ia->ia_addr.sin6_addr;
   2125 				ip6_src_filled = true;
   2126 			}
   2127 		}
   2128 		pserialize_read_exit(_s);
   2129 	}
   2130 
   2131 	if (!ip6_src_filled) {
   2132 		int e;
   2133 		struct sockaddr_in6 sin6;
   2134 		struct route ro;
   2135 
   2136 		/*
   2137 		 * This case matches to multicasts, our anycast, or unicasts
   2138 		 * that we do not own.  Select a source address based on the
   2139 		 * source address of the erroneous packet.
   2140 		 */
   2141 		/* zone ID should be embedded */
   2142 		sockaddr_in6_init(&sin6, &ip6->ip6_dst, 0, 0, 0);
   2143 
   2144 		memset(&ro, 0, sizeof(ro));
   2145 		e = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, NULL, NULL,
   2146 		    &ip6->ip6_src);
   2147 		rtcache_free(&ro);
   2148 		if (e != 0) {
   2149 			char ip6buf[INET6_ADDRSTRLEN];
   2150 			nd6log(LOG_DEBUG,
   2151 			    "source can't be determined: "
   2152 			    "dst=%s, error=%d\n",
   2153 			    IN6_PRINT(ip6buf, &sin6.sin6_addr), e);
   2154 			goto bad;
   2155 		}
   2156 	}
   2157 
   2158 	ip6->ip6_flow = 0;
   2159 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
   2160 	ip6->ip6_vfc |= IPV6_VERSION;
   2161 	ip6->ip6_nxt = IPPROTO_ICMPV6;
   2162 	rcvif = m_get_rcvif(m, &s);
   2163 	if (rcvif) {
   2164 		/* XXX: This may not be the outgoing interface */
   2165 		ip6->ip6_hlim = ND_IFINFO(rcvif)->chlim;
   2166 	} else
   2167 		ip6->ip6_hlim = ip6_defhlim;
   2168 	m_put_rcvif(rcvif, &s);
   2169 
   2170 	m->m_pkthdr.csum_flags = 0;
   2171 	icmp6->icmp6_cksum = 0;
   2172 	icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
   2173 					sizeof(struct ip6_hdr), plen);
   2174 
   2175 	/*
   2176 	 * XXX option handling
   2177 	 */
   2178 
   2179 	m->m_flags &= ~(M_BCAST|M_MCAST);
   2180 
   2181 	/*
   2182 	 * To avoid a "too big" situation at an intermediate router
   2183 	 * and the path MTU discovery process, specify the IPV6_MINMTU flag.
   2184 	 * Note that only echo and node information replies are affected,
   2185 	 * since the length of ICMP6 errors is limited to the minimum MTU.
   2186 	 */
   2187 	if (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, &outif)
   2188 	    != 0 && outif)
   2189 		icmp6_ifstat_inc(outif, ifs6_out_error);
   2190 	if (outif)
   2191 		icmp6_ifoutstat_inc(outif, type, code);
   2192 
   2193 	return;
   2194 
   2195  bad:
   2196 	m_freem(m);
   2197 	return;
   2198 }
   2199 
   2200 static const char *
   2201 icmp6_redirect_diag(char *buf, size_t buflen, struct in6_addr *src6, struct in6_addr *dst6,
   2202 	struct in6_addr *tgt6)
   2203 {
   2204 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
   2205 	char ip6buft[INET6_ADDRSTRLEN];
   2206 
   2207 	snprintf(buf, buflen, "(src=%s dst=%s tgt=%s)",
   2208 	    IN6_PRINT(ip6bufs, src6), IN6_PRINT(ip6bufd, dst6),
   2209 	    IN6_PRINT(ip6buft, tgt6));
   2210 	return buf;
   2211 }
   2212 
   2213 void
   2214 icmp6_redirect_input(struct mbuf *m, int off)
   2215 {
   2216 	struct ifnet *ifp;
   2217 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
   2218 	struct nd_redirect *nd_rd;
   2219 	int icmp6len = ntohs(ip6->ip6_plen);
   2220 	char *lladdr = NULL;
   2221 	int lladdrlen = 0;
   2222 	struct rtentry *rt = NULL;
   2223 	int is_router;
   2224 	int is_onlink;
   2225 	struct in6_addr src6 = ip6->ip6_src;
   2226 	struct in6_addr redtgt6;
   2227 	struct in6_addr reddst6;
   2228 	union nd_opts ndopts;
   2229 	struct psref psref;
   2230 	char ip6buf[INET6_ADDRSTRLEN];
   2231 	char diagbuf[256];
   2232 
   2233 	ifp = m_get_rcvif_psref(m, &psref);
   2234 	if (ifp == NULL)
   2235 		goto freeit;
   2236 
   2237 	/* XXX if we are router, we don't update route by icmp6 redirect */
   2238 	if (ip6_forwarding)
   2239 		goto freeit;
   2240 	if (!icmp6_rediraccept)
   2241 		goto freeit;
   2242 
   2243 	IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
   2244 	if (nd_rd == NULL) {
   2245 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
   2246 		m_put_rcvif_psref(ifp, &psref);
   2247 		return;
   2248 	}
   2249 	redtgt6 = nd_rd->nd_rd_target;
   2250 	reddst6 = nd_rd->nd_rd_dst;
   2251 
   2252 	if (in6_setscope(&redtgt6, ifp, NULL) ||
   2253 	    in6_setscope(&reddst6, ifp, NULL)) {
   2254 		goto freeit;
   2255 	}
   2256 
   2257 	/* validation */
   2258 	if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
   2259 		nd6log(LOG_ERR,
   2260 		    "ICMP6 redirect sent from %s rejected; "
   2261 		    "must be from linklocal\n", IN6_PRINT(ip6buf, &src6));
   2262 		goto bad;
   2263 	}
   2264 	if (ip6->ip6_hlim != 255) {
   2265 		nd6log(LOG_ERR,
   2266 		    "ICMP6 redirect sent from %s rejected; "
   2267 		    "hlim=%d (must be 255)\n",
   2268 		    IN6_PRINT(ip6buf, &src6), ip6->ip6_hlim);
   2269 		goto bad;
   2270 	}
   2271     {
   2272 	/* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
   2273 	struct sockaddr_in6 sin6;
   2274 	struct in6_addr *gw6;
   2275 
   2276 	sockaddr_in6_init(&sin6, &reddst6, 0, 0, 0);
   2277 	rt = rtalloc1(sin6tosa(&sin6), 0);
   2278 	if (rt) {
   2279 		if (rt->rt_gateway == NULL ||
   2280 		    rt->rt_gateway->sa_family != AF_INET6) {
   2281 			nd6log(LOG_ERR,
   2282 			    "ICMP6 redirect rejected; no route "
   2283 			    "with inet6 gateway found for redirect dst: %s\n",
   2284 			    icmp6_redirect_diag(diagbuf, sizeof(diagbuf),
   2285 			    &src6, &reddst6, &redtgt6));
   2286 			rt_unref(rt);
   2287 			goto bad;
   2288 		}
   2289 
   2290 		gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
   2291 		if (memcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
   2292 			nd6log(LOG_ERR,
   2293 			    "ICMP6 redirect rejected; "
   2294 			    "not equal to gw-for-src=%s (must be same): %s\n",
   2295 			    IN6_PRINT(ip6buf, gw6),
   2296 			    icmp6_redirect_diag(diagbuf, sizeof(diagbuf),
   2297 			    &src6, &reddst6, &redtgt6));
   2298 			rt_unref(rt);
   2299 			goto bad;
   2300 		}
   2301 	} else {
   2302 		nd6log(LOG_ERR, "ICMP6 redirect rejected; "
   2303 		    "no route found for redirect dst: %s\n",
   2304 		    icmp6_redirect_diag(diagbuf, sizeof(diagbuf),
   2305 		    &src6, &reddst6, &redtgt6));
   2306 		goto bad;
   2307 	}
   2308 	rt_unref(rt);
   2309 	rt = NULL;
   2310     }
   2311 	if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
   2312 		nd6log(LOG_ERR, "ICMP6 redirect rejected; "
   2313 		    "redirect dst must be unicast: %s\n",
   2314 		    icmp6_redirect_diag(diagbuf, sizeof(diagbuf),
   2315 		    &src6, &reddst6, &redtgt6));
   2316 		goto bad;
   2317 	}
   2318 
   2319 	is_router = is_onlink = 0;
   2320 	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
   2321 		is_router = 1;	/* router case */
   2322 	if (memcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
   2323 		is_onlink = 1;	/* on-link destination case */
   2324 	if (!is_router && !is_onlink) {
   2325 		nd6log(LOG_ERR, "ICMP6 redirect rejected; "
   2326 		    "neither router case nor onlink case: %s\n",
   2327 		    icmp6_redirect_diag(diagbuf, sizeof(diagbuf),
   2328 		    &src6, &reddst6, &redtgt6));
   2329 		goto bad;
   2330 	}
   2331 	/* validation passed */
   2332 
   2333 	icmp6len -= sizeof(*nd_rd);
   2334 	nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
   2335 	if (nd6_options(&ndopts) < 0) {
   2336 		nd6log(LOG_INFO, "invalid ND option, rejected: %s\n",
   2337 		    icmp6_redirect_diag(diagbuf, sizeof(diagbuf),
   2338 		    &src6, &reddst6, &redtgt6));
   2339 		/* nd6_options have incremented stats */
   2340 		goto freeit;
   2341 	}
   2342 
   2343 	if (ndopts.nd_opts_tgt_lladdr) {
   2344 		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
   2345 		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
   2346 	}
   2347 
   2348 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
   2349 		nd6log(LOG_INFO, "lladdrlen mismatch for %s "
   2350 		    "(if %d, icmp6 packet %d): %s\n",
   2351 		    IN6_PRINT(ip6buf, &redtgt6),
   2352 		    ifp->if_addrlen, lladdrlen - 2,
   2353 		    icmp6_redirect_diag(diagbuf, sizeof(diagbuf),
   2354 		    &src6, &reddst6, &redtgt6));
   2355 		goto bad;
   2356 	}
   2357 
   2358 	/* RFC 2461 8.3 */
   2359 	nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
   2360 			 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
   2361 
   2362 	m_put_rcvif_psref(ifp, &psref);
   2363 	ifp = NULL;
   2364 
   2365 	if (!is_onlink) {	/* better router case.  perform rtredirect. */
   2366 		/* perform rtredirect */
   2367 		struct sockaddr_in6 sdst;
   2368 		struct sockaddr_in6 sgw;
   2369 		struct sockaddr_in6 ssrc;
   2370 		unsigned long rtcount;
   2371 		struct rtentry *newrt = NULL;
   2372 
   2373 		/*
   2374 		 * do not install redirect route, if the number of entries
   2375 		 * is too much (> hiwat).  note that, the node (= host) will
   2376 		 * work just fine even if we do not install redirect route
   2377 		 * (there will be additional hops, though).
   2378 		 */
   2379 		rtcount = rt_timer_count(icmp6_redirect_timeout_q);
   2380 		if (0 <= ip6_maxdynroutes && rtcount >= ip6_maxdynroutes)
   2381 			goto freeit;
   2382 		if (0 <= icmp6_redirect_hiwat && rtcount > icmp6_redirect_hiwat)
   2383 			goto freeit;
   2384 		else if (0 <= icmp6_redirect_lowat &&
   2385 		    rtcount > icmp6_redirect_lowat) {
   2386 			/*
   2387 			 * XXX nuke a victim, install the new one.
   2388 			 */
   2389 		}
   2390 
   2391 		memset(&sdst, 0, sizeof(sdst));
   2392 		memset(&sgw, 0, sizeof(sgw));
   2393 		memset(&ssrc, 0, sizeof(ssrc));
   2394 		sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
   2395 		sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
   2396 			sizeof(struct sockaddr_in6);
   2397 		bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
   2398 		bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
   2399 		bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
   2400 		rtredirect(sin6tosa(&sdst), sin6tosa(&sgw), NULL,
   2401 		           RTF_GATEWAY | RTF_HOST, sin6tosa(&ssrc),
   2402 			   &newrt);
   2403 
   2404 		if (newrt) {
   2405 			(void)rt_timer_add(newrt, icmp6_redirect_timeout,
   2406 			    icmp6_redirect_timeout_q);
   2407 			rt_unref(newrt);
   2408 		}
   2409 	}
   2410 	/* finally update cached route in each socket via pfctlinput */
   2411 	{
   2412 		struct sockaddr_in6 sdst;
   2413 
   2414 		sockaddr_in6_init(&sdst, &reddst6, 0, 0, 0);
   2415 		pfctlinput(PRC_REDIRECT_HOST, sin6tosa(&sdst));
   2416 #if defined(IPSEC)
   2417 		if (ipsec_used)
   2418 			key_sa_routechange(sin6tosa(&sdst));
   2419 #endif
   2420 	}
   2421 
   2422  freeit:
   2423 	if (ifp != NULL)
   2424 		m_put_rcvif_psref(ifp, &psref);
   2425 	m_freem(m);
   2426 	return;
   2427 
   2428  bad:
   2429 	m_put_rcvif_psref(ifp, &psref);
   2430 	ICMP6_STATINC(ICMP6_STAT_BADREDIRECT);
   2431 	m_freem(m);
   2432 }
   2433 
   2434 void
   2435 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
   2436 {
   2437 	struct ifnet *ifp;	/* my outgoing interface */
   2438 	struct in6_addr *ifp_ll6;
   2439 	struct in6_addr *nexthop;
   2440 	struct ip6_hdr *sip6;	/* m0 as struct ip6_hdr */
   2441 	struct mbuf *m = NULL;	/* newly allocated one */
   2442 	struct ip6_hdr *ip6;	/* m as struct ip6_hdr */
   2443 	struct nd_redirect *nd_rd;
   2444 	size_t maxlen;
   2445 	u_char *p;
   2446 	struct sockaddr_in6 src_sa;
   2447 
   2448 	icmp6_errcount(ICMP6_STAT_OUTERRHIST, ND_REDIRECT, 0);
   2449 
   2450 	/* if we are not router, we don't send icmp6 redirect */
   2451 	if (!ip6_forwarding)
   2452 		goto fail;
   2453 
   2454 	/* sanity check */
   2455 	KASSERT(m0 != NULL);
   2456 	KASSERT(rt != NULL);
   2457 
   2458 	ifp = rt->rt_ifp;
   2459 
   2460 	/*
   2461 	 * Address check:
   2462 	 *  the source address must identify a neighbor, and
   2463 	 *  the destination address must not be a multicast address
   2464 	 *  [RFC 2461, sec 8.2]
   2465 	 */
   2466 	sip6 = mtod(m0, struct ip6_hdr *);
   2467 	sockaddr_in6_init(&src_sa, &sip6->ip6_src, 0, 0, 0);
   2468 	if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
   2469 		goto fail;
   2470 	if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
   2471 		goto fail;	/* what should we do here? */
   2472 
   2473 	/* rate limit */
   2474 	if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
   2475 		goto fail;
   2476 
   2477 	/*
   2478 	 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
   2479 	 * we almost always ask for an mbuf cluster for simplicity.
   2480 	 * (MHLEN < IPV6_MMTU is almost always true)
   2481 	 */
   2482 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
   2483 	if (m && IPV6_MMTU >= MHLEN) {
   2484 #if IPV6_MMTU >= MCLBYTES
   2485 		_MCLGET(m, mcl_cache, IPV6_MMTU, M_DONTWAIT);
   2486 #else
   2487 		MCLGET(m, M_DONTWAIT);
   2488 #endif
   2489 	}
   2490 
   2491 	if (!m)
   2492 		goto fail;
   2493 	m_reset_rcvif(m);
   2494 	m->m_len = 0;
   2495 	maxlen = M_TRAILINGSPACE(m);
   2496 	maxlen = min(IPV6_MMTU, maxlen);
   2497 	/* just for safety */
   2498 	if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
   2499 	    ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
   2500 		goto fail;
   2501 	}
   2502 
   2503 	{
   2504 		/* get ip6 linklocal address for ifp(my outgoing interface). */
   2505 		struct in6_ifaddr *ia;
   2506 		int s = pserialize_read_enter();
   2507 		if ((ia = in6ifa_ifpforlinklocal(ifp,
   2508 						 IN6_IFF_NOTREADY|
   2509 						 IN6_IFF_ANYCAST)) == NULL) {
   2510 			pserialize_read_exit(s);
   2511 			goto fail;
   2512 		}
   2513 		ifp_ll6 = &ia->ia_addr.sin6_addr;
   2514 		pserialize_read_exit(s);
   2515 	}
   2516 
   2517 	/* get ip6 linklocal address for the router. */
   2518 	if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
   2519 		struct sockaddr_in6 *sin6;
   2520 		sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
   2521 		nexthop = &sin6->sin6_addr;
   2522 		if (!IN6_IS_ADDR_LINKLOCAL(nexthop))
   2523 			nexthop = NULL;
   2524 	} else
   2525 		nexthop = NULL;
   2526 
   2527 	/* ip6 */
   2528 	ip6 = mtod(m, struct ip6_hdr *);
   2529 	ip6->ip6_flow = 0;
   2530 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
   2531 	ip6->ip6_vfc |= IPV6_VERSION;
   2532 	/* ip6->ip6_plen will be set later */
   2533 	ip6->ip6_nxt = IPPROTO_ICMPV6;
   2534 	ip6->ip6_hlim = 255;
   2535 	/* ip6->ip6_src must be linklocal addr for my outgoing if. */
   2536 	bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
   2537 	bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
   2538 
   2539 	/* ND Redirect */
   2540 	nd_rd = (struct nd_redirect *)(ip6 + 1);
   2541 	nd_rd->nd_rd_type = ND_REDIRECT;
   2542 	nd_rd->nd_rd_code = 0;
   2543 	nd_rd->nd_rd_reserved = 0;
   2544 	if (rt->rt_flags & RTF_GATEWAY) {
   2545 		/*
   2546 		 * nd_rd->nd_rd_target must be a link-local address in
   2547 		 * better router cases.
   2548 		 */
   2549 		if (!nexthop)
   2550 			goto fail;
   2551 		bcopy(nexthop, &nd_rd->nd_rd_target,
   2552 		      sizeof(nd_rd->nd_rd_target));
   2553 		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
   2554 		      sizeof(nd_rd->nd_rd_dst));
   2555 	} else {
   2556 		/* make sure redtgt == reddst */
   2557 		nexthop = &sip6->ip6_dst;
   2558 		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
   2559 		      sizeof(nd_rd->nd_rd_target));
   2560 		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
   2561 		      sizeof(nd_rd->nd_rd_dst));
   2562 	}
   2563 
   2564 	p = (u_char *)(nd_rd + 1);
   2565 
   2566 	{
   2567 		/* target lladdr option */
   2568 		struct llentry *ln = NULL;
   2569 		int len;
   2570 		struct nd_opt_hdr *nd_opt;
   2571 		char *lladdr;
   2572 
   2573 		ln = nd6_lookup(nexthop, ifp, false);
   2574 		if (ln == NULL)
   2575 			goto nolladdropt;
   2576 		len = sizeof(*nd_opt) + ifp->if_addrlen;
   2577 		len = (len + 7) & ~7;	/* round by 8 */
   2578 		/* safety check */
   2579 		if (len + (p - (u_char *)ip6) > maxlen) {
   2580 			LLE_RUNLOCK(ln);
   2581 			goto nolladdropt;
   2582 		}
   2583 		if (ln->la_flags & LLE_VALID) {
   2584 			nd_opt = (struct nd_opt_hdr *)p;
   2585 			nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
   2586 			nd_opt->nd_opt_len = len >> 3;
   2587 			lladdr = (char *)(nd_opt + 1);
   2588 			memcpy(lladdr, &ln->ll_addr, ifp->if_addrlen);
   2589 			p += len;
   2590 		}
   2591 		LLE_RUNLOCK(ln);
   2592 	}
   2593   nolladdropt:;
   2594 
   2595 	m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
   2596 
   2597 	/* just to be safe */
   2598 	if (m0->m_flags & M_DECRYPTED)
   2599 		goto noredhdropt;
   2600 	if (p - (u_char *)ip6 > maxlen)
   2601 		goto noredhdropt;
   2602 
   2603 	{
   2604 		/* redirected header option */
   2605 		int len;
   2606 		struct nd_opt_rd_hdr *nd_opt_rh;
   2607 
   2608 		/*
   2609 		 * compute the maximum size for icmp6 redirect header option.
   2610 		 * XXX room for auth header?
   2611 		 */
   2612 		len = maxlen - (p - (u_char *)ip6);
   2613 		len &= ~7;
   2614 
   2615 		/*
   2616 		 * Redirected header option spec (RFC2461 4.6.3) talks nothing
   2617 		 * about padding/truncate rule for the original IP packet.
   2618 		 * From the discussion on IPv6imp in Feb 1999,
   2619 		 * the consensus was:
   2620 		 * - "attach as much as possible" is the goal
   2621 		 * - pad if not aligned (original size can be guessed by
   2622 		 *   original ip6 header)
   2623 		 * Following code adds the padding if it is simple enough,
   2624 		 * and truncates if not.
   2625 		 */
   2626 		if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
   2627 			/* not enough room, truncate */
   2628 			m_adj(m0, (len - sizeof(*nd_opt_rh)) -
   2629 			    m0->m_pkthdr.len);
   2630 		} else {
   2631 			/*
   2632 			 * enough room, truncate if not aligned.
   2633 			 * we don't pad here for simplicity.
   2634 			 */
   2635 			size_t extra;
   2636 
   2637 			extra = m0->m_pkthdr.len % 8;
   2638 			if (extra) {
   2639 				/* truncate */
   2640 				m_adj(m0, -extra);
   2641 			}
   2642 			len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
   2643 		}
   2644 
   2645 		nd_opt_rh = (struct nd_opt_rd_hdr *)p;
   2646 		memset(nd_opt_rh, 0, sizeof(*nd_opt_rh));
   2647 		nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
   2648 		nd_opt_rh->nd_opt_rh_len = len >> 3;
   2649 		p += sizeof(*nd_opt_rh);
   2650 		m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
   2651 
   2652 		/* connect m0 to m */
   2653 		m->m_pkthdr.len += m0->m_pkthdr.len;
   2654 		m_cat(m, m0);
   2655 		m0 = NULL;
   2656 	}
   2657 noredhdropt:
   2658 	if (m0) {
   2659 		m_freem(m0);
   2660 		m0 = NULL;
   2661 	}
   2662 
   2663 	/* XXX: clear embedded link IDs in the inner header */
   2664 	in6_clearscope(&sip6->ip6_src);
   2665 	in6_clearscope(&sip6->ip6_dst);
   2666 	in6_clearscope(&nd_rd->nd_rd_target);
   2667 	in6_clearscope(&nd_rd->nd_rd_dst);
   2668 
   2669 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
   2670 
   2671 	nd_rd->nd_rd_cksum = 0;
   2672 	nd_rd->nd_rd_cksum
   2673 		= in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
   2674 
   2675 	/* send the packet to outside... */
   2676 	if (ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL) != 0)
   2677 		icmp6_ifstat_inc(ifp, ifs6_out_error);
   2678 
   2679 	icmp6_ifstat_inc(ifp, ifs6_out_msg);
   2680 	icmp6_ifstat_inc(ifp, ifs6_out_redirect);
   2681 	ICMP6_STATINC(ICMP6_STAT_OUTHIST + ND_REDIRECT);
   2682 
   2683 	return;
   2684 
   2685 fail:
   2686 	if (m)
   2687 		m_freem(m);
   2688 	if (m0)
   2689 		m_freem(m0);
   2690 }
   2691 
   2692 /*
   2693  * ICMPv6 socket option processing.
   2694  */
   2695 int
   2696 icmp6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
   2697 {
   2698 	int error = 0;
   2699 	struct in6pcb *in6p = sotoin6pcb(so);
   2700 
   2701 	if (sopt->sopt_level != IPPROTO_ICMPV6)
   2702 		return rip6_ctloutput(op, so, sopt);
   2703 
   2704 	switch (op) {
   2705 	case PRCO_SETOPT:
   2706 		switch (sopt->sopt_name) {
   2707 		case ICMP6_FILTER:
   2708 		    {
   2709 			struct icmp6_filter fil;
   2710 
   2711 			error = sockopt_get(sopt, &fil, sizeof(fil));
   2712 			if (error)
   2713 				break;
   2714 			memcpy(in6p->in6p_icmp6filt, &fil,
   2715 			    sizeof(struct icmp6_filter));
   2716 			error = 0;
   2717 			break;
   2718 		    }
   2719 
   2720 		default:
   2721 			error = ENOPROTOOPT;
   2722 			break;
   2723 		}
   2724 		break;
   2725 
   2726 	case PRCO_GETOPT:
   2727 		switch (sopt->sopt_name) {
   2728 		case ICMP6_FILTER:
   2729 		    {
   2730 			if (in6p->in6p_icmp6filt == NULL) {
   2731 				error = EINVAL;
   2732 				break;
   2733 			}
   2734 			error = sockopt_set(sopt, in6p->in6p_icmp6filt,
   2735 			    sizeof(struct icmp6_filter));
   2736 			break;
   2737 		    }
   2738 
   2739 		default:
   2740 			error = ENOPROTOOPT;
   2741 			break;
   2742 		}
   2743 		break;
   2744 	}
   2745 
   2746 	return (error);
   2747 }
   2748 
   2749 /*
   2750  * Perform rate limit check.
   2751  * Returns 0 if it is okay to send the icmp6 packet.
   2752  * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
   2753  * limitation.
   2754  *
   2755  * XXX per-destination/type check necessary?
   2756  */
   2757 static int
   2758 icmp6_ratelimit(
   2759 	const struct in6_addr *dst,	/* not used at this moment */
   2760 	const int type,		/* not used at this moment */
   2761 	const int code)		/* not used at this moment */
   2762 {
   2763 	int ret;
   2764 
   2765 	ret = 0;	/* okay to send */
   2766 
   2767 	/* PPS limit */
   2768 	if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
   2769 	    icmp6errppslim)) {
   2770 		/* The packet is subject to rate limit */
   2771 		ret++;
   2772 	}
   2773 
   2774 	return ret;
   2775 }
   2776 
   2777 static struct rtentry *
   2778 icmp6_mtudisc_clone(struct sockaddr *dst)
   2779 {
   2780 	struct rtentry *rt;
   2781 	int    error;
   2782 
   2783 	rt = rtalloc1(dst, 1);
   2784 	if (rt == 0)
   2785 		return NULL;
   2786 
   2787 	/* If we didn't get a host route, allocate one */
   2788 	if ((rt->rt_flags & RTF_HOST) == 0) {
   2789 		struct rtentry *nrt;
   2790 
   2791 		error = rtrequest(RTM_ADD, dst, rt->rt_gateway, NULL,
   2792 		    RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
   2793 		if (error) {
   2794 			rt_unref(rt);
   2795 			return NULL;
   2796 		}
   2797 		nrt->rt_rmx = rt->rt_rmx;
   2798 		rt_unref(rt);
   2799 		rt = nrt;
   2800 	}
   2801 	error = rt_timer_add(rt, icmp6_mtudisc_timeout,
   2802 			icmp6_mtudisc_timeout_q);
   2803 	if (error) {
   2804 		rt_unref(rt);
   2805 		return NULL;
   2806 	}
   2807 
   2808 	return rt;	/* caller need to call rtfree() */
   2809 }
   2810 
   2811 static void
   2812 icmp6_mtudisc_timeout(struct rtentry *rt, struct rttimer *r)
   2813 {
   2814 
   2815 	KASSERT(rt != NULL);
   2816 	rt_assert_referenced(rt);
   2817 
   2818 	if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
   2819 	    (RTF_DYNAMIC | RTF_HOST)) {
   2820 		rtrequest(RTM_DELETE, rt_getkey(rt),
   2821 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
   2822 	} else {
   2823 		if (!(rt->rt_rmx.rmx_locks & RTV_MTU))
   2824 			rt->rt_rmx.rmx_mtu = 0;
   2825 	}
   2826 }
   2827 
   2828 static void
   2829 icmp6_redirect_timeout(struct rtentry *rt, struct rttimer *r)
   2830 {
   2831 
   2832 	KASSERT(rt != NULL);
   2833 	rt_assert_referenced(rt);
   2834 
   2835 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) ==
   2836 	    (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) {
   2837 		rtrequest(RTM_DELETE, rt_getkey(rt),
   2838 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
   2839 	}
   2840 }
   2841 
   2842 /*
   2843  * sysctl helper routine for the net.inet6.icmp6.nd6 nodes.  silly?
   2844  */
   2845 static int
   2846 sysctl_net_inet6_icmp6_nd6(SYSCTLFN_ARGS)
   2847 {
   2848 	(void)&name;
   2849 	(void)&l;
   2850 	(void)&oname;
   2851 
   2852 	if (namelen != 0)
   2853 		return (EINVAL);
   2854 
   2855 	return (nd6_sysctl(rnode->sysctl_num, oldp, oldlenp,
   2856 	    /*XXXUNCONST*/
   2857 	    __UNCONST(newp), newlen));
   2858 }
   2859 
   2860 static int
   2861 sysctl_net_inet6_icmp6_stats(SYSCTLFN_ARGS)
   2862 {
   2863 
   2864 	return (NETSTAT_SYSCTL(icmp6stat_percpu, ICMP6_NSTATS));
   2865 }
   2866 
   2867 static int
   2868 sysctl_net_inet6_icmp6_redirtimeout(SYSCTLFN_ARGS)
   2869 {
   2870 	int error, tmp;
   2871 	struct sysctlnode node;
   2872 
   2873 	node = *rnode;
   2874 	node.sysctl_data = &tmp;
   2875 	tmp = icmp6_redirtimeout;
   2876 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2877 	if (error || newp == NULL)
   2878 		return error;
   2879 	if (tmp < 0)
   2880 		return EINVAL;
   2881 	icmp6_redirtimeout = tmp;
   2882 
   2883 	if (icmp6_redirect_timeout_q != NULL) {
   2884 		if (icmp6_redirtimeout == 0) {
   2885 			rt_timer_queue_destroy(icmp6_redirect_timeout_q);
   2886 		} else {
   2887 			rt_timer_queue_change(icmp6_redirect_timeout_q,
   2888 			    icmp6_redirtimeout);
   2889 		}
   2890 	} else if (icmp6_redirtimeout > 0) {
   2891 		icmp6_redirect_timeout_q =
   2892 		    rt_timer_queue_create(icmp6_redirtimeout);
   2893 	}
   2894 
   2895 	return 0;
   2896 }
   2897 
   2898 static void
   2899 sysctl_net_inet6_icmp6_setup(struct sysctllog **clog)
   2900 {
   2901 	extern int nd6_maxqueuelen; /* defined in nd6.c */
   2902 
   2903 	sysctl_createv(clog, 0, NULL, NULL,
   2904 		       CTLFLAG_PERMANENT,
   2905 		       CTLTYPE_NODE, "inet6", NULL,
   2906 		       NULL, 0, NULL, 0,
   2907 		       CTL_NET, PF_INET6, CTL_EOL);
   2908 	sysctl_createv(clog, 0, NULL, NULL,
   2909 		       CTLFLAG_PERMANENT,
   2910 		       CTLTYPE_NODE, "icmp6",
   2911 		       SYSCTL_DESCR("ICMPv6 related settings"),
   2912 		       NULL, 0, NULL, 0,
   2913 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6, CTL_EOL);
   2914 
   2915 	sysctl_createv(clog, 0, NULL, NULL,
   2916 		       CTLFLAG_PERMANENT,
   2917 		       CTLTYPE_STRUCT, "stats",
   2918 		       SYSCTL_DESCR("ICMPv6 transmission statistics"),
   2919 		       sysctl_net_inet6_icmp6_stats, 0, NULL, 0,
   2920 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2921 		       ICMPV6CTL_STATS, CTL_EOL);
   2922 	sysctl_createv(clog, 0, NULL, NULL,
   2923 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2924 		       CTLTYPE_INT, "rediraccept",
   2925 		       SYSCTL_DESCR("Accept and process redirect messages"),
   2926 		       NULL, 0, &icmp6_rediraccept, 0,
   2927 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2928 		       ICMPV6CTL_REDIRACCEPT, CTL_EOL);
   2929 	sysctl_createv(clog, 0, NULL, NULL,
   2930 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2931 		       CTLTYPE_INT, "redirtimeout",
   2932 		       SYSCTL_DESCR("Redirect generated route lifetime"),
   2933 		       sysctl_net_inet6_icmp6_redirtimeout, 0,
   2934 		       &icmp6_redirtimeout, 0,
   2935 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2936 		       ICMPV6CTL_REDIRTIMEOUT, CTL_EOL);
   2937 #if 0 /* obsoleted */
   2938 	sysctl_createv(clog, 0, NULL, NULL,
   2939 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2940 		       CTLTYPE_INT, "errratelimit", NULL,
   2941 		       NULL, 0, &icmp6_errratelimit, 0,
   2942 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2943 		       ICMPV6CTL_ERRRATELIMIT, CTL_EOL);
   2944 #endif
   2945 	sysctl_createv(clog, 0, NULL, NULL,
   2946 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2947 		       CTLTYPE_INT, "nd6_prune",
   2948 		       SYSCTL_DESCR("Neighbor discovery prune interval"),
   2949 		       NULL, 0, &nd6_prune, 0,
   2950 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2951 		       ICMPV6CTL_ND6_PRUNE, CTL_EOL);
   2952 	sysctl_createv(clog, 0, NULL, NULL,
   2953 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2954 		       CTLTYPE_INT, "nd6_delay",
   2955 		       SYSCTL_DESCR("First probe delay time"),
   2956 		       NULL, 0, &nd6_delay, 0,
   2957 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2958 		       ICMPV6CTL_ND6_DELAY, CTL_EOL);
   2959 	sysctl_createv(clog, 0, NULL, NULL,
   2960 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2961 		       CTLTYPE_INT, "nd6_umaxtries",
   2962 		       SYSCTL_DESCR("Number of unicast discovery attempts"),
   2963 		       NULL, 0, &nd6_umaxtries, 0,
   2964 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2965 		       ICMPV6CTL_ND6_UMAXTRIES, CTL_EOL);
   2966 	sysctl_createv(clog, 0, NULL, NULL,
   2967 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2968 		       CTLTYPE_INT, "nd6_mmaxtries",
   2969 		       SYSCTL_DESCR("Number of multicast discovery attempts"),
   2970 		       NULL, 0, &nd6_mmaxtries, 0,
   2971 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2972 		       ICMPV6CTL_ND6_MMAXTRIES, CTL_EOL);
   2973 	sysctl_createv(clog, 0, NULL, NULL,
   2974 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2975 		       CTLTYPE_INT, "nd6_useloopback",
   2976 		       SYSCTL_DESCR("Use loopback interface for local traffic"),
   2977 		       NULL, 0, &nd6_useloopback, 0,
   2978 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2979 		       ICMPV6CTL_ND6_USELOOPBACK, CTL_EOL);
   2980 #if 0 /* obsoleted */
   2981 	sysctl_createv(clog, 0, NULL, NULL,
   2982 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2983 		       CTLTYPE_INT, "nd6_proxyall", NULL,
   2984 		       NULL, 0, &nd6_proxyall, 0,
   2985 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2986 		       ICMPV6CTL_ND6_PROXYALL, CTL_EOL);
   2987 #endif
   2988 	sysctl_createv(clog, 0, NULL, NULL,
   2989 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2990 		       CTLTYPE_INT, "nodeinfo",
   2991 		       SYSCTL_DESCR("Respond to node information requests"),
   2992 		       NULL, 0, &icmp6_nodeinfo, 0,
   2993 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   2994 		       ICMPV6CTL_NODEINFO, CTL_EOL);
   2995 	sysctl_createv(clog, 0, NULL, NULL,
   2996 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2997 		       CTLTYPE_INT, "errppslimit",
   2998 		       SYSCTL_DESCR("Maximum ICMP errors sent per second"),
   2999 		       NULL, 0, &icmp6errppslim, 0,
   3000 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   3001 		       ICMPV6CTL_ERRPPSLIMIT, CTL_EOL);
   3002 	sysctl_createv(clog, 0, NULL, NULL,
   3003 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3004 		       CTLTYPE_INT, "nd6_maxnudhint",
   3005 		       SYSCTL_DESCR("Maximum neighbor unreachable hint count"),
   3006 		       NULL, 0, &nd6_maxnudhint, 0,
   3007 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   3008 		       ICMPV6CTL_ND6_MAXNUDHINT, CTL_EOL);
   3009 	sysctl_createv(clog, 0, NULL, NULL,
   3010 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3011 		       CTLTYPE_INT, "mtudisc_hiwat",
   3012 		       SYSCTL_DESCR("Low mark on MTU Discovery route timers"),
   3013 		       NULL, 0, &icmp6_mtudisc_hiwat, 0,
   3014 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   3015 		       ICMPV6CTL_MTUDISC_HIWAT, CTL_EOL);
   3016 	sysctl_createv(clog, 0, NULL, NULL,
   3017 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3018 		       CTLTYPE_INT, "mtudisc_lowat",
   3019 		       SYSCTL_DESCR("Low mark on MTU Discovery route timers"),
   3020 		       NULL, 0, &icmp6_mtudisc_lowat, 0,
   3021 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   3022 		       ICMPV6CTL_MTUDISC_LOWAT, CTL_EOL);
   3023 	sysctl_createv(clog, 0, NULL, NULL,
   3024 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3025 		       CTLTYPE_INT, "nd6_debug",
   3026 		       SYSCTL_DESCR("Enable neighbor discovery debug output"),
   3027 		       NULL, 0, &nd6_debug, 0,
   3028 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   3029 		       ICMPV6CTL_ND6_DEBUG, CTL_EOL);
   3030 	sysctl_createv(clog, 0, NULL, NULL,
   3031 		       CTLFLAG_PERMANENT,
   3032 		       CTLTYPE_STRUCT, "nd6_drlist",
   3033 		       SYSCTL_DESCR("Default router list"),
   3034 		       sysctl_net_inet6_icmp6_nd6, 0, NULL, 0,
   3035 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   3036 		       ICMPV6CTL_ND6_DRLIST, CTL_EOL);
   3037 	sysctl_createv(clog, 0, NULL, NULL,
   3038 		       CTLFLAG_PERMANENT,
   3039 		       CTLTYPE_STRUCT, "nd6_prlist",
   3040 		       SYSCTL_DESCR("Prefix list"),
   3041 		       sysctl_net_inet6_icmp6_nd6, 0, NULL, 0,
   3042 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   3043 		       ICMPV6CTL_ND6_PRLIST, CTL_EOL);
   3044 	sysctl_createv(clog, 0, NULL, NULL,
   3045 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   3046 		       CTLTYPE_INT, "maxqueuelen",
   3047 		       SYSCTL_DESCR("max packet queue len for a unresolved ND"),
   3048 		       NULL, 1, &nd6_maxqueuelen, 0,
   3049 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
   3050 		       ICMPV6CTL_ND6_MAXQLEN, CTL_EOL);
   3051 }
   3052 
   3053 void
   3054 icmp6_statinc(u_int stat)
   3055 {
   3056 
   3057 	KASSERT(stat < ICMP6_NSTATS);
   3058 	ICMP6_STATINC(stat);
   3059 }
   3060