2 * Copyright (c) 1998-2018 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();
137 init_proto(struct protosw
*pp
, struct domain
*dp
)
139 VERIFY(pp
->pr_flags
& PR_ATTACHED
);
141 if (!(pp
->pr_flags
& PR_INITIALIZED
)) {
142 TAILQ_INIT(&pp
->pr_filter_head
);
143 if (pp
->pr_init
!= NULL
) {
146 pp
->pr_flags
|= PR_INITIALIZED
;
151 attach_proto(struct protosw
*pp
, struct domain
*dp
)
153 domain_proto_mtx_lock_assert_held();
154 VERIFY(!(pp
->pr_flags
& PR_ATTACHED
));
155 VERIFY(pp
->pr_domain
== NULL
);
156 VERIFY(pp
->pr_protosw
== NULL
);
158 TAILQ_INSERT_TAIL(&dp
->dom_protosw
, pp
, pr_entry
);
159 pp
->pr_flags
|= PR_ATTACHED
;
163 /* do some cleaning up on user request callbacks */
164 pru_sanitize(pp
->pr_usrreqs
);
168 detach_proto(struct protosw
*pp
, struct domain
*dp
)
170 domain_proto_mtx_lock_assert_held();
171 VERIFY(pp
->pr_flags
& PR_ATTACHED
);
172 VERIFY(pp
->pr_domain
== dp
);
173 VERIFY(pp
->pr_protosw
== pp
);
175 TAILQ_REMOVE(&dp
->dom_protosw
, pp
, pr_entry
);
176 pp
->pr_flags
&= ~PR_ATTACHED
;
177 pp
->pr_domain
= NULL
;
178 pp
->pr_protosw
= NULL
;
182 dom_init_old(struct domain
*dp
)
184 VERIFY(dp
->dom_flags
& DOM_OLD
);
185 VERIFY(dp
->dom_old
!= NULL
);
187 if (dp
->dom_old
->dom_init
!= NULL
) {
188 dp
->dom_old
->dom_init();
193 init_domain(struct domain
*dp
)
195 VERIFY(dp
->dom_flags
& DOM_ATTACHED
);
197 if (!(dp
->dom_flags
& DOM_INITIALIZED
)) {
198 lck_mtx_init(&dp
->dom_mtx_s
, domain_proto_mtx_grp
,
199 domain_proto_mtx_attr
);
200 dp
->dom_mtx
= &dp
->dom_mtx_s
;
201 TAILQ_INIT(&dp
->dom_protosw
);
202 if (dp
->dom_init
!= NULL
) {
205 dp
->dom_flags
|= DOM_INITIALIZED
;
208 /* Recompute for new protocol */
209 if (_max_linkhdr
< 16) { /* XXX - Sheesh; everything's ether? */
212 _max_linkhdr
= max_linkhdr
; /* round it up */
214 if (dp
->dom_protohdrlen
> _max_protohdr
) {
215 _max_protohdr
= dp
->dom_protohdrlen
;
217 _max_protohdr
= max_protohdr
; /* round it up */
219 max_hdr
= max_linkhdr
+ max_protohdr
;
220 max_datalen
= MHLEN
- max_hdr
;
224 attach_domain(struct domain
*dp
)
226 domain_proto_mtx_lock_assert_held();
227 VERIFY(!(dp
->dom_flags
& DOM_ATTACHED
));
229 TAILQ_INSERT_TAIL(&domains
, dp
, dom_entry
);
230 dp
->dom_flags
|= DOM_ATTACHED
;
234 detach_domain(struct domain
*dp
)
236 domain_proto_mtx_lock_assert_held();
237 VERIFY(dp
->dom_flags
& DOM_ATTACHED
);
239 TAILQ_REMOVE(&domains
, dp
, dom_entry
);
240 dp
->dom_flags
&= ~DOM_ATTACHED
;
242 if (dp
->dom_flags
& DOM_OLD
) {
243 struct domain_old
*odp
= dp
->dom_old
;
246 odp
->dom_next
= NULL
;
252 * Exported (private) routine, indirection of net_add_domain.
255 net_add_domain_old(struct domain_old
*odp
)
258 domain_guard_t guard
;
262 guard
= domain_guard_deploy();
263 if ((dp
= pffinddomain_locked(odp
->dom_family
)) != NULL
) {
265 * There is really nothing better than to panic here,
266 * as the caller would not have been able to handle
267 * any failures otherwise.
269 panic("%s: domain (%d,%s) already exists for %s\n", __func__
,
270 dp
->dom_family
, dp
->dom_name
, odp
->dom_name
);
274 /* Make sure nothing is currently pointing to the odp. */
275 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
276 if (dp
->dom_old
== odp
) {
277 panic("%s: domain %p (%d,%s) is already "
278 "associated with %p (%d,%s)\n", __func__
,
279 odp
, odp
->dom_family
, odp
->dom_name
, dp
,
280 dp
->dom_family
, dp
->dom_name
);
285 if (odp
->dom_protosw
!= NULL
) {
286 panic("%s: domain (%d,%s) protocols need to added "
287 "via net_add_proto\n", __func__
, odp
->dom_family
,
292 dp
= _MALLOC(sizeof(*dp
), M_TEMP
, M_WAITOK
| M_ZERO
);
295 * There is really nothing better than to panic here,
296 * as the caller would not have been able to handle
297 * any failures otherwise.
299 panic("%s: unable to allocate memory for domain family "
300 "%d (%s)\n", __func__
, odp
->dom_family
, odp
->dom_name
);
304 /* Copy everything but dom_init, dom_mtx, dom_next and dom_refs */
305 dp
->dom_family
= odp
->dom_family
;
306 dp
->dom_flags
= (odp
->dom_flags
& DOMF_USERFLAGS
) | DOM_OLD
;
307 dp
->dom_name
= odp
->dom_name
;
308 dp
->dom_init
= dom_init_old
;
309 dp
->dom_externalize
= odp
->dom_externalize
;
310 dp
->dom_dispose
= odp
->dom_dispose
;
311 dp
->dom_rtattach
= odp
->dom_rtattach
;
312 dp
->dom_rtoffset
= odp
->dom_rtoffset
;
313 dp
->dom_maxrtkey
= odp
->dom_maxrtkey
;
314 dp
->dom_protohdrlen
= odp
->dom_protohdrlen
;
320 /* Point the mutex back to the internal structure's */
321 odp
->dom_mtx
= dp
->dom_mtx
;
322 domain_guard_release(guard
);
326 * Exported (private) routine, indirection of net_del_domain.
329 net_del_domain_old(struct domain_old
*odp
)
331 struct domain
*dp1
, *dp2
;
333 domain_guard_t guard
;
337 guard
= domain_guard_deploy();
338 if (odp
->dom_refs
!= 0) {
343 TAILQ_FOREACH_SAFE(dp1
, &domains
, dom_entry
, dp2
) {
344 if (!(dp1
->dom_flags
& DOM_OLD
)) {
347 VERIFY(dp1
->dom_old
!= NULL
);
348 if (odp
== dp1
->dom_old
) {
353 struct protosw
*pp1
, *pp2
;
355 VERIFY(dp1
->dom_flags
& DOM_OLD
);
356 VERIFY(dp1
->dom_old
== odp
);
358 /* Remove all protocols attached to this domain */
359 TAILQ_FOREACH_SAFE(pp1
, &dp1
->dom_protosw
, pr_entry
, pp2
) {
360 detach_proto(pp1
, dp1
);
361 if (pp1
->pr_usrreqs
->pru_flags
& PRUF_OLD
) {
362 FREE(pp1
->pr_usrreqs
, M_TEMP
);
364 if (pp1
->pr_flags
& PR_OLD
) {
372 error
= EPFNOSUPPORT
;
375 domain_guard_release(guard
);
380 * Internal routine, not exported.
382 * net_add_proto - link a protosw into a domain's protosw chain
384 * NOTE: Caller must have acquired domain_proto_mtx
387 net_add_proto(struct protosw
*pp
, struct domain
*dp
, int doinit
)
392 * This could be called as part of initializing the domain,
393 * and thus DOM_INITIALIZED may not be set (yet).
395 domain_proto_mtx_lock_assert_held();
396 VERIFY(!(pp
->pr_flags
& PR_ATTACHED
));
398 /* pr_domain is set only after the protocol is attached */
399 if (pp
->pr_domain
!= NULL
) {
400 panic("%s: domain (%d,%s), proto %d has non-NULL pr_domain!\n",
401 __func__
, dp
->dom_family
, dp
->dom_name
, pp
->pr_protocol
);
405 if (pp
->pr_usrreqs
== NULL
) {
406 panic("%s: domain (%d,%s), proto %d has no usrreqs!\n",
407 __func__
, dp
->dom_family
, dp
->dom_name
, pp
->pr_protocol
);
411 TAILQ_FOREACH(pp1
, &dp
->dom_protosw
, pr_entry
) {
412 if (pp1
->pr_type
== pp
->pr_type
&&
413 pp1
->pr_protocol
== pp
->pr_protocol
) {
418 attach_proto(pp
, dp
);
420 net_init_proto(pp
, dp
);
427 net_init_proto(struct protosw
*pp
, struct domain
*dp
)
430 * This could be called as part of initializing the domain,
431 * and thus DOM_INITIALIZED may not be set (yet). The protocol
432 * must have been attached via net_addr_protosw() by now.
434 domain_proto_mtx_lock_assert_held();
435 VERIFY(pp
->pr_flags
& PR_ATTACHED
);
441 * Exported (private) routine, indirection of net_add_proto.
444 net_add_proto_old(struct protosw_old
*opp
, struct domain_old
*odp
)
446 struct pr_usrreqs_old
*opru
;
447 struct pr_usrreqs
*pru
= NULL
;
448 struct protosw
*pp
= NULL
, *pp1
;
451 domain_guard_t guard
;
454 * This could be called as part of initializing the domain,
455 * and thus DOM_INITIALIZED may not be set (yet).
457 guard
= domain_guard_deploy();
459 /* Make sure the domain has been added via net_add_domain */
460 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
461 if (!(dp
->dom_flags
& DOM_OLD
)) {
464 if (dp
->dom_old
== odp
) {
473 TAILQ_FOREACH(pp1
, &dp
->dom_protosw
, pr_entry
) {
474 if (pp1
->pr_type
== opp
->pr_type
&&
475 pp1
->pr_protocol
== opp
->pr_protocol
) {
481 if ((opru
= opp
->pr_usrreqs
) == NULL
) {
482 panic("%s: domain (%d,%s), proto %d has no usrreqs!\n",
483 __func__
, odp
->dom_family
, odp
->dom_name
, opp
->pr_protocol
);
487 pru
= _MALLOC(sizeof(*pru
), M_TEMP
, M_WAITOK
| M_ZERO
);
493 pru
->pru_flags
= PRUF_OLD
;
494 pru
->pru_abort
= opru
->pru_abort
;
495 pru
->pru_accept
= opru
->pru_accept
;
496 pru
->pru_attach
= opru
->pru_attach
;
497 pru
->pru_bind
= opru
->pru_bind
;
498 pru
->pru_connect
= opru
->pru_connect
;
499 pru
->pru_connect2
= opru
->pru_connect2
;
500 pru
->pru_control
= opru
->pru_control
;
501 pru
->pru_detach
= opru
->pru_detach
;
502 pru
->pru_disconnect
= opru
->pru_disconnect
;
503 pru
->pru_listen
= opru
->pru_listen
;
504 pru
->pru_peeraddr
= opru
->pru_peeraddr
;
505 pru
->pru_rcvd
= opru
->pru_rcvd
;
506 pru
->pru_rcvoob
= opru
->pru_rcvoob
;
507 pru
->pru_send
= opru
->pru_send
;
508 pru
->pru_sense
= opru
->pru_sense
;
509 pru
->pru_shutdown
= opru
->pru_shutdown
;
510 pru
->pru_sockaddr
= opru
->pru_sockaddr
;
511 pru
->pru_sosend
= opru
->pru_sosend
;
512 pru
->pru_soreceive
= opru
->pru_soreceive
;
513 pru
->pru_sopoll
= opru
->pru_sopoll
;
515 pp
= _MALLOC(sizeof(*pp
), M_TEMP
, M_WAITOK
| M_ZERO
);
522 * Protocol fast and slow timers are now deprecated.
524 if (opp
->pr_unused
!= NULL
) {
525 printf("%s: domain (%d,%s), proto %d: pr_fasttimo is "
526 "deprecated and won't be called\n", __func__
,
527 odp
->dom_family
, odp
->dom_name
, opp
->pr_protocol
);
529 if (opp
->pr_unused2
!= NULL
) {
530 printf("%s: domain (%d,%s), proto %d: pr_slowtimo is "
531 "deprecated and won't be called\n", __func__
,
532 odp
->dom_family
, odp
->dom_name
, opp
->pr_protocol
);
535 /* Copy everything but pr_init, pr_next, pr_domain, pr_protosw */
536 pp
->pr_type
= opp
->pr_type
;
537 pp
->pr_protocol
= opp
->pr_protocol
;
538 pp
->pr_flags
= (opp
->pr_flags
& PRF_USERFLAGS
) | PR_OLD
;
539 pp
->pr_input
= opp
->pr_input
;
540 pp
->pr_output
= opp
->pr_output
;
541 pp
->pr_ctlinput
= opp
->pr_ctlinput
;
542 pp
->pr_ctloutput
= opp
->pr_ctloutput
;
543 pp
->pr_usrreqs
= pru
;
544 pp
->pr_init
= pr_init_old
;
545 pp
->pr_drain
= opp
->pr_drain
;
546 pp
->pr_sysctl
= opp
->pr_sysctl
;
547 pp
->pr_lock
= opp
->pr_lock
;
548 pp
->pr_unlock
= opp
->pr_unlock
;
549 pp
->pr_getlock
= opp
->pr_getlock
;
552 /* attach as well as initialize */
553 attach_proto(pp
, dp
);
554 net_init_proto(pp
, dp
);
557 printf("%s: domain (%d,%s), proto %d: failed to attach, "
558 "error %d\n", __func__
, odp
->dom_family
,
559 odp
->dom_name
, opp
->pr_protocol
, error
);
569 domain_guard_release(guard
);
574 * Internal routine, not exported.
576 * net_del_proto - remove a protosw from a domain's protosw chain.
577 * Search the protosw chain for the element with matching data.
578 * Then unlink and return.
580 * NOTE: Caller must have acquired domain_proto_mtx
583 net_del_proto(int type
, int protocol
, struct domain
*dp
)
588 * This could be called as part of initializing the domain,
589 * and thus DOM_INITIALIZED may not be set (yet).
591 domain_proto_mtx_lock_assert_held();
593 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
594 if (pp
->pr_type
== type
&& pp
->pr_protocol
== protocol
) {
602 detach_proto(pp
, dp
);
603 if (pp
->pr_usrreqs
->pru_flags
& PRUF_OLD
) {
604 FREE(pp
->pr_usrreqs
, M_TEMP
);
606 if (pp
->pr_flags
& PR_OLD
) {
614 * Exported (private) routine, indirection of net_del_proto.
617 net_del_proto_old(int type
, int protocol
, struct domain_old
*odp
)
622 domain_guard_t guard
;
625 * This could be called as part of initializing the domain,
626 * and thus DOM_INITIALIZED may not be set (yet).
628 guard
= domain_guard_deploy();
630 /* Make sure the domain has been added via net_add_domain */
631 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
632 if (!(dp
->dom_flags
& DOM_OLD
)) {
635 if (dp
->dom_old
== odp
) {
644 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
645 if (pp
->pr_type
== type
&& pp
->pr_protocol
== protocol
) {
653 detach_proto(pp
, dp
);
654 if (pp
->pr_usrreqs
->pru_flags
& PRUF_OLD
) {
655 FREE(pp
->pr_usrreqs
, M_TEMP
);
657 if (pp
->pr_flags
& PR_OLD
) {
662 domain_guard_release(guard
);
667 domain_sched_timeout(void)
669 LCK_MTX_ASSERT(&domain_timeout_mtx
, LCK_MTX_ASSERT_OWNED
);
671 if (!domain_timeout_run
&& domain_draining
) {
672 domain_timeout_run
= TRUE
;
673 timeout(domain_timeout
, NULL
, hz
);
678 net_drain_domains(void)
680 lck_mtx_lock(&domain_timeout_mtx
);
681 domain_draining
= TRUE
;
682 domain_sched_timeout();
683 lck_mtx_unlock(&domain_timeout_mtx
);
687 extern struct domain inet6domain_s
;
690 extern struct domain keydomain_s
;
693 extern struct domain routedomain_s
, ndrvdomain_s
, inetdomain_s
;
694 extern struct domain systemdomain_s
, localdomain_s
;
697 extern struct domain mpdomain_s
;
698 #endif /* MULTIPATH */
701 domain_timeout(void *arg
)
706 domain_guard_t guard
;
708 lck_mtx_lock(&domain_timeout_mtx
);
709 if (domain_draining
) {
710 domain_draining
= FALSE
;
711 lck_mtx_unlock(&domain_timeout_mtx
);
713 guard
= domain_guard_deploy();
714 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
715 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
716 if (pp
->pr_drain
!= NULL
) {
721 domain_guard_release(guard
);
723 lck_mtx_lock(&domain_timeout_mtx
);
726 /* re-arm the timer if there's work to do */
727 domain_timeout_run
= FALSE
;
728 domain_sched_timeout();
729 lck_mtx_unlock(&domain_timeout_mtx
);
736 domain_guard_t guard
;
738 eventhandler_lists_ctxt_init(&protoctl_evhdlr_ctxt
);
740 * allocate lock group attribute and group for domain mutexes
742 domain_proto_mtx_grp_attr
= lck_grp_attr_alloc_init();
744 domain_proto_mtx_grp
= lck_grp_alloc_init("domain",
745 domain_proto_mtx_grp_attr
);
748 * allocate the lock attribute for per domain mutexes
750 domain_proto_mtx_attr
= lck_attr_alloc_init();
752 lck_mtx_init(&domain_proto_mtx
, domain_proto_mtx_grp
,
753 domain_proto_mtx_attr
);
754 lck_mtx_init(&domain_timeout_mtx
, domain_proto_mtx_grp
,
755 domain_proto_mtx_attr
);
757 guard
= domain_guard_deploy();
759 * Add all the static domains to the domains list. route domain
760 * gets added and initialized last, since we need it to attach
761 * rt_tables[] to everything that's already there. This also
762 * means that domains added after this point won't get their
763 * dom_rtattach() called on rt_tables[].
765 attach_domain(&inetdomain_s
);
767 attach_domain(&inet6domain_s
);
770 attach_domain(&mpdomain_s
);
771 #endif /* MULTIPATH */
772 attach_domain(&systemdomain_s
);
773 attach_domain(&localdomain_s
);
775 attach_domain(&keydomain_s
);
777 attach_domain(&ndrvdomain_s
);
778 attach_domain(&routedomain_s
); /* must be last domain */
781 * Now ask them all to init (XXX including the routing domain,
784 TAILQ_FOREACH(dp
, &domains
, dom_entry
)
787 domain_guard_release(guard
);
790 static __inline__
struct domain
*
791 pffinddomain_locked(int pf
)
795 domain_proto_mtx_lock_assert_held();
797 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
798 if (dp
->dom_family
== pf
) {
806 pffindtype(int family
, int type
)
808 struct protosw
*pp
= NULL
;
810 domain_guard_t guard
;
812 guard
= domain_guard_deploy();
813 if ((dp
= pffinddomain_locked(family
)) == NULL
) {
817 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
818 if (pp
->pr_type
!= 0 && pp
->pr_type
== type
) {
823 domain_guard_release(guard
);
828 * Internal routine, not exported.
834 domain_guard_t guard
;
836 guard
= domain_guard_deploy();
837 dp
= pffinddomain_locked(pf
);
838 domain_guard_release(guard
);
843 * Exported (private) routine, indirection of pffinddomain.
846 pffinddomain_old(int pf
)
848 struct domain_old
*odp
= NULL
;
850 domain_guard_t guard
;
852 guard
= domain_guard_deploy();
853 if ((dp
= pffinddomain_locked(pf
)) != NULL
&& (dp
->dom_flags
& DOM_OLD
)) {
856 domain_guard_release(guard
);
861 * Internal routine, not exported.
864 pffindproto(int family
, int protocol
, int type
)
867 domain_guard_t guard
;
869 guard
= domain_guard_deploy();
870 pp
= pffindproto_locked(family
, protocol
, type
);
871 domain_guard_release(guard
);
876 pffindproto_locked(int family
, int protocol
, int type
)
878 struct protosw
*maybe
= NULL
;
882 domain_proto_mtx_lock_assert_held();
888 dp
= pffinddomain_locked(family
);
893 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
894 if ((pp
->pr_protocol
== protocol
) && (pp
->pr_type
== type
)) {
898 if (type
== SOCK_RAW
&& pp
->pr_type
== SOCK_RAW
&&
899 pp
->pr_protocol
== 0 && maybe
== NULL
) {
907 * Exported (private) routine, indirection of pffindproto.
910 pffindproto_old(int family
, int protocol
, int type
)
912 struct protosw_old
*opr
= NULL
;
914 domain_guard_t guard
;
916 guard
= domain_guard_deploy();
917 if ((pp
= pffindproto_locked(family
, protocol
, type
)) != NULL
&&
918 (pp
->pr_flags
& PR_OLD
)) {
921 domain_guard_release(guard
);
925 static struct protosw
*
926 pffindprotonotype_locked(int family
, int protocol
, int type
)
932 domain_proto_mtx_lock_assert_held();
938 dp
= pffinddomain_locked(family
);
943 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
944 if (pp
->pr_protocol
== protocol
) {
952 pffindprotonotype(int family
, int protocol
)
955 domain_guard_t guard
;
961 guard
= domain_guard_deploy();
962 pp
= pffindprotonotype_locked(family
, protocol
, 0);
963 domain_guard_release(guard
);
968 pfctlinput(int cmd
, struct sockaddr
*sa
)
970 pfctlinput2(cmd
, sa
, NULL
);
974 pfctlinput2(int cmd
, struct sockaddr
*sa
, void *ctlparam
)
978 domain_guard_t guard
;
984 guard
= domain_guard_deploy();
985 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
986 TAILQ_FOREACH(pp
, &dp
->dom_protosw
, pr_entry
) {
987 if (pp
->pr_ctlinput
!= NULL
) {
988 (*pp
->pr_ctlinput
)(cmd
, sa
, ctlparam
, NULL
);
992 domain_guard_release(guard
);
996 net_update_uptime_with_time(const struct timeval
*tvp
)
998 _net_uptime
= tvp
->tv_sec
;
1000 * Round up the timer to the nearest integer value because otherwise
1001 * we might setup networking timers that are off by almost 1 second.
1003 if (tvp
->tv_usec
> 500000) {
1009 net_update_uptime(void)
1015 net_update_uptime_with_time(&tv
);
1019 * Convert our uin64_t net_uptime to a struct timeval.
1022 net_uptime2timeval(struct timeval
*tv
)
1029 tv
->tv_sec
= net_uptime();
1033 * An alternative way to obtain the coarse-grained uptime (in seconds)
1034 * for networking code which do not require high-precision timestamp,
1035 * as this is significantly cheaper than microuptime().
1040 if (_net_uptime
== 0) {
1041 net_update_uptime();
1048 domain_proto_mtx_lock_assert_held(void)
1050 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1054 domain_proto_mtx_lock_assert_notheld(void)
1056 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1060 domain_guard_deploy(void)
1062 net_thread_marks_t marks
;
1064 marks
= net_thread_marks_push(NET_THREAD_HELD_DOMAIN
);
1065 if (marks
!= net_thread_marks_none
) {
1066 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1067 lck_mtx_lock(&domain_proto_mtx
);
1069 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1072 return (domain_guard_t
)(const void*)marks
;
1076 domain_guard_release(domain_guard_t guard
)
1078 net_thread_marks_t marks
= (net_thread_marks_t
)(const void*)guard
;
1080 if (marks
!= net_thread_marks_none
) {
1081 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1082 lck_mtx_unlock(&domain_proto_mtx
);
1083 net_thread_marks_pop(marks
);
1085 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1090 domain_unguard_deploy(void)
1092 net_thread_marks_t marks
;
1094 marks
= net_thread_unmarks_push(NET_THREAD_HELD_DOMAIN
);
1095 if (marks
!= net_thread_marks_none
) {
1096 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1097 lck_mtx_unlock(&domain_proto_mtx
);
1099 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1102 return (domain_unguard_t
)(const void*)marks
;
1106 domain_unguard_release(domain_unguard_t unguard
)
1108 net_thread_marks_t marks
= (net_thread_marks_t
)(const void*)unguard
;
1110 if (marks
!= net_thread_marks_none
) {
1111 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1112 lck_mtx_lock(&domain_proto_mtx
);
1113 net_thread_unmarks_pop(marks
);
1115 LCK_MTX_ASSERT(&domain_proto_mtx
, LCK_MTX_ASSERT_OWNED
);
1120 #if (DEVELOPMENT || DEBUG)
1123 sysctl_do_drain_domains SYSCTL_HANDLER_ARGS
1125 #pragma unused(arg1, arg2)
1129 error
= sysctl_handle_int(oidp
, &dummy
, 0, req
);
1130 if (error
|| req
->newptr
== USER_ADDR_NULL
) {
1134 net_drain_domains();
1139 #endif /* DEVELOPMENT || DEBUG */