2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/param.h> /* for definition of NULL */
25 #include <sys/errno.h>
26 #include <sys/malloc.h>
27 #include <sys/socket.h>
29 #include <sys/systm.h>
32 #include <net/if_var.h>
33 #include <net/route.h>
34 #include <net/kpi_protocol.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/in.h>
38 #include <netinet/in_var.h>
39 #include <netinet6/in6_var.h>
40 #include <netinet/ip.h>
41 #include <netinet/ip6.h>
42 #include <netinet/ip_var.h>
43 #include <netinet6/ip6_var.h>
44 #include <netinet/kpi_ipfilter_var.h>
47 * kipf_lock and kipf_ref protect the linkage of the list of IP filters
48 * An IP filter can be removed only when kipf_ref is zero
49 * If an IP filter cannot be removed because kipf_ref is not null, then
50 * the IP filter is marjed and kipf_delayed_remove is set so that when
51 * kipf_ref eventually goes down to zero, the IP filter is removed
53 static lck_mtx_t
*kipf_lock
= 0;
54 static unsigned long kipf_ref
= 0;
55 static unsigned long kipf_delayed_remove
= 0;
57 __private_extern__
struct ipfilter_list ipv4_filters
= TAILQ_HEAD_INITIALIZER(ipv4_filters
);
58 __private_extern__
struct ipfilter_list ipv6_filters
= TAILQ_HEAD_INITIALIZER(ipv6_filters
);
59 __private_extern__
struct ipfilter_list tbr_filters
= TAILQ_HEAD_INITIALIZER(tbr_filters
);
61 __private_extern__
void
64 lck_mtx_lock(kipf_lock
);
66 lck_mtx_unlock(kipf_lock
);
69 __private_extern__
void
72 lck_mtx_lock(kipf_lock
);
75 panic("ipf_unref: kipf_ref == 0\n");
78 if (kipf_ref
== 0 && kipf_delayed_remove
!= 0) {
79 struct ipfilter
*filter
;
81 while ((filter
= TAILQ_FIRST(&tbr_filters
))) {
82 ipf_detach_func ipf_detach
= filter
->ipf_filter
.ipf_detach
;
83 void* cookie
= filter
->ipf_filter
.cookie
;
85 TAILQ_REMOVE(filter
->ipf_head
, filter
, ipf_link
);
86 TAILQ_REMOVE(&tbr_filters
, filter
, ipf_tbr
);
87 kipf_delayed_remove
--;
90 lck_mtx_unlock(kipf_lock
);
92 lck_mtx_lock(kipf_lock
);
93 /* In case some filter got to run while we released the lock */
99 lck_mtx_unlock(kipf_lock
);
104 const struct ipf_filter
* filter
,
105 ipfilter_t
*filter_ref
,
106 struct ipfilter_list
*head
)
108 struct ipfilter
*new_filter
;
109 if (filter
->name
== NULL
|| (filter
->ipf_input
== NULL
&& filter
->ipf_output
== NULL
))
112 MALLOC(new_filter
, struct ipfilter
*, sizeof(*new_filter
), M_IFADDR
, M_WAITOK
);
113 if (new_filter
== NULL
)
116 lck_mtx_lock(kipf_lock
);
117 new_filter
->ipf_filter
= *filter
;
118 new_filter
->ipf_head
= head
;
122 * Make sure third parties have a chance to filter packets before
123 * SharedIP. Always SharedIP at the end of the list.
125 if (filter
->name
!= NULL
&&
126 strcmp(filter
->name
, "com.apple.nke.SharedIP") == 0) {
127 TAILQ_INSERT_TAIL(head
, new_filter
, ipf_link
);
130 TAILQ_INSERT_HEAD(head
, new_filter
, ipf_link
);
133 lck_mtx_unlock(kipf_lock
);
135 *filter_ref
= (ipfilter_t
)new_filter
;
141 const struct ipf_filter
* filter
,
142 ipfilter_t
*filter_ref
)
144 return ipf_add(filter
, filter_ref
, &ipv4_filters
);
149 const struct ipf_filter
* filter
,
150 ipfilter_t
*filter_ref
)
152 return ipf_add(filter
, filter_ref
, &ipv6_filters
);
157 ipfilter_t filter_ref
)
159 struct ipfilter
*match
= (struct ipfilter
*)filter_ref
;
160 struct ipfilter_list
*head
;
162 if (match
== 0 || (match
->ipf_head
!= &ipv4_filters
&& match
->ipf_head
!= &ipv6_filters
))
165 head
= match
->ipf_head
;
167 lck_mtx_lock(kipf_lock
);
168 TAILQ_FOREACH(match
, head
, ipf_link
) {
169 if (match
== (struct ipfilter
*)filter_ref
) {
170 ipf_detach_func ipf_detach
= match
->ipf_filter
.ipf_detach
;
171 void* cookie
= match
->ipf_filter
.cookie
;
174 * Cannot detach when they are filters running
177 kipf_delayed_remove
++;
178 TAILQ_INSERT_TAIL(&tbr_filters
, match
, ipf_tbr
);
179 match
->ipf_filter
.ipf_input
= 0;
180 match
->ipf_filter
.ipf_output
= 0;
181 lck_mtx_unlock(kipf_lock
);
183 TAILQ_REMOVE(head
, match
, ipf_link
);
184 lck_mtx_unlock(kipf_lock
);
187 FREE(match
, M_IFADDR
);
192 lck_mtx_unlock(kipf_lock
);
202 ipfilter_t filter_ref
)
204 struct mbuf
*m
= (struct mbuf
*)data
;
205 struct m_tag
*mtag
= 0;
206 struct ip
*ip
= mtod(m
, struct ip
*);
210 protocol_family_t proto
;
212 vers
= IP_VHL_V(ip
->ip_vhl
);
226 if (filter_ref
== 0 && m
->m_pkthdr
.rcvif
== 0) {
227 m
->m_pkthdr
.rcvif
= ifunit("lo0");
228 m
->m_pkthdr
.csum_data
= 0;
229 m
->m_pkthdr
.csum_flags
= 0;
231 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
233 ip
->ip_sum
= in_cksum(m
, hlen
);
236 if (filter_ref
!= 0) {
237 mtag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFILT
,
238 sizeof (ipfilter_t
), M_NOWAIT
);
243 *(ipfilter_t
*)(mtag
+1) = filter_ref
;
244 m_tag_prepend(m
, mtag
);
247 error
= proto_inject(proto
, data
);
256 ipfilter_t filter_ref
,
257 ipf_pktopts_t options
)
260 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&ro
.ro_dst
;
262 struct mbuf
*m
= (struct mbuf
*)data
;
264 struct m_tag
*mtag
= 0;
265 struct ip_moptions
*imo
= 0, ip_moptions
;
267 /* Make the IP header contiguous in the mbuf */
268 if ((size_t)m
->m_len
< sizeof(struct ip
)) {
269 m
= m_pullup(m
, sizeof(struct ip
));
270 if (m
== NULL
) return ENOMEM
;
272 ip
= (struct ip
*)m_mtod(m
);
274 if (filter_ref
!= 0) {
275 mtag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFILT
,
276 sizeof (ipfilter_t
), M_NOWAIT
);
281 *(ipfilter_t
*)(mtag
+1) = filter_ref
;
282 m_tag_prepend(m
, mtag
);
285 if (options
&& (options
->ippo_flags
& IPPOF_MCAST_OPTS
)) {
288 bzero(imo
, sizeof(struct ip6_moptions
));
289 imo
->imo_multicast_ifp
= options
->ippo_mcast_ifnet
;
290 imo
->imo_multicast_ttl
= options
->ippo_mcast_ttl
;
291 imo
->imo_multicast_loop
= options
->ippo_mcast_loop
;
294 /* Fill out a route structure and get a route */
295 bzero(&ro
, sizeof(struct route
));
296 sin
->sin_len
= sizeof(struct sockaddr_in
);
297 sin
->sin_family
= AF_INET
;
299 sin
->sin_addr
= ip
->ip_dst
;
301 if (ro
.ro_rt
== NULL
) {
306 error
= ip_output(m
, NULL
, &ro
, IP_ALLOWBROADCAST
| IP_RAWOUTPUT
, imo
);
308 /* Release the route */
318 ipfilter_t filter_ref
,
319 ipf_pktopts_t options
)
322 struct sockaddr_in6
*sin6
= &ro
.ro_dst
;
324 struct mbuf
*m
= (struct mbuf
*)data
;
326 struct m_tag
*mtag
= 0;
327 struct ip6_moptions
*im6o
= 0, ip6_moptions
;
329 /* Make the IP header contiguous in the mbuf */
330 if ((size_t)m
->m_len
< sizeof(struct ip6_hdr
)) {
331 m
= m_pullup(m
, sizeof(struct ip6_hdr
));
332 if (m
== NULL
) return ENOMEM
;
334 ip6
= (struct ip6_hdr
*)m_mtod(m
);
336 if (filter_ref
!= 0) {
337 mtag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFILT
,
338 sizeof (ipfilter_t
), M_NOWAIT
);
343 *(ipfilter_t
*)(mtag
+1) = filter_ref
;
344 m_tag_prepend(m
, mtag
);
347 if (options
&& (options
->ippo_flags
& IPPOF_MCAST_OPTS
)) {
348 im6o
= &ip6_moptions
;
350 bzero(im6o
, sizeof(struct ip6_moptions
));
351 im6o
->im6o_multicast_ifp
= options
->ippo_mcast_ifnet
;
352 im6o
->im6o_multicast_hlim
= options
->ippo_mcast_ttl
;
353 im6o
->im6o_multicast_loop
= options
->ippo_mcast_loop
;
357 /* Fill out a route structure and get a route */
358 bzero(&ro
, sizeof(struct route_in6
));
359 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
360 sin6
->sin6_family
= AF_INET6
;
361 sin6
->sin6_addr
= ip6
->ip6_dst
;
363 /* This is breaks loopback multicast! */
364 /* The scope ID should already at s6_addr16[1] */
365 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
)) {
366 /* Hack, pull the scope_id out of the dest addr */
367 sin6
->sin6_scope_id
= ntohs(ip6
->ip6_dst
.s6_addr16
[1]);
368 ip6
->ip6_dst
.s6_addr16
[1] = 0;
370 sin6
->sin6_scope_id
= 0;
372 rtalloc((struct route
*)&ro
);
373 if (ro
.ro_rt
== NULL
) {
379 error
= ip6_output(m
, NULL
, &ro
, 0, im6o
, NULL
, 0);
381 /* Release the route */
391 ipfilter_t filter_ref
,
392 ipf_pktopts_t options
)
394 struct mbuf
*m
= (struct mbuf
*)data
;
398 /* Make one byte of the header contiguous in the mbuf */
405 vers
= (*(u_int8_t
*)m_mtod(m
)) >> 4;
409 error
= ipf_injectv4_out(data
, filter_ref
, options
);
412 error
= ipf_injectv6_out(data
, filter_ref
, options
);
424 __private_extern__ ipfilter_t
425 ipf_get_inject_filter(struct mbuf
*m
)
427 ipfilter_t filter_ref
= 0;
430 mtag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFILT
, NULL
);
432 filter_ref
= *(ipfilter_t
*)(mtag
+1);
434 m_tag_delete(m
, mtag
);
439 __private_extern__
int
443 lck_grp_attr_t
*grp_attributes
= 0;
444 lck_attr_t
*lck_attributes
= 0;
445 lck_grp_t
*lck_grp
= 0;
447 grp_attributes
= lck_grp_attr_alloc_init();
448 if (grp_attributes
== 0) {
449 printf("ipf_init: lck_grp_attr_alloc_init failed\n");
453 lck_grp_attr_setdefault(grp_attributes
);
455 lck_grp
= lck_grp_alloc_init("IP Filter", grp_attributes
);
457 printf("ipf_init: lck_grp_alloc_init failed\n");
462 lck_attributes
= lck_attr_alloc_init();
463 if (lck_attributes
== 0) {
464 printf("ipf_init: lck_attr_alloc_init failed\n");
468 lck_attr_setdefault(lck_attributes
);
470 kipf_lock
= lck_mtx_alloc_init(lck_grp
, lck_attributes
);
471 if (kipf_lock
== 0) {
472 printf("ipf_init: lck_mtx_alloc_init failed\n");
479 lck_mtx_free(kipf_lock
, lck_grp
);
484 lck_grp_free(lck_grp
);
487 if (grp_attributes
) {
488 lck_grp_attr_free(grp_attributes
);
491 if (lck_attributes
) {
492 lck_attr_free(lck_attributes
);