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