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