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