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