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