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