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