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