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