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