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