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