ip6_input.c revision 169557
1/*	$FreeBSD: head/sys/netinet6/ip6_input.c 169557 2007-05-14 17:56:13Z jinmei $	*/
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			char ip6bufs[INET6_ADDRSTRLEN];
552			char ip6bufd[INET6_ADDRSTRLEN];
553			/* address is not ready, so discard the packet. */
554			nd6log((LOG_INFO,
555			    "ip6_input: packet to an unready address %s->%s\n",
556			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
557			    ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
558
559			goto bad;
560		}
561	}
562
563	/*
564	 * FAITH (Firewall Aided Internet Translator)
565	 */
566	if (ip6_keepfaith) {
567		if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
568		 && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
569			/* XXX do we need more sanity checks? */
570			ours = 1;
571			deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
572			goto hbhcheck;
573		}
574	}
575
576	/*
577	 * Now there is no reason to process the packet if it's not our own
578	 * and we're not a router.
579	 */
580	if (!ip6_forwarding) {
581		ip6stat.ip6s_cantforward++;
582		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
583		goto bad;
584	}
585
586  hbhcheck:
587	/*
588	 * record address information into m_tag, if we don't have one yet.
589	 * note that we are unable to record it, if the address is not listed
590	 * as our interface address (e.g. multicast addresses, addresses
591	 * within FAITH prefixes and such).
592	 */
593	if (deliverifp && !ip6_getdstifaddr(m)) {
594		struct in6_ifaddr *ia6;
595
596		ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
597		if (ia6) {
598			if (!ip6_setdstifaddr(m, ia6)) {
599				/*
600				 * XXX maybe we should drop the packet here,
601				 * as we could not provide enough information
602				 * to the upper layers.
603				 */
604			}
605		}
606	}
607
608	/*
609	 * Process Hop-by-Hop options header if it's contained.
610	 * m may be modified in ip6_hopopts_input().
611	 * If a JumboPayload option is included, plen will also be modified.
612	 */
613	plen = (u_int32_t)ntohs(ip6->ip6_plen);
614	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
615		struct ip6_hbh *hbh;
616
617		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
618#if 0	/*touches NULL pointer*/
619			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
620#endif
621			return;	/* m have already been freed */
622		}
623
624		/* adjust pointer */
625		ip6 = mtod(m, struct ip6_hdr *);
626
627		/*
628		 * if the payload length field is 0 and the next header field
629		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
630		 * option MUST be included.
631		 */
632		if (ip6->ip6_plen == 0 && plen == 0) {
633			/*
634			 * Note that if a valid jumbo payload option is
635			 * contained, ip6_hopopts_input() must set a valid
636			 * (non-zero) payload length to the variable plen.
637			 */
638			ip6stat.ip6s_badoptions++;
639			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
640			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
641			icmp6_error(m, ICMP6_PARAM_PROB,
642				    ICMP6_PARAMPROB_HEADER,
643				    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
644			return;
645		}
646#ifndef PULLDOWN_TEST
647		/* ip6_hopopts_input() ensures that mbuf is contiguous */
648		hbh = (struct ip6_hbh *)(ip6 + 1);
649#else
650		IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
651			sizeof(struct ip6_hbh));
652		if (hbh == NULL) {
653			ip6stat.ip6s_tooshort++;
654			return;
655		}
656#endif
657		nxt = hbh->ip6h_nxt;
658
659		/*
660		 * If we are acting as a router and the packet contains a
661		 * router alert option, see if we know the option value.
662		 * Currently, we only support the option value for MLD, in which
663		 * case we should pass the packet to the multicast routing
664		 * daemon.
665		 */
666		if (rtalert != ~0 && ip6_forwarding) {
667			switch (rtalert) {
668			case IP6OPT_RTALERT_MLD:
669				ours = 1;
670				break;
671			default:
672				/*
673				 * RFC2711 requires unrecognized values must be
674				 * silently ignored.
675				 */
676				break;
677			}
678		}
679	} else
680		nxt = ip6->ip6_nxt;
681
682	/*
683	 * Check that the amount of data in the buffers
684	 * is as at least much as the IPv6 header would have us expect.
685	 * Trim mbufs if longer than we expect.
686	 * Drop packet if shorter than we expect.
687	 */
688	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
689		ip6stat.ip6s_tooshort++;
690		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
691		goto bad;
692	}
693	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
694		if (m->m_len == m->m_pkthdr.len) {
695			m->m_len = sizeof(struct ip6_hdr) + plen;
696			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
697		} else
698			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
699	}
700
701	/*
702	 * Forward if desirable.
703	 */
704	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
705		/*
706		 * If we are acting as a multicast router, all
707		 * incoming multicast packets are passed to the
708		 * kernel-level multicast forwarding function.
709		 * The packet is returned (relatively) intact; if
710		 * ip6_mforward() returns a non-zero value, the packet
711		 * must be discarded, else it may be accepted below.
712		 */
713		if (ip6_mrouter && ip6_mforward &&
714		    ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
715			ip6stat.ip6s_cantforward++;
716			m_freem(m);
717			return;
718		}
719		if (!ours) {
720			m_freem(m);
721			return;
722		}
723	} else if (!ours) {
724		ip6_forward(m, srcrt);
725		return;
726	}
727
728	ip6 = mtod(m, struct ip6_hdr *);
729
730	/*
731	 * Malicious party may be able to use IPv4 mapped addr to confuse
732	 * tcp/udp stack and bypass security checks (act as if it was from
733	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
734	 *
735	 * For SIIT end node behavior, you may want to disable the check.
736	 * However, you will  become vulnerable to attacks using IPv4 mapped
737	 * source.
738	 */
739	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
740	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
741		ip6stat.ip6s_badscope++;
742		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
743		goto bad;
744	}
745
746	/*
747	 * Tell launch routine the next header
748	 */
749	ip6stat.ip6s_delivered++;
750	in6_ifstat_inc(deliverifp, ifs6_in_deliver);
751	nest = 0;
752
753	while (nxt != IPPROTO_DONE) {
754		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
755			ip6stat.ip6s_toomanyhdr++;
756			goto bad;
757		}
758
759		/*
760		 * protection against faulty packet - there should be
761		 * more sanity checks in header chain processing.
762		 */
763		if (m->m_pkthdr.len < off) {
764			ip6stat.ip6s_tooshort++;
765			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
766			goto bad;
767		}
768
769#ifdef IPSEC
770		/*
771		 * enforce IPsec policy checking if we are seeing last header.
772		 * note that we do not visit this with protocols with pcb layer
773		 * code - like udp/tcp/raw ip.
774		 */
775		if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
776		    ipsec6_in_reject(m, NULL)) {
777			ipsec6stat.in_polvio++;
778			goto bad;
779		}
780#endif
781		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
782	}
783	return;
784 bad:
785	m_freem(m);
786}
787
788/*
789 * set/grab in6_ifaddr correspond to IPv6 destination address.
790 * XXX backward compatibility wrapper
791 */
792static struct ip6aux *
793ip6_setdstifaddr(m, ia6)
794	struct mbuf *m;
795	struct in6_ifaddr *ia6;
796{
797	struct ip6aux *ip6a;
798
799	ip6a = ip6_addaux(m);
800	if (ip6a)
801		ip6a->ip6a_dstia6 = ia6;
802	return ip6a;	/* NULL if failed to set */
803}
804
805struct in6_ifaddr *
806ip6_getdstifaddr(m)
807	struct mbuf *m;
808{
809	struct ip6aux *ip6a;
810
811	ip6a = ip6_findaux(m);
812	if (ip6a)
813		return ip6a->ip6a_dstia6;
814	else
815		return NULL;
816}
817
818/*
819 * Hop-by-Hop options header processing. If a valid jumbo payload option is
820 * included, the real payload length will be stored in plenp.
821 */
822static int
823ip6_hopopts_input(plenp, rtalertp, mp, offp)
824	u_int32_t *plenp;
825	u_int32_t *rtalertp;	/* XXX: should be stored more smart way */
826	struct mbuf **mp;
827	int *offp;
828{
829	struct mbuf *m = *mp;
830	int off = *offp, hbhlen;
831	struct ip6_hbh *hbh;
832	u_int8_t *opt;
833
834	/* validation of the length of the header */
835#ifndef PULLDOWN_TEST
836	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
837	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
838	hbhlen = (hbh->ip6h_len + 1) << 3;
839
840	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
841	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
842#else
843	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
844		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
845	if (hbh == NULL) {
846		ip6stat.ip6s_tooshort++;
847		return -1;
848	}
849	hbhlen = (hbh->ip6h_len + 1) << 3;
850	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
851		hbhlen);
852	if (hbh == NULL) {
853		ip6stat.ip6s_tooshort++;
854		return -1;
855	}
856#endif
857	off += hbhlen;
858	hbhlen -= sizeof(struct ip6_hbh);
859	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
860
861	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
862				hbhlen, rtalertp, plenp) < 0)
863		return (-1);
864
865	*offp = off;
866	*mp = m;
867	return (0);
868}
869
870/*
871 * Search header for all Hop-by-hop options and process each option.
872 * This function is separate from ip6_hopopts_input() in order to
873 * handle a case where the sending node itself process its hop-by-hop
874 * options header. In such a case, the function is called from ip6_output().
875 *
876 * The function assumes that hbh header is located right after the IPv6 header
877 * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
878 * opthead + hbhlen is located in continuous memory region.
879 */
880int
881ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
882	struct mbuf *m;
883	u_int8_t *opthead;
884	int hbhlen;
885	u_int32_t *rtalertp;
886	u_int32_t *plenp;
887{
888	struct ip6_hdr *ip6;
889	int optlen = 0;
890	u_int8_t *opt = opthead;
891	u_int16_t rtalert_val;
892	u_int32_t jumboplen;
893	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
894
895	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
896		switch (*opt) {
897		case IP6OPT_PAD1:
898			optlen = 1;
899			break;
900		case IP6OPT_PADN:
901			if (hbhlen < IP6OPT_MINLEN) {
902				ip6stat.ip6s_toosmall++;
903				goto bad;
904			}
905			optlen = *(opt + 1) + 2;
906			break;
907		case IP6OPT_ROUTER_ALERT:
908			/* XXX may need check for alignment */
909			if (hbhlen < IP6OPT_RTALERT_LEN) {
910				ip6stat.ip6s_toosmall++;
911				goto bad;
912			}
913			if (*(opt + 1) != IP6OPT_RTALERT_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_RTALERT_LEN;
921			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
922			*rtalertp = ntohs(rtalert_val);
923			break;
924		case IP6OPT_JUMBO:
925			/* XXX may need check for alignment */
926			if (hbhlen < IP6OPT_JUMBO_LEN) {
927				ip6stat.ip6s_toosmall++;
928				goto bad;
929			}
930			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
931				/* XXX stat */
932				icmp6_error(m, ICMP6_PARAM_PROB,
933				    ICMP6_PARAMPROB_HEADER,
934				    erroff + opt + 1 - opthead);
935				return (-1);
936			}
937			optlen = IP6OPT_JUMBO_LEN;
938
939			/*
940			 * IPv6 packets that have non 0 payload length
941			 * must not contain a jumbo payload option.
942			 */
943			ip6 = mtod(m, struct ip6_hdr *);
944			if (ip6->ip6_plen) {
945				ip6stat.ip6s_badoptions++;
946				icmp6_error(m, ICMP6_PARAM_PROB,
947				    ICMP6_PARAMPROB_HEADER,
948				    erroff + opt - opthead);
949				return (-1);
950			}
951
952			/*
953			 * We may see jumbolen in unaligned location, so
954			 * we'd need to perform bcopy().
955			 */
956			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
957			jumboplen = (u_int32_t)htonl(jumboplen);
958
959#if 1
960			/*
961			 * if there are multiple jumbo payload options,
962			 * *plenp will be non-zero and the packet will be
963			 * rejected.
964			 * the behavior may need some debate in ipngwg -
965			 * multiple options does not make sense, however,
966			 * there's no explicit mention in specification.
967			 */
968			if (*plenp != 0) {
969				ip6stat.ip6s_badoptions++;
970				icmp6_error(m, ICMP6_PARAM_PROB,
971				    ICMP6_PARAMPROB_HEADER,
972				    erroff + opt + 2 - opthead);
973				return (-1);
974			}
975#endif
976
977			/*
978			 * jumbo payload length must be larger than 65535.
979			 */
980			if (jumboplen <= IPV6_MAXPACKET) {
981				ip6stat.ip6s_badoptions++;
982				icmp6_error(m, ICMP6_PARAM_PROB,
983				    ICMP6_PARAMPROB_HEADER,
984				    erroff + opt + 2 - opthead);
985				return (-1);
986			}
987			*plenp = jumboplen;
988
989			break;
990		default:		/* unknown option */
991			if (hbhlen < IP6OPT_MINLEN) {
992				ip6stat.ip6s_toosmall++;
993				goto bad;
994			}
995			optlen = ip6_unknown_opt(opt, m,
996			    erroff + opt - opthead);
997			if (optlen == -1)
998				return (-1);
999			optlen += 2;
1000			break;
1001		}
1002	}
1003
1004	return (0);
1005
1006  bad:
1007	m_freem(m);
1008	return (-1);
1009}
1010
1011/*
1012 * Unknown option processing.
1013 * The third argument `off' is the offset from the IPv6 header to the option,
1014 * which is necessary if the IPv6 header the and option header and IPv6 header
1015 * is not continuous in order to return an ICMPv6 error.
1016 */
1017int
1018ip6_unknown_opt(optp, m, off)
1019	u_int8_t *optp;
1020	struct mbuf *m;
1021	int off;
1022{
1023	struct ip6_hdr *ip6;
1024
1025	switch (IP6OPT_TYPE(*optp)) {
1026	case IP6OPT_TYPE_SKIP: /* ignore the option */
1027		return ((int)*(optp + 1));
1028	case IP6OPT_TYPE_DISCARD:	/* silently discard */
1029		m_freem(m);
1030		return (-1);
1031	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1032		ip6stat.ip6s_badoptions++;
1033		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1034		return (-1);
1035	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1036		ip6stat.ip6s_badoptions++;
1037		ip6 = mtod(m, struct ip6_hdr *);
1038		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1039		    (m->m_flags & (M_BCAST|M_MCAST)))
1040			m_freem(m);
1041		else
1042			icmp6_error(m, ICMP6_PARAM_PROB,
1043				    ICMP6_PARAMPROB_OPTION, off);
1044		return (-1);
1045	}
1046
1047	m_freem(m);		/* XXX: NOTREACHED */
1048	return (-1);
1049}
1050
1051/*
1052 * Create the "control" list for this pcb.
1053 * The function will not modify mbuf chain at all.
1054 *
1055 * with KAME mbuf chain restriction:
1056 * The routine will be called from upper layer handlers like tcp6_input().
1057 * Thus the routine assumes that the caller (tcp6_input) have already
1058 * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1059 * very first mbuf on the mbuf chain.
1060 */
1061void
1062ip6_savecontrol(in6p, m, mp)
1063	struct inpcb *in6p;
1064	struct mbuf *m, **mp;
1065{
1066#define IS2292(x, y)	((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1067	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1068
1069#ifdef SO_TIMESTAMP
1070	if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) {
1071		struct timeval tv;
1072
1073		microtime(&tv);
1074		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1075		    SCM_TIMESTAMP, SOL_SOCKET);
1076		if (*mp)
1077			mp = &(*mp)->m_next;
1078	}
1079#endif
1080
1081	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
1082		return;
1083
1084	/* RFC 2292 sec. 5 */
1085	if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1086		struct in6_pktinfo pi6;
1087
1088		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1089		in6_clearscope(&pi6.ipi6_addr);	/* XXX */
1090		pi6.ipi6_ifindex =
1091		    (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1092
1093		*mp = sbcreatecontrol((caddr_t) &pi6,
1094		    sizeof(struct in6_pktinfo),
1095		    IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1096		if (*mp)
1097			mp = &(*mp)->m_next;
1098	}
1099
1100	if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) {
1101		int hlim = ip6->ip6_hlim & 0xff;
1102
1103		*mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1104		    IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1105		if (*mp)
1106			mp = &(*mp)->m_next;
1107	}
1108
1109	if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1110		u_int32_t flowinfo;
1111		int tclass;
1112
1113		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1114		flowinfo >>= 20;
1115
1116		tclass = flowinfo & 0xff;
1117		*mp = sbcreatecontrol((caddr_t) &tclass, sizeof(tclass),
1118		    IPV6_TCLASS, IPPROTO_IPV6);
1119		if (*mp)
1120			mp = &(*mp)->m_next;
1121	}
1122
1123	/*
1124	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
1125	 * privilege for the option (see ip6_ctloutput), but it might be too
1126	 * strict, since there might be some hop-by-hop options which can be
1127	 * returned to normal user.
1128	 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1129	 */
1130	if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1131		/*
1132		 * Check if a hop-by-hop options header is contatined in the
1133		 * received packet, and if so, store the options as ancillary
1134		 * data. Note that a hop-by-hop options header must be
1135		 * just after the IPv6 header, which is assured through the
1136		 * IPv6 input processing.
1137		 */
1138		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1139			struct ip6_hbh *hbh;
1140			int hbhlen = 0;
1141#ifdef PULLDOWN_TEST
1142			struct mbuf *ext;
1143#endif
1144
1145#ifndef PULLDOWN_TEST
1146			hbh = (struct ip6_hbh *)(ip6 + 1);
1147			hbhlen = (hbh->ip6h_len + 1) << 3;
1148#else
1149			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1150			    ip6->ip6_nxt);
1151			if (ext == NULL) {
1152				ip6stat.ip6s_tooshort++;
1153				return;
1154			}
1155			hbh = mtod(ext, struct ip6_hbh *);
1156			hbhlen = (hbh->ip6h_len + 1) << 3;
1157			if (hbhlen != ext->m_len) {
1158				m_freem(ext);
1159				ip6stat.ip6s_tooshort++;
1160				return;
1161			}
1162#endif
1163
1164			/*
1165			 * XXX: We copy the whole header even if a
1166			 * jumbo payload option is included, the option which
1167			 * is to be removed before returning according to
1168			 * RFC2292.
1169			 * Note: this constraint is removed in RFC3542
1170			 */
1171			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1172			    IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1173			    IPPROTO_IPV6);
1174			if (*mp)
1175				mp = &(*mp)->m_next;
1176#ifdef PULLDOWN_TEST
1177			m_freem(ext);
1178#endif
1179		}
1180	}
1181
1182	if ((in6p->in6p_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1183		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1184
1185		/*
1186		 * Search for destination options headers or routing
1187		 * header(s) through the header chain, and stores each
1188		 * header as ancillary data.
1189		 * Note that the order of the headers remains in
1190		 * the chain of ancillary data.
1191		 */
1192		while (1) {	/* is explicit loop prevention necessary? */
1193			struct ip6_ext *ip6e = NULL;
1194			int elen;
1195#ifdef PULLDOWN_TEST
1196			struct mbuf *ext = NULL;
1197#endif
1198
1199			/*
1200			 * if it is not an extension header, don't try to
1201			 * pull it from the chain.
1202			 */
1203			switch (nxt) {
1204			case IPPROTO_DSTOPTS:
1205			case IPPROTO_ROUTING:
1206			case IPPROTO_HOPOPTS:
1207			case IPPROTO_AH: /* is it possible? */
1208				break;
1209			default:
1210				goto loopend;
1211			}
1212
1213#ifndef PULLDOWN_TEST
1214			if (off + sizeof(*ip6e) > m->m_len)
1215				goto loopend;
1216			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1217			if (nxt == IPPROTO_AH)
1218				elen = (ip6e->ip6e_len + 2) << 2;
1219			else
1220				elen = (ip6e->ip6e_len + 1) << 3;
1221			if (off + elen > m->m_len)
1222				goto loopend;
1223#else
1224			ext = ip6_pullexthdr(m, off, nxt);
1225			if (ext == NULL) {
1226				ip6stat.ip6s_tooshort++;
1227				return;
1228			}
1229			ip6e = mtod(ext, struct ip6_ext *);
1230			if (nxt == IPPROTO_AH)
1231				elen = (ip6e->ip6e_len + 2) << 2;
1232			else
1233				elen = (ip6e->ip6e_len + 1) << 3;
1234			if (elen != ext->m_len) {
1235				m_freem(ext);
1236				ip6stat.ip6s_tooshort++;
1237				return;
1238			}
1239#endif
1240
1241			switch (nxt) {
1242			case IPPROTO_DSTOPTS:
1243				if (!(in6p->in6p_flags & IN6P_DSTOPTS))
1244					break;
1245
1246				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1247				    IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1248				    IPPROTO_IPV6);
1249				if (*mp)
1250					mp = &(*mp)->m_next;
1251				break;
1252			case IPPROTO_ROUTING:
1253				if (!in6p->in6p_flags & IN6P_RTHDR)
1254					break;
1255
1256				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1257				    IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1258				    IPPROTO_IPV6);
1259				if (*mp)
1260					mp = &(*mp)->m_next;
1261				break;
1262			case IPPROTO_HOPOPTS:
1263			case IPPROTO_AH: /* is it possible? */
1264				break;
1265
1266			default:
1267				/*
1268			 	 * other cases have been filtered in the above.
1269				 * none will visit this case.  here we supply
1270				 * the code just in case (nxt overwritten or
1271				 * other cases).
1272				 */
1273#ifdef PULLDOWN_TEST
1274				m_freem(ext);
1275#endif
1276				goto loopend;
1277
1278			}
1279
1280			/* proceed with the next header. */
1281			off += elen;
1282			nxt = ip6e->ip6e_nxt;
1283			ip6e = NULL;
1284#ifdef PULLDOWN_TEST
1285			m_freem(ext);
1286			ext = NULL;
1287#endif
1288		}
1289	  loopend:
1290		;
1291	}
1292
1293#undef IS2292
1294}
1295
1296void
1297ip6_notify_pmtu(in6p, dst, mtu)
1298	struct inpcb *in6p;
1299	struct sockaddr_in6 *dst;
1300	u_int32_t *mtu;
1301{
1302	struct socket *so;
1303	struct mbuf *m_mtu;
1304	struct ip6_mtuinfo mtuctl;
1305
1306	so =  in6p->inp_socket;
1307
1308	if (mtu == NULL)
1309		return;
1310
1311#ifdef DIAGNOSTIC
1312	if (so == NULL)		/* I believe this is impossible */
1313		panic("ip6_notify_pmtu: socket is NULL");
1314#endif
1315
1316	bzero(&mtuctl, sizeof(mtuctl));	/* zero-clear for safety */
1317	mtuctl.ip6m_mtu = *mtu;
1318	mtuctl.ip6m_addr = *dst;
1319	if (sa6_recoverscope(&mtuctl.ip6m_addr))
1320		return;
1321
1322	if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1323	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1324		return;
1325
1326	if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1327	    == 0) {
1328		m_freem(m_mtu);
1329		/* XXX: should count statistics */
1330	} else
1331		sorwakeup(so);
1332
1333	return;
1334}
1335
1336#ifdef PULLDOWN_TEST
1337/*
1338 * pull single extension header from mbuf chain.  returns single mbuf that
1339 * contains the result, or NULL on error.
1340 */
1341static struct mbuf *
1342ip6_pullexthdr(m, off, nxt)
1343	struct mbuf *m;
1344	size_t off;
1345	int nxt;
1346{
1347	struct ip6_ext ip6e;
1348	size_t elen;
1349	struct mbuf *n;
1350
1351#ifdef DIAGNOSTIC
1352	switch (nxt) {
1353	case IPPROTO_DSTOPTS:
1354	case IPPROTO_ROUTING:
1355	case IPPROTO_HOPOPTS:
1356	case IPPROTO_AH: /* is it possible? */
1357		break;
1358	default:
1359		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1360	}
1361#endif
1362
1363	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1364	if (nxt == IPPROTO_AH)
1365		elen = (ip6e.ip6e_len + 2) << 2;
1366	else
1367		elen = (ip6e.ip6e_len + 1) << 3;
1368
1369	MGET(n, M_DONTWAIT, MT_DATA);
1370	if (n && elen >= MLEN) {
1371		MCLGET(n, M_DONTWAIT);
1372		if ((n->m_flags & M_EXT) == 0) {
1373			m_free(n);
1374			n = NULL;
1375		}
1376	}
1377	if (!n)
1378		return NULL;
1379
1380	n->m_len = 0;
1381	if (elen >= M_TRAILINGSPACE(n)) {
1382		m_free(n);
1383		return NULL;
1384	}
1385
1386	m_copydata(m, off, elen, mtod(n, caddr_t));
1387	n->m_len = elen;
1388	return n;
1389}
1390#endif
1391
1392/*
1393 * Get pointer to the previous header followed by the header
1394 * currently processed.
1395 * XXX: This function supposes that
1396 *	M includes all headers,
1397 *	the next header field and the header length field of each header
1398 *	are valid, and
1399 *	the sum of each header length equals to OFF.
1400 * Because of these assumptions, this function must be called very
1401 * carefully. Moreover, it will not be used in the near future when
1402 * we develop `neater' mechanism to process extension headers.
1403 */
1404char *
1405ip6_get_prevhdr(m, off)
1406	struct mbuf *m;
1407	int off;
1408{
1409	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1410
1411	if (off == sizeof(struct ip6_hdr))
1412		return (&ip6->ip6_nxt);
1413	else {
1414		int len, nxt;
1415		struct ip6_ext *ip6e = NULL;
1416
1417		nxt = ip6->ip6_nxt;
1418		len = sizeof(struct ip6_hdr);
1419		while (len < off) {
1420			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1421
1422			switch (nxt) {
1423			case IPPROTO_FRAGMENT:
1424				len += sizeof(struct ip6_frag);
1425				break;
1426			case IPPROTO_AH:
1427				len += (ip6e->ip6e_len + 2) << 2;
1428				break;
1429			default:
1430				len += (ip6e->ip6e_len + 1) << 3;
1431				break;
1432			}
1433			nxt = ip6e->ip6e_nxt;
1434		}
1435		if (ip6e)
1436			return (&ip6e->ip6e_nxt);
1437		else
1438			return NULL;
1439	}
1440}
1441
1442/*
1443 * get next header offset.  m will be retained.
1444 */
1445int
1446ip6_nexthdr(m, off, proto, nxtp)
1447	struct mbuf *m;
1448	int off;
1449	int proto;
1450	int *nxtp;
1451{
1452	struct ip6_hdr ip6;
1453	struct ip6_ext ip6e;
1454	struct ip6_frag fh;
1455
1456	/* just in case */
1457	if (m == NULL)
1458		panic("ip6_nexthdr: m == NULL");
1459	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1460		return -1;
1461
1462	switch (proto) {
1463	case IPPROTO_IPV6:
1464		if (m->m_pkthdr.len < off + sizeof(ip6))
1465			return -1;
1466		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1467		if (nxtp)
1468			*nxtp = ip6.ip6_nxt;
1469		off += sizeof(ip6);
1470		return off;
1471
1472	case IPPROTO_FRAGMENT:
1473		/*
1474		 * terminate parsing if it is not the first fragment,
1475		 * it does not make sense to parse through it.
1476		 */
1477		if (m->m_pkthdr.len < off + sizeof(fh))
1478			return -1;
1479		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1480		/* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1481		if (fh.ip6f_offlg & IP6F_OFF_MASK)
1482			return -1;
1483		if (nxtp)
1484			*nxtp = fh.ip6f_nxt;
1485		off += sizeof(struct ip6_frag);
1486		return off;
1487
1488	case IPPROTO_AH:
1489		if (m->m_pkthdr.len < off + sizeof(ip6e))
1490			return -1;
1491		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1492		if (nxtp)
1493			*nxtp = ip6e.ip6e_nxt;
1494		off += (ip6e.ip6e_len + 2) << 2;
1495		return off;
1496
1497	case IPPROTO_HOPOPTS:
1498	case IPPROTO_ROUTING:
1499	case IPPROTO_DSTOPTS:
1500		if (m->m_pkthdr.len < off + sizeof(ip6e))
1501			return -1;
1502		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1503		if (nxtp)
1504			*nxtp = ip6e.ip6e_nxt;
1505		off += (ip6e.ip6e_len + 1) << 3;
1506		return off;
1507
1508	case IPPROTO_NONE:
1509	case IPPROTO_ESP:
1510	case IPPROTO_IPCOMP:
1511		/* give up */
1512		return -1;
1513
1514	default:
1515		return -1;
1516	}
1517
1518	return -1;
1519}
1520
1521/*
1522 * get offset for the last header in the chain.  m will be kept untainted.
1523 */
1524int
1525ip6_lasthdr(m, off, proto, nxtp)
1526	struct mbuf *m;
1527	int off;
1528	int proto;
1529	int *nxtp;
1530{
1531	int newoff;
1532	int nxt;
1533
1534	if (!nxtp) {
1535		nxt = -1;
1536		nxtp = &nxt;
1537	}
1538	while (1) {
1539		newoff = ip6_nexthdr(m, off, proto, nxtp);
1540		if (newoff < 0)
1541			return off;
1542		else if (newoff < off)
1543			return -1;	/* invalid */
1544		else if (newoff == off)
1545			return newoff;
1546
1547		off = newoff;
1548		proto = *nxtp;
1549	}
1550}
1551
1552struct ip6aux *
1553ip6_addaux(m)
1554	struct mbuf *m;
1555{
1556	struct m_tag *mtag;
1557
1558	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1559	if (!mtag) {
1560		mtag = m_tag_get(PACKET_TAG_IPV6_INPUT, sizeof(struct ip6aux),
1561		    M_NOWAIT);
1562		if (mtag) {
1563			m_tag_prepend(m, mtag);
1564			bzero(mtag + 1, sizeof(struct ip6aux));
1565		}
1566	}
1567	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1568}
1569
1570struct ip6aux *
1571ip6_findaux(m)
1572	struct mbuf *m;
1573{
1574	struct m_tag *mtag;
1575
1576	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1577	return mtag ? (struct ip6aux *)(mtag + 1) : NULL;
1578}
1579
1580void
1581ip6_delaux(m)
1582	struct mbuf *m;
1583{
1584	struct m_tag *mtag;
1585
1586	mtag = m_tag_find(m, PACKET_TAG_IPV6_INPUT, NULL);
1587	if (mtag)
1588		m_tag_delete(m, mtag);
1589}
1590
1591/*
1592 * System control for IP6
1593 */
1594
1595u_char	inet6ctlerrmap[PRC_NCMDS] = {
1596	0,		0,		0,		0,
1597	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1598	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1599	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1600	0,		0,		0,		0,
1601	ENOPROTOOPT
1602};
1603