xref: /xnu-10002.81.5/bsd/netinet/kpi_ipfilter.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2004-2021 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions #include <sys/param.h>  /* for definition of NULL */
30*5e3eaea3SApple OSS Distributions #include <sys/errno.h>
31*5e3eaea3SApple OSS Distributions #include <sys/malloc.h>
32*5e3eaea3SApple OSS Distributions #include <sys/socket.h>
33*5e3eaea3SApple OSS Distributions #include <sys/mbuf.h>
34*5e3eaea3SApple OSS Distributions #include <sys/systm.h>
35*5e3eaea3SApple OSS Distributions #include <libkern/OSAtomic.h>
36*5e3eaea3SApple OSS Distributions 
37*5e3eaea3SApple OSS Distributions #include <machine/endian.h>
38*5e3eaea3SApple OSS Distributions 
39*5e3eaea3SApple OSS Distributions #define _IP_VHL
40*5e3eaea3SApple OSS Distributions #include <net/if_var.h>
41*5e3eaea3SApple OSS Distributions #include <net/route.h>
42*5e3eaea3SApple OSS Distributions #include <net/kpi_protocol.h>
43*5e3eaea3SApple OSS Distributions #include <net/net_api_stats.h>
44*5e3eaea3SApple OSS Distributions #if SKYWALK && defined(XNU_TARGET_OS_OSX)
45*5e3eaea3SApple OSS Distributions #include <skywalk/lib/net_filter_event.h>
46*5e3eaea3SApple OSS Distributions #endif /* SKYWALK && XNU_TARGET_OS_OSX */
47*5e3eaea3SApple OSS Distributions 
48*5e3eaea3SApple OSS Distributions #include <netinet/in_systm.h>
49*5e3eaea3SApple OSS Distributions #include <netinet/in.h>
50*5e3eaea3SApple OSS Distributions #include <netinet/in_var.h>
51*5e3eaea3SApple OSS Distributions #include <netinet6/in6_var.h>
52*5e3eaea3SApple OSS Distributions #include <netinet/ip.h>
53*5e3eaea3SApple OSS Distributions #include <netinet/ip6.h>
54*5e3eaea3SApple OSS Distributions #include <netinet/ip_var.h>
55*5e3eaea3SApple OSS Distributions #include <netinet6/ip6_var.h>
56*5e3eaea3SApple OSS Distributions #include <netinet/kpi_ipfilter_var.h>
57*5e3eaea3SApple OSS Distributions 
58*5e3eaea3SApple OSS Distributions #include <stdbool.h>
59*5e3eaea3SApple OSS Distributions 
60*5e3eaea3SApple OSS Distributions #if SKYWALK
61*5e3eaea3SApple OSS Distributions #include <skywalk/core/skywalk_var.h>
62*5e3eaea3SApple OSS Distributions #endif /* SKYWALK */
63*5e3eaea3SApple OSS Distributions 
64*5e3eaea3SApple OSS Distributions /*
65*5e3eaea3SApple OSS Distributions  * kipf_lock and kipf_ref protect the linkage of the list of IP filters
66*5e3eaea3SApple OSS Distributions  * An IP filter can be removed only when kipf_ref is zero
67*5e3eaea3SApple OSS Distributions  * If an IP filter cannot be removed because kipf_ref is not null, then
68*5e3eaea3SApple OSS Distributions  * the IP filter is marjed and kipf_delayed_remove is set so that when
69*5e3eaea3SApple OSS Distributions  * kipf_ref eventually goes down to zero, the IP filter is removed
70*5e3eaea3SApple OSS Distributions  */
71*5e3eaea3SApple OSS Distributions static LCK_GRP_DECLARE(kipf_lock_grp, "IP Filter");
72*5e3eaea3SApple OSS Distributions static LCK_MTX_DECLARE(kipf_lock, &kipf_lock_grp);
73*5e3eaea3SApple OSS Distributions static u_int32_t kipf_ref = 0;
74*5e3eaea3SApple OSS Distributions static u_int32_t kipf_delayed_remove = 0;
75*5e3eaea3SApple OSS Distributions u_int32_t kipf_count = 0;
76*5e3eaea3SApple OSS Distributions 
77*5e3eaea3SApple OSS Distributions __private_extern__ struct ipfilter_list ipv4_filters = TAILQ_HEAD_INITIALIZER(ipv4_filters);
78*5e3eaea3SApple OSS Distributions __private_extern__ struct ipfilter_list ipv6_filters = TAILQ_HEAD_INITIALIZER(ipv6_filters);
79*5e3eaea3SApple OSS Distributions __private_extern__ struct ipfilter_list tbr_filters = TAILQ_HEAD_INITIALIZER(tbr_filters);
80*5e3eaea3SApple OSS Distributions 
81*5e3eaea3SApple OSS Distributions #undef ipf_addv4
82*5e3eaea3SApple OSS Distributions #undef ipf_addv6
83*5e3eaea3SApple OSS Distributions extern errno_t ipf_addv4(const struct ipf_filter *filter,
84*5e3eaea3SApple OSS Distributions     ipfilter_t *filter_ref);
85*5e3eaea3SApple OSS Distributions extern errno_t ipf_addv6(const struct ipf_filter *filter,
86*5e3eaea3SApple OSS Distributions     ipfilter_t *filter_ref);
87*5e3eaea3SApple OSS Distributions 
88*5e3eaea3SApple OSS Distributions static errno_t ipf_add(const struct ipf_filter *filter,
89*5e3eaea3SApple OSS Distributions     ipfilter_t *filter_ref, struct ipfilter_list *head, bool is_internal);
90*5e3eaea3SApple OSS Distributions 
91*5e3eaea3SApple OSS Distributions #if SKYWALK && defined(XNU_TARGET_OS_OSX)
92*5e3eaea3SApple OSS Distributions static bool net_check_compatible_ipf(void);
93*5e3eaea3SApple OSS Distributions #endif /* SKYWALK && XNU_TARGET_OS_OSX */
94*5e3eaea3SApple OSS Distributions 
95*5e3eaea3SApple OSS Distributions __private_extern__ void
ipf_ref(void)96*5e3eaea3SApple OSS Distributions ipf_ref(void)
97*5e3eaea3SApple OSS Distributions {
98*5e3eaea3SApple OSS Distributions 	lck_mtx_lock(&kipf_lock);
99*5e3eaea3SApple OSS Distributions 	if (os_inc_overflow(&kipf_ref)) {
100*5e3eaea3SApple OSS Distributions 		panic("kipf_ref overflow");
101*5e3eaea3SApple OSS Distributions 	}
102*5e3eaea3SApple OSS Distributions 	lck_mtx_unlock(&kipf_lock);
103*5e3eaea3SApple OSS Distributions }
104*5e3eaea3SApple OSS Distributions 
105*5e3eaea3SApple OSS Distributions __private_extern__ void
ipf_unref(void)106*5e3eaea3SApple OSS Distributions ipf_unref(void)
107*5e3eaea3SApple OSS Distributions {
108*5e3eaea3SApple OSS Distributions 	lck_mtx_lock(&kipf_lock);
109*5e3eaea3SApple OSS Distributions 
110*5e3eaea3SApple OSS Distributions 	if (os_dec_overflow(&kipf_ref)) {
111*5e3eaea3SApple OSS Distributions 		panic("kipf_ref underflow");
112*5e3eaea3SApple OSS Distributions 	}
113*5e3eaea3SApple OSS Distributions 
114*5e3eaea3SApple OSS Distributions 	if (kipf_ref == 0 && kipf_delayed_remove != 0) {
115*5e3eaea3SApple OSS Distributions 		struct ipfilter *filter;
116*5e3eaea3SApple OSS Distributions 
117*5e3eaea3SApple OSS Distributions 		while ((filter = TAILQ_FIRST(&tbr_filters))) {
118*5e3eaea3SApple OSS Distributions 			VERIFY(OSDecrementAtomic64(&net_api_stats.nas_ipf_add_count) > 0);
119*5e3eaea3SApple OSS Distributions 			if (filter->ipf_flags & IPFF_INTERNAL) {
120*5e3eaea3SApple OSS Distributions 				VERIFY(OSDecrementAtomic64(&net_api_stats.nas_ipf_add_os_count) > 0);
121*5e3eaea3SApple OSS Distributions 			}
122*5e3eaea3SApple OSS Distributions 
123*5e3eaea3SApple OSS Distributions 			ipf_detach_func ipf_detach = filter->ipf_filter.ipf_detach;
124*5e3eaea3SApple OSS Distributions 			void* cookie = filter->ipf_filter.cookie;
125*5e3eaea3SApple OSS Distributions 
126*5e3eaea3SApple OSS Distributions 			TAILQ_REMOVE(filter->ipf_head, filter, ipf_link);
127*5e3eaea3SApple OSS Distributions 			TAILQ_REMOVE(&tbr_filters, filter, ipf_tbr);
128*5e3eaea3SApple OSS Distributions 			kipf_delayed_remove--;
129*5e3eaea3SApple OSS Distributions 
130*5e3eaea3SApple OSS Distributions 			if (ipf_detach) {
131*5e3eaea3SApple OSS Distributions 				lck_mtx_unlock(&kipf_lock);
132*5e3eaea3SApple OSS Distributions 				ipf_detach(cookie);
133*5e3eaea3SApple OSS Distributions 				lck_mtx_lock(&kipf_lock);
134*5e3eaea3SApple OSS Distributions 				/* In case some filter got to run while we released the lock */
135*5e3eaea3SApple OSS Distributions 				if (kipf_ref != 0) {
136*5e3eaea3SApple OSS Distributions 					break;
137*5e3eaea3SApple OSS Distributions 				}
138*5e3eaea3SApple OSS Distributions 			}
139*5e3eaea3SApple OSS Distributions 		}
140*5e3eaea3SApple OSS Distributions 	}
141*5e3eaea3SApple OSS Distributions #if SKYWALK && defined(XNU_TARGET_OS_OSX)
142*5e3eaea3SApple OSS Distributions 	net_filter_event_mark(NET_FILTER_EVENT_IP,
143*5e3eaea3SApple OSS Distributions 	    net_check_compatible_ipf());
144*5e3eaea3SApple OSS Distributions #endif /* SKYWALK && XNU_TARGET_OS_OSX */
145*5e3eaea3SApple OSS Distributions 	lck_mtx_unlock(&kipf_lock);
146*5e3eaea3SApple OSS Distributions }
147*5e3eaea3SApple OSS Distributions 
148*5e3eaea3SApple OSS Distributions static errno_t
ipf_add(const struct ipf_filter * filter,ipfilter_t * filter_ref,struct ipfilter_list * head,bool is_internal)149*5e3eaea3SApple OSS Distributions ipf_add(
150*5e3eaea3SApple OSS Distributions 	const struct ipf_filter *filter,
151*5e3eaea3SApple OSS Distributions 	ipfilter_t *filter_ref,
152*5e3eaea3SApple OSS Distributions 	struct ipfilter_list *head,
153*5e3eaea3SApple OSS Distributions 	bool is_internal)
154*5e3eaea3SApple OSS Distributions {
155*5e3eaea3SApple OSS Distributions 	struct ipfilter *new_filter;
156*5e3eaea3SApple OSS Distributions 	if (filter->name == NULL || (filter->ipf_input == NULL && filter->ipf_output == NULL)) {
157*5e3eaea3SApple OSS Distributions 		return EINVAL;
158*5e3eaea3SApple OSS Distributions 	}
159*5e3eaea3SApple OSS Distributions 
160*5e3eaea3SApple OSS Distributions 	new_filter = kalloc_type(struct ipfilter, Z_WAITOK | Z_NOFAIL);
161*5e3eaea3SApple OSS Distributions 
162*5e3eaea3SApple OSS Distributions 	lck_mtx_lock(&kipf_lock);
163*5e3eaea3SApple OSS Distributions 	new_filter->ipf_filter = *filter;
164*5e3eaea3SApple OSS Distributions 	new_filter->ipf_head = head;
165*5e3eaea3SApple OSS Distributions 
166*5e3eaea3SApple OSS Distributions 	TAILQ_INSERT_HEAD(head, new_filter, ipf_link);
167*5e3eaea3SApple OSS Distributions 
168*5e3eaea3SApple OSS Distributions 	OSIncrementAtomic64(&net_api_stats.nas_ipf_add_count);
169*5e3eaea3SApple OSS Distributions 	INC_ATOMIC_INT64_LIM(net_api_stats.nas_ipf_add_total);
170*5e3eaea3SApple OSS Distributions 	if (is_internal) {
171*5e3eaea3SApple OSS Distributions 		new_filter->ipf_flags = IPFF_INTERNAL;
172*5e3eaea3SApple OSS Distributions 		OSIncrementAtomic64(&net_api_stats.nas_ipf_add_os_count);
173*5e3eaea3SApple OSS Distributions 		INC_ATOMIC_INT64_LIM(net_api_stats.nas_ipf_add_os_total);
174*5e3eaea3SApple OSS Distributions 	}
175*5e3eaea3SApple OSS Distributions #if SKYWALK && defined(XNU_TARGET_OS_OSX)
176*5e3eaea3SApple OSS Distributions 	net_filter_event_mark(NET_FILTER_EVENT_IP,
177*5e3eaea3SApple OSS Distributions 	    net_check_compatible_ipf());
178*5e3eaea3SApple OSS Distributions #endif /* SKYWALK && XNU_TARGET_OS_OSX */
179*5e3eaea3SApple OSS Distributions 
180*5e3eaea3SApple OSS Distributions 	lck_mtx_unlock(&kipf_lock);
181*5e3eaea3SApple OSS Distributions 
182*5e3eaea3SApple OSS Distributions 	*filter_ref = (ipfilter_t)new_filter;
183*5e3eaea3SApple OSS Distributions 
184*5e3eaea3SApple OSS Distributions 	/* This will force TCP to re-evaluate its use of TSO */
185*5e3eaea3SApple OSS Distributions 	OSAddAtomic(1, &kipf_count);
186*5e3eaea3SApple OSS Distributions 	routegenid_update();
187*5e3eaea3SApple OSS Distributions 
188*5e3eaea3SApple OSS Distributions 	return 0;
189*5e3eaea3SApple OSS Distributions }
190*5e3eaea3SApple OSS Distributions 
191*5e3eaea3SApple OSS Distributions errno_t
ipf_addv4_internal(const struct ipf_filter * filter,ipfilter_t * filter_ref)192*5e3eaea3SApple OSS Distributions ipf_addv4_internal(
193*5e3eaea3SApple OSS Distributions 	const struct ipf_filter *filter,
194*5e3eaea3SApple OSS Distributions 	ipfilter_t *filter_ref)
195*5e3eaea3SApple OSS Distributions {
196*5e3eaea3SApple OSS Distributions 	return ipf_add(filter, filter_ref, &ipv4_filters, true);
197*5e3eaea3SApple OSS Distributions }
198*5e3eaea3SApple OSS Distributions 
199*5e3eaea3SApple OSS Distributions errno_t
ipf_addv4(const struct ipf_filter * filter,ipfilter_t * filter_ref)200*5e3eaea3SApple OSS Distributions ipf_addv4(
201*5e3eaea3SApple OSS Distributions 	const struct ipf_filter *filter,
202*5e3eaea3SApple OSS Distributions 	ipfilter_t *filter_ref)
203*5e3eaea3SApple OSS Distributions {
204*5e3eaea3SApple OSS Distributions 	return ipf_add(filter, filter_ref, &ipv4_filters, false);
205*5e3eaea3SApple OSS Distributions }
206*5e3eaea3SApple OSS Distributions 
207*5e3eaea3SApple OSS Distributions errno_t
ipf_addv6_internal(const struct ipf_filter * filter,ipfilter_t * filter_ref)208*5e3eaea3SApple OSS Distributions ipf_addv6_internal(
209*5e3eaea3SApple OSS Distributions 	const struct ipf_filter *filter,
210*5e3eaea3SApple OSS Distributions 	ipfilter_t *filter_ref)
211*5e3eaea3SApple OSS Distributions {
212*5e3eaea3SApple OSS Distributions 	return ipf_add(filter, filter_ref, &ipv6_filters, true);
213*5e3eaea3SApple OSS Distributions }
214*5e3eaea3SApple OSS Distributions 
215*5e3eaea3SApple OSS Distributions errno_t
ipf_addv6(const struct ipf_filter * filter,ipfilter_t * filter_ref)216*5e3eaea3SApple OSS Distributions ipf_addv6(
217*5e3eaea3SApple OSS Distributions 	const struct ipf_filter *filter,
218*5e3eaea3SApple OSS Distributions 	ipfilter_t *filter_ref)
219*5e3eaea3SApple OSS Distributions {
220*5e3eaea3SApple OSS Distributions 	return ipf_add(filter, filter_ref, &ipv6_filters, false);
221*5e3eaea3SApple OSS Distributions }
222*5e3eaea3SApple OSS Distributions 
223*5e3eaea3SApple OSS Distributions static errno_t
ipf_input_detached(void * cookie,mbuf_t * data,int offset,u_int8_t protocol)224*5e3eaea3SApple OSS Distributions ipf_input_detached(void *cookie, mbuf_t *data, int offset, u_int8_t protocol)
225*5e3eaea3SApple OSS Distributions {
226*5e3eaea3SApple OSS Distributions #pragma unused(cookie, data, offset, protocol)
227*5e3eaea3SApple OSS Distributions 
228*5e3eaea3SApple OSS Distributions #if DEBUG
229*5e3eaea3SApple OSS Distributions 	printf("ipf_input_detached\n");
230*5e3eaea3SApple OSS Distributions #endif /* DEBUG */
231*5e3eaea3SApple OSS Distributions 
232*5e3eaea3SApple OSS Distributions 	return 0;
233*5e3eaea3SApple OSS Distributions }
234*5e3eaea3SApple OSS Distributions 
235*5e3eaea3SApple OSS Distributions static errno_t
ipf_output_detached(void * cookie,mbuf_t * data,ipf_pktopts_t options)236*5e3eaea3SApple OSS Distributions ipf_output_detached(void *cookie, mbuf_t *data, ipf_pktopts_t options)
237*5e3eaea3SApple OSS Distributions {
238*5e3eaea3SApple OSS Distributions #pragma unused(cookie, data, options)
239*5e3eaea3SApple OSS Distributions 
240*5e3eaea3SApple OSS Distributions #if DEBUG
241*5e3eaea3SApple OSS Distributions 	printf("ipf_output_detached\n");
242*5e3eaea3SApple OSS Distributions #endif /* DEBUG */
243*5e3eaea3SApple OSS Distributions 
244*5e3eaea3SApple OSS Distributions 	return 0;
245*5e3eaea3SApple OSS Distributions }
246*5e3eaea3SApple OSS Distributions 
247*5e3eaea3SApple OSS Distributions errno_t
ipf_remove(ipfilter_t filter_ref)248*5e3eaea3SApple OSS Distributions ipf_remove(
249*5e3eaea3SApple OSS Distributions 	ipfilter_t filter_ref)
250*5e3eaea3SApple OSS Distributions {
251*5e3eaea3SApple OSS Distributions 	struct ipfilter *match = (struct ipfilter *)filter_ref;
252*5e3eaea3SApple OSS Distributions 	struct ipfilter_list *head;
253*5e3eaea3SApple OSS Distributions 
254*5e3eaea3SApple OSS Distributions 	if (match == 0 || (match->ipf_head != &ipv4_filters && match->ipf_head != &ipv6_filters)) {
255*5e3eaea3SApple OSS Distributions 		return EINVAL;
256*5e3eaea3SApple OSS Distributions 	}
257*5e3eaea3SApple OSS Distributions 
258*5e3eaea3SApple OSS Distributions 	head = match->ipf_head;
259*5e3eaea3SApple OSS Distributions 
260*5e3eaea3SApple OSS Distributions 	lck_mtx_lock(&kipf_lock);
261*5e3eaea3SApple OSS Distributions 	TAILQ_FOREACH(match, head, ipf_link) {
262*5e3eaea3SApple OSS Distributions 		if (match == (struct ipfilter *)filter_ref) {
263*5e3eaea3SApple OSS Distributions 			ipf_detach_func ipf_detach = match->ipf_filter.ipf_detach;
264*5e3eaea3SApple OSS Distributions 			void* cookie = match->ipf_filter.cookie;
265*5e3eaea3SApple OSS Distributions 
266*5e3eaea3SApple OSS Distributions 			/*
267*5e3eaea3SApple OSS Distributions 			 * Cannot detach when they are filters running
268*5e3eaea3SApple OSS Distributions 			 */
269*5e3eaea3SApple OSS Distributions 			if (kipf_ref) {
270*5e3eaea3SApple OSS Distributions 				kipf_delayed_remove++;
271*5e3eaea3SApple OSS Distributions 				TAILQ_INSERT_TAIL(&tbr_filters, match, ipf_tbr);
272*5e3eaea3SApple OSS Distributions 				match->ipf_filter.ipf_input = ipf_input_detached;
273*5e3eaea3SApple OSS Distributions 				match->ipf_filter.ipf_output = ipf_output_detached;
274*5e3eaea3SApple OSS Distributions 				lck_mtx_unlock(&kipf_lock);
275*5e3eaea3SApple OSS Distributions 			} else {
276*5e3eaea3SApple OSS Distributions 				VERIFY(OSDecrementAtomic64(&net_api_stats.nas_ipf_add_count) > 0);
277*5e3eaea3SApple OSS Distributions 				if (match->ipf_flags & IPFF_INTERNAL) {
278*5e3eaea3SApple OSS Distributions 					VERIFY(OSDecrementAtomic64(&net_api_stats.nas_ipf_add_os_count) > 0);
279*5e3eaea3SApple OSS Distributions 				}
280*5e3eaea3SApple OSS Distributions 
281*5e3eaea3SApple OSS Distributions 				TAILQ_REMOVE(head, match, ipf_link);
282*5e3eaea3SApple OSS Distributions 				lck_mtx_unlock(&kipf_lock);
283*5e3eaea3SApple OSS Distributions 
284*5e3eaea3SApple OSS Distributions 				if (ipf_detach) {
285*5e3eaea3SApple OSS Distributions 					ipf_detach(cookie);
286*5e3eaea3SApple OSS Distributions 				}
287*5e3eaea3SApple OSS Distributions 				kfree_type(struct ipfilter, match);
288*5e3eaea3SApple OSS Distributions 
289*5e3eaea3SApple OSS Distributions 				/* This will force TCP to re-evaluate its use of TSO */
290*5e3eaea3SApple OSS Distributions 				OSAddAtomic(-1, &kipf_count);
291*5e3eaea3SApple OSS Distributions 				routegenid_update();
292*5e3eaea3SApple OSS Distributions 			}
293*5e3eaea3SApple OSS Distributions 			return 0;
294*5e3eaea3SApple OSS Distributions 		}
295*5e3eaea3SApple OSS Distributions 	}
296*5e3eaea3SApple OSS Distributions #if SKYWALK && defined(XNU_TARGET_OS_OSX)
297*5e3eaea3SApple OSS Distributions 	net_filter_event_mark(NET_FILTER_EVENT_IP,
298*5e3eaea3SApple OSS Distributions 	    net_check_compatible_ipf());
299*5e3eaea3SApple OSS Distributions #endif /* SKYWALK && XNU_TARGET_OS_OSX */
300*5e3eaea3SApple OSS Distributions 
301*5e3eaea3SApple OSS Distributions 	lck_mtx_unlock(&kipf_lock);
302*5e3eaea3SApple OSS Distributions 
303*5e3eaea3SApple OSS Distributions 	return ENOENT;
304*5e3eaea3SApple OSS Distributions }
305*5e3eaea3SApple OSS Distributions 
306*5e3eaea3SApple OSS Distributions int log_for_en1 = 0;
307*5e3eaea3SApple OSS Distributions 
308*5e3eaea3SApple OSS Distributions errno_t
ipf_inject_input(mbuf_t data,ipfilter_t filter_ref)309*5e3eaea3SApple OSS Distributions ipf_inject_input(
310*5e3eaea3SApple OSS Distributions 	mbuf_t data,
311*5e3eaea3SApple OSS Distributions 	ipfilter_t filter_ref)
312*5e3eaea3SApple OSS Distributions {
313*5e3eaea3SApple OSS Distributions 	struct mbuf *m = (struct mbuf *)data;
314*5e3eaea3SApple OSS Distributions 	struct m_tag *mtag = 0;
315*5e3eaea3SApple OSS Distributions 	struct ip *ip = mtod(m, struct ip *);
316*5e3eaea3SApple OSS Distributions 	struct ip6_hdr *ip6;
317*5e3eaea3SApple OSS Distributions 	u_int8_t        vers;
318*5e3eaea3SApple OSS Distributions 	int hlen;
319*5e3eaea3SApple OSS Distributions 	errno_t error = 0;
320*5e3eaea3SApple OSS Distributions 	protocol_family_t proto;
321*5e3eaea3SApple OSS Distributions 	struct in_ifaddr *ia = NULL;
322*5e3eaea3SApple OSS Distributions 	struct in_addr *pkt_dst = NULL;
323*5e3eaea3SApple OSS Distributions 	struct in6_ifaddr *ia6 = NULL;
324*5e3eaea3SApple OSS Distributions 	struct sockaddr_in6 pkt_dst6;
325*5e3eaea3SApple OSS Distributions 
326*5e3eaea3SApple OSS Distributions 	vers = IP_VHL_V(ip->ip_vhl);
327*5e3eaea3SApple OSS Distributions 
328*5e3eaea3SApple OSS Distributions 	switch (vers) {
329*5e3eaea3SApple OSS Distributions 	case 4:
330*5e3eaea3SApple OSS Distributions 		proto = PF_INET;
331*5e3eaea3SApple OSS Distributions 		break;
332*5e3eaea3SApple OSS Distributions 	case 6:
333*5e3eaea3SApple OSS Distributions 		proto = PF_INET6;
334*5e3eaea3SApple OSS Distributions 		break;
335*5e3eaea3SApple OSS Distributions 	default:
336*5e3eaea3SApple OSS Distributions 		error = ENOTSUP;
337*5e3eaea3SApple OSS Distributions 		goto done;
338*5e3eaea3SApple OSS Distributions 	}
339*5e3eaea3SApple OSS Distributions 
340*5e3eaea3SApple OSS Distributions 	if (filter_ref == 0 && m->m_pkthdr.rcvif == 0) {
341*5e3eaea3SApple OSS Distributions 		/*
342*5e3eaea3SApple OSS Distributions 		 * Search for interface with the local address
343*5e3eaea3SApple OSS Distributions 		 */
344*5e3eaea3SApple OSS Distributions 		switch (proto) {
345*5e3eaea3SApple OSS Distributions 		case PF_INET:
346*5e3eaea3SApple OSS Distributions 			pkt_dst = &ip->ip_dst;
347*5e3eaea3SApple OSS Distributions 			lck_rw_lock_shared(&in_ifaddr_rwlock);
348*5e3eaea3SApple OSS Distributions 			TAILQ_FOREACH(ia, INADDR_HASH(pkt_dst->s_addr), ia_hash) {
349*5e3eaea3SApple OSS Distributions 				if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst->s_addr) {
350*5e3eaea3SApple OSS Distributions 					m->m_pkthdr.rcvif = ia->ia_ifp;
351*5e3eaea3SApple OSS Distributions 					break;
352*5e3eaea3SApple OSS Distributions 				}
353*5e3eaea3SApple OSS Distributions 			}
354*5e3eaea3SApple OSS Distributions 			lck_rw_done(&in_ifaddr_rwlock);
355*5e3eaea3SApple OSS Distributions 			break;
356*5e3eaea3SApple OSS Distributions 
357*5e3eaea3SApple OSS Distributions 		case PF_INET6:
358*5e3eaea3SApple OSS Distributions 			ip6 = mtod(m, struct ip6_hdr *);
359*5e3eaea3SApple OSS Distributions 			pkt_dst6.sin6_addr = ip6->ip6_dst;
360*5e3eaea3SApple OSS Distributions 			lck_rw_lock_shared(&in6_ifaddr_rwlock);
361*5e3eaea3SApple OSS Distributions 			TAILQ_FOREACH(ia6, IN6ADDR_HASH(&pkt_dst6.sin6_addr), ia6_hash) {
362*5e3eaea3SApple OSS Distributions 				if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &pkt_dst6.sin6_addr)) {
363*5e3eaea3SApple OSS Distributions 					m->m_pkthdr.rcvif = ia6->ia_ifp;
364*5e3eaea3SApple OSS Distributions 					break;
365*5e3eaea3SApple OSS Distributions 				}
366*5e3eaea3SApple OSS Distributions 			}
367*5e3eaea3SApple OSS Distributions 			lck_rw_done(&in6_ifaddr_rwlock);
368*5e3eaea3SApple OSS Distributions 			break;
369*5e3eaea3SApple OSS Distributions 
370*5e3eaea3SApple OSS Distributions 		default:
371*5e3eaea3SApple OSS Distributions 			break;
372*5e3eaea3SApple OSS Distributions 		}
373*5e3eaea3SApple OSS Distributions 
374*5e3eaea3SApple OSS Distributions 		/*
375*5e3eaea3SApple OSS Distributions 		 * If none found, fallback to loopback
376*5e3eaea3SApple OSS Distributions 		 */
377*5e3eaea3SApple OSS Distributions 		if (m->m_pkthdr.rcvif == NULL) {
378*5e3eaea3SApple OSS Distributions 			m->m_pkthdr.rcvif = lo_ifp;
379*5e3eaea3SApple OSS Distributions 		}
380*5e3eaea3SApple OSS Distributions 
381*5e3eaea3SApple OSS Distributions 		m->m_pkthdr.csum_data = 0;
382*5e3eaea3SApple OSS Distributions 		m->m_pkthdr.csum_flags = 0;
383*5e3eaea3SApple OSS Distributions 		if (vers == 4) {
384*5e3eaea3SApple OSS Distributions 			hlen = IP_VHL_HL(ip->ip_vhl) << 2;
385*5e3eaea3SApple OSS Distributions 			ip->ip_sum = 0;
386*5e3eaea3SApple OSS Distributions 			ip->ip_sum = in_cksum(m, hlen);
387*5e3eaea3SApple OSS Distributions 		}
388*5e3eaea3SApple OSS Distributions 	}
389*5e3eaea3SApple OSS Distributions 	if (filter_ref != 0) {
390*5e3eaea3SApple OSS Distributions 		mtag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_IPFILT,
391*5e3eaea3SApple OSS Distributions 		    sizeof(ipfilter_t), M_NOWAIT, m);
392*5e3eaea3SApple OSS Distributions 		if (mtag == NULL) {
393*5e3eaea3SApple OSS Distributions 			error = ENOMEM;
394*5e3eaea3SApple OSS Distributions 			goto done;
395*5e3eaea3SApple OSS Distributions 		}
396*5e3eaea3SApple OSS Distributions 		*(ipfilter_t *)(mtag->m_tag_data) = filter_ref;
397*5e3eaea3SApple OSS Distributions 		m_tag_prepend(m, mtag);
398*5e3eaea3SApple OSS Distributions 	}
399*5e3eaea3SApple OSS Distributions 
400*5e3eaea3SApple OSS Distributions 	error = proto_inject(proto, data);
401*5e3eaea3SApple OSS Distributions 
402*5e3eaea3SApple OSS Distributions done:
403*5e3eaea3SApple OSS Distributions 	return error;
404*5e3eaea3SApple OSS Distributions }
405*5e3eaea3SApple OSS Distributions 
406*5e3eaea3SApple OSS Distributions static errno_t
ipf_injectv4_out(mbuf_t data,ipfilter_t filter_ref,ipf_pktopts_t options)407*5e3eaea3SApple OSS Distributions ipf_injectv4_out(mbuf_t data, ipfilter_t filter_ref, ipf_pktopts_t options)
408*5e3eaea3SApple OSS Distributions {
409*5e3eaea3SApple OSS Distributions 	struct route ro;
410*5e3eaea3SApple OSS Distributions 	struct ip *ip;
411*5e3eaea3SApple OSS Distributions 	struct mbuf *m = (struct mbuf *)data;
412*5e3eaea3SApple OSS Distributions 	errno_t error = 0;
413*5e3eaea3SApple OSS Distributions 	struct m_tag *mtag = NULL;
414*5e3eaea3SApple OSS Distributions 	struct ip_moptions *imo = NULL;
415*5e3eaea3SApple OSS Distributions 	struct ip_out_args ipoa;
416*5e3eaea3SApple OSS Distributions 
417*5e3eaea3SApple OSS Distributions 	bzero(&ipoa, sizeof(ipoa));
418*5e3eaea3SApple OSS Distributions 	ipoa.ipoa_boundif = IFSCOPE_NONE;
419*5e3eaea3SApple OSS Distributions 	ipoa.ipoa_sotc = SO_TC_UNSPEC;
420*5e3eaea3SApple OSS Distributions 	ipoa.ipoa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
421*5e3eaea3SApple OSS Distributions 
422*5e3eaea3SApple OSS Distributions 	/* Make the IP header contiguous in the mbuf */
423*5e3eaea3SApple OSS Distributions 	if ((size_t)m->m_len < sizeof(struct ip)) {
424*5e3eaea3SApple OSS Distributions 		m = m_pullup(m, sizeof(struct ip));
425*5e3eaea3SApple OSS Distributions 		if (m == NULL) {
426*5e3eaea3SApple OSS Distributions 			return ENOMEM;
427*5e3eaea3SApple OSS Distributions 		}
428*5e3eaea3SApple OSS Distributions 	}
429*5e3eaea3SApple OSS Distributions 	ip = (struct ip *)m_mtod(m);
430*5e3eaea3SApple OSS Distributions 
431*5e3eaea3SApple OSS Distributions 	if (filter_ref != 0) {
432*5e3eaea3SApple OSS Distributions 		mtag = m_tag_create(KERNEL_MODULE_TAG_ID,
433*5e3eaea3SApple OSS Distributions 		    KERNEL_TAG_TYPE_IPFILT, sizeof(ipfilter_t), M_NOWAIT, m);
434*5e3eaea3SApple OSS Distributions 		if (mtag == NULL) {
435*5e3eaea3SApple OSS Distributions 			m_freem(m);
436*5e3eaea3SApple OSS Distributions 			return ENOMEM;
437*5e3eaea3SApple OSS Distributions 		}
438*5e3eaea3SApple OSS Distributions 		*(ipfilter_t *)(mtag->m_tag_data) = filter_ref;
439*5e3eaea3SApple OSS Distributions 		m_tag_prepend(m, mtag);
440*5e3eaea3SApple OSS Distributions 	}
441*5e3eaea3SApple OSS Distributions 
442*5e3eaea3SApple OSS Distributions 	if (options != NULL && (options->ippo_flags & IPPOF_MCAST_OPTS) &&
443*5e3eaea3SApple OSS Distributions 	    (imo = ip_allocmoptions(Z_NOWAIT)) != NULL) {
444*5e3eaea3SApple OSS Distributions 		imo->imo_multicast_ifp = options->ippo_mcast_ifnet;
445*5e3eaea3SApple OSS Distributions 		imo->imo_multicast_ttl = options->ippo_mcast_ttl;
446*5e3eaea3SApple OSS Distributions 		imo->imo_multicast_loop = (u_char)options->ippo_mcast_loop;
447*5e3eaea3SApple OSS Distributions 	}
448*5e3eaea3SApple OSS Distributions 
449*5e3eaea3SApple OSS Distributions 	if (options != NULL) {
450*5e3eaea3SApple OSS Distributions 		if (options->ippo_flags & IPPOF_SELECT_SRCIF) {
451*5e3eaea3SApple OSS Distributions 			ipoa.ipoa_flags |= IPOAF_SELECT_SRCIF;
452*5e3eaea3SApple OSS Distributions 		}
453*5e3eaea3SApple OSS Distributions 		if (options->ippo_flags & IPPOF_BOUND_IF) {
454*5e3eaea3SApple OSS Distributions 			ipoa.ipoa_flags |= IPOAF_BOUND_IF;
455*5e3eaea3SApple OSS Distributions 			ipoa.ipoa_boundif = options->ippo_flags >>
456*5e3eaea3SApple OSS Distributions 			    IPPOF_SHIFT_IFSCOPE;
457*5e3eaea3SApple OSS Distributions 		}
458*5e3eaea3SApple OSS Distributions 		if (options->ippo_flags & IPPOF_NO_IFT_CELLULAR) {
459*5e3eaea3SApple OSS Distributions 			ipoa.ipoa_flags |= IPOAF_NO_CELLULAR;
460*5e3eaea3SApple OSS Distributions 		}
461*5e3eaea3SApple OSS Distributions 		if (options->ippo_flags & IPPOF_BOUND_SRCADDR) {
462*5e3eaea3SApple OSS Distributions 			ipoa.ipoa_flags |= IPOAF_BOUND_SRCADDR;
463*5e3eaea3SApple OSS Distributions 		}
464*5e3eaea3SApple OSS Distributions 		if (options->ippo_flags & IPPOF_NO_IFF_EXPENSIVE) {
465*5e3eaea3SApple OSS Distributions 			ipoa.ipoa_flags |= IPOAF_NO_EXPENSIVE;
466*5e3eaea3SApple OSS Distributions 		}
467*5e3eaea3SApple OSS Distributions 		if (options->ippo_flags & IPPOF_NO_IFF_CONSTRAINED) {
468*5e3eaea3SApple OSS Distributions 			ipoa.ipoa_flags |= IPOAF_NO_CONSTRAINED;
469*5e3eaea3SApple OSS Distributions 		}
470*5e3eaea3SApple OSS Distributions 	}
471*5e3eaea3SApple OSS Distributions 
472*5e3eaea3SApple OSS Distributions 	bzero(&ro, sizeof(struct route));
473*5e3eaea3SApple OSS Distributions 
474*5e3eaea3SApple OSS Distributions 	/* Put ip_len and ip_off in host byte order, ip_output expects that */
475*5e3eaea3SApple OSS Distributions 
476*5e3eaea3SApple OSS Distributions #if BYTE_ORDER != BIG_ENDIAN
477*5e3eaea3SApple OSS Distributions 	NTOHS(ip->ip_len);
478*5e3eaea3SApple OSS Distributions 	NTOHS(ip->ip_off);
479*5e3eaea3SApple OSS Distributions #endif
480*5e3eaea3SApple OSS Distributions 
481*5e3eaea3SApple OSS Distributions 	/* Send; enforce source interface selection via IP_OUTARGS flag */
482*5e3eaea3SApple OSS Distributions 	error = ip_output(m, NULL, &ro,
483*5e3eaea3SApple OSS Distributions 	    IP_ALLOWBROADCAST | IP_RAWOUTPUT | IP_OUTARGS, imo, &ipoa);
484*5e3eaea3SApple OSS Distributions 
485*5e3eaea3SApple OSS Distributions 	/* Release the route */
486*5e3eaea3SApple OSS Distributions 	ROUTE_RELEASE(&ro);
487*5e3eaea3SApple OSS Distributions 
488*5e3eaea3SApple OSS Distributions 	if (imo != NULL) {
489*5e3eaea3SApple OSS Distributions 		IMO_REMREF(imo);
490*5e3eaea3SApple OSS Distributions 	}
491*5e3eaea3SApple OSS Distributions 
492*5e3eaea3SApple OSS Distributions 	return error;
493*5e3eaea3SApple OSS Distributions }
494*5e3eaea3SApple OSS Distributions 
495*5e3eaea3SApple OSS Distributions static errno_t
ipf_injectv6_out(mbuf_t data,ipfilter_t filter_ref,ipf_pktopts_t options)496*5e3eaea3SApple OSS Distributions ipf_injectv6_out(mbuf_t data, ipfilter_t filter_ref, ipf_pktopts_t options)
497*5e3eaea3SApple OSS Distributions {
498*5e3eaea3SApple OSS Distributions 	struct route_in6 ro;
499*5e3eaea3SApple OSS Distributions 	struct ip6_hdr *ip6;
500*5e3eaea3SApple OSS Distributions 	struct mbuf *m = (struct mbuf *)data;
501*5e3eaea3SApple OSS Distributions 	errno_t error = 0;
502*5e3eaea3SApple OSS Distributions 	struct m_tag *mtag = NULL;
503*5e3eaea3SApple OSS Distributions 	struct ip6_moptions *im6o = NULL;
504*5e3eaea3SApple OSS Distributions 	struct ip6_out_args ip6oa;
505*5e3eaea3SApple OSS Distributions 
506*5e3eaea3SApple OSS Distributions 	bzero(&ip6oa, sizeof(ip6oa));
507*5e3eaea3SApple OSS Distributions 	ip6oa.ip6oa_boundif = IFSCOPE_NONE;
508*5e3eaea3SApple OSS Distributions 	ip6oa.ip6oa_sotc = SO_TC_UNSPEC;
509*5e3eaea3SApple OSS Distributions 	ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
510*5e3eaea3SApple OSS Distributions 
511*5e3eaea3SApple OSS Distributions 	/* Make the IP header contiguous in the mbuf */
512*5e3eaea3SApple OSS Distributions 	if ((size_t)m->m_len < sizeof(struct ip6_hdr)) {
513*5e3eaea3SApple OSS Distributions 		m = m_pullup(m, sizeof(struct ip6_hdr));
514*5e3eaea3SApple OSS Distributions 		if (m == NULL) {
515*5e3eaea3SApple OSS Distributions 			return ENOMEM;
516*5e3eaea3SApple OSS Distributions 		}
517*5e3eaea3SApple OSS Distributions 	}
518*5e3eaea3SApple OSS Distributions 	ip6 = (struct ip6_hdr *)m_mtod(m);
519*5e3eaea3SApple OSS Distributions 
520*5e3eaea3SApple OSS Distributions 	if (filter_ref != 0) {
521*5e3eaea3SApple OSS Distributions 		mtag = m_tag_create(KERNEL_MODULE_TAG_ID,
522*5e3eaea3SApple OSS Distributions 		    KERNEL_TAG_TYPE_IPFILT, sizeof(ipfilter_t), M_NOWAIT, m);
523*5e3eaea3SApple OSS Distributions 		if (mtag == NULL) {
524*5e3eaea3SApple OSS Distributions 			m_freem(m);
525*5e3eaea3SApple OSS Distributions 			return ENOMEM;
526*5e3eaea3SApple OSS Distributions 		}
527*5e3eaea3SApple OSS Distributions 		*(ipfilter_t *)(mtag->m_tag_data) = filter_ref;
528*5e3eaea3SApple OSS Distributions 		m_tag_prepend(m, mtag);
529*5e3eaea3SApple OSS Distributions 	}
530*5e3eaea3SApple OSS Distributions 
531*5e3eaea3SApple OSS Distributions 	if (options != NULL && (options->ippo_flags & IPPOF_MCAST_OPTS) &&
532*5e3eaea3SApple OSS Distributions 	    (im6o = ip6_allocmoptions(Z_NOWAIT)) != NULL) {
533*5e3eaea3SApple OSS Distributions 		im6o->im6o_multicast_ifp = options->ippo_mcast_ifnet;
534*5e3eaea3SApple OSS Distributions 		im6o->im6o_multicast_hlim = options->ippo_mcast_ttl;
535*5e3eaea3SApple OSS Distributions 		im6o->im6o_multicast_loop = (u_char)options->ippo_mcast_loop;
536*5e3eaea3SApple OSS Distributions 	}
537*5e3eaea3SApple OSS Distributions 
538*5e3eaea3SApple OSS Distributions 	if (options != NULL) {
539*5e3eaea3SApple OSS Distributions 		if (options->ippo_flags & IPPOF_SELECT_SRCIF) {
540*5e3eaea3SApple OSS Distributions 			ip6oa.ip6oa_flags |= IP6OAF_SELECT_SRCIF;
541*5e3eaea3SApple OSS Distributions 		}
542*5e3eaea3SApple OSS Distributions 		if (options->ippo_flags & IPPOF_BOUND_IF) {
543*5e3eaea3SApple OSS Distributions 			ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
544*5e3eaea3SApple OSS Distributions 			ip6oa.ip6oa_boundif = options->ippo_flags >>
545*5e3eaea3SApple OSS Distributions 			    IPPOF_SHIFT_IFSCOPE;
546*5e3eaea3SApple OSS Distributions 		}
547*5e3eaea3SApple OSS Distributions 		if (options->ippo_flags & IPPOF_NO_IFT_CELLULAR) {
548*5e3eaea3SApple OSS Distributions 			ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR;
549*5e3eaea3SApple OSS Distributions 		}
550*5e3eaea3SApple OSS Distributions 		if (options->ippo_flags & IPPOF_BOUND_SRCADDR) {
551*5e3eaea3SApple OSS Distributions 			ip6oa.ip6oa_flags |= IP6OAF_BOUND_SRCADDR;
552*5e3eaea3SApple OSS Distributions 		}
553*5e3eaea3SApple OSS Distributions 		if (options->ippo_flags & IPPOF_NO_IFF_EXPENSIVE) {
554*5e3eaea3SApple OSS Distributions 			ip6oa.ip6oa_flags |= IP6OAF_NO_EXPENSIVE;
555*5e3eaea3SApple OSS Distributions 		}
556*5e3eaea3SApple OSS Distributions 		if (options->ippo_flags & IPPOF_NO_IFF_CONSTRAINED) {
557*5e3eaea3SApple OSS Distributions 			ip6oa.ip6oa_flags |= IP6OAF_NO_CONSTRAINED;
558*5e3eaea3SApple OSS Distributions 		}
559*5e3eaea3SApple OSS Distributions 	}
560*5e3eaea3SApple OSS Distributions 
561*5e3eaea3SApple OSS Distributions 	bzero(&ro, sizeof(struct route_in6));
562*5e3eaea3SApple OSS Distributions 
563*5e3eaea3SApple OSS Distributions 	/*
564*5e3eaea3SApple OSS Distributions 	 * Send  mbuf and ifscope information. Check for correctness
565*5e3eaea3SApple OSS Distributions 	 * of ifscope information is done while searching for a route in
566*5e3eaea3SApple OSS Distributions 	 * ip6_output.
567*5e3eaea3SApple OSS Distributions 	 */
568*5e3eaea3SApple OSS Distributions 	ip6_output_setsrcifscope(m, IFSCOPE_UNKNOWN, NULL);
569*5e3eaea3SApple OSS Distributions 	ip6_output_setdstifscope(m, IFSCOPE_UNKNOWN, NULL);
570*5e3eaea3SApple OSS Distributions 	error = ip6_output(m, NULL, &ro, IPV6_OUTARGS, im6o, NULL, &ip6oa);
571*5e3eaea3SApple OSS Distributions 
572*5e3eaea3SApple OSS Distributions 	/* Release the route */
573*5e3eaea3SApple OSS Distributions 	ROUTE_RELEASE(&ro);
574*5e3eaea3SApple OSS Distributions 
575*5e3eaea3SApple OSS Distributions 	if (im6o != NULL) {
576*5e3eaea3SApple OSS Distributions 		IM6O_REMREF(im6o);
577*5e3eaea3SApple OSS Distributions 	}
578*5e3eaea3SApple OSS Distributions 
579*5e3eaea3SApple OSS Distributions 	return error;
580*5e3eaea3SApple OSS Distributions }
581*5e3eaea3SApple OSS Distributions 
582*5e3eaea3SApple OSS Distributions errno_t
ipf_inject_output(mbuf_t data,ipfilter_t filter_ref,ipf_pktopts_t options)583*5e3eaea3SApple OSS Distributions ipf_inject_output(
584*5e3eaea3SApple OSS Distributions 	mbuf_t data,
585*5e3eaea3SApple OSS Distributions 	ipfilter_t filter_ref,
586*5e3eaea3SApple OSS Distributions 	ipf_pktopts_t options)
587*5e3eaea3SApple OSS Distributions {
588*5e3eaea3SApple OSS Distributions 	struct mbuf     *m = (struct mbuf *)data;
589*5e3eaea3SApple OSS Distributions 	u_int8_t        vers;
590*5e3eaea3SApple OSS Distributions 	errno_t         error = 0;
591*5e3eaea3SApple OSS Distributions 
592*5e3eaea3SApple OSS Distributions #if SKYWALK
593*5e3eaea3SApple OSS Distributions 	sk_protect_t protect = sk_async_transmit_protect();
594*5e3eaea3SApple OSS Distributions #endif /* SKYWALK */
595*5e3eaea3SApple OSS Distributions 
596*5e3eaea3SApple OSS Distributions 	/* Make one byte of the header contiguous in the mbuf */
597*5e3eaea3SApple OSS Distributions 	if (m->m_len < 1) {
598*5e3eaea3SApple OSS Distributions 		m = m_pullup(m, 1);
599*5e3eaea3SApple OSS Distributions 		if (m == NULL) {
600*5e3eaea3SApple OSS Distributions 			goto done;
601*5e3eaea3SApple OSS Distributions 		}
602*5e3eaea3SApple OSS Distributions 	}
603*5e3eaea3SApple OSS Distributions 
604*5e3eaea3SApple OSS Distributions 	vers = (*(u_int8_t *)m_mtod(m)) >> 4;
605*5e3eaea3SApple OSS Distributions 	switch (vers) {
606*5e3eaea3SApple OSS Distributions 	case 4:
607*5e3eaea3SApple OSS Distributions 		error = ipf_injectv4_out(data, filter_ref, options);
608*5e3eaea3SApple OSS Distributions 		break;
609*5e3eaea3SApple OSS Distributions 	case 6:
610*5e3eaea3SApple OSS Distributions 		error = ipf_injectv6_out(data, filter_ref, options);
611*5e3eaea3SApple OSS Distributions 		break;
612*5e3eaea3SApple OSS Distributions 	default:
613*5e3eaea3SApple OSS Distributions 		m_freem(m);
614*5e3eaea3SApple OSS Distributions 		error = ENOTSUP;
615*5e3eaea3SApple OSS Distributions 		break;
616*5e3eaea3SApple OSS Distributions 	}
617*5e3eaea3SApple OSS Distributions 
618*5e3eaea3SApple OSS Distributions done:
619*5e3eaea3SApple OSS Distributions #if SKYWALK
620*5e3eaea3SApple OSS Distributions 	sk_async_transmit_unprotect(protect);
621*5e3eaea3SApple OSS Distributions #endif /* SKYWALK */
622*5e3eaea3SApple OSS Distributions 
623*5e3eaea3SApple OSS Distributions 	return error;
624*5e3eaea3SApple OSS Distributions }
625*5e3eaea3SApple OSS Distributions 
626*5e3eaea3SApple OSS Distributions __private_extern__ ipfilter_t
ipf_get_inject_filter(struct mbuf * m)627*5e3eaea3SApple OSS Distributions ipf_get_inject_filter(struct mbuf *m)
628*5e3eaea3SApple OSS Distributions {
629*5e3eaea3SApple OSS Distributions 	ipfilter_t filter_ref = 0;
630*5e3eaea3SApple OSS Distributions 	struct m_tag *mtag;
631*5e3eaea3SApple OSS Distributions 
632*5e3eaea3SApple OSS Distributions 	mtag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_IPFILT);
633*5e3eaea3SApple OSS Distributions 	if (mtag) {
634*5e3eaea3SApple OSS Distributions 		filter_ref = *(ipfilter_t *)(mtag->m_tag_data);;
635*5e3eaea3SApple OSS Distributions 
636*5e3eaea3SApple OSS Distributions 		m_tag_delete(m, mtag);
637*5e3eaea3SApple OSS Distributions 	}
638*5e3eaea3SApple OSS Distributions 	return filter_ref;
639*5e3eaea3SApple OSS Distributions }
640*5e3eaea3SApple OSS Distributions 
641*5e3eaea3SApple OSS Distributions struct ipfilt_tag_container {
642*5e3eaea3SApple OSS Distributions 	struct m_tag    ipft_m_tag;
643*5e3eaea3SApple OSS Distributions 	ipfilter_t      ipft_filter_ref;
644*5e3eaea3SApple OSS Distributions };
645*5e3eaea3SApple OSS Distributions 
646*5e3eaea3SApple OSS Distributions static struct m_tag *
m_tag_kalloc_ipfilt(u_int32_t id,u_int16_t type,uint16_t len,int wait)647*5e3eaea3SApple OSS Distributions m_tag_kalloc_ipfilt(u_int32_t id, u_int16_t type, uint16_t len, int wait)
648*5e3eaea3SApple OSS Distributions {
649*5e3eaea3SApple OSS Distributions 	struct ipfilt_tag_container *tag_container;
650*5e3eaea3SApple OSS Distributions 	struct m_tag *tag = NULL;
651*5e3eaea3SApple OSS Distributions 
652*5e3eaea3SApple OSS Distributions 	assert3u(id, ==, KERNEL_MODULE_TAG_ID);
653*5e3eaea3SApple OSS Distributions 	assert3u(type, ==, KERNEL_TAG_TYPE_IPFILT);
654*5e3eaea3SApple OSS Distributions 	assert3u(len, ==, sizeof(ipfilter_t));
655*5e3eaea3SApple OSS Distributions 
656*5e3eaea3SApple OSS Distributions 	if (len != sizeof(ipfilter_t)) {
657*5e3eaea3SApple OSS Distributions 		return NULL;
658*5e3eaea3SApple OSS Distributions 	}
659*5e3eaea3SApple OSS Distributions 
660*5e3eaea3SApple OSS Distributions 	tag_container = kalloc_type(struct ipfilt_tag_container, wait | M_ZERO);
661*5e3eaea3SApple OSS Distributions 	if (tag_container != NULL) {
662*5e3eaea3SApple OSS Distributions 		tag =  &tag_container->ipft_m_tag;
663*5e3eaea3SApple OSS Distributions 
664*5e3eaea3SApple OSS Distributions 		assert3p(tag, ==, tag_container);
665*5e3eaea3SApple OSS Distributions 
666*5e3eaea3SApple OSS Distributions 		M_TAG_INIT(tag, id, type, len, &tag_container->ipft_filter_ref, NULL);
667*5e3eaea3SApple OSS Distributions 	}
668*5e3eaea3SApple OSS Distributions 
669*5e3eaea3SApple OSS Distributions 	return tag;
670*5e3eaea3SApple OSS Distributions }
671*5e3eaea3SApple OSS Distributions 
672*5e3eaea3SApple OSS Distributions static void
m_tag_kfree_ipfilt(struct m_tag * tag)673*5e3eaea3SApple OSS Distributions m_tag_kfree_ipfilt(struct m_tag *tag)
674*5e3eaea3SApple OSS Distributions {
675*5e3eaea3SApple OSS Distributions 	struct ipfilt_tag_container *tag_container = (struct ipfilt_tag_container *)tag;
676*5e3eaea3SApple OSS Distributions 
677*5e3eaea3SApple OSS Distributions 	assert3u(tag->m_tag_len, ==, sizeof(ipfilter_t));
678*5e3eaea3SApple OSS Distributions 
679*5e3eaea3SApple OSS Distributions 	kfree_type(struct ipfilt_tag_container, tag_container);
680*5e3eaea3SApple OSS Distributions }
681*5e3eaea3SApple OSS Distributions 
682*5e3eaea3SApple OSS Distributions void
ipfilter_register_m_tag(void)683*5e3eaea3SApple OSS Distributions ipfilter_register_m_tag(void)
684*5e3eaea3SApple OSS Distributions {
685*5e3eaea3SApple OSS Distributions 	int error;
686*5e3eaea3SApple OSS Distributions 
687*5e3eaea3SApple OSS Distributions 	error = m_register_internal_tag_type(KERNEL_TAG_TYPE_IPFILT, sizeof(ipfilter_t),
688*5e3eaea3SApple OSS Distributions 	    m_tag_kalloc_ipfilt, m_tag_kfree_ipfilt);
689*5e3eaea3SApple OSS Distributions 
690*5e3eaea3SApple OSS Distributions 	assert3u(error, ==, 0);
691*5e3eaea3SApple OSS Distributions }
692*5e3eaea3SApple OSS Distributions 
693*5e3eaea3SApple OSS Distributions #if SKYWALK && defined(XNU_TARGET_OS_OSX)
694*5e3eaea3SApple OSS Distributions bool
net_check_compatible_ipf(void)695*5e3eaea3SApple OSS Distributions net_check_compatible_ipf(void)
696*5e3eaea3SApple OSS Distributions {
697*5e3eaea3SApple OSS Distributions 	if (net_api_stats.nas_ipf_add_count > net_api_stats.nas_ipf_add_os_count) {
698*5e3eaea3SApple OSS Distributions 		return false;
699*5e3eaea3SApple OSS Distributions 	}
700*5e3eaea3SApple OSS Distributions 	return true;
701*5e3eaea3SApple OSS Distributions }
702*5e3eaea3SApple OSS Distributions #endif /* SKYWALK && XNU_TARGET_OS_OSX */
703