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