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