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