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