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