2 * Copyright (c) 2004-2018 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/param.h> /* for definition of NULL */
30 #include <sys/errno.h>
31 #include <sys/malloc.h>
32 #include <sys/socket.h>
34 #include <sys/systm.h>
35 #include <libkern/OSAtomic.h>
37 #include <machine/endian.h>
40 #include <net/if_var.h>
41 #include <net/route.h>
42 #include <net/kpi_protocol.h>
43 #include <net/net_api_stats.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/in.h>
47 #include <netinet/in_var.h>
48 #include <netinet6/in6_var.h>
49 #include <netinet/ip.h>
50 #include <netinet/ip6.h>
51 #include <netinet/ip_var.h>
52 #include <netinet6/ip6_var.h>
53 #include <netinet/kpi_ipfilter_var.h>
58 * kipf_lock and kipf_ref protect the linkage of the list of IP filters
59 * An IP filter can be removed only when kipf_ref is zero
60 * If an IP filter cannot be removed because kipf_ref is not null, then
61 * the IP filter is marjed and kipf_delayed_remove is set so that when
62 * kipf_ref eventually goes down to zero, the IP filter is removed
64 decl_lck_mtx_data(static, kipf_lock_data
);
65 static lck_mtx_t
*kipf_lock
= &kipf_lock_data
;
66 static u_int32_t kipf_ref
= 0;
67 static u_int32_t kipf_delayed_remove
= 0;
68 u_int32_t kipf_count
= 0;
70 __private_extern__
struct ipfilter_list ipv4_filters
= TAILQ_HEAD_INITIALIZER(ipv4_filters
);
71 __private_extern__
struct ipfilter_list ipv6_filters
= TAILQ_HEAD_INITIALIZER(ipv6_filters
);
72 __private_extern__
struct ipfilter_list tbr_filters
= TAILQ_HEAD_INITIALIZER(tbr_filters
);
76 extern errno_t
ipf_addv4(const struct ipf_filter
*filter
,
77 ipfilter_t
*filter_ref
);
78 extern errno_t
ipf_addv6(const struct ipf_filter
*filter
,
79 ipfilter_t
*filter_ref
);
81 static errno_t
ipf_add(const struct ipf_filter
*filter
,
82 ipfilter_t
*filter_ref
, struct ipfilter_list
*head
, bool is_internal
);
84 __private_extern__
void
87 lck_mtx_lock(kipf_lock
);
89 lck_mtx_unlock(kipf_lock
);
92 __private_extern__
void
95 lck_mtx_lock(kipf_lock
);
98 panic("ipf_unref: kipf_ref == 0\n");
101 if (kipf_ref
== 0 && kipf_delayed_remove
!= 0) {
102 struct ipfilter
*filter
;
104 while ((filter
= TAILQ_FIRST(&tbr_filters
))) {
105 VERIFY(OSDecrementAtomic64(&net_api_stats
.nas_ipf_add_count
) > 0);
107 ipf_detach_func ipf_detach
= filter
->ipf_filter
.ipf_detach
;
108 void* cookie
= filter
->ipf_filter
.cookie
;
110 TAILQ_REMOVE(filter
->ipf_head
, filter
, ipf_link
);
111 TAILQ_REMOVE(&tbr_filters
, filter
, ipf_tbr
);
112 kipf_delayed_remove
--;
115 lck_mtx_unlock(kipf_lock
);
117 lck_mtx_lock(kipf_lock
);
118 /* In case some filter got to run while we released the lock */
124 lck_mtx_unlock(kipf_lock
);
129 const struct ipf_filter
*filter
,
130 ipfilter_t
*filter_ref
,
131 struct ipfilter_list
*head
,
134 struct ipfilter
*new_filter
;
135 if (filter
->name
== NULL
|| (filter
->ipf_input
== NULL
&& filter
->ipf_output
== NULL
))
138 MALLOC(new_filter
, struct ipfilter
*, sizeof(*new_filter
), M_IFADDR
, M_WAITOK
);
139 if (new_filter
== NULL
)
142 lck_mtx_lock(kipf_lock
);
143 new_filter
->ipf_filter
= *filter
;
144 new_filter
->ipf_head
= head
;
146 TAILQ_INSERT_HEAD(head
, new_filter
, ipf_link
);
148 OSIncrementAtomic64(&net_api_stats
.nas_ipf_add_count
);
149 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_ipf_add_total
);
151 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_ipf_add_os_total
);
154 lck_mtx_unlock(kipf_lock
);
156 *filter_ref
= (ipfilter_t
)new_filter
;
158 /* This will force TCP to re-evaluate its use of TSO */
159 OSAddAtomic(1, &kipf_count
);
167 const struct ipf_filter
*filter
,
168 ipfilter_t
*filter_ref
)
170 return (ipf_add(filter
, filter_ref
, &ipv4_filters
, true));
175 const struct ipf_filter
*filter
,
176 ipfilter_t
*filter_ref
)
178 return (ipf_add(filter
, filter_ref
, &ipv4_filters
, false));
183 const struct ipf_filter
*filter
,
184 ipfilter_t
*filter_ref
)
186 return (ipf_add(filter
, filter_ref
, &ipv6_filters
, true));
191 const struct ipf_filter
*filter
,
192 ipfilter_t
*filter_ref
)
194 return (ipf_add(filter
, filter_ref
, &ipv6_filters
, false));
198 ipf_input_detached(void *cookie
, mbuf_t
*data
, int offset
, u_int8_t protocol
)
200 #pragma unused(cookie, data, offset, protocol)
203 printf("ipf_input_detached\n");
210 ipf_output_detached(void *cookie
, mbuf_t
*data
, ipf_pktopts_t options
)
212 #pragma unused(cookie, data, options)
215 printf("ipf_output_detached\n");
223 ipfilter_t filter_ref
)
225 struct ipfilter
*match
= (struct ipfilter
*)filter_ref
;
226 struct ipfilter_list
*head
;
228 if (match
== 0 || (match
->ipf_head
!= &ipv4_filters
&& match
->ipf_head
!= &ipv6_filters
))
231 head
= match
->ipf_head
;
233 lck_mtx_lock(kipf_lock
);
234 TAILQ_FOREACH(match
, head
, ipf_link
) {
235 if (match
== (struct ipfilter
*)filter_ref
) {
236 ipf_detach_func ipf_detach
= match
->ipf_filter
.ipf_detach
;
237 void* cookie
= match
->ipf_filter
.cookie
;
240 * Cannot detach when they are filters running
243 kipf_delayed_remove
++;
244 TAILQ_INSERT_TAIL(&tbr_filters
, match
, ipf_tbr
);
245 match
->ipf_filter
.ipf_input
= ipf_input_detached
;
246 match
->ipf_filter
.ipf_output
= ipf_output_detached
;
247 lck_mtx_unlock(kipf_lock
);
249 VERIFY(OSDecrementAtomic64(&net_api_stats
.nas_ipf_add_count
) > 0);
251 TAILQ_REMOVE(head
, match
, ipf_link
);
252 lck_mtx_unlock(kipf_lock
);
256 FREE(match
, M_IFADDR
);
258 /* This will force TCP to re-evaluate its use of TSO */
259 OSAddAtomic(-1, &kipf_count
);
266 lck_mtx_unlock(kipf_lock
);
276 ipfilter_t filter_ref
)
278 struct mbuf
*m
= (struct mbuf
*)data
;
279 struct m_tag
*mtag
= 0;
280 struct ip
*ip
= mtod(m
, struct ip
*);
284 protocol_family_t proto
;
286 vers
= IP_VHL_V(ip
->ip_vhl
);
300 if (filter_ref
== 0 && m
->m_pkthdr
.rcvif
== 0) {
301 m
->m_pkthdr
.rcvif
= lo_ifp
;
302 m
->m_pkthdr
.csum_data
= 0;
303 m
->m_pkthdr
.csum_flags
= 0;
305 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
307 ip
->ip_sum
= in_cksum(m
, hlen
);
310 if (filter_ref
!= 0) {
311 mtag
= m_tag_create(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFILT
,
312 sizeof (ipfilter_t
), M_NOWAIT
, m
);
317 *(ipfilter_t
*)(mtag
+1) = filter_ref
;
318 m_tag_prepend(m
, mtag
);
321 error
= proto_inject(proto
, data
);
328 ipf_injectv4_out(mbuf_t data
, ipfilter_t filter_ref
, ipf_pktopts_t options
)
332 struct mbuf
*m
= (struct mbuf
*)data
;
334 struct m_tag
*mtag
= NULL
;
335 struct ip_moptions
*imo
= NULL
;
336 struct ip_out_args ipoa
;
338 bzero(&ipoa
, sizeof(ipoa
));
339 ipoa
.ipoa_boundif
= IFSCOPE_NONE
;
340 ipoa
.ipoa_sotc
= SO_TC_UNSPEC
;
341 ipoa
.ipoa_netsvctype
= _NET_SERVICE_TYPE_UNSPEC
;
343 /* Make the IP header contiguous in the mbuf */
344 if ((size_t)m
->m_len
< sizeof (struct ip
)) {
345 m
= m_pullup(m
, sizeof (struct ip
));
349 ip
= (struct ip
*)m_mtod(m
);
351 if (filter_ref
!= 0) {
352 mtag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
353 KERNEL_TAG_TYPE_IPFILT
, sizeof (ipfilter_t
), M_NOWAIT
, m
);
358 *(ipfilter_t
*)(mtag
+ 1) = filter_ref
;
359 m_tag_prepend(m
, mtag
);
362 if (options
!= NULL
&& (options
->ippo_flags
& IPPOF_MCAST_OPTS
) &&
363 (imo
= ip_allocmoptions(M_DONTWAIT
)) != NULL
) {
364 imo
->imo_multicast_ifp
= options
->ippo_mcast_ifnet
;
365 imo
->imo_multicast_ttl
= options
->ippo_mcast_ttl
;
366 imo
->imo_multicast_loop
= options
->ippo_mcast_loop
;
369 if (options
!= NULL
) {
370 if (options
->ippo_flags
& IPPOF_SELECT_SRCIF
)
371 ipoa
.ipoa_flags
|= IPOAF_SELECT_SRCIF
;
372 if (options
->ippo_flags
& IPPOF_BOUND_IF
) {
373 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
374 ipoa
.ipoa_boundif
= options
->ippo_flags
>>
377 if (options
->ippo_flags
& IPPOF_NO_IFT_CELLULAR
)
378 ipoa
.ipoa_flags
|= IPOAF_NO_CELLULAR
;
379 if (options
->ippo_flags
& IPPOF_BOUND_SRCADDR
)
380 ipoa
.ipoa_flags
|= IPOAF_BOUND_SRCADDR
;
381 if (options
->ippo_flags
& IPPOF_NO_IFF_EXPENSIVE
)
382 ipoa
.ipoa_flags
|= IPOAF_NO_EXPENSIVE
;
385 bzero(&ro
, sizeof(struct route
));
387 /* Put ip_len and ip_off in host byte order, ip_output expects that */
389 #if BYTE_ORDER != BIG_ENDIAN
394 /* Send; enforce source interface selection via IP_OUTARGS flag */
395 error
= ip_output(m
, NULL
, &ro
,
396 IP_ALLOWBROADCAST
| IP_RAWOUTPUT
| IP_OUTARGS
, imo
, &ipoa
);
398 /* Release the route */
409 ipf_injectv6_out(mbuf_t data
, ipfilter_t filter_ref
, ipf_pktopts_t options
)
413 struct mbuf
*m
= (struct mbuf
*)data
;
415 struct m_tag
*mtag
= NULL
;
416 struct ip6_moptions
*im6o
= NULL
;
417 struct ip6_out_args ip6oa
;
419 bzero(&ip6oa
, sizeof(ip6oa
));
420 ip6oa
.ip6oa_boundif
= IFSCOPE_NONE
;
421 ip6oa
.ip6oa_sotc
= SO_TC_UNSPEC
;
422 ip6oa
.ip6oa_netsvctype
= _NET_SERVICE_TYPE_UNSPEC
;
424 /* Make the IP header contiguous in the mbuf */
425 if ((size_t)m
->m_len
< sizeof(struct ip6_hdr
)) {
426 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
430 ip6
= (struct ip6_hdr
*)m_mtod(m
);
432 if (filter_ref
!= 0) {
433 mtag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
434 KERNEL_TAG_TYPE_IPFILT
, sizeof (ipfilter_t
), M_NOWAIT
, m
);
439 *(ipfilter_t
*)(mtag
+ 1) = filter_ref
;
440 m_tag_prepend(m
, mtag
);
443 if (options
!= NULL
&& (options
->ippo_flags
& IPPOF_MCAST_OPTS
) &&
444 (im6o
= ip6_allocmoptions(M_DONTWAIT
)) != NULL
) {
445 im6o
->im6o_multicast_ifp
= options
->ippo_mcast_ifnet
;
446 im6o
->im6o_multicast_hlim
= options
->ippo_mcast_ttl
;
447 im6o
->im6o_multicast_loop
= options
->ippo_mcast_loop
;
450 if (options
!= NULL
) {
451 if (options
->ippo_flags
& IPPOF_SELECT_SRCIF
)
452 ip6oa
.ip6oa_flags
|= IP6OAF_SELECT_SRCIF
;
453 if (options
->ippo_flags
& IPPOF_BOUND_IF
) {
454 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
455 ip6oa
.ip6oa_boundif
= options
->ippo_flags
>>
458 if (options
->ippo_flags
& IPPOF_NO_IFT_CELLULAR
)
459 ip6oa
.ip6oa_flags
|= IP6OAF_NO_CELLULAR
;
460 if (options
->ippo_flags
& IPPOF_BOUND_SRCADDR
)
461 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_SRCADDR
;
462 if (options
->ippo_flags
& IPPOF_NO_IFF_EXPENSIVE
)
463 ip6oa
.ip6oa_flags
|= IP6OAF_NO_EXPENSIVE
;
466 bzero(&ro
, sizeof(struct route_in6
));
469 * Send mbuf and ifscope information. Check for correctness
470 * of ifscope information is done while searching for a route in
473 error
= ip6_output(m
, NULL
, &ro
, IPV6_OUTARGS
, im6o
, NULL
, &ip6oa
);
475 /* Release the route */
488 ipfilter_t filter_ref
,
489 ipf_pktopts_t options
)
491 struct mbuf
*m
= (struct mbuf
*)data
;
495 /* Make one byte of the header contiguous in the mbuf */
502 vers
= (*(u_int8_t
*)m_mtod(m
)) >> 4;
505 error
= ipf_injectv4_out(data
, filter_ref
, options
);
509 error
= ipf_injectv6_out(data
, filter_ref
, options
);
522 __private_extern__ ipfilter_t
523 ipf_get_inject_filter(struct mbuf
*m
)
525 ipfilter_t filter_ref
= 0;
528 mtag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFILT
, NULL
);
530 filter_ref
= *(ipfilter_t
*)(mtag
+1);
532 m_tag_delete(m
, mtag
);
537 __private_extern__
int
541 lck_grp_attr_t
*grp_attributes
= 0;
542 lck_attr_t
*lck_attributes
= 0;
543 lck_grp_t
*lck_grp
= 0;
545 grp_attributes
= lck_grp_attr_alloc_init();
546 if (grp_attributes
== 0) {
547 printf("ipf_init: lck_grp_attr_alloc_init failed\n");
552 lck_grp
= lck_grp_alloc_init("IP Filter", grp_attributes
);
554 printf("ipf_init: lck_grp_alloc_init failed\n");
559 lck_attributes
= lck_attr_alloc_init();
560 if (lck_attributes
== 0) {
561 printf("ipf_init: lck_attr_alloc_init failed\n");
566 lck_mtx_init(kipf_lock
, lck_grp
, lck_attributes
);
570 lck_grp_free(lck_grp
);
573 if (grp_attributes
) {
574 lck_grp_attr_free(grp_attributes
);
577 if (lck_attributes
) {
578 lck_attr_free(lck_attributes
);