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