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