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