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