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