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