xref: /netbsd-src/sys/netinet6/ip6_input.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: ip6_input.c,v 1.173 2017/01/16 15:44:47 christos 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.173 2017/01/16 15:44:47 christos 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 	struct rtentry *rt = NULL;
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 	ro = percpu_getref(ip6_forward_rt_percpu);
458 	/*
459 	 * Multicast check
460 	 */
461 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
462 	  	struct	in6_multi *in6m = 0;
463 
464 		in6_ifstat_inc(rcvif, ifs6_in_mcast);
465 		/*
466 		 * See if we belong to the destination multicast group on the
467 		 * arrival interface.
468 		 */
469 		IN6_LOOKUP_MULTI(ip6->ip6_dst, rcvif, in6m);
470 		if (in6m)
471 			ours = 1;
472 		else if (!ip6_mrouter) {
473 			uint64_t *ip6s = IP6_STAT_GETREF();
474 			ip6s[IP6_STAT_NOTMEMBER]++;
475 			ip6s[IP6_STAT_CANTFORWARD]++;
476 			IP6_STAT_PUTREF();
477 			in6_ifstat_inc(rcvif, ifs6_in_discard);
478 			goto bad_unref;
479 		}
480 		deliverifp = rcvif;
481 		goto hbhcheck;
482 	}
483 
484 	sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
485 
486 	/*
487 	 *  Unicast check
488 	 */
489 	rt = rtcache_lookup2(ro, &u.dst, 1, &hit);
490 	if (hit)
491 		IP6_STATINC(IP6_STAT_FORWARD_CACHEHIT);
492 	else
493 		IP6_STATINC(IP6_STAT_FORWARD_CACHEMISS);
494 
495 #define rt6_getkey(__rt) satocsin6(rt_getkey(__rt))
496 
497 	/*
498 	 * Accept the packet if the forwarding interface to the destination
499 	 * according to the routing table is the loopback interface,
500 	 * unless the associated route has a gateway.
501 	 * Note that this approach causes to accept a packet if there is a
502 	 * route to the loopback interface for the destination of the packet.
503 	 * But we think it's even useful in some situations, e.g. when using
504 	 * a special daemon which wants to intercept the packet.
505 	 */
506 	if (rt != NULL &&
507 	    (rt->rt_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
508 #if 0
509 	    /*
510 	     * The check below is redundant since the comparison of
511 	     * the destination and the key of the rtentry has
512 	     * already done through looking up the routing table.
513 	     */
514 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &rt6_getkey(rt)->sin6_addr) &&
515 #endif
516 	    rt->rt_ifp->if_type == IFT_LOOP) {
517 		struct in6_ifaddr *ia6 = (struct in6_ifaddr *)rt->rt_ifa;
518 		if (ia6->ia6_flags & IN6_IFF_ANYCAST)
519 			m->m_flags |= M_ANYCAST6;
520 		/*
521 		 * packets to a tentative, duplicated, or somehow invalid
522 		 * address must not be accepted.
523 		 */
524 		if (!(ia6->ia6_flags & (IN6_IFF_NOTREADY | IN6_IFF_DETACHED))) {
525 			/* this address is ready */
526 			ours = 1;
527 			deliverifp = ia6->ia_ifp;	/* correct? */
528 			goto hbhcheck;
529 		} else {
530 			/* address is not ready, so discard the packet. */
531 			char ip6bufs[INET6_ADDRSTRLEN];
532 			char ip6bufd[INET6_ADDRSTRLEN];
533 			nd6log(LOG_INFO, "packet to an unready address %s->%s\n",
534 			    IN6_PRINT(ip6bufs, &ip6->ip6_src),
535 			    IN6_PRINT(ip6bufd, &ip6->ip6_dst));
536 
537 			goto bad_unref;
538 		}
539 	}
540 
541 	/*
542 	 * FAITH (Firewall Aided Internet Translator)
543 	 */
544 #if defined(NFAITH) && 0 < NFAITH
545 	if (ip6_keepfaith) {
546 		if (rt != NULL && rt->rt_ifp != NULL &&
547 		    rt->rt_ifp->if_type == IFT_FAITH) {
548 			/* XXX do we need more sanity checks? */
549 			ours = 1;
550 			deliverifp = rt->rt_ifp; /* faith */
551 			goto hbhcheck;
552 		}
553 	}
554 #endif
555 
556 #if 0
557     {
558 	/*
559 	 * Last resort: check in6_ifaddr for incoming interface.
560 	 * The code is here until I update the "goto ours hack" code above
561 	 * working right.
562 	 */
563 	struct ifaddr *ifa;
564 	IFADDR_READER_FOREACH(ifa, rcvif) {
565 		if (ifa->ifa_addr->sa_family != AF_INET6)
566 			continue;
567 		if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
568 			ours = 1;
569 			deliverifp = ifa->ifa_ifp;
570 			goto hbhcheck;
571 		}
572 	}
573     }
574 #endif
575 
576 	/*
577 	 * Now there is no reason to process the packet if it's not our own
578 	 * and we're not a router.
579 	 */
580 	if (!ip6_forwarding) {
581 		IP6_STATINC(IP6_STAT_CANTFORWARD);
582 		in6_ifstat_inc(rcvif, ifs6_in_discard);
583 		goto bad_unref;
584 	}
585 
586   hbhcheck:
587 	/*
588 	 * record address information into m_tag, if we don't have one yet.
589 	 * note that we are unable to record it, if the address is not listed
590 	 * as our interface address (e.g. multicast addresses, addresses
591 	 * within FAITH prefixes and such).
592 	 */
593 	if (deliverifp && ip6_getdstifaddr(m) == NULL) {
594 		struct in6_ifaddr *ia6;
595 		int s = pserialize_read_enter();
596 
597 		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
598 		/* Depends on ip6_setdstifaddr never sleep */
599 		if (ia6 != NULL && ip6_setdstifaddr(m, ia6) == NULL) {
600 			/*
601 			 * XXX maybe we should drop the packet here,
602 			 * as we could not provide enough information
603 			 * to the upper layers.
604 			 */
605 		}
606 		pserialize_read_exit(s);
607 	}
608 
609 	/*
610 	 * Process Hop-by-Hop options header if it's contained.
611 	 * m may be modified in ip6_hopopts_input().
612 	 * If a JumboPayload option is included, plen will also be modified.
613 	 */
614 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
615 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
616 		struct ip6_hbh *hbh;
617 
618 		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
619 #if 0	/*touches NULL pointer*/
620 			in6_ifstat_inc(rcvif, ifs6_in_discard);
621 #endif
622 			rtcache_unref(rt, ro);
623 			percpu_putref(ip6_forward_rt_percpu);
624 			return;	/* m have already been freed */
625 		}
626 
627 		/* adjust pointer */
628 		ip6 = mtod(m, struct ip6_hdr *);
629 
630 		/*
631 		 * if the payload length field is 0 and the next header field
632 		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
633 		 * option MUST be included.
634 		 */
635 		if (ip6->ip6_plen == 0 && plen == 0) {
636 			/*
637 			 * Note that if a valid jumbo payload option is
638 			 * contained, ip6_hopopts_input() must set a valid
639 			 * (non-zero) payload length to the variable plen.
640 			 */
641 			IP6_STATINC(IP6_STAT_BADOPTIONS);
642 			in6_ifstat_inc(rcvif, ifs6_in_discard);
643 			in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
644 			icmp6_error(m, ICMP6_PARAM_PROB,
645 				    ICMP6_PARAMPROB_HEADER,
646 				    (char *)&ip6->ip6_plen - (char *)ip6);
647 			rtcache_unref(rt, ro);
648 			percpu_putref(ip6_forward_rt_percpu);
649 			return;
650 		}
651 		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
652 			sizeof(struct ip6_hbh));
653 		if (hbh == NULL) {
654 			IP6_STATINC(IP6_STAT_TOOSHORT);
655 			rtcache_unref(rt, ro);
656 			percpu_putref(ip6_forward_rt_percpu);
657 			return;
658 		}
659 		KASSERT(IP6_HDR_ALIGNED_P(hbh));
660 		nxt = hbh->ip6h_nxt;
661 
662 		/*
663 		 * accept the packet if a router alert option is included
664 		 * and we act as an IPv6 router.
665 		 */
666 		if (rtalert != ~0 && ip6_forwarding)
667 			ours = 1;
668 	} else
669 		nxt = ip6->ip6_nxt;
670 
671 	/*
672 	 * Check that the amount of data in the buffers
673 	 * is as at least much as the IPv6 header would have us expect.
674 	 * Trim mbufs if longer than we expect.
675 	 * Drop packet if shorter than we expect.
676 	 */
677 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
678 		IP6_STATINC(IP6_STAT_TOOSHORT);
679 		in6_ifstat_inc(rcvif, ifs6_in_truncated);
680 		goto bad_unref;
681 	}
682 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
683 		if (m->m_len == m->m_pkthdr.len) {
684 			m->m_len = sizeof(struct ip6_hdr) + plen;
685 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
686 		} else
687 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
688 	}
689 
690 	/*
691 	 * Forward if desirable.
692 	 */
693 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
694 		/*
695 		 * If we are acting as a multicast router, all
696 		 * incoming multicast packets are passed to the
697 		 * kernel-level multicast forwarding function.
698 		 * The packet is returned (relatively) intact; if
699 		 * ip6_mforward() returns a non-zero value, the packet
700 		 * must be discarded, else it may be accepted below.
701 		 */
702 		if (ip6_mrouter != NULL) {
703 			int error;
704 
705 			SOFTNET_LOCK();
706 			error = ip6_mforward(ip6, rcvif, m);
707 			SOFTNET_UNLOCK();
708 
709 			if (error != 0) {
710 				rtcache_unref(rt, ro);
711 				percpu_putref(ip6_forward_rt_percpu);
712 				IP6_STATINC(IP6_STAT_CANTFORWARD);
713 				goto bad;
714 			}
715 		}
716 		if (!ours)
717 			goto bad_unref;
718 	} else if (!ours) {
719 		rtcache_unref(rt, ro);
720 		percpu_putref(ip6_forward_rt_percpu);
721 		ip6_forward(m, srcrt);
722 		return;
723 	}
724 
725 	ip6 = mtod(m, struct ip6_hdr *);
726 
727 	/*
728 	 * Malicious party may be able to use IPv4 mapped addr to confuse
729 	 * tcp/udp stack and bypass security checks (act as if it was from
730 	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
731 	 *
732 	 * For SIIT end node behavior, you may want to disable the check.
733 	 * However, you will  become vulnerable to attacks using IPv4 mapped
734 	 * source.
735 	 */
736 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
737 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
738 		IP6_STATINC(IP6_STAT_BADSCOPE);
739 		in6_ifstat_inc(rcvif, ifs6_in_addrerr);
740 		goto bad_unref;
741 	}
742 
743 	/*
744 	 * Tell launch routine the next header
745 	 */
746 #ifdef IFA_STATS
747 	if (deliverifp != NULL) {
748 		struct in6_ifaddr *ia6;
749 		int s = pserialize_read_enter();
750 		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
751 		if (ia6)
752 			ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
753 		pserialize_read_exit(s);
754 	}
755 #endif
756 	IP6_STATINC(IP6_STAT_DELIVERED);
757 	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
758 	nest = 0;
759 
760 	if (rt != NULL) {
761 		rtcache_unref(rt, ro);
762 		rt = NULL;
763 	}
764 	percpu_putref(ip6_forward_rt_percpu);
765 
766 	rh_present = 0;
767 	while (nxt != IPPROTO_DONE) {
768 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
769 			IP6_STATINC(IP6_STAT_TOOMANYHDR);
770 			in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
771 			goto bad;
772 		}
773 
774 		/*
775 		 * protection against faulty packet - there should be
776 		 * more sanity checks in header chain processing.
777 		 */
778 		if (m->m_pkthdr.len < off) {
779 			IP6_STATINC(IP6_STAT_TOOSHORT);
780 			in6_ifstat_inc(rcvif, ifs6_in_truncated);
781 			goto bad;
782 		}
783 
784 		if (nxt == IPPROTO_ROUTING) {
785 			if (rh_present++) {
786 				in6_ifstat_inc(rcvif, ifs6_in_hdrerr);
787 				IP6_STATINC(IP6_STAT_BADOPTIONS);
788 				goto bad;
789 			}
790 		}
791 
792 #ifdef IPSEC
793 		if (ipsec_used) {
794 			/*
795 			 * enforce IPsec policy checking if we are seeing last
796 			 * header. note that we do not visit this with
797 			 * protocols with pcb layer code - like udp/tcp/raw ip.
798 			 */
799 			if ((inet6sw[ip_protox[nxt]].pr_flags
800 			    & PR_LASTHDR) != 0) {
801 				int error;
802 
803 				SOFTNET_LOCK();
804 				error = ipsec6_input(m);
805 				SOFTNET_UNLOCK();
806 				if (error)
807 					goto bad;
808 			}
809 		}
810 #endif /* IPSEC */
811 
812 		SOFTNET_LOCK();
813 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
814 		SOFTNET_UNLOCK();
815 	}
816 	return;
817 
818  bad_unref:
819 	rtcache_unref(rt, ro);
820 	percpu_putref(ip6_forward_rt_percpu);
821  bad:
822 	m_freem(m);
823 	return;
824 }
825 
826 /*
827  * set/grab in6_ifaddr correspond to IPv6 destination address.
828  */
829 static struct m_tag *
830 ip6_setdstifaddr(struct mbuf *m, const struct in6_ifaddr *ia)
831 {
832 	struct m_tag *mtag;
833 	struct ip6aux *ip6a;
834 
835 	mtag = ip6_addaux(m);
836 	if (mtag == NULL)
837 		return NULL;
838 
839 	ip6a = (struct ip6aux *)(mtag + 1);
840 	if (in6_setscope(&ip6a->ip6a_src, ia->ia_ifp, &ip6a->ip6a_scope_id)) {
841 		IP6_STATINC(IP6_STAT_BADSCOPE);
842 		return NULL;
843 	}
844 
845 	ip6a->ip6a_src = ia->ia_addr.sin6_addr;
846 	ip6a->ip6a_flags = ia->ia6_flags;
847 	return mtag;
848 }
849 
850 const struct ip6aux *
851 ip6_getdstifaddr(struct mbuf *m)
852 {
853 	struct m_tag *mtag;
854 
855 	mtag = ip6_findaux(m);
856 	if (mtag != NULL)
857 		return (struct ip6aux *)(mtag + 1);
858 	else
859 		return NULL;
860 }
861 
862 /*
863  * Hop-by-Hop options header processing. If a valid jumbo payload option is
864  * included, the real payload length will be stored in plenp.
865  *
866  * rtalertp - XXX: should be stored more smart way
867  */
868 int
869 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
870 	struct mbuf **mp, int *offp)
871 {
872 	struct mbuf *m = *mp;
873 	int off = *offp, hbhlen;
874 	struct ip6_hbh *hbh;
875 
876 	/* validation of the length of the header */
877 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
878 		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
879 	if (hbh == NULL) {
880 		IP6_STATINC(IP6_STAT_TOOSHORT);
881 		return -1;
882 	}
883 	hbhlen = (hbh->ip6h_len + 1) << 3;
884 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
885 		hbhlen);
886 	if (hbh == NULL) {
887 		IP6_STATINC(IP6_STAT_TOOSHORT);
888 		return -1;
889 	}
890 	KASSERT(IP6_HDR_ALIGNED_P(hbh));
891 	off += hbhlen;
892 	hbhlen -= sizeof(struct ip6_hbh);
893 
894 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
895 				hbhlen, rtalertp, plenp) < 0)
896 		return (-1);
897 
898 	*offp = off;
899 	*mp = m;
900 	return (0);
901 }
902 
903 /*
904  * Search header for all Hop-by-hop options and process each option.
905  * This function is separate from ip6_hopopts_input() in order to
906  * handle a case where the sending node itself process its hop-by-hop
907  * options header. In such a case, the function is called from ip6_output().
908  *
909  * The function assumes that hbh header is located right after the IPv6 header
910  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
911  * opthead + hbhlen is located in continuous memory region.
912  */
913 static int
914 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
915 	u_int32_t *rtalertp, u_int32_t *plenp)
916 {
917 	struct ip6_hdr *ip6;
918 	int optlen = 0;
919 	u_int8_t *opt = opthead;
920 	u_int16_t rtalert_val;
921 	u_int32_t jumboplen;
922 	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
923 
924 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
925 		switch (*opt) {
926 		case IP6OPT_PAD1:
927 			optlen = 1;
928 			break;
929 		case IP6OPT_PADN:
930 			if (hbhlen < IP6OPT_MINLEN) {
931 				IP6_STATINC(IP6_STAT_TOOSMALL);
932 				goto bad;
933 			}
934 			optlen = *(opt + 1) + 2;
935 			break;
936 		case IP6OPT_RTALERT:
937 			/* XXX may need check for alignment */
938 			if (hbhlen < IP6OPT_RTALERT_LEN) {
939 				IP6_STATINC(IP6_STAT_TOOSMALL);
940 				goto bad;
941 			}
942 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
943 				/* XXX stat */
944 				icmp6_error(m, ICMP6_PARAM_PROB,
945 				    ICMP6_PARAMPROB_HEADER,
946 				    erroff + opt + 1 - opthead);
947 				return (-1);
948 			}
949 			optlen = IP6OPT_RTALERT_LEN;
950 			memcpy((void *)&rtalert_val, (void *)(opt + 2), 2);
951 			*rtalertp = ntohs(rtalert_val);
952 			break;
953 		case IP6OPT_JUMBO:
954 			/* XXX may need check for alignment */
955 			if (hbhlen < IP6OPT_JUMBO_LEN) {
956 				IP6_STATINC(IP6_STAT_TOOSMALL);
957 				goto bad;
958 			}
959 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
960 				/* XXX stat */
961 				icmp6_error(m, ICMP6_PARAM_PROB,
962 				    ICMP6_PARAMPROB_HEADER,
963 				    erroff + opt + 1 - opthead);
964 				return (-1);
965 			}
966 			optlen = IP6OPT_JUMBO_LEN;
967 
968 			/*
969 			 * IPv6 packets that have non 0 payload length
970 			 * must not contain a jumbo payload option.
971 			 */
972 			ip6 = mtod(m, struct ip6_hdr *);
973 			if (ip6->ip6_plen) {
974 				IP6_STATINC(IP6_STAT_BADOPTIONS);
975 				icmp6_error(m, ICMP6_PARAM_PROB,
976 				    ICMP6_PARAMPROB_HEADER,
977 				    erroff + opt - opthead);
978 				return (-1);
979 			}
980 
981 			/*
982 			 * We may see jumbolen in unaligned location, so
983 			 * we'd need to perform bcopy().
984 			 */
985 			memcpy(&jumboplen, opt + 2, sizeof(jumboplen));
986 			jumboplen = (u_int32_t)htonl(jumboplen);
987 
988 #if 1
989 			/*
990 			 * if there are multiple jumbo payload options,
991 			 * *plenp will be non-zero and the packet will be
992 			 * rejected.
993 			 * the behavior may need some debate in ipngwg -
994 			 * multiple options does not make sense, however,
995 			 * there's no explicit mention in specification.
996 			 */
997 			if (*plenp != 0) {
998 				IP6_STATINC(IP6_STAT_BADOPTIONS);
999 				icmp6_error(m, ICMP6_PARAM_PROB,
1000 				    ICMP6_PARAMPROB_HEADER,
1001 				    erroff + opt + 2 - opthead);
1002 				return (-1);
1003 			}
1004 #endif
1005 
1006 			/*
1007 			 * jumbo payload length must be larger than 65535.
1008 			 */
1009 			if (jumboplen <= IPV6_MAXPACKET) {
1010 				IP6_STATINC(IP6_STAT_BADOPTIONS);
1011 				icmp6_error(m, ICMP6_PARAM_PROB,
1012 				    ICMP6_PARAMPROB_HEADER,
1013 				    erroff + opt + 2 - opthead);
1014 				return (-1);
1015 			}
1016 			*plenp = jumboplen;
1017 
1018 			break;
1019 		default:		/* unknown option */
1020 			if (hbhlen < IP6OPT_MINLEN) {
1021 				IP6_STATINC(IP6_STAT_TOOSMALL);
1022 				goto bad;
1023 			}
1024 			optlen = ip6_unknown_opt(opt, m,
1025 			    erroff + opt - opthead);
1026 			if (optlen == -1)
1027 				return (-1);
1028 			optlen += 2;
1029 			break;
1030 		}
1031 	}
1032 
1033 	return (0);
1034 
1035   bad:
1036 	m_freem(m);
1037 	return (-1);
1038 }
1039 
1040 /*
1041  * Unknown option processing.
1042  * The third argument `off' is the offset from the IPv6 header to the option,
1043  * which is necessary if the IPv6 header the and option header and IPv6 header
1044  * is not continuous in order to return an ICMPv6 error.
1045  */
1046 int
1047 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1048 {
1049 	struct ip6_hdr *ip6;
1050 
1051 	switch (IP6OPT_TYPE(*optp)) {
1052 	case IP6OPT_TYPE_SKIP: /* ignore the option */
1053 		return ((int)*(optp + 1));
1054 	case IP6OPT_TYPE_DISCARD:	/* silently discard */
1055 		m_freem(m);
1056 		return (-1);
1057 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1058 		IP6_STATINC(IP6_STAT_BADOPTIONS);
1059 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1060 		return (-1);
1061 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1062 		IP6_STATINC(IP6_STAT_BADOPTIONS);
1063 		ip6 = mtod(m, struct ip6_hdr *);
1064 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1065 		    (m->m_flags & (M_BCAST|M_MCAST)))
1066 			m_freem(m);
1067 		else
1068 			icmp6_error(m, ICMP6_PARAM_PROB,
1069 				    ICMP6_PARAMPROB_OPTION, off);
1070 		return (-1);
1071 	}
1072 
1073 	m_freem(m);		/* XXX: NOTREACHED */
1074 	return (-1);
1075 }
1076 
1077 /*
1078  * Create the "control" list for this pcb.
1079  *
1080  * The routine will be called from upper layer handlers like tcp6_input().
1081  * Thus the routine assumes that the caller (tcp6_input) have already
1082  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1083  * very first mbuf on the mbuf chain.
1084  * We may want to add some infinite loop prevention or sanity checks for safety.
1085  * (This applies only when you are using KAME mbuf chain restriction, i.e.
1086  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
1087  */
1088 void
1089 ip6_savecontrol(struct in6pcb *in6p, struct mbuf **mp,
1090 	struct ip6_hdr *ip6, struct mbuf *m)
1091 {
1092 #ifdef RFC2292
1093 #define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1094 #else
1095 #define IS2292(x, y)	(y)
1096 #endif
1097 
1098 	if (in6p->in6p_socket->so_options & SO_TIMESTAMP
1099 #ifdef SO_OTIMESTAMP
1100 	    || in6p->in6p_socket->so_options & SO_OTIMESTAMP
1101 #endif
1102 	) {
1103 		struct timeval tv;
1104 
1105 		microtime(&tv);
1106 #ifdef SO_OTIMESTAMP
1107 		if (in6p->in6p_socket->so_options & SO_OTIMESTAMP) {
1108 			struct timeval50 tv50;
1109 			timeval_to_timeval50(&tv, &tv50);
1110 			*mp = sbcreatecontrol((void *) &tv50, sizeof(tv50),
1111 			    SCM_OTIMESTAMP, SOL_SOCKET);
1112 		} else
1113 #endif
1114 		*mp = sbcreatecontrol((void *) &tv, sizeof(tv),
1115 		    SCM_TIMESTAMP, SOL_SOCKET);
1116 		if (*mp)
1117 			mp = &(*mp)->m_next;
1118 	}
1119 
1120 	/* some OSes call this logic with IPv4 packet, for SO_TIMESTAMP */
1121 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
1122 		return;
1123 
1124 	/* RFC 2292 sec. 5 */
1125 	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1126 		struct in6_pktinfo pi6;
1127 
1128 		memcpy(&pi6.ipi6_addr, &ip6->ip6_dst, sizeof(struct in6_addr));
1129 		in6_clearscope(&pi6.ipi6_addr);	/* XXX */
1130 		pi6.ipi6_ifindex = m->m_pkthdr.rcvif_index;
1131 		*mp = sbcreatecontrol((void *) &pi6,
1132 		    sizeof(struct in6_pktinfo),
1133 		    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1134 		if (*mp)
1135 			mp = &(*mp)->m_next;
1136 	}
1137 
1138 	if (in6p->in6p_flags & IN6P_HOPLIMIT) {
1139 		int hlim = ip6->ip6_hlim & 0xff;
1140 
1141 		*mp = sbcreatecontrol((void *) &hlim, sizeof(int),
1142 		    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1143 		if (*mp)
1144 			mp = &(*mp)->m_next;
1145 	}
1146 
1147 	if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1148 		u_int32_t flowinfo;
1149 		int tclass;
1150 
1151 		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1152 		flowinfo >>= 20;
1153 
1154 		tclass = flowinfo & 0xff;
1155 		*mp = sbcreatecontrol((void *)&tclass, sizeof(tclass),
1156 		    IPV6_TCLASS, IPPROTO_IPV6);
1157 
1158 		if (*mp)
1159 			mp = &(*mp)->m_next;
1160 	}
1161 
1162 	/*
1163 	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
1164 	 * privilege for the option (see ip6_ctloutput), but it might be too
1165 	 * strict, since there might be some hop-by-hop options which can be
1166 	 * returned to normal user.
1167 	 * See also RFC3542 section 8 (or RFC2292 section 6).
1168 	 */
1169 	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1170 		/*
1171 		 * Check if a hop-by-hop options header is contatined in the
1172 		 * received packet, and if so, store the options as ancillary
1173 		 * data. Note that a hop-by-hop options header must be
1174 		 * just after the IPv6 header, which fact is assured through
1175 		 * the IPv6 input processing.
1176 		 */
1177 		struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
1178 		if (xip6->ip6_nxt == IPPROTO_HOPOPTS) {
1179 			struct ip6_hbh *hbh;
1180 			int hbhlen;
1181 			struct mbuf *ext;
1182 
1183 			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1184 			    xip6->ip6_nxt);
1185 			if (ext == NULL) {
1186 				IP6_STATINC(IP6_STAT_TOOSHORT);
1187 				return;
1188 			}
1189 			hbh = mtod(ext, struct ip6_hbh *);
1190 			hbhlen = (hbh->ip6h_len + 1) << 3;
1191 			if (hbhlen != ext->m_len) {
1192 				m_freem(ext);
1193 				IP6_STATINC(IP6_STAT_TOOSHORT);
1194 				return;
1195 			}
1196 
1197 			/*
1198 			 * XXX: We copy whole the header even if a jumbo
1199 			 * payload option is included, which option is to
1200 			 * be removed before returning in the RFC 2292.
1201 			 * Note: this constraint is removed in RFC3542.
1202 			 */
1203 			*mp = sbcreatecontrol((void *)hbh, hbhlen,
1204 			    IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1205 			    IPPROTO_IPV6);
1206 			if (*mp)
1207 				mp = &(*mp)->m_next;
1208 			m_freem(ext);
1209 		}
1210 	}
1211 
1212 	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1213 	if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
1214 		struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
1215 		int nxt = xip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1216 
1217 		/*
1218 		 * Search for destination options headers or routing
1219 		 * header(s) through the header chain, and stores each
1220 		 * header as ancillary data.
1221 		 * Note that the order of the headers remains in
1222 		 * the chain of ancillary data.
1223 		 */
1224 		for (;;) {	/* is explicit loop prevention necessary? */
1225 			struct ip6_ext *ip6e = NULL;
1226 			int elen;
1227 			struct mbuf *ext = NULL;
1228 
1229 			/*
1230 			 * if it is not an extension header, don't try to
1231 			 * pull it from the chain.
1232 			 */
1233 			switch (nxt) {
1234 			case IPPROTO_DSTOPTS:
1235 			case IPPROTO_ROUTING:
1236 			case IPPROTO_HOPOPTS:
1237 			case IPPROTO_AH: /* is it possible? */
1238 				break;
1239 			default:
1240 				goto loopend;
1241 			}
1242 
1243 			ext = ip6_pullexthdr(m, off, nxt);
1244 			if (ext == NULL) {
1245 				IP6_STATINC(IP6_STAT_TOOSHORT);
1246 				return;
1247 			}
1248 			ip6e = mtod(ext, struct ip6_ext *);
1249 			if (nxt == IPPROTO_AH)
1250 				elen = (ip6e->ip6e_len + 2) << 2;
1251 			else
1252 				elen = (ip6e->ip6e_len + 1) << 3;
1253 			if (elen != ext->m_len) {
1254 				m_freem(ext);
1255 				IP6_STATINC(IP6_STAT_TOOSHORT);
1256 				return;
1257 			}
1258 			KASSERT(IP6_HDR_ALIGNED_P(ip6e));
1259 
1260 			switch (nxt) {
1261 			case IPPROTO_DSTOPTS:
1262 				if (!(in6p->in6p_flags & IN6P_DSTOPTS))
1263 					break;
1264 
1265 				*mp = sbcreatecontrol((void *)ip6e, elen,
1266 				    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1267 				    IPPROTO_IPV6);
1268 				if (*mp)
1269 					mp = &(*mp)->m_next;
1270 				break;
1271 
1272 			case IPPROTO_ROUTING:
1273 				if (!(in6p->in6p_flags & IN6P_RTHDR))
1274 					break;
1275 
1276 				*mp = sbcreatecontrol((void *)ip6e, elen,
1277 				    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1278 				    IPPROTO_IPV6);
1279 				if (*mp)
1280 					mp = &(*mp)->m_next;
1281 				break;
1282 
1283 			case IPPROTO_HOPOPTS:
1284 			case IPPROTO_AH: /* is it possible? */
1285 				break;
1286 
1287 			default:
1288 				/*
1289 			 	 * other cases have been filtered in the above.
1290 				 * none will visit this case.  here we supply
1291 				 * the code just in case (nxt overwritten or
1292 				 * other cases).
1293 				 */
1294 				m_freem(ext);
1295 				goto loopend;
1296 
1297 			}
1298 
1299 			/* proceed with the next header. */
1300 			off += elen;
1301 			nxt = ip6e->ip6e_nxt;
1302 			ip6e = NULL;
1303 			m_freem(ext);
1304 			ext = NULL;
1305 		}
1306 	  loopend:
1307 	  	;
1308 	}
1309 }
1310 #undef IS2292
1311 
1312 
1313 void
1314 ip6_notify_pmtu(struct in6pcb *in6p, const struct sockaddr_in6 *dst,
1315     uint32_t *mtu)
1316 {
1317 	struct socket *so;
1318 	struct mbuf *m_mtu;
1319 	struct ip6_mtuinfo mtuctl;
1320 
1321 	so = in6p->in6p_socket;
1322 
1323 	if (mtu == NULL)
1324 		return;
1325 
1326 #ifdef DIAGNOSTIC
1327 	if (so == NULL)		/* I believe this is impossible */
1328 		panic("ip6_notify_pmtu: socket is NULL");
1329 #endif
1330 
1331 	memset(&mtuctl, 0, sizeof(mtuctl));	/* zero-clear for safety */
1332 	mtuctl.ip6m_mtu = *mtu;
1333 	mtuctl.ip6m_addr = *dst;
1334 	if (sa6_recoverscope(&mtuctl.ip6m_addr))
1335 		return;
1336 
1337 	if ((m_mtu = sbcreatecontrol((void *)&mtuctl, sizeof(mtuctl),
1338 	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1339 		return;
1340 
1341 	if (sbappendaddr(&so->so_rcv, (const struct sockaddr *)dst, NULL, m_mtu)
1342 	    == 0) {
1343 		m_freem(m_mtu);
1344 		/* XXX: should count statistics */
1345 	} else
1346 		sorwakeup(so);
1347 
1348 	return;
1349 }
1350 
1351 /*
1352  * pull single extension header from mbuf chain.  returns single mbuf that
1353  * contains the result, or NULL on error.
1354  */
1355 static struct mbuf *
1356 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1357 {
1358 	struct ip6_ext ip6e;
1359 	size_t elen;
1360 	struct mbuf *n;
1361 
1362 #ifdef DIAGNOSTIC
1363 	switch (nxt) {
1364 	case IPPROTO_DSTOPTS:
1365 	case IPPROTO_ROUTING:
1366 	case IPPROTO_HOPOPTS:
1367 	case IPPROTO_AH: /* is it possible? */
1368 		break;
1369 	default:
1370 		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1371 	}
1372 #endif
1373 
1374 	m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1375 	if (nxt == IPPROTO_AH)
1376 		elen = (ip6e.ip6e_len + 2) << 2;
1377 	else
1378 		elen = (ip6e.ip6e_len + 1) << 3;
1379 
1380 	MGET(n, M_DONTWAIT, MT_DATA);
1381 	if (n && elen >= MLEN) {
1382 		MCLGET(n, M_DONTWAIT);
1383 		if ((n->m_flags & M_EXT) == 0) {
1384 			m_free(n);
1385 			n = NULL;
1386 		}
1387 	}
1388 	if (!n)
1389 		return NULL;
1390 
1391 	n->m_len = 0;
1392 	if (elen >= M_TRAILINGSPACE(n)) {
1393 		m_free(n);
1394 		return NULL;
1395 	}
1396 
1397 	m_copydata(m, off, elen, mtod(n, void *));
1398 	n->m_len = elen;
1399 	return n;
1400 }
1401 
1402 /*
1403  * Get pointer to the previous header followed by the header
1404  * currently processed.
1405  * XXX: This function supposes that
1406  *	M includes all headers,
1407  *	the next header field and the header length field of each header
1408  *	are valid, and
1409  *	the sum of each header length equals to OFF.
1410  * Because of these assumptions, this function must be called very
1411  * carefully. Moreover, it will not be used in the near future when
1412  * we develop `neater' mechanism to process extension headers.
1413  */
1414 u_int8_t *
1415 ip6_get_prevhdr(struct mbuf *m, int off)
1416 {
1417 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1418 
1419 	if (off == sizeof(struct ip6_hdr))
1420 		return (&ip6->ip6_nxt);
1421 	else {
1422 		int len, nxt;
1423 		struct ip6_ext *ip6e = NULL;
1424 
1425 		nxt = ip6->ip6_nxt;
1426 		len = sizeof(struct ip6_hdr);
1427 		while (len < off) {
1428 			ip6e = (struct ip6_ext *)(mtod(m, char *) + len);
1429 
1430 			switch (nxt) {
1431 			case IPPROTO_FRAGMENT:
1432 				len += sizeof(struct ip6_frag);
1433 				break;
1434 			case IPPROTO_AH:
1435 				len += (ip6e->ip6e_len + 2) << 2;
1436 				break;
1437 			default:
1438 				len += (ip6e->ip6e_len + 1) << 3;
1439 				break;
1440 			}
1441 			nxt = ip6e->ip6e_nxt;
1442 		}
1443 		if (ip6e)
1444 			return (&ip6e->ip6e_nxt);
1445 		else
1446 			return NULL;
1447 	}
1448 }
1449 
1450 /*
1451  * get next header offset.  m will be retained.
1452  */
1453 int
1454 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
1455 {
1456 	struct ip6_hdr ip6;
1457 	struct ip6_ext ip6e;
1458 	struct ip6_frag fh;
1459 
1460 	/* just in case */
1461 	if (m == NULL)
1462 		panic("ip6_nexthdr: m == NULL");
1463 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1464 		return -1;
1465 
1466 	switch (proto) {
1467 	case IPPROTO_IPV6:
1468 		/* do not chase beyond intermediate IPv6 headers */
1469 		if (off != 0)
1470 			return -1;
1471 		if (m->m_pkthdr.len < off + sizeof(ip6))
1472 			return -1;
1473 		m_copydata(m, off, sizeof(ip6), (void *)&ip6);
1474 		if (nxtp)
1475 			*nxtp = ip6.ip6_nxt;
1476 		off += sizeof(ip6);
1477 		return off;
1478 
1479 	case IPPROTO_FRAGMENT:
1480 		/*
1481 		 * terminate parsing if it is not the first fragment,
1482 		 * it does not make sense to parse through it.
1483 		 */
1484 		if (m->m_pkthdr.len < off + sizeof(fh))
1485 			return -1;
1486 		m_copydata(m, off, sizeof(fh), (void *)&fh);
1487 		if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0)
1488 			return -1;
1489 		if (nxtp)
1490 			*nxtp = fh.ip6f_nxt;
1491 		off += sizeof(struct ip6_frag);
1492 		return off;
1493 
1494 	case IPPROTO_AH:
1495 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1496 			return -1;
1497 		m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1498 		if (nxtp)
1499 			*nxtp = ip6e.ip6e_nxt;
1500 		off += (ip6e.ip6e_len + 2) << 2;
1501 		if (m->m_pkthdr.len < off)
1502 			return -1;
1503 		return off;
1504 
1505 	case IPPROTO_HOPOPTS:
1506 	case IPPROTO_ROUTING:
1507 	case IPPROTO_DSTOPTS:
1508 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1509 			return -1;
1510 		m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1511 		if (nxtp)
1512 			*nxtp = ip6e.ip6e_nxt;
1513 		off += (ip6e.ip6e_len + 1) << 3;
1514 		if (m->m_pkthdr.len < off)
1515 			return -1;
1516 		return off;
1517 
1518 	case IPPROTO_NONE:
1519 	case IPPROTO_ESP:
1520 	case IPPROTO_IPCOMP:
1521 		/* give up */
1522 		return -1;
1523 
1524 	default:
1525 		return -1;
1526 	}
1527 }
1528 
1529 /*
1530  * get offset for the last header in the chain.  m will be kept untainted.
1531  */
1532 int
1533 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
1534 {
1535 	int newoff;
1536 	int nxt;
1537 
1538 	if (!nxtp) {
1539 		nxt = -1;
1540 		nxtp = &nxt;
1541 	}
1542 	for (;;) {
1543 		newoff = ip6_nexthdr(m, off, proto, nxtp);
1544 		if (newoff < 0)
1545 			return off;
1546 		else if (newoff < off)
1547 			return -1;	/* invalid */
1548 		else if (newoff == off)
1549 			return newoff;
1550 
1551 		off = newoff;
1552 		proto = *nxtp;
1553 	}
1554 }
1555 
1556 struct m_tag *
1557 ip6_addaux(struct mbuf *m)
1558 {
1559 	struct m_tag *mtag;
1560 
1561 	mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
1562 	if (!mtag) {
1563 		mtag = m_tag_get(PACKET_TAG_INET6, sizeof(struct ip6aux),
1564 		    M_NOWAIT);
1565 		if (mtag) {
1566 			m_tag_prepend(m, mtag);
1567 			memset(mtag + 1, 0, sizeof(struct ip6aux));
1568 		}
1569 	}
1570 	return mtag;
1571 }
1572 
1573 struct m_tag *
1574 ip6_findaux(struct mbuf *m)
1575 {
1576 	struct m_tag *mtag;
1577 
1578 	mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
1579 	return mtag;
1580 }
1581 
1582 void
1583 ip6_delaux(struct mbuf *m)
1584 {
1585 	struct m_tag *mtag;
1586 
1587 	mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
1588 	if (mtag)
1589 		m_tag_delete(m, mtag);
1590 }
1591 
1592 /*
1593  * System control for IP6
1594  */
1595 
1596 const u_char inet6ctlerrmap[PRC_NCMDS] = {
1597 	0,		0,		0,		0,
1598 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1599 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1600 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1601 	0,		0,		0,		0,
1602 	ENOPROTOOPT
1603 };
1604 
1605 extern int sysctl_net_inet6_addrctlpolicy(SYSCTLFN_ARGS);
1606 
1607 static int
1608 sysctl_net_inet6_ip6_stats(SYSCTLFN_ARGS)
1609 {
1610 
1611 	return (NETSTAT_SYSCTL(ip6stat_percpu, IP6_NSTATS));
1612 }
1613 
1614 static void
1615 sysctl_net_inet6_ip6_setup(struct sysctllog **clog)
1616 {
1617 #ifdef RFC2292
1618 #define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1619 #else
1620 #define IS2292(x, y)	(y)
1621 #endif
1622 
1623 	sysctl_createv(clog, 0, NULL, NULL,
1624 		       CTLFLAG_PERMANENT,
1625 		       CTLTYPE_NODE, "inet6",
1626 		       SYSCTL_DESCR("PF_INET6 related settings"),
1627 		       NULL, 0, NULL, 0,
1628 		       CTL_NET, PF_INET6, CTL_EOL);
1629 	sysctl_createv(clog, 0, NULL, NULL,
1630 		       CTLFLAG_PERMANENT,
1631 		       CTLTYPE_NODE, "ip6",
1632 		       SYSCTL_DESCR("IPv6 related settings"),
1633 		       NULL, 0, NULL, 0,
1634 		       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_EOL);
1635 
1636 	sysctl_createv(clog, 0, NULL, NULL,
1637 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1638 		       CTLTYPE_INT, "forwarding",
1639 		       SYSCTL_DESCR("Enable forwarding of INET6 datagrams"),
1640 		       NULL, 0, &ip6_forwarding, 0,
1641 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1642 		       IPV6CTL_FORWARDING, CTL_EOL);
1643 	sysctl_createv(clog, 0, NULL, NULL,
1644 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1645 		       CTLTYPE_INT, "redirect",
1646 		       SYSCTL_DESCR("Enable sending of ICMPv6 redirect messages"),
1647 		       NULL, 0, &ip6_sendredirects, 0,
1648 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1649 		       IPV6CTL_SENDREDIRECTS, CTL_EOL);
1650 	sysctl_createv(clog, 0, NULL, NULL,
1651 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1652 		       CTLTYPE_INT, "hlim",
1653 		       SYSCTL_DESCR("Hop limit for an INET6 datagram"),
1654 		       NULL, 0, &ip6_defhlim, 0,
1655 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1656 		       IPV6CTL_DEFHLIM, CTL_EOL);
1657 #ifdef notyet
1658 	sysctl_createv(clog, 0, NULL, NULL,
1659 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1660 		       CTLTYPE_INT, "mtu", NULL,
1661 		       NULL, 0, &, 0,
1662 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1663 		       IPV6CTL_DEFMTU, CTL_EOL);
1664 #endif
1665 #ifdef __no_idea__
1666 	sysctl_createv(clog, 0, NULL, NULL,
1667 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1668 		       CTLTYPE_INT, "forwsrcrt", NULL,
1669 		       NULL, 0, &?, 0,
1670 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1671 		       IPV6CTL_FORWSRCRT, CTL_EOL);
1672 	sysctl_createv(clog, 0, NULL, NULL,
1673 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1674 		       CTLTYPE_STRUCT, "mrtstats", NULL,
1675 		       NULL, 0, &?, sizeof(?),
1676 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1677 		       IPV6CTL_MRTSTATS, CTL_EOL);
1678 	sysctl_createv(clog, 0, NULL, NULL,
1679 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1680 		       CTLTYPE_?, "mrtproto", NULL,
1681 		       NULL, 0, &?, sizeof(?),
1682 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1683 		       IPV6CTL_MRTPROTO, CTL_EOL);
1684 #endif
1685 	sysctl_createv(clog, 0, NULL, NULL,
1686 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1687 		       CTLTYPE_INT, "maxfragpackets",
1688 		       SYSCTL_DESCR("Maximum number of fragments to buffer "
1689 				    "for reassembly"),
1690 		       NULL, 0, &ip6_maxfragpackets, 0,
1691 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1692 		       IPV6CTL_MAXFRAGPACKETS, CTL_EOL);
1693 #ifdef __no_idea__
1694 	sysctl_createv(clog, 0, NULL, NULL,
1695 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1696 		       CTLTYPE_INT, "sourcecheck", NULL,
1697 		       NULL, 0, &?, 0,
1698 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1699 		       IPV6CTL_SOURCECHECK, CTL_EOL);
1700 	sysctl_createv(clog, 0, NULL, NULL,
1701 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1702 		       CTLTYPE_INT, "sourcecheck_logint", NULL,
1703 		       NULL, 0, &?, 0,
1704 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1705 		       IPV6CTL_SOURCECHECK_LOGINT, CTL_EOL);
1706 #endif
1707 	sysctl_createv(clog, 0, NULL, NULL,
1708 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1709 		       CTLTYPE_INT, "accept_rtadv",
1710 		       SYSCTL_DESCR("Accept router advertisements"),
1711 		       NULL, 0, &ip6_accept_rtadv, 0,
1712 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1713 		       IPV6CTL_ACCEPT_RTADV, CTL_EOL);
1714 	sysctl_createv(clog, 0, NULL, NULL,
1715 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1716 		       CTLTYPE_INT, "rtadv_maxroutes",
1717 		       SYSCTL_DESCR("Maximum number of routes accepted via router advertisements"),
1718 		       NULL, 0, &ip6_rtadv_maxroutes, 0,
1719 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1720 		       IPV6CTL_RTADV_MAXROUTES, CTL_EOL);
1721 	sysctl_createv(clog, 0, NULL, NULL,
1722 		       CTLFLAG_PERMANENT,
1723 		       CTLTYPE_INT, "rtadv_numroutes",
1724 		       SYSCTL_DESCR("Current number of routes accepted via router advertisements"),
1725 		       NULL, 0, &nd6_numroutes, 0,
1726 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1727 		       IPV6CTL_RTADV_NUMROUTES, CTL_EOL);
1728 	sysctl_createv(clog, 0, NULL, NULL,
1729 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1730 		       CTLTYPE_INT, "keepfaith",
1731 		       SYSCTL_DESCR("Activate faith interface"),
1732 		       NULL, 0, &ip6_keepfaith, 0,
1733 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1734 		       IPV6CTL_KEEPFAITH, CTL_EOL);
1735 	sysctl_createv(clog, 0, NULL, NULL,
1736 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1737 		       CTLTYPE_INT, "log_interval",
1738 		       SYSCTL_DESCR("Minumum interval between logging "
1739 				    "unroutable packets"),
1740 		       NULL, 0, &ip6_log_interval, 0,
1741 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1742 		       IPV6CTL_LOG_INTERVAL, CTL_EOL);
1743 	sysctl_createv(clog, 0, NULL, NULL,
1744 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1745 		       CTLTYPE_INT, "hdrnestlimit",
1746 		       SYSCTL_DESCR("Maximum number of nested IPv6 headers"),
1747 		       NULL, 0, &ip6_hdrnestlimit, 0,
1748 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1749 		       IPV6CTL_HDRNESTLIMIT, CTL_EOL);
1750 	sysctl_createv(clog, 0, NULL, NULL,
1751 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1752 		       CTLTYPE_INT, "dad_count",
1753 		       SYSCTL_DESCR("Number of Duplicate Address Detection "
1754 				    "probes to send"),
1755 		       NULL, 0, &ip6_dad_count, 0,
1756 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1757 		       IPV6CTL_DAD_COUNT, CTL_EOL);
1758 	sysctl_createv(clog, 0, NULL, NULL,
1759 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1760 		       CTLTYPE_INT, "auto_flowlabel",
1761 		       SYSCTL_DESCR("Assign random IPv6 flow labels"),
1762 		       NULL, 0, &ip6_auto_flowlabel, 0,
1763 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1764 		       IPV6CTL_AUTO_FLOWLABEL, CTL_EOL);
1765 	sysctl_createv(clog, 0, NULL, NULL,
1766 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1767 		       CTLTYPE_INT, "defmcasthlim",
1768 		       SYSCTL_DESCR("Default multicast hop limit"),
1769 		       NULL, 0, &ip6_defmcasthlim, 0,
1770 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1771 		       IPV6CTL_DEFMCASTHLIM, CTL_EOL);
1772 	sysctl_createv(clog, 0, NULL, NULL,
1773 		       CTLFLAG_PERMANENT,
1774 		       CTLTYPE_STRING, "kame_version",
1775 		       SYSCTL_DESCR("KAME Version"),
1776 		       NULL, 0, __UNCONST(__KAME_VERSION), 0,
1777 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1778 		       IPV6CTL_KAME_VERSION, CTL_EOL);
1779 	sysctl_createv(clog, 0, NULL, NULL,
1780 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1781 		       CTLTYPE_INT, "use_deprecated",
1782 		       SYSCTL_DESCR("Allow use of deprecated addresses as "
1783 				    "source addresses"),
1784 		       NULL, 0, &ip6_use_deprecated, 0,
1785 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1786 		       IPV6CTL_USE_DEPRECATED, CTL_EOL);
1787 	sysctl_createv(clog, 0, NULL, NULL,
1788 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1789 		       CTLTYPE_INT, "rr_prune", NULL,
1790 		       NULL, 0, &ip6_rr_prune, 0,
1791 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1792 		       IPV6CTL_RR_PRUNE, CTL_EOL);
1793 	sysctl_createv(clog, 0, NULL, NULL,
1794 		       CTLFLAG_PERMANENT
1795 #ifndef INET6_BINDV6ONLY
1796 		       |CTLFLAG_READWRITE,
1797 #endif
1798 		       CTLTYPE_INT, "v6only",
1799 		       SYSCTL_DESCR("Disallow PF_INET6 sockets from connecting "
1800 				    "to PF_INET sockets"),
1801 		       NULL, 0, &ip6_v6only, 0,
1802 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1803 		       IPV6CTL_V6ONLY, CTL_EOL);
1804 	sysctl_createv(clog, 0, NULL, NULL,
1805 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1806 		       CTLTYPE_INT, "anonportmin",
1807 		       SYSCTL_DESCR("Lowest ephemeral port number to assign"),
1808 		       sysctl_net_inet_ip_ports, 0, &ip6_anonportmin, 0,
1809 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1810 		       IPV6CTL_ANONPORTMIN, CTL_EOL);
1811 	sysctl_createv(clog, 0, NULL, NULL,
1812 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1813 		       CTLTYPE_INT, "anonportmax",
1814 		       SYSCTL_DESCR("Highest ephemeral port number to assign"),
1815 		       sysctl_net_inet_ip_ports, 0, &ip6_anonportmax, 0,
1816 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1817 		       IPV6CTL_ANONPORTMAX, CTL_EOL);
1818 #ifndef IPNOPRIVPORTS
1819 	sysctl_createv(clog, 0, NULL, NULL,
1820 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1821 		       CTLTYPE_INT, "lowportmin",
1822 		       SYSCTL_DESCR("Lowest privileged ephemeral port number "
1823 				    "to assign"),
1824 		       sysctl_net_inet_ip_ports, 0, &ip6_lowportmin, 0,
1825 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1826 		       IPV6CTL_LOWPORTMIN, CTL_EOL);
1827 	sysctl_createv(clog, 0, NULL, NULL,
1828 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1829 		       CTLTYPE_INT, "lowportmax",
1830 		       SYSCTL_DESCR("Highest privileged ephemeral port number "
1831 				    "to assign"),
1832 		       sysctl_net_inet_ip_ports, 0, &ip6_lowportmax, 0,
1833 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1834 		       IPV6CTL_LOWPORTMAX, CTL_EOL);
1835 #endif /* IPNOPRIVPORTS */
1836 	sysctl_createv(clog, 0, NULL, NULL,
1837 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1838 		       CTLTYPE_INT, "auto_linklocal",
1839 		       SYSCTL_DESCR("Default value of per-interface flag for "
1840 		                    "adding an IPv6 link-local address to "
1841 				    "interfaces when attached"),
1842 		       NULL, 0, &ip6_auto_linklocal, 0,
1843 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1844 		       IPV6CTL_AUTO_LINKLOCAL, CTL_EOL);
1845 	sysctl_createv(clog, 0, NULL, NULL,
1846 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
1847 		       CTLTYPE_STRUCT, "addctlpolicy",
1848 		       SYSCTL_DESCR("Return the current address control"
1849 			   " policy"),
1850 		       sysctl_net_inet6_addrctlpolicy, 0, NULL, 0,
1851 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1852 		       IPV6CTL_ADDRCTLPOLICY, CTL_EOL);
1853 	sysctl_createv(clog, 0, NULL, NULL,
1854 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1855 		       CTLTYPE_INT, "use_tempaddr",
1856 		       SYSCTL_DESCR("Use temporary address"),
1857 		       NULL, 0, &ip6_use_tempaddr, 0,
1858 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1859 		       CTL_CREATE, CTL_EOL);
1860 	sysctl_createv(clog, 0, NULL, NULL,
1861 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1862 		       CTLTYPE_INT, "prefer_tempaddr",
1863 		       SYSCTL_DESCR("Prefer temporary address as source "
1864 		                    "address"),
1865 		       NULL, 0, &ip6_prefer_tempaddr, 0,
1866 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1867 		       CTL_CREATE, CTL_EOL);
1868 	sysctl_createv(clog, 0, NULL, NULL,
1869 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1870 		       CTLTYPE_INT, "temppltime",
1871 		       SYSCTL_DESCR("preferred lifetime of a temporary address"),
1872 		       NULL, 0, &ip6_temp_preferred_lifetime, 0,
1873 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1874 		       CTL_CREATE, CTL_EOL);
1875 	sysctl_createv(clog, 0, NULL, NULL,
1876 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1877 		       CTLTYPE_INT, "tempvltime",
1878 		       SYSCTL_DESCR("valid lifetime of a temporary address"),
1879 		       NULL, 0, &ip6_temp_valid_lifetime, 0,
1880 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1881 		       CTL_CREATE, CTL_EOL);
1882 	sysctl_createv(clog, 0, NULL, NULL,
1883 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1884 		       CTLTYPE_INT, "maxfrags",
1885 		       SYSCTL_DESCR("Maximum fragments in reassembly queue"),
1886 		       NULL, 0, &ip6_maxfrags, 0,
1887 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1888 		       IPV6CTL_MAXFRAGS, CTL_EOL);
1889 	sysctl_createv(clog, 0, NULL, NULL,
1890 		       CTLFLAG_PERMANENT,
1891 		       CTLTYPE_STRUCT, "stats",
1892 		       SYSCTL_DESCR("IPv6 statistics"),
1893 		       sysctl_net_inet6_ip6_stats, 0, NULL, 0,
1894 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1895 		       IPV6CTL_STATS, CTL_EOL);
1896 	sysctl_createv(clog, 0, NULL, NULL,
1897 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1898 		       CTLTYPE_INT, "use_defaultzone",
1899 		       SYSCTL_DESCR("Whether to use the default scope zones"),
1900 		       NULL, 0, &ip6_use_defzone, 0,
1901 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1902 		       IPV6CTL_USE_DEFAULTZONE, CTL_EOL);
1903 	sysctl_createv(clog, 0, NULL, NULL,
1904 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1905 		       CTLTYPE_INT, "mcast_pmtu",
1906 		       SYSCTL_DESCR("Enable pMTU discovery for multicast packet"),
1907 		       NULL, 0, &ip6_mcast_pmtu, 0,
1908 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1909 		       CTL_CREATE, CTL_EOL);
1910 	/* anonportalgo RFC6056 subtree */
1911 	const struct sysctlnode *portalgo_node;
1912 	sysctl_createv(clog, 0, NULL, &portalgo_node,
1913 		       CTLFLAG_PERMANENT,
1914 		       CTLTYPE_NODE, "anonportalgo",
1915 		       SYSCTL_DESCR("Anonymous port algorithm selection (RFC 6056)"),
1916 	    	       NULL, 0, NULL, 0,
1917 		       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_CREATE, CTL_EOL);
1918 	sysctl_createv(clog, 0, &portalgo_node, NULL,
1919 		       CTLFLAG_PERMANENT,
1920 		       CTLTYPE_STRING, "available",
1921 		       SYSCTL_DESCR("available algorithms"),
1922 		       sysctl_portalgo_available, 0, NULL, PORTALGO_MAXLEN,
1923 		       CTL_CREATE, CTL_EOL);
1924 	sysctl_createv(clog, 0, &portalgo_node, NULL,
1925 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1926 		       CTLTYPE_STRING, "selected",
1927 		       SYSCTL_DESCR("selected algorithm"),
1928 	               sysctl_portalgo_selected6, 0, NULL, PORTALGO_MAXLEN,
1929 		       CTL_CREATE, CTL_EOL);
1930 	sysctl_createv(clog, 0, &portalgo_node, NULL,
1931 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1932 		       CTLTYPE_STRUCT, "reserve",
1933 		       SYSCTL_DESCR("bitmap of reserved ports"),
1934 		       sysctl_portalgo_reserve6, 0, NULL, 0,
1935 		       CTL_CREATE, CTL_EOL);
1936 	sysctl_createv(clog, 0, NULL, NULL,
1937 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1938 		       CTLTYPE_INT, "neighborgcthresh",
1939 		       SYSCTL_DESCR("Maximum number of entries in neighbor"
1940 			" cache"),
1941 		       NULL, 1, &ip6_neighborgcthresh, 0,
1942 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1943 		       CTL_CREATE, CTL_EOL);
1944 	sysctl_createv(clog, 0, NULL, NULL,
1945 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1946 		       CTLTYPE_INT, "maxifprefixes",
1947 		       SYSCTL_DESCR("Maximum number of prefixes created by"
1948 			   " route advertisement per interface"),
1949 		       NULL, 1, &ip6_maxifprefixes, 0,
1950 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1951 		       CTL_CREATE, CTL_EOL);
1952 	sysctl_createv(clog, 0, NULL, NULL,
1953 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1954 		       CTLTYPE_INT, "maxifdefrouters",
1955 		       SYSCTL_DESCR("Maximum number of default routers created"
1956 			   " by route advertisement per interface"),
1957 		       NULL, 1, &ip6_maxifdefrouters, 0,
1958 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1959 		       CTL_CREATE, CTL_EOL);
1960 	sysctl_createv(clog, 0, NULL, NULL,
1961 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1962 		       CTLTYPE_INT, "maxdynroutes",
1963 		       SYSCTL_DESCR("Maximum number of routes created via"
1964 			   " redirect"),
1965 		       NULL, 1, &ip6_maxdynroutes, 0,
1966 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1967 		       CTL_CREATE, CTL_EOL);
1968 }
1969 
1970 void
1971 ip6_statinc(u_int stat)
1972 {
1973 
1974 	KASSERT(stat < IP6_NSTATS);
1975 	IP6_STATINC(stat);
1976 }
1977