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