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