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