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