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