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