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