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