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