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