xref: /xnu-11215.41.3/bsd/skywalk/lib/net_filter_event.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 /*
2  * Copyright (c) 2021-2023 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 <stdbool.h>
30 
31 #include <sys/systm.h>
32 #include <sys/sysctl.h>
33 #include <sys/sbuf.h>
34 #include <sys/types.h>
35 #include <sys/mcache.h>
36 #include <sys/malloc.h>
37 
38 #include <os/log.h>
39 
40 #include <net/nwk_wq.h>
41 #include <skywalk/lib/net_filter_event.h>
42 
43 static uint32_t net_filter_event_state;
44 static bool net_filter_event_initialized;
45 static struct eventhandler_lists_ctxt net_filter_evhdlr_ctxt;
46 
47 EVENTHANDLER_DECLARE(net_filter_event, net_filter_event_callback_t);
48 
49 static struct sbuf *
net_filter_event_description(uint32_t state)50 net_filter_event_description(uint32_t state)
51 {
52 	struct sbuf *sbuf;
53 
54 	sbuf = sbuf_new(NULL, NULL, 128, SBUF_AUTOEXTEND);
55 	if (state & NET_FILTER_EVENT_PF) {
56 		sbuf_cat(sbuf, "pf ");
57 	}
58 	if (state & NET_FILTER_EVENT_SOCKET) {
59 		sbuf_cat(sbuf, "socket ");
60 	}
61 	if (state & NET_FILTER_EVENT_INTERFACE) {
62 		sbuf_cat(sbuf, "interface ");
63 	}
64 	if (state & NET_FILTER_EVENT_IP) {
65 		sbuf_cat(sbuf, "ip ");
66 	}
67 	if (state & NET_FILTER_EVENT_ALF) {
68 		sbuf_cat(sbuf, "application-firewall ");
69 	}
70 	if (state & NET_FILTER_EVENT_PARENTAL_CONTROLS) {
71 		sbuf_cat(sbuf, "parental-controls ");
72 	}
73 	sbuf_trim(sbuf);
74 	sbuf_finish(sbuf);
75 
76 	return sbuf;
77 }
78 
79 static void
net_filter_event_callback(struct eventhandler_entry_arg arg0 __unused,enum net_filter_event_subsystems state)80 net_filter_event_callback(struct eventhandler_entry_arg arg0 __unused,
81     enum net_filter_event_subsystems state)
82 {
83 	struct sbuf *sbuf = net_filter_event_description(state);
84 
85 	os_log(OS_LOG_DEFAULT, "net_filter_event: new state (0x%x) %s",
86 	    state, sbuf_data(sbuf));
87 	evhlog(debug, "%s: eventhandler saw event type=net_filter_event_state event_code=%s",
88 	    __func__, sbuf_data(sbuf));
89 	sbuf_delete(sbuf);
90 }
91 
92 static void
net_filter_event_init(void)93 net_filter_event_init(void)
94 {
95 	if (net_filter_event_initialized) {
96 		return;
97 	}
98 	net_filter_event_initialized = true;
99 	eventhandler_lists_ctxt_init(&net_filter_evhdlr_ctxt);
100 	net_filter_event_register(net_filter_event_callback);
101 }
102 
103 static void
net_filter_event_enqueue_callback(struct nwk_wq_entry * nwk_kwqe)104 net_filter_event_enqueue_callback(struct nwk_wq_entry *nwk_kwqe)
105 {
106 	EVENTHANDLER_INVOKE(&net_filter_evhdlr_ctxt, net_filter_event,
107 	    net_filter_event_state);
108 	kfree_type(struct nwk_wq_entry, nwk_kwqe);
109 }
110 
111 static void
net_filter_event_enqueue(void)112 net_filter_event_enqueue(void)
113 {
114 	struct nwk_wq_entry *__single nwk_wqe;
115 
116 	struct sbuf *sbuf = net_filter_event_description(net_filter_event_state);
117 	evhlog(debug, "%s: eventhandler enqueuing event of type=net_filter_event_state event_code=%s",
118 	    __func__, sbuf_data(sbuf));
119 	sbuf_delete(sbuf);
120 
121 	nwk_wqe = kalloc_type(struct nwk_wq_entry, Z_WAITOK | Z_ZERO | Z_NOFAIL);
122 	nwk_wqe->func = net_filter_event_enqueue_callback;
123 	nwk_wq_enqueue(nwk_wqe);
124 }
125 
126 void
net_filter_event_mark(enum net_filter_event_subsystems subsystem,bool compatible)127 net_filter_event_mark(enum net_filter_event_subsystems subsystem, bool compatible)
128 {
129 	uint32_t old_state = net_filter_event_state;
130 
131 	net_filter_event_init();
132 	if (!compatible) {
133 		os_atomic_or(&net_filter_event_state, subsystem, relaxed);
134 	} else {
135 		os_atomic_andnot(&net_filter_event_state, subsystem, relaxed);
136 	}
137 	if (old_state != net_filter_event_state) {
138 		net_filter_event_enqueue();
139 	}
140 }
141 
142 enum net_filter_event_subsystems
net_filter_event_get_state(void)143 net_filter_event_get_state(void)
144 {
145 	return net_filter_event_state;
146 }
147 
148 void
net_filter_event_register(net_filter_event_callback_t callback)149 net_filter_event_register(net_filter_event_callback_t callback)
150 {
151 	net_filter_event_init();
152 	(void)EVENTHANDLER_REGISTER(&net_filter_evhdlr_ctxt,
153 	    net_filter_event, callback,
154 	    eventhandler_entry_dummy_arg,
155 	    EVENTHANDLER_PRI_ANY);
156 }
157 
158 static int
net_filter_event_sysctl(struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)159 net_filter_event_sysctl(struct sysctl_oid *oidp, void *arg1, int arg2,
160     struct sysctl_req *req)
161 {
162 #pragma unused(oidp, arg1, arg2)
163 	struct sbuf *sbuf = net_filter_event_description(net_filter_event_state);
164 
165 	int error = sysctl_io_string(req, sbuf_data(sbuf), 0, 0, NULL);
166 	sbuf_delete(sbuf);
167 
168 	return error;
169 }
170 
171 SYSCTL_PROC(_net, OID_AUTO, filter_state,
172     CTLTYPE_STRING | CTLFLAG_LOCKED, NULL, 0,
173     net_filter_event_sysctl, "A", "State of the network filters");
174