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