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