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