2 * Copyright (c) 2004-2014 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>
44 #include <netinet/in_systm.h>
45 #include <netinet/in.h>
46 #include <netinet/in_var.h>
47 #include <netinet6/in6_var.h>
48 #include <netinet/ip.h>
49 #include <netinet/ip6.h>
50 #include <netinet/ip_var.h>
51 #include <netinet6/ip6_var.h>
52 #include <netinet/kpi_ipfilter_var.h>
56 * kipf_lock and kipf_ref protect the linkage of the list of IP filters
57 * An IP filter can be removed only when kipf_ref is zero
58 * If an IP filter cannot be removed because kipf_ref is not null, then
59 * the IP filter is marjed and kipf_delayed_remove is set so that when
60 * kipf_ref eventually goes down to zero, the IP filter is removed
62 decl_lck_mtx_data(static, kipf_lock_data
);
63 static lck_mtx_t
*kipf_lock
= &kipf_lock_data
;
64 static u_int32_t kipf_ref
= 0;
65 static u_int32_t kipf_delayed_remove
= 0;
66 u_int32_t kipf_count
= 0;
68 __private_extern__
struct ipfilter_list ipv4_filters
= TAILQ_HEAD_INITIALIZER(ipv4_filters
);
69 __private_extern__
struct ipfilter_list ipv6_filters
= TAILQ_HEAD_INITIALIZER(ipv6_filters
);
70 __private_extern__
struct ipfilter_list tbr_filters
= TAILQ_HEAD_INITIALIZER(tbr_filters
);
72 __private_extern__
void
75 lck_mtx_lock(kipf_lock
);
77 lck_mtx_unlock(kipf_lock
);
80 __private_extern__
void
83 lck_mtx_lock(kipf_lock
);
86 panic("ipf_unref: kipf_ref == 0\n");
89 if (kipf_ref
== 0 && kipf_delayed_remove
!= 0) {
90 struct ipfilter
*filter
;
92 while ((filter
= TAILQ_FIRST(&tbr_filters
))) {
93 ipf_detach_func ipf_detach
= filter
->ipf_filter
.ipf_detach
;
94 void* cookie
= filter
->ipf_filter
.cookie
;
96 TAILQ_REMOVE(filter
->ipf_head
, filter
, ipf_link
);
97 TAILQ_REMOVE(&tbr_filters
, filter
, ipf_tbr
);
98 kipf_delayed_remove
--;
101 lck_mtx_unlock(kipf_lock
);
103 lck_mtx_lock(kipf_lock
);
104 /* In case some filter got to run while we released the lock */
110 lck_mtx_unlock(kipf_lock
);
115 const struct ipf_filter
* filter
,
116 ipfilter_t
*filter_ref
,
117 struct ipfilter_list
*head
)
119 struct ipfilter
*new_filter
;
120 if (filter
->name
== NULL
|| (filter
->ipf_input
== NULL
&& filter
->ipf_output
== NULL
))
123 MALLOC(new_filter
, struct ipfilter
*, sizeof(*new_filter
), M_IFADDR
, M_WAITOK
);
124 if (new_filter
== NULL
)
127 lck_mtx_lock(kipf_lock
);
128 new_filter
->ipf_filter
= *filter
;
129 new_filter
->ipf_head
= head
;
131 TAILQ_INSERT_HEAD(head
, new_filter
, ipf_link
);
133 lck_mtx_unlock(kipf_lock
);
135 *filter_ref
= (ipfilter_t
)new_filter
;
137 /* This will force TCP to re-evaluate its use of TSO */
138 OSAddAtomic(1, &kipf_count
);
146 const struct ipf_filter
* filter
,
147 ipfilter_t
*filter_ref
)
149 return ipf_add(filter
, filter_ref
, &ipv4_filters
);
154 const struct ipf_filter
* filter
,
155 ipfilter_t
*filter_ref
)
157 return ipf_add(filter
, filter_ref
, &ipv6_filters
);
161 ipf_input_detached(void *cookie
, mbuf_t
*data
, int offset
, u_int8_t protocol
)
163 #pragma unused(cookie, data, offset, protocol)
166 printf("ipf_input_detached\n");
173 ipf_output_detached(void *cookie
, mbuf_t
*data
, ipf_pktopts_t options
)
175 #pragma unused(cookie, data, options)
178 printf("ipf_output_detached\n");
186 ipfilter_t filter_ref
)
188 struct ipfilter
*match
= (struct ipfilter
*)filter_ref
;
189 struct ipfilter_list
*head
;
191 if (match
== 0 || (match
->ipf_head
!= &ipv4_filters
&& match
->ipf_head
!= &ipv6_filters
))
194 head
= match
->ipf_head
;
196 lck_mtx_lock(kipf_lock
);
197 TAILQ_FOREACH(match
, head
, ipf_link
) {
198 if (match
== (struct ipfilter
*)filter_ref
) {
199 ipf_detach_func ipf_detach
= match
->ipf_filter
.ipf_detach
;
200 void* cookie
= match
->ipf_filter
.cookie
;
203 * Cannot detach when they are filters running
206 kipf_delayed_remove
++;
207 TAILQ_INSERT_TAIL(&tbr_filters
, match
, ipf_tbr
);
208 match
->ipf_filter
.ipf_input
= ipf_input_detached
;
209 match
->ipf_filter
.ipf_output
= ipf_output_detached
;
210 lck_mtx_unlock(kipf_lock
);
212 TAILQ_REMOVE(head
, match
, ipf_link
);
213 lck_mtx_unlock(kipf_lock
);
216 FREE(match
, M_IFADDR
);
218 /* This will force TCP to re-evaluate its use of TSO */
219 OSAddAtomic(-1, &kipf_count
);
226 lck_mtx_unlock(kipf_lock
);
236 ipfilter_t filter_ref
)
238 struct mbuf
*m
= (struct mbuf
*)data
;
239 struct m_tag
*mtag
= 0;
240 struct ip
*ip
= mtod(m
, struct ip
*);
244 protocol_family_t proto
;
246 vers
= IP_VHL_V(ip
->ip_vhl
);
260 if (filter_ref
== 0 && m
->m_pkthdr
.rcvif
== 0) {
261 m
->m_pkthdr
.rcvif
= lo_ifp
;
262 m
->m_pkthdr
.csum_data
= 0;
263 m
->m_pkthdr
.csum_flags
= 0;
265 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
267 ip
->ip_sum
= in_cksum(m
, hlen
);
270 if (filter_ref
!= 0) {
271 mtag
= m_tag_create(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFILT
,
272 sizeof (ipfilter_t
), M_NOWAIT
, m
);
277 *(ipfilter_t
*)(mtag
+1) = filter_ref
;
278 m_tag_prepend(m
, mtag
);
281 error
= proto_inject(proto
, data
);
288 ipf_injectv4_out(mbuf_t data
, ipfilter_t filter_ref
, ipf_pktopts_t options
)
292 struct mbuf
*m
= (struct mbuf
*)data
;
294 struct m_tag
*mtag
= NULL
;
295 struct ip_moptions
*imo
= NULL
;
296 struct ip_out_args ipoa
= { IFSCOPE_NONE
, { 0 }, 0, 0 };
298 /* Make the IP header contiguous in the mbuf */
299 if ((size_t)m
->m_len
< sizeof (struct ip
)) {
300 m
= m_pullup(m
, sizeof (struct ip
));
304 ip
= (struct ip
*)m_mtod(m
);
306 if (filter_ref
!= 0) {
307 mtag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
308 KERNEL_TAG_TYPE_IPFILT
, sizeof (ipfilter_t
), M_NOWAIT
, m
);
313 *(ipfilter_t
*)(mtag
+ 1) = filter_ref
;
314 m_tag_prepend(m
, mtag
);
317 if (options
!= NULL
&& (options
->ippo_flags
& IPPOF_MCAST_OPTS
) &&
318 (imo
= ip_allocmoptions(M_DONTWAIT
)) != NULL
) {
319 imo
->imo_multicast_ifp
= options
->ippo_mcast_ifnet
;
320 imo
->imo_multicast_ttl
= options
->ippo_mcast_ttl
;
321 imo
->imo_multicast_loop
= options
->ippo_mcast_loop
;
324 if (options
!= NULL
) {
325 if (options
->ippo_flags
& IPPOF_SELECT_SRCIF
)
326 ipoa
.ipoa_flags
|= IPOAF_SELECT_SRCIF
;
327 if (options
->ippo_flags
& IPPOF_BOUND_IF
) {
328 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
329 ipoa
.ipoa_boundif
= options
->ippo_flags
>>
332 if (options
->ippo_flags
& IPPOF_NO_IFT_CELLULAR
)
333 ipoa
.ipoa_flags
|= IPOAF_NO_CELLULAR
;
334 if (options
->ippo_flags
& IPPOF_BOUND_SRCADDR
)
335 ipoa
.ipoa_flags
|= IPOAF_BOUND_SRCADDR
;
336 if (options
->ippo_flags
& IPPOF_NO_IFF_EXPENSIVE
)
337 ipoa
.ipoa_flags
|= IPOAF_NO_EXPENSIVE
;
340 bzero(&ro
, sizeof(struct route
));
342 /* Put ip_len and ip_off in host byte order, ip_output expects that */
344 #if BYTE_ORDER != BIG_ENDIAN
349 /* Send; enforce source interface selection via IP_OUTARGS flag */
350 error
= ip_output(m
, NULL
, &ro
,
351 IP_ALLOWBROADCAST
| IP_RAWOUTPUT
| IP_OUTARGS
, imo
, &ipoa
);
353 /* Release the route */
364 ipf_injectv6_out(mbuf_t data
, ipfilter_t filter_ref
, ipf_pktopts_t options
)
368 struct mbuf
*m
= (struct mbuf
*)data
;
370 struct m_tag
*mtag
= NULL
;
371 struct ip6_moptions
*im6o
= NULL
;
372 struct ip6_out_args ip6oa
= { IFSCOPE_NONE
, { 0 }, 0, 0 };
374 /* Make the IP header contiguous in the mbuf */
375 if ((size_t)m
->m_len
< sizeof(struct ip6_hdr
)) {
376 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
380 ip6
= (struct ip6_hdr
*)m_mtod(m
);
382 if (filter_ref
!= 0) {
383 mtag
= m_tag_create(KERNEL_MODULE_TAG_ID
,
384 KERNEL_TAG_TYPE_IPFILT
, sizeof (ipfilter_t
), M_NOWAIT
, m
);
389 *(ipfilter_t
*)(mtag
+ 1) = filter_ref
;
390 m_tag_prepend(m
, mtag
);
393 if (options
!= NULL
&& (options
->ippo_flags
& IPPOF_MCAST_OPTS
) &&
394 (im6o
= ip6_allocmoptions(M_DONTWAIT
)) != NULL
) {
395 im6o
->im6o_multicast_ifp
= options
->ippo_mcast_ifnet
;
396 im6o
->im6o_multicast_hlim
= options
->ippo_mcast_ttl
;
397 im6o
->im6o_multicast_loop
= options
->ippo_mcast_loop
;
400 if (options
!= NULL
) {
401 if (options
->ippo_flags
& IPPOF_SELECT_SRCIF
)
402 ip6oa
.ip6oa_flags
|= IP6OAF_SELECT_SRCIF
;
403 if (options
->ippo_flags
& IPPOF_BOUND_IF
) {
404 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
405 ip6oa
.ip6oa_boundif
= options
->ippo_flags
>>
408 if (options
->ippo_flags
& IPPOF_NO_IFT_CELLULAR
)
409 ip6oa
.ip6oa_flags
|= IP6OAF_NO_CELLULAR
;
410 if (options
->ippo_flags
& IPPOF_BOUND_SRCADDR
)
411 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_SRCADDR
;
412 if (options
->ippo_flags
& IPPOF_NO_IFF_EXPENSIVE
)
413 ip6oa
.ip6oa_flags
|= IP6OAF_NO_EXPENSIVE
;
416 bzero(&ro
, sizeof(struct route_in6
));
419 * Send mbuf and ifscope information. Check for correctness
420 * of ifscope information is done while searching for a route in
423 error
= ip6_output(m
, NULL
, &ro
, IPV6_OUTARGS
, im6o
, NULL
, &ip6oa
);
425 /* Release the route */
438 ipfilter_t filter_ref
,
439 ipf_pktopts_t options
)
441 struct mbuf
*m
= (struct mbuf
*)data
;
445 /* Make one byte of the header contiguous in the mbuf */
452 vers
= (*(u_int8_t
*)m_mtod(m
)) >> 4;
456 error
= ipf_injectv4_out(data
, filter_ref
, options
);
460 error
= ipf_injectv6_out(data
, filter_ref
, options
);
473 __private_extern__ ipfilter_t
474 ipf_get_inject_filter(struct mbuf
*m
)
476 ipfilter_t filter_ref
= 0;
479 mtag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFILT
, NULL
);
481 filter_ref
= *(ipfilter_t
*)(mtag
+1);
483 m_tag_delete(m
, mtag
);
488 __private_extern__
int
492 lck_grp_attr_t
*grp_attributes
= 0;
493 lck_attr_t
*lck_attributes
= 0;
494 lck_grp_t
*lck_grp
= 0;
496 grp_attributes
= lck_grp_attr_alloc_init();
497 if (grp_attributes
== 0) {
498 printf("ipf_init: lck_grp_attr_alloc_init failed\n");
503 lck_grp
= lck_grp_alloc_init("IP Filter", grp_attributes
);
505 printf("ipf_init: lck_grp_alloc_init failed\n");
510 lck_attributes
= lck_attr_alloc_init();
511 if (lck_attributes
== 0) {
512 printf("ipf_init: lck_attr_alloc_init failed\n");
517 lck_mtx_init(kipf_lock
, lck_grp
, lck_attributes
);
521 lck_grp_free(lck_grp
);
524 if (grp_attributes
) {
525 lck_grp_attr_free(grp_attributes
);
528 if (lck_attributes
) {
529 lck_attr_free(lck_attributes
);