1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions * Copyright (c) 2000-2021 Apple Computer, Inc. All rights reserved.
3*043036a2SApple OSS Distributions *
4*043036a2SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions *
6*043036a2SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions *
15*043036a2SApple OSS Distributions * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions *
18*043036a2SApple OSS Distributions * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions * limitations under the License.
25*043036a2SApple OSS Distributions *
26*043036a2SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions */
28*043036a2SApple OSS Distributions
29*043036a2SApple OSS Distributions #include <kern/kalloc.h>
30*043036a2SApple OSS Distributions #include <libkern/OSAtomic.h>
31*043036a2SApple OSS Distributions #include <sys/errno.h>
32*043036a2SApple OSS Distributions #include <sys/sysctl.h>
33*043036a2SApple OSS Distributions #include <net/init.h>
34*043036a2SApple OSS Distributions #include <libkern/libkern.h>
35*043036a2SApple OSS Distributions #include <os/atomic_private.h>
36*043036a2SApple OSS Distributions #include <pexpert/pexpert.h>
37*043036a2SApple OSS Distributions #include <string.h>
38*043036a2SApple OSS Distributions
39*043036a2SApple OSS Distributions #if (DEBUG | DEVELOPMENT)
40*043036a2SApple OSS Distributions SYSCTL_DECL(_net_diagnose);
41*043036a2SApple OSS Distributions SYSCTL_NODE(_net, OID_AUTO, diagnose, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "");
42*043036a2SApple OSS Distributions
43*043036a2SApple OSS Distributions static int net_diagnose_on = 0;
44*043036a2SApple OSS Distributions SYSCTL_INT(_net_diagnose, OID_AUTO, on,
45*043036a2SApple OSS Distributions CTLFLAG_RW | CTLFLAG_LOCKED, &net_diagnose_on, 0, "");
46*043036a2SApple OSS Distributions #endif /* (DEBUG | DEVELOPMENT) */
47*043036a2SApple OSS Distributions
48*043036a2SApple OSS Distributions struct init_list_entry {
49*043036a2SApple OSS Distributions struct init_list_entry *next;
50*043036a2SApple OSS Distributions net_init_func_ptr func;
51*043036a2SApple OSS Distributions };
52*043036a2SApple OSS Distributions
53*043036a2SApple OSS Distributions static struct init_list_entry *LIST_RAN = __unsafe_forge_single(struct init_list_entry *, ~(uintptr_t)0);
54*043036a2SApple OSS Distributions static struct init_list_entry *list_head = NULL;
55*043036a2SApple OSS Distributions
56*043036a2SApple OSS Distributions errno_t
net_init_add(net_init_func_ptr init_func)57*043036a2SApple OSS Distributions net_init_add(net_init_func_ptr init_func)
58*043036a2SApple OSS Distributions {
59*043036a2SApple OSS Distributions struct init_list_entry *entry;
60*043036a2SApple OSS Distributions
61*043036a2SApple OSS Distributions if (init_func == 0) {
62*043036a2SApple OSS Distributions return EINVAL;
63*043036a2SApple OSS Distributions }
64*043036a2SApple OSS Distributions
65*043036a2SApple OSS Distributions /* Check if we've already started */
66*043036a2SApple OSS Distributions if (list_head == LIST_RAN) {
67*043036a2SApple OSS Distributions return EALREADY;
68*043036a2SApple OSS Distributions }
69*043036a2SApple OSS Distributions
70*043036a2SApple OSS Distributions entry = kalloc_type(struct init_list_entry,
71*043036a2SApple OSS Distributions Z_WAITOK | Z_ZERO | Z_NOFAIL);
72*043036a2SApple OSS Distributions
73*043036a2SApple OSS Distributions entry->func = init_func;
74*043036a2SApple OSS Distributions
75*043036a2SApple OSS Distributions do {
76*043036a2SApple OSS Distributions entry->next = list_head;
77*043036a2SApple OSS Distributions
78*043036a2SApple OSS Distributions if (entry->next == LIST_RAN) {
79*043036a2SApple OSS Distributions /* List already ran, cleanup and call the function */
80*043036a2SApple OSS Distributions kfree_type(struct init_list_entry, entry);
81*043036a2SApple OSS Distributions return EALREADY;
82*043036a2SApple OSS Distributions }
83*043036a2SApple OSS Distributions } while (!os_atomic_cmpxchg(&list_head, entry->next, entry, acq_rel));
84*043036a2SApple OSS Distributions
85*043036a2SApple OSS Distributions return 0;
86*043036a2SApple OSS Distributions }
87*043036a2SApple OSS Distributions
88*043036a2SApple OSS Distributions __private_extern__ void
net_init_run(void)89*043036a2SApple OSS Distributions net_init_run(void)
90*043036a2SApple OSS Distributions {
91*043036a2SApple OSS Distributions struct init_list_entry *__single backward_head = NULL;
92*043036a2SApple OSS Distributions struct init_list_entry *__single forward_head = NULL;
93*043036a2SApple OSS Distributions struct init_list_entry *__single current = NULL;
94*043036a2SApple OSS Distributions
95*043036a2SApple OSS Distributions /*
96*043036a2SApple OSS Distributions * Grab the list, replacing the head with 0xffffffff to indicate
97*043036a2SApple OSS Distributions * that we've already run.
98*043036a2SApple OSS Distributions */
99*043036a2SApple OSS Distributions do {
100*043036a2SApple OSS Distributions backward_head = list_head;
101*043036a2SApple OSS Distributions } while (!os_atomic_cmpxchg(&list_head, backward_head, LIST_RAN, acq_rel));
102*043036a2SApple OSS Distributions
103*043036a2SApple OSS Distributions /* Reverse the order of the list */
104*043036a2SApple OSS Distributions while (backward_head != 0) {
105*043036a2SApple OSS Distributions current = backward_head;
106*043036a2SApple OSS Distributions backward_head = current->next;
107*043036a2SApple OSS Distributions current->next = forward_head;
108*043036a2SApple OSS Distributions forward_head = current;
109*043036a2SApple OSS Distributions }
110*043036a2SApple OSS Distributions
111*043036a2SApple OSS Distributions /* Call each function pointer registered */
112*043036a2SApple OSS Distributions while (forward_head != 0) {
113*043036a2SApple OSS Distributions current = forward_head;
114*043036a2SApple OSS Distributions forward_head = current->next;
115*043036a2SApple OSS Distributions current->func();
116*043036a2SApple OSS Distributions kfree_type(struct init_list_entry, current);
117*043036a2SApple OSS Distributions }
118*043036a2SApple OSS Distributions
119*043036a2SApple OSS Distributions #if (DEBUG || DEVELOPMENT)
120*043036a2SApple OSS Distributions (void) PE_parse_boot_argn("net_diagnose_on", &net_diagnose_on, sizeof(net_diagnose_on));
121*043036a2SApple OSS Distributions #endif /* (DEBUG || DEVELOPMENT) */
122*043036a2SApple OSS Distributions }
123