]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kpi_socketfilter.c
xnu-1228.tar.gz
[apple/xnu.git] / bsd / kern / kpi_socketfilter.c
1 /*
2 * Copyright (c) 2003-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <sys/kpi_socketfilter.h>
30
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>
38
39 #include <string.h>
40
41 static struct socket_filter_list sock_filter_head;
42 static lck_mtx_t *sock_filter_lock = 0;
43
44 static void sflt_detach_private(struct socket_filter_entry *entry, int unregistering);
45
46 __private_extern__ void
47 sflt_init(void)
48 {
49 lck_grp_attr_t *grp_attrib = 0;
50 lck_attr_t *lck_attrib = 0;
51 lck_grp_t *lck_group = 0;
52
53 TAILQ_INIT(&sock_filter_head);
54
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);
63 }
64
65 __private_extern__ void
66 sflt_initsock(
67 struct socket *so)
68 {
69 struct protosw *proto = so->so_proto;
70 struct socket_filter *filter;
71
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);
76 }
77 lck_mtx_unlock(sock_filter_lock);
78 }
79 }
80
81 __private_extern__ void
82 sflt_termsock(
83 struct socket *so)
84 {
85 struct socket_filter_entry *filter;
86 struct socket_filter_entry *filter_next;
87
88 for (filter = so->so_filt; filter; filter = filter_next) {
89 filter_next = filter->sfe_next_onsocket;
90 sflt_detach_private(filter, 0);
91 }
92 so->so_filt = NULL;
93 }
94
95 __private_extern__ void
96 sflt_use(
97 struct socket *so)
98 {
99 so->so_filteruse++;
100 }
101
102 __private_extern__ void
103 sflt_unuse(
104 struct socket *so)
105 {
106 so->so_filteruse--;
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;
113
114 if (filter->sfe_flags & SFEF_DETACHUSEZERO) {
115 sflt_detach_private(filter, 0);
116 }
117 }
118 }
119 }
120
121 __private_extern__ void
122 sflt_notify(
123 struct socket *so,
124 sflt_event_t event,
125 void *param)
126 {
127 struct socket_filter_entry *filter;
128 int filtered = 0;
129
130 for (filter = so->so_filt; filter;
131 filter = filter->sfe_next_onsocket) {
132 if (filter->sfe_filter->sf_filter.sf_notify) {
133 if (filtered == 0) {
134 filtered = 1;
135 sflt_use(so);
136 socket_unlock(so, 0);
137 }
138 filter->sfe_filter->sf_filter.sf_notify(
139 filter->sfe_cookie, so, event, param);
140 }
141 }
142
143 if (filtered != 0) {
144 socket_lock(so, 0);
145 sflt_unuse(so);
146 }
147 }
148
149 __private_extern__ int
150 sflt_data_in(
151 struct socket *so,
152 const struct sockaddr *from,
153 mbuf_t *data,
154 mbuf_t *control,
155 sflt_data_flag_t flags,
156 int *filtered)
157 {
158 struct socket_filter_entry *filter;
159 int error = 0;
160 int filtered_storage;
161
162 if (filtered == NULL)
163 filtered = &filtered_storage;
164 *filtered = 0;
165
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) {
170 *filtered = 1;
171 sflt_use(so);
172 socket_unlock(so, 0);
173 }
174 error = filter->sfe_filter->sf_filter.sf_data_in(
175 filter->sfe_cookie, so, from, data, control, flags);
176 }
177 }
178
179 if (*filtered != 0) {
180 socket_lock(so, 0);
181 sflt_unuse(so);
182 }
183
184 return error;
185 }
186
187 /* sflt_attach_private
188 *
189 * Assumptions: If filter is not NULL, socket_filter_lock is held.
190 */
191
192 __private_extern__ int
193 sflt_attach_private(
194 struct socket *so,
195 struct socket_filter *filter,
196 sflt_handle handle,
197 int sock_locked)
198 {
199 struct socket_filter_entry *entry = NULL;
200 int didlock = 0;
201 int error = 0;
202
203 if (filter == NULL) {
204 /* Find the filter by the handle */
205 lck_mtx_lock(sock_filter_lock);
206 didlock = 1;
207
208 TAILQ_FOREACH(filter, &sock_filter_head, sf_global_next) {
209 if (filter->sf_filter.sf_handle == handle)
210 break;
211 }
212 }
213
214 if (filter == NULL)
215 error = ENOENT;
216
217 if (error == 0) {
218 /* allocate the socket filter entry */
219 MALLOC(entry, struct socket_filter_entry *, sizeof(*entry), M_IFADDR, M_WAITOK);
220 if (entry == NULL) {
221 error = ENOMEM;
222 }
223 }
224
225 if (error == 0) {
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++;
233
234 if (sock_locked)
235 socket_unlock(so, 0);
236 error = entry->sfe_filter->sf_filter.sf_attach(&entry->sfe_cookie, so);
237 if (sock_locked)
238 socket_lock(so, 0);
239
240 filter->sf_usecount--;
241
242 /* If the attach function returns an error, this filter is not attached */
243 if (error) {
244 FREE(entry, M_IFADDR);
245 entry = NULL;
246 }
247 }
248 }
249
250 if (error == 0) {
251 /* Put the entry in the socket list */
252 entry->sfe_next_onsocket = so->so_filt;
253 so->so_filt = entry;
254
255 /* Put the entry in the filter list */
256 entry->sfe_next_onfilter = filter->sf_entry_head;
257 filter->sf_entry_head = entry;
258
259 /* Incremenet the parent filter's usecount */
260 filter->sf_usecount++;
261 }
262
263 if (didlock) {
264 lck_mtx_unlock(sock_filter_lock);
265 }
266
267 return error;
268 }
269
270
271 /* sflt_detach_private
272 *
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.
277 */
278
279 static void
280 sflt_detach_private(
281 struct socket_filter_entry *entry,
282 int unregistering)
283 {
284 struct socket_filter_entry **next_ptr;
285 int detached = 0;
286 int found = 0;
287
288 if (unregistering) {
289 socket_lock(entry->sfe_socket, 0);
290 }
291
292 /*
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.
296 */
297 lck_mtx_lock(sock_filter_lock);
298 if (!unregistering) {
299 if ((entry->sfe_flags & SFEF_UNREGISTERING) != 0) {
300 /*
301 * Another thread is unregistering the filter, we need to
302 * avoid detaching the filter here so the socket won't go
303 * away.
304 */
305 lck_mtx_unlock(sock_filter_lock);
306 return;
307 }
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) {
311 found = 1;
312 *next_ptr = entry->sfe_next_onfilter;
313 break;
314 }
315 }
316
317 if (!found && (entry->sfe_flags & SFEF_DETACHUSEZERO) == 0) {
318 lck_mtx_unlock(sock_filter_lock);
319 return;
320 }
321 }
322 else {
323 /*
324 * Clear the removing flag. We will perform the detach here or
325 * request a delayed deatch.
326 */
327 entry->sfe_flags &= ~SFEF_UNREGISTERING;
328 }
329
330 if (entry->sfe_socket->so_filteruse != 0) {
331 entry->sfe_flags |= SFEF_DETACHUSEZERO;
332 lck_mtx_unlock(sock_filter_lock);
333 return;
334 }
335 else {
336 /*
337 * Check if we are removing the last attached filter and
338 * the parent filter is being unregistered.
339 */
340 entry->sfe_filter->sf_usecount--;
341 if ((entry->sfe_filter->sf_usecount == 0) &&
342 (entry->sfe_filter->sf_flags & SFF_DETACHING) != 0)
343 detached = 1;
344 }
345 lck_mtx_unlock(sock_filter_lock);
346
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;
352 break;
353 }
354 }
355
356 if (entry->sfe_filter->sf_filter.sf_detach)
357 entry->sfe_filter->sf_filter.sf_detach(entry->sfe_cookie, entry->sfe_socket);
358
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);
362 }
363
364 if (unregistering)
365 socket_unlock(entry->sfe_socket, 1);
366
367 FREE(entry, M_IFADDR);
368 }
369
370 errno_t
371 sflt_attach(
372 socket_t socket,
373 sflt_handle handle)
374 {
375 if (socket == NULL || handle == 0)
376 return EINVAL;
377
378 return sflt_attach_private(socket, NULL, handle, 0);
379 }
380
381 errno_t
382 sflt_detach(
383 socket_t socket,
384 sflt_handle handle)
385 {
386 struct socket_filter_entry *filter;
387 errno_t result = 0;
388
389 if (socket == NULL || handle == 0)
390 return EINVAL;
391
392 socket_lock(socket, 1);
393
394 for (filter = socket->so_filt; filter;
395 filter = filter->sfe_next_onsocket) {
396 if (filter->sfe_filter->sf_filter.sf_handle == handle)
397 break;
398 }
399
400 if (filter != NULL) {
401 sflt_detach_private(filter, 0);
402 }
403 else {
404 socket->so_filt = NULL;
405 result = ENOENT;
406 }
407
408 socket_unlock(socket, 1);
409
410 return result;
411 }
412
413
414 errno_t
415 sflt_register(
416 const struct sflt_filter *filter,
417 int domain,
418 int type,
419 int protocol)
420 {
421 struct socket_filter *sock_filt = NULL;
422 struct socket_filter *match = NULL;
423 int error = 0;
424 struct protosw *pr = pffindproto(domain, protocol, type);
425 unsigned int len;
426
427 if (pr == NULL)
428 return ENOENT;
429
430 if (filter->sf_attach == NULL || filter->sf_detach == NULL ||
431 filter->sf_handle == 0 || filter->sf_name == NULL)
432 return EINVAL;
433
434 /* Allocate the socket filter */
435 MALLOC(sock_filt, struct socket_filter *, sizeof (*sock_filt),
436 M_IFADDR, M_WAITOK);
437 if (sock_filt == NULL) {
438 return ENOBUFS;
439 }
440
441 bzero(sock_filt, sizeof (*sock_filt));
442
443 /* Legacy sflt_filter length; current structure minus extended */
444 len = sizeof (*filter) - sizeof (struct sflt_filter_ext);
445 /*
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.
449 */
450 if (filter->sf_flags & SFLT_EXTENDED) {
451 unsigned int ext_len = filter->sf_len;
452
453 if (ext_len > sizeof (struct sflt_filter_ext))
454 ext_len = sizeof (struct sflt_filter_ext);
455
456 len += ext_len;
457 }
458 bcopy(filter, &sock_filt->sf_filter, len);
459
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) {
465 break;
466 }
467 }
468
469 /* Add the entry only if there was no existing entry */
470 if (match == NULL) {
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,
474 sf_protosw_next);
475 sock_filt->sf_proto = pr;
476 }
477 }
478 lck_mtx_unlock(sock_filter_lock);
479
480 if (match != NULL) {
481 FREE(sock_filt, M_IFADDR);
482 return EEXIST;
483 }
484
485 return error;
486 }
487
488 errno_t
489 sflt_unregister(
490 sflt_handle handle)
491 {
492 struct socket_filter *filter;
493 struct socket_filter_entry *entry_head = NULL;
494 struct socket_filter_entry *next_entry = NULL;
495
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)
500 break;
501 }
502
503 if (filter) {
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);
507 }
508 entry_head = filter->sf_entry_head;
509 filter->sf_entry_head = NULL;
510 filter->sf_flags |= SFF_DETACHING;
511
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 */
517 }
518 }
519
520 lck_mtx_unlock(sock_filter_lock);
521
522 if (filter == NULL)
523 return ENOENT;
524
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);
529 } else {
530 while (entry_head) {
531 next_entry = entry_head->sfe_next_onfilter;
532 sflt_detach_private(entry_head, 1);
533 entry_head = next_entry;
534 }
535 }
536
537 return 0;
538 }
539
540 errno_t
541 sock_inject_data_in(
542 socket_t so,
543 const struct sockaddr* from,
544 mbuf_t data,
545 mbuf_t control,
546 sflt_data_flag_t flags)
547 {
548 int error = 0;
549 if (so == NULL || data == NULL) return EINVAL;
550
551 if (flags & sock_data_filt_flag_oob) {
552 return ENOTSUP;
553 }
554
555 socket_lock(so, 1);
556
557 if (from) {
558 if (sbappendaddr(&so->so_rcv, (struct sockaddr*)from, data,
559 control, NULL))
560 sorwakeup(so);
561 goto done;
562 }
563
564 if (control) {
565 if (sbappendcontrol(&so->so_rcv, data, control, NULL))
566 sorwakeup(so);
567 goto done;
568 }
569
570 if (flags & sock_data_filt_flag_record) {
571 if (control || from) {
572 error = EINVAL;
573 goto done;
574 }
575 if (sbappendrecord(&so->so_rcv, (struct mbuf*)data))
576 sorwakeup(so);
577 goto done;
578 }
579
580 if (sbappend(&so->so_rcv, data))
581 sorwakeup(so);
582 done:
583 socket_unlock(so, 1);
584 return error;
585 }
586
587 errno_t
588 sock_inject_data_out(
589 socket_t so,
590 const struct sockaddr* to,
591 mbuf_t data,
592 mbuf_t control,
593 sflt_data_flag_t flags)
594 {
595 int sosendflags = 0;
596 if (flags & sock_data_filt_flag_oob) sosendflags = MSG_OOB;
597 return sosend(so, (struct sockaddr*)to, NULL,
598 data, control, sosendflags);
599 }
600
601 sockopt_dir
602 sockopt_direction(
603 sockopt_t sopt)
604 {
605 return (sopt->sopt_dir == SOPT_GET) ? sockopt_get : sockopt_set;
606 }
607
608 int
609 sockopt_level(
610 sockopt_t sopt)
611 {
612 return sopt->sopt_level;
613 }
614
615 int
616 sockopt_name(
617 sockopt_t sopt)
618 {
619 return sopt->sopt_name;
620 }
621
622 size_t
623 sockopt_valsize(
624 sockopt_t sopt)
625 {
626 return sopt->sopt_valsize;
627 }
628
629 errno_t
630 sockopt_copyin(
631 sockopt_t sopt,
632 void *data,
633 size_t len)
634 {
635 return sooptcopyin(sopt, data, len, len);
636 }
637
638 errno_t
639 sockopt_copyout(
640 sockopt_t sopt,
641 void *data,
642 size_t len)
643 {
644 return sooptcopyout(sopt, data, len);
645 }