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