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