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