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