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