xref: /netbsd-src/sys/netinet6/icmp6.c (revision 27527e67bbdf8d9ec84fd58803048ed6d181ece2)
1 /*	$NetBSD: icmp6.c,v 1.113 2006/01/21 00:15:36 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.113 2006/01/21 00:15:36 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 	mld6_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 outut 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 wheter 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 deilver 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 			mld6_input(m, off);
687 			m = NULL;
688 			goto freeit;
689 		}
690 		mld6_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 		switch (ni6->ni_code) {
1247 		case ICMP6_NI_SUBJ_IPV6:
1248 #if ICMP6_NI_SUBJ_IPV6 != 0
1249 		case 0:
1250 #endif
1251 			/*
1252 			 * backward compatibility - try to accept 03 draft
1253 			 * format, where no Subject is present.
1254 			 */
1255 			if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1256 			    subjlen == 0) {
1257 				oldfqdn++;
1258 				break;
1259 			}
1260 #if ICMP6_NI_SUBJ_IPV6 != 0
1261 			if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1262 				goto bad;
1263 #endif
1264 
1265 			if (subjlen != sizeof(sin6.sin6_addr))
1266 				goto bad;
1267 
1268 			/*
1269 			 * Validate Subject address.
1270 			 *
1271 			 * Not sure what exactly "address belongs to the node"
1272 			 * means in the spec, is it just unicast, or what?
1273 			 *
1274 			 * At this moment we consider Subject address as
1275 			 * "belong to the node" if the Subject address equals
1276 			 * to the IPv6 destination address; validation for
1277 			 * IPv6 destination address should have done enough
1278 			 * check for us.
1279 			 *
1280 			 * We do not do proxy at this moment.
1281 			 */
1282 			/* m_pulldown instead of copy? */
1283 			m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1284 			    subjlen, (caddr_t)&in6_subj);
1285 			if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1286 				goto bad;
1287 
1288 			subj = (char *)&in6_subj;
1289 			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
1290 				break;
1291 
1292 			/*
1293 			 * XXX if we are to allow other cases, we should really
1294 			 * be careful about scope here.
1295 			 * basically, we should disallow queries toward IPv6
1296 			 * destination X with subject Y, if scope(X) > scope(Y).
1297 			 * if we allow scope(X) > scope(Y), it will result in
1298 			 * information leakage across scope boundary.
1299 			 */
1300 			goto bad;
1301 
1302 		case ICMP6_NI_SUBJ_FQDN:
1303 			/*
1304 			 * Validate Subject name with gethostname(3).
1305 			 *
1306 			 * The behavior may need some debate, since:
1307 			 * - we are not sure if the node has FQDN as
1308 			 *   hostname (returned by gethostname(3)).
1309 			 * - the code does wildcard match for truncated names.
1310 			 *   however, we are not sure if we want to perform
1311 			 *   wildcard match, if gethostname(3) side has
1312 			 *   truncated hostname.
1313 			 */
1314 			n = ni6_nametodns(hostname, hostnamelen, 0);
1315 			if (!n || n->m_next || n->m_len == 0)
1316 				goto bad;
1317 			IP6_EXTHDR_GET(subj, char *, m,
1318 			    off + sizeof(struct icmp6_nodeinfo), subjlen);
1319 			if (subj == NULL)
1320 				goto bad;
1321 			if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1322 					n->m_len)) {
1323 				goto bad;
1324 			}
1325 			m_freem(n);
1326 			n = NULL;
1327 			break;
1328 
1329 		case ICMP6_NI_SUBJ_IPV4:	/* XXX: to be implemented? */
1330 		default:
1331 			goto bad;
1332 		}
1333 		break;
1334 	}
1335 
1336 	/* refuse based on configuration.  XXX ICMP6_NI_REFUSED? */
1337 	switch (qtype) {
1338 	case NI_QTYPE_FQDN:
1339 		if ((icmp6_nodeinfo & 1) == 0)
1340 			goto bad;
1341 		break;
1342 	case NI_QTYPE_NODEADDR:
1343 		if ((icmp6_nodeinfo & 2) == 0)
1344 			goto bad;
1345 		break;
1346 	}
1347 
1348 	/* guess reply length */
1349 	switch (qtype) {
1350 	case NI_QTYPE_NOOP:
1351 		break;		/* no reply data */
1352 	case NI_QTYPE_SUPTYPES:
1353 		replylen += sizeof(u_int32_t);
1354 		break;
1355 	case NI_QTYPE_FQDN:
1356 		/* XXX will append an mbuf */
1357 		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1358 		break;
1359 	case NI_QTYPE_NODEADDR:
1360 		addrs = ni6_addrs(ni6, m, &ifp, subj);
1361 		if ((replylen += addrs * (sizeof(struct in6_addr) +
1362 					  sizeof(u_int32_t))) > MCLBYTES)
1363 			replylen = MCLBYTES; /* XXX: will truncate pkt later */
1364 		break;
1365 	default:
1366 		/*
1367 		 * XXX: We must return a reply with the ICMP6 code
1368 		 * `unknown Qtype' in this case.  However we regard the case
1369 		 * as an FQDN query for backward compatibility.
1370 		 * Older versions set a random value to this field,
1371 		 * so it rarely varies in the defined qtypes.
1372 		 * But the mechanism is not reliable...
1373 		 * maybe we should obsolete older versions.
1374 		 */
1375 		qtype = NI_QTYPE_FQDN;
1376 		/* XXX will append an mbuf */
1377 		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1378 		oldfqdn++;
1379 		break;
1380 	}
1381 
1382 	/* allocate an mbuf to reply. */
1383 	MGETHDR(n, M_DONTWAIT, m->m_type);
1384 	if (n == NULL) {
1385 		m_freem(m);
1386 		return (NULL);
1387 	}
1388 	M_MOVE_PKTHDR(n, m); /* just for rcvif */
1389 	if (replylen > MHLEN) {
1390 		if (replylen > MCLBYTES) {
1391 			/*
1392 			 * XXX: should we try to allocate more? But MCLBYTES
1393 			 * is probably much larger than IPV6_MMTU...
1394 			 */
1395 			goto bad;
1396 		}
1397 		MCLGET(n, M_DONTWAIT);
1398 		if ((n->m_flags & M_EXT) == 0) {
1399 			goto bad;
1400 		}
1401 	}
1402 	n->m_pkthdr.len = n->m_len = replylen;
1403 
1404 	/* copy mbuf header and IPv6 + Node Information base headers */
1405 	bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1406 	nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1407 	bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1408 
1409 	/* qtype dependent procedure */
1410 	switch (qtype) {
1411 	case NI_QTYPE_NOOP:
1412 		nni6->ni_code = ICMP6_NI_SUCCESS;
1413 		nni6->ni_flags = 0;
1414 		break;
1415 	case NI_QTYPE_SUPTYPES:
1416 	{
1417 		u_int32_t v;
1418 		nni6->ni_code = ICMP6_NI_SUCCESS;
1419 		nni6->ni_flags = htons(0x0000);	/* raw bitmap */
1420 		/* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1421 		v = (u_int32_t)htonl(0x0000000f);
1422 		bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1423 		break;
1424 	}
1425 	case NI_QTYPE_FQDN:
1426 		nni6->ni_code = ICMP6_NI_SUCCESS;
1427 		fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1428 						sizeof(struct ip6_hdr) +
1429 						sizeof(struct icmp6_nodeinfo));
1430 		nni6->ni_flags = 0; /* XXX: meaningless TTL */
1431 		fqdn->ni_fqdn_ttl = 0;	/* ditto. */
1432 		/*
1433 		 * XXX do we really have FQDN in variable "hostname"?
1434 		 */
1435 		n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1436 		if (n->m_next == NULL)
1437 			goto bad;
1438 		/* XXX we assume that n->m_next is not a chain */
1439 		if (n->m_next->m_next != NULL)
1440 			goto bad;
1441 		n->m_pkthdr.len += n->m_next->m_len;
1442 		break;
1443 	case NI_QTYPE_NODEADDR:
1444 	{
1445 		int lenlim, copied;
1446 
1447 		nni6->ni_code = ICMP6_NI_SUCCESS;
1448 		n->m_pkthdr.len = n->m_len =
1449 		    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1450 		lenlim = M_TRAILINGSPACE(n);
1451 		copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1452 		/* XXX: reset mbuf length */
1453 		n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1454 			sizeof(struct icmp6_nodeinfo) + copied;
1455 		break;
1456 	}
1457 	default:
1458 		break;		/* XXX impossible! */
1459 	}
1460 
1461 	nni6->ni_type = ICMP6_NI_REPLY;
1462 	m_freem(m);
1463 	return (n);
1464 
1465   bad:
1466 	m_freem(m);
1467 	if (n)
1468 		m_freem(n);
1469 	return (NULL);
1470 }
1471 #undef hostnamelen
1472 
1473 #define isupper(x) ('A' <= (x) && (x) <= 'Z')
1474 #define isalpha(x) (('A' <= (x) && (x) <= 'Z') || ('a' <= (x) && (x) <= 'z'))
1475 #define isalnum(x) (isalpha(x) || ('0' <= (x) && (x) <= '9'))
1476 #define tolower(x) (isupper(x) ? (x) + 'a' - 'A' : (x))
1477 
1478 /*
1479  * make a mbuf with DNS-encoded string.  no compression support.
1480  *
1481  * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1482  * treated as truncated name (two \0 at the end).  this is a wild guess.
1483  */
1484 static struct mbuf *
1485 ni6_nametodns(name, namelen, old)
1486 	const char *name;
1487 	int namelen;
1488 	int old;	/* return pascal string if non-zero */
1489 {
1490 	struct mbuf *m;
1491 	char *cp, *ep;
1492 	const char *p, *q;
1493 	int i, len, nterm;
1494 
1495 	if (old)
1496 		len = namelen + 1;
1497 	else
1498 		len = MCLBYTES;
1499 
1500 	/* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1501 	MGET(m, M_DONTWAIT, MT_DATA);
1502 	if (m && len > MLEN) {
1503 		MCLGET(m, M_DONTWAIT);
1504 		if ((m->m_flags & M_EXT) == 0)
1505 			goto fail;
1506 	}
1507 	if (!m)
1508 		goto fail;
1509 	m->m_next = NULL;
1510 
1511 	if (old) {
1512 		m->m_len = len;
1513 		*mtod(m, char *) = namelen;
1514 		bcopy(name, mtod(m, char *) + 1, namelen);
1515 		return m;
1516 	} else {
1517 		m->m_len = 0;
1518 		cp = mtod(m, char *);
1519 		ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1520 
1521 		/* if not certain about my name, return empty buffer */
1522 		if (namelen == 0)
1523 			return m;
1524 
1525 		/*
1526 		 * guess if it looks like shortened hostname, or FQDN.
1527 		 * shortened hostname needs two trailing "\0".
1528 		 */
1529 		i = 0;
1530 		for (p = name; p < name + namelen; p++) {
1531 			if (*p && *p == '.')
1532 				i++;
1533 		}
1534 		if (i < 2)
1535 			nterm = 2;
1536 		else
1537 			nterm = 1;
1538 
1539 		p = name;
1540 		while (cp < ep && p < name + namelen) {
1541 			i = 0;
1542 			for (q = p; q < name + namelen && *q && *q != '.'; q++)
1543 				i++;
1544 			/* result does not fit into mbuf */
1545 			if (cp + i + 1 >= ep)
1546 				goto fail;
1547 			/*
1548 			 * DNS label length restriction, RFC1035 page 8.
1549 			 * "i == 0" case is included here to avoid returning
1550 			 * 0-length label on "foo..bar".
1551 			 */
1552 			if (i <= 0 || i >= 64)
1553 				goto fail;
1554 			*cp++ = i;
1555 			if (!isalpha(p[0]) || !isalnum(p[i - 1]))
1556 				goto fail;
1557 			while (i > 0) {
1558 				if (!isalnum(*p) && *p != '-')
1559 					goto fail;
1560 				if (isupper(*p)) {
1561 					*cp++ = tolower(*p);
1562 					p++;
1563 				} else
1564 					*cp++ = *p++;
1565 				i--;
1566 			}
1567 			p = q;
1568 			if (p < name + namelen && *p == '.')
1569 				p++;
1570 		}
1571 		/* termination */
1572 		if (cp + nterm >= ep)
1573 			goto fail;
1574 		while (nterm-- > 0)
1575 			*cp++ = '\0';
1576 		m->m_len = cp - mtod(m, char *);
1577 		return m;
1578 	}
1579 
1580 	panic("should not reach here");
1581 	/* NOTREACHED */
1582 
1583  fail:
1584 	if (m)
1585 		m_freem(m);
1586 	return NULL;
1587 }
1588 
1589 /*
1590  * check if two DNS-encoded string matches.  takes care of truncated
1591  * form (with \0\0 at the end).  no compression support.
1592  * XXX upper/lowercase match (see RFC2065)
1593  */
1594 static int
1595 ni6_dnsmatch(a, alen, b, blen)
1596 	const char *a;
1597 	int alen;
1598 	const char *b;
1599 	int blen;
1600 {
1601 	const char *a0, *b0;
1602 	int l;
1603 
1604 	/* simplest case - need validation? */
1605 	if (alen == blen && bcmp(a, b, alen) == 0)
1606 		return 1;
1607 
1608 	a0 = a;
1609 	b0 = b;
1610 
1611 	/* termination is mandatory */
1612 	if (alen < 2 || blen < 2)
1613 		return 0;
1614 	if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1615 		return 0;
1616 	alen--;
1617 	blen--;
1618 
1619 	while (a - a0 < alen && b - b0 < blen) {
1620 		if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1621 			return 0;
1622 
1623 		if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1624 			return 0;
1625 		/* we don't support compression yet */
1626 		if (a[0] >= 64 || b[0] >= 64)
1627 			return 0;
1628 
1629 		/* truncated case */
1630 		if (a[0] == 0 && a - a0 == alen - 1)
1631 			return 1;
1632 		if (b[0] == 0 && b - b0 == blen - 1)
1633 			return 1;
1634 		if (a[0] == 0 || b[0] == 0)
1635 			return 0;
1636 
1637 		if (a[0] != b[0])
1638 			return 0;
1639 		l = a[0];
1640 		if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1641 			return 0;
1642 		if (bcmp(a + 1, b + 1, l) != 0)
1643 			return 0;
1644 
1645 		a += 1 + l;
1646 		b += 1 + l;
1647 	}
1648 
1649 	if (a - a0 == alen && b - b0 == blen)
1650 		return 1;
1651 	else
1652 		return 0;
1653 }
1654 
1655 /*
1656  * calculate the number of addresses to be returned in the node info reply.
1657  */
1658 static int
1659 ni6_addrs(ni6, m, ifpp, subj)
1660 	struct icmp6_nodeinfo *ni6;
1661 	struct mbuf *m;
1662 	struct ifnet **ifpp;
1663 	char *subj;
1664 {
1665 	struct ifnet *ifp;
1666 	struct in6_ifaddr *ifa6;
1667 	struct ifaddr *ifa;
1668 	struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
1669 	int addrs = 0, addrsofif, iffound = 0;
1670 	int niflags = ni6->ni_flags;
1671 
1672 	if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1673 		switch (ni6->ni_code) {
1674 		case ICMP6_NI_SUBJ_IPV6:
1675 			if (subj == NULL) /* must be impossible... */
1676 				return (0);
1677 			subj_ip6 = (struct sockaddr_in6 *)subj;
1678 			break;
1679 		default:
1680 			/*
1681 			 * XXX: we only support IPv6 subject address for
1682 			 * this Qtype.
1683 			 */
1684 			return (0);
1685 		}
1686 	}
1687 
1688 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1689 	{
1690 		addrsofif = 0;
1691 		for (ifa = ifp->if_addrlist.tqh_first; ifa;
1692 		     ifa = ifa->ifa_list.tqe_next)
1693 		{
1694 			if (ifa->ifa_addr->sa_family != AF_INET6)
1695 				continue;
1696 			ifa6 = (struct in6_ifaddr *)ifa;
1697 
1698 			if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1699 			    IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1700 					       &ifa6->ia_addr.sin6_addr))
1701 				iffound = 1;
1702 
1703 			/*
1704 			 * IPv4-mapped addresses can only be returned by a
1705 			 * Node Information proxy, since they represent
1706 			 * addresses of IPv4-only nodes, which perforce do
1707 			 * not implement this protocol.
1708 			 * [icmp-name-lookups-07, Section 5.4]
1709 			 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1710 			 * this function at this moment.
1711 			 */
1712 
1713 			/* What do we have to do about ::1? */
1714 			switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1715 			case IPV6_ADDR_SCOPE_LINKLOCAL:
1716 				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1717 					continue;
1718 				break;
1719 			case IPV6_ADDR_SCOPE_SITELOCAL:
1720 				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1721 					continue;
1722 				break;
1723 			case IPV6_ADDR_SCOPE_GLOBAL:
1724 				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1725 					continue;
1726 				break;
1727 			default:
1728 				continue;
1729 			}
1730 
1731 			/*
1732 			 * check if anycast is okay.
1733 			 * XXX: just experimental.  not in the spec.
1734 			 */
1735 			if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1736 			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1737 				continue; /* we need only unicast addresses */
1738 
1739 			addrsofif++; /* count the address */
1740 		}
1741 		if (iffound) {
1742 			*ifpp = ifp;
1743 			return (addrsofif);
1744 		}
1745 
1746 		addrs += addrsofif;
1747 	}
1748 
1749 	return (addrs);
1750 }
1751 
1752 static int
1753 ni6_store_addrs(ni6, nni6, ifp0, resid)
1754 	struct icmp6_nodeinfo *ni6, *nni6;
1755 	struct ifnet *ifp0;
1756 	int resid;
1757 {
1758 	struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1759 	struct in6_ifaddr *ifa6;
1760 	struct ifaddr *ifa;
1761 	struct ifnet *ifp_dep = NULL;
1762 	int copied = 0, allow_deprecated = 0;
1763 	u_char *cp = (u_char *)(nni6 + 1);
1764 	int niflags = ni6->ni_flags;
1765 	u_int32_t ltime;
1766 
1767 	if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1768 		return (0);	/* needless to copy */
1769 
1770   again:
1771 
1772 	for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
1773 	{
1774 		for (ifa = ifp->if_addrlist.tqh_first; ifa;
1775 		     ifa = ifa->ifa_list.tqe_next)
1776 		{
1777 			if (ifa->ifa_addr->sa_family != AF_INET6)
1778 				continue;
1779 			ifa6 = (struct in6_ifaddr *)ifa;
1780 
1781 			if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1782 			    allow_deprecated == 0) {
1783 				/*
1784 				 * prefererred address should be put before
1785 				 * deprecated addresses.
1786 				 */
1787 
1788 				/* record the interface for later search */
1789 				if (ifp_dep == NULL)
1790 					ifp_dep = ifp;
1791 
1792 				continue;
1793 			}
1794 			else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1795 				 allow_deprecated != 0)
1796 				continue; /* we now collect deprecated addrs */
1797 
1798 			/* What do we have to do about ::1? */
1799 			switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1800 			case IPV6_ADDR_SCOPE_LINKLOCAL:
1801 				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1802 					continue;
1803 				break;
1804 			case IPV6_ADDR_SCOPE_SITELOCAL:
1805 				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1806 					continue;
1807 				break;
1808 			case IPV6_ADDR_SCOPE_GLOBAL:
1809 				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1810 					continue;
1811 				break;
1812 			default:
1813 				continue;
1814 			}
1815 
1816 			/*
1817 			 * check if anycast is okay.
1818 			 * XXX: just experimental.  not in the spec.
1819 			 */
1820 			if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1821 			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1822 				continue;
1823 
1824 			/* now we can copy the address */
1825 			if (resid < sizeof(struct in6_addr) +
1826 			    sizeof(u_int32_t)) {
1827 				/*
1828 				 * We give up much more copy.
1829 				 * Set the truncate flag and return.
1830 				 */
1831 				nni6->ni_flags |=
1832 					NI_NODEADDR_FLAG_TRUNCATE;
1833 				return (copied);
1834 			}
1835 
1836 			/*
1837 			 * Set the TTL of the address.
1838 			 * The TTL value should be one of the following
1839 			 * according to the specification:
1840 			 *
1841 			 * 1. The remaining lifetime of a DHCP lease on the
1842 			 *    address, or
1843 			 * 2. The remaining Valid Lifetime of a prefix from
1844 			 *    which the address was derived through Stateless
1845 			 *    Autoconfiguration.
1846 			 *
1847 			 * Note that we currently do not support stateful
1848 			 * address configuration by DHCPv6, so the former
1849 			 * case can't happen.
1850 			 *
1851 			 * TTL must be 2^31 > TTL >= 0.
1852 			 */
1853 			if (ifa6->ia6_lifetime.ia6t_expire == 0)
1854 				ltime = ND6_INFINITE_LIFETIME;
1855 			else {
1856 				if (ifa6->ia6_lifetime.ia6t_expire >
1857 				    time.tv_sec)
1858 					ltime = ifa6->ia6_lifetime.ia6t_expire - time.tv_sec;
1859 				else
1860 					ltime = 0;
1861 			}
1862 			if (ltime > 0x7fffffff)
1863 				ltime = 0x7fffffff;
1864 			ltime = htonl(ltime);
1865 
1866 			bcopy(&ltime, cp, sizeof(u_int32_t));
1867 			cp += sizeof(u_int32_t);
1868 
1869 			/* copy the address itself */
1870 			bcopy(&ifa6->ia_addr.sin6_addr, cp,
1871 			      sizeof(struct in6_addr));
1872 			in6_clearscope((struct in6_addr *)cp); /* XXX */
1873 			cp += sizeof(struct in6_addr);
1874 
1875 			resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1876 			copied += (sizeof(struct in6_addr) +
1877 				   sizeof(u_int32_t));
1878 		}
1879 		if (ifp0)	/* we need search only on the specified IF */
1880 			break;
1881 	}
1882 
1883 	if (allow_deprecated == 0 && ifp_dep != NULL) {
1884 		ifp = ifp_dep;
1885 		allow_deprecated = 1;
1886 
1887 		goto again;
1888 	}
1889 
1890 	return (copied);
1891 }
1892 
1893 /*
1894  * XXX almost dup'ed code with rip6_input.
1895  */
1896 static int
1897 icmp6_rip6_input(mp, off)
1898 	struct	mbuf **mp;
1899 	int	off;
1900 {
1901 	struct mbuf *m = *mp;
1902 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1903 	struct inpcb_hdr *inph;
1904 	struct in6pcb *in6p;
1905 	struct in6pcb *last = NULL;
1906 	struct sockaddr_in6 rip6src;
1907 	struct icmp6_hdr *icmp6;
1908 	struct mbuf *opts = NULL;
1909 
1910 	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1911 	if (icmp6 == NULL) {
1912 		/* m is already reclaimed */
1913 		return IPPROTO_DONE;
1914 	}
1915 
1916 	/*
1917 	 * XXX: the address may have embedded scope zone ID, which should be
1918 	 * hidden from applications.
1919 	 */
1920 	bzero(&rip6src, sizeof(rip6src));
1921 	rip6src.sin6_len = sizeof(struct sockaddr_in6);
1922 	rip6src.sin6_family = AF_INET6;
1923 	rip6src.sin6_addr = ip6->ip6_src;
1924 	if (sa6_recoverscope(&rip6src)) {
1925 		m_freem(m);
1926 		return (IPPROTO_DONE);
1927 	}
1928 
1929 	CIRCLEQ_FOREACH(inph, &raw6cbtable.inpt_queue, inph_queue) {
1930 		in6p = (struct in6pcb *)inph;
1931 		if (in6p->in6p_af != AF_INET6)
1932 			continue;
1933 		if (in6p->in6p_ip6.ip6_nxt != IPPROTO_ICMPV6)
1934 			continue;
1935 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1936 		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1937 			continue;
1938 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1939 		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1940 			continue;
1941 		if (in6p->in6p_icmp6filt
1942 		    && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1943 				 in6p->in6p_icmp6filt))
1944 			continue;
1945 		if (last) {
1946 			struct	mbuf *n;
1947 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1948 				if (last->in6p_flags & IN6P_CONTROLOPTS)
1949 					ip6_savecontrol(last, &opts, ip6, n);
1950 				/* strip intermediate headers */
1951 				m_adj(n, off);
1952 				if (sbappendaddr(&last->in6p_socket->so_rcv,
1953 						 (struct sockaddr *)&rip6src,
1954 						 n, opts) == 0) {
1955 					/* should notify about lost packet */
1956 					m_freem(n);
1957 					if (opts)
1958 						m_freem(opts);
1959 				} else
1960 					sorwakeup(last->in6p_socket);
1961 				opts = NULL;
1962 			}
1963 		}
1964 		last = in6p;
1965 	}
1966 	if (last) {
1967 		if (last->in6p_flags & IN6P_CONTROLOPTS)
1968 			ip6_savecontrol(last, &opts, ip6, m);
1969 		/* strip intermediate headers */
1970 		m_adj(m, off);
1971 		if (sbappendaddr(&last->in6p_socket->so_rcv,
1972 				(struct sockaddr *)&rip6src, m, opts) == 0) {
1973 			m_freem(m);
1974 			if (opts)
1975 				m_freem(opts);
1976 		} else
1977 			sorwakeup(last->in6p_socket);
1978 	} else {
1979 		m_freem(m);
1980 		ip6stat.ip6s_delivered--;
1981 	}
1982 	return IPPROTO_DONE;
1983 }
1984 
1985 /*
1986  * Reflect the ip6 packet back to the source.
1987  * OFF points to the icmp6 header, counted from the top of the mbuf.
1988  *
1989  * Note: RFC 1885 required that an echo reply should be truncated if it
1990  * did not fit in with (return) path MTU, and KAME code supported the
1991  * behavior.  However, as a clarification after the RFC, this limitation
1992  * was removed in a revised version of the spec, RFC 2463.  We had kept the
1993  * old behavior, with a (non-default) ifdef block, while the new version of
1994  * the spec was an internet-draft status, and even after the new RFC was
1995  * published.  But it would rather make sense to clean the obsoleted part
1996  * up, and to make the code simpler at this stage.
1997  */
1998 void
1999 icmp6_reflect(m, off)
2000 	struct	mbuf *m;
2001 	size_t off;
2002 {
2003 	struct ip6_hdr *ip6;
2004 	struct icmp6_hdr *icmp6;
2005 	struct in6_ifaddr *ia;
2006 	int plen;
2007 	int type, code;
2008 	struct ifnet *outif = NULL;
2009 	struct in6_addr origdst, *src = NULL;
2010 
2011 	/* too short to reflect */
2012 	if (off < sizeof(struct ip6_hdr)) {
2013 		nd6log((LOG_DEBUG,
2014 		    "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2015 		    (u_long)off, (u_long)sizeof(struct ip6_hdr),
2016 		    __FILE__, __LINE__));
2017 		goto bad;
2018 	}
2019 
2020 	/*
2021 	 * If there are extra headers between IPv6 and ICMPv6, strip
2022 	 * off that header first.
2023 	 */
2024 #ifdef DIAGNOSTIC
2025 	if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2026 		panic("assumption failed in icmp6_reflect");
2027 #endif
2028 	if (off > sizeof(struct ip6_hdr)) {
2029 		size_t l;
2030 		struct ip6_hdr nip6;
2031 
2032 		l = off - sizeof(struct ip6_hdr);
2033 		m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2034 		m_adj(m, l);
2035 		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2036 		if (m->m_len < l) {
2037 			if ((m = m_pullup(m, l)) == NULL)
2038 				return;
2039 		}
2040 		bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2041 	} else /* off == sizeof(struct ip6_hdr) */ {
2042 		size_t l;
2043 		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2044 		if (m->m_len < l) {
2045 			if ((m = m_pullup(m, l)) == NULL)
2046 				return;
2047 		}
2048 	}
2049 	plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2050 	ip6 = mtod(m, struct ip6_hdr *);
2051 	ip6->ip6_nxt = IPPROTO_ICMPV6;
2052 	icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2053 	type = icmp6->icmp6_type; /* keep type for statistics */
2054 	code = icmp6->icmp6_code; /* ditto. */
2055 
2056 	origdst = ip6->ip6_dst;
2057 	/*
2058 	 * ip6_input() drops a packet if its src is multicast.
2059 	 * So, the src is never multicast.
2060 	 */
2061 	ip6->ip6_dst = ip6->ip6_src;
2062 
2063 	/*
2064 	 * If the incoming packet was addressed directly to us (i.e. unicast),
2065 	 * use dst as the src for the reply.
2066 	 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
2067 	 * (for example) when we encounter an error while forwarding procedure
2068 	 * destined to a duplicated address of ours.
2069 	 * Note that ip6_getdstifaddr() may fail if we are in an error handling
2070 	 * procedure of an outgoing packet of our own, in which case we need
2071 	 * to search in the ifaddr list.
2072 	 */
2073 	if (!IN6_IS_ADDR_MULTICAST(&origdst)) {
2074 		if ((ia = ip6_getdstifaddr(m))) {
2075 			if (!(ia->ia6_flags &
2076 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)))
2077 				src = &ia->ia_addr.sin6_addr;
2078 		} else {
2079 			struct sockaddr_in6 d;
2080 
2081 			bzero(&d, sizeof(d));
2082 			d.sin6_family = AF_INET6;
2083 			d.sin6_len = sizeof(d);
2084 			d.sin6_addr = origdst;
2085 			ia = (struct in6_ifaddr *)
2086 			    ifa_ifwithaddr((struct sockaddr *)&d);
2087 			if (ia &&
2088 			    !(ia->ia6_flags &
2089 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) {
2090 				src = &ia->ia_addr.sin6_addr;
2091 			}
2092 		}
2093 	}
2094 
2095 	if (src == NULL) {
2096 		int e;
2097 		struct sockaddr_in6 sin6;
2098 		struct route_in6 ro;
2099 
2100 		/*
2101 		 * This case matches to multicasts, our anycast, or unicasts
2102 		 * that we do not own.  Select a source address based on the
2103 		 * source address of the erroneous packet.
2104 		 */
2105 		bzero(&sin6, sizeof(sin6));
2106 		sin6.sin6_family = AF_INET6;
2107 		sin6.sin6_len = sizeof(sin6);
2108 		sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */
2109 
2110 		bzero(&ro, sizeof(ro));
2111 		src = in6_selectsrc(&sin6, NULL, NULL, &ro, NULL, &outif, &e);
2112 		if (ro.ro_rt) { /* XXX: see comments in icmp6_mtudisc_update */
2113 			RTFREE(ro.ro_rt); /* XXX: we could use this */
2114 		}
2115 		if (src == NULL) {
2116 			nd6log((LOG_DEBUG,
2117 			    "icmp6_reflect: source can't be determined: "
2118 			    "dst=%s, error=%d\n",
2119 			    ip6_sprintf(&sin6.sin6_addr), e));
2120 			goto bad;
2121 		}
2122 	}
2123 
2124 	ip6->ip6_src = *src;
2125 	ip6->ip6_flow = 0;
2126 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2127 	ip6->ip6_vfc |= IPV6_VERSION;
2128 	ip6->ip6_nxt = IPPROTO_ICMPV6;
2129 	if (m->m_pkthdr.rcvif) {
2130 		/* XXX: This may not be the outgoing interface */
2131 		ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2132 	} else
2133 		ip6->ip6_hlim = ip6_defhlim;
2134 
2135 	icmp6->icmp6_cksum = 0;
2136 	icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2137 					sizeof(struct ip6_hdr), plen);
2138 
2139 	/*
2140 	 * XXX option handling
2141 	 */
2142 
2143 	m->m_flags &= ~(M_BCAST|M_MCAST);
2144 
2145 	/*
2146 	 * To avoid a "too big" situation at an intermediate router
2147 	 * and the path MTU discovery process, specify the IPV6_MINMTU flag.
2148 	 * Note that only echo and node information replies are affected,
2149 	 * since the length of ICMP6 errors is limited to the minimum MTU.
2150 	 */
2151 	if (ip6_output(m, NULL, NULL, IPV6_MINMTU,
2152 		(struct ip6_moptions *)NULL, (struct socket *)NULL, &outif) != 0
2153 	    && outif)
2154 		icmp6_ifstat_inc(outif, ifs6_out_error);
2155 
2156 	if (outif)
2157 		icmp6_ifoutstat_inc(outif, type, code);
2158 
2159 	return;
2160 
2161  bad:
2162 	m_freem(m);
2163 	return;
2164 }
2165 
2166 void
2167 icmp6_fasttimo()
2168 {
2169 
2170 	mld6_fasttimeo();
2171 }
2172 
2173 static const char *
2174 icmp6_redirect_diag(src6, dst6, tgt6)
2175 	struct in6_addr *src6;
2176 	struct in6_addr *dst6;
2177 	struct in6_addr *tgt6;
2178 {
2179 	static char buf[1024];
2180 	snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2181 		ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2182 	return buf;
2183 }
2184 
2185 void
2186 icmp6_redirect_input(m, off)
2187 	struct mbuf *m;
2188 	int off;
2189 {
2190 	struct ifnet *ifp = m->m_pkthdr.rcvif;
2191 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2192 	struct nd_redirect *nd_rd;
2193 	int icmp6len = ntohs(ip6->ip6_plen);
2194 	char *lladdr = NULL;
2195 	int lladdrlen = 0;
2196 	struct rtentry *rt = NULL;
2197 	int is_router;
2198 	int is_onlink;
2199 	struct in6_addr src6 = ip6->ip6_src;
2200 	struct in6_addr redtgt6;
2201 	struct in6_addr reddst6;
2202 	union nd_opts ndopts;
2203 
2204 	if (!ifp)
2205 		return;
2206 
2207 	/* XXX if we are router, we don't update route by icmp6 redirect */
2208 	if (ip6_forwarding)
2209 		goto freeit;
2210 	if (!icmp6_rediraccept)
2211 		goto freeit;
2212 
2213 	IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2214 	if (nd_rd == NULL) {
2215 		icmp6stat.icp6s_tooshort++;
2216 		return;
2217 	}
2218 	redtgt6 = nd_rd->nd_rd_target;
2219 	reddst6 = nd_rd->nd_rd_dst;
2220 
2221 	if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) ||
2222 	    in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) {
2223 		goto freeit;
2224 	}
2225 
2226 	/* validation */
2227 	if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2228 		nd6log((LOG_ERR,
2229 			"ICMP6 redirect sent from %s rejected; "
2230 			"must be from linklocal\n", ip6_sprintf(&src6)));
2231 		goto bad;
2232 	}
2233 	if (ip6->ip6_hlim != 255) {
2234 		nd6log((LOG_ERR,
2235 			"ICMP6 redirect sent from %s rejected; "
2236 			"hlim=%d (must be 255)\n",
2237 			ip6_sprintf(&src6), ip6->ip6_hlim));
2238 		goto bad;
2239 	}
2240     {
2241 	/* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2242 	struct sockaddr_in6 sin6;
2243 	struct in6_addr *gw6;
2244 
2245 	bzero(&sin6, sizeof(sin6));
2246 	sin6.sin6_family = AF_INET6;
2247 	sin6.sin6_len = sizeof(struct sockaddr_in6);
2248 	bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2249 	rt = rtalloc1((struct sockaddr *)&sin6, 0);
2250 	if (rt) {
2251 		if (rt->rt_gateway == NULL ||
2252 		    rt->rt_gateway->sa_family != AF_INET6) {
2253 			nd6log((LOG_ERR,
2254 			    "ICMP6 redirect rejected; no route "
2255 			    "with inet6 gateway found for redirect dst: %s\n",
2256 			    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2257 			RTFREE(rt);
2258 			goto bad;
2259 		}
2260 
2261 		gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2262 		if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2263 			nd6log((LOG_ERR,
2264 				"ICMP6 redirect rejected; "
2265 				"not equal to gw-for-src=%s (must be same): "
2266 				"%s\n",
2267 				ip6_sprintf(gw6),
2268 				icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2269 			RTFREE(rt);
2270 			goto bad;
2271 		}
2272 	} else {
2273 		nd6log((LOG_ERR,
2274 			"ICMP6 redirect rejected; "
2275 			"no route found for redirect dst: %s\n",
2276 			icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2277 		goto bad;
2278 	}
2279 	RTFREE(rt);
2280 	rt = NULL;
2281     }
2282 	if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2283 		nd6log((LOG_ERR,
2284 			"ICMP6 redirect rejected; "
2285 			"redirect dst must be unicast: %s\n",
2286 			icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2287 		goto bad;
2288 	}
2289 
2290 	is_router = is_onlink = 0;
2291 	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2292 		is_router = 1;	/* router case */
2293 	if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2294 		is_onlink = 1;	/* on-link destination case */
2295 	if (!is_router && !is_onlink) {
2296 		nd6log((LOG_ERR,
2297 			"ICMP6 redirect rejected; "
2298 			"neither router case nor onlink case: %s\n",
2299 			icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2300 		goto bad;
2301 	}
2302 	/* validation passed */
2303 
2304 	icmp6len -= sizeof(*nd_rd);
2305 	nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2306 	if (nd6_options(&ndopts) < 0) {
2307 		nd6log((LOG_INFO, "icmp6_redirect_input: "
2308 			"invalid ND option, rejected: %s\n",
2309 			icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2310 		/* nd6_options have incremented stats */
2311 		goto freeit;
2312 	}
2313 
2314 	if (ndopts.nd_opts_tgt_lladdr) {
2315 		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2316 		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2317 	}
2318 
2319 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2320 		nd6log((LOG_INFO,
2321 			"icmp6_redirect_input: lladdrlen mismatch for %s "
2322 			"(if %d, icmp6 packet %d): %s\n",
2323 			ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2324 			icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2325 		goto bad;
2326 	}
2327 
2328 	/* RFC 2461 8.3 */
2329 	nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2330 			 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2331 
2332 	if (!is_onlink) {	/* better router case.  perform rtredirect. */
2333 		/* perform rtredirect */
2334 		struct sockaddr_in6 sdst;
2335 		struct sockaddr_in6 sgw;
2336 		struct sockaddr_in6 ssrc;
2337 		unsigned long rtcount;
2338 		struct rtentry *newrt = NULL;
2339 
2340 		/*
2341 		 * do not install redirect route, if the number of entries
2342 		 * is too much (> hiwat).  note that, the node (= host) will
2343 		 * work just fine even if we do not install redirect route
2344 		 * (there will be additional hops, though).
2345 		 */
2346 		rtcount = rt_timer_count(icmp6_redirect_timeout_q);
2347 		if (0 <= icmp6_redirect_hiwat && rtcount > icmp6_redirect_hiwat)
2348 			return;
2349 		else if (0 <= icmp6_redirect_lowat &&
2350 		    rtcount > icmp6_redirect_lowat) {
2351 			/*
2352 			 * XXX nuke a victim, install the new one.
2353 			 */
2354 		}
2355 
2356 		bzero(&sdst, sizeof(sdst));
2357 		bzero(&sgw, sizeof(sgw));
2358 		bzero(&ssrc, sizeof(ssrc));
2359 		sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2360 		sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2361 			sizeof(struct sockaddr_in6);
2362 		bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2363 		bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2364 		bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2365 		rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2366 			   (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2367 			   (struct sockaddr *)&ssrc,
2368 			   &newrt);
2369 
2370 		if (newrt) {
2371 			(void)rt_timer_add(newrt, icmp6_redirect_timeout,
2372 			    icmp6_redirect_timeout_q);
2373 			rtfree(newrt);
2374 		}
2375 	}
2376 	/* finally update cached route in each socket via pfctlinput */
2377 	{
2378 		struct sockaddr_in6 sdst;
2379 
2380 		bzero(&sdst, sizeof(sdst));
2381 		sdst.sin6_family = AF_INET6;
2382 		sdst.sin6_len = sizeof(struct sockaddr_in6);
2383 		bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2384 		pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2385 #ifdef IPSEC
2386 		key_sa_routechange((struct sockaddr *)&sdst);
2387 #endif
2388 	}
2389 
2390  freeit:
2391 	m_freem(m);
2392 	return;
2393 
2394  bad:
2395 	icmp6stat.icp6s_badredirect++;
2396 	m_freem(m);
2397 }
2398 
2399 void
2400 icmp6_redirect_output(m0, rt)
2401 	struct mbuf *m0;
2402 	struct rtentry *rt;
2403 {
2404 	struct ifnet *ifp;	/* my outgoing interface */
2405 	struct in6_addr *ifp_ll6;
2406 	struct in6_addr *nexthop;
2407 	struct ip6_hdr *sip6;	/* m0 as struct ip6_hdr */
2408 	struct mbuf *m = NULL;	/* newly allocated one */
2409 	struct ip6_hdr *ip6;	/* m as struct ip6_hdr */
2410 	struct nd_redirect *nd_rd;
2411 	size_t maxlen;
2412 	u_char *p;
2413 	struct sockaddr_in6 src_sa;
2414 
2415 	icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2416 
2417 	/* if we are not router, we don't send icmp6 redirect */
2418 	if (!ip6_forwarding)
2419 		goto fail;
2420 
2421 	/* sanity check */
2422 	if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2423 		goto fail;
2424 
2425 	/*
2426 	 * Address check:
2427 	 *  the source address must identify a neighbor, and
2428 	 *  the destination address must not be a multicast address
2429 	 *  [RFC 2461, sec 8.2]
2430 	 */
2431 	sip6 = mtod(m0, struct ip6_hdr *);
2432 	bzero(&src_sa, sizeof(src_sa));
2433 	src_sa.sin6_family = AF_INET6;
2434 	src_sa.sin6_len = sizeof(src_sa);
2435 	src_sa.sin6_addr = sip6->ip6_src;
2436 	if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2437 		goto fail;
2438 	if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2439 		goto fail;	/* what should we do here? */
2440 
2441 	/* rate limit */
2442 	if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2443 		goto fail;
2444 
2445 	/*
2446 	 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2447 	 * we almost always ask for an mbuf cluster for simplicity.
2448 	 * (MHLEN < IPV6_MMTU is almost always true)
2449 	 */
2450 #if IPV6_MMTU >= MCLBYTES
2451 # error assumption failed about IPV6_MMTU and MCLBYTES
2452 #endif
2453 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
2454 	if (m && IPV6_MMTU >= MHLEN)
2455 		MCLGET(m, M_DONTWAIT);
2456 	if (!m)
2457 		goto fail;
2458 	m->m_pkthdr.rcvif = NULL;
2459 	m->m_len = 0;
2460 	maxlen = M_TRAILINGSPACE(m);
2461 	maxlen = min(IPV6_MMTU, maxlen);
2462 	/* just for safety */
2463 	if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2464 	    ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2465 		goto fail;
2466 	}
2467 
2468 	{
2469 		/* get ip6 linklocal address for ifp(my outgoing interface). */
2470 		struct in6_ifaddr *ia;
2471 		if ((ia = in6ifa_ifpforlinklocal(ifp,
2472 						 IN6_IFF_NOTREADY|
2473 						 IN6_IFF_ANYCAST)) == NULL)
2474 			goto fail;
2475 		ifp_ll6 = &ia->ia_addr.sin6_addr;
2476 	}
2477 
2478 	/* get ip6 linklocal address for the router. */
2479 	if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2480 		struct sockaddr_in6 *sin6;
2481 		sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2482 		nexthop = &sin6->sin6_addr;
2483 		if (!IN6_IS_ADDR_LINKLOCAL(nexthop))
2484 			nexthop = NULL;
2485 	} else
2486 		nexthop = NULL;
2487 
2488 	/* ip6 */
2489 	ip6 = mtod(m, struct ip6_hdr *);
2490 	ip6->ip6_flow = 0;
2491 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2492 	ip6->ip6_vfc |= IPV6_VERSION;
2493 	/* ip6->ip6_plen will be set later */
2494 	ip6->ip6_nxt = IPPROTO_ICMPV6;
2495 	ip6->ip6_hlim = 255;
2496 	/* ip6->ip6_src must be linklocal addr for my outgoing if. */
2497 	bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2498 	bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2499 
2500 	/* ND Redirect */
2501 	nd_rd = (struct nd_redirect *)(ip6 + 1);
2502 	nd_rd->nd_rd_type = ND_REDIRECT;
2503 	nd_rd->nd_rd_code = 0;
2504 	nd_rd->nd_rd_reserved = 0;
2505 	if (rt->rt_flags & RTF_GATEWAY) {
2506 		/*
2507 		 * nd_rd->nd_rd_target must be a link-local address in
2508 		 * better router cases.
2509 		 */
2510 		if (!nexthop)
2511 			goto fail;
2512 		bcopy(nexthop, &nd_rd->nd_rd_target,
2513 		      sizeof(nd_rd->nd_rd_target));
2514 		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2515 		      sizeof(nd_rd->nd_rd_dst));
2516 	} else {
2517 		/* make sure redtgt == reddst */
2518 		nexthop = &sip6->ip6_dst;
2519 		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2520 		      sizeof(nd_rd->nd_rd_target));
2521 		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2522 		      sizeof(nd_rd->nd_rd_dst));
2523 	}
2524 
2525 	p = (u_char *)(nd_rd + 1);
2526 
2527 	{
2528 		/* target lladdr option */
2529 		struct rtentry *rt_nexthop = NULL;
2530 		int len;
2531 		struct sockaddr_dl *sdl;
2532 		struct nd_opt_hdr *nd_opt;
2533 		char *lladdr;
2534 
2535 		rt_nexthop = nd6_lookup(nexthop, 0, ifp);
2536 		if (!rt_nexthop)
2537 			goto nolladdropt;
2538 		len = sizeof(*nd_opt) + ifp->if_addrlen;
2539 		len = (len + 7) & ~7;	/* round by 8 */
2540 		/* safety check */
2541 		if (len + (p - (u_char *)ip6) > maxlen)
2542 			goto nolladdropt;
2543 		if (!(rt_nexthop->rt_flags & RTF_GATEWAY) &&
2544 		    (rt_nexthop->rt_flags & RTF_LLINFO) &&
2545 		    (rt_nexthop->rt_gateway->sa_family == AF_LINK) &&
2546 		    (sdl = (struct sockaddr_dl *)rt_nexthop->rt_gateway) &&
2547 		    sdl->sdl_alen) {
2548 			nd_opt = (struct nd_opt_hdr *)p;
2549 			nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2550 			nd_opt->nd_opt_len = len >> 3;
2551 			lladdr = (char *)(nd_opt + 1);
2552 			bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2553 			p += len;
2554 		}
2555 	}
2556   nolladdropt:;
2557 
2558 	m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2559 
2560 	/* just to be safe */
2561 	if (m0->m_flags & M_DECRYPTED)
2562 		goto noredhdropt;
2563 	if (p - (u_char *)ip6 > maxlen)
2564 		goto noredhdropt;
2565 
2566 	{
2567 		/* redirected header option */
2568 		int len;
2569 		struct nd_opt_rd_hdr *nd_opt_rh;
2570 
2571 		/*
2572 		 * compute the maximum size for icmp6 redirect header option.
2573 		 * XXX room for auth header?
2574 		 */
2575 		len = maxlen - (p - (u_char *)ip6);
2576 		len &= ~7;
2577 
2578 		/*
2579 		 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2580 		 * about padding/truncate rule for the original IP packet.
2581 		 * From the discussion on IPv6imp in Feb 1999,
2582 		 * the consensus was:
2583 		 * - "attach as much as possible" is the goal
2584 		 * - pad if not aligned (original size can be guessed by
2585 		 *   original ip6 header)
2586 		 * Following code adds the padding if it is simple enough,
2587 		 * and truncates if not.
2588 		 */
2589 		if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2590 			/* not enough room, truncate */
2591 			m_adj(m0, (len - sizeof(*nd_opt_rh)) -
2592 			    m0->m_pkthdr.len);
2593 		} else {
2594 			/*
2595                          * enough room, truncate if not aligned.
2596 			 * we don't pad here for simplicity.
2597 			 */
2598 			size_t extra;
2599 
2600 			extra = m0->m_pkthdr.len % 8;
2601 			if (extra) {
2602 				/* truncate */
2603 				m_adj(m0, -extra);
2604 			}
2605 			len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2606 		}
2607 
2608 		nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2609 		bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2610 		nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2611 		nd_opt_rh->nd_opt_rh_len = len >> 3;
2612 		p += sizeof(*nd_opt_rh);
2613 		m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2614 
2615 		/* connect m0 to m */
2616 		m->m_pkthdr.len += m0->m_pkthdr.len;
2617 		m_cat(m, m0);
2618 		m0 = NULL;
2619 	}
2620 noredhdropt:
2621 	if (m0) {
2622 		m_freem(m0);
2623 		m0 = NULL;
2624 	}
2625 
2626 	sip6 = mtod(m, struct ip6_hdr *);
2627 	/* XXX: clear embedded link IDs in the inner header */
2628 	in6_clearscope(&sip6->ip6_src);
2629 	in6_clearscope(&sip6->ip6_dst);
2630 	in6_clearscope(&nd_rd->nd_rd_target);
2631 	in6_clearscope(&nd_rd->nd_rd_dst);
2632 
2633 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2634 
2635 	nd_rd->nd_rd_cksum = 0;
2636 	nd_rd->nd_rd_cksum
2637 		= in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
2638 
2639 	/* send the packet to outside... */
2640 	if (ip6_output(m, NULL, NULL, 0,
2641 		(struct ip6_moptions *)NULL, (struct socket *)NULL, NULL) != 0)
2642 		icmp6_ifstat_inc(ifp, ifs6_out_error);
2643 
2644 	icmp6_ifstat_inc(ifp, ifs6_out_msg);
2645 	icmp6_ifstat_inc(ifp, ifs6_out_redirect);
2646 	icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2647 
2648 	return;
2649 
2650 fail:
2651 	if (m)
2652 		m_freem(m);
2653 	if (m0)
2654 		m_freem(m0);
2655 }
2656 
2657 /*
2658  * ICMPv6 socket option processing.
2659  */
2660 int
2661 icmp6_ctloutput(op, so, level, optname, mp)
2662 	int op;
2663 	struct socket *so;
2664 	int level, optname;
2665 	struct mbuf **mp;
2666 {
2667 	int error = 0;
2668 	int optlen;
2669 	struct in6pcb *in6p = sotoin6pcb(so);
2670 	struct mbuf *m = *mp;
2671 
2672 	optlen = m ? m->m_len : 0;
2673 
2674 	if (level != IPPROTO_ICMPV6) {
2675 		if (op == PRCO_SETOPT && m)
2676 			(void)m_free(m);
2677 		return EINVAL;
2678 	}
2679 
2680 	switch (op) {
2681 	case PRCO_SETOPT:
2682 		switch (optname) {
2683 		case ICMP6_FILTER:
2684 		    {
2685 			struct icmp6_filter *p;
2686 
2687 			if (optlen != sizeof(*p)) {
2688 				error = EMSGSIZE;
2689 				break;
2690 			}
2691 			p = mtod(m, struct icmp6_filter *);
2692 			if (!p || !in6p->in6p_icmp6filt) {
2693 				error = EINVAL;
2694 				break;
2695 			}
2696 			bcopy(p, in6p->in6p_icmp6filt,
2697 				sizeof(struct icmp6_filter));
2698 			error = 0;
2699 			break;
2700 		    }
2701 
2702 		default:
2703 			error = ENOPROTOOPT;
2704 			break;
2705 		}
2706 		if (m)
2707 			(void)m_freem(m);
2708 		break;
2709 
2710 	case PRCO_GETOPT:
2711 		switch (optname) {
2712 		case ICMP6_FILTER:
2713 		    {
2714 			struct icmp6_filter *p;
2715 
2716 			if (!in6p->in6p_icmp6filt) {
2717 				error = EINVAL;
2718 				break;
2719 			}
2720 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
2721 			m->m_len = sizeof(struct icmp6_filter);
2722 			p = mtod(m, struct icmp6_filter *);
2723 			bcopy(in6p->in6p_icmp6filt, p,
2724 				sizeof(struct icmp6_filter));
2725 			error = 0;
2726 			break;
2727 		    }
2728 
2729 		default:
2730 			error = ENOPROTOOPT;
2731 			break;
2732 		}
2733 		break;
2734 	}
2735 
2736 	return (error);
2737 }
2738 
2739 /*
2740  * Perform rate limit check.
2741  * Returns 0 if it is okay to send the icmp6 packet.
2742  * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2743  * limitation.
2744  *
2745  * XXX per-destination/type check necessary?
2746  */
2747 static int
2748 icmp6_ratelimit(dst, type, code)
2749 	const struct in6_addr *dst;	/* not used at this moment */
2750 	const int type;			/* not used at this moment */
2751 	const int code;			/* not used at this moment */
2752 {
2753 	int ret;
2754 
2755 	ret = 0;	/* okay to send */
2756 
2757 	/* PPS limit */
2758 	if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2759 	    icmp6errppslim)) {
2760 		/* The packet is subject to rate limit */
2761 		ret++;
2762 	}
2763 
2764 	return ret;
2765 }
2766 
2767 static struct rtentry *
2768 icmp6_mtudisc_clone(dst)
2769 	struct sockaddr *dst;
2770 {
2771 	struct rtentry *rt;
2772 	int    error;
2773 
2774 	rt = rtalloc1(dst, 1);
2775 	if (rt == 0)
2776 		return NULL;
2777 
2778 	/* If we didn't get a host route, allocate one */
2779 	if ((rt->rt_flags & RTF_HOST) == 0) {
2780 		struct rtentry *nrt;
2781 
2782 		error = rtrequest((int) RTM_ADD, dst,
2783 		    (struct sockaddr *) rt->rt_gateway,
2784 		    (struct sockaddr *) 0,
2785 		    RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
2786 		if (error) {
2787 			rtfree(rt);
2788 			return NULL;
2789 		}
2790 		nrt->rt_rmx = rt->rt_rmx;
2791 		rtfree(rt);
2792 		rt = nrt;
2793 	}
2794 	error = rt_timer_add(rt, icmp6_mtudisc_timeout,
2795 			icmp6_mtudisc_timeout_q);
2796 	if (error) {
2797 		rtfree(rt);
2798 		return NULL;
2799 	}
2800 
2801 	return rt;	/* caller need to call rtfree() */
2802 }
2803 
2804 static void
2805 icmp6_mtudisc_timeout(rt, r)
2806 	struct rtentry *rt;
2807 	struct rttimer *r;
2808 {
2809 	if (rt == NULL)
2810 		panic("icmp6_mtudisc_timeout: bad route to timeout");
2811 	if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
2812 	    (RTF_DYNAMIC | RTF_HOST)) {
2813 		rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
2814 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
2815 	} else {
2816 		if (!(rt->rt_rmx.rmx_locks & RTV_MTU))
2817 			rt->rt_rmx.rmx_mtu = 0;
2818 	}
2819 }
2820 
2821 static void
2822 icmp6_redirect_timeout(rt, r)
2823 	struct rtentry *rt;
2824 	struct rttimer *r;
2825 {
2826 	if (rt == NULL)
2827 		panic("icmp6_redirect_timeout: bad route to timeout");
2828 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) ==
2829 	    (RTF_GATEWAY | RTF_DYNAMIC | RTF_HOST)) {
2830 		rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
2831 		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
2832 	}
2833 }
2834 
2835 /*
2836  * sysctl helper routine for the net.inet6.icmp6.nd6 nodes.  silly?
2837  */
2838 static int
2839 sysctl_net_inet6_icmp6_nd6(SYSCTLFN_ARGS)
2840 {
2841 
2842 	if (namelen != 0)
2843 		return (EINVAL);
2844 
2845 	return (nd6_sysctl(rnode->sysctl_num, oldp, oldlenp,
2846 	    /*XXXUNCONST*/
2847 	    __UNCONST(newp), newlen));
2848 }
2849 
2850 SYSCTL_SETUP(sysctl_net_inet6_icmp6_setup,
2851 	     "sysctl net.inet6.icmp6 subtree setup")
2852 {
2853 
2854 	sysctl_createv(clog, 0, NULL, NULL,
2855 		       CTLFLAG_PERMANENT,
2856 		       CTLTYPE_NODE, "net", NULL,
2857 		       NULL, 0, NULL, 0,
2858 		       CTL_NET, CTL_EOL);
2859 	sysctl_createv(clog, 0, NULL, NULL,
2860 		       CTLFLAG_PERMANENT,
2861 		       CTLTYPE_NODE, "inet6", NULL,
2862 		       NULL, 0, NULL, 0,
2863 		       CTL_NET, PF_INET6, CTL_EOL);
2864 	sysctl_createv(clog, 0, NULL, NULL,
2865 		       CTLFLAG_PERMANENT,
2866 		       CTLTYPE_NODE, "icmp6",
2867 		       SYSCTL_DESCR("ICMPv6 related settings"),
2868 		       NULL, 0, NULL, 0,
2869 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6, CTL_EOL);
2870 
2871 	sysctl_createv(clog, 0, NULL, NULL,
2872 		       CTLFLAG_PERMANENT,
2873 		       CTLTYPE_STRUCT, "stats",
2874 		       SYSCTL_DESCR("ICMPv6 transmission statistics"),
2875 		       NULL, 0, &icmp6stat, sizeof(icmp6stat),
2876 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2877 		       ICMPV6CTL_STATS, CTL_EOL);
2878 	sysctl_createv(clog, 0, NULL, NULL,
2879 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2880 		       CTLTYPE_INT, "rediraccept",
2881 		       SYSCTL_DESCR("Accept and process redirect messages"),
2882 		       NULL, 0, &icmp6_rediraccept, 0,
2883 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2884 		       ICMPV6CTL_REDIRACCEPT, CTL_EOL);
2885 	sysctl_createv(clog, 0, NULL, NULL,
2886 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2887 		       CTLTYPE_INT, "redirtimeout",
2888 		       SYSCTL_DESCR("Redirect generated route lifetime"),
2889 		       NULL, 0, &icmp6_redirtimeout, 0,
2890 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2891 		       ICMPV6CTL_REDIRTIMEOUT, CTL_EOL);
2892 #if 0 /* obsoleted */
2893 	sysctl_createv(clog, 0, NULL, NULL,
2894 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2895 		       CTLTYPE_INT, "errratelimit", NULL,
2896 		       NULL, 0, &icmp6_errratelimit, 0,
2897 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2898 		       ICMPV6CTL_ERRRATELIMIT, CTL_EOL);
2899 #endif
2900 	sysctl_createv(clog, 0, NULL, NULL,
2901 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2902 		       CTLTYPE_INT, "nd6_prune",
2903 		       SYSCTL_DESCR("Neighbor discovery prune interval"),
2904 		       NULL, 0, &nd6_prune, 0,
2905 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2906 		       ICMPV6CTL_ND6_PRUNE, CTL_EOL);
2907 	sysctl_createv(clog, 0, NULL, NULL,
2908 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2909 		       CTLTYPE_INT, "nd6_delay",
2910 		       SYSCTL_DESCR("First probe delay time"),
2911 		       NULL, 0, &nd6_delay, 0,
2912 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2913 		       ICMPV6CTL_ND6_DELAY, CTL_EOL);
2914 	sysctl_createv(clog, 0, NULL, NULL,
2915 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2916 		       CTLTYPE_INT, "nd6_umaxtries",
2917 		       SYSCTL_DESCR("Number of unicast discovery attempts"),
2918 		       NULL, 0, &nd6_umaxtries, 0,
2919 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2920 		       ICMPV6CTL_ND6_UMAXTRIES, CTL_EOL);
2921 	sysctl_createv(clog, 0, NULL, NULL,
2922 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2923 		       CTLTYPE_INT, "nd6_mmaxtries",
2924 		       SYSCTL_DESCR("Number of multicast discovery attempts"),
2925 		       NULL, 0, &nd6_mmaxtries, 0,
2926 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2927 		       ICMPV6CTL_ND6_MMAXTRIES, CTL_EOL);
2928 	sysctl_createv(clog, 0, NULL, NULL,
2929 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2930 		       CTLTYPE_INT, "nd6_useloopback",
2931 		       SYSCTL_DESCR("Use loopback interface for local traffic"),
2932 		       NULL, 0, &nd6_useloopback, 0,
2933 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2934 		       ICMPV6CTL_ND6_USELOOPBACK, CTL_EOL);
2935 #if 0 /* obsoleted */
2936 	sysctl_createv(clog, 0, NULL, NULL,
2937 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2938 		       CTLTYPE_INT, "nd6_proxyall", NULL,
2939 		       NULL, 0, &nd6_proxyall, 0,
2940 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2941 		       ICMPV6CTL_ND6_PROXYALL, CTL_EOL);
2942 #endif
2943 	sysctl_createv(clog, 0, NULL, NULL,
2944 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2945 		       CTLTYPE_INT, "nodeinfo",
2946 		       SYSCTL_DESCR("Respond to node information requests"),
2947 		       NULL, 0, &icmp6_nodeinfo, 0,
2948 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2949 		       ICMPV6CTL_NODEINFO, CTL_EOL);
2950 	sysctl_createv(clog, 0, NULL, NULL,
2951 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2952 		       CTLTYPE_INT, "errppslimit",
2953 		       SYSCTL_DESCR("Maximum ICMP errors sent per second"),
2954 		       NULL, 0, &icmp6errppslim, 0,
2955 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2956 		       ICMPV6CTL_ERRPPSLIMIT, CTL_EOL);
2957 	sysctl_createv(clog, 0, NULL, NULL,
2958 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2959 		       CTLTYPE_INT, "nd6_maxnudhint",
2960 		       SYSCTL_DESCR("Maximum neighbor unreachable hint count"),
2961 		       NULL, 0, &nd6_maxnudhint, 0,
2962 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2963 		       ICMPV6CTL_ND6_MAXNUDHINT, CTL_EOL);
2964 	sysctl_createv(clog, 0, NULL, NULL,
2965 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2966 		       CTLTYPE_INT, "mtudisc_hiwat",
2967 		       SYSCTL_DESCR("Low mark on MTU Discovery route timers"),
2968 		       NULL, 0, &icmp6_mtudisc_hiwat, 0,
2969 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2970 		       ICMPV6CTL_MTUDISC_HIWAT, CTL_EOL);
2971 	sysctl_createv(clog, 0, NULL, NULL,
2972 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2973 		       CTLTYPE_INT, "mtudisc_lowat",
2974 		       SYSCTL_DESCR("Low mark on MTU Discovery route timers"),
2975 		       NULL, 0, &icmp6_mtudisc_lowat, 0,
2976 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2977 		       ICMPV6CTL_MTUDISC_LOWAT, CTL_EOL);
2978 	sysctl_createv(clog, 0, NULL, NULL,
2979 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2980 		       CTLTYPE_INT, "nd6_debug",
2981 		       SYSCTL_DESCR("Enable neighbor discovery debug output"),
2982 		       NULL, 0, &nd6_debug, 0,
2983 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2984 		       ICMPV6CTL_ND6_DEBUG, CTL_EOL);
2985 	sysctl_createv(clog, 0, NULL, NULL,
2986 		       CTLFLAG_PERMANENT,
2987 		       CTLTYPE_STRUCT, "nd6_drlist",
2988 		       SYSCTL_DESCR("Default router list"),
2989 		       sysctl_net_inet6_icmp6_nd6, 0, NULL, 0,
2990 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2991 		       ICMPV6CTL_ND6_DRLIST, CTL_EOL);
2992 	sysctl_createv(clog, 0, NULL, NULL,
2993 		       CTLFLAG_PERMANENT,
2994 		       CTLTYPE_STRUCT, "nd6_prlist",
2995 		       SYSCTL_DESCR("Prefix list"),
2996 		       sysctl_net_inet6_icmp6_nd6, 0, NULL, 0,
2997 		       CTL_NET, PF_INET6, IPPROTO_ICMPV6,
2998 		       ICMPV6CTL_ND6_PRLIST, CTL_EOL);
2999 }
3000