1 /*
2 * Copyright (c) 2016-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 <skywalk/os_skywalk_private.h>
30 #include <skywalk/nexus/flowswitch/nx_flowswitch.h>
31 #include <skywalk/nexus/flowswitch/fsw_var.h>
32
33 /* automatically register a netagent at constructor time */
34 static int fsw_netagent = 1;
35
36 #if (DEVELOPMENT || DEBUG)
37 SYSCTL_INT(_kern_skywalk_flowswitch, OID_AUTO, netagent,
38 CTLFLAG_RW | CTLFLAG_LOCKED, &fsw_netagent, 0, "");
39 #endif /* !DEVELOPMENT && !DEBUG */
40
41 static packet_svc_class_t
fsw_netagent_tc2sc(uint32_t tc,uint32_t * scp)42 fsw_netagent_tc2sc(uint32_t tc, uint32_t *scp)
43 {
44 uint32_t sc;
45 int ret = 0;
46
47 switch (tc) {
48 case SO_TC_BK_SYS:
49 sc = PKT_SC_BK_SYS;
50 break;
51 case SO_TC_BK:
52 sc = PKT_SC_BK;
53 break;
54 case SO_TC_BE:
55 sc = PKT_SC_BE;
56 break;
57 case SO_TC_RD:
58 sc = PKT_SC_RD;
59 break;
60 case SO_TC_OAM:
61 sc = PKT_SC_OAM;
62 break;
63 case SO_TC_AV:
64 sc = PKT_SC_AV;
65 break;
66 case SO_TC_RV:
67 sc = PKT_SC_RV;
68 break;
69 case SO_TC_VI:
70 sc = PKT_SC_VI;
71 break;
72 case SO_TC_NETSVC_SIG:
73 sc = PKT_SC_SIG;
74 break;
75 case SO_TC_VO:
76 sc = PKT_SC_VO;
77 break;
78 case SO_TC_CTL:
79 sc = PKT_SC_CTL;
80 break;
81 default:
82 sc = PKT_SC_BE;
83 ret = -1;
84 break;
85 }
86
87 *scp = sc;
88 return ret;
89 }
90
91 static int
fsw_netagent_flow_add(struct nx_flowswitch * fsw,uuid_t flow_uuid,pid_t pid,void * context,struct necp_client_nexus_parameters * cparams,void ** results,size_t * results_length)92 fsw_netagent_flow_add(struct nx_flowswitch *fsw, uuid_t flow_uuid, pid_t pid,
93 void *context, struct necp_client_nexus_parameters *cparams,
94 void **results, size_t *results_length)
95 {
96 struct nx_flow_req req;
97 struct flow_owner *fo = NULL;
98 size_t assign_message_length = 0;
99 void *assign_message = NULL;
100 struct necp_client_endpoint local_endpoint;
101 struct necp_client_endpoint remote_endpoint;
102 int error;
103
104 ASSERT(cparams != NULL);
105 ASSERT(results != NULL && *results == NULL);
106 ASSERT(results_length != NULL && *results_length == 0);
107
108 bzero(&req, sizeof(req));
109 req.nfr_nx_port = NEXUS_PORT_ANY;
110 req.nfr_flowadv_idx = FLOWADV_IDX_NONE;
111 bcopy((void *)&cparams->local_addr, (void *)&req.nfr_saddr,
112 sizeof(cparams->local_addr));
113 bcopy((void *)&cparams->remote_addr, (void *)&req.nfr_daddr,
114 sizeof(cparams->remote_addr));
115 req.nfr_flags = (NXFLOWREQF_TRACK | NXFLOWREQF_FLOWADV);
116 req.nfr_ip_protocol = cparams->ip_protocol;
117 req.nfr_transport_protocol = cparams->transport_protocol;
118 uuid_copy(req.nfr_flow_uuid, flow_uuid);
119 uuid_copy(req.nfr_euuid, cparams->euuid);
120 req.nfr_epid = cparams->epid;
121 req.nfr_policy_id = (uint32_t)cparams->policy_id;
122
123 if (fsw_netagent_tc2sc(cparams->traffic_class,
124 &req.nfr_svc_class) != 0) {
125 error = EINVAL;
126 goto done;
127 }
128
129 if (cparams->allow_qos_marking) {
130 req.nfr_flags |= NXFLOWREQF_QOS_MARKING;
131 }
132 if (cparams->override_address_selection) {
133 req.nfr_flags |= NXFLOWREQF_OVERRIDE_ADDRESS_SELECTION;
134 }
135 if (cparams->use_stable_address) {
136 req.nfr_flags |= NXFLOWREQF_USE_STABLE_ADDRESS;
137 }
138 if (cparams->no_wake_from_sleep) {
139 req.nfr_flags |= NXFLOWREQF_NOWAKEFROMSLEEP;
140 }
141 if (cparams->reuse_port) {
142 req.nfr_flags |= NXFLOWREQF_REUSEPORT;
143 }
144
145 req.nfr_context = context;
146 req.nfr_pid = pid;
147 req.nfr_port_reservation = cparams->port_reservation;
148
149 if (cparams->is_demuxable_parent) {
150 req.nfr_flags |= NXFLOWREQF_PARENT;
151 } else {
152 uuid_copy(req.nfr_parent_flow_uuid, cparams->parent_flow_uuid);
153 if (cparams->demux_pattern_count > 0) {
154 if (cparams->demux_pattern_count > MAX_FLOW_DEMUX_PATTERN) {
155 error = EINVAL;
156 goto done;
157 }
158
159 _CASSERT(sizeof(struct necp_demux_pattern) == sizeof(struct flow_demux_pattern));
160 for (int i = 0; i < cparams->demux_pattern_count; i++) {
161 memcpy(&req.nfr_flow_demux_patterns[i], &cparams->demux_patterns[i],
162 sizeof(struct flow_demux_pattern));
163 }
164 req.nfr_flow_demux_count = cparams->demux_pattern_count;
165 }
166 }
167
168 ASSERT(req.nfr_flags & NXFLOWREQF_FLOWADV);
169 fo = fsw_flow_add(fsw, &req, &error);
170 if (fo == NULL) {
171 ASSERT(error != 0);
172 ASSERT(req.nfr_flow_stats == NULL);
173 goto done;
174 }
175
176 ASSERT(error == 0);
177 ASSERT(req.nfr_nx_port != NEXUS_PORT_ANY);
178 ASSERT(!uuid_is_null(fo->fo_key));
179 ASSERT(req.nfr_flowadv_idx != FLOWADV_IDX_NONE);
180 ASSERT(req.nfr_flow_stats != NULL);
181 ASSERT(flow_stats_refcnt(req.nfr_flow_stats) >= 1);
182
183 bzero(&local_endpoint, sizeof(local_endpoint));
184 bzero(&remote_endpoint, sizeof(remote_endpoint));
185
186 bcopy((void *)&req.nfr_saddr, (void *)&local_endpoint.u.sin6,
187 sizeof(req.nfr_saddr));
188 bcopy((void *)&req.nfr_daddr, (void *)&remote_endpoint.u.sin6,
189 sizeof(req.nfr_daddr));
190
191 assign_message =
192 necp_create_nexus_assign_message(fsw->fsw_nx->nx_uuid,
193 req.nfr_nx_port, fo->fo_key, sizeof(fo->fo_key),
194 &local_endpoint, &remote_endpoint, NULL, req.nfr_flowadv_idx,
195 req.nfr_flow_stats, &assign_message_length);
196
197 if (assign_message != NULL) {
198 req.nfr_flow_stats = NULL;
199 ASSERT(error == 0);
200 *results = assign_message;
201 *results_length = assign_message_length;
202 } else {
203 error = ENOMEM;
204 }
205
206 done:
207 #if SK_LOG
208 if (__improbable((sk_verbose & SK_VERB_FLOW) != 0)) {
209 uuid_string_t uuidstr;
210 SK_DF(SK_VERB_FLOW, "pid %d connect flow_uuid %s nx_port %d "
211 "(err %d)", pid, sk_uuid_unparse(flow_uuid, uuidstr),
212 (int)req.nfr_nx_port, error);
213 }
214 #endif /* SK_LOG */
215 if (error != 0) {
216 ASSERT(*results == NULL);
217 ASSERT(*results_length == 0);
218 if (assign_message != NULL) {
219 kfree_data(assign_message, assign_message_length);
220 assign_message = NULL;
221 }
222 if (fo != NULL) {
223 req.nfr_pid = pid;
224 fsw_flow_del(fsw, &req, TRUE, NULL);
225 }
226 if (req.nfr_flow_stats != NULL) {
227 flow_stats_release(req.nfr_flow_stats);
228 req.nfr_flow_stats = NULL;
229 }
230 }
231
232 return error;
233 }
234
235 static int
fsw_netagent_flow_del(struct nx_flowswitch * fsw,uuid_t flow_uuid,pid_t pid,bool nolinger,void * context,void * params)236 fsw_netagent_flow_del(struct nx_flowswitch *fsw, uuid_t flow_uuid, pid_t pid,
237 bool nolinger, void *context, void *params)
238 {
239 struct nx_flow_req req;
240 int error;
241
242 bzero(&req, sizeof(req));
243 uuid_copy(req.nfr_flow_uuid, flow_uuid);
244 req.nfr_proc = NULL;
245 req.nfr_pid = pid;
246 req.nfr_context = context;
247
248 error = fsw_flow_del(fsw, &req, nolinger, params);
249
250 #if SK_LOG
251 if (__improbable((sk_verbose & SK_VERB_FLOW) != 0)) {
252 uuid_string_t uuidstr;
253 SK_DF(SK_VERB_FLOW, "pid %d flow_uuid %s (err %d)",
254 pid, sk_uuid_unparse(flow_uuid, uuidstr), error);
255 }
256 #endif /* SK_LOG */
257
258 return error;
259 }
260
261 static int
fsw_netagent_event(u_int8_t event,uuid_t flow_uuid,pid_t pid,void * context,void * ctx,struct necp_client_agent_parameters * cparams,void ** results,size_t * results_length)262 fsw_netagent_event(u_int8_t event, uuid_t flow_uuid, pid_t pid, void *context,
263 void *ctx, struct necp_client_agent_parameters *cparams, void **results,
264 size_t *results_length)
265 {
266 struct nx_flowswitch *fsw;
267 int error = 0;
268
269 ASSERT(!uuid_is_null(flow_uuid));
270
271 fsw = (struct nx_flowswitch *)ctx;
272 ASSERT(fsw != NULL);
273
274 switch (event) {
275 case NETAGENT_EVENT_NEXUS_FLOW_INSERT:
276 /* these are required for this event */
277 ASSERT(cparams != NULL);
278 ASSERT(results != NULL);
279 ASSERT(results_length != NULL);
280 *results = NULL;
281 *results_length = 0;
282 error = fsw_netagent_flow_add(fsw, flow_uuid, pid, context,
283 &cparams->u.nexus_request, results, results_length);
284 break;
285
286 case NETAGENT_EVENT_NEXUS_FLOW_REMOVE:
287 case NETAGENT_EVENT_NEXUS_FLOW_ABORT:
288 /*
289 * A flow can be removed gracefully (FLOW_REMOVE) by
290 * the client, in which case we don't need to have it
291 * linger around to hold the namespace. If the process
292 * crashed or if it's suspended, then we treat that
293 * as the abort case (FLOW_ABORT) where we'll hold on
294 * to the namespace if needed.
295 */
296 error = fsw_netagent_flow_del(fsw, flow_uuid, pid,
297 (event == NETAGENT_EVENT_NEXUS_FLOW_REMOVE), context,
298 cparams);
299 break;
300
301 default:
302 /* events not handled */
303 return 0;
304 }
305
306 return error;
307 }
308
309 int
fsw_netagent_register(struct nx_flowswitch * fsw,struct ifnet * ifp)310 fsw_netagent_register(struct nx_flowswitch *fsw, struct ifnet *ifp)
311 {
312 struct netagent_nexus_agent agent;
313 int error = 0;
314
315 _CASSERT(FLOWADV_IDX_NONE == UINT32_MAX);
316 _CASSERT(NECP_FLOWADV_IDX_INVALID == FLOWADV_IDX_NONE);
317
318 if (!fsw_netagent) {
319 return 0;
320 }
321
322 fsw->fsw_agent_session = netagent_create(fsw_netagent_event, fsw);
323 if (fsw->fsw_agent_session == NULL) {
324 return ENOMEM;
325 }
326
327 bzero(&agent, sizeof(agent));
328 uuid_generate_random(agent.agent.netagent_uuid);
329 uuid_copy(fsw->fsw_agent_uuid, agent.agent.netagent_uuid);
330 (void) snprintf(agent.agent.netagent_domain,
331 sizeof(agent.agent.netagent_domain), "%s", "Skywalk");
332 (void) snprintf(agent.agent.netagent_type,
333 sizeof(agent.agent.netagent_type), "%s", "FlowSwitch");
334 (void) snprintf(agent.agent.netagent_desc,
335 sizeof(agent.agent.netagent_desc), "%s", "Userspace Networking");
336 agent.agent.netagent_flags = NETAGENT_FLAG_ACTIVE;
337 if (ifnet_needs_fsw_transport_netagent(ifp)) {
338 agent.agent.netagent_flags |= (NETAGENT_FLAG_NEXUS_PROVIDER |
339 NETAGENT_FLAG_NEXUS_LISTENER);
340 }
341 if (ifnet_needs_fsw_ip_netagent(ifp)) {
342 ASSERT((sk_features & SK_FEATURE_PROTONS) != 0);
343 agent.agent.netagent_flags |= (NETAGENT_FLAG_CUSTOM_IP_NEXUS |
344 NETAGENT_FLAG_NEXUS_LISTENER);
345 }
346 agent.agent.netagent_data_size = sizeof(struct netagent_nexus);
347 agent.nexus_data.frame_type = NETAGENT_NEXUS_FRAME_TYPE_INTERNET;
348 agent.nexus_data.endpoint_assignment_type =
349 NETAGENT_NEXUS_ENDPOINT_TYPE_ADDRESS;
350 agent.nexus_data.endpoint_request_types[0] =
351 NETAGENT_NEXUS_ENDPOINT_TYPE_ADDRESS;
352 agent.nexus_data.endpoint_resolution_type_pairs[0] =
353 NETAGENT_NEXUS_ENDPOINT_TYPE_HOST;
354 agent.nexus_data.endpoint_resolution_type_pairs[1] =
355 NETAGENT_NEXUS_ENDPOINT_TYPE_ADDRESS;
356 agent.nexus_data.endpoint_resolution_type_pairs[2] =
357 NETAGENT_NEXUS_ENDPOINT_TYPE_BONJOUR;
358 agent.nexus_data.endpoint_resolution_type_pairs[3] =
359 NETAGENT_NEXUS_ENDPOINT_TYPE_HOST;
360 agent.nexus_data.endpoint_resolution_type_pairs[4] =
361 NETAGENT_NEXUS_ENDPOINT_TYPE_SRV;
362 agent.nexus_data.endpoint_resolution_type_pairs[5] =
363 NETAGENT_NEXUS_ENDPOINT_TYPE_HOST;
364 agent.nexus_data.nexus_max_buf_size =
365 fsw->fsw_nx->nx_prov->nxprov_params->nxp_buf_size;
366 agent.nexus_data.nexus_flags |=
367 (NETAGENT_NEXUS_FLAG_SUPPORTS_USER_PACKET_POOL |
368 NETAGENT_NEXUS_FLAG_ASSERT_UNSUPPORTED);
369
370 error = netagent_register(fsw->fsw_agent_session, &agent.agent);
371 if (error != 0) {
372 goto fail_netagent_register;
373 }
374
375 if (ifp->if_bridge != NULL) {
376 /* see rdar://107076453 */
377 SK_ERR("%s is bridged, not adding netagent",
378 if_name(ifp));
379 goto done;
380 }
381 error = if_add_netagent(ifp, fsw->fsw_agent_uuid);
382 if (error != 0) {
383 goto fail_netagent_add;
384 }
385 fsw->fsw_state_flags |= FSW_STATEF_NETAGENT_ADDED;
386 if (if_is_fsw_netagent_enabled()) {
387 fsw->fsw_state_flags |= FSW_STATEF_NETAGENT_ENABLED;
388 }
389 done:
390 return 0;
391
392 fail_netagent_add:
393 netagent_unregister(fsw->fsw_agent_session);
394
395 fail_netagent_register:
396 netagent_destroy(fsw->fsw_agent_session);
397 fsw->fsw_agent_session = NULL;
398 uuid_clear(fsw->fsw_agent_uuid);
399
400 return error;
401 }
402
403 void
fsw_netagent_unregister(struct nx_flowswitch * fsw,struct ifnet * ifp)404 fsw_netagent_unregister(struct nx_flowswitch *fsw, struct ifnet *ifp)
405 {
406 if (!uuid_is_null(fsw->fsw_agent_uuid)) {
407 if_delete_netagent(ifp, fsw->fsw_agent_uuid);
408 }
409
410 if (fsw->fsw_agent_session != NULL) {
411 netagent_destroy(fsw->fsw_agent_session);
412 fsw->fsw_agent_session = NULL;
413 uuid_clear(fsw->fsw_agent_uuid);
414 }
415 }
416