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/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>
39 static struct socket_filter_list sock_filter_head
;
40 static lck_mtx_t
*sock_filter_lock
= 0;
42 static void sflt_detach_private(struct socket_filter_entry
*entry
, int unregistering
);
44 __private_extern__
void
47 lck_grp_attr_t
*grp_attrib
= 0;
48 lck_attr_t
*lck_attrib
= 0;
49 lck_grp_t
*lck_group
= 0;
51 TAILQ_INIT(&sock_filter_head
);
53 /* Allocate a spin lock */
54 grp_attrib
= lck_grp_attr_alloc_init();
55 lck_grp_attr_setdefault(grp_attrib
);
56 lck_group
= lck_grp_alloc_init("socket filter lock", grp_attrib
);
57 lck_grp_attr_free(grp_attrib
);
58 lck_attrib
= lck_attr_alloc_init();
59 lck_attr_setdefault(lck_attrib
);
60 lck_attr_setdebug(lck_attrib
);
61 sock_filter_lock
= lck_mtx_alloc_init(lck_group
, lck_attrib
);
62 lck_grp_free(lck_group
);
63 lck_attr_free(lck_attrib
);
66 __private_extern__
void
70 struct protosw
*proto
= so
->so_proto
;
71 struct socket_filter
*filter
;
73 if (TAILQ_FIRST(&proto
->pr_filter_head
) != NULL
) {
74 lck_mtx_lock(sock_filter_lock
);
75 TAILQ_FOREACH(filter
, &proto
->pr_filter_head
, sf_protosw_next
) {
76 sflt_attach_private(so
, filter
, 0, 0);
78 lck_mtx_unlock(sock_filter_lock
);
82 __private_extern__
void
86 struct socket_filter_entry
*filter
;
87 struct socket_filter_entry
*filter_next
;
89 for (filter
= so
->so_filt
; filter
; filter
= filter_next
) {
90 filter_next
= filter
->sfe_next_onsocket
;
91 sflt_detach_private(filter
, 0);
96 __private_extern__
void
103 __private_extern__
void
108 if (so
->so_filteruse
== 0) {
109 struct socket_filter_entry
*filter
;
110 struct socket_filter_entry
*next_filter
;
111 // search for detaching filters
112 for (filter
= so
->so_filt
; filter
; filter
= next_filter
) {
113 next_filter
= filter
->sfe_next_onsocket
;
115 if (filter
->sfe_flags
& SFEF_DETACHUSEZERO
) {
116 sflt_detach_private(filter
, 0);
122 __private_extern__
void
128 struct socket_filter_entry
*filter
;
131 for (filter
= so
->so_filt
; filter
;
132 filter
= filter
->sfe_next_onsocket
) {
133 if (filter
->sfe_filter
->sf_filter
.sf_notify
) {
137 socket_unlock(so
, 0);
139 filter
->sfe_filter
->sf_filter
.sf_notify(
140 filter
->sfe_cookie
, so
, event
, param
);
150 __private_extern__
int
153 const struct sockaddr
*from
,
156 sflt_data_flag_t flags
,
159 struct socket_filter_entry
*filter
;
161 int filtered_storage
;
163 if (filtered
== NULL
)
164 filtered
= &filtered_storage
;
167 for (filter
= so
->so_filt
; filter
&& (error
== 0);
168 filter
= filter
->sfe_next_onsocket
) {
169 if (filter
->sfe_filter
->sf_filter
.sf_data_in
) {
170 if (*filtered
== 0) {
173 socket_unlock(so
, 0);
175 error
= filter
->sfe_filter
->sf_filter
.sf_data_in(
176 filter
->sfe_cookie
, so
, from
, data
, control
, flags
);
180 if (*filtered
!= 0) {
188 /* sflt_attach_private
190 * Assumptions: If filter is not NULL, socket_filter_lock is held.
193 __private_extern__
int
196 struct socket_filter
*filter
,
200 struct socket_filter_entry
*entry
= NULL
;
204 if (filter
== NULL
) {
205 /* Find the filter by the handle */
206 lck_mtx_lock(sock_filter_lock
);
209 TAILQ_FOREACH(filter
, &sock_filter_head
, sf_global_next
) {
210 if (filter
->sf_filter
.sf_handle
== handle
)
219 /* allocate the socket filter entry */
220 MALLOC(entry
, struct socket_filter_entry
*, sizeof(*entry
), M_IFADDR
, M_WAITOK
);
227 /* Initialize the socket filter entry and call the attach function */
228 entry
->sfe_filter
= filter
;
229 entry
->sfe_socket
= so
;
230 entry
->sfe_cookie
= NULL
;
231 entry
->sfe_flags
= 0;
232 if (entry
->sfe_filter
->sf_filter
.sf_attach
) {
233 filter
->sf_usecount
++;
236 socket_unlock(so
, 0);
237 error
= entry
->sfe_filter
->sf_filter
.sf_attach(&entry
->sfe_cookie
, so
);
241 filter
->sf_usecount
--;
243 /* If the attach function returns an error, this filter is not attached */
245 FREE(entry
, M_IFADDR
);
252 /* Put the entry in the socket list */
253 entry
->sfe_next_onsocket
= so
->so_filt
;
256 /* Put the entry in the filter list */
257 entry
->sfe_next_onfilter
= filter
->sf_entry_head
;
258 filter
->sf_entry_head
= entry
;
260 /* Incremenet the parent filter's usecount */
261 filter
->sf_usecount
++;
265 lck_mtx_unlock(sock_filter_lock
);
272 /* sflt_detach_private
274 * Assumptions: if you pass 0 in for the second parameter, you are holding the
275 * socket lock for the socket the entry is attached to. If you pass 1 in for
276 * the second parameter, it is assumed that the entry is not on the filter's
277 * list and the socket lock is not held.
282 struct socket_filter_entry
*entry
,
285 struct socket
*so
= entry
->sfe_socket
;
286 struct socket_filter_entry
**next_ptr
;
291 socket_lock(entry
->sfe_socket
, 0);
295 * Attempt to find the entry on the filter's list and
296 * remove it. This prevents a filter detaching at the
297 * same time from attempting to remove the same entry.
299 lck_mtx_lock(sock_filter_lock
);
300 if (!unregistering
) {
301 if ((entry
->sfe_flags
& SFEF_UNREGISTERING
) != 0) {
303 * Another thread is unregistering the filter, we need to
304 * avoid detaching the filter here so the socket won't go
307 lck_mtx_unlock(sock_filter_lock
);
310 for (next_ptr
= &entry
->sfe_filter
->sf_entry_head
; *next_ptr
;
311 next_ptr
= &((*next_ptr
)->sfe_next_onfilter
)) {
312 if (*next_ptr
== entry
) {
314 *next_ptr
= entry
->sfe_next_onfilter
;
319 if (!found
&& (entry
->sfe_flags
& SFEF_DETACHUSEZERO
) == 0) {
320 lck_mtx_unlock(sock_filter_lock
);
326 * Clear the removing flag. We will perform the detach here or
327 * request a delayed deatch.
329 entry
->sfe_flags
&= ~SFEF_UNREGISTERING
;
332 if (entry
->sfe_socket
->so_filteruse
!= 0) {
333 entry
->sfe_flags
|= SFEF_DETACHUSEZERO
;
334 lck_mtx_unlock(sock_filter_lock
);
339 * Check if we are removing the last attached filter and
340 * the parent filter is being unregistered.
342 entry
->sfe_filter
->sf_usecount
--;
343 if ((entry
->sfe_filter
->sf_usecount
== 0) &&
344 (entry
->sfe_filter
->sf_flags
& SFF_DETACHING
) != 0)
347 lck_mtx_unlock(sock_filter_lock
);
349 /* Remove from the socket list */
350 for (next_ptr
= &entry
->sfe_socket
->so_filt
; *next_ptr
;
351 next_ptr
= &((*next_ptr
)->sfe_next_onsocket
)) {
352 if (*next_ptr
== entry
) {
353 *next_ptr
= entry
->sfe_next_onsocket
;
358 if (entry
->sfe_filter
->sf_filter
.sf_detach
)
359 entry
->sfe_filter
->sf_filter
.sf_detach(entry
->sfe_cookie
, entry
->sfe_socket
);
361 if (detached
&& entry
->sfe_filter
->sf_filter
.sf_unregistered
) {
362 entry
->sfe_filter
->sf_filter
.sf_unregistered(entry
->sfe_filter
->sf_filter
.sf_handle
);
363 FREE(entry
->sfe_filter
, M_IFADDR
);
367 socket_unlock(entry
->sfe_socket
, 1);
369 FREE(entry
, M_IFADDR
);
377 if (socket
== NULL
|| handle
== 0)
380 return sflt_attach_private(socket
, NULL
, handle
, 0);
388 struct socket_filter_entry
*filter
;
391 if (socket
== NULL
|| handle
== 0)
394 socket_lock(socket
, 1);
396 for (filter
= socket
->so_filt
; filter
;
397 filter
= filter
->sfe_next_onsocket
) {
398 if (filter
->sfe_filter
->sf_filter
.sf_handle
== handle
)
402 if (filter
!= NULL
) {
403 sflt_detach_private(filter
, 0);
406 socket
->so_filt
= NULL
;
410 socket_unlock(socket
, 1);
418 const struct sflt_filter
*filter
,
423 struct socket_filter
*sock_filt
= NULL
;
424 struct socket_filter
*match
= NULL
;
426 struct protosw
*pr
= pffindproto(domain
, protocol
, type
);
428 if (pr
== NULL
) return ENOENT
;
430 if (filter
->sf_attach
== NULL
|| filter
->sf_detach
== NULL
) return EINVAL
;
431 if (filter
->sf_handle
== 0) return EINVAL
;
432 if (filter
->sf_name
== NULL
) return EINVAL
;
434 /* Allocate the socket filter */
435 MALLOC(sock_filt
, struct socket_filter
*, sizeof(*sock_filt
), M_IFADDR
, M_WAITOK
);
436 if (sock_filt
== NULL
) {
440 bzero(sock_filt
, sizeof(*sock_filt
));
441 sock_filt
->sf_filter
= *filter
;
443 lck_mtx_lock(sock_filter_lock
);
444 /* Look for an existing entry */
445 TAILQ_FOREACH(match
, &sock_filter_head
, sf_global_next
) {
446 if (match
->sf_filter
.sf_handle
== sock_filt
->sf_filter
.sf_handle
) {
451 /* Add the entry only if there was no existing entry */
453 TAILQ_INSERT_TAIL(&sock_filter_head
, sock_filt
, sf_global_next
);
454 if ((sock_filt
->sf_filter
.sf_flags
& SFLT_GLOBAL
) != 0) {
455 TAILQ_INSERT_TAIL(&pr
->pr_filter_head
, sock_filt
, sf_protosw_next
);
456 sock_filt
->sf_proto
= pr
;
459 lck_mtx_unlock(sock_filter_lock
);
462 FREE(sock_filt
, M_IFADDR
);
473 struct socket_filter
*filter
;
474 struct socket_filter_entry
*entry_head
= NULL
;
475 struct socket_filter_entry
*next_entry
= NULL
;
477 /* Find the entry and remove it from the global and protosw lists */
478 lck_mtx_lock(sock_filter_lock
);
479 TAILQ_FOREACH(filter
, &sock_filter_head
, sf_global_next
) {
480 if (filter
->sf_filter
.sf_handle
== handle
)
485 TAILQ_REMOVE(&sock_filter_head
, filter
, sf_global_next
);
486 if ((filter
->sf_filter
.sf_flags
& SFLT_GLOBAL
) != 0) {
487 TAILQ_REMOVE(&filter
->sf_proto
->pr_filter_head
, filter
, sf_protosw_next
);
489 entry_head
= filter
->sf_entry_head
;
490 filter
->sf_entry_head
= NULL
;
491 filter
->sf_flags
|= SFF_DETACHING
;
493 for (next_entry
= entry_head
; next_entry
;
494 next_entry
= next_entry
->sfe_next_onfilter
) {
495 socket_lock(next_entry
->sfe_socket
, 1);
496 next_entry
->sfe_flags
|= SFEF_UNREGISTERING
;
497 socket_unlock(next_entry
->sfe_socket
, 0); /* Radar 4201550: prevents the socket from being deleted while being unregistered */
501 lck_mtx_unlock(sock_filter_lock
);
506 /* We need to detach the filter from any sockets it's attached to */
507 if (entry_head
== 0) {
508 if (filter
->sf_filter
.sf_unregistered
)
509 filter
->sf_filter
.sf_unregistered(filter
->sf_filter
.sf_handle
);
512 next_entry
= entry_head
->sfe_next_onfilter
;
513 sflt_detach_private(entry_head
, 1);
514 entry_head
= next_entry
;
524 const struct sockaddr
* from
,
527 sflt_data_flag_t flags
)
530 if (so
== NULL
|| data
== NULL
) return EINVAL
;
532 if (flags
& sock_data_filt_flag_oob
) {
539 if (sbappendaddr(&so
->so_rcv
, (struct sockaddr
*)from
, data
,
546 if (sbappendcontrol(&so
->so_rcv
, data
, control
, NULL
))
551 if (flags
& sock_data_filt_flag_record
) {
552 if (control
|| from
) {
556 if (sbappendrecord(&so
->so_rcv
, (struct mbuf
*)data
))
561 if (sbappend(&so
->so_rcv
, data
))
564 socket_unlock(so
, 1);
569 sock_inject_data_out(
571 const struct sockaddr
* to
,
574 sflt_data_flag_t flags
)
577 if (flags
& sock_data_filt_flag_oob
) sosendflags
= MSG_OOB
;
578 return sosend(so
, (const struct sockaddr
*)to
, NULL
,
579 data
, control
, sosendflags
);
586 return (sopt
->sopt_dir
== SOPT_GET
) ? sockopt_get
: sockopt_set
;
593 return sopt
->sopt_level
;
600 return sopt
->sopt_name
;
607 return sopt
->sopt_valsize
;
616 return sooptcopyin(sopt
, data
, len
, len
);
625 return sooptcopyout(sopt
, data
, len
);