xref: /freebsd/sys/netinet6/ip6_input.c (revision 27beb2e98db3193bd22010b9eb00cc7787bb0a2f)
1 /*	$FreeBSD$	*/
2 /*	$KAME: ip6_input.c,v 1.95 2000/07/02 07:49:37 jinmei 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 "opt_ip6fw.h"
69 #include "opt_inet.h"
70 #include "opt_inet6.h"
71 #include "opt_ipsec.h"
72 #include "opt_pfil_hooks.h"
73 
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/mbuf.h>
77 #include <sys/domain.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/errno.h>
82 #include <sys/time.h>
83 #include <sys/kernel.h>
84 #include <sys/syslog.h>
85 
86 #include <net/if.h>
87 #include <net/if_types.h>
88 #include <net/if_dl.h>
89 #include <net/route.h>
90 #include <net/netisr.h>
91 #include <net/intrq.h>
92 #ifdef PFIL_HOOKS
93 #include <net/pfil.h>
94 #endif
95 
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #ifdef INET
99 #include <netinet/ip.h>
100 #include <netinet/ip_icmp.h>
101 #endif /*INET*/
102 #include <netinet/ip6.h>
103 #include <netinet6/in6_var.h>
104 #include <netinet6/ip6_var.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet/icmp6.h>
107 #include <netinet6/in6_ifattach.h>
108 #include <netinet6/nd6.h>
109 #include <netinet6/in6_prefix.h>
110 
111 #include <netinet6/ip6_fw.h>
112 
113 #include <netinet6/ip6protosw.h>
114 
115 /* we need it for NLOOP. */
116 #include "loop.h"
117 #include "faith.h"
118 #include "gif.h"
119 
120 #include <net/net_osdep.h>
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 
129 int ip6_forward_srcrt;			/* XXX */
130 int ip6_sourcecheck;			/* XXX */
131 int ip6_sourcecheck_interval;		/* XXX */
132 const int int6intrq_present = 1;
133 
134 /* firewall hooks */
135 ip6_fw_chk_t *ip6_fw_chk_ptr;
136 ip6_fw_ctl_t *ip6_fw_ctl_ptr;
137 int ip6_fw_enable = 1;
138 
139 struct ip6stat ip6stat;
140 
141 static void ip6_init2 __P((void *));
142 
143 static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
144 #ifdef PULLDOWN_TEST
145 static struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int));
146 #endif
147 
148 /*
149  * IP6 initialization: fill in IP6 protocol switch table.
150  * All protocols not implemented in kernel go to raw IP6 protocol handler.
151  */
152 void
153 ip6_init()
154 {
155 	register struct ip6protosw *pr;
156 	register int i;
157 	struct timeval tv;
158 
159 	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
160 	if (pr == 0)
161 		panic("ip6_init");
162 	for (i = 0; i < IPPROTO_MAX; i++)
163 		ip6_protox[i] = pr - inet6sw;
164 	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
165 	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
166 		if (pr->pr_domain->dom_family == PF_INET6 &&
167 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
168 			ip6_protox[pr->pr_protocol] = pr - inet6sw;
169 	ip6intrq.ifq_maxlen = ip6qmaxlen;
170 	register_netisr(NETISR_IPV6, ip6intr);
171 	nd6_init();
172 	frag6_init();
173 	/*
174 	 * in many cases, random() here does NOT return random number
175 	 * as initialization during bootstrap time occur in fixed order.
176 	 */
177 	microtime(&tv);
178 	ip6_flow_seq = random() ^ tv.tv_usec;
179 }
180 
181 static void
182 ip6_init2(dummy)
183 	void *dummy;
184 {
185 
186 	/*
187 	 * to route local address of p2p link to loopback,
188 	 * assign loopback address first.
189 	 */
190 	in6_ifattach(&loif[0], NULL);
191 
192 	/* nd6_timer_init */
193 	timeout(nd6_timer, (caddr_t)0, hz);
194 	/* router renumbering prefix list maintenance */
195 	timeout(in6_rr_timer, (caddr_t)0, hz);
196 }
197 
198 /* cheat */
199 /* This must be after route_init(), which is now SI_ORDER_THIRD */
200 SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
201 
202 /*
203  * IP6 input interrupt handling. Just pass the packet to ip6_input.
204  */
205 void
206 ip6intr()
207 {
208 	int s;
209 	struct mbuf *m;
210 
211 	for (;;) {
212 		s = splimp();
213 		IF_DEQUEUE(&ip6intrq, m);
214 		splx(s);
215 		if (m == 0)
216 			return;
217 		ip6_input(m);
218 	}
219 }
220 
221 extern struct	route_in6 ip6_forward_rt;
222 
223 void
224 ip6_input(m)
225 	struct mbuf *m;
226 {
227 	struct ip6_hdr *ip6;
228 	int off = sizeof(struct ip6_hdr), nest;
229 	u_int32_t plen;
230 	u_int32_t rtalert = ~0;
231 	int nxt, ours = 0;
232 	struct ifnet *deliverifp = NULL;
233 #ifdef  PFIL_HOOKS
234 	struct packet_filter_hook *pfh;
235 	struct mbuf *m0;
236 	int rv;
237 #endif  /* PFIL_HOOKS */
238 
239 #ifdef IPSEC
240 	/*
241 	 * should the inner packet be considered authentic?
242 	 * see comment in ah4_input().
243 	 */
244 	if (m) {
245 		m->m_flags &= ~M_AUTHIPHDR;
246 		m->m_flags &= ~M_AUTHIPDGM;
247 	}
248 #endif
249 
250 	/*
251 	 * mbuf statistics by kazu
252 	 */
253 	if (m->m_flags & M_EXT) {
254 		if (m->m_next)
255 			ip6stat.ip6s_mext2m++;
256 		else
257 			ip6stat.ip6s_mext1++;
258 	} else {
259 		if (m->m_next) {
260 			if (m->m_flags & M_LOOP) {
261 				ip6stat.ip6s_m2m[loif[0].if_index]++;	/*XXX*/
262 			} else if (m->m_pkthdr.rcvif->if_index <= 31)
263 				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
264 			else
265 				ip6stat.ip6s_m2m[0]++;
266 		} else
267 			ip6stat.ip6s_m1++;
268 	}
269 
270 	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
271 	ip6stat.ip6s_total++;
272 
273 #ifndef PULLDOWN_TEST
274 	/* XXX is the line really necessary? */
275 	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
276 #endif
277 
278 	if (m->m_len < sizeof(struct ip6_hdr)) {
279 		struct ifnet *inifp;
280 		inifp = m->m_pkthdr.rcvif;
281 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
282 			ip6stat.ip6s_toosmall++;
283 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
284 			return;
285 		}
286 	}
287 
288 	ip6 = mtod(m, struct ip6_hdr *);
289 
290 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
291 		ip6stat.ip6s_badvers++;
292 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
293 		goto bad;
294 	}
295 
296 #ifdef PFIL_HOOKS
297 	/*
298 	 * Run through list of hooks for input packets.  If there are any
299 	 * filters which require that additional packets in the flow are
300 	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
301 	 * Note that filters must _never_ set this flag, as another filter
302 	 * in the list may have previously cleared it.
303 	 */
304 	m0 = m;
305 	pfh = pfil_hook_get(PFIL_IN, &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
306 	for (; pfh; pfh = pfh->pfil_link.tqe_next)
307 		if (pfh->pfil_func) {
308 			rv = pfh->pfil_func(ip6, sizeof(*ip6),
309 					    m->m_pkthdr.rcvif, 0, &m0);
310 			if (rv)
311 				return;
312 			m = m0;
313 			if (m == NULL)
314 				return;
315 			ip6 = mtod(m, struct ip6_hdr *);
316 		}
317 #endif /* PFIL_HOOKS */
318 
319 	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
320 
321 	/*
322 	 * Check with the firewall...
323 	 */
324 	if (ip6_fw_enable && ip6_fw_chk_ptr) {
325 		u_short port = 0;
326 		/* If ipfw says divert, we have to just drop packet */
327 		/* use port as a dummy argument */
328 		if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
329 			m_freem(m);
330 			m = NULL;
331 		}
332 		if (!m)
333 			return;
334 	}
335 
336 	/*
337 	 * Scope check
338 	 */
339 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
340 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
341 		ip6stat.ip6s_badscope++;
342 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
343 		goto bad;
344 	}
345 
346 	/*
347 	 * Don't check IPv4 mapped address here.  SIIT assumes that
348 	 * routers would forward IPv6 native packets with IPv4 mapped
349 	 * address normally.
350 	 */
351 #if 0
352 	/*
353 	 * Reject packets with IPv4 compatible addresses (auto tunnel).
354 	 *
355 	 * The code forbids auto tunnel relay case in RFC1933 (the check is
356 	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
357 	 * is revised to forbid relaying case.
358 	 */
359 	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
360 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
361 		ip6stat.ip6s_badscope++;
362 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
363 		goto bad;
364 	}
365 #endif
366 	if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
367 	    IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
368 		if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
369 			ours = 1;
370 			deliverifp = m->m_pkthdr.rcvif;
371 			goto hbhcheck;
372 		} else {
373 			ip6stat.ip6s_badscope++;
374 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
375 			goto bad;
376 		}
377 	}
378 
379 #ifndef FAKE_LOOPBACK_IF
380 	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0)
381 #else
382 	if (1)
383 #endif
384 	{
385 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
386 			ip6->ip6_src.s6_addr16[1]
387 				= htons(m->m_pkthdr.rcvif->if_index);
388 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
389 			ip6->ip6_dst.s6_addr16[1]
390 				= htons(m->m_pkthdr.rcvif->if_index);
391 	}
392 
393 	/*
394 	 * XXX we need this since we do not have "goto ours" hack route
395 	 * for some of our ifaddrs on loopback interface.
396 	 * we should correct it by changing in6_ifattach to install
397 	 * "goto ours" hack route.
398 	 */
399 	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0) {
400 		if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
401 			ours = 1;
402 			deliverifp = m->m_pkthdr.rcvif;
403 			goto hbhcheck;
404 		}
405 	}
406 
407 	/*
408 	 * Multicast check
409 	 */
410 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
411 	  	struct	in6_multi *in6m = 0;
412 
413 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
414 		/*
415 		 * See if we belong to the destination multicast group on the
416 		 * arrival interface.
417 		 */
418 		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
419 		if (in6m)
420 			ours = 1;
421 		else if (!ip6_mrouter) {
422 			ip6stat.ip6s_notmember++;
423 			ip6stat.ip6s_cantforward++;
424 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
425 			goto bad;
426 		}
427 		deliverifp = m->m_pkthdr.rcvif;
428 		goto hbhcheck;
429 	}
430 
431 	/*
432 	 *  Unicast check
433 	 */
434 	if (ip6_forward_rt.ro_rt != NULL &&
435 	    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
436 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
437 			       &ip6_forward_rt.ro_dst.sin6_addr))
438 		ip6stat.ip6s_forward_cachehit++;
439 	else {
440 		if (ip6_forward_rt.ro_rt) {
441 			/* route is down or destination is different */
442 			ip6stat.ip6s_forward_cachemiss++;
443 			RTFREE(ip6_forward_rt.ro_rt);
444 			ip6_forward_rt.ro_rt = 0;
445 		}
446 
447 		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
448 		ip6_forward_rt.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
449 		ip6_forward_rt.ro_dst.sin6_family = AF_INET6;
450 		ip6_forward_rt.ro_dst.sin6_addr = ip6->ip6_dst;
451 #ifdef SCOPEDROUTING
452 		ip6_forward_rt.ro_dst.sin6_scope_id =
453 			in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_dst);
454 #endif
455 
456 		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
457 	}
458 
459 #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
460 
461 	/*
462 	 * Accept the packet if the forwarding interface to the destination
463 	 * according to the routing table is the loopback interface,
464 	 * unless the associated route has a gateway.
465 	 * Note that this approach causes to accept a packet if there is a
466 	 * route to the loopback interface for the destination of the packet.
467 	 * But we think it's even useful in some situations, e.g. when using
468 	 * a special daemon which wants to intercept the packet.
469 	 */
470 	if (ip6_forward_rt.ro_rt &&
471 	    (ip6_forward_rt.ro_rt->rt_flags &
472 	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
473 #if 0
474 	    /*
475 	     * The check below is redundant since the comparison of
476 	     * the destination and the key of the rtentry has
477 	     * already done through looking up the routing table.
478 	     */
479 	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
480 			       &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) &&
481 #endif
482 	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
483 		struct in6_ifaddr *ia6 =
484 			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
485 		if (ia6->ia6_flags & IN6_IFF_ANYCAST)
486 			m->m_flags |= M_ANYCAST6;
487 		/*
488 		 * packets to a tentative, duplicated, or somehow invalid
489 		 * address must not be accepted.
490 		 */
491 		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
492 			/* this address is ready */
493 			ours = 1;
494 			deliverifp = ia6->ia_ifp;	/* correct? */
495 
496 			/* Count the packet in the ip address stats */
497 			ia6->ia_ifa.if_ipackets++;
498 			ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
499 
500 			goto hbhcheck;
501 		} else {
502 			/* address is not ready, so discard the packet. */
503 			log(LOG_INFO,
504 			    "ip6_input: packet to an unready address %s->%s",
505 			    ip6_sprintf(&ip6->ip6_src),
506 			    ip6_sprintf(&ip6->ip6_dst));
507 
508 			goto bad;
509 		}
510 	}
511 
512 	/*
513 	 * FAITH(Firewall Aided Internet Translator)
514 	 */
515 #if defined(NFAITH) && 0 < NFAITH
516 	if (ip6_keepfaith) {
517 		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
518 		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
519 			/* XXX do we need more sanity checks? */
520 			ours = 1;
521 			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /*faith*/
522 			goto hbhcheck;
523 		}
524 	}
525 #endif
526 
527 	/*
528 	 * Now there is no reason to process the packet if it's not our own
529 	 * and we're not a router.
530 	 */
531 	if (!ip6_forwarding) {
532 		ip6stat.ip6s_cantforward++;
533 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
534 		goto bad;
535 	}
536 
537   hbhcheck:
538 	/*
539 	 * Process Hop-by-Hop options header if it's contained.
540 	 * m may be modified in ip6_hopopts_input().
541 	 * If a JumboPayload option is included, plen will also be modified.
542 	 */
543 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
544 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
545 		struct ip6_hbh *hbh;
546 
547 		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
548 #if 0	/*touches NULL pointer*/
549 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
550 #endif
551 			return;	/* m have already been freed */
552 		}
553 
554 		/* adjust pointer */
555 		ip6 = mtod(m, struct ip6_hdr *);
556 
557 		/*
558 		 * if the payload length field is 0 and the next header field
559 		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
560 		 * option MUST be included.
561 		 */
562 		if (ip6->ip6_plen == 0 && plen == 0) {
563 			/*
564 			 * Note that if a valid jumbo payload option is
565 			 * contained, ip6_hoptops_input() must set a valid
566 			 * (non-zero) payload length to the variable plen.
567 			 */
568 			ip6stat.ip6s_badoptions++;
569 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
570 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
571 			icmp6_error(m, ICMP6_PARAM_PROB,
572 				    ICMP6_PARAMPROB_HEADER,
573 				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
574 			return;
575 		}
576 #ifndef PULLDOWN_TEST
577 		/* ip6_hopopts_input() ensures that mbuf is contiguous */
578 		hbh = (struct ip6_hbh *)(ip6 + 1);
579 #else
580 		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
581 			sizeof(struct ip6_hbh));
582 		if (hbh == NULL) {
583 			ip6stat.ip6s_tooshort++;
584 			return;
585 		}
586 #endif
587 		nxt = hbh->ip6h_nxt;
588 
589 		/*
590 		 * accept the packet if a router alert option is included
591 		 * and we act as an IPv6 router.
592 		 */
593 		if (rtalert != ~0 && ip6_forwarding)
594 			ours = 1;
595 	} else
596 		nxt = ip6->ip6_nxt;
597 
598 	/*
599 	 * Check that the amount of data in the buffers
600 	 * is as at least much as the IPv6 header would have us expect.
601 	 * Trim mbufs if longer than we expect.
602 	 * Drop packet if shorter than we expect.
603 	 */
604 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
605 		ip6stat.ip6s_tooshort++;
606 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
607 		goto bad;
608 	}
609 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
610 		if (m->m_len == m->m_pkthdr.len) {
611 			m->m_len = sizeof(struct ip6_hdr) + plen;
612 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
613 		} else
614 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
615 	}
616 
617 	/*
618 	 * Forward if desirable.
619 	 */
620 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
621 		/*
622 		 * If we are acting as a multicast router, all
623 		 * incoming multicast packets are passed to the
624 		 * kernel-level multicast forwarding function.
625 		 * The packet is returned (relatively) intact; if
626 		 * ip6_mforward() returns a non-zero value, the packet
627 		 * must be discarded, else it may be accepted below.
628 		 */
629 		if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
630 			ip6stat.ip6s_cantforward++;
631 			m_freem(m);
632 			return;
633 		}
634 		if (!ours) {
635 			m_freem(m);
636 			return;
637 		}
638 	} else if (!ours) {
639 		ip6_forward(m, 0);
640 		return;
641 	}
642 
643 	ip6 = mtod(m, struct ip6_hdr *);
644 
645 	/*
646 	 * Malicious party may be able to use IPv4 mapped addr to confuse
647 	 * tcp/udp stack and bypass security checks (act as if it was from
648 	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
649 	 *
650 	 * For SIIT end node behavior, you may want to disable the check.
651 	 * However, you will  become vulnerable to attacks using IPv4 mapped
652 	 * source.
653 	 */
654 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
655 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
656 		ip6stat.ip6s_badscope++;
657 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
658 		goto bad;
659 	}
660 
661 	/*
662 	 * Tell launch routine the next header
663 	 */
664 	ip6stat.ip6s_delivered++;
665 	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
666 	nest = 0;
667 	while (nxt != IPPROTO_DONE) {
668 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
669 			ip6stat.ip6s_toomanyhdr++;
670 			goto bad;
671 		}
672 
673 		/*
674 		 * protection against faulty packet - there should be
675 		 * more sanity checks in header chain processing.
676 		 */
677 		if (m->m_pkthdr.len < off) {
678 			ip6stat.ip6s_tooshort++;
679 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
680 			goto bad;
681 		}
682 
683 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
684 	}
685 	return;
686  bad:
687 	m_freem(m);
688 }
689 
690 /*
691  * Hop-by-Hop options header processing. If a valid jumbo payload option is
692  * included, the real payload length will be stored in plenp.
693  */
694 static int
695 ip6_hopopts_input(plenp, rtalertp, mp, offp)
696 	u_int32_t *plenp;
697 	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
698 	struct mbuf **mp;
699 	int *offp;
700 {
701 	register struct mbuf *m = *mp;
702 	int off = *offp, hbhlen;
703 	struct ip6_hbh *hbh;
704 	u_int8_t *opt;
705 
706 	/* validation of the length of the header */
707 #ifndef PULLDOWN_TEST
708 	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
709 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
710 	hbhlen = (hbh->ip6h_len + 1) << 3;
711 
712 	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
713 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
714 #else
715 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
716 		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
717 	if (hbh == NULL) {
718 		ip6stat.ip6s_tooshort++;
719 		return -1;
720 	}
721 	hbhlen = (hbh->ip6h_len + 1) << 3;
722 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
723 		hbhlen);
724 	if (hbh == NULL) {
725 		ip6stat.ip6s_tooshort++;
726 		return -1;
727 	}
728 #endif
729 	off += hbhlen;
730 	hbhlen -= sizeof(struct ip6_hbh);
731 	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
732 
733 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
734 				hbhlen, rtalertp, plenp) < 0)
735 		return(-1);
736 
737 	*offp = off;
738 	*mp = m;
739 	return(0);
740 }
741 
742 /*
743  * Search header for all Hop-by-hop options and process each option.
744  * This function is separate from ip6_hopopts_input() in order to
745  * handle a case where the sending node itself process its hop-by-hop
746  * options header. In such a case, the function is called from ip6_output().
747  */
748 int
749 ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
750 	struct mbuf *m;
751 	u_int8_t *opthead;
752 	int hbhlen;
753 	u_int32_t *rtalertp;
754 	u_int32_t *plenp;
755 {
756 	struct ip6_hdr *ip6;
757 	int optlen = 0;
758 	u_int8_t *opt = opthead;
759 	u_int16_t rtalert_val;
760 	u_int32_t jumboplen;
761 
762 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
763 		switch(*opt) {
764 		 case IP6OPT_PAD1:
765 			 optlen = 1;
766 			 break;
767 		 case IP6OPT_PADN:
768 			 if (hbhlen < IP6OPT_MINLEN) {
769 				 ip6stat.ip6s_toosmall++;
770 				 goto bad;
771 			 }
772 			 optlen = *(opt + 1) + 2;
773 			 break;
774 		 case IP6OPT_RTALERT:
775 			 /* XXX may need check for alignment */
776 			 if (hbhlen < IP6OPT_RTALERT_LEN) {
777 				 ip6stat.ip6s_toosmall++;
778 				 goto bad;
779 			 }
780 			 if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2)
781 				  /* XXX: should we discard the packet? */
782 				 log(LOG_ERR, "length of router alert opt is inconsitent(%d)",
783 				     *(opt + 1));
784 			 optlen = IP6OPT_RTALERT_LEN;
785 			 bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
786 			 *rtalertp = ntohs(rtalert_val);
787 			 break;
788 		 case IP6OPT_JUMBO:
789 			/* XXX may need check for alignment */
790 			if (hbhlen < IP6OPT_JUMBO_LEN) {
791 				ip6stat.ip6s_toosmall++;
792 				goto bad;
793 			}
794 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2)
795 				 /* XXX: should we discard the packet? */
796 				log(LOG_ERR, "length of jumbopayload opt "
797 				    "is inconsistent(%d)",
798 				    *(opt + 1));
799 			optlen = IP6OPT_JUMBO_LEN;
800 
801 			/*
802 			 * IPv6 packets that have non 0 payload length
803 			 * must not contain a jumbo paylod option.
804 			 */
805 			ip6 = mtod(m, struct ip6_hdr *);
806 			if (ip6->ip6_plen) {
807 				ip6stat.ip6s_badoptions++;
808 				icmp6_error(m, ICMP6_PARAM_PROB,
809 					    ICMP6_PARAMPROB_HEADER,
810 					    sizeof(struct ip6_hdr) +
811 					    sizeof(struct ip6_hbh) +
812 					    opt - opthead);
813 				return(-1);
814 			}
815 
816 			/*
817 			 * We may see jumbolen in unaligned location, so
818 			 * we'd need to perform bcopy().
819 			 */
820 			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
821 			jumboplen = (u_int32_t)htonl(jumboplen);
822 
823 #if 1
824 			/*
825 			 * if there are multiple jumbo payload options,
826 			 * *plenp will be non-zero and the packet will be
827 			 * rejected.
828 			 * the behavior may need some debate in ipngwg -
829 			 * multiple options does not make sense, however,
830 			 * there's no explicit mention in specification.
831 			 */
832 			if (*plenp != 0) {
833 				ip6stat.ip6s_badoptions++;
834 				icmp6_error(m, ICMP6_PARAM_PROB,
835 					    ICMP6_PARAMPROB_HEADER,
836 					    sizeof(struct ip6_hdr) +
837 					    sizeof(struct ip6_hbh) +
838 					    opt + 2 - opthead);
839 				return(-1);
840 			}
841 #endif
842 
843 			/*
844 			 * jumbo payload length must be larger than 65535.
845 			 */
846 			if (jumboplen <= IPV6_MAXPACKET) {
847 				ip6stat.ip6s_badoptions++;
848 				icmp6_error(m, ICMP6_PARAM_PROB,
849 					    ICMP6_PARAMPROB_HEADER,
850 					    sizeof(struct ip6_hdr) +
851 					    sizeof(struct ip6_hbh) +
852 					    opt + 2 - opthead);
853 				return(-1);
854 			}
855 			*plenp = jumboplen;
856 
857 			break;
858 		 default:		/* unknown option */
859 			 if (hbhlen < IP6OPT_MINLEN) {
860 				 ip6stat.ip6s_toosmall++;
861 				 goto bad;
862 			 }
863 			 if ((optlen = ip6_unknown_opt(opt, m,
864 						       sizeof(struct ip6_hdr) +
865 						       sizeof(struct ip6_hbh) +
866 						       opt - opthead)) == -1)
867 				 return(-1);
868 			 optlen += 2;
869 			 break;
870 		}
871 	}
872 
873 	return(0);
874 
875   bad:
876 	m_freem(m);
877 	return(-1);
878 }
879 
880 /*
881  * Unknown option processing.
882  * The third argument `off' is the offset from the IPv6 header to the option,
883  * which is necessary if the IPv6 header the and option header and IPv6 header
884  * is not continuous in order to return an ICMPv6 error.
885  */
886 int
887 ip6_unknown_opt(optp, m, off)
888 	u_int8_t *optp;
889 	struct mbuf *m;
890 	int off;
891 {
892 	struct ip6_hdr *ip6;
893 
894 	switch(IP6OPT_TYPE(*optp)) {
895 	 case IP6OPT_TYPE_SKIP: /* ignore the option */
896 		 return((int)*(optp + 1));
897 	 case IP6OPT_TYPE_DISCARD:	/* silently discard */
898 		 m_freem(m);
899 		 return(-1);
900 	 case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
901 		 ip6stat.ip6s_badoptions++;
902 		 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
903 		 return(-1);
904 	 case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
905 		 ip6stat.ip6s_badoptions++;
906 		 ip6 = mtod(m, struct ip6_hdr *);
907 		 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
908 		     (m->m_flags & (M_BCAST|M_MCAST)))
909 			 m_freem(m);
910 		 else
911 			 icmp6_error(m, ICMP6_PARAM_PROB,
912 				     ICMP6_PARAMPROB_OPTION, off);
913 		 return(-1);
914 	}
915 
916 	m_freem(m);		/* XXX: NOTREACHED */
917 	return(-1);
918 }
919 
920 /*
921  * Create the "control" list for this pcb.
922  *
923  * The routine will be called from upper layer handlers like tcp6_input().
924  * Thus the routine assumes that the caller (tcp6_input) have already
925  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
926  * very first mbuf on the mbuf chain.
927  * We may want to add some infinite loop prevention or sanity checks for safety.
928  * (This applies only when you are using KAME mbuf chain restriction, i.e.
929  * you are using IP6_EXTHDR_CHECK() not m_pulldown())
930  */
931 void
932 ip6_savecontrol(in6p, mp, ip6, m)
933 	register struct in6pcb *in6p;
934 	register struct mbuf **mp;
935 	register struct ip6_hdr *ip6;
936 	register struct mbuf *m;
937 {
938 	struct proc *p = curproc;	/* XXX */
939 	int privileged;
940 
941 	privileged = 0;
942 	if (p && !suser(p))
943 		privileged++;
944 
945 	if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
946 		struct timeval tv;
947 
948 		microtime(&tv);
949 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
950 			SCM_TIMESTAMP, SOL_SOCKET);
951 		if (*mp)
952 			mp = &(*mp)->m_next;
953 	}
954 
955 #ifdef noyet
956 	/* options were tossed above */
957 	if (in6p->in6p_flags & IN6P_RECVOPTS)
958 		/* broken */
959 	/* ip6_srcroute doesn't do what we want here, need to fix */
960 	if (in6p->in6p_flags & IPV6P_RECVRETOPTS)
961 		/* broken */
962 #endif
963 
964 	/* RFC 2292 sec. 5 */
965 	if (in6p->in6p_flags & IN6P_PKTINFO) {
966 		struct in6_pktinfo pi6;
967 		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
968 		if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
969 			pi6.ipi6_addr.s6_addr16[1] = 0;
970 		pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
971 					? m->m_pkthdr.rcvif->if_index
972 					: 0;
973 		*mp = sbcreatecontrol((caddr_t) &pi6,
974 			sizeof(struct in6_pktinfo), IPV6_PKTINFO,
975 			IPPROTO_IPV6);
976 		if (*mp)
977 			mp = &(*mp)->m_next;
978 	}
979 	if (in6p->in6p_flags & IN6P_HOPLIMIT) {
980 		int hlim = ip6->ip6_hlim & 0xff;
981 		*mp = sbcreatecontrol((caddr_t) &hlim,
982 			sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
983 		if (*mp)
984 			mp = &(*mp)->m_next;
985 	}
986 	/* IN6P_NEXTHOP - for outgoing packet only */
987 
988 	/*
989 	 * IPV6_HOPOPTS socket option. We require super-user privilege
990 	 * for the option, but it might be too strict, since there might
991 	 * be some hop-by-hop options which can be returned to normal user.
992 	 * See RFC 2292 section 6.
993 	 */
994 	if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
995 		/*
996 		 * Check if a hop-by-hop options header is contatined in the
997 		 * received packet, and if so, store the options as ancillary
998 		 * data. Note that a hop-by-hop options header must be
999 		 * just after the IPv6 header, which fact is assured through
1000 		 * the IPv6 input processing.
1001 		 */
1002 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1003 		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1004 			struct ip6_hbh *hbh;
1005 			int hbhlen;
1006 
1007 #ifndef PULLDOWN_TEST
1008 			hbh = (struct ip6_hbh *)(ip6 + 1);
1009 			hbhlen = (hbh->ip6h_len + 1) << 3;
1010 #else
1011 			IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
1012 				sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
1013 			if (hbh == NULL) {
1014 				ip6stat.ip6s_tooshort++;
1015 				return;
1016 			}
1017 			hbhlen = (hbh->ip6h_len + 1) << 3;
1018 			IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
1019 				sizeof(struct ip6_hdr), hbhlen);
1020 			if (hbh == NULL) {
1021 				ip6stat.ip6s_tooshort++;
1022 				return;
1023 			}
1024 #endif
1025 
1026 			/*
1027 			 * XXX: We copy whole the header even if a jumbo
1028 			 * payload option is included, which option is to
1029 			 * be removed before returning in the RFC 2292.
1030 			 * But it's too painful operation...
1031 			 */
1032 			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1033 					      IPV6_HOPOPTS, IPPROTO_IPV6);
1034 			if (*mp)
1035 				mp = &(*mp)->m_next;
1036 		}
1037 	}
1038 
1039 	/* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1040 	if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
1041 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1042 		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);;
1043 
1044 		/*
1045 		 * Search for destination options headers or routing
1046 		 * header(s) through the header chain, and stores each
1047 		 * header as ancillary data.
1048 		 * Note that the order of the headers remains in
1049 		 * the chain of ancillary data.
1050 		 */
1051 		while(1) {	/* is explicit loop prevention necessary? */
1052 			struct ip6_ext *ip6e;
1053 			int elen;
1054 
1055 #ifndef PULLDOWN_TEST
1056 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1057 			if (nxt == IPPROTO_AH)
1058 				elen = (ip6e->ip6e_len + 2) << 2;
1059 			else
1060 				elen = (ip6e->ip6e_len + 1) << 3;
1061 #else
1062 			IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off,
1063 				sizeof(struct ip6_ext));
1064 			if (ip6e == NULL) {
1065 				ip6stat.ip6s_tooshort++;
1066 				return;
1067 			}
1068 			if (nxt == IPPROTO_AH)
1069 				elen = (ip6e->ip6e_len + 2) << 2;
1070 			else
1071 				elen = (ip6e->ip6e_len + 1) << 3;
1072 			IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off, elen);
1073 			if (ip6e == NULL) {
1074 				ip6stat.ip6s_tooshort++;
1075 				return;
1076 			}
1077 #endif
1078 
1079 			switch(nxt) {
1080 		         case IPPROTO_DSTOPTS:
1081 				 if (!in6p->in6p_flags & IN6P_DSTOPTS)
1082 					 break;
1083 
1084 				 /*
1085 				  * We also require super-user privilege for
1086 				  * the option.
1087 				  * See the comments on IN6_HOPOPTS.
1088 				  */
1089 				 if (!privileged)
1090 					 break;
1091 
1092 				 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1093 						       IPV6_DSTOPTS,
1094 						       IPPROTO_IPV6);
1095 				 if (*mp)
1096 					 mp = &(*mp)->m_next;
1097 				 break;
1098 
1099 			 case IPPROTO_ROUTING:
1100 				 if (!in6p->in6p_flags & IN6P_RTHDR)
1101 					 break;
1102 
1103 				 *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1104 						       IPV6_RTHDR,
1105 						       IPPROTO_IPV6);
1106 				 if (*mp)
1107 					 mp = &(*mp)->m_next;
1108 				 break;
1109 
1110 			 case IPPROTO_UDP:
1111 			 case IPPROTO_TCP:
1112 			 case IPPROTO_ICMPV6:
1113 			 default:
1114 				 /*
1115 				  * stop search if we encounter an upper
1116 				  * layer protocol headers.
1117 				  */
1118 				 goto loopend;
1119 
1120 			 case IPPROTO_HOPOPTS:
1121 			 case IPPROTO_AH: /* is it possible? */
1122 				 break;
1123 			}
1124 
1125 			/* proceed with the next header. */
1126 			off += elen;
1127 			nxt = ip6e->ip6e_nxt;
1128 		}
1129 	  loopend:
1130 	}
1131 	if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
1132 		/* to be done */
1133 	}
1134 	if ((in6p->in6p_flags & IN6P_DSTOPTS) && privileged) {
1135 		/* to be done */
1136 	}
1137 	/* IN6P_RTHDR - to be done */
1138 
1139 }
1140 
1141 /*
1142  * Get pointer to the previous header followed by the header
1143  * currently processed.
1144  * XXX: This function supposes that
1145  *	M includes all headers,
1146  *	the next header field and the header length field of each header
1147  *	are valid, and
1148  *	the sum of each header length equals to OFF.
1149  * Because of these assumptions, this function must be called very
1150  * carefully. Moreover, it will not be used in the near future when
1151  * we develop `neater' mechanism to process extension headers.
1152  */
1153 char *
1154 ip6_get_prevhdr(m, off)
1155 	struct mbuf *m;
1156 	int off;
1157 {
1158 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1159 
1160 	if (off == sizeof(struct ip6_hdr))
1161 		return(&ip6->ip6_nxt);
1162 	else {
1163 		int len, nxt;
1164 		struct ip6_ext *ip6e = NULL;
1165 
1166 		nxt = ip6->ip6_nxt;
1167 		len = sizeof(struct ip6_hdr);
1168 		while (len < off) {
1169 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1170 
1171 			switch(nxt) {
1172 			case IPPROTO_FRAGMENT:
1173 				len += sizeof(struct ip6_frag);
1174 				break;
1175 			case IPPROTO_AH:
1176 				len += (ip6e->ip6e_len + 2) << 2;
1177 				break;
1178 			default:
1179 				len += (ip6e->ip6e_len + 1) << 3;
1180 				break;
1181 			}
1182 			nxt = ip6e->ip6e_nxt;
1183 		}
1184 		if (ip6e)
1185 			return(&ip6e->ip6e_nxt);
1186 		else
1187 			return NULL;
1188 	}
1189 }
1190 
1191 /*
1192  * get next header offset.  m will be retained.
1193  */
1194 int
1195 ip6_nexthdr(m, off, proto, nxtp)
1196 	struct mbuf *m;
1197 	int off;
1198 	int proto;
1199 	int *nxtp;
1200 {
1201 	struct ip6_hdr ip6;
1202 	struct ip6_ext ip6e;
1203 	struct ip6_frag fh;
1204 
1205 	/* just in case */
1206 	if (m == NULL)
1207 		panic("ip6_nexthdr: m == NULL");
1208 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1209 		return -1;
1210 
1211 	switch (proto) {
1212 	case IPPROTO_IPV6:
1213 		if (m->m_pkthdr.len < off + sizeof(ip6))
1214 			return -1;
1215 		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1216 		if (nxtp)
1217 			*nxtp = ip6.ip6_nxt;
1218 		off += sizeof(ip6);
1219 		return off;
1220 
1221 	case IPPROTO_FRAGMENT:
1222 		/*
1223 		 * terminate parsing if it is not the first fragment,
1224 		 * it does not make sense to parse through it.
1225 		 */
1226 		if (m->m_pkthdr.len < off + sizeof(fh))
1227 			return -1;
1228 		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1229 		if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
1230 			return -1;
1231 		if (nxtp)
1232 			*nxtp = fh.ip6f_nxt;
1233 		off += sizeof(struct ip6_frag);
1234 		return off;
1235 
1236 	case IPPROTO_AH:
1237 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1238 			return -1;
1239 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1240 		if (nxtp)
1241 			*nxtp = ip6e.ip6e_nxt;
1242 		off += (ip6e.ip6e_len + 2) << 2;
1243 		return off;
1244 
1245 	case IPPROTO_HOPOPTS:
1246 	case IPPROTO_ROUTING:
1247 	case IPPROTO_DSTOPTS:
1248 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1249 			return -1;
1250 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1251 		if (nxtp)
1252 			*nxtp = ip6e.ip6e_nxt;
1253 		off += (ip6e.ip6e_len + 1) << 3;
1254 		return off;
1255 
1256 	case IPPROTO_NONE:
1257 	case IPPROTO_ESP:
1258 	case IPPROTO_IPCOMP:
1259 		/* give up */
1260 		return -1;
1261 
1262 	default:
1263 		return -1;
1264 	}
1265 
1266 	return -1;
1267 }
1268 
1269 /*
1270  * get offset for the last header in the chain.  m will be kept untainted.
1271  */
1272 int
1273 ip6_lasthdr(m, off, proto, nxtp)
1274 	struct mbuf *m;
1275 	int off;
1276 	int proto;
1277 	int *nxtp;
1278 {
1279 	int newoff;
1280 	int nxt;
1281 
1282 	if (!nxtp) {
1283 		nxt = -1;
1284 		nxtp = &nxt;
1285 	}
1286 	while (1) {
1287 		newoff = ip6_nexthdr(m, off, proto, nxtp);
1288 		if (newoff < 0)
1289 			return off;
1290 		else if (newoff < off)
1291 			return -1;	/* invalid */
1292 		else if (newoff == off)
1293 			return newoff;
1294 
1295 		off = newoff;
1296 		proto = *nxtp;
1297 	}
1298 }
1299 
1300 /*
1301  * System control for IP6
1302  */
1303 
1304 u_char	inet6ctlerrmap[PRC_NCMDS] = {
1305 	0,		0,		0,		0,
1306 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1307 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1308 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1309 	0,		0,		0,		0,
1310 	ENOPROTOOPT
1311 };
1312