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