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