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