ip6_input.c revision 121742
1/*	$FreeBSD: head/sys/netinet6/ip6_input.c 121742 2003-10-30 15:29:17Z ume $	*/
2/*	$KAME: ip6_input.c,v 1.259 2002/01/21 04:58:09 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#include "opt_random_ip_id.h"
74
75#include <sys/param.h>
76#include <sys/systm.h>
77#include <sys/malloc.h>
78#include <sys/mbuf.h>
79#include <sys/proc.h>
80#include <sys/domain.h>
81#include <sys/protosw.h>
82#include <sys/socket.h>
83#include <sys/socketvar.h>
84#include <sys/errno.h>
85#include <sys/time.h>
86#include <sys/kernel.h>
87#include <sys/syslog.h>
88
89#include <net/if.h>
90#include <net/if_types.h>
91#include <net/if_dl.h>
92#include <net/route.h>
93#include <net/netisr.h>
94#ifdef PFIL_HOOKS
95#include <net/pfil.h>
96#endif
97
98#include <netinet/in.h>
99#include <netinet/in_systm.h>
100#ifdef INET
101#include <netinet/ip.h>
102#include <netinet/ip_icmp.h>
103#endif /* INET */
104#include <netinet/ip6.h>
105#include <netinet6/in6_var.h>
106#include <netinet6/ip6_var.h>
107#include <netinet/in_pcb.h>
108#include <netinet/icmp6.h>
109#include <netinet6/scope6_var.h>
110#include <netinet6/in6_ifattach.h>
111#include <netinet6/nd6.h>
112#include <netinet6/in6_prefix.h>
113
114#ifdef IPSEC
115#include <netinet6/ipsec.h>
116#ifdef INET6
117#include <netinet6/ipsec6.h>
118#endif
119#endif
120
121#ifdef FAST_IPSEC
122#include <netipsec/ipsec.h>
123#include <netipsec/ipsec6.h>
124#define	IPSEC
125#endif /* FAST_IPSEC */
126
127#include <netinet6/ip6_fw.h>
128
129#include <netinet6/ip6protosw.h>
130
131#include <net/net_osdep.h>
132
133extern struct domain inet6domain;
134
135u_char ip6_protox[IPPROTO_MAX];
136static struct ifqueue ip6intrq;
137static int ip6qmaxlen = IFQ_MAXLEN;
138struct in6_ifaddr *in6_ifaddr;
139
140extern struct callout in6_tmpaddrtimer_ch;
141
142int ip6_forward_srcrt;			/* XXX */
143int ip6_sourcecheck;			/* XXX */
144int ip6_sourcecheck_interval;		/* XXX */
145
146int ip6_ours_check_algorithm;
147
148#ifdef PFIL_HOOKS
149struct pfil_head inet6_pfil_hook;
150#endif
151
152/* firewall hooks */
153ip6_fw_chk_t *ip6_fw_chk_ptr;
154ip6_fw_ctl_t *ip6_fw_ctl_ptr;
155int ip6_fw_enable = 1;
156
157struct ip6stat ip6stat;
158
159static void ip6_init2 __P((void *));
160static struct ip6aux *ip6_setdstifaddr __P((struct mbuf *, struct in6_ifaddr *));
161static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
162#ifdef PULLDOWN_TEST
163static struct mbuf *ip6_pullexthdr __P((struct mbuf *, size_t, int));
164#endif
165
166/*
167 * IP6 initialization: fill in IP6 protocol switch table.
168 * All protocols not implemented in kernel go to raw IP6 protocol handler.
169 */
170void
171ip6_init()
172{
173	struct ip6protosw *pr;
174	int i;
175
176#ifdef DIAGNOSTIC
177	if (sizeof(struct protosw) != sizeof(struct ip6protosw))
178		panic("sizeof(protosw) != sizeof(ip6protosw)");
179#endif
180	pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
181	if (pr == 0)
182		panic("ip6_init");
183	for (i = 0; i < IPPROTO_MAX; i++)
184		ip6_protox[i] = pr - inet6sw;
185	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
186	    pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
187		if (pr->pr_domain->dom_family == PF_INET6 &&
188		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
189			ip6_protox[pr->pr_protocol] = pr - inet6sw;
190#ifdef PFIL_HOOKS
191	inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
192	inet6_pfil_hook.ph_af = AF_INET6;
193	if ((i = pfil_head_register(&inet6_pfil_hook)) != 0)
194		printf("%s: WARNING: unable to register pfil hook, "
195			"error %d\n", __func__, i);
196#endif /* PFIL_HOOKS */
197	ip6intrq.ifq_maxlen = ip6qmaxlen;
198	mtx_init(&ip6intrq.ifq_mtx, "ip6_inq", NULL, MTX_DEF);
199	netisr_register(NETISR_IPV6, ip6_input, &ip6intrq);
200	scope6_init();
201	addrsel_policy_init();
202	nd6_init();
203	frag6_init();
204#ifndef RANDOM_IP_ID
205	ip6_flow_seq = arc4random();
206#endif
207	ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
208}
209
210static void
211ip6_init2(dummy)
212	void *dummy;
213{
214
215	/* nd6_timer_init */
216	callout_init(&nd6_timer_ch, 0);
217	callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
218
219	/* router renumbering prefix list maintenance */
220	callout_init(&in6_rr_timer_ch, 0);
221	callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL);
222
223	/* timer for regeneranation of temporary addresses randomize ID */
224	callout_reset(&in6_tmpaddrtimer_ch,
225		      (ip6_temp_preferred_lifetime - ip6_desync_factor -
226		       ip6_temp_regen_advance) * hz,
227		      in6_tmpaddrtimer, NULL);
228}
229
230/* cheat */
231/* This must be after route_init(), which is now SI_ORDER_THIRD */
232SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL);
233
234extern struct	route_in6 ip6_forward_rt;
235
236void
237ip6_input(m)
238	struct mbuf *m;
239{
240	struct ip6_hdr *ip6;
241	int off = sizeof(struct ip6_hdr), nest;
242	u_int32_t plen;
243	u_int32_t rtalert = ~0;
244	int nxt, ours = 0;
245	struct ifnet *deliverifp = NULL;
246#ifdef PFIL_HOOKS
247	struct in6_addr odst;
248#endif
249	int srcrt = 0;
250
251#ifdef IPSEC
252	/*
253	 * should the inner packet be considered authentic?
254	 * see comment in ah4_input().
255	 */
256	if (m) {
257		m->m_flags &= ~M_AUTHIPHDR;
258		m->m_flags &= ~M_AUTHIPDGM;
259	}
260#endif
261
262	/*
263	 * make sure we don't have onion peering information into m_tag.
264	 */
265	ip6_delaux(m);
266
267	/*
268	 * mbuf statistics
269	 */
270	if (m->m_flags & M_EXT) {
271		if (m->m_next)
272			ip6stat.ip6s_mext2m++;
273		else
274			ip6stat.ip6s_mext1++;
275	} else {
276#define M2MMAX	(sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
277		if (m->m_next) {
278			if (m->m_flags & M_LOOP) {
279				ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
280			} else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
281				ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
282			else
283				ip6stat.ip6s_m2m[0]++;
284		} else
285			ip6stat.ip6s_m1++;
286#undef M2MMAX
287	}
288
289	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
290	ip6stat.ip6s_total++;
291
292#ifndef PULLDOWN_TEST
293	/*
294	 * L2 bridge code and some other code can return mbuf chain
295	 * that does not conform to KAME requirement.  too bad.
296	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
297	 */
298	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
299		struct mbuf *n;
300
301		MGETHDR(n, M_DONTWAIT, MT_HEADER);
302		if (n)
303			M_MOVE_PKTHDR(n, m);
304		if (n && n->m_pkthdr.len > MHLEN) {
305			MCLGET(n, M_DONTWAIT);
306			if ((n->m_flags & M_EXT) == 0) {
307				m_freem(n);
308				n = NULL;
309			}
310		}
311		if (n == NULL) {
312			m_freem(m);
313			return;	/* ENOBUFS */
314		}
315
316		m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
317		n->m_len = n->m_pkthdr.len;
318		m_freem(m);
319		m = n;
320	}
321	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
322#endif
323
324	if (m->m_len < sizeof(struct ip6_hdr)) {
325		struct ifnet *inifp;
326		inifp = m->m_pkthdr.rcvif;
327		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
328			ip6stat.ip6s_toosmall++;
329			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
330			return;
331		}
332	}
333
334	ip6 = mtod(m, struct ip6_hdr *);
335
336	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
337		ip6stat.ip6s_badvers++;
338		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
339		goto bad;
340	}
341
342#ifdef PFIL_HOOKS
343	/*
344	 * Run through list of hooks for input packets.
345	 *
346	 * NB: Beware of the destination address changing
347	 *     (e.g. by NAT rewriting).  When this happens,
348	 *     tell ip6_forward to do the right thing.
349	 */
350	odst = ip6->ip6_dst;
351	if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif, PFIL_IN))
352		return;
353	if (m == NULL)			/* consumed by filter */
354		return;
355	ip6 = mtod(m, struct ip6_hdr *);
356	srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
357#endif /* PFIL_HOOKS */
358
359	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
360
361	/*
362	 * Check with the firewall...
363	 */
364	if (ip6_fw_enable && ip6_fw_chk_ptr) {
365		u_short port = 0;
366		/* If ipfw says divert, we have to just drop packet */
367		/* use port as a dummy argument */
368		if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) {
369			m_freem(m);
370			m = NULL;
371		}
372		if (!m)
373			return;
374	}
375
376	/*
377	 * Check against address spoofing/corruption.
378	 */
379	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
380	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
381		/*
382		 * XXX: "badscope" is not very suitable for a multicast source.
383		 */
384		ip6stat.ip6s_badscope++;
385		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
386		goto bad;
387	}
388	if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
389	     IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) &&
390	    (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
391		ip6stat.ip6s_badscope++;
392		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
393		goto bad;
394	}
395
396	/*
397	 * The following check is not documented in specs.  A malicious
398	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
399	 * and bypass security checks (act as if it was from 127.0.0.1 by using
400	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
401	 *
402	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
403	 * support IPv4-less kernel compilation, we cannot support SIIT
404	 * environment at all.  So, it makes more sense for us to reject any
405	 * malicious packets for non-SIIT environment, than try to do a
406	 * partial support for SIIT environment.
407	 */
408	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
409	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
410		ip6stat.ip6s_badscope++;
411		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
412		goto bad;
413	}
414#if 0
415	/*
416	 * Reject packets with IPv4 compatible addresses (auto tunnel).
417	 *
418	 * The code forbids auto tunnel relay case in RFC1933 (the check is
419	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
420	 * is revised to forbid relaying case.
421	 */
422	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
423	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
424		ip6stat.ip6s_badscope++;
425		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
426		goto bad;
427	}
428#endif
429
430	/* drop packets if interface ID portion is already filled */
431	if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
432		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
433		    ip6->ip6_src.s6_addr16[1]) {
434			ip6stat.ip6s_badscope++;
435			goto bad;
436		}
437		if ((IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) ||
438		     IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) &&
439		    ip6->ip6_dst.s6_addr16[1]) {
440			ip6stat.ip6s_badscope++;
441			goto bad;
442		}
443	}
444
445	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
446		ip6->ip6_src.s6_addr16[1]
447			= htons(m->m_pkthdr.rcvif->if_index);
448	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
449		ip6->ip6_dst.s6_addr16[1]
450			= htons(m->m_pkthdr.rcvif->if_index);
451
452	/*
453	 * Multicast check
454	 */
455	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
456	  	struct in6_multi *in6m = 0;
457
458		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
459		/*
460		 * See if we belong to the destination multicast group on the
461		 * arrival interface.
462		 */
463		IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
464		if (in6m)
465			ours = 1;
466		else if (!ip6_mrouter) {
467			ip6stat.ip6s_notmember++;
468			ip6stat.ip6s_cantforward++;
469			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
470			goto bad;
471		}
472		deliverifp = m->m_pkthdr.rcvif;
473		goto hbhcheck;
474	}
475
476	/*
477	 *  Unicast check
478	 */
479	if (ip6_forward_rt.ro_rt != NULL &&
480	    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
481	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
482	    &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
483		ip6stat.ip6s_forward_cachehit++;
484	else {
485		struct sockaddr_in6 *dst6;
486
487		if (ip6_forward_rt.ro_rt) {
488			/* route is down or destination is different */
489			ip6stat.ip6s_forward_cachemiss++;
490			RTFREE(ip6_forward_rt.ro_rt);
491			ip6_forward_rt.ro_rt = 0;
492		}
493
494		bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
495		dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
496		dst6->sin6_len = sizeof(struct sockaddr_in6);
497		dst6->sin6_family = AF_INET6;
498		dst6->sin6_addr = ip6->ip6_dst;
499
500		rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
501	}
502
503#define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
504
505	/*
506	 * Accept the packet if the forwarding interface to the destination
507	 * according to the routing table is the loopback interface,
508	 * unless the associated route has a gateway.
509	 * Note that this approach causes to accept a packet if there is a
510	 * route to the loopback interface for the destination of the packet.
511	 * But we think it's even useful in some situations, e.g. when using
512	 * a special daemon which wants to intercept the packet.
513	 *
514	 * XXX: some OSes automatically make a cloned route for the destination
515	 * of an outgoing packet.  If the outgoing interface of the packet
516	 * is a loopback one, the kernel would consider the packet to be
517	 * accepted, even if we have no such address assinged on the interface.
518	 * We check the cloned flag of the route entry to reject such cases,
519	 * assuming that route entries for our own addresses are not made by
520	 * cloning (it should be true because in6_addloop explicitly installs
521	 * the host route).  However, we might have to do an explicit check
522	 * while it would be less efficient.  Or, should we rather install a
523	 * reject route for such a case?
524	 */
525	if (ip6_forward_rt.ro_rt &&
526	    (ip6_forward_rt.ro_rt->rt_flags &
527	     (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
528#ifdef RTF_WASCLONED
529	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) &&
530#endif
531#ifdef RTF_CLONED
532	    !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
533#endif
534#if 0
535	    /*
536	     * The check below is redundant since the comparison of
537	     * the destination and the key of the rtentry has
538	     * already done through looking up the routing table.
539	     */
540	    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
541	    &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr)
542#endif
543	    ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
544		struct in6_ifaddr *ia6 =
545			(struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
546
547		/*
548		 * record address information into m_tag.
549		 */
550		(void)ip6_setdstifaddr(m, ia6);
551
552		/*
553		 * packets to a tentative, duplicated, or somehow invalid
554		 * address must not be accepted.
555		 */
556		if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
557			/* this address is ready */
558			ours = 1;
559			deliverifp = ia6->ia_ifp;	/* correct? */
560			/* Count the packet in the ip address stats */
561			ia6->ia_ifa.if_ipackets++;
562			ia6->ia_ifa.if_ibytes += m->m_pkthdr.len;
563			goto hbhcheck;
564		} else {
565			/* address is not ready, so discard the packet. */
566			nd6log((LOG_INFO,
567			    "ip6_input: packet to an unready address %s->%s\n",
568			    ip6_sprintf(&ip6->ip6_src),
569			    ip6_sprintf(&ip6->ip6_dst)));
570
571			goto bad;
572		}
573	}
574
575	/*
576	 * FAITH (Firewall Aided Internet Translator)
577	 */
578	if (ip6_keepfaith) {
579		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
580		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
581			/* XXX do we need more sanity checks? */
582			ours = 1;
583			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
584			goto hbhcheck;
585		}
586	}
587
588	/*
589	 * Now there is no reason to process the packet if it's not our own
590	 * and we're not a router.
591	 */
592	if (!ip6_forwarding) {
593		ip6stat.ip6s_cantforward++;
594		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
595		goto bad;
596	}
597
598  hbhcheck:
599	/*
600	 * record address information into m_tag, if we don't have one yet.
601	 * note that we are unable to record it, if the address is not listed
602	 * as our interface address (e.g. multicast addresses, addresses
603	 * within FAITH prefixes and such).
604	 */
605	if (deliverifp && !ip6_getdstifaddr(m)) {
606		struct in6_ifaddr *ia6;
607
608		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
609		if (ia6) {
610			if (!ip6_setdstifaddr(m, ia6)) {
611				/*
612				 * XXX maybe we should drop the packet here,
613				 * as we could not provide enough information
614				 * to the upper layers.
615				 */
616			}
617		}
618	}
619
620	/*
621	 * Process Hop-by-Hop options header if it's contained.
622	 * m may be modified in ip6_hopopts_input().
623	 * If a JumboPayload option is included, plen will also be modified.
624	 */
625	plen = (u_int32_t)ntohs(ip6->ip6_plen);
626	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
627		struct ip6_hbh *hbh;
628
629		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
630#if 0	/*touches NULL pointer*/
631			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
632#endif
633			return;	/* m have already been freed */
634		}
635
636		/* adjust pointer */
637		ip6 = mtod(m, struct ip6_hdr *);
638
639		/*
640		 * if the payload length field is 0 and the next header field
641		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
642		 * option MUST be included.
643		 */
644		if (ip6->ip6_plen == 0 && plen == 0) {
645			/*
646			 * Note that if a valid jumbo payload option is
647			 * contained, ip6_hopopts_input() must set a valid
648			 * (non-zero) payload length to the variable plen.
649			 */
650			ip6stat.ip6s_badoptions++;
651			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
652			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
653			icmp6_error(m, ICMP6_PARAM_PROB,
654				    ICMP6_PARAMPROB_HEADER,
655				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
656			return;
657		}
658#ifndef PULLDOWN_TEST
659		/* ip6_hopopts_input() ensures that mbuf is contiguous */
660		hbh = (struct ip6_hbh *)(ip6 + 1);
661#else
662		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
663			sizeof(struct ip6_hbh));
664		if (hbh == NULL) {
665			ip6stat.ip6s_tooshort++;
666			return;
667		}
668#endif
669		nxt = hbh->ip6h_nxt;
670
671		/*
672		 * accept the packet if a router alert option is included
673		 * and we act as an IPv6 router.
674		 */
675		if (rtalert != ~0 && ip6_forwarding)
676			ours = 1;
677	} else
678		nxt = ip6->ip6_nxt;
679
680	/*
681	 * Check that the amount of data in the buffers
682	 * is as at least much as the IPv6 header would have us expect.
683	 * Trim mbufs if longer than we expect.
684	 * Drop packet if shorter than we expect.
685	 */
686	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
687		ip6stat.ip6s_tooshort++;
688		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
689		goto bad;
690	}
691	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
692		if (m->m_len == m->m_pkthdr.len) {
693			m->m_len = sizeof(struct ip6_hdr) + plen;
694			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
695		} else
696			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
697	}
698
699	/*
700	 * Forward if desirable.
701	 */
702	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
703		/*
704		 * If we are acting as a multicast router, all
705		 * incoming multicast packets are passed to the
706		 * kernel-level multicast forwarding function.
707		 * The packet is returned (relatively) intact; if
708		 * ip6_mforward() returns a non-zero value, the packet
709		 * must be discarded, else it may be accepted below.
710		 */
711		if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
712			ip6stat.ip6s_cantforward++;
713			m_freem(m);
714			return;
715		}
716		if (!ours) {
717			m_freem(m);
718			return;
719		}
720	} else if (!ours) {
721		ip6_forward(m, srcrt);
722		return;
723	}
724
725	ip6 = mtod(m, struct ip6_hdr *);
726
727	/*
728	 * Malicious party may be able to use IPv4 mapped addr to confuse
729	 * tcp/udp stack and bypass security checks (act as if it was from
730	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
731	 *
732	 * For SIIT end node behavior, you may want to disable the check.
733	 * However, you will  become vulnerable to attacks using IPv4 mapped
734	 * source.
735	 */
736	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
737	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
738		ip6stat.ip6s_badscope++;
739		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
740		goto bad;
741	}
742
743	/*
744	 * Tell launch routine the next header
745	 */
746	ip6stat.ip6s_delivered++;
747	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
748	nest = 0;
749
750	while (nxt != IPPROTO_DONE) {
751		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
752			ip6stat.ip6s_toomanyhdr++;
753			goto bad;
754		}
755
756		/*
757		 * protection against faulty packet - there should be
758		 * more sanity checks in header chain processing.
759		 */
760		if (m->m_pkthdr.len < off) {
761			ip6stat.ip6s_tooshort++;
762			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
763			goto bad;
764		}
765
766#ifdef IPSEC
767		/*
768		 * enforce IPsec policy checking if we are seeing last header.
769		 * note that we do not visit this with protocols with pcb layer
770		 * code - like udp/tcp/raw ip.
771		 */
772		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
773		    ipsec6_in_reject(m, NULL)) {
774			ipsec6stat.in_polvio++;
775			goto bad;
776		}
777#endif
778		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
779	}
780	return;
781 bad:
782	m_freem(m);
783}
784
785/*
786 * set/grab in6_ifaddr correspond to IPv6 destination address.
787 * XXX backward compatibility wrapper
788 */
789static struct ip6aux *
790ip6_setdstifaddr(m, ia6)
791	struct mbuf *m;
792	struct in6_ifaddr *ia6;
793{
794	struct ip6aux *ip6a;
795
796	ip6a = ip6_addaux(m);
797	if (ip6a)
798		ip6a->ip6a_dstia6 = ia6;
799	return ip6a;	/* NULL if failed to set */
800}
801
802struct in6_ifaddr *
803ip6_getdstifaddr(m)
804	struct mbuf *m;
805{
806	struct ip6aux *ip6a;
807
808	ip6a = ip6_findaux(m);
809	if (ip6a)
810		return ip6a->ip6a_dstia6;
811	else
812		return NULL;
813}
814
815/*
816 * Hop-by-Hop options header processing. If a valid jumbo payload option is
817 * included, the real payload length will be stored in plenp.
818 */
819static int
820ip6_hopopts_input(plenp, rtalertp, mp, offp)
821	u_int32_t *plenp;
822	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
823	struct mbuf **mp;
824	int *offp;
825{
826	struct mbuf *m = *mp;
827	int off = *offp, hbhlen;
828	struct ip6_hbh *hbh;
829	u_int8_t *opt;
830
831	/* validation of the length of the header */
832#ifndef PULLDOWN_TEST
833	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
834	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
835	hbhlen = (hbh->ip6h_len + 1) << 3;
836
837	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
838	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
839#else
840	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
841		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
842	if (hbh == NULL) {
843		ip6stat.ip6s_tooshort++;
844		return -1;
845	}
846	hbhlen = (hbh->ip6h_len + 1) << 3;
847	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
848		hbhlen);
849	if (hbh == NULL) {
850		ip6stat.ip6s_tooshort++;
851		return -1;
852	}
853#endif
854	off += hbhlen;
855	hbhlen -= sizeof(struct ip6_hbh);
856	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
857
858	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
859				hbhlen, rtalertp, plenp) < 0)
860		return (-1);
861
862	*offp = off;
863	*mp = m;
864	return (0);
865}
866
867/*
868 * Search header for all Hop-by-hop options and process each option.
869 * This function is separate from ip6_hopopts_input() in order to
870 * handle a case where the sending node itself process its hop-by-hop
871 * options header. In such a case, the function is called from ip6_output().
872 *
873 * The function assumes that hbh header is located right after the IPv6 header
874 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
875 * opthead + hbhlen is located in continuous memory region.
876 */
877int
878ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
879	struct mbuf *m;
880	u_int8_t *opthead;
881	int hbhlen;
882	u_int32_t *rtalertp;
883	u_int32_t *plenp;
884{
885	struct ip6_hdr *ip6;
886	int optlen = 0;
887	u_int8_t *opt = opthead;
888	u_int16_t rtalert_val;
889	u_int32_t jumboplen;
890	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
891
892	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
893		switch (*opt) {
894		case IP6OPT_PAD1:
895			optlen = 1;
896			break;
897		case IP6OPT_PADN:
898			if (hbhlen < IP6OPT_MINLEN) {
899				ip6stat.ip6s_toosmall++;
900				goto bad;
901			}
902			optlen = *(opt + 1) + 2;
903			break;
904		case IP6OPT_ROUTER_ALERT:
905			/* XXX may need check for alignment */
906			if (hbhlen < IP6OPT_RTALERT_LEN) {
907				ip6stat.ip6s_toosmall++;
908				goto bad;
909			}
910			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
911				/* XXX stat */
912				icmp6_error(m, ICMP6_PARAM_PROB,
913				    ICMP6_PARAMPROB_HEADER,
914				    erroff + opt + 1 - opthead);
915				return (-1);
916			}
917			optlen = IP6OPT_RTALERT_LEN;
918			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
919			*rtalertp = ntohs(rtalert_val);
920			break;
921		case IP6OPT_JUMBO:
922			/* XXX may need check for alignment */
923			if (hbhlen < IP6OPT_JUMBO_LEN) {
924				ip6stat.ip6s_toosmall++;
925				goto bad;
926			}
927			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
928				/* XXX stat */
929				icmp6_error(m, ICMP6_PARAM_PROB,
930				    ICMP6_PARAMPROB_HEADER,
931				    erroff + opt + 1 - opthead);
932				return (-1);
933			}
934			optlen = IP6OPT_JUMBO_LEN;
935
936			/*
937			 * IPv6 packets that have non 0 payload length
938			 * must not contain a jumbo payload option.
939			 */
940			ip6 = mtod(m, struct ip6_hdr *);
941			if (ip6->ip6_plen) {
942				ip6stat.ip6s_badoptions++;
943				icmp6_error(m, ICMP6_PARAM_PROB,
944				    ICMP6_PARAMPROB_HEADER,
945				    erroff + opt - opthead);
946				return (-1);
947			}
948
949			/*
950			 * We may see jumbolen in unaligned location, so
951			 * we'd need to perform bcopy().
952			 */
953			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
954			jumboplen = (u_int32_t)htonl(jumboplen);
955
956#if 1
957			/*
958			 * if there are multiple jumbo payload options,
959			 * *plenp will be non-zero and the packet will be
960			 * rejected.
961			 * the behavior may need some debate in ipngwg -
962			 * multiple options does not make sense, however,
963			 * there's no explicit mention in specification.
964			 */
965			if (*plenp != 0) {
966				ip6stat.ip6s_badoptions++;
967				icmp6_error(m, ICMP6_PARAM_PROB,
968				    ICMP6_PARAMPROB_HEADER,
969				    erroff + opt + 2 - opthead);
970				return (-1);
971			}
972#endif
973
974			/*
975			 * jumbo payload length must be larger than 65535.
976			 */
977			if (jumboplen <= IPV6_MAXPACKET) {
978				ip6stat.ip6s_badoptions++;
979				icmp6_error(m, ICMP6_PARAM_PROB,
980				    ICMP6_PARAMPROB_HEADER,
981				    erroff + opt + 2 - opthead);
982				return (-1);
983			}
984			*plenp = jumboplen;
985
986			break;
987		default:		/* unknown option */
988			if (hbhlen < IP6OPT_MINLEN) {
989				ip6stat.ip6s_toosmall++;
990				goto bad;
991			}
992			optlen = ip6_unknown_opt(opt, m,
993			    erroff + opt - opthead);
994			if (optlen == -1)
995				return (-1);
996			optlen += 2;
997			break;
998		}
999	}
1000
1001	return (0);
1002
1003  bad:
1004	m_freem(m);
1005	return (-1);
1006}
1007
1008/*
1009 * Unknown option processing.
1010 * The third argument `off' is the offset from the IPv6 header to the option,
1011 * which is necessary if the IPv6 header the and option header and IPv6 header
1012 * is not continuous in order to return an ICMPv6 error.
1013 */
1014int
1015ip6_unknown_opt(optp, m, off)
1016	u_int8_t *optp;
1017	struct mbuf *m;
1018	int off;
1019{
1020	struct ip6_hdr *ip6;
1021
1022	switch (IP6OPT_TYPE(*optp)) {
1023	case IP6OPT_TYPE_SKIP: /* ignore the option */
1024		return ((int)*(optp + 1));
1025	case IP6OPT_TYPE_DISCARD:	/* silently discard */
1026		m_freem(m);
1027		return (-1);
1028	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1029		ip6stat.ip6s_badoptions++;
1030		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1031		return (-1);
1032	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1033		ip6stat.ip6s_badoptions++;
1034		ip6 = mtod(m, struct ip6_hdr *);
1035		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1036		    (m->m_flags & (M_BCAST|M_MCAST)))
1037			m_freem(m);
1038		else
1039			icmp6_error(m, ICMP6_PARAM_PROB,
1040				    ICMP6_PARAMPROB_OPTION, off);
1041		return (-1);
1042	}
1043
1044	m_freem(m);		/* XXX: NOTREACHED */
1045	return (-1);
1046}
1047
1048/*
1049 * Create the "control" list for this pcb.
1050 * The function will not modify mbuf chain at all.
1051 *
1052 * with KAME mbuf chain restriction:
1053 * The routine will be called from upper layer handlers like tcp6_input().
1054 * Thus the routine assumes that the caller (tcp6_input) have already
1055 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1056 * very first mbuf on the mbuf chain.
1057 */
1058void
1059ip6_savecontrol(in6p, m, mp)
1060	struct inpcb *in6p;
1061	struct mbuf *m, **mp;
1062{
1063#define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1064	struct thread *td = curthread;	/* XXX */
1065	int privileged = 0;
1066	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1067
1068	if (td && !suser(td))
1069		privileged++;
1070
1071#ifdef SO_TIMESTAMP
1072	if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
1073		struct timeval tv;
1074
1075		microtime(&tv);
1076		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1077		    SCM_TIMESTAMP, SOL_SOCKET);
1078		if (*mp)
1079			mp = &(*mp)->m_next;
1080	}
1081#endif
1082
1083	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
1084		return;
1085
1086	/* RFC 2292 sec. 5 */
1087	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1088		struct in6_pktinfo pi6;
1089
1090		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1091		in6_clearscope(&pi6.ipi6_addr);	/* XXX */
1092		pi6.ipi6_ifindex =
1093		    (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1094
1095		*mp = sbcreatecontrol((caddr_t) &pi6,
1096		    sizeof(struct in6_pktinfo),
1097		    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1098		if (*mp)
1099			mp = &(*mp)->m_next;
1100	}
1101
1102	if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
1103		int hlim = ip6->ip6_hlim & 0xff;
1104
1105		*mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1106		    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1107		if (*mp)
1108			mp = &(*mp)->m_next;
1109	}
1110
1111	if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1112		u_int32_t flowinfo;
1113		int tclass;
1114
1115		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1116		flowinfo >>= 20;
1117
1118		tclass = flowinfo & 0xff;
1119		*mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass),
1120		    IPV6_TCLASS, IPPROTO_IPV6);
1121		if (*mp)
1122			mp = &(*mp)->m_next;
1123	}
1124
1125	/*
1126	 * IPV6_HOPOPTS socket option. We require super-user privilege
1127	 * for the option, but it might be too strict, since there might
1128	 * be some hop-by-hop options which can be returned to normal user.
1129	 * See RFC 2292 section 6.
1130	 */
1131	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1132#ifdef DIAGNOSTIC
1133		if (!privileged)
1134			panic("IN6P_HOPOPTS is set for unprivileged socket");
1135#endif
1136		/*
1137		 * Check if a hop-by-hop options header is contatined in the
1138		 * received packet, and if so, store the options as ancillary
1139		 * data. Note that a hop-by-hop options header must be
1140		 * just after the IPv6 header, which is assured through the
1141		 * IPv6 input processing.
1142		 */
1143		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1144			struct ip6_hbh *hbh;
1145			int hbhlen = 0;
1146#ifdef PULLDOWN_TEST
1147			struct mbuf *ext;
1148#endif
1149
1150#ifndef PULLDOWN_TEST
1151			hbh = (struct ip6_hbh *)(ip6 + 1);
1152			hbhlen = (hbh->ip6h_len + 1) << 3;
1153#else
1154			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1155			    ip6->ip6_nxt);
1156			if (ext == NULL) {
1157				ip6stat.ip6s_tooshort++;
1158				return;
1159			}
1160			hbh = mtod(ext, struct ip6_hbh *);
1161			hbhlen = (hbh->ip6h_len + 1) << 3;
1162			if (hbhlen != ext->m_len) {
1163				m_freem(ext);
1164				ip6stat.ip6s_tooshort++;
1165				return;
1166			}
1167#endif
1168
1169			/*
1170			 * XXX: We copy the whole header even if a
1171			 * jumbo payload option is included, the option which
1172			 * is to be removed before returning according to
1173			 * RFC2292.
1174			 * Note: this constraint is removed in 2292bis.
1175			 */
1176			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1177			    IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1178			    IPPROTO_IPV6);
1179			if (*mp)
1180				mp = &(*mp)->m_next;
1181#ifdef PULLDOWN_TEST
1182			m_freem(ext);
1183#endif
1184		}
1185	}
1186
1187	if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1188		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1189
1190		/*
1191		 * Search for destination options headers or routing
1192		 * header(s) through the header chain, and stores each
1193		 * header as ancillary data.
1194		 * Note that the order of the headers remains in
1195		 * the chain of ancillary data.
1196		 */
1197		while (1) {	/* is explicit loop prevention necessary? */
1198			struct ip6_ext *ip6e = NULL;
1199			int elen;
1200#ifdef PULLDOWN_TEST
1201			struct mbuf *ext = NULL;
1202#endif
1203
1204			/*
1205			 * if it is not an extension header, don't try to
1206			 * pull it from the chain.
1207			 */
1208			switch (nxt) {
1209			case IPPROTO_DSTOPTS:
1210			case IPPROTO_ROUTING:
1211			case IPPROTO_HOPOPTS:
1212			case IPPROTO_AH: /* is it possible? */
1213				break;
1214			default:
1215				goto loopend;
1216			}
1217
1218#ifndef PULLDOWN_TEST
1219			if (off + sizeof(*ip6e) > m->m_len)
1220				goto loopend;
1221			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1222			if (nxt == IPPROTO_AH)
1223				elen = (ip6e->ip6e_len + 2) << 2;
1224			else
1225				elen = (ip6e->ip6e_len + 1) << 3;
1226			if (off + elen > m->m_len)
1227				goto loopend;
1228#else
1229			ext = ip6_pullexthdr(m, off, nxt);
1230			if (ext == NULL) {
1231				ip6stat.ip6s_tooshort++;
1232				return;
1233			}
1234			ip6e = mtod(ext, struct ip6_ext *);
1235			if (nxt == IPPROTO_AH)
1236				elen = (ip6e->ip6e_len + 2) << 2;
1237			else
1238				elen = (ip6e->ip6e_len + 1) << 3;
1239			if (elen != ext->m_len) {
1240				m_freem(ext);
1241				ip6stat.ip6s_tooshort++;
1242				return;
1243			}
1244#endif
1245
1246			switch (nxt) {
1247			case IPPROTO_DSTOPTS:
1248				if (!(in6p->in6p_flags & IN6P_DSTOPTS))
1249					break;
1250
1251				/*
1252				 * We also require super-user privilege for
1253				 * the option.  See comments on IN6_HOPOPTS.
1254				 */
1255				if (!privileged)
1256					break;
1257
1258				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1259				    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1260				    IPPROTO_IPV6);
1261				if (*mp)
1262					mp = &(*mp)->m_next;
1263				break;
1264			case IPPROTO_ROUTING:
1265				if (!in6p->in6p_flags & IN6P_RTHDR)
1266					break;
1267
1268				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1269				    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1270				    IPPROTO_IPV6);
1271				if (*mp)
1272					mp = &(*mp)->m_next;
1273				break;
1274			case IPPROTO_HOPOPTS:
1275			case IPPROTO_AH: /* is it possible? */
1276				break;
1277
1278			default:
1279				/*
1280			 	 * other cases have been filtered in the above.
1281				 * none will visit this case.  here we supply
1282				 * the code just in case (nxt overwritten or
1283				 * other cases).
1284				 */
1285#ifdef PULLDOWN_TEST
1286				m_freem(ext);
1287#endif
1288				goto loopend;
1289
1290			}
1291
1292			/* proceed with the next header. */
1293			off += elen;
1294			nxt = ip6e->ip6e_nxt;
1295			ip6e = NULL;
1296#ifdef PULLDOWN_TEST
1297			m_freem(ext);
1298			ext = NULL;
1299#endif
1300		}
1301	  loopend:
1302		;
1303	}
1304
1305#undef IS2292
1306}
1307
1308#ifdef PULLDOWN_TEST
1309/*
1310 * pull single extension header from mbuf chain.  returns single mbuf that
1311 * contains the result, or NULL on error.
1312 */
1313static struct mbuf *
1314ip6_pullexthdr(m, off, nxt)
1315	struct mbuf *m;
1316	size_t off;
1317	int nxt;
1318{
1319	struct ip6_ext ip6e;
1320	size_t elen;
1321	struct mbuf *n;
1322
1323#ifdef DIAGNOSTIC
1324	switch (nxt) {
1325	case IPPROTO_DSTOPTS:
1326	case IPPROTO_ROUTING:
1327	case IPPROTO_HOPOPTS:
1328	case IPPROTO_AH: /* is it possible? */
1329		break;
1330	default:
1331		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1332	}
1333#endif
1334
1335	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1336	if (nxt == IPPROTO_AH)
1337		elen = (ip6e.ip6e_len + 2) << 2;
1338	else
1339		elen = (ip6e.ip6e_len + 1) << 3;
1340
1341	MGET(n, M_DONTWAIT, MT_DATA);
1342	if (n && elen >= MLEN) {
1343		MCLGET(n, M_DONTWAIT);
1344		if ((n->m_flags & M_EXT) == 0) {
1345			m_free(n);
1346			n = NULL;
1347		}
1348	}
1349	if (!n)
1350		return NULL;
1351
1352	n->m_len = 0;
1353	if (elen >= M_TRAILINGSPACE(n)) {
1354		m_free(n);
1355		return NULL;
1356	}
1357
1358	m_copydata(m, off, elen, mtod(n, caddr_t));
1359	n->m_len = elen;
1360	return n;
1361}
1362#endif
1363
1364/*
1365 * Get pointer to the previous header followed by the header
1366 * currently processed.
1367 * XXX: This function supposes that
1368 *	M includes all headers,
1369 *	the next header field and the header length field of each header
1370 *	are valid, and
1371 *	the sum of each header length equals to OFF.
1372 * Because of these assumptions, this function must be called very
1373 * carefully. Moreover, it will not be used in the near future when
1374 * we develop `neater' mechanism to process extension headers.
1375 */
1376char *
1377ip6_get_prevhdr(m, off)
1378	struct mbuf *m;
1379	int off;
1380{
1381	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1382
1383	if (off == sizeof(struct ip6_hdr))
1384		return (&ip6->ip6_nxt);
1385	else {
1386		int len, nxt;
1387		struct ip6_ext *ip6e = NULL;
1388
1389		nxt = ip6->ip6_nxt;
1390		len = sizeof(struct ip6_hdr);
1391		while (len < off) {
1392			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1393
1394			switch (nxt) {
1395			case IPPROTO_FRAGMENT:
1396				len += sizeof(struct ip6_frag);
1397				break;
1398			case IPPROTO_AH:
1399				len += (ip6e->ip6e_len + 2) << 2;
1400				break;
1401			default:
1402				len += (ip6e->ip6e_len + 1) << 3;
1403				break;
1404			}
1405			nxt = ip6e->ip6e_nxt;
1406		}
1407		if (ip6e)
1408			return (&ip6e->ip6e_nxt);
1409		else
1410			return NULL;
1411	}
1412}
1413
1414/*
1415 * get next header offset.  m will be retained.
1416 */
1417int
1418ip6_nexthdr(m, off, proto, nxtp)
1419	struct mbuf *m;
1420	int off;
1421	int proto;
1422	int *nxtp;
1423{
1424	struct ip6_hdr ip6;
1425	struct ip6_ext ip6e;
1426	struct ip6_frag fh;
1427
1428	/* just in case */
1429	if (m == NULL)
1430		panic("ip6_nexthdr: m == NULL");
1431	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1432		return -1;
1433
1434	switch (proto) {
1435	case IPPROTO_IPV6:
1436		if (m->m_pkthdr.len < off + sizeof(ip6))
1437			return -1;
1438		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1439		if (nxtp)
1440			*nxtp = ip6.ip6_nxt;
1441		off += sizeof(ip6);
1442		return off;
1443
1444	case IPPROTO_FRAGMENT:
1445		/*
1446		 * terminate parsing if it is not the first fragment,
1447		 * it does not make sense to parse through it.
1448		 */
1449		if (m->m_pkthdr.len < off + sizeof(fh))
1450			return -1;
1451		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1452		/* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1453		if (fh.ip6f_offlg & IP6F_OFF_MASK)
1454			return -1;
1455		if (nxtp)
1456			*nxtp = fh.ip6f_nxt;
1457		off += sizeof(struct ip6_frag);
1458		return off;
1459
1460	case IPPROTO_AH:
1461		if (m->m_pkthdr.len < off + sizeof(ip6e))
1462			return -1;
1463		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1464		if (nxtp)
1465			*nxtp = ip6e.ip6e_nxt;
1466		off += (ip6e.ip6e_len + 2) << 2;
1467		return off;
1468
1469	case IPPROTO_HOPOPTS:
1470	case IPPROTO_ROUTING:
1471	case IPPROTO_DSTOPTS:
1472		if (m->m_pkthdr.len < off + sizeof(ip6e))
1473			return -1;
1474		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1475		if (nxtp)
1476			*nxtp = ip6e.ip6e_nxt;
1477		off += (ip6e.ip6e_len + 1) << 3;
1478		return off;
1479
1480	case IPPROTO_NONE:
1481	case IPPROTO_ESP:
1482	case IPPROTO_IPCOMP:
1483		/* give up */
1484		return -1;
1485
1486	default:
1487		return -1;
1488	}
1489
1490	return -1;
1491}
1492
1493/*
1494 * get offset for the last header in the chain.  m will be kept untainted.
1495 */
1496int
1497ip6_lasthdr(m, off, proto, nxtp)
1498	struct mbuf *m;
1499	int off;
1500	int proto;
1501	int *nxtp;
1502{
1503	int newoff;
1504	int nxt;
1505
1506	if (!nxtp) {
1507		nxt = -1;
1508		nxtp = &nxt;
1509	}
1510	while (1) {
1511		newoff = ip6_nexthdr(m, off, proto, nxtp);
1512		if (newoff < 0)
1513			return off;
1514		else if (newoff < off)
1515			return -1;	/* invalid */
1516		else if (newoff == off)
1517			return newoff;
1518
1519		off = newoff;
1520		proto = *nxtp;
1521	}
1522}
1523
1524struct ip6aux *
1525ip6_addaux(m)
1526	struct mbuf *m;
1527{
1528	struct m_tag *mtag;
1529
1530	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1531	if (!mtag) {
1532		mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux),
1533		    M_NOWAIT);
1534		if (mtag) {
1535			m_tag_prepend(m, mtag);
1536			bzero(mtag + 1, sizeof(struct ip6aux));
1537		}
1538	}
1539	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1540}
1541
1542struct ip6aux *
1543ip6_findaux(m)
1544	struct mbuf *m;
1545{
1546	struct m_tag *mtag;
1547
1548	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1549	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1550}
1551
1552void
1553ip6_delaux(m)
1554	struct mbuf *m;
1555{
1556	struct m_tag *mtag;
1557
1558	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1559	if (mtag)
1560		m_tag_delete(m, mtag);
1561}
1562
1563/*
1564 * System control for IP6
1565 */
1566
1567u_char	inet6ctlerrmap[PRC_NCMDS] = {
1568	0,		0,		0,		0,
1569	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1570	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1571	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1572	0,		0,		0,		0,
1573	ENOPROTOOPT
1574};
1575