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