xref: /xnu-12377.41.6/bsd/netinet6/ip6_input.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1 /*
2  * Copyright (c) 2003-2025 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /*
30  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 3. Neither the name of the project nor the names of its contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57 
58 /*
59  * Copyright (c) 1982, 1986, 1988, 1993
60  *	The Regents of the University of California.  All rights reserved.
61  *
62  * Redistribution and use in source and binary forms, with or without
63  * modification, are permitted provided that the following conditions
64  * are met:
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  * 2. Redistributions in binary form must reproduce the above copyright
68  *    notice, this list of conditions and the following disclaimer in the
69  *    documentation and/or other materials provided with the distribution.
70  * 3. All advertising materials mentioning features or use of this software
71  *    must display the following acknowledgement:
72  *	This product includes software developed by the University of
73  *	California, Berkeley and its contributors.
74  * 4. Neither the name of the University nor the names of its contributors
75  *    may be used to endorse or promote products derived from this software
76  *    without specific prior written permission.
77  *
78  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88  * SUCH DAMAGE.
89  *
90  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
91  */
92 
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/malloc.h>
96 #include <sys/mbuf.h>
97 #include <sys/domain.h>
98 #include <sys/protosw.h>
99 #include <sys/socket.h>
100 #include <sys/socketvar.h>
101 #include <sys/errno.h>
102 #include <sys/time.h>
103 #include <sys/kernel.h>
104 #include <sys/syslog.h>
105 #include <sys/sysctl.h>
106 #include <sys/proc.h>
107 #include <sys/kauth.h>
108 #include <sys/mcache.h>
109 
110 #include <mach/mach_time.h>
111 #include <mach/sdt.h>
112 #include <pexpert/pexpert.h>
113 #include <dev/random/randomdev.h>
114 
115 #include <net/if.h>
116 #include <net/if_var.h>
117 #include <net/if_types.h>
118 #include <net/if_dl.h>
119 #include <net/route.h>
120 #include <net/kpi_protocol.h>
121 #include <net/ntstat.h>
122 #include <net/init.h>
123 #include <net/net_osdep.h>
124 #include <net/net_perf.h>
125 #include <net/if_ports_used.h>
126 #include <net/droptap.h>
127 
128 #include <netinet/in.h>
129 #include <netinet/in_systm.h>
130 #if INET
131 #include <netinet/ip.h>
132 #include <netinet/ip_icmp.h>
133 #endif /* INET */
134 #include <netinet/kpi_ipfilter_var.h>
135 #include <netinet/ip6.h>
136 #include <netinet/udp.h>
137 #include <netinet6/in6_var.h>
138 #include <netinet6/ip6_var.h>
139 #include <netinet/in_pcb.h>
140 #include <netinet/icmp6.h>
141 #include <netinet6/in6_ifattach.h>
142 #include <netinet6/nd6.h>
143 #include <netinet6/scope6_var.h>
144 #include <netinet6/ip6protosw.h>
145 
146 #if IPSEC
147 #include <netinet6/ipsec.h>
148 #include <netinet6/ipsec6.h>
149 extern int ipsec_bypass;
150 #endif /* IPSEC */
151 
152 #if DUMMYNET
153 #include <netinet/ip_dummynet.h>
154 #endif /* DUMMYNET */
155 
156 /* we need it for NLOOP. */
157 #include "loop.h"
158 
159 #if PF
160 #include <net/pfvar.h>
161 #endif /* PF */
162 
163 #include <os/log.h>
164 
165 struct ip6protosw *ip6_protox[IPPROTO_MAX];
166 
167 static LCK_GRP_DECLARE(in6_ifaddr_rwlock_grp, "in6_ifaddr_rwlock");
168 LCK_RW_DECLARE(in6_ifaddr_rwlock, &in6_ifaddr_rwlock_grp);
169 
170 /* Protected by in6_ifaddr_rwlock */
171 struct in6_ifaddrhead in6_ifaddrhead;
172 uint32_t in6_ifaddrlist_genid = 0;
173 
174 #define IN6ADDR_NHASH    61
175 u_int32_t in6addr_hashp = 0;                  /* next largest prime */
176 u_int32_t in6addr_nhash = 0;                  /* hash table size */
177 struct in6_ifaddrhashhead *__counted_by(in6addr_nhash) in6_ifaddrhashtbl = 0;
178 
179 #define IN6_IFSTAT_REQUIRE_ALIGNED_64(f)        \
180 	static_assert(!(offsetof(struct in6_ifstat, f) % sizeof (uint64_t)))
181 
182 #define ICMP6_IFSTAT_REQUIRE_ALIGNED_64(f)      \
183 	static_assert(!(offsetof(struct icmp6_ifstat, f) % sizeof (uint64_t)))
184 
185 struct ip6stat ip6stat;
186 
187 LCK_ATTR_DECLARE(ip6_mutex_attr, 0, 0);
188 LCK_GRP_DECLARE(ip6_mutex_grp, "ip6");
189 
190 LCK_MTX_DECLARE_ATTR(proxy6_lock, &ip6_mutex_grp, &ip6_mutex_attr);
191 LCK_MTX_DECLARE_ATTR(nd6_mutex_data, &ip6_mutex_grp, &ip6_mutex_attr);
192 
193 extern int loopattach_done;
194 extern void addrsel_policy_init(void);
195 
196 static int sysctl_reset_ip6_input_stats SYSCTL_HANDLER_ARGS;
197 static int sysctl_ip6_input_measure_bins SYSCTL_HANDLER_ARGS;
198 static int sysctl_ip6_input_getperf SYSCTL_HANDLER_ARGS;
199 static void ip6_init_delayed(void);
200 static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
201 
202 static void in6_ifaddrhashtbl_init(void);
203 
204 static struct m_tag *m_tag_kalloc_inet6(u_int32_t id, u_int16_t type, uint16_t len, int wait);
205 static void m_tag_kfree_inet6(struct m_tag *tag);
206 
207 #if NSTF
208 extern void stfattach(void);
209 #endif /* NSTF */
210 
211 SYSCTL_DECL(_net_inet6_ip6);
212 
213 static uint32_t ip6_adj_clear_hwcksum = 0;
214 SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, adj_clear_hwcksum,
215     CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_adj_clear_hwcksum, 0,
216     "Invalidate hwcksum info when adjusting length");
217 
218 static uint32_t ip6_adj_partial_sum = 1;
219 SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, adj_partial_sum,
220     CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_adj_partial_sum, 0,
221     "Perform partial sum adjustment of trailing bytes at IP layer");
222 
223 static int ip6_input_measure = 0;
224 SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, input_perf,
225     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
226     &ip6_input_measure, 0, sysctl_reset_ip6_input_stats, "I", "Do time measurement");
227 
228 static uint64_t ip6_input_measure_bins = 0;
229 SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, input_perf_bins,
230     CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_input_measure_bins, 0,
231     sysctl_ip6_input_measure_bins, "I",
232     "bins for chaining performance data histogram");
233 
234 static net_perf_t net_perf;
235 SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, input_perf_data,
236     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
237     0, 0, sysctl_ip6_input_getperf, "S,net_perf",
238     "IP6 input performance data (struct net_perf, net/net_perf.h)");
239 
240 /*
241  * ip6_checkinterface controls the receive side of the models for multihoming
242  * that are discussed in RFC 1122.
243  *
244  * sysctl_ip6_checkinterface values are:
245  *  IP6_CHECKINTERFACE_WEAK_ES:
246  *	This corresponds to the Weak End-System model where incoming packets from
247  *	any interface are accepted provided the destination address of the incoming packet
248  *	is assigned to some interface.
249  *
250  *  IP6_CHECKINTERFACE_HYBRID_ES:
251  *	The Hybrid End-System model use the Strong End-System for tunnel interfaces
252  *	(ipsec and utun) and the weak End-System model for other interfaces families.
253  *	This prevents a rogue middle box to probe for signs of TCP connections
254  *	that use the tunnel interface.
255  *
256  *  IP6_CHECKINTERFACE_STRONG_ES:
257  *	The Strong model model requires the packet arrived on an interface that
258  *	is assigned the destination address of the packet.
259  *
260  * Since the routing table and transmit implementation do not implement the Strong ES model,
261  * setting this to a value different from IP6_CHECKINTERFACE_WEAK_ES may lead to unexpected results.
262  *
263  * When forwarding is enabled, the system reverts to the Weak ES model as a router
264  * is expected by design to receive packets from several interfaces to the same address.
265  */
266 #define IP6_CHECKINTERFACE_WEAK_ES       0
267 #define IP6_CHECKINTERFACE_HYBRID_ES     1
268 #define IP6_CHECKINTERFACE_STRONG_ES     2
269 
270 static int ip6_checkinterface = IP6_CHECKINTERFACE_HYBRID_ES;
271 
272 static int sysctl_ip6_checkinterface SYSCTL_HANDLER_ARGS;
273 SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, check_interface,
274     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
275     0, 0, sysctl_ip6_checkinterface, "I", "Verify packet arrives on correct interface");
276 
277 #if (DEBUG || DEVELOPMENT)
278 #define IP6_CHECK_IFDEBUG 1
279 #else
280 #define IP6_CHECK_IFDEBUG 0
281 #endif /* (DEBUG || DEVELOPMENT) */
282 static int ip6_checkinterface_debug = IP6_CHECK_IFDEBUG;
283 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, checkinterface_debug, CTLFLAG_RW | CTLFLAG_LOCKED,
284     &ip6_checkinterface_debug, IP6_CHECK_IFDEBUG, "");
285 
286 typedef enum ip6_check_if_result {
287 	IP6_CHECK_IF_NONE = 0,
288 	IP6_CHECK_IF_OURS = 1,
289 	IP6_CHECK_IF_DROP = 2,
290 	IP6_CHECK_IF_FORWARD = 3
291 } ip6_check_if_result_t;
292 
293 static ip6_check_if_result_t ip6_input_check_interface(struct mbuf *, struct ip6_hdr *, struct ifnet *, struct route_in6 *rin6, struct ifnet **);
294 
295 /*
296  * On platforms which require strict alignment (currently for anything but
297  * i386 or x86_64 or arm64), check if the IP header pointer is 32-bit aligned; if not,
298  * copy the contents of the mbuf chain into a new chain, and free the original
299  * one.  Create some head room in the first mbuf of the new chain, in case
300  * it's needed later on.
301  *
302  * RFC 2460 says that IPv6 headers are 64-bit aligned, but network interfaces
303  * mostly align to 32-bit boundaries.  Care should be taken never to use 64-bit
304  * load/store operations on the fields in IPv6 headers.
305  */
306 #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
307 #define IP6_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do { } while (0)
308 #else /* !__i386__ && !__x86_64__ && !__arm64__ */
309 #define IP6_HDR_ALIGNMENT_FIXUP(_m, _ifp, _action) do {             \
310 	if (!IP6_HDR_ALIGNED_P(mtod(_m, caddr_t))) {                    \
311 	        mbuf_ref_t _n;                                          \
312 	        ifnet_ref_t __ifp = (_ifp);                             \
313 	        os_atomic_inc(&(__ifp)->if_alignerrs, relaxed);         \
314 	        if (((_m)->m_flags & M_PKTHDR) &&                       \
315 	            (_m)->m_pkthdr.pkt_hdr != NULL)                     \
316 	                (_m)->m_pkthdr.pkt_hdr = NULL;                  \
317 	        _n = m_defrag_offset(_m, max_linkhdr, M_NOWAIT);        \
318 	        if (_n == NULL) {                                       \
319 	                ip6stat.ip6s_toosmall++;                        \
320 	                m_drop(_m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_TOO_SMALL, NULL, 0); \
321 	                (_m) = NULL;                                    \
322 	                _action;                                        \
323 	        } else {                                                \
324 	                VERIFY(_n != (_m));                             \
325 	                (_m) = _n;                                      \
326 	        }                                                       \
327 	}                                                               \
328 } while (0)
329 #endif /* !__i386__ && !__x86_64___ && !__arm64__ */
330 
331 static void
ip6_proto_input(protocol_family_t protocol,mbuf_t packet)332 ip6_proto_input(protocol_family_t protocol, mbuf_t packet)
333 {
334 #pragma unused(protocol)
335 #if INET
336 	struct timeval start_tv;
337 	if (ip6_input_measure) {
338 		net_perf_start_time(&net_perf, &start_tv);
339 	}
340 #endif /* INET */
341 	ip6_input(packet);
342 #if INET
343 	if (ip6_input_measure) {
344 		net_perf_measure_time(&net_perf, &start_tv, 1);
345 		net_perf_histogram(&net_perf, 1);
346 	}
347 #endif /* INET */
348 }
349 
350 /*
351  * IP6 initialization: fill in IP6 protocol switch table.
352  * All protocols not implemented in kernel go to raw IP6 protocol handler.
353  */
354 void
ip6_init(struct protosw * pp,struct domain * dp)355 ip6_init(struct protosw *pp, struct domain *dp)
356 {
357 	static int ip6_initialized = 0;
358 	struct protosw *__single pr;
359 	struct timeval tv;
360 	int i;
361 	domain_unguard_t __single unguard;
362 
363 	domain_proto_mtx_lock_assert_held();
364 	VERIFY((pp->pr_flags & (PR_INITIALIZED | PR_ATTACHED)) == PR_ATTACHED);
365 
366 	static_assert((sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) <= _MHLEN);
367 
368 	static_assert(IP_RECV_LINK_ADDR_TYPE == IPV6_RECV_LINK_ADDR_TYPE);
369 
370 	if (!os_atomic_cmpxchg(&ip6_initialized, 0, 1, relaxed)) {
371 		return;
372 	}
373 
374 	eventhandler_lists_ctxt_init(&in6_evhdlr_ctxt);
375 	(void)EVENTHANDLER_REGISTER(&in6_evhdlr_ctxt, in6_event,
376 	    &in6_eventhdlr_callback, eventhandler_entry_dummy_arg,
377 	    EVENTHANDLER_PRI_ANY);
378 
379 	eventhandler_lists_ctxt_init(&in6_clat46_evhdlr_ctxt);
380 	(void)EVENTHANDLER_REGISTER(&in6_clat46_evhdlr_ctxt, in6_clat46_event,
381 	    &in6_clat46_eventhdlr_callback, eventhandler_entry_dummy_arg,
382 	    EVENTHANDLER_PRI_ANY);
383 
384 	for (i = 0; i < IN6_EVENT_MAX; i++) {
385 		VERIFY(in6_event2kev_array[i].in6_event_code == i);
386 	}
387 
388 	pr = pffindproto_locked(PF_INET6, IPPROTO_RAW, SOCK_RAW);
389 	if (pr == NULL) {
390 		panic("%s: Unable to find [PF_INET6,IPPROTO_RAW,SOCK_RAW]",
391 		    __func__);
392 		/* NOTREACHED */
393 	}
394 
395 	/* Initialize the entire ip6_protox[] array to IPPROTO_RAW. */
396 	for (i = 0; i < IPPROTO_MAX; i++) {
397 		ip6_protox[i] = (struct ip6protosw *)pr;
398 	}
399 	/*
400 	 * Cycle through IP protocols and put them into the appropriate place
401 	 * in ip6_protox[], skipping protocols IPPROTO_{IP,RAW}.
402 	 */
403 	VERIFY(dp == inet6domain && dp->dom_family == PF_INET6);
404 	TAILQ_FOREACH(pr, &dp->dom_protosw, pr_entry) {
405 		VERIFY(pr->pr_domain == dp);
406 		if (pr->pr_protocol != 0 && pr->pr_protocol != IPPROTO_RAW) {
407 			/* Be careful to only index valid IP protocols. */
408 			if (pr->pr_protocol < IPPROTO_MAX) {
409 				ip6_protox[pr->pr_protocol] =
410 				    (struct ip6protosw *)pr;
411 			}
412 		}
413 	}
414 
415 	TAILQ_INIT(&in6_ifaddrhead);
416 	in6_ifaddrhashtbl_init();
417 
418 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_receive);
419 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_hdrerr);
420 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_toobig);
421 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_noroute);
422 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_addrerr);
423 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_protounknown);
424 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_truncated);
425 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_discard);
426 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_deliver);
427 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_forward);
428 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_request);
429 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_discard);
430 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragok);
431 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragfail);
432 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_fragcreat);
433 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_reqd);
434 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_ok);
435 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_reass_fail);
436 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mcast);
437 	IN6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mcast);
438 
439 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_msg);
440 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_error);
441 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_dstunreach);
442 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_adminprohib);
443 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_timeexceed);
444 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_paramprob);
445 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_pkttoobig);
446 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echo);
447 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_echoreply);
448 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routersolicit);
449 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_routeradvert);
450 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighborsolicit);
451 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_neighboradvert);
452 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_redirect);
453 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldquery);
454 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mldreport);
455 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_in_mlddone);
456 
457 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_msg);
458 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_error);
459 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_dstunreach);
460 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_adminprohib);
461 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_timeexceed);
462 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_paramprob);
463 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_pkttoobig);
464 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echo);
465 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_echoreply);
466 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routersolicit);
467 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_routeradvert);
468 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighborsolicit);
469 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_neighboradvert);
470 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_redirect);
471 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldquery);
472 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mldreport);
473 	ICMP6_IFSTAT_REQUIRE_ALIGNED_64(ifs6_out_mlddone);
474 
475 	getmicrotime(&tv);
476 	ip6_desync_factor =
477 	    (RandomULong() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR;
478 
479 	PE_parse_boot_argn("in6_embedded_scope", &in6_embedded_scope, sizeof(in6_embedded_scope));
480 	PE_parse_boot_argn("ip6_checkinterface", &i, sizeof(i));
481 	switch (i) {
482 	case IP6_CHECKINTERFACE_WEAK_ES:
483 	case IP6_CHECKINTERFACE_HYBRID_ES:
484 	case IP6_CHECKINTERFACE_STRONG_ES:
485 		ip6_checkinterface = i;
486 		break;
487 	default:
488 		break;
489 	}
490 
491 	in6_ifaddr_init();
492 	ip6_moptions_init();
493 	nd6_init();
494 	frag6_init();
495 	icmp6_init(NULL, dp);
496 	addrsel_policy_init();
497 
498 	/*
499 	 * P2P interfaces often route the local address to the loopback
500 	 * interface. At this point, lo0 hasn't been initialized yet, which
501 	 * means that we need to delay the IPv6 configuration of lo0.
502 	 */
503 	net_init_add(ip6_init_delayed);
504 
505 	unguard = domain_unguard_deploy();
506 	i = proto_register_input(PF_INET6, ip6_proto_input, NULL, 0);
507 	if (i != 0) {
508 		panic("%s: failed to register PF_INET6 protocol: %d",
509 		    __func__, i);
510 		/* NOTREACHED */
511 	}
512 	domain_unguard_release(unguard);
513 }
514 
515 static void
ip6_init_delayed(void)516 ip6_init_delayed(void)
517 {
518 	(void) in6_ifattach_prelim(lo_ifp);
519 
520 	/* timer for regeneranation of temporary addresses randomize ID */
521 	timeout(in6_tmpaddrtimer, NULL,
522 	    (ip6_temp_preferred_lifetime - ip6_desync_factor -
523 	    ip6_temp_regen_advance) * hz);
524 
525 #if NSTF
526 	stfattach();
527 #endif /* NSTF */
528 }
529 
530 static void
ip6_input_adjust(struct mbuf * m,struct ip6_hdr * ip6,uint32_t plen,struct ifnet * inifp)531 ip6_input_adjust(struct mbuf *m, struct ip6_hdr *ip6, uint32_t plen,
532     struct ifnet *inifp)
533 {
534 	boolean_t adjust = TRUE;
535 	uint32_t tot_len = sizeof(*ip6) + plen;
536 
537 	ASSERT(m_pktlen(m) > tot_len);
538 
539 	/*
540 	 * Invalidate hardware checksum info if ip6_adj_clear_hwcksum
541 	 * is set; useful to handle buggy drivers.  Note that this
542 	 * should not be enabled by default, as we may get here due
543 	 * to link-layer padding.
544 	 */
545 	if (ip6_adj_clear_hwcksum &&
546 	    (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) &&
547 	    !(inifp->if_flags & IFF_LOOPBACK) &&
548 	    !(m->m_pkthdr.pkt_flags & PKTF_LOOP)) {
549 		m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
550 		m->m_pkthdr.csum_data = 0;
551 		ip6stat.ip6s_adj_hwcsum_clr++;
552 	}
553 
554 	/*
555 	 * If partial checksum information is available, subtract
556 	 * out the partial sum of postpended extraneous bytes, and
557 	 * update the checksum metadata accordingly.  By doing it
558 	 * here, the upper layer transport only needs to adjust any
559 	 * prepended extraneous bytes (else it will do both.)
560 	 */
561 	if (ip6_adj_partial_sum &&
562 	    (m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PARTIAL)) ==
563 	    (CSUM_DATA_VALID | CSUM_PARTIAL)) {
564 		m->m_pkthdr.csum_rx_val = m_adj_sum16(m,
565 		    m->m_pkthdr.csum_rx_start, m->m_pkthdr.csum_rx_start,
566 		    (tot_len - m->m_pkthdr.csum_rx_start),
567 		    m->m_pkthdr.csum_rx_val);
568 	} else if ((m->m_pkthdr.csum_flags &
569 	    (CSUM_DATA_VALID | CSUM_PARTIAL)) ==
570 	    (CSUM_DATA_VALID | CSUM_PARTIAL)) {
571 		/*
572 		 * If packet has partial checksum info and we decided not
573 		 * to subtract the partial sum of postpended extraneous
574 		 * bytes here (not the default case), leave that work to
575 		 * be handled by the other layers.  For now, only TCP, UDP
576 		 * layers are capable of dealing with this.  For all other
577 		 * protocols (including fragments), trim and ditch the
578 		 * partial sum as those layers might not implement partial
579 		 * checksumming (or adjustment) at all.
580 		 */
581 		if (ip6->ip6_nxt == IPPROTO_TCP ||
582 		    ip6->ip6_nxt == IPPROTO_UDP) {
583 			adjust = FALSE;
584 		} else {
585 			m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
586 			m->m_pkthdr.csum_data = 0;
587 			ip6stat.ip6s_adj_hwcsum_clr++;
588 		}
589 	}
590 
591 	if (adjust) {
592 		ip6stat.ip6s_adj++;
593 		if (m->m_len == m->m_pkthdr.len) {
594 			m->m_len = tot_len;
595 			m->m_pkthdr.len = tot_len;
596 		} else {
597 			m_adj(m, tot_len - m->m_pkthdr.len);
598 		}
599 	}
600 }
601 
602 static ip6_check_if_result_t
ip6_input_check_interface(struct mbuf * m,struct ip6_hdr * ip6,struct ifnet * inifp,struct route_in6 * rin6,struct ifnet ** deliverifp)603 ip6_input_check_interface(struct mbuf *m, struct ip6_hdr *ip6, struct ifnet *inifp, struct route_in6 *rin6, struct ifnet **deliverifp)
604 {
605 	struct in6_ifaddr *__single ia6 = NULL;
606 	struct in6_addr tmp_dst = ip6->ip6_dst; /* copy to avoid unaligned access */
607 	struct in6_ifaddr *__single best_ia6 = NULL;
608 	uint32_t dst_ifscope = IFSCOPE_NONE;
609 	ip6_check_if_result_t result = IP6_CHECK_IF_NONE;
610 
611 	*deliverifp = NULL;
612 
613 	if (m->m_pkthdr.pkt_flags & PKTF_IFAINFO) {
614 		dst_ifscope = m->m_pkthdr.dst_ifindex;
615 	} else {
616 		dst_ifscope = inifp->if_index;
617 	}
618 	/*
619 	 * Check for exact addresses in the hash bucket.
620 	 */
621 	lck_rw_lock_shared(&in6_ifaddr_rwlock);
622 	TAILQ_FOREACH(ia6, IN6ADDR_HASH(&tmp_dst), ia6_hash) {
623 		/*
624 		 * TODO: should we accept loopback
625 		 */
626 		if (in6_are_addr_equal_scoped(&ia6->ia_addr.sin6_addr, &tmp_dst, ia6->ia_ifp->if_index, dst_ifscope)) {
627 			if ((ia6->ia6_flags & IN6_IFF_NOTREADY) != 0) {
628 				continue;
629 			}
630 			best_ia6 = ia6;
631 			if (ia6->ia_ifp == inifp) {
632 				/*
633 				 * TODO: should we also accept locally originated packets
634 				 * or from loopback ???
635 				 */
636 				break;
637 			}
638 			/*
639 			 * Continue the loop in case there's a exact match with another
640 			 * interface
641 			 */
642 		}
643 	}
644 	if (best_ia6 != NULL) {
645 		if (best_ia6->ia_ifp != inifp && ip6_forwarding == 0 &&
646 		    ((ip6_checkinterface == IP6_CHECKINTERFACE_HYBRID_ES &&
647 		    (best_ia6->ia_ifp->if_family == IFNET_FAMILY_IPSEC ||
648 		    best_ia6->ia_ifp->if_family == IFNET_FAMILY_UTUN)) ||
649 		    ip6_checkinterface == IP6_CHECKINTERFACE_STRONG_ES)) {
650 			/*
651 			 * Drop when interface address check is strict and forwarding
652 			 * is disabled
653 			 */
654 			result = IP6_CHECK_IF_DROP;
655 		} else {
656 			result = IP6_CHECK_IF_OURS;
657 			*deliverifp = best_ia6->ia_ifp;
658 			ip6_setdstifaddr_info(m, 0, best_ia6);
659 			ip6_setsrcifaddr_info(m, best_ia6->ia_ifp->if_index, NULL);
660 		}
661 	}
662 	lck_rw_done(&in6_ifaddr_rwlock);
663 
664 	if (result == IP6_CHECK_IF_NONE) {
665 		/*
666 		 * Slow path: route lookup.
667 		 */
668 		struct sockaddr_in6 *__single dst6;
669 
670 		dst6 = SIN6(&rin6->ro_dst);
671 		dst6->sin6_len = sizeof(struct sockaddr_in6);
672 		dst6->sin6_family = AF_INET6;
673 		dst6->sin6_addr = ip6->ip6_dst;
674 		if (!in6_embedded_scope && IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
675 			dst6->sin6_scope_id = dst_ifscope;
676 		}
677 		rtalloc_scoped_ign((struct route *)rin6,
678 		    RTF_PRCLONING, IFSCOPE_NONE);
679 		if (rin6->ro_rt != NULL) {
680 			RT_LOCK_SPIN(rin6->ro_rt);
681 		}
682 
683 #define rt6_key(r) (SIN6(rn_get_key((r)->rt_nodes)))
684 
685 		/*
686 		 * Accept the packet if the forwarding interface to the destination
687 		 * according to the routing table is the loopback interface,
688 		 * unless the associated route has a gateway.
689 		 * Note that this approach causes to accept a packet if there is a
690 		 * route to the loopback interface for the destination of the packet.
691 		 * But we think it's even useful in some situations, e.g. when using
692 		 * a special daemon which wants to intercept the packet.
693 		 *
694 		 * XXX: some OSes automatically make a cloned route for the destination
695 		 * of an outgoing packet.  If the outgoing interface of the packet
696 		 * is a loopback one, the kernel would consider the packet to be
697 		 * accepted, even if we have no such address assinged on the interface.
698 		 * We check the cloned flag of the route entry to reject such cases,
699 		 * assuming that route entries for our own addresses are not made by
700 		 * cloning (it should be true because in6_addloop explicitly installs
701 		 * the host route).  However, we might have to do an explicit check
702 		 * while it would be less efficient.  Or, should we rather install a
703 		 * reject route for such a case?
704 		 */
705 		if (rin6->ro_rt != NULL &&
706 		    (rin6->ro_rt->rt_flags & (RTF_HOST | RTF_GATEWAY)) == RTF_HOST &&
707 #if RTF_WASCLONED
708 		    !(rin6->ro_rt->rt_flags & RTF_WASCLONED) &&
709 #endif
710 		    rin6->ro_rt->rt_ifp->if_type == IFT_LOOP) {
711 			ia6 = ifatoia6(rin6->ro_rt->rt_ifa);
712 			/*
713 			 * Packets to a tentative, duplicated, or somehow invalid
714 			 * address must not be accepted.
715 			 *
716 			 * For performance, test without acquiring the address lock;
717 			 * a lot of things in the address are set once and never
718 			 * changed (e.g. ia_ifp.)
719 			 */
720 			if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
721 				/* this address is ready */
722 				result = IP6_CHECK_IF_OURS;
723 				*deliverifp = ia6->ia_ifp;       /* correct? */
724 				/*
725 				 * record dst address information into mbuf.
726 				 */
727 				(void) ip6_setdstifaddr_info(m, 0, ia6);
728 				(void) ip6_setsrcifaddr_info(m, ia6->ia_ifp->if_index, NULL);
729 			}
730 		}
731 
732 		if (rin6->ro_rt != NULL) {
733 			RT_UNLOCK(rin6->ro_rt);
734 		}
735 	}
736 
737 	if (result == IP6_CHECK_IF_NONE) {
738 		if (ip6_forwarding == 0) {
739 			result = IP6_CHECK_IF_DROP;
740 		} else {
741 			result = IP6_CHECK_IF_FORWARD;
742 			ip6_setdstifaddr_info(m, inifp->if_index, NULL);
743 			ip6_setsrcifaddr_info(m, inifp->if_index, NULL);
744 		}
745 	}
746 
747 	if (result == IP6_CHECK_IF_OURS && *deliverifp != inifp) {
748 		ASSERT(*deliverifp != NULL);
749 		ip6stat.ip6s_rcv_if_weak_match++;
750 
751 		/*  Logging is too noisy when forwarding is enabled */
752 		if (ip6_checkinterface_debug != IP6_CHECKINTERFACE_WEAK_ES && ip6_forwarding != 0) {
753 			char src_str[MAX_IPv6_STR_LEN];
754 			char dst_str[MAX_IPv6_STR_LEN];
755 
756 			inet_ntop(AF_INET6, &ip6->ip6_src, src_str, sizeof(src_str));
757 			inet_ntop(AF_INET6, &ip6->ip6_dst, dst_str, sizeof(dst_str));
758 			os_log_info(OS_LOG_DEFAULT,
759 			    "%s: weak ES interface match to %s for packet from %s to %s proto %u received via %s",
760 			    __func__, (*deliverifp)->if_xname, src_str, dst_str, ip6->ip6_nxt, inifp->if_xname);
761 		}
762 	} else if (result == IP6_CHECK_IF_DROP) {
763 		ip6stat.ip6s_rcv_if_no_match++;
764 		if (ip6_checkinterface_debug > 0) {
765 			char src_str[MAX_IPv6_STR_LEN];
766 			char dst_str[MAX_IPv6_STR_LEN];
767 
768 			inet_ntop(AF_INET6, &ip6->ip6_src, src_str, sizeof(src_str));
769 			inet_ntop(AF_INET6, &ip6->ip6_dst, dst_str, sizeof(dst_str));
770 			os_log(OS_LOG_DEFAULT,
771 			    "%s: no interface match for packet from %s to %s proto %u received via %s",
772 			    __func__, src_str, dst_str, ip6->ip6_nxt, inifp->if_xname);
773 		}
774 	}
775 
776 	return result;
777 }
778 
779 static void
ip6_input_process_wake_packet(struct mbuf * m)780 ip6_input_process_wake_packet(struct mbuf *m)
781 {
782 	struct ifnet *ifp = m->m_pkthdr.rcvif;
783 
784 	if (if_is_lpw_enabled(ifp)) {
785 		if_exit_lpw(ifp, "IP6 packet");
786 	}
787 }
788 
789 void
ip6_input(struct mbuf * m)790 ip6_input(struct mbuf *m)
791 {
792 	struct ip6_hdr *ip6;
793 	int off = sizeof(struct ip6_hdr), nest;
794 	u_int32_t plen;
795 	u_int32_t rtalert = ~0;
796 	int nxt = 0, ours = 0;
797 	ifnet_ref_t inifp, deliverifp = NULL;
798 	ipfilter_t __single inject_ipfref = NULL;
799 	int seen = 1;
800 #if DUMMYNET
801 	struct m_tag *__single tag;
802 	struct ip_fw_args args = {};
803 #endif /* DUMMYNET */
804 	struct route_in6 rin6 = {};
805 
806 	/*
807 	 * Check if the packet we received is valid after interface filter
808 	 * processing
809 	 */
810 	MBUF_INPUT_CHECK(m, m->m_pkthdr.rcvif);
811 	inifp = m->m_pkthdr.rcvif;
812 	VERIFY(inifp != NULL);
813 
814 	/* Perform IP header alignment fixup, if needed */
815 	IP6_HDR_ALIGNMENT_FIXUP(m, inifp, return );
816 
817 	m->m_pkthdr.pkt_flags &= ~PKTF_FORWARDED;
818 #if IPSEC
819 	/*
820 	 * should the inner packet be considered authentic?
821 	 * see comment in ah4_input().
822 	 */
823 	m->m_flags &= ~M_AUTHIPHDR;
824 	m->m_flags &= ~M_AUTHIPDGM;
825 #endif /* IPSEC */
826 
827 	/*
828 	 * make sure we don't have onion peering information into m_aux.
829 	 */
830 	ip6_delaux(m);
831 
832 #if DUMMYNET
833 	if ((tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
834 	    KERNEL_TAG_TYPE_DUMMYNET)) != NULL) {
835 		struct dn_pkt_tag *__single dn_tag;
836 
837 		dn_tag = (struct dn_pkt_tag *)(tag->m_tag_data);
838 
839 		args.fwa_pf_rule = dn_tag->dn_pf_rule;
840 
841 		m_tag_delete(m, tag);
842 	}
843 
844 	if (args.fwa_pf_rule) {
845 		ip6 = mtod(m, struct ip6_hdr *); /* In case PF got disabled */
846 
847 		goto check_with_pf;
848 	}
849 #endif /* DUMMYNET */
850 
851 	/*
852 	 * No need to process packet twice if we've already seen it.
853 	 */
854 	inject_ipfref = ipf_get_inject_filter(m);
855 	if (inject_ipfref != NULL) {
856 		ip6 = mtod(m, struct ip6_hdr *);
857 		nxt = ip6->ip6_nxt;
858 		seen = 0;
859 		goto injectit;
860 	} else {
861 		seen = 1;
862 	}
863 
864 	if (__improbable(m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT)) {
865 		if_ports_used_match_mbuf(inifp, PF_INET6, m);
866 	}
867 
868 	/*
869 	 * mbuf statistics
870 	 */
871 	if (m->m_flags & M_EXT) {
872 		if (m->m_next != NULL) {
873 			ip6stat.ip6s_mext2m++;
874 		} else {
875 			ip6stat.ip6s_mext1++;
876 		}
877 	} else {
878 #define M2MMAX  (sizeof (ip6stat.ip6s_m2m) / sizeof (ip6stat.ip6s_m2m[0]))
879 		if (m->m_next != NULL) {
880 			if (m->m_pkthdr.pkt_flags & PKTF_LOOP) {
881 				/* XXX */
882 				ip6stat.ip6s_m2m[ifnet_index(lo_ifp)]++;
883 			} else if (inifp->if_index < M2MMAX) {
884 				ip6stat.ip6s_m2m[inifp->if_index]++;
885 			} else {
886 				ip6stat.ip6s_m2m[0]++;
887 			}
888 		} else {
889 			ip6stat.ip6s_m1++;
890 		}
891 #undef M2MMAX
892 	}
893 
894 	/*
895 	 * Drop the packet if IPv6 operation is disabled on the interface.
896 	 */
897 	if (inifp->if_eflags & IFEF_IPV6_DISABLED) {
898 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_IF_IPV6_DISABLED, NULL, 0);
899 		goto bad;
900 	}
901 
902 	in6_ifstat_inc_na(inifp, ifs6_in_receive);
903 	ip6stat.ip6s_total++;
904 
905 	/*
906 	 * L2 bridge code and some other code can return mbuf chain
907 	 * that does not conform to KAME requirement.  too bad.
908 	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
909 	 */
910 	if (m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
911 		mbuf_ref_t n;
912 
913 		MGETHDR(n, M_DONTWAIT, MT_HEADER);      /* MAC-OK */
914 		if (n) {
915 			M_COPY_PKTHDR(n, m);
916 		}
917 		if (n && m->m_pkthdr.len > MHLEN) {
918 			MCLGET(n, M_DONTWAIT);
919 			if ((n->m_flags & M_EXT) == 0) {
920 				m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_TOO_SMALL, NULL, 0);
921 				n = NULL;
922 			}
923 		}
924 		if (n == NULL) {
925 			goto bad;
926 		}
927 
928 		m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
929 		n->m_len = m->m_pkthdr.len;
930 		m_freem(m);
931 		m = n;
932 	}
933 	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), { goto done; });
934 
935 	if (m->m_len < sizeof(struct ip6_hdr)) {
936 		if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) {
937 			ip6stat.ip6s_toosmall++;
938 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
939 			goto done;
940 		}
941 	}
942 
943 	ip6 = mtod(m, struct ip6_hdr *);
944 
945 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
946 		ip6stat.ip6s_badvers++;
947 		in6_ifstat_inc(inifp, ifs6_in_hdrerr);
948 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_BAD_VERSION, NULL, 0);
949 		goto bad;
950 	}
951 
952 	ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
953 
954 	/*
955 	 * Check against address spoofing/corruption.
956 	 */
957 	if (!(m->m_pkthdr.pkt_flags & PKTF_LOOP) &&
958 	    IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src)) {
959 		ip6stat.ip6s_badscope++;
960 		in6_ifstat_inc(inifp, ifs6_in_addrerr);
961 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_SCOPE, NULL, 0);
962 		goto bad;
963 	}
964 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
965 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
966 		/*
967 		 * XXX: "badscope" is not very suitable for a multicast source.
968 		 */
969 		ip6stat.ip6s_badscope++;
970 		in6_ifstat_inc(inifp, ifs6_in_addrerr);
971 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_SCOPE, NULL, 0);
972 		goto bad;
973 	}
974 	if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) &&
975 	    !(m->m_pkthdr.pkt_flags & PKTF_LOOP)) {
976 		/*
977 		 * In this case, the packet should come from the loopback
978 		 * interface.  However, we cannot just check the if_flags,
979 		 * because ip6_mloopback() passes the "actual" interface
980 		 * as the outgoing/incoming interface.
981 		 */
982 		ip6stat.ip6s_badscope++;
983 		in6_ifstat_inc(inifp, ifs6_in_addrerr);
984 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_SCOPE, NULL, 0);
985 		goto bad;
986 	}
987 
988 	/*
989 	 * The following check is not documented in specs.  A malicious
990 	 * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
991 	 * and bypass security checks (act as if it was from 127.0.0.1 by using
992 	 * IPv6 src ::ffff:127.0.0.1).  Be cautious.
993 	 *
994 	 * This check chokes if we are in an SIIT cloud.  As none of BSDs
995 	 * support IPv4-less kernel compilation, we cannot support SIIT
996 	 * environment at all.  So, it makes more sense for us to reject any
997 	 * malicious packets for non-SIIT environment, than try to do a
998 	 * partial support for SIIT environment.
999 	 */
1000 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
1001 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
1002 		ip6stat.ip6s_badscope++;
1003 		in6_ifstat_inc(inifp, ifs6_in_addrerr);
1004 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_SCOPE, NULL, 0);
1005 		goto bad;
1006 	}
1007 
1008 	if (((ntohl(ip6->ip6_flow & IPV6_FLOW_ECN_MASK) >> 20) & IPTOS_ECN_ECT1) == IPTOS_ECN_ECT1) {
1009 		m->m_pkthdr.pkt_ext_flags |= PKTF_EXT_L4S;
1010 	}
1011 
1012 #if 0
1013 	/*
1014 	 * Reject packets with IPv4 compatible addresses (auto tunnel).
1015 	 *
1016 	 * The code forbids auto tunnel relay case in RFC1933 (the check is
1017 	 * stronger than RFC1933).  We may want to re-enable it if mech-xx
1018 	 * is revised to forbid relaying case.
1019 	 */
1020 	if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
1021 	    IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
1022 		ip6stat.ip6s_badscope++;
1023 		in6_ifstat_inc(inifp, ifs6_in_addrerr);
1024 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_SCOPE, NULL, 0);
1025 		goto bad;
1026 	}
1027 #endif
1028 
1029 	/*
1030 	 * Naively assume we can attribute inbound data to the route we would
1031 	 * use to send to this destination. Asymetric routing breaks this
1032 	 * assumption, but it still allows us to account for traffic from
1033 	 * a remote node in the routing table.
1034 	 * this has a very significant performance impact so we bypass
1035 	 * if nstat_collect is disabled. We may also bypass if the
1036 	 * protocol is tcp in the future because tcp will have a route that
1037 	 * we can use to attribute the data to. That does mean we would not
1038 	 * account for forwarded tcp traffic.
1039 	 */
1040 	if (nstat_collect) {
1041 		rtentry_ref_t rte =
1042 		    ifnet_cached_rtlookup_inet6(inifp, &ip6->ip6_src);
1043 		if (rte != NULL) {
1044 			nstat_route_rx(rte, 1, m->m_pkthdr.len, 0);
1045 			rtfree(rte);
1046 		}
1047 	}
1048 
1049 #if DUMMYNET
1050 check_with_pf:
1051 #endif /* DUMMYNET */
1052 #if PF
1053 	/* Invoke inbound packet filter */
1054 	if (PF_IS_ENABLED) {
1055 		int error;
1056 #if DUMMYNET
1057 		error = pf_af_hook(inifp, NULL, &m, AF_INET6, TRUE, &args);
1058 #else /* !DUMMYNET */
1059 		error = pf_af_hook(inifp, NULL, &m, AF_INET6, TRUE, NULL);
1060 #endif /* !DUMMYNET */
1061 		if (error != 0 || m == NULL) {
1062 			if (m != NULL) {
1063 				panic("%s: unexpected packet %p",
1064 				    __func__, m);
1065 				/* NOTREACHED */
1066 			}
1067 			/* Already freed by callee */
1068 			goto done;
1069 		}
1070 		ip6 = mtod(m, struct ip6_hdr *);
1071 	}
1072 #endif /* PF */
1073 
1074 	/* drop packets if interface ID portion is already filled */
1075 	if (!(inifp->if_flags & IFF_LOOPBACK) &&
1076 	    !(m->m_pkthdr.pkt_flags & PKTF_LOOP)) {
1077 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
1078 		    ip6->ip6_src.s6_addr16[1]) {
1079 			ip6stat.ip6s_badscope++;
1080 			m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_SCOPE, NULL, 0);
1081 			goto bad;
1082 		}
1083 		if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst) &&
1084 		    ip6->ip6_dst.s6_addr16[1]) {
1085 			ip6stat.ip6s_badscope++;
1086 			m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_SCOPE, NULL, 0);
1087 			goto bad;
1088 		}
1089 	}
1090 
1091 	if ((m->m_pkthdr.pkt_flags & PKTF_IFAINFO) != 0 && in6_embedded_scope) {
1092 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
1093 			ip6->ip6_src.s6_addr16[1] =
1094 			    htons(m->m_pkthdr.src_ifindex);
1095 		}
1096 		if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
1097 			ip6->ip6_dst.s6_addr16[1] =
1098 			    htons(m->m_pkthdr.dst_ifindex);
1099 		}
1100 	} else if (in6_embedded_scope) {
1101 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
1102 			ip6->ip6_src.s6_addr16[1] = htons(inifp->if_index);
1103 		}
1104 		if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
1105 			ip6->ip6_dst.s6_addr16[1] = htons(inifp->if_index);
1106 		}
1107 	}
1108 
1109 	/*
1110 	 * Multicast check
1111 	 */
1112 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1113 		struct in6_multi *__single in6m = NULL;
1114 
1115 		in6_ifstat_inc_na(inifp, ifs6_in_mcast);
1116 		/*
1117 		 * See if we belong to the destination multicast group on the
1118 		 * arrival interface.
1119 		 */
1120 		in6_multihead_lock_shared();
1121 		IN6_LOOKUP_MULTI(&ip6->ip6_dst, inifp, in6m);
1122 		in6_multihead_lock_done();
1123 		if (in6m != NULL) {
1124 			IN6M_REMREF(in6m);
1125 			ours = 1;
1126 		} else if (!nd6_prproxy) {
1127 			ip6stat.ip6s_notmember++;
1128 			ip6stat.ip6s_cantforward++;
1129 			in6_ifstat_inc(inifp, ifs6_in_discard);
1130 			m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_UNKNOWN_MULTICAST_GROUP, NULL, 0);
1131 			goto bad;
1132 		}
1133 		deliverifp = inifp;
1134 		/*
1135 		 * record dst address information into mbuf, if we don't have one yet.
1136 		 * note that we are unable to record it, if the address is not listed
1137 		 * as our interface address (e.g. multicast addresses, etc.)
1138 		 */
1139 		if (deliverifp != NULL) {
1140 			struct in6_ifaddr *__single ia6 = NULL;
1141 
1142 			ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
1143 			if (ia6 != NULL) {
1144 				(void) ip6_setdstifaddr_info(m, 0, ia6);
1145 				(void) ip6_setsrcifaddr_info(m, ia6->ia_ifp->if_index, NULL);
1146 				ifa_remref(&ia6->ia_ifa);
1147 			} else {
1148 				(void) ip6_setdstifaddr_info(m, inifp->if_index, NULL);
1149 				(void) ip6_setsrcifaddr_info(m, inifp->if_index, NULL);
1150 			}
1151 		}
1152 		goto hbhcheck;
1153 	} else {
1154 		/*
1155 		 * Unicast check
1156 		 */
1157 		ip6_check_if_result_t check_if_result = IP6_CHECK_IF_NONE;
1158 		check_if_result = ip6_input_check_interface(m, ip6, inifp, &rin6, &deliverifp);
1159 		ASSERT(check_if_result != IP6_CHECK_IF_NONE);
1160 		if (check_if_result == IP6_CHECK_IF_OURS) {
1161 			ours = 1;
1162 			goto hbhcheck;
1163 		} else if (check_if_result == IP6_CHECK_IF_DROP) {
1164 			m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_RCV_IF_NO_MATCH, NULL, 0);
1165 			goto bad;
1166 		}
1167 	}
1168 
1169 	/*
1170 	 * Now there is no reason to process the packet if it's not our own
1171 	 * and we're not a router.
1172 	 */
1173 	if (!ip6_forwarding) {
1174 		ip6stat.ip6s_cantforward++;
1175 		in6_ifstat_inc(inifp, ifs6_in_discard);
1176 		/*
1177 		 * Raise a kernel event if the packet received on cellular
1178 		 * interface is not intended for local host.
1179 		 * For now limit it to ICMPv6 packets.
1180 		 */
1181 		if (inifp->if_type == IFT_CELLULAR &&
1182 		    ip6->ip6_nxt == IPPROTO_ICMPV6) {
1183 			in6_ifstat_inc(inifp, ifs6_cantfoward_icmp6);
1184 		}
1185 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_CANNOT_FORWARD, NULL, 0);
1186 		goto bad;
1187 	}
1188 
1189 hbhcheck:
1190 	/*
1191 	 * Process Hop-by-Hop options header if it's contained.
1192 	 * m may be modified in ip6_hopopts_input().
1193 	 * If a JumboPayload option is included, plen will also be modified.
1194 	 */
1195 	plen = (u_int32_t)ntohs(ip6->ip6_plen);
1196 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1197 		struct ip6_hbh *__single hbh;
1198 
1199 		/*
1200 		 * Mark the packet to imply that HBH option has been checked.
1201 		 * This can only be true is the packet came in unfragmented
1202 		 * or if the option is in the first fragment
1203 		 */
1204 		m->m_pkthdr.pkt_flags |= PKTF_HBH_CHKED;
1205 		if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
1206 #if 0   /* touches NULL pointer */
1207 			in6_ifstat_inc(inifp, ifs6_in_discard);
1208 #endif
1209 			goto done;      /* m have already been freed */
1210 		}
1211 
1212 		/* adjust pointer */
1213 		ip6 = mtod(m, struct ip6_hdr *);
1214 
1215 		/*
1216 		 * if the payload length field is 0 and the next header field
1217 		 * indicates Hop-by-Hop Options header, then a Jumbo Payload
1218 		 * option MUST be included.
1219 		 */
1220 		if (ip6->ip6_plen == 0 && plen == 0) {
1221 			/*
1222 			 * Note that if a valid jumbo payload option is
1223 			 * contained, ip6_hopopts_input() must set a valid
1224 			 * (non-zero) payload length to the variable plen.
1225 			 */
1226 			ip6stat.ip6s_badoptions++;
1227 			in6_ifstat_inc(inifp, ifs6_in_discard);
1228 			in6_ifstat_inc(inifp, ifs6_in_hdrerr);
1229 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
1230 			    (int)((caddr_t)&ip6->ip6_plen - (caddr_t)ip6));
1231 			goto done;
1232 		}
1233 		/* ip6_hopopts_input() ensures that mbuf is contiguous */
1234 		hbh = (struct ip6_hbh *)(ip6 + 1);
1235 		nxt = hbh->ip6h_nxt;
1236 
1237 		/*
1238 		 * If we are acting as a router and the packet contains a
1239 		 * router alert option, see if we know the option value.
1240 		 * Currently, we only support the option value for MLD, in which
1241 		 * case we should pass the packet to the multicast routing
1242 		 * daemon.
1243 		 */
1244 		if (rtalert != ~0 && ip6_forwarding) {
1245 			switch (rtalert) {
1246 			case IP6OPT_RTALERT_MLD:
1247 				ours = 1;
1248 				break;
1249 			default:
1250 				/*
1251 				 * RFC2711 requires unrecognized values must be
1252 				 * silently ignored.
1253 				 */
1254 				break;
1255 			}
1256 		}
1257 	} else {
1258 		nxt = ip6->ip6_nxt;
1259 	}
1260 
1261 	/*
1262 	 * Check that the amount of data in the buffers
1263 	 * is as at least much as the IPv6 header would have us expect.
1264 	 * Trim mbufs if longer than we expect.
1265 	 * Drop packet if shorter than we expect.
1266 	 */
1267 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
1268 		ip6stat.ip6s_tooshort++;
1269 		in6_ifstat_inc(inifp, ifs6_in_truncated);
1270 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_TOO_SHORT, NULL, 0);
1271 		goto bad;
1272 	}
1273 
1274 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
1275 		ip6_input_adjust(m, ip6, plen, inifp);
1276 	}
1277 
1278 	/*
1279 	 * Forward if desirable.
1280 	 */
1281 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1282 		if (!ours && nd6_prproxy) {
1283 			/*
1284 			 * If this isn't for us, this might be a Neighbor
1285 			 * Solicitation (dst is solicited-node multicast)
1286 			 * against an address in one of the proxied prefixes;
1287 			 * if so, claim the packet and let icmp6_input()
1288 			 * handle the rest.
1289 			 */
1290 			ours = nd6_prproxy_isours(m, ip6, NULL, IFSCOPE_NONE);
1291 			VERIFY(!ours ||
1292 			    (m->m_pkthdr.pkt_flags & PKTF_PROXY_DST));
1293 		}
1294 		if (!ours) {
1295 			m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_UNPROXIED_NS, NULL, 0);
1296 			goto bad;
1297 		}
1298 	} else if (!ours) {
1299 		/*
1300 		 * The unicast forwarding function might return the packet
1301 		 * if we are proxying prefix(es), and if the packet is an
1302 		 * ICMPv6 packet that has failed the zone checks, but is
1303 		 * targetted towards a proxied address (this is optimized by
1304 		 * way of RTF_PROXY test.)  If so, claim the packet as ours
1305 		 * and let icmp6_input() handle the rest.  The packet's hop
1306 		 * limit value is kept intact (it's not decremented).  This
1307 		 * is for supporting Neighbor Unreachability Detection between
1308 		 * proxied nodes on different links (src is link-local, dst
1309 		 * is target address.)
1310 		 */
1311 		if ((m = ip6_forward(m, &rin6, 0)) == NULL) {
1312 			goto done;
1313 		}
1314 		VERIFY(rin6.ro_rt != NULL);
1315 		VERIFY(m->m_pkthdr.pkt_flags & PKTF_PROXY_DST);
1316 		deliverifp = rin6.ro_rt->rt_ifp;
1317 		ours = 1;
1318 	}
1319 
1320 	ip6 = mtod(m, struct ip6_hdr *);
1321 
1322 	/*
1323 	 * Malicious party may be able to use IPv4 mapped addr to confuse
1324 	 * tcp/udp stack and bypass security checks (act as if it was from
1325 	 * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1).  Be cautious.
1326 	 *
1327 	 * For SIIT end node behavior, you may want to disable the check.
1328 	 * However, you will  become vulnerable to attacks using IPv4 mapped
1329 	 * source.
1330 	 */
1331 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
1332 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
1333 		ip6stat.ip6s_badscope++;
1334 		in6_ifstat_inc(inifp, ifs6_in_addrerr);
1335 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_SCOPE, NULL, 0);
1336 		goto bad;
1337 	}
1338 
1339 	/*
1340 	 * Tell launch routine the next header
1341 	 */
1342 	ip6stat.ip6s_delivered++;
1343 	in6_ifstat_inc_na(deliverifp, ifs6_in_deliver);
1344 
1345 injectit:
1346 	nest = 0;
1347 
1348 	/*
1349 	 * Perform IP header alignment fixup again, if needed.  Note that
1350 	 * we do it once for the outermost protocol, and we assume each
1351 	 * protocol handler wouldn't mess with the alignment afterwards.
1352 	 */
1353 	IP6_HDR_ALIGNMENT_FIXUP(m, inifp, return );
1354 
1355 	while (nxt != IPPROTO_DONE) {
1356 		struct ipfilter *__single filter;
1357 		int (*pr_input)(struct mbuf **, int *, int);
1358 
1359 		/*
1360 		 * This would imply either IPPROTO_HOPOPTS was not the first
1361 		 * option or it did not come in the first fragment.
1362 		 */
1363 		if (nxt == IPPROTO_HOPOPTS &&
1364 		    (m->m_pkthdr.pkt_flags & PKTF_HBH_CHKED) == 0) {
1365 			/*
1366 			 * This implies that HBH option was not contained
1367 			 * in the first fragment
1368 			 */
1369 			ip6stat.ip6s_badoptions++;
1370 			m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_OPTION, NULL, 0);
1371 			goto bad;
1372 		}
1373 
1374 		if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
1375 			ip6stat.ip6s_toomanyhdr++;
1376 			m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_TOO_MANY_OPTIONS, NULL, 0);
1377 			goto bad;
1378 		}
1379 
1380 		/*
1381 		 * protection against faulty packet - there should be
1382 		 * more sanity checks in header chain processing.
1383 		 */
1384 		if (m->m_pkthdr.len < off) {
1385 			ip6stat.ip6s_tooshort++;
1386 			in6_ifstat_inc(inifp, ifs6_in_truncated);
1387 			m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_TOO_SHORT, NULL, 0);
1388 			goto bad;
1389 		}
1390 
1391 #if IPSEC
1392 		/*
1393 		 * enforce IPsec policy checking if we are seeing last header.
1394 		 * note that we do not visit this with protocols with pcb layer
1395 		 * code - like udp/tcp/raw ip.
1396 		 */
1397 		if ((ipsec_bypass == 0) &&
1398 		    (ip6_protox[nxt]->pr_flags & PR_LASTHDR) != 0) {
1399 			if (ipsec6_in_reject(m, NULL)) {
1400 				IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
1401 				m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IPSEC_REJECT, NULL, 0);
1402 				goto bad;
1403 			}
1404 		}
1405 #endif /* IPSEC */
1406 
1407 		/*
1408 		 * Call IP filter
1409 		 */
1410 		if (!TAILQ_EMPTY(&ipv6_filters) && !IFNET_IS_INTCOPROC(inifp)) {
1411 			ipf_ref();
1412 			TAILQ_FOREACH(filter, &ipv6_filters, ipf_link) {
1413 				if (seen == 0) {
1414 					if ((struct ipfilter *)inject_ipfref ==
1415 					    filter) {
1416 						seen = 1;
1417 					}
1418 				} else if (filter->ipf_filter.ipf_input) {
1419 					errno_t result;
1420 
1421 					result = filter->ipf_filter.ipf_input(
1422 						filter->ipf_filter.cookie,
1423 						(mbuf_t *)&m, off, (uint8_t)nxt);
1424 					if (result == EJUSTRETURN) {
1425 						ipf_unref();
1426 						goto done;
1427 					}
1428 					if (result != 0) {
1429 						ipf_unref();
1430 						m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_FILTER_DROP, NULL, 0);
1431 						goto bad;
1432 					}
1433 				}
1434 			}
1435 			ipf_unref();
1436 		}
1437 
1438 		DTRACE_IP6(receive, struct mbuf *, m, struct inpcb *, NULL,
1439 		    struct ip6_hdr *, ip6, struct ifnet *, inifp,
1440 		    struct ip *, NULL, struct ip6_hdr *, ip6);
1441 
1442 		/*
1443 		 * Check if need to switch to full wake mode -- TCP knows about idle connections
1444 		 */
1445 		if (__improbable(nxt != IPPROTO_TCP && (m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT) != 0)) {
1446 			ip6_input_process_wake_packet(m);
1447 		}
1448 
1449 		if ((pr_input = ip6_protox[nxt]->pr_input) == NULL) {
1450 			m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_NO_PROTO, NULL, 0);
1451 			m = NULL;
1452 			nxt = IPPROTO_DONE;
1453 		} else if (!(ip6_protox[nxt]->pr_flags & PR_PROTOLOCK)) {
1454 			lck_mtx_lock(inet6_domain_mutex);
1455 			nxt = pr_input(&m, &off, nxt);
1456 			lck_mtx_unlock(inet6_domain_mutex);
1457 		} else {
1458 			nxt = pr_input(&m, &off, nxt);
1459 		}
1460 	}
1461 done:
1462 	ROUTE_RELEASE(&rin6);
1463 	return;
1464 bad:
1465 	goto done;
1466 }
1467 
1468 void
ip6_setsrcifaddr_info(struct mbuf * m,uint32_t src_idx,struct in6_ifaddr * ia6)1469 ip6_setsrcifaddr_info(struct mbuf *m, uint32_t src_idx, struct in6_ifaddr *ia6)
1470 {
1471 	VERIFY(m->m_flags & M_PKTHDR);
1472 	m->m_pkthdr.pkt_ext_flags &= ~PKTF_EXT_OUTPUT_SCOPE;
1473 	/*
1474 	 * If the source ifaddr is specified, pick up the information
1475 	 * from there; otherwise just grab the passed-in ifindex as the
1476 	 * caller may not have the ifaddr available.
1477 	 */
1478 	if (ia6 != NULL) {
1479 		m->m_pkthdr.pkt_flags |= PKTF_IFAINFO;
1480 		m->m_pkthdr.src_ifindex = ia6->ia_ifp->if_index;
1481 
1482 		/* See IN6_IFF comments in in6_var.h */
1483 		m->m_pkthdr.src_iff = (ia6->ia6_flags & 0xffff);
1484 	} else {
1485 		m->m_pkthdr.src_iff = 0;
1486 		m->m_pkthdr.src_ifindex = (uint16_t)src_idx;
1487 		if (src_idx != 0) {
1488 			m->m_pkthdr.pkt_flags |= PKTF_IFAINFO;
1489 		}
1490 	}
1491 }
1492 
1493 void
ip6_setdstifaddr_info(struct mbuf * m,uint32_t dst_idx,struct in6_ifaddr * ia6)1494 ip6_setdstifaddr_info(struct mbuf *m, uint32_t dst_idx, struct in6_ifaddr *ia6)
1495 {
1496 	VERIFY(m->m_flags & M_PKTHDR);
1497 	m->m_pkthdr.pkt_ext_flags &= ~PKTF_EXT_OUTPUT_SCOPE;
1498 
1499 	/*
1500 	 * If the destination ifaddr is specified, pick up the information
1501 	 * from there; otherwise just grab the passed-in ifindex as the
1502 	 * caller may not have the ifaddr available.
1503 	 */
1504 	if (ia6 != NULL) {
1505 		m->m_pkthdr.pkt_flags |= PKTF_IFAINFO;
1506 		m->m_pkthdr.dst_ifindex = ia6->ia_ifp->if_index;
1507 
1508 		/* See IN6_IFF comments in in6_var.h */
1509 		m->m_pkthdr.dst_iff = (ia6->ia6_flags & 0xffff);
1510 	} else {
1511 		m->m_pkthdr.dst_iff = 0;
1512 		m->m_pkthdr.dst_ifindex = (uint16_t)dst_idx;
1513 		if (dst_idx != 0) {
1514 			m->m_pkthdr.pkt_flags |= PKTF_IFAINFO;
1515 		}
1516 	}
1517 }
1518 
1519 int
ip6_getsrcifaddr_info(struct mbuf * m,uint32_t * src_idx,uint32_t * ia6f)1520 ip6_getsrcifaddr_info(struct mbuf *m, uint32_t *src_idx, uint32_t *ia6f)
1521 {
1522 	VERIFY(m->m_flags & M_PKTHDR);
1523 
1524 	if (!(m->m_pkthdr.pkt_flags & PKTF_IFAINFO)) {
1525 		return -1;
1526 	}
1527 
1528 	if (src_idx != NULL) {
1529 		*src_idx = m->m_pkthdr.src_ifindex;
1530 	}
1531 
1532 	if (ia6f != NULL) {
1533 		*ia6f = m->m_pkthdr.src_iff;
1534 	}
1535 
1536 	return 0;
1537 }
1538 
1539 int
ip6_getdstifaddr_info(struct mbuf * m,uint32_t * dst_idx,uint32_t * ia6f)1540 ip6_getdstifaddr_info(struct mbuf *m, uint32_t *dst_idx, uint32_t *ia6f)
1541 {
1542 	VERIFY(m->m_flags & M_PKTHDR);
1543 
1544 	if (!(m->m_pkthdr.pkt_flags & PKTF_IFAINFO)) {
1545 		return -1;
1546 	}
1547 
1548 	if (dst_idx != NULL) {
1549 		*dst_idx = m->m_pkthdr.dst_ifindex;
1550 	}
1551 
1552 	if (ia6f != NULL) {
1553 		*ia6f = m->m_pkthdr.dst_iff;
1554 	}
1555 
1556 	return 0;
1557 }
1558 
1559 uint32_t
ip6_input_getsrcifscope(struct mbuf * m)1560 ip6_input_getsrcifscope(struct mbuf *m)
1561 {
1562 	VERIFY(m->m_flags & M_PKTHDR);
1563 
1564 	if (m->m_pkthdr.rcvif != NULL) {
1565 		return m->m_pkthdr.rcvif->if_index;
1566 	}
1567 
1568 	uint32_t src_ifscope = IFSCOPE_NONE;
1569 	ip6_getsrcifaddr_info(m, &src_ifscope, NULL);
1570 	return src_ifscope;
1571 }
1572 
1573 uint32_t
ip6_input_getdstifscope(struct mbuf * m)1574 ip6_input_getdstifscope(struct mbuf *m)
1575 {
1576 	VERIFY(m->m_flags & M_PKTHDR);
1577 
1578 	if (m->m_pkthdr.rcvif != NULL) {
1579 		return m->m_pkthdr.rcvif->if_index;
1580 	}
1581 
1582 	uint32_t dst_ifscope = IFSCOPE_NONE;
1583 	ip6_getdstifaddr_info(m, &dst_ifscope, NULL);
1584 	return dst_ifscope;
1585 }
1586 
1587 /*
1588  * Hop-by-Hop options header processing. If a valid jumbo payload option is
1589  * included, the real payload length will be stored in plenp.
1590  */
1591 static int
ip6_hopopts_input(uint32_t * plenp,uint32_t * rtalertp,struct mbuf ** mp,int * offp)1592 ip6_hopopts_input(uint32_t *plenp, uint32_t *rtalertp, struct mbuf **mp,
1593     int *offp)
1594 {
1595 	mbuf_ref_t m = *mp;
1596 	int off = *offp, hbhlen;
1597 	struct ip6_hbh *hbh;
1598 	u_int8_t *opt;
1599 
1600 	/* validation of the length of the header */
1601 	IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), return (-1));
1602 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1603 	hbhlen = (hbh->ip6h_len + 1) << 3;
1604 
1605 	IP6_EXTHDR_CHECK(m, off, hbhlen, return (-1));
1606 	hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1607 	off += hbhlen;
1608 	hbhlen -= sizeof(struct ip6_hbh);
1609 	opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
1610 
1611 	if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
1612 	    hbhlen, rtalertp, plenp) < 0) {
1613 		return -1;
1614 	}
1615 
1616 	*offp = off;
1617 	*mp = m;
1618 	return 0;
1619 }
1620 
1621 /*
1622  * Search header for all Hop-by-hop options and process each option.
1623  * This function is separate from ip6_hopopts_input() in order to
1624  * handle a case where the sending node itself process its hop-by-hop
1625  * options header. In such a case, the function is called from ip6_output().
1626  *
1627  * The function assumes that hbh header is located right after the IPv6 header
1628  * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
1629  * opthead + hbhlen is located in continuous memory region.
1630  */
1631 int
ip6_process_hopopts(struct mbuf * m,u_int8_t * __sized_by (inhbhlen)opthead,int inhbhlen,u_int32_t * rtalertp,u_int32_t * plenp)1632 ip6_process_hopopts(struct mbuf *m, u_int8_t *__sized_by(inhbhlen)opthead, int inhbhlen,
1633     u_int32_t *rtalertp, u_int32_t *plenp)
1634 {
1635 	struct ip6_hdr *__single ip6;
1636 	int optlen = 0;
1637 	int hbhlen = inhbhlen;
1638 	u_int8_t *opt = opthead;
1639 	u_int16_t rtalert_val;
1640 	u_int32_t jumboplen;
1641 	const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1642 
1643 	for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1644 		switch (*opt) {
1645 		case IP6OPT_PAD1:
1646 			optlen = 1;
1647 			break;
1648 		case IP6OPT_PADN:
1649 			if (hbhlen < IP6OPT_MINLEN) {
1650 				ip6stat.ip6s_toosmall++;
1651 				m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_TOO_SMALL, NULL, 0);
1652 				goto bad;
1653 			}
1654 			optlen = *(opt + 1) + 2;
1655 			break;
1656 		case IP6OPT_ROUTER_ALERT:
1657 			/* XXX may need check for alignment */
1658 			if (hbhlen < IP6OPT_RTALERT_LEN) {
1659 				ip6stat.ip6s_toosmall++;
1660 				goto bad;
1661 			}
1662 			if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1663 				/* XXX stat */
1664 				icmp6_error(m, ICMP6_PARAM_PROB,
1665 				    ICMP6_PARAMPROB_HEADER,
1666 				    (int)(erroff + opt + 1 - opthead));
1667 				return -1;
1668 			}
1669 			optlen = IP6OPT_RTALERT_LEN;
1670 			bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
1671 			*rtalertp = ntohs(rtalert_val);
1672 			break;
1673 		case IP6OPT_JUMBO:
1674 			/* XXX may need check for alignment */
1675 			if (hbhlen < IP6OPT_JUMBO_LEN) {
1676 				ip6stat.ip6s_toosmall++;
1677 				m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_TOO_SMALL, NULL, 0);
1678 				goto bad;
1679 			}
1680 			if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1681 				/* XXX stat */
1682 				icmp6_error(m, ICMP6_PARAM_PROB,
1683 				    ICMP6_PARAMPROB_HEADER,
1684 				    (int)(erroff + opt + 1 - opthead));
1685 				return -1;
1686 			}
1687 			optlen = IP6OPT_JUMBO_LEN;
1688 
1689 			/*
1690 			 * IPv6 packets that have non 0 payload length
1691 			 * must not contain a jumbo payload option.
1692 			 */
1693 			ip6 = mtod(m, struct ip6_hdr *);
1694 			if (ip6->ip6_plen) {
1695 				ip6stat.ip6s_badoptions++;
1696 				icmp6_error(m, ICMP6_PARAM_PROB,
1697 				    ICMP6_PARAMPROB_HEADER,
1698 				    (int)(erroff + opt - opthead));
1699 				return -1;
1700 			}
1701 
1702 			/*
1703 			 * We may see jumbolen in unaligned location, so
1704 			 * we'd need to perform bcopy().
1705 			 */
1706 			bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
1707 			jumboplen = (u_int32_t)htonl(jumboplen);
1708 
1709 #if 1
1710 			/*
1711 			 * if there are multiple jumbo payload options,
1712 			 * *plenp will be non-zero and the packet will be
1713 			 * rejected.
1714 			 * the behavior may need some debate in ipngwg -
1715 			 * multiple options does not make sense, however,
1716 			 * there's no explicit mention in specification.
1717 			 */
1718 			if (*plenp != 0) {
1719 				ip6stat.ip6s_badoptions++;
1720 				icmp6_error(m, ICMP6_PARAM_PROB,
1721 				    ICMP6_PARAMPROB_HEADER,
1722 				    (int)(erroff + opt + 2 - opthead));
1723 				return -1;
1724 			}
1725 #endif
1726 
1727 			/*
1728 			 * jumbo payload length must be larger than 65535.
1729 			 */
1730 			if (jumboplen <= IPV6_MAXPACKET) {
1731 				ip6stat.ip6s_badoptions++;
1732 				icmp6_error(m, ICMP6_PARAM_PROB,
1733 				    ICMP6_PARAMPROB_HEADER,
1734 				    (int)(erroff + opt + 2 - opthead));
1735 				return -1;
1736 			}
1737 			*plenp = jumboplen;
1738 
1739 			break;
1740 		default:                /* unknown option */
1741 			if (hbhlen < IP6OPT_MINLEN) {
1742 				ip6stat.ip6s_toosmall++;
1743 				m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP_TOO_SMALL, NULL, 0);
1744 				goto bad;
1745 			}
1746 			optlen = ip6_unknown_opt(opt, hbhlen, m,
1747 			    erroff + opt - opthead);
1748 			if (optlen == -1) {
1749 				return -1;
1750 			}
1751 			optlen += 2;
1752 			break;
1753 		}
1754 	}
1755 
1756 	return 0;
1757 
1758 bad:
1759 	return -1;
1760 }
1761 
1762 /*
1763  * Unknown option processing.
1764  * The fourth argument `off' is the offset from the IPv6 header to the option,
1765  * which is necessary if the IPv6 header the and option header and IPv6 header
1766  * is not continuous in order to return an ICMPv6 error.
1767  */
1768 int
ip6_unknown_opt(uint8_t * __counted_by (optplen)optp,size_t optplen,struct mbuf * m,size_t off)1769 ip6_unknown_opt(uint8_t *__counted_by(optplen) optp, size_t optplen, struct mbuf *m, size_t off)
1770 {
1771 #pragma unused(optplen)
1772 
1773 	struct ip6_hdr *__single ip6;
1774 
1775 	switch (IP6OPT_TYPE(*optp)) {
1776 	case IP6OPT_TYPE_SKIP: /* ignore the option */
1777 		return (int)*(optp + 1);
1778 
1779 	case IP6OPT_TYPE_DISCARD:       /* silently discard */
1780 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_OPT_DISCARD, NULL, 0);
1781 		return -1;
1782 
1783 	case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1784 		ip6stat.ip6s_badoptions++;
1785 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, (int)off);
1786 		return -1;
1787 
1788 	case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1789 		ip6stat.ip6s_badoptions++;
1790 		ip6 = mtod(m, struct ip6_hdr *);
1791 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1792 		    (m->m_flags & (M_BCAST | M_MCAST))) {
1793 			m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_OPTION, NULL, 0);
1794 		} else {
1795 			icmp6_error(m, ICMP6_PARAM_PROB,
1796 			    ICMP6_PARAMPROB_OPTION, (int)off);
1797 		}
1798 		return -1;
1799 	}
1800 
1801 	m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_UNSPECIFIED, NULL, 0); /* XXX: NOTREACHED */
1802 	return -1;
1803 }
1804 
1805 /*
1806  * Create the "control" list for this pcb.
1807  * These functions will not modify mbuf chain at all.
1808  *
1809  * With KAME mbuf chain restriction:
1810  * The routine will be called from upper layer handlers like tcp6_input().
1811  * Thus the routine assumes that the caller (tcp6_input) have already
1812  * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1813  * very first mbuf on the mbuf chain.
1814  *
1815  * ip6_savecontrol_v4 will handle those options that are possible to be
1816  * set on a v4-mapped socket.
1817  * ip6_savecontrol will directly call ip6_savecontrol_v4 to handle those
1818  * options and handle the v6-only ones itself.
1819  */
1820 struct mbuf **
ip6_savecontrol_v4(struct inpcb * inp,struct mbuf * m,struct mbuf ** mp,int * v4only)1821 ip6_savecontrol_v4(struct inpcb *inp, struct mbuf *m, struct mbuf **mp,
1822     int *v4only)
1823 {
1824 	struct ip6_hdr *__single ip6 = mtod(m, struct ip6_hdr *);
1825 
1826 	if ((inp->inp_socket->so_options & SO_TIMESTAMP) != 0) {
1827 		struct timeval tv;
1828 
1829 		getmicrotime(&tv);
1830 		mp = sbcreatecontrol_mbuf((caddr_t)&tv, sizeof(tv),
1831 		    SCM_TIMESTAMP, SOL_SOCKET, mp);
1832 		if (*mp == NULL) {
1833 			return NULL;
1834 		}
1835 	}
1836 	if ((inp->inp_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
1837 		uint64_t time;
1838 
1839 		time = mach_absolute_time();
1840 		mp = sbcreatecontrol_mbuf((caddr_t)&time, sizeof(time),
1841 		    SCM_TIMESTAMP_MONOTONIC, SOL_SOCKET, mp);
1842 		if (*mp == NULL) {
1843 			return NULL;
1844 		}
1845 	}
1846 	if ((inp->inp_socket->so_options & SO_TIMESTAMP_CONTINUOUS) != 0) {
1847 		uint64_t time;
1848 
1849 		time = mach_continuous_time();
1850 		mp = sbcreatecontrol_mbuf((caddr_t)&time, sizeof(time),
1851 		    SCM_TIMESTAMP_CONTINUOUS, SOL_SOCKET, mp);
1852 		if (*mp == NULL) {
1853 			return NULL;
1854 		}
1855 	}
1856 	if ((inp->inp_socket->so_flags & SOF_RECV_TRAFFIC_CLASS) != 0) {
1857 		int tc = m_get_traffic_class(m);
1858 
1859 		mp = sbcreatecontrol_mbuf((caddr_t)&tc, sizeof(tc),
1860 		    SO_TRAFFIC_CLASS, SOL_SOCKET, mp);
1861 		if (*mp == NULL) {
1862 			return NULL;
1863 		}
1864 	}
1865 
1866 	if ((inp->inp_socket->so_flags & SOF_RECV_WAKE_PKT) &&
1867 	    (m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT)) {
1868 		int flag = 1;
1869 
1870 		mp = sbcreatecontrol_mbuf((caddr_t)&flag, sizeof(flag),
1871 		    SO_RECV_WAKE_PKT, SOL_SOCKET, mp);
1872 		if (*mp == NULL) {
1873 			return NULL;
1874 		}
1875 	}
1876 
1877 #define IS2292(inp, x, y)       (((inp)->inp_flags & IN6P_RFC2292) ? (x) : (y))
1878 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1879 		if (v4only != NULL) {
1880 			*v4only = 1;
1881 		}
1882 
1883 		// Send ECN flags for v4-mapped addresses
1884 		if ((inp->inp_flags & IN6P_TCLASS) != 0) {
1885 			struct ip *__single ip_header = mtod(m, struct ip *);
1886 
1887 			int tclass = (int)(ip_header->ip_tos);
1888 			mp = sbcreatecontrol_mbuf((caddr_t)&tclass, sizeof(tclass),
1889 			    IPV6_TCLASS, IPPROTO_IPV6, mp);
1890 			if (*mp == NULL) {
1891 				return NULL;
1892 			}
1893 		}
1894 
1895 		// Send IN6P_PKTINFO for v4-mapped address
1896 		if ((inp->inp_flags & IN6P_PKTINFO) != 0 || SOFLOW_ENABLED(inp->inp_socket)) {
1897 			struct in6_pktinfo pi6 = {
1898 				.ipi6_addr = IN6ADDR_V4MAPPED_INIT,
1899 				.ipi6_ifindex = (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0,
1900 			};
1901 
1902 			struct ip *__single ip_header = mtod(m, struct ip *);
1903 			bcopy(&ip_header->ip_dst, &pi6.ipi6_addr.s6_addr32[3], sizeof(struct in_addr));
1904 
1905 			mp = sbcreatecontrol_mbuf((caddr_t)&pi6,
1906 			    sizeof(struct in6_pktinfo),
1907 			    IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO),
1908 			    IPPROTO_IPV6, mp);
1909 			if (*mp == NULL) {
1910 				return NULL;
1911 			}
1912 		}
1913 		return mp;
1914 	}
1915 
1916 	/* RFC 2292 sec. 5 */
1917 	if ((inp->inp_flags & IN6P_PKTINFO) != 0 || SOFLOW_ENABLED(inp->inp_socket)) {
1918 		struct in6_pktinfo pi6;
1919 
1920 		bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1921 		in6_clearscope(&pi6.ipi6_addr); /* XXX */
1922 		pi6.ipi6_ifindex =
1923 		    (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0;
1924 
1925 		mp = sbcreatecontrol_mbuf((caddr_t)&pi6,
1926 		    sizeof(struct in6_pktinfo),
1927 		    IS2292(inp, IPV6_2292PKTINFO, IPV6_PKTINFO),
1928 		    IPPROTO_IPV6, mp);
1929 		if (*mp == NULL) {
1930 			return NULL;
1931 		}
1932 	}
1933 
1934 	if ((inp->inp_flags & IN6P_HOPLIMIT) != 0) {
1935 		int hlim = ip6->ip6_hlim & 0xff;
1936 
1937 		mp = sbcreatecontrol_mbuf((caddr_t)&hlim, sizeof(int),
1938 		    IS2292(inp, IPV6_2292HOPLIMIT, IPV6_HOPLIMIT),
1939 		    IPPROTO_IPV6, mp);
1940 		if (*mp == NULL) {
1941 			return NULL;
1942 		}
1943 	}
1944 
1945 	if (inp->inp_flags2 & INP2_RECV_LINK_ADDR_TYPE) {
1946 		int mode = IP_RECV_LINK_ADDR_UNICAST;
1947 
1948 		/* There is no broadcast for IPv6 */
1949 		if (m->m_flags & M_MCAST) {
1950 			mode = IP_RECV_LINK_ADDR_MULTICAST;
1951 		}
1952 
1953 		mp = sbcreatecontrol_mbuf((caddr_t)&mode,
1954 		    sizeof(int), IPV6_RECV_LINK_ADDR_TYPE, IPPROTO_IPV6, mp);
1955 		if (*mp == NULL) {
1956 			return NULL;
1957 		}
1958 	}
1959 
1960 	if (v4only != NULL) {
1961 		*v4only = 0;
1962 	}
1963 	return mp;
1964 }
1965 
1966 int
ip6_savecontrol(struct inpcb * in6p,struct mbuf * m,struct mbuf ** mp)1967 ip6_savecontrol(struct inpcb *in6p, struct mbuf *m, struct mbuf **mp)
1968 {
1969 	struct mbuf **np;
1970 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1971 	int v4only = 0;
1972 
1973 	*mp = NULL;
1974 	np = ip6_savecontrol_v4(in6p, m, mp, &v4only);
1975 	if (np == NULL) {
1976 		goto no_mbufs;
1977 	}
1978 
1979 	mp = np;
1980 	if (v4only) {
1981 		return 0;
1982 	}
1983 
1984 	if ((in6p->inp_flags & IN6P_TCLASS) != 0) {
1985 		u_int32_t flowinfo;
1986 		int tclass;
1987 
1988 		flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1989 		flowinfo >>= 20;
1990 
1991 		tclass = flowinfo & 0xff;
1992 		mp = sbcreatecontrol_mbuf((caddr_t)&tclass, sizeof(tclass),
1993 		    IPV6_TCLASS, IPPROTO_IPV6, mp);
1994 		if (*mp == NULL) {
1995 			goto no_mbufs;
1996 		}
1997 	}
1998 
1999 	/*
2000 	 * IPV6_HOPOPTS socket option.  Recall that we required super-user
2001 	 * privilege for the option (see ip6_ctloutput), but it might be too
2002 	 * strict, since there might be some hop-by-hop options which can be
2003 	 * returned to normal user.
2004 	 * See also RFC 2292 section 6 (or RFC 3542 section 8).
2005 	 */
2006 	if ((in6p->inp_flags & IN6P_HOPOPTS) != 0) {
2007 		/*
2008 		 * Check if a hop-by-hop options header is contatined in the
2009 		 * received packet, and if so, store the options as ancillary
2010 		 * data. Note that a hop-by-hop options header must be
2011 		 * just after the IPv6 header, which is assured through the
2012 		 * IPv6 input processing.
2013 		 */
2014 		ip6 = mtod(m, struct ip6_hdr *);
2015 		if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
2016 			struct ip6_hbh *hbh;
2017 			int hbhlen = 0;
2018 			hbh = (struct ip6_hbh *)(ip6 + 1);
2019 			hbhlen = (hbh->ip6h_len + 1) << 3;
2020 
2021 			/*
2022 			 * XXX: We copy the whole header even if a
2023 			 * jumbo payload option is included, the option which
2024 			 * is to be removed before returning according to
2025 			 * RFC2292.
2026 			 * Note: this constraint is removed in RFC3542
2027 			 */
2028 			mp = sbcreatecontrol_mbuf((caddr_t)hbh, hbhlen,
2029 			    IS2292(in6p, IPV6_2292HOPOPTS, IPV6_HOPOPTS),
2030 			    IPPROTO_IPV6, mp);
2031 
2032 			if (*mp == NULL) {
2033 				goto no_mbufs;
2034 			}
2035 		}
2036 	}
2037 
2038 	if ((in6p->inp_flags & (IN6P_RTHDR | IN6P_DSTOPTS)) != 0) {
2039 		int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);
2040 
2041 		/*
2042 		 * Search for destination options headers or routing
2043 		 * header(s) through the header chain, and stores each
2044 		 * header as ancillary data.
2045 		 * Note that the order of the headers remains in
2046 		 * the chain of ancillary data.
2047 		 */
2048 		while (1) {     /* is explicit loop prevention necessary? */
2049 			struct ip6_ext *ip6e = NULL;
2050 			int elen;
2051 
2052 			/*
2053 			 * if it is not an extension header, don't try to
2054 			 * pull it from the chain.
2055 			 */
2056 			switch (nxt) {
2057 			case IPPROTO_DSTOPTS:
2058 			case IPPROTO_ROUTING:
2059 			case IPPROTO_HOPOPTS:
2060 			case IPPROTO_AH: /* is it possible? */
2061 				break;
2062 			default:
2063 				goto loopend;
2064 			}
2065 
2066 			if (off + sizeof(*ip6e) > m->m_len) {
2067 				goto loopend;
2068 			}
2069 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
2070 			if (nxt == IPPROTO_AH) {
2071 				elen = (ip6e->ip6e_len + 2) << 2;
2072 			} else {
2073 				elen = (ip6e->ip6e_len + 1) << 3;
2074 			}
2075 			if (off + elen > m->m_len) {
2076 				goto loopend;
2077 			}
2078 
2079 			switch (nxt) {
2080 			case IPPROTO_DSTOPTS:
2081 				if (!(in6p->inp_flags & IN6P_DSTOPTS)) {
2082 					break;
2083 				}
2084 
2085 				mp = sbcreatecontrol_mbuf((caddr_t)ip6e, elen,
2086 				    IS2292(in6p, IPV6_2292DSTOPTS,
2087 				    IPV6_DSTOPTS), IPPROTO_IPV6, mp);
2088 				if (*mp == NULL) {
2089 					goto no_mbufs;
2090 				}
2091 				break;
2092 			case IPPROTO_ROUTING:
2093 				if (!(in6p->inp_flags & IN6P_RTHDR)) {
2094 					break;
2095 				}
2096 
2097 				mp = sbcreatecontrol_mbuf((caddr_t)ip6e, elen,
2098 				    IS2292(in6p, IPV6_2292RTHDR, IPV6_RTHDR),
2099 				    IPPROTO_IPV6, mp);
2100 				if (*mp == NULL) {
2101 					goto no_mbufs;
2102 				}
2103 				break;
2104 			case IPPROTO_HOPOPTS:
2105 			case IPPROTO_AH: /* is it possible? */
2106 				break;
2107 
2108 			default:
2109 				/*
2110 				 * other cases have been filtered in the above.
2111 				 * none will visit this case.  here we supply
2112 				 * the code just in case (nxt overwritten or
2113 				 * other cases).
2114 				 */
2115 				goto loopend;
2116 			}
2117 
2118 			/* proceed with the next header. */
2119 			off += elen;
2120 			nxt = ip6e->ip6e_nxt;
2121 			ip6e = NULL;
2122 		}
2123 loopend:
2124 		;
2125 	}
2126 	return 0;
2127 no_mbufs:
2128 	ip6stat.ip6s_pktdropcntrl++;
2129 	/* XXX increment a stat to show the failure */
2130 	return ENOBUFS;
2131 }
2132 #undef IS2292
2133 
2134 void
ip6_notify_pmtu(struct inpcb * in6p,struct sockaddr_in6 * dst,u_int32_t * mtu)2135 ip6_notify_pmtu(struct inpcb *in6p, struct sockaddr_in6 *dst, u_int32_t *mtu)
2136 {
2137 	struct socket *__single so;
2138 	mbuf_ref_t m_mtu;
2139 	struct ip6_mtuinfo mtuctl;
2140 
2141 	so =  in6p->inp_socket;
2142 
2143 	if ((in6p->inp_flags & IN6P_MTU) == 0) {
2144 		return;
2145 	}
2146 
2147 	if (mtu == NULL) {
2148 		return;
2149 	}
2150 
2151 	if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && SOCK_CHECK_PROTO(so, IPPROTO_TCP)) {
2152 		return;
2153 	}
2154 
2155 	if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
2156 	    !in6_are_addr_equal_scoped(&in6p->in6p_faddr, &dst->sin6_addr, in6p->inp_fifscope, dst->sin6_scope_id)) {
2157 		return;
2158 	}
2159 
2160 	bzero(&mtuctl, sizeof(mtuctl));         /* zero-clear for safety */
2161 	mtuctl.ip6m_mtu = *mtu;
2162 	mtuctl.ip6m_addr = *dst;
2163 	if (!in6_embedded_scope) {
2164 		mtuctl.ip6m_addr.sin6_scope_id = dst->sin6_scope_id;
2165 	}
2166 	if (sa6_recoverscope(&mtuctl.ip6m_addr, TRUE)) {
2167 		return;
2168 	}
2169 
2170 	if ((m_mtu = sbcreatecontrol((caddr_t)&mtuctl, sizeof(mtuctl),
2171 	    IPV6_PATHMTU, IPPROTO_IPV6)) == NULL) {
2172 		return;
2173 	}
2174 
2175 	if (sbappendaddr(&so->so_rcv, SA(dst), NULL, m_mtu, NULL) == 0) {
2176 		return;
2177 	}
2178 	sorwakeup(so);
2179 }
2180 
2181 /*
2182  * Get pointer to the previous header followed by the header
2183  * currently processed.
2184  * XXX: This function supposes that
2185  *	M includes all headers,
2186  *	the next header field and the header length field of each header
2187  *	are valid, and
2188  *	the sum of each header length equals to OFF.
2189  * Because of these assumptions, this function must be called very
2190  * carefully. Moreover, it will not be used in the near future when
2191  * we develop `neater' mechanism to process extension headers.
2192  */
2193 char *
ip6_get_prevhdr(struct mbuf * m,int off)2194 ip6_get_prevhdr(struct mbuf *m, int off)
2195 {
2196 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2197 
2198 	if (off == sizeof(struct ip6_hdr)) {
2199 		return (char *)&ip6->ip6_nxt;
2200 	} else {
2201 		int len, nxt;
2202 		struct ip6_ext *ip6e = NULL;
2203 
2204 		nxt = ip6->ip6_nxt;
2205 		len = sizeof(struct ip6_hdr);
2206 		while (len < off) {
2207 			ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
2208 
2209 			switch (nxt) {
2210 			case IPPROTO_FRAGMENT:
2211 				len += sizeof(struct ip6_frag);
2212 				break;
2213 			case IPPROTO_AH:
2214 				len += (ip6e->ip6e_len + 2) << 2;
2215 				break;
2216 			default:
2217 				len += (ip6e->ip6e_len + 1) << 3;
2218 				break;
2219 			}
2220 			nxt = ip6e->ip6e_nxt;
2221 		}
2222 		if (ip6e) {
2223 			return (char *)&ip6e->ip6e_nxt;
2224 		} else {
2225 			return NULL;
2226 		}
2227 	}
2228 }
2229 
2230 /*
2231  * get next header offset.  m will be retained.
2232  */
2233 int
ip6_nexthdr(struct mbuf * m,int off,int proto,int * nxtp)2234 ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
2235 {
2236 	struct ip6_hdr ip6;
2237 	struct ip6_ext ip6e;
2238 	struct ip6_frag fh;
2239 
2240 	/* just in case */
2241 	VERIFY(m != NULL);
2242 	if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) {
2243 		return -1;
2244 	}
2245 
2246 	switch (proto) {
2247 	case IPPROTO_IPV6:
2248 		if (m->m_pkthdr.len < off + sizeof(ip6)) {
2249 			return -1;
2250 		}
2251 		m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
2252 		if (nxtp) {
2253 			*nxtp = ip6.ip6_nxt;
2254 		}
2255 		off += sizeof(ip6);
2256 		return off;
2257 
2258 	case IPPROTO_FRAGMENT:
2259 		/*
2260 		 * terminate parsing if it is not the first fragment,
2261 		 * it does not make sense to parse through it.
2262 		 */
2263 		if (m->m_pkthdr.len < off + sizeof(fh)) {
2264 			return -1;
2265 		}
2266 		m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
2267 		/* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */
2268 		if (fh.ip6f_offlg & IP6F_OFF_MASK) {
2269 			return -1;
2270 		}
2271 		if (nxtp) {
2272 			*nxtp = fh.ip6f_nxt;
2273 		}
2274 		off += sizeof(struct ip6_frag);
2275 		return off;
2276 
2277 	case IPPROTO_AH:
2278 		if (m->m_pkthdr.len < off + sizeof(ip6e)) {
2279 			return -1;
2280 		}
2281 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
2282 		if (nxtp) {
2283 			*nxtp = ip6e.ip6e_nxt;
2284 		}
2285 		off += (ip6e.ip6e_len + 2) << 2;
2286 		return off;
2287 
2288 	case IPPROTO_HOPOPTS:
2289 	case IPPROTO_ROUTING:
2290 	case IPPROTO_DSTOPTS:
2291 		if (m->m_pkthdr.len < off + sizeof(ip6e)) {
2292 			return -1;
2293 		}
2294 		m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
2295 		if (nxtp) {
2296 			*nxtp = ip6e.ip6e_nxt;
2297 		}
2298 		off += (ip6e.ip6e_len + 1) << 3;
2299 		return off;
2300 
2301 	case IPPROTO_NONE:
2302 	case IPPROTO_ESP:
2303 	case IPPROTO_IPCOMP:
2304 		/* give up */
2305 		return -1;
2306 
2307 	default:
2308 		return -1;
2309 	}
2310 }
2311 
2312 /*
2313  * get offset for the last header in the chain.  m will be kept untainted.
2314  */
2315 int
ip6_lasthdr(struct mbuf * m,int off,int proto,int * nxtp)2316 ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
2317 {
2318 	int newoff;
2319 	int nxt;
2320 
2321 	if (!nxtp) {
2322 		nxt = -1;
2323 		nxtp = &nxt;
2324 	}
2325 	while (1) {
2326 		newoff = ip6_nexthdr(m, off, proto, nxtp);
2327 		if (newoff < 0) {
2328 			return off;
2329 		} else if (newoff < off) {
2330 			return -1;    /* invalid */
2331 		} else if (newoff == off) {
2332 			return newoff;
2333 		}
2334 
2335 		off = newoff;
2336 		proto = *nxtp;
2337 	}
2338 }
2339 
2340 boolean_t
ip6_pkt_has_ulp(struct mbuf * m)2341 ip6_pkt_has_ulp(struct mbuf *m)
2342 {
2343 	int off = 0, nxt = IPPROTO_NONE;
2344 
2345 	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
2346 	if (off < 0 || m->m_pkthdr.len < off) {
2347 		return FALSE;
2348 	}
2349 
2350 	switch (nxt) {
2351 	case IPPROTO_TCP:
2352 		if (off + sizeof(struct tcphdr) > m->m_pkthdr.len) {
2353 			return FALSE;
2354 		}
2355 		break;
2356 	case IPPROTO_UDP:
2357 		if (off + sizeof(struct udphdr) > m->m_pkthdr.len) {
2358 			return FALSE;
2359 		}
2360 		break;
2361 	case IPPROTO_ICMPV6:
2362 		if (off + sizeof(uint32_t) > m->m_pkthdr.len) {
2363 			return FALSE;
2364 		}
2365 		break;
2366 	case IPPROTO_NONE:
2367 		return TRUE;
2368 	case IPPROTO_ESP:
2369 		return TRUE;
2370 	case IPPROTO_IPCOMP:
2371 		return TRUE;
2372 	default:
2373 		return FALSE;
2374 	}
2375 	return TRUE;
2376 }
2377 
2378 struct ip6aux *
ip6_addaux(struct mbuf * m)2379 ip6_addaux(struct mbuf *m)
2380 {
2381 	struct m_tag *__single tag;
2382 
2383 	/* Check if one is already allocated */
2384 	tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
2385 	    KERNEL_TAG_TYPE_INET6);
2386 	if (tag == NULL) {
2387 		/* Allocate a tag */
2388 		tag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_INET6,
2389 		    sizeof(struct ip6aux), M_DONTWAIT, m);
2390 
2391 		/* Attach it to the mbuf */
2392 		if (tag) {
2393 			m_tag_prepend(m, tag);
2394 		}
2395 	}
2396 
2397 	return tag ? (struct ip6aux *)(tag->m_tag_data) : NULL;
2398 }
2399 
2400 struct ip6aux *
ip6_findaux(struct mbuf * m)2401 ip6_findaux(struct mbuf *m)
2402 {
2403 	struct m_tag *__single tag;
2404 
2405 	tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
2406 	    KERNEL_TAG_TYPE_INET6);
2407 
2408 	return tag != NULL ? (struct ip6aux *)(tag->m_tag_data) : NULL;
2409 }
2410 
2411 void
ip6_delaux(struct mbuf * m)2412 ip6_delaux(struct mbuf *m)
2413 {
2414 	struct m_tag *__single tag;
2415 
2416 	tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
2417 	    KERNEL_TAG_TYPE_INET6);
2418 	if (tag != NULL) {
2419 		m_tag_delete(m, tag);
2420 	}
2421 }
2422 
2423 struct inet6_tag_container {
2424 	struct m_tag    inet6_m_tag;
2425 	struct ip6aux   inet6_ip6a;
2426 };
2427 
2428 struct m_tag *
m_tag_kalloc_inet6(u_int32_t id,u_int16_t type,uint16_t len,int wait)2429 m_tag_kalloc_inet6(u_int32_t id, u_int16_t type, uint16_t len, int wait)
2430 {
2431 	struct inet6_tag_container *tag_container;
2432 	struct m_tag *tag = NULL;
2433 
2434 	assert3u(id, ==, KERNEL_MODULE_TAG_ID);
2435 	assert3u(type, ==, KERNEL_TAG_TYPE_INET6);
2436 	assert3u(len, ==, sizeof(struct ip6aux));
2437 
2438 	if (len != sizeof(struct ip6aux)) {
2439 		return NULL;
2440 	}
2441 
2442 	tag_container = kalloc_type(struct inet6_tag_container, wait | M_ZERO);
2443 	if (tag_container != NULL) {
2444 		tag =  &tag_container->inet6_m_tag;
2445 
2446 		assert3p(tag, ==, tag_container);
2447 
2448 		M_TAG_INIT(tag, id, type, len, &tag_container->inet6_ip6a, NULL);
2449 	}
2450 
2451 	return tag;
2452 }
2453 
2454 void
m_tag_kfree_inet6(struct m_tag * tag)2455 m_tag_kfree_inet6(struct m_tag *tag)
2456 {
2457 	struct inet6_tag_container *__single tag_container = (struct inet6_tag_container *)tag;
2458 
2459 	assert3u(tag->m_tag_len, ==, sizeof(struct ip6aux));
2460 
2461 	kfree_type(struct inet6_tag_container, tag_container);
2462 }
2463 
2464 void
ip6_register_m_tag(void)2465 ip6_register_m_tag(void)
2466 {
2467 	int error;
2468 
2469 	error = m_register_internal_tag_type(KERNEL_TAG_TYPE_INET6, sizeof(struct ip6aux),
2470 	    m_tag_kalloc_inet6, m_tag_kfree_inet6);
2471 
2472 	assert3u(error, ==, 0);
2473 }
2474 
2475 /*
2476  * Drain callback
2477  */
2478 void
ip6_drain(void)2479 ip6_drain(void)
2480 {
2481 	frag6_drain();          /* fragments */
2482 	in6_rtqdrain();         /* protocol cloned routes */
2483 	nd6_drain(NULL);        /* cloned routes: ND6 */
2484 }
2485 
2486 /*
2487  * System control for IP6
2488  */
2489 
2490 u_char  inet6ctlerrmap[PRC_NCMDS] = {
2491 	0, 0, 0, 0,
2492 	0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
2493 	EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
2494 	EMSGSIZE, EHOSTUNREACH, 0, 0,
2495 	0, 0, 0, 0,
2496 	ENOPROTOOPT, ECONNREFUSED
2497 };
2498 
2499 static int
2500 sysctl_reset_ip6_input_stats SYSCTL_HANDLER_ARGS
2501 {
2502 #pragma unused(arg1, arg2)
2503 	int error, i;
2504 
2505 	i = ip6_input_measure;
2506 	error = sysctl_handle_int(oidp, &i, 0, req);
2507 	if (error || req->newptr == USER_ADDR_NULL) {
2508 		goto done;
2509 	}
2510 	/* impose bounds */
2511 	if (i < 0 || i > 1) {
2512 		error = EINVAL;
2513 		goto done;
2514 	}
2515 	if (ip6_input_measure != i && i == 1) {
2516 		net_perf_initialize(&net_perf, ip6_input_measure_bins);
2517 	}
2518 	ip6_input_measure = i;
2519 done:
2520 	return error;
2521 }
2522 
2523 static int
2524 sysctl_ip6_input_measure_bins SYSCTL_HANDLER_ARGS
2525 {
2526 #pragma unused(arg1, arg2)
2527 	int error;
2528 	uint64_t i;
2529 
2530 	i = ip6_input_measure_bins;
2531 	error = sysctl_handle_quad(oidp, &i, 0, req);
2532 	if (error || req->newptr == USER_ADDR_NULL) {
2533 		goto done;
2534 	}
2535 	/* validate data */
2536 	if (!net_perf_validate_bins(i)) {
2537 		error = EINVAL;
2538 		goto done;
2539 	}
2540 	ip6_input_measure_bins = i;
2541 done:
2542 	return error;
2543 }
2544 
2545 static int
2546 sysctl_ip6_input_getperf SYSCTL_HANDLER_ARGS
2547 {
2548 #pragma unused(oidp, arg1, arg2)
2549 	if (req->oldptr == USER_ADDR_NULL) {
2550 		req->oldlen = (size_t)sizeof(struct net_perf);
2551 	}
2552 
2553 	return SYSCTL_OUT(req, &net_perf, MIN(sizeof(net_perf), req->oldlen));
2554 }
2555 
2556 
2557 /*
2558  * Initialize IPv6 source address hash table.
2559  */
2560 static void
in6_ifaddrhashtbl_init(void)2561 in6_ifaddrhashtbl_init(void)
2562 {
2563 	int i, k, p;
2564 	uint32_t nhash = 0;
2565 	uint32_t hash_size;
2566 
2567 	if (in6_ifaddrhashtbl != NULL) {
2568 		return;
2569 	}
2570 
2571 	PE_parse_boot_argn("ina6ddr_nhash", &nhash,
2572 	    sizeof(in6addr_nhash));
2573 	if (nhash == 0) {
2574 		nhash = IN6ADDR_NHASH;
2575 	}
2576 
2577 	hash_size = nhash * sizeof(*in6_ifaddrhashtbl);
2578 
2579 	in6_ifaddrhashtbl = zalloc_permanent(
2580 		hash_size,
2581 		ZALIGN_PTR);
2582 	in6addr_nhash = nhash;
2583 
2584 	/*
2585 	 * Generate the next largest prime greater than in6addr_nhash.
2586 	 */
2587 	k = (in6addr_nhash % 2 == 0) ? in6addr_nhash + 1 : in6addr_nhash + 2;
2588 	for (;;) {
2589 		p = 1;
2590 		for (i = 3; i * i <= k; i += 2) {
2591 			if (k % i == 0) {
2592 				p = 0;
2593 			}
2594 		}
2595 		if (p == 1) {
2596 			break;
2597 		}
2598 		k += 2;
2599 	}
2600 	in6addr_hashp = k;
2601 }
2602 
2603 static int
2604 sysctl_ip6_checkinterface SYSCTL_HANDLER_ARGS
2605 {
2606 #pragma unused(arg1, arg2)
2607 	int error, i;
2608 
2609 	i = ip6_checkinterface;
2610 	error = sysctl_handle_int(oidp, &i, 0, req);
2611 	if (error || req->newptr == USER_ADDR_NULL) {
2612 		return error;
2613 	}
2614 
2615 	switch (i) {
2616 	case IP6_CHECKINTERFACE_WEAK_ES:
2617 	case IP6_CHECKINTERFACE_HYBRID_ES:
2618 	case IP6_CHECKINTERFACE_STRONG_ES:
2619 		if (ip6_checkinterface != i) {
2620 			ip6_checkinterface = i;
2621 			os_log(OS_LOG_DEFAULT, "%s: ip6_checkinterface is now %d\n",
2622 			    __func__, ip6_checkinterface);
2623 		}
2624 		break;
2625 	default:
2626 		error = EINVAL;
2627 		break;
2628 	}
2629 	return error;
2630 }
2631