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