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