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