xref: /netbsd-src/sys/netinet6/ip6_input.c (revision 975a152cfcdb39ae6e496af647af0c7275ca0b61)
1 /*	$NetBSD: ip6_input.c,v 1.143 2013/06/29 21:06:58 rmind 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.143 2013/06/29 21:06:58 rmind 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 
810 	mtag = ip6_addaux(m);
811 	if (mtag != NULL) {
812 		struct ip6aux *ip6a;
813 
814 		ip6a = (struct ip6aux *)(mtag + 1);
815 		in6_setscope(&ip6a->ip6a_src, ia->ia_ifp, &ip6a->ip6a_scope_id);
816 		ip6a->ip6a_src = ia->ia_addr.sin6_addr;
817 		ip6a->ip6a_flags = ia->ia6_flags;
818 	}
819 	return mtag;	/* NULL if failed to set */
820 }
821 
822 const struct ip6aux *
823 ip6_getdstifaddr(struct mbuf *m)
824 {
825 	struct m_tag *mtag;
826 
827 	mtag = ip6_findaux(m);
828 	if (mtag != NULL)
829 		return (struct ip6aux *)(mtag + 1);
830 	else
831 		return NULL;
832 }
833 
834 /*
835  * Hop-by-Hop options header processing. If a valid jumbo payload option is
836  * included, the real payload length will be stored in plenp.
837  *
838  * rtalertp - XXX: should be stored more smart way
839  */
840 int
841 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
842 	struct mbuf **mp, int *offp)
843 {
844 	struct mbuf *m = *mp;
845 	int off = *offp, hbhlen;
846 	struct ip6_hbh *hbh;
847 
848 	/* validation of the length of the header */
849 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
850 		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
851 	if (hbh == NULL) {
852 		IP6_STATINC(IP6_STAT_TOOSHORT);
853 		return -1;
854 	}
855 	hbhlen = (hbh->ip6h_len + 1) << 3;
856 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
857 		hbhlen);
858 	if (hbh == NULL) {
859 		IP6_STATINC(IP6_STAT_TOOSHORT);
860 		return -1;
861 	}
862 	KASSERT(IP6_HDR_ALIGNED_P(hbh));
863 	off += hbhlen;
864 	hbhlen -= sizeof(struct ip6_hbh);
865 
866 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
867 				hbhlen, rtalertp, plenp) < 0)
868 		return (-1);
869 
870 	*offp = off;
871 	*mp = m;
872 	return (0);
873 }
874 
875 /*
876  * Search header for all Hop-by-hop options and process each option.
877  * This function is separate from ip6_hopopts_input() in order to
878  * handle a case where the sending node itself process its hop-by-hop
879  * options header. In such a case, the function is called from ip6_output().
880  *
881  * The function assumes that hbh header is located right after the IPv6 header
882  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
883  * opthead + hbhlen is located in continuous memory region.
884  */
885 static int
886 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
887 	u_int32_t *rtalertp, u_int32_t *plenp)
888 {
889 	struct ip6_hdr *ip6;
890 	int optlen = 0;
891 	u_int8_t *opt = opthead;
892 	u_int16_t rtalert_val;
893 	u_int32_t jumboplen;
894 	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
895 
896 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
897 		switch (*opt) {
898 		case IP6OPT_PAD1:
899 			optlen = 1;
900 			break;
901 		case IP6OPT_PADN:
902 			if (hbhlen < IP6OPT_MINLEN) {
903 				IP6_STATINC(IP6_STAT_TOOSMALL);
904 				goto bad;
905 			}
906 			optlen = *(opt + 1) + 2;
907 			break;
908 		case IP6OPT_RTALERT:
909 			/* XXX may need check for alignment */
910 			if (hbhlen < IP6OPT_RTALERT_LEN) {
911 				IP6_STATINC(IP6_STAT_TOOSMALL);
912 				goto bad;
913 			}
914 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
915 				/* XXX stat */
916 				icmp6_error(m, ICMP6_PARAM_PROB,
917 				    ICMP6_PARAMPROB_HEADER,
918 				    erroff + opt + 1 - opthead);
919 				return (-1);
920 			}
921 			optlen = IP6OPT_RTALERT_LEN;
922 			memcpy((void *)&rtalert_val, (void *)(opt + 2), 2);
923 			*rtalertp = ntohs(rtalert_val);
924 			break;
925 		case IP6OPT_JUMBO:
926 			/* XXX may need check for alignment */
927 			if (hbhlen < IP6OPT_JUMBO_LEN) {
928 				IP6_STATINC(IP6_STAT_TOOSMALL);
929 				goto bad;
930 			}
931 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
932 				/* XXX stat */
933 				icmp6_error(m, ICMP6_PARAM_PROB,
934 				    ICMP6_PARAMPROB_HEADER,
935 				    erroff + opt + 1 - opthead);
936 				return (-1);
937 			}
938 			optlen = IP6OPT_JUMBO_LEN;
939 
940 			/*
941 			 * IPv6 packets that have non 0 payload length
942 			 * must not contain a jumbo payload option.
943 			 */
944 			ip6 = mtod(m, struct ip6_hdr *);
945 			if (ip6->ip6_plen) {
946 				IP6_STATINC(IP6_STAT_BADOPTIONS);
947 				icmp6_error(m, ICMP6_PARAM_PROB,
948 				    ICMP6_PARAMPROB_HEADER,
949 				    erroff + opt - opthead);
950 				return (-1);
951 			}
952 
953 			/*
954 			 * We may see jumbolen in unaligned location, so
955 			 * we'd need to perform bcopy().
956 			 */
957 			memcpy(&jumboplen, opt + 2, sizeof(jumboplen));
958 			jumboplen = (u_int32_t)htonl(jumboplen);
959 
960 #if 1
961 			/*
962 			 * if there are multiple jumbo payload options,
963 			 * *plenp will be non-zero and the packet will be
964 			 * rejected.
965 			 * the behavior may need some debate in ipngwg -
966 			 * multiple options does not make sense, however,
967 			 * there's no explicit mention in specification.
968 			 */
969 			if (*plenp != 0) {
970 				IP6_STATINC(IP6_STAT_BADOPTIONS);
971 				icmp6_error(m, ICMP6_PARAM_PROB,
972 				    ICMP6_PARAMPROB_HEADER,
973 				    erroff + opt + 2 - opthead);
974 				return (-1);
975 			}
976 #endif
977 
978 			/*
979 			 * jumbo payload length must be larger than 65535.
980 			 */
981 			if (jumboplen <= IPV6_MAXPACKET) {
982 				IP6_STATINC(IP6_STAT_BADOPTIONS);
983 				icmp6_error(m, ICMP6_PARAM_PROB,
984 				    ICMP6_PARAMPROB_HEADER,
985 				    erroff + opt + 2 - opthead);
986 				return (-1);
987 			}
988 			*plenp = jumboplen;
989 
990 			break;
991 		default:		/* unknown option */
992 			if (hbhlen < IP6OPT_MINLEN) {
993 				IP6_STATINC(IP6_STAT_TOOSMALL);
994 				goto bad;
995 			}
996 			optlen = ip6_unknown_opt(opt, m,
997 			    erroff + opt - opthead);
998 			if (optlen == -1)
999 				return (-1);
1000 			optlen += 2;
1001 			break;
1002 		}
1003 	}
1004 
1005 	return (0);
1006 
1007   bad:
1008 	m_freem(m);
1009 	return (-1);
1010 }
1011 
1012 /*
1013  * Unknown option processing.
1014  * The third argument `off' is the offset from the IPv6 header to the option,
1015  * which is necessary if the IPv6 header the and option header and IPv6 header
1016  * is not continuous in order to return an ICMPv6 error.
1017  */
1018 int
1019 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1020 {
1021 	struct ip6_hdr *ip6;
1022 
1023 	switch (IP6OPT_TYPE(*optp)) {
1024 	case IP6OPT_TYPE_SKIP: /* ignore the option */
1025 		return ((int)*(optp + 1));
1026 	case IP6OPT_TYPE_DISCARD:	/* silently discard */
1027 		m_freem(m);
1028 		return (-1);
1029 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1030 		IP6_STATINC(IP6_STAT_BADOPTIONS);
1031 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1032 		return (-1);
1033 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1034 		IP6_STATINC(IP6_STAT_BADOPTIONS);
1035 		ip6 = mtod(m, struct ip6_hdr *);
1036 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1037 		    (m->m_flags & (M_BCAST|M_MCAST)))
1038 			m_freem(m);
1039 		else
1040 			icmp6_error(m, ICMP6_PARAM_PROB,
1041 				    ICMP6_PARAMPROB_OPTION, off);
1042 		return (-1);
1043 	}
1044 
1045 	m_freem(m);		/* XXX: NOTREACHED */
1046 	return (-1);
1047 }
1048 
1049 /*
1050  * Create the "control" list for this pcb.
1051  *
1052  * The routine will be called from upper layer handlers like tcp6_input().
1053  * Thus the routine assumes that the caller (tcp6_input) have already
1054  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1055  * very first mbuf on the mbuf chain.
1056  * We may want to add some infinite loop prevention or sanity checks for safety.
1057  * (This applies only when you are using KAME mbuf chain restriction, i.e.
1058  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
1059  */
1060 void
1061 ip6_savecontrol(struct in6pcb *in6p, struct mbuf **mp,
1062 	struct ip6_hdr *ip6, struct mbuf *m)
1063 {
1064 #ifdef RFC2292
1065 #define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1066 #else
1067 #define IS2292(x, y)	(y)
1068 #endif
1069 
1070 	if (in6p->in6p_socket->so_options & SO_TIMESTAMP
1071 #ifdef SO_OTIMESTAMP
1072 	    || in6p->in6p_socket->so_options & SO_OTIMESTAMP
1073 #endif
1074 	) {
1075 		struct timeval tv;
1076 
1077 		microtime(&tv);
1078 #ifdef SO_OTIMESTAMP
1079 		if (in6p->in6p_socket->so_options & SO_OTIMESTAMP) {
1080 			struct timeval50 tv50;
1081 			timeval_to_timeval50(&tv, &tv50);
1082 			*mp = sbcreatecontrol((void *) &tv50, sizeof(tv50),
1083 			    SCM_OTIMESTAMP, SOL_SOCKET);
1084 		} else
1085 #endif
1086 		*mp = sbcreatecontrol((void *) &tv, sizeof(tv),
1087 		    SCM_TIMESTAMP, SOL_SOCKET);
1088 		if (*mp)
1089 			mp = &(*mp)->m_next;
1090 	}
1091 
1092 	/* some OSes call this logic with IPv4 packet, for SO_TIMESTAMP */
1093 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
1094 		return;
1095 
1096 	/* RFC 2292 sec. 5 */
1097 	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1098 		struct in6_pktinfo pi6;
1099 
1100 		memcpy(&pi6.ipi6_addr, &ip6->ip6_dst, sizeof(struct in6_addr));
1101 		in6_clearscope(&pi6.ipi6_addr);	/* XXX */
1102 		pi6.ipi6_ifindex = m->m_pkthdr.rcvif ?
1103 		    m->m_pkthdr.rcvif->if_index : 0;
1104 		*mp = sbcreatecontrol((void *) &pi6,
1105 		    sizeof(struct in6_pktinfo),
1106 		    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1107 		if (*mp)
1108 			mp = &(*mp)->m_next;
1109 	}
1110 
1111 	if (in6p->in6p_flags & IN6P_HOPLIMIT) {
1112 		int hlim = ip6->ip6_hlim & 0xff;
1113 
1114 		*mp = sbcreatecontrol((void *) &hlim, sizeof(int),
1115 		    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1116 		if (*mp)
1117 			mp = &(*mp)->m_next;
1118 	}
1119 
1120 	if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1121 		u_int32_t flowinfo;
1122 		int tclass;
1123 
1124 		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1125 		flowinfo >>= 20;
1126 
1127 		tclass = flowinfo & 0xff;
1128 		*mp = sbcreatecontrol((void *)&tclass, sizeof(tclass),
1129 		    IPV6_TCLASS, IPPROTO_IPV6);
1130 
1131 		if (*mp)
1132 			mp = &(*mp)->m_next;
1133 	}
1134 
1135 	/*
1136 	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
1137 	 * privilege for the option (see ip6_ctloutput), but it might be too
1138 	 * strict, since there might be some hop-by-hop options which can be
1139 	 * returned to normal user.
1140 	 * See also RFC3542 section 8 (or RFC2292 section 6).
1141 	 */
1142 	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1143 		/*
1144 		 * Check if a hop-by-hop options header is contatined in the
1145 		 * received packet, and if so, store the options as ancillary
1146 		 * data. Note that a hop-by-hop options header must be
1147 		 * just after the IPv6 header, which fact is assured through
1148 		 * the IPv6 input processing.
1149 		 */
1150 		struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
1151 		if (xip6->ip6_nxt == IPPROTO_HOPOPTS) {
1152 			struct ip6_hbh *hbh;
1153 			int hbhlen;
1154 			struct mbuf *ext;
1155 
1156 			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1157 			    xip6->ip6_nxt);
1158 			if (ext == NULL) {
1159 				IP6_STATINC(IP6_STAT_TOOSHORT);
1160 				return;
1161 			}
1162 			hbh = mtod(ext, struct ip6_hbh *);
1163 			hbhlen = (hbh->ip6h_len + 1) << 3;
1164 			if (hbhlen != ext->m_len) {
1165 				m_freem(ext);
1166 				IP6_STATINC(IP6_STAT_TOOSHORT);
1167 				return;
1168 			}
1169 
1170 			/*
1171 			 * XXX: We copy whole the header even if a jumbo
1172 			 * payload option is included, which option is to
1173 			 * be removed before returning in the RFC 2292.
1174 			 * Note: this constraint is removed in RFC3542.
1175 			 */
1176 			*mp = sbcreatecontrol((void *)hbh, hbhlen,
1177 			    IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1178 			    IPPROTO_IPV6);
1179 			if (*mp)
1180 				mp = &(*mp)->m_next;
1181 			m_freem(ext);
1182 		}
1183 	}
1184 
1185 	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1186 	if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
1187 		struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
1188 		int nxt = xip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1189 
1190 		/*
1191 		 * Search for destination options headers or routing
1192 		 * header(s) through the header chain, and stores each
1193 		 * header as ancillary data.
1194 		 * Note that the order of the headers remains in
1195 		 * the chain of ancillary data.
1196 		 */
1197 		for (;;) {	/* is explicit loop prevention necessary? */
1198 			struct ip6_ext *ip6e = NULL;
1199 			int elen;
1200 			struct mbuf *ext = NULL;
1201 
1202 			/*
1203 			 * if it is not an extension header, don't try to
1204 			 * pull it from the chain.
1205 			 */
1206 			switch (nxt) {
1207 			case IPPROTO_DSTOPTS:
1208 			case IPPROTO_ROUTING:
1209 			case IPPROTO_HOPOPTS:
1210 			case IPPROTO_AH: /* is it possible? */
1211 				break;
1212 			default:
1213 				goto loopend;
1214 			}
1215 
1216 			ext = ip6_pullexthdr(m, off, nxt);
1217 			if (ext == NULL) {
1218 				IP6_STATINC(IP6_STAT_TOOSHORT);
1219 				return;
1220 			}
1221 			ip6e = mtod(ext, struct ip6_ext *);
1222 			if (nxt == IPPROTO_AH)
1223 				elen = (ip6e->ip6e_len + 2) << 2;
1224 			else
1225 				elen = (ip6e->ip6e_len + 1) << 3;
1226 			if (elen != ext->m_len) {
1227 				m_freem(ext);
1228 				IP6_STATINC(IP6_STAT_TOOSHORT);
1229 				return;
1230 			}
1231 			KASSERT(IP6_HDR_ALIGNED_P(ip6e));
1232 
1233 			switch (nxt) {
1234 			case IPPROTO_DSTOPTS:
1235 				if (!(in6p->in6p_flags & IN6P_DSTOPTS))
1236 					break;
1237 
1238 				*mp = sbcreatecontrol((void *)ip6e, elen,
1239 				    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1240 				    IPPROTO_IPV6);
1241 				if (*mp)
1242 					mp = &(*mp)->m_next;
1243 				break;
1244 
1245 			case IPPROTO_ROUTING:
1246 				if (!(in6p->in6p_flags & IN6P_RTHDR))
1247 					break;
1248 
1249 				*mp = sbcreatecontrol((void *)ip6e, elen,
1250 				    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1251 				    IPPROTO_IPV6);
1252 				if (*mp)
1253 					mp = &(*mp)->m_next;
1254 				break;
1255 
1256 			case IPPROTO_HOPOPTS:
1257 			case IPPROTO_AH: /* is it possible? */
1258 				break;
1259 
1260 			default:
1261 				/*
1262 			 	 * other cases have been filtered in the above.
1263 				 * none will visit this case.  here we supply
1264 				 * the code just in case (nxt overwritten or
1265 				 * other cases).
1266 				 */
1267 				m_freem(ext);
1268 				goto loopend;
1269 
1270 			}
1271 
1272 			/* proceed with the next header. */
1273 			off += elen;
1274 			nxt = ip6e->ip6e_nxt;
1275 			ip6e = NULL;
1276 			m_freem(ext);
1277 			ext = NULL;
1278 		}
1279 	  loopend:
1280 	  	;
1281 	}
1282 }
1283 #undef IS2292
1284 
1285 
1286 void
1287 ip6_notify_pmtu(struct in6pcb *in6p, const struct sockaddr_in6 *dst,
1288     uint32_t *mtu)
1289 {
1290 	struct socket *so;
1291 	struct mbuf *m_mtu;
1292 	struct ip6_mtuinfo mtuctl;
1293 
1294 	so = in6p->in6p_socket;
1295 
1296 	if (mtu == NULL)
1297 		return;
1298 
1299 #ifdef DIAGNOSTIC
1300 	if (so == NULL)		/* I believe this is impossible */
1301 		panic("ip6_notify_pmtu: socket is NULL");
1302 #endif
1303 
1304 	memset(&mtuctl, 0, sizeof(mtuctl));	/* zero-clear for safety */
1305 	mtuctl.ip6m_mtu = *mtu;
1306 	mtuctl.ip6m_addr = *dst;
1307 	if (sa6_recoverscope(&mtuctl.ip6m_addr))
1308 		return;
1309 
1310 	if ((m_mtu = sbcreatecontrol((void *)&mtuctl, sizeof(mtuctl),
1311 	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1312 		return;
1313 
1314 	if (sbappendaddr(&so->so_rcv, (const struct sockaddr *)dst, NULL, m_mtu)
1315 	    == 0) {
1316 		m_freem(m_mtu);
1317 		/* XXX: should count statistics */
1318 	} else
1319 		sorwakeup(so);
1320 
1321 	return;
1322 }
1323 
1324 /*
1325  * pull single extension header from mbuf chain.  returns single mbuf that
1326  * contains the result, or NULL on error.
1327  */
1328 static struct mbuf *
1329 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1330 {
1331 	struct ip6_ext ip6e;
1332 	size_t elen;
1333 	struct mbuf *n;
1334 
1335 #ifdef DIAGNOSTIC
1336 	switch (nxt) {
1337 	case IPPROTO_DSTOPTS:
1338 	case IPPROTO_ROUTING:
1339 	case IPPROTO_HOPOPTS:
1340 	case IPPROTO_AH: /* is it possible? */
1341 		break;
1342 	default:
1343 		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1344 	}
1345 #endif
1346 
1347 	m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1348 	if (nxt == IPPROTO_AH)
1349 		elen = (ip6e.ip6e_len + 2) << 2;
1350 	else
1351 		elen = (ip6e.ip6e_len + 1) << 3;
1352 
1353 	MGET(n, M_DONTWAIT, MT_DATA);
1354 	if (n && elen >= MLEN) {
1355 		MCLGET(n, M_DONTWAIT);
1356 		if ((n->m_flags & M_EXT) == 0) {
1357 			m_free(n);
1358 			n = NULL;
1359 		}
1360 	}
1361 	if (!n)
1362 		return NULL;
1363 
1364 	n->m_len = 0;
1365 	if (elen >= M_TRAILINGSPACE(n)) {
1366 		m_free(n);
1367 		return NULL;
1368 	}
1369 
1370 	m_copydata(m, off, elen, mtod(n, void *));
1371 	n->m_len = elen;
1372 	return n;
1373 }
1374 
1375 /*
1376  * Get pointer to the previous header followed by the header
1377  * currently processed.
1378  * XXX: This function supposes that
1379  *	M includes all headers,
1380  *	the next header field and the header length field of each header
1381  *	are valid, and
1382  *	the sum of each header length equals to OFF.
1383  * Because of these assumptions, this function must be called very
1384  * carefully. Moreover, it will not be used in the near future when
1385  * we develop `neater' mechanism to process extension headers.
1386  */
1387 u_int8_t *
1388 ip6_get_prevhdr(struct mbuf *m, int off)
1389 {
1390 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1391 
1392 	if (off == sizeof(struct ip6_hdr))
1393 		return (&ip6->ip6_nxt);
1394 	else {
1395 		int len, nxt;
1396 		struct ip6_ext *ip6e = NULL;
1397 
1398 		nxt = ip6->ip6_nxt;
1399 		len = sizeof(struct ip6_hdr);
1400 		while (len < off) {
1401 			ip6e = (struct ip6_ext *)(mtod(m, char *) + len);
1402 
1403 			switch (nxt) {
1404 			case IPPROTO_FRAGMENT:
1405 				len += sizeof(struct ip6_frag);
1406 				break;
1407 			case IPPROTO_AH:
1408 				len += (ip6e->ip6e_len + 2) << 2;
1409 				break;
1410 			default:
1411 				len += (ip6e->ip6e_len + 1) << 3;
1412 				break;
1413 			}
1414 			nxt = ip6e->ip6e_nxt;
1415 		}
1416 		if (ip6e)
1417 			return (&ip6e->ip6e_nxt);
1418 		else
1419 			return NULL;
1420 	}
1421 }
1422 
1423 /*
1424  * get next header offset.  m will be retained.
1425  */
1426 int
1427 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
1428 {
1429 	struct ip6_hdr ip6;
1430 	struct ip6_ext ip6e;
1431 	struct ip6_frag fh;
1432 
1433 	/* just in case */
1434 	if (m == NULL)
1435 		panic("ip6_nexthdr: m == NULL");
1436 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1437 		return -1;
1438 
1439 	switch (proto) {
1440 	case IPPROTO_IPV6:
1441 		/* do not chase beyond intermediate IPv6 headers */
1442 		if (off != 0)
1443 			return -1;
1444 		if (m->m_pkthdr.len < off + sizeof(ip6))
1445 			return -1;
1446 		m_copydata(m, off, sizeof(ip6), (void *)&ip6);
1447 		if (nxtp)
1448 			*nxtp = ip6.ip6_nxt;
1449 		off += sizeof(ip6);
1450 		return off;
1451 
1452 	case IPPROTO_FRAGMENT:
1453 		/*
1454 		 * terminate parsing if it is not the first fragment,
1455 		 * it does not make sense to parse through it.
1456 		 */
1457 		if (m->m_pkthdr.len < off + sizeof(fh))
1458 			return -1;
1459 		m_copydata(m, off, sizeof(fh), (void *)&fh);
1460 		if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0)
1461 			return -1;
1462 		if (nxtp)
1463 			*nxtp = fh.ip6f_nxt;
1464 		off += sizeof(struct ip6_frag);
1465 		return off;
1466 
1467 	case IPPROTO_AH:
1468 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1469 			return -1;
1470 		m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1471 		if (nxtp)
1472 			*nxtp = ip6e.ip6e_nxt;
1473 		off += (ip6e.ip6e_len + 2) << 2;
1474 		if (m->m_pkthdr.len < off)
1475 			return -1;
1476 		return off;
1477 
1478 	case IPPROTO_HOPOPTS:
1479 	case IPPROTO_ROUTING:
1480 	case IPPROTO_DSTOPTS:
1481 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1482 			return -1;
1483 		m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1484 		if (nxtp)
1485 			*nxtp = ip6e.ip6e_nxt;
1486 		off += (ip6e.ip6e_len + 1) << 3;
1487 		if (m->m_pkthdr.len < off)
1488 			return -1;
1489 		return off;
1490 
1491 	case IPPROTO_NONE:
1492 	case IPPROTO_ESP:
1493 	case IPPROTO_IPCOMP:
1494 		/* give up */
1495 		return -1;
1496 
1497 	default:
1498 		return -1;
1499 	}
1500 }
1501 
1502 /*
1503  * get offset for the last header in the chain.  m will be kept untainted.
1504  */
1505 int
1506 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
1507 {
1508 	int newoff;
1509 	int nxt;
1510 
1511 	if (!nxtp) {
1512 		nxt = -1;
1513 		nxtp = &nxt;
1514 	}
1515 	for (;;) {
1516 		newoff = ip6_nexthdr(m, off, proto, nxtp);
1517 		if (newoff < 0)
1518 			return off;
1519 		else if (newoff < off)
1520 			return -1;	/* invalid */
1521 		else if (newoff == off)
1522 			return newoff;
1523 
1524 		off = newoff;
1525 		proto = *nxtp;
1526 	}
1527 }
1528 
1529 struct m_tag *
1530 ip6_addaux(struct mbuf *m)
1531 {
1532 	struct m_tag *mtag;
1533 
1534 	mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
1535 	if (!mtag) {
1536 		mtag = m_tag_get(PACKET_TAG_INET6, sizeof(struct ip6aux),
1537 		    M_NOWAIT);
1538 		if (mtag) {
1539 			m_tag_prepend(m, mtag);
1540 			memset(mtag + 1, 0, sizeof(struct ip6aux));
1541 		}
1542 	}
1543 	return mtag;
1544 }
1545 
1546 struct m_tag *
1547 ip6_findaux(struct mbuf *m)
1548 {
1549 	struct m_tag *mtag;
1550 
1551 	mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
1552 	return mtag;
1553 }
1554 
1555 void
1556 ip6_delaux(struct mbuf *m)
1557 {
1558 	struct m_tag *mtag;
1559 
1560 	mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
1561 	if (mtag)
1562 		m_tag_delete(m, mtag);
1563 }
1564 
1565 #ifdef GATEWAY
1566 /*
1567  * sysctl helper routine for net.inet.ip6.maxflows. Since
1568  * we could reduce this value, call ip6flow_reap();
1569  */
1570 static int
1571 sysctl_net_inet6_ip6_maxflows(SYSCTLFN_ARGS)
1572 {
1573 	int error;
1574 
1575 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
1576 	if (error || newp == NULL)
1577 		return (error);
1578 
1579 	mutex_enter(softnet_lock);
1580 	KERNEL_LOCK(1, NULL);
1581 
1582 	ip6flow_reap(0);
1583 
1584 	KERNEL_UNLOCK_ONE(NULL);
1585 	mutex_exit(softnet_lock);
1586 
1587 	return (0);
1588 }
1589 
1590 static int
1591 sysctl_net_inet6_ip6_hashsize(SYSCTLFN_ARGS)
1592 {
1593 	int error, tmp;
1594 	struct sysctlnode node;
1595 
1596 	node = *rnode;
1597 	tmp = ip6_hashsize;
1598 	node.sysctl_data = &tmp;
1599 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1600 	if (error || newp == NULL)
1601 		return (error);
1602 
1603 	if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
1604 		/*
1605 		 * Can only fail due to malloc()
1606 		 */
1607 		mutex_enter(softnet_lock);
1608 		KERNEL_LOCK(1, NULL);
1609 
1610 		error = ip6flow_invalidate_all(tmp);
1611 
1612 		KERNEL_UNLOCK_ONE(NULL);
1613 		mutex_exit(softnet_lock);
1614 	} else {
1615 		/*
1616 		 * EINVAL if not a power of 2
1617 		 */
1618 		error = EINVAL;
1619 	}
1620 
1621 	return error;
1622 }
1623 #endif /* GATEWAY */
1624 
1625 /*
1626  * System control for IP6
1627  */
1628 
1629 const u_char inet6ctlerrmap[PRC_NCMDS] = {
1630 	0,		0,		0,		0,
1631 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1632 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1633 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1634 	0,		0,		0,		0,
1635 	ENOPROTOOPT
1636 };
1637 
1638 static int
1639 sysctl_net_inet6_ip6_stats(SYSCTLFN_ARGS)
1640 {
1641 
1642 	return (NETSTAT_SYSCTL(ip6stat_percpu, IP6_NSTATS));
1643 }
1644 
1645 static void
1646 sysctl_net_inet6_ip6_setup(struct sysctllog **clog)
1647 {
1648 #ifdef RFC2292
1649 #define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1650 #else
1651 #define IS2292(x, y)	(y)
1652 #endif
1653 
1654 	sysctl_createv(clog, 0, NULL, NULL,
1655 		       CTLFLAG_PERMANENT,
1656 		       CTLTYPE_NODE, "net", NULL,
1657 		       NULL, 0, NULL, 0,
1658 		       CTL_NET, CTL_EOL);
1659 	sysctl_createv(clog, 0, NULL, NULL,
1660 		       CTLFLAG_PERMANENT,
1661 		       CTLTYPE_NODE, "inet6",
1662 		       SYSCTL_DESCR("PF_INET6 related settings"),
1663 		       NULL, 0, NULL, 0,
1664 		       CTL_NET, PF_INET6, CTL_EOL);
1665 	sysctl_createv(clog, 0, NULL, NULL,
1666 		       CTLFLAG_PERMANENT,
1667 		       CTLTYPE_NODE, "ip6",
1668 		       SYSCTL_DESCR("IPv6 related settings"),
1669 		       NULL, 0, NULL, 0,
1670 		       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_EOL);
1671 
1672 	sysctl_createv(clog, 0, NULL, NULL,
1673 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1674 		       CTLTYPE_INT, "forwarding",
1675 		       SYSCTL_DESCR("Enable forwarding of INET6 datagrams"),
1676 		       NULL, 0, &ip6_forwarding, 0,
1677 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1678 		       IPV6CTL_FORWARDING, CTL_EOL);
1679 	sysctl_createv(clog, 0, NULL, NULL,
1680 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1681 		       CTLTYPE_INT, "redirect",
1682 		       SYSCTL_DESCR("Enable sending of ICMPv6 redirect messages"),
1683 		       NULL, 0, &ip6_sendredirects, 0,
1684 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1685 		       IPV6CTL_SENDREDIRECTS, CTL_EOL);
1686 	sysctl_createv(clog, 0, NULL, NULL,
1687 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1688 		       CTLTYPE_INT, "hlim",
1689 		       SYSCTL_DESCR("Hop limit for an INET6 datagram"),
1690 		       NULL, 0, &ip6_defhlim, 0,
1691 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1692 		       IPV6CTL_DEFHLIM, CTL_EOL);
1693 #ifdef notyet
1694 	sysctl_createv(clog, 0, NULL, NULL,
1695 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1696 		       CTLTYPE_INT, "mtu", NULL,
1697 		       NULL, 0, &, 0,
1698 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1699 		       IPV6CTL_DEFMTU, CTL_EOL);
1700 #endif
1701 #ifdef __no_idea__
1702 	sysctl_createv(clog, 0, NULL, NULL,
1703 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1704 		       CTLTYPE_INT, "forwsrcrt", NULL,
1705 		       NULL, 0, &?, 0,
1706 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1707 		       IPV6CTL_FORWSRCRT, CTL_EOL);
1708 	sysctl_createv(clog, 0, NULL, NULL,
1709 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1710 		       CTLTYPE_STRUCT, "mrtstats", NULL,
1711 		       NULL, 0, &?, sizeof(?),
1712 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1713 		       IPV6CTL_MRTSTATS, CTL_EOL);
1714 	sysctl_createv(clog, 0, NULL, NULL,
1715 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1716 		       CTLTYPE_?, "mrtproto", NULL,
1717 		       NULL, 0, &?, sizeof(?),
1718 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1719 		       IPV6CTL_MRTPROTO, CTL_EOL);
1720 #endif
1721 	sysctl_createv(clog, 0, NULL, NULL,
1722 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1723 		       CTLTYPE_INT, "maxfragpackets",
1724 		       SYSCTL_DESCR("Maximum number of fragments to buffer "
1725 				    "for reassembly"),
1726 		       NULL, 0, &ip6_maxfragpackets, 0,
1727 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1728 		       IPV6CTL_MAXFRAGPACKETS, CTL_EOL);
1729 #ifdef __no_idea__
1730 	sysctl_createv(clog, 0, NULL, NULL,
1731 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1732 		       CTLTYPE_INT, "sourcecheck", NULL,
1733 		       NULL, 0, &?, 0,
1734 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1735 		       IPV6CTL_SOURCECHECK, CTL_EOL);
1736 	sysctl_createv(clog, 0, NULL, NULL,
1737 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1738 		       CTLTYPE_INT, "sourcecheck_logint", NULL,
1739 		       NULL, 0, &?, 0,
1740 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1741 		       IPV6CTL_SOURCECHECK_LOGINT, CTL_EOL);
1742 #endif
1743 	sysctl_createv(clog, 0, NULL, NULL,
1744 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1745 		       CTLTYPE_INT, "accept_rtadv",
1746 		       SYSCTL_DESCR("Accept router advertisements"),
1747 		       NULL, 0, &ip6_accept_rtadv, 0,
1748 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1749 		       IPV6CTL_ACCEPT_RTADV, CTL_EOL);
1750 	sysctl_createv(clog, 0, NULL, NULL,
1751 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1752 		       CTLTYPE_INT, "rtadv_maxroutes",
1753 		       SYSCTL_DESCR("Maximum number of routes accepted via router advertisements"),
1754 		       NULL, 0, &ip6_rtadv_maxroutes, 0,
1755 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1756 		       IPV6CTL_RTADV_MAXROUTES, CTL_EOL);
1757 	sysctl_createv(clog, 0, NULL, NULL,
1758 		       CTLFLAG_PERMANENT,
1759 		       CTLTYPE_INT, "rtadv_numroutes",
1760 		       SYSCTL_DESCR("Current number of routes accepted via router advertisements"),
1761 		       NULL, 0, &nd6_numroutes, 0,
1762 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1763 		       IPV6CTL_RTADV_NUMROUTES, CTL_EOL);
1764 	sysctl_createv(clog, 0, NULL, NULL,
1765 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1766 		       CTLTYPE_INT, "keepfaith",
1767 		       SYSCTL_DESCR("Activate faith interface"),
1768 		       NULL, 0, &ip6_keepfaith, 0,
1769 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1770 		       IPV6CTL_KEEPFAITH, CTL_EOL);
1771 	sysctl_createv(clog, 0, NULL, NULL,
1772 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1773 		       CTLTYPE_INT, "log_interval",
1774 		       SYSCTL_DESCR("Minumum interval between logging "
1775 				    "unroutable packets"),
1776 		       NULL, 0, &ip6_log_interval, 0,
1777 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1778 		       IPV6CTL_LOG_INTERVAL, CTL_EOL);
1779 	sysctl_createv(clog, 0, NULL, NULL,
1780 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1781 		       CTLTYPE_INT, "hdrnestlimit",
1782 		       SYSCTL_DESCR("Maximum number of nested IPv6 headers"),
1783 		       NULL, 0, &ip6_hdrnestlimit, 0,
1784 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1785 		       IPV6CTL_HDRNESTLIMIT, CTL_EOL);
1786 	sysctl_createv(clog, 0, NULL, NULL,
1787 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1788 		       CTLTYPE_INT, "dad_count",
1789 		       SYSCTL_DESCR("Number of Duplicate Address Detection "
1790 				    "probes to send"),
1791 		       NULL, 0, &ip6_dad_count, 0,
1792 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1793 		       IPV6CTL_DAD_COUNT, CTL_EOL);
1794 	sysctl_createv(clog, 0, NULL, NULL,
1795 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1796 		       CTLTYPE_INT, "auto_flowlabel",
1797 		       SYSCTL_DESCR("Assign random IPv6 flow labels"),
1798 		       NULL, 0, &ip6_auto_flowlabel, 0,
1799 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1800 		       IPV6CTL_AUTO_FLOWLABEL, CTL_EOL);
1801 	sysctl_createv(clog, 0, NULL, NULL,
1802 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1803 		       CTLTYPE_INT, "defmcasthlim",
1804 		       SYSCTL_DESCR("Default multicast hop limit"),
1805 		       NULL, 0, &ip6_defmcasthlim, 0,
1806 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1807 		       IPV6CTL_DEFMCASTHLIM, CTL_EOL);
1808 #if NGIF > 0
1809 	sysctl_createv(clog, 0, NULL, NULL,
1810 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1811 		       CTLTYPE_INT, "gifhlim",
1812 		       SYSCTL_DESCR("Default hop limit for a gif tunnel datagram"),
1813 		       NULL, 0, &ip6_gif_hlim, 0,
1814 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1815 		       IPV6CTL_GIF_HLIM, CTL_EOL);
1816 #endif /* NGIF */
1817 	sysctl_createv(clog, 0, NULL, NULL,
1818 		       CTLFLAG_PERMANENT,
1819 		       CTLTYPE_STRING, "kame_version",
1820 		       SYSCTL_DESCR("KAME Version"),
1821 		       NULL, 0, __UNCONST(__KAME_VERSION), 0,
1822 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1823 		       IPV6CTL_KAME_VERSION, CTL_EOL);
1824 	sysctl_createv(clog, 0, NULL, NULL,
1825 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1826 		       CTLTYPE_INT, "use_deprecated",
1827 		       SYSCTL_DESCR("Allow use of deprecated addresses as "
1828 				    "source addresses"),
1829 		       NULL, 0, &ip6_use_deprecated, 0,
1830 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1831 		       IPV6CTL_USE_DEPRECATED, CTL_EOL);
1832 	sysctl_createv(clog, 0, NULL, NULL,
1833 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1834 		       CTLTYPE_INT, "rr_prune", NULL,
1835 		       NULL, 0, &ip6_rr_prune, 0,
1836 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1837 		       IPV6CTL_RR_PRUNE, CTL_EOL);
1838 	sysctl_createv(clog, 0, NULL, NULL,
1839 		       CTLFLAG_PERMANENT
1840 #ifndef INET6_BINDV6ONLY
1841 		       |CTLFLAG_READWRITE,
1842 #endif
1843 		       CTLTYPE_INT, "v6only",
1844 		       SYSCTL_DESCR("Disallow PF_INET6 sockets from connecting "
1845 				    "to PF_INET sockets"),
1846 		       NULL, 0, &ip6_v6only, 0,
1847 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1848 		       IPV6CTL_V6ONLY, CTL_EOL);
1849 	sysctl_createv(clog, 0, NULL, NULL,
1850 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1851 		       CTLTYPE_INT, "anonportmin",
1852 		       SYSCTL_DESCR("Lowest ephemeral port number to assign"),
1853 		       sysctl_net_inet_ip_ports, 0, &ip6_anonportmin, 0,
1854 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1855 		       IPV6CTL_ANONPORTMIN, CTL_EOL);
1856 	sysctl_createv(clog, 0, NULL, NULL,
1857 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1858 		       CTLTYPE_INT, "anonportmax",
1859 		       SYSCTL_DESCR("Highest ephemeral port number to assign"),
1860 		       sysctl_net_inet_ip_ports, 0, &ip6_anonportmax, 0,
1861 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1862 		       IPV6CTL_ANONPORTMAX, CTL_EOL);
1863 #ifndef IPNOPRIVPORTS
1864 	sysctl_createv(clog, 0, NULL, NULL,
1865 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1866 		       CTLTYPE_INT, "lowportmin",
1867 		       SYSCTL_DESCR("Lowest privileged ephemeral port number "
1868 				    "to assign"),
1869 		       sysctl_net_inet_ip_ports, 0, &ip6_lowportmin, 0,
1870 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1871 		       IPV6CTL_LOWPORTMIN, CTL_EOL);
1872 	sysctl_createv(clog, 0, NULL, NULL,
1873 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1874 		       CTLTYPE_INT, "lowportmax",
1875 		       SYSCTL_DESCR("Highest privileged ephemeral port number "
1876 				    "to assign"),
1877 		       sysctl_net_inet_ip_ports, 0, &ip6_lowportmax, 0,
1878 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1879 		       IPV6CTL_LOWPORTMAX, CTL_EOL);
1880 #endif /* IPNOPRIVPORTS */
1881 	sysctl_createv(clog, 0, NULL, NULL,
1882 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1883 		       CTLTYPE_INT, "use_tempaddr",
1884 		       SYSCTL_DESCR("Use temporary address"),
1885 		       NULL, 0, &ip6_use_tempaddr, 0,
1886 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1887 		       CTL_CREATE, CTL_EOL);
1888 	sysctl_createv(clog, 0, NULL, NULL,
1889 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1890 		       CTLTYPE_INT, "temppltime",
1891 		       SYSCTL_DESCR("preferred lifetime of a temporary address"),
1892 		       NULL, 0, &ip6_temp_preferred_lifetime, 0,
1893 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1894 		       CTL_CREATE, CTL_EOL);
1895 	sysctl_createv(clog, 0, NULL, NULL,
1896 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1897 		       CTLTYPE_INT, "tempvltime",
1898 		       SYSCTL_DESCR("valid lifetime of a temporary address"),
1899 		       NULL, 0, &ip6_temp_valid_lifetime, 0,
1900 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1901 		       CTL_CREATE, CTL_EOL);
1902 	sysctl_createv(clog, 0, NULL, NULL,
1903 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1904 		       CTLTYPE_INT, "maxfrags",
1905 		       SYSCTL_DESCR("Maximum fragments in reassembly queue"),
1906 		       NULL, 0, &ip6_maxfrags, 0,
1907 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1908 		       IPV6CTL_MAXFRAGS, CTL_EOL);
1909 	sysctl_createv(clog, 0, NULL, NULL,
1910 		       CTLFLAG_PERMANENT,
1911 		       CTLTYPE_STRUCT, "stats",
1912 		       SYSCTL_DESCR("IPv6 statistics"),
1913 		       sysctl_net_inet6_ip6_stats, 0, NULL, 0,
1914 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1915 		       IPV6CTL_STATS, CTL_EOL);
1916 	sysctl_createv(clog, 0, NULL, NULL,
1917 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1918 		       CTLTYPE_INT, "use_defaultzone",
1919 		       SYSCTL_DESCR("Whether to use the default scope zones"),
1920 		       NULL, 0, &ip6_use_defzone, 0,
1921 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1922 		       IPV6CTL_USE_DEFAULTZONE, CTL_EOL);
1923 	sysctl_createv(clog, 0, NULL, NULL,
1924 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1925 		       CTLTYPE_INT, "mcast_pmtu",
1926 		       SYSCTL_DESCR("Enable pMTU discovery for multicast packet"),
1927 		       NULL, 0, &ip6_mcast_pmtu, 0,
1928 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1929 		       CTL_CREATE, CTL_EOL);
1930 #ifdef GATEWAY
1931 	sysctl_createv(clog, 0, NULL, NULL,
1932 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1933 			CTLTYPE_INT, "maxflows",
1934 			SYSCTL_DESCR("Number of flows for fast forwarding (IPv6)"),
1935 			sysctl_net_inet6_ip6_maxflows, 0, &ip6_maxflows, 0,
1936 			CTL_NET, PF_INET6, IPPROTO_IPV6,
1937 			CTL_CREATE, CTL_EOL);
1938 	sysctl_createv(clog, 0, NULL, NULL,
1939 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1940 			CTLTYPE_INT, "hashsize",
1941 			SYSCTL_DESCR("Size of hash table for fast forwarding (IPv6)"),
1942 			sysctl_net_inet6_ip6_hashsize, 0, &ip6_hashsize, 0,
1943 			CTL_NET, PF_INET6, IPPROTO_IPV6,
1944 			CTL_CREATE, CTL_EOL);
1945 #endif
1946 	/* anonportalgo RFC6056 subtree */
1947 	const struct sysctlnode *portalgo_node;
1948 	sysctl_createv(clog, 0, NULL, &portalgo_node,
1949 		       CTLFLAG_PERMANENT,
1950 		       CTLTYPE_NODE, "anonportalgo",
1951 		       SYSCTL_DESCR("Anonymous port algorithm selection (RFC 6056)"),
1952 	    	       NULL, 0, NULL, 0,
1953 		       CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_CREATE, CTL_EOL);
1954 	sysctl_createv(clog, 0, &portalgo_node, NULL,
1955 		       CTLFLAG_PERMANENT,
1956 		       CTLTYPE_STRING, "available",
1957 		       SYSCTL_DESCR("available algorithms"),
1958 		       sysctl_portalgo_available, 0, NULL, PORTALGO_MAXLEN,
1959 		       CTL_CREATE, CTL_EOL);
1960 	sysctl_createv(clog, 0, &portalgo_node, NULL,
1961 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1962 		       CTLTYPE_STRING, "selected",
1963 		       SYSCTL_DESCR("selected algorithm"),
1964 	               sysctl_portalgo_selected6, 0, NULL, PORTALGO_MAXLEN,
1965 		       CTL_CREATE, CTL_EOL);
1966 	sysctl_createv(clog, 0, &portalgo_node, NULL,
1967 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1968 		       CTLTYPE_STRUCT, "reserve",
1969 		       SYSCTL_DESCR("bitmap of reserved ports"),
1970 		       sysctl_portalgo_reserve6, 0, NULL, 0,
1971 		       CTL_CREATE, CTL_EOL);
1972 	sysctl_createv(clog, 0, NULL, NULL,
1973 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1974 		       CTLTYPE_INT, "neighborgcthresh",
1975 		       SYSCTL_DESCR("Maximum number of entries in neighbor"
1976 			" cache"),
1977 		       NULL, 1, &ip6_neighborgcthresh, 0,
1978 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1979 		       CTL_CREATE, CTL_EOL);
1980 	sysctl_createv(clog, 0, NULL, NULL,
1981 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1982 		       CTLTYPE_INT, "maxifprefixes",
1983 		       SYSCTL_DESCR("Maximum number of prefixes created by"
1984 			   " route advertisement per interface"),
1985 		       NULL, 1, &ip6_maxifprefixes, 0,
1986 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1987 		       CTL_CREATE, CTL_EOL);
1988 	sysctl_createv(clog, 0, NULL, NULL,
1989 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1990 		       CTLTYPE_INT, "maxifdefrouters",
1991 		       SYSCTL_DESCR("Maximum number of default routers created"
1992 			   " by route advertisement per interface"),
1993 		       NULL, 1, &ip6_maxifdefrouters, 0,
1994 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
1995 		       CTL_CREATE, CTL_EOL);
1996 	sysctl_createv(clog, 0, NULL, NULL,
1997 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1998 		       CTLTYPE_INT, "maxdynroutes",
1999 		       SYSCTL_DESCR("Maximum number of routes created via"
2000 			   " redirect"),
2001 		       NULL, 1, &ip6_maxdynroutes, 0,
2002 		       CTL_NET, PF_INET6, IPPROTO_IPV6,
2003 		       CTL_CREATE, CTL_EOL);
2004 }
2005 
2006 void
2007 ip6_statinc(u_int stat)
2008 {
2009 
2010 	KASSERT(stat < IP6_NSTATS);
2011 	IP6_STATINC(stat);
2012 }
2013