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