xref: /netbsd-src/sys/netinet6/ip6_input.c (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /*	$NetBSD: ip6_input.c,v 1.214 2019/10/18 04:33:53 ozaki-r Exp $	*/
2 /*	$KAME: ip6_input.c,v 1.188 2001/03/29 05:34:31 itojun 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_input.c	8.2 (Berkeley) 1/4/94
62  */
63 
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: ip6_input.c,v 1.214 2019/10/18 04:33:53 ozaki-r Exp $");
66 
67 #ifdef _KERNEL_OPT
68 #include "opt_gateway.h"
69 #include "opt_inet.h"
70 #include "opt_inet6.h"
71 #include "opt_ipsec.h"
72 #include "opt_net_mpsafe.h"
73 #endif
74 
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/mbuf.h>
78 #include <sys/domain.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/errno.h>
83 #include <sys/time.h>
84 #include <sys/kernel.h>
85 #include <sys/syslog.h>
86 #include <sys/proc.h>
87 #include <sys/sysctl.h>
88 #include <sys/cprng.h>
89 #include <sys/percpu.h>
90 
91 #include <net/if.h>
92 #include <net/if_types.h>
93 #include <net/if_dl.h>
94 #include <net/route.h>
95 #include <net/pktqueue.h>
96 #include <net/pfil.h>
97 
98 #include <netinet/in.h>
99 #include <netinet/in_systm.h>
100 #ifdef INET
101 #include <netinet/ip.h>
102 #include <netinet/ip_var.h>
103 #include <netinet/ip_icmp.h>
104 #endif /* INET */
105 #include <netinet/ip6.h>
106 #include <netinet/portalgo.h>
107 #include <netinet6/in6_var.h>
108 #include <netinet6/ip6_var.h>
109 #include <netinet6/ip6_private.h>
110 #include <netinet6/in6_pcb.h>
111 #include <netinet/icmp6.h>
112 #include <netinet6/scope6_var.h>
113 #include <netinet6/in6_ifattach.h>
114 #include <netinet6/nd6.h>
115 
116 #ifdef IPSEC
117 #include <netipsec/ipsec.h>
118 #include <netipsec/ipsec6.h>
119 #include <netipsec/key.h>
120 #endif /* IPSEC */
121 
122 #include <netinet6/ip6protosw.h>
123 
124 #include "faith.h"
125 
126 extern struct domain inet6domain;
127 
128 u_char ip6_protox[IPPROTO_MAX];
129 pktqueue_t *ip6_pktq __read_mostly;
130 
131 pfil_head_t *inet6_pfil_hook;
132 
133 percpu_t *ip6stat_percpu;
134 
135 percpu_t *ip6_forward_rt_percpu __cacheline_aligned;
136 
137 static void ip6intr(void *);
138 static bool ip6_badaddr(struct ip6_hdr *);
139 static struct m_tag *ip6_setdstifaddr(struct mbuf *, const struct in6_ifaddr *);
140 
141 static int ip6_process_hopopts(struct mbuf *, u_int8_t *, int, u_int32_t *,
142     u_int32_t *);
143 static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
144 static void sysctl_net_inet6_ip6_setup(struct sysctllog **);
145 
146 #ifdef NET_MPSAFE
147 #define	SOFTNET_LOCK()		mutex_enter(softnet_lock)
148 #define	SOFTNET_UNLOCK()	mutex_exit(softnet_lock)
149 #else
150 #define	SOFTNET_LOCK()		KASSERT(mutex_owned(softnet_lock))
151 #define	SOFTNET_UNLOCK()	KASSERT(mutex_owned(softnet_lock))
152 #endif
153 
154 /*
155  * IP6 initialization: fill in IP6 protocol switch table.
156  * All protocols not implemented in kernel go to raw IP6 protocol handler.
157  */
158 void
159 ip6_init(void)
160 {
161 	const struct ip6protosw *pr;
162 	int i;
163 
164 	in6_init();
165 
166 	sysctl_net_inet6_ip6_setup(NULL);
167 	pr = (const struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
168 	if (pr == 0)
169 		panic("ip6_init");
170 	for (i = 0; i < IPPROTO_MAX; i++)
171 		ip6_protox[i] = pr - inet6sw;
172 	for (pr = (const struct ip6protosw *)inet6domain.dom_protosw;
173 	    pr < (const struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
174 		if (pr->pr_domain->dom_family == PF_INET6 &&
175 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
176 			ip6_protox[pr->pr_protocol] = pr - inet6sw;
177 
178 	ip6_pktq = pktq_create(IFQ_MAXLEN, ip6intr, NULL);
179 	KASSERT(ip6_pktq != NULL);
180 
181 	scope6_init();
182 	addrsel_policy_init();
183 	nd6_init();
184 	frag6_init();
185 	ip6_desync_factor = cprng_fast32() % MAX_TEMP_DESYNC_FACTOR;
186 
187 	in6_tmpaddrtimer_init();
188 #ifdef GATEWAY
189 	ip6flow_init(ip6_hashsize);
190 #endif
191 	/* Register our Packet Filter hook. */
192 	inet6_pfil_hook = pfil_head_create(PFIL_TYPE_AF, (void *)AF_INET6);
193 	KASSERT(inet6_pfil_hook != NULL);
194 
195 	ip6stat_percpu = percpu_alloc(sizeof(uint64_t) * IP6_NSTATS);
196 	ip6_forward_rt_percpu = rtcache_percpu_alloc();
197 }
198 
199 /*
200  * IP6 input interrupt handling. Just pass the packet to ip6_input.
201  */
202 static void
203 ip6intr(void *arg __unused)
204 {
205 	struct mbuf *m;
206 
207 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
208 	while ((m = pktq_dequeue(ip6_pktq)) != NULL) {
209 		struct psref psref;
210 		struct ifnet *rcvif = m_get_rcvif_psref(m, &psref);
211 
212 		if (rcvif == NULL) {
213 			m_freem(m);
214 			continue;
215 		}
216 		/*
217 		 * Drop the packet if IPv6 is disabled on the interface.
218 		 */
219 		if ((ND_IFINFO(rcvif)->flags & ND6_IFF_IFDISABLED)) {
220 			m_put_rcvif_psref(rcvif, &psref);
221 			m_freem(m);
222 			continue;
223 		}
224 		ip6_input(m, rcvif);
225 		m_put_rcvif_psref(rcvif, &psref);
226 	}
227 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
228 }
229 
230 void
231 ip6_input(struct mbuf *m, struct ifnet *rcvif)
232 {
233 	struct ip6_hdr *ip6;
234 	int hit, off = sizeof(struct ip6_hdr), nest;
235 	u_int32_t plen;
236 	u_int32_t rtalert = ~0;
237 	int nxt, ours = 0, rh_present = 0, frg_present;
238 	struct ifnet *deliverifp = NULL;
239 	int srcrt = 0;
240 	struct rtentry *rt = NULL;
241 	union {
242 		struct sockaddr		dst;
243 		struct sockaddr_in6	dst6;
244 	} u;
245 	struct route *ro;
246 
247 	KASSERT(rcvif != NULL);
248 
249 	/*
250 	 * make sure we don't have onion peering information into m_tag.
251 	 */
252 	ip6_delaux(m);
253 
254 	/*
255 	 * mbuf statistics
256 	 */
257 	if (m->m_flags & M_EXT) {
258 		if (m->m_next)
259 			IP6_STATINC(IP6_STAT_MEXT2M);
260 		else
261 			IP6_STATINC(IP6_STAT_MEXT1);
262 	} else {
263 #define M2MMAX	32
264 		if (m->m_next) {
265 			if (m->m_flags & M_LOOP)
266 			/*XXX*/	IP6_STATINC(IP6_STAT_M2M + lo0ifp->if_index);
267 			else if (rcvif->if_index < M2MMAX)
268 				IP6_STATINC(IP6_STAT_M2M + rcvif->if_index);
269 			else
270 				IP6_STATINC(IP6_STAT_M2M);
271 		} else
272 			IP6_STATINC(IP6_STAT_M1);
273 #undef M2MMAX
274 	}
275 
276 	in6_ifstat_inc(rcvif, ifs6_in_receive);
277 	IP6_STATINC(IP6_STAT_TOTAL);
278 
279 	/*
280 	 * If the IPv6 header is not aligned, slurp it up into a new
281 	 * mbuf with space for link headers, in the event we forward
282 	 * it.  Otherwise, if it is aligned, make sure the entire base
283 	 * IPv6 header is in the first mbuf of the chain.
284 	 */
285 	if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
286 		if ((m = m_copyup(m, sizeof(struct ip6_hdr),
287 		    (max_linkhdr + 3) & ~3)) == NULL) {
288 			/* XXXJRT new stat, please */
289 			IP6_STATINC(IP6_STAT_TOOSMALL);
290 			in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
291 			return;
292 		}
293 	} else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
294 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
295 			IP6_STATINC(IP6_STAT_TOOSMALL);
296 			in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
297 			return;
298 		}
299 	}
300 
301 	ip6 = mtod(m, struct ip6_hdr *);
302 
303 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
304 		IP6_STATINC(IP6_STAT_BADVERS);
305 		in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
306 		goto bad;
307 	}
308 
309 	if (ip6_badaddr(ip6)) {
310 		IP6_STATINC(IP6_STAT_BADSCOPE);
311 		in6_ifstat_inc(rcvif, ifs6_in_addrerr);
312 		goto bad;
313 	}
314 
315 	/*
316 	 * Assume that we can create a fast-forward IP flow entry
317 	 * based on this packet.
318 	 */
319 	m->m_flags |= M_CANFASTFWD;
320 
321 	/*
322 	 * Run through list of hooks for input packets.  If there are any
323 	 * filters which require that additional packets in the flow are
324 	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
325 	 * Note that filters must _never_ set this flag, as another filter
326 	 * in the list may have previously cleared it.
327 	 *
328 	 * Don't call hooks if the packet has already been processed by
329 	 * IPsec (encapsulated, tunnel mode).
330 	 */
331 #if defined(IPSEC)
332 	if (!ipsec_used || !ipsec_skip_pfil(m))
333 #else
334 	if (1)
335 #endif
336 	{
337 		struct in6_addr odst;
338 		int error;
339 
340 		odst = ip6->ip6_dst;
341 		error = pfil_run_hooks(inet6_pfil_hook, &m, rcvif, PFIL_IN);
342 		if (error != 0 || m == NULL) {
343 			IP6_STATINC(IP6_STAT_PFILDROP_IN);
344 			return;
345 		}
346 		if (m->m_len < sizeof(struct ip6_hdr)) {
347 			if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
348 				IP6_STATINC(IP6_STAT_TOOSMALL);
349 				in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
350 				return;
351 			}
352 		}
353 		ip6 = mtod(m, struct ip6_hdr *);
354 		srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
355 	}
356 
357 	IP6_STATINC(IP6_STAT_NXTHIST + ip6->ip6_nxt);
358 
359 #ifdef ALTQ
360 	if (altq_input != NULL) {
361 		SOFTNET_LOCK();
362 		if ((*altq_input)(m, AF_INET6) == 0) {
363 			SOFTNET_UNLOCK();
364 			/* packet is dropped by traffic conditioner */
365 			return;
366 		}
367 		SOFTNET_UNLOCK();
368 	}
369 #endif
370 
371 	/*
372 	 * Disambiguate address scope zones (if there is ambiguity).
373 	 * We first make sure that the original source or destination address
374 	 * is not in our internal form for scoped addresses.  Such addresses
375 	 * are not necessarily invalid spec-wise, but we cannot accept them due
376 	 * to the usage conflict.
377 	 * in6_setscope() then also checks and rejects the cases where src or
378 	 * dst are the loopback address and the receiving interface
379 	 * is not loopback.
380 	 */
381 	if (__predict_false(
382 	    m_makewritable(&m, 0, sizeof(struct ip6_hdr), M_DONTWAIT)))
383 		goto bad;
384 	ip6 = mtod(m, struct ip6_hdr *);
385 	if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
386 		IP6_STATINC(IP6_STAT_BADSCOPE);	/* XXX */
387 		goto bad;
388 	}
389 	if (in6_setscope(&ip6->ip6_src, rcvif, NULL) ||
390 	    in6_setscope(&ip6->ip6_dst, rcvif, NULL)) {
391 		IP6_STATINC(IP6_STAT_BADSCOPE);
392 		goto bad;
393 	}
394 
395 	ro = rtcache_percpu_getref(ip6_forward_rt_percpu);
396 
397 	/*
398 	 * Multicast check
399 	 */
400 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
401 		bool ingroup;
402 
403 		in6_ifstat_inc(rcvif, ifs6_in_mcast);
404 		/*
405 		 * See if we belong to the destination multicast group on the
406 		 * arrival interface.
407 		 */
408 		ingroup = in6_multi_group(&ip6->ip6_dst, rcvif);
409 		if (ingroup) {
410 			ours = 1;
411 		} else if (!ip6_mrouter) {
412 			uint64_t *ip6s = IP6_STAT_GETREF();
413 			ip6s[IP6_STAT_NOTMEMBER]++;
414 			ip6s[IP6_STAT_CANTFORWARD]++;
415 			IP6_STAT_PUTREF();
416 			in6_ifstat_inc(rcvif, ifs6_in_discard);
417 			goto bad_unref;
418 		}
419 		deliverifp = rcvif;
420 		goto hbhcheck;
421 	}
422 
423 	sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
424 
425 	/*
426 	 * Unicast check
427 	 */
428 	rt = rtcache_lookup2(ro, &u.dst, 1, &hit);
429 	if (hit)
430 		IP6_STATINC(IP6_STAT_FORWARD_CACHEHIT);
431 	else
432 		IP6_STATINC(IP6_STAT_FORWARD_CACHEMISS);
433 
434 	/*
435 	 * Accept the packet if the forwarding interface to the destination
436 	 * (according to the routing table) is the loopback interface,
437 	 * unless the associated route has a gateway.
438 	 *
439 	 * We don't explicitly match ip6_dst against an interface here. It
440 	 * is already done in rtcache_lookup2: rt->rt_ifp->if_type will be
441 	 * IFT_LOOP if the packet is for us.
442 	 *
443 	 * Note that this approach causes to accept a packet if there is a
444 	 * route to the loopback interface for the destination of the packet.
445 	 * But we think it's even useful in some situations, e.g. when using
446 	 * a special daemon which wants to intercept the packet.
447 	 */
448 	if (rt != NULL &&
449 	    (rt->rt_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
450 	    rt->rt_ifp->if_type == IFT_LOOP) {
451 		struct in6_ifaddr *ia6 = (struct in6_ifaddr *)rt->rt_ifa;
452 		int addrok;
453 
454 		if (ia6->ia6_flags & IN6_IFF_ANYCAST)
455 			m->m_flags |= M_ANYCAST6;
456 		/*
457 		 * packets to a tentative, duplicated, or somehow invalid
458 		 * address must not be accepted.
459 		 */
460 		if (ia6->ia6_flags & IN6_IFF_NOTREADY)
461 			addrok = 0;
462 		else if (ia6->ia6_flags & IN6_IFF_DETACHED &&
463 		    !IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src))
464 		{
465 			/* Allow internal traffic to DETACHED addresses */
466 			struct sockaddr_in6 sin6;
467 			int s;
468 
469 			memset(&sin6, 0, sizeof(sin6));
470 			sin6.sin6_family = AF_INET6;
471 			sin6.sin6_len = sizeof(sin6);
472 			sin6.sin6_addr = ip6->ip6_src;
473 			s = pserialize_read_enter();
474 			addrok = (ifa_ifwithaddr(sin6tosa(&sin6)) != NULL);
475 			pserialize_read_exit(s);
476 		} else
477 			addrok = 1;
478 		if (addrok) {
479 			/* this address is ready */
480 			ours = 1;
481 			deliverifp = ia6->ia_ifp;	/* correct? */
482 			goto hbhcheck;
483 		} else {
484 			/* address is not ready, so discard the packet. */
485 			char ip6bufs[INET6_ADDRSTRLEN];
486 			char ip6bufd[INET6_ADDRSTRLEN];
487 			nd6log(LOG_INFO, "packet to an unready address %s->%s\n",
488 			    IN6_PRINT(ip6bufs, &ip6->ip6_src),
489 			    IN6_PRINT(ip6bufd, &ip6->ip6_dst));
490 
491 			goto bad_unref;
492 		}
493 	}
494 
495 	/*
496 	 * FAITH (Firewall Aided Internet Translator)
497 	 */
498 #if defined(NFAITH) && 0 < NFAITH
499 	if (ip6_keepfaith) {
500 		if (rt != NULL && rt->rt_ifp != NULL &&
501 		    rt->rt_ifp->if_type == IFT_FAITH) {
502 			/* XXX do we need more sanity checks? */
503 			ours = 1;
504 			deliverifp = rt->rt_ifp; /* faith */
505 			goto hbhcheck;
506 		}
507 	}
508 #endif
509 
510 	/*
511 	 * Now there is no reason to process the packet if it's not our own
512 	 * and we're not a router.
513 	 */
514 	if (!ip6_forwarding) {
515 		IP6_STATINC(IP6_STAT_CANTFORWARD);
516 		in6_ifstat_inc(rcvif, ifs6_in_discard);
517 		goto bad_unref;
518 	}
519 
520 hbhcheck:
521 	/*
522 	 * Record address information into m_tag, if we don't have one yet.
523 	 * Note that we are unable to record it, if the address is not listed
524 	 * as our interface address (e.g. multicast addresses, addresses
525 	 * within FAITH prefixes and such).
526 	 */
527 	if (deliverifp && ip6_getdstifaddr(m) == NULL) {
528 		struct in6_ifaddr *ia6;
529 		int s = pserialize_read_enter();
530 
531 		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
532 		/* Depends on ip6_setdstifaddr never sleep */
533 		if (ia6 != NULL && ip6_setdstifaddr(m, ia6) == NULL) {
534 			/*
535 			 * XXX maybe we should drop the packet here,
536 			 * as we could not provide enough information
537 			 * to the upper layers.
538 			 */
539 		}
540 		pserialize_read_exit(s);
541 	}
542 
543 	/*
544 	 * Process Hop-by-Hop options header if it's contained.
545 	 * m may be modified in ip6_hopopts_input().
546 	 * If a JumboPayload option is included, plen will also be modified.
547 	 */
548 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
549 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
550 		struct ip6_hbh *hbh;
551 
552 		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
553 			/* m already freed */
554 			in6_ifstat_inc(rcvif, ifs6_in_discard);
555 			rtcache_unref(rt, ro);
556 			rtcache_percpu_putref(ip6_forward_rt_percpu);
557 			return;
558 		}
559 
560 		/* adjust pointer */
561 		ip6 = mtod(m, struct ip6_hdr *);
562 
563 		/*
564 		 * if the payload length field is 0 and the next header field
565 		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
566 		 * option MUST be included.
567 		 */
568 		if (ip6->ip6_plen == 0 && plen == 0) {
569 			/*
570 			 * Note that if a valid jumbo payload option is
571 			 * contained, ip6_hopopts_input() must set a valid
572 			 * (non-zero) payload length to the variable plen.
573 			 */
574 			IP6_STATINC(IP6_STAT_BADOPTIONS);
575 			in6_ifstat_inc(rcvif, ifs6_in_discard);
576 			in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
577 			icmp6_error(m, ICMP6_PARAM_PROB,
578 				    ICMP6_PARAMPROB_HEADER,
579 				    (char *)&ip6->ip6_plen - (char *)ip6);
580 			rtcache_unref(rt, ro);
581 			rtcache_percpu_putref(ip6_forward_rt_percpu);
582 			return;
583 		}
584 		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
585 			sizeof(struct ip6_hbh));
586 		if (hbh == NULL) {
587 			IP6_STATINC(IP6_STAT_TOOSHORT);
588 			rtcache_unref(rt, ro);
589 			rtcache_percpu_putref(ip6_forward_rt_percpu);
590 			return;
591 		}
592 		KASSERT(IP6_HDR_ALIGNED_P(hbh));
593 		nxt = hbh->ip6h_nxt;
594 
595 		/*
596 		 * accept the packet if a router alert option is included
597 		 * and we act as an IPv6 router.
598 		 */
599 		if (rtalert != ~0 && ip6_forwarding)
600 			ours = 1;
601 	} else
602 		nxt = ip6->ip6_nxt;
603 
604 	/*
605 	 * Check that the amount of data in the buffers is at least much as
606 	 * the IPv6 header would have us expect. Trim mbufs if longer than we
607 	 * expect. Drop packet if shorter than we expect.
608 	 */
609 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
610 		IP6_STATINC(IP6_STAT_TOOSHORT);
611 		in6_ifstat_inc(rcvif, ifs6_in_truncated);
612 		goto bad_unref;
613 	}
614 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
615 		if (m->m_len == m->m_pkthdr.len) {
616 			m->m_len = sizeof(struct ip6_hdr) + plen;
617 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
618 		} else
619 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
620 	}
621 
622 	/*
623 	 * Forward if desirable.
624 	 */
625 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
626 		/*
627 		 * If we are acting as a multicast router, all
628 		 * incoming multicast packets are passed to the
629 		 * kernel-level multicast forwarding function.
630 		 * The packet is returned (relatively) intact; if
631 		 * ip6_mforward() returns a non-zero value, the packet
632 		 * must be discarded, else it may be accepted below.
633 		 */
634 		if (ip6_mrouter != NULL) {
635 			int error;
636 
637 			SOFTNET_LOCK();
638 			error = ip6_mforward(ip6, rcvif, m);
639 			SOFTNET_UNLOCK();
640 
641 			if (error != 0) {
642 				rtcache_unref(rt, ro);
643 				rtcache_percpu_putref(ip6_forward_rt_percpu);
644 				IP6_STATINC(IP6_STAT_CANTFORWARD);
645 				goto bad;
646 			}
647 		}
648 		if (!ours)
649 			goto bad_unref;
650 	} else if (!ours) {
651 		rtcache_unref(rt, ro);
652 		rtcache_percpu_putref(ip6_forward_rt_percpu);
653 		ip6_forward(m, srcrt);
654 		return;
655 	}
656 
657 	ip6 = mtod(m, struct ip6_hdr *);
658 
659 	/*
660 	 * Malicious party may be able to use IPv4 mapped addr to confuse
661 	 * tcp/udp stack and bypass security checks (act as if it was from
662 	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
663 	 *
664 	 * For SIIT end node behavior, you may want to disable the check.
665 	 * However, you will  become vulnerable to attacks using IPv4 mapped
666 	 * source.
667 	 */
668 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
669 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
670 		IP6_STATINC(IP6_STAT_BADSCOPE);
671 		in6_ifstat_inc(rcvif, ifs6_in_addrerr);
672 		goto bad_unref;
673 	}
674 
675 #ifdef IFA_STATS
676 	if (deliverifp != NULL) {
677 		struct in6_ifaddr *ia6;
678 		int s = pserialize_read_enter();
679 		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
680 		if (ia6)
681 			ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
682 		pserialize_read_exit(s);
683 	}
684 #endif
685 	IP6_STATINC(IP6_STAT_DELIVERED);
686 	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
687 	nest = 0;
688 
689 	if (rt != NULL) {
690 		rtcache_unref(rt, ro);
691 		rt = NULL;
692 	}
693 	rtcache_percpu_putref(ip6_forward_rt_percpu);
694 
695 	rh_present = 0;
696 	frg_present = 0;
697 	while (nxt != IPPROTO_DONE) {
698 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
699 			IP6_STATINC(IP6_STAT_TOOMANYHDR);
700 			in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
701 			goto bad;
702 		}
703 
704 		M_VERIFY_PACKET(m);
705 
706 		/*
707 		 * protection against faulty packet - there should be
708 		 * more sanity checks in header chain processing.
709 		 */
710 		if (m->m_pkthdr.len < off) {
711 			IP6_STATINC(IP6_STAT_TOOSHORT);
712 			in6_ifstat_inc(rcvif, ifs6_in_truncated);
713 			goto bad;
714 		}
715 
716 		if (nxt == IPPROTO_ROUTING) {
717 			if (rh_present++) {
718 				in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
719 				IP6_STATINC(IP6_STAT_BADOPTIONS);
720 				goto bad;
721 			}
722 		} else if (nxt == IPPROTO_FRAGMENT) {
723 			if (frg_present++) {
724 				in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
725 				IP6_STATINC(IP6_STAT_BADOPTIONS);
726 				goto bad;
727 			}
728 		}
729 
730 #ifdef IPSEC
731 		if (ipsec_used) {
732 			/*
733 			 * Enforce IPsec policy checking if we are seeing last
734 			 * header. Note that we do not visit this with
735 			 * protocols with pcb layer code - like udp/tcp/raw ip.
736 			 */
737 			if ((inet6sw[ip6_protox[nxt]].pr_flags
738 			    & PR_LASTHDR) != 0) {
739 				int error;
740 
741 				error = ipsec_ip_input(m, false);
742 				if (error)
743 					goto bad;
744 			}
745 		}
746 #endif
747 
748 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
749 	}
750 	return;
751 
752 bad_unref:
753 	rtcache_unref(rt, ro);
754 	rtcache_percpu_putref(ip6_forward_rt_percpu);
755 bad:
756 	m_freem(m);
757 	return;
758 }
759 
760 static bool
761 ip6_badaddr(struct ip6_hdr *ip6)
762 {
763 	/* Check against address spoofing/corruption. */
764 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
765 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
766 		return true;
767 	}
768 
769 	/*
770 	 * The following check is not documented in specs.  A malicious
771 	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
772 	 * and bypass security checks (act as if it was from 127.0.0.1 by using
773 	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
774 	 *
775 	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
776 	 * support IPv4-less kernel compilation, we cannot support SIIT
777 	 * environment at all.  So, it makes more sense for us to reject any
778 	 * malicious packets for non-SIIT environment, than try to do a
779 	 * partial support for SIIT environment.
780 	 */
781 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
782 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
783 		return true;
784 	}
785 
786 	/*
787 	 * Reject packets with IPv4-compatible IPv6 addresses (RFC4291).
788 	 */
789 	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
790 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
791 		return true;
792 	}
793 
794 	return false;
795 }
796 
797 /*
798  * set/grab in6_ifaddr correspond to IPv6 destination address.
799  */
800 static struct m_tag *
801 ip6_setdstifaddr(struct mbuf *m, const struct in6_ifaddr *ia)
802 {
803 	struct m_tag *mtag;
804 	struct ip6aux *ip6a;
805 
806 	mtag = ip6_addaux(m);
807 	if (mtag == NULL)
808 		return NULL;
809 
810 	ip6a = (struct ip6aux *)(mtag + 1);
811 	if (in6_setscope(&ip6a->ip6a_src, ia->ia_ifp, &ip6a->ip6a_scope_id)) {
812 		IP6_STATINC(IP6_STAT_BADSCOPE);
813 		return NULL;
814 	}
815 
816 	ip6a->ip6a_src = ia->ia_addr.sin6_addr;
817 	ip6a->ip6a_flags = ia->ia6_flags;
818 	return mtag;
819 }
820 
821 const struct ip6aux *
822 ip6_getdstifaddr(struct mbuf *m)
823 {
824 	struct m_tag *mtag;
825 
826 	mtag = ip6_findaux(m);
827 	if (mtag != NULL)
828 		return (struct ip6aux *)(mtag + 1);
829 	else
830 		return NULL;
831 }
832 
833 /*
834  * Hop-by-Hop options header processing. If a valid jumbo payload option is
835  * included, the real payload length will be stored in plenp.
836  *
837  * rtalertp - XXX: should be stored more smart way
838  */
839 int
840 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
841 	struct mbuf **mp, int *offp)
842 {
843 	struct mbuf *m = *mp;
844 	int off = *offp, hbhlen;
845 	struct ip6_hbh *hbh;
846 
847 	/* validation of the length of the header */
848 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
849 	    sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
850 	if (hbh == NULL) {
851 		IP6_STATINC(IP6_STAT_TOOSHORT);
852 		return -1;
853 	}
854 	hbhlen = (hbh->ip6h_len + 1) << 3;
855 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
856 	    hbhlen);
857 	if (hbh == NULL) {
858 		IP6_STATINC(IP6_STAT_TOOSHORT);
859 		return -1;
860 	}
861 	KASSERT(IP6_HDR_ALIGNED_P(hbh));
862 	off += hbhlen;
863 	hbhlen -= sizeof(struct ip6_hbh);
864 
865 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
866 	    hbhlen, rtalertp, plenp) < 0)
867 		return -1;
868 
869 	*offp = off;
870 	*mp = m;
871 	return 0;
872 }
873 
874 /*
875  * Search header for all Hop-by-hop options and process each option.
876  * This function is separate from ip6_hopopts_input() in order to
877  * handle a case where the sending node itself process its hop-by-hop
878  * options header. In such a case, the function is called from ip6_output().
879  *
880  * The function assumes that hbh header is located right after the IPv6 header
881  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
882  * opthead + hbhlen is located in continuous memory region.
883  */
884 static int
885 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
886 	u_int32_t *rtalertp, u_int32_t *plenp)
887 {
888 	struct ip6_hdr *ip6;
889 	int optlen = 0;
890 	u_int8_t *opt = opthead;
891 	u_int16_t rtalert_val;
892 	u_int32_t jumboplen;
893 	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
894 
895 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
896 		switch (*opt) {
897 		case IP6OPT_PAD1:
898 			optlen = 1;
899 			break;
900 		case IP6OPT_PADN:
901 			if (hbhlen < IP6OPT_MINLEN) {
902 				IP6_STATINC(IP6_STAT_TOOSMALL);
903 				goto bad;
904 			}
905 			optlen = *(opt + 1) + 2;
906 			break;
907 		case IP6OPT_RTALERT:
908 			/* XXX may need check for alignment */
909 			if (hbhlen < IP6OPT_RTALERT_LEN) {
910 				IP6_STATINC(IP6_STAT_TOOSMALL);
911 				goto bad;
912 			}
913 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
914 				/* XXX stat */
915 				icmp6_error(m, ICMP6_PARAM_PROB,
916 				    ICMP6_PARAMPROB_HEADER,
917 				    erroff + opt + 1 - opthead);
918 				return (-1);
919 			}
920 			optlen = IP6OPT_RTALERT_LEN;
921 			memcpy((void *)&rtalert_val, (void *)(opt + 2), 2);
922 			*rtalertp = ntohs(rtalert_val);
923 			break;
924 		case IP6OPT_JUMBO:
925 			/* XXX may need check for alignment */
926 			if (hbhlen < IP6OPT_JUMBO_LEN) {
927 				IP6_STATINC(IP6_STAT_TOOSMALL);
928 				goto bad;
929 			}
930 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
931 				/* XXX stat */
932 				icmp6_error(m, ICMP6_PARAM_PROB,
933 				    ICMP6_PARAMPROB_HEADER,
934 				    erroff + opt + 1 - opthead);
935 				return (-1);
936 			}
937 			optlen = IP6OPT_JUMBO_LEN;
938 
939 			/*
940 			 * IPv6 packets that have non 0 payload length
941 			 * must not contain a jumbo payload option.
942 			 */
943 			ip6 = mtod(m, struct ip6_hdr *);
944 			if (ip6->ip6_plen) {
945 				IP6_STATINC(IP6_STAT_BADOPTIONS);
946 				icmp6_error(m, ICMP6_PARAM_PROB,
947 				    ICMP6_PARAMPROB_HEADER,
948 				    erroff + opt - opthead);
949 				return (-1);
950 			}
951 
952 			/*
953 			 * We may see jumbolen in unaligned location, so
954 			 * we'd need to perform memcpy().
955 			 */
956 			memcpy(&jumboplen, opt + 2, sizeof(jumboplen));
957 			jumboplen = (u_int32_t)htonl(jumboplen);
958 
959 #if 1
960 			/*
961 			 * if there are multiple jumbo payload options,
962 			 * *plenp will be non-zero and the packet will be
963 			 * rejected.
964 			 * the behavior may need some debate in ipngwg -
965 			 * multiple options does not make sense, however,
966 			 * there's no explicit mention in specification.
967 			 */
968 			if (*plenp != 0) {
969 				IP6_STATINC(IP6_STAT_BADOPTIONS);
970 				icmp6_error(m, ICMP6_PARAM_PROB,
971 				    ICMP6_PARAMPROB_HEADER,
972 				    erroff + opt + 2 - opthead);
973 				return (-1);
974 			}
975 #endif
976 
977 			/*
978 			 * jumbo payload length must be larger than 65535.
979 			 */
980 			if (jumboplen <= IPV6_MAXPACKET) {
981 				IP6_STATINC(IP6_STAT_BADOPTIONS);
982 				icmp6_error(m, ICMP6_PARAM_PROB,
983 				    ICMP6_PARAMPROB_HEADER,
984 				    erroff + opt + 2 - opthead);
985 				return (-1);
986 			}
987 			*plenp = jumboplen;
988 
989 			break;
990 		default:		/* unknown option */
991 			if (hbhlen < IP6OPT_MINLEN) {
992 				IP6_STATINC(IP6_STAT_TOOSMALL);
993 				goto bad;
994 			}
995 			optlen = ip6_unknown_opt(opt, m,
996 			    erroff + opt - opthead);
997 			if (optlen == -1)
998 				return (-1);
999 			optlen += 2;
1000 			break;
1001 		}
1002 	}
1003 
1004 	return (0);
1005 
1006   bad:
1007 	m_freem(m);
1008 	return (-1);
1009 }
1010 
1011 /*
1012  * Unknown option processing.
1013  * The third argument `off' is the offset from the IPv6 header to the option,
1014  * which is necessary if the IPv6 header the and option header and IPv6 header
1015  * is not continuous in order to return an ICMPv6 error.
1016  */
1017 int
1018 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1019 {
1020 	struct ip6_hdr *ip6;
1021 
1022 	switch (IP6OPT_TYPE(*optp)) {
1023 	case IP6OPT_TYPE_SKIP: /* ignore the option */
1024 		return ((int)*(optp + 1));
1025 	case IP6OPT_TYPE_DISCARD:	/* silently discard */
1026 		m_freem(m);
1027 		return (-1);
1028 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1029 		IP6_STATINC(IP6_STAT_BADOPTIONS);
1030 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1031 		return (-1);
1032 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1033 		IP6_STATINC(IP6_STAT_BADOPTIONS);
1034 		ip6 = mtod(m, struct ip6_hdr *);
1035 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1036 		    (m->m_flags & (M_BCAST|M_MCAST)))
1037 			m_freem(m);
1038 		else
1039 			icmp6_error(m, ICMP6_PARAM_PROB,
1040 				    ICMP6_PARAMPROB_OPTION, off);
1041 		return (-1);
1042 	}
1043 
1044 	m_freem(m);		/* XXX: NOTREACHED */
1045 	return (-1);
1046 }
1047 
1048 void
1049 ip6_savecontrol(struct in6pcb *in6p, struct mbuf **mp,
1050 	struct ip6_hdr *ip6, struct mbuf *m)
1051 {
1052 	struct socket *so = in6p->in6p_socket;
1053 #ifdef RFC2292
1054 #define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1055 #else
1056 #define IS2292(x, y)	(y)
1057 #endif
1058 
1059 	if (SOOPT_TIMESTAMP(so->so_options))
1060 		mp = sbsavetimestamp(so->so_options, mp);
1061 
1062 	/* some OSes call this logic with IPv4 packet, for SO_TIMESTAMP */
1063 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
1064 		return;
1065 
1066 	/* RFC 2292 sec. 5 */
1067 	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1068 		struct in6_pktinfo pi6;
1069 
1070 		memcpy(&pi6.ipi6_addr, &ip6->ip6_dst, sizeof(struct in6_addr));
1071 		in6_clearscope(&pi6.ipi6_addr);	/* XXX */
1072 		pi6.ipi6_ifindex = m->m_pkthdr.rcvif_index;
1073 		*mp = sbcreatecontrol(&pi6, sizeof(pi6),
1074 		    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1075 		if (*mp)
1076 			mp = &(*mp)->m_next;
1077 	}
1078 
1079 	if (in6p->in6p_flags & IN6P_HOPLIMIT) {
1080 		int hlim = ip6->ip6_hlim & 0xff;
1081 
1082 		*mp = sbcreatecontrol(&hlim, sizeof(hlim),
1083 		    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1084 		if (*mp)
1085 			mp = &(*mp)->m_next;
1086 	}
1087 
1088 	if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1089 		u_int32_t flowinfo;
1090 		int tclass;
1091 
1092 		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1093 		flowinfo >>= 20;
1094 
1095 		tclass = flowinfo & 0xff;
1096 		*mp = sbcreatecontrol(&tclass, sizeof(tclass),
1097 		    IPV6_TCLASS, IPPROTO_IPV6);
1098 
1099 		if (*mp)
1100 			mp = &(*mp)->m_next;
1101 	}
1102 
1103 	/*
1104 	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
1105 	 * privilege for the option (see ip6_ctloutput), but it might be too
1106 	 * strict, since there might be some hop-by-hop options which can be
1107 	 * returned to normal user.
1108 	 * See also RFC3542 section 8 (or RFC2292 section 6).
1109 	 */
1110 	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1111 		/*
1112 		 * Check if a hop-by-hop options header is contatined in the
1113 		 * received packet, and if so, store the options as ancillary
1114 		 * data. Note that a hop-by-hop options header must be
1115 		 * just after the IPv6 header, which fact is assured through
1116 		 * the IPv6 input processing.
1117 		 */
1118 		struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
1119 		if (xip6->ip6_nxt == IPPROTO_HOPOPTS) {
1120 			struct ip6_hbh *hbh;
1121 			int hbhlen;
1122 			struct mbuf *ext;
1123 
1124 			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1125 			    xip6->ip6_nxt);
1126 			if (ext == NULL) {
1127 				IP6_STATINC(IP6_STAT_TOOSHORT);
1128 				return;
1129 			}
1130 			hbh = mtod(ext, struct ip6_hbh *);
1131 			hbhlen = (hbh->ip6h_len + 1) << 3;
1132 			if (hbhlen != ext->m_len) {
1133 				m_freem(ext);
1134 				IP6_STATINC(IP6_STAT_TOOSHORT);
1135 				return;
1136 			}
1137 
1138 			/*
1139 			 * XXX: We copy whole the header even if a jumbo
1140 			 * payload option is included, which option is to
1141 			 * be removed before returning in the RFC 2292.
1142 			 * Note: this constraint is removed in RFC3542.
1143 			 */
1144 			*mp = sbcreatecontrol(hbh, hbhlen,
1145 			    IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1146 			    IPPROTO_IPV6);
1147 			if (*mp)
1148 				mp = &(*mp)->m_next;
1149 			m_freem(ext);
1150 		}
1151 	}
1152 
1153 	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1154 	if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
1155 		struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
1156 		int nxt = xip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1157 
1158 		/*
1159 		 * Search for destination options headers or routing
1160 		 * header(s) through the header chain, and stores each
1161 		 * header as ancillary data.
1162 		 * Note that the order of the headers remains in
1163 		 * the chain of ancillary data.
1164 		 */
1165 		for (;;) {	/* is explicit loop prevention necessary? */
1166 			struct ip6_ext *ip6e = NULL;
1167 			int elen;
1168 			struct mbuf *ext = NULL;
1169 
1170 			/*
1171 			 * if it is not an extension header, don't try to
1172 			 * pull it from the chain.
1173 			 */
1174 			switch (nxt) {
1175 			case IPPROTO_DSTOPTS:
1176 			case IPPROTO_ROUTING:
1177 			case IPPROTO_HOPOPTS:
1178 			case IPPROTO_AH: /* is it possible? */
1179 				break;
1180 			default:
1181 				goto loopend;
1182 			}
1183 
1184 			ext = ip6_pullexthdr(m, off, nxt);
1185 			if (ext == NULL) {
1186 				IP6_STATINC(IP6_STAT_TOOSHORT);
1187 				return;
1188 			}
1189 			ip6e = mtod(ext, struct ip6_ext *);
1190 			if (nxt == IPPROTO_AH)
1191 				elen = (ip6e->ip6e_len + 2) << 2;
1192 			else
1193 				elen = (ip6e->ip6e_len + 1) << 3;
1194 			if (elen != ext->m_len) {
1195 				m_freem(ext);
1196 				IP6_STATINC(IP6_STAT_TOOSHORT);
1197 				return;
1198 			}
1199 			KASSERT(IP6_HDR_ALIGNED_P(ip6e));
1200 
1201 			switch (nxt) {
1202 			case IPPROTO_DSTOPTS:
1203 				if (!(in6p->in6p_flags & IN6P_DSTOPTS))
1204 					break;
1205 
1206 				*mp = sbcreatecontrol(ip6e, elen,
1207 				    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1208 				    IPPROTO_IPV6);
1209 				if (*mp)
1210 					mp = &(*mp)->m_next;
1211 				break;
1212 
1213 			case IPPROTO_ROUTING:
1214 				if (!(in6p->in6p_flags & IN6P_RTHDR))
1215 					break;
1216 
1217 				*mp = sbcreatecontrol(ip6e, elen,
1218 				    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1219 				    IPPROTO_IPV6);
1220 				if (*mp)
1221 					mp = &(*mp)->m_next;
1222 				break;
1223 
1224 			case IPPROTO_HOPOPTS:
1225 			case IPPROTO_AH: /* is it possible? */
1226 				break;
1227 
1228 			default:
1229 				/*
1230 			 	 * other cases have been filtered in the above.
1231 				 * none will visit this case.  here we supply
1232 				 * the code just in case (nxt overwritten or
1233 				 * other cases).
1234 				 */
1235 				m_freem(ext);
1236 				goto loopend;
1237 
1238 			}
1239 
1240 			/* proceed with the next header. */
1241 			off += elen;
1242 			nxt = ip6e->ip6e_nxt;
1243 			ip6e = NULL;
1244 			m_freem(ext);
1245 			ext = NULL;
1246 		}
1247 	  loopend:
1248 	  	;
1249 	}
1250 }
1251 #undef IS2292
1252 
1253 
1254 void
1255 ip6_notify_pmtu(struct in6pcb *in6p, const struct sockaddr_in6 *dst,
1256     uint32_t *mtu)
1257 {
1258 	struct socket *so;
1259 	struct mbuf *m_mtu;
1260 	struct ip6_mtuinfo mtuctl;
1261 
1262 	so = in6p->in6p_socket;
1263 
1264 	if (mtu == NULL)
1265 		return;
1266 
1267 	KASSERT(so != NULL);
1268 
1269 	memset(&mtuctl, 0, sizeof(mtuctl));	/* zero-clear for safety */
1270 	mtuctl.ip6m_mtu = *mtu;
1271 	mtuctl.ip6m_addr = *dst;
1272 	if (sa6_recoverscope(&mtuctl.ip6m_addr))
1273 		return;
1274 
1275 	if ((m_mtu = sbcreatecontrol(&mtuctl, sizeof(mtuctl),
1276 	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1277 		return;
1278 
1279 	if (sbappendaddr(&so->so_rcv, (const struct sockaddr *)dst, NULL, m_mtu)
1280 	    == 0) {
1281 		soroverflow(so);
1282 		m_freem(m_mtu);
1283 	} else
1284 		sorwakeup(so);
1285 
1286 	return;
1287 }
1288 
1289 /*
1290  * pull single extension header from mbuf chain.  returns single mbuf that
1291  * contains the result, or NULL on error.
1292  */
1293 static struct mbuf *
1294 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1295 {
1296 	struct ip6_ext ip6e;
1297 	size_t elen;
1298 	struct mbuf *n;
1299 
1300 	m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1301 	if (nxt == IPPROTO_AH)
1302 		elen = (ip6e.ip6e_len + 2) << 2;
1303 	else
1304 		elen = (ip6e.ip6e_len + 1) << 3;
1305 
1306 	MGET(n, M_DONTWAIT, MT_DATA);
1307 	if (n && elen >= MLEN) {
1308 		MCLGET(n, M_DONTWAIT);
1309 		if ((n->m_flags & M_EXT) == 0) {
1310 			m_free(n);
1311 			n = NULL;
1312 		}
1313 	}
1314 	if (!n)
1315 		return NULL;
1316 
1317 	n->m_len = 0;
1318 	if (elen >= M_TRAILINGSPACE(n)) {
1319 		m_free(n);
1320 		return NULL;
1321 	}
1322 
1323 	m_copydata(m, off, elen, mtod(n, void *));
1324 	n->m_len = elen;
1325 	return n;
1326 }
1327 
1328 /*
1329  * Get offset to the previous header followed by the header
1330  * currently processed.
1331  */
1332 int
1333 ip6_get_prevhdr(struct mbuf *m, int off)
1334 {
1335 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1336 
1337 	if (off == sizeof(struct ip6_hdr)) {
1338 		return offsetof(struct ip6_hdr, ip6_nxt);
1339 	} else if (off < sizeof(struct ip6_hdr)) {
1340 		panic("%s: off < sizeof(struct ip6_hdr)", __func__);
1341 	} else {
1342 		int len, nlen, nxt;
1343 		struct ip6_ext ip6e;
1344 
1345 		nxt = ip6->ip6_nxt;
1346 		len = sizeof(struct ip6_hdr);
1347 		nlen = 0;
1348 		while (len < off) {
1349 			m_copydata(m, len, sizeof(ip6e), &ip6e);
1350 
1351 			switch (nxt) {
1352 			case IPPROTO_FRAGMENT:
1353 				nlen = sizeof(struct ip6_frag);
1354 				break;
1355 			case IPPROTO_AH:
1356 				nlen = (ip6e.ip6e_len + 2) << 2;
1357 				break;
1358 			default:
1359 				nlen = (ip6e.ip6e_len + 1) << 3;
1360 				break;
1361 			}
1362 			len += nlen;
1363 			nxt = ip6e.ip6e_nxt;
1364 		}
1365 
1366 		return (len - nlen);
1367 	}
1368 }
1369 
1370 /*
1371  * get next header offset.  m will be retained.
1372  */
1373 int
1374 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
1375 {
1376 	struct ip6_hdr ip6;
1377 	struct ip6_ext ip6e;
1378 	struct ip6_frag fh;
1379 
1380 	/* just in case */
1381 	if (m == NULL)
1382 		panic("%s: m == NULL", __func__);
1383 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1384 		return -1;
1385 
1386 	switch (proto) {
1387 	case IPPROTO_IPV6:
1388 		/* do not chase beyond intermediate IPv6 headers */
1389 		if (off != 0)
1390 			return -1;
1391 		if (m->m_pkthdr.len < off + sizeof(ip6))
1392 			return -1;
1393 		m_copydata(m, off, sizeof(ip6), (void *)&ip6);
1394 		if (nxtp)
1395 			*nxtp = ip6.ip6_nxt;
1396 		off += sizeof(ip6);
1397 		return off;
1398 
1399 	case IPPROTO_FRAGMENT:
1400 		/*
1401 		 * terminate parsing if it is not the first fragment,
1402 		 * it does not make sense to parse through it.
1403 		 */
1404 		if (m->m_pkthdr.len < off + sizeof(fh))
1405 			return -1;
1406 		m_copydata(m, off, sizeof(fh), (void *)&fh);
1407 		if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0)
1408 			return -1;
1409 		if (nxtp)
1410 			*nxtp = fh.ip6f_nxt;
1411 		off += sizeof(struct ip6_frag);
1412 		return off;
1413 
1414 	case IPPROTO_AH:
1415 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1416 			return -1;
1417 		m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1418 		if (nxtp)
1419 			*nxtp = ip6e.ip6e_nxt;
1420 		off += (ip6e.ip6e_len + 2) << 2;
1421 		if (m->m_pkthdr.len < off)
1422 			return -1;
1423 		return off;
1424 
1425 	case IPPROTO_HOPOPTS:
1426 	case IPPROTO_ROUTING:
1427 	case IPPROTO_DSTOPTS:
1428 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1429 			return -1;
1430 		m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1431 		if (nxtp)
1432 			*nxtp = ip6e.ip6e_nxt;
1433 		off += (ip6e.ip6e_len + 1) << 3;
1434 		if (m->m_pkthdr.len < off)
1435 			return -1;
1436 		return off;
1437 
1438 	case IPPROTO_NONE:
1439 	case IPPROTO_ESP:
1440 	case IPPROTO_IPCOMP:
1441 		/* give up */
1442 		return -1;
1443 
1444 	default:
1445 		return -1;
1446 	}
1447 }
1448 
1449 /*
1450  * get offset for the last header in the chain.  m will be kept untainted.
1451  */
1452 int
1453 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
1454 {
1455 	int newoff;
1456 	int nxt;
1457 
1458 	if (!nxtp) {
1459 		nxt = -1;
1460 		nxtp = &nxt;
1461 	}
1462 	for (;;) {
1463 		newoff = ip6_nexthdr(m, off, proto, nxtp);
1464 		if (newoff < 0)
1465 			return off;
1466 		else if (newoff < off)
1467 			return -1;	/* invalid */
1468 		else if (newoff == off)
1469 			return newoff;
1470 
1471 		off = newoff;
1472 		proto = *nxtp;
1473 	}
1474 }
1475 
1476 struct m_tag *
1477 ip6_addaux(struct mbuf *m)
1478 {
1479 	struct m_tag *mtag;
1480 
1481 	mtag = m_tag_find(m, PACKET_TAG_INET6);
1482 	if (!mtag) {
1483 		mtag = m_tag_get(PACKET_TAG_INET6, sizeof(struct ip6aux),
1484 		    M_NOWAIT);
1485 		if (mtag) {
1486 			m_tag_prepend(m, mtag);
1487 			memset(mtag + 1, 0, sizeof(struct ip6aux));
1488 		}
1489 	}
1490 	return mtag;
1491 }
1492 
1493 struct m_tag *
1494 ip6_findaux(struct mbuf *m)
1495 {
1496 	struct m_tag *mtag;
1497 
1498 	mtag = m_tag_find(m, PACKET_TAG_INET6);
1499 	return mtag;
1500 }
1501 
1502 void
1503 ip6_delaux(struct mbuf *m)
1504 {
1505 	struct m_tag *mtag;
1506 
1507 	mtag = m_tag_find(m, PACKET_TAG_INET6);
1508 	if (mtag)
1509 		m_tag_delete(m, mtag);
1510 }
1511 
1512 /*
1513  * System control for IP6
1514  */
1515 
1516 const u_char inet6ctlerrmap[PRC_NCMDS] = {
1517 	0,		0,		0,		0,
1518 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1519 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1520 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1521 	0,		0,		0,		0,
1522 	ENOPROTOOPT
1523 };
1524 
1525 extern int sysctl_net_inet6_addrctlpolicy(SYSCTLFN_ARGS);
1526 
1527 static int
1528 sysctl_net_inet6_ip6_stats(SYSCTLFN_ARGS)
1529 {
1530 
1531 	return (NETSTAT_SYSCTL(ip6stat_percpu, IP6_NSTATS));
1532 }
1533 
1534 static int
1535 sysctl_net_inet6_ip6_temppltime(SYSCTLFN_ARGS)
1536 {
1537 	int error;
1538 	uint32_t pltime;
1539 	struct sysctlnode node;
1540 
1541 	node = *rnode;
1542 	node.sysctl_data = &pltime;
1543 	pltime = ip6_temp_preferred_lifetime;
1544 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1545 	if (error || newp == NULL)
1546 		return error;
1547 
1548 	if (pltime <= (MAX_TEMP_DESYNC_FACTOR + TEMPADDR_REGEN_ADVANCE))
1549 		return EINVAL;
1550 
1551 	ip6_temp_preferred_lifetime = pltime;
1552 
1553 	in6_tmpaddrtimer_schedule();
1554 
1555 	return 0;
1556 }
1557 
1558 static void
1559 sysctl_net_inet6_ip6_setup(struct sysctllog **clog)
1560 {
1561 
1562 	sysctl_createv(clog, 0, NULL, NULL,
1563 		       CTLFLAG_PERMANENT,
1564 		       CTLTYPE_NODE, "inet6",
1565 		       SYSCTL_DESCR("PF_INET6 related settings"),
1566 		       NULL, 0, NULL, 0,
1567 		       CTL_NET, PF_INET6, CTL_EOL);
1568 	sysctl_createv(clog, 0, NULL, NULL,
1569 		       CTLFLAG_PERMANENT,
1570 		       CTLTYPE_NODE, "ip6",
1571 		       SYSCTL_DESCR("IPv6 related settings"),
1572 		       NULL, 0, NULL, 0,
1573 		       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_EOL);
1574 
1575 	sysctl_createv(clog, 0, NULL, NULL,
1576 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1577 		       CTLTYPE_INT, "forwarding",
1578 		       SYSCTL_DESCR("Enable forwarding of INET6 datagrams"),
1579 		       NULL, 0, &ip6_forwarding, 0,
1580 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1581 		       IPV6CTL_FORWARDING, CTL_EOL);
1582 	sysctl_createv(clog, 0, NULL, NULL,
1583 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1584 		       CTLTYPE_INT, "redirect",
1585 		       SYSCTL_DESCR("Enable sending of ICMPv6 redirect messages"),
1586 		       NULL, 0, &ip6_sendredirects, 0,
1587 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1588 		       IPV6CTL_SENDREDIRECTS, CTL_EOL);
1589 	sysctl_createv(clog, 0, NULL, NULL,
1590 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1591 		       CTLTYPE_INT, "hlim",
1592 		       SYSCTL_DESCR("Hop limit for an INET6 datagram"),
1593 		       NULL, 0, &ip6_defhlim, 0,
1594 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1595 		       IPV6CTL_DEFHLIM, CTL_EOL);
1596 	sysctl_createv(clog, 0, NULL, NULL,
1597 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1598 		       CTLTYPE_INT, "maxfragpackets",
1599 		       SYSCTL_DESCR("Maximum number of fragments to buffer "
1600 				    "for reassembly"),
1601 		       NULL, 0, &ip6_maxfragpackets, 0,
1602 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1603 		       IPV6CTL_MAXFRAGPACKETS, CTL_EOL);
1604 	sysctl_createv(clog, 0, NULL, NULL,
1605 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1606 		       CTLTYPE_INT, "accept_rtadv",
1607 		       SYSCTL_DESCR("Accept router advertisements"),
1608 		       NULL, 0, &ip6_accept_rtadv, 0,
1609 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1610 		       IPV6CTL_ACCEPT_RTADV, CTL_EOL);
1611 	sysctl_createv(clog, 0, NULL, NULL,
1612 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1613 		       CTLTYPE_INT, "rtadv_maxroutes",
1614 		       SYSCTL_DESCR("Maximum number of routes accepted via router advertisements"),
1615 		       NULL, 0, &ip6_rtadv_maxroutes, 0,
1616 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1617 		       IPV6CTL_RTADV_MAXROUTES, CTL_EOL);
1618 	sysctl_createv(clog, 0, NULL, NULL,
1619 		       CTLFLAG_PERMANENT,
1620 		       CTLTYPE_INT, "rtadv_numroutes",
1621 		       SYSCTL_DESCR("Current number of routes accepted via router advertisements"),
1622 		       NULL, 0, &nd6_numroutes, 0,
1623 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1624 		       IPV6CTL_RTADV_NUMROUTES, CTL_EOL);
1625 	sysctl_createv(clog, 0, NULL, NULL,
1626 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1627 		       CTLTYPE_INT, "keepfaith",
1628 		       SYSCTL_DESCR("Activate faith interface"),
1629 		       NULL, 0, &ip6_keepfaith, 0,
1630 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1631 		       IPV6CTL_KEEPFAITH, CTL_EOL);
1632 	sysctl_createv(clog, 0, NULL, NULL,
1633 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1634 		       CTLTYPE_INT, "log_interval",
1635 		       SYSCTL_DESCR("Minimum interval between logging "
1636 				    "unroutable packets"),
1637 		       NULL, 0, &ip6_log_interval, 0,
1638 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1639 		       IPV6CTL_LOG_INTERVAL, CTL_EOL);
1640 	sysctl_createv(clog, 0, NULL, NULL,
1641 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1642 		       CTLTYPE_INT, "hdrnestlimit",
1643 		       SYSCTL_DESCR("Maximum number of nested IPv6 headers"),
1644 		       NULL, 0, &ip6_hdrnestlimit, 0,
1645 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1646 		       IPV6CTL_HDRNESTLIMIT, CTL_EOL);
1647 	sysctl_createv(clog, 0, NULL, NULL,
1648 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1649 		       CTLTYPE_INT, "dad_count",
1650 		       SYSCTL_DESCR("Number of Duplicate Address Detection "
1651 				    "probes to send"),
1652 		       NULL, 0, &ip6_dad_count, 0,
1653 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1654 		       IPV6CTL_DAD_COUNT, CTL_EOL);
1655 	sysctl_createv(clog, 0, NULL, NULL,
1656 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1657 		       CTLTYPE_INT, "auto_flowlabel",
1658 		       SYSCTL_DESCR("Assign random IPv6 flow labels"),
1659 		       NULL, 0, &ip6_auto_flowlabel, 0,
1660 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1661 		       IPV6CTL_AUTO_FLOWLABEL, CTL_EOL);
1662 	sysctl_createv(clog, 0, NULL, NULL,
1663 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1664 		       CTLTYPE_INT, "defmcasthlim",
1665 		       SYSCTL_DESCR("Default multicast hop limit"),
1666 		       NULL, 0, &ip6_defmcasthlim, 0,
1667 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1668 		       IPV6CTL_DEFMCASTHLIM, CTL_EOL);
1669 	sysctl_createv(clog, 0, NULL, NULL,
1670 		       CTLFLAG_PERMANENT,
1671 		       CTLTYPE_STRING, "kame_version",
1672 		       SYSCTL_DESCR("KAME Version"),
1673 		       NULL, 0, __UNCONST(__KAME_VERSION), 0,
1674 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1675 		       IPV6CTL_KAME_VERSION, CTL_EOL);
1676 	sysctl_createv(clog, 0, NULL, NULL,
1677 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1678 		       CTLTYPE_INT, "use_deprecated",
1679 		       SYSCTL_DESCR("Allow use of deprecated addresses as "
1680 				    "source addresses"),
1681 		       NULL, 0, &ip6_use_deprecated, 0,
1682 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1683 		       IPV6CTL_USE_DEPRECATED, CTL_EOL);
1684 	sysctl_createv(clog, 0, NULL, NULL,
1685 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1686 		       CTLTYPE_INT, "rr_prune", NULL,
1687 		       NULL, 0, &ip6_rr_prune, 0,
1688 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1689 		       IPV6CTL_RR_PRUNE, CTL_EOL);
1690 	sysctl_createv(clog, 0, NULL, NULL,
1691 		       CTLFLAG_PERMANENT
1692 #ifndef INET6_BINDV6ONLY
1693 		       |CTLFLAG_READWRITE,
1694 #endif
1695 		       CTLTYPE_INT, "v6only",
1696 		       SYSCTL_DESCR("Disallow PF_INET6 sockets from connecting "
1697 				    "to PF_INET sockets"),
1698 		       NULL, 0, &ip6_v6only, 0,
1699 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1700 		       IPV6CTL_V6ONLY, CTL_EOL);
1701 	sysctl_createv(clog, 0, NULL, NULL,
1702 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1703 		       CTLTYPE_INT, "anonportmin",
1704 		       SYSCTL_DESCR("Lowest ephemeral port number to assign"),
1705 		       sysctl_net_inet_ip_ports, 0, &ip6_anonportmin, 0,
1706 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1707 		       IPV6CTL_ANONPORTMIN, CTL_EOL);
1708 	sysctl_createv(clog, 0, NULL, NULL,
1709 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1710 		       CTLTYPE_INT, "anonportmax",
1711 		       SYSCTL_DESCR("Highest ephemeral port number to assign"),
1712 		       sysctl_net_inet_ip_ports, 0, &ip6_anonportmax, 0,
1713 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1714 		       IPV6CTL_ANONPORTMAX, CTL_EOL);
1715 #ifndef IPNOPRIVPORTS
1716 	sysctl_createv(clog, 0, NULL, NULL,
1717 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1718 		       CTLTYPE_INT, "lowportmin",
1719 		       SYSCTL_DESCR("Lowest privileged ephemeral port number "
1720 				    "to assign"),
1721 		       sysctl_net_inet_ip_ports, 0, &ip6_lowportmin, 0,
1722 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1723 		       IPV6CTL_LOWPORTMIN, CTL_EOL);
1724 	sysctl_createv(clog, 0, NULL, NULL,
1725 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1726 		       CTLTYPE_INT, "lowportmax",
1727 		       SYSCTL_DESCR("Highest privileged ephemeral port number "
1728 				    "to assign"),
1729 		       sysctl_net_inet_ip_ports, 0, &ip6_lowportmax, 0,
1730 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1731 		       IPV6CTL_LOWPORTMAX, CTL_EOL);
1732 #endif /* IPNOPRIVPORTS */
1733 	sysctl_createv(clog, 0, NULL, NULL,
1734 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1735 		       CTLTYPE_INT, "auto_linklocal",
1736 		       SYSCTL_DESCR("Default value of per-interface flag for "
1737 		                    "adding an IPv6 link-local address to "
1738 				    "interfaces when attached"),
1739 		       NULL, 0, &ip6_auto_linklocal, 0,
1740 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1741 		       IPV6CTL_AUTO_LINKLOCAL, CTL_EOL);
1742 	sysctl_createv(clog, 0, NULL, NULL,
1743 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
1744 		       CTLTYPE_STRUCT, "addctlpolicy",
1745 		       SYSCTL_DESCR("Return the current address control"
1746 			   " policy"),
1747 		       sysctl_net_inet6_addrctlpolicy, 0, NULL, 0,
1748 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1749 		       IPV6CTL_ADDRCTLPOLICY, CTL_EOL);
1750 	sysctl_createv(clog, 0, NULL, NULL,
1751 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1752 		       CTLTYPE_INT, "use_tempaddr",
1753 		       SYSCTL_DESCR("Use temporary address"),
1754 		       NULL, 0, &ip6_use_tempaddr, 0,
1755 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1756 		       CTL_CREATE, CTL_EOL);
1757 	sysctl_createv(clog, 0, NULL, NULL,
1758 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1759 		       CTLTYPE_INT, "prefer_tempaddr",
1760 		       SYSCTL_DESCR("Prefer temporary address as source "
1761 		                    "address"),
1762 		       NULL, 0, &ip6_prefer_tempaddr, 0,
1763 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1764 		       CTL_CREATE, CTL_EOL);
1765 	sysctl_createv(clog, 0, NULL, NULL,
1766 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1767 		       CTLTYPE_INT, "temppltime",
1768 		       SYSCTL_DESCR("preferred lifetime of a temporary address"),
1769 		       sysctl_net_inet6_ip6_temppltime, 0, NULL, 0,
1770 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1771 		       CTL_CREATE, CTL_EOL);
1772 	sysctl_createv(clog, 0, NULL, NULL,
1773 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1774 		       CTLTYPE_INT, "tempvltime",
1775 		       SYSCTL_DESCR("valid lifetime of a temporary address"),
1776 		       NULL, 0, &ip6_temp_valid_lifetime, 0,
1777 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1778 		       CTL_CREATE, CTL_EOL);
1779 	sysctl_createv(clog, 0, NULL, NULL,
1780 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1781 		       CTLTYPE_INT, "maxfrags",
1782 		       SYSCTL_DESCR("Maximum fragments in reassembly queue"),
1783 		       NULL, 0, &ip6_maxfrags, 0,
1784 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1785 		       IPV6CTL_MAXFRAGS, CTL_EOL);
1786 	sysctl_createv(clog, 0, NULL, NULL,
1787 		       CTLFLAG_PERMANENT,
1788 		       CTLTYPE_STRUCT, "stats",
1789 		       SYSCTL_DESCR("IPv6 statistics"),
1790 		       sysctl_net_inet6_ip6_stats, 0, NULL, 0,
1791 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1792 		       IPV6CTL_STATS, CTL_EOL);
1793 	sysctl_createv(clog, 0, NULL, NULL,
1794 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1795 		       CTLTYPE_INT, "use_defaultzone",
1796 		       SYSCTL_DESCR("Whether to use the default scope zones"),
1797 		       NULL, 0, &ip6_use_defzone, 0,
1798 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1799 		       IPV6CTL_USE_DEFAULTZONE, CTL_EOL);
1800 	sysctl_createv(clog, 0, NULL, NULL,
1801 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1802 		       CTLTYPE_INT, "mcast_pmtu",
1803 		       SYSCTL_DESCR("Enable pMTU discovery for multicast packet"),
1804 		       NULL, 0, &ip6_mcast_pmtu, 0,
1805 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1806 		       CTL_CREATE, CTL_EOL);
1807 	/* anonportalgo RFC6056 subtree */
1808 	const struct sysctlnode *portalgo_node;
1809 	sysctl_createv(clog, 0, NULL, &portalgo_node,
1810 		       CTLFLAG_PERMANENT,
1811 		       CTLTYPE_NODE, "anonportalgo",
1812 		       SYSCTL_DESCR("Anonymous port algorithm selection (RFC 6056)"),
1813 	    	       NULL, 0, NULL, 0,
1814 		       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_CREATE, CTL_EOL);
1815 	sysctl_createv(clog, 0, &portalgo_node, NULL,
1816 		       CTLFLAG_PERMANENT,
1817 		       CTLTYPE_STRING, "available",
1818 		       SYSCTL_DESCR("available algorithms"),
1819 		       sysctl_portalgo_available, 0, NULL, PORTALGO_MAXLEN,
1820 		       CTL_CREATE, CTL_EOL);
1821 	sysctl_createv(clog, 0, &portalgo_node, NULL,
1822 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1823 		       CTLTYPE_STRING, "selected",
1824 		       SYSCTL_DESCR("selected algorithm"),
1825 	               sysctl_portalgo_selected6, 0, NULL, PORTALGO_MAXLEN,
1826 		       CTL_CREATE, CTL_EOL);
1827 	sysctl_createv(clog, 0, &portalgo_node, NULL,
1828 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1829 		       CTLTYPE_STRUCT, "reserve",
1830 		       SYSCTL_DESCR("bitmap of reserved ports"),
1831 		       sysctl_portalgo_reserve6, 0, NULL, 0,
1832 		       CTL_CREATE, CTL_EOL);
1833 	sysctl_createv(clog, 0, NULL, NULL,
1834 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1835 		       CTLTYPE_INT, "neighborgcthresh",
1836 		       SYSCTL_DESCR("Maximum number of entries in neighbor"
1837 			" cache"),
1838 		       NULL, 1, &ip6_neighborgcthresh, 0,
1839 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1840 		       CTL_CREATE, CTL_EOL);
1841 	sysctl_createv(clog, 0, NULL, NULL,
1842 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1843 		       CTLTYPE_INT, "maxifprefixes",
1844 		       SYSCTL_DESCR("Maximum number of prefixes created by"
1845 			   " route advertisement per interface"),
1846 		       NULL, 1, &ip6_maxifprefixes, 0,
1847 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1848 		       CTL_CREATE, CTL_EOL);
1849 	sysctl_createv(clog, 0, NULL, NULL,
1850 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1851 		       CTLTYPE_INT, "maxifdefrouters",
1852 		       SYSCTL_DESCR("Maximum number of default routers created"
1853 			   " by route advertisement per interface"),
1854 		       NULL, 1, &ip6_maxifdefrouters, 0,
1855 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1856 		       CTL_CREATE, CTL_EOL);
1857 	sysctl_createv(clog, 0, NULL, NULL,
1858 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1859 		       CTLTYPE_INT, "maxdynroutes",
1860 		       SYSCTL_DESCR("Maximum number of routes created via"
1861 			   " redirect"),
1862 		       NULL, 1, &ip6_maxdynroutes, 0,
1863 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1864 		       CTL_CREATE, CTL_EOL);
1865 }
1866 
1867 void
1868 ip6_statinc(u_int stat)
1869 {
1870 
1871 	KASSERT(stat < IP6_NSTATS);
1872 	IP6_STATINC(stat);
1873 }
1874