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