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