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