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