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