2 * Copyright (c) 2003 Apple Computer, 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>
37 #include <net/if_var.h>
38 #include <net/route.h>
39 #include <net/kpi_protocol.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/in.h>
43 #include <netinet/in_var.h>
44 #include <netinet6/in6_var.h>
45 #include <netinet/ip.h>
46 #include <netinet/ip6.h>
47 #include <netinet/ip_var.h>
48 #include <netinet6/ip6_var.h>
49 #include <netinet/kpi_ipfilter_var.h>
52 * kipf_lock and kipf_ref protect the linkage of the list of IP filters
53 * An IP filter can be removed only when kipf_ref is zero
54 * If an IP filter cannot be removed because kipf_ref is not null, then
55 * the IP filter is marjed and kipf_delayed_remove is set so that when
56 * kipf_ref eventually goes down to zero, the IP filter is removed
58 static lck_mtx_t
*kipf_lock
= 0;
59 static unsigned long kipf_ref
= 0;
60 static unsigned long kipf_delayed_remove
= 0;
62 __private_extern__
struct ipfilter_list ipv4_filters
= TAILQ_HEAD_INITIALIZER(ipv4_filters
);
63 __private_extern__
struct ipfilter_list ipv6_filters
= TAILQ_HEAD_INITIALIZER(ipv6_filters
);
64 __private_extern__
struct ipfilter_list tbr_filters
= TAILQ_HEAD_INITIALIZER(tbr_filters
);
66 __private_extern__
void
69 lck_mtx_lock(kipf_lock
);
71 lck_mtx_unlock(kipf_lock
);
74 __private_extern__
void
77 lck_mtx_lock(kipf_lock
);
80 panic("ipf_unref: kipf_ref == 0\n");
83 if (kipf_ref
== 0 && kipf_delayed_remove
!= 0) {
84 struct ipfilter
*filter
;
86 while ((filter
= TAILQ_FIRST(&tbr_filters
))) {
87 ipf_detach_func ipf_detach
= filter
->ipf_filter
.ipf_detach
;
88 void* cookie
= filter
->ipf_filter
.cookie
;
90 TAILQ_REMOVE(filter
->ipf_head
, filter
, ipf_link
);
91 TAILQ_REMOVE(&tbr_filters
, filter
, ipf_tbr
);
92 kipf_delayed_remove
--;
95 lck_mtx_unlock(kipf_lock
);
97 lck_mtx_lock(kipf_lock
);
98 /* In case some filter got to run while we released the lock */
104 lck_mtx_unlock(kipf_lock
);
109 const struct ipf_filter
* filter
,
110 ipfilter_t
*filter_ref
,
111 struct ipfilter_list
*head
)
113 struct ipfilter
*new_filter
;
114 if (filter
->name
== NULL
|| (filter
->ipf_input
== NULL
&& filter
->ipf_output
== NULL
))
117 MALLOC(new_filter
, struct ipfilter
*, sizeof(*new_filter
), M_IFADDR
, M_WAITOK
);
118 if (new_filter
== NULL
)
121 lck_mtx_lock(kipf_lock
);
122 new_filter
->ipf_filter
= *filter
;
123 new_filter
->ipf_head
= head
;
127 * Make sure third parties have a chance to filter packets before
128 * SharedIP. Always SharedIP at the end of the list.
130 if (filter
->name
!= NULL
&&
131 strcmp(filter
->name
, "com.apple.nke.SharedIP") == 0) {
132 TAILQ_INSERT_TAIL(head
, new_filter
, ipf_link
);
135 TAILQ_INSERT_HEAD(head
, new_filter
, ipf_link
);
138 lck_mtx_unlock(kipf_lock
);
140 *filter_ref
= (ipfilter_t
)new_filter
;
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
);
162 ipfilter_t filter_ref
)
164 struct ipfilter
*match
= (struct ipfilter
*)filter_ref
;
165 struct ipfilter_list
*head
;
167 if (match
== 0 || (match
->ipf_head
!= &ipv4_filters
&& match
->ipf_head
!= &ipv6_filters
))
170 head
= match
->ipf_head
;
172 lck_mtx_lock(kipf_lock
);
173 TAILQ_FOREACH(match
, head
, ipf_link
) {
174 if (match
== (struct ipfilter
*)filter_ref
) {
175 ipf_detach_func ipf_detach
= match
->ipf_filter
.ipf_detach
;
176 void* cookie
= match
->ipf_filter
.cookie
;
179 * Cannot detach when they are filters running
182 kipf_delayed_remove
++;
183 TAILQ_INSERT_TAIL(&tbr_filters
, match
, ipf_tbr
);
184 match
->ipf_filter
.ipf_input
= 0;
185 match
->ipf_filter
.ipf_output
= 0;
186 lck_mtx_unlock(kipf_lock
);
188 TAILQ_REMOVE(head
, match
, ipf_link
);
189 lck_mtx_unlock(kipf_lock
);
192 FREE(match
, M_IFADDR
);
197 lck_mtx_unlock(kipf_lock
);
207 ipfilter_t filter_ref
)
209 struct mbuf
*m
= (struct mbuf
*)data
;
210 struct m_tag
*mtag
= 0;
211 struct ip
*ip
= mtod(m
, struct ip
*);
215 protocol_family_t proto
;
217 vers
= IP_VHL_V(ip
->ip_vhl
);
231 if (filter_ref
== 0 && m
->m_pkthdr
.rcvif
== 0) {
232 m
->m_pkthdr
.rcvif
= ifunit("lo0");
233 m
->m_pkthdr
.csum_data
= 0;
234 m
->m_pkthdr
.csum_flags
= 0;
236 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
238 ip
->ip_sum
= in_cksum(m
, hlen
);
241 if (filter_ref
!= 0) {
242 mtag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFILT
,
243 sizeof (ipfilter_t
), M_NOWAIT
);
248 *(ipfilter_t
*)(mtag
+1) = filter_ref
;
249 m_tag_prepend(m
, mtag
);
252 error
= proto_inject(proto
, data
);
261 ipfilter_t filter_ref
,
262 ipf_pktopts_t options
)
265 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&ro
.ro_dst
;
267 struct mbuf
*m
= (struct mbuf
*)data
;
269 struct m_tag
*mtag
= 0;
270 struct ip_moptions
*imo
= 0, ip_moptions
;
272 /* Make the IP header contiguous in the mbuf */
273 if ((size_t)m
->m_len
< sizeof(struct ip
)) {
274 m
= m_pullup(m
, sizeof(struct ip
));
275 if (m
== NULL
) return ENOMEM
;
277 ip
= (struct ip
*)m_mtod(m
);
279 if (filter_ref
!= 0) {
280 mtag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFILT
,
281 sizeof (ipfilter_t
), M_NOWAIT
);
286 *(ipfilter_t
*)(mtag
+1) = filter_ref
;
287 m_tag_prepend(m
, mtag
);
290 if (options
&& (options
->ippo_flags
& IPPOF_MCAST_OPTS
)) {
293 bzero(imo
, sizeof(struct ip6_moptions
));
294 imo
->imo_multicast_ifp
= options
->ippo_mcast_ifnet
;
295 imo
->imo_multicast_ttl
= options
->ippo_mcast_ttl
;
296 imo
->imo_multicast_loop
= options
->ippo_mcast_loop
;
299 /* Fill out a route structure and get a route */
300 bzero(&ro
, sizeof(struct route
));
301 sin
->sin_len
= sizeof(struct sockaddr_in
);
302 sin
->sin_family
= AF_INET
;
304 sin
->sin_addr
= ip
->ip_dst
;
306 if (ro
.ro_rt
== NULL
) {
311 /* Put ip_len and ip_off in host byte order, ip_output expects that */
316 error
= ip_output(m
, NULL
, &ro
, IP_ALLOWBROADCAST
| IP_RAWOUTPUT
, imo
, NULL
);
318 /* Release the route */
331 ipfilter_t filter_ref
,
332 ipf_pktopts_t options
)
335 struct sockaddr_in6
*sin6
= &ro
.ro_dst
;
337 struct mbuf
*m
= (struct mbuf
*)data
;
339 struct m_tag
*mtag
= 0;
340 struct ip6_moptions
*im6o
= 0, ip6_moptions
;
342 /* Make the IP header contiguous in the mbuf */
343 if ((size_t)m
->m_len
< sizeof(struct ip6_hdr
)) {
344 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
345 if (m
== NULL
) return ENOMEM
;
347 ip6
= (struct ip6_hdr
*)m_mtod(m
);
349 if (filter_ref
!= 0) {
350 mtag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFILT
,
351 sizeof (ipfilter_t
), M_NOWAIT
);
356 *(ipfilter_t
*)(mtag
+1) = filter_ref
;
357 m_tag_prepend(m
, mtag
);
360 if (options
&& (options
->ippo_flags
& IPPOF_MCAST_OPTS
)) {
361 im6o
= &ip6_moptions
;
363 bzero(im6o
, sizeof(struct ip6_moptions
));
364 im6o
->im6o_multicast_ifp
= options
->ippo_mcast_ifnet
;
365 im6o
->im6o_multicast_hlim
= options
->ippo_mcast_ttl
;
366 im6o
->im6o_multicast_loop
= options
->ippo_mcast_loop
;
370 /* Fill out a route structure and get a route */
371 bzero(&ro
, sizeof(struct route_in6
));
372 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
373 sin6
->sin6_family
= AF_INET6
;
374 sin6
->sin6_addr
= ip6
->ip6_dst
;
376 /* This is breaks loopback multicast! */
377 /* The scope ID should already at s6_addr16[1] */
378 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
379 /* Hack, pull the scope_id out of the dest addr */
380 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
381 ip6
->ip6_dst
.s6_addr16
[1] = 0;
383 sin6
->sin6_scope_id
= 0;
385 rtalloc((struct route
*)&ro
);
386 if (ro
.ro_rt
== NULL
) {
392 error
= ip6_output(m
, NULL
, &ro
, 0, im6o
, NULL
, 0);
394 /* Release the route */
407 ipfilter_t filter_ref
,
408 ipf_pktopts_t options
)
410 struct mbuf
*m
= (struct mbuf
*)data
;
414 /* Make one byte of the header contiguous in the mbuf */
421 vers
= (*(u_int8_t
*)m_mtod(m
)) >> 4;
425 error
= ipf_injectv4_out(data
, filter_ref
, options
);
429 error
= ipf_injectv6_out(data
, filter_ref
, options
);
442 __private_extern__ ipfilter_t
443 ipf_get_inject_filter(struct mbuf
*m
)
445 ipfilter_t filter_ref
= 0;
448 mtag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFILT
, NULL
);
450 filter_ref
= *(ipfilter_t
*)(mtag
+1);
452 m_tag_delete(m
, mtag
);
457 __private_extern__
int
461 lck_grp_attr_t
*grp_attributes
= 0;
462 lck_attr_t
*lck_attributes
= 0;
463 lck_grp_t
*lck_grp
= 0;
465 grp_attributes
= lck_grp_attr_alloc_init();
466 if (grp_attributes
== 0) {
467 printf("ipf_init: lck_grp_attr_alloc_init failed\n");
472 lck_grp
= lck_grp_alloc_init("IP Filter", grp_attributes
);
474 printf("ipf_init: lck_grp_alloc_init failed\n");
479 lck_attributes
= lck_attr_alloc_init();
480 if (lck_attributes
== 0) {
481 printf("ipf_init: lck_attr_alloc_init failed\n");
486 kipf_lock
= lck_mtx_alloc_init(lck_grp
, lck_attributes
);
487 if (kipf_lock
== 0) {
488 printf("ipf_init: lck_mtx_alloc_init failed\n");
495 lck_mtx_free(kipf_lock
, lck_grp
);
500 lck_grp_free(lck_grp
);
503 if (grp_attributes
) {
504 lck_grp_attr_free(grp_attributes
);
507 if (lck_attributes
) {
508 lck_attr_free(lck_attributes
);