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