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