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