xref: /openbsd-src/sys/netinet6/ip6_input.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: ip6_input.c,v 1.103 2012/04/03 15:03:08 mikeb 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 "pf.h"
65 #include "carp.h"
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/malloc.h>
70 #include <sys/mbuf.h>
71 #include <sys/domain.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/errno.h>
76 #include <sys/time.h>
77 #include <sys/kernel.h>
78 #include <sys/syslog.h>
79 #include <sys/proc.h>
80 
81 #include <net/if.h>
82 #include <net/if_types.h>
83 #include <net/if_dl.h>
84 #include <net/route.h>
85 #include <net/netisr.h>
86 
87 #include <netinet/in.h>
88 #include <netinet/in_systm.h>
89 
90 #ifdef INET
91 #include <netinet/ip.h>
92 #endif
93 
94 #include <netinet/in_pcb.h>
95 #include <netinet6/in6_var.h>
96 #include <netinet/ip6.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet/icmp6.h>
99 #include <netinet6/in6_ifattach.h>
100 #include <netinet6/nd6.h>
101 
102 #include <netinet6/ip6protosw.h>
103 
104 #include "faith.h"
105 #include "gif.h"
106 #include "bpfilter.h"
107 
108 #ifdef MROUTING
109 #include <netinet6/ip6_mroute.h>
110 #endif
111 
112 #if NPF > 0
113 #include <net/pfvar.h>
114 #endif
115 
116 #if NCARP > 0
117 #include <netinet/in_var.h>
118 #include <netinet/ip_carp.h>
119 #endif
120 
121 extern struct domain inet6domain;
122 extern struct ip6protosw inet6sw[];
123 
124 u_char ip6_protox[IPPROTO_MAX];
125 static int ip6qmaxlen = IFQ_MAXLEN;
126 struct in6_ifaddr *in6_ifaddr;
127 struct ifqueue ip6intrq;
128 
129 struct ip6stat ip6stat;
130 
131 void ip6_init2(void *);
132 int ip6_check_rh0hdr(struct mbuf *, int *);
133 
134 int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
135 struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
136 
137 /*
138  * IP6 initialization: fill in IP6 protocol switch table.
139  * All protocols not implemented in kernel go to raw IP6 protocol handler.
140  */
141 void
142 ip6_init(void)
143 {
144 	struct ip6protosw *pr;
145 	int i;
146 
147 	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
148 	if (pr == 0)
149 		panic("ip6_init");
150 	for (i = 0; i < IPPROTO_MAX; i++)
151 		ip6_protox[i] = pr - inet6sw;
152 	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
153 	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
154 		if (pr->pr_domain->dom_family == PF_INET6 &&
155 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW &&
156 		    pr->pr_protocol < IPPROTO_MAX)
157 			ip6_protox[pr->pr_protocol] = pr - inet6sw;
158 	IFQ_SET_MAXLEN(&ip6intrq, ip6qmaxlen);
159 	ip6_randomid_init();
160 	nd6_init();
161 	frag6_init();
162 	ip6_init2((void *)0);
163 }
164 
165 void
166 ip6_init2(void *dummy)
167 {
168 
169 	/* nd6_timer_init */
170 	bzero(&nd6_timer_ch, sizeof(nd6_timer_ch));
171 	timeout_set(&nd6_timer_ch, nd6_timer, NULL);
172 	timeout_add_sec(&nd6_timer_ch, 1);
173 }
174 
175 /*
176  * IP6 input interrupt handling. Just pass the packet to ip6_input.
177  */
178 void
179 ip6intr(void)
180 {
181 	int s;
182 	struct mbuf *m;
183 
184 	for (;;) {
185 		s = splnet();
186 		IF_DEQUEUE(&ip6intrq, m);
187 		splx(s);
188 		if (m == NULL)
189 			return;
190 		ip6_input(m);
191 	}
192 }
193 
194 extern struct	route_in6 ip6_forward_rt;
195 
196 void
197 ip6_input(struct mbuf *m)
198 {
199 	struct ip6_hdr *ip6;
200 	int off, nest;
201 	u_int32_t plen;
202 	u_int32_t rtalert = ~0;
203 	int nxt, ours = 0;
204 	struct ifnet *deliverifp = NULL;
205 #if NPF > 0
206 	struct in6_addr odst;
207 #endif
208 	int srcrt = 0, isanycast = 0;
209 	u_int rtableid = 0;
210 
211 	/*
212 	 * mbuf statistics by kazu
213 	 */
214 	if (m->m_flags & M_EXT) {
215 		if (m->m_next)
216 			ip6stat.ip6s_mext2m++;
217 		else
218 			ip6stat.ip6s_mext1++;
219 	} else {
220 		if (m->m_next) {
221 			if (m->m_flags & M_LOOP) {
222 				ip6stat.ip6s_m2m[lo0ifp->if_index]++;	/*XXX*/
223 			} else if (m->m_pkthdr.rcvif->if_index < nitems(ip6stat.ip6s_m2m))
224 				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
225 			else
226 				ip6stat.ip6s_m2m[0]++;
227 		} else
228 			ip6stat.ip6s_m1++;
229 	}
230 
231 	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
232 	ip6stat.ip6s_total++;
233 
234 	if (m->m_len < sizeof(struct ip6_hdr)) {
235 		struct ifnet *inifp;
236 		inifp = m->m_pkthdr.rcvif;
237 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
238 			ip6stat.ip6s_toosmall++;
239 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
240 			return;
241 		}
242 	}
243 
244 	ip6 = mtod(m, struct ip6_hdr *);
245 
246 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
247 		ip6stat.ip6s_badvers++;
248 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
249 		goto bad;
250 	}
251 
252 #if NCARP > 0
253 	if (m->m_pkthdr.rcvif->if_type == IFT_CARP &&
254 	    ip6->ip6_nxt != IPPROTO_ICMPV6 &&
255 	    carp_lsdrop(m, AF_INET6, ip6->ip6_src.s6_addr32,
256 	    ip6->ip6_dst.s6_addr32))
257 		goto bad;
258 #endif
259 	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
260 
261 	/*
262 	 * Check against address spoofing/corruption.
263 	 */
264 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
265 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
266 		/*
267 		 * XXX: "badscope" is not very suitable for a multicast source.
268 		 */
269 		ip6stat.ip6s_badscope++;
270 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
271 		goto bad;
272 	}
273 	if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
274 	    IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) &&
275 	    (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
276 		    ip6stat.ip6s_badscope++;
277 		    in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
278 		    goto bad;
279 	}
280 	if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
281 	    !(m->m_flags & M_LOOP)) {
282 		/*
283 		 * In this case, the packet should come from the loopback
284 		 * interface.  However, we cannot just check the if_flags,
285 		 * because ip6_mloopback() passes the "actual" interface
286 		 * as the outgoing/incoming interface.
287 		 */
288 		ip6stat.ip6s_badscope++;
289 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
290 		goto bad;
291 	}
292 
293 	/*
294 	 * The following check is not documented in specs.  A malicious
295 	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
296 	 * and bypass security checks (act as if it was from 127.0.0.1 by using
297 	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
298 	 *
299 	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
300 	 * support IPv4-less kernel compilation, we cannot support SIIT
301 	 * environment at all.  So, it makes more sense for us to reject any
302 	 * malicious packets for non-SIIT environment, than try to do a
303 	 * partial support for SIIT environment.
304 	 */
305 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
306 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
307 		ip6stat.ip6s_badscope++;
308 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
309 		goto bad;
310 	}
311 #if 0
312 	/*
313 	 * Reject packets with IPv4 compatible addresses (auto tunnel).
314 	 *
315 	 * The code forbids auto tunnel relay case in RFC1933 (the check is
316 	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
317 	 * is revised to forbid relaying case.
318 	 */
319 	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
320 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
321 		ip6stat.ip6s_badscope++;
322 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
323 		goto bad;
324 	}
325 #endif
326 
327 	if (ip6_check_rh0hdr(m, &off)) {
328 		ip6stat.ip6s_badoptions++;
329 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
330 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
331 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, off);
332 		/* m is already freed */
333 		return;
334 	}
335 
336 #if NPF > 0
337         /*
338          * Packet filter
339          */
340 	odst = ip6->ip6_dst;
341 	if (pf_test(AF_INET6, PF_IN, m->m_pkthdr.rcvif, &m, NULL) != PF_PASS)
342 		goto bad;
343 	if (m == NULL)
344 		return;
345 
346 	ip6 = mtod(m, struct ip6_hdr *);
347 	srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
348 #endif
349 
350 	if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
351 	    IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
352 		ours = 1;
353 		deliverifp = m->m_pkthdr.rcvif;
354 		goto hbhcheck;
355 	}
356 
357 	/* drop packets if interface ID portion is already filled */
358 	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
359 		if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src) &&
360 		    ip6->ip6_src.s6_addr16[1]) {
361 			ip6stat.ip6s_badscope++;
362 			goto bad;
363 		}
364 		if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst) &&
365 		    ip6->ip6_dst.s6_addr16[1]) {
366 			ip6stat.ip6s_badscope++;
367 			goto bad;
368 		}
369 	}
370 
371 	if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
372 		ip6->ip6_src.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
373 	if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
374 		ip6->ip6_dst.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
375 
376 	/*
377 	 * We use rt->rt_ifp to determine if the address is ours or not.
378 	 * If rt_ifp is lo0, the address is ours.
379 	 * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0,
380 	 * so any address under fe80::%lo0/64 will be mistakenly considered
381 	 * local.  The special case is supplied to handle the case properly
382 	 * by actually looking at interface addresses
383 	 * (using in6ifa_ifpwithaddr).
384 	 */
385 	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 &&
386 	    IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
387 		if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) {
388 			icmp6_error(m, ICMP6_DST_UNREACH,
389 			    ICMP6_DST_UNREACH_ADDR, 0);
390 			/* m is already freed */
391 			return;
392 		}
393 
394 		ours = 1;
395 		deliverifp = m->m_pkthdr.rcvif;
396 		goto hbhcheck;
397 	}
398 
399 	if (m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) {
400 		ours = 1;
401 		deliverifp = m->m_pkthdr.rcvif;
402 		goto hbhcheck;
403 	}
404 
405 	/*
406 	 * Multicast check
407 	 */
408 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
409 	  	struct	in6_multi *in6m = 0;
410 
411 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
412 		/*
413 		 * See if we belong to the destination multicast group on the
414 		 * arrival interface.
415 		 */
416 		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
417 		if (in6m)
418 			ours = 1;
419 #ifdef MROUTING
420 		else if (!ip6_mforwarding || !ip6_mrouter)
421 #else
422 		else
423 #endif
424 		{
425 			ip6stat.ip6s_notmember++;
426 			if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
427 				ip6stat.ip6s_cantforward++;
428 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
429 			goto bad;
430 		}
431 		deliverifp = m->m_pkthdr.rcvif;
432 		goto hbhcheck;
433 	}
434 
435 #if NPF > 0
436 	rtableid = m->m_pkthdr.rdomain;
437 #endif
438 
439 	/*
440 	 *  Unicast check
441 	 */
442 	if (ip6_forward_rt.ro_rt != NULL &&
443 	    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
444 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
445 			       &ip6_forward_rt.ro_dst.sin6_addr) &&
446 	    rtableid == ip6_forward_rt.ro_tableid)
447 		ip6stat.ip6s_forward_cachehit++;
448 	else {
449 		if (ip6_forward_rt.ro_rt) {
450 			/* route is down or destination is different */
451 			ip6stat.ip6s_forward_cachemiss++;
452 			RTFREE(ip6_forward_rt.ro_rt);
453 			ip6_forward_rt.ro_rt = 0;
454 		}
455 
456 		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
457 		ip6_forward_rt.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
458 		ip6_forward_rt.ro_dst.sin6_family = AF_INET6;
459 		ip6_forward_rt.ro_dst.sin6_addr = ip6->ip6_dst;
460 		ip6_forward_rt.ro_tableid = rtableid;
461 
462 		rtalloc_mpath((struct route *)&ip6_forward_rt,
463 		    &ip6->ip6_src.s6_addr32[0]);
464 	}
465 
466 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
467 
468 	/*
469 	 * Accept the packet if the forwarding interface to the destination
470 	 * according to the routing table is the loopback interface,
471 	 * unless the associated route has a gateway.
472 	 * Note that this approach causes to accept a packet if there is a
473 	 * route to the loopback interface for the destination of the packet.
474 	 * But we think it's even useful in some situations, e.g. when using
475 	 * a special daemon which wants to intercept the packet.
476 	 */
477 	if (ip6_forward_rt.ro_rt &&
478 	    (ip6_forward_rt.ro_rt->rt_flags &
479 	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
480 #if 0
481 	    /*
482 	     * The check below is redundant since the comparison of
483 	     * the destination and the key of the rtentry has
484 	     * already done through looking up the routing table.
485 	     */
486 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
487 	    &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) &&
488 #endif
489 	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
490 		struct in6_ifaddr *ia6 =
491 			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
492 		if (ia6->ia6_flags & IN6_IFF_ANYCAST)
493 			isanycast = 1;
494 		/*
495 		 * packets to a tentative, duplicated, or somehow invalid
496 		 * address must not be accepted.
497 		 */
498 		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
499 			/* this address is ready */
500 			ours = 1;
501 			deliverifp = ia6->ia_ifp;	/* correct? */
502 			goto hbhcheck;
503 		} else {
504 			/* address is not ready, so discard the packet. */
505 			nd6log((LOG_INFO,
506 			    "ip6_input: packet to an unready address %s->%s\n",
507 			    ip6_sprintf(&ip6->ip6_src),
508 			    ip6_sprintf(&ip6->ip6_dst)));
509 
510 			goto bad;
511 		}
512 	}
513 
514 	/*
515 	 * FAITH (Firewall Aided Internet Translator)
516 	 */
517 #if defined(NFAITH) && 0 < NFAITH
518 	if (ip6_keepfaith) {
519 		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
520 		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
521 			/* XXX do we need more sanity checks? */
522 			ours = 1;
523 			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /*faith*/
524 			goto hbhcheck;
525 		}
526 	}
527 #endif
528 
529 #if 0
530     {
531 	/*
532 	 * Last resort: check in6_ifaddr for incoming interface.
533 	 * The code is here until I update the "goto ours hack" code above
534 	 * working right.
535 	 */
536 	struct ifaddr *ifa;
537 	TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) {
538 		if (ifa->ifa_addr == NULL)
539 			continue;	/* just for safety */
540 		if (ifa->ifa_addr->sa_family != AF_INET6)
541 			continue;
542 		if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
543 			ours = 1;
544 			deliverifp = ifa->ifa_ifp;
545 			goto hbhcheck;
546 		}
547 	}
548     }
549 #endif
550 
551 #if NCARP > 0
552 	if (m->m_pkthdr.rcvif->if_type == IFT_CARP &&
553 	    ip6->ip6_nxt == IPPROTO_ICMPV6 &&
554 	    carp_lsdrop(m, AF_INET6, ip6->ip6_src.s6_addr32,
555 	    ip6->ip6_dst.s6_addr32))
556 		goto bad;
557 #endif
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 		ip6stat.ip6s_cantforward++;
564 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
565 		goto bad;
566 	}
567 
568   hbhcheck:
569 	/*
570 	 * Process Hop-by-Hop options header if it's contained.
571 	 * m may be modified in ip6_hopopts_input().
572 	 * If a JumboPayload option is included, plen will also be modified.
573 	 */
574 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
575 	off = sizeof(struct ip6_hdr);
576 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
577 		struct ip6_hbh *hbh;
578 
579 		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
580 #if 0	/*touches NULL pointer*/
581 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
582 #endif
583 			return;	/* m have already been freed */
584 		}
585 
586 		/* adjust pointer */
587 		ip6 = mtod(m, struct ip6_hdr *);
588 
589 		/*
590 		 * if the payload length field is 0 and the next header field
591 		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
592 		 * option MUST be included.
593 		 */
594 		if (ip6->ip6_plen == 0 && plen == 0) {
595 			/*
596 			 * Note that if a valid jumbo payload option is
597 			 * contained, ip6_hoptops_input() must set a valid
598 			 * (non-zero) payload length to the variable plen.
599 			 */
600 			ip6stat.ip6s_badoptions++;
601 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
602 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
603 			icmp6_error(m, ICMP6_PARAM_PROB,
604 				    ICMP6_PARAMPROB_HEADER,
605 				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
606 			return;
607 		}
608 		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
609 			sizeof(struct ip6_hbh));
610 		if (hbh == NULL) {
611 			ip6stat.ip6s_tooshort++;
612 			return;
613 		}
614 		nxt = hbh->ip6h_nxt;
615 
616 		/*
617 		 * accept the packet if a router alert option is included
618 		 * and we act as an IPv6 router.
619 		 */
620 		if (rtalert != ~0 && ip6_forwarding)
621 			ours = 1;
622 	} else
623 		nxt = ip6->ip6_nxt;
624 
625 	/*
626 	 * Check that the amount of data in the buffers
627 	 * is as at least much as the IPv6 header would have us expect.
628 	 * Trim mbufs if longer than we expect.
629 	 * Drop packet if shorter than we expect.
630 	 */
631 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
632 		ip6stat.ip6s_tooshort++;
633 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
634 		goto bad;
635 	}
636 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
637 		if (m->m_len == m->m_pkthdr.len) {
638 			m->m_len = sizeof(struct ip6_hdr) + plen;
639 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
640 		} else
641 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
642 	}
643 
644 	/*
645 	 * Forward if desirable.
646 	 */
647 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
648 		/*
649 		 * If we are acting as a multicast router, all
650 		 * incoming multicast packets are passed to the
651 		 * kernel-level multicast forwarding function.
652 		 * The packet is returned (relatively) intact; if
653 		 * ip6_mforward() returns a non-zero value, the packet
654 		 * must be discarded, else it may be accepted below.
655 		 */
656 #ifdef MROUTING
657 		if (ip6_mforwarding && ip6_mrouter &&
658 		    ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
659 			ip6stat.ip6s_cantforward++;
660 			m_freem(m);
661 			return;
662 		}
663 #endif
664 		if (!ours) {
665 			m_freem(m);
666 			return;
667 		}
668 	} else if (!ours) {
669 		ip6_forward(m, srcrt);
670 		return;
671 	}
672 
673 	ip6 = mtod(m, struct ip6_hdr *);
674 
675 	/*
676 	 * Malicious party may be able to use IPv4 mapped addr to confuse
677 	 * tcp/udp stack and bypass security checks (act as if it was from
678 	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
679 	 *
680 	 * For SIIT end node behavior, you may want to disable the check.
681 	 * However, you will  become vulnerable to attacks using IPv4 mapped
682 	 * source.
683 	 */
684 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
685 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
686 		ip6stat.ip6s_badscope++;
687 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
688 		goto bad;
689 	}
690 
691 	/*
692 	 * Tell launch routine the next header
693 	 */
694 	ip6stat.ip6s_delivered++;
695 	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
696 	nest = 0;
697 
698 	while (nxt != IPPROTO_DONE) {
699 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
700 			ip6stat.ip6s_toomanyhdr++;
701 			goto bad;
702 		}
703 
704 		/*
705 		 * protection against faulty packet - there should be
706 		 * more sanity checks in header chain processing.
707 		 */
708 		if (m->m_pkthdr.len < off) {
709 			ip6stat.ip6s_tooshort++;
710 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
711 			goto bad;
712 		}
713 
714 		/* draft-itojun-ipv6-tcp-to-anycast */
715 		if (isanycast && nxt == IPPROTO_TCP) {
716 			if (m->m_len >= sizeof(struct ip6_hdr)) {
717 				ip6 = mtod(m, struct ip6_hdr *);
718 				icmp6_error(m, ICMP6_DST_UNREACH,
719 					ICMP6_DST_UNREACH_ADDR,
720 					(caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
721 				break;
722 			} else
723 				goto bad;
724 		}
725 
726 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
727 	}
728 	return;
729  bad:
730 	m_freem(m);
731 }
732 
733 /* scan packet for RH0 routing header. Mostly stolen from pf.c:pf_test() */
734 int
735 ip6_check_rh0hdr(struct mbuf *m, int *offp)
736 {
737 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
738 	struct ip6_rthdr rthdr;
739 	struct ip6_ext opt6;
740 	u_int8_t proto = ip6->ip6_nxt;
741 	int done = 0, lim, off, rh_cnt = 0;
742 
743 	off = ((caddr_t)ip6 - m->m_data) + sizeof(struct ip6_hdr);
744 	lim = min(m->m_pkthdr.len, ntohs(ip6->ip6_plen) + sizeof(*ip6));
745 	do {
746 		switch (proto) {
747 		case IPPROTO_ROUTING:
748 			*offp = off;
749 			if (rh_cnt++) {
750 				/* more then one rh header present */
751 				return (1);
752 			}
753 
754 			if (off + sizeof(rthdr) > lim) {
755 				/* packet to short to make sense */
756 				return (1);
757 			}
758 
759 			m_copydata(m, off, sizeof(rthdr), (caddr_t)&rthdr);
760 
761 			if (rthdr.ip6r_type == IPV6_RTHDR_TYPE_0) {
762 				*offp += offsetof(struct ip6_rthdr, ip6r_type);
763 				return (1);
764 			}
765 
766 			off += (rthdr.ip6r_len + 1) * 8;
767 			proto = rthdr.ip6r_nxt;
768 			break;
769 		case IPPROTO_AH:
770 		case IPPROTO_HOPOPTS:
771 		case IPPROTO_DSTOPTS:
772 			/* get next header and header length */
773 			if (off + sizeof(opt6) > lim) {
774 				/*
775 				 * Packet to short to make sense, we could
776 				 * reject the packet but as a router we
777 				 * should not do that so forward it.
778 				 */
779 				return (0);
780 			}
781 
782 			m_copydata(m, off, sizeof(opt6), (caddr_t)&opt6);
783 
784 			if (proto == IPPROTO_AH)
785 				off += (opt6.ip6e_len + 2) * 4;
786 			else
787 				off += (opt6.ip6e_len + 1) * 8;
788 			proto = opt6.ip6e_nxt;
789 			break;
790 		case IPPROTO_FRAGMENT:
791 		default:
792 			/* end of header stack */
793 			done = 1;
794 			break;
795 		}
796 	} while (!done);
797 
798 	return (0);
799 }
800 
801 /*
802  * Hop-by-Hop options header processing. If a valid jumbo payload option is
803  * included, the real payload length will be stored in plenp.
804  *
805  * rtalertp - XXX: should be stored in a more smart way
806  */
807 int
808 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp, struct mbuf **mp,
809     int *offp)
810 {
811 	struct mbuf *m = *mp;
812 	int off = *offp, hbhlen;
813 	struct ip6_hbh *hbh;
814 
815 	/* validation of the length of the header */
816 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
817 		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
818 	if (hbh == NULL) {
819 		ip6stat.ip6s_tooshort++;
820 		return -1;
821 	}
822 	hbhlen = (hbh->ip6h_len + 1) << 3;
823 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
824 		hbhlen);
825 	if (hbh == NULL) {
826 		ip6stat.ip6s_tooshort++;
827 		return -1;
828 	}
829 	off += hbhlen;
830 	hbhlen -= sizeof(struct ip6_hbh);
831 
832 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
833 				hbhlen, rtalertp, plenp) < 0)
834 		return (-1);
835 
836 	*offp = off;
837 	*mp = m;
838 	return (0);
839 }
840 
841 /*
842  * Search header for all Hop-by-hop options and process each option.
843  * This function is separate from ip6_hopopts_input() in order to
844  * handle a case where the sending node itself process its hop-by-hop
845  * options header. In such a case, the function is called from ip6_output().
846  *
847  * The function assumes that hbh header is located right after the IPv6 header
848  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
849  * opthead + hbhlen is located in continuous memory region.
850  */
851 int
852 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
853     u_int32_t *rtalertp, u_int32_t *plenp)
854 {
855 	struct ip6_hdr *ip6;
856 	int optlen = 0;
857 	u_int8_t *opt = opthead;
858 	u_int16_t rtalert_val;
859 	u_int32_t jumboplen;
860 	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
861 
862 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
863 		switch (*opt) {
864 		case IP6OPT_PAD1:
865 			optlen = 1;
866 			break;
867 		case IP6OPT_PADN:
868 			if (hbhlen < IP6OPT_MINLEN) {
869 				ip6stat.ip6s_toosmall++;
870 				goto bad;
871 			}
872 			optlen = *(opt + 1) + 2;
873 			break;
874 		case IP6OPT_ROUTER_ALERT:
875 			/* XXX may need check for alignment */
876 			if (hbhlen < IP6OPT_RTALERT_LEN) {
877 				ip6stat.ip6s_toosmall++;
878 				goto bad;
879 			}
880 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
881 				/* XXX stat */
882 				icmp6_error(m, ICMP6_PARAM_PROB,
883 				    ICMP6_PARAMPROB_HEADER,
884 				    erroff + opt + 1 - opthead);
885 				return (-1);
886 			}
887 			optlen = IP6OPT_RTALERT_LEN;
888 			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
889 			*rtalertp = ntohs(rtalert_val);
890 			break;
891 		case IP6OPT_JUMBO:
892 			/* XXX may need check for alignment */
893 			if (hbhlen < IP6OPT_JUMBO_LEN) {
894 				ip6stat.ip6s_toosmall++;
895 				goto bad;
896 			}
897 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
898 				/* XXX stat */
899 				icmp6_error(m, ICMP6_PARAM_PROB,
900 				    ICMP6_PARAMPROB_HEADER,
901 				    erroff + opt + 1 - opthead);
902 				return (-1);
903 			}
904 			optlen = IP6OPT_JUMBO_LEN;
905 
906 			/*
907 			 * IPv6 packets that have non 0 payload length
908 			 * must not contain a jumbo payload option.
909 			 */
910 			ip6 = mtod(m, struct ip6_hdr *);
911 			if (ip6->ip6_plen) {
912 				ip6stat.ip6s_badoptions++;
913 				icmp6_error(m, ICMP6_PARAM_PROB,
914 				    ICMP6_PARAMPROB_HEADER,
915 				    erroff + opt - opthead);
916 				return (-1);
917 			}
918 
919 			/*
920 			 * We may see jumbolen in unaligned location, so
921 			 * we'd need to perform bcopy().
922 			 */
923 			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
924 			jumboplen = (u_int32_t)htonl(jumboplen);
925 
926 #if 1
927 			/*
928 			 * if there are multiple jumbo payload options,
929 			 * *plenp will be non-zero and the packet will be
930 			 * rejected.
931 			 * the behavior may need some debate in ipngwg -
932 			 * multiple options does not make sense, however,
933 			 * there's no explicit mention in specification.
934 			 */
935 			if (*plenp != 0) {
936 				ip6stat.ip6s_badoptions++;
937 				icmp6_error(m, ICMP6_PARAM_PROB,
938 				    ICMP6_PARAMPROB_HEADER,
939 				    erroff + opt + 2 - opthead);
940 				return (-1);
941 			}
942 #endif
943 
944 			/*
945 			 * jumbo payload length must be larger than 65535.
946 			 */
947 			if (jumboplen <= IPV6_MAXPACKET) {
948 				ip6stat.ip6s_badoptions++;
949 				icmp6_error(m, ICMP6_PARAM_PROB,
950 				    ICMP6_PARAMPROB_HEADER,
951 				    erroff + opt + 2 - opthead);
952 				return (-1);
953 			}
954 			*plenp = jumboplen;
955 
956 			break;
957 		default:		/* unknown option */
958 			if (hbhlen < IP6OPT_MINLEN) {
959 				ip6stat.ip6s_toosmall++;
960 				goto bad;
961 			}
962 			optlen = ip6_unknown_opt(opt, m,
963 			    erroff + opt - opthead);
964 			if (optlen == -1)
965 				return (-1);
966 			optlen += 2;
967 			break;
968 		}
969 	}
970 
971 	return (0);
972 
973   bad:
974 	m_freem(m);
975 	return (-1);
976 }
977 
978 /*
979  * Unknown option processing.
980  * The third argument `off' is the offset from the IPv6 header to the option,
981  * which allows returning an ICMPv6 error even if the IPv6 header and the
982  * option header are not continuous.
983  */
984 int
985 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
986 {
987 	struct ip6_hdr *ip6;
988 
989 	switch (IP6OPT_TYPE(*optp)) {
990 	case IP6OPT_TYPE_SKIP: /* ignore the option */
991 		return ((int)*(optp + 1));
992 	case IP6OPT_TYPE_DISCARD:	/* silently discard */
993 		m_freem(m);
994 		return (-1);
995 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
996 		ip6stat.ip6s_badoptions++;
997 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
998 		return (-1);
999 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1000 		ip6stat.ip6s_badoptions++;
1001 		ip6 = mtod(m, struct ip6_hdr *);
1002 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1003 		    (m->m_flags & (M_BCAST|M_MCAST)))
1004 			m_freem(m);
1005 		else
1006 			icmp6_error(m, ICMP6_PARAM_PROB,
1007 				    ICMP6_PARAMPROB_OPTION, off);
1008 		return (-1);
1009 	}
1010 
1011 	m_freem(m);		/* XXX: NOTREACHED */
1012 	return (-1);
1013 }
1014 
1015 /*
1016  * Create the "control" list for this pcb.
1017  *
1018  * The routine will be called from upper layer handlers like tcp6_input().
1019  * Thus the routine assumes that the caller (tcp6_input) have already
1020  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1021  * very first mbuf on the mbuf chain.
1022  * We may want to add some infinite loop prevention or sanity checks for safety.
1023  * (This applies only when you are using KAME mbuf chain restriction, i.e.
1024  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
1025  */
1026 void
1027 ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
1028 {
1029 #define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1030 # define in6p_flags	inp_flags
1031 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1032 
1033 #ifdef SO_TIMESTAMP
1034 	if (in6p->inp_socket->so_options & SO_TIMESTAMP) {
1035 		struct timeval tv;
1036 
1037 		microtime(&tv);
1038 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1039 		    SCM_TIMESTAMP, SOL_SOCKET);
1040 		if (*mp)
1041 			mp = &(*mp)->m_next;
1042 	}
1043 #endif
1044 
1045 	/* RFC 2292 sec. 5 */
1046 	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1047 		struct in6_pktinfo pi6;
1048 		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1049 		if (IN6_IS_SCOPE_EMBED(&pi6.ipi6_addr))
1050 			pi6.ipi6_addr.s6_addr16[1] = 0;
1051 		pi6.ipi6_ifindex =
1052 		    (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1053 		*mp = sbcreatecontrol((caddr_t) &pi6,
1054 		    sizeof(struct in6_pktinfo),
1055 		    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1056 		if (*mp)
1057 			mp = &(*mp)->m_next;
1058 	}
1059 
1060 	if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
1061 		int hlim = ip6->ip6_hlim & 0xff;
1062 		*mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1063 		    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1064 		if (*mp)
1065 			mp = &(*mp)->m_next;
1066 	}
1067 
1068 	if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1069 		u_int32_t flowinfo;
1070 		int tclass;
1071 
1072 		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1073 		flowinfo >>= 20;
1074 
1075 		tclass = flowinfo & 0xff;
1076 		*mp = sbcreatecontrol((caddr_t)&tclass, sizeof(tclass),
1077 		    IPV6_TCLASS, IPPROTO_IPV6);
1078 		if (*mp)
1079 			mp = &(*mp)->m_next;
1080 	}
1081 
1082 	/*
1083 	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
1084 	 * privilege for the option (see ip6_ctloutput), but it might be too
1085 	 * strict, since there might be some hop-by-hop options which can be
1086 	 * returned to normal user.
1087 	 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1088 	 */
1089 	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1090 		/*
1091 		 * Check if a hop-by-hop options header is contatined in the
1092 		 * received packet, and if so, store the options as ancillary
1093 		 * data. Note that a hop-by-hop options header must be
1094 		 * just after the IPv6 header, which is assured through the
1095 		 * IPv6 input processing.
1096 		 */
1097 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1098 		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1099 			struct ip6_hbh *hbh;
1100 			int hbhlen = 0;
1101 			struct mbuf *ext;
1102 
1103 			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1104 			    ip6->ip6_nxt);
1105 			if (ext == NULL) {
1106 				ip6stat.ip6s_tooshort++;
1107 				return;
1108 			}
1109 			hbh = mtod(ext, struct ip6_hbh *);
1110 			hbhlen = (hbh->ip6h_len + 1) << 3;
1111 			if (hbhlen != ext->m_len) {
1112 				m_freem(ext);
1113 				ip6stat.ip6s_tooshort++;
1114 				return;
1115 			}
1116 
1117 			/*
1118 			 * XXX: We copy the whole header even if a
1119 			 * jumbo payload option is included, the option which
1120 			 * is to be removed before returning according to
1121 			 * RFC2292.
1122 			 * Note: this constraint is removed in RFC3542.
1123 			 */
1124 			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1125 			    IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1126 			    IPPROTO_IPV6);
1127 			if (*mp)
1128 				mp = &(*mp)->m_next;
1129 			m_freem(ext);
1130 		}
1131 	}
1132 
1133 	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1134 	if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1135 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1136 		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1137 
1138 		/*
1139 		 * Search for destination options headers or routing
1140 		 * header(s) through the header chain, and stores each
1141 		 * header as ancillary data.
1142 		 * Note that the order of the headers remains in
1143 		 * the chain of ancillary data.
1144 		 */
1145 		while (1) {	/* is explicit loop prevention necessary? */
1146 			struct ip6_ext *ip6e = NULL;
1147 			int elen;
1148 			struct mbuf *ext = NULL;
1149 
1150 			/*
1151 			 * if it is not an extension header, don't try to
1152 			 * pull it from the chain.
1153 			 */
1154 			switch (nxt) {
1155 			case IPPROTO_DSTOPTS:
1156 			case IPPROTO_ROUTING:
1157 			case IPPROTO_HOPOPTS:
1158 			case IPPROTO_AH: /* is it possible? */
1159 				break;
1160 			default:
1161 				goto loopend;
1162 			}
1163 
1164 			ext = ip6_pullexthdr(m, off, nxt);
1165 			if (ext == NULL) {
1166 				ip6stat.ip6s_tooshort++;
1167 				return;
1168 			}
1169 			ip6e = mtod(ext, struct ip6_ext *);
1170 			if (nxt == IPPROTO_AH)
1171 				elen = (ip6e->ip6e_len + 2) << 2;
1172 			else
1173 				elen = (ip6e->ip6e_len + 1) << 3;
1174 			if (elen != ext->m_len) {
1175 				m_freem(ext);
1176 				ip6stat.ip6s_tooshort++;
1177 				return;
1178 			}
1179 
1180 			switch (nxt) {
1181 			case IPPROTO_DSTOPTS:
1182 				if (!(in6p->in6p_flags & IN6P_DSTOPTS))
1183 					break;
1184 
1185 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1186 				    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1187 				    IPPROTO_IPV6);
1188 				if (*mp)
1189 					mp = &(*mp)->m_next;
1190 				break;
1191 
1192 			case IPPROTO_ROUTING:
1193 				if (!(in6p->in6p_flags & IN6P_RTHDR))
1194 					break;
1195 
1196 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1197 				    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1198 				    IPPROTO_IPV6);
1199 				if (*mp)
1200 					mp = &(*mp)->m_next;
1201 				break;
1202 
1203 			case IPPROTO_HOPOPTS:
1204 			case IPPROTO_AH: /* is it possible? */
1205 				break;
1206 
1207 			default:
1208 				/*
1209 				 * other cases have been filtered in the above.
1210 				 * none will visit this case.  here we supply
1211 				 * the code just in case (nxt overwritten or
1212 				 * other cases).
1213 				 */
1214 				m_freem(ext);
1215 				goto loopend;
1216 
1217 			}
1218 
1219 			/* proceed with the next header. */
1220 			off += elen;
1221 			nxt = ip6e->ip6e_nxt;
1222 			ip6e = NULL;
1223 			m_freem(ext);
1224 			ext = NULL;
1225 		}
1226 	  loopend:
1227 	  	;
1228 	}
1229 # undef in6p_flags
1230 }
1231 
1232 /*
1233  * pull single extension header from mbuf chain.  returns single mbuf that
1234  * contains the result, or NULL on error.
1235  */
1236 struct mbuf *
1237 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1238 {
1239 	struct ip6_ext ip6e;
1240 	size_t elen;
1241 	struct mbuf *n;
1242 
1243 #ifdef DIAGNOSTIC
1244 	switch (nxt) {
1245 	case IPPROTO_DSTOPTS:
1246 	case IPPROTO_ROUTING:
1247 	case IPPROTO_HOPOPTS:
1248 	case IPPROTO_AH: /* is it possible? */
1249 		break;
1250 	default:
1251 		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1252 	}
1253 #endif
1254 
1255 	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1256 	if (nxt == IPPROTO_AH)
1257 		elen = (ip6e.ip6e_len + 2) << 2;
1258 	else
1259 		elen = (ip6e.ip6e_len + 1) << 3;
1260 
1261 	MGET(n, M_DONTWAIT, MT_DATA);
1262 	if (n && elen >= MLEN) {
1263 		MCLGET(n, M_DONTWAIT);
1264 		if ((n->m_flags & M_EXT) == 0) {
1265 			m_free(n);
1266 			n = NULL;
1267 		}
1268 	}
1269 	if (!n)
1270 		return NULL;
1271 
1272 	n->m_len = 0;
1273 	if (elen >= M_TRAILINGSPACE(n)) {
1274 		m_free(n);
1275 		return NULL;
1276 	}
1277 
1278 	m_copydata(m, off, elen, mtod(n, caddr_t));
1279 	n->m_len = elen;
1280 	return n;
1281 }
1282 
1283 /*
1284  * Get pointer to the previous header followed by the header
1285  * currently processed.
1286  * XXX: This function supposes that
1287  *	M includes all headers,
1288  *	the next header field and the header length field of each header
1289  *	are valid, and
1290  *	the sum of each header length equals to OFF.
1291  * Because of these assumptions, this function must be called very
1292  * carefully. Moreover, it will not be used in the near future when
1293  * we develop `neater' mechanism to process extension headers.
1294  */
1295 u_int8_t *
1296 ip6_get_prevhdr(struct mbuf *m, int off)
1297 {
1298 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1299 
1300 	if (off == sizeof(struct ip6_hdr))
1301 		return (&ip6->ip6_nxt);
1302 	else {
1303 		int len, nxt;
1304 		struct ip6_ext *ip6e = NULL;
1305 
1306 		nxt = ip6->ip6_nxt;
1307 		len = sizeof(struct ip6_hdr);
1308 		while (len < off) {
1309 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1310 
1311 			switch (nxt) {
1312 			case IPPROTO_FRAGMENT:
1313 				len += sizeof(struct ip6_frag);
1314 				break;
1315 			case IPPROTO_AH:
1316 				len += (ip6e->ip6e_len + 2) << 2;
1317 				break;
1318 			default:
1319 				len += (ip6e->ip6e_len + 1) << 3;
1320 				break;
1321 			}
1322 			nxt = ip6e->ip6e_nxt;
1323 		}
1324 		if (ip6e)
1325 			return (&ip6e->ip6e_nxt);
1326 		else
1327 			return NULL;
1328 	}
1329 }
1330 
1331 /*
1332  * get next header offset.  m will be retained.
1333  */
1334 int
1335 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
1336 {
1337 	struct ip6_hdr ip6;
1338 	struct ip6_ext ip6e;
1339 	struct ip6_frag fh;
1340 
1341 	/* just in case */
1342 	if (m == NULL)
1343 		panic("ip6_nexthdr: m == NULL");
1344 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1345 		return -1;
1346 
1347 	switch (proto) {
1348 	case IPPROTO_IPV6:
1349 		if (m->m_pkthdr.len < off + sizeof(ip6))
1350 			return -1;
1351 		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1352 		if (nxtp)
1353 			*nxtp = ip6.ip6_nxt;
1354 		off += sizeof(ip6);
1355 		return off;
1356 
1357 	case IPPROTO_FRAGMENT:
1358 		/*
1359 		 * terminate parsing if it is not the first fragment,
1360 		 * it does not make sense to parse through it.
1361 		 */
1362 		if (m->m_pkthdr.len < off + sizeof(fh))
1363 			return -1;
1364 		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1365 		if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0)
1366 			return -1;
1367 		if (nxtp)
1368 			*nxtp = fh.ip6f_nxt;
1369 		off += sizeof(struct ip6_frag);
1370 		return off;
1371 
1372 	case IPPROTO_AH:
1373 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1374 			return -1;
1375 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1376 		if (nxtp)
1377 			*nxtp = ip6e.ip6e_nxt;
1378 		off += (ip6e.ip6e_len + 2) << 2;
1379 		if (m->m_pkthdr.len < off)
1380 			return -1;
1381 		return off;
1382 
1383 	case IPPROTO_HOPOPTS:
1384 	case IPPROTO_ROUTING:
1385 	case IPPROTO_DSTOPTS:
1386 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1387 			return -1;
1388 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1389 		if (nxtp)
1390 			*nxtp = ip6e.ip6e_nxt;
1391 		off += (ip6e.ip6e_len + 1) << 3;
1392 		if (m->m_pkthdr.len < off)
1393 			return -1;
1394 		return off;
1395 
1396 	case IPPROTO_NONE:
1397 	case IPPROTO_ESP:
1398 	case IPPROTO_IPCOMP:
1399 		/* give up */
1400 		return -1;
1401 
1402 	default:
1403 		return -1;
1404 	}
1405 
1406 	return -1;
1407 }
1408 
1409 /*
1410  * get offset for the last header in the chain.  m will be kept untainted.
1411  */
1412 int
1413 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
1414 {
1415 	int newoff;
1416 	int nxt;
1417 
1418 	if (!nxtp) {
1419 		nxt = -1;
1420 		nxtp = &nxt;
1421 	}
1422 	while (1) {
1423 		newoff = ip6_nexthdr(m, off, proto, nxtp);
1424 		if (newoff < 0)
1425 			return off;
1426 		else if (newoff < off)
1427 			return -1;	/* invalid */
1428 		else if (newoff == off)
1429 			return newoff;
1430 
1431 		off = newoff;
1432 		proto = *nxtp;
1433 	}
1434 }
1435 
1436 /*
1437  * System control for IP6
1438  */
1439 
1440 u_char	inet6ctlerrmap[PRC_NCMDS] = {
1441 	0,		0,		0,		0,
1442 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1443 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1444 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1445 	0,		0,		0,		0,
1446 	ENOPROTOOPT
1447 };
1448 
1449 #include <uvm/uvm_extern.h>
1450 #include <sys/sysctl.h>
1451 
1452 int *ipv6ctl_vars[IPV6CTL_MAXID] = IPV6CTL_VARS;
1453 
1454 int
1455 ip6_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
1456     void *newp, size_t newlen)
1457 {
1458 #ifdef MROUTING
1459 	extern int ip6_mrtproto;
1460 	extern struct mrt6stat mrt6stat;
1461 #endif
1462 
1463 	/* All sysctl names at this level are terminal. */
1464 	if (namelen != 1)
1465 		return ENOTDIR;
1466 
1467 	switch (name[0]) {
1468 	case IPV6CTL_KAME_VERSION:
1469 		return sysctl_rdstring(oldp, oldlenp, newp, __KAME_VERSION);
1470 	case IPV6CTL_V6ONLY:
1471 		return sysctl_rdint(oldp, oldlenp, newp, ip6_v6only);
1472 	case IPV6CTL_DAD_PENDING:
1473 		return sysctl_rdint(oldp, oldlenp, newp, ip6_dad_pending);
1474 	case IPV6CTL_STATS:
1475 		if (newp != NULL)
1476 			return (EPERM);
1477 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
1478 		    &ip6stat, sizeof(ip6stat)));
1479 	case IPV6CTL_MRTSTATS:
1480 #ifdef MROUTING
1481 		if (newp != NULL)
1482 			return (EPERM);
1483 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
1484 		    &mrt6stat, sizeof(mrt6stat)));
1485 #else
1486 		return (EOPNOTSUPP);
1487 #endif
1488 	case IPV6CTL_MRTPROTO:
1489 #ifdef MROUTING
1490 		return sysctl_rdint(oldp, oldlenp, newp, ip6_mrtproto);
1491 #else
1492 		return (EOPNOTSUPP);
1493 #endif
1494 	default:
1495 		if (name[0] < IPV6CTL_MAXID)
1496 			return (sysctl_int_arr(ipv6ctl_vars, name, namelen,
1497 			    oldp, oldlenp, newp, newlen));
1498 		return (EOPNOTSUPP);
1499 	}
1500 	/* NOTREACHED */
1501 }
1502