2 * Copyright (c) 2003-2007 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/kpi_socketfilter.h>
31 #include <sys/socket.h>
32 #include <sys/param.h>
33 #include <sys/errno.h>
34 #include <sys/malloc.h>
35 #include <sys/protosw.h>
36 #include <kern/locks.h>
37 #include <net/kext_net.h>
41 static struct socket_filter_list sock_filter_head
;
42 static lck_mtx_t
*sock_filter_lock
= 0;
44 static void sflt_detach_private(struct socket_filter_entry
*entry
, int unregistering
);
46 __private_extern__
void
49 lck_grp_attr_t
*grp_attrib
= 0;
50 lck_attr_t
*lck_attrib
= 0;
51 lck_grp_t
*lck_group
= 0;
53 TAILQ_INIT(&sock_filter_head
);
55 /* Allocate a spin lock */
56 grp_attrib
= lck_grp_attr_alloc_init();
57 lck_group
= lck_grp_alloc_init("socket filter lock", grp_attrib
);
58 lck_grp_attr_free(grp_attrib
);
59 lck_attrib
= lck_attr_alloc_init();
60 sock_filter_lock
= lck_mtx_alloc_init(lck_group
, lck_attrib
);
61 lck_grp_free(lck_group
);
62 lck_attr_free(lck_attrib
);
65 __private_extern__
void
69 struct protosw
*proto
= so
->so_proto
;
70 struct socket_filter
*filter
;
72 if (TAILQ_FIRST(&proto
->pr_filter_head
) != NULL
) {
73 lck_mtx_lock(sock_filter_lock
);
74 TAILQ_FOREACH(filter
, &proto
->pr_filter_head
, sf_protosw_next
) {
75 sflt_attach_private(so
, filter
, 0, 0);
77 lck_mtx_unlock(sock_filter_lock
);
81 __private_extern__
void
85 struct socket_filter_entry
*filter
;
86 struct socket_filter_entry
*filter_next
;
88 for (filter
= so
->so_filt
; filter
; filter
= filter_next
) {
89 filter_next
= filter
->sfe_next_onsocket
;
90 sflt_detach_private(filter
, 0);
95 __private_extern__
void
102 __private_extern__
void
107 if (so
->so_filteruse
== 0) {
108 struct socket_filter_entry
*filter
;
109 struct socket_filter_entry
*next_filter
;
110 // search for detaching filters
111 for (filter
= so
->so_filt
; filter
; filter
= next_filter
) {
112 next_filter
= filter
->sfe_next_onsocket
;
114 if (filter
->sfe_flags
& SFEF_DETACHUSEZERO
) {
115 sflt_detach_private(filter
, 0);
121 __private_extern__
void
127 struct socket_filter_entry
*filter
;
130 for (filter
= so
->so_filt
; filter
;
131 filter
= filter
->sfe_next_onsocket
) {
132 if (filter
->sfe_filter
->sf_filter
.sf_notify
) {
136 socket_unlock(so
, 0);
138 filter
->sfe_filter
->sf_filter
.sf_notify(
139 filter
->sfe_cookie
, so
, event
, param
);
149 __private_extern__
int
152 const struct sockaddr
*from
,
155 sflt_data_flag_t flags
,
158 struct socket_filter_entry
*filter
;
160 int filtered_storage
;
162 if (filtered
== NULL
)
163 filtered
= &filtered_storage
;
166 for (filter
= so
->so_filt
; filter
&& (error
== 0);
167 filter
= filter
->sfe_next_onsocket
) {
168 if (filter
->sfe_filter
->sf_filter
.sf_data_in
) {
169 if (*filtered
== 0) {
172 socket_unlock(so
, 0);
174 error
= filter
->sfe_filter
->sf_filter
.sf_data_in(
175 filter
->sfe_cookie
, so
, from
, data
, control
, flags
);
179 if (*filtered
!= 0) {
187 /* sflt_attach_private
189 * Assumptions: If filter is not NULL, socket_filter_lock is held.
192 __private_extern__
int
195 struct socket_filter
*filter
,
199 struct socket_filter_entry
*entry
= NULL
;
203 if (filter
== NULL
) {
204 /* Find the filter by the handle */
205 lck_mtx_lock(sock_filter_lock
);
208 TAILQ_FOREACH(filter
, &sock_filter_head
, sf_global_next
) {
209 if (filter
->sf_filter
.sf_handle
== handle
)
218 /* allocate the socket filter entry */
219 MALLOC(entry
, struct socket_filter_entry
*, sizeof(*entry
), M_IFADDR
, M_WAITOK
);
226 /* Initialize the socket filter entry and call the attach function */
227 entry
->sfe_filter
= filter
;
228 entry
->sfe_socket
= so
;
229 entry
->sfe_cookie
= NULL
;
230 entry
->sfe_flags
= 0;
231 if (entry
->sfe_filter
->sf_filter
.sf_attach
) {
232 filter
->sf_usecount
++;
235 socket_unlock(so
, 0);
236 error
= entry
->sfe_filter
->sf_filter
.sf_attach(&entry
->sfe_cookie
, so
);
240 filter
->sf_usecount
--;
242 /* If the attach function returns an error, this filter is not attached */
244 FREE(entry
, M_IFADDR
);
251 /* Put the entry in the socket list */
252 entry
->sfe_next_onsocket
= so
->so_filt
;
255 /* Put the entry in the filter list */
256 entry
->sfe_next_onfilter
= filter
->sf_entry_head
;
257 filter
->sf_entry_head
= entry
;
259 /* Incremenet the parent filter's usecount */
260 filter
->sf_usecount
++;
264 lck_mtx_unlock(sock_filter_lock
);
271 /* sflt_detach_private
273 * Assumptions: if you pass 0 in for the second parameter, you are holding the
274 * socket lock for the socket the entry is attached to. If you pass 1 in for
275 * the second parameter, it is assumed that the entry is not on the filter's
276 * list and the socket lock is not held.
281 struct socket_filter_entry
*entry
,
284 struct socket_filter_entry
**next_ptr
;
289 socket_lock(entry
->sfe_socket
, 0);
293 * Attempt to find the entry on the filter's list and
294 * remove it. This prevents a filter detaching at the
295 * same time from attempting to remove the same entry.
297 lck_mtx_lock(sock_filter_lock
);
298 if (!unregistering
) {
299 if ((entry
->sfe_flags
& SFEF_UNREGISTERING
) != 0) {
301 * Another thread is unregistering the filter, we need to
302 * avoid detaching the filter here so the socket won't go
305 lck_mtx_unlock(sock_filter_lock
);
308 for (next_ptr
= &entry
->sfe_filter
->sf_entry_head
; *next_ptr
;
309 next_ptr
= &((*next_ptr
)->sfe_next_onfilter
)) {
310 if (*next_ptr
== entry
) {
312 *next_ptr
= entry
->sfe_next_onfilter
;
317 if (!found
&& (entry
->sfe_flags
& SFEF_DETACHUSEZERO
) == 0) {
318 lck_mtx_unlock(sock_filter_lock
);
324 * Clear the removing flag. We will perform the detach here or
325 * request a delayed deatch.
327 entry
->sfe_flags
&= ~SFEF_UNREGISTERING
;
330 if (entry
->sfe_socket
->so_filteruse
!= 0) {
331 entry
->sfe_flags
|= SFEF_DETACHUSEZERO
;
332 lck_mtx_unlock(sock_filter_lock
);
337 * Check if we are removing the last attached filter and
338 * the parent filter is being unregistered.
340 entry
->sfe_filter
->sf_usecount
--;
341 if ((entry
->sfe_filter
->sf_usecount
== 0) &&
342 (entry
->sfe_filter
->sf_flags
& SFF_DETACHING
) != 0)
345 lck_mtx_unlock(sock_filter_lock
);
347 /* Remove from the socket list */
348 for (next_ptr
= &entry
->sfe_socket
->so_filt
; *next_ptr
;
349 next_ptr
= &((*next_ptr
)->sfe_next_onsocket
)) {
350 if (*next_ptr
== entry
) {
351 *next_ptr
= entry
->sfe_next_onsocket
;
356 if (entry
->sfe_filter
->sf_filter
.sf_detach
)
357 entry
->sfe_filter
->sf_filter
.sf_detach(entry
->sfe_cookie
, entry
->sfe_socket
);
359 if (detached
&& entry
->sfe_filter
->sf_filter
.sf_unregistered
) {
360 entry
->sfe_filter
->sf_filter
.sf_unregistered(entry
->sfe_filter
->sf_filter
.sf_handle
);
361 FREE(entry
->sfe_filter
, M_IFADDR
);
365 socket_unlock(entry
->sfe_socket
, 1);
367 FREE(entry
, M_IFADDR
);
375 if (socket
== NULL
|| handle
== 0)
378 return sflt_attach_private(socket
, NULL
, handle
, 0);
386 struct socket_filter_entry
*filter
;
389 if (socket
== NULL
|| handle
== 0)
392 socket_lock(socket
, 1);
394 for (filter
= socket
->so_filt
; filter
;
395 filter
= filter
->sfe_next_onsocket
) {
396 if (filter
->sfe_filter
->sf_filter
.sf_handle
== handle
)
400 if (filter
!= NULL
) {
401 sflt_detach_private(filter
, 0);
404 socket
->so_filt
= NULL
;
408 socket_unlock(socket
, 1);
416 const struct sflt_filter
*filter
,
421 struct socket_filter
*sock_filt
= NULL
;
422 struct socket_filter
*match
= NULL
;
424 struct protosw
*pr
= pffindproto(domain
, protocol
, type
);
430 if (filter
->sf_attach
== NULL
|| filter
->sf_detach
== NULL
||
431 filter
->sf_handle
== 0 || filter
->sf_name
== NULL
)
434 /* Allocate the socket filter */
435 MALLOC(sock_filt
, struct socket_filter
*, sizeof (*sock_filt
),
437 if (sock_filt
== NULL
) {
441 bzero(sock_filt
, sizeof (*sock_filt
));
443 /* Legacy sflt_filter length; current structure minus extended */
444 len
= sizeof (*filter
) - sizeof (struct sflt_filter_ext
);
446 * Include extended fields if filter defines SFLT_EXTENDED.
447 * We've zeroed out our internal sflt_filter placeholder,
448 * so any unused portion would have been taken care of.
450 if (filter
->sf_flags
& SFLT_EXTENDED
) {
451 unsigned int ext_len
= filter
->sf_len
;
453 if (ext_len
> sizeof (struct sflt_filter_ext
))
454 ext_len
= sizeof (struct sflt_filter_ext
);
458 bcopy(filter
, &sock_filt
->sf_filter
, len
);
460 lck_mtx_lock(sock_filter_lock
);
461 /* Look for an existing entry */
462 TAILQ_FOREACH(match
, &sock_filter_head
, sf_global_next
) {
463 if (match
->sf_filter
.sf_handle
==
464 sock_filt
->sf_filter
.sf_handle
) {
469 /* Add the entry only if there was no existing entry */
471 TAILQ_INSERT_TAIL(&sock_filter_head
, sock_filt
, sf_global_next
);
472 if ((sock_filt
->sf_filter
.sf_flags
& SFLT_GLOBAL
) != 0) {
473 TAILQ_INSERT_TAIL(&pr
->pr_filter_head
, sock_filt
,
475 sock_filt
->sf_proto
= pr
;
478 lck_mtx_unlock(sock_filter_lock
);
481 FREE(sock_filt
, M_IFADDR
);
492 struct socket_filter
*filter
;
493 struct socket_filter_entry
*entry_head
= NULL
;
494 struct socket_filter_entry
*next_entry
= NULL
;
496 /* Find the entry and remove it from the global and protosw lists */
497 lck_mtx_lock(sock_filter_lock
);
498 TAILQ_FOREACH(filter
, &sock_filter_head
, sf_global_next
) {
499 if (filter
->sf_filter
.sf_handle
== handle
)
504 TAILQ_REMOVE(&sock_filter_head
, filter
, sf_global_next
);
505 if ((filter
->sf_filter
.sf_flags
& SFLT_GLOBAL
) != 0) {
506 TAILQ_REMOVE(&filter
->sf_proto
->pr_filter_head
, filter
, sf_protosw_next
);
508 entry_head
= filter
->sf_entry_head
;
509 filter
->sf_entry_head
= NULL
;
510 filter
->sf_flags
|= SFF_DETACHING
;
512 for (next_entry
= entry_head
; next_entry
;
513 next_entry
= next_entry
->sfe_next_onfilter
) {
514 socket_lock(next_entry
->sfe_socket
, 1);
515 next_entry
->sfe_flags
|= SFEF_UNREGISTERING
;
516 socket_unlock(next_entry
->sfe_socket
, 0); /* Radar 4201550: prevents the socket from being deleted while being unregistered */
520 lck_mtx_unlock(sock_filter_lock
);
525 /* We need to detach the filter from any sockets it's attached to */
526 if (entry_head
== 0) {
527 if (filter
->sf_filter
.sf_unregistered
)
528 filter
->sf_filter
.sf_unregistered(filter
->sf_filter
.sf_handle
);
531 next_entry
= entry_head
->sfe_next_onfilter
;
532 sflt_detach_private(entry_head
, 1);
533 entry_head
= next_entry
;
543 const struct sockaddr
* from
,
546 sflt_data_flag_t flags
)
549 if (so
== NULL
|| data
== NULL
) return EINVAL
;
551 if (flags
& sock_data_filt_flag_oob
) {
558 if (sbappendaddr(&so
->so_rcv
, (struct sockaddr
*)from
, data
,
565 if (sbappendcontrol(&so
->so_rcv
, data
, control
, NULL
))
570 if (flags
& sock_data_filt_flag_record
) {
571 if (control
|| from
) {
575 if (sbappendrecord(&so
->so_rcv
, (struct mbuf
*)data
))
580 if (sbappend(&so
->so_rcv
, data
))
583 socket_unlock(so
, 1);
588 sock_inject_data_out(
590 const struct sockaddr
* to
,
593 sflt_data_flag_t flags
)
596 if (flags
& sock_data_filt_flag_oob
) sosendflags
= MSG_OOB
;
597 return sosend(so
, (struct sockaddr
*)to
, NULL
,
598 data
, control
, sosendflags
);
605 return (sopt
->sopt_dir
== SOPT_GET
) ? sockopt_get
: sockopt_set
;
612 return sopt
->sopt_level
;
619 return sopt
->sopt_name
;
626 return sopt
->sopt_valsize
;
635 return sooptcopyin(sopt
, data
, len
, len
);
644 return sooptcopyout(sopt
, data
, len
);