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