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