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