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