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>
79 #include <net/nwk_wq.h>
81 #include <mach/boolean.h>
82 #include <pexpert/pexpert.h>
84 /* Eventhandler context for protocol events */
85 struct eventhandler_lists_ctxt protoctl_evhdlr_ctxt
;
87 static void pr_init_old(struct protosw
*, struct domain
*);
88 static void init_proto(struct protosw
*, struct domain
*);
89 static void attach_proto(struct protosw
*, struct domain
*);
90 static void detach_proto(struct protosw
*, struct domain
*);
91 static void dom_init_old(struct domain
*);
92 static void init_domain(struct domain
*);
93 static void attach_domain(struct domain
*);
94 static void detach_domain(struct domain
*);
95 static struct protosw
*pffindprotonotype_locked(int, int, int);
96 static struct domain
*pffinddomain_locked(int);
98 static boolean_t domain_timeout_run
; /* domain timer is scheduled to run */
99 static boolean_t domain_draining
;
100 static void domain_sched_timeout(void);
101 static void domain_timeout(void *);
103 lck_grp_t
*domain_proto_mtx_grp
;
104 lck_attr_t
*domain_proto_mtx_attr
;
105 static lck_grp_attr_t
*domain_proto_mtx_grp_attr
;
106 decl_lck_mtx_data(static, domain_proto_mtx
);
107 decl_lck_mtx_data(static, domain_timeout_mtx
);
109 u_int64_t _net_uptime
;
111 #if (DEVELOPMENT || DEBUG)
113 SYSCTL_DECL(_kern_ipc
);
115 static int sysctl_do_drain_domains SYSCTL_HANDLER_ARGS
;
117 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, do_drain_domains
,
118 CTLTYPE_INT
|CTLFLAG_RW
|CTLFLAG_LOCKED
,
120 sysctl_do_drain_domains
, "I", "force manual drain domains");
122 #endif /* DEVELOPMENT || DEBUG */
125 pr_init_old(struct protosw
*pp
, struct domain
*dp
)
128 VERIFY(pp
->pr_flags
& PR_OLD
);
129 VERIFY(pp
->pr_old
!= NULL
);
131 if (pp
->pr_old
->pr_init
!= NULL
)
132 pp
->pr_old
->pr_init();
136 init_proto(struct protosw
*pp
, struct domain
*dp
)
138 VERIFY(pp
->pr_flags
& PR_ATTACHED
);
140 if (!(pp
->pr_flags
& PR_INITIALIZED
)) {
141 TAILQ_INIT(&pp
->pr_filter_head
);
142 if (pp
->pr_init
!= NULL
)
144 pp
->pr_flags
|= PR_INITIALIZED
;
149 attach_proto(struct protosw
*pp
, struct domain
*dp
)
151 domain_proto_mtx_lock_assert_held();
152 VERIFY(!(pp
->pr_flags
& PR_ATTACHED
));
153 VERIFY(pp
->pr_domain
== NULL
);
154 VERIFY(pp
->pr_protosw
== NULL
);
156 TAILQ_INSERT_TAIL(&dp
->dom_protosw
, pp
, pr_entry
);
157 pp
->pr_flags
|= PR_ATTACHED
;
161 /* do some cleaning up on user request callbacks */
162 pru_sanitize(pp
->pr_usrreqs
);
166 detach_proto(struct protosw
*pp
, struct domain
*dp
)
168 domain_proto_mtx_lock_assert_held();
169 VERIFY(pp
->pr_flags
& PR_ATTACHED
);
170 VERIFY(pp
->pr_domain
== dp
);
171 VERIFY(pp
->pr_protosw
== pp
);
173 TAILQ_REMOVE(&dp
->dom_protosw
, pp
, pr_entry
);
174 pp
->pr_flags
&= ~PR_ATTACHED
;
175 pp
->pr_domain
= NULL
;
176 pp
->pr_protosw
= NULL
;
180 dom_init_old(struct domain
*dp
)
182 VERIFY(dp
->dom_flags
& DOM_OLD
);
183 VERIFY(dp
->dom_old
!= NULL
);
185 if (dp
->dom_old
->dom_init
!= NULL
)
186 dp
->dom_old
->dom_init();
190 init_domain(struct domain
*dp
)
192 VERIFY(dp
->dom_flags
& DOM_ATTACHED
);
194 if (!(dp
->dom_flags
& DOM_INITIALIZED
)) {
195 lck_mtx_init(&dp
->dom_mtx_s
, domain_proto_mtx_grp
,
196 domain_proto_mtx_attr
);
197 dp
->dom_mtx
= &dp
->dom_mtx_s
;
198 TAILQ_INIT(&dp
->dom_protosw
);
199 if (dp
->dom_init
!= NULL
)
201 dp
->dom_flags
|= DOM_INITIALIZED
;
204 /* Recompute for new protocol */
205 if (_max_linkhdr
< 16) /* XXX - Sheesh; everything's ether? */
207 _max_linkhdr
= max_linkhdr
; /* round it up */
209 if (dp
->dom_protohdrlen
> _max_protohdr
)
210 _max_protohdr
= dp
->dom_protohdrlen
;
211 _max_protohdr
= max_protohdr
; /* round it up */
213 max_hdr
= max_linkhdr
+ max_protohdr
;
214 max_datalen
= MHLEN
- max_hdr
;
218 attach_domain(struct domain
*dp
)
220 domain_proto_mtx_lock_assert_held();
221 VERIFY(!(dp
->dom_flags
& DOM_ATTACHED
));
223 TAILQ_INSERT_TAIL(&domains
, dp
, dom_entry
);
224 dp
->dom_flags
|= DOM_ATTACHED
;
228 detach_domain(struct domain
*dp
)
230 domain_proto_mtx_lock_assert_held();
231 VERIFY(dp
->dom_flags
& DOM_ATTACHED
);
233 TAILQ_REMOVE(&domains
, dp
, dom_entry
);
234 dp
->dom_flags
&= ~DOM_ATTACHED
;
236 if (dp
->dom_flags
& DOM_OLD
) {
237 struct domain_old
*odp
= dp
->dom_old
;
240 odp
->dom_next
= NULL
;
246 * Exported (private) routine, indirection of net_add_domain.
249 net_add_domain_old(struct domain_old
*odp
)
252 domain_guard_t guard
;
256 guard
= domain_guard_deploy();
257 if ((dp
= pffinddomain_locked(odp
->dom_family
)) != NULL
) {
259 * There is really nothing better than to panic here,
260 * as the caller would not have been able to handle
261 * any failures otherwise.
263 panic("%s: domain (%d,%s) already exists for %s\n", __func__
,
264 dp
->dom_family
, dp
->dom_name
, odp
->dom_name
);
268 /* Make sure nothing is currently pointing to the odp. */
269 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
270 if (dp
->dom_old
== odp
) {
271 panic("%s: domain %p (%d,%s) is already "
272 "associated with %p (%d,%s)\n", __func__
,
273 odp
, odp
->dom_family
, odp
->dom_name
, dp
,
274 dp
->dom_family
, dp
->dom_name
);
279 if (odp
->dom_protosw
!= NULL
) {
280 panic("%s: domain (%d,%s) protocols need to added "
281 "via net_add_proto\n", __func__
, odp
->dom_family
,
286 dp
= _MALLOC(sizeof (*dp
), M_TEMP
, M_WAITOK
| M_ZERO
);
289 * There is really nothing better than to panic here,
290 * as the caller would not have been able to handle
291 * any failures otherwise.
293 panic("%s: unable to allocate memory for domain family "
294 "%d (%s)\n", __func__
, odp
->dom_family
, odp
->dom_name
);
298 /* Copy everything but dom_init, dom_mtx, dom_next and dom_refs */
299 dp
->dom_family
= odp
->dom_family
;
300 dp
->dom_flags
= (odp
->dom_flags
& DOMF_USERFLAGS
) | DOM_OLD
;
301 dp
->dom_name
= odp
->dom_name
;
302 dp
->dom_init
= dom_init_old
;
303 dp
->dom_externalize
= odp
->dom_externalize
;
304 dp
->dom_dispose
= odp
->dom_dispose
;
305 dp
->dom_rtattach
= odp
->dom_rtattach
;
306 dp
->dom_rtoffset
= odp
->dom_rtoffset
;
307 dp
->dom_maxrtkey
= odp
->dom_maxrtkey
;
308 dp
->dom_protohdrlen
= odp
->dom_protohdrlen
;
314 /* Point the mutex back to the internal structure's */
315 odp
->dom_mtx
= dp
->dom_mtx
;
316 domain_guard_release(guard
);
320 * Exported (private) routine, indirection of net_del_domain.
323 net_del_domain_old(struct domain_old
*odp
)
325 struct domain
*dp1
, *dp2
;
327 domain_guard_t guard
;
331 guard
= domain_guard_deploy();
332 if (odp
->dom_refs
!= 0) {
337 TAILQ_FOREACH_SAFE(dp1
, &domains
, dom_entry
, dp2
) {
338 if (!(dp1
->dom_flags
& DOM_OLD
))
340 VERIFY(dp1
->dom_old
!= NULL
);
341 if (odp
== dp1
->dom_old
)
345 struct protosw
*pp1
, *pp2
;
347 VERIFY(dp1
->dom_flags
& DOM_OLD
);
348 VERIFY(dp1
->dom_old
== odp
);
350 /* Remove all protocols attached to this domain */
351 TAILQ_FOREACH_SAFE(pp1
, &dp1
->dom_protosw
, pr_entry
, pp2
) {
352 detach_proto(pp1
, dp1
);
353 if (pp1
->pr_usrreqs
->pru_flags
& PRUF_OLD
)
354 FREE(pp1
->pr_usrreqs
, M_TEMP
);
355 if (pp1
->pr_flags
& PR_OLD
)
362 error
= EPFNOSUPPORT
;
365 domain_guard_release(guard
);
370 * Internal routine, not exported.
372 * net_add_proto - link a protosw into a domain's protosw chain
374 * NOTE: Caller must have acquired domain_proto_mtx
377 net_add_proto(struct protosw
*pp
, struct domain
*dp
, int doinit
)
382 * This could be called as part of initializing the domain,
383 * and thus DOM_INITIALIZED may not be set (yet).
385 domain_proto_mtx_lock_assert_held();
386 VERIFY(!(pp
->pr_flags
& PR_ATTACHED
));
388 /* pr_domain is set only after the protocol is attached */
389 if (pp
->pr_domain
!= NULL
) {
390 panic("%s: domain (%d,%s), proto %d has non-NULL pr_domain!\n",
391 __func__
, dp
->dom_family
, dp
->dom_name
, pp
->pr_protocol
);
395 if (pp
->pr_usrreqs
== NULL
) {
396 panic("%s: domain (%d,%s), proto %d has no usrreqs!\n",
397 __func__
, dp
->dom_family
, dp
->dom_name
, pp
->pr_protocol
);
401 TAILQ_FOREACH(pp1
, &dp
->dom_protosw
, pr_entry
) {
402 if (pp1
->pr_type
== pp
->pr_type
&&
403 pp1
->pr_protocol
== pp
->pr_protocol
)
407 attach_proto(pp
, dp
);
409 net_init_proto(pp
, dp
);
415 net_init_proto(struct protosw
*pp
, struct domain
*dp
)
418 * This could be called as part of initializing the domain,
419 * and thus DOM_INITIALIZED may not be set (yet). The protocol
420 * must have been attached via net_addr_protosw() by now.
422 domain_proto_mtx_lock_assert_held();
423 VERIFY(pp
->pr_flags
& PR_ATTACHED
);
429 * Exported (private) routine, indirection of net_add_proto.
432 net_add_proto_old(struct protosw_old
*opp
, struct domain_old
*odp
)
434 struct pr_usrreqs_old
*opru
;
435 struct pr_usrreqs
*pru
= NULL
;
436 struct protosw
*pp
= NULL
, *pp1
;
439 domain_guard_t guard
;
442 * This could be called as part of initializing the domain,
443 * and thus DOM_INITIALIZED may not be set (yet).
445 guard
= domain_guard_deploy();
447 /* Make sure the domain has been added via net_add_domain */
448 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
449 if (!(dp
->dom_flags
& DOM_OLD
))
451 if (dp
->dom_old
== odp
)
459 TAILQ_FOREACH(pp1
, &dp
->dom_protosw
, pr_entry
) {
460 if (pp1
->pr_type
== opp
->pr_type
&&
461 pp1
->pr_protocol
== opp
->pr_protocol
) {
467 if ((opru
= opp
->pr_usrreqs
) == NULL
) {
468 panic("%s: domain (%d,%s), proto %d has no usrreqs!\n",
469 __func__
, odp
->dom_family
, odp
->dom_name
, opp
->pr_protocol
);
473 pru
= _MALLOC(sizeof (*pru
), M_TEMP
, M_WAITOK
| M_ZERO
);
479 pru
->pru_flags
= PRUF_OLD
;
480 pru
->pru_abort
= opru
->pru_abort
;
481 pru
->pru_accept
= opru
->pru_accept
;
482 pru
->pru_attach
= opru
->pru_attach
;
483 pru
->pru_bind
= opru
->pru_bind
;
484 pru
->pru_connect
= opru
->pru_connect
;
485 pru
->pru_connect2
= opru
->pru_connect2
;
486 pru
->pru_control
= opru
->pru_control
;
487 pru
->pru_detach
= opru
->pru_detach
;
488 pru
->pru_disconnect
= opru
->pru_disconnect
;
489 pru
->pru_listen
= opru
->pru_listen
;
490 pru
->pru_peeraddr
= opru
->pru_peeraddr
;
491 pru
->pru_rcvd
= opru
->pru_rcvd
;
492 pru
->pru_rcvoob
= opru
->pru_rcvoob
;
493 pru
->pru_send
= opru
->pru_send
;
494 pru
->pru_sense
= opru
->pru_sense
;
495 pru
->pru_shutdown
= opru
->pru_shutdown
;
496 pru
->pru_sockaddr
= opru
->pru_sockaddr
;
497 pru
->pru_sosend
= opru
->pru_sosend
;
498 pru
->pru_soreceive
= opru
->pru_soreceive
;
499 pru
->pru_sopoll
= opru
->pru_sopoll
;
501 pp
= _MALLOC(sizeof (*pp
), M_TEMP
, M_WAITOK
| M_ZERO
);
508 * Protocol fast and slow timers are now deprecated.
510 if (opp
->pr_unused
!= NULL
) {
511 printf("%s: domain (%d,%s), proto %d: pr_fasttimo is "
512 "deprecated and won't be called\n", __func__
,
513 odp
->dom_family
, odp
->dom_name
, opp
->pr_protocol
);
515 if (opp
->pr_unused2
!= NULL
) {
516 printf("%s: domain (%d,%s), proto %d: pr_slowtimo is "
517 "deprecated and won't be called\n", __func__
,
518 odp
->dom_family
, odp
->dom_name
, opp
->pr_protocol
);
521 /* Copy everything but pr_init, pr_next, pr_domain, pr_protosw */
522 pp
->pr_type
= opp
->pr_type
;
523 pp
->pr_protocol
= opp
->pr_protocol
;
524 pp
->pr_flags
= (opp
->pr_flags
& PRF_USERFLAGS
) | PR_OLD
;
525 pp
->pr_input
= opp
->pr_input
;
526 pp
->pr_output
= opp
->pr_output
;
527 pp
->pr_ctlinput
= opp
->pr_ctlinput
;
528 pp
->pr_ctloutput
= opp
->pr_ctloutput
;
529 pp
->pr_usrreqs
= pru
;
530 pp
->pr_init
= pr_init_old
;
531 pp
->pr_drain
= opp
->pr_drain
;
532 pp
->pr_sysctl
= opp
->pr_sysctl
;
533 pp
->pr_lock
= opp
->pr_lock
;
534 pp
->pr_unlock
= opp
->pr_unlock
;
535 pp
->pr_getlock
= opp
->pr_getlock
;
538 /* attach as well as initialize */
539 attach_proto(pp
, dp
);
540 net_init_proto(pp
, dp
);
543 printf("%s: domain (%d,%s), proto %d: failed to attach, "
544 "error %d\n", __func__
, odp
->dom_family
,
545 odp
->dom_name
, opp
->pr_protocol
, error
);
553 domain_guard_release(guard
);
558 * Internal routine, not exported.
560 * net_del_proto - remove a protosw from a domain's protosw chain.
561 * Search the protosw chain for the element with matching data.
562 * Then unlink and return.
564 * NOTE: Caller must have acquired domain_proto_mtx
567 net_del_proto(int type
, int protocol
, struct domain
*dp
)
572 * This could be called as part of initializing the domain,
573 * and thus DOM_INITIALIZED may not be set (yet).
575 domain_proto_mtx_lock_assert_held();
577 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
578 if (pp
->pr_type
== type
&& pp
->pr_protocol
== protocol
)
584 detach_proto(pp
, dp
);
585 if (pp
->pr_usrreqs
->pru_flags
& PRUF_OLD
)
586 FREE(pp
->pr_usrreqs
, M_TEMP
);
587 if (pp
->pr_flags
& PR_OLD
)
594 * Exported (private) routine, indirection of net_del_proto.
597 net_del_proto_old(int type
, int protocol
, struct domain_old
*odp
)
602 domain_guard_t guard
;
605 * This could be called as part of initializing the domain,
606 * and thus DOM_INITIALIZED may not be set (yet).
608 guard
= domain_guard_deploy();
610 /* Make sure the domain has been added via net_add_domain */
611 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
612 if (!(dp
->dom_flags
& DOM_OLD
))
614 if (dp
->dom_old
== odp
)
622 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
623 if (pp
->pr_type
== type
&& pp
->pr_protocol
== protocol
)
630 detach_proto(pp
, dp
);
631 if (pp
->pr_usrreqs
->pru_flags
& PRUF_OLD
)
632 FREE(pp
->pr_usrreqs
, M_TEMP
);
633 if (pp
->pr_flags
& PR_OLD
)
637 domain_guard_release(guard
);
642 domain_sched_timeout(void)
644 LCK_MTX_ASSERT(&domain_timeout_mtx
, LCK_MTX_ASSERT_OWNED
);
646 if (!domain_timeout_run
&& domain_draining
) {
647 domain_timeout_run
= TRUE
;
648 timeout(domain_timeout
, NULL
, hz
);
653 net_drain_domains(void)
655 lck_mtx_lock(&domain_timeout_mtx
);
656 domain_draining
= TRUE
;
657 domain_sched_timeout();
658 lck_mtx_unlock(&domain_timeout_mtx
);
662 extern struct domain inet6domain_s
;
665 extern struct domain keydomain_s
;
668 extern struct domain routedomain_s
, ndrvdomain_s
, inetdomain_s
;
669 extern struct domain systemdomain_s
, localdomain_s
;
672 extern struct domain mpdomain_s
;
673 #endif /* MULTIPATH */
676 domain_timeout(void *arg
)
681 domain_guard_t guard
;
683 lck_mtx_lock(&domain_timeout_mtx
);
684 if (domain_draining
) {
685 domain_draining
= FALSE
;
686 lck_mtx_unlock(&domain_timeout_mtx
);
688 guard
= domain_guard_deploy();
689 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
690 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
691 if (pp
->pr_drain
!= NULL
)
695 domain_guard_release(guard
);
697 lck_mtx_lock(&domain_timeout_mtx
);
700 /* re-arm the timer if there's work to do */
701 domain_timeout_run
= FALSE
;
702 domain_sched_timeout();
703 lck_mtx_unlock(&domain_timeout_mtx
);
710 domain_guard_t guard
;
712 eventhandler_lists_ctxt_init(&protoctl_evhdlr_ctxt
);
714 * allocate lock group attribute and group for domain mutexes
716 domain_proto_mtx_grp_attr
= lck_grp_attr_alloc_init();
718 domain_proto_mtx_grp
= lck_grp_alloc_init("domain",
719 domain_proto_mtx_grp_attr
);
722 * allocate the lock attribute for per domain mutexes
724 domain_proto_mtx_attr
= lck_attr_alloc_init();
726 lck_mtx_init(&domain_proto_mtx
, domain_proto_mtx_grp
,
727 domain_proto_mtx_attr
);
728 lck_mtx_init(&domain_timeout_mtx
, domain_proto_mtx_grp
,
729 domain_proto_mtx_attr
);
731 guard
= domain_guard_deploy();
733 * Add all the static domains to the domains list. route domain
734 * gets added and initialized last, since we need it to attach
735 * rt_tables[] to everything that's already there. This also
736 * means that domains added after this point won't get their
737 * dom_rtattach() called on rt_tables[].
739 attach_domain(&inetdomain_s
);
741 attach_domain(&inet6domain_s
);
744 attach_domain(&mpdomain_s
);
745 #endif /* MULTIPATH */
746 attach_domain(&systemdomain_s
);
747 attach_domain(&localdomain_s
);
749 attach_domain(&keydomain_s
);
751 attach_domain(&ndrvdomain_s
);
752 attach_domain(&routedomain_s
); /* must be last domain */
755 * Now ask them all to init (XXX including the routing domain,
758 TAILQ_FOREACH(dp
, &domains
, dom_entry
)
761 domain_guard_release(guard
);
764 static __inline__
struct domain
*
765 pffinddomain_locked(int pf
)
769 domain_proto_mtx_lock_assert_held();
771 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
772 if (dp
->dom_family
== pf
)
779 pffindtype(int family
, int type
)
781 struct protosw
*pp
= NULL
;
783 domain_guard_t guard
;
785 guard
= domain_guard_deploy();
786 if ((dp
= pffinddomain_locked(family
)) == NULL
)
789 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
790 if (pp
->pr_type
!= 0 && pp
->pr_type
== type
)
794 domain_guard_release(guard
);
799 * Internal routine, not exported.
805 domain_guard_t guard
;
807 guard
= domain_guard_deploy();
808 dp
= pffinddomain_locked(pf
);
809 domain_guard_release(guard
);
814 * Exported (private) routine, indirection of pffinddomain.
817 pffinddomain_old(int pf
)
819 struct domain_old
*odp
= NULL
;
821 domain_guard_t guard
;
823 guard
= domain_guard_deploy();
824 if ((dp
= pffinddomain_locked(pf
)) != NULL
&& (dp
->dom_flags
& DOM_OLD
))
826 domain_guard_release(guard
);
831 * Internal routine, not exported.
834 pffindproto(int family
, int protocol
, int type
)
837 domain_guard_t guard
;
839 guard
= domain_guard_deploy();
840 pp
= pffindproto_locked(family
, protocol
, type
);
841 domain_guard_release(guard
);
846 pffindproto_locked(int family
, int protocol
, int type
)
848 struct protosw
*maybe
= NULL
;
852 domain_proto_mtx_lock_assert_held();
857 dp
= pffinddomain_locked(family
);
861 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
862 if ((pp
->pr_protocol
== protocol
) && (pp
->pr_type
== type
))
865 if (type
== SOCK_RAW
&& pp
->pr_type
== SOCK_RAW
&&
866 pp
->pr_protocol
== 0 && maybe
== NULL
)
873 * Exported (private) routine, indirection of pffindproto.
876 pffindproto_old(int family
, int protocol
, int type
)
878 struct protosw_old
*opr
= NULL
;
880 domain_guard_t guard
;
882 guard
= domain_guard_deploy();
883 if ((pp
= pffindproto_locked(family
, protocol
, type
)) != NULL
&&
884 (pp
->pr_flags
& PR_OLD
))
886 domain_guard_release(guard
);
890 static struct protosw
*
891 pffindprotonotype_locked(int family
, int protocol
, int type
)
897 domain_proto_mtx_lock_assert_held();
902 dp
= pffinddomain_locked(family
);
906 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
907 if (pp
->pr_protocol
== protocol
)
914 pffindprotonotype(int family
, int protocol
)
917 domain_guard_t guard
;
922 guard
= domain_guard_deploy();
923 pp
= pffindprotonotype_locked(family
, protocol
, 0);
924 domain_guard_release(guard
);
929 pfctlinput(int cmd
, struct sockaddr
*sa
)
931 pfctlinput2(cmd
, sa
, NULL
);
935 pfctlinput2(int cmd
, struct sockaddr
*sa
, void *ctlparam
)
939 domain_guard_t guard
;
944 guard
= domain_guard_deploy();
945 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
946 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
947 if (pp
->pr_ctlinput
!= NULL
)
948 (*pp
->pr_ctlinput
)(cmd
, sa
, ctlparam
, NULL
);
951 domain_guard_release(guard
);
955 net_update_uptime_with_time(const struct timeval
*tvp
)
957 _net_uptime
= tvp
->tv_sec
;
959 * Round up the timer to the nearest integer value because otherwise
960 * we might setup networking timers that are off by almost 1 second.
962 if (tvp
->tv_usec
> 500000)
967 net_update_uptime(void)
973 net_update_uptime_with_time(&tv
);
977 * Convert our uin64_t net_uptime to a struct timeval.
980 net_uptime2timeval(struct timeval
*tv
)
986 tv
->tv_sec
= net_uptime();
990 * An alternative way to obtain the coarse-grained uptime (in seconds)
991 * for networking code which do not require high-precision timestamp,
992 * as this is significantly cheaper than microuptime().
997 if (_net_uptime
== 0)
1000 return (_net_uptime
);
1004 domain_proto_mtx_lock_assert_held(void)
1006 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1010 domain_proto_mtx_lock_assert_notheld(void)
1012 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1016 domain_guard_deploy(void)
1018 net_thread_marks_t marks
;
1020 marks
= net_thread_marks_push(NET_THREAD_HELD_DOMAIN
);
1021 if (marks
!= net_thread_marks_none
) {
1022 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1023 lck_mtx_lock(&domain_proto_mtx
);
1026 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1028 return ((domain_guard_t
)(const void*)marks
);
1032 domain_guard_release(domain_guard_t guard
)
1034 net_thread_marks_t marks
= (net_thread_marks_t
)(const void*)guard
;
1036 if (marks
!= net_thread_marks_none
) {
1037 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1038 lck_mtx_unlock(&domain_proto_mtx
);
1039 net_thread_marks_pop(marks
);
1042 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1046 domain_unguard_deploy(void)
1048 net_thread_marks_t marks
;
1050 marks
= net_thread_unmarks_push(NET_THREAD_HELD_DOMAIN
);
1051 if (marks
!= net_thread_marks_none
) {
1052 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1053 lck_mtx_unlock(&domain_proto_mtx
);
1056 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1058 return ((domain_unguard_t
)(const void*)marks
);
1062 domain_unguard_release(domain_unguard_t unguard
)
1064 net_thread_marks_t marks
= (net_thread_marks_t
)(const void*)unguard
;
1066 if (marks
!= net_thread_marks_none
) {
1067 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1068 lck_mtx_lock(&domain_proto_mtx
);
1069 net_thread_unmarks_pop(marks
);
1072 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1076 #if (DEVELOPMENT || DEBUG)
1079 sysctl_do_drain_domains SYSCTL_HANDLER_ARGS
1081 #pragma unused(arg1, arg2)
1085 error
= sysctl_handle_int(oidp
, &dummy
, 0, req
);
1086 if (error
|| req
->newptr
== USER_ADDR_NULL
)
1089 net_drain_domains();
1094 #endif /* DEVELOPMENT || DEBUG */