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