xref: /freebsd/sys/netinet6/icmp6.c (revision bd18fd57db1df29da1a3adf94d47924a977a29c2)
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 & 5) != 5)
671 				break;
672 
673 			if (code != 0)
674 				goto badcode;
675 
676 			CTASSERT(sizeof(*nip6) + sizeof(*nicmp6) + 4 <= MHLEN);
677 			n = m_gethdr(M_NOWAIT, m->m_type);
678 			if (n == NULL) {
679 				/* Give up remote */
680 				break;
681 			}
682 			if (!m_dup_pkthdr(n, m, M_NOWAIT)) {
683 				/*
684 				 * Previous code did a blind M_COPY_PKTHDR
685 				 * and said "just for rcvif".  If true, then
686 				 * we could tolerate the dup failing (due to
687 				 * the deep copy of the tag chain).  For now
688 				 * be conservative and just fail.
689 				 */
690 				m_free(n);
691 				n = NULL;
692 			}
693 			maxhlen = M_TRAILINGSPACE(n) -
694 			    (sizeof(*nip6) + sizeof(*nicmp6) + 4);
695 			pr = curthread->td_ucred->cr_prison;
696 			mtx_lock(&pr->pr_mtx);
697 			hlen = strlen(pr->pr_hostname);
698 			if (maxhlen > hlen)
699 				maxhlen = hlen;
700 			/*
701 			 * Copy IPv6 and ICMPv6 only.
702 			 */
703 			nip6 = mtod(n, struct ip6_hdr *);
704 			bcopy(ip6, nip6, sizeof(struct ip6_hdr));
705 			nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
706 			bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
707 			p = (u_char *)(nicmp6 + 1);
708 			bzero(p, 4);
709 			/* meaningless TTL */
710 			bcopy(pr->pr_hostname, p + 4, maxhlen);
711 			mtx_unlock(&pr->pr_mtx);
712 			noff = sizeof(struct ip6_hdr);
713 			n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
714 				sizeof(struct icmp6_hdr) + 4 + maxhlen;
715 			nicmp6->icmp6_type = ICMP6_WRUREPLY;
716 			nicmp6->icmp6_code = 0;
717 		}
718 		if (n) {
719 			ICMP6STAT_INC(icp6s_reflect);
720 			ICMP6STAT_INC(icp6s_outhist[ICMP6_WRUREPLY]);
721 			icmp6_reflect(n, noff);
722 		}
723 		break;
724 	    }
725 
726 	case ICMP6_WRUREPLY:
727 		if (code != 0)
728 			goto badcode;
729 		break;
730 
731 	case ND_ROUTER_SOLICIT:
732 		icmp6_ifstat_inc(ifp, ifs6_in_routersolicit);
733 		if (code != 0)
734 			goto badcode;
735 		if (icmp6len < sizeof(struct nd_router_solicit))
736 			goto badlen;
737 		if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
738 			/* give up local */
739 
740 			/* Send incoming SeND packet to user space. */
741 			if (send_sendso_input_hook != NULL) {
742 				IP6_EXTHDR_CHECK(m, off,
743 				    icmp6len, IPPROTO_DONE);
744 				error = send_sendso_input_hook(m, ifp,
745 				    SND_IN, ip6len);
746 				/* -1 == no app on SEND socket */
747 				if (error == 0)
748 					return (IPPROTO_DONE);
749 				nd6_rs_input(m, off, icmp6len);
750 			} else
751 				nd6_rs_input(m, off, icmp6len);
752 			m = NULL;
753 			goto freeit;
754 		}
755 		if (send_sendso_input_hook != NULL) {
756 			IP6_EXTHDR_CHECK(n, off,
757 			    icmp6len, IPPROTO_DONE);
758                         error = send_sendso_input_hook(n, ifp,
759 			    SND_IN, ip6len);
760 			if (error == 0)
761 				goto freeit;
762 			/* -1 == no app on SEND socket */
763 			nd6_rs_input(n, off, icmp6len);
764 		} else
765 			nd6_rs_input(n, off, icmp6len);
766 		/* m stays. */
767 		break;
768 
769 	case ND_ROUTER_ADVERT:
770 		icmp6_ifstat_inc(ifp, ifs6_in_routeradvert);
771 		if (code != 0)
772 			goto badcode;
773 		if (icmp6len < sizeof(struct nd_router_advert))
774 			goto badlen;
775 		if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
776 
777 			/* Send incoming SeND-protected/ND packet to user space. */
778 			if (send_sendso_input_hook != NULL) {
779 				error = send_sendso_input_hook(m, ifp,
780 				    SND_IN, ip6len);
781 				if (error == 0)
782 					return (IPPROTO_DONE);
783 				nd6_ra_input(m, off, icmp6len);
784 			} else
785 				nd6_ra_input(m, off, icmp6len);
786 			m = NULL;
787 			goto freeit;
788 		}
789 		if (send_sendso_input_hook != NULL) {
790 			error = send_sendso_input_hook(n, ifp,
791 			    SND_IN, ip6len);
792 			if (error == 0)
793 				goto freeit;
794 			nd6_ra_input(n, off, icmp6len);
795 		} else
796 			nd6_ra_input(n, off, icmp6len);
797 		/* m stays. */
798 		break;
799 
800 	case ND_NEIGHBOR_SOLICIT:
801 		icmp6_ifstat_inc(ifp, ifs6_in_neighborsolicit);
802 		if (code != 0)
803 			goto badcode;
804 		if (icmp6len < sizeof(struct nd_neighbor_solicit))
805 			goto badlen;
806 		if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
807 			if (send_sendso_input_hook != NULL) {
808 				error = send_sendso_input_hook(m, ifp,
809 				    SND_IN, ip6len);
810 				if (error == 0)
811 					return (IPPROTO_DONE);
812 				nd6_ns_input(m, off, icmp6len);
813 			} else
814 				nd6_ns_input(m, off, icmp6len);
815 			m = NULL;
816 			goto freeit;
817 		}
818 		if (send_sendso_input_hook != NULL) {
819 			error = send_sendso_input_hook(n, ifp,
820 			    SND_IN, ip6len);
821 			if (error == 0)
822 				goto freeit;
823 			nd6_ns_input(n, off, icmp6len);
824 		} else
825 			nd6_ns_input(n, off, icmp6len);
826 		/* m stays. */
827 		break;
828 
829 	case ND_NEIGHBOR_ADVERT:
830 		icmp6_ifstat_inc(ifp, ifs6_in_neighboradvert);
831 		if (code != 0)
832 			goto badcode;
833 		if (icmp6len < sizeof(struct nd_neighbor_advert))
834 			goto badlen;
835 		if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
836 
837 			/* Send incoming SeND-protected/ND packet to user space. */
838 			if (send_sendso_input_hook != NULL) {
839 				error = send_sendso_input_hook(m, ifp,
840 				    SND_IN, ip6len);
841 				if (error == 0)
842 					return (IPPROTO_DONE);
843 				nd6_na_input(m, off, icmp6len);
844 			} else
845 				nd6_na_input(m, off, icmp6len);
846 			m = NULL;
847 			goto freeit;
848 		}
849 		if (send_sendso_input_hook != NULL) {
850 			error = send_sendso_input_hook(n, ifp,
851 			    SND_IN, ip6len);
852 			if (error == 0)
853 				goto freeit;
854 			nd6_na_input(n, off, icmp6len);
855 		} else
856 			nd6_na_input(n, off, icmp6len);
857 		/* m stays. */
858 		break;
859 
860 	case ND_REDIRECT:
861 		icmp6_ifstat_inc(ifp, ifs6_in_redirect);
862 		if (code != 0)
863 			goto badcode;
864 		if (icmp6len < sizeof(struct nd_redirect))
865 			goto badlen;
866 		if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) {
867 			if (send_sendso_input_hook != NULL) {
868 				error = send_sendso_input_hook(m, ifp,
869 				    SND_IN, ip6len);
870 		 		if (error == 0)
871 					return (IPPROTO_DONE);
872 			    icmp6_redirect_input(m, off);
873 			} else
874 				icmp6_redirect_input(m, off);
875 			m = NULL;
876 			goto freeit;
877 		}
878 		if (send_sendso_input_hook != NULL) {
879 			error = send_sendso_input_hook(n, ifp,
880 			    SND_IN, ip6len);
881 			if (error == 0)
882 				goto freeit;
883 			icmp6_redirect_input(n, off);
884 		} else
885 			icmp6_redirect_input(n, off);
886 		/* m stays. */
887 		break;
888 
889 	case ICMP6_ROUTER_RENUMBERING:
890 		if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
891 		    code != ICMP6_ROUTER_RENUMBERING_RESULT)
892 			goto badcode;
893 		if (icmp6len < sizeof(struct icmp6_router_renum))
894 			goto badlen;
895 		break;
896 
897 	default:
898 		nd6log((LOG_DEBUG,
899 		    "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
900 		    icmp6->icmp6_type, ip6_sprintf(ip6bufs, &ip6->ip6_src),
901 		    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
902 		    ifp ? ifp->if_index : 0));
903 		if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
904 			/* ICMPv6 error: MUST deliver it by spec... */
905 			code = PRC_NCMDS;
906 			/* deliver */
907 		} else {
908 			/* ICMPv6 informational: MUST not deliver */
909 			break;
910 		}
911 	deliver:
912 		if (icmp6_notify_error(&m, off, icmp6len, code) != 0) {
913 			/* In this case, m should've been freed. */
914 			return (IPPROTO_DONE);
915 		}
916 		break;
917 
918 	badcode:
919 		ICMP6STAT_INC(icp6s_badcode);
920 		break;
921 
922 	badlen:
923 		ICMP6STAT_INC(icp6s_badlen);
924 		break;
925 	}
926 
927 	/* deliver the packet to appropriate sockets */
928 	icmp6_rip6_input(&m, *offp);
929 
930 	return IPPROTO_DONE;
931 
932  freeit:
933 	m_freem(m);
934 	return IPPROTO_DONE;
935 }
936 
937 static int
938 icmp6_notify_error(struct mbuf **mp, int off, int icmp6len, int code)
939 {
940 	struct mbuf *m = *mp;
941 	struct icmp6_hdr *icmp6;
942 	struct ip6_hdr *eip6;
943 	u_int32_t notifymtu;
944 	struct sockaddr_in6 icmp6src, icmp6dst;
945 
946 	if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
947 		ICMP6STAT_INC(icp6s_tooshort);
948 		goto freeit;
949 	}
950 #ifndef PULLDOWN_TEST
951 	IP6_EXTHDR_CHECK(m, off,
952 	    sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1);
953 	icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
954 #else
955 	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
956 	    sizeof(*icmp6) + sizeof(struct ip6_hdr));
957 	if (icmp6 == NULL) {
958 		ICMP6STAT_INC(icp6s_tooshort);
959 		return (-1);
960 	}
961 #endif
962 	eip6 = (struct ip6_hdr *)(icmp6 + 1);
963 
964 	/* Detect the upper level protocol */
965 	{
966 		void (*ctlfunc)(int, struct sockaddr *, void *);
967 		u_int8_t nxt = eip6->ip6_nxt;
968 		int eoff = off + sizeof(struct icmp6_hdr) +
969 		    sizeof(struct ip6_hdr);
970 		struct ip6ctlparam ip6cp;
971 		struct in6_addr *finaldst = NULL;
972 		int icmp6type = icmp6->icmp6_type;
973 		struct ip6_frag *fh;
974 		struct ip6_rthdr *rth;
975 		struct ip6_rthdr0 *rth0;
976 		int rthlen;
977 
978 		while (1) { /* XXX: should avoid infinite loop explicitly? */
979 			struct ip6_ext *eh;
980 
981 			switch (nxt) {
982 			case IPPROTO_HOPOPTS:
983 			case IPPROTO_DSTOPTS:
984 			case IPPROTO_AH:
985 #ifndef PULLDOWN_TEST
986 				IP6_EXTHDR_CHECK(m, 0,
987 				    eoff + sizeof(struct ip6_ext), -1);
988 				eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff);
989 #else
990 				IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
991 				    eoff, sizeof(*eh));
992 				if (eh == NULL) {
993 					ICMP6STAT_INC(icp6s_tooshort);
994 					return (-1);
995 				}
996 #endif
997 
998 				if (nxt == IPPROTO_AH)
999 					eoff += (eh->ip6e_len + 2) << 2;
1000 				else
1001 					eoff += (eh->ip6e_len + 1) << 3;
1002 				nxt = eh->ip6e_nxt;
1003 				break;
1004 			case IPPROTO_ROUTING:
1005 				/*
1006 				 * When the erroneous packet contains a
1007 				 * routing header, we should examine the
1008 				 * header to determine the final destination.
1009 				 * Otherwise, we can't properly update
1010 				 * information that depends on the final
1011 				 * destination (e.g. path MTU).
1012 				 */
1013 #ifndef PULLDOWN_TEST
1014 				IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1);
1015 				rth = (struct ip6_rthdr *)
1016 				    (mtod(m, caddr_t) + eoff);
1017 #else
1018 				IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
1019 				    eoff, sizeof(*rth));
1020 				if (rth == NULL) {
1021 					ICMP6STAT_INC(icp6s_tooshort);
1022 					return (-1);
1023 				}
1024 #endif
1025 				rthlen = (rth->ip6r_len + 1) << 3;
1026 				/*
1027 				 * XXX: currently there is no
1028 				 * officially defined type other
1029 				 * than type-0.
1030 				 * Note that if the segment left field
1031 				 * is 0, all intermediate hops must
1032 				 * have been passed.
1033 				 */
1034 				if (rth->ip6r_segleft &&
1035 				    rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
1036 					int hops;
1037 
1038 #ifndef PULLDOWN_TEST
1039 					IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1);
1040 					rth0 = (struct ip6_rthdr0 *)
1041 					    (mtod(m, caddr_t) + eoff);
1042 #else
1043 					IP6_EXTHDR_GET(rth0,
1044 					    struct ip6_rthdr0 *, m,
1045 					    eoff, rthlen);
1046 					if (rth0 == NULL) {
1047 						ICMP6STAT_INC(icp6s_tooshort);
1048 						return (-1);
1049 					}
1050 #endif
1051 					/* just ignore a bogus header */
1052 					if ((rth0->ip6r0_len % 2) == 0 &&
1053 					    (hops = rth0->ip6r0_len/2))
1054 						finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
1055 				}
1056 				eoff += rthlen;
1057 				nxt = rth->ip6r_nxt;
1058 				break;
1059 			case IPPROTO_FRAGMENT:
1060 #ifndef PULLDOWN_TEST
1061 				IP6_EXTHDR_CHECK(m, 0, eoff +
1062 				    sizeof(struct ip6_frag), -1);
1063 				fh = (struct ip6_frag *)(mtod(m, caddr_t) +
1064 				    eoff);
1065 #else
1066 				IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
1067 				    eoff, sizeof(*fh));
1068 				if (fh == NULL) {
1069 					ICMP6STAT_INC(icp6s_tooshort);
1070 					return (-1);
1071 				}
1072 #endif
1073 				/*
1074 				 * Data after a fragment header is meaningless
1075 				 * unless it is the first fragment, but
1076 				 * we'll go to the notify label for path MTU
1077 				 * discovery.
1078 				 */
1079 				if (fh->ip6f_offlg & IP6F_OFF_MASK)
1080 					goto notify;
1081 
1082 				eoff += sizeof(struct ip6_frag);
1083 				nxt = fh->ip6f_nxt;
1084 				break;
1085 			default:
1086 				/*
1087 				 * This case includes ESP and the No Next
1088 				 * Header.  In such cases going to the notify
1089 				 * label does not have any meaning
1090 				 * (i.e. ctlfunc will be NULL), but we go
1091 				 * anyway since we might have to update
1092 				 * path MTU information.
1093 				 */
1094 				goto notify;
1095 			}
1096 		}
1097 	  notify:
1098 #ifndef PULLDOWN_TEST
1099 		icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1100 #else
1101 		IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1102 		    sizeof(*icmp6) + sizeof(struct ip6_hdr));
1103 		if (icmp6 == NULL) {
1104 			ICMP6STAT_INC(icp6s_tooshort);
1105 			return (-1);
1106 		}
1107 #endif
1108 
1109 		/*
1110 		 * retrieve parameters from the inner IPv6 header, and convert
1111 		 * them into sockaddr structures.
1112 		 * XXX: there is no guarantee that the source or destination
1113 		 * addresses of the inner packet are in the same scope as
1114 		 * the addresses of the icmp packet.  But there is no other
1115 		 * way to determine the zone.
1116 		 */
1117 		eip6 = (struct ip6_hdr *)(icmp6 + 1);
1118 
1119 		bzero(&icmp6dst, sizeof(icmp6dst));
1120 		icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1121 		icmp6dst.sin6_family = AF_INET6;
1122 		if (finaldst == NULL)
1123 			icmp6dst.sin6_addr = eip6->ip6_dst;
1124 		else
1125 			icmp6dst.sin6_addr = *finaldst;
1126 		if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL))
1127 			goto freeit;
1128 		bzero(&icmp6src, sizeof(icmp6src));
1129 		icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1130 		icmp6src.sin6_family = AF_INET6;
1131 		icmp6src.sin6_addr = eip6->ip6_src;
1132 		if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL))
1133 			goto freeit;
1134 		icmp6src.sin6_flowinfo =
1135 		    (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1136 
1137 		if (finaldst == NULL)
1138 			finaldst = &eip6->ip6_dst;
1139 		ip6cp.ip6c_m = m;
1140 		ip6cp.ip6c_icmp6 = icmp6;
1141 		ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1142 		ip6cp.ip6c_off = eoff;
1143 		ip6cp.ip6c_finaldst = finaldst;
1144 		ip6cp.ip6c_src = &icmp6src;
1145 		ip6cp.ip6c_nxt = nxt;
1146 
1147 		if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1148 			notifymtu = ntohl(icmp6->icmp6_mtu);
1149 			ip6cp.ip6c_cmdarg = (void *)&notifymtu;
1150 			icmp6_mtudisc_update(&ip6cp, 1);	/*XXX*/
1151 		}
1152 
1153 		ctlfunc = (void (*)(int, struct sockaddr *, void *))
1154 		    (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1155 		if (ctlfunc) {
1156 			(void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1157 			    &ip6cp);
1158 		}
1159 	}
1160 	*mp = m;
1161 	return (0);
1162 
1163   freeit:
1164 	m_freem(m);
1165 	return (-1);
1166 }
1167 
1168 void
1169 icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated)
1170 {
1171 	struct in6_addr *dst = ip6cp->ip6c_finaldst;
1172 	struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1173 	struct mbuf *m = ip6cp->ip6c_m;	/* will be necessary for scope issue */
1174 	u_int mtu = ntohl(icmp6->icmp6_mtu);
1175 	struct in_conninfo inc;
1176 
1177 #if 0
1178 	/*
1179 	 * RFC2460 section 5, last paragraph.
1180 	 * even though minimum link MTU for IPv6 is IPV6_MMTU,
1181 	 * we may see ICMPv6 too big with mtu < IPV6_MMTU
1182 	 * due to packet translator in the middle.
1183 	 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for
1184 	 * special handling.
1185 	 */
1186 	if (mtu < IPV6_MMTU)
1187 		return;
1188 #endif
1189 
1190 	/*
1191 	 * we reject ICMPv6 too big with abnormally small value.
1192 	 * XXX what is the good definition of "abnormally small"?
1193 	 */
1194 	if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
1195 		return;
1196 
1197 	if (!validated)
1198 		return;
1199 
1200 	/*
1201 	 * In case the suggested mtu is less than IPV6_MMTU, we
1202 	 * only need to remember that it was for above mentioned
1203 	 * "alwaysfrag" case.
1204 	 * Try to be as close to the spec as possible.
1205 	 */
1206 	if (mtu < IPV6_MMTU)
1207 		mtu = IPV6_MMTU - 8;
1208 
1209 	bzero(&inc, sizeof(inc));
1210 	inc.inc_fibnum = M_GETFIB(m);
1211 	inc.inc_flags |= INC_ISIPV6;
1212 	inc.inc6_faddr = *dst;
1213 	if (in6_setscope(&inc.inc6_faddr, m->m_pkthdr.rcvif, NULL))
1214 		return;
1215 
1216 	if (mtu < tcp_maxmtu6(&inc, NULL)) {
1217 		tcp_hc_updatemtu(&inc, mtu);
1218 		ICMP6STAT_INC(icp6s_pmtuchg);
1219 	}
1220 }
1221 
1222 /*
1223  * Process a Node Information Query packet, based on
1224  * draft-ietf-ipngwg-icmp-name-lookups-07.
1225  *
1226  * Spec incompatibilities:
1227  * - IPv6 Subject address handling
1228  * - IPv4 Subject address handling support missing
1229  * - Proxy reply (answer even if it's not for me)
1230  * - joins NI group address at in6_ifattach() time only, does not cope
1231  *   with hostname changes by sethostname(3)
1232  */
1233 static struct mbuf *
1234 ni6_input(struct mbuf *m, int off)
1235 {
1236 	struct icmp6_nodeinfo *ni6, *nni6;
1237 	struct mbuf *n = NULL;
1238 	struct prison *pr;
1239 	u_int16_t qtype;
1240 	int subjlen;
1241 	int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1242 	struct ni_reply_fqdn *fqdn;
1243 	int addrs;		/* for NI_QTYPE_NODEADDR */
1244 	struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1245 	struct in6_addr in6_subj; /* subject address */
1246 	struct ip6_hdr *ip6;
1247 	int oldfqdn = 0;	/* if 1, return pascal string (03 draft) */
1248 	char *subj = NULL;
1249 	struct in6_ifaddr *ia6 = NULL;
1250 
1251 	ip6 = mtod(m, struct ip6_hdr *);
1252 #ifndef PULLDOWN_TEST
1253 	ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1254 #else
1255 	IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1256 	if (ni6 == NULL) {
1257 		/* m is already reclaimed */
1258 		return (NULL);
1259 	}
1260 #endif
1261 
1262 	/*
1263 	 * Validate IPv6 source address.
1264 	 * The default configuration MUST be to refuse answering queries from
1265 	 * global-scope addresses according to RFC4602.
1266 	 * Notes:
1267 	 *  - it's not very clear what "refuse" means; this implementation
1268 	 *    simply drops it.
1269 	 *  - it's not very easy to identify global-scope (unicast) addresses
1270 	 *    since there are many prefixes for them.  It should be safer
1271 	 *    and in practice sufficient to check "all" but loopback and
1272 	 *    link-local (note that site-local unicast was deprecated and
1273 	 *    ULA is defined as global scope-wise)
1274 	 */
1275 	if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 &&
1276 	    !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) &&
1277 	    !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
1278 		goto bad;
1279 
1280 	/*
1281 	 * Validate IPv6 destination address.
1282 	 *
1283 	 * The Responder must discard the Query without further processing
1284 	 * unless it is one of the Responder's unicast or anycast addresses, or
1285 	 * a link-local scope multicast address which the Responder has joined.
1286 	 * [RFC4602, Section 5.]
1287 	 */
1288 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1289 		if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1290 			goto bad;
1291 		/* else it's a link-local multicast, fine */
1292 	} else {		/* unicast or anycast */
1293 		ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
1294 		if (ia6 == NULL)
1295 			goto bad; /* XXX impossible */
1296 
1297 		if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) &&
1298 		    !(V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) {
1299 			ifa_free(&ia6->ia_ifa);
1300 			nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1301 				"a temporary address in %s:%d",
1302 			       __FILE__, __LINE__));
1303 			goto bad;
1304 		}
1305 		ifa_free(&ia6->ia_ifa);
1306 	}
1307 
1308 	/* validate query Subject field. */
1309 	qtype = ntohs(ni6->ni_qtype);
1310 	subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1311 	switch (qtype) {
1312 	case NI_QTYPE_NOOP:
1313 	case NI_QTYPE_SUPTYPES:
1314 		/* 07 draft */
1315 		if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1316 			break;
1317 		/* FALLTHROUGH */
1318 	case NI_QTYPE_FQDN:
1319 	case NI_QTYPE_NODEADDR:
1320 	case NI_QTYPE_IPV4ADDR:
1321 		switch (ni6->ni_code) {
1322 		case ICMP6_NI_SUBJ_IPV6:
1323 #if ICMP6_NI_SUBJ_IPV6 != 0
1324 		case 0:
1325 #endif
1326 			/*
1327 			 * backward compatibility - try to accept 03 draft
1328 			 * format, where no Subject is present.
1329 			 */
1330 			if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1331 			    subjlen == 0) {
1332 				oldfqdn++;
1333 				break;
1334 			}
1335 #if ICMP6_NI_SUBJ_IPV6 != 0
1336 			if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1337 				goto bad;
1338 #endif
1339 
1340 			if (subjlen != sizeof(struct in6_addr))
1341 				goto bad;
1342 
1343 			/*
1344 			 * Validate Subject address.
1345 			 *
1346 			 * Not sure what exactly "address belongs to the node"
1347 			 * means in the spec, is it just unicast, or what?
1348 			 *
1349 			 * At this moment we consider Subject address as
1350 			 * "belong to the node" if the Subject address equals
1351 			 * to the IPv6 destination address; validation for
1352 			 * IPv6 destination address should have done enough
1353 			 * check for us.
1354 			 *
1355 			 * We do not do proxy at this moment.
1356 			 */
1357 			/* m_pulldown instead of copy? */
1358 			m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1359 			    subjlen, (caddr_t)&in6_subj);
1360 			if (in6_setscope(&in6_subj, m->m_pkthdr.rcvif, NULL))
1361 				goto bad;
1362 
1363 			subj = (char *)&in6_subj;
1364 			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &in6_subj))
1365 				break;
1366 
1367 			/*
1368 			 * XXX if we are to allow other cases, we should really
1369 			 * be careful about scope here.
1370 			 * basically, we should disallow queries toward IPv6
1371 			 * destination X with subject Y,
1372 			 * if scope(X) > scope(Y).
1373 			 * if we allow scope(X) > scope(Y), it will result in
1374 			 * information leakage across scope boundary.
1375 			 */
1376 			goto bad;
1377 
1378 		case ICMP6_NI_SUBJ_FQDN:
1379 			/*
1380 			 * Validate Subject name with gethostname(3).
1381 			 *
1382 			 * The behavior may need some debate, since:
1383 			 * - we are not sure if the node has FQDN as
1384 			 *   hostname (returned by gethostname(3)).
1385 			 * - the code does wildcard match for truncated names.
1386 			 *   however, we are not sure if we want to perform
1387 			 *   wildcard match, if gethostname(3) side has
1388 			 *   truncated hostname.
1389 			 */
1390 			pr = curthread->td_ucred->cr_prison;
1391 			mtx_lock(&pr->pr_mtx);
1392 			n = ni6_nametodns(pr->pr_hostname,
1393 			    strlen(pr->pr_hostname), 0);
1394 			mtx_unlock(&pr->pr_mtx);
1395 			if (!n || n->m_next || n->m_len == 0)
1396 				goto bad;
1397 			IP6_EXTHDR_GET(subj, char *, m,
1398 			    off + sizeof(struct icmp6_nodeinfo), subjlen);
1399 			if (subj == NULL)
1400 				goto bad;
1401 			if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1402 			    n->m_len)) {
1403 				goto bad;
1404 			}
1405 			m_freem(n);
1406 			n = NULL;
1407 			break;
1408 
1409 		case ICMP6_NI_SUBJ_IPV4:	/* XXX: to be implemented? */
1410 		default:
1411 			goto bad;
1412 		}
1413 		break;
1414 	}
1415 
1416 	/* refuse based on configuration.  XXX ICMP6_NI_REFUSED? */
1417 	switch (qtype) {
1418 	case NI_QTYPE_FQDN:
1419 		if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0)
1420 			goto bad;
1421 		break;
1422 	case NI_QTYPE_NODEADDR:
1423 	case NI_QTYPE_IPV4ADDR:
1424 		if ((V_icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0)
1425 			goto bad;
1426 		break;
1427 	}
1428 
1429 	/* guess reply length */
1430 	switch (qtype) {
1431 	case NI_QTYPE_NOOP:
1432 		break;		/* no reply data */
1433 	case NI_QTYPE_SUPTYPES:
1434 		replylen += sizeof(u_int32_t);
1435 		break;
1436 	case NI_QTYPE_FQDN:
1437 		/* XXX will append an mbuf */
1438 		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1439 		break;
1440 	case NI_QTYPE_NODEADDR:
1441 		addrs = ni6_addrs(ni6, m, &ifp, (struct in6_addr *)subj);
1442 		if ((replylen += addrs * (sizeof(struct in6_addr) +
1443 		    sizeof(u_int32_t))) > MCLBYTES)
1444 			replylen = MCLBYTES; /* XXX: will truncate pkt later */
1445 		break;
1446 	case NI_QTYPE_IPV4ADDR:
1447 		/* unsupported - should respond with unknown Qtype? */
1448 		break;
1449 	default:
1450 		/*
1451 		 * XXX: We must return a reply with the ICMP6 code
1452 		 * `unknown Qtype' in this case.  However we regard the case
1453 		 * as an FQDN query for backward compatibility.
1454 		 * Older versions set a random value to this field,
1455 		 * so it rarely varies in the defined qtypes.
1456 		 * But the mechanism is not reliable...
1457 		 * maybe we should obsolete older versions.
1458 		 */
1459 		qtype = NI_QTYPE_FQDN;
1460 		/* XXX will append an mbuf */
1461 		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1462 		oldfqdn++;
1463 		break;
1464 	}
1465 
1466 	/* Allocate an mbuf to reply. */
1467 	if (replylen > MCLBYTES) {
1468 		/*
1469 		 * XXX: should we try to allocate more? But MCLBYTES
1470 		 * is probably much larger than IPV6_MMTU...
1471 		 */
1472 		goto bad;
1473 	}
1474 	if (replylen > MHLEN)
1475 		n = m_getcl(M_NOWAIT, m->m_type, M_PKTHDR);
1476 	else
1477 		n = m_gethdr(M_NOWAIT, m->m_type);
1478 	if (n == NULL) {
1479 		m_freem(m);
1480 		return (NULL);
1481 	}
1482 	m_move_pkthdr(n, m); /* just for recvif and FIB */
1483 	n->m_pkthdr.len = n->m_len = replylen;
1484 
1485 	/* copy mbuf header and IPv6 + Node Information base headers */
1486 	bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1487 	nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1488 	bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1489 
1490 	/* qtype dependent procedure */
1491 	switch (qtype) {
1492 	case NI_QTYPE_NOOP:
1493 		nni6->ni_code = ICMP6_NI_SUCCESS;
1494 		nni6->ni_flags = 0;
1495 		break;
1496 	case NI_QTYPE_SUPTYPES:
1497 	{
1498 		u_int32_t v;
1499 		nni6->ni_code = ICMP6_NI_SUCCESS;
1500 		nni6->ni_flags = htons(0x0000);	/* raw bitmap */
1501 		/* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1502 		v = (u_int32_t)htonl(0x0000000f);
1503 		bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1504 		break;
1505 	}
1506 	case NI_QTYPE_FQDN:
1507 		nni6->ni_code = ICMP6_NI_SUCCESS;
1508 		fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1509 		    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
1510 		nni6->ni_flags = 0; /* XXX: meaningless TTL */
1511 		fqdn->ni_fqdn_ttl = 0;	/* ditto. */
1512 		/*
1513 		 * XXX do we really have FQDN in hostname?
1514 		 */
1515 		pr = curthread->td_ucred->cr_prison;
1516 		mtx_lock(&pr->pr_mtx);
1517 		n->m_next = ni6_nametodns(pr->pr_hostname,
1518 		    strlen(pr->pr_hostname), oldfqdn);
1519 		mtx_unlock(&pr->pr_mtx);
1520 		if (n->m_next == NULL)
1521 			goto bad;
1522 		/* XXX we assume that n->m_next is not a chain */
1523 		if (n->m_next->m_next != NULL)
1524 			goto bad;
1525 		n->m_pkthdr.len += n->m_next->m_len;
1526 		break;
1527 	case NI_QTYPE_NODEADDR:
1528 	{
1529 		int lenlim, copied;
1530 
1531 		nni6->ni_code = ICMP6_NI_SUCCESS;
1532 		n->m_pkthdr.len = n->m_len =
1533 		    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1534 		lenlim = M_TRAILINGSPACE(n);
1535 		copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1536 		/* XXX: reset mbuf length */
1537 		n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1538 		    sizeof(struct icmp6_nodeinfo) + copied;
1539 		break;
1540 	}
1541 	default:
1542 		break;		/* XXX impossible! */
1543 	}
1544 
1545 	nni6->ni_type = ICMP6_NI_REPLY;
1546 	m_freem(m);
1547 	return (n);
1548 
1549   bad:
1550 	m_freem(m);
1551 	if (n)
1552 		m_freem(n);
1553 	return (NULL);
1554 }
1555 
1556 /*
1557  * make a mbuf with DNS-encoded string.  no compression support.
1558  *
1559  * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1560  * treated as truncated name (two \0 at the end).  this is a wild guess.
1561  *
1562  * old - return pascal string if non-zero
1563  */
1564 static struct mbuf *
1565 ni6_nametodns(const char *name, int namelen, int old)
1566 {
1567 	struct mbuf *m;
1568 	char *cp, *ep;
1569 	const char *p, *q;
1570 	int i, len, nterm;
1571 
1572 	if (old)
1573 		len = namelen + 1;
1574 	else
1575 		len = MCLBYTES;
1576 
1577 	/* Because MAXHOSTNAMELEN is usually 256, we use cluster mbuf. */
1578 	if (len > MLEN)
1579 		m = m_getcl(M_NOWAIT, MT_DATA, 0);
1580 	else
1581 		m = m_get(M_NOWAIT, MT_DATA);
1582 	if (m == NULL)
1583 		goto fail;
1584 
1585 	if (old) {
1586 		m->m_len = len;
1587 		*mtod(m, char *) = namelen;
1588 		bcopy(name, mtod(m, char *) + 1, namelen);
1589 		return m;
1590 	} else {
1591 		m->m_len = 0;
1592 		cp = mtod(m, char *);
1593 		ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1594 
1595 		/* if not certain about my name, return empty buffer */
1596 		if (namelen == 0)
1597 			return m;
1598 
1599 		/*
1600 		 * guess if it looks like shortened hostname, or FQDN.
1601 		 * shortened hostname needs two trailing "\0".
1602 		 */
1603 		i = 0;
1604 		for (p = name; p < name + namelen; p++) {
1605 			if (*p && *p == '.')
1606 				i++;
1607 		}
1608 		if (i < 2)
1609 			nterm = 2;
1610 		else
1611 			nterm = 1;
1612 
1613 		p = name;
1614 		while (cp < ep && p < name + namelen) {
1615 			i = 0;
1616 			for (q = p; q < name + namelen && *q && *q != '.'; q++)
1617 				i++;
1618 			/* result does not fit into mbuf */
1619 			if (cp + i + 1 >= ep)
1620 				goto fail;
1621 			/*
1622 			 * DNS label length restriction, RFC1035 page 8.
1623 			 * "i == 0" case is included here to avoid returning
1624 			 * 0-length label on "foo..bar".
1625 			 */
1626 			if (i <= 0 || i >= 64)
1627 				goto fail;
1628 			*cp++ = i;
1629 			bcopy(p, cp, i);
1630 			cp += i;
1631 			p = q;
1632 			if (p < name + namelen && *p == '.')
1633 				p++;
1634 		}
1635 		/* termination */
1636 		if (cp + nterm >= ep)
1637 			goto fail;
1638 		while (nterm-- > 0)
1639 			*cp++ = '\0';
1640 		m->m_len = cp - mtod(m, char *);
1641 		return m;
1642 	}
1643 
1644 	panic("should not reach here");
1645 	/* NOTREACHED */
1646 
1647  fail:
1648 	if (m)
1649 		m_freem(m);
1650 	return NULL;
1651 }
1652 
1653 /*
1654  * check if two DNS-encoded string matches.  takes care of truncated
1655  * form (with \0\0 at the end).  no compression support.
1656  * XXX upper/lowercase match (see RFC2065)
1657  */
1658 static int
1659 ni6_dnsmatch(const char *a, int alen, const char *b, int blen)
1660 {
1661 	const char *a0, *b0;
1662 	int l;
1663 
1664 	/* simplest case - need validation? */
1665 	if (alen == blen && bcmp(a, b, alen) == 0)
1666 		return 1;
1667 
1668 	a0 = a;
1669 	b0 = b;
1670 
1671 	/* termination is mandatory */
1672 	if (alen < 2 || blen < 2)
1673 		return 0;
1674 	if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1675 		return 0;
1676 	alen--;
1677 	blen--;
1678 
1679 	while (a - a0 < alen && b - b0 < blen) {
1680 		if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1681 			return 0;
1682 
1683 		if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1684 			return 0;
1685 		/* we don't support compression yet */
1686 		if (a[0] >= 64 || b[0] >= 64)
1687 			return 0;
1688 
1689 		/* truncated case */
1690 		if (a[0] == 0 && a - a0 == alen - 1)
1691 			return 1;
1692 		if (b[0] == 0 && b - b0 == blen - 1)
1693 			return 1;
1694 		if (a[0] == 0 || b[0] == 0)
1695 			return 0;
1696 
1697 		if (a[0] != b[0])
1698 			return 0;
1699 		l = a[0];
1700 		if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1701 			return 0;
1702 		if (bcmp(a + 1, b + 1, l) != 0)
1703 			return 0;
1704 
1705 		a += 1 + l;
1706 		b += 1 + l;
1707 	}
1708 
1709 	if (a - a0 == alen && b - b0 == blen)
1710 		return 1;
1711 	else
1712 		return 0;
1713 }
1714 
1715 /*
1716  * calculate the number of addresses to be returned in the node info reply.
1717  */
1718 static int
1719 ni6_addrs(struct icmp6_nodeinfo *ni6, struct mbuf *m, struct ifnet **ifpp,
1720     struct in6_addr *subj)
1721 {
1722 	struct ifnet *ifp;
1723 	struct in6_ifaddr *ifa6;
1724 	struct ifaddr *ifa;
1725 	int addrs = 0, addrsofif, iffound = 0;
1726 	int niflags = ni6->ni_flags;
1727 
1728 	if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1729 		switch (ni6->ni_code) {
1730 		case ICMP6_NI_SUBJ_IPV6:
1731 			if (subj == NULL) /* must be impossible... */
1732 				return (0);
1733 			break;
1734 		default:
1735 			/*
1736 			 * XXX: we only support IPv6 subject address for
1737 			 * this Qtype.
1738 			 */
1739 			return (0);
1740 		}
1741 	}
1742 
1743 	IFNET_RLOCK_NOSLEEP();
1744 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1745 		addrsofif = 0;
1746 		IF_ADDR_RLOCK(ifp);
1747 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1748 			if (ifa->ifa_addr->sa_family != AF_INET6)
1749 				continue;
1750 			ifa6 = (struct in6_ifaddr *)ifa;
1751 
1752 			if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1753 			    IN6_ARE_ADDR_EQUAL(subj, &ifa6->ia_addr.sin6_addr))
1754 				iffound = 1;
1755 
1756 			/*
1757 			 * IPv4-mapped addresses can only be returned by a
1758 			 * Node Information proxy, since they represent
1759 			 * addresses of IPv4-only nodes, which perforce do
1760 			 * not implement this protocol.
1761 			 * [icmp-name-lookups-07, Section 5.4]
1762 			 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1763 			 * this function at this moment.
1764 			 */
1765 
1766 			/* What do we have to do about ::1? */
1767 			switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1768 			case IPV6_ADDR_SCOPE_LINKLOCAL:
1769 				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1770 					continue;
1771 				break;
1772 			case IPV6_ADDR_SCOPE_SITELOCAL:
1773 				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1774 					continue;
1775 				break;
1776 			case IPV6_ADDR_SCOPE_GLOBAL:
1777 				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1778 					continue;
1779 				break;
1780 			default:
1781 				continue;
1782 			}
1783 
1784 			/*
1785 			 * check if anycast is okay.
1786 			 * XXX: just experimental.  not in the spec.
1787 			 */
1788 			if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1789 			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1790 				continue; /* we need only unicast addresses */
1791 			if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1792 			    (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1793 				continue;
1794 			}
1795 			addrsofif++; /* count the address */
1796 		}
1797 		IF_ADDR_RUNLOCK(ifp);
1798 		if (iffound) {
1799 			*ifpp = ifp;
1800 			IFNET_RUNLOCK_NOSLEEP();
1801 			return (addrsofif);
1802 		}
1803 
1804 		addrs += addrsofif;
1805 	}
1806 	IFNET_RUNLOCK_NOSLEEP();
1807 
1808 	return (addrs);
1809 }
1810 
1811 static int
1812 ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6,
1813     struct ifnet *ifp0, int resid)
1814 {
1815 	struct ifnet *ifp;
1816 	struct in6_ifaddr *ifa6;
1817 	struct ifaddr *ifa;
1818 	struct ifnet *ifp_dep = NULL;
1819 	int copied = 0, allow_deprecated = 0;
1820 	u_char *cp = (u_char *)(nni6 + 1);
1821 	int niflags = ni6->ni_flags;
1822 	u_int32_t ltime;
1823 
1824 	if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1825 		return (0);	/* needless to copy */
1826 
1827 	IFNET_RLOCK_NOSLEEP();
1828 	ifp = ifp0 ? ifp0 : TAILQ_FIRST(&V_ifnet);
1829   again:
1830 
1831 	for (; ifp; ifp = TAILQ_NEXT(ifp, if_link)) {
1832 		IF_ADDR_RLOCK(ifp);
1833 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1834 			if (ifa->ifa_addr->sa_family != AF_INET6)
1835 				continue;
1836 			ifa6 = (struct in6_ifaddr *)ifa;
1837 
1838 			if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1839 			    allow_deprecated == 0) {
1840 				/*
1841 				 * prefererred address should be put before
1842 				 * deprecated addresses.
1843 				 */
1844 
1845 				/* record the interface for later search */
1846 				if (ifp_dep == NULL)
1847 					ifp_dep = ifp;
1848 
1849 				continue;
1850 			} else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1851 			    allow_deprecated != 0)
1852 				continue; /* we now collect deprecated addrs */
1853 
1854 			/* What do we have to do about ::1? */
1855 			switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1856 			case IPV6_ADDR_SCOPE_LINKLOCAL:
1857 				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1858 					continue;
1859 				break;
1860 			case IPV6_ADDR_SCOPE_SITELOCAL:
1861 				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1862 					continue;
1863 				break;
1864 			case IPV6_ADDR_SCOPE_GLOBAL:
1865 				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1866 					continue;
1867 				break;
1868 			default:
1869 				continue;
1870 			}
1871 
1872 			/*
1873 			 * check if anycast is okay.
1874 			 * XXX: just experimental.  not in the spec.
1875 			 */
1876 			if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1877 			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1878 				continue;
1879 			if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1880 			    (V_icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) {
1881 				continue;
1882 			}
1883 
1884 			/* now we can copy the address */
1885 			if (resid < sizeof(struct in6_addr) +
1886 			    sizeof(u_int32_t)) {
1887 				IF_ADDR_RUNLOCK(ifp);
1888 				/*
1889 				 * We give up much more copy.
1890 				 * Set the truncate flag and return.
1891 				 */
1892 				nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1893 				IFNET_RUNLOCK_NOSLEEP();
1894 				return (copied);
1895 			}
1896 
1897 			/*
1898 			 * Set the TTL of the address.
1899 			 * The TTL value should be one of the following
1900 			 * according to the specification:
1901 			 *
1902 			 * 1. The remaining lifetime of a DHCP lease on the
1903 			 *    address, or
1904 			 * 2. The remaining Valid Lifetime of a prefix from
1905 			 *    which the address was derived through Stateless
1906 			 *    Autoconfiguration.
1907 			 *
1908 			 * Note that we currently do not support stateful
1909 			 * address configuration by DHCPv6, so the former
1910 			 * case can't happen.
1911 			 */
1912 			if (ifa6->ia6_lifetime.ia6t_expire == 0)
1913 				ltime = ND6_INFINITE_LIFETIME;
1914 			else {
1915 				if (ifa6->ia6_lifetime.ia6t_expire >
1916 				    time_uptime)
1917 					ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_uptime);
1918 				else
1919 					ltime = 0;
1920 			}
1921 
1922 			bcopy(&ltime, cp, sizeof(u_int32_t));
1923 			cp += sizeof(u_int32_t);
1924 
1925 			/* copy the address itself */
1926 			bcopy(&ifa6->ia_addr.sin6_addr, cp,
1927 			    sizeof(struct in6_addr));
1928 			in6_clearscope((struct in6_addr *)cp); /* XXX */
1929 			cp += sizeof(struct in6_addr);
1930 
1931 			resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1932 			copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
1933 		}
1934 		IF_ADDR_RUNLOCK(ifp);
1935 		if (ifp0)	/* we need search only on the specified IF */
1936 			break;
1937 	}
1938 
1939 	if (allow_deprecated == 0 && ifp_dep != NULL) {
1940 		ifp = ifp_dep;
1941 		allow_deprecated = 1;
1942 
1943 		goto again;
1944 	}
1945 
1946 	IFNET_RUNLOCK_NOSLEEP();
1947 
1948 	return (copied);
1949 }
1950 
1951 /*
1952  * XXX almost dup'ed code with rip6_input.
1953  */
1954 static int
1955 icmp6_rip6_input(struct mbuf **mp, int off)
1956 {
1957 	struct mbuf *m = *mp;
1958 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1959 	struct inpcb *in6p;
1960 	struct inpcb *last = NULL;
1961 	struct sockaddr_in6 fromsa;
1962 	struct icmp6_hdr *icmp6;
1963 	struct mbuf *opts = NULL;
1964 
1965 #ifndef PULLDOWN_TEST
1966 	/* this is assumed to be safe. */
1967 	icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1968 #else
1969 	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1970 	if (icmp6 == NULL) {
1971 		/* m is already reclaimed */
1972 		return (IPPROTO_DONE);
1973 	}
1974 #endif
1975 
1976 	/*
1977 	 * XXX: the address may have embedded scope zone ID, which should be
1978 	 * hidden from applications.
1979 	 */
1980 	bzero(&fromsa, sizeof(fromsa));
1981 	fromsa.sin6_family = AF_INET6;
1982 	fromsa.sin6_len = sizeof(struct sockaddr_in6);
1983 	fromsa.sin6_addr = ip6->ip6_src;
1984 	if (sa6_recoverscope(&fromsa)) {
1985 		m_freem(m);
1986 		return (IPPROTO_DONE);
1987 	}
1988 
1989 	INP_INFO_RLOCK(&V_ripcbinfo);
1990 	LIST_FOREACH(in6p, &V_ripcb, inp_list) {
1991 		if ((in6p->inp_vflag & INP_IPV6) == 0)
1992 			continue;
1993 		if (in6p->inp_ip_p != IPPROTO_ICMPV6)
1994 			continue;
1995 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1996 		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1997 			continue;
1998 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1999 		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
2000 			continue;
2001 		INP_RLOCK(in6p);
2002 		if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
2003 		    in6p->in6p_icmp6filt)) {
2004 			INP_RUNLOCK(in6p);
2005 			continue;
2006 		}
2007 		if (last != NULL) {
2008 			struct	mbuf *n = NULL;
2009 
2010 			/*
2011 			 * Recent network drivers tend to allocate a single
2012 			 * mbuf cluster, rather than to make a couple of
2013 			 * mbufs without clusters.  Also, since the IPv6 code
2014 			 * path tries to avoid m_pullup(), it is highly
2015 			 * probable that we still have an mbuf cluster here
2016 			 * even though the necessary length can be stored in an
2017 			 * mbuf's internal buffer.
2018 			 * Meanwhile, the default size of the receive socket
2019 			 * buffer for raw sockets is not so large.  This means
2020 			 * the possibility of packet loss is relatively higher
2021 			 * than before.  To avoid this scenario, we copy the
2022 			 * received data to a separate mbuf that does not use
2023 			 * a cluster, if possible.
2024 			 * XXX: it is better to copy the data after stripping
2025 			 * intermediate headers.
2026 			 */
2027 			if ((m->m_flags & M_EXT) && m->m_next == NULL &&
2028 			    m->m_len <= MHLEN) {
2029 				n = m_get(M_NOWAIT, m->m_type);
2030 				if (n != NULL) {
2031 					if (m_dup_pkthdr(n, m, M_NOWAIT)) {
2032 						bcopy(m->m_data, n->m_data,
2033 						      m->m_len);
2034 						n->m_len = m->m_len;
2035 					} else {
2036 						m_free(n);
2037 						n = NULL;
2038 					}
2039 				}
2040 			}
2041 			if (n != NULL ||
2042 			    (n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
2043 				if (last->inp_flags & INP_CONTROLOPTS)
2044 					ip6_savecontrol(last, n, &opts);
2045 				/* strip intermediate headers */
2046 				m_adj(n, off);
2047 				SOCKBUF_LOCK(&last->inp_socket->so_rcv);
2048 				if (sbappendaddr_locked(
2049 				    &last->inp_socket->so_rcv,
2050 				    (struct sockaddr *)&fromsa, n, opts)
2051 				    == 0) {
2052 					/* should notify about lost packet */
2053 					m_freem(n);
2054 					if (opts) {
2055 						m_freem(opts);
2056 					}
2057 					SOCKBUF_UNLOCK(
2058 					    &last->inp_socket->so_rcv);
2059 				} else
2060 					sorwakeup_locked(last->inp_socket);
2061 				opts = NULL;
2062 			}
2063 			INP_RUNLOCK(last);
2064 		}
2065 		last = in6p;
2066 	}
2067 	INP_INFO_RUNLOCK(&V_ripcbinfo);
2068 	if (last != NULL) {
2069 		if (last->inp_flags & INP_CONTROLOPTS)
2070 			ip6_savecontrol(last, m, &opts);
2071 		/* strip intermediate headers */
2072 		m_adj(m, off);
2073 
2074 		/* avoid using mbuf clusters if possible (see above) */
2075 		if ((m->m_flags & M_EXT) && m->m_next == NULL &&
2076 		    m->m_len <= MHLEN) {
2077 			struct mbuf *n;
2078 
2079 			n = m_get(M_NOWAIT, m->m_type);
2080 			if (n != NULL) {
2081 				if (m_dup_pkthdr(n, m, M_NOWAIT)) {
2082 					bcopy(m->m_data, n->m_data, m->m_len);
2083 					n->m_len = m->m_len;
2084 
2085 					m_freem(m);
2086 					m = n;
2087 				} else {
2088 					m_freem(n);
2089 					n = NULL;
2090 				}
2091 			}
2092 		}
2093 		SOCKBUF_LOCK(&last->inp_socket->so_rcv);
2094 		if (sbappendaddr_locked(&last->inp_socket->so_rcv,
2095 		    (struct sockaddr *)&fromsa, m, opts) == 0) {
2096 			m_freem(m);
2097 			if (opts)
2098 				m_freem(opts);
2099 			SOCKBUF_UNLOCK(&last->inp_socket->so_rcv);
2100 		} else
2101 			sorwakeup_locked(last->inp_socket);
2102 		INP_RUNLOCK(last);
2103 	} else {
2104 		m_freem(m);
2105 		IP6STAT_DEC(ip6s_delivered);
2106 	}
2107 	return IPPROTO_DONE;
2108 }
2109 
2110 /*
2111  * Reflect the ip6 packet back to the source.
2112  * OFF points to the icmp6 header, counted from the top of the mbuf.
2113  */
2114 void
2115 icmp6_reflect(struct mbuf *m, size_t off)
2116 {
2117 	struct in6_addr src6, *srcp;
2118 	struct ip6_hdr *ip6;
2119 	struct icmp6_hdr *icmp6;
2120 	struct in6_ifaddr *ia = NULL;
2121 	struct ifnet *outif = NULL;
2122 	int plen;
2123 	int type, code, hlim;
2124 
2125 	/* too short to reflect */
2126 	if (off < sizeof(struct ip6_hdr)) {
2127 		nd6log((LOG_DEBUG,
2128 		    "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2129 		    (u_long)off, (u_long)sizeof(struct ip6_hdr),
2130 		    __FILE__, __LINE__));
2131 		goto bad;
2132 	}
2133 
2134 	/*
2135 	 * If there are extra headers between IPv6 and ICMPv6, strip
2136 	 * off that header first.
2137 	 */
2138 #ifdef DIAGNOSTIC
2139 	if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2140 		panic("assumption failed in icmp6_reflect");
2141 #endif
2142 	if (off > sizeof(struct ip6_hdr)) {
2143 		size_t l;
2144 		struct ip6_hdr nip6;
2145 
2146 		l = off - sizeof(struct ip6_hdr);
2147 		m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2148 		m_adj(m, l);
2149 		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2150 		if (m->m_len < l) {
2151 			if ((m = m_pullup(m, l)) == NULL)
2152 				return;
2153 		}
2154 		bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2155 	} else /* off == sizeof(struct ip6_hdr) */ {
2156 		size_t l;
2157 		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2158 		if (m->m_len < l) {
2159 			if ((m = m_pullup(m, l)) == NULL)
2160 				return;
2161 		}
2162 	}
2163 	plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2164 	ip6 = mtod(m, struct ip6_hdr *);
2165 	ip6->ip6_nxt = IPPROTO_ICMPV6;
2166 	icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2167 	type = icmp6->icmp6_type; /* keep type for statistics */
2168 	code = icmp6->icmp6_code; /* ditto. */
2169 	hlim = 0;
2170 	srcp = NULL;
2171 
2172 	/*
2173 	 * If the incoming packet was addressed directly to us (i.e. unicast),
2174 	 * use dst as the src for the reply.
2175 	 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2176 	 * (for example) when we encounter an error while forwarding procedure
2177 	 * destined to a duplicated address of ours.
2178 	 */
2179 	if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
2180 		ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
2181 		if (ia != NULL && !(ia->ia6_flags &
2182 		    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY))) {
2183 			src6 = ia->ia_addr.sin6_addr;
2184 			srcp = &src6;
2185 
2186 			if (m->m_pkthdr.rcvif != NULL) {
2187 				/* XXX: This may not be the outgoing interface */
2188 				hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2189 			} else
2190 				hlim = V_ip6_defhlim;
2191 		}
2192 		if (ia != NULL)
2193 			ifa_free(&ia->ia_ifa);
2194 	}
2195 
2196 	if (srcp == NULL) {
2197 		int error;
2198 		struct in6_addr dst6;
2199 		uint32_t scopeid;
2200 
2201 		/*
2202 		 * This case matches to multicasts, our anycast, or unicasts
2203 		 * that we do not own.  Select a source address based on the
2204 		 * source address of the erroneous packet.
2205 		 */
2206 		in6_splitscope(&ip6->ip6_dst, &dst6, &scopeid);
2207 		error = in6_selectsrc_addr(RT_DEFAULT_FIB, &dst6,
2208 		    scopeid, NULL, &src6, &hlim);
2209 
2210 		if (error) {
2211 			char ip6buf[INET6_ADDRSTRLEN];
2212 			nd6log((LOG_DEBUG,
2213 			    "icmp6_reflect: source can't be determined: "
2214 			    "dst=%s, error=%d\n",
2215 			    ip6_sprintf(ip6buf, &ip6->ip6_dst), error));
2216 			goto bad;
2217 		}
2218 		srcp = &src6;
2219 	}
2220 	/*
2221 	 * ip6_input() drops a packet if its src is multicast.
2222 	 * So, the src is never multicast.
2223 	 */
2224 	ip6->ip6_dst = ip6->ip6_src;
2225 	ip6->ip6_src = *srcp;
2226 	ip6->ip6_flow = 0;
2227 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2228 	ip6->ip6_vfc |= IPV6_VERSION;
2229 	ip6->ip6_nxt = IPPROTO_ICMPV6;
2230 	ip6->ip6_hlim = hlim;
2231 
2232 	icmp6->icmp6_cksum = 0;
2233 	icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2234 	    sizeof(struct ip6_hdr), plen);
2235 
2236 	/*
2237 	 * XXX option handling
2238 	 */
2239 
2240 	m->m_flags &= ~(M_BCAST|M_MCAST);
2241 
2242 	ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2243 	if (outif)
2244 		icmp6_ifoutstat_inc(outif, type, code);
2245 
2246 	return;
2247 
2248  bad:
2249 	m_freem(m);
2250 	return;
2251 }
2252 
2253 void
2254 icmp6_fasttimo(void)
2255 {
2256 
2257 	mld_fasttimo();
2258 }
2259 
2260 void
2261 icmp6_slowtimo(void)
2262 {
2263 
2264 	mld_slowtimo();
2265 }
2266 
2267 static const char *
2268 icmp6_redirect_diag(struct in6_addr *src6, struct in6_addr *dst6,
2269     struct in6_addr *tgt6)
2270 {
2271 	static char buf[1024];
2272 	char ip6bufs[INET6_ADDRSTRLEN];
2273 	char ip6bufd[INET6_ADDRSTRLEN];
2274 	char ip6buft[INET6_ADDRSTRLEN];
2275 	snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2276 	    ip6_sprintf(ip6bufs, src6), ip6_sprintf(ip6bufd, dst6),
2277 	    ip6_sprintf(ip6buft, tgt6));
2278 	return buf;
2279 }
2280 
2281 void
2282 icmp6_redirect_input(struct mbuf *m, int off)
2283 {
2284 	struct ifnet *ifp;
2285 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2286 	struct nd_redirect *nd_rd;
2287 	int icmp6len = ntohs(ip6->ip6_plen);
2288 	char *lladdr = NULL;
2289 	int lladdrlen = 0;
2290 	int is_router;
2291 	int is_onlink;
2292 	struct in6_addr src6 = ip6->ip6_src;
2293 	struct in6_addr redtgt6;
2294 	struct in6_addr reddst6;
2295 	union nd_opts ndopts;
2296 	char ip6buf[INET6_ADDRSTRLEN];
2297 
2298 	M_ASSERTPKTHDR(m);
2299 	KASSERT(m->m_pkthdr.rcvif != NULL, ("%s: no rcvif", __func__));
2300 
2301 	ifp = m->m_pkthdr.rcvif;
2302 
2303 	/* XXX if we are router, we don't update route by icmp6 redirect */
2304 	if (V_ip6_forwarding)
2305 		goto freeit;
2306 	if (!V_icmp6_rediraccept)
2307 		goto freeit;
2308 
2309 #ifndef PULLDOWN_TEST
2310 	IP6_EXTHDR_CHECK(m, off, icmp6len,);
2311 	nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2312 #else
2313 	IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2314 	if (nd_rd == NULL) {
2315 		ICMP6STAT_INC(icp6s_tooshort);
2316 		return;
2317 	}
2318 #endif
2319 	redtgt6 = nd_rd->nd_rd_target;
2320 	reddst6 = nd_rd->nd_rd_dst;
2321 
2322 	if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) ||
2323 	    in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) {
2324 		goto freeit;
2325 	}
2326 
2327 	/* validation */
2328 	if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2329 		nd6log((LOG_ERR,
2330 		    "ICMP6 redirect sent from %s rejected; "
2331 		    "must be from linklocal\n",
2332 		    ip6_sprintf(ip6buf, &src6)));
2333 		goto bad;
2334 	}
2335 	if (ip6->ip6_hlim != 255) {
2336 		nd6log((LOG_ERR,
2337 		    "ICMP6 redirect sent from %s rejected; "
2338 		    "hlim=%d (must be 255)\n",
2339 		    ip6_sprintf(ip6buf, &src6), ip6->ip6_hlim));
2340 		goto bad;
2341 	}
2342     {
2343 	/* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2344 	struct nhop6_basic nh6;
2345 	struct in6_addr kdst;
2346 	uint32_t scopeid;
2347 
2348 	in6_splitscope(&reddst6, &kdst, &scopeid);
2349 	if (fib6_lookup_nh_basic(RT_DEFAULT_FIB, &kdst, scopeid, 0, 0,&nh6)==0){
2350 		if ((nh6.nh_flags & NHF_GATEWAY) == 0) {
2351 			nd6log((LOG_ERR,
2352 			    "ICMP6 redirect rejected; no route "
2353 			    "with inet6 gateway found for redirect dst: %s\n",
2354 			    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2355 			goto bad;
2356 		}
2357 
2358 		if (IN6_ARE_ADDR_EQUAL(&src6, &nh6.nh_addr) == 0) {
2359 			nd6log((LOG_ERR,
2360 			    "ICMP6 redirect rejected; "
2361 			    "not equal to gw-for-src=%s (must be same): "
2362 			    "%s\n",
2363 			    ip6_sprintf(ip6buf, &nh6.nh_addr),
2364 			    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2365 			goto bad;
2366 		}
2367 	} else {
2368 		nd6log((LOG_ERR,
2369 		    "ICMP6 redirect rejected; "
2370 		    "no route found for redirect dst: %s\n",
2371 		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2372 		goto bad;
2373 	}
2374     }
2375 	if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2376 		nd6log((LOG_ERR,
2377 		    "ICMP6 redirect rejected; "
2378 		    "redirect dst must be unicast: %s\n",
2379 		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2380 		goto bad;
2381 	}
2382 
2383 	is_router = is_onlink = 0;
2384 	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2385 		is_router = 1;	/* router case */
2386 	if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2387 		is_onlink = 1;	/* on-link destination case */
2388 	if (!is_router && !is_onlink) {
2389 		nd6log((LOG_ERR,
2390 		    "ICMP6 redirect rejected; "
2391 		    "neither router case nor onlink case: %s\n",
2392 		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2393 		goto bad;
2394 	}
2395 
2396 	icmp6len -= sizeof(*nd_rd);
2397 	nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2398 	if (nd6_options(&ndopts) < 0) {
2399 		nd6log((LOG_INFO, "%s: invalid ND option, rejected: %s\n",
2400 		    __func__, icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2401 		/* nd6_options have incremented stats */
2402 		goto freeit;
2403 	}
2404 
2405 	if (ndopts.nd_opts_tgt_lladdr) {
2406 		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2407 		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2408 	}
2409 
2410 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2411 		nd6log((LOG_INFO, "%s: lladdrlen mismatch for %s "
2412 		    "(if %d, icmp6 packet %d): %s\n",
2413 		    __func__, ip6_sprintf(ip6buf, &redtgt6),
2414 		    ifp->if_addrlen, lladdrlen - 2,
2415 		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2416 		goto bad;
2417 	}
2418 
2419 	/* Validation passed. */
2420 
2421 	/* RFC 2461 8.3 */
2422 	nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2423 	    is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2424 
2425 	/*
2426 	 * Install a gateway route in the better-router case or an interface
2427 	 * route in the on-link-destination case.
2428 	 */
2429 	{
2430 		struct sockaddr_in6 sdst;
2431 		struct sockaddr_in6 sgw;
2432 		struct sockaddr_in6 ssrc;
2433 		struct sockaddr *gw;
2434 		int rt_flags;
2435 		u_int fibnum;
2436 
2437 		bzero(&sdst, sizeof(sdst));
2438 		bzero(&ssrc, sizeof(ssrc));
2439 		sdst.sin6_family = ssrc.sin6_family = AF_INET6;
2440 		sdst.sin6_len = ssrc.sin6_len = sizeof(struct sockaddr_in6);
2441 		bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2442 		bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2443 		rt_flags = RTF_HOST;
2444 		if (is_router) {
2445 			bzero(&sgw, sizeof(sgw));
2446 			sgw.sin6_family = AF_INET6;
2447 			sgw.sin6_len = sizeof(struct sockaddr_in6);
2448 			bcopy(&redtgt6, &sgw.sin6_addr,
2449 				sizeof(struct in6_addr));
2450 			gw = (struct sockaddr *)&sgw;
2451 			rt_flags |= RTF_GATEWAY;
2452 		} else
2453 			gw = ifp->if_addr->ifa_addr;
2454 		for (fibnum = 0; fibnum < rt_numfibs; fibnum++)
2455 			in6_rtredirect((struct sockaddr *)&sdst, gw,
2456 			    (struct sockaddr *)NULL, rt_flags,
2457 			    (struct sockaddr *)&ssrc, fibnum);
2458 	}
2459 	/* finally update cached route in each socket via pfctlinput */
2460     {
2461 	struct sockaddr_in6 sdst;
2462 
2463 	bzero(&sdst, sizeof(sdst));
2464 	sdst.sin6_family = AF_INET6;
2465 	sdst.sin6_len = sizeof(struct sockaddr_in6);
2466 	bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2467 	pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2468     }
2469 
2470  freeit:
2471 	m_freem(m);
2472 	return;
2473 
2474  bad:
2475 	ICMP6STAT_INC(icp6s_badredirect);
2476 	m_freem(m);
2477 }
2478 
2479 void
2480 icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt)
2481 {
2482 	struct ifnet *ifp;	/* my outgoing interface */
2483 	struct in6_addr *ifp_ll6;
2484 	struct in6_addr *router_ll6;
2485 	struct ip6_hdr *sip6;	/* m0 as struct ip6_hdr */
2486 	struct mbuf *m = NULL;	/* newly allocated one */
2487 	struct m_tag *mtag;
2488 	struct ip6_hdr *ip6;	/* m as struct ip6_hdr */
2489 	struct nd_redirect *nd_rd;
2490 	struct llentry *ln = NULL;
2491 	size_t maxlen;
2492 	u_char *p;
2493 	struct ifnet *outif = NULL;
2494 	struct sockaddr_in6 src_sa;
2495 
2496 	icmp6_errcount(ND_REDIRECT, 0);
2497 
2498 	/* if we are not router, we don't send icmp6 redirect */
2499 	if (!V_ip6_forwarding)
2500 		goto fail;
2501 
2502 	/* sanity check */
2503 	if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2504 		goto fail;
2505 
2506 	/*
2507 	 * Address check:
2508 	 *  the source address must identify a neighbor, and
2509 	 *  the destination address must not be a multicast address
2510 	 *  [RFC 2461, sec 8.2]
2511 	 */
2512 	sip6 = mtod(m0, struct ip6_hdr *);
2513 	bzero(&src_sa, sizeof(src_sa));
2514 	src_sa.sin6_family = AF_INET6;
2515 	src_sa.sin6_len = sizeof(src_sa);
2516 	src_sa.sin6_addr = sip6->ip6_src;
2517 	if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2518 		goto fail;
2519 	if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2520 		goto fail;	/* what should we do here? */
2521 
2522 	/* rate limit */
2523 	if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2524 		goto fail;
2525 
2526 	/*
2527 	 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2528 	 * we almost always ask for an mbuf cluster for simplicity.
2529 	 * (MHLEN < IPV6_MMTU is almost always true)
2530 	 */
2531 #if IPV6_MMTU >= MCLBYTES
2532 # error assumption failed about IPV6_MMTU and MCLBYTES
2533 #endif
2534 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2535 	if (m == NULL)
2536 		goto fail;
2537 	M_SETFIB(m, rt->rt_fibnum);
2538 	maxlen = M_TRAILINGSPACE(m);
2539 	maxlen = min(IPV6_MMTU, maxlen);
2540 	/* just for safety */
2541 	if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2542 	    ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2543 		goto fail;
2544 	}
2545 
2546 	{
2547 		/* get ip6 linklocal address for ifp(my outgoing interface). */
2548 		struct in6_ifaddr *ia;
2549 		if ((ia = in6ifa_ifpforlinklocal(ifp,
2550 						 IN6_IFF_NOTREADY|
2551 						 IN6_IFF_ANYCAST)) == NULL)
2552 			goto fail;
2553 		ifp_ll6 = &ia->ia_addr.sin6_addr;
2554 		/* XXXRW: reference released prematurely. */
2555 		ifa_free(&ia->ia_ifa);
2556 	}
2557 
2558 	/* get ip6 linklocal address for the router. */
2559 	if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2560 		struct sockaddr_in6 *sin6;
2561 		sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2562 		router_ll6 = &sin6->sin6_addr;
2563 		if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2564 			router_ll6 = (struct in6_addr *)NULL;
2565 	} else
2566 		router_ll6 = (struct in6_addr *)NULL;
2567 
2568 	/* ip6 */
2569 	ip6 = mtod(m, struct ip6_hdr *);
2570 	ip6->ip6_flow = 0;
2571 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2572 	ip6->ip6_vfc |= IPV6_VERSION;
2573 	/* ip6->ip6_plen will be set later */
2574 	ip6->ip6_nxt = IPPROTO_ICMPV6;
2575 	ip6->ip6_hlim = 255;
2576 	/* ip6->ip6_src must be linklocal addr for my outgoing if. */
2577 	bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2578 	bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2579 
2580 	/* ND Redirect */
2581 	nd_rd = (struct nd_redirect *)(ip6 + 1);
2582 	nd_rd->nd_rd_type = ND_REDIRECT;
2583 	nd_rd->nd_rd_code = 0;
2584 	nd_rd->nd_rd_reserved = 0;
2585 	if (rt->rt_flags & RTF_GATEWAY) {
2586 		/*
2587 		 * nd_rd->nd_rd_target must be a link-local address in
2588 		 * better router cases.
2589 		 */
2590 		if (!router_ll6)
2591 			goto fail;
2592 		bcopy(router_ll6, &nd_rd->nd_rd_target,
2593 		    sizeof(nd_rd->nd_rd_target));
2594 		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2595 		    sizeof(nd_rd->nd_rd_dst));
2596 	} else {
2597 		/* make sure redtgt == reddst */
2598 		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2599 		    sizeof(nd_rd->nd_rd_target));
2600 		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2601 		    sizeof(nd_rd->nd_rd_dst));
2602 	}
2603 
2604 	p = (u_char *)(nd_rd + 1);
2605 
2606 	if (!router_ll6)
2607 		goto nolladdropt;
2608 
2609 	{
2610 		/* target lladdr option */
2611 		int len;
2612 		struct nd_opt_hdr *nd_opt;
2613 		char *lladdr;
2614 
2615 		IF_AFDATA_RLOCK(ifp);
2616 		ln = nd6_lookup(router_ll6, 0, ifp);
2617 		IF_AFDATA_RUNLOCK(ifp);
2618 		if (ln == NULL)
2619 			goto nolladdropt;
2620 
2621 		len = sizeof(*nd_opt) + ifp->if_addrlen;
2622 		len = (len + 7) & ~7;	/* round by 8 */
2623 		/* safety check */
2624 		if (len + (p - (u_char *)ip6) > maxlen)
2625 			goto nolladdropt;
2626 
2627 		if (ln->la_flags & LLE_VALID) {
2628 			nd_opt = (struct nd_opt_hdr *)p;
2629 			nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2630 			nd_opt->nd_opt_len = len >> 3;
2631 			lladdr = (char *)(nd_opt + 1);
2632 			bcopy(ln->ll_addr, lladdr, ifp->if_addrlen);
2633 			p += len;
2634 		}
2635 	}
2636 nolladdropt:
2637 	if (ln != NULL)
2638 		LLE_RUNLOCK(ln);
2639 
2640 	m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2641 
2642 	/* just to be safe */
2643 #ifdef M_DECRYPTED	/*not openbsd*/
2644 	if (m0->m_flags & M_DECRYPTED)
2645 		goto noredhdropt;
2646 #endif
2647 	if (p - (u_char *)ip6 > maxlen)
2648 		goto noredhdropt;
2649 
2650 	{
2651 		/* redirected header option */
2652 		int len;
2653 		struct nd_opt_rd_hdr *nd_opt_rh;
2654 
2655 		/*
2656 		 * compute the maximum size for icmp6 redirect header option.
2657 		 * XXX room for auth header?
2658 		 */
2659 		len = maxlen - (p - (u_char *)ip6);
2660 		len &= ~7;
2661 
2662 		/* This is just for simplicity. */
2663 		if (m0->m_pkthdr.len != m0->m_len) {
2664 			if (m0->m_next) {
2665 				m_freem(m0->m_next);
2666 				m0->m_next = NULL;
2667 			}
2668 			m0->m_pkthdr.len = m0->m_len;
2669 		}
2670 
2671 		/*
2672 		 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2673 		 * about padding/truncate rule for the original IP packet.
2674 		 * From the discussion on IPv6imp in Feb 1999,
2675 		 * the consensus was:
2676 		 * - "attach as much as possible" is the goal
2677 		 * - pad if not aligned (original size can be guessed by
2678 		 *   original ip6 header)
2679 		 * Following code adds the padding if it is simple enough,
2680 		 * and truncates if not.
2681 		 */
2682 		if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2683 			panic("assumption failed in %s:%d", __FILE__,
2684 			    __LINE__);
2685 
2686 		if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2687 			/* not enough room, truncate */
2688 			m0->m_pkthdr.len = m0->m_len = len -
2689 			    sizeof(*nd_opt_rh);
2690 		} else {
2691 			/* enough room, pad or truncate */
2692 			size_t extra;
2693 
2694 			extra = m0->m_pkthdr.len % 8;
2695 			if (extra) {
2696 				/* pad if easy enough, truncate if not */
2697 				if (8 - extra <= M_TRAILINGSPACE(m0)) {
2698 					/* pad */
2699 					m0->m_len += (8 - extra);
2700 					m0->m_pkthdr.len += (8 - extra);
2701 				} else {
2702 					/* truncate */
2703 					m0->m_pkthdr.len -= extra;
2704 					m0->m_len -= extra;
2705 				}
2706 			}
2707 			len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2708 			m0->m_pkthdr.len = m0->m_len = len -
2709 			    sizeof(*nd_opt_rh);
2710 		}
2711 
2712 		nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2713 		bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2714 		nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2715 		nd_opt_rh->nd_opt_rh_len = len >> 3;
2716 		p += sizeof(*nd_opt_rh);
2717 		m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2718 
2719 		/* connect m0 to m */
2720 		m_tag_delete_chain(m0, NULL);
2721 		m0->m_flags &= ~M_PKTHDR;
2722 		m->m_next = m0;
2723 		m->m_pkthdr.len = m->m_len + m0->m_len;
2724 		m0 = NULL;
2725 	}
2726 noredhdropt:;
2727 	if (m0) {
2728 		m_freem(m0);
2729 		m0 = NULL;
2730 	}
2731 
2732 	/* XXX: clear embedded link IDs in the inner header */
2733 	in6_clearscope(&sip6->ip6_src);
2734 	in6_clearscope(&sip6->ip6_dst);
2735 	in6_clearscope(&nd_rd->nd_rd_target);
2736 	in6_clearscope(&nd_rd->nd_rd_dst);
2737 
2738 	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2739 
2740 	nd_rd->nd_rd_cksum = 0;
2741 	nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2742 	    sizeof(*ip6), ntohs(ip6->ip6_plen));
2743 
2744         if (send_sendso_input_hook != NULL) {
2745 		mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, sizeof(unsigned short),
2746 			M_NOWAIT);
2747 		if (mtag == NULL)
2748 			goto fail;
2749 		*(unsigned short *)(mtag + 1) = nd_rd->nd_rd_type;
2750 		m_tag_prepend(m, mtag);
2751 	}
2752 
2753 	/* send the packet to outside... */
2754 	ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2755 	if (outif) {
2756 		icmp6_ifstat_inc(outif, ifs6_out_msg);
2757 		icmp6_ifstat_inc(outif, ifs6_out_redirect);
2758 	}
2759 	ICMP6STAT_INC(icp6s_outhist[ND_REDIRECT]);
2760 
2761 	return;
2762 
2763 fail:
2764 	if (m)
2765 		m_freem(m);
2766 	if (m0)
2767 		m_freem(m0);
2768 }
2769 
2770 /*
2771  * ICMPv6 socket option processing.
2772  */
2773 int
2774 icmp6_ctloutput(struct socket *so, struct sockopt *sopt)
2775 {
2776 	int error = 0;
2777 	int optlen;
2778 	struct inpcb *inp = sotoinpcb(so);
2779 	int level, op, optname;
2780 
2781 	if (sopt) {
2782 		level = sopt->sopt_level;
2783 		op = sopt->sopt_dir;
2784 		optname = sopt->sopt_name;
2785 		optlen = sopt->sopt_valsize;
2786 	} else
2787 		level = op = optname = optlen = 0;
2788 
2789 	if (level != IPPROTO_ICMPV6) {
2790 		return EINVAL;
2791 	}
2792 
2793 	switch (op) {
2794 	case PRCO_SETOPT:
2795 		switch (optname) {
2796 		case ICMP6_FILTER:
2797 		    {
2798 			struct icmp6_filter ic6f;
2799 
2800 			if (optlen != sizeof(ic6f)) {
2801 				error = EMSGSIZE;
2802 				break;
2803 			}
2804 			error = sooptcopyin(sopt, &ic6f, optlen, optlen);
2805 			if (error == 0) {
2806 				INP_WLOCK(inp);
2807 				*inp->in6p_icmp6filt = ic6f;
2808 				INP_WUNLOCK(inp);
2809 			}
2810 			break;
2811 		    }
2812 
2813 		default:
2814 			error = ENOPROTOOPT;
2815 			break;
2816 		}
2817 		break;
2818 
2819 	case PRCO_GETOPT:
2820 		switch (optname) {
2821 		case ICMP6_FILTER:
2822 		    {
2823 			struct icmp6_filter ic6f;
2824 
2825 			INP_RLOCK(inp);
2826 			ic6f = *inp->in6p_icmp6filt;
2827 			INP_RUNLOCK(inp);
2828 			error = sooptcopyout(sopt, &ic6f, sizeof(ic6f));
2829 			break;
2830 		    }
2831 
2832 		default:
2833 			error = ENOPROTOOPT;
2834 			break;
2835 		}
2836 		break;
2837 	}
2838 
2839 	return (error);
2840 }
2841 
2842 /*
2843  * Perform rate limit check.
2844  * Returns 0 if it is okay to send the icmp6 packet.
2845  * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2846  * limitation.
2847  *
2848  * XXX per-destination/type check necessary?
2849  *
2850  * dst - not used at this moment
2851  * type - not used at this moment
2852  * code - not used at this moment
2853  */
2854 static int
2855 icmp6_ratelimit(const struct in6_addr *dst, const int type,
2856     const int code)
2857 {
2858 	int ret;
2859 
2860 	ret = 0;	/* okay to send */
2861 
2862 	/* PPS limit */
2863 	if (!ppsratecheck(&V_icmp6errppslim_last, &V_icmp6errpps_count,
2864 	    V_icmp6errppslim)) {
2865 		/* The packet is subject to rate limit */
2866 		ret++;
2867 	}
2868 
2869 	return ret;
2870 }
2871