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