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