2 * Copyright (c) 1998-2013 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@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)uipc_domain.c 8.3 (Berkeley) 2/14/95
64 #include <sys/param.h>
65 #include <sys/socket.h>
66 #include <sys/protosw.h>
67 #include <sys/domain.h>
68 #include <sys/mcache.h>
71 #include <sys/kernel.h>
72 #include <sys/systm.h>
73 #include <sys/proc_internal.h>
74 #include <sys/sysctl.h>
75 #include <sys/syslog.h>
76 #include <sys/queue.h>
80 #include <mach/boolean.h>
81 #include <pexpert/pexpert.h>
83 static void pr_init_old(struct protosw
*, struct domain
*);
84 static void init_proto(struct protosw
*, struct domain
*);
85 static void attach_proto(struct protosw
*, struct domain
*);
86 static void detach_proto(struct protosw
*, struct domain
*);
87 static void dom_init_old(struct domain
*);
88 static void init_domain(struct domain
*);
89 static void attach_domain(struct domain
*);
90 static void detach_domain(struct domain
*);
91 static struct protosw
*pffindprotonotype_locked(int, int, int);
92 static struct domain
*pffinddomain_locked(int);
94 static boolean_t domain_timeout_run
; /* domain timer is scheduled to run */
95 static boolean_t domain_draining
;
96 static void domain_sched_timeout(void);
97 static void domain_timeout(void *);
99 lck_grp_t
*domain_proto_mtx_grp
;
100 lck_attr_t
*domain_proto_mtx_attr
;
101 static lck_grp_attr_t
*domain_proto_mtx_grp_attr
;
102 decl_lck_mtx_data(static, domain_proto_mtx
);
103 decl_lck_mtx_data(static, domain_timeout_mtx
);
105 static u_int64_t _net_uptime
;
107 #if (DEVELOPMENT || DEBUG)
109 SYSCTL_DECL(_kern_ipc
);
111 static int sysctl_do_drain_domains SYSCTL_HANDLER_ARGS
;
113 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, do_drain_domains
,
114 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_LOCKED
,
116 sysctl_do_drain_domains
, "I", "force manual drain domains");
118 #endif /* DEVELOPMENT || DEBUG */
121 pr_init_old(struct protosw
*pp
, struct domain
*dp
)
124 VERIFY(pp
->pr_flags
& PR_OLD
);
125 VERIFY(pp
->pr_old
!= NULL
);
127 if (pp
->pr_old
->pr_init
!= NULL
)
128 pp
->pr_old
->pr_init();
132 init_proto(struct protosw
*pp
, struct domain
*dp
)
134 VERIFY(pp
->pr_flags
& PR_ATTACHED
);
136 if (!(pp
->pr_flags
& PR_INITIALIZED
)) {
137 TAILQ_INIT(&pp
->pr_filter_head
);
138 if (pp
->pr_init
!= NULL
)
140 pp
->pr_flags
|= PR_INITIALIZED
;
145 attach_proto(struct protosw
*pp
, struct domain
*dp
)
147 domain_proto_mtx_lock_assert_held();
148 VERIFY(!(pp
->pr_flags
& PR_ATTACHED
));
149 VERIFY(pp
->pr_domain
== NULL
);
150 VERIFY(pp
->pr_protosw
== NULL
);
152 TAILQ_INSERT_TAIL(&dp
->dom_protosw
, pp
, pr_entry
);
153 pp
->pr_flags
|= PR_ATTACHED
;
157 /* do some cleaning up on user request callbacks */
158 pru_sanitize(pp
->pr_usrreqs
);
162 detach_proto(struct protosw
*pp
, struct domain
*dp
)
164 domain_proto_mtx_lock_assert_held();
165 VERIFY(pp
->pr_flags
& PR_ATTACHED
);
166 VERIFY(pp
->pr_domain
== dp
);
167 VERIFY(pp
->pr_protosw
== pp
);
169 TAILQ_REMOVE(&dp
->dom_protosw
, pp
, pr_entry
);
170 pp
->pr_flags
&= ~PR_ATTACHED
;
171 pp
->pr_domain
= NULL
;
172 pp
->pr_protosw
= NULL
;
176 dom_init_old(struct domain
*dp
)
178 VERIFY(dp
->dom_flags
& DOM_OLD
);
179 VERIFY(dp
->dom_old
!= NULL
);
181 if (dp
->dom_old
->dom_init
!= NULL
)
182 dp
->dom_old
->dom_init();
186 init_domain(struct domain
*dp
)
188 VERIFY(dp
->dom_flags
& DOM_ATTACHED
);
190 if (!(dp
->dom_flags
& DOM_INITIALIZED
)) {
191 lck_mtx_init(&dp
->dom_mtx_s
, domain_proto_mtx_grp
,
192 domain_proto_mtx_attr
);
193 dp
->dom_mtx
= &dp
->dom_mtx_s
;
194 TAILQ_INIT(&dp
->dom_protosw
);
195 if (dp
->dom_init
!= NULL
)
197 dp
->dom_flags
|= DOM_INITIALIZED
;
200 /* Recompute for new protocol */
201 if (_max_linkhdr
< 16) /* XXX - Sheesh; everything's ether? */
203 _max_linkhdr
= max_linkhdr
; /* round it up */
205 if (dp
->dom_protohdrlen
> _max_protohdr
)
206 _max_protohdr
= dp
->dom_protohdrlen
;
207 _max_protohdr
= max_protohdr
; /* round it up */
209 max_hdr
= max_linkhdr
+ max_protohdr
;
210 max_datalen
= MHLEN
- max_hdr
;
214 attach_domain(struct domain
*dp
)
216 domain_proto_mtx_lock_assert_held();
217 VERIFY(!(dp
->dom_flags
& DOM_ATTACHED
));
219 TAILQ_INSERT_TAIL(&domains
, dp
, dom_entry
);
220 dp
->dom_flags
|= DOM_ATTACHED
;
224 detach_domain(struct domain
*dp
)
226 domain_proto_mtx_lock_assert_held();
227 VERIFY(dp
->dom_flags
& DOM_ATTACHED
);
229 TAILQ_REMOVE(&domains
, dp
, dom_entry
);
230 dp
->dom_flags
&= ~DOM_ATTACHED
;
232 if (dp
->dom_flags
& DOM_OLD
) {
233 struct domain_old
*odp
= dp
->dom_old
;
236 odp
->dom_next
= NULL
;
242 * Exported (private) routine, indirection of net_add_domain.
245 net_add_domain_old(struct domain_old
*odp
)
248 domain_guard_t guard
;
252 guard
= domain_guard_deploy();
253 if ((dp
= pffinddomain_locked(odp
->dom_family
)) != NULL
) {
255 * There is really nothing better than to panic here,
256 * as the caller would not have been able to handle
257 * any failures otherwise.
259 panic("%s: domain (%d,%s) already exists for %s\n", __func__
,
260 dp
->dom_family
, dp
->dom_name
, odp
->dom_name
);
264 /* Make sure nothing is currently pointing to the odp. */
265 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
266 if (dp
->dom_old
== odp
) {
267 panic("%s: domain %p (%d,%s) is already "
268 "associated with %p (%d,%s)\n", __func__
,
269 odp
, odp
->dom_family
, odp
->dom_name
, dp
,
270 dp
->dom_family
, dp
->dom_name
);
275 if (odp
->dom_protosw
!= NULL
) {
276 panic("%s: domain (%d,%s) protocols need to added "
277 "via net_add_proto\n", __func__
, odp
->dom_family
,
282 dp
= _MALLOC(sizeof (*dp
), M_TEMP
, M_WAITOK
| M_ZERO
);
285 * There is really nothing better than to panic here,
286 * as the caller would not have been able to handle
287 * any failures otherwise.
289 panic("%s: unable to allocate memory for domain family "
290 "%d (%s)\n", __func__
, odp
->dom_family
, odp
->dom_name
);
294 /* Copy everything but dom_init, dom_mtx, dom_next and dom_refs */
295 dp
->dom_family
= odp
->dom_family
;
296 dp
->dom_flags
= (odp
->dom_flags
& DOMF_USERFLAGS
) | DOM_OLD
;
297 dp
->dom_name
= odp
->dom_name
;
298 dp
->dom_init
= dom_init_old
;
299 dp
->dom_externalize
= odp
->dom_externalize
;
300 dp
->dom_dispose
= odp
->dom_dispose
;
301 dp
->dom_rtattach
= odp
->dom_rtattach
;
302 dp
->dom_rtoffset
= odp
->dom_rtoffset
;
303 dp
->dom_maxrtkey
= odp
->dom_maxrtkey
;
304 dp
->dom_protohdrlen
= odp
->dom_protohdrlen
;
310 /* Point the mutex back to the internal structure's */
311 odp
->dom_mtx
= dp
->dom_mtx
;
312 domain_guard_release(guard
);
316 * Exported (private) routine, indirection of net_del_domain.
319 net_del_domain_old(struct domain_old
*odp
)
321 struct domain
*dp1
, *dp2
;
323 domain_guard_t guard
;
327 guard
= domain_guard_deploy();
328 if (odp
->dom_refs
!= 0) {
333 TAILQ_FOREACH_SAFE(dp1
, &domains
, dom_entry
, dp2
) {
334 if (!(dp1
->dom_flags
& DOM_OLD
))
336 VERIFY(dp1
->dom_old
!= NULL
);
337 if (odp
== dp1
->dom_old
)
341 struct protosw
*pp1
, *pp2
;
343 VERIFY(dp1
->dom_flags
& DOM_OLD
);
344 VERIFY(dp1
->dom_old
== odp
);
346 /* Remove all protocols attached to this domain */
347 TAILQ_FOREACH_SAFE(pp1
, &dp1
->dom_protosw
, pr_entry
, pp2
) {
348 detach_proto(pp1
, dp1
);
349 if (pp1
->pr_usrreqs
->pru_flags
& PRUF_OLD
)
350 FREE(pp1
->pr_usrreqs
, M_TEMP
);
351 if (pp1
->pr_flags
& PR_OLD
)
358 error
= EPFNOSUPPORT
;
361 domain_guard_release(guard
);
366 * Internal routine, not exported.
368 * net_add_proto - link a protosw into a domain's protosw chain
370 * NOTE: Caller must have acquired domain_proto_mtx
373 net_add_proto(struct protosw
*pp
, struct domain
*dp
, int doinit
)
378 * This could be called as part of initializing the domain,
379 * and thus DOM_INITIALIZED may not be set (yet).
381 domain_proto_mtx_lock_assert_held();
382 VERIFY(!(pp
->pr_flags
& PR_ATTACHED
));
384 /* pr_domain is set only after the protocol is attached */
385 if (pp
->pr_domain
!= NULL
) {
386 panic("%s: domain (%d,%s), proto %d has non-NULL pr_domain!\n",
387 __func__
, dp
->dom_family
, dp
->dom_name
, pp
->pr_protocol
);
391 if (pp
->pr_usrreqs
== NULL
) {
392 panic("%s: domain (%d,%s), proto %d has no usrreqs!\n",
393 __func__
, dp
->dom_family
, dp
->dom_name
, pp
->pr_protocol
);
397 TAILQ_FOREACH(pp1
, &dp
->dom_protosw
, pr_entry
) {
398 if (pp1
->pr_type
== pp
->pr_type
&&
399 pp1
->pr_protocol
== pp
->pr_protocol
)
403 attach_proto(pp
, dp
);
405 net_init_proto(pp
, dp
);
411 net_init_proto(struct protosw
*pp
, struct domain
*dp
)
414 * This could be called as part of initializing the domain,
415 * and thus DOM_INITIALIZED may not be set (yet). The protocol
416 * must have been attached via net_addr_protosw() by now.
418 domain_proto_mtx_lock_assert_held();
419 VERIFY(pp
->pr_flags
& PR_ATTACHED
);
425 * Exported (private) routine, indirection of net_add_proto.
428 net_add_proto_old(struct protosw_old
*opp
, struct domain_old
*odp
)
430 struct pr_usrreqs_old
*opru
;
431 struct pr_usrreqs
*pru
= NULL
;
432 struct protosw
*pp
= NULL
, *pp1
;
435 domain_guard_t guard
;
438 * This could be called as part of initializing the domain,
439 * and thus DOM_INITIALIZED may not be set (yet).
441 guard
= domain_guard_deploy();
443 /* Make sure the domain has been added via net_add_domain */
444 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
445 if (!(dp
->dom_flags
& DOM_OLD
))
447 if (dp
->dom_old
== odp
)
455 TAILQ_FOREACH(pp1
, &dp
->dom_protosw
, pr_entry
) {
456 if (pp1
->pr_type
== opp
->pr_type
&&
457 pp1
->pr_protocol
== opp
->pr_protocol
) {
463 if ((opru
= opp
->pr_usrreqs
) == NULL
) {
464 panic("%s: domain (%d,%s), proto %d has no usrreqs!\n",
465 __func__
, odp
->dom_family
, odp
->dom_name
, opp
->pr_protocol
);
469 pru
= _MALLOC(sizeof (*pru
), M_TEMP
, M_WAITOK
| M_ZERO
);
475 pru
->pru_flags
= PRUF_OLD
;
476 pru
->pru_abort
= opru
->pru_abort
;
477 pru
->pru_accept
= opru
->pru_accept
;
478 pru
->pru_attach
= opru
->pru_attach
;
479 pru
->pru_bind
= opru
->pru_bind
;
480 pru
->pru_connect
= opru
->pru_connect
;
481 pru
->pru_connect2
= opru
->pru_connect2
;
482 pru
->pru_control
= opru
->pru_control
;
483 pru
->pru_detach
= opru
->pru_detach
;
484 pru
->pru_disconnect
= opru
->pru_disconnect
;
485 pru
->pru_listen
= opru
->pru_listen
;
486 pru
->pru_peeraddr
= opru
->pru_peeraddr
;
487 pru
->pru_rcvd
= opru
->pru_rcvd
;
488 pru
->pru_rcvoob
= opru
->pru_rcvoob
;
489 pru
->pru_send
= opru
->pru_send
;
490 pru
->pru_sense
= opru
->pru_sense
;
491 pru
->pru_shutdown
= opru
->pru_shutdown
;
492 pru
->pru_sockaddr
= opru
->pru_sockaddr
;
493 pru
->pru_sosend
= opru
->pru_sosend
;
494 pru
->pru_soreceive
= opru
->pru_soreceive
;
495 pru
->pru_sopoll
= opru
->pru_sopoll
;
497 pp
= _MALLOC(sizeof (*pp
), M_TEMP
, M_WAITOK
| M_ZERO
);
504 * Protocol fast and slow timers are now deprecated.
506 if (opp
->pr_unused
!= NULL
) {
507 printf("%s: domain (%d,%s), proto %d: pr_fasttimo is "
508 "deprecated and won't be called\n", __func__
,
509 odp
->dom_family
, odp
->dom_name
, opp
->pr_protocol
);
511 if (opp
->pr_unused2
!= NULL
) {
512 printf("%s: domain (%d,%s), proto %d: pr_slowtimo is "
513 "deprecated and won't be called\n", __func__
,
514 odp
->dom_family
, odp
->dom_name
, opp
->pr_protocol
);
517 /* Copy everything but pr_init, pr_next, pr_domain, pr_protosw */
518 pp
->pr_type
= opp
->pr_type
;
519 pp
->pr_protocol
= opp
->pr_protocol
;
520 pp
->pr_flags
= (opp
->pr_flags
& PRF_USERFLAGS
) | PR_OLD
;
521 pp
->pr_input
= opp
->pr_input
;
522 pp
->pr_output
= opp
->pr_output
;
523 pp
->pr_ctlinput
= opp
->pr_ctlinput
;
524 pp
->pr_ctloutput
= opp
->pr_ctloutput
;
525 pp
->pr_usrreqs
= pru
;
526 pp
->pr_init
= pr_init_old
;
527 pp
->pr_drain
= opp
->pr_drain
;
528 pp
->pr_sysctl
= opp
->pr_sysctl
;
529 pp
->pr_lock
= opp
->pr_lock
;
530 pp
->pr_unlock
= opp
->pr_unlock
;
531 pp
->pr_getlock
= opp
->pr_getlock
;
534 /* attach as well as initialize */
535 attach_proto(pp
, dp
);
536 net_init_proto(pp
, dp
);
539 printf("%s: domain (%d,%s), proto %d: failed to attach, "
540 "error %d\n", __func__
, odp
->dom_family
,
541 odp
->dom_name
, opp
->pr_protocol
, error
);
549 domain_guard_release(guard
);
554 * Internal routine, not exported.
556 * net_del_proto - remove a protosw from a domain's protosw chain.
557 * Search the protosw chain for the element with matching data.
558 * Then unlink and return.
560 * NOTE: Caller must have acquired domain_proto_mtx
563 net_del_proto(int type
, int protocol
, struct domain
*dp
)
568 * This could be called as part of initializing the domain,
569 * and thus DOM_INITIALIZED may not be set (yet).
571 domain_proto_mtx_lock_assert_held();
573 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
574 if (pp
->pr_type
== type
&& pp
->pr_protocol
== protocol
)
580 detach_proto(pp
, dp
);
581 if (pp
->pr_usrreqs
->pru_flags
& PRUF_OLD
)
582 FREE(pp
->pr_usrreqs
, M_TEMP
);
583 if (pp
->pr_flags
& PR_OLD
)
590 * Exported (private) routine, indirection of net_del_proto.
593 net_del_proto_old(int type
, int protocol
, struct domain_old
*odp
)
598 domain_guard_t guard
;
601 * This could be called as part of initializing the domain,
602 * and thus DOM_INITIALIZED may not be set (yet).
604 guard
= domain_guard_deploy();
606 /* Make sure the domain has been added via net_add_domain */
607 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
608 if (!(dp
->dom_flags
& DOM_OLD
))
610 if (dp
->dom_old
== odp
)
618 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
619 if (pp
->pr_type
== type
&& pp
->pr_protocol
== protocol
)
626 detach_proto(pp
, dp
);
627 if (pp
->pr_usrreqs
->pru_flags
& PRUF_OLD
)
628 FREE(pp
->pr_usrreqs
, M_TEMP
);
629 if (pp
->pr_flags
& PR_OLD
)
633 domain_guard_release(guard
);
638 domain_sched_timeout(void)
640 lck_mtx_assert(&domain_timeout_mtx
, LCK_MTX_ASSERT_OWNED
);
642 if (!domain_timeout_run
&& domain_draining
) {
643 domain_timeout_run
= TRUE
;
644 timeout(domain_timeout
, NULL
, hz
);
649 net_drain_domains(void)
651 lck_mtx_lock(&domain_timeout_mtx
);
652 domain_draining
= TRUE
;
653 domain_sched_timeout();
654 lck_mtx_unlock(&domain_timeout_mtx
);
658 extern struct domain inet6domain_s
;
661 extern struct domain keydomain_s
;
664 extern struct domain routedomain_s
, ndrvdomain_s
, inetdomain_s
;
665 extern struct domain systemdomain_s
, localdomain_s
;
668 extern struct domain mpdomain_s
;
669 #endif /* MULTIPATH */
672 domain_timeout(void *arg
)
677 domain_guard_t guard
;
679 lck_mtx_lock(&domain_timeout_mtx
);
680 if (domain_draining
) {
681 domain_draining
= FALSE
;
682 lck_mtx_unlock(&domain_timeout_mtx
);
684 guard
= domain_guard_deploy();
685 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
686 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
687 if (pp
->pr_drain
!= NULL
)
691 domain_guard_release(guard
);
693 lck_mtx_lock(&domain_timeout_mtx
);
696 /* re-arm the timer if there's work to do */
697 domain_timeout_run
= FALSE
;
698 domain_sched_timeout();
699 lck_mtx_unlock(&domain_timeout_mtx
);
706 domain_guard_t guard
;
709 * allocate lock group attribute and group for domain mutexes
711 domain_proto_mtx_grp_attr
= lck_grp_attr_alloc_init();
713 domain_proto_mtx_grp
= lck_grp_alloc_init("domain",
714 domain_proto_mtx_grp_attr
);
717 * allocate the lock attribute for per domain mutexes
719 domain_proto_mtx_attr
= lck_attr_alloc_init();
721 lck_mtx_init(&domain_proto_mtx
, domain_proto_mtx_grp
,
722 domain_proto_mtx_attr
);
723 lck_mtx_init(&domain_timeout_mtx
, domain_proto_mtx_grp
,
724 domain_proto_mtx_attr
);
726 guard
= domain_guard_deploy();
728 * Add all the static domains to the domains list. route domain
729 * gets added and initialized last, since we need it to attach
730 * rt_tables[] to everything that's already there. This also
731 * means that domains added after this point won't get their
732 * dom_rtattach() called on rt_tables[].
734 attach_domain(&inetdomain_s
);
736 attach_domain(&inet6domain_s
);
739 attach_domain(&mpdomain_s
);
740 #endif /* MULTIPATH */
741 attach_domain(&systemdomain_s
);
742 attach_domain(&localdomain_s
);
744 attach_domain(&keydomain_s
);
746 attach_domain(&ndrvdomain_s
);
747 attach_domain(&routedomain_s
); /* must be last domain */
750 * Now ask them all to init (XXX including the routing domain,
753 TAILQ_FOREACH(dp
, &domains
, dom_entry
)
756 domain_guard_release(guard
);
759 static __inline__
struct domain
*
760 pffinddomain_locked(int pf
)
764 domain_proto_mtx_lock_assert_held();
766 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
767 if (dp
->dom_family
== pf
)
774 pffindtype(int family
, int type
)
776 struct protosw
*pp
= NULL
;
778 domain_guard_t guard
;
780 guard
= domain_guard_deploy();
781 if ((dp
= pffinddomain_locked(family
)) == NULL
)
784 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
785 if (pp
->pr_type
!= 0 && pp
->pr_type
== type
)
789 domain_guard_release(guard
);
794 * Internal routine, not exported.
800 domain_guard_t guard
;
802 guard
= domain_guard_deploy();
803 dp
= pffinddomain_locked(pf
);
804 domain_guard_release(guard
);
809 * Exported (private) routine, indirection of pffinddomain.
812 pffinddomain_old(int pf
)
814 struct domain_old
*odp
= NULL
;
816 domain_guard_t guard
;
818 guard
= domain_guard_deploy();
819 if ((dp
= pffinddomain_locked(pf
)) != NULL
&& (dp
->dom_flags
& DOM_OLD
))
821 domain_guard_release(guard
);
826 * Internal routine, not exported.
829 pffindproto(int family
, int protocol
, int type
)
832 domain_guard_t guard
;
834 guard
= domain_guard_deploy();
835 pp
= pffindproto_locked(family
, protocol
, type
);
836 domain_guard_release(guard
);
841 pffindproto_locked(int family
, int protocol
, int type
)
843 struct protosw
*maybe
= NULL
;
847 domain_proto_mtx_lock_assert_held();
852 dp
= pffinddomain_locked(family
);
856 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
857 if ((pp
->pr_protocol
== protocol
) && (pp
->pr_type
== type
))
860 if (type
== SOCK_RAW
&& pp
->pr_type
== SOCK_RAW
&&
861 pp
->pr_protocol
== 0 && maybe
== NULL
)
868 * Exported (private) routine, indirection of pffindproto.
871 pffindproto_old(int family
, int protocol
, int type
)
873 struct protosw_old
*opr
= NULL
;
875 domain_guard_t guard
;
877 guard
= domain_guard_deploy();
878 if ((pp
= pffindproto_locked(family
, protocol
, type
)) != NULL
&&
879 (pp
->pr_flags
& PR_OLD
))
881 domain_guard_release(guard
);
885 static struct protosw
*
886 pffindprotonotype_locked(int family
, int protocol
, int type
)
892 domain_proto_mtx_lock_assert_held();
897 dp
= pffinddomain_locked(family
);
901 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
902 if (pp
->pr_protocol
== protocol
)
909 pffindprotonotype(int family
, int protocol
)
912 domain_guard_t guard
;
917 guard
= domain_guard_deploy();
918 pp
= pffindprotonotype_locked(family
, protocol
, 0);
919 domain_guard_release(guard
);
924 pfctlinput(int cmd
, struct sockaddr
*sa
)
926 pfctlinput2(cmd
, sa
, NULL
);
930 pfctlinput2(int cmd
, struct sockaddr
*sa
, void *ctlparam
)
934 domain_guard_t guard
;
939 guard
= domain_guard_deploy();
940 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
941 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
942 if (pp
->pr_ctlinput
!= NULL
)
943 (*pp
->pr_ctlinput
)(cmd
, sa
, ctlparam
);
946 domain_guard_release(guard
);
950 net_update_uptime(void)
955 _net_uptime
= tv
.tv_sec
;
957 * Round up the timer to the nearest integer value because otherwise
958 * we might setup networking timers that are off by almost 1 second.
960 if (tv
.tv_usec
> 500000)
965 net_update_uptime_secs(uint64_t secs
)
971 * Convert our uin64_t net_uptime to a struct timeval.
974 net_uptime2timeval(struct timeval
*tv
)
980 tv
->tv_sec
= net_uptime();
984 * An alternative way to obtain the coarse-grained uptime (in seconds)
985 * for networking code which do not require high-precision timestamp,
986 * as this is significantly cheaper than microuptime().
991 if (_net_uptime
== 0)
994 return (_net_uptime
);
998 domain_proto_mtx_lock_assert_held(void)
1000 lck_mtx_assert(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1004 domain_proto_mtx_lock_assert_notheld(void)
1006 lck_mtx_assert(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1010 domain_guard_deploy(void)
1012 net_thread_marks_t marks
;
1014 marks
= net_thread_marks_push(NET_THREAD_HELD_DOMAIN
);
1015 if (marks
!= net_thread_marks_none
) {
1016 lck_mtx_assert(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1017 lck_mtx_lock(&domain_proto_mtx
);
1020 lck_mtx_assert(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1022 return ((domain_guard_t
)(const void*)marks
);
1026 domain_guard_release(domain_guard_t guard
)
1028 net_thread_marks_t marks
= (net_thread_marks_t
)(const void*)guard
;
1030 if (marks
!= net_thread_marks_none
) {
1031 lck_mtx_assert(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1032 lck_mtx_unlock(&domain_proto_mtx
);
1033 net_thread_marks_pop(marks
);
1036 lck_mtx_assert(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1040 domain_unguard_deploy(void)
1042 net_thread_marks_t marks
;
1044 marks
= net_thread_unmarks_push(NET_THREAD_HELD_DOMAIN
);
1045 if (marks
!= net_thread_marks_none
) {
1046 lck_mtx_assert(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1047 lck_mtx_unlock(&domain_proto_mtx
);
1050 lck_mtx_assert(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1052 return ((domain_unguard_t
)(const void*)marks
);
1056 domain_unguard_release(domain_unguard_t unguard
)
1058 net_thread_marks_t marks
= (net_thread_marks_t
)(const void*)unguard
;
1060 if (marks
!= net_thread_marks_none
) {
1061 lck_mtx_assert(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1062 lck_mtx_lock(&domain_proto_mtx
);
1063 net_thread_unmarks_pop(marks
);
1066 lck_mtx_assert(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1069 #if (DEVELOPMENT || DEBUG)
1072 sysctl_do_drain_domains SYSCTL_HANDLER_ARGS
1074 #pragma unused(arg1, arg2)
1078 error
= sysctl_handle_int(oidp
, &dummy
, 0, req
);
1079 if (error
|| req
->newptr
== USER_ADDR_NULL
)
1082 net_drain_domains();
1087 #endif /* DEVELOPMENT || DEBUG */