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