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