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