2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/kpi_socketfilter.h>
25 #include <sys/socket.h>
26 #include <sys/param.h>
27 #include <sys/errno.h>
28 #include <sys/malloc.h>
29 #include <sys/protosw.h>
30 #include <kern/locks.h>
31 #include <net/kext_net.h>
33 static struct socket_filter_list sock_filter_head
;
34 static lck_mtx_t
*sock_filter_lock
= 0;
36 __private_extern__
void
39 lck_grp_attr_t
*grp_attrib
= 0;
40 lck_attr_t
*lck_attrib
= 0;
41 lck_grp_t
*lck_group
= 0;
43 TAILQ_INIT(&sock_filter_head
);
45 /* Allocate a spin lock */
46 grp_attrib
= lck_grp_attr_alloc_init();
47 lck_grp_attr_setdefault(grp_attrib
);
48 lck_group
= lck_grp_alloc_init("socket filter lock", grp_attrib
);
49 lck_grp_attr_free(grp_attrib
);
50 lck_attrib
= lck_attr_alloc_init();
51 lck_attr_setdefault(lck_attrib
);
52 lck_attr_setdebug(lck_attrib
);
53 sock_filter_lock
= lck_mtx_alloc_init(lck_group
, lck_attrib
);
54 lck_grp_free(lck_group
);
55 lck_attr_free(lck_attrib
);
58 __private_extern__
void
62 struct protosw
*proto
= so
->so_proto
;
63 struct socket_filter
*filter
;
65 if (TAILQ_FIRST(&proto
->pr_filter_head
) != NULL
) {
66 lck_mtx_lock(sock_filter_lock
);
67 TAILQ_FOREACH(filter
, &proto
->pr_filter_head
, sf_protosw_next
) {
68 sflt_attach_private(so
, filter
, 0, 0);
70 lck_mtx_unlock(sock_filter_lock
);
74 __private_extern__
void
78 struct socket_filter_entry
*filter
;
79 struct socket_filter_entry
*filter_next
;
81 for (filter
= so
->so_filt
; filter
; filter
= filter_next
) {
82 filter_next
= filter
->sfe_next_onsocket
;
83 sflt_detach_private(filter
, 0);
87 __private_extern__
void
94 __private_extern__
void
99 if (so
->so_filteruse
== 0) {
100 struct socket_filter_entry
*filter
;
101 struct socket_filter_entry
*next_filter
;
102 // search for detaching filters
103 for (filter
= so
->so_filt
; filter
; filter
= next_filter
) {
104 next_filter
= filter
->sfe_next_onsocket
;
106 if (filter
->sfe_flags
& SFEF_DETACHING
) {
107 sflt_detach_private(filter
, 0);
113 __private_extern__
void
119 struct socket_filter_entry
*filter
;
122 for (filter
= so
->so_filt
; filter
;
123 filter
= filter
->sfe_next_onsocket
) {
124 if (filter
->sfe_filter
->sf_filter
.sf_notify
) {
128 socket_unlock(so
, 0);
130 filter
->sfe_filter
->sf_filter
.sf_notify(
131 filter
->sfe_cookie
, so
, event
, param
);
141 __private_extern__
int
144 const struct sockaddr
*from
,
147 sflt_data_flag_t flags
,
150 struct socket_filter_entry
*filter
;
152 int filtered_storage
;
154 if (filtered
== NULL
)
155 filtered
= &filtered_storage
;
158 for (filter
= so
->so_filt
; filter
&& (error
== 0);
159 filter
= filter
->sfe_next_onsocket
) {
160 if (filter
->sfe_filter
->sf_filter
.sf_data_in
) {
161 if (*filtered
== 0) {
164 socket_unlock(so
, 0);
166 error
= filter
->sfe_filter
->sf_filter
.sf_data_in(
167 filter
->sfe_cookie
, so
, from
, data
, control
, flags
);
171 if (*filtered
!= 0) {
179 /* sflt_attach_private
181 * Assumptions: If filter is not NULL, socket_filter_lock is held.
184 __private_extern__
int
187 struct socket_filter
*filter
,
191 struct socket_filter_entry
*entry
= NULL
;
195 if (filter
== NULL
) {
196 /* Find the filter by the handle */
197 lck_mtx_lock(sock_filter_lock
);
200 TAILQ_FOREACH(filter
, &sock_filter_head
, sf_global_next
) {
201 if (filter
->sf_filter
.sf_handle
== handle
)
210 /* allocate the socket filter entry */
211 MALLOC(entry
, struct socket_filter_entry
*, sizeof(*entry
), M_IFADDR
, M_WAITOK
);
218 /* Initialize the socket filter entry and call the attach function */
219 entry
->sfe_filter
= filter
;
220 entry
->sfe_socket
= so
;
221 entry
->sfe_cookie
= NULL
;
222 if (entry
->sfe_filter
->sf_filter
.sf_attach
) {
223 filter
->sf_usecount
++;
226 socket_unlock(so
, 0);
227 error
= entry
->sfe_filter
->sf_filter
.sf_attach(&entry
->sfe_cookie
, so
);
231 filter
->sf_usecount
--;
233 /* If the attach function returns an error, this filter is not attached */
235 FREE(entry
, M_IFADDR
);
242 /* Put the entry in the socket list */
243 entry
->sfe_next_onsocket
= so
->so_filt
;
246 /* Put the entry in the filter list */
247 entry
->sfe_next_onfilter
= filter
->sf_entry_head
;
248 filter
->sf_entry_head
= entry
;
250 /* Increment the socket's usecount */
253 /* Incremenet the parent filter's usecount */
254 filter
->sf_usecount
++;
258 lck_mtx_unlock(sock_filter_lock
);
265 /* sflt_detach_private
267 * Assumptions: if you pass 0 in for the second parameter, you are holding the
268 * socket lock for the socket the entry is attached to. If you pass 1 in for
269 * the second parameter, it is assumed that the entry is not on the filter's
270 * list and the socket lock is not held.
273 __private_extern__
void
275 struct socket_filter_entry
*entry
,
278 struct socket
*so
= entry
->sfe_socket
;
279 struct socket_filter_entry
**next_ptr
;
283 if (filter_detached
) {
284 socket_lock(entry
->sfe_socket
, 0);
288 * Attempt to find the entry on the filter's list and
289 * remove it. This prevents a filter detaching at the
290 * same time from attempting to remove the same entry.
292 lck_mtx_lock(sock_filter_lock
);
293 if (!filter_detached
) {
294 for (next_ptr
= &entry
->sfe_filter
->sf_entry_head
; *next_ptr
;
295 next_ptr
= &((*next_ptr
)->sfe_next_onfilter
)) {
296 if (*next_ptr
== entry
) {
298 *next_ptr
= entry
->sfe_next_onfilter
;
304 if (!filter_detached
&& !found
&& (entry
->sfe_flags
& SFEF_DETACHING
) == 0) {
305 lck_mtx_unlock(sock_filter_lock
);
309 if (entry
->sfe_socket
->so_filteruse
!= 0) {
310 lck_mtx_unlock(sock_filter_lock
);
311 entry
->sfe_flags
|= SFEF_DETACHING
;
316 * Check if we are removing the last attached filter and
317 * the parent filter is being unregistered.
319 if (entry
->sfe_socket
->so_filteruse
== 0) {
320 entry
->sfe_filter
->sf_usecount
--;
321 if ((entry
->sfe_filter
->sf_usecount
== 0) &&
322 (entry
->sfe_filter
->sf_flags
& SFF_DETACHING
) != 0)
325 lck_mtx_unlock(sock_filter_lock
);
327 /* Remove from the socket list */
328 for (next_ptr
= &entry
->sfe_socket
->so_filt
; *next_ptr
;
329 next_ptr
= &((*next_ptr
)->sfe_next_onsocket
)) {
330 if (*next_ptr
== entry
) {
331 *next_ptr
= entry
->sfe_next_onsocket
;
336 if (entry
->sfe_filter
->sf_filter
.sf_detach
)
337 entry
->sfe_filter
->sf_filter
.sf_detach(entry
->sfe_cookie
, entry
->sfe_socket
);
339 if (detached
&& entry
->sfe_filter
->sf_filter
.sf_unregistered
) {
340 entry
->sfe_filter
->sf_filter
.sf_unregistered(entry
->sfe_filter
->sf_filter
.sf_handle
);
341 FREE(entry
->sfe_filter
, M_IFADDR
);
344 if (filter_detached
) {
345 socket_unlock(entry
->sfe_socket
, 1);
348 // We need some better way to decrement the usecount
351 FREE(entry
, M_IFADDR
);
359 if (socket
== NULL
|| handle
== 0)
362 return sflt_attach_private(socket
, NULL
, handle
, 0);
370 struct socket_filter_entry
*filter
;
373 if (socket
== NULL
|| handle
== 0)
376 socket_lock(socket
, 1);
378 for (filter
= socket
->so_filt
; filter
;
379 filter
= filter
->sfe_next_onsocket
) {
380 if (filter
->sfe_filter
->sf_filter
.sf_handle
== handle
)
384 if (filter
!= NULL
) {
385 sflt_detach_private(filter
, 0);
391 socket_unlock(socket
, 1);
399 const struct sflt_filter
*filter
,
404 struct socket_filter
*sock_filt
= NULL
;
405 struct socket_filter
*match
= NULL
;
407 struct protosw
*pr
= pffindproto(domain
, protocol
, type
);
409 if (pr
== NULL
) return ENOENT
;
411 if (filter
->sf_attach
== NULL
|| filter
->sf_detach
== NULL
) return EINVAL
;
412 if (filter
->sf_handle
== 0) return EINVAL
;
413 if (filter
->sf_name
== NULL
) return EINVAL
;
415 /* Allocate the socket filter */
416 MALLOC(sock_filt
, struct socket_filter
*, sizeof(*sock_filt
), M_IFADDR
, M_WAITOK
);
417 if (sock_filt
== NULL
) {
421 bzero(sock_filt
, sizeof(*sock_filt
));
422 sock_filt
->sf_filter
= *filter
;
424 lck_mtx_lock(sock_filter_lock
);
425 /* Look for an existing entry */
426 TAILQ_FOREACH(match
, &sock_filter_head
, sf_global_next
) {
427 if (match
->sf_filter
.sf_handle
== sock_filt
->sf_filter
.sf_handle
) {
432 /* Add the entry only if there was no existing entry */
434 TAILQ_INSERT_TAIL(&sock_filter_head
, sock_filt
, sf_global_next
);
435 if ((sock_filt
->sf_filter
.sf_flags
& SFLT_GLOBAL
) != 0) {
436 TAILQ_INSERT_TAIL(&pr
->pr_filter_head
, sock_filt
, sf_protosw_next
);
437 sock_filt
->sf_proto
= pr
;
440 lck_mtx_unlock(sock_filter_lock
);
443 FREE(sock_filt
, M_IFADDR
);
454 struct socket_filter
*filter
;
455 struct socket_filter_entry
*entry_head
= NULL
;
457 /* Find the entry and remove it from the global and protosw lists */
458 lck_mtx_lock(sock_filter_lock
);
459 TAILQ_FOREACH(filter
, &sock_filter_head
, sf_global_next
) {
460 if (filter
->sf_filter
.sf_handle
== handle
)
465 TAILQ_REMOVE(&sock_filter_head
, filter
, sf_global_next
);
466 if ((filter
->sf_filter
.sf_flags
& SFLT_GLOBAL
) != 0) {
467 TAILQ_REMOVE(&filter
->sf_proto
->pr_filter_head
, filter
, sf_protosw_next
);
469 entry_head
= filter
->sf_entry_head
;
470 filter
->sf_entry_head
= NULL
;
471 filter
->sf_flags
|= SFF_DETACHING
;
474 lck_mtx_unlock(sock_filter_lock
);
479 /* We need to detach the filter from any sockets it's attached to */
480 if (entry_head
== 0) {
481 if (filter
->sf_filter
.sf_unregistered
)
482 filter
->sf_filter
.sf_unregistered(filter
->sf_filter
.sf_handle
);
485 struct socket_filter_entry
*next_entry
;
486 next_entry
= entry_head
->sfe_next_onfilter
;
487 sflt_detach_private(entry_head
, 1);
488 entry_head
= next_entry
;
498 const struct sockaddr
* from
,
501 sflt_data_flag_t flags
)
504 if (so
== NULL
|| data
== NULL
) return EINVAL
;
506 if (flags
& sock_data_filt_flag_oob
) {
513 if (sbappendaddr(&so
->so_rcv
, (struct sockaddr
*)from
, data
,
520 if (sbappendcontrol(&so
->so_rcv
, data
, control
, NULL
))
525 if (flags
& sock_data_filt_flag_record
) {
526 if (control
|| from
) {
530 if (sbappendrecord(&so
->so_rcv
, (struct mbuf
*)data
))
535 if (sbappend(&so
->so_rcv
, data
))
538 socket_unlock(so
, 1);
543 sock_inject_data_out(
545 const struct sockaddr
* to
,
548 sflt_data_flag_t flags
)
551 if (flags
& sock_data_filt_flag_oob
) sosendflags
= MSG_OOB
;
552 return sosend(so
, (const struct sockaddr
*)to
, NULL
,
553 data
, control
, sosendflags
);
560 return (sopt
->sopt_dir
== SOPT_GET
) ? sockopt_get
: sockopt_set
;
567 return sopt
->sopt_level
;
574 return sopt
->sopt_name
;
581 return sopt
->sopt_valsize
;
590 return sooptcopyin(sopt
, data
, len
, len
);
599 return sooptcopyout(sopt
, data
, len
);