xref: /freebsd/sys/netinet6/ip6_input.c (revision f061a2215f9bf0bea98ac601a34750f89428db67)
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 #include "opt_route.h"
70 #include "opt_rss.h"
71 
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/hhook.h>
75 #include <sys/malloc.h>
76 #include <sys/mbuf.h>
77 #include <sys/proc.h>
78 #include <sys/domain.h>
79 #include <sys/protosw.h>
80 #include <sys/sdt.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/errno.h>
84 #include <sys/time.h>
85 #include <sys/kernel.h>
86 #include <sys/lock.h>
87 #include <sys/rmlock.h>
88 #include <sys/syslog.h>
89 
90 #include <net/if.h>
91 #include <net/if_var.h>
92 #include <net/if_types.h>
93 #include <net/if_dl.h>
94 #include <net/route.h>
95 #include <net/netisr.h>
96 #include <net/rss_config.h>
97 #include <net/pfil.h>
98 #include <net/vnet.h>
99 
100 #include <netinet/in.h>
101 #include <netinet/in_kdtrace.h>
102 #include <netinet/ip_var.h>
103 #include <netinet/in_systm.h>
104 #include <net/if_llatbl.h>
105 #ifdef INET
106 #include <netinet/ip.h>
107 #include <netinet/ip_icmp.h>
108 #endif /* INET */
109 #include <netinet/ip6.h>
110 #include <netinet6/in6_var.h>
111 #include <netinet6/ip6_var.h>
112 #include <netinet/in_pcb.h>
113 #include <netinet/icmp6.h>
114 #include <netinet6/scope6_var.h>
115 #include <netinet6/in6_ifattach.h>
116 #include <netinet6/mld6_var.h>
117 #include <netinet6/nd6.h>
118 #include <netinet6/in6_rss.h>
119 
120 #ifdef IPSEC
121 #include <netipsec/ipsec.h>
122 #include <netinet6/ip6_ipsec.h>
123 #include <netipsec/ipsec6.h>
124 #endif /* IPSEC */
125 
126 #include <netinet6/ip6protosw.h>
127 
128 extern struct domain inet6domain;
129 
130 u_char ip6_protox[IPPROTO_MAX];
131 VNET_DEFINE(struct in6_ifaddrhead, in6_ifaddrhead);
132 VNET_DEFINE(struct in6_ifaddrlisthead *, in6_ifaddrhashtbl);
133 VNET_DEFINE(u_long, in6_ifaddrhmask);
134 
135 static struct netisr_handler ip6_nh = {
136 	.nh_name = "ip6",
137 	.nh_handler = ip6_input,
138 	.nh_proto = NETISR_IPV6,
139 #ifdef RSS
140 	.nh_m2cpuid = rss_soft_m2cpuid_v6,
141 	.nh_policy = NETISR_POLICY_CPU,
142 	.nh_dispatch = NETISR_DISPATCH_HYBRID,
143 #else
144 	.nh_policy = NETISR_POLICY_FLOW,
145 #endif
146 };
147 
148 #ifdef RSS
149 static struct netisr_handler ip6_direct_nh = {
150 	.nh_name = "ip6_direct",
151 	.nh_handler = ip6_direct_input,
152 	.nh_proto = NETISR_IPV6_DIRECT,
153 	.nh_m2cpuid = rss_soft_m2cpuid_v6,
154 	.nh_policy = NETISR_POLICY_CPU,
155 	.nh_dispatch = NETISR_DISPATCH_HYBRID,
156 };
157 #endif
158 
159 VNET_DEFINE(struct pfil_head, inet6_pfil_hook);
160 
161 VNET_PCPUSTAT_DEFINE(struct ip6stat, ip6stat);
162 VNET_PCPUSTAT_SYSINIT(ip6stat);
163 #ifdef VIMAGE
164 VNET_PCPUSTAT_SYSUNINIT(ip6stat);
165 #endif /* VIMAGE */
166 
167 struct rmlock in6_ifaddr_lock;
168 RM_SYSINIT(in6_ifaddr_lock, &in6_ifaddr_lock, "in6_ifaddr_lock");
169 
170 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
171 #ifdef PULLDOWN_TEST
172 static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
173 #endif
174 
175 /*
176  * IP6 initialization: fill in IP6 protocol switch table.
177  * All protocols not implemented in kernel go to raw IP6 protocol handler.
178  */
179 void
180 ip6_init(void)
181 {
182 	struct protosw *pr;
183 	int i;
184 
185 	TUNABLE_INT_FETCH("net.inet6.ip6.auto_linklocal",
186 	    &V_ip6_auto_linklocal);
187 	TUNABLE_INT_FETCH("net.inet6.ip6.accept_rtadv", &V_ip6_accept_rtadv);
188 	TUNABLE_INT_FETCH("net.inet6.ip6.no_radr", &V_ip6_no_radr);
189 
190 	TAILQ_INIT(&V_in6_ifaddrhead);
191 	V_in6_ifaddrhashtbl = hashinit(IN6ADDR_NHASH, M_IFADDR,
192 	    &V_in6_ifaddrhmask);
193 
194 	/* Initialize packet filter hooks. */
195 	V_inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
196 	V_inet6_pfil_hook.ph_af = AF_INET6;
197 	if ((i = pfil_head_register(&V_inet6_pfil_hook)) != 0)
198 		printf("%s: WARNING: unable to register pfil hook, "
199 			"error %d\n", __func__, i);
200 
201 	if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET6,
202 	    &V_ipsec_hhh_in[HHOOK_IPSEC_INET6],
203 	    HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
204 		printf("%s: WARNING: unable to register input helper hook\n",
205 		    __func__);
206 	if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET6,
207 	    &V_ipsec_hhh_out[HHOOK_IPSEC_INET6],
208 	    HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
209 		printf("%s: WARNING: unable to register output helper hook\n",
210 		    __func__);
211 
212 	scope6_init();
213 	addrsel_policy_init();
214 	nd6_init();
215 	frag6_init();
216 
217 	V_ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
218 
219 	/* Skip global initialization stuff for non-default instances. */
220 #ifdef VIMAGE
221 	if (!IS_DEFAULT_VNET(curvnet)) {
222 		netisr_register_vnet(&ip6_nh);
223 #ifdef RSS
224 		netisr_register_vnet(&ip6_direct_nh);
225 #endif
226 		return;
227 	}
228 #endif
229 
230 	pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
231 	if (pr == NULL)
232 		panic("ip6_init");
233 
234 	/* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
235 	for (i = 0; i < IPPROTO_MAX; i++)
236 		ip6_protox[i] = pr - inet6sw;
237 	/*
238 	 * Cycle through IP protocols and put them into the appropriate place
239 	 * in ip6_protox[].
240 	 */
241 	for (pr = inet6domain.dom_protosw;
242 	    pr < inet6domain.dom_protoswNPROTOSW; pr++)
243 		if (pr->pr_domain->dom_family == PF_INET6 &&
244 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
245 			/* Be careful to only index valid IP protocols. */
246 			if (pr->pr_protocol < IPPROTO_MAX)
247 				ip6_protox[pr->pr_protocol] = pr - inet6sw;
248 		}
249 
250 	netisr_register(&ip6_nh);
251 #ifdef RSS
252 	netisr_register(&ip6_direct_nh);
253 #endif
254 }
255 
256 /*
257  * The protocol to be inserted into ip6_protox[] must be already registered
258  * in inet6sw[], either statically or through pf_proto_register().
259  */
260 int
261 ip6proto_register(short ip6proto)
262 {
263 	struct protosw *pr;
264 
265 	/* Sanity checks. */
266 	if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
267 		return (EPROTONOSUPPORT);
268 
269 	/*
270 	 * The protocol slot must not be occupied by another protocol
271 	 * already.  An index pointing to IPPROTO_RAW is unused.
272 	 */
273 	pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
274 	if (pr == NULL)
275 		return (EPFNOSUPPORT);
276 	if (ip6_protox[ip6proto] != pr - inet6sw)	/* IPPROTO_RAW */
277 		return (EEXIST);
278 
279 	/*
280 	 * Find the protocol position in inet6sw[] and set the index.
281 	 */
282 	for (pr = inet6domain.dom_protosw;
283 	    pr < inet6domain.dom_protoswNPROTOSW; pr++) {
284 		if (pr->pr_domain->dom_family == PF_INET6 &&
285 		    pr->pr_protocol && pr->pr_protocol == ip6proto) {
286 			ip6_protox[pr->pr_protocol] = pr - inet6sw;
287 			return (0);
288 		}
289 	}
290 	return (EPROTONOSUPPORT);
291 }
292 
293 int
294 ip6proto_unregister(short ip6proto)
295 {
296 	struct protosw *pr;
297 
298 	/* Sanity checks. */
299 	if (ip6proto <= 0 || ip6proto >= IPPROTO_MAX)
300 		return (EPROTONOSUPPORT);
301 
302 	/* Check if the protocol was indeed registered. */
303 	pr = pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
304 	if (pr == NULL)
305 		return (EPFNOSUPPORT);
306 	if (ip6_protox[ip6proto] == pr - inet6sw)	/* IPPROTO_RAW */
307 		return (ENOENT);
308 
309 	/* Reset the protocol slot to IPPROTO_RAW. */
310 	ip6_protox[ip6proto] = pr - inet6sw;
311 	return (0);
312 }
313 
314 #ifdef VIMAGE
315 static void
316 ip6_destroy(void *unused __unused)
317 {
318 	struct ifaddr *ifa, *nifa;
319 	struct ifnet *ifp;
320 	int error;
321 
322 #ifdef RSS
323 	netisr_unregister_vnet(&ip6_direct_nh);
324 #endif
325 	netisr_unregister_vnet(&ip6_nh);
326 
327 	if ((error = pfil_head_unregister(&V_inet6_pfil_hook)) != 0)
328 		printf("%s: WARNING: unable to unregister pfil hook, "
329 		    "error %d\n", __func__, error);
330 	error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET6]);
331 	if (error != 0) {
332 		printf("%s: WARNING: unable to deregister input helper hook "
333 		    "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET6: "
334 		    "error %d returned\n", __func__, error);
335 	}
336 	error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET6]);
337 	if (error != 0) {
338 		printf("%s: WARNING: unable to deregister output helper hook "
339 		    "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET6: "
340 		    "error %d returned\n", __func__, error);
341 	}
342 
343 	/* Cleanup addresses. */
344 	IFNET_RLOCK();
345 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
346 		/* Cannot lock here - lock recursion. */
347 		/* IF_ADDR_LOCK(ifp); */
348 		TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
349 
350 			if (ifa->ifa_addr->sa_family != AF_INET6)
351 				continue;
352 			in6_purgeaddr(ifa);
353 		}
354 		/* IF_ADDR_UNLOCK(ifp); */
355 		in6_ifdetach_destroy(ifp);
356 		mld_domifdetach(ifp);
357 		/* Make sure any routes are gone as well. */
358 		rt_flushifroutes_af(ifp, AF_INET6);
359 	}
360 	IFNET_RUNLOCK();
361 
362 	nd6_destroy();
363 	in6_ifattach_destroy();
364 
365 	hashdestroy(V_in6_ifaddrhashtbl, M_IFADDR, V_in6_ifaddrhmask);
366 }
367 
368 VNET_SYSUNINIT(inet6, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip6_destroy, NULL);
369 #endif
370 
371 static int
372 ip6_input_hbh(struct mbuf *m, uint32_t *plen, uint32_t *rtalert, int *off,
373     int *nxt, int *ours)
374 {
375 	struct ip6_hdr *ip6;
376 	struct ip6_hbh *hbh;
377 
378 	if (ip6_hopopts_input(plen, rtalert, &m, off)) {
379 #if 0	/*touches NULL pointer*/
380 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
381 #endif
382 		goto out;	/* m have already been freed */
383 	}
384 
385 	/* adjust pointer */
386 	ip6 = mtod(m, struct ip6_hdr *);
387 
388 	/*
389 	 * if the payload length field is 0 and the next header field
390 	 * indicates Hop-by-Hop Options header, then a Jumbo Payload
391 	 * option MUST be included.
392 	 */
393 	if (ip6->ip6_plen == 0 && *plen == 0) {
394 		/*
395 		 * Note that if a valid jumbo payload option is
396 		 * contained, ip6_hopopts_input() must set a valid
397 		 * (non-zero) payload length to the variable plen.
398 		 */
399 		IP6STAT_INC(ip6s_badoptions);
400 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
401 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
402 		icmp6_error(m, ICMP6_PARAM_PROB,
403 			    ICMP6_PARAMPROB_HEADER,
404 			    (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
405 		goto out;
406 	}
407 #ifndef PULLDOWN_TEST
408 	/* ip6_hopopts_input() ensures that mbuf is contiguous */
409 	hbh = (struct ip6_hbh *)(ip6 + 1);
410 #else
411 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
412 		sizeof(struct ip6_hbh));
413 	if (hbh == NULL) {
414 		IP6STAT_INC(ip6s_tooshort);
415 		goto out;
416 	}
417 #endif
418 	*nxt = hbh->ip6h_nxt;
419 
420 	/*
421 	 * If we are acting as a router and the packet contains a
422 	 * router alert option, see if we know the option value.
423 	 * Currently, we only support the option value for MLD, in which
424 	 * case we should pass the packet to the multicast routing
425 	 * daemon.
426 	 */
427 	if (*rtalert != ~0) {
428 		switch (*rtalert) {
429 		case IP6OPT_RTALERT_MLD:
430 			if (V_ip6_forwarding)
431 				*ours = 1;
432 			break;
433 		default:
434 			/*
435 			 * RFC2711 requires unrecognized values must be
436 			 * silently ignored.
437 			 */
438 			break;
439 		}
440 	}
441 
442 	return (0);
443 
444 out:
445 	return (1);
446 }
447 
448 #ifdef RSS
449 /*
450  * IPv6 direct input routine.
451  *
452  * This is called when reinjecting completed fragments where
453  * all of the previous checking and book-keeping has been done.
454  */
455 void
456 ip6_direct_input(struct mbuf *m)
457 {
458 	int off, nxt;
459 	int nest;
460 	struct m_tag *mtag;
461 	struct ip6_direct_ctx *ip6dc;
462 
463 	mtag = m_tag_locate(m, MTAG_ABI_IPV6, IPV6_TAG_DIRECT, NULL);
464 	KASSERT(mtag != NULL, ("Reinjected packet w/o direct ctx tag!"));
465 
466 	ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
467 	nxt = ip6dc->ip6dc_nxt;
468 	off = ip6dc->ip6dc_off;
469 
470 	nest = 0;
471 
472 	m_tag_delete(m, mtag);
473 
474 	while (nxt != IPPROTO_DONE) {
475 		if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
476 			IP6STAT_INC(ip6s_toomanyhdr);
477 			goto bad;
478 		}
479 
480 		/*
481 		 * protection against faulty packet - there should be
482 		 * more sanity checks in header chain processing.
483 		 */
484 		if (m->m_pkthdr.len < off) {
485 			IP6STAT_INC(ip6s_tooshort);
486 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
487 			goto bad;
488 		}
489 
490 #ifdef IPSEC
491 		/*
492 		 * enforce IPsec policy checking if we are seeing last header.
493 		 * note that we do not visit this with protocols with pcb layer
494 		 * code - like udp/tcp/raw ip.
495 		 */
496 		if (ip6_ipsec_input(m, nxt))
497 			goto bad;
498 #endif /* IPSEC */
499 
500 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
501 	}
502 	return;
503 bad:
504 	m_freem(m);
505 }
506 #endif
507 
508 void
509 ip6_input(struct mbuf *m)
510 {
511 	struct in6_addr odst;
512 	struct ip6_hdr *ip6;
513 	struct in6_ifaddr *ia;
514 	u_int32_t plen;
515 	u_int32_t rtalert = ~0;
516 	int off = sizeof(struct ip6_hdr), nest;
517 	int nxt, ours = 0;
518 	int srcrt = 0;
519 
520 #ifdef IPSEC
521 	/*
522 	 * should the inner packet be considered authentic?
523 	 * see comment in ah4_input().
524 	 * NB: m cannot be NULL when passed to the input routine
525 	 */
526 
527 	m->m_flags &= ~M_AUTHIPHDR;
528 	m->m_flags &= ~M_AUTHIPDGM;
529 
530 #endif /* IPSEC */
531 
532 	if (m->m_flags & M_FASTFWD_OURS) {
533 		/*
534 		 * Firewall changed destination to local.
535 		 */
536 		m->m_flags &= ~M_FASTFWD_OURS;
537 		ours = 1;
538 		ip6 = mtod(m, struct ip6_hdr *);
539 		goto hbhcheck;
540 	}
541 
542 	/*
543 	 * mbuf statistics
544 	 */
545 	if (m->m_flags & M_EXT) {
546 		if (m->m_next)
547 			IP6STAT_INC(ip6s_mext2m);
548 		else
549 			IP6STAT_INC(ip6s_mext1);
550 	} else {
551 		if (m->m_next) {
552 			if (m->m_flags & M_LOOP) {
553 				IP6STAT_INC(ip6s_m2m[V_loif->if_index]);
554 			} else if (m->m_pkthdr.rcvif->if_index < IP6S_M2MMAX)
555 				IP6STAT_INC(
556 				    ip6s_m2m[m->m_pkthdr.rcvif->if_index]);
557 			else
558 				IP6STAT_INC(ip6s_m2m[0]);
559 		} else
560 			IP6STAT_INC(ip6s_m1);
561 	}
562 
563 	/* drop the packet if IPv6 operation is disabled on the IF */
564 	if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED))
565 		goto bad;
566 
567 	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
568 	IP6STAT_INC(ip6s_total);
569 
570 #ifndef PULLDOWN_TEST
571 	/*
572 	 * L2 bridge code and some other code can return mbuf chain
573 	 * that does not conform to KAME requirement.  too bad.
574 	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
575 	 */
576 	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
577 		struct mbuf *n;
578 
579 		if (m->m_pkthdr.len > MHLEN)
580 			n = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
581 		else
582 			n = m_gethdr(M_NOWAIT, MT_DATA);
583 		if (n == NULL) {
584 			m_freem(m);
585 			return;	/* ENOBUFS */
586 		}
587 
588 		m_move_pkthdr(n, m);
589 		m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
590 		n->m_len = n->m_pkthdr.len;
591 		m_freem(m);
592 		m = n;
593 	}
594 	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /* nothing */);
595 #endif
596 
597 	if (m->m_len < sizeof(struct ip6_hdr)) {
598 		struct ifnet *inifp;
599 		inifp = m->m_pkthdr.rcvif;
600 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
601 			IP6STAT_INC(ip6s_toosmall);
602 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
603 			return;
604 		}
605 	}
606 
607 	ip6 = mtod(m, struct ip6_hdr *);
608 
609 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
610 		IP6STAT_INC(ip6s_badvers);
611 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
612 		goto bad;
613 	}
614 
615 	IP6STAT_INC(ip6s_nxthist[ip6->ip6_nxt]);
616 
617 	IP_PROBE(receive, NULL, NULL, ip6, m->m_pkthdr.rcvif, NULL, ip6);
618 
619 	/*
620 	 * Check against address spoofing/corruption.
621 	 */
622 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
623 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
624 		/*
625 		 * XXX: "badscope" is not very suitable for a multicast source.
626 		 */
627 		IP6STAT_INC(ip6s_badscope);
628 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
629 		goto bad;
630 	}
631 	if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
632 	    !(m->m_flags & M_LOOP)) {
633 		/*
634 		 * In this case, the packet should come from the loopback
635 		 * interface.  However, we cannot just check the if_flags,
636 		 * because ip6_mloopback() passes the "actual" interface
637 		 * as the outgoing/incoming interface.
638 		 */
639 		IP6STAT_INC(ip6s_badscope);
640 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
641 		goto bad;
642 	}
643 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
644 	    IPV6_ADDR_MC_SCOPE(&ip6->ip6_dst) == 0) {
645 		/*
646 		 * RFC4291 2.7:
647 		 * Nodes must not originate a packet to a multicast address
648 		 * whose scop field contains the reserved value 0; if such
649 		 * a packet is received, it must be silently dropped.
650 		 */
651 		IP6STAT_INC(ip6s_badscope);
652 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
653 		goto bad;
654 	}
655 #ifdef ALTQ
656 	if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
657 		/* packet is dropped by traffic conditioner */
658 		return;
659 	}
660 #endif
661 	/*
662 	 * The following check is not documented in specs.  A malicious
663 	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
664 	 * and bypass security checks (act as if it was from 127.0.0.1 by using
665 	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
666 	 *
667 	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
668 	 * support IPv4-less kernel compilation, we cannot support SIIT
669 	 * environment at all.  So, it makes more sense for us to reject any
670 	 * malicious packets for non-SIIT environment, than try to do a
671 	 * partial support for SIIT environment.
672 	 */
673 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
674 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
675 		IP6STAT_INC(ip6s_badscope);
676 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
677 		goto bad;
678 	}
679 #if 0
680 	/*
681 	 * Reject packets with IPv4 compatible addresses (auto tunnel).
682 	 *
683 	 * The code forbids auto tunnel relay case in RFC1933 (the check is
684 	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
685 	 * is revised to forbid relaying case.
686 	 */
687 	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
688 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
689 		IP6STAT_INC(ip6s_badscope);
690 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
691 		goto bad;
692 	}
693 #endif
694 #ifdef IPSEC
695 	/*
696 	 * Bypass packet filtering for packets previously handled by IPsec.
697 	 */
698 	if (ip6_ipsec_filtertunnel(m))
699 		goto passin;
700 #endif /* IPSEC */
701 
702 	/*
703 	 * Run through list of hooks for input packets.
704 	 *
705 	 * NB: Beware of the destination address changing
706 	 *     (e.g. by NAT rewriting).  When this happens,
707 	 *     tell ip6_forward to do the right thing.
708 	 */
709 	odst = ip6->ip6_dst;
710 
711 	/* Jump over all PFIL processing if hooks are not active. */
712 	if (!PFIL_HOOKED(&V_inet6_pfil_hook))
713 		goto passin;
714 
715 	if (pfil_run_hooks(&V_inet6_pfil_hook, &m,
716 	    m->m_pkthdr.rcvif, PFIL_IN, NULL))
717 		return;
718 	if (m == NULL)			/* consumed by filter */
719 		return;
720 	ip6 = mtod(m, struct ip6_hdr *);
721 	srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
722 
723 	if (m->m_flags & M_FASTFWD_OURS) {
724 		m->m_flags &= ~M_FASTFWD_OURS;
725 		ours = 1;
726 		goto hbhcheck;
727 	}
728 	if ((m->m_flags & M_IP6_NEXTHOP) &&
729 	    m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
730 		/*
731 		 * Directly ship the packet on.  This allows forwarding
732 		 * packets originally destined to us to some other directly
733 		 * connected host.
734 		 */
735 		ip6_forward(m, 1);
736 		return;
737 	}
738 
739 passin:
740 	/*
741 	 * Disambiguate address scope zones (if there is ambiguity).
742 	 * We first make sure that the original source or destination address
743 	 * is not in our internal form for scoped addresses.  Such addresses
744 	 * are not necessarily invalid spec-wise, but we cannot accept them due
745 	 * to the usage conflict.
746 	 * in6_setscope() then also checks and rejects the cases where src or
747 	 * dst are the loopback address and the receiving interface
748 	 * is not loopback.
749 	 */
750 	if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
751 		IP6STAT_INC(ip6s_badscope); /* XXX */
752 		goto bad;
753 	}
754 	if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) ||
755 	    in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) {
756 		IP6STAT_INC(ip6s_badscope);
757 		goto bad;
758 	}
759 	/*
760 	 * Multicast check. Assume packet is for us to avoid
761 	 * prematurely taking locks.
762 	 */
763 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
764 		ours = 1;
765 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
766 		goto hbhcheck;
767 	}
768 	/*
769 	 * Unicast check
770 	 * XXX: For now we keep link-local IPv6 addresses with embedded
771 	 *      scope zone id, therefore we use zero zoneid here.
772 	 */
773 	ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
774 	if (ia != NULL) {
775 		if (ia->ia6_flags & IN6_IFF_NOTREADY) {
776 			char ip6bufs[INET6_ADDRSTRLEN];
777 			char ip6bufd[INET6_ADDRSTRLEN];
778 			/* address is not ready, so discard the packet. */
779 			nd6log((LOG_INFO,
780 			    "ip6_input: packet to an unready address %s->%s\n",
781 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
782 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst)));
783 			ifa_free(&ia->ia_ifa);
784 			goto bad;
785 		}
786 		/* Count the packet in the ip address stats */
787 		counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
788 		counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len);
789 		ifa_free(&ia->ia_ifa);
790 		ours = 1;
791 		goto hbhcheck;
792 	}
793 
794 	/*
795 	 * Now there is no reason to process the packet if it's not our own
796 	 * and we're not a router.
797 	 */
798 	if (!V_ip6_forwarding) {
799 		IP6STAT_INC(ip6s_cantforward);
800 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
801 		goto bad;
802 	}
803 
804   hbhcheck:
805 	/*
806 	 * Process Hop-by-Hop options header if it's contained.
807 	 * m may be modified in ip6_hopopts_input().
808 	 * If a JumboPayload option is included, plen will also be modified.
809 	 */
810 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
811 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
812 		if (ip6_input_hbh(m, &plen, &rtalert, &off, &nxt, &ours) != 0)
813 			return;
814 	} else
815 		nxt = ip6->ip6_nxt;
816 
817 	/*
818 	 * Use mbuf flags to propagate Router Alert option to
819 	 * ICMPv6 layer, as hop-by-hop options have been stripped.
820 	 */
821 	if (rtalert != ~0)
822 		m->m_flags |= M_RTALERT_MLD;
823 
824 	/*
825 	 * Check that the amount of data in the buffers
826 	 * is as at least much as the IPv6 header would have us expect.
827 	 * Trim mbufs if longer than we expect.
828 	 * Drop packet if shorter than we expect.
829 	 */
830 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
831 		IP6STAT_INC(ip6s_tooshort);
832 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
833 		goto bad;
834 	}
835 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
836 		if (m->m_len == m->m_pkthdr.len) {
837 			m->m_len = sizeof(struct ip6_hdr) + plen;
838 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
839 		} else
840 			m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
841 	}
842 
843 	/*
844 	 * Forward if desirable.
845 	 */
846 	if (V_ip6_mrouter &&
847 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
848 		/*
849 		 * If we are acting as a multicast router, all
850 		 * incoming multicast packets are passed to the
851 		 * kernel-level multicast forwarding function.
852 		 * The packet is returned (relatively) intact; if
853 		 * ip6_mforward() returns a non-zero value, the packet
854 		 * must be discarded, else it may be accepted below.
855 		 *
856 		 * XXX TODO: Check hlim and multicast scope here to avoid
857 		 * unnecessarily calling into ip6_mforward().
858 		 */
859 		if (ip6_mforward &&
860 		    ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
861 			IP6STAT_INC(ip6s_cantforward);
862 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
863 			goto bad;
864 		}
865 	} else if (!ours) {
866 		ip6_forward(m, srcrt);
867 		return;
868 	}
869 
870 	ip6 = mtod(m, struct ip6_hdr *);
871 
872 	/*
873 	 * Malicious party may be able to use IPv4 mapped addr to confuse
874 	 * tcp/udp stack and bypass security checks (act as if it was from
875 	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
876 	 *
877 	 * For SIIT end node behavior, you may want to disable the check.
878 	 * However, you will  become vulnerable to attacks using IPv4 mapped
879 	 * source.
880 	 */
881 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
882 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
883 		IP6STAT_INC(ip6s_badscope);
884 		in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
885 		goto bad;
886 	}
887 
888 	/*
889 	 * Tell launch routine the next header
890 	 */
891 	IP6STAT_INC(ip6s_delivered);
892 	in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_deliver);
893 	nest = 0;
894 
895 	while (nxt != IPPROTO_DONE) {
896 		if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
897 			IP6STAT_INC(ip6s_toomanyhdr);
898 			goto bad;
899 		}
900 
901 		/*
902 		 * protection against faulty packet - there should be
903 		 * more sanity checks in header chain processing.
904 		 */
905 		if (m->m_pkthdr.len < off) {
906 			IP6STAT_INC(ip6s_tooshort);
907 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
908 			goto bad;
909 		}
910 
911 #ifdef IPSEC
912 		/*
913 		 * enforce IPsec policy checking if we are seeing last header.
914 		 * note that we do not visit this with protocols with pcb layer
915 		 * code - like udp/tcp/raw ip.
916 		 */
917 		if (ip6_ipsec_input(m, nxt))
918 			goto bad;
919 #endif /* IPSEC */
920 
921 		nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
922 	}
923 	return;
924 bad:
925 	m_freem(m);
926 }
927 
928 /*
929  * Hop-by-Hop options header processing. If a valid jumbo payload option is
930  * included, the real payload length will be stored in plenp.
931  *
932  * rtalertp - XXX: should be stored more smart way
933  */
934 static int
935 ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
936     struct mbuf **mp, int *offp)
937 {
938 	struct mbuf *m = *mp;
939 	int off = *offp, hbhlen;
940 	struct ip6_hbh *hbh;
941 
942 	/* validation of the length of the header */
943 #ifndef PULLDOWN_TEST
944 	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
945 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
946 	hbhlen = (hbh->ip6h_len + 1) << 3;
947 
948 	IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
949 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
950 #else
951 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
952 		sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
953 	if (hbh == NULL) {
954 		IP6STAT_INC(ip6s_tooshort);
955 		return -1;
956 	}
957 	hbhlen = (hbh->ip6h_len + 1) << 3;
958 	IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
959 		hbhlen);
960 	if (hbh == NULL) {
961 		IP6STAT_INC(ip6s_tooshort);
962 		return -1;
963 	}
964 #endif
965 	off += hbhlen;
966 	hbhlen -= sizeof(struct ip6_hbh);
967 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
968 				hbhlen, rtalertp, plenp) < 0)
969 		return (-1);
970 
971 	*offp = off;
972 	*mp = m;
973 	return (0);
974 }
975 
976 /*
977  * Search header for all Hop-by-hop options and process each option.
978  * This function is separate from ip6_hopopts_input() in order to
979  * handle a case where the sending node itself process its hop-by-hop
980  * options header. In such a case, the function is called from ip6_output().
981  *
982  * The function assumes that hbh header is located right after the IPv6 header
983  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
984  * opthead + hbhlen is located in contiguous memory region.
985  */
986 int
987 ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
988     u_int32_t *rtalertp, u_int32_t *plenp)
989 {
990 	struct ip6_hdr *ip6;
991 	int optlen = 0;
992 	u_int8_t *opt = opthead;
993 	u_int16_t rtalert_val;
994 	u_int32_t jumboplen;
995 	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
996 
997 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
998 		switch (*opt) {
999 		case IP6OPT_PAD1:
1000 			optlen = 1;
1001 			break;
1002 		case IP6OPT_PADN:
1003 			if (hbhlen < IP6OPT_MINLEN) {
1004 				IP6STAT_INC(ip6s_toosmall);
1005 				goto bad;
1006 			}
1007 			optlen = *(opt + 1) + 2;
1008 			break;
1009 		case IP6OPT_ROUTER_ALERT:
1010 			/* XXX may need check for alignment */
1011 			if (hbhlen < IP6OPT_RTALERT_LEN) {
1012 				IP6STAT_INC(ip6s_toosmall);
1013 				goto bad;
1014 			}
1015 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1016 				/* XXX stat */
1017 				icmp6_error(m, ICMP6_PARAM_PROB,
1018 				    ICMP6_PARAMPROB_HEADER,
1019 				    erroff + opt + 1 - opthead);
1020 				return (-1);
1021 			}
1022 			optlen = IP6OPT_RTALERT_LEN;
1023 			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
1024 			*rtalertp = ntohs(rtalert_val);
1025 			break;
1026 		case IP6OPT_JUMBO:
1027 			/* XXX may need check for alignment */
1028 			if (hbhlen < IP6OPT_JUMBO_LEN) {
1029 				IP6STAT_INC(ip6s_toosmall);
1030 				goto bad;
1031 			}
1032 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1033 				/* XXX stat */
1034 				icmp6_error(m, ICMP6_PARAM_PROB,
1035 				    ICMP6_PARAMPROB_HEADER,
1036 				    erroff + opt + 1 - opthead);
1037 				return (-1);
1038 			}
1039 			optlen = IP6OPT_JUMBO_LEN;
1040 
1041 			/*
1042 			 * IPv6 packets that have non 0 payload length
1043 			 * must not contain a jumbo payload option.
1044 			 */
1045 			ip6 = mtod(m, struct ip6_hdr *);
1046 			if (ip6->ip6_plen) {
1047 				IP6STAT_INC(ip6s_badoptions);
1048 				icmp6_error(m, ICMP6_PARAM_PROB,
1049 				    ICMP6_PARAMPROB_HEADER,
1050 				    erroff + opt - opthead);
1051 				return (-1);
1052 			}
1053 
1054 			/*
1055 			 * We may see jumbolen in unaligned location, so
1056 			 * we'd need to perform bcopy().
1057 			 */
1058 			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1059 			jumboplen = (u_int32_t)htonl(jumboplen);
1060 
1061 #if 1
1062 			/*
1063 			 * if there are multiple jumbo payload options,
1064 			 * *plenp will be non-zero and the packet will be
1065 			 * rejected.
1066 			 * the behavior may need some debate in ipngwg -
1067 			 * multiple options does not make sense, however,
1068 			 * there's no explicit mention in specification.
1069 			 */
1070 			if (*plenp != 0) {
1071 				IP6STAT_INC(ip6s_badoptions);
1072 				icmp6_error(m, ICMP6_PARAM_PROB,
1073 				    ICMP6_PARAMPROB_HEADER,
1074 				    erroff + opt + 2 - opthead);
1075 				return (-1);
1076 			}
1077 #endif
1078 
1079 			/*
1080 			 * jumbo payload length must be larger than 65535.
1081 			 */
1082 			if (jumboplen <= IPV6_MAXPACKET) {
1083 				IP6STAT_INC(ip6s_badoptions);
1084 				icmp6_error(m, ICMP6_PARAM_PROB,
1085 				    ICMP6_PARAMPROB_HEADER,
1086 				    erroff + opt + 2 - opthead);
1087 				return (-1);
1088 			}
1089 			*plenp = jumboplen;
1090 
1091 			break;
1092 		default:		/* unknown option */
1093 			if (hbhlen < IP6OPT_MINLEN) {
1094 				IP6STAT_INC(ip6s_toosmall);
1095 				goto bad;
1096 			}
1097 			optlen = ip6_unknown_opt(opt, m,
1098 			    erroff + opt - opthead);
1099 			if (optlen == -1)
1100 				return (-1);
1101 			optlen += 2;
1102 			break;
1103 		}
1104 	}
1105 
1106 	return (0);
1107 
1108   bad:
1109 	m_freem(m);
1110 	return (-1);
1111 }
1112 
1113 /*
1114  * Unknown option processing.
1115  * The third argument `off' is the offset from the IPv6 header to the option,
1116  * which is necessary if the IPv6 header the and option header and IPv6 header
1117  * is not contiguous in order to return an ICMPv6 error.
1118  */
1119 int
1120 ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1121 {
1122 	struct ip6_hdr *ip6;
1123 
1124 	switch (IP6OPT_TYPE(*optp)) {
1125 	case IP6OPT_TYPE_SKIP: /* ignore the option */
1126 		return ((int)*(optp + 1));
1127 	case IP6OPT_TYPE_DISCARD:	/* silently discard */
1128 		m_freem(m);
1129 		return (-1);
1130 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1131 		IP6STAT_INC(ip6s_badoptions);
1132 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1133 		return (-1);
1134 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1135 		IP6STAT_INC(ip6s_badoptions);
1136 		ip6 = mtod(m, struct ip6_hdr *);
1137 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1138 		    (m->m_flags & (M_BCAST|M_MCAST)))
1139 			m_freem(m);
1140 		else
1141 			icmp6_error(m, ICMP6_PARAM_PROB,
1142 				    ICMP6_PARAMPROB_OPTION, off);
1143 		return (-1);
1144 	}
1145 
1146 	m_freem(m);		/* XXX: NOTREACHED */
1147 	return (-1);
1148 }
1149 
1150 /*
1151  * Create the "control" list for this pcb.
1152  * These functions will not modify mbuf chain at all.
1153  *
1154  * With KAME mbuf chain restriction:
1155  * The routine will be called from upper layer handlers like tcp6_input().
1156  * Thus the routine assumes that the caller (tcp6_input) have already
1157  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1158  * very first mbuf on the mbuf chain.
1159  *
1160  * ip6_savecontrol_v4 will handle those options that are possible to be
1161  * set on a v4-mapped socket.
1162  * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1163  * options and handle the v6-only ones itself.
1164  */
1165 struct mbuf **
1166 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
1167     int *v4only)
1168 {
1169 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1170 
1171 #ifdef SO_TIMESTAMP
1172 	if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1173 		struct timeval tv;
1174 
1175 		microtime(&tv);
1176 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1177 		    SCM_TIMESTAMP, SOL_SOCKET);
1178 		if (*mp)
1179 			mp = &(*mp)->m_next;
1180 	}
1181 #endif
1182 
1183 #define IS2292(inp, x, y)	(((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1184 	/* RFC 2292 sec. 5 */
1185 	if ((inp->inp_flags & IN6P_PKTINFO) != 0) {
1186 		struct in6_pktinfo pi6;
1187 
1188 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1189 #ifdef INET
1190 			struct ip *ip;
1191 
1192 			ip = mtod(m, struct ip *);
1193 			pi6.ipi6_addr.s6_addr32[0] = 0;
1194 			pi6.ipi6_addr.s6_addr32[1] = 0;
1195 			pi6.ipi6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
1196 			pi6.ipi6_addr.s6_addr32[3] = ip->ip_dst.s_addr;
1197 #else
1198 			/* We won't hit this code */
1199 			bzero(&pi6.ipi6_addr, sizeof(struct in6_addr));
1200 #endif
1201 		} else {
1202 			bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1203 			in6_clearscope(&pi6.ipi6_addr);	/* XXX */
1204 		}
1205 		pi6.ipi6_ifindex =
1206 		    (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1207 
1208 		*mp = sbcreatecontrol((caddr_t) &pi6,
1209 		    sizeof(struct in6_pktinfo),
1210 		    IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1211 		if (*mp)
1212 			mp = &(*mp)->m_next;
1213 	}
1214 
1215 	if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
1216 		int hlim;
1217 
1218 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1219 #ifdef INET
1220 			struct ip *ip;
1221 
1222 			ip = mtod(m, struct ip *);
1223 			hlim = ip->ip_ttl;
1224 #else
1225 			/* We won't hit this code */
1226 			hlim = 0;
1227 #endif
1228 		} else {
1229 			hlim = ip6->ip6_hlim & 0xff;
1230 		}
1231 		*mp = sbcreatecontrol((caddr_t) &hlim, sizeof(int),
1232 		    IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1233 		    IPPROTO_IPV6);
1234 		if (*mp)
1235 			mp = &(*mp)->m_next;
1236 	}
1237 
1238 	if ((inp->inp_flags & IN6P_TCLASS) != 0) {
1239 		int tclass;
1240 
1241 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1242 #ifdef INET
1243 			struct ip *ip;
1244 
1245 			ip = mtod(m, struct ip *);
1246 			tclass = ip->ip_tos;
1247 #else
1248 			/* We won't hit this code */
1249 			tclass = 0;
1250 #endif
1251 		} else {
1252 			u_int32_t flowinfo;
1253 
1254 			flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1255 			flowinfo >>= 20;
1256 			tclass = flowinfo & 0xff;
1257 		}
1258 		*mp = sbcreatecontrol((caddr_t) &tclass, sizeof(int),
1259 		    IPV6_TCLASS, IPPROTO_IPV6);
1260 		if (*mp)
1261 			mp = &(*mp)->m_next;
1262 	}
1263 
1264 	if (v4only != NULL) {
1265 		if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1266 			*v4only = 1;
1267 		} else {
1268 			*v4only = 0;
1269 		}
1270 	}
1271 
1272 	return (mp);
1273 }
1274 
1275 void
1276 ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
1277 {
1278 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1279 	int v4only = 0;
1280 
1281 	mp = ip6_savecontrol_v4(in6p, m, mp, &v4only);
1282 	if (v4only)
1283 		return;
1284 
1285 	/*
1286 	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
1287 	 * privilege for the option (see ip6_ctloutput), but it might be too
1288 	 * strict, since there might be some hop-by-hop options which can be
1289 	 * returned to normal user.
1290 	 * See also RFC 2292 section 6 (or RFC 3542 section 8).
1291 	 */
1292 	if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) {
1293 		/*
1294 		 * Check if a hop-by-hop options header is contatined in the
1295 		 * received packet, and if so, store the options as ancillary
1296 		 * data. Note that a hop-by-hop options header must be
1297 		 * just after the IPv6 header, which is assured through the
1298 		 * IPv6 input processing.
1299 		 */
1300 		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1301 			struct ip6_hbh *hbh;
1302 			int hbhlen = 0;
1303 #ifdef PULLDOWN_TEST
1304 			struct mbuf *ext;
1305 #endif
1306 
1307 #ifndef PULLDOWN_TEST
1308 			hbh = (struct ip6_hbh *)(ip6 + 1);
1309 			hbhlen = (hbh->ip6h_len + 1) << 3;
1310 #else
1311 			ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1312 			    ip6->ip6_nxt);
1313 			if (ext == NULL) {
1314 				IP6STAT_INC(ip6s_tooshort);
1315 				return;
1316 			}
1317 			hbh = mtod(ext, struct ip6_hbh *);
1318 			hbhlen = (hbh->ip6h_len + 1) << 3;
1319 			if (hbhlen != ext->m_len) {
1320 				m_freem(ext);
1321 				IP6STAT_INC(ip6s_tooshort);
1322 				return;
1323 			}
1324 #endif
1325 
1326 			/*
1327 			 * XXX: We copy the whole header even if a
1328 			 * jumbo payload option is included, the option which
1329 			 * is to be removed before returning according to
1330 			 * RFC2292.
1331 			 * Note: this constraint is removed in RFC3542
1332 			 */
1333 			*mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1334 			    IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1335 			    IPPROTO_IPV6);
1336 			if (*mp)
1337 				mp = &(*mp)->m_next;
1338 #ifdef PULLDOWN_TEST
1339 			m_freem(ext);
1340 #endif
1341 		}
1342 	}
1343 
1344 	if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
1345 		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1346 
1347 		/*
1348 		 * Search for destination options headers or routing
1349 		 * header(s) through the header chain, and stores each
1350 		 * header as ancillary data.
1351 		 * Note that the order of the headers remains in
1352 		 * the chain of ancillary data.
1353 		 */
1354 		while (1) {	/* is explicit loop prevention necessary? */
1355 			struct ip6_ext *ip6e = NULL;
1356 			int elen;
1357 #ifdef PULLDOWN_TEST
1358 			struct mbuf *ext = NULL;
1359 #endif
1360 
1361 			/*
1362 			 * if it is not an extension header, don't try to
1363 			 * pull it from the chain.
1364 			 */
1365 			switch (nxt) {
1366 			case IPPROTO_DSTOPTS:
1367 			case IPPROTO_ROUTING:
1368 			case IPPROTO_HOPOPTS:
1369 			case IPPROTO_AH: /* is it possible? */
1370 				break;
1371 			default:
1372 				goto loopend;
1373 			}
1374 
1375 #ifndef PULLDOWN_TEST
1376 			if (off + sizeof(*ip6e) > m->m_len)
1377 				goto loopend;
1378 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1379 			if (nxt == IPPROTO_AH)
1380 				elen = (ip6e->ip6e_len + 2) << 2;
1381 			else
1382 				elen = (ip6e->ip6e_len + 1) << 3;
1383 			if (off + elen > m->m_len)
1384 				goto loopend;
1385 #else
1386 			ext = ip6_pullexthdr(m, off, nxt);
1387 			if (ext == NULL) {
1388 				IP6STAT_INC(ip6s_tooshort);
1389 				return;
1390 			}
1391 			ip6e = mtod(ext, struct ip6_ext *);
1392 			if (nxt == IPPROTO_AH)
1393 				elen = (ip6e->ip6e_len + 2) << 2;
1394 			else
1395 				elen = (ip6e->ip6e_len + 1) << 3;
1396 			if (elen != ext->m_len) {
1397 				m_freem(ext);
1398 				IP6STAT_INC(ip6s_tooshort);
1399 				return;
1400 			}
1401 #endif
1402 
1403 			switch (nxt) {
1404 			case IPPROTO_DSTOPTS:
1405 				if (!(in6p->inp_flags & IN6P_DSTOPTS))
1406 					break;
1407 
1408 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1409 				    IS2292(in6p,
1410 					IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1411 				    IPPROTO_IPV6);
1412 				if (*mp)
1413 					mp = &(*mp)->m_next;
1414 				break;
1415 			case IPPROTO_ROUTING:
1416 				if (!(in6p->inp_flags & IN6P_RTHDR))
1417 					break;
1418 
1419 				*mp = sbcreatecontrol((caddr_t)ip6e, elen,
1420 				    IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR),
1421 				    IPPROTO_IPV6);
1422 				if (*mp)
1423 					mp = &(*mp)->m_next;
1424 				break;
1425 			case IPPROTO_HOPOPTS:
1426 			case IPPROTO_AH: /* is it possible? */
1427 				break;
1428 
1429 			default:
1430 				/*
1431 				 * other cases have been filtered in the above.
1432 				 * none will visit this case.  here we supply
1433 				 * the code just in case (nxt overwritten or
1434 				 * other cases).
1435 				 */
1436 #ifdef PULLDOWN_TEST
1437 				m_freem(ext);
1438 #endif
1439 				goto loopend;
1440 
1441 			}
1442 
1443 			/* proceed with the next header. */
1444 			off += elen;
1445 			nxt = ip6e->ip6e_nxt;
1446 			ip6e = NULL;
1447 #ifdef PULLDOWN_TEST
1448 			m_freem(ext);
1449 			ext = NULL;
1450 #endif
1451 		}
1452 	  loopend:
1453 		;
1454 	}
1455 
1456 	if (in6p->inp_flags2 & INP_RECVFLOWID) {
1457 		uint32_t flowid, flow_type;
1458 
1459 		flowid = m->m_pkthdr.flowid;
1460 		flow_type = M_HASHTYPE_GET(m);
1461 
1462 		/*
1463 		 * XXX should handle the failure of one or the
1464 		 * other - don't populate both?
1465 		 */
1466 		*mp = sbcreatecontrol((caddr_t) &flowid,
1467 		    sizeof(uint32_t), IPV6_FLOWID, IPPROTO_IPV6);
1468 		if (*mp)
1469 			mp = &(*mp)->m_next;
1470 		*mp = sbcreatecontrol((caddr_t) &flow_type,
1471 		    sizeof(uint32_t), IPV6_FLOWTYPE, IPPROTO_IPV6);
1472 		if (*mp)
1473 			mp = &(*mp)->m_next;
1474 	}
1475 
1476 #ifdef	RSS
1477 	if (in6p->inp_flags2 & INP_RECVRSSBUCKETID) {
1478 		uint32_t flowid, flow_type;
1479 		uint32_t rss_bucketid;
1480 
1481 		flowid = m->m_pkthdr.flowid;
1482 		flow_type = M_HASHTYPE_GET(m);
1483 
1484 		if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1485 			*mp = sbcreatecontrol((caddr_t) &rss_bucketid,
1486 			   sizeof(uint32_t), IPV6_RSSBUCKETID, IPPROTO_IPV6);
1487 			if (*mp)
1488 				mp = &(*mp)->m_next;
1489 		}
1490 	}
1491 #endif
1492 
1493 }
1494 #undef IS2292
1495 
1496 void
1497 ip6_notify_pmtu(struct inpcb *inp, struct sockaddr_in6 *dst, u_int32_t mtu)
1498 {
1499 	struct socket *so;
1500 	struct mbuf *m_mtu;
1501 	struct ip6_mtuinfo mtuctl;
1502 
1503 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1504 	/*
1505 	 * Notify the error by sending IPV6_PATHMTU ancillary data if
1506 	 * application wanted to know the MTU value.
1507 	 * NOTE: we notify disconnected sockets, because some udp
1508 	 * applications keep sending sockets disconnected.
1509 	 * NOTE: our implementation doesn't notify connected sockets that has
1510 	 * foreign address that is different than given destination addresses
1511 	 * (this is permitted by RFC 3542).
1512 	 */
1513 	if ((inp->inp_flags & IN6P_MTU) == 0 || (
1514 	    !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1515 	    !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &dst->sin6_addr)))
1516 		return;
1517 
1518 	mtuctl.ip6m_mtu = mtu;
1519 	mtuctl.ip6m_addr = *dst;
1520 	if (sa6_recoverscope(&mtuctl.ip6m_addr))
1521 		return;
1522 
1523 	if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
1524 	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1525 		return;
1526 
1527 	so =  inp->inp_socket;
1528 	if (sbappendaddr(&so->so_rcv, (struct sockaddr *)dst, NULL, m_mtu)
1529 	    == 0) {
1530 		m_freem(m_mtu);
1531 		/* XXX: should count statistics */
1532 	} else
1533 		sorwakeup(so);
1534 }
1535 
1536 #ifdef PULLDOWN_TEST
1537 /*
1538  * pull single extension header from mbuf chain.  returns single mbuf that
1539  * contains the result, or NULL on error.
1540  */
1541 static struct mbuf *
1542 ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1543 {
1544 	struct ip6_ext ip6e;
1545 	size_t elen;
1546 	struct mbuf *n;
1547 
1548 #ifdef DIAGNOSTIC
1549 	switch (nxt) {
1550 	case IPPROTO_DSTOPTS:
1551 	case IPPROTO_ROUTING:
1552 	case IPPROTO_HOPOPTS:
1553 	case IPPROTO_AH: /* is it possible? */
1554 		break;
1555 	default:
1556 		printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1557 	}
1558 #endif
1559 
1560 	m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1561 	if (nxt == IPPROTO_AH)
1562 		elen = (ip6e.ip6e_len + 2) << 2;
1563 	else
1564 		elen = (ip6e.ip6e_len + 1) << 3;
1565 
1566 	if (elen > MLEN)
1567 		n = m_getcl(M_NOWAIT, MT_DATA, 0);
1568 	else
1569 		n = m_get(M_NOWAIT, MT_DATA);
1570 	if (n == NULL)
1571 		return NULL;
1572 
1573 	m_copydata(m, off, elen, mtod(n, caddr_t));
1574 	n->m_len = elen;
1575 	return n;
1576 }
1577 #endif
1578 
1579 /*
1580  * Get pointer to the previous header followed by the header
1581  * currently processed.
1582  * XXX: This function supposes that
1583  *	M includes all headers,
1584  *	the next header field and the header length field of each header
1585  *	are valid, and
1586  *	the sum of each header length equals to OFF.
1587  * Because of these assumptions, this function must be called very
1588  * carefully. Moreover, it will not be used in the near future when
1589  * we develop `neater' mechanism to process extension headers.
1590  */
1591 char *
1592 ip6_get_prevhdr(const struct mbuf *m, int off)
1593 {
1594 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1595 
1596 	if (off == sizeof(struct ip6_hdr))
1597 		return (&ip6->ip6_nxt);
1598 	else {
1599 		int len, nxt;
1600 		struct ip6_ext *ip6e = NULL;
1601 
1602 		nxt = ip6->ip6_nxt;
1603 		len = sizeof(struct ip6_hdr);
1604 		while (len < off) {
1605 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1606 
1607 			switch (nxt) {
1608 			case IPPROTO_FRAGMENT:
1609 				len += sizeof(struct ip6_frag);
1610 				break;
1611 			case IPPROTO_AH:
1612 				len += (ip6e->ip6e_len + 2) << 2;
1613 				break;
1614 			default:
1615 				len += (ip6e->ip6e_len + 1) << 3;
1616 				break;
1617 			}
1618 			nxt = ip6e->ip6e_nxt;
1619 		}
1620 		if (ip6e)
1621 			return (&ip6e->ip6e_nxt);
1622 		else
1623 			return NULL;
1624 	}
1625 }
1626 
1627 /*
1628  * get next header offset.  m will be retained.
1629  */
1630 int
1631 ip6_nexthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1632 {
1633 	struct ip6_hdr ip6;
1634 	struct ip6_ext ip6e;
1635 	struct ip6_frag fh;
1636 
1637 	/* just in case */
1638 	if (m == NULL)
1639 		panic("ip6_nexthdr: m == NULL");
1640 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1641 		return -1;
1642 
1643 	switch (proto) {
1644 	case IPPROTO_IPV6:
1645 		if (m->m_pkthdr.len < off + sizeof(ip6))
1646 			return -1;
1647 		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1648 		if (nxtp)
1649 			*nxtp = ip6.ip6_nxt;
1650 		off += sizeof(ip6);
1651 		return off;
1652 
1653 	case IPPROTO_FRAGMENT:
1654 		/*
1655 		 * terminate parsing if it is not the first fragment,
1656 		 * it does not make sense to parse through it.
1657 		 */
1658 		if (m->m_pkthdr.len < off + sizeof(fh))
1659 			return -1;
1660 		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1661 		/* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
1662 		if (fh.ip6f_offlg & IP6F_OFF_MASK)
1663 			return -1;
1664 		if (nxtp)
1665 			*nxtp = fh.ip6f_nxt;
1666 		off += sizeof(struct ip6_frag);
1667 		return off;
1668 
1669 	case IPPROTO_AH:
1670 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1671 			return -1;
1672 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1673 		if (nxtp)
1674 			*nxtp = ip6e.ip6e_nxt;
1675 		off += (ip6e.ip6e_len + 2) << 2;
1676 		return off;
1677 
1678 	case IPPROTO_HOPOPTS:
1679 	case IPPROTO_ROUTING:
1680 	case IPPROTO_DSTOPTS:
1681 		if (m->m_pkthdr.len < off + sizeof(ip6e))
1682 			return -1;
1683 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1684 		if (nxtp)
1685 			*nxtp = ip6e.ip6e_nxt;
1686 		off += (ip6e.ip6e_len + 1) << 3;
1687 		return off;
1688 
1689 	case IPPROTO_NONE:
1690 	case IPPROTO_ESP:
1691 	case IPPROTO_IPCOMP:
1692 		/* give up */
1693 		return -1;
1694 
1695 	default:
1696 		return -1;
1697 	}
1698 
1699 	/* NOTREACHED */
1700 }
1701 
1702 /*
1703  * get offset for the last header in the chain.  m will be kept untainted.
1704  */
1705 int
1706 ip6_lasthdr(const struct mbuf *m, int off, int proto, int *nxtp)
1707 {
1708 	int newoff;
1709 	int nxt;
1710 
1711 	if (!nxtp) {
1712 		nxt = -1;
1713 		nxtp = &nxt;
1714 	}
1715 	while (1) {
1716 		newoff = ip6_nexthdr(m, off, proto, nxtp);
1717 		if (newoff < 0)
1718 			return off;
1719 		else if (newoff < off)
1720 			return -1;	/* invalid */
1721 		else if (newoff == off)
1722 			return newoff;
1723 
1724 		off = newoff;
1725 		proto = *nxtp;
1726 	}
1727 }
1728 
1729 /*
1730  * System control for IP6
1731  */
1732 
1733 u_char	inet6ctlerrmap[PRC_NCMDS] = {
1734 	0,		0,		0,		0,
1735 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1736 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1737 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1738 	0,		0,		0,		0,
1739 	ENOPROTOOPT
1740 };
1741