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