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