1 /*
2 * Copyright (c) 2015-2022 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 <string.h>
30
31 #include <kern/thread_call.h>
32 #include <kern/zalloc.h>
33
34 #include <net/if.h>
35 #include <net/if_var.h>
36 #include <net/net_api_stats.h>
37 #include <net/necp.h>
38 #include <net/network_agent.h>
39 #include <net/ntstat.h>
40
41 #include <netinet/in_pcb.h>
42 #include <netinet/in_var.h>
43 #include <netinet/ip.h>
44 #include <netinet/ip6.h>
45 #include <netinet/mp_pcb.h>
46 #include <netinet/tcp_cc.h>
47 #include <netinet/tcp_fsm.h>
48 #include <netinet/tcp_cache.h>
49 #include <netinet6/in6_var.h>
50
51 #include <sys/domain.h>
52 #include <sys/file_internal.h>
53 #include <sys/kauth.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/poll.h>
57 #include <sys/priv.h>
58 #include <sys/protosw.h>
59 #include <sys/queue.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/sysproto.h>
63 #include <sys/systm.h>
64 #include <sys/types.h>
65 #include <sys/codesign.h>
66 #include <libkern/section_keywords.h>
67
68 #include <os/refcnt.h>
69
70 #if SKYWALK
71 #include <skywalk/os_skywalk_private.h>
72 #include <skywalk/nexus/flowswitch/flow/flow_var.h>
73 #include <skywalk/nexus/netif/nx_netif.h>
74 #endif /* SKYWALK */
75
76 #if CONFIG_MACF
77 #include <security/mac_framework.h>
78 #endif
79
80 /*
81 * NECP Client Architecture
82 * ------------------------------------------------
83 * See <net/necp.c> for a discussion on NECP database architecture.
84 *
85 * Each client of NECP provides a set of parameters for a connection or network state
86 * evaluation, on which NECP policy evaluation is run. This produces a policy result
87 * which can be accessed by the originating process, along with events for when policies
88 * results have changed.
89 *
90 * ------------------------------------------------
91 * NECP Client FD
92 * ------------------------------------------------
93 * A process opens an NECP file descriptor using necp_open(). This is a very simple
94 * file descriptor, upon which the process may do the following operations:
95 * - necp_client_action(...), to add/remove/query clients
96 * - kqueue, to watch for readable events
97 * - close(), to close the client session and release all clients
98 *
99 * Client objects are allocated structures that hang off of the file descriptor. Each
100 * client contains:
101 * - Client ID, a UUID that references the client across the system
102 * - Parameters, a buffer of TLVs that describe the client's connection parameters,
103 * such as the remote and local endpoints, interface requirements, etc.
104 * - Result, a buffer of TLVs containing the current policy evaluation for the client.
105 * This result will be updated whenever a network change occurs that impacts the
106 * policy result for that client.
107 *
108 * +--------------+
109 * | NECP fd |
110 * +--------------+
111 * ||
112 * ==================================
113 * || || ||
114 * +--------------+ +--------------+ +--------------+
115 * | Client ID | | Client ID | | Client ID |
116 * | ---- | | ---- | | ---- |
117 * | Parameters | | Parameters | | Parameters |
118 * | ---- | | ---- | | ---- |
119 * | Result | | Result | | Result |
120 * +--------------+ +--------------+ +--------------+
121 *
122 * ------------------------------------------------
123 * Client Actions
124 * ------------------------------------------------
125 * - Add. Input parameters as a buffer of TLVs, and output a client ID. Allocates a
126 * new client structure on the file descriptor.
127 * - Remove. Input a client ID. Removes a client structure from the file descriptor.
128 * - Copy Parameters. Input a client ID, and output parameter TLVs.
129 * - Copy Result. Input a client ID, and output result TLVs. Alternatively, input empty
130 * client ID and get next unread client result.
131 * - Copy List. List all client IDs.
132 *
133 * ------------------------------------------------
134 * Client Policy Evaluation
135 * ------------------------------------------------
136 * Policies are evaluated for clients upon client creation, and upon update events,
137 * which are network/agent/policy changes coalesced by a timer.
138 *
139 * The policy evaluation goes through the following steps:
140 * 1. Parse client parameters.
141 * 2. Select a scoped interface if applicable. This involves using require/prohibit
142 * parameters, along with the local address, to select the most appropriate interface
143 * if not explicitly set by the client parameters.
144 * 3. Run NECP application-level policy evalution
145 * 4. Set policy result into client result buffer.
146 *
147 * ------------------------------------------------
148 * Client Observers
149 * ------------------------------------------------
150 * If necp_open() is called with the NECP_OPEN_FLAG_OBSERVER flag, and the process
151 * passes the necessary privilege check, the fd is allowed to use necp_client_action()
152 * to copy client state attached to the file descriptors of other processes, and to
153 * list all client IDs on the system.
154 */
155
156 extern u_int32_t necp_debug;
157
158 static int necpop_select(struct fileproc *, int, void *, vfs_context_t);
159 static int necpop_close(struct fileglob *, vfs_context_t);
160 static int necpop_kqfilter(struct fileproc *, struct knote *, struct kevent_qos_s *);
161
162 // Timer functions
163 static int necp_timeout_microseconds = 1000 * 100; // 100ms
164 static int necp_timeout_leeway_microseconds = 1000 * 50; // 50ms
165 #if SKYWALK
166 static int necp_collect_stats_timeout_microseconds = 1000 * 1000 * 1; // 1s
167 static int necp_collect_stats_timeout_leeway_microseconds = 1000 * 500; // 500ms
168 static int necp_close_arenas_timeout_microseconds = 1000 * 1000 * 10; // 10s
169 static int necp_close_arenas_timeout_leeway_microseconds = 1000 * 1000 * 1; // 1s
170 #endif /* SKYWALK */
171
172 static int necp_client_fd_count = 0;
173 static int necp_observer_fd_count = 0;
174 static int necp_client_count = 0;
175 static int necp_socket_flow_count = 0;
176 static int necp_if_flow_count = 0;
177 static int necp_observer_message_limit = 256;
178
179 /*
180 * NECP client tracing control -
181 *
182 * necp_client_tracing_level : 1 for client trace, 2 for flow trace, 3 for parameter details
183 * necp_client_tracing_pid : match client with pid
184 */
185 static int necp_client_tracing_level = 0;
186 static int necp_client_tracing_pid = 0;
187
188 #define NECP_CLIENT_TRACE_LEVEL_CLIENT 1
189 #define NECP_CLIENT_TRACE_LEVEL_FLOW 2
190 #define NECP_CLIENT_TRACE_LEVEL_PARAMS 3
191
192 #define NECP_CLIENT_TRACE_PID_MATCHED(pid) \
193 (pid == necp_client_tracing_pid)
194
195 #define NECP_ENABLE_CLIENT_TRACE(level) \
196 ((necp_client_tracing_level >= level && \
197 (!necp_client_tracing_pid || NECP_CLIENT_TRACE_PID_MATCHED(client->proc_pid))) ? necp_client_tracing_level : 0)
198
199 #define NECP_CLIENT_LOG(client, fmt, ...) \
200 if (client && NECP_ENABLE_CLIENT_TRACE(NECP_CLIENT_TRACE_LEVEL_CLIENT)) { \
201 uuid_string_t client_uuid_str = { }; \
202 uuid_unparse_lower(client->client_id, client_uuid_str); \
203 NECPLOG(LOG_NOTICE, "NECP_CLIENT_LOG <pid %d %s>: " fmt "\n", client ? client->proc_pid : 0, client_uuid_str, ##__VA_ARGS__); \
204 }
205
206 #define NECP_CLIENT_FLOW_LOG(client, flow, fmt, ...) \
207 if (client && flow && NECP_ENABLE_CLIENT_TRACE(NECP_CLIENT_TRACE_LEVEL_FLOW)) { \
208 uuid_string_t client_uuid_str = { }; \
209 uuid_unparse_lower(client->client_id, client_uuid_str); \
210 uuid_string_t flow_uuid_str = { }; \
211 uuid_unparse_lower(flow->registration_id, flow_uuid_str); \
212 NECPLOG(LOG_NOTICE, "NECP CLIENT FLOW TRACE <pid %d %s> <flow %s>: " fmt "\n", client ? client->proc_pid : 0, client_uuid_str, flow_uuid_str, ##__VA_ARGS__); \
213 }
214
215 #define NECP_CLIENT_PARAMS_LOG(client, fmt, ...) \
216 if (client && NECP_ENABLE_CLIENT_TRACE(NECP_CLIENT_TRACE_LEVEL_PARAMS)) { \
217 uuid_string_t client_uuid_str = { }; \
218 uuid_unparse_lower(client->client_id, client_uuid_str); \
219 NECPLOG(LOG_NOTICE, "NECP_CLIENT_PARAMS_LOG <pid %d %s>: " fmt "\n", client ? client->proc_pid : 0, client_uuid_str, ##__VA_ARGS__); \
220 }
221
222 #define NECP_SOCKET_PID(so) \
223 ((so->so_flags & SOF_DELEGATED) ? so->e_pid : so->last_pid)
224
225 #define NECP_ENABLE_SOCKET_TRACE(level) \
226 ((necp_client_tracing_level >= level && \
227 (!necp_client_tracing_pid || NECP_CLIENT_TRACE_PID_MATCHED(NECP_SOCKET_PID(so)))) ? necp_client_tracing_level : 0)
228
229 #define NECP_SOCKET_PARAMS_LOG(so, fmt, ...) \
230 if (so && NECP_ENABLE_SOCKET_TRACE(NECP_CLIENT_TRACE_LEVEL_PARAMS)) { \
231 NECPLOG(LOG_NOTICE, "NECP_SOCKET_PARAMS_LOG <pid %d>: " fmt "\n", NECP_SOCKET_PID(so), ##__VA_ARGS__); \
232 }
233
234 #define NECP_SOCKET_ATTRIBUTE_LOG(fmt, ...) \
235 if (necp_client_tracing_level >= NECP_CLIENT_TRACE_LEVEL_PARAMS) { \
236 NECPLOG(LOG_NOTICE, "NECP_SOCKET_ATTRIBUTE_LOG: " fmt "\n", ##__VA_ARGS__); \
237 }
238
239 #define NECP_CLIENT_TRACKER_LOG(pid, fmt, ...) \
240 if (pid) { \
241 NECPLOG(LOG_NOTICE, "NECP_CLIENT_TRACKER_LOG <pid %d>: " fmt "\n", pid, ##__VA_ARGS__); \
242 }
243
244 #if SKYWALK
245 static int necp_arena_count = 0;
246 static int necp_sysctl_arena_count = 0;
247 static int necp_nexus_flow_count = 0;
248
249 /* userspace stats sanity check range, same unit as TCP (see TCP_RTT_SCALE) */
250 static uint32_t necp_client_stats_rtt_floor = 1; // 32us
251 static uint32_t necp_client_stats_rtt_ceiling = 1920000; // 60s
252 const static struct sk_stats_flow ntstat_sk_stats_zero;
253 #endif /* SKYWALK */
254
255 /*
256 * Global lock to protect socket inp_necp_attributes across updates.
257 * NECP updating these attributes and clients accessing these attributes
258 * must take this lock.
259 */
260 static LCK_GRP_DECLARE(necp_socket_attr_lock_grp, "necpSocketAttrGroup");
261 LCK_MTX_DECLARE(necp_socket_attr_lock, &necp_socket_attr_lock_grp);
262
263 os_refgrp_decl(static, necp_client_refgrp, "NECPClientRefGroup", NULL);
264
265 SYSCTL_INT(_net_necp, NECPCTL_CLIENT_FD_COUNT, client_fd_count, CTLFLAG_LOCKED | CTLFLAG_RD, &necp_client_fd_count, 0, "");
266 SYSCTL_INT(_net_necp, NECPCTL_OBSERVER_FD_COUNT, observer_fd_count, CTLFLAG_LOCKED | CTLFLAG_RD, &necp_observer_fd_count, 0, "");
267 SYSCTL_INT(_net_necp, NECPCTL_CLIENT_COUNT, client_count, CTLFLAG_LOCKED | CTLFLAG_RD, &necp_client_count, 0, "");
268 SYSCTL_INT(_net_necp, NECPCTL_SOCKET_FLOW_COUNT, socket_flow_count, CTLFLAG_LOCKED | CTLFLAG_RD, &necp_socket_flow_count, 0, "");
269 SYSCTL_INT(_net_necp, NECPCTL_IF_FLOW_COUNT, if_flow_count, CTLFLAG_LOCKED | CTLFLAG_RD, &necp_if_flow_count, 0, "");
270 SYSCTL_INT(_net_necp, NECPCTL_OBSERVER_MESSAGE_LIMIT, observer_message_limit, CTLFLAG_LOCKED | CTLFLAG_RW, &necp_observer_message_limit, 256, "");
271 SYSCTL_INT(_net_necp, NECPCTL_CLIENT_TRACING_LEVEL, necp_client_tracing_level, CTLFLAG_LOCKED | CTLFLAG_RW, &necp_client_tracing_level, 0, "");
272 SYSCTL_INT(_net_necp, NECPCTL_CLIENT_TRACING_PID, necp_client_tracing_pid, CTLFLAG_LOCKED | CTLFLAG_RW, &necp_client_tracing_pid, 0, "");
273
274 #if SKYWALK
275 SYSCTL_INT(_net_necp, NECPCTL_ARENA_COUNT, arena_count, CTLFLAG_LOCKED | CTLFLAG_RD, &necp_arena_count, 0, "");
276 SYSCTL_INT(_net_necp, NECPCTL_SYSCTL_ARENA_COUNT, sysctl_arena_count, CTLFLAG_LOCKED | CTLFLAG_RD, &necp_sysctl_arena_count, 0, "");
277 SYSCTL_INT(_net_necp, NECPCTL_NEXUS_FLOW_COUNT, nexus_flow_count, CTLFLAG_LOCKED | CTLFLAG_RD, &necp_nexus_flow_count, 0, "");
278 #if (DEVELOPMENT || DEBUG)
279 SYSCTL_UINT(_net_necp, OID_AUTO, collect_stats_interval_us, CTLFLAG_RW | CTLFLAG_LOCKED, &necp_collect_stats_timeout_microseconds, 0, "");
280 SYSCTL_UINT(_net_necp, OID_AUTO, necp_client_stats_rtt_floor, CTLFLAG_RW | CTLFLAG_LOCKED, &necp_client_stats_rtt_floor, 0, "");
281 SYSCTL_UINT(_net_necp, OID_AUTO, necp_client_stats_rtt_ceiling, CTLFLAG_RW | CTLFLAG_LOCKED, &necp_client_stats_rtt_ceiling, 0, "");
282 #endif /* (DEVELOPMENT || DEBUG) */
283 #endif /* SKYWALK */
284
285 #define NECP_MAX_CLIENT_LIST_SIZE 1024 * 1024 // 1MB
286 #define NECP_MAX_AGENT_ACTION_SIZE 10 * 1024 // 10K
287
288 extern int tvtohz(struct timeval *);
289 extern unsigned int get_maxmtu(struct rtentry *);
290
291 // Parsed parameters
292 #define NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR 0x00001
293 #define NECP_PARSED_PARAMETERS_FIELD_REMOTE_ADDR 0x00002
294 #define NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF 0x00004
295 #define NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IF 0x00008
296 #define NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE 0x00010
297 #define NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IFTYPE 0x00020
298 #define NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT 0x00040
299 #define NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT 0x00080
300 #define NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT 0x00100
301 #define NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT 0x00200
302 #define NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE 0x00400
303 #define NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT_TYPE 0x00800
304 #define NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE 0x01000
305 #define NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE 0x02000
306 #define NECP_PARSED_PARAMETERS_FIELD_FLAGS 0x04000
307 #define NECP_PARSED_PARAMETERS_FIELD_IP_PROTOCOL 0x08000
308 #define NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID 0x10000
309 #define NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_UUID 0x20000
310 #define NECP_PARSED_PARAMETERS_FIELD_TRAFFIC_CLASS 0x40000
311 #define NECP_PARSED_PARAMETERS_FIELD_LOCAL_PORT 0x80000
312 #define NECP_PARSED_PARAMETERS_FIELD_DELEGATED_UPID 0x100000
313 #define NECP_PARSED_PARAMETERS_FIELD_ETHERTYPE 0x200000
314 #define NECP_PARSED_PARAMETERS_FIELD_TRANSPORT_PROTOCOL 0x400000
315 #define NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR_PREFERENCE 0x800000
316 #define NECP_PARSED_PARAMETERS_FIELD_ATTRIBUTED_BUNDLE_IDENTIFIER 0x1000000
317 #define NECP_PARSED_PARAMETERS_FIELD_PARENT_UUID 0x2000000
318 #define NECP_PARSED_PARAMETERS_FIELD_FLOW_DEMUX_PATTERN 0x4000000
319
320
321 #define NECP_MAX_INTERFACE_PARAMETERS 16
322 #define NECP_MAX_AGENT_PARAMETERS 4
323 struct necp_client_parsed_parameters {
324 u_int32_t valid_fields;
325 u_int32_t flags;
326 u_int64_t delegated_upid;
327 union necp_sockaddr_union local_addr;
328 union necp_sockaddr_union remote_addr;
329 u_int32_t required_interface_index;
330 char prohibited_interfaces[NECP_MAX_INTERFACE_PARAMETERS][IFXNAMSIZ];
331 u_int8_t required_interface_type;
332 u_int8_t local_address_preference;
333 u_int8_t prohibited_interface_types[NECP_MAX_INTERFACE_PARAMETERS];
334 struct necp_client_parameter_netagent_type required_netagent_types[NECP_MAX_AGENT_PARAMETERS];
335 struct necp_client_parameter_netagent_type prohibited_netagent_types[NECP_MAX_AGENT_PARAMETERS];
336 struct necp_client_parameter_netagent_type preferred_netagent_types[NECP_MAX_AGENT_PARAMETERS];
337 struct necp_client_parameter_netagent_type avoided_netagent_types[NECP_MAX_AGENT_PARAMETERS];
338 uuid_t required_netagents[NECP_MAX_AGENT_PARAMETERS];
339 uuid_t prohibited_netagents[NECP_MAX_AGENT_PARAMETERS];
340 uuid_t preferred_netagents[NECP_MAX_AGENT_PARAMETERS];
341 uuid_t avoided_netagents[NECP_MAX_AGENT_PARAMETERS];
342 u_int8_t ip_protocol;
343 u_int8_t transport_protocol;
344 u_int16_t ethertype;
345 pid_t effective_pid;
346 uuid_t effective_uuid;
347 uuid_t parent_uuid;
348 u_int32_t traffic_class;
349 struct necp_demux_pattern demux_patterns[NECP_MAX_DEMUX_PATTERNS];
350 u_int8_t demux_pattern_count;
351 };
352
353 static bool
354 necp_find_matching_interface_index(struct necp_client_parsed_parameters *parsed_parameters,
355 u_int *return_ifindex, bool *validate_agents);
356
357 static bool
358 necp_ifnet_matches_local_address(struct ifnet *ifp, struct sockaddr *sa);
359
360 static bool
361 necp_ifnet_matches_parameters(struct ifnet *ifp,
362 struct necp_client_parsed_parameters *parsed_parameters,
363 u_int32_t override_flags,
364 u_int32_t *preferred_count,
365 bool secondary_interface,
366 bool require_scoped_field);
367
368 static const struct fileops necp_fd_ops = {
369 .fo_type = DTYPE_NETPOLICY,
370 .fo_read = fo_no_read,
371 .fo_write = fo_no_write,
372 .fo_ioctl = fo_no_ioctl,
373 .fo_select = necpop_select,
374 .fo_close = necpop_close,
375 .fo_drain = fo_no_drain,
376 .fo_kqfilter = necpop_kqfilter,
377 };
378
379 struct necp_client_assertion {
380 LIST_ENTRY(necp_client_assertion) assertion_chain;
381 uuid_t asserted_netagent;
382 };
383
384 struct necp_client_flow_header {
385 struct necp_tlv_header outer_header;
386 struct necp_tlv_header flow_id_tlv_header;
387 uuid_t flow_id;
388 struct necp_tlv_header flags_tlv_header;
389 u_int32_t flags_value;
390 struct necp_tlv_header interface_tlv_header;
391 struct necp_client_result_interface interface_value;
392 } __attribute__((__packed__));
393
394 struct necp_client_flow_protoctl_event_header {
395 struct necp_tlv_header protoctl_tlv_header;
396 struct necp_client_flow_protoctl_event protoctl_event;
397 } __attribute__((__packed__));
398
399 struct necp_client_nexus_flow_header {
400 struct necp_client_flow_header flow_header;
401 struct necp_tlv_header agent_tlv_header;
402 struct necp_client_result_netagent agent_value;
403 struct necp_tlv_header tfo_cookie_tlv_header;
404 u_int8_t tfo_cookie_value[NECP_TFO_COOKIE_LEN_MAX];
405 } __attribute__((__packed__));
406
407 #if SKYWALK
408 struct necp_arena_info;
409 #endif
410
411 struct necp_client_flow {
412 LIST_ENTRY(necp_client_flow) flow_chain;
413 unsigned invalid : 1;
414 unsigned nexus : 1; // If true, flow is a nexus; if false, flow is attached to socket
415 unsigned socket : 1;
416 unsigned viable : 1;
417 unsigned assigned : 1;
418 unsigned has_protoctl_event : 1;
419 unsigned check_tcp_heuristics : 1;
420 unsigned _reserved : 1;
421 union {
422 uuid_t nexus_agent;
423 struct {
424 void *socket_handle;
425 necp_client_flow_cb cb;
426 };
427 } u;
428 uint32_t interface_index;
429 u_short delegated_interface_index;
430 uint16_t interface_flags;
431 uint32_t necp_flow_flags;
432 struct necp_client_flow_protoctl_event protoctl_event;
433 union necp_sockaddr_union local_addr;
434 union necp_sockaddr_union remote_addr;
435
436 size_t assigned_results_length;
437 u_int8_t *assigned_results;
438 };
439
440 struct necp_client_flow_registration {
441 RB_ENTRY(necp_client_flow_registration) fd_link;
442 RB_ENTRY(necp_client_flow_registration) global_link;
443 RB_ENTRY(necp_client_flow_registration) client_link;
444 LIST_ENTRY(necp_client_flow_registration) collect_stats_chain;
445 uuid_t registration_id;
446 u_int32_t flags;
447 unsigned flow_result_read : 1;
448 unsigned defunct : 1;
449 void *interface_handle;
450 necp_client_flow_cb interface_cb;
451 struct necp_client *client;
452 LIST_HEAD(_necp_registration_flow_list, necp_client_flow) flow_list;
453 #if SKYWALK
454 struct necp_arena_info *stats_arena; /* arena where the stats objects came from */
455 void * kstats_kaddr; /* kernel snapshot of untrusted userspace stats, for calculating delta */
456 mach_vm_address_t ustats_uaddr; /* userspace stats (untrusted) */
457 nstat_userland_context stats_handler_context;
458 struct flow_stats *nexus_stats; /* shared stats objects between necp_client and skywalk */
459 #endif /* !SKYWALK */
460 u_int64_t last_interface_details __attribute__((aligned(sizeof(u_int64_t))));
461 };
462
463 static int necp_client_flow_id_cmp(struct necp_client_flow_registration *flow0, struct necp_client_flow_registration *flow1);
464
465 RB_HEAD(_necp_client_flow_tree, necp_client_flow_registration);
466 RB_PROTOTYPE_PREV(_necp_client_flow_tree, necp_client_flow_registration, client_link, necp_client_flow_id_cmp);
467 RB_GENERATE_PREV(_necp_client_flow_tree, necp_client_flow_registration, client_link, necp_client_flow_id_cmp);
468
469 #define NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT 4
470 #define NECP_CLIENT_MAX_INTERFACE_OPTIONS 32
471
472 #define NECP_CLIENT_INTERFACE_OPTION_EXTRA_COUNT (NECP_CLIENT_MAX_INTERFACE_OPTIONS - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT)
473
474 struct necp_client {
475 RB_ENTRY(necp_client) link;
476 RB_ENTRY(necp_client) global_link;
477
478 decl_lck_mtx_data(, lock);
479 decl_lck_mtx_data(, route_lock);
480 os_refcnt_t reference_count;
481
482 uuid_t client_id;
483 unsigned result_read : 1;
484 unsigned group_members_read : 1;
485 unsigned allow_multiple_flows : 1;
486 unsigned legacy_client_is_flow : 1;
487
488 unsigned platform_binary : 1;
489 unsigned validated_parent : 1;
490
491 size_t result_length;
492 u_int8_t result[NECP_BASE_CLIENT_RESULT_SIZE];
493
494 necp_policy_id policy_id;
495
496 u_int8_t ip_protocol;
497 int proc_pid;
498
499 u_int64_t delegated_upid;
500
501 struct _necp_client_flow_tree flow_registrations;
502 LIST_HEAD(_necp_client_assertion_list, necp_client_assertion) assertion_list;
503
504 size_t assigned_group_members_length;
505 u_int8_t *assigned_group_members;
506
507 struct rtentry *current_route;
508
509 struct necp_client_interface_option interface_options[NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT];
510 struct necp_client_interface_option *extra_interface_options;
511 u_int8_t interface_option_count; // Number in interface_options + extra_interface_options
512
513 struct necp_client_result_netagent failed_trigger_agent;
514
515 void *agent_handle;
516
517 uuid_t override_euuid;
518
519 #if SKYWALK
520 netns_token port_reservation;
521 nstat_context nstat_context;
522 uuid_t latest_flow_registration_id;
523 uuid_t parent_client_id;
524 struct necp_client *original_parameters_source;
525 #endif /* !SKYWALK */
526
527 size_t parameters_length;
528 u_int8_t *parameters;
529 };
530
531 #define NECP_CLIENT_LOCK(_c) lck_mtx_lock(&_c->lock)
532 #define NECP_CLIENT_UNLOCK(_c) lck_mtx_unlock(&_c->lock)
533 #define NECP_CLIENT_ASSERT_LOCKED(_c) LCK_MTX_ASSERT(&_c->lock, LCK_MTX_ASSERT_OWNED)
534 #define NECP_CLIENT_ASSERT_UNLOCKED(_c) LCK_MTX_ASSERT(&_c->lock, LCK_MTX_ASSERT_NOTOWNED)
535
536 #define NECP_CLIENT_ROUTE_LOCK(_c) lck_mtx_lock(&_c->route_lock)
537 #define NECP_CLIENT_ROUTE_UNLOCK(_c) lck_mtx_unlock(&_c->route_lock)
538
539 static void necp_client_retain_locked(struct necp_client *client);
540 static void necp_client_retain(struct necp_client *client);
541
542 static bool necp_client_release_locked(struct necp_client *client);
543 static bool necp_client_release(struct necp_client *client);
544
545 static void
546 necp_client_add_assertion(struct necp_client *client, uuid_t netagent_uuid);
547
548 static bool
549 necp_client_remove_assertion(struct necp_client *client, uuid_t netagent_uuid);
550
551 static int
552 necp_client_copy_parameters_locked(struct necp_client *client,
553 struct necp_client_nexus_parameters *parameters);
554
555 LIST_HEAD(_necp_flow_registration_list, necp_client_flow_registration);
556 static struct _necp_flow_registration_list necp_collect_stats_flow_list;
557
558 struct necp_flow_defunct {
559 LIST_ENTRY(necp_flow_defunct) chain;
560
561 uuid_t flow_id;
562 uuid_t nexus_agent;
563 void *agent_handle;
564 int proc_pid;
565 u_int32_t flags;
566 struct necp_client_agent_parameters close_parameters;
567 bool has_close_parameters;
568 };
569
570 LIST_HEAD(_necp_flow_defunct_list, necp_flow_defunct);
571
572 static int necp_client_id_cmp(struct necp_client *client0, struct necp_client *client1);
573
574 RB_HEAD(_necp_client_tree, necp_client);
575 RB_PROTOTYPE_PREV(_necp_client_tree, necp_client, link, necp_client_id_cmp);
576 RB_GENERATE_PREV(_necp_client_tree, necp_client, link, necp_client_id_cmp);
577
578 RB_HEAD(_necp_client_global_tree, necp_client);
579 RB_PROTOTYPE_PREV(_necp_client_global_tree, necp_client, global_link, necp_client_id_cmp);
580 RB_GENERATE_PREV(_necp_client_global_tree, necp_client, global_link, necp_client_id_cmp);
581
582 RB_HEAD(_necp_fd_flow_tree, necp_client_flow_registration);
583 RB_PROTOTYPE_PREV(_necp_fd_flow_tree, necp_client_flow_registration, fd_link, necp_client_flow_id_cmp);
584 RB_GENERATE_PREV(_necp_fd_flow_tree, necp_client_flow_registration, fd_link, necp_client_flow_id_cmp);
585
586 RB_HEAD(_necp_client_flow_global_tree, necp_client_flow_registration);
587 RB_PROTOTYPE_PREV(_necp_client_flow_global_tree, necp_client_flow_registration, global_link, necp_client_flow_id_cmp);
588 RB_GENERATE_PREV(_necp_client_flow_global_tree, necp_client_flow_registration, global_link, necp_client_flow_id_cmp);
589
590 static struct _necp_client_global_tree necp_client_global_tree;
591 static struct _necp_client_flow_global_tree necp_client_flow_global_tree;
592
593 struct necp_client_update {
594 TAILQ_ENTRY(necp_client_update) chain;
595
596 uuid_t client_id;
597
598 size_t update_length;
599 struct necp_client_observer_update *update;
600 };
601
602 #if SKYWALK
603 struct necp_arena_info {
604 LIST_ENTRY(necp_arena_info) nai_chain;
605 u_int32_t nai_flags;
606 pid_t nai_proc_pid;
607 struct skmem_arena *nai_arena;
608 struct skmem_arena_mmap_info nai_mmap;
609 mach_vm_offset_t nai_roff;
610 u_int32_t nai_use_count;
611 };
612 #endif /* !SKYWALK */
613
614 #define NAIF_ATTACHED 0x1 // arena is attached to list
615 #define NAIF_REDIRECT 0x2 // arena mmap has been redirected
616 #define NAIF_DEFUNCT 0x4 // arena is now defunct
617
618 struct necp_fd_data {
619 u_int8_t necp_fd_type;
620 LIST_ENTRY(necp_fd_data) chain;
621 struct _necp_client_tree clients;
622 struct _necp_fd_flow_tree flows;
623 TAILQ_HEAD(_necp_client_update_list, necp_client_update) update_list;
624 int update_count;
625 int flags;
626
627 unsigned background : 1;
628
629 int proc_pid;
630 decl_lck_mtx_data(, fd_lock);
631 struct selinfo si;
632 #if SKYWALK
633 // Arenas and their mmap info for per-process stats. Stats objects are allocated from an active arena
634 // that is not redirected/defunct. The stats_arena_active keeps track of such an arena, and it also
635 // holds a reference count on the object. Each flow allocating a stats object also holds a reference
636 // the necp_arena_info (where the object got allocated from). During defunct, we redirect the mapping
637 // of the arena such that any attempt to access (read/write) will result in getting zero-filled pages.
638 // We then go thru all of the flows for the process and free the stats objects associated with them,
639 // followed by destroying the skmem region(s) associated with the arena. The stats_arena_list keeps
640 // track of all current and defunct stats arenas; there could be more than one arena created for the
641 // process as the arena destruction happens when its reference count drops to 0.
642 struct necp_arena_info *stats_arena_active;
643 LIST_HEAD(_necp_arena_info_list, necp_arena_info) stats_arena_list;
644 u_int32_t stats_arena_gencnt;
645
646 struct skmem_arena *sysctl_arena;
647 struct skmem_arena_mmap_info sysctl_mmap;
648 mach_vm_offset_t system_sysctls_roff;
649 #endif /* !SKYWALK */
650 };
651
652 #define NECP_FD_LOCK(_f) lck_mtx_lock(&_f->fd_lock)
653 #define NECP_FD_UNLOCK(_f) lck_mtx_unlock(&_f->fd_lock)
654 #define NECP_FD_ASSERT_LOCKED(_f) LCK_MTX_ASSERT(&_f->fd_lock, LCK_MTX_ASSERT_OWNED)
655 #define NECP_FD_ASSERT_UNLOCKED(_f) LCK_MTX_ASSERT(&_f->fd_lock, LCK_MTX_ASSERT_NOTOWNED)
656
657 static LIST_HEAD(_necp_fd_list, necp_fd_data) necp_fd_list;
658 static LIST_HEAD(_necp_fd_observer_list, necp_fd_data) necp_fd_observer_list;
659
660 #if SKYWALK
661 static ZONE_DEFINE(necp_arena_info_zone, "necp.arenainfo",
662 sizeof(struct necp_arena_info), ZC_ZFREE_CLEARMEM);
663 #endif /* !SKYWALK */
664
665 static LCK_ATTR_DECLARE(necp_fd_mtx_attr, 0, 0);
666 static LCK_GRP_DECLARE(necp_fd_mtx_grp, "necp_fd");
667
668 static LCK_RW_DECLARE_ATTR(necp_fd_lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
669 static LCK_RW_DECLARE_ATTR(necp_observer_lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
670 static LCK_RW_DECLARE_ATTR(necp_client_tree_lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
671 static LCK_RW_DECLARE_ATTR(necp_flow_tree_lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
672 static LCK_RW_DECLARE_ATTR(necp_collect_stats_list_lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
673
674
675 #define NECP_STATS_LIST_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_collect_stats_list_lock)
676 #define NECP_STATS_LIST_LOCK_SHARED() lck_rw_lock_shared(&necp_collect_stats_list_lock)
677 #define NECP_STATS_LIST_UNLOCK() lck_rw_done(&necp_collect_stats_list_lock)
678
679 #define NECP_CLIENT_TREE_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_client_tree_lock)
680 #define NECP_CLIENT_TREE_LOCK_SHARED() lck_rw_lock_shared(&necp_client_tree_lock)
681 #define NECP_CLIENT_TREE_UNLOCK() lck_rw_done(&necp_client_tree_lock)
682 #define NECP_CLIENT_TREE_ASSERT_LOCKED() LCK_RW_ASSERT(&necp_client_tree_lock, LCK_RW_ASSERT_HELD)
683
684 #define NECP_FLOW_TREE_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_flow_tree_lock)
685 #define NECP_FLOW_TREE_LOCK_SHARED() lck_rw_lock_shared(&necp_flow_tree_lock)
686 #define NECP_FLOW_TREE_UNLOCK() lck_rw_done(&necp_flow_tree_lock)
687 #define NECP_FLOW_TREE_ASSERT_LOCKED() LCK_RW_ASSERT(&necp_flow_tree_lock, LCK_RW_ASSERT_HELD)
688
689 #define NECP_FD_LIST_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_fd_lock)
690 #define NECP_FD_LIST_LOCK_SHARED() lck_rw_lock_shared(&necp_fd_lock)
691 #define NECP_FD_LIST_UNLOCK() lck_rw_done(&necp_fd_lock)
692
693 #define NECP_OBSERVER_LIST_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_observer_lock)
694 #define NECP_OBSERVER_LIST_LOCK_SHARED() lck_rw_lock_shared(&necp_observer_lock)
695 #define NECP_OBSERVER_LIST_UNLOCK() lck_rw_done(&necp_observer_lock)
696
697 // Locking Notes
698
699 // Take NECP_FD_LIST_LOCK when accessing or modifying the necp_fd_list
700 // Take NECP_CLIENT_TREE_LOCK when accessing or modifying the necp_client_global_tree
701 // Take NECP_FLOW_TREE_LOCK when accessing or modifying the necp_client_flow_global_tree
702 // Take NECP_STATS_LIST_LOCK when accessing or modifying the necp_collect_stats_flow_list
703 // Take NECP_FD_LOCK when accessing or modifying an necp_fd_data entry
704 // Take NECP_CLIENT_LOCK when accessing or modifying a single necp_client
705 // Take NECP_CLIENT_ROUTE_LOCK when accessing or modifying a client's route
706
707 // Precedence, where 1 is the first lock that must be taken
708 // 1. NECP_FD_LIST_LOCK
709 // 2. NECP_FD_LOCK (any)
710 // 3. NECP_CLIENT_TREE_LOCK
711 // 4. NECP_CLIENT_LOCK (any)
712 // 5. NECP_FLOW_TREE_LOCK
713 // 6. NECP_STATS_LIST_LOCK
714 // 7. NECP_CLIENT_ROUTE_LOCK (any)
715
716 static thread_call_t necp_client_update_tcall;
717 static uint32_t necp_update_all_clients_sched_cnt = 0;
718 static uint64_t necp_update_all_clients_sched_abstime = 0;
719 static LCK_RW_DECLARE_ATTR(necp_update_all_clients_lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
720 #define NECP_UPDATE_ALL_CLIENTS_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_update_all_clients_lock)
721 #define NECP_UPDATE_ALL_CLIENTS_SHARED_TO_EXCLUSIVE() lck_rw_lock_shared_to_exclusive(&necp_update_all_clients_lock)
722 #define NECP_UPDATE_ALL_CLIENTS_SHARED() lck_rw_lock_shared(&necp_update_all_clients_lock)
723 #define NECP_UPDATE_ALL_CLIENTS_UNLOCK() lck_rw_done(&necp_update_all_clients_lock)
724
725
726 #if SKYWALK
727 static thread_call_t necp_client_collect_stats_tcall;
728 static thread_call_t necp_close_empty_arenas_tcall;
729
730 static void necp_fd_insert_stats_arena(struct necp_fd_data *fd_data, struct necp_arena_info *nai);
731 static void necp_fd_remove_stats_arena(struct necp_fd_data *fd_data, struct necp_arena_info *nai);
732 static struct necp_arena_info *necp_fd_mredirect_stats_arena(struct necp_fd_data *fd_data, struct proc *proc);
733
734 static void necp_arena_info_retain(struct necp_arena_info *nai);
735 static void necp_arena_info_release(struct necp_arena_info *nai);
736 static struct necp_arena_info *necp_arena_info_alloc(void);
737 static void necp_arena_info_free(struct necp_arena_info *nai);
738
739 static int necp_arena_initialize(struct necp_fd_data *fd_data, bool locked);
740 static int necp_stats_initialize(struct necp_fd_data *fd_data, struct necp_client *client,
741 struct necp_client_flow_registration *flow_registration, struct necp_stats_bufreq *bufreq);
742 static int necp_arena_create(struct necp_fd_data *fd_data, size_t obj_size, size_t obj_cnt, struct proc *p);
743 static int necp_arena_stats_obj_alloc(struct necp_fd_data *fd_data, mach_vm_offset_t *off, struct necp_arena_info **stats_arena, void **kstats_kaddr, boolean_t cansleep);
744 static void necp_arena_stats_obj_free(struct necp_fd_data *fd_data, struct necp_arena_info *stats_arena, void **kstats_kaddr, mach_vm_address_t *ustats_uaddr);
745 static void necp_stats_arenas_destroy(struct necp_fd_data *fd_data, boolean_t closing);
746
747 static int necp_sysctl_arena_initialize(struct necp_fd_data *fd_data, bool locked);
748 static void necp_sysctl_arena_destroy(struct necp_fd_data *fd_data);
749 static void *necp_arena_sysctls_obj(struct necp_fd_data *fd_data, mach_vm_offset_t *off, size_t *size);
750 #endif /* !SKYWALK */
751
752 void necp_copy_inp_domain_info(struct inpcb *, struct socket *, nstat_domain_info *);
753 void necp_with_inp_domain_name(struct socket *so, void *ctx, void (*with_func)(char *domain_name, void *ctx));
754
755 static void
necp_lock_socket_attributes(void)756 necp_lock_socket_attributes(void)
757 {
758 lck_mtx_lock(&necp_socket_attr_lock);
759 }
760
761 static void
necp_unlock_socket_attributes(void)762 necp_unlock_socket_attributes(void)
763 {
764 lck_mtx_unlock(&necp_socket_attr_lock);
765 }
766
767 /// NECP file descriptor functions
768
769 static void
necp_fd_notify(struct necp_fd_data * fd_data,bool locked)770 necp_fd_notify(struct necp_fd_data *fd_data, bool locked)
771 {
772 struct selinfo *si = &fd_data->si;
773
774 if (!locked) {
775 NECP_FD_LOCK(fd_data);
776 }
777
778 selwakeup(si);
779
780 // use a non-zero hint to tell the notification from the
781 // call done in kqueue_scan() which uses 0
782 KNOTE(&si->si_note, 1); // notification
783
784 if (!locked) {
785 NECP_FD_UNLOCK(fd_data);
786 }
787 }
788
789 static inline bool
necp_client_has_unread_flows(struct necp_client * client)790 necp_client_has_unread_flows(struct necp_client *client)
791 {
792 NECP_CLIENT_ASSERT_LOCKED(client);
793 struct necp_client_flow_registration *flow_registration = NULL;
794 RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
795 if (!flow_registration->flow_result_read) {
796 return true;
797 }
798 }
799 return false;
800 }
801
802 static int
necp_fd_poll(struct necp_fd_data * fd_data,int events,void * wql,struct proc * p,int is_kevent)803 necp_fd_poll(struct necp_fd_data *fd_data, int events, void *wql, struct proc *p, int is_kevent)
804 {
805 #pragma unused(wql, p, is_kevent)
806 u_int revents = 0;
807
808 u_int want_rx = events & (POLLIN | POLLRDNORM);
809 if (want_rx) {
810 if (fd_data->flags & NECP_OPEN_FLAG_PUSH_OBSERVER) {
811 // Push-mode observers are readable when they have a new update
812 if (!TAILQ_EMPTY(&fd_data->update_list)) {
813 revents |= want_rx;
814 }
815 } else {
816 // Standard fds are readable when some client is unread
817 struct necp_client *client = NULL;
818 bool has_unread_clients = FALSE;
819 RB_FOREACH(client, _necp_client_tree, &fd_data->clients) {
820 NECP_CLIENT_LOCK(client);
821 if (!client->result_read || !client->group_members_read || necp_client_has_unread_flows(client)) {
822 has_unread_clients = TRUE;
823 }
824 NECP_CLIENT_UNLOCK(client);
825 if (has_unread_clients) {
826 break;
827 }
828 }
829
830 if (has_unread_clients) {
831 revents |= want_rx;
832 }
833 }
834 }
835
836 return revents;
837 }
838
839 static inline void
necp_generate_client_id(uuid_t client_id,bool is_flow)840 necp_generate_client_id(uuid_t client_id, bool is_flow)
841 {
842 uuid_generate_random(client_id);
843
844 if (is_flow) {
845 client_id[9] |= 0x01;
846 } else {
847 client_id[9] &= ~0x01;
848 }
849 }
850
851 static inline bool
necp_client_id_is_flow(uuid_t client_id)852 necp_client_id_is_flow(uuid_t client_id)
853 {
854 return client_id[9] & 0x01;
855 }
856
857 static struct necp_client *
necp_find_client_and_lock(uuid_t client_id)858 necp_find_client_and_lock(uuid_t client_id)
859 {
860 NECP_CLIENT_TREE_ASSERT_LOCKED();
861
862 struct necp_client *client = NULL;
863
864 if (necp_client_id_is_flow(client_id)) {
865 NECP_FLOW_TREE_LOCK_SHARED();
866 struct necp_client_flow_registration find;
867 uuid_copy(find.registration_id, client_id);
868 struct necp_client_flow_registration *flow = RB_FIND(_necp_client_flow_global_tree, &necp_client_flow_global_tree, &find);
869 if (flow != NULL) {
870 client = flow->client;
871 }
872 NECP_FLOW_TREE_UNLOCK();
873 } else {
874 struct necp_client find;
875 uuid_copy(find.client_id, client_id);
876 client = RB_FIND(_necp_client_global_tree, &necp_client_global_tree, &find);
877 }
878
879 if (client != NULL) {
880 NECP_CLIENT_LOCK(client);
881 }
882
883 return client;
884 }
885
886 static struct necp_client_flow_registration *
necp_client_find_flow(struct necp_client * client,uuid_t flow_id)887 necp_client_find_flow(struct necp_client *client, uuid_t flow_id)
888 {
889 NECP_CLIENT_ASSERT_LOCKED(client);
890 struct necp_client_flow_registration *flow = NULL;
891
892 if (necp_client_id_is_flow(flow_id)) {
893 struct necp_client_flow_registration find;
894 uuid_copy(find.registration_id, flow_id);
895 flow = RB_FIND(_necp_client_flow_tree, &client->flow_registrations, &find);
896 } else {
897 flow = RB_ROOT(&client->flow_registrations);
898 }
899
900 return flow;
901 }
902
903 static struct necp_client *
necp_client_fd_find_client_unlocked(struct necp_fd_data * client_fd,uuid_t client_id)904 necp_client_fd_find_client_unlocked(struct necp_fd_data *client_fd, uuid_t client_id)
905 {
906 NECP_FD_ASSERT_LOCKED(client_fd);
907 struct necp_client *client = NULL;
908
909 if (necp_client_id_is_flow(client_id)) {
910 struct necp_client_flow_registration find;
911 uuid_copy(find.registration_id, client_id);
912 struct necp_client_flow_registration *flow = RB_FIND(_necp_fd_flow_tree, &client_fd->flows, &find);
913 if (flow != NULL) {
914 client = flow->client;
915 }
916 } else {
917 struct necp_client find;
918 uuid_copy(find.client_id, client_id);
919 client = RB_FIND(_necp_client_tree, &client_fd->clients, &find);
920 }
921
922 return client;
923 }
924
925 static struct necp_client *
necp_client_fd_find_client_and_lock(struct necp_fd_data * client_fd,uuid_t client_id)926 necp_client_fd_find_client_and_lock(struct necp_fd_data *client_fd, uuid_t client_id)
927 {
928 struct necp_client *client = necp_client_fd_find_client_unlocked(client_fd, client_id);
929 if (client != NULL) {
930 NECP_CLIENT_LOCK(client);
931 }
932
933 return client;
934 }
935
936 static inline int
necp_client_id_cmp(struct necp_client * client0,struct necp_client * client1)937 necp_client_id_cmp(struct necp_client *client0, struct necp_client *client1)
938 {
939 return uuid_compare(client0->client_id, client1->client_id);
940 }
941
942 static inline int
necp_client_flow_id_cmp(struct necp_client_flow_registration * flow0,struct necp_client_flow_registration * flow1)943 necp_client_flow_id_cmp(struct necp_client_flow_registration *flow0, struct necp_client_flow_registration *flow1)
944 {
945 return uuid_compare(flow0->registration_id, flow1->registration_id);
946 }
947
948 static int
necpop_select(struct fileproc * fp,int which,void * wql,vfs_context_t ctx)949 necpop_select(struct fileproc *fp, int which, void *wql, vfs_context_t ctx)
950 {
951 #pragma unused(fp, which, wql, ctx)
952 return 0;
953 struct necp_fd_data *fd_data = NULL;
954 int revents = 0;
955 int events = 0;
956 proc_t procp;
957
958 fd_data = (struct necp_fd_data *)fp_get_data(fp);
959 if (fd_data == NULL) {
960 return 0;
961 }
962
963 procp = vfs_context_proc(ctx);
964
965 switch (which) {
966 case FREAD: {
967 events = POLLIN;
968 break;
969 }
970
971 default: {
972 return 1;
973 }
974 }
975
976 NECP_FD_LOCK(fd_data);
977 revents = necp_fd_poll(fd_data, events, wql, procp, 0);
978 NECP_FD_UNLOCK(fd_data);
979
980 return (events & revents) ? 1 : 0;
981 }
982
983 static void
necp_fd_knrdetach(struct knote * kn)984 necp_fd_knrdetach(struct knote *kn)
985 {
986 struct necp_fd_data *fd_data = (struct necp_fd_data *)kn->kn_hook;
987 struct selinfo *si = &fd_data->si;
988
989 NECP_FD_LOCK(fd_data);
990 KNOTE_DETACH(&si->si_note, kn);
991 NECP_FD_UNLOCK(fd_data);
992 }
993
994 static int
necp_fd_knread(struct knote * kn,long hint)995 necp_fd_knread(struct knote *kn, long hint)
996 {
997 #pragma unused(kn, hint)
998 return 1; /* assume we are ready */
999 }
1000
1001 static int
necp_fd_knrprocess(struct knote * kn,struct kevent_qos_s * kev)1002 necp_fd_knrprocess(struct knote *kn, struct kevent_qos_s *kev)
1003 {
1004 struct necp_fd_data *fd_data;
1005 int revents;
1006 int res;
1007
1008 fd_data = (struct necp_fd_data *)kn->kn_hook;
1009
1010 NECP_FD_LOCK(fd_data);
1011 revents = necp_fd_poll(fd_data, POLLIN, NULL, current_proc(), 1);
1012 res = ((revents & POLLIN) != 0);
1013 if (res) {
1014 knote_fill_kevent(kn, kev, 0);
1015 }
1016 NECP_FD_UNLOCK(fd_data);
1017 return res;
1018 }
1019
1020 static int
necp_fd_knrtouch(struct knote * kn,struct kevent_qos_s * kev)1021 necp_fd_knrtouch(struct knote *kn, struct kevent_qos_s *kev)
1022 {
1023 #pragma unused(kev)
1024 struct necp_fd_data *fd_data;
1025 int revents;
1026
1027 fd_data = (struct necp_fd_data *)kn->kn_hook;
1028
1029 NECP_FD_LOCK(fd_data);
1030 revents = necp_fd_poll(fd_data, POLLIN, NULL, current_proc(), 1);
1031 NECP_FD_UNLOCK(fd_data);
1032
1033 return (revents & POLLIN) != 0;
1034 }
1035
1036 SECURITY_READ_ONLY_EARLY(struct filterops) necp_fd_rfiltops = {
1037 .f_isfd = 1,
1038 .f_detach = necp_fd_knrdetach,
1039 .f_event = necp_fd_knread,
1040 .f_touch = necp_fd_knrtouch,
1041 .f_process = necp_fd_knrprocess,
1042 };
1043
1044 static int
necpop_kqfilter(struct fileproc * fp,struct knote * kn,__unused struct kevent_qos_s * kev)1045 necpop_kqfilter(struct fileproc *fp, struct knote *kn,
1046 __unused struct kevent_qos_s *kev)
1047 {
1048 struct necp_fd_data *fd_data = NULL;
1049 int revents;
1050
1051 if (kn->kn_filter != EVFILT_READ) {
1052 NECPLOG(LOG_ERR, "bad filter request %d", kn->kn_filter);
1053 knote_set_error(kn, EINVAL);
1054 return 0;
1055 }
1056
1057 fd_data = (struct necp_fd_data *)fp_get_data(fp);
1058 if (fd_data == NULL) {
1059 NECPLOG0(LOG_ERR, "No channel for kqfilter");
1060 knote_set_error(kn, ENOENT);
1061 return 0;
1062 }
1063
1064 NECP_FD_LOCK(fd_data);
1065 kn->kn_filtid = EVFILTID_NECP_FD;
1066 kn->kn_hook = fd_data;
1067 KNOTE_ATTACH(&fd_data->si.si_note, kn);
1068
1069 revents = necp_fd_poll(fd_data, POLLIN, NULL, current_proc(), 1);
1070
1071 NECP_FD_UNLOCK(fd_data);
1072
1073 return (revents & POLLIN) != 0;
1074 }
1075
1076 #define INTERFACE_FLAGS_SHIFT 32
1077 #define INTERFACE_FLAGS_MASK 0xffff
1078 #define INTERFACE_INDEX_SHIFT 0
1079 #define INTERFACE_INDEX_MASK 0xffffffff
1080
1081 static uint64_t
combine_interface_details(uint32_t interface_index,uint16_t interface_flags)1082 combine_interface_details(uint32_t interface_index, uint16_t interface_flags)
1083 {
1084 return ((uint64_t)interface_flags & INTERFACE_FLAGS_MASK) << INTERFACE_FLAGS_SHIFT |
1085 ((uint64_t)interface_index & INTERFACE_INDEX_MASK) << INTERFACE_INDEX_SHIFT;
1086 }
1087
1088 #if SKYWALK
1089
1090 static void
split_interface_details(uint64_t combined_details,uint32_t * interface_index,uint16_t * interface_flags)1091 split_interface_details(uint64_t combined_details, uint32_t *interface_index, uint16_t *interface_flags)
1092 {
1093 *interface_index = (combined_details >> INTERFACE_INDEX_SHIFT) & INTERFACE_INDEX_MASK;
1094 *interface_flags = (combined_details >> INTERFACE_FLAGS_SHIFT) & INTERFACE_FLAGS_MASK;
1095 }
1096
1097 static void
necp_flow_save_current_interface_details(struct necp_client_flow_registration * flow_registration)1098 necp_flow_save_current_interface_details(struct necp_client_flow_registration *flow_registration)
1099 {
1100 struct necp_client_flow *flow = NULL;
1101 LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
1102 if (flow->nexus) {
1103 uint64_t combined_details = combine_interface_details(flow->interface_index, flow->interface_flags);
1104 atomic_set_64(&flow_registration->last_interface_details, combined_details);
1105 break;
1106 }
1107 }
1108 }
1109
1110 static void
necp_client_collect_interface_stats(struct necp_client_flow_registration * flow_registration,struct ifnet_stats_per_flow * ifs)1111 necp_client_collect_interface_stats(struct necp_client_flow_registration *flow_registration, struct ifnet_stats_per_flow *ifs)
1112 {
1113 struct necp_client_flow *flow = NULL;
1114
1115 if (ifs == NULL || ifs->txpackets == 0 || ifs->rxpackets == 0) {
1116 return; // App might have crashed without publishing ifs
1117 }
1118
1119 // Do malicious stats detection here
1120
1121 // Fold userspace stats into (trusted) kernel stats (stored in ifp).
1122 LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
1123 uint32_t if_idx = flow->interface_index;
1124 ifnet_t ifp = NULL;
1125 ifnet_head_lock_shared();
1126 if (if_idx != IFSCOPE_NONE && if_idx <= (uint32_t)if_index) {
1127 ifp = ifindex2ifnet[if_idx];
1128 ifnet_update_stats_per_flow(ifs, ifp);
1129 }
1130 ifnet_head_done();
1131
1132 // Currently there is only one flow that uses the shared necp
1133 // stats region, so this loop should exit after updating an ifp
1134 break;
1135 }
1136 }
1137
1138 static void
necp_client_collect_stats(struct necp_client_flow_registration * flow_registration)1139 necp_client_collect_stats(struct necp_client_flow_registration *flow_registration)
1140 {
1141 struct necp_all_kstats *kstats = (struct necp_all_kstats *)flow_registration->kstats_kaddr;
1142 if (kstats == NULL) {
1143 return;
1144 }
1145
1146 // Grab userspace stats delta (untrusted).
1147 struct necp_tcp_stats *curr_tcpstats = (struct necp_tcp_stats *)kstats->necp_stats_ustats;
1148 struct necp_tcp_stats *prev_tcpstats = (struct necp_tcp_stats *)&kstats->necp_stats_comm;
1149 #define diff_n_update(field) \
1150 u_int32_t d_##field = (curr_tcpstats->necp_tcp_counts.necp_stat_##field - prev_tcpstats->necp_tcp_counts.necp_stat_##field); \
1151 prev_tcpstats->necp_tcp_counts.necp_stat_##field += d_##field;
1152 diff_n_update(rxpackets);
1153 diff_n_update(txpackets);
1154 if (d_rxpackets == 0 && d_txpackets == 0) {
1155 return; // no activity since last collection, stop here
1156 }
1157 diff_n_update(rxbytes);
1158 diff_n_update(txbytes);
1159 diff_n_update(rxduplicatebytes);
1160 diff_n_update(rxoutoforderbytes);
1161 diff_n_update(txretransmit);
1162 diff_n_update(connectattempts);
1163 diff_n_update(connectsuccesses);
1164 uint32_t rtt = prev_tcpstats->necp_tcp_counts.necp_stat_avg_rtt = curr_tcpstats->necp_tcp_counts.necp_stat_avg_rtt;
1165 uint32_t rtt_var = prev_tcpstats->necp_tcp_counts.necp_stat_var_rtt = curr_tcpstats->necp_tcp_counts.necp_stat_var_rtt;
1166 #undef diff_n_update
1167
1168 // Do malicious stats detection with the deltas here.
1169 // RTT check (not necessarily attacks, might just be not measured since we report stats async periodically).
1170 if (rtt < necp_client_stats_rtt_floor || rtt > necp_client_stats_rtt_ceiling) {
1171 rtt = rtt_var = 0; // nstat_route_update to skip 0 rtt
1172 }
1173
1174 // Fold userspace stats into (trusted) kernel stats (stored in route).
1175 NECP_CLIENT_ROUTE_LOCK(flow_registration->client);
1176 struct rtentry *route = flow_registration->client->current_route;
1177 if (route != NULL) {
1178 nstat_route_update(route, d_connectattempts, d_connectsuccesses, d_rxpackets, d_rxbytes, d_rxduplicatebytes,
1179 d_rxoutoforderbytes, d_txpackets, d_txbytes, d_txretransmit, rtt, rtt_var);
1180 }
1181 NECP_CLIENT_ROUTE_UNLOCK(flow_registration->client);
1182 }
1183
1184 // This is called from various places; "closing" here implies the client being closed/removed if true, otherwise being
1185 // defunct. In the former, we expect the caller to not hold the lock; for the latter it must have acquired it.
1186 static void
necp_destroy_flow_stats(struct necp_fd_data * fd_data,struct necp_client_flow_registration * flow_registration,struct ifnet_stats_per_flow * flow_ifnet_stats,boolean_t closing)1187 necp_destroy_flow_stats(struct necp_fd_data *fd_data,
1188 struct necp_client_flow_registration *flow_registration,
1189 struct ifnet_stats_per_flow *flow_ifnet_stats,
1190 boolean_t closing)
1191 {
1192 NECP_FD_ASSERT_LOCKED(fd_data);
1193
1194 struct necp_client *client = flow_registration->client;
1195
1196 if (closing) {
1197 NECP_CLIENT_ASSERT_UNLOCKED(client);
1198 NECP_CLIENT_LOCK(client);
1199 } else {
1200 NECP_CLIENT_ASSERT_LOCKED(client);
1201 }
1202
1203 // the interface stats are independent of the flow stats, hence we check here
1204 if (flow_ifnet_stats != NULL) {
1205 necp_client_collect_interface_stats(flow_registration, flow_ifnet_stats);
1206 }
1207
1208 if (flow_registration->kstats_kaddr != NULL) {
1209 NECP_STATS_LIST_LOCK_EXCLUSIVE();
1210 necp_client_collect_stats(flow_registration);
1211 const bool destroyed = necp_client_release_locked(client); // Drop the reference held by the stats list
1212 ASSERT(!destroyed);
1213 (void)destroyed;
1214 LIST_REMOVE(flow_registration, collect_stats_chain);
1215 NECP_STATS_LIST_UNLOCK();
1216 if (flow_registration->stats_handler_context != NULL) {
1217 ntstat_userland_stats_close(flow_registration->stats_handler_context);
1218 flow_registration->stats_handler_context = NULL;
1219 }
1220 necp_arena_stats_obj_free(fd_data, flow_registration->stats_arena, &flow_registration->kstats_kaddr, &flow_registration->ustats_uaddr);
1221 ASSERT(flow_registration->kstats_kaddr == NULL);
1222 ASSERT(flow_registration->ustats_uaddr == 0);
1223 }
1224
1225 if (flow_registration->nexus_stats != NULL) {
1226 flow_stats_release(flow_registration->nexus_stats);
1227 flow_registration->nexus_stats = NULL;
1228 }
1229
1230 if (closing) {
1231 NECP_CLIENT_UNLOCK(client);
1232 }
1233 }
1234
1235 static void
necp_schedule_collect_stats_clients(bool recur)1236 necp_schedule_collect_stats_clients(bool recur)
1237 {
1238 if (necp_client_collect_stats_tcall == NULL ||
1239 (!recur && thread_call_isactive(necp_client_collect_stats_tcall))) {
1240 return;
1241 }
1242
1243 uint64_t deadline = 0;
1244 uint64_t leeway = 0;
1245 clock_interval_to_deadline(necp_collect_stats_timeout_microseconds, NSEC_PER_USEC, &deadline);
1246 clock_interval_to_absolutetime_interval(necp_collect_stats_timeout_leeway_microseconds, NSEC_PER_USEC, &leeway);
1247
1248 thread_call_enter_delayed_with_leeway(necp_client_collect_stats_tcall, NULL,
1249 deadline, leeway, THREAD_CALL_DELAY_LEEWAY);
1250 }
1251
1252 static void
necp_collect_stats_client_callout(__unused thread_call_param_t dummy,__unused thread_call_param_t arg)1253 necp_collect_stats_client_callout(__unused thread_call_param_t dummy,
1254 __unused thread_call_param_t arg)
1255 {
1256 struct necp_client_flow_registration *flow_registration;
1257
1258 net_update_uptime();
1259 NECP_STATS_LIST_LOCK_SHARED();
1260 if (LIST_EMPTY(&necp_collect_stats_flow_list)) {
1261 NECP_STATS_LIST_UNLOCK();
1262 return;
1263 }
1264 LIST_FOREACH(flow_registration, &necp_collect_stats_flow_list, collect_stats_chain) {
1265 // Collecting stats should be cheap (atomic increments)
1266 // Values like flow_registration->kstats_kaddr are guaranteed to be valid
1267 // as long as the flow_registration is in the stats list
1268 necp_client_collect_stats(flow_registration);
1269 }
1270 NECP_STATS_LIST_UNLOCK();
1271
1272 necp_schedule_collect_stats_clients(TRUE); // recurring collection
1273 }
1274
1275 #endif /* !SKYWALK */
1276
1277 static void
necp_defunct_flow_registration(struct necp_client * client,struct necp_client_flow_registration * flow_registration,struct _necp_flow_defunct_list * defunct_list)1278 necp_defunct_flow_registration(struct necp_client *client,
1279 struct necp_client_flow_registration *flow_registration,
1280 struct _necp_flow_defunct_list *defunct_list)
1281 {
1282 NECP_CLIENT_ASSERT_LOCKED(client);
1283
1284 if (!flow_registration->defunct) {
1285 bool needs_defunct = false;
1286 struct necp_client_flow *search_flow = NULL;
1287 LIST_FOREACH(search_flow, &flow_registration->flow_list, flow_chain) {
1288 if (search_flow->nexus &&
1289 !uuid_is_null(search_flow->u.nexus_agent)) {
1290 // Save defunct values for the nexus
1291 if (defunct_list != NULL) {
1292 // Sleeping alloc won't fail; copy only what's necessary
1293 struct necp_flow_defunct *flow_defunct = kalloc_type(struct necp_flow_defunct,
1294 Z_WAITOK | Z_ZERO);
1295 uuid_copy(flow_defunct->nexus_agent, search_flow->u.nexus_agent);
1296 uuid_copy(flow_defunct->flow_id, ((flow_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
1297 client->client_id :
1298 flow_registration->registration_id));
1299 flow_defunct->proc_pid = client->proc_pid;
1300 flow_defunct->agent_handle = client->agent_handle;
1301 flow_defunct->flags = flow_registration->flags;
1302 #if SKYWALK
1303 if (flow_registration->kstats_kaddr != NULL) {
1304 struct necp_all_stats *ustats_kaddr = ((struct necp_all_kstats *)flow_registration->kstats_kaddr)->necp_stats_ustats;
1305 struct necp_quic_stats *quicstats = (struct necp_quic_stats *)ustats_kaddr;
1306 if (quicstats != NULL) {
1307 memcpy(flow_defunct->close_parameters.u.close_token, quicstats->necp_quic_extra.ssr_token, sizeof(flow_defunct->close_parameters.u.close_token));
1308 flow_defunct->has_close_parameters = true;
1309 }
1310 }
1311 #endif /* SKYWALK */
1312 // Add to the list provided by caller
1313 LIST_INSERT_HEAD(defunct_list, flow_defunct, chain);
1314 }
1315
1316 needs_defunct = true;
1317 }
1318 }
1319
1320 if (needs_defunct) {
1321 #if SKYWALK
1322 // Close the stats early
1323 if (flow_registration->stats_handler_context != NULL) {
1324 ntstat_userland_stats_event(flow_registration->stats_handler_context,
1325 NECP_CLIENT_STATISTICS_EVENT_TIME_WAIT);
1326 }
1327 #endif /* SKYWALK */
1328
1329 // Only set defunct if there was some assigned flow
1330 flow_registration->defunct = true;
1331 }
1332 }
1333 }
1334
1335 static void
necp_defunct_client_for_policy(struct necp_client * client,struct _necp_flow_defunct_list * defunct_list)1336 necp_defunct_client_for_policy(struct necp_client *client,
1337 struct _necp_flow_defunct_list *defunct_list)
1338 {
1339 NECP_CLIENT_ASSERT_LOCKED(client);
1340
1341 struct necp_client_flow_registration *flow_registration = NULL;
1342 RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
1343 necp_defunct_flow_registration(client, flow_registration, defunct_list);
1344 }
1345 }
1346
1347 static void
necp_client_free(struct necp_client * client)1348 necp_client_free(struct necp_client *client)
1349 {
1350 NECP_CLIENT_ASSERT_UNLOCKED(client);
1351
1352 kfree_data(client->extra_interface_options,
1353 sizeof(struct necp_client_interface_option) * NECP_CLIENT_INTERFACE_OPTION_EXTRA_COUNT);
1354 client->extra_interface_options = NULL;
1355
1356 kfree_data(client->parameters, client->parameters_length);
1357 client->parameters = NULL;
1358
1359 lck_mtx_destroy(&client->route_lock, &necp_fd_mtx_grp);
1360 lck_mtx_destroy(&client->lock, &necp_fd_mtx_grp);
1361
1362 kfree_type(struct necp_client, client);
1363 }
1364
1365 static void
necp_client_retain_locked(struct necp_client * client)1366 necp_client_retain_locked(struct necp_client *client)
1367 {
1368 NECP_CLIENT_ASSERT_LOCKED(client);
1369
1370 os_ref_retain_locked(&client->reference_count);
1371 }
1372
1373 static void
necp_client_retain(struct necp_client * client)1374 necp_client_retain(struct necp_client *client)
1375 {
1376 NECP_CLIENT_LOCK(client);
1377 necp_client_retain_locked(client);
1378 NECP_CLIENT_UNLOCK(client);
1379 }
1380
1381 static bool
necp_client_release_locked(struct necp_client * client)1382 necp_client_release_locked(struct necp_client *client)
1383 {
1384 NECP_CLIENT_ASSERT_LOCKED(client);
1385
1386 os_ref_count_t count = os_ref_release_locked(&client->reference_count);
1387 if (count == 0) {
1388 NECP_CLIENT_UNLOCK(client);
1389 necp_client_free(client);
1390 }
1391
1392 return count == 0;
1393 }
1394
1395 static bool
necp_client_release(struct necp_client * client)1396 necp_client_release(struct necp_client *client)
1397 {
1398 bool last_ref;
1399
1400 NECP_CLIENT_LOCK(client);
1401 if (!(last_ref = necp_client_release_locked(client))) {
1402 NECP_CLIENT_UNLOCK(client);
1403 }
1404
1405 return last_ref;
1406 }
1407
1408 static struct necp_client_update *
necp_client_update_alloc(const void * data,size_t length)1409 necp_client_update_alloc(const void *data, size_t length)
1410 {
1411 struct necp_client_update *client_update;
1412 struct necp_client_observer_update *buffer;
1413 size_t alloc_size;
1414
1415 if (os_add_overflow(length, sizeof(*buffer), &alloc_size)) {
1416 return NULL;
1417 }
1418 buffer = kalloc_data(alloc_size, Z_WAITOK);
1419 if (buffer == NULL) {
1420 return NULL;
1421 }
1422
1423 client_update = kalloc_type(struct necp_client_update,
1424 Z_WAITOK | Z_ZERO | Z_NOFAIL);
1425 client_update->update_length = alloc_size;
1426 client_update->update = buffer;
1427 memcpy(buffer->tlv_buffer, data, length);
1428 return client_update;
1429 }
1430
1431 static void
necp_client_update_free(struct necp_client_update * client_update)1432 necp_client_update_free(struct necp_client_update *client_update)
1433 {
1434 kfree_data(client_update->update, client_update->update_length);
1435 kfree_type(struct necp_client_update, client_update);
1436 }
1437
1438 static void
necp_client_update_observer_add_internal(struct necp_fd_data * observer_fd,struct necp_client * client)1439 necp_client_update_observer_add_internal(struct necp_fd_data *observer_fd, struct necp_client *client)
1440 {
1441 struct necp_client_update *client_update;
1442
1443 NECP_FD_LOCK(observer_fd);
1444
1445 if (observer_fd->update_count >= necp_observer_message_limit) {
1446 NECP_FD_UNLOCK(observer_fd);
1447 return;
1448 }
1449
1450 client_update = necp_client_update_alloc(client->parameters, client->parameters_length);
1451 if (client_update != NULL) {
1452 uuid_copy(client_update->client_id, client->client_id);
1453 client_update->update->update_type = NECP_CLIENT_UPDATE_TYPE_PARAMETERS;
1454 TAILQ_INSERT_TAIL(&observer_fd->update_list, client_update, chain);
1455 observer_fd->update_count++;
1456
1457 necp_fd_notify(observer_fd, true);
1458 }
1459
1460 NECP_FD_UNLOCK(observer_fd);
1461 }
1462
1463 static void
necp_client_update_observer_update_internal(struct necp_fd_data * observer_fd,struct necp_client * client)1464 necp_client_update_observer_update_internal(struct necp_fd_data *observer_fd, struct necp_client *client)
1465 {
1466 NECP_FD_LOCK(observer_fd);
1467
1468 if (observer_fd->update_count >= necp_observer_message_limit) {
1469 NECP_FD_UNLOCK(observer_fd);
1470 return;
1471 }
1472
1473 struct necp_client_update *client_update = necp_client_update_alloc(client->result, client->result_length);
1474 if (client_update != NULL) {
1475 uuid_copy(client_update->client_id, client->client_id);
1476 client_update->update->update_type = NECP_CLIENT_UPDATE_TYPE_RESULT;
1477 TAILQ_INSERT_TAIL(&observer_fd->update_list, client_update, chain);
1478 observer_fd->update_count++;
1479
1480 necp_fd_notify(observer_fd, true);
1481 }
1482
1483 NECP_FD_UNLOCK(observer_fd);
1484 }
1485
1486 static void
necp_client_update_observer_remove_internal(struct necp_fd_data * observer_fd,struct necp_client * client)1487 necp_client_update_observer_remove_internal(struct necp_fd_data *observer_fd, struct necp_client *client)
1488 {
1489 NECP_FD_LOCK(observer_fd);
1490
1491 if (observer_fd->update_count >= necp_observer_message_limit) {
1492 NECP_FD_UNLOCK(observer_fd);
1493 return;
1494 }
1495
1496 struct necp_client_update *client_update = necp_client_update_alloc(NULL, 0);
1497 if (client_update != NULL) {
1498 uuid_copy(client_update->client_id, client->client_id);
1499 client_update->update->update_type = NECP_CLIENT_UPDATE_TYPE_REMOVE;
1500 TAILQ_INSERT_TAIL(&observer_fd->update_list, client_update, chain);
1501 observer_fd->update_count++;
1502
1503 necp_fd_notify(observer_fd, true);
1504 }
1505
1506 NECP_FD_UNLOCK(observer_fd);
1507 }
1508
1509 static void
necp_client_update_observer_add(struct necp_client * client)1510 necp_client_update_observer_add(struct necp_client *client)
1511 {
1512 NECP_OBSERVER_LIST_LOCK_SHARED();
1513
1514 if (LIST_EMPTY(&necp_fd_observer_list)) {
1515 // No observers, bail
1516 NECP_OBSERVER_LIST_UNLOCK();
1517 return;
1518 }
1519
1520 struct necp_fd_data *observer_fd = NULL;
1521 LIST_FOREACH(observer_fd, &necp_fd_observer_list, chain) {
1522 necp_client_update_observer_add_internal(observer_fd, client);
1523 }
1524
1525 NECP_OBSERVER_LIST_UNLOCK();
1526 }
1527
1528 static void
necp_client_update_observer_update(struct necp_client * client)1529 necp_client_update_observer_update(struct necp_client *client)
1530 {
1531 NECP_OBSERVER_LIST_LOCK_SHARED();
1532
1533 if (LIST_EMPTY(&necp_fd_observer_list)) {
1534 // No observers, bail
1535 NECP_OBSERVER_LIST_UNLOCK();
1536 return;
1537 }
1538
1539 struct necp_fd_data *observer_fd = NULL;
1540 LIST_FOREACH(observer_fd, &necp_fd_observer_list, chain) {
1541 necp_client_update_observer_update_internal(observer_fd, client);
1542 }
1543
1544 NECP_OBSERVER_LIST_UNLOCK();
1545 }
1546
1547 static void
necp_client_update_observer_remove(struct necp_client * client)1548 necp_client_update_observer_remove(struct necp_client *client)
1549 {
1550 NECP_OBSERVER_LIST_LOCK_SHARED();
1551
1552 if (LIST_EMPTY(&necp_fd_observer_list)) {
1553 // No observers, bail
1554 NECP_OBSERVER_LIST_UNLOCK();
1555 return;
1556 }
1557
1558 struct necp_fd_data *observer_fd = NULL;
1559 LIST_FOREACH(observer_fd, &necp_fd_observer_list, chain) {
1560 necp_client_update_observer_remove_internal(observer_fd, client);
1561 }
1562
1563 NECP_OBSERVER_LIST_UNLOCK();
1564 }
1565
1566 static void
necp_destroy_client_flow_registration(struct necp_client * client,struct necp_client_flow_registration * flow_registration,pid_t pid,bool abort)1567 necp_destroy_client_flow_registration(struct necp_client *client,
1568 struct necp_client_flow_registration *flow_registration,
1569 pid_t pid, bool abort)
1570 {
1571 NECP_CLIENT_ASSERT_LOCKED(client);
1572
1573 bool has_close_parameters = false;
1574 struct necp_client_agent_parameters close_parameters = {};
1575 memset(close_parameters.u.close_token, 0, sizeof(close_parameters.u.close_token));
1576 #if SKYWALK
1577 if (flow_registration->kstats_kaddr != NULL) {
1578 struct necp_all_stats *ustats_kaddr = ((struct necp_all_kstats *)flow_registration->kstats_kaddr)->necp_stats_ustats;
1579 struct necp_quic_stats *quicstats = (struct necp_quic_stats *)ustats_kaddr;
1580 if (quicstats != NULL &&
1581 quicstats->necp_quic_udp_stats.necp_udp_hdr.necp_stats_type == NECP_CLIENT_STATISTICS_TYPE_QUIC) {
1582 memcpy(close_parameters.u.close_token, quicstats->necp_quic_extra.ssr_token, sizeof(close_parameters.u.close_token));
1583 has_close_parameters = true;
1584 }
1585 }
1586
1587 // Release reference held on the stats arena
1588 if (flow_registration->stats_arena != NULL) {
1589 necp_arena_info_release(flow_registration->stats_arena);
1590 flow_registration->stats_arena = NULL;
1591 }
1592 #endif /* SKYWALK */
1593
1594 struct necp_client_flow *search_flow = NULL;
1595 struct necp_client_flow *temp_flow = NULL;
1596 LIST_FOREACH_SAFE(search_flow, &flow_registration->flow_list, flow_chain, temp_flow) {
1597 if (search_flow->nexus &&
1598 !uuid_is_null(search_flow->u.nexus_agent)) {
1599 // Don't unregister for defunct flows
1600 if (!flow_registration->defunct) {
1601 u_int8_t message_type = (abort ? NETAGENT_MESSAGE_TYPE_ABORT_NEXUS :
1602 NETAGENT_MESSAGE_TYPE_CLOSE_NEXUS);
1603 if (((flow_registration->flags & NECP_CLIENT_FLOW_FLAGS_BROWSE) ||
1604 (flow_registration->flags & NECP_CLIENT_FLOW_FLAGS_RESOLVE)) &&
1605 !(flow_registration->flags & NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS)) {
1606 message_type = NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT;
1607 }
1608 int netagent_error = netagent_client_message_with_params(search_flow->u.nexus_agent,
1609 ((flow_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
1610 client->client_id :
1611 flow_registration->registration_id),
1612 pid, client->agent_handle,
1613 message_type,
1614 has_close_parameters ? &close_parameters : NULL,
1615 NULL, 0);
1616 if (netagent_error != 0 && netagent_error != ENOENT) {
1617 NECPLOG(LOG_ERR, "necp_client_remove close nexus error (%d) MESSAGE TYPE %u", netagent_error, message_type);
1618 }
1619 }
1620 uuid_clear(search_flow->u.nexus_agent);
1621 }
1622 if (search_flow->assigned_results != NULL) {
1623 kfree_data(search_flow->assigned_results, search_flow->assigned_results_length);
1624 search_flow->assigned_results = NULL;
1625 }
1626 LIST_REMOVE(search_flow, flow_chain);
1627 #if SKYWALK
1628 if (search_flow->nexus) {
1629 OSDecrementAtomic(&necp_nexus_flow_count);
1630 } else
1631 #endif /* SKYWALK */
1632 if (search_flow->socket) {
1633 OSDecrementAtomic(&necp_socket_flow_count);
1634 } else {
1635 OSDecrementAtomic(&necp_if_flow_count);
1636 }
1637 kfree_type(struct necp_client_flow, search_flow);
1638 }
1639
1640 RB_REMOVE(_necp_client_flow_tree, &client->flow_registrations, flow_registration);
1641 flow_registration->client = NULL;
1642
1643 kfree_type(struct necp_client_flow_registration, flow_registration);
1644 }
1645
1646 static void
necp_destroy_client(struct necp_client * client,pid_t pid,bool abort)1647 necp_destroy_client(struct necp_client *client, pid_t pid, bool abort)
1648 {
1649 NECP_CLIENT_ASSERT_UNLOCKED(client);
1650
1651 #if SKYWALK
1652 if (client->nstat_context != NULL) {
1653 // This is a catch-all that should be rarely used.
1654 nstat_provider_stats_close(client->nstat_context);
1655 client->nstat_context = NULL;
1656 }
1657 if (client->original_parameters_source != NULL) {
1658 necp_client_release(client->original_parameters_source);
1659 client->original_parameters_source = NULL;
1660 }
1661 #endif /* SKYWALK */
1662 necp_client_update_observer_remove(client);
1663
1664 NECP_CLIENT_LOCK(client);
1665
1666 // Free route
1667 NECP_CLIENT_ROUTE_LOCK(client);
1668 if (client->current_route != NULL) {
1669 rtfree(client->current_route);
1670 client->current_route = NULL;
1671 }
1672 NECP_CLIENT_ROUTE_UNLOCK(client);
1673
1674 // Remove flow assignments
1675 struct necp_client_flow_registration *flow_registration = NULL;
1676 struct necp_client_flow_registration *temp_flow_registration = NULL;
1677 RB_FOREACH_SAFE(flow_registration, _necp_client_flow_tree, &client->flow_registrations, temp_flow_registration) {
1678 necp_destroy_client_flow_registration(client, flow_registration, pid, abort);
1679 }
1680
1681 #if SKYWALK
1682 // Remove port reservation
1683 if (NETNS_TOKEN_VALID(&client->port_reservation)) {
1684 netns_release(&client->port_reservation);
1685 }
1686 #endif /* !SKYWALK */
1687
1688 // Remove agent assertions
1689 struct necp_client_assertion *search_assertion = NULL;
1690 struct necp_client_assertion *temp_assertion = NULL;
1691 LIST_FOREACH_SAFE(search_assertion, &client->assertion_list, assertion_chain, temp_assertion) {
1692 int netagent_error = netagent_client_message(search_assertion->asserted_netagent, client->client_id, pid,
1693 client->agent_handle, NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT);
1694 if (netagent_error != 0) {
1695 NECPLOG((netagent_error == ENOENT ? LOG_DEBUG : LOG_ERR),
1696 "necp_client_remove unassert agent error (%d)", netagent_error);
1697 }
1698 LIST_REMOVE(search_assertion, assertion_chain);
1699 kfree_type(struct necp_client_assertion, search_assertion);
1700 }
1701
1702 if (!necp_client_release_locked(client)) {
1703 NECP_CLIENT_UNLOCK(client);
1704 }
1705
1706 OSDecrementAtomic(&necp_client_count);
1707 }
1708
1709 static bool
1710 necp_defunct_client_fd_locked_inner(struct necp_fd_data *client_fd, struct _necp_flow_defunct_list *defunct_list, bool destroy_stats);
1711
1712 static void
necp_process_defunct_list(struct _necp_flow_defunct_list * defunct_list)1713 necp_process_defunct_list(struct _necp_flow_defunct_list *defunct_list)
1714 {
1715 if (!LIST_EMPTY(defunct_list)) {
1716 struct necp_flow_defunct *flow_defunct = NULL;
1717 struct necp_flow_defunct *temp_flow_defunct = NULL;
1718
1719 // For each newly defunct client, send a message to the nexus to remove the flow
1720 LIST_FOREACH_SAFE(flow_defunct, defunct_list, chain, temp_flow_defunct) {
1721 if (!uuid_is_null(flow_defunct->nexus_agent)) {
1722 u_int8_t message_type = NETAGENT_MESSAGE_TYPE_ABORT_NEXUS;
1723 if (((flow_defunct->flags & NECP_CLIENT_FLOW_FLAGS_BROWSE) ||
1724 (flow_defunct->flags & NECP_CLIENT_FLOW_FLAGS_RESOLVE)) &&
1725 !(flow_defunct->flags & NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS)) {
1726 message_type = NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT;
1727 }
1728 int netagent_error = netagent_client_message_with_params(flow_defunct->nexus_agent,
1729 flow_defunct->flow_id,
1730 flow_defunct->proc_pid,
1731 flow_defunct->agent_handle,
1732 message_type,
1733 flow_defunct->has_close_parameters ? &flow_defunct->close_parameters : NULL,
1734 NULL, 0);
1735 if (netagent_error != 0) {
1736 char namebuf[MAXCOMLEN + 1];
1737 (void) strlcpy(namebuf, "unknown", sizeof(namebuf));
1738 proc_name(flow_defunct->proc_pid, namebuf, sizeof(namebuf));
1739 NECPLOG((netagent_error == ENOENT ? LOG_DEBUG : LOG_ERR), "necp_update_client abort nexus error (%d) for pid %d %s", netagent_error, flow_defunct->proc_pid, namebuf);
1740 }
1741 }
1742 LIST_REMOVE(flow_defunct, chain);
1743 kfree_type(struct necp_flow_defunct, flow_defunct);
1744 }
1745 }
1746 ASSERT(LIST_EMPTY(defunct_list));
1747 }
1748
1749 static int
necpop_close(struct fileglob * fg,vfs_context_t ctx)1750 necpop_close(struct fileglob *fg, vfs_context_t ctx)
1751 {
1752 #pragma unused(ctx)
1753 struct necp_fd_data *fd_data = NULL;
1754 int error = 0;
1755
1756 fd_data = (struct necp_fd_data *)fg_get_data(fg);
1757 fg_set_data(fg, NULL);
1758
1759 if (fd_data != NULL) {
1760 struct _necp_client_tree clients_to_close;
1761 RB_INIT(&clients_to_close);
1762
1763 // Remove from list quickly
1764 if (fd_data->flags & NECP_OPEN_FLAG_PUSH_OBSERVER) {
1765 NECP_OBSERVER_LIST_LOCK_EXCLUSIVE();
1766 LIST_REMOVE(fd_data, chain);
1767 NECP_OBSERVER_LIST_UNLOCK();
1768 } else {
1769 NECP_FD_LIST_LOCK_EXCLUSIVE();
1770 LIST_REMOVE(fd_data, chain);
1771 NECP_FD_LIST_UNLOCK();
1772 }
1773
1774 NECP_FD_LOCK(fd_data);
1775 pid_t pid = fd_data->proc_pid;
1776
1777 struct _necp_flow_defunct_list defunct_list;
1778 LIST_INIT(&defunct_list);
1779
1780 (void)necp_defunct_client_fd_locked_inner(fd_data, &defunct_list, false);
1781
1782 struct necp_client_flow_registration *flow_registration = NULL;
1783 struct necp_client_flow_registration *temp_flow_registration = NULL;
1784 RB_FOREACH_SAFE(flow_registration, _necp_fd_flow_tree, &fd_data->flows, temp_flow_registration) {
1785 #if SKYWALK
1786 necp_destroy_flow_stats(fd_data, flow_registration, NULL, TRUE);
1787 #endif /* SKYWALK */
1788 NECP_FLOW_TREE_LOCK_EXCLUSIVE();
1789 RB_REMOVE(_necp_client_flow_global_tree, &necp_client_flow_global_tree, flow_registration);
1790 NECP_FLOW_TREE_UNLOCK();
1791 RB_REMOVE(_necp_fd_flow_tree, &fd_data->flows, flow_registration);
1792 }
1793
1794 struct necp_client *client = NULL;
1795 struct necp_client *temp_client = NULL;
1796 RB_FOREACH_SAFE(client, _necp_client_tree, &fd_data->clients, temp_client) {
1797 NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
1798 RB_REMOVE(_necp_client_global_tree, &necp_client_global_tree, client);
1799 NECP_CLIENT_TREE_UNLOCK();
1800 RB_REMOVE(_necp_client_tree, &fd_data->clients, client);
1801 RB_INSERT(_necp_client_tree, &clients_to_close, client);
1802 }
1803
1804 struct necp_client_update *client_update = NULL;
1805 struct necp_client_update *temp_update = NULL;
1806 TAILQ_FOREACH_SAFE(client_update, &fd_data->update_list, chain, temp_update) {
1807 // Flush pending updates
1808 TAILQ_REMOVE(&fd_data->update_list, client_update, chain);
1809 necp_client_update_free(client_update);
1810 }
1811 fd_data->update_count = 0;
1812
1813 #if SKYWALK
1814 // Cleanup stats arena(s); indicate that we're closing
1815 necp_stats_arenas_destroy(fd_data, TRUE);
1816 ASSERT(fd_data->stats_arena_active == NULL);
1817 ASSERT(LIST_EMPTY(&fd_data->stats_arena_list));
1818
1819 // Cleanup systctl arena
1820 necp_sysctl_arena_destroy(fd_data);
1821 ASSERT(fd_data->sysctl_arena == NULL);
1822 #endif /* SKYWALK */
1823
1824 NECP_FD_UNLOCK(fd_data);
1825
1826 selthreadclear(&fd_data->si);
1827
1828 lck_mtx_destroy(&fd_data->fd_lock, &necp_fd_mtx_grp);
1829
1830 if (fd_data->flags & NECP_OPEN_FLAG_PUSH_OBSERVER) {
1831 OSDecrementAtomic(&necp_observer_fd_count);
1832 } else {
1833 OSDecrementAtomic(&necp_client_fd_count);
1834 }
1835
1836 kfree_type(struct necp_fd_data, fd_data);
1837
1838 RB_FOREACH_SAFE(client, _necp_client_tree, &clients_to_close, temp_client) {
1839 RB_REMOVE(_necp_client_tree, &clients_to_close, client);
1840 necp_destroy_client(client, pid, true);
1841 }
1842
1843 necp_process_defunct_list(&defunct_list);
1844 }
1845
1846 return error;
1847 }
1848
1849 /// NECP client utilities
1850
1851 static inline bool
necp_address_is_wildcard(const union necp_sockaddr_union * const addr)1852 necp_address_is_wildcard(const union necp_sockaddr_union * const addr)
1853 {
1854 return (addr->sa.sa_family == AF_INET && addr->sin.sin_addr.s_addr == INADDR_ANY) ||
1855 (addr->sa.sa_family == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(&addr->sin6.sin6_addr));
1856 }
1857
1858 static int
necp_find_fd_data(struct proc * p,int fd,struct fileproc ** fpp,struct necp_fd_data ** fd_data)1859 necp_find_fd_data(struct proc *p, int fd,
1860 struct fileproc **fpp, struct necp_fd_data **fd_data)
1861 {
1862 struct fileproc *fp;
1863 int error = fp_get_ftype(p, fd, DTYPE_NETPOLICY, ENODEV, &fp);
1864
1865 if (error == 0) {
1866 *fd_data = (struct necp_fd_data *)fp_get_data(fp);
1867 *fpp = fp;
1868
1869 if ((*fd_data)->necp_fd_type != necp_fd_type_client) {
1870 // Not a client fd, ignore
1871 fp_drop(p, fd, fp, 0);
1872 error = EINVAL;
1873 }
1874 }
1875 return error;
1876 }
1877
1878 static void
necp_client_add_nexus_flow(struct necp_client_flow_registration * flow_registration,uuid_t nexus_agent,uint32_t interface_index,uint16_t interface_flags)1879 necp_client_add_nexus_flow(struct necp_client_flow_registration *flow_registration,
1880 uuid_t nexus_agent,
1881 uint32_t interface_index,
1882 uint16_t interface_flags)
1883 {
1884 struct necp_client_flow *new_flow = kalloc_type(struct necp_client_flow, Z_WAITOK | Z_ZERO | Z_NOFAIL);
1885
1886 new_flow->nexus = TRUE;
1887 uuid_copy(new_flow->u.nexus_agent, nexus_agent);
1888 new_flow->interface_index = interface_index;
1889 new_flow->interface_flags = interface_flags;
1890 new_flow->check_tcp_heuristics = TRUE;
1891
1892 #if SKYWALK
1893 OSIncrementAtomic(&necp_nexus_flow_count);
1894 #endif /* SKYWALK */
1895
1896 LIST_INSERT_HEAD(&flow_registration->flow_list, new_flow, flow_chain);
1897
1898 #if SKYWALK
1899 necp_flow_save_current_interface_details(flow_registration);
1900 #endif /* SKYWALK */
1901 }
1902
1903 static void
necp_client_add_nexus_flow_if_needed(struct necp_client_flow_registration * flow_registration,uuid_t nexus_agent,uint32_t interface_index)1904 necp_client_add_nexus_flow_if_needed(struct necp_client_flow_registration *flow_registration,
1905 uuid_t nexus_agent,
1906 uint32_t interface_index)
1907 {
1908 struct necp_client_flow *flow = NULL;
1909 LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
1910 if (flow->nexus &&
1911 uuid_compare(flow->u.nexus_agent, nexus_agent) == 0) {
1912 return;
1913 }
1914 }
1915
1916 uint16_t interface_flags = 0;
1917 ifnet_t ifp = NULL;
1918 ifnet_head_lock_shared();
1919 if (interface_index != IFSCOPE_NONE && interface_index <= (u_int32_t)if_index) {
1920 ifp = ifindex2ifnet[interface_index];
1921 if (ifp != NULL) {
1922 ifnet_lock_shared(ifp);
1923 interface_flags = nstat_ifnet_to_flags(ifp);
1924 ifnet_lock_done(ifp);
1925 }
1926 }
1927 ifnet_head_done();
1928 necp_client_add_nexus_flow(flow_registration, nexus_agent, interface_index, interface_flags);
1929 }
1930
1931 static struct necp_client_flow *
necp_client_add_interface_flow(struct necp_client_flow_registration * flow_registration,uint32_t interface_index)1932 necp_client_add_interface_flow(struct necp_client_flow_registration *flow_registration,
1933 uint32_t interface_index)
1934 {
1935 struct necp_client_flow *new_flow = kalloc_type(struct necp_client_flow, Z_WAITOK | Z_ZERO | Z_NOFAIL);
1936
1937 // Neither nexus nor socket
1938 new_flow->interface_index = interface_index;
1939 new_flow->u.socket_handle = flow_registration->interface_handle;
1940 new_flow->u.cb = flow_registration->interface_cb;
1941
1942 OSIncrementAtomic(&necp_if_flow_count);
1943
1944 LIST_INSERT_HEAD(&flow_registration->flow_list, new_flow, flow_chain);
1945
1946 return new_flow;
1947 }
1948
1949 static struct necp_client_flow *
necp_client_add_interface_flow_if_needed(struct necp_client * client,struct necp_client_flow_registration * flow_registration,uint32_t interface_index)1950 necp_client_add_interface_flow_if_needed(struct necp_client *client,
1951 struct necp_client_flow_registration *flow_registration,
1952 uint32_t interface_index)
1953 {
1954 if (!client->allow_multiple_flows ||
1955 interface_index == IFSCOPE_NONE) {
1956 // Interface not set, or client not allowed to use this mode
1957 return NULL;
1958 }
1959
1960 struct necp_client_flow *flow = NULL;
1961 LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
1962 if (!flow->nexus && !flow->socket && flow->interface_index == interface_index) {
1963 // Already have the flow
1964 flow->invalid = FALSE;
1965 flow->u.socket_handle = flow_registration->interface_handle;
1966 flow->u.cb = flow_registration->interface_cb;
1967 return NULL;
1968 }
1969 }
1970 return necp_client_add_interface_flow(flow_registration, interface_index);
1971 }
1972
1973 static void
necp_client_add_interface_option_if_needed(struct necp_client * client,uint32_t interface_index,uint32_t interface_generation,uuid_t * nexus_agent,bool network_provider)1974 necp_client_add_interface_option_if_needed(struct necp_client *client,
1975 uint32_t interface_index,
1976 uint32_t interface_generation,
1977 uuid_t *nexus_agent,
1978 bool network_provider)
1979 {
1980 if ((interface_index == IFSCOPE_NONE && !network_provider) ||
1981 (client->interface_option_count != 0 && !client->allow_multiple_flows)) {
1982 // Interface not set, or client not allowed to use this mode
1983 return;
1984 }
1985
1986 if (client->interface_option_count >= NECP_CLIENT_MAX_INTERFACE_OPTIONS) {
1987 // Cannot take any more interface options
1988 return;
1989 }
1990
1991 // Check if already present
1992 for (u_int32_t option_i = 0; option_i < client->interface_option_count; option_i++) {
1993 if (option_i < NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT) {
1994 struct necp_client_interface_option *option = &client->interface_options[option_i];
1995 if (option->interface_index == interface_index) {
1996 if (nexus_agent == NULL) {
1997 return;
1998 }
1999 if (uuid_compare(option->nexus_agent, *nexus_agent) == 0) {
2000 return;
2001 }
2002 if (uuid_is_null(option->nexus_agent)) {
2003 uuid_copy(option->nexus_agent, *nexus_agent);
2004 return;
2005 }
2006 // If we get to this point, this is a new nexus flow
2007 }
2008 } else {
2009 struct necp_client_interface_option *option = &client->extra_interface_options[option_i - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT];
2010 if (option->interface_index == interface_index) {
2011 if (nexus_agent == NULL) {
2012 return;
2013 }
2014 if (uuid_compare(option->nexus_agent, *nexus_agent) == 0) {
2015 return;
2016 }
2017 if (uuid_is_null(option->nexus_agent)) {
2018 uuid_copy(option->nexus_agent, *nexus_agent);
2019 return;
2020 }
2021 // If we get to this point, this is a new nexus flow
2022 }
2023 }
2024 }
2025
2026 // Add a new entry
2027 if (client->interface_option_count < NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT) {
2028 // Add to static
2029 struct necp_client_interface_option *option = &client->interface_options[client->interface_option_count];
2030 option->interface_index = interface_index;
2031 option->interface_generation = interface_generation;
2032 if (nexus_agent != NULL) {
2033 uuid_copy(option->nexus_agent, *nexus_agent);
2034 } else {
2035 uuid_clear(option->nexus_agent);
2036 }
2037 client->interface_option_count++;
2038 } else {
2039 // Add to extra
2040 if (client->extra_interface_options == NULL) {
2041 client->extra_interface_options = (struct necp_client_interface_option *)kalloc_data(
2042 sizeof(struct necp_client_interface_option) * NECP_CLIENT_INTERFACE_OPTION_EXTRA_COUNT, Z_WAITOK | Z_ZERO);
2043 }
2044 if (client->extra_interface_options != NULL) {
2045 struct necp_client_interface_option *option = &client->extra_interface_options[client->interface_option_count - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT];
2046 option->interface_index = interface_index;
2047 option->interface_generation = interface_generation;
2048 if (nexus_agent != NULL) {
2049 uuid_copy(option->nexus_agent, *nexus_agent);
2050 } else {
2051 uuid_clear(option->nexus_agent);
2052 }
2053 client->interface_option_count++;
2054 }
2055 }
2056 }
2057
2058 static bool
necp_client_flow_is_viable(proc_t proc,struct necp_client * client,struct necp_client_flow * flow)2059 necp_client_flow_is_viable(proc_t proc, struct necp_client *client,
2060 struct necp_client_flow *flow)
2061 {
2062 struct necp_aggregate_result result;
2063 bool ignore_address = (client->allow_multiple_flows && !flow->nexus && !flow->socket);
2064
2065 flow->necp_flow_flags = 0;
2066 int error = necp_application_find_policy_match_internal(proc, client->parameters,
2067 (u_int32_t)client->parameters_length,
2068 &result, &flow->necp_flow_flags, NULL,
2069 flow->interface_index,
2070 &flow->local_addr, &flow->remote_addr, NULL, NULL,
2071 NULL, ignore_address, true, NULL);
2072
2073 // Check for blocking agents
2074 for (int i = 0; i < NECP_MAX_NETAGENTS; i++) {
2075 if (uuid_is_null(result.netagents[i])) {
2076 // Passed end of valid agents
2077 break;
2078 }
2079 if (result.netagent_use_flags[i] & NECP_AGENT_USE_FLAG_REMOVE) {
2080 // A removed agent, ignore
2081 continue;
2082 }
2083 u_int32_t flags = netagent_get_flags(result.netagents[i]);
2084 if ((flags & NETAGENT_FLAG_REGISTERED) &&
2085 !(flags & NETAGENT_FLAG_VOLUNTARY) &&
2086 !(flags & NETAGENT_FLAG_ACTIVE) &&
2087 !(flags & NETAGENT_FLAG_SPECIFIC_USE_ONLY)) {
2088 // A required agent is not active, cause the flow to be marked non-viable
2089 return false;
2090 }
2091 }
2092
2093 if (flow->interface_index != IFSCOPE_NONE) {
2094 ifnet_head_lock_shared();
2095
2096 struct ifnet *ifp = ifindex2ifnet[flow->interface_index];
2097 if (ifp && ifp->if_delegated.ifp != IFSCOPE_NONE) {
2098 flow->delegated_interface_index = ifp->if_delegated.ifp->if_index;
2099 }
2100
2101 ifnet_head_done();
2102 }
2103
2104 return error == 0 &&
2105 result.routed_interface_index != IFSCOPE_NONE &&
2106 result.routing_result != NECP_KERNEL_POLICY_RESULT_DROP;
2107 }
2108
2109 static void
necp_flow_add_interface_flows(proc_t proc,struct necp_client * client,struct necp_client_flow_registration * flow_registration,bool send_initial)2110 necp_flow_add_interface_flows(proc_t proc,
2111 struct necp_client *client,
2112 struct necp_client_flow_registration *flow_registration,
2113 bool send_initial)
2114 {
2115 // Traverse all interfaces and add a tracking flow if needed
2116 for (u_int32_t option_i = 0; option_i < client->interface_option_count; option_i++) {
2117 if (option_i < NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT) {
2118 struct necp_client_interface_option *option = &client->interface_options[option_i];
2119 struct necp_client_flow *flow = necp_client_add_interface_flow_if_needed(client, flow_registration, option->interface_index);
2120 if (flow != NULL && send_initial) {
2121 flow->viable = necp_client_flow_is_viable(proc, client, flow);
2122 if (flow->viable && flow->u.cb) {
2123 bool viable = flow->viable;
2124 flow->u.cb(flow_registration->interface_handle, NECP_CLIENT_CBACTION_INITIAL, flow->interface_index, flow->necp_flow_flags, &viable);
2125 flow->viable = viable;
2126 }
2127 }
2128 } else {
2129 struct necp_client_interface_option *option = &client->extra_interface_options[option_i - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT];
2130 struct necp_client_flow *flow = necp_client_add_interface_flow_if_needed(client, flow_registration, option->interface_index);
2131 if (flow != NULL && send_initial) {
2132 flow->viable = necp_client_flow_is_viable(proc, client, flow);
2133 if (flow->viable && flow->u.cb) {
2134 bool viable = flow->viable;
2135 flow->u.cb(flow_registration->interface_handle, NECP_CLIENT_CBACTION_INITIAL, flow->interface_index, flow->necp_flow_flags, &viable);
2136 flow->viable = viable;
2137 }
2138 }
2139 }
2140 }
2141 }
2142
2143 static bool
necp_client_update_flows(proc_t proc,struct necp_client * client,struct _necp_flow_defunct_list * defunct_list)2144 necp_client_update_flows(proc_t proc,
2145 struct necp_client *client,
2146 struct _necp_flow_defunct_list *defunct_list)
2147 {
2148 NECP_CLIENT_ASSERT_LOCKED(client);
2149
2150 bool any_client_updated = FALSE;
2151 struct necp_client_flow *flow = NULL;
2152 struct necp_client_flow *temp_flow = NULL;
2153 struct necp_client_flow_registration *flow_registration = NULL;
2154 RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
2155 if (flow_registration->interface_cb != NULL) {
2156 // Add any interface flows that are not already tracked
2157 necp_flow_add_interface_flows(proc, client, flow_registration, false);
2158 }
2159
2160 LIST_FOREACH_SAFE(flow, &flow_registration->flow_list, flow_chain, temp_flow) {
2161 bool client_updated = FALSE;
2162
2163 // Check policy result for flow
2164 u_short old_delegated_ifindex = flow->delegated_interface_index;
2165
2166 int old_flags = flow->necp_flow_flags;
2167 bool viable = necp_client_flow_is_viable(proc, client, flow);
2168
2169 // TODO: Defunct nexus flows that are blocked by policy
2170
2171 if (flow->viable != viable) {
2172 flow->viable = viable;
2173 client_updated = TRUE;
2174 }
2175
2176 if ((old_flags & NECP_CLIENT_RESULT_FLAG_FORCE_UPDATE) !=
2177 (flow->necp_flow_flags & NECP_CLIENT_RESULT_FLAG_FORCE_UPDATE)) {
2178 client_updated = TRUE;
2179 }
2180
2181 if (flow->delegated_interface_index != old_delegated_ifindex) {
2182 client_updated = TRUE;
2183 }
2184
2185 if (flow->viable && client_updated && (flow->socket || (!flow->socket && !flow->nexus)) && flow->u.cb) {
2186 bool flow_viable = flow->viable;
2187 flow->u.cb(flow->u.socket_handle, NECP_CLIENT_CBACTION_VIABLE, flow->interface_index, flow->necp_flow_flags, &flow_viable);
2188 flow->viable = flow_viable;
2189 }
2190
2191 if (!flow->viable || flow->invalid) {
2192 if (client_updated && (flow->socket || (!flow->socket && !flow->nexus)) && flow->u.cb) {
2193 bool flow_viable = flow->viable;
2194 flow->u.cb(flow->u.socket_handle, NECP_CLIENT_CBACTION_NONVIABLE, flow->interface_index, flow->necp_flow_flags, &flow_viable);
2195 flow->viable = flow_viable;
2196 }
2197 // The callback might change the viable-flag of the
2198 // flow depending on its policy. Thus, we need to
2199 // check the flags again after the callback.
2200 }
2201
2202 #if SKYWALK
2203 if (defunct_list != NULL) {
2204 if (flow->invalid && flow->nexus && flow->assigned && !uuid_is_null(flow->u.nexus_agent)) {
2205 // This is a nexus flow that was assigned, but not found on path
2206 u_int32_t flags = netagent_get_flags(flow->u.nexus_agent);
2207 if (!(flags & NETAGENT_FLAG_REGISTERED)) {
2208 // The agent is no longer registered! Mark defunct.
2209 necp_defunct_flow_registration(client, flow_registration, defunct_list);
2210 client_updated = TRUE;
2211 }
2212 }
2213 }
2214 #else /* !SKYWALK */
2215 (void)defunct_list;
2216 #endif /* !SKYWALK */
2217
2218 // Handle flows that no longer match
2219 if (!flow->viable || flow->invalid) {
2220 // Drop them as long as they aren't assigned data
2221 if (!flow->nexus && !flow->assigned) {
2222 if (flow->assigned_results != NULL) {
2223 kfree_data(flow->assigned_results, flow->assigned_results_length);
2224 flow->assigned_results = NULL;
2225 client_updated = TRUE;
2226 }
2227 LIST_REMOVE(flow, flow_chain);
2228 #if SKYWALK
2229 if (flow->nexus) {
2230 OSDecrementAtomic(&necp_nexus_flow_count);
2231 } else
2232 #endif /* SKYWALK */
2233 if (flow->socket) {
2234 OSDecrementAtomic(&necp_socket_flow_count);
2235 } else {
2236 OSDecrementAtomic(&necp_if_flow_count);
2237 }
2238 kfree_type(struct necp_client_flow, flow);
2239 }
2240 }
2241
2242 any_client_updated |= client_updated;
2243 }
2244 #if SKYWALK
2245 necp_flow_save_current_interface_details(flow_registration);
2246 #endif /* SKYWALK */
2247 }
2248
2249 return any_client_updated;
2250 }
2251
2252 static void
necp_client_mark_all_nonsocket_flows_as_invalid(struct necp_client * client)2253 necp_client_mark_all_nonsocket_flows_as_invalid(struct necp_client *client)
2254 {
2255 struct necp_client_flow_registration *flow_registration = NULL;
2256 struct necp_client_flow *flow = NULL;
2257 RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
2258 LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
2259 if (!flow->socket) { // Socket flows are not marked as invalid
2260 flow->invalid = TRUE;
2261 }
2262 }
2263 }
2264
2265 // Reset option count every update
2266 client->interface_option_count = 0;
2267 }
2268
2269 static inline bool
necp_netagent_is_requested(const struct necp_client_parsed_parameters * parameters,uuid_t * netagent_uuid)2270 necp_netagent_is_requested(const struct necp_client_parsed_parameters *parameters,
2271 uuid_t *netagent_uuid)
2272 {
2273 // Specific use agents only apply when requested
2274 bool requested = false;
2275 if (parameters != NULL) {
2276 // Check required agent UUIDs
2277 for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
2278 if (uuid_is_null(parameters->required_netagents[i])) {
2279 break;
2280 }
2281 if (uuid_compare(parameters->required_netagents[i], *netagent_uuid) == 0) {
2282 requested = true;
2283 break;
2284 }
2285 }
2286
2287 if (!requested) {
2288 // Check required agent types
2289 bool fetched_type = false;
2290 char netagent_domain[NETAGENT_DOMAINSIZE];
2291 char netagent_type[NETAGENT_TYPESIZE];
2292 memset(&netagent_domain, 0, NETAGENT_DOMAINSIZE);
2293 memset(&netagent_type, 0, NETAGENT_TYPESIZE);
2294
2295 for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
2296 if (strlen(parameters->required_netagent_types[i].netagent_domain) == 0 ||
2297 strlen(parameters->required_netagent_types[i].netagent_type) == 0) {
2298 break;
2299 }
2300
2301 if (!fetched_type) {
2302 if (netagent_get_agent_domain_and_type(*netagent_uuid, netagent_domain, netagent_type)) {
2303 fetched_type = TRUE;
2304 } else {
2305 break;
2306 }
2307 }
2308
2309 if ((strlen(parameters->required_netagent_types[i].netagent_domain) == 0 ||
2310 strncmp(netagent_domain, parameters->required_netagent_types[i].netagent_domain, NETAGENT_DOMAINSIZE) == 0) &&
2311 (strlen(parameters->required_netagent_types[i].netagent_type) == 0 ||
2312 strncmp(netagent_type, parameters->required_netagent_types[i].netagent_type, NETAGENT_TYPESIZE) == 0)) {
2313 requested = true;
2314 break;
2315 }
2316 }
2317 }
2318
2319 // Check preferred agent UUIDs
2320 for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
2321 if (uuid_is_null(parameters->preferred_netagents[i])) {
2322 break;
2323 }
2324 if (uuid_compare(parameters->preferred_netagents[i], *netagent_uuid) == 0) {
2325 requested = true;
2326 break;
2327 }
2328 }
2329
2330 if (!requested) {
2331 // Check preferred agent types
2332 bool fetched_type = false;
2333 char netagent_domain[NETAGENT_DOMAINSIZE];
2334 char netagent_type[NETAGENT_TYPESIZE];
2335 memset(&netagent_domain, 0, NETAGENT_DOMAINSIZE);
2336 memset(&netagent_type, 0, NETAGENT_TYPESIZE);
2337
2338 for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
2339 if (strlen(parameters->preferred_netagent_types[i].netagent_domain) == 0 ||
2340 strlen(parameters->preferred_netagent_types[i].netagent_type) == 0) {
2341 break;
2342 }
2343
2344 if (!fetched_type) {
2345 if (netagent_get_agent_domain_and_type(*netagent_uuid, netagent_domain, netagent_type)) {
2346 fetched_type = TRUE;
2347 } else {
2348 break;
2349 }
2350 }
2351
2352 if ((strlen(parameters->preferred_netagent_types[i].netagent_domain) == 0 ||
2353 strncmp(netagent_domain, parameters->preferred_netagent_types[i].netagent_domain, NETAGENT_DOMAINSIZE) == 0) &&
2354 (strlen(parameters->preferred_netagent_types[i].netagent_type) == 0 ||
2355 strncmp(netagent_type, parameters->preferred_netagent_types[i].netagent_type, NETAGENT_TYPESIZE) == 0)) {
2356 requested = true;
2357 break;
2358 }
2359 }
2360 }
2361 }
2362
2363 return requested;
2364 }
2365
2366 static bool
necp_netagent_applies_to_client(struct necp_client * client,const struct necp_client_parsed_parameters * parameters,uuid_t * netagent_uuid,bool allow_nexus,uint32_t interface_index,uint32_t interface_generation)2367 necp_netagent_applies_to_client(struct necp_client *client,
2368 const struct necp_client_parsed_parameters *parameters,
2369 uuid_t *netagent_uuid, bool allow_nexus,
2370 uint32_t interface_index, uint32_t interface_generation)
2371 {
2372 #pragma unused(interface_index, interface_generation)
2373 bool applies = FALSE;
2374 u_int32_t flags = netagent_get_flags(*netagent_uuid);
2375 if (!(flags & NETAGENT_FLAG_REGISTERED)) {
2376 // Unregistered agents never apply
2377 return applies;
2378 }
2379
2380 const bool is_nexus_agent = ((flags & NETAGENT_FLAG_NEXUS_PROVIDER) ||
2381 (flags & NETAGENT_FLAG_NEXUS_LISTENER) ||
2382 (flags & NETAGENT_FLAG_CUSTOM_ETHER_NEXUS) ||
2383 (flags & NETAGENT_FLAG_CUSTOM_IP_NEXUS) ||
2384 (flags & NETAGENT_FLAG_INTERPOSE_NEXUS));
2385 if (is_nexus_agent) {
2386 if (!allow_nexus) {
2387 // Hide nexus providers unless allowed
2388 // Direct interfaces and direct policies are allowed to use a nexus
2389 // Delegate interfaces or re-scoped interfaces are not allowed
2390 return applies;
2391 }
2392
2393 if ((parameters->flags & NECP_CLIENT_PARAMETER_FLAG_CUSTOM_ETHER) &&
2394 !(flags & NETAGENT_FLAG_CUSTOM_ETHER_NEXUS)) {
2395 // Client requested a custom ether nexus, but this nexus isn't one
2396 return applies;
2397 }
2398
2399 if ((parameters->flags & NECP_CLIENT_PARAMETER_FLAG_CUSTOM_IP) &&
2400 !(flags & NETAGENT_FLAG_CUSTOM_IP_NEXUS)) {
2401 // Client requested a custom IP nexus, but this nexus isn't one
2402 return applies;
2403 }
2404
2405 if ((parameters->flags & NECP_CLIENT_PARAMETER_FLAG_INTERPOSE) &&
2406 !(flags & NETAGENT_FLAG_INTERPOSE_NEXUS)) {
2407 // Client requested an interpose nexus, but this nexus isn't one
2408 return applies;
2409 }
2410
2411 if (!(parameters->flags & NECP_CLIENT_PARAMETER_FLAG_CUSTOM_ETHER) &&
2412 !(parameters->flags & NECP_CLIENT_PARAMETER_FLAG_CUSTOM_IP) &&
2413 !(parameters->flags & NECP_CLIENT_PARAMETER_FLAG_INTERPOSE) &&
2414 !(flags & NETAGENT_FLAG_NEXUS_PROVIDER)) {
2415 // Client requested default parameters, but this nexus isn't generic
2416 return applies;
2417 }
2418 }
2419
2420 if (uuid_compare(client->failed_trigger_agent.netagent_uuid, *netagent_uuid) == 0) {
2421 if (client->failed_trigger_agent.generation == netagent_get_generation(*netagent_uuid)) {
2422 // If this agent was triggered, and failed, and hasn't changed, keep hiding it
2423 return applies;
2424 } else {
2425 // Mismatch generation, clear out old trigger
2426 uuid_clear(client->failed_trigger_agent.netagent_uuid);
2427 client->failed_trigger_agent.generation = 0;
2428 }
2429 }
2430
2431 if (flags & NETAGENT_FLAG_SPECIFIC_USE_ONLY) {
2432 // Specific use agents only apply when requested
2433 applies = necp_netagent_is_requested(parameters, netagent_uuid);
2434 } else {
2435 applies = TRUE;
2436 }
2437
2438 #if SKYWALK
2439 // Add nexus agent if it is a nexus, and either is not a listener, or the nexus supports listeners
2440 if (applies && is_nexus_agent &&
2441 !(parameters->flags & NECP_CLIENT_PARAMETER_FLAG_BROWSE) && // Don't add for browse paths
2442 ((flags & NETAGENT_FLAG_NEXUS_LISTENER) || !(parameters->flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER))) {
2443 necp_client_add_interface_option_if_needed(client, interface_index,
2444 interface_generation, netagent_uuid,
2445 (flags & NETAGENT_FLAG_NETWORK_PROVIDER));
2446 }
2447 #endif /* SKYWALK */
2448
2449 return applies;
2450 }
2451
2452 static void
necp_client_add_agent_interface_options(struct necp_client * client,const struct necp_client_parsed_parameters * parsed_parameters,ifnet_t ifp)2453 necp_client_add_agent_interface_options(struct necp_client *client,
2454 const struct necp_client_parsed_parameters *parsed_parameters,
2455 ifnet_t ifp)
2456 {
2457 if (ifp != NULL && ifp->if_agentids != NULL) {
2458 for (u_int32_t i = 0; i < ifp->if_agentcount; i++) {
2459 if (uuid_is_null(ifp->if_agentids[i])) {
2460 continue;
2461 }
2462 // Relies on the side effect that nexus agents that apply will create flows
2463 (void)necp_netagent_applies_to_client(client, parsed_parameters, &ifp->if_agentids[i], TRUE,
2464 ifp->if_index, ifnet_get_generation(ifp));
2465 }
2466 }
2467 }
2468
2469 static void
necp_client_add_browse_interface_options(struct necp_client * client,const struct necp_client_parsed_parameters * parsed_parameters,ifnet_t ifp)2470 necp_client_add_browse_interface_options(struct necp_client *client,
2471 const struct necp_client_parsed_parameters *parsed_parameters,
2472 ifnet_t ifp)
2473 {
2474 if (ifp != NULL && ifp->if_agentids != NULL) {
2475 for (u_int32_t i = 0; i < ifp->if_agentcount; i++) {
2476 if (uuid_is_null(ifp->if_agentids[i])) {
2477 continue;
2478 }
2479
2480 u_int32_t flags = netagent_get_flags(ifp->if_agentids[i]);
2481 if ((flags & NETAGENT_FLAG_REGISTERED) &&
2482 (flags & NETAGENT_FLAG_ACTIVE) &&
2483 (flags & NETAGENT_FLAG_SUPPORTS_BROWSE) &&
2484 (!(flags & NETAGENT_FLAG_SPECIFIC_USE_ONLY) ||
2485 necp_netagent_is_requested(parsed_parameters, &ifp->if_agentids[i]))) {
2486 necp_client_add_interface_option_if_needed(client, ifp->if_index, ifnet_get_generation(ifp), &ifp->if_agentids[i], (flags & NETAGENT_FLAG_NETWORK_PROVIDER));
2487
2488 // Finding one is enough
2489 break;
2490 }
2491 }
2492 }
2493 }
2494
2495 static inline bool
_necp_client_address_is_valid(struct sockaddr * address)2496 _necp_client_address_is_valid(struct sockaddr *address)
2497 {
2498 if (address->sa_family == AF_INET) {
2499 return address->sa_len == sizeof(struct sockaddr_in);
2500 } else if (address->sa_family == AF_INET6) {
2501 return address->sa_len == sizeof(struct sockaddr_in6);
2502 } else {
2503 return FALSE;
2504 }
2505 }
2506
2507 #define necp_client_address_is_valid(S) _necp_client_address_is_valid(SA(S))
2508
2509 static inline bool
necp_client_endpoint_is_unspecified(struct necp_client_endpoint * endpoint)2510 necp_client_endpoint_is_unspecified(struct necp_client_endpoint *endpoint)
2511 {
2512 if (necp_client_address_is_valid(&endpoint->u.sa)) {
2513 if (endpoint->u.sa.sa_family == AF_INET) {
2514 return endpoint->u.sin.sin_addr.s_addr == INADDR_ANY;
2515 } else if (endpoint->u.sa.sa_family == AF_INET6) {
2516 return IN6_IS_ADDR_UNSPECIFIED(&endpoint->u.sin6.sin6_addr);
2517 } else {
2518 return TRUE;
2519 }
2520 } else {
2521 return TRUE;
2522 }
2523 }
2524
2525 #if SKYWALK
2526 static void
necp_client_update_local_port_parameters(u_int8_t * parameters,u_int32_t parameters_size,uint16_t local_port)2527 necp_client_update_local_port_parameters(u_int8_t *parameters,
2528 u_int32_t parameters_size,
2529 uint16_t local_port)
2530 {
2531 size_t offset = 0;
2532 while ((offset + sizeof(struct necp_tlv_header)) <= parameters_size) {
2533 u_int8_t type = necp_buffer_get_tlv_type(parameters, offset);
2534 u_int32_t length = necp_buffer_get_tlv_length(parameters, offset);
2535
2536 if (length > (parameters_size - (offset + sizeof(struct necp_tlv_header)))) {
2537 // If the length is larger than what can fit in the remaining parameters size, bail
2538 NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
2539 break;
2540 }
2541
2542 if (length > 0) {
2543 u_int8_t *value = necp_buffer_get_tlv_value(parameters, offset, NULL);
2544 if (value != NULL) {
2545 switch (type) {
2546 case NECP_CLIENT_PARAMETER_LOCAL_ADDRESS: {
2547 if (length >= sizeof(struct necp_policy_condition_addr)) {
2548 struct necp_policy_condition_addr *address_struct = (struct necp_policy_condition_addr *)(void *)value;
2549 if (necp_client_address_is_valid(&address_struct->address.sa)) {
2550 if (address_struct->address.sa.sa_family == AF_INET) {
2551 address_struct->address.sin.sin_port = local_port;
2552 } else if (address_struct->address.sa.sa_family == AF_INET6) {
2553 address_struct->address.sin6.sin6_port = local_port;
2554 }
2555 }
2556 }
2557 break;
2558 }
2559 case NECP_CLIENT_PARAMETER_LOCAL_ENDPOINT: {
2560 if (length >= sizeof(struct necp_client_endpoint)) {
2561 struct necp_client_endpoint *endpoint = (struct necp_client_endpoint *)(void *)value;
2562 if (necp_client_address_is_valid(&endpoint->u.sa)) {
2563 if (endpoint->u.sa.sa_family == AF_INET) {
2564 endpoint->u.sin.sin_port = local_port;
2565 } else if (endpoint->u.sa.sa_family == AF_INET6) {
2566 endpoint->u.sin6.sin6_port = local_port;
2567 }
2568 }
2569 }
2570 break;
2571 }
2572 default: {
2573 break;
2574 }
2575 }
2576 }
2577 }
2578
2579 offset += sizeof(struct necp_tlv_header) + length;
2580 }
2581 }
2582 #endif /* !SKYWALK */
2583
2584 #define NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH 253
2585
2586 static void
necp_client_trace_parameter_parsing(struct necp_client * client,u_int8_t type,u_int8_t * value,u_int32_t length)2587 necp_client_trace_parameter_parsing(struct necp_client *client, u_int8_t type, u_int8_t *value, u_int32_t length)
2588 {
2589 uint64_t num = 0;
2590 uint16_t shortBuf;
2591 uint32_t intBuf;
2592 char buffer[NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH + 1];
2593
2594 if (value != NULL && length > 0) {
2595 switch (length) {
2596 case 1:
2597 num = *value;
2598 break;
2599 case 2:
2600 memcpy(&shortBuf, value, sizeof(shortBuf));
2601 num = shortBuf;
2602 break;
2603 case 4:
2604 memcpy(&intBuf, value, sizeof(intBuf));
2605 num = intBuf;
2606 break;
2607 case 8:
2608 memcpy(&num, value, sizeof(num));
2609 break;
2610 default:
2611 num = 0;
2612 break;
2613 }
2614 int len = NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH < length ? NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH : length;
2615 memcpy(buffer, value, len);
2616 buffer[len] = 0;
2617 NECP_CLIENT_PARAMS_LOG(client, "Parsing param - type %d length %d value <%llu (%llX)> %s", type, length, num, num, buffer);
2618 } else {
2619 NECP_CLIENT_PARAMS_LOG(client, "Parsing param - type %d length %d", type, length);
2620 }
2621 }
2622
2623 static void
necp_client_trace_parsed_parameters(struct necp_client * client,struct necp_client_parsed_parameters * parsed_parameters)2624 necp_client_trace_parsed_parameters(struct necp_client *client, struct necp_client_parsed_parameters *parsed_parameters)
2625 {
2626 int i;
2627 char local_buffer[64] = { };
2628 char remote_buffer[64] = { };
2629 uuid_string_t uuid_str = { };
2630 uuid_unparse_lower(parsed_parameters->effective_uuid, uuid_str);
2631
2632 switch (parsed_parameters->local_addr.sa.sa_family) {
2633 case AF_INET:
2634 if (parsed_parameters->local_addr.sa.sa_len == sizeof(struct sockaddr_in)) {
2635 struct sockaddr_in *addr = &parsed_parameters->local_addr.sin;
2636 inet_ntop(AF_INET, &(addr->sin_addr), local_buffer, sizeof(local_buffer));
2637 }
2638 break;
2639 case AF_INET6:
2640 if (parsed_parameters->local_addr.sa.sa_len == sizeof(struct sockaddr_in6)) {
2641 struct sockaddr_in6 *addr6 = &parsed_parameters->local_addr.sin6;
2642 inet_ntop(AF_INET6, &(addr6->sin6_addr), local_buffer, sizeof(local_buffer));
2643 }
2644 break;
2645 default:
2646 break;
2647 }
2648
2649 switch (parsed_parameters->remote_addr.sa.sa_family) {
2650 case AF_INET:
2651 if (parsed_parameters->remote_addr.sa.sa_len == sizeof(struct sockaddr_in)) {
2652 struct sockaddr_in *addr = &parsed_parameters->remote_addr.sin;
2653 inet_ntop(AF_INET, &(addr->sin_addr), remote_buffer, sizeof(remote_buffer));
2654 }
2655 break;
2656 case AF_INET6:
2657 if (parsed_parameters->remote_addr.sa.sa_len == sizeof(struct sockaddr_in6)) {
2658 struct sockaddr_in6 *addr6 = &parsed_parameters->remote_addr.sin6;
2659 inet_ntop(AF_INET6, &(addr6->sin6_addr), remote_buffer, sizeof(remote_buffer));
2660 }
2661 break;
2662 default:
2663 break;
2664 }
2665
2666 NECP_CLIENT_PARAMS_LOG(client, "Parsed params - valid_fields %X flags %X delegated_upid %llu local_addr %s remote_addr %s "
2667 "required_interface_index %u required_interface_type %d local_address_preference %d "
2668 "ip_protocol %d transport_protocol %d ethertype %d effective_pid %d effective_uuid %s traffic_class %d",
2669 parsed_parameters->valid_fields,
2670 parsed_parameters->flags,
2671 parsed_parameters->delegated_upid,
2672 local_buffer, remote_buffer,
2673 parsed_parameters->required_interface_index,
2674 parsed_parameters->required_interface_type,
2675 parsed_parameters->local_address_preference,
2676 parsed_parameters->ip_protocol,
2677 parsed_parameters->transport_protocol,
2678 parsed_parameters->ethertype,
2679 parsed_parameters->effective_pid,
2680 uuid_str,
2681 parsed_parameters->traffic_class);
2682
2683 NECP_CLIENT_PARAMS_LOG(client, "Parsed params - tracker flags <known-tracker %X> <non-app-initiated %X> <silent %X> <app-approved %X>",
2684 parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_KNOWN_TRACKER,
2685 parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_NON_APP_INITIATED,
2686 parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_SILENT,
2687 parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_APPROVED_APP_DOMAIN);
2688
2689 for (i = 0; i < NECP_MAX_INTERFACE_PARAMETERS && parsed_parameters->prohibited_interfaces[i][0]; i++) {
2690 NECP_CLIENT_PARAMS_LOG(client, "Parsed prohibited_interfaces[%d] <%s>", i, parsed_parameters->prohibited_interfaces[i]);
2691 }
2692
2693 for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && parsed_parameters->required_netagent_types[i].netagent_domain[0]; i++) {
2694 NECP_CLIENT_PARAMS_LOG(client, "Parsed required_netagent_types[%d] <%s> <%s>", i,
2695 parsed_parameters->required_netagent_types[i].netagent_domain,
2696 parsed_parameters->required_netagent_types[i].netagent_type);
2697 }
2698 for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && parsed_parameters->prohibited_netagent_types[i].netagent_domain[0]; i++) {
2699 NECP_CLIENT_PARAMS_LOG(client, "Parsed prohibited_netagent_types[%d] <%s> <%s>", i,
2700 parsed_parameters->prohibited_netagent_types[i].netagent_domain,
2701 parsed_parameters->prohibited_netagent_types[i].netagent_type);
2702 }
2703 for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && parsed_parameters->preferred_netagent_types[i].netagent_domain[0]; i++) {
2704 NECP_CLIENT_PARAMS_LOG(client, "Parsed preferred_netagent_types[%d] <%s> <%s>", i,
2705 parsed_parameters->preferred_netagent_types[i].netagent_domain,
2706 parsed_parameters->preferred_netagent_types[i].netagent_type);
2707 }
2708 for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && parsed_parameters->avoided_netagent_types[i].netagent_domain[0]; i++) {
2709 NECP_CLIENT_PARAMS_LOG(client, "Parsed avoided_netagent_types[%d] <%s> <%s>", i,
2710 parsed_parameters->avoided_netagent_types[i].netagent_domain,
2711 parsed_parameters->avoided_netagent_types[i].netagent_type);
2712 }
2713
2714 for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && !uuid_is_null(parsed_parameters->required_netagents[i]); i++) {
2715 uuid_unparse_lower(parsed_parameters->required_netagents[i], uuid_str);
2716 NECP_CLIENT_PARAMS_LOG(client, "Parsed required_netagents[%d] <%s>", i, uuid_str);
2717 }
2718 for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && !uuid_is_null(parsed_parameters->prohibited_netagents[i]); i++) {
2719 uuid_unparse_lower(parsed_parameters->prohibited_netagents[i], uuid_str);
2720 NECP_CLIENT_PARAMS_LOG(client, "Parsed prohibited_netagents[%d] <%s>", i, uuid_str);
2721 }
2722 for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && !uuid_is_null(parsed_parameters->preferred_netagents[i]); i++) {
2723 uuid_unparse_lower(parsed_parameters->preferred_netagents[i], uuid_str);
2724 NECP_CLIENT_PARAMS_LOG(client, "Parsed preferred_netagents[%d] <%s>", i, uuid_str);
2725 }
2726 for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && !uuid_is_null(parsed_parameters->avoided_netagents[i]); i++) {
2727 uuid_unparse_lower(parsed_parameters->avoided_netagents[i], uuid_str);
2728 NECP_CLIENT_PARAMS_LOG(client, "Parsed avoided_netagents[%d] <%s>", i, uuid_str);
2729 }
2730 }
2731
2732 static bool
necp_client_strings_are_equal(const char * string1,size_t string1_length,const char * string2,size_t string2_length)2733 necp_client_strings_are_equal(const char *string1, size_t string1_length,
2734 const char *string2, size_t string2_length)
2735 {
2736 if (string1 == NULL || string2 == NULL) {
2737 return false;
2738 }
2739 const size_t string1_actual_length = strnlen(string1, string1_length);
2740 const size_t string2_actual_length = strnlen(string2, string2_length);
2741 if (string1_actual_length != string2_actual_length) {
2742 return false;
2743 }
2744 return strncmp(string1, string2, string1_actual_length) == 0;
2745 }
2746
2747 static int
necp_client_parse_parameters(struct necp_client * client,u_int8_t * parameters,u_int32_t parameters_size,struct necp_client_parsed_parameters * parsed_parameters)2748 necp_client_parse_parameters(struct necp_client *client, u_int8_t *parameters,
2749 u_int32_t parameters_size,
2750 struct necp_client_parsed_parameters *parsed_parameters)
2751 {
2752 int error = 0;
2753 size_t offset = 0;
2754
2755 u_int32_t num_prohibited_interfaces = 0;
2756 u_int32_t num_prohibited_interface_types = 0;
2757 u_int32_t num_required_agents = 0;
2758 u_int32_t num_prohibited_agents = 0;
2759 u_int32_t num_preferred_agents = 0;
2760 u_int32_t num_avoided_agents = 0;
2761 u_int32_t num_required_agent_types = 0;
2762 u_int32_t num_prohibited_agent_types = 0;
2763 u_int32_t num_preferred_agent_types = 0;
2764 u_int32_t num_avoided_agent_types = 0;
2765 u_int8_t *resolver_tag = NULL;
2766 u_int32_t resolver_tag_length = 0;
2767 u_int8_t *client_hostname = NULL;
2768 u_int32_t hostname_length = 0;
2769 uuid_t parent_id = {};
2770
2771 if (parsed_parameters == NULL) {
2772 return EINVAL;
2773 }
2774
2775 memset(parsed_parameters, 0, sizeof(struct necp_client_parsed_parameters));
2776
2777 while ((offset + sizeof(struct necp_tlv_header)) <= parameters_size) {
2778 u_int8_t type = necp_buffer_get_tlv_type(parameters, offset);
2779 u_int32_t length = necp_buffer_get_tlv_length(parameters, offset);
2780
2781 if (length > (parameters_size - (offset + sizeof(struct necp_tlv_header)))) {
2782 // If the length is larger than what can fit in the remaining parameters size, bail
2783 NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
2784 break;
2785 }
2786
2787 if (length > 0) {
2788 u_int8_t *value = necp_buffer_get_tlv_value(parameters, offset, NULL);
2789 if (value != NULL) {
2790 switch (type) {
2791 case NECP_CLIENT_PARAMETER_BOUND_INTERFACE: {
2792 if (length <= IFXNAMSIZ && length > 0) {
2793 ifnet_t bound_interface = NULL;
2794 char interface_name[IFXNAMSIZ];
2795 memcpy(interface_name, value, length);
2796 interface_name[length - 1] = 0; // Make sure the string is NULL terminated
2797 if (ifnet_find_by_name(interface_name, &bound_interface) == 0) {
2798 parsed_parameters->required_interface_index = bound_interface->if_index;
2799 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF;
2800 ifnet_release(bound_interface);
2801 }
2802 }
2803 break;
2804 }
2805 case NECP_CLIENT_PARAMETER_LOCAL_ADDRESS: {
2806 if (length >= sizeof(struct necp_policy_condition_addr)) {
2807 struct necp_policy_condition_addr *address_struct = (struct necp_policy_condition_addr *)(void *)value;
2808 if (necp_client_address_is_valid(&address_struct->address.sa)) {
2809 memcpy(&parsed_parameters->local_addr, &address_struct->address, sizeof(address_struct->address));
2810 if (!necp_address_is_wildcard(&parsed_parameters->local_addr)) {
2811 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR;
2812 }
2813 if ((parsed_parameters->local_addr.sa.sa_family == AF_INET && parsed_parameters->local_addr.sin.sin_port) ||
2814 (parsed_parameters->local_addr.sa.sa_family == AF_INET6 && parsed_parameters->local_addr.sin6.sin6_port)) {
2815 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_LOCAL_PORT;
2816 }
2817 }
2818 }
2819 break;
2820 }
2821 case NECP_CLIENT_PARAMETER_LOCAL_ENDPOINT: {
2822 if (length >= sizeof(struct necp_client_endpoint)) {
2823 struct necp_client_endpoint *endpoint = (struct necp_client_endpoint *)(void *)value;
2824 if (necp_client_address_is_valid(&endpoint->u.sa)) {
2825 memcpy(&parsed_parameters->local_addr, &endpoint->u.sa, sizeof(union necp_sockaddr_union));
2826 if (!necp_address_is_wildcard(&parsed_parameters->local_addr)) {
2827 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR;
2828 }
2829 if ((parsed_parameters->local_addr.sa.sa_family == AF_INET && parsed_parameters->local_addr.sin.sin_port) ||
2830 (parsed_parameters->local_addr.sa.sa_family == AF_INET6 && parsed_parameters->local_addr.sin6.sin6_port)) {
2831 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_LOCAL_PORT;
2832 }
2833 }
2834 }
2835 break;
2836 }
2837 case NECP_CLIENT_PARAMETER_REMOTE_ADDRESS: {
2838 if (length >= sizeof(struct necp_policy_condition_addr)) {
2839 struct necp_policy_condition_addr *address_struct = (struct necp_policy_condition_addr *)(void *)value;
2840 if (necp_client_address_is_valid(&address_struct->address.sa)) {
2841 memcpy(&parsed_parameters->remote_addr, &address_struct->address, sizeof(address_struct->address));
2842 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_REMOTE_ADDR;
2843 }
2844 }
2845 break;
2846 }
2847 case NECP_CLIENT_PARAMETER_REMOTE_ENDPOINT: {
2848 if (length >= sizeof(struct necp_client_endpoint)) {
2849 struct necp_client_endpoint *endpoint = (struct necp_client_endpoint *)(void *)value;
2850 if (necp_client_address_is_valid(&endpoint->u.sa)) {
2851 memcpy(&parsed_parameters->remote_addr, &endpoint->u.sa, sizeof(union necp_sockaddr_union));
2852 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_REMOTE_ADDR;
2853 }
2854 }
2855 break;
2856 }
2857 case NECP_CLIENT_PARAMETER_PROHIBIT_INTERFACE: {
2858 if (num_prohibited_interfaces >= NECP_MAX_INTERFACE_PARAMETERS) {
2859 break;
2860 }
2861 if (length <= IFXNAMSIZ && length > 0) {
2862 memcpy(parsed_parameters->prohibited_interfaces[num_prohibited_interfaces], value, length);
2863 parsed_parameters->prohibited_interfaces[num_prohibited_interfaces][length - 1] = 0; // Make sure the string is NULL terminated
2864 num_prohibited_interfaces++;
2865 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IF;
2866 }
2867 break;
2868 }
2869 case NECP_CLIENT_PARAMETER_REQUIRE_IF_TYPE: {
2870 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE) {
2871 break;
2872 }
2873 if (length >= sizeof(u_int8_t)) {
2874 memcpy(&parsed_parameters->required_interface_type, value, sizeof(u_int8_t));
2875 if (parsed_parameters->required_interface_type) {
2876 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE;
2877 }
2878 }
2879 break;
2880 }
2881 case NECP_CLIENT_PARAMETER_PROHIBIT_IF_TYPE: {
2882 if (num_prohibited_interface_types >= NECP_MAX_INTERFACE_PARAMETERS) {
2883 break;
2884 }
2885 if (length >= sizeof(u_int8_t)) {
2886 memcpy(&parsed_parameters->prohibited_interface_types[num_prohibited_interface_types], value, sizeof(u_int8_t));
2887 num_prohibited_interface_types++;
2888 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IFTYPE;
2889 }
2890 break;
2891 }
2892 case NECP_CLIENT_PARAMETER_REQUIRE_AGENT: {
2893 if (num_required_agents >= NECP_MAX_AGENT_PARAMETERS) {
2894 break;
2895 }
2896 if (length >= sizeof(uuid_t)) {
2897 memcpy(&parsed_parameters->required_netagents[num_required_agents], value, sizeof(uuid_t));
2898 num_required_agents++;
2899 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT;
2900 }
2901 break;
2902 }
2903 case NECP_CLIENT_PARAMETER_PROHIBIT_AGENT: {
2904 if (num_prohibited_agents >= NECP_MAX_AGENT_PARAMETERS) {
2905 break;
2906 }
2907 if (length >= sizeof(uuid_t)) {
2908 memcpy(&parsed_parameters->prohibited_netagents[num_prohibited_agents], value, sizeof(uuid_t));
2909 num_prohibited_agents++;
2910 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT;
2911 }
2912 break;
2913 }
2914 case NECP_CLIENT_PARAMETER_PREFER_AGENT: {
2915 if (num_preferred_agents >= NECP_MAX_AGENT_PARAMETERS) {
2916 break;
2917 }
2918 if (length >= sizeof(uuid_t)) {
2919 memcpy(&parsed_parameters->preferred_netagents[num_preferred_agents], value, sizeof(uuid_t));
2920 num_preferred_agents++;
2921 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT;
2922 }
2923 break;
2924 }
2925 case NECP_CLIENT_PARAMETER_AVOID_AGENT: {
2926 if (num_avoided_agents >= NECP_MAX_AGENT_PARAMETERS) {
2927 break;
2928 }
2929 if (length >= sizeof(uuid_t)) {
2930 memcpy(&parsed_parameters->avoided_netagents[num_avoided_agents], value, sizeof(uuid_t));
2931 num_avoided_agents++;
2932 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT;
2933 }
2934 break;
2935 }
2936 case NECP_CLIENT_PARAMETER_REQUIRE_AGENT_TYPE: {
2937 if (num_required_agent_types >= NECP_MAX_AGENT_PARAMETERS) {
2938 break;
2939 }
2940 if (length >= sizeof(struct necp_client_parameter_netagent_type)) {
2941 memcpy(&parsed_parameters->required_netagent_types[num_required_agent_types], value, sizeof(struct necp_client_parameter_netagent_type));
2942 num_required_agent_types++;
2943 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE;
2944 }
2945 break;
2946 }
2947 case NECP_CLIENT_PARAMETER_PROHIBIT_AGENT_TYPE: {
2948 if (num_prohibited_agent_types >= NECP_MAX_AGENT_PARAMETERS) {
2949 break;
2950 }
2951 if (length >= sizeof(struct necp_client_parameter_netagent_type)) {
2952 memcpy(&parsed_parameters->prohibited_netagent_types[num_prohibited_agent_types], value, sizeof(struct necp_client_parameter_netagent_type));
2953 num_prohibited_agent_types++;
2954 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT_TYPE;
2955 }
2956 break;
2957 }
2958 case NECP_CLIENT_PARAMETER_PREFER_AGENT_TYPE: {
2959 if (num_preferred_agent_types >= NECP_MAX_AGENT_PARAMETERS) {
2960 break;
2961 }
2962 if (length >= sizeof(struct necp_client_parameter_netagent_type)) {
2963 memcpy(&parsed_parameters->preferred_netagent_types[num_preferred_agent_types], value, sizeof(struct necp_client_parameter_netagent_type));
2964 num_preferred_agent_types++;
2965 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE;
2966 }
2967 break;
2968 }
2969 case NECP_CLIENT_PARAMETER_AVOID_AGENT_TYPE: {
2970 if (num_avoided_agent_types >= NECP_MAX_AGENT_PARAMETERS) {
2971 break;
2972 }
2973 if (length >= sizeof(struct necp_client_parameter_netagent_type)) {
2974 memcpy(&parsed_parameters->avoided_netagent_types[num_avoided_agent_types], value, sizeof(struct necp_client_parameter_netagent_type));
2975 num_avoided_agent_types++;
2976 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE;
2977 }
2978 break;
2979 }
2980 case NECP_CLIENT_PARAMETER_FLAGS: {
2981 if (length >= sizeof(u_int32_t)) {
2982 memcpy(&parsed_parameters->flags, value, sizeof(parsed_parameters->flags));
2983 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_FLAGS;
2984 }
2985 break;
2986 }
2987 case NECP_CLIENT_PARAMETER_IP_PROTOCOL: {
2988 if (length == sizeof(u_int16_t)) {
2989 u_int16_t large_ip_protocol = 0;
2990 memcpy(&large_ip_protocol, value, sizeof(large_ip_protocol));
2991 parsed_parameters->ip_protocol = (u_int8_t)large_ip_protocol;
2992 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_IP_PROTOCOL;
2993 } else if (length >= sizeof(parsed_parameters->ip_protocol)) {
2994 memcpy(&parsed_parameters->ip_protocol, value, sizeof(parsed_parameters->ip_protocol));
2995 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_IP_PROTOCOL;
2996 }
2997 break;
2998 }
2999 case NECP_CLIENT_PARAMETER_TRANSPORT_PROTOCOL: {
3000 if (length >= sizeof(parsed_parameters->transport_protocol)) {
3001 memcpy(&parsed_parameters->transport_protocol, value, sizeof(parsed_parameters->transport_protocol));
3002 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_TRANSPORT_PROTOCOL;
3003 }
3004 break;
3005 }
3006 case NECP_CLIENT_PARAMETER_PID: {
3007 if (length >= sizeof(parsed_parameters->effective_pid)) {
3008 memcpy(&parsed_parameters->effective_pid, value, sizeof(parsed_parameters->effective_pid));
3009 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID;
3010 }
3011 break;
3012 }
3013 case NECP_CLIENT_PARAMETER_DELEGATED_UPID: {
3014 if (length >= sizeof(parsed_parameters->delegated_upid)) {
3015 memcpy(&parsed_parameters->delegated_upid, value, sizeof(parsed_parameters->delegated_upid));
3016 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_DELEGATED_UPID;
3017 }
3018 break;
3019 }
3020 case NECP_CLIENT_PARAMETER_ETHERTYPE: {
3021 if (length >= sizeof(parsed_parameters->ethertype)) {
3022 memcpy(&parsed_parameters->ethertype, value, sizeof(parsed_parameters->ethertype));
3023 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_ETHERTYPE;
3024 }
3025 break;
3026 }
3027 case NECP_CLIENT_PARAMETER_APPLICATION: {
3028 if (length >= sizeof(parsed_parameters->effective_uuid)) {
3029 memcpy(&parsed_parameters->effective_uuid, value, sizeof(parsed_parameters->effective_uuid));
3030 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_UUID;
3031 }
3032 break;
3033 }
3034 case NECP_CLIENT_PARAMETER_TRAFFIC_CLASS: {
3035 if (length >= sizeof(parsed_parameters->traffic_class)) {
3036 memcpy(&parsed_parameters->traffic_class, value, sizeof(parsed_parameters->traffic_class));
3037 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_TRAFFIC_CLASS;
3038 }
3039 break;
3040 }
3041 case NECP_CLIENT_PARAMETER_RESOLVER_TAG: {
3042 if (length > 0) {
3043 if (resolver_tag != NULL) {
3044 // Multiple resolver tags is invalid
3045 NECPLOG0(LOG_ERR, "Multiple resolver tags are not supported");
3046 error = EINVAL;
3047 } else {
3048 resolver_tag = (u_int8_t *)value;
3049 resolver_tag_length = length;
3050 }
3051 }
3052 break;
3053 }
3054 case NECP_CLIENT_PARAMETER_DOMAIN: {
3055 if (length > 0) {
3056 client_hostname = (u_int8_t *)value;
3057 hostname_length = length;
3058 }
3059 break;
3060 }
3061 case NECP_CLIENT_PARAMETER_PARENT_ID: {
3062 if (length == sizeof(parent_id)) {
3063 uuid_copy(parent_id, value);
3064 memcpy(&parsed_parameters->parent_uuid, value, sizeof(parsed_parameters->parent_uuid));
3065 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PARENT_UUID;
3066 }
3067 break;
3068 }
3069 case NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE: {
3070 if (length >= sizeof(parsed_parameters->local_address_preference)) {
3071 memcpy(&parsed_parameters->local_address_preference, value, sizeof(parsed_parameters->local_address_preference));
3072 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR_PREFERENCE;
3073 }
3074 break;
3075 }
3076 case NECP_CLIENT_PARAMETER_ATTRIBUTED_BUNDLE_IDENTIFIER: {
3077 if (length > 0) {
3078 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_ATTRIBUTED_BUNDLE_IDENTIFIER;
3079 }
3080 break;
3081 }
3082 case NECP_CLIENT_PARAMETER_FLOW_DEMUX_PATTERN: {
3083 if (parsed_parameters->demux_pattern_count >= NECP_MAX_DEMUX_PATTERNS) {
3084 break;
3085 }
3086 if (length >= sizeof(struct necp_demux_pattern)) {
3087 memcpy(&parsed_parameters->demux_patterns[parsed_parameters->demux_pattern_count], value, sizeof(struct necp_demux_pattern));
3088 parsed_parameters->demux_pattern_count++;
3089 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_FLOW_DEMUX_PATTERN;
3090 }
3091 break;
3092 }
3093 default: {
3094 break;
3095 }
3096 }
3097 }
3098
3099 if (NECP_ENABLE_CLIENT_TRACE(NECP_CLIENT_TRACE_LEVEL_PARAMS)) {
3100 necp_client_trace_parameter_parsing(client, type, value, length);
3101 }
3102 }
3103
3104 offset += sizeof(struct necp_tlv_header) + length;
3105 }
3106
3107 if (resolver_tag != NULL) {
3108 struct necp_client_validatable *validatable = (struct necp_client_validatable *)resolver_tag;
3109 if (resolver_tag_length <= sizeof(struct necp_client_validatable)) {
3110 error = EINVAL;
3111 NECPLOG(LOG_ERR, "Resolver tag length too short: %u", resolver_tag_length);
3112 } else {
3113 bool matches = true;
3114
3115 // Check the client UUID for client-specific results
3116 if (validatable->signable.sign_type == NECP_CLIENT_SIGN_TYPE_RESOLVER_ANSWER ||
3117 validatable->signable.sign_type == NECP_CLIENT_SIGN_TYPE_BROWSE_RESULT ||
3118 validatable->signable.sign_type == NECP_CLIENT_SIGN_TYPE_SERVICE_RESOLVER_ANSWER) {
3119 if (uuid_compare(parent_id, validatable->signable.client_id) != 0 &&
3120 uuid_compare(client->client_id, validatable->signable.client_id) != 0) {
3121 NECPLOG0(LOG_ERR, "Resolver tag invalid client ID");
3122 matches = false;
3123 }
3124 }
3125
3126 size_t data_length = resolver_tag_length - sizeof(struct necp_client_validatable);
3127 switch (validatable->signable.sign_type) {
3128 case NECP_CLIENT_SIGN_TYPE_RESOLVER_ANSWER:
3129 case NECP_CLIENT_SIGN_TYPE_SYSTEM_RESOLVER_ANSWER: {
3130 if (data_length < (sizeof(struct necp_client_host_resolver_answer) - sizeof(struct necp_client_signable))) {
3131 NECPLOG0(LOG_ERR, "Resolver tag invalid length for resolver answer");
3132 matches = false;
3133 } else {
3134 struct necp_client_host_resolver_answer *answer_struct = (struct necp_client_host_resolver_answer *)&validatable->signable;
3135 if (data_length != (sizeof(struct necp_client_host_resolver_answer) + answer_struct->hostname_length - sizeof(struct necp_client_signable))) {
3136 NECPLOG0(LOG_ERR, "Resolver tag invalid length for resolver answer");
3137 matches = false;
3138 } else {
3139 if (answer_struct->hostname_length != 0 && // If the hostname on the signed answer is empty, ignore
3140 !necp_client_strings_are_equal((const char *)client_hostname, hostname_length,
3141 answer_struct->hostname, answer_struct->hostname_length)) {
3142 NECPLOG0(LOG_ERR, "Resolver tag hostname does not match");
3143 matches = false;
3144 } else if (answer_struct->address_answer.sa.sa_family != parsed_parameters->remote_addr.sa.sa_family ||
3145 answer_struct->address_answer.sa.sa_len != parsed_parameters->remote_addr.sa.sa_len) {
3146 NECPLOG0(LOG_ERR, "Resolver tag address type does not match");
3147 matches = false;
3148 } else if (answer_struct->address_answer.sin.sin_port != 0 && // If the port on the signed answer is empty, ignore
3149 answer_struct->address_answer.sin.sin_port != parsed_parameters->remote_addr.sin.sin_port) {
3150 NECPLOG0(LOG_ERR, "Resolver tag port does not match");
3151 matches = false;
3152 } else if ((answer_struct->address_answer.sa.sa_family == AF_INET &&
3153 answer_struct->address_answer.sin.sin_addr.s_addr != parsed_parameters->remote_addr.sin.sin_addr.s_addr) ||
3154 (answer_struct->address_answer.sa.sa_family == AF_INET6 &&
3155 memcmp(&answer_struct->address_answer.sin6.sin6_addr, &parsed_parameters->remote_addr.sin6.sin6_addr, sizeof(struct in6_addr)) != 0)) {
3156 NECPLOG0(LOG_ERR, "Resolver tag address does not match");
3157 matches = false;
3158 }
3159 }
3160 }
3161 break;
3162 }
3163 case NECP_CLIENT_SIGN_TYPE_BROWSE_RESULT:
3164 case NECP_CLIENT_SIGN_TYPE_SYSTEM_BROWSE_RESULT: {
3165 if (data_length < (sizeof(struct necp_client_browse_result) - sizeof(struct necp_client_signable))) {
3166 NECPLOG0(LOG_ERR, "Resolver tag invalid length for browse result");
3167 matches = false;
3168 } else {
3169 struct necp_client_browse_result *answer_struct = (struct necp_client_browse_result *)&validatable->signable;
3170 if (data_length != (sizeof(struct necp_client_browse_result) + answer_struct->service_length - sizeof(struct necp_client_signable))) {
3171 NECPLOG0(LOG_ERR, "Resolver tag invalid length for browse result");
3172 matches = false;
3173 }
3174 }
3175 break;
3176 }
3177 case NECP_CLIENT_SIGN_TYPE_SERVICE_RESOLVER_ANSWER:
3178 case NECP_CLIENT_SIGN_TYPE_SYSTEM_SERVICE_RESOLVER_ANSWER: {
3179 if (data_length < (sizeof(struct necp_client_service_resolver_answer) - sizeof(struct necp_client_signable))) {
3180 NECPLOG0(LOG_ERR, "Resolver tag invalid length for service resolver answer");
3181 matches = false;
3182 } else {
3183 struct necp_client_service_resolver_answer *answer_struct = (struct necp_client_service_resolver_answer *)&validatable->signable;
3184 if (data_length != (sizeof(struct necp_client_service_resolver_answer) + answer_struct->service_length + answer_struct->hostname_length - sizeof(struct necp_client_signable))) {
3185 NECPLOG0(LOG_ERR, "Resolver tag invalid length for service resolver answer");
3186 matches = false;
3187 }
3188 }
3189 break;
3190 }
3191 default: {
3192 NECPLOG(LOG_ERR, "Resolver tag unknown sign type: %u", validatable->signable.sign_type);
3193 matches = false;
3194 break;
3195 }
3196 }
3197 if (!matches) {
3198 error = EAUTH;
3199 } else {
3200 const bool validated = necp_validate_resolver_answer(validatable->signable.client_id,
3201 validatable->signable.sign_type,
3202 validatable->signable.signable_data, data_length,
3203 validatable->signature.signed_tag, sizeof(validatable->signature.signed_tag));
3204 if (!validated) {
3205 error = EAUTH;
3206 NECPLOG0(LOG_ERR, "Failed to validate resolve answer");
3207 }
3208 }
3209 }
3210 }
3211
3212 // Log if it is a known tracker
3213 if (parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_KNOWN_TRACKER && client) {
3214 NECP_CLIENT_TRACKER_LOG(client->proc_pid, "Parsing tracker flags - known-tracker %X non-app-initiated %X silent %X approved-app-domain %X",
3215 parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_KNOWN_TRACKER,
3216 parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_NON_APP_INITIATED,
3217 parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_SILENT,
3218 parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_APPROVED_APP_DOMAIN);
3219 }
3220
3221 if (NECP_ENABLE_CLIENT_TRACE(NECP_CLIENT_TRACE_LEVEL_PARAMS)) {
3222 necp_client_trace_parsed_parameters(client, parsed_parameters);
3223 }
3224
3225 return error;
3226 }
3227
3228 static int
necp_client_parse_result(u_int8_t * result,u_int32_t result_size,union necp_sockaddr_union * local_address,union necp_sockaddr_union * remote_address,void ** flow_stats)3229 necp_client_parse_result(u_int8_t *result,
3230 u_int32_t result_size,
3231 union necp_sockaddr_union *local_address,
3232 union necp_sockaddr_union *remote_address,
3233 void **flow_stats)
3234 {
3235 #pragma unused(flow_stats)
3236 int error = 0;
3237 size_t offset = 0;
3238
3239 while ((offset + sizeof(struct necp_tlv_header)) <= result_size) {
3240 u_int8_t type = necp_buffer_get_tlv_type(result, offset);
3241 u_int32_t length = necp_buffer_get_tlv_length(result, offset);
3242
3243 if (length > 0 && (offset + sizeof(struct necp_tlv_header) + length) <= result_size) {
3244 u_int8_t *value = necp_buffer_get_tlv_value(result, offset, NULL);
3245 if (value != NULL) {
3246 switch (type) {
3247 case NECP_CLIENT_RESULT_LOCAL_ENDPOINT: {
3248 if (length >= sizeof(struct necp_client_endpoint)) {
3249 struct necp_client_endpoint *endpoint = (struct necp_client_endpoint *)(void *)value;
3250 if (local_address != NULL && necp_client_address_is_valid(&endpoint->u.sa)) {
3251 memcpy(local_address, &endpoint->u.sa, endpoint->u.sa.sa_len);
3252 }
3253 }
3254 break;
3255 }
3256 case NECP_CLIENT_RESULT_REMOTE_ENDPOINT: {
3257 if (length >= sizeof(struct necp_client_endpoint)) {
3258 struct necp_client_endpoint *endpoint = (struct necp_client_endpoint *)(void *)value;
3259 if (remote_address != NULL && necp_client_address_is_valid(&endpoint->u.sa)) {
3260 memcpy(remote_address, &endpoint->u.sa, endpoint->u.sa.sa_len);
3261 }
3262 }
3263 break;
3264 }
3265 #if SKYWALK
3266 case NECP_CLIENT_RESULT_NEXUS_FLOW_STATS: {
3267 // this TLV contains flow_stats pointer which is refcnt'ed.
3268 if (flow_stats != NULL && length >= sizeof(struct sk_stats_flow *)) {
3269 struct flow_stats *fs = *(void **)(void *)value;
3270 // transfer the refcnt to flow_stats pointer
3271 *flow_stats = fs;
3272 }
3273 memset(value, 0, length); // nullify TLV always
3274 break;
3275 }
3276 #endif /* SKYWALK */
3277 default: {
3278 break;
3279 }
3280 }
3281 }
3282 }
3283
3284 offset += sizeof(struct necp_tlv_header) + length;
3285 }
3286
3287 return error;
3288 }
3289
3290 static struct necp_client_flow_registration *
necp_client_create_flow_registration(struct necp_fd_data * fd_data,struct necp_client * client)3291 necp_client_create_flow_registration(struct necp_fd_data *fd_data, struct necp_client *client)
3292 {
3293 NECP_FD_ASSERT_LOCKED(fd_data);
3294 NECP_CLIENT_ASSERT_LOCKED(client);
3295
3296 struct necp_client_flow_registration *new_registration = kalloc_type(struct necp_client_flow_registration, Z_WAITOK | Z_ZERO | Z_NOFAIL);
3297
3298 new_registration->last_interface_details = combine_interface_details(IFSCOPE_NONE, NSTAT_IFNET_IS_UNKNOWN_TYPE);
3299
3300 necp_generate_client_id(new_registration->registration_id, true);
3301 LIST_INIT(&new_registration->flow_list);
3302
3303 // Add registration to client list
3304 RB_INSERT(_necp_client_flow_tree, &client->flow_registrations, new_registration);
3305
3306 // Add registration to fd list
3307 RB_INSERT(_necp_fd_flow_tree, &fd_data->flows, new_registration);
3308
3309 // Add registration to global tree for lookup
3310 NECP_FLOW_TREE_LOCK_EXCLUSIVE();
3311 RB_INSERT(_necp_client_flow_global_tree, &necp_client_flow_global_tree, new_registration);
3312 NECP_FLOW_TREE_UNLOCK();
3313
3314 new_registration->client = client;
3315
3316 #if SKYWALK
3317 {
3318 // The uuid caching here is something of a hack, but saves a dynamic lookup with attendant lock hierarchy issues
3319 uint64_t stats_event_type = (uuid_is_null(client->latest_flow_registration_id)) ? NSTAT_EVENT_SRC_FLOW_UUID_ASSIGNED : NSTAT_EVENT_SRC_FLOW_UUID_CHANGED;
3320 uuid_copy(client->latest_flow_registration_id, new_registration->registration_id);
3321
3322 // With the flow uuid known, push a new statistics update to ensure the uuid gets known by any clients before the flow can close
3323 if (client->nstat_context != NULL) {
3324 nstat_provider_stats_event(client->nstat_context, stats_event_type);
3325 }
3326 }
3327 #endif /* !SKYWALK */
3328
3329 // Start out assuming there is nothing to read from the flow
3330 new_registration->flow_result_read = true;
3331
3332 return new_registration;
3333 }
3334
3335 static void
necp_client_add_socket_flow(struct necp_client_flow_registration * flow_registration,struct inpcb * inp)3336 necp_client_add_socket_flow(struct necp_client_flow_registration *flow_registration,
3337 struct inpcb *inp)
3338 {
3339 struct necp_client_flow *new_flow = kalloc_type(struct necp_client_flow, Z_WAITOK | Z_ZERO | Z_NOFAIL);
3340
3341 new_flow->socket = TRUE;
3342 new_flow->u.socket_handle = inp;
3343 new_flow->u.cb = inp->necp_cb;
3344
3345 OSIncrementAtomic(&necp_socket_flow_count);
3346
3347 LIST_INSERT_HEAD(&flow_registration->flow_list, new_flow, flow_chain);
3348 }
3349
3350 static int
necp_client_register_socket_inner(pid_t pid,uuid_t client_id,struct inpcb * inp,bool is_listener)3351 necp_client_register_socket_inner(pid_t pid, uuid_t client_id, struct inpcb *inp, bool is_listener)
3352 {
3353 int error = 0;
3354 struct necp_fd_data *client_fd = NULL;
3355 bool found_client = FALSE;
3356
3357 NECP_FD_LIST_LOCK_SHARED();
3358 LIST_FOREACH(client_fd, &necp_fd_list, chain) {
3359 NECP_FD_LOCK(client_fd);
3360 struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
3361 if (client != NULL) {
3362 if (!pid || client->proc_pid == pid) {
3363 if (is_listener) {
3364 found_client = TRUE;
3365 #if SKYWALK
3366 // Check netns token for registration
3367 if (!NETNS_TOKEN_VALID(&client->port_reservation)) {
3368 error = EINVAL;
3369 }
3370 #endif /* !SKYWALK */
3371 } else {
3372 // Find client flow and assign from socket
3373 struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
3374 if (flow_registration != NULL) {
3375 // Found the right client and flow registration, add a new flow
3376 found_client = TRUE;
3377 necp_client_add_socket_flow(flow_registration, inp);
3378 } else if (RB_EMPTY(&client->flow_registrations) && !necp_client_id_is_flow(client_id)) {
3379 // No flows yet on this client, add a new registration
3380 flow_registration = necp_client_create_flow_registration(client_fd, client);
3381 if (flow_registration == NULL) {
3382 error = ENOMEM;
3383 } else {
3384 // Add a new flow
3385 found_client = TRUE;
3386 necp_client_add_socket_flow(flow_registration, inp);
3387 }
3388 }
3389 }
3390 }
3391
3392 NECP_CLIENT_UNLOCK(client);
3393 }
3394 NECP_FD_UNLOCK(client_fd);
3395
3396 if (found_client) {
3397 break;
3398 }
3399 }
3400 NECP_FD_LIST_UNLOCK();
3401
3402 if (!found_client) {
3403 error = ENOENT;
3404 } else {
3405 // Count the sockets that have the NECP client UUID set
3406 struct socket *so = inp->inp_socket;
3407 if (!(so->so_flags1 & SOF1_HAS_NECP_CLIENT_UUID)) {
3408 so->so_flags1 |= SOF1_HAS_NECP_CLIENT_UUID;
3409 INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_necp_clientuuid_total);
3410 }
3411 }
3412
3413 return error;
3414 }
3415
3416 int
necp_client_register_socket_flow(pid_t pid,uuid_t client_id,struct inpcb * inp)3417 necp_client_register_socket_flow(pid_t pid, uuid_t client_id, struct inpcb *inp)
3418 {
3419 return necp_client_register_socket_inner(pid, client_id, inp, false);
3420 }
3421
3422 int
necp_client_register_socket_listener(pid_t pid,uuid_t client_id,struct inpcb * inp)3423 necp_client_register_socket_listener(pid_t pid, uuid_t client_id, struct inpcb *inp)
3424 {
3425 return necp_client_register_socket_inner(pid, client_id, inp, true);
3426 }
3427
3428 #if SKYWALK
3429 int
necp_client_get_netns_flow_info(uuid_t client_id,struct ns_flow_info * flow_info)3430 necp_client_get_netns_flow_info(uuid_t client_id, struct ns_flow_info *flow_info)
3431 {
3432 int error = 0;
3433 struct necp_fd_data *client_fd = NULL;
3434 bool found_client = FALSE;
3435
3436 NECP_FD_LIST_LOCK_SHARED();
3437 LIST_FOREACH(client_fd, &necp_fd_list, chain) {
3438 NECP_FD_LOCK(client_fd);
3439 struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
3440 if (client != NULL) {
3441 found_client = TRUE;
3442 if (!NETNS_TOKEN_VALID(&client->port_reservation)) {
3443 error = EINVAL;
3444 } else {
3445 error = netns_get_flow_info(&client->port_reservation, flow_info);
3446 }
3447
3448 NECP_CLIENT_UNLOCK(client);
3449 }
3450 NECP_FD_UNLOCK(client_fd);
3451
3452 if (found_client) {
3453 break;
3454 }
3455 }
3456 NECP_FD_LIST_UNLOCK();
3457
3458 if (!found_client) {
3459 error = ENOENT;
3460 }
3461
3462 return error;
3463 }
3464 #endif /* !SKYWALK */
3465
3466 static void
necp_client_add_multipath_interface_flows(struct necp_client_flow_registration * flow_registration,struct necp_client * client,struct mppcb * mpp)3467 necp_client_add_multipath_interface_flows(struct necp_client_flow_registration *flow_registration,
3468 struct necp_client *client,
3469 struct mppcb *mpp)
3470 {
3471 flow_registration->interface_handle = mpp;
3472 flow_registration->interface_cb = mpp->necp_cb;
3473
3474 proc_t proc = proc_find(client->proc_pid);
3475 if (proc == PROC_NULL) {
3476 return;
3477 }
3478
3479 // Traverse all interfaces and add a tracking flow if needed
3480 necp_flow_add_interface_flows(proc, client, flow_registration, true);
3481
3482 proc_rele(proc);
3483 proc = PROC_NULL;
3484 }
3485
3486 int
necp_client_register_multipath_cb(pid_t pid,uuid_t client_id,struct mppcb * mpp)3487 necp_client_register_multipath_cb(pid_t pid, uuid_t client_id, struct mppcb *mpp)
3488 {
3489 int error = 0;
3490 struct necp_fd_data *client_fd = NULL;
3491 bool found_client = FALSE;
3492
3493 NECP_FD_LIST_LOCK_SHARED();
3494 LIST_FOREACH(client_fd, &necp_fd_list, chain) {
3495 NECP_FD_LOCK(client_fd);
3496 struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
3497 if (client != NULL) {
3498 if (!pid || client->proc_pid == pid) {
3499 struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
3500 if (flow_registration != NULL) {
3501 // Found the right client and flow registration, add a new flow
3502 found_client = TRUE;
3503 necp_client_add_multipath_interface_flows(flow_registration, client, mpp);
3504 } else if (RB_EMPTY(&client->flow_registrations) && !necp_client_id_is_flow(client_id)) {
3505 // No flows yet on this client, add a new registration
3506 flow_registration = necp_client_create_flow_registration(client_fd, client);
3507 if (flow_registration == NULL) {
3508 error = ENOMEM;
3509 } else {
3510 // Add a new flow
3511 found_client = TRUE;
3512 necp_client_add_multipath_interface_flows(flow_registration, client, mpp);
3513 }
3514 }
3515 }
3516
3517 NECP_CLIENT_UNLOCK(client);
3518 }
3519 NECP_FD_UNLOCK(client_fd);
3520
3521 if (found_client) {
3522 break;
3523 }
3524 }
3525 NECP_FD_LIST_UNLOCK();
3526
3527 if (!found_client && error == 0) {
3528 error = ENOENT;
3529 }
3530
3531 return error;
3532 }
3533
3534 #define NETAGENT_DOMAIN_RADIO_MANAGER "WirelessRadioManager"
3535 #define NETAGENT_TYPE_RADIO_MANAGER "WirelessRadioManager:BB Manager"
3536
3537 static int
necp_client_lookup_bb_radio_manager(struct necp_client * client,uuid_t netagent_uuid)3538 necp_client_lookup_bb_radio_manager(struct necp_client *client,
3539 uuid_t netagent_uuid)
3540 {
3541 char netagent_domain[NETAGENT_DOMAINSIZE];
3542 char netagent_type[NETAGENT_TYPESIZE];
3543 struct necp_aggregate_result result;
3544 proc_t proc;
3545 int error;
3546
3547 proc = proc_find(client->proc_pid);
3548 if (proc == PROC_NULL) {
3549 return ESRCH;
3550 }
3551
3552 error = necp_application_find_policy_match_internal(proc, client->parameters, (u_int32_t)client->parameters_length,
3553 &result, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, true, true, NULL);
3554
3555 proc_rele(proc);
3556 proc = PROC_NULL;
3557
3558 if (error) {
3559 return error;
3560 }
3561
3562 for (int i = 0; i < NECP_MAX_NETAGENTS; i++) {
3563 if (uuid_is_null(result.netagents[i])) {
3564 // Passed end of valid agents
3565 break;
3566 }
3567
3568 memset(&netagent_domain, 0, NETAGENT_DOMAINSIZE);
3569 memset(&netagent_type, 0, NETAGENT_TYPESIZE);
3570 if (netagent_get_agent_domain_and_type(result.netagents[i], netagent_domain, netagent_type) == FALSE) {
3571 continue;
3572 }
3573
3574 if (strncmp(netagent_domain, NETAGENT_DOMAIN_RADIO_MANAGER, NETAGENT_DOMAINSIZE) != 0) {
3575 continue;
3576 }
3577
3578 if (strncmp(netagent_type, NETAGENT_TYPE_RADIO_MANAGER, NETAGENT_TYPESIZE) != 0) {
3579 continue;
3580 }
3581
3582 uuid_copy(netagent_uuid, result.netagents[i]);
3583
3584 break;
3585 }
3586
3587 return 0;
3588 }
3589
3590 static int
necp_client_assert_bb_radio_manager_common(struct necp_client * client,bool assert)3591 necp_client_assert_bb_radio_manager_common(struct necp_client *client, bool assert)
3592 {
3593 uuid_t netagent_uuid;
3594 uint8_t assert_type;
3595 int error;
3596
3597 error = necp_client_lookup_bb_radio_manager(client, netagent_uuid);
3598 if (error) {
3599 NECPLOG0(LOG_ERR, "BB radio manager agent not found");
3600 return error;
3601 }
3602
3603 // Before unasserting, verify that the assertion was already taken
3604 if (assert == FALSE) {
3605 assert_type = NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT;
3606
3607 if (!necp_client_remove_assertion(client, netagent_uuid)) {
3608 return EINVAL;
3609 }
3610 } else {
3611 assert_type = NETAGENT_MESSAGE_TYPE_CLIENT_ASSERT;
3612 }
3613
3614 error = netagent_client_message(netagent_uuid, client->client_id, client->proc_pid, client->agent_handle, assert_type);
3615 if (error) {
3616 NECPLOG0(LOG_ERR, "netagent_client_message failed");
3617 return error;
3618 }
3619
3620 // Only save the assertion if the action succeeded
3621 if (assert == TRUE) {
3622 necp_client_add_assertion(client, netagent_uuid);
3623 }
3624
3625 return 0;
3626 }
3627
3628 int
necp_client_assert_bb_radio_manager(uuid_t client_id,bool assert)3629 necp_client_assert_bb_radio_manager(uuid_t client_id, bool assert)
3630 {
3631 struct necp_client *client;
3632 int error = 0;
3633
3634 NECP_CLIENT_TREE_LOCK_SHARED();
3635
3636 client = necp_find_client_and_lock(client_id);
3637
3638 if (client) {
3639 // Found the right client!
3640 error = necp_client_assert_bb_radio_manager_common(client, assert);
3641
3642 NECP_CLIENT_UNLOCK(client);
3643 } else {
3644 NECPLOG0(LOG_ERR, "Couldn't find client");
3645 error = ENOENT;
3646 }
3647
3648 NECP_CLIENT_TREE_UNLOCK();
3649
3650 return error;
3651 }
3652
3653 static int
necp_client_unregister_socket_flow(uuid_t client_id,void * handle)3654 necp_client_unregister_socket_flow(uuid_t client_id, void *handle)
3655 {
3656 int error = 0;
3657 struct necp_fd_data *client_fd = NULL;
3658 bool found_client = FALSE;
3659 bool client_updated = FALSE;
3660
3661 NECP_FD_LIST_LOCK_SHARED();
3662 LIST_FOREACH(client_fd, &necp_fd_list, chain) {
3663 NECP_FD_LOCK(client_fd);
3664
3665 struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
3666 if (client != NULL) {
3667 struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
3668 if (flow_registration != NULL) {
3669 // Found the right client and flow!
3670 found_client = TRUE;
3671
3672 // Remove flow assignment
3673 struct necp_client_flow *search_flow = NULL;
3674 struct necp_client_flow *temp_flow = NULL;
3675 LIST_FOREACH_SAFE(search_flow, &flow_registration->flow_list, flow_chain, temp_flow) {
3676 if (search_flow->socket && search_flow->u.socket_handle == handle) {
3677 if (search_flow->assigned_results != NULL) {
3678 kfree_data(search_flow->assigned_results, search_flow->assigned_results_length);
3679 search_flow->assigned_results = NULL;
3680 }
3681 client_updated = TRUE;
3682 flow_registration->flow_result_read = FALSE;
3683 LIST_REMOVE(search_flow, flow_chain);
3684 OSDecrementAtomic(&necp_socket_flow_count);
3685 kfree_type(struct necp_client_flow, search_flow);
3686 }
3687 }
3688 }
3689
3690 NECP_CLIENT_UNLOCK(client);
3691 }
3692
3693 if (client_updated) {
3694 necp_fd_notify(client_fd, true);
3695 }
3696 NECP_FD_UNLOCK(client_fd);
3697
3698 if (found_client) {
3699 break;
3700 }
3701 }
3702 NECP_FD_LIST_UNLOCK();
3703
3704 if (!found_client) {
3705 error = ENOENT;
3706 }
3707
3708 return error;
3709 }
3710
3711 static int
necp_client_unregister_multipath_cb(uuid_t client_id,void * handle)3712 necp_client_unregister_multipath_cb(uuid_t client_id, void *handle)
3713 {
3714 int error = 0;
3715 bool found_client = FALSE;
3716
3717 NECP_CLIENT_TREE_LOCK_SHARED();
3718
3719 struct necp_client *client = necp_find_client_and_lock(client_id);
3720 if (client != NULL) {
3721 struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
3722 if (flow_registration != NULL) {
3723 // Found the right client and flow!
3724 found_client = TRUE;
3725
3726 // Remove flow assignment
3727 struct necp_client_flow *search_flow = NULL;
3728 struct necp_client_flow *temp_flow = NULL;
3729 LIST_FOREACH_SAFE(search_flow, &flow_registration->flow_list, flow_chain, temp_flow) {
3730 if (!search_flow->socket && !search_flow->nexus &&
3731 search_flow->u.socket_handle == handle) {
3732 search_flow->u.socket_handle = NULL;
3733 search_flow->u.cb = NULL;
3734 }
3735 }
3736
3737 flow_registration->interface_handle = NULL;
3738 flow_registration->interface_cb = NULL;
3739 }
3740
3741 NECP_CLIENT_UNLOCK(client);
3742 }
3743
3744 NECP_CLIENT_TREE_UNLOCK();
3745
3746 if (!found_client) {
3747 error = ENOENT;
3748 }
3749
3750 return error;
3751 }
3752
3753 int
necp_client_assign_from_socket(pid_t pid,uuid_t client_id,struct inpcb * inp)3754 necp_client_assign_from_socket(pid_t pid, uuid_t client_id, struct inpcb *inp)
3755 {
3756 int error = 0;
3757 struct necp_fd_data *client_fd = NULL;
3758 bool found_client = FALSE;
3759 bool client_updated = FALSE;
3760
3761 NECP_FD_LIST_LOCK_SHARED();
3762 LIST_FOREACH(client_fd, &necp_fd_list, chain) {
3763 if (pid && client_fd->proc_pid != pid) {
3764 continue;
3765 }
3766
3767 proc_t proc = proc_find(client_fd->proc_pid);
3768 if (proc == PROC_NULL) {
3769 continue;
3770 }
3771
3772 NECP_FD_LOCK(client_fd);
3773
3774 struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
3775 if (client != NULL) {
3776 struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
3777 if (flow_registration == NULL && RB_EMPTY(&client->flow_registrations) && !necp_client_id_is_flow(client_id)) {
3778 // No flows yet on this client, add a new registration
3779 flow_registration = necp_client_create_flow_registration(client_fd, client);
3780 if (flow_registration == NULL) {
3781 error = ENOMEM;
3782 }
3783 }
3784 if (flow_registration != NULL) {
3785 // Found the right client and flow!
3786 found_client = TRUE;
3787
3788 struct necp_client_flow *flow = NULL;
3789 LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
3790 if (flow->socket && flow->u.socket_handle == inp) {
3791 // Release prior results and route
3792 if (flow->assigned_results != NULL) {
3793 kfree_data(flow->assigned_results, flow->assigned_results_length);
3794 flow->assigned_results = NULL;
3795 }
3796
3797 ifnet_t ifp = NULL;
3798 if ((inp->inp_flags & INP_BOUND_IF) && inp->inp_boundifp) {
3799 ifp = inp->inp_boundifp;
3800 } else {
3801 ifp = inp->inp_last_outifp;
3802 }
3803
3804 if (ifp != NULL) {
3805 flow->interface_index = ifp->if_index;
3806 } else {
3807 flow->interface_index = IFSCOPE_NONE;
3808 }
3809
3810 if (inp->inp_vflag & INP_IPV4) {
3811 flow->local_addr.sin.sin_family = AF_INET;
3812 flow->local_addr.sin.sin_len = sizeof(struct sockaddr_in);
3813 flow->local_addr.sin.sin_port = inp->inp_lport;
3814 memcpy(&flow->local_addr.sin.sin_addr, &inp->inp_laddr, sizeof(struct in_addr));
3815
3816 flow->remote_addr.sin.sin_family = AF_INET;
3817 flow->remote_addr.sin.sin_len = sizeof(struct sockaddr_in);
3818 flow->remote_addr.sin.sin_port = inp->inp_fport;
3819 memcpy(&flow->remote_addr.sin.sin_addr, &inp->inp_faddr, sizeof(struct in_addr));
3820 } else if (inp->inp_vflag & INP_IPV6) {
3821 in6_ip6_to_sockaddr(&inp->in6p_laddr, inp->inp_lport, inp->inp_lifscope, &flow->local_addr.sin6, sizeof(flow->local_addr));
3822 in6_ip6_to_sockaddr(&inp->in6p_faddr, inp->inp_fport, inp->inp_fifscope, &flow->remote_addr.sin6, sizeof(flow->remote_addr));
3823 }
3824
3825 flow->viable = necp_client_flow_is_viable(proc, client, flow);
3826
3827 uuid_t empty_uuid;
3828 uuid_clear(empty_uuid);
3829 flow->assigned = TRUE;
3830 flow->assigned_results = necp_create_nexus_assign_message(empty_uuid, 0, NULL, 0,
3831 (struct necp_client_endpoint *)&flow->local_addr,
3832 (struct necp_client_endpoint *)&flow->remote_addr,
3833 NULL, 0, NULL, &flow->assigned_results_length);
3834 flow_registration->flow_result_read = FALSE;
3835 client_updated = TRUE;
3836 break;
3837 }
3838 }
3839 }
3840
3841 NECP_CLIENT_UNLOCK(client);
3842 }
3843 if (client_updated) {
3844 necp_fd_notify(client_fd, true);
3845 }
3846 NECP_FD_UNLOCK(client_fd);
3847
3848 proc_rele(proc);
3849 proc = PROC_NULL;
3850
3851 if (found_client) {
3852 break;
3853 }
3854 }
3855 NECP_FD_LIST_UNLOCK();
3856
3857 if (error == 0) {
3858 if (!found_client) {
3859 error = ENOENT;
3860 } else if (!client_updated) {
3861 error = EINVAL;
3862 }
3863 }
3864
3865 return error;
3866 }
3867
3868 bool
necp_socket_is_allowed_to_recv_on_interface(struct inpcb * inp,ifnet_t interface)3869 necp_socket_is_allowed_to_recv_on_interface(struct inpcb *inp, ifnet_t interface)
3870 {
3871 if (interface == NULL ||
3872 inp == NULL ||
3873 !(inp->inp_flags2 & INP2_EXTERNAL_PORT) ||
3874 uuid_is_null(inp->necp_client_uuid)) {
3875 // If there's no interface or client ID to check,
3876 // or if this is not a listener, pass.
3877 // Outbound connections will have already been
3878 // validated for policy.
3879 return TRUE;
3880 }
3881
3882 // Only filter out listener sockets (no remote address specified)
3883 if ((inp->inp_vflag & INP_IPV4) &&
3884 inp->inp_faddr.s_addr != INADDR_ANY) {
3885 return TRUE;
3886 }
3887 if ((inp->inp_vflag & INP_IPV6) &&
3888 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
3889 return TRUE;
3890 }
3891
3892 bool allowed = TRUE;
3893
3894 NECP_CLIENT_TREE_LOCK_SHARED();
3895
3896 struct necp_client *client = necp_find_client_and_lock(inp->necp_client_uuid);
3897 if (client != NULL) {
3898 struct necp_client_parsed_parameters *parsed_parameters = NULL;
3899
3900 parsed_parameters = kalloc_type(struct necp_client_parsed_parameters,
3901 Z_WAITOK | Z_ZERO | Z_NOFAIL);
3902 int error = necp_client_parse_parameters(client, client->parameters, (u_int32_t)client->parameters_length, parsed_parameters);
3903 if (error == 0) {
3904 if (!necp_ifnet_matches_parameters(interface, parsed_parameters, 0, NULL, true, false)) {
3905 allowed = FALSE;
3906 }
3907 }
3908 kfree_type(struct necp_client_parsed_parameters, parsed_parameters);
3909
3910 NECP_CLIENT_UNLOCK(client);
3911 }
3912
3913 NECP_CLIENT_TREE_UNLOCK();
3914
3915 return allowed;
3916 }
3917
3918 int
necp_update_flow_protoctl_event(uuid_t netagent_uuid,uuid_t client_id,uint32_t protoctl_event_code,uint32_t protoctl_event_val,uint32_t protoctl_event_tcp_seq_number)3919 necp_update_flow_protoctl_event(uuid_t netagent_uuid, uuid_t client_id,
3920 uint32_t protoctl_event_code, uint32_t protoctl_event_val,
3921 uint32_t protoctl_event_tcp_seq_number)
3922 {
3923 int error = 0;
3924 struct necp_fd_data *client_fd = NULL;
3925 bool found_client = FALSE;
3926 bool client_updated = FALSE;
3927
3928 NECP_FD_LIST_LOCK_SHARED();
3929 LIST_FOREACH(client_fd, &necp_fd_list, chain) {
3930 proc_t proc = proc_find(client_fd->proc_pid);
3931 if (proc == PROC_NULL) {
3932 continue;
3933 }
3934
3935 NECP_FD_LOCK(client_fd);
3936
3937 struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
3938 if (client != NULL) {
3939 struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
3940 if (flow_registration != NULL) {
3941 // Found the right client and flow!
3942 found_client = TRUE;
3943
3944 struct necp_client_flow *flow = NULL;
3945 LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
3946 // Verify that the client nexus agent matches
3947 if ((flow->nexus && uuid_compare(flow->u.nexus_agent, netagent_uuid) == 0) ||
3948 flow->socket) {
3949 flow->has_protoctl_event = TRUE;
3950 flow->protoctl_event.protoctl_event_code = protoctl_event_code;
3951 flow->protoctl_event.protoctl_event_val = protoctl_event_val;
3952 flow->protoctl_event.protoctl_event_tcp_seq_num = protoctl_event_tcp_seq_number;
3953 flow_registration->flow_result_read = FALSE;
3954 client_updated = TRUE;
3955 break;
3956 }
3957 }
3958 }
3959
3960 NECP_CLIENT_UNLOCK(client);
3961 }
3962
3963 if (client_updated) {
3964 necp_fd_notify(client_fd, true);
3965 }
3966
3967 NECP_FD_UNLOCK(client_fd);
3968 proc_rele(proc);
3969 proc = PROC_NULL;
3970
3971 if (found_client) {
3972 break;
3973 }
3974 }
3975 NECP_FD_LIST_UNLOCK();
3976
3977 if (!found_client) {
3978 error = ENOENT;
3979 } else if (!client_updated) {
3980 error = EINVAL;
3981 }
3982 return error;
3983 }
3984
3985 static bool
necp_assign_client_result_locked(struct proc * proc,struct necp_fd_data * client_fd,struct necp_client * client,struct necp_client_flow_registration * flow_registration,uuid_t netagent_uuid,u_int8_t * assigned_results,size_t assigned_results_length,bool notify_fd,bool assigned_from_userspace_agent)3986 necp_assign_client_result_locked(struct proc *proc,
3987 struct necp_fd_data *client_fd,
3988 struct necp_client *client,
3989 struct necp_client_flow_registration *flow_registration,
3990 uuid_t netagent_uuid,
3991 u_int8_t *assigned_results,
3992 size_t assigned_results_length,
3993 bool notify_fd,
3994 bool assigned_from_userspace_agent)
3995 {
3996 bool client_updated = FALSE;
3997
3998 NECP_FD_ASSERT_LOCKED(client_fd);
3999 NECP_CLIENT_ASSERT_LOCKED(client);
4000
4001 struct necp_client_flow *flow = NULL;
4002 LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
4003 // Verify that the client nexus agent matches
4004 if (flow->nexus &&
4005 uuid_compare(flow->u.nexus_agent, netagent_uuid) == 0) {
4006 // Release prior results and route
4007 if (flow->assigned_results != NULL) {
4008 kfree_data(flow->assigned_results, flow->assigned_results_length);
4009 flow->assigned_results = NULL;
4010 }
4011
4012 void *nexus_stats = NULL;
4013 if (assigned_results != NULL && assigned_results_length > 0) {
4014 int error = necp_client_parse_result(assigned_results, (u_int32_t)assigned_results_length,
4015 &flow->local_addr, &flow->remote_addr,
4016 assigned_from_userspace_agent ? NULL : &nexus_stats); // Only assign stats from kernel agents
4017 VERIFY(error == 0);
4018 }
4019
4020 flow->viable = necp_client_flow_is_viable(proc, client, flow);
4021
4022 flow->assigned = TRUE;
4023 flow->assigned_results = assigned_results;
4024 flow->assigned_results_length = assigned_results_length;
4025 flow_registration->flow_result_read = FALSE;
4026 #if SKYWALK
4027 if (nexus_stats != NULL) {
4028 if (flow_registration->nexus_stats != NULL) {
4029 flow_stats_release(flow_registration->nexus_stats);
4030 }
4031 flow_registration->nexus_stats = nexus_stats;
4032 }
4033 #endif /* SKYWALK */
4034 client_updated = TRUE;
4035 break;
4036 }
4037 }
4038
4039 if (client_updated && notify_fd) {
4040 necp_fd_notify(client_fd, true);
4041 }
4042
4043 // if not updated, client must free assigned_results
4044 return client_updated;
4045 }
4046
4047 int
necp_assign_client_result(uuid_t netagent_uuid,uuid_t client_id,u_int8_t * assigned_results,size_t assigned_results_length)4048 necp_assign_client_result(uuid_t netagent_uuid, uuid_t client_id,
4049 u_int8_t *assigned_results, size_t assigned_results_length)
4050 {
4051 int error = 0;
4052 struct necp_fd_data *client_fd = NULL;
4053 bool found_client = FALSE;
4054 bool client_updated = FALSE;
4055
4056 NECP_FD_LIST_LOCK_SHARED();
4057
4058 LIST_FOREACH(client_fd, &necp_fd_list, chain) {
4059 proc_t proc = proc_find(client_fd->proc_pid);
4060 if (proc == PROC_NULL) {
4061 continue;
4062 }
4063
4064 NECP_FD_LOCK(client_fd);
4065 struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
4066 if (client != NULL) {
4067 struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
4068 if (flow_registration != NULL) {
4069 // Found the right client and flow!
4070 found_client = TRUE;
4071 if (necp_assign_client_result_locked(proc, client_fd, client, flow_registration, netagent_uuid,
4072 assigned_results, assigned_results_length, true, true)) {
4073 client_updated = TRUE;
4074 }
4075 }
4076
4077 NECP_CLIENT_UNLOCK(client);
4078 }
4079 NECP_FD_UNLOCK(client_fd);
4080
4081 proc_rele(proc);
4082 proc = PROC_NULL;
4083
4084 if (found_client) {
4085 break;
4086 }
4087 }
4088
4089 NECP_FD_LIST_UNLOCK();
4090
4091 // upon error, client must free assigned_results
4092 if (!found_client) {
4093 error = ENOENT;
4094 } else if (!client_updated) {
4095 error = EINVAL;
4096 }
4097
4098 return error;
4099 }
4100
4101 int
necp_assign_client_group_members(uuid_t netagent_uuid,uuid_t client_id,u_int8_t * assigned_group_members,size_t assigned_group_members_length)4102 necp_assign_client_group_members(uuid_t netagent_uuid, uuid_t client_id,
4103 u_int8_t *assigned_group_members, size_t assigned_group_members_length)
4104 {
4105 #pragma unused(netagent_uuid)
4106 int error = 0;
4107 struct necp_fd_data *client_fd = NULL;
4108 bool found_client = false;
4109 bool client_updated = false;
4110
4111 NECP_FD_LIST_LOCK_SHARED();
4112
4113 LIST_FOREACH(client_fd, &necp_fd_list, chain) {
4114 proc_t proc = proc_find(client_fd->proc_pid);
4115 if (proc == PROC_NULL) {
4116 continue;
4117 }
4118
4119 NECP_FD_LOCK(client_fd);
4120 struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
4121 if (client != NULL) {
4122 found_client = true;
4123 // Release prior results
4124 if (client->assigned_group_members != NULL) {
4125 kfree_data(client->assigned_group_members, client->assigned_group_members_length);
4126 client->assigned_group_members = NULL;
4127 }
4128
4129 // Save new results
4130 client->assigned_group_members = assigned_group_members;
4131 client->assigned_group_members_length = assigned_group_members_length;
4132 client->group_members_read = false;
4133
4134 client_updated = true;
4135 necp_fd_notify(client_fd, true);
4136
4137 NECP_CLIENT_UNLOCK(client);
4138 }
4139 NECP_FD_UNLOCK(client_fd);
4140
4141 proc_rele(proc);
4142 proc = PROC_NULL;
4143
4144 if (found_client) {
4145 break;
4146 }
4147 }
4148
4149 NECP_FD_LIST_UNLOCK();
4150
4151 // upon error, client must free assigned_results
4152 if (!found_client) {
4153 error = ENOENT;
4154 } else if (!client_updated) {
4155 error = EINVAL;
4156 }
4157
4158 return error;
4159 }
4160
4161 /// Client updating
4162
4163 static bool
necp_update_parsed_parameters(struct necp_client_parsed_parameters * parsed_parameters,struct necp_aggregate_result * result)4164 necp_update_parsed_parameters(struct necp_client_parsed_parameters *parsed_parameters,
4165 struct necp_aggregate_result *result)
4166 {
4167 if (parsed_parameters == NULL ||
4168 result == NULL) {
4169 return false;
4170 }
4171
4172 bool updated = false;
4173 for (int i = 0; i < NECP_MAX_NETAGENTS; i++) {
4174 if (uuid_is_null(result->netagents[i])) {
4175 // Passed end of valid agents
4176 break;
4177 }
4178
4179 if (!(result->netagent_use_flags[i] & NECP_AGENT_USE_FLAG_SCOPE)) {
4180 // Not a scoped agent, ignore
4181 continue;
4182 }
4183
4184 // This is a scoped agent. Add it to the required agents.
4185 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT) {
4186 // Already some required agents, add this at the end
4187 for (int j = 0; j < NECP_MAX_AGENT_PARAMETERS; j++) {
4188 if (uuid_compare(parsed_parameters->required_netagents[j], result->netagents[i]) == 0) {
4189 // Already required, break
4190 break;
4191 }
4192 if (uuid_is_null(parsed_parameters->required_netagents[j])) {
4193 // Add here
4194 memcpy(&parsed_parameters->required_netagents[j], result->netagents[i], sizeof(uuid_t));
4195 updated = true;
4196 break;
4197 }
4198 }
4199 } else {
4200 // No required agents yet, add this one
4201 parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT;
4202 memcpy(&parsed_parameters->required_netagents[0], result->netagents[i], sizeof(uuid_t));
4203 updated = true;
4204 }
4205
4206 // Remove requirements for agents of the same type
4207 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE) {
4208 char remove_agent_domain[NETAGENT_DOMAINSIZE] = { 0 };
4209 char remove_agent_type[NETAGENT_TYPESIZE] = { 0 };
4210 if (netagent_get_agent_domain_and_type(result->netagents[i], remove_agent_domain, remove_agent_type)) {
4211 for (int j = 0; j < NECP_MAX_AGENT_PARAMETERS; j++) {
4212 if (strlen(parsed_parameters->required_netagent_types[j].netagent_domain) == 0 &&
4213 strlen(parsed_parameters->required_netagent_types[j].netagent_type) == 0) {
4214 break;
4215 }
4216
4217 if (strncmp(parsed_parameters->required_netagent_types[j].netagent_domain, remove_agent_domain, NETAGENT_DOMAINSIZE) == 0 &&
4218 strncmp(parsed_parameters->required_netagent_types[j].netagent_type, remove_agent_type, NETAGENT_TYPESIZE) == 0) {
4219 updated = true;
4220
4221 if (j == NECP_MAX_AGENT_PARAMETERS - 1) {
4222 // Last field, just clear and break
4223 memset(&parsed_parameters->required_netagent_types[NECP_MAX_AGENT_PARAMETERS - 1], 0, sizeof(struct necp_client_parameter_netagent_type));
4224 break;
4225 } else {
4226 // Move the parameters down, clear the last entry
4227 memmove(&parsed_parameters->required_netagent_types[j],
4228 &parsed_parameters->required_netagent_types[j + 1],
4229 sizeof(struct necp_client_parameter_netagent_type) * (NECP_MAX_AGENT_PARAMETERS - (j + 1)));
4230 memset(&parsed_parameters->required_netagent_types[NECP_MAX_AGENT_PARAMETERS - 1], 0, sizeof(struct necp_client_parameter_netagent_type));
4231 // Continue, don't increment but look at the new shifted item instead
4232 continue;
4233 }
4234 }
4235
4236 // Increment j to look at the next agent type parameter
4237 j++;
4238 }
4239 }
4240 }
4241 }
4242
4243 if (updated &&
4244 parsed_parameters->required_interface_index != IFSCOPE_NONE &&
4245 (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF) == 0) {
4246 // A required interface index was added after the fact. Clear it.
4247 parsed_parameters->required_interface_index = IFSCOPE_NONE;
4248 }
4249
4250
4251 return updated;
4252 }
4253
4254 static inline bool
necp_agent_types_match(const char * agent_domain1,const char * agent_type1,const char * agent_domain2,const char * agent_type2)4255 necp_agent_types_match(const char *agent_domain1, const char *agent_type1,
4256 const char *agent_domain2, const char *agent_type2)
4257 {
4258 return (strlen(agent_domain1) == 0 ||
4259 strncmp(agent_domain2, agent_domain1, NETAGENT_DOMAINSIZE) == 0) &&
4260 (strlen(agent_type1) == 0 ||
4261 strncmp(agent_type2, agent_type1, NETAGENT_TYPESIZE) == 0);
4262 }
4263
4264 static inline bool
necp_calculate_client_result(proc_t proc,struct necp_client * client,struct necp_client_parsed_parameters * parsed_parameters,struct necp_aggregate_result * result,u_int32_t * flags,u_int32_t * reason,struct necp_client_endpoint * v4_gateway,struct necp_client_endpoint * v6_gateway,uuid_t * override_euuid)4265 necp_calculate_client_result(proc_t proc,
4266 struct necp_client *client,
4267 struct necp_client_parsed_parameters *parsed_parameters,
4268 struct necp_aggregate_result *result,
4269 u_int32_t *flags,
4270 u_int32_t *reason,
4271 struct necp_client_endpoint *v4_gateway,
4272 struct necp_client_endpoint *v6_gateway,
4273 uuid_t *override_euuid)
4274 {
4275 struct rtentry *route = NULL;
4276
4277 // Check parameters to find best interface
4278 bool validate_agents = false;
4279 u_int matching_if_index = 0;
4280 if (necp_find_matching_interface_index(parsed_parameters, &matching_if_index, &validate_agents)) {
4281 if (matching_if_index != 0) {
4282 parsed_parameters->required_interface_index = matching_if_index;
4283 }
4284 // Interface found or not needed, match policy.
4285 memset(result, 0, sizeof(*result));
4286 int error = necp_application_find_policy_match_internal(proc, client->parameters,
4287 (u_int32_t)client->parameters_length,
4288 result, flags, reason, matching_if_index,
4289 NULL, NULL,
4290 v4_gateway, v6_gateway,
4291 &route, false, true,
4292 override_euuid);
4293 if (error != 0) {
4294 if (route != NULL) {
4295 rtfree(route);
4296 }
4297 return FALSE;
4298 }
4299
4300 if (validate_agents) {
4301 bool requirement_failed = FALSE;
4302 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT) {
4303 for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
4304 if (uuid_is_null(parsed_parameters->required_netagents[i])) {
4305 break;
4306 }
4307
4308 bool requirement_found = FALSE;
4309 for (int j = 0; j < NECP_MAX_NETAGENTS; j++) {
4310 if (uuid_is_null(result->netagents[j])) {
4311 break;
4312 }
4313
4314 if (result->netagent_use_flags[j] & NECP_AGENT_USE_FLAG_REMOVE) {
4315 // A removed agent, ignore
4316 continue;
4317 }
4318
4319 if (uuid_compare(parsed_parameters->required_netagents[i], result->netagents[j]) == 0) {
4320 requirement_found = TRUE;
4321 break;
4322 }
4323 }
4324
4325 if (!requirement_found) {
4326 requirement_failed = TRUE;
4327 break;
4328 }
4329 }
4330 }
4331
4332 if (!requirement_failed && parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE) {
4333 for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
4334 if (strlen(parsed_parameters->required_netagent_types[i].netagent_domain) == 0 &&
4335 strlen(parsed_parameters->required_netagent_types[i].netagent_type) == 0) {
4336 break;
4337 }
4338
4339 bool requirement_found = FALSE;
4340 for (int j = 0; j < NECP_MAX_NETAGENTS; j++) {
4341 if (uuid_is_null(result->netagents[j])) {
4342 break;
4343 }
4344
4345 if (result->netagent_use_flags[j] & NECP_AGENT_USE_FLAG_REMOVE) {
4346 // A removed agent, ignore
4347 continue;
4348 }
4349
4350 char policy_agent_domain[NETAGENT_DOMAINSIZE] = { 0 };
4351 char policy_agent_type[NETAGENT_TYPESIZE] = { 0 };
4352
4353 if (netagent_get_agent_domain_and_type(result->netagents[j], policy_agent_domain, policy_agent_type)) {
4354 if (necp_agent_types_match(parsed_parameters->required_netagent_types[i].netagent_domain,
4355 parsed_parameters->required_netagent_types[i].netagent_type,
4356 policy_agent_domain, policy_agent_type)) {
4357 requirement_found = TRUE;
4358 break;
4359 }
4360 }
4361 }
4362
4363 if (!requirement_found) {
4364 requirement_failed = TRUE;
4365 break;
4366 }
4367 }
4368 }
4369
4370 if (requirement_failed) {
4371 // Agent requirement failed. Clear out the whole result, make everything fail.
4372 memset(result, 0, sizeof(*result));
4373 if (route != NULL) {
4374 rtfree(route);
4375 }
4376 return TRUE;
4377 }
4378 }
4379
4380 // Reset current route
4381 NECP_CLIENT_ROUTE_LOCK(client);
4382 if (client->current_route != NULL) {
4383 rtfree(client->current_route);
4384 }
4385 client->current_route = route;
4386 NECP_CLIENT_ROUTE_UNLOCK(client);
4387 } else {
4388 // Interface not found. Clear out the whole result, make everything fail.
4389 memset(result, 0, sizeof(*result));
4390 }
4391
4392 return TRUE;
4393 }
4394
4395 #define NECP_PARSED_PARAMETERS_REQUIRED_FIELDS (NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF | \
4396 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE | \
4397 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT | \
4398 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE)
4399
4400 static bool
necp_update_client_result(proc_t proc,struct necp_fd_data * client_fd,struct necp_client * client,struct _necp_flow_defunct_list * defunct_list)4401 necp_update_client_result(proc_t proc,
4402 struct necp_fd_data *client_fd,
4403 struct necp_client *client,
4404 struct _necp_flow_defunct_list *defunct_list)
4405 {
4406 struct necp_client_result_netagent netagent;
4407 struct necp_aggregate_result result;
4408 struct necp_client_parsed_parameters *parsed_parameters = NULL;
4409 u_int32_t flags = 0;
4410 u_int32_t reason = 0;
4411
4412 NECP_CLIENT_ASSERT_LOCKED(client);
4413
4414 parsed_parameters = kalloc_type(struct necp_client_parsed_parameters,
4415 Z_WAITOK | Z_ZERO | Z_NOFAIL);
4416
4417 // Nexus flows will be brought back if they are still valid
4418 necp_client_mark_all_nonsocket_flows_as_invalid(client);
4419
4420 int error = necp_client_parse_parameters(client, client->parameters, (u_int32_t)client->parameters_length, parsed_parameters);
4421 if (error != 0) {
4422 kfree_type(struct necp_client_parsed_parameters, parsed_parameters);
4423 return FALSE;
4424 }
4425
4426 // Update saved IP protocol
4427 client->ip_protocol = parsed_parameters->ip_protocol;
4428
4429 // Calculate the policy result
4430 struct necp_client_endpoint v4_gateway = {};
4431 struct necp_client_endpoint v6_gateway = {};
4432 uuid_t override_euuid;
4433 uuid_clear(override_euuid);
4434 if (!necp_calculate_client_result(proc, client, parsed_parameters, &result, &flags, &reason, &v4_gateway, &v6_gateway, &override_euuid)) {
4435 kfree_type(struct necp_client_parsed_parameters, parsed_parameters);
4436 return FALSE;
4437 }
4438
4439 if (necp_update_parsed_parameters(parsed_parameters, &result)) {
4440 // Changed the parameters based on result, try again (only once)
4441 if (!necp_calculate_client_result(proc, client, parsed_parameters, &result, &flags, &reason, &v4_gateway, &v6_gateway, &override_euuid)) {
4442 kfree_type(struct necp_client_parsed_parameters, parsed_parameters);
4443 return FALSE;
4444 }
4445 }
4446
4447 if ((parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) &&
4448 parsed_parameters->required_interface_index != IFSCOPE_NONE &&
4449 (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF) == 0) {
4450 // Listener should not apply required interface index if
4451 parsed_parameters->required_interface_index = IFSCOPE_NONE;
4452 }
4453
4454 // Save the last policy id on the client
4455 client->policy_id = result.policy_id;
4456 uuid_copy(client->override_euuid, override_euuid);
4457
4458 if ((parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_MULTIPATH) ||
4459 (parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_BROWSE) ||
4460 ((parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) &&
4461 result.routing_result != NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED)) {
4462 client->allow_multiple_flows = TRUE;
4463 } else {
4464 client->allow_multiple_flows = FALSE;
4465 }
4466
4467 // If the original request was scoped, and the policy result matches, make sure the result is scoped
4468 if ((result.routing_result == NECP_KERNEL_POLICY_RESULT_NONE ||
4469 result.routing_result == NECP_KERNEL_POLICY_RESULT_PASS) &&
4470 result.routed_interface_index != IFSCOPE_NONE &&
4471 parsed_parameters->required_interface_index == result.routed_interface_index) {
4472 result.routing_result = NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED;
4473 result.routing_result_parameter.scoped_interface_index = result.routed_interface_index;
4474 }
4475
4476 if (defunct_list != NULL &&
4477 result.routing_result == NECP_KERNEL_POLICY_RESULT_DROP) {
4478 // If we are forced to drop the client, defunct it if it has flows
4479 necp_defunct_client_for_policy(client, defunct_list);
4480 }
4481
4482 // Recalculate flags
4483 if (parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) {
4484 // Listeners are valid as long as they aren't dropped
4485 if (result.routing_result != NECP_KERNEL_POLICY_RESULT_DROP) {
4486 flags |= NECP_CLIENT_RESULT_FLAG_SATISFIED;
4487 }
4488 } else if (result.routed_interface_index != 0) {
4489 // Clients without flows determine viability based on having some routable interface
4490 flags |= NECP_CLIENT_RESULT_FLAG_SATISFIED;
4491 }
4492
4493 bool updated = FALSE;
4494 u_int8_t *cursor = client->result;
4495 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_FLAGS, sizeof(flags), &flags, &updated, client->result, sizeof(client->result));
4496 if (reason != 0) {
4497 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_REASON, sizeof(reason), &reason, &updated, client->result, sizeof(client->result));
4498 }
4499 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_CLIENT_ID, sizeof(uuid_t), client->client_id, &updated,
4500 client->result, sizeof(client->result));
4501 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_POLICY_RESULT, sizeof(result.routing_result), &result.routing_result, &updated,
4502 client->result, sizeof(client->result));
4503 if (result.routing_result_parameter.tunnel_interface_index != 0) {
4504 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_POLICY_RESULT_PARAMETER,
4505 sizeof(result.routing_result_parameter), &result.routing_result_parameter, &updated,
4506 client->result, sizeof(client->result));
4507 }
4508 if (result.filter_control_unit != 0) {
4509 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_FILTER_CONTROL_UNIT,
4510 sizeof(result.filter_control_unit), &result.filter_control_unit, &updated,
4511 client->result, sizeof(client->result));
4512 }
4513 if (result.flow_divert_aggregate_unit != 0) {
4514 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_FLOW_DIVERT_AGGREGATE_UNIT,
4515 sizeof(result.flow_divert_aggregate_unit), &result.flow_divert_aggregate_unit, &updated,
4516 client->result, sizeof(client->result));
4517 }
4518 if (result.routed_interface_index != 0) {
4519 u_int routed_interface_index = result.routed_interface_index;
4520 if (result.routing_result == NECP_KERNEL_POLICY_RESULT_IP_TUNNEL &&
4521 (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_REQUIRED_FIELDS) &&
4522 parsed_parameters->required_interface_index != IFSCOPE_NONE &&
4523 parsed_parameters->required_interface_index != result.routed_interface_index) {
4524 routed_interface_index = parsed_parameters->required_interface_index;
4525 }
4526
4527 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_INTERFACE_INDEX,
4528 sizeof(routed_interface_index), &routed_interface_index, &updated,
4529 client->result, sizeof(client->result));
4530 }
4531 if (client_fd && client_fd->flags & NECP_OPEN_FLAG_BACKGROUND) {
4532 u_int32_t effective_traffic_class = SO_TC_BK_SYS;
4533 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_EFFECTIVE_TRAFFIC_CLASS,
4534 sizeof(effective_traffic_class), &effective_traffic_class, &updated,
4535 client->result, sizeof(client->result));
4536 }
4537
4538 if (client_fd->background) {
4539 bool has_assigned_flow = FALSE;
4540 struct necp_client_flow_registration *flow_registration = NULL;
4541 struct necp_client_flow *search_flow = NULL;
4542 RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
4543 LIST_FOREACH(search_flow, &flow_registration->flow_list, flow_chain) {
4544 if (search_flow->assigned) {
4545 has_assigned_flow = TRUE;
4546 break;
4547 }
4548 }
4549 }
4550
4551 if (has_assigned_flow) {
4552 u_int32_t background = client_fd->background;
4553 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_TRAFFIC_MGMT_BG,
4554 sizeof(background), &background, &updated,
4555 client->result, sizeof(client->result));
4556 }
4557 }
4558
4559 bool write_v4_gateway = !necp_client_endpoint_is_unspecified(&v4_gateway);
4560 bool write_v6_gateway = !necp_client_endpoint_is_unspecified(&v6_gateway);
4561
4562 NECP_CLIENT_ROUTE_LOCK(client);
4563 if (client->current_route != NULL) {
4564 const u_int32_t route_mtu = get_maxmtu(client->current_route);
4565 if (route_mtu != 0) {
4566 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_EFFECTIVE_MTU,
4567 sizeof(route_mtu), &route_mtu, &updated,
4568 client->result, sizeof(client->result));
4569 }
4570 bool has_remote_addr = parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REMOTE_ADDR;
4571 if (has_remote_addr && client->current_route->rt_gateway != NULL) {
4572 if (client->current_route->rt_gateway->sa_family == AF_INET) {
4573 write_v6_gateway = false;
4574 } else if (client->current_route->rt_gateway->sa_family == AF_INET6) {
4575 write_v4_gateway = false;
4576 }
4577 }
4578 }
4579 NECP_CLIENT_ROUTE_UNLOCK(client);
4580
4581 if (write_v4_gateway) {
4582 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_GATEWAY,
4583 sizeof(struct necp_client_endpoint), &v4_gateway, &updated,
4584 client->result, sizeof(client->result));
4585 }
4586
4587 if (write_v6_gateway) {
4588 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_GATEWAY,
4589 sizeof(struct necp_client_endpoint), &v6_gateway, &updated,
4590 client->result, sizeof(client->result));
4591 }
4592
4593 for (int i = 0; i < NAT64_MAX_NUM_PREFIXES; i++) {
4594 if (result.nat64_prefixes[i].prefix_len != 0) {
4595 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_NAT64,
4596 sizeof(result.nat64_prefixes), result.nat64_prefixes, &updated,
4597 client->result, sizeof(client->result));
4598 break;
4599 }
4600 }
4601
4602 if (result.mss_recommended != 0) {
4603 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_RECOMMENDED_MSS,
4604 sizeof(result.mss_recommended), &result.mss_recommended, &updated,
4605 client->result, sizeof(client->result));
4606 }
4607
4608 for (int i = 0; i < NECP_MAX_NETAGENTS; i++) {
4609 if (uuid_is_null(result.netagents[i])) {
4610 break;
4611 }
4612 if (result.netagent_use_flags[i] & NECP_AGENT_USE_FLAG_REMOVE) {
4613 // A removed agent, ignore
4614 continue;
4615 }
4616 uuid_copy(netagent.netagent_uuid, result.netagents[i]);
4617 netagent.generation = netagent_get_generation(netagent.netagent_uuid);
4618 if (necp_netagent_applies_to_client(client, parsed_parameters, &netagent.netagent_uuid, TRUE, 0, 0)) {
4619 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_NETAGENT, sizeof(netagent), &netagent, &updated,
4620 client->result, sizeof(client->result));
4621 }
4622 }
4623
4624 ifnet_head_lock_shared();
4625 ifnet_t direct_interface = NULL;
4626 ifnet_t delegate_interface = NULL;
4627 ifnet_t original_scoped_interface = NULL;
4628
4629 if (result.routed_interface_index != IFSCOPE_NONE && result.routed_interface_index <= (u_int32_t)if_index) {
4630 direct_interface = ifindex2ifnet[result.routed_interface_index];
4631 } else if (parsed_parameters->required_interface_index != IFSCOPE_NONE &&
4632 parsed_parameters->required_interface_index <= (u_int32_t)if_index) {
4633 // If the request was scoped, but the route didn't match, still grab the agents
4634 direct_interface = ifindex2ifnet[parsed_parameters->required_interface_index];
4635 } else if (result.routed_interface_index == IFSCOPE_NONE &&
4636 result.routing_result == NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED &&
4637 result.routing_result_parameter.scoped_interface_index != IFSCOPE_NONE) {
4638 direct_interface = ifindex2ifnet[result.routing_result_parameter.scoped_interface_index];
4639 }
4640 if (direct_interface != NULL) {
4641 delegate_interface = direct_interface->if_delegated.ifp;
4642 }
4643 if (result.routing_result == NECP_KERNEL_POLICY_RESULT_IP_TUNNEL &&
4644 (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_REQUIRED_FIELDS) &&
4645 parsed_parameters->required_interface_index != IFSCOPE_NONE &&
4646 parsed_parameters->required_interface_index != result.routing_result_parameter.tunnel_interface_index &&
4647 parsed_parameters->required_interface_index <= (u_int32_t)if_index) {
4648 original_scoped_interface = ifindex2ifnet[parsed_parameters->required_interface_index];
4649 }
4650 // Add interfaces
4651 if (original_scoped_interface != NULL) {
4652 struct necp_client_result_interface interface_struct;
4653 interface_struct.index = original_scoped_interface->if_index;
4654 interface_struct.generation = ifnet_get_generation(original_scoped_interface);
4655 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_INTERFACE, sizeof(interface_struct), &interface_struct, &updated,
4656 client->result, sizeof(client->result));
4657 }
4658 if (direct_interface != NULL) {
4659 struct necp_client_result_interface interface_struct;
4660 interface_struct.index = direct_interface->if_index;
4661 interface_struct.generation = ifnet_get_generation(direct_interface);
4662 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_INTERFACE, sizeof(interface_struct), &interface_struct, &updated,
4663 client->result, sizeof(client->result));
4664
4665 // Set the delta time since interface up/down
4666 struct timeval updown_delta = {};
4667 if (ifnet_updown_delta(direct_interface, &updown_delta) == 0) {
4668 u_int32_t delta = updown_delta.tv_sec;
4669 bool ignore_updated = FALSE;
4670 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_INTERFACE_TIME_DELTA,
4671 sizeof(delta), &delta, &ignore_updated,
4672 client->result, sizeof(client->result));
4673 }
4674 }
4675 if (delegate_interface != NULL) {
4676 struct necp_client_result_interface interface_struct;
4677 interface_struct.index = delegate_interface->if_index;
4678 interface_struct.generation = ifnet_get_generation(delegate_interface);
4679 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_INTERFACE, sizeof(interface_struct), &interface_struct, &updated,
4680 client->result, sizeof(client->result));
4681 }
4682
4683 // Update multipath/listener interface flows
4684 if (parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_MULTIPATH) {
4685 // Add the interface option for the routed interface first
4686 if (direct_interface != NULL) {
4687 // Add nexus agent
4688 necp_client_add_agent_interface_options(client, parsed_parameters, direct_interface);
4689
4690 // Add interface option in case it is not a nexus
4691 necp_client_add_interface_option_if_needed(client, direct_interface->if_index,
4692 ifnet_get_generation(direct_interface), NULL, false);
4693 }
4694 // Get other multipath interface options from ordered list
4695 struct ifnet *multi_interface = NULL;
4696 TAILQ_FOREACH(multi_interface, &ifnet_ordered_head, if_ordered_link) {
4697 if (multi_interface != direct_interface &&
4698 necp_ifnet_matches_parameters(multi_interface, parsed_parameters, 0, NULL, true, false)) {
4699 // Add nexus agents for multipath
4700 necp_client_add_agent_interface_options(client, parsed_parameters, multi_interface);
4701
4702 // Add multipath interface flows for kernel MPTCP
4703 necp_client_add_interface_option_if_needed(client, multi_interface->if_index,
4704 ifnet_get_generation(multi_interface), NULL, false);
4705 }
4706 }
4707 } else if (parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) {
4708 if (result.routing_result == NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED) {
4709 if (direct_interface != NULL) {
4710 // If scoped, only listen on that interface
4711 // Add nexus agents for listeners
4712 necp_client_add_agent_interface_options(client, parsed_parameters, direct_interface);
4713
4714 // Add interface option in case it is not a nexus
4715 necp_client_add_interface_option_if_needed(client, direct_interface->if_index,
4716 ifnet_get_generation(direct_interface), NULL, false);
4717 }
4718 } else {
4719 // Get listener interface options from global list
4720 struct ifnet *listen_interface = NULL;
4721 TAILQ_FOREACH(listen_interface, &ifnet_head, if_link) {
4722 if ((listen_interface->if_flags & (IFF_UP | IFF_RUNNING)) &&
4723 necp_ifnet_matches_parameters(listen_interface, parsed_parameters, 0, NULL, true, false)) {
4724 // Add nexus agents for listeners
4725 necp_client_add_agent_interface_options(client, parsed_parameters, listen_interface);
4726 }
4727 }
4728 }
4729 } else if (parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_BROWSE) {
4730 if (result.routing_result == NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED) {
4731 if (direct_interface != NULL) {
4732 // Add browse option if it has an agent
4733 necp_client_add_browse_interface_options(client, parsed_parameters, direct_interface);
4734 }
4735 } else {
4736 // Get browse interface options from global list
4737 struct ifnet *browse_interface = NULL;
4738 TAILQ_FOREACH(browse_interface, &ifnet_head, if_link) {
4739 if (necp_ifnet_matches_parameters(browse_interface, parsed_parameters, 0, NULL, true, false)) {
4740 necp_client_add_browse_interface_options(client, parsed_parameters, browse_interface);
4741 }
4742 }
4743 }
4744 }
4745
4746 struct necp_client_result_estimated_throughput throughput = {
4747 .up = 0,
4748 .down = 0,
4749 };
4750
4751 // Add agents
4752 if (original_scoped_interface != NULL) {
4753 ifnet_lock_shared(original_scoped_interface);
4754 if (original_scoped_interface->if_agentids != NULL) {
4755 for (u_int32_t i = 0; i < original_scoped_interface->if_agentcount; i++) {
4756 if (uuid_is_null(original_scoped_interface->if_agentids[i])) {
4757 continue;
4758 }
4759 bool skip_agent = false;
4760 for (int j = 0; j < NECP_MAX_NETAGENTS; j++) {
4761 if (uuid_is_null(result.netagents[j])) {
4762 break;
4763 }
4764 if ((result.netagent_use_flags[j] & NECP_AGENT_USE_FLAG_REMOVE) &&
4765 uuid_compare(original_scoped_interface->if_agentids[i], result.netagents[j]) == 0) {
4766 skip_agent = true;
4767 break;
4768 }
4769 }
4770 if (skip_agent) {
4771 continue;
4772 }
4773 uuid_copy(netagent.netagent_uuid, original_scoped_interface->if_agentids[i]);
4774 netagent.generation = netagent_get_generation(netagent.netagent_uuid);
4775 if (necp_netagent_applies_to_client(client, parsed_parameters, &netagent.netagent_uuid, FALSE,
4776 original_scoped_interface->if_index, ifnet_get_generation(original_scoped_interface))) {
4777 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_NETAGENT, sizeof(netagent), &netagent, &updated,
4778 client->result, sizeof(client->result));
4779 }
4780 }
4781 }
4782 ifnet_lock_done(original_scoped_interface);
4783 }
4784 if (direct_interface != NULL) {
4785 ifnet_lock_shared(direct_interface);
4786 throughput.up = direct_interface->if_estimated_up_bucket;
4787 throughput.down = direct_interface->if_estimated_down_bucket;
4788 if (direct_interface->if_agentids != NULL) {
4789 for (u_int32_t i = 0; i < direct_interface->if_agentcount; i++) {
4790 if (uuid_is_null(direct_interface->if_agentids[i])) {
4791 continue;
4792 }
4793 bool skip_agent = false;
4794 for (int j = 0; j < NECP_MAX_NETAGENTS; j++) {
4795 if (uuid_is_null(result.netagents[j])) {
4796 break;
4797 }
4798 if ((result.netagent_use_flags[j] & NECP_AGENT_USE_FLAG_REMOVE) &&
4799 uuid_compare(direct_interface->if_agentids[i], result.netagents[j]) == 0) {
4800 skip_agent = true;
4801 break;
4802 }
4803 }
4804 if (skip_agent) {
4805 continue;
4806 }
4807 uuid_copy(netagent.netagent_uuid, direct_interface->if_agentids[i]);
4808 netagent.generation = netagent_get_generation(netagent.netagent_uuid);
4809 if (necp_netagent_applies_to_client(client, parsed_parameters, &netagent.netagent_uuid, TRUE,
4810 direct_interface->if_index, ifnet_get_generation(direct_interface))) {
4811 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_NETAGENT, sizeof(netagent), &netagent, &updated,
4812 client->result, sizeof(client->result));
4813 }
4814 }
4815 }
4816 ifnet_lock_done(direct_interface);
4817 }
4818 if (delegate_interface != NULL) {
4819 ifnet_lock_shared(delegate_interface);
4820 if (throughput.up == 0 && throughput.down == 0) {
4821 throughput.up = delegate_interface->if_estimated_up_bucket;
4822 throughput.down = delegate_interface->if_estimated_down_bucket;
4823 }
4824 if (delegate_interface->if_agentids != NULL) {
4825 for (u_int32_t i = 0; i < delegate_interface->if_agentcount; i++) {
4826 if (uuid_is_null(delegate_interface->if_agentids[i])) {
4827 continue;
4828 }
4829 bool skip_agent = false;
4830 for (int j = 0; j < NECP_MAX_NETAGENTS; j++) {
4831 if (uuid_is_null(result.netagents[j])) {
4832 break;
4833 }
4834 if ((result.netagent_use_flags[j] & NECP_AGENT_USE_FLAG_REMOVE) &&
4835 uuid_compare(delegate_interface->if_agentids[i], result.netagents[j]) == 0) {
4836 skip_agent = true;
4837 break;
4838 }
4839 }
4840 if (skip_agent) {
4841 continue;
4842 }
4843 uuid_copy(netagent.netagent_uuid, delegate_interface->if_agentids[i]);
4844 netagent.generation = netagent_get_generation(netagent.netagent_uuid);
4845 if (necp_netagent_applies_to_client(client, parsed_parameters, &netagent.netagent_uuid, FALSE,
4846 delegate_interface->if_index, ifnet_get_generation(delegate_interface))) {
4847 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_NETAGENT, sizeof(netagent), &netagent, &updated,
4848 client->result, sizeof(client->result));
4849 }
4850 }
4851 }
4852 ifnet_lock_done(delegate_interface);
4853 }
4854 ifnet_head_done();
4855
4856 if (throughput.up != 0 || throughput.down != 0) {
4857 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_ESTIMATED_THROUGHPUT,
4858 sizeof(throughput), &throughput, &updated, client->result, sizeof(client->result));
4859 }
4860
4861 // Add interface options
4862 for (u_int32_t option_i = 0; option_i < client->interface_option_count; option_i++) {
4863 if (option_i < NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT) {
4864 struct necp_client_interface_option *option = &client->interface_options[option_i];
4865 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_INTERFACE_OPTION, sizeof(*option), option, &updated,
4866 client->result, sizeof(client->result));
4867 } else {
4868 struct necp_client_interface_option *option = &client->extra_interface_options[option_i - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT];
4869 cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_INTERFACE_OPTION, sizeof(*option), option, &updated,
4870 client->result, sizeof(client->result));
4871 }
4872 }
4873
4874 size_t new_result_length = (cursor - client->result);
4875 if (new_result_length != client->result_length) {
4876 client->result_length = new_result_length;
4877 updated = TRUE;
4878 }
4879
4880 // Update flow viability/flags
4881 if (necp_client_update_flows(proc, client, defunct_list)) {
4882 updated = TRUE;
4883 }
4884
4885 if (updated) {
4886 client->result_read = FALSE;
4887 necp_client_update_observer_update(client);
4888 }
4889
4890 kfree_type(struct necp_client_parsed_parameters, parsed_parameters);
4891 return updated;
4892 }
4893
4894 static bool
necp_defunct_client_fd_locked_inner(struct necp_fd_data * client_fd,struct _necp_flow_defunct_list * defunct_list,bool destroy_stats)4895 necp_defunct_client_fd_locked_inner(struct necp_fd_data *client_fd, struct _necp_flow_defunct_list *defunct_list, bool destroy_stats)
4896 {
4897 bool updated_result = FALSE;
4898 struct necp_client *client = NULL;
4899
4900 NECP_FD_ASSERT_LOCKED(client_fd);
4901
4902 RB_FOREACH(client, _necp_client_tree, &client_fd->clients) {
4903 struct necp_client_flow_registration *flow_registration = NULL;
4904
4905 NECP_CLIENT_LOCK(client);
4906
4907 // Prepare close events to be sent to the nexus to effectively remove the flows
4908 struct necp_client_flow *search_flow = NULL;
4909 RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
4910 LIST_FOREACH(search_flow, &flow_registration->flow_list, flow_chain) {
4911 if (search_flow->nexus &&
4912 !uuid_is_null(search_flow->u.nexus_agent)) {
4913 // Sleeping alloc won't fail; copy only what's necessary
4914 struct necp_flow_defunct *flow_defunct = kalloc_type(struct necp_flow_defunct, Z_WAITOK | Z_ZERO);
4915 uuid_copy(flow_defunct->nexus_agent, search_flow->u.nexus_agent);
4916 uuid_copy(flow_defunct->flow_id, ((flow_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
4917 client->client_id :
4918 flow_registration->registration_id));
4919 flow_defunct->proc_pid = client->proc_pid;
4920 flow_defunct->agent_handle = client->agent_handle;
4921 flow_defunct->flags = flow_registration->flags;
4922 #if SKYWALK
4923 if (flow_registration->kstats_kaddr != NULL) {
4924 struct necp_all_stats *ustats_kaddr = ((struct necp_all_kstats *)flow_registration->kstats_kaddr)->necp_stats_ustats;
4925 struct necp_quic_stats *quicstats = (struct necp_quic_stats *)ustats_kaddr;
4926 if (quicstats != NULL &&
4927 quicstats->necp_quic_udp_stats.necp_udp_hdr.necp_stats_type == NECP_CLIENT_STATISTICS_TYPE_QUIC) {
4928 memcpy(flow_defunct->close_parameters.u.close_token, quicstats->necp_quic_extra.ssr_token, sizeof(flow_defunct->close_parameters.u.close_token));
4929 flow_defunct->has_close_parameters = true;
4930 }
4931 }
4932 #endif /* SKYWALK */
4933 // Add to the list provided by caller
4934 LIST_INSERT_HEAD(defunct_list, flow_defunct, chain);
4935
4936 flow_registration->defunct = true;
4937 flow_registration->flow_result_read = false;
4938 updated_result = true;
4939 }
4940 }
4941 }
4942 if (destroy_stats) {
4943 #if SKYWALK
4944 // Free any remaining stats objects back to the arena where they came from;
4945 // do this independent of the above defunct check, as the client may have
4946 // been marked as defunct separately via necp_defunct_client_for_policy().
4947 RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
4948 necp_destroy_flow_stats(client_fd, flow_registration, NULL, FALSE);
4949 }
4950 #endif /* SKYWALK */
4951 }
4952 NECP_CLIENT_UNLOCK(client);
4953 }
4954
4955 return updated_result;
4956 }
4957
4958 static inline void
necp_defunct_client_fd_locked(struct necp_fd_data * client_fd,struct _necp_flow_defunct_list * defunct_list,struct proc * proc)4959 necp_defunct_client_fd_locked(struct necp_fd_data *client_fd, struct _necp_flow_defunct_list *defunct_list, struct proc *proc)
4960 {
4961 #pragma unused(proc)
4962 bool updated_result = FALSE;
4963
4964 NECP_FD_ASSERT_LOCKED(client_fd);
4965 #if SKYWALK
4966 // redirect regions of currently-active stats arena to zero-filled pages
4967 struct necp_arena_info *nai = necp_fd_mredirect_stats_arena(client_fd, proc);
4968 #endif /* SKYWALK */
4969
4970 updated_result = necp_defunct_client_fd_locked_inner(client_fd, defunct_list, true);
4971
4972 #if SKYWALK
4973 // and tear down the currently-active arena's regions now that the redirection and freeing are done
4974 if (nai != NULL) {
4975 ASSERT((nai->nai_flags & (NAIF_REDIRECT | NAIF_DEFUNCT)) == NAIF_REDIRECT);
4976 ASSERT(nai->nai_arena != NULL);
4977 ASSERT(nai->nai_mmap.ami_mapref != NULL);
4978
4979 int err = skmem_arena_defunct(nai->nai_arena);
4980 VERIFY(err == 0);
4981
4982 nai->nai_flags |= NAIF_DEFUNCT;
4983 }
4984 #endif /* SKYWALK */
4985
4986 if (updated_result) {
4987 necp_fd_notify(client_fd, true);
4988 }
4989 }
4990
4991 static inline void
necp_update_client_fd_locked(struct necp_fd_data * client_fd,proc_t proc,struct _necp_flow_defunct_list * defunct_list)4992 necp_update_client_fd_locked(struct necp_fd_data *client_fd,
4993 proc_t proc,
4994 struct _necp_flow_defunct_list *defunct_list)
4995 {
4996 struct necp_client *client = NULL;
4997 bool updated_result = FALSE;
4998 NECP_FD_ASSERT_LOCKED(client_fd);
4999 RB_FOREACH(client, _necp_client_tree, &client_fd->clients) {
5000 NECP_CLIENT_LOCK(client);
5001 if (necp_update_client_result(proc, client_fd, client, defunct_list)) {
5002 updated_result = TRUE;
5003 }
5004 NECP_CLIENT_UNLOCK(client);
5005 }
5006 if (updated_result) {
5007 necp_fd_notify(client_fd, true);
5008 }
5009 }
5010
5011 #if SKYWALK
5012 static void
necp_close_empty_arenas_callout(__unused thread_call_param_t dummy,__unused thread_call_param_t arg)5013 necp_close_empty_arenas_callout(__unused thread_call_param_t dummy,
5014 __unused thread_call_param_t arg)
5015 {
5016 struct necp_fd_data *client_fd = NULL;
5017
5018 NECP_FD_LIST_LOCK_SHARED();
5019
5020 LIST_FOREACH(client_fd, &necp_fd_list, chain) {
5021 NECP_FD_LOCK(client_fd);
5022 necp_stats_arenas_destroy(client_fd, FALSE);
5023 NECP_FD_UNLOCK(client_fd);
5024 }
5025
5026 NECP_FD_LIST_UNLOCK();
5027 }
5028 #endif /* SKYWALK */
5029
5030 static void
necp_update_all_clients_callout(__unused thread_call_param_t dummy,__unused thread_call_param_t arg)5031 necp_update_all_clients_callout(__unused thread_call_param_t dummy,
5032 __unused thread_call_param_t arg)
5033 {
5034 struct necp_fd_data *client_fd = NULL;
5035
5036 NECP_UPDATE_ALL_CLIENTS_LOCK_EXCLUSIVE();
5037 uint32_t count = necp_update_all_clients_sched_cnt;
5038 necp_update_all_clients_sched_cnt = 0;
5039 necp_update_all_clients_sched_abstime = 0;
5040 NECP_UPDATE_ALL_CLIENTS_UNLOCK();
5041
5042 if (necp_debug > 0) {
5043 NECPLOG(LOG_DEBUG,
5044 "necp_update_all_clients_callout running for coalesced %u updates",
5045 count);
5046 }
5047
5048 struct _necp_flow_defunct_list defunct_list;
5049 LIST_INIT(&defunct_list);
5050
5051 NECP_FD_LIST_LOCK_SHARED();
5052
5053 LIST_FOREACH(client_fd, &necp_fd_list, chain) {
5054 proc_t proc = proc_find(client_fd->proc_pid);
5055 if (proc == PROC_NULL) {
5056 continue;
5057 }
5058
5059 // Update all clients on one fd
5060 NECP_FD_LOCK(client_fd);
5061 necp_update_client_fd_locked(client_fd, proc, &defunct_list);
5062 NECP_FD_UNLOCK(client_fd);
5063
5064 proc_rele(proc);
5065 proc = PROC_NULL;
5066 }
5067
5068 NECP_FD_LIST_UNLOCK();
5069
5070 // Handle the case in which some clients became newly defunct
5071 necp_process_defunct_list(&defunct_list);
5072 }
5073
5074 void
necp_update_all_clients(void)5075 necp_update_all_clients(void)
5076 {
5077 necp_update_all_clients_immediately_if_needed(false);
5078 }
5079
5080 void
necp_update_all_clients_immediately_if_needed(bool should_update_immediately)5081 necp_update_all_clients_immediately_if_needed(bool should_update_immediately)
5082 {
5083 if (necp_client_update_tcall == NULL) {
5084 // Don't try to update clients if the module is not initialized
5085 return;
5086 }
5087
5088 uint64_t deadline = 0;
5089 uint64_t leeway = 0;
5090
5091 uint32_t timeout_to_use = necp_timeout_microseconds;
5092 uint32_t leeway_to_use = necp_timeout_leeway_microseconds;
5093 if (should_update_immediately) {
5094 timeout_to_use = 1000 * 10; // 10ms
5095 leeway_to_use = 1000 * 10; // 10ms;
5096 }
5097
5098 clock_interval_to_deadline(timeout_to_use, NSEC_PER_USEC, &deadline);
5099 clock_interval_to_absolutetime_interval(leeway_to_use, NSEC_PER_USEC, &leeway);
5100
5101 NECP_UPDATE_ALL_CLIENTS_LOCK_EXCLUSIVE();
5102 bool need_cancel = false;
5103 bool need_schedule = true;
5104 uint64_t sched_abstime;
5105
5106 clock_absolutetime_interval_to_deadline(deadline + leeway, &sched_abstime);
5107
5108 /*
5109 * Do not push the timer if it is already scheduled
5110 */
5111 if (necp_update_all_clients_sched_abstime != 0) {
5112 need_schedule = false;
5113
5114 if (should_update_immediately) {
5115 /*
5116 * To update immediately we may have to cancel the current timer
5117 * if it's scheduled too far out.
5118 */
5119 if (necp_update_all_clients_sched_abstime > sched_abstime) {
5120 need_cancel = true;
5121 need_schedule = true;
5122 }
5123 }
5124 }
5125
5126 /*
5127 * Record the time of the deadline with leeway
5128 */
5129 if (need_schedule) {
5130 necp_update_all_clients_sched_abstime = sched_abstime;
5131 }
5132
5133 necp_update_all_clients_sched_cnt += 1;
5134 uint32_t count = necp_update_all_clients_sched_cnt;
5135 NECP_UPDATE_ALL_CLIENTS_UNLOCK();
5136
5137 if (need_schedule) {
5138 /*
5139 * Wait if the thread call is currently executing to make sure the
5140 * next update will be delivered to all clients
5141 */
5142 if (need_cancel) {
5143 (void) thread_call_cancel_wait(necp_client_update_tcall);
5144 }
5145
5146 (void) thread_call_enter_delayed_with_leeway(necp_client_update_tcall, NULL,
5147 deadline, leeway, THREAD_CALL_DELAY_LEEWAY);
5148 }
5149 if (necp_debug > 0) {
5150 NECPLOG(LOG_DEBUG,
5151 "necp_update_all_clients immediate %s update %u",
5152 should_update_immediately ? "true" : "false", count);
5153 }
5154 }
5155
5156 bool
necp_set_client_as_background(proc_t proc,struct fileproc * fp,bool background)5157 necp_set_client_as_background(proc_t proc,
5158 struct fileproc *fp,
5159 bool background)
5160 {
5161 if (proc == PROC_NULL) {
5162 NECPLOG0(LOG_ERR, "NULL proc");
5163 return FALSE;
5164 }
5165
5166 if (fp == NULL) {
5167 NECPLOG0(LOG_ERR, "NULL fp");
5168 return FALSE;
5169 }
5170
5171 struct necp_fd_data *client_fd = (struct necp_fd_data *)fp_get_data(fp);
5172 if (client_fd == NULL) {
5173 NECPLOG0(LOG_ERR, "Could not find client structure for backgrounded client");
5174 return FALSE;
5175 }
5176
5177 if (client_fd->necp_fd_type != necp_fd_type_client) {
5178 // Not a client fd, ignore
5179 NECPLOG0(LOG_ERR, "Not a client fd, ignore");
5180 return FALSE;
5181 }
5182
5183 client_fd->background = background;
5184
5185 return TRUE;
5186 }
5187
5188 void
necp_fd_memstatus(proc_t proc,uint32_t status,struct necp_fd_data * client_fd)5189 necp_fd_memstatus(proc_t proc, uint32_t status,
5190 struct necp_fd_data *client_fd)
5191 {
5192 #pragma unused(proc, status, client_fd)
5193 ASSERT(proc != PROC_NULL);
5194 ASSERT(client_fd != NULL);
5195
5196 // Nothing to reap for the process or client for now,
5197 // but this is where we would trigger that in future.
5198 }
5199
5200 void
necp_fd_defunct(proc_t proc,struct necp_fd_data * client_fd)5201 necp_fd_defunct(proc_t proc, struct necp_fd_data *client_fd)
5202 {
5203 struct _necp_flow_defunct_list defunct_list;
5204
5205 ASSERT(proc != PROC_NULL);
5206 ASSERT(client_fd != NULL);
5207
5208 if (client_fd->necp_fd_type != necp_fd_type_client) {
5209 // Not a client fd, ignore
5210 return;
5211 }
5212
5213 // Our local temporary list
5214 LIST_INIT(&defunct_list);
5215
5216 // Need to hold lock so ntstats defunct the same set of clients
5217 NECP_FD_LOCK(client_fd);
5218 #if SKYWALK
5219 // Shut down statistics
5220 nstats_userland_stats_defunct_for_process(proc_getpid(proc));
5221 #endif /* SKYWALK */
5222 necp_defunct_client_fd_locked(client_fd, &defunct_list, proc);
5223 NECP_FD_UNLOCK(client_fd);
5224
5225 necp_process_defunct_list(&defunct_list);
5226 }
5227
5228 static void
necp_client_remove_agent_from_result(struct necp_client * client,uuid_t netagent_uuid)5229 necp_client_remove_agent_from_result(struct necp_client *client, uuid_t netagent_uuid)
5230 {
5231 size_t offset = 0;
5232
5233 u_int8_t *result_buffer = client->result;
5234 while ((offset + sizeof(struct necp_tlv_header)) <= client->result_length) {
5235 u_int8_t type = necp_buffer_get_tlv_type(result_buffer, offset);
5236 u_int32_t length = necp_buffer_get_tlv_length(result_buffer, offset);
5237
5238 size_t tlv_total_length = (sizeof(struct necp_tlv_header) + length);
5239 if (type == NECP_CLIENT_RESULT_NETAGENT &&
5240 length == sizeof(struct necp_client_result_netagent) &&
5241 (offset + tlv_total_length) <= client->result_length) {
5242 struct necp_client_result_netagent *value = ((struct necp_client_result_netagent *)(void *)
5243 necp_buffer_get_tlv_value(result_buffer, offset, NULL));
5244 if (uuid_compare(value->netagent_uuid, netagent_uuid) == 0) {
5245 // Found a netagent to remove
5246 // Shift bytes down to remove the tlv, and adjust total length
5247 // Don't adjust the current offset
5248 memmove(result_buffer + offset,
5249 result_buffer + offset + tlv_total_length,
5250 client->result_length - (offset + tlv_total_length));
5251 client->result_length -= tlv_total_length;
5252 memset(result_buffer + client->result_length, 0, sizeof(client->result) - client->result_length);
5253 continue;
5254 }
5255 }
5256
5257 offset += tlv_total_length;
5258 }
5259 }
5260
5261 void
necp_force_update_client(uuid_t client_id,uuid_t remove_netagent_uuid,u_int32_t agent_generation)5262 necp_force_update_client(uuid_t client_id, uuid_t remove_netagent_uuid, u_int32_t agent_generation)
5263 {
5264 struct necp_fd_data *client_fd = NULL;
5265
5266 NECP_FD_LIST_LOCK_SHARED();
5267
5268 LIST_FOREACH(client_fd, &necp_fd_list, chain) {
5269 bool updated_result = FALSE;
5270 NECP_FD_LOCK(client_fd);
5271 struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
5272 if (client != NULL) {
5273 client->failed_trigger_agent.generation = agent_generation;
5274 uuid_copy(client->failed_trigger_agent.netagent_uuid, remove_netagent_uuid);
5275 if (!uuid_is_null(remove_netagent_uuid)) {
5276 necp_client_remove_agent_from_result(client, remove_netagent_uuid);
5277 }
5278 client->result_read = FALSE;
5279 // Found the client, break
5280 updated_result = TRUE;
5281 NECP_CLIENT_UNLOCK(client);
5282 }
5283 if (updated_result) {
5284 necp_fd_notify(client_fd, true);
5285 }
5286 NECP_FD_UNLOCK(client_fd);
5287 if (updated_result) {
5288 // Found the client, break
5289 break;
5290 }
5291 }
5292
5293 NECP_FD_LIST_UNLOCK();
5294 }
5295
5296 #if SKYWALK
5297 void
necp_client_early_close(uuid_t client_id)5298 necp_client_early_close(uuid_t client_id)
5299 {
5300 NECP_CLIENT_TREE_LOCK_SHARED();
5301
5302 struct necp_client *client = necp_find_client_and_lock(client_id);
5303 if (client != NULL) {
5304 struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
5305 if (flow_registration != NULL) {
5306 // Found the right client and flow, mark the stats as over
5307 if (flow_registration->stats_handler_context != NULL) {
5308 ntstat_userland_stats_event(flow_registration->stats_handler_context,
5309 NECP_CLIENT_STATISTICS_EVENT_TIME_WAIT);
5310 }
5311 }
5312 NECP_CLIENT_UNLOCK(client);
5313 }
5314
5315 NECP_CLIENT_TREE_UNLOCK();
5316 }
5317 #endif /* SKYWALK */
5318
5319 /// Interface matching
5320
5321 #define NECP_PARSED_PARAMETERS_INTERESTING_IFNET_FIELDS (NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR | \
5322 NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IF | \
5323 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE | \
5324 NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IFTYPE | \
5325 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT | \
5326 NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT | \
5327 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT | \
5328 NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT | \
5329 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE | \
5330 NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT_TYPE | \
5331 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE | \
5332 NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE)
5333
5334 #define NECP_PARSED_PARAMETERS_SCOPED_FIELDS (NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR | \
5335 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE | \
5336 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT | \
5337 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT | \
5338 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE | \
5339 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE)
5340
5341 #define NECP_PARSED_PARAMETERS_SCOPED_IFNET_FIELDS (NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR | \
5342 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE)
5343
5344 #define NECP_PARSED_PARAMETERS_PREFERRED_FIELDS (NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT | \
5345 NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT | \
5346 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE | \
5347 NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE)
5348
5349 static bool
necp_ifnet_matches_type(struct ifnet * ifp,u_int8_t interface_type,bool check_delegates)5350 necp_ifnet_matches_type(struct ifnet *ifp, u_int8_t interface_type, bool check_delegates)
5351 {
5352 struct ifnet *check_ifp = ifp;
5353 while (check_ifp) {
5354 if (if_functional_type(check_ifp, TRUE) == interface_type) {
5355 return TRUE;
5356 }
5357 if (!check_delegates) {
5358 break;
5359 }
5360 check_ifp = check_ifp->if_delegated.ifp;
5361 }
5362 return FALSE;
5363 }
5364
5365 static bool
necp_ifnet_matches_name(struct ifnet * ifp,const char * interface_name,bool check_delegates)5366 necp_ifnet_matches_name(struct ifnet *ifp, const char *interface_name, bool check_delegates)
5367 {
5368 struct ifnet *check_ifp = ifp;
5369 while (check_ifp) {
5370 if (strncmp(check_ifp->if_xname, interface_name, IFXNAMSIZ) == 0) {
5371 return TRUE;
5372 }
5373 if (!check_delegates) {
5374 break;
5375 }
5376 check_ifp = check_ifp->if_delegated.ifp;
5377 }
5378 return FALSE;
5379 }
5380
5381 static bool
necp_ifnet_matches_agent(struct ifnet * ifp,uuid_t * agent_uuid,bool check_delegates)5382 necp_ifnet_matches_agent(struct ifnet *ifp, uuid_t *agent_uuid, bool check_delegates)
5383 {
5384 struct ifnet *check_ifp = ifp;
5385
5386 while (check_ifp != NULL) {
5387 ifnet_lock_shared(check_ifp);
5388 if (check_ifp->if_agentids != NULL) {
5389 for (u_int32_t index = 0; index < check_ifp->if_agentcount; index++) {
5390 if (uuid_compare(check_ifp->if_agentids[index], *agent_uuid) == 0) {
5391 ifnet_lock_done(check_ifp);
5392 return TRUE;
5393 }
5394 }
5395 }
5396 ifnet_lock_done(check_ifp);
5397
5398 if (!check_delegates) {
5399 break;
5400 }
5401 check_ifp = check_ifp->if_delegated.ifp;
5402 }
5403 return FALSE;
5404 }
5405
5406 static bool
necp_ifnet_matches_agent_type(struct ifnet * ifp,const char * agent_domain,const char * agent_type,bool check_delegates)5407 necp_ifnet_matches_agent_type(struct ifnet *ifp, const char *agent_domain, const char *agent_type, bool check_delegates)
5408 {
5409 struct ifnet *check_ifp = ifp;
5410
5411 while (check_ifp != NULL) {
5412 ifnet_lock_shared(check_ifp);
5413 if (check_ifp->if_agentids != NULL) {
5414 for (u_int32_t index = 0; index < check_ifp->if_agentcount; index++) {
5415 if (uuid_is_null(check_ifp->if_agentids[index])) {
5416 continue;
5417 }
5418
5419 char if_agent_domain[NETAGENT_DOMAINSIZE] = { 0 };
5420 char if_agent_type[NETAGENT_TYPESIZE] = { 0 };
5421
5422 if (netagent_get_agent_domain_and_type(check_ifp->if_agentids[index], if_agent_domain, if_agent_type)) {
5423 if (necp_agent_types_match(agent_domain, agent_type, if_agent_domain, if_agent_type)) {
5424 ifnet_lock_done(check_ifp);
5425 return TRUE;
5426 }
5427 }
5428 }
5429 }
5430 ifnet_lock_done(check_ifp);
5431
5432 if (!check_delegates) {
5433 break;
5434 }
5435 check_ifp = check_ifp->if_delegated.ifp;
5436 }
5437 return FALSE;
5438 }
5439
5440 static bool
necp_ifnet_matches_local_address(struct ifnet * ifp,struct sockaddr * sa)5441 necp_ifnet_matches_local_address(struct ifnet *ifp, struct sockaddr *sa)
5442 {
5443 struct ifaddr *ifa = NULL;
5444 bool matched_local_address = FALSE;
5445
5446 // Transform sa into the ifaddr form
5447 // IPv6 Scope IDs are always embedded in the ifaddr list
5448 struct sockaddr_storage address;
5449 u_int ifscope = IFSCOPE_NONE;
5450 (void)sa_copy(sa, &address, &ifscope);
5451 SIN(&address)->sin_port = 0;
5452 if (address.ss_family == AF_INET6) {
5453 if (in6_embedded_scope ||
5454 !IN6_IS_SCOPE_EMBED(&SIN6(&address)->sin6_addr)) {
5455 SIN6(&address)->sin6_scope_id = 0;
5456 }
5457 }
5458
5459 ifa = ifa_ifwithaddr_scoped_locked((struct sockaddr *)&address, ifp->if_index);
5460 matched_local_address = (ifa != NULL);
5461
5462 if (ifa) {
5463 ifaddr_release(ifa);
5464 }
5465
5466 return matched_local_address;
5467 }
5468
5469 static bool
necp_interface_type_should_match_unranked_interfaces(u_int8_t interface_type)5470 necp_interface_type_should_match_unranked_interfaces(u_int8_t interface_type)
5471 {
5472 switch (interface_type) {
5473 // These are the interface types we allow a client to request even if the matching
5474 // interface isn't currently eligible to be primary (has default route, dns, etc)
5475 case IFRTYPE_FUNCTIONAL_WIFI_AWDL:
5476 case IFRTYPE_FUNCTIONAL_INTCOPROC:
5477 case IFRTYPE_FUNCTIONAL_COMPANIONLINK:
5478 return true;
5479 default:
5480 break;
5481 }
5482 return false;
5483 }
5484
5485 #define NECP_IFP_IS_ON_ORDERED_LIST(_ifp) ((_ifp)->if_ordered_link.tqe_next != NULL || (_ifp)->if_ordered_link.tqe_prev != NULL)
5486
5487 // Secondary interface flag indicates that the interface is being
5488 // used for multipath or a listener as an extra path
5489 static bool
necp_ifnet_matches_parameters(struct ifnet * ifp,struct necp_client_parsed_parameters * parsed_parameters,u_int32_t override_flags,u_int32_t * preferred_count,bool secondary_interface,bool require_scoped_field)5490 necp_ifnet_matches_parameters(struct ifnet *ifp,
5491 struct necp_client_parsed_parameters *parsed_parameters,
5492 u_int32_t override_flags,
5493 u_int32_t *preferred_count,
5494 bool secondary_interface,
5495 bool require_scoped_field)
5496 {
5497 bool matched_some_scoped_field = FALSE;
5498
5499 if (preferred_count) {
5500 *preferred_count = 0;
5501 }
5502
5503 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF) {
5504 if (parsed_parameters->required_interface_index != ifp->if_index) {
5505 return FALSE;
5506 }
5507 }
5508 #if SKYWALK
5509 else {
5510 if (ifnet_is_low_latency(ifp)) {
5511 return FALSE;
5512 }
5513 }
5514 #endif /* SKYWALK */
5515
5516 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR) {
5517 if (!necp_ifnet_matches_local_address(ifp, SA(&parsed_parameters->local_addr.sa))) {
5518 return FALSE;
5519 }
5520 if (require_scoped_field) {
5521 matched_some_scoped_field = TRUE;
5522 }
5523 }
5524
5525 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) {
5526 if (override_flags != 0) {
5527 if ((override_flags & NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_EXPENSIVE) &&
5528 IFNET_IS_EXPENSIVE(ifp)) {
5529 return FALSE;
5530 }
5531 if ((override_flags & NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_CONSTRAINED) &&
5532 IFNET_IS_CONSTRAINED(ifp)) {
5533 return FALSE;
5534 }
5535 } else {
5536 if ((parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_EXPENSIVE) &&
5537 IFNET_IS_EXPENSIVE(ifp)) {
5538 return FALSE;
5539 }
5540 if ((parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_CONSTRAINED) &&
5541 IFNET_IS_CONSTRAINED(ifp)) {
5542 return FALSE;
5543 }
5544 }
5545 }
5546
5547 if ((!secondary_interface || // Enforce interface type if this is the primary interface
5548 !(parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) || // or if there are no flags
5549 !(parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_ONLY_PRIMARY_REQUIRES_TYPE)) && // or if the flags don't give an exception
5550 (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE) &&
5551 !necp_ifnet_matches_type(ifp, parsed_parameters->required_interface_type, FALSE)) {
5552 return FALSE;
5553 }
5554
5555 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE) {
5556 if (require_scoped_field) {
5557 matched_some_scoped_field = TRUE;
5558 }
5559 }
5560
5561 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IFTYPE) {
5562 for (int i = 0; i < NECP_MAX_INTERFACE_PARAMETERS; i++) {
5563 if (parsed_parameters->prohibited_interface_types[i] == 0) {
5564 break;
5565 }
5566
5567 if (necp_ifnet_matches_type(ifp, parsed_parameters->prohibited_interface_types[i], TRUE)) {
5568 return FALSE;
5569 }
5570 }
5571 }
5572
5573 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IF) {
5574 for (int i = 0; i < NECP_MAX_INTERFACE_PARAMETERS; i++) {
5575 if (strlen(parsed_parameters->prohibited_interfaces[i]) == 0) {
5576 break;
5577 }
5578
5579 if (necp_ifnet_matches_name(ifp, parsed_parameters->prohibited_interfaces[i], TRUE)) {
5580 return FALSE;
5581 }
5582 }
5583 }
5584
5585 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT) {
5586 for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5587 if (uuid_is_null(parsed_parameters->required_netagents[i])) {
5588 break;
5589 }
5590
5591 if (!necp_ifnet_matches_agent(ifp, &parsed_parameters->required_netagents[i], FALSE)) {
5592 return FALSE;
5593 }
5594
5595 if (require_scoped_field) {
5596 matched_some_scoped_field = TRUE;
5597 }
5598 }
5599 }
5600
5601 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT) {
5602 for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5603 if (uuid_is_null(parsed_parameters->prohibited_netagents[i])) {
5604 break;
5605 }
5606
5607 if (necp_ifnet_matches_agent(ifp, &parsed_parameters->prohibited_netagents[i], TRUE)) {
5608 return FALSE;
5609 }
5610 }
5611 }
5612
5613 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE) {
5614 for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5615 if (strlen(parsed_parameters->required_netagent_types[i].netagent_domain) == 0 &&
5616 strlen(parsed_parameters->required_netagent_types[i].netagent_type) == 0) {
5617 break;
5618 }
5619
5620 if (!necp_ifnet_matches_agent_type(ifp, parsed_parameters->required_netagent_types[i].netagent_domain, parsed_parameters->required_netagent_types[i].netagent_type, FALSE)) {
5621 return FALSE;
5622 }
5623
5624 if (require_scoped_field) {
5625 matched_some_scoped_field = TRUE;
5626 }
5627 }
5628 }
5629
5630 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT_TYPE) {
5631 for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5632 if (strlen(parsed_parameters->prohibited_netagent_types[i].netagent_domain) == 0 &&
5633 strlen(parsed_parameters->prohibited_netagent_types[i].netagent_type) == 0) {
5634 break;
5635 }
5636
5637 if (necp_ifnet_matches_agent_type(ifp, parsed_parameters->prohibited_netagent_types[i].netagent_domain, parsed_parameters->prohibited_netagent_types[i].netagent_type, TRUE)) {
5638 return FALSE;
5639 }
5640 }
5641 }
5642
5643 // Checked preferred properties
5644 if (preferred_count) {
5645 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT) {
5646 for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5647 if (uuid_is_null(parsed_parameters->preferred_netagents[i])) {
5648 break;
5649 }
5650
5651 if (necp_ifnet_matches_agent(ifp, &parsed_parameters->preferred_netagents[i], TRUE)) {
5652 (*preferred_count)++;
5653 if (require_scoped_field) {
5654 matched_some_scoped_field = TRUE;
5655 }
5656 }
5657 }
5658 }
5659
5660 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE) {
5661 for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5662 if (strlen(parsed_parameters->preferred_netagent_types[i].netagent_domain) == 0 &&
5663 strlen(parsed_parameters->preferred_netagent_types[i].netagent_type) == 0) {
5664 break;
5665 }
5666
5667 if (necp_ifnet_matches_agent_type(ifp, parsed_parameters->preferred_netagent_types[i].netagent_domain, parsed_parameters->preferred_netagent_types[i].netagent_type, TRUE)) {
5668 (*preferred_count)++;
5669 if (require_scoped_field) {
5670 matched_some_scoped_field = TRUE;
5671 }
5672 }
5673 }
5674 }
5675
5676 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT) {
5677 for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5678 if (uuid_is_null(parsed_parameters->avoided_netagents[i])) {
5679 break;
5680 }
5681
5682 if (!necp_ifnet_matches_agent(ifp, &parsed_parameters->avoided_netagents[i], TRUE)) {
5683 (*preferred_count)++;
5684 }
5685 }
5686 }
5687
5688 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE) {
5689 for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5690 if (strlen(parsed_parameters->avoided_netagent_types[i].netagent_domain) == 0 &&
5691 strlen(parsed_parameters->avoided_netagent_types[i].netagent_type) == 0) {
5692 break;
5693 }
5694
5695 if (!necp_ifnet_matches_agent_type(ifp, parsed_parameters->avoided_netagent_types[i].netagent_domain,
5696 parsed_parameters->avoided_netagent_types[i].netagent_type, TRUE)) {
5697 (*preferred_count)++;
5698 }
5699 }
5700 }
5701 }
5702
5703 if (require_scoped_field) {
5704 return matched_some_scoped_field;
5705 }
5706
5707 return TRUE;
5708 }
5709
5710 static bool
necp_find_matching_interface_index(struct necp_client_parsed_parameters * parsed_parameters,u_int * return_ifindex,bool * validate_agents)5711 necp_find_matching_interface_index(struct necp_client_parsed_parameters *parsed_parameters,
5712 u_int *return_ifindex, bool *validate_agents)
5713 {
5714 struct ifnet *ifp = NULL;
5715 u_int32_t best_preferred_count = 0;
5716 bool has_preferred_fields = FALSE;
5717 *return_ifindex = 0;
5718
5719 if (parsed_parameters->required_interface_index != 0) {
5720 *return_ifindex = parsed_parameters->required_interface_index;
5721 return TRUE;
5722 }
5723
5724 // Check and save off flags
5725 u_int32_t flags = 0;
5726 bool has_prohibit_flags = FALSE;
5727 if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) {
5728 flags = parsed_parameters->flags;
5729 has_prohibit_flags = (parsed_parameters->flags &
5730 (NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_EXPENSIVE |
5731 NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_CONSTRAINED));
5732 }
5733
5734 if (!(parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_INTERESTING_IFNET_FIELDS) &&
5735 !has_prohibit_flags) {
5736 return TRUE;
5737 }
5738
5739 has_preferred_fields = (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_PREFERRED_FIELDS);
5740
5741 // We have interesting parameters to parse and find a matching interface
5742 ifnet_head_lock_shared();
5743
5744 if (!(parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_SCOPED_FIELDS) &&
5745 !has_preferred_fields) {
5746 // We do have fields to match, but they are only prohibitory
5747 // If the first interface in the list matches, or there are no ordered interfaces, we don't need to scope
5748 ifp = TAILQ_FIRST(&ifnet_ordered_head);
5749 if (ifp == NULL || necp_ifnet_matches_parameters(ifp, parsed_parameters, 0, NULL, false, false)) {
5750 // Don't set return_ifindex, so the client doesn't need to scope
5751 ifnet_head_done();
5752 return TRUE;
5753 }
5754 }
5755
5756 // First check the ordered interface list
5757 TAILQ_FOREACH(ifp, &ifnet_ordered_head, if_ordered_link) {
5758 u_int32_t preferred_count = 0;
5759 if (necp_ifnet_matches_parameters(ifp, parsed_parameters, flags, &preferred_count, false, false)) {
5760 if (preferred_count > best_preferred_count ||
5761 *return_ifindex == 0) {
5762 // Everything matched, and is most preferred. Return this interface.
5763 *return_ifindex = ifp->if_index;
5764 best_preferred_count = preferred_count;
5765
5766 if (!has_preferred_fields) {
5767 break;
5768 }
5769 }
5770 }
5771
5772 if (has_prohibit_flags &&
5773 ifp == TAILQ_FIRST(&ifnet_ordered_head)) {
5774 // This was the first interface. From here on, if the
5775 // client prohibited either expensive or constrained,
5776 // don't allow either as a secondary interface option.
5777 flags |= (NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_EXPENSIVE |
5778 NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_CONSTRAINED);
5779 }
5780 }
5781
5782 bool is_listener = ((parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) &&
5783 (parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER));
5784
5785 // Then check the remaining interfaces
5786 if ((parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_SCOPED_FIELDS) &&
5787 ((!(parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE)) ||
5788 necp_interface_type_should_match_unranked_interfaces(parsed_parameters->required_interface_type) ||
5789 (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR) ||
5790 is_listener) &&
5791 (*return_ifindex == 0 || has_preferred_fields)) {
5792 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
5793 u_int32_t preferred_count = 0;
5794 if (NECP_IFP_IS_ON_ORDERED_LIST(ifp)) {
5795 // This interface was in the ordered list, skip
5796 continue;
5797 }
5798 if (necp_ifnet_matches_parameters(ifp, parsed_parameters, flags, &preferred_count, false, true)) {
5799 if (preferred_count > best_preferred_count ||
5800 *return_ifindex == 0) {
5801 // Everything matched, and is most preferred. Return this interface.
5802 *return_ifindex = ifp->if_index;
5803 best_preferred_count = preferred_count;
5804
5805 if (!has_preferred_fields) {
5806 break;
5807 }
5808 }
5809 }
5810 }
5811 }
5812
5813 ifnet_head_done();
5814
5815 if (has_preferred_fields && best_preferred_count == 0 &&
5816 ((parsed_parameters->valid_fields & (NECP_PARSED_PARAMETERS_SCOPED_FIELDS | NECP_PARSED_PARAMETERS_PREFERRED_FIELDS)) ==
5817 (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_PREFERRED_FIELDS))) {
5818 // If only has preferred ifnet fields, and nothing was found, clear the interface index and return TRUE
5819 *return_ifindex = 0;
5820 return TRUE;
5821 }
5822
5823 if (*return_ifindex == 0 &&
5824 !(parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_SCOPED_IFNET_FIELDS)) {
5825 // Has required fields, but not including specific interface fields. Pass for now, and check
5826 // to see if agents are satisfied by policy.
5827 *validate_agents = TRUE;
5828 return TRUE;
5829 }
5830
5831 return *return_ifindex != 0;
5832 }
5833
5834 void
necp_copy_inp_domain_info(struct inpcb * inp,struct socket * so,nstat_domain_info * domain_info)5835 necp_copy_inp_domain_info(struct inpcb *inp, struct socket *so, nstat_domain_info *domain_info)
5836 {
5837 if (inp == NULL || so == NULL || domain_info == NULL) {
5838 return;
5839 }
5840
5841 necp_lock_socket_attributes();
5842
5843 domain_info->is_tracker = !!(so->so_flags1 & SOF1_KNOWN_TRACKER);
5844 domain_info->is_non_app_initiated = !!(so->so_flags1 & SOF1_TRACKER_NON_APP_INITIATED);
5845 if (domain_info->is_tracker &&
5846 inp->inp_necp_attributes.inp_tracker_domain != NULL) {
5847 strlcpy(domain_info->domain_name, inp->inp_necp_attributes.inp_tracker_domain,
5848 sizeof(domain_info->domain_name));
5849 } else if (inp->inp_necp_attributes.inp_domain != NULL) {
5850 strlcpy(domain_info->domain_name, inp->inp_necp_attributes.inp_domain,
5851 sizeof(domain_info->domain_name));
5852 }
5853 if (inp->inp_necp_attributes.inp_domain_owner != NULL) {
5854 strlcpy(domain_info->domain_owner, inp->inp_necp_attributes.inp_domain_owner,
5855 sizeof(domain_info->domain_owner));
5856 }
5857 if (inp->inp_necp_attributes.inp_domain_context != NULL) {
5858 strlcpy(domain_info->domain_tracker_ctxt, inp->inp_necp_attributes.inp_domain_context,
5859 sizeof(domain_info->domain_tracker_ctxt));
5860 }
5861
5862 necp_unlock_socket_attributes();
5863 }
5864
5865 void
necp_with_inp_domain_name(struct socket * so,void * ctx,void (* with_func)(char * domain_name,void * ctx))5866 necp_with_inp_domain_name(struct socket *so, void *ctx, void (*with_func)(char *domain_name, void *ctx))
5867 {
5868 struct inpcb *inp = NULL;
5869
5870 if (so == NULL || with_func == NULL) {
5871 return;
5872 }
5873
5874 inp = (struct inpcb *)so->so_pcb;
5875 if (inp == NULL) {
5876 return;
5877 }
5878
5879 necp_lock_socket_attributes();
5880 with_func(inp->inp_necp_attributes.inp_domain, ctx);
5881 necp_unlock_socket_attributes();
5882 }
5883
5884 static size_t
necp_find_domain_info_common(struct necp_client * client,u_int8_t * parameters,size_t parameters_size,struct necp_client_flow_registration * flow_registration,nstat_domain_info * domain_info)5885 necp_find_domain_info_common(struct necp_client *client,
5886 u_int8_t *parameters,
5887 size_t parameters_size,
5888 struct necp_client_flow_registration *flow_registration, /* For logging purposes only */
5889 nstat_domain_info *domain_info)
5890 {
5891 if (client == NULL) {
5892 return 0;
5893 }
5894 if (domain_info == NULL) {
5895 return sizeof(nstat_domain_info);
5896 }
5897
5898 size_t offset = 0;
5899 u_int32_t flags = 0;
5900 u_int8_t *tracker_domain = NULL;
5901 u_int8_t *domain = NULL;
5902 size_t tracker_domain_length = 0;
5903 size_t domain_length = 0;
5904
5905 NECP_CLIENT_FLOW_LOG(client, flow_registration, "Collecting stats");
5906
5907 while ((offset + sizeof(struct necp_tlv_header)) <= parameters_size) {
5908 u_int8_t type = necp_buffer_get_tlv_type(parameters, offset);
5909 u_int32_t length = necp_buffer_get_tlv_length(parameters, offset);
5910
5911 if (length > (parameters_size - (offset + sizeof(struct necp_tlv_header)))) {
5912 // If the length is larger than what can fit in the remaining parameters size, bail
5913 NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
5914 break;
5915 }
5916
5917 if (length > 0) {
5918 u_int8_t *value = necp_buffer_get_tlv_value(parameters, offset, NULL);
5919 if (value != NULL) {
5920 switch (type) {
5921 case NECP_CLIENT_PARAMETER_FLAGS: {
5922 if (length >= sizeof(u_int32_t)) {
5923 memcpy(&flags, value, sizeof(u_int32_t));
5924 }
5925
5926 domain_info->is_tracker =
5927 !!(flags & NECP_CLIENT_PARAMETER_FLAG_KNOWN_TRACKER);
5928 domain_info->is_non_app_initiated =
5929 !!(flags & NECP_CLIENT_PARAMETER_FLAG_NON_APP_INITIATED);
5930 domain_info->is_silent =
5931 !!(flags & NECP_CLIENT_PARAMETER_FLAG_SILENT);
5932 break;
5933 }
5934 case NECP_CLIENT_PARAMETER_TRACKER_DOMAIN: {
5935 tracker_domain_length = length;
5936 tracker_domain = value;
5937 break;
5938 }
5939 case NECP_CLIENT_PARAMETER_DOMAIN: {
5940 domain_length = length;
5941 domain = value;
5942 break;
5943 }
5944 case NECP_CLIENT_PARAMETER_DOMAIN_OWNER: {
5945 size_t length_to_copy = MIN(length, sizeof(domain_info->domain_owner));
5946 strlcpy(domain_info->domain_owner, (const char *)value, length_to_copy);
5947 break;
5948 }
5949 case NECP_CLIENT_PARAMETER_DOMAIN_CONTEXT: {
5950 size_t length_to_copy = MIN(length, sizeof(domain_info->domain_tracker_ctxt));
5951 strlcpy(domain_info->domain_tracker_ctxt, (const char *)value, length_to_copy);
5952 break;
5953 }
5954 case NECP_CLIENT_PARAMETER_ATTRIBUTED_BUNDLE_IDENTIFIER: {
5955 size_t length_to_copy = MIN(length, sizeof(domain_info->domain_attributed_bundle_id));
5956 strlcpy(domain_info->domain_attributed_bundle_id, (const char *)value, length_to_copy);
5957 break;
5958 }
5959 case NECP_CLIENT_PARAMETER_REMOTE_ADDRESS: {
5960 if (length >= sizeof(struct necp_policy_condition_addr)) {
5961 struct necp_policy_condition_addr *address_struct = (struct necp_policy_condition_addr *)(void *)value;
5962 if (necp_client_address_is_valid(&address_struct->address.sa)) {
5963 memcpy(&domain_info->remote, &address_struct->address, sizeof(address_struct->address));
5964 }
5965 }
5966 break;
5967 }
5968 default: {
5969 break;
5970 }
5971 }
5972 }
5973 }
5974 offset += sizeof(struct necp_tlv_header) + length;
5975 }
5976
5977 if (domain_info->is_tracker && tracker_domain != NULL && tracker_domain_length > 0) {
5978 size_t length_to_copy = MIN(tracker_domain_length, sizeof(domain_info->domain_name));
5979 strlcpy(domain_info->domain_name, (const char *)tracker_domain, length_to_copy);
5980 } else if (domain != NULL && domain_length > 0) {
5981 size_t length_to_copy = MIN(domain_length, sizeof(domain_info->domain_name));
5982 strlcpy(domain_info->domain_name, (const char *)domain, length_to_copy);
5983 }
5984
5985 // Log if it is a known tracker
5986 if (domain_info->is_tracker && client) {
5987 NECP_CLIENT_TRACKER_LOG(client->proc_pid,
5988 "Collected stats - domain <%s> owner <%s> ctxt <%s> bundle id <%s> "
5989 "is_tracker %d is_non_app_initiated %d is_silent %d",
5990 domain_info->domain_name[0] ? "present" : "not set",
5991 domain_info->domain_owner[0] ? "present" : "not set",
5992 domain_info->domain_tracker_ctxt[0] ? "present" : "not set",
5993 domain_info->domain_attributed_bundle_id[0] ? "present" : "not set",
5994 domain_info->is_tracker,
5995 domain_info->is_non_app_initiated,
5996 domain_info->is_silent);
5997 }
5998
5999 NECP_CLIENT_FLOW_LOG(client, flow_registration,
6000 "Collected stats - domain <%s> owner <%s> ctxt <%s> bundle id <%s> "
6001 "is_tracker %d is_non_app_initiated %d is_silent %d",
6002 domain_info->domain_name,
6003 domain_info->domain_owner,
6004 domain_info->domain_tracker_ctxt,
6005 domain_info->domain_attributed_bundle_id,
6006 domain_info->is_tracker,
6007 domain_info->is_non_app_initiated,
6008 domain_info->is_silent);
6009
6010 return sizeof(nstat_domain_info);
6011 }
6012
6013 static size_t
necp_find_conn_extension_info(nstat_provider_context ctx,int requested_extension,void * buf,size_t buf_size)6014 necp_find_conn_extension_info(nstat_provider_context ctx,
6015 int requested_extension, /* The extension to be returned */
6016 void *buf, /* If not NULL, the address for extensions to be returned in */
6017 size_t buf_size) /* The size of the buffer space, typically matching the return from a previous call with a NULL buf pointer */
6018 {
6019 // Note, the caller has guaranteed that any buffer has been zeroed, there is no need to clear it again
6020
6021 if (ctx == NULL) {
6022 return 0;
6023 }
6024 struct necp_client *client = (struct necp_client *)ctx;
6025 switch (requested_extension) {
6026 case NSTAT_EXTENDED_UPDATE_TYPE_DOMAIN:
6027 // This is for completeness. The intent is that domain information can be extracted at user level from the TLV parameters
6028 if (buf == NULL) {
6029 return sizeof(nstat_domain_info);
6030 }
6031 if (buf_size < sizeof(nstat_domain_info)) {
6032 return 0;
6033 }
6034 return necp_find_domain_info_common(client, client->parameters, client->parameters_length, NULL, (nstat_domain_info *)buf);
6035
6036 case NSTAT_EXTENDED_UPDATE_TYPE_NECP_TLV:
6037 if (buf == NULL) {
6038 return client->parameters_length;
6039 }
6040 if (buf_size < client->parameters_length) {
6041 return 0;
6042 }
6043 memcpy(buf, client->parameters, client->parameters_length);
6044 return client->parameters_length;
6045
6046 case NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_NECP_TLV:
6047 if (buf == NULL) {
6048 return (client->original_parameters_source != NULL) ? client->original_parameters_source->parameters_length : 0;
6049 }
6050 if ((client->original_parameters_source == NULL) || (buf_size < client->original_parameters_source->parameters_length)) {
6051 return 0;
6052 }
6053 memcpy(buf, client->original_parameters_source->parameters, client->original_parameters_source->parameters_length);
6054 return client->original_parameters_source->parameters_length;
6055
6056 case NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_DOMAIN:
6057 if (buf == NULL) {
6058 return (client->original_parameters_source != NULL) ? sizeof(nstat_domain_info) : 0;
6059 }
6060 if ((buf_size < sizeof(nstat_domain_info)) || (client->original_parameters_source == NULL)) {
6061 return 0;
6062 }
6063 return necp_find_domain_info_common(client, client->original_parameters_source->parameters, client->original_parameters_source->parameters_length,
6064 NULL, (nstat_domain_info *)buf);
6065
6066 default:
6067 return 0;
6068 }
6069 }
6070
6071 #if SKYWALK
6072
6073 static size_t
necp_find_extension_info(userland_stats_provider_context * ctx,int requested_extension,void * buf,size_t buf_size)6074 necp_find_extension_info(userland_stats_provider_context *ctx,
6075 int requested_extension, /* The extension to be returned */
6076 void *buf, /* If not NULL, the address for extensions to be returned in */
6077 size_t buf_size) /* The size of the buffer space, typically matching the return from a previous call with a NULL buf pointer */
6078 {
6079 if (ctx == NULL) {
6080 return 0;
6081 }
6082 struct necp_client_flow_registration *flow_registration = (struct necp_client_flow_registration *)(uintptr_t)ctx;
6083 struct necp_client *client = flow_registration->client;
6084
6085 switch (requested_extension) {
6086 case NSTAT_EXTENDED_UPDATE_TYPE_DOMAIN:
6087 if (buf == NULL) {
6088 return sizeof(nstat_domain_info);
6089 }
6090 if (buf_size < sizeof(nstat_domain_info)) {
6091 return 0;
6092 }
6093 return necp_find_domain_info_common(client, client->parameters, client->parameters_length, flow_registration, (nstat_domain_info *)buf);
6094
6095 case NSTAT_EXTENDED_UPDATE_TYPE_NECP_TLV:
6096 if (buf == NULL) {
6097 return client->parameters_length;
6098 }
6099 if (buf_size < client->parameters_length) {
6100 return 0;
6101 }
6102 memcpy(buf, client->parameters, client->parameters_length);
6103 return client->parameters_length;
6104
6105 case NSTAT_EXTENDED_UPDATE_TYPE_FUUID:
6106 if (buf == NULL) {
6107 return sizeof(uuid_t);
6108 }
6109 if (buf_size < sizeof(uuid_t)) {
6110 return 0;
6111 }
6112 uuid_copy(buf, flow_registration->registration_id);
6113 return sizeof(uuid_t);
6114
6115 default:
6116 return 0;
6117 }
6118 }
6119
6120 static void
necp_find_netstat_data(struct necp_client * client,union necp_sockaddr_union * remote,pid_t * effective_pid,uuid_t euuid,u_int32_t * traffic_class,u_int8_t * fallback_mode)6121 necp_find_netstat_data(struct necp_client *client,
6122 union necp_sockaddr_union *remote,
6123 pid_t *effective_pid,
6124 uuid_t euuid,
6125 u_int32_t *traffic_class,
6126 u_int8_t *fallback_mode)
6127 {
6128 size_t offset = 0;
6129 u_int8_t *parameters;
6130 u_int32_t parameters_size;
6131
6132 parameters = client->parameters;
6133 parameters_size = (u_int32_t)client->parameters_length;
6134
6135 while ((offset + sizeof(struct necp_tlv_header)) <= parameters_size) {
6136 u_int8_t type = necp_buffer_get_tlv_type(parameters, offset);
6137 u_int32_t length = necp_buffer_get_tlv_length(parameters, offset);
6138
6139 if (length > (parameters_size - (offset + sizeof(struct necp_tlv_header)))) {
6140 // If the length is larger than what can fit in the remaining parameters size, bail
6141 NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
6142 break;
6143 }
6144
6145 if (length > 0) {
6146 u_int8_t *value = necp_buffer_get_tlv_value(parameters, offset, NULL);
6147 if (value != NULL) {
6148 switch (type) {
6149 case NECP_CLIENT_PARAMETER_APPLICATION: {
6150 if (length >= sizeof(uuid_t)) {
6151 uuid_copy(euuid, value);
6152 }
6153 break;
6154 }
6155 case NECP_CLIENT_PARAMETER_PID: {
6156 if (length >= sizeof(pid_t)) {
6157 memcpy(effective_pid, value, sizeof(pid_t));
6158 }
6159 break;
6160 }
6161 case NECP_CLIENT_PARAMETER_TRAFFIC_CLASS: {
6162 if (length >= sizeof(u_int32_t)) {
6163 memcpy(traffic_class, value, sizeof(u_int32_t));
6164 }
6165 break;
6166 }
6167 case NECP_CLIENT_PARAMETER_FALLBACK_MODE: {
6168 if (length >= sizeof(u_int8_t)) {
6169 memcpy(fallback_mode, value, sizeof(u_int8_t));
6170 }
6171 break;
6172 }
6173 // It is an implementation quirk that the remote address can be found in the necp parameters
6174 // while the local address must be retrieved from the flowswitch
6175 case NECP_CLIENT_PARAMETER_REMOTE_ADDRESS: {
6176 if (length >= sizeof(struct necp_policy_condition_addr)) {
6177 struct necp_policy_condition_addr *address_struct = (struct necp_policy_condition_addr *)(void *)value;
6178 if (necp_client_address_is_valid(&address_struct->address.sa)) {
6179 memcpy(remote, &address_struct->address, sizeof(address_struct->address));
6180 }
6181 }
6182 break;
6183 }
6184 default: {
6185 break;
6186 }
6187 }
6188 }
6189 }
6190 offset += sizeof(struct necp_tlv_header) + length;
6191 }
6192 }
6193
6194 // Called from NetworkStatistics when it wishes to collect latest information for a TCP flow.
6195 // It is a responsibility of NetworkStatistics to have previously zeroed any supplied memory.
6196 static bool
necp_request_tcp_netstats(userland_stats_provider_context * ctx,u_int16_t * ifflagsp,nstat_progress_digest * digestp,nstat_counts * countsp,void * metadatap)6197 necp_request_tcp_netstats(userland_stats_provider_context *ctx,
6198 u_int16_t *ifflagsp,
6199 nstat_progress_digest *digestp,
6200 nstat_counts *countsp,
6201 void *metadatap)
6202 {
6203 if (ctx == NULL) {
6204 return false;
6205 }
6206
6207 struct necp_client_flow_registration *flow_registration = (struct necp_client_flow_registration *)(uintptr_t)ctx;
6208 struct necp_client *client = flow_registration->client;
6209 struct necp_all_stats *ustats_kaddr = ((struct necp_all_kstats *)flow_registration->kstats_kaddr)->necp_stats_ustats;
6210 struct necp_tcp_stats *tcpstats = (struct necp_tcp_stats *)ustats_kaddr;
6211 ASSERT(tcpstats != NULL);
6212
6213 u_int16_t nstat_diagnostic_flags = 0;
6214
6215 // Retrieve details from the last time the assigned flows were updated
6216 u_int32_t route_ifindex = IFSCOPE_NONE;
6217 u_int16_t route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6218 u_int64_t combined_interface_details = 0;
6219
6220 atomic_get_64(combined_interface_details, &flow_registration->last_interface_details);
6221 split_interface_details(combined_interface_details, &route_ifindex, &route_ifflags);
6222
6223 if (route_ifindex == IFSCOPE_NONE) {
6224 // Mark no interface
6225 nstat_diagnostic_flags |= NSTAT_IFNET_ROUTE_VALUE_UNOBTAINABLE;
6226 route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6227 NECPLOG(LOG_INFO, "req tcp stats, failed to get route details for pid %d curproc %d %s\n",
6228 client->proc_pid, proc_pid(current_proc()), proc_best_name(current_proc()));
6229 }
6230
6231 if (ifflagsp) {
6232 *ifflagsp = route_ifflags | nstat_diagnostic_flags;
6233 if (tcpstats->necp_tcp_extra.flags1 & SOF1_CELLFALLBACK) {
6234 *ifflagsp |= NSTAT_IFNET_VIA_CELLFALLBACK;
6235 }
6236 if ((digestp == NULL) && (countsp == NULL) && (metadatap == NULL)) {
6237 return true;
6238 }
6239 }
6240
6241 if (digestp) {
6242 // The digest is intended to give information that may help give insight into the state of the link
6243 // while avoiding the need to do the relatively expensive flowswitch lookup
6244 digestp->rxbytes = tcpstats->necp_tcp_counts.necp_stat_rxbytes;
6245 digestp->txbytes = tcpstats->necp_tcp_counts.necp_stat_txbytes;
6246 digestp->rxduplicatebytes = tcpstats->necp_tcp_counts.necp_stat_rxduplicatebytes;
6247 digestp->rxoutoforderbytes = tcpstats->necp_tcp_counts.necp_stat_rxoutoforderbytes;
6248 digestp->txretransmit = tcpstats->necp_tcp_counts.necp_stat_txretransmit;
6249 digestp->ifindex = route_ifindex;
6250 digestp->state = tcpstats->necp_tcp_extra.state;
6251 digestp->txunacked = tcpstats->necp_tcp_extra.txunacked;
6252 digestp->txwindow = tcpstats->necp_tcp_extra.txwindow;
6253 digestp->connstatus.probe_activated = tcpstats->necp_tcp_extra.probestatus.probe_activated;
6254 digestp->connstatus.write_probe_failed = tcpstats->necp_tcp_extra.probestatus.write_probe_failed;
6255 digestp->connstatus.read_probe_failed = tcpstats->necp_tcp_extra.probestatus.read_probe_failed;
6256 digestp->connstatus.conn_probe_failed = tcpstats->necp_tcp_extra.probestatus.conn_probe_failed;
6257
6258 if ((countsp == NULL) && (metadatap == NULL)) {
6259 return true;
6260 }
6261 }
6262
6263 const struct sk_stats_flow *sf = &flow_registration->nexus_stats->fs_stats;
6264 if (sf == NULL) {
6265 nstat_diagnostic_flags |= NSTAT_IFNET_FLOWSWITCH_VALUE_UNOBTAINABLE;
6266 char namebuf[MAXCOMLEN + 1];
6267 (void) strlcpy(namebuf, "unknown", sizeof(namebuf));
6268 proc_name(client->proc_pid, namebuf, sizeof(namebuf));
6269 NECPLOG(LOG_ERR, "req tcp stats, necp_client flow_registration flow_stats missing for pid %d %s curproc %d %s\n",
6270 client->proc_pid, namebuf, proc_pid(current_proc()), proc_best_name(current_proc()));
6271 sf = &ntstat_sk_stats_zero;
6272 }
6273
6274 if (countsp) {
6275 countsp->nstat_rxbytes = tcpstats->necp_tcp_counts.necp_stat_rxbytes;
6276 countsp->nstat_txbytes = tcpstats->necp_tcp_counts.necp_stat_txbytes;
6277
6278 countsp->nstat_rxduplicatebytes = tcpstats->necp_tcp_counts.necp_stat_rxduplicatebytes;
6279 countsp->nstat_rxoutoforderbytes = tcpstats->necp_tcp_counts.necp_stat_rxoutoforderbytes;
6280 countsp->nstat_txretransmit = tcpstats->necp_tcp_counts.necp_stat_txretransmit;
6281
6282 countsp->nstat_min_rtt = tcpstats->necp_tcp_counts.necp_stat_min_rtt;
6283 countsp->nstat_avg_rtt = tcpstats->necp_tcp_counts.necp_stat_avg_rtt;
6284 countsp->nstat_var_rtt = tcpstats->necp_tcp_counts.necp_stat_var_rtt;
6285
6286 countsp->nstat_connectattempts = tcpstats->necp_tcp_extra.state >= TCPS_SYN_SENT ? 1 : 0;
6287 countsp->nstat_connectsuccesses = tcpstats->necp_tcp_extra.state >= TCPS_ESTABLISHED ? 1 : 0;
6288
6289 // Supplement what the user level has told us with what we know from the flowswitch
6290 countsp->nstat_rxpackets = sf->sf_ipackets;
6291 countsp->nstat_txpackets = sf->sf_opackets;
6292 if (route_ifflags & NSTAT_IFNET_IS_CELLULAR) {
6293 countsp->nstat_cell_rxbytes = sf->sf_ibytes;
6294 countsp->nstat_cell_txbytes = sf->sf_obytes;
6295 } else if (route_ifflags & NSTAT_IFNET_IS_WIFI) {
6296 countsp->nstat_wifi_rxbytes = sf->sf_ibytes;
6297 countsp->nstat_wifi_txbytes = sf->sf_obytes;
6298 } else if (route_ifflags & NSTAT_IFNET_IS_WIRED) {
6299 countsp->nstat_wired_rxbytes = sf->sf_ibytes;
6300 countsp->nstat_wired_txbytes = sf->sf_obytes;
6301 }
6302 }
6303
6304 if (metadatap) {
6305 nstat_tcp_descriptor *desc = (nstat_tcp_descriptor *)metadatap;
6306 memset(desc, 0, sizeof(*desc));
6307
6308 // Metadata from the flow registration
6309 uuid_copy(desc->fuuid, flow_registration->registration_id);
6310
6311 // Metadata that the necp client should have in TLV format.
6312 pid_t effective_pid = client->proc_pid;
6313 necp_find_netstat_data(client, (union necp_sockaddr_union *)&desc->remote, &effective_pid, desc->euuid, &desc->traffic_class, &desc->fallback_mode);
6314 desc->epid = (u_int32_t)effective_pid;
6315
6316 // Metadata from the flow registration
6317 // This needs to revisited if multiple flows are created from one flow registration
6318 struct necp_client_flow *flow = NULL;
6319 LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
6320 memcpy(&desc->local, &flow->local_addr, sizeof(desc->local));
6321 break;
6322 }
6323
6324 // Metadata from the route
6325 desc->ifindex = route_ifindex;
6326 desc->ifnet_properties = route_ifflags | nstat_diagnostic_flags;
6327 desc->ifnet_properties |= (sf->sf_flags & SFLOWF_ONLINK) ? NSTAT_IFNET_IS_LOCAL : NSTAT_IFNET_IS_NON_LOCAL;
6328 if (tcpstats->necp_tcp_extra.flags1 & SOF1_CELLFALLBACK) {
6329 desc->ifnet_properties |= NSTAT_IFNET_VIA_CELLFALLBACK;
6330 }
6331
6332 // Basic metadata from userland
6333 desc->rcvbufsize = tcpstats->necp_tcp_basic.rcvbufsize;
6334 desc->rcvbufused = tcpstats->necp_tcp_basic.rcvbufused;
6335
6336 // Additional TCP specific data
6337 desc->sndbufsize = tcpstats->necp_tcp_extra.sndbufsize;
6338 desc->sndbufused = tcpstats->necp_tcp_extra.sndbufused;
6339 desc->txunacked = tcpstats->necp_tcp_extra.txunacked;
6340 desc->txwindow = tcpstats->necp_tcp_extra.txwindow;
6341 desc->txcwindow = tcpstats->necp_tcp_extra.txcwindow;
6342 desc->traffic_mgt_flags = tcpstats->necp_tcp_extra.traffic_mgt_flags;
6343 desc->state = tcpstats->necp_tcp_extra.state;
6344
6345 u_int32_t cc_alg_index = tcpstats->necp_tcp_extra.cc_alg_index;
6346 if (cc_alg_index < TCP_CC_ALGO_COUNT) {
6347 strlcpy(desc->cc_algo, tcp_cc_algo_list[cc_alg_index]->name, sizeof(desc->cc_algo));
6348 } else {
6349 strlcpy(desc->cc_algo, "unknown", sizeof(desc->cc_algo));
6350 }
6351
6352 desc->connstatus.probe_activated = tcpstats->necp_tcp_extra.probestatus.probe_activated;
6353 desc->connstatus.write_probe_failed = tcpstats->necp_tcp_extra.probestatus.write_probe_failed;
6354 desc->connstatus.read_probe_failed = tcpstats->necp_tcp_extra.probestatus.read_probe_failed;
6355 desc->connstatus.conn_probe_failed = tcpstats->necp_tcp_extra.probestatus.conn_probe_failed;
6356
6357 memcpy(&desc->activity_bitmap, &sf->sf_activity, sizeof(sf->sf_activity));
6358 }
6359
6360 return true;
6361 }
6362
6363 // Called from NetworkStatistics when it wishes to collect latest information for a UDP flow.
6364 static bool
necp_request_udp_netstats(userland_stats_provider_context * ctx,u_int16_t * ifflagsp,nstat_progress_digest * digestp,nstat_counts * countsp,void * metadatap)6365 necp_request_udp_netstats(userland_stats_provider_context *ctx,
6366 u_int16_t *ifflagsp,
6367 nstat_progress_digest *digestp,
6368 nstat_counts *countsp,
6369 void *metadatap)
6370 {
6371 #pragma unused(digestp)
6372
6373 if (ctx == NULL) {
6374 return false;
6375 }
6376
6377 struct necp_client_flow_registration *flow_registration = (struct necp_client_flow_registration *)(uintptr_t)ctx;
6378 struct necp_client *client = flow_registration->client;
6379 struct necp_all_stats *ustats_kaddr = ((struct necp_all_kstats *)flow_registration->kstats_kaddr)->necp_stats_ustats;
6380 struct necp_udp_stats *udpstats = (struct necp_udp_stats *)ustats_kaddr;
6381 ASSERT(udpstats != NULL);
6382
6383 u_int16_t nstat_diagnostic_flags = 0;
6384
6385 // Retrieve details from the last time the assigned flows were updated
6386 u_int32_t route_ifindex = IFSCOPE_NONE;
6387 u_int16_t route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6388 u_int64_t combined_interface_details = 0;
6389
6390 atomic_get_64(combined_interface_details, &flow_registration->last_interface_details);
6391 split_interface_details(combined_interface_details, &route_ifindex, &route_ifflags);
6392
6393 if (route_ifindex == IFSCOPE_NONE) {
6394 // Mark no interface
6395 nstat_diagnostic_flags |= NSTAT_IFNET_ROUTE_VALUE_UNOBTAINABLE;
6396 route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6397 NECPLOG(LOG_INFO, "req udp stats, failed to get route details for pid %d curproc %d %s\n",
6398 client->proc_pid, proc_pid(current_proc()), proc_best_name(current_proc()));
6399 }
6400
6401 if (ifflagsp) {
6402 *ifflagsp = route_ifflags | nstat_diagnostic_flags;
6403 if ((countsp == NULL) && (metadatap == NULL)) {
6404 return true;
6405 }
6406 }
6407 const struct sk_stats_flow *sf = &flow_registration->nexus_stats->fs_stats;
6408 if (sf == NULL) {
6409 nstat_diagnostic_flags |= NSTAT_IFNET_FLOWSWITCH_VALUE_UNOBTAINABLE;
6410 char namebuf[MAXCOMLEN + 1];
6411 (void) strlcpy(namebuf, "unknown", sizeof(namebuf));
6412 proc_name(client->proc_pid, namebuf, sizeof(namebuf));
6413 NECPLOG(LOG_ERR, "req udp stats, necp_client flow_registration flow_stats missing for pid %d %s curproc %d %s\n",
6414 client->proc_pid, namebuf, proc_pid(current_proc()), proc_best_name(current_proc()));
6415 sf = &ntstat_sk_stats_zero;
6416 }
6417
6418 if (countsp) {
6419 countsp->nstat_rxbytes = udpstats->necp_udp_counts.necp_stat_rxbytes;
6420 countsp->nstat_txbytes = udpstats->necp_udp_counts.necp_stat_txbytes;
6421
6422 countsp->nstat_rxduplicatebytes = udpstats->necp_udp_counts.necp_stat_rxduplicatebytes;
6423 countsp->nstat_rxoutoforderbytes = udpstats->necp_udp_counts.necp_stat_rxoutoforderbytes;
6424 countsp->nstat_txretransmit = udpstats->necp_udp_counts.necp_stat_txretransmit;
6425
6426 countsp->nstat_min_rtt = udpstats->necp_udp_counts.necp_stat_min_rtt;
6427 countsp->nstat_avg_rtt = udpstats->necp_udp_counts.necp_stat_avg_rtt;
6428 countsp->nstat_var_rtt = udpstats->necp_udp_counts.necp_stat_var_rtt;
6429
6430 // Supplement what the user level has told us with what we know from the flowswitch
6431 countsp->nstat_rxpackets = sf->sf_ipackets;
6432 countsp->nstat_txpackets = sf->sf_opackets;
6433 if (route_ifflags & NSTAT_IFNET_IS_CELLULAR) {
6434 countsp->nstat_cell_rxbytes = sf->sf_ibytes;
6435 countsp->nstat_cell_txbytes = sf->sf_obytes;
6436 } else if (route_ifflags & NSTAT_IFNET_IS_WIFI) {
6437 countsp->nstat_wifi_rxbytes = sf->sf_ibytes;
6438 countsp->nstat_wifi_txbytes = sf->sf_obytes;
6439 } else if (route_ifflags & NSTAT_IFNET_IS_WIRED) {
6440 countsp->nstat_wired_rxbytes = sf->sf_ibytes;
6441 countsp->nstat_wired_txbytes = sf->sf_obytes;
6442 }
6443 }
6444
6445 if (metadatap) {
6446 nstat_udp_descriptor *desc = (nstat_udp_descriptor *)metadatap;
6447 memset(desc, 0, sizeof(*desc));
6448
6449 // Metadata from the flow registration
6450 uuid_copy(desc->fuuid, flow_registration->registration_id);
6451
6452 // Metadata that the necp client should have in TLV format.
6453 pid_t effective_pid = client->proc_pid;
6454 necp_find_netstat_data(client, (union necp_sockaddr_union *)&desc->remote, &effective_pid, desc->euuid, &desc->traffic_class, &desc->fallback_mode);
6455 desc->epid = (u_int32_t)effective_pid;
6456
6457 // Metadata from the flow registration
6458 // This needs to revisited if multiple flows are created from one flow registration
6459 struct necp_client_flow *flow = NULL;
6460 LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
6461 memcpy(&desc->local, &flow->local_addr, sizeof(desc->local));
6462 break;
6463 }
6464
6465 // Metadata from the route
6466 desc->ifindex = route_ifindex;
6467 desc->ifnet_properties = route_ifflags | nstat_diagnostic_flags;
6468
6469 // Basic metadata is all that is required for UDP
6470 desc->rcvbufsize = udpstats->necp_udp_basic.rcvbufsize;
6471 desc->rcvbufused = udpstats->necp_udp_basic.rcvbufused;
6472
6473 memcpy(&desc->activity_bitmap, &sf->sf_activity, sizeof(sf->sf_activity));
6474 }
6475
6476 return true;
6477 }
6478
6479 // Called from NetworkStatistics when it wishes to collect latest information for a QUIC flow.
6480 //
6481 // TODO: For now it is an exact implementation as that of TCP.
6482 // Still to keep the logic separate for future divergence, keeping the routines separate.
6483 // It also seems there are lots of common code between existing implementations and
6484 // it would be good to refactor this logic at some point.
6485 static bool
necp_request_quic_netstats(userland_stats_provider_context * ctx,u_int16_t * ifflagsp,nstat_progress_digest * digestp,nstat_counts * countsp,void * metadatap)6486 necp_request_quic_netstats(userland_stats_provider_context *ctx,
6487 u_int16_t *ifflagsp,
6488 nstat_progress_digest *digestp,
6489 nstat_counts *countsp,
6490 void *metadatap)
6491 {
6492 if (ctx == NULL) {
6493 return false;
6494 }
6495
6496 struct necp_client_flow_registration *flow_registration = (struct necp_client_flow_registration *)(uintptr_t)ctx;
6497 struct necp_client *client = flow_registration->client;
6498 struct necp_all_stats *ustats_kaddr = ((struct necp_all_kstats *)flow_registration->kstats_kaddr)->necp_stats_ustats;
6499 struct necp_quic_stats *quicstats = (struct necp_quic_stats *)ustats_kaddr;
6500 ASSERT(quicstats != NULL);
6501
6502 u_int16_t nstat_diagnostic_flags = 0;
6503
6504 // Retrieve details from the last time the assigned flows were updated
6505 u_int32_t route_ifindex = IFSCOPE_NONE;
6506 u_int16_t route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6507 u_int64_t combined_interface_details = 0;
6508
6509 atomic_get_64(combined_interface_details, &flow_registration->last_interface_details);
6510 split_interface_details(combined_interface_details, &route_ifindex, &route_ifflags);
6511
6512 if (route_ifindex == IFSCOPE_NONE) {
6513 // Mark no interface
6514 nstat_diagnostic_flags |= NSTAT_IFNET_ROUTE_VALUE_UNOBTAINABLE;
6515 route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6516 NECPLOG(LOG_INFO, "req quic stats, failed to get route details for pid %d curproc %d %s\n",
6517 client->proc_pid, proc_pid(current_proc()), proc_best_name(current_proc()));
6518 }
6519
6520 if (ifflagsp) {
6521 *ifflagsp = route_ifflags | nstat_diagnostic_flags;
6522 if ((digestp == NULL) && (countsp == NULL) && (metadatap == NULL)) {
6523 return true;
6524 }
6525 }
6526
6527 if (digestp) {
6528 // The digest is intended to give information that may help give insight into the state of the link
6529 // while avoiding the need to do the relatively expensive flowswitch lookup
6530 digestp->rxbytes = quicstats->necp_quic_counts.necp_stat_rxbytes;
6531 digestp->txbytes = quicstats->necp_quic_counts.necp_stat_txbytes;
6532 digestp->rxduplicatebytes = quicstats->necp_quic_counts.necp_stat_rxduplicatebytes;
6533 digestp->rxoutoforderbytes = quicstats->necp_quic_counts.necp_stat_rxoutoforderbytes;
6534 digestp->txretransmit = quicstats->necp_quic_counts.necp_stat_txretransmit;
6535 digestp->ifindex = route_ifindex;
6536 digestp->state = quicstats->necp_quic_extra.state;
6537 digestp->txunacked = quicstats->necp_quic_extra.txunacked;
6538 digestp->txwindow = quicstats->necp_quic_extra.txwindow;
6539
6540 if ((countsp == NULL) && (metadatap == NULL)) {
6541 return true;
6542 }
6543 }
6544
6545 const struct sk_stats_flow *sf = &flow_registration->nexus_stats->fs_stats;
6546 if (sf == NULL) {
6547 nstat_diagnostic_flags |= NSTAT_IFNET_FLOWSWITCH_VALUE_UNOBTAINABLE;
6548 char namebuf[MAXCOMLEN + 1];
6549 (void) strlcpy(namebuf, "unknown", sizeof(namebuf));
6550 proc_name(client->proc_pid, namebuf, sizeof(namebuf));
6551 NECPLOG(LOG_ERR, "req quic stats, necp_client flow_registration flow_stats missing for pid %d %s curproc %d %s\n",
6552 client->proc_pid, namebuf, proc_pid(current_proc()), proc_best_name(current_proc()));
6553 sf = &ntstat_sk_stats_zero;
6554 }
6555
6556 if (countsp) {
6557 countsp->nstat_rxbytes = quicstats->necp_quic_counts.necp_stat_rxbytes;
6558 countsp->nstat_txbytes = quicstats->necp_quic_counts.necp_stat_txbytes;
6559
6560 countsp->nstat_rxduplicatebytes = quicstats->necp_quic_counts.necp_stat_rxduplicatebytes;
6561 countsp->nstat_rxoutoforderbytes = quicstats->necp_quic_counts.necp_stat_rxoutoforderbytes;
6562 countsp->nstat_txretransmit = quicstats->necp_quic_counts.necp_stat_txretransmit;
6563
6564 countsp->nstat_min_rtt = quicstats->necp_quic_counts.necp_stat_min_rtt;
6565 countsp->nstat_avg_rtt = quicstats->necp_quic_counts.necp_stat_avg_rtt;
6566 countsp->nstat_var_rtt = quicstats->necp_quic_counts.necp_stat_var_rtt;
6567
6568 // TODO: It would be good to expose QUIC stats for CH/SH retransmission and connection state
6569 // Supplement what the user level has told us with what we know from the flowswitch
6570 countsp->nstat_rxpackets = sf->sf_ipackets;
6571 countsp->nstat_txpackets = sf->sf_opackets;
6572 if (route_ifflags & NSTAT_IFNET_IS_CELLULAR) {
6573 countsp->nstat_cell_rxbytes = sf->sf_ibytes;
6574 countsp->nstat_cell_txbytes = sf->sf_obytes;
6575 } else if (route_ifflags & NSTAT_IFNET_IS_WIFI) {
6576 countsp->nstat_wifi_rxbytes = sf->sf_ibytes;
6577 countsp->nstat_wifi_txbytes = sf->sf_obytes;
6578 } else if (route_ifflags & NSTAT_IFNET_IS_WIRED) {
6579 countsp->nstat_wired_rxbytes = sf->sf_ibytes;
6580 countsp->nstat_wired_txbytes = sf->sf_obytes;
6581 }
6582 }
6583
6584 if (metadatap) {
6585 nstat_quic_descriptor *desc = (nstat_quic_descriptor *)metadatap;
6586 memset(desc, 0, sizeof(*desc));
6587
6588 // Metadata from the flow registration
6589 uuid_copy(desc->fuuid, flow_registration->registration_id);
6590
6591 // Metadata, that the necp client should have, in TLV format.
6592 pid_t effective_pid = client->proc_pid;
6593 necp_find_netstat_data(client, (union necp_sockaddr_union *)&desc->remote, &effective_pid, desc->euuid, &desc->traffic_class, &desc->fallback_mode);
6594 desc->epid = (u_int32_t)effective_pid;
6595
6596 // Metadata from the flow registration
6597 // This needs to revisited if multiple flows are created from one flow registration
6598 struct necp_client_flow *flow = NULL;
6599 LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
6600 memcpy(&desc->local, &flow->local_addr, sizeof(desc->local));
6601 break;
6602 }
6603
6604 // Metadata from the route
6605 desc->ifindex = route_ifindex;
6606 desc->ifnet_properties = route_ifflags | nstat_diagnostic_flags;
6607
6608 // Basic metadata from userland
6609 desc->rcvbufsize = quicstats->necp_quic_basic.rcvbufsize;
6610 desc->rcvbufused = quicstats->necp_quic_basic.rcvbufused;
6611
6612 // Additional QUIC specific data
6613 desc->sndbufsize = quicstats->necp_quic_extra.sndbufsize;
6614 desc->sndbufused = quicstats->necp_quic_extra.sndbufused;
6615 desc->txunacked = quicstats->necp_quic_extra.txunacked;
6616 desc->txwindow = quicstats->necp_quic_extra.txwindow;
6617 desc->txcwindow = quicstats->necp_quic_extra.txcwindow;
6618 desc->traffic_mgt_flags = quicstats->necp_quic_extra.traffic_mgt_flags;
6619 desc->state = quicstats->necp_quic_extra.state;
6620
6621 // TODO: CC algo defines should be named agnostic of the protocol
6622 u_int32_t cc_alg_index = quicstats->necp_quic_extra.cc_alg_index;
6623 if (cc_alg_index < TCP_CC_ALGO_COUNT) {
6624 strlcpy(desc->cc_algo, tcp_cc_algo_list[cc_alg_index]->name, sizeof(desc->cc_algo));
6625 } else {
6626 strlcpy(desc->cc_algo, "unknown", sizeof(desc->cc_algo));
6627 }
6628
6629 memcpy(&desc->activity_bitmap, &sf->sf_activity, sizeof(sf->sf_activity));
6630 }
6631 return true;
6632 }
6633
6634 #endif /* SKYWALK */
6635
6636 // Support functions for NetworkStatistics support for necp_client connections
6637
6638 static void
necp_client_inherit_from_parent(struct necp_client * client,struct necp_client * parent)6639 necp_client_inherit_from_parent(
6640 struct necp_client *client,
6641 struct necp_client *parent)
6642 {
6643 assert(client->original_parameters_source == NULL);
6644
6645 if (parent->original_parameters_source != NULL) {
6646 client->original_parameters_source = parent->original_parameters_source;
6647 } else {
6648 client->original_parameters_source = parent;
6649 }
6650 necp_client_retain(client->original_parameters_source);
6651 }
6652
6653 static void
necp_find_conn_netstat_data(struct necp_client * client,u_int32_t * ntstat_flags,pid_t * effective_pid,uuid_t puuid,uuid_t euuid)6654 necp_find_conn_netstat_data(struct necp_client *client,
6655 u_int32_t *ntstat_flags,
6656 pid_t *effective_pid,
6657 uuid_t puuid,
6658 uuid_t euuid)
6659 {
6660 bool has_remote_address = false;
6661 bool has_ip_protocol = false;
6662 bool has_transport_protocol = false;
6663 size_t offset = 0;
6664 u_int8_t *parameters;
6665 u_int32_t parameters_size;
6666
6667
6668 parameters = client->parameters;
6669 parameters_size = (u_int32_t)client->parameters_length;
6670
6671 while ((offset + sizeof(struct necp_tlv_header)) <= parameters_size) {
6672 u_int8_t type = necp_buffer_get_tlv_type(parameters, offset);
6673 u_int32_t length = necp_buffer_get_tlv_length(parameters, offset);
6674
6675 if (length > (parameters_size - (offset + sizeof(struct necp_tlv_header)))) {
6676 // If the length is larger than what can fit in the remaining parameters size, bail
6677 NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
6678 break;
6679 }
6680
6681 if (length > 0) {
6682 u_int8_t *value = necp_buffer_get_tlv_value(parameters, offset, NULL);
6683 if (value != NULL) {
6684 switch (type) {
6685 case NECP_CLIENT_PARAMETER_APPLICATION: {
6686 if ((euuid) && (length >= sizeof(uuid_t))) {
6687 uuid_copy(euuid, value);
6688 }
6689 break;
6690 }
6691 case NECP_CLIENT_PARAMETER_IP_PROTOCOL: {
6692 if (length >= 1) {
6693 has_ip_protocol = true;
6694 }
6695 break;
6696 }
6697 case NECP_CLIENT_PARAMETER_PID: {
6698 if ((effective_pid) && length >= sizeof(pid_t)) {
6699 memcpy(effective_pid, value, sizeof(pid_t));
6700 }
6701 break;
6702 }
6703 case NECP_CLIENT_PARAMETER_PARENT_ID: {
6704 if ((puuid) && (length == sizeof(uuid_t))) {
6705 uuid_copy(puuid, value);
6706 }
6707 break;
6708 }
6709 // It is an implementation quirk that the remote address can be found in the necp parameters
6710 case NECP_CLIENT_PARAMETER_REMOTE_ADDRESS: {
6711 if (length >= sizeof(struct necp_policy_condition_addr)) {
6712 struct necp_policy_condition_addr *address_struct = (struct necp_policy_condition_addr *)(void *)value;
6713 if (necp_client_address_is_valid(&address_struct->address.sa)) {
6714 has_remote_address = true;
6715 }
6716 }
6717 break;
6718 }
6719 case NECP_CLIENT_PARAMETER_TRANSPORT_PROTOCOL: {
6720 if (length >= 1) {
6721 has_transport_protocol = true;
6722 }
6723 break;
6724 }
6725 default: {
6726 break;
6727 }
6728 }
6729 }
6730 }
6731 offset += sizeof(struct necp_tlv_header) + length;
6732 }
6733 if (ntstat_flags) {
6734 *ntstat_flags = (has_remote_address && has_ip_protocol && has_transport_protocol)? NSTAT_NECP_CONN_HAS_NET_ACCESS: 0;
6735 }
6736 }
6737
6738 static bool
necp_request_conn_netstats(nstat_provider_context ctx,u_int32_t * ifflagsp,nstat_counts * countsp,void * metadatap)6739 necp_request_conn_netstats(nstat_provider_context ctx,
6740 u_int32_t *ifflagsp,
6741 nstat_counts *countsp,
6742 void *metadatap)
6743 {
6744 if (ctx == NULL) {
6745 return false;
6746 }
6747 struct necp_client *client = (struct necp_client *)(uintptr_t)ctx;
6748 nstat_connection_descriptor *desc = (nstat_connection_descriptor *)metadatap;
6749
6750 if (ifflagsp) {
6751 necp_find_conn_netstat_data(client, ifflagsp, NULL, NULL, NULL);
6752 }
6753 if (countsp) {
6754 memset(countsp, 0, sizeof(*countsp));
6755 }
6756 if (desc) {
6757 memset(desc, 0, sizeof(*desc));
6758 // Metadata, that the necp client should have, in TLV format.
6759 pid_t effective_pid = client->proc_pid;
6760 necp_find_conn_netstat_data(client, &desc->ifnet_properties, &effective_pid, desc->puuid, desc->euuid);
6761 desc->epid = (u_int32_t)effective_pid;
6762
6763 // User level should obtain almost all connection information from an extension
6764 // leaving little to do here
6765 uuid_copy(desc->fuuid, client->latest_flow_registration_id);
6766 uuid_copy(desc->cuuid, client->client_id);
6767 }
6768 return true;
6769 }
6770
6771 static int
necp_skywalk_priv_check_cred(proc_t p,kauth_cred_t cred)6772 necp_skywalk_priv_check_cred(proc_t p, kauth_cred_t cred)
6773 {
6774 #pragma unused(p, cred)
6775 #if SKYWALK
6776 /* This includes Nexus controller and Skywalk observer privs */
6777 return skywalk_nxctl_check_privileges(p, cred);
6778 #else /* !SKYWALK */
6779 return 0;
6780 #endif /* !SKYWALK */
6781 }
6782
6783 /// System calls
6784
6785 int
necp_open(struct proc * p,struct necp_open_args * uap,int * retval)6786 necp_open(struct proc *p, struct necp_open_args *uap, int *retval)
6787 {
6788 #pragma unused(retval)
6789 int error = 0;
6790 struct necp_fd_data *fd_data = NULL;
6791 struct fileproc *fp = NULL;
6792 int fd = -1;
6793
6794 if (uap->flags & NECP_OPEN_FLAG_OBSERVER ||
6795 uap->flags & NECP_OPEN_FLAG_PUSH_OBSERVER) {
6796 if (necp_skywalk_priv_check_cred(p, kauth_cred_get()) != 0 &&
6797 priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_NETWORK_STATISTICS, 0) != 0) {
6798 NECPLOG0(LOG_ERR, "Client does not hold necessary entitlement to observe other NECP clients");
6799 error = EACCES;
6800 goto done;
6801 }
6802 }
6803
6804 #if CONFIG_MACF
6805 error = mac_necp_check_open(p, uap->flags);
6806 if (error) {
6807 goto done;
6808 }
6809 #endif /* MACF */
6810
6811 error = falloc(p, &fp, &fd, vfs_context_current());
6812 if (error != 0) {
6813 goto done;
6814 }
6815
6816 fd_data = kalloc_type(struct necp_fd_data, Z_WAITOK | Z_ZERO | Z_NOFAIL);
6817
6818 fd_data->necp_fd_type = necp_fd_type_client;
6819 fd_data->flags = uap->flags;
6820 RB_INIT(&fd_data->clients);
6821 RB_INIT(&fd_data->flows);
6822 TAILQ_INIT(&fd_data->update_list);
6823 lck_mtx_init(&fd_data->fd_lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
6824 klist_init(&fd_data->si.si_note);
6825 fd_data->proc_pid = proc_pid(p);
6826 #if SKYWALK
6827 LIST_INIT(&fd_data->stats_arena_list);
6828 #endif /* !SKYWALK */
6829
6830 fp->fp_flags |= FP_CLOEXEC | FP_CLOFORK;
6831 fp->fp_glob->fg_flag = FREAD;
6832 fp->fp_glob->fg_ops = &necp_fd_ops;
6833 fp_set_data(fp, fd_data);
6834
6835 proc_fdlock(p);
6836
6837 procfdtbl_releasefd(p, fd, NULL);
6838 fp_drop(p, fd, fp, 1);
6839
6840 *retval = fd;
6841
6842 if (fd_data->flags & NECP_OPEN_FLAG_PUSH_OBSERVER) {
6843 NECP_OBSERVER_LIST_LOCK_EXCLUSIVE();
6844 LIST_INSERT_HEAD(&necp_fd_observer_list, fd_data, chain);
6845 OSIncrementAtomic(&necp_observer_fd_count);
6846 NECP_OBSERVER_LIST_UNLOCK();
6847
6848 // Walk all existing clients and add them
6849 NECP_CLIENT_TREE_LOCK_SHARED();
6850 struct necp_client *existing_client = NULL;
6851 RB_FOREACH(existing_client, _necp_client_global_tree, &necp_client_global_tree) {
6852 NECP_CLIENT_LOCK(existing_client);
6853 necp_client_update_observer_add_internal(fd_data, existing_client);
6854 necp_client_update_observer_update_internal(fd_data, existing_client);
6855 NECP_CLIENT_UNLOCK(existing_client);
6856 }
6857 NECP_CLIENT_TREE_UNLOCK();
6858 } else {
6859 NECP_FD_LIST_LOCK_EXCLUSIVE();
6860 LIST_INSERT_HEAD(&necp_fd_list, fd_data, chain);
6861 OSIncrementAtomic(&necp_client_fd_count);
6862 NECP_FD_LIST_UNLOCK();
6863 }
6864
6865 proc_fdunlock(p);
6866
6867 done:
6868 if (error != 0) {
6869 if (fp != NULL) {
6870 fp_free(p, fd, fp);
6871 fp = NULL;
6872 }
6873 if (fd_data != NULL) {
6874 kfree_type(struct necp_fd_data, fd_data);
6875 }
6876 }
6877
6878 return error;
6879 }
6880
6881 // All functions called directly from necp_client_action() to handle one of the
6882 // types should be marked with NECP_CLIENT_ACTION_FUNCTION. This ensures that
6883 // necp_client_action() does not inline all the actions into a single function.
6884 #define NECP_CLIENT_ACTION_FUNCTION __attribute__((noinline))
6885
6886 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_add(struct proc * p,struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)6887 necp_client_add(struct proc *p, struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
6888 {
6889 int error = 0;
6890 struct necp_client *client = NULL;
6891 const size_t buffer_size = uap->buffer_size;
6892
6893 if (fd_data->flags & NECP_OPEN_FLAG_PUSH_OBSERVER) {
6894 NECPLOG0(LOG_ERR, "NECP client observers with push enabled may not add their own clients");
6895 return EINVAL;
6896 }
6897
6898 if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
6899 buffer_size == 0 || buffer_size > NECP_MAX_CLIENT_PARAMETERS_SIZE || uap->buffer == 0) {
6900 return EINVAL;
6901 }
6902
6903 client = kalloc_type(struct necp_client, Z_WAITOK | Z_ZERO | Z_NOFAIL);
6904 client->parameters = kalloc_data(buffer_size, Z_WAITOK | Z_NOFAIL);
6905 client->parameters_length = buffer_size;
6906 lck_mtx_init(&client->lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
6907 lck_mtx_init(&client->route_lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
6908
6909 error = copyin(uap->buffer, client->parameters, buffer_size);
6910 if (error) {
6911 NECPLOG(LOG_ERR, "necp_client_add parameters copyin error (%d)", error);
6912 goto done;
6913 }
6914
6915 os_ref_init(&client->reference_count, &necp_client_refgrp); // Hold our reference until close
6916
6917 client->proc_pid = fd_data->proc_pid; // Save off proc pid in case the client will persist past fd
6918 client->agent_handle = (void *)fd_data;
6919 client->platform_binary = ((csproc_get_platform_binary(p) == 0) ? 0 : 1);
6920
6921 necp_generate_client_id(client->client_id, false);
6922 LIST_INIT(&client->assertion_list);
6923 RB_INIT(&client->flow_registrations);
6924
6925 NECP_CLIENT_LOG(client, "Adding client");
6926
6927 error = copyout(client->client_id, uap->client_id, sizeof(uuid_t));
6928 if (error) {
6929 NECPLOG(LOG_ERR, "necp_client_add client_id copyout error (%d)", error);
6930 goto done;
6931 }
6932
6933 #if SKYWALK
6934 struct necp_client_parsed_parameters parsed_parameters = {};
6935 int parse_error = necp_client_parse_parameters(client, client->parameters, (u_int32_t)client->parameters_length, &parsed_parameters);
6936
6937 if (parse_error == 0 &&
6938 ((parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_DELEGATED_UPID) ||
6939 (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_ATTRIBUTED_BUNDLE_IDENTIFIER))) {
6940 bool has_delegation_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_SOCKET_DELEGATE, 0) == 0);
6941 if (!has_delegation_entitlement) {
6942 if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_DELEGATED_UPID) {
6943 NECPLOG(LOG_ERR, "%s(%d) does not hold the necessary entitlement to delegate network traffic for other processes by upid",
6944 proc_name_address(p), proc_pid(p));
6945 }
6946 if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_ATTRIBUTED_BUNDLE_IDENTIFIER) {
6947 NECPLOG(LOG_ERR, "%s(%d) does not hold the necessary entitlement to set attributed bundle identifier",
6948 proc_name_address(p), proc_pid(p));
6949 }
6950 error = EPERM;
6951 goto done;
6952 }
6953
6954 if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_DELEGATED_UPID) {
6955 // Save off delegated unique PID
6956 client->delegated_upid = parsed_parameters.delegated_upid;
6957 }
6958 }
6959
6960 if (parse_error == 0 && parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_INTERPOSE) {
6961 bool has_nexus_entitlement = (necp_skywalk_priv_check_cred(p, kauth_cred_get()) == 0);
6962 if (!has_nexus_entitlement) {
6963 NECPLOG(LOG_ERR, "%s(%d) does not hold the necessary entitlement to open a custom nexus client",
6964 proc_name_address(p), proc_pid(p));
6965 error = EPERM;
6966 goto done;
6967 }
6968 }
6969
6970 if (parse_error == 0 && (parsed_parameters.flags &
6971 (NECP_CLIENT_PARAMETER_FLAG_CUSTOM_ETHER | NECP_CLIENT_PARAMETER_FLAG_CUSTOM_IP))) {
6972 bool has_custom_protocol_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_CUSTOM_PROTOCOL, 0) == 0);
6973 if (!has_custom_protocol_entitlement) {
6974 NECPLOG(LOG_ERR, "%s(%d) does not hold the necessary entitlement for custom protocol APIs",
6975 proc_name_address(p), proc_pid(p));
6976 error = EPERM;
6977 goto done;
6978 }
6979 }
6980
6981 if (parse_error == 0 && parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER &&
6982 (parsed_parameters.ip_protocol == IPPROTO_TCP || parsed_parameters.ip_protocol == IPPROTO_UDP)) {
6983 uint32_t *netns_addr = NULL;
6984 uint8_t netns_addr_len = 0;
6985 struct ns_flow_info flow_info = {};
6986 uint32_t netns_flags = NETNS_LISTENER;
6987 uuid_copy(flow_info.nfi_flow_uuid, client->client_id);
6988 flow_info.nfi_protocol = parsed_parameters.ip_protocol;
6989 flow_info.nfi_owner_pid = client->proc_pid;
6990 if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID) {
6991 flow_info.nfi_effective_pid = parsed_parameters.effective_pid;
6992 } else {
6993 flow_info.nfi_effective_pid = flow_info.nfi_owner_pid;
6994 }
6995 proc_name(flow_info.nfi_owner_pid, flow_info.nfi_owner_name, MAXCOMLEN);
6996 proc_name(flow_info.nfi_effective_pid, flow_info.nfi_effective_name, MAXCOMLEN);
6997
6998 if (parsed_parameters.local_addr.sa.sa_family == AF_UNSPEC) {
6999 // Treat no local address as a wildcard IPv6
7000 // parsed_parameters is already initialized to all zeros
7001 parsed_parameters.local_addr.sin6.sin6_family = AF_INET6;
7002 parsed_parameters.local_addr.sin6.sin6_len = sizeof(struct sockaddr_in6);
7003 }
7004
7005 switch (parsed_parameters.local_addr.sa.sa_family) {
7006 case AF_INET: {
7007 memcpy(&flow_info.nfi_laddr, &parsed_parameters.local_addr.sa, parsed_parameters.local_addr.sa.sa_len);
7008 netns_addr = (uint32_t *)&parsed_parameters.local_addr.sin.sin_addr;
7009 netns_addr_len = 4;
7010 break;
7011 }
7012 case AF_INET6: {
7013 memcpy(&flow_info.nfi_laddr, &parsed_parameters.local_addr.sa, parsed_parameters.local_addr.sa.sa_len);
7014 netns_addr = (uint32_t *)&parsed_parameters.local_addr.sin6.sin6_addr;
7015 netns_addr_len = 16;
7016 break;
7017 }
7018
7019 default: {
7020 NECPLOG(LOG_ERR, "necp_client_add listener invalid address family (%d)", parsed_parameters.local_addr.sa.sa_family);
7021 error = EINVAL;
7022 goto done;
7023 }
7024 }
7025 if ((parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) &&
7026 (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_REUSE_LOCAL)) {
7027 netns_flags |= NETNS_REUSEPORT;
7028 }
7029 if (parsed_parameters.local_addr.sin.sin_port == 0) {
7030 error = netns_reserve_ephemeral(&client->port_reservation, netns_addr, netns_addr_len, parsed_parameters.ip_protocol,
7031 &parsed_parameters.local_addr.sin.sin_port, netns_flags, &flow_info);
7032 if (error) {
7033 NECPLOG(LOG_ERR, "necp_client_add netns_reserve_ephemeral error (%d)", error);
7034 goto done;
7035 }
7036
7037 // Update the parameter TLVs with the assigned port
7038 necp_client_update_local_port_parameters(client->parameters, (u_int32_t)client->parameters_length, parsed_parameters.local_addr.sin.sin_port);
7039 } else {
7040 error = netns_reserve(&client->port_reservation, netns_addr, netns_addr_len, parsed_parameters.ip_protocol,
7041 parsed_parameters.local_addr.sin.sin_port, netns_flags, &flow_info);
7042 if (error) {
7043 NECPLOG(LOG_ERR, "necp_client_add netns_reserve error (%d)", error);
7044 goto done;
7045 }
7046 }
7047 }
7048
7049 struct necp_client *parent = NULL;
7050 uuid_t parent_client_id;
7051 uuid_clear(parent_client_id);
7052 struct necp_client_nexus_parameters parent_parameters = {};
7053 uint16_t num_flow_regs = 0;
7054 if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_PARENT_UUID) {
7055 // The parent "should" be found on fd_data without having to search across the whole necp_fd_list
7056 // It would be nice to do this a little further down where there's another instance of NECP_FD_LOCK
7057 // but the logic here depends on the parse paramters
7058 NECP_FD_LOCK(fd_data);
7059 parent = necp_client_fd_find_client_unlocked(fd_data, parsed_parameters.parent_uuid);
7060 if (parent != NULL) {
7061 necp_client_inherit_from_parent(client, parent);
7062 necp_client_copy_parameters_locked(client, &parent_parameters);
7063 uuid_copy(parent_client_id, parsed_parameters.parent_uuid);
7064 struct necp_client_flow_registration *flow_registration = NULL;
7065 RB_FOREACH(flow_registration, _necp_client_flow_tree, &parent->flow_registrations) {
7066 num_flow_regs++;
7067 }
7068 }
7069 NECP_FD_UNLOCK(fd_data);
7070 if (parent == NULL) {
7071 NECPLOG0(LOG_ERR, "necp_client_add, no necp_client_inherit_from_parent as can't find parent on fd_data");
7072 }
7073 }
7074 if (parse_error == 0 && parent != NULL && parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLOW_DEMUX_PATTERN) {
7075 do {
7076 if (parsed_parameters.demux_patterns[0].len == 0) {
7077 NECPLOG0(LOG_INFO, "necp_client_add, child does not have a demux pattern");
7078 break;
7079 }
7080
7081 if (uuid_is_null(parent_client_id)) {
7082 NECPLOG0(LOG_INFO, "necp_client_add, parent ID is null");
7083 break;
7084 }
7085
7086 if (num_flow_regs > 1) {
7087 NECPLOG0(LOG_INFO, "necp_client_add, multiple parent flows not supported");
7088 break;
7089 }
7090 if (parsed_parameters.ip_protocol != IPPROTO_UDP) {
7091 NECPLOG(LOG_INFO, "necp_client_add, flow demux pattern not supported for %d protocol",
7092 parsed_parameters.ip_protocol);
7093 break;
7094 }
7095 if (parsed_parameters.ip_protocol != parent_parameters.transport_protocol) {
7096 NECPLOG0(LOG_INFO, "necp_client_add, parent/child ip protocol mismatch");
7097 break;
7098 }
7099 if (parsed_parameters.local_addr.sa.sa_family != AF_INET && parsed_parameters.local_addr.sa.sa_family != AF_INET6) {
7100 NECPLOG(LOG_INFO, "necp_client_add, flow demux pattern not supported for %d family",
7101 parsed_parameters.local_addr.sa.sa_family);
7102 break;
7103 }
7104 if (parsed_parameters.local_addr.sa.sa_family != parsed_parameters.remote_addr.sa.sa_family) {
7105 NECPLOG0(LOG_INFO, "necp_client_add, local/remote address family mismatch");
7106 break;
7107 }
7108 if (parsed_parameters.local_addr.sa.sa_family != parent_parameters.local_addr.sa.sa_family) {
7109 NECPLOG0(LOG_INFO, "necp_client_add, parent/child address family mismatch");
7110 break;
7111 }
7112 if (memcmp(&parsed_parameters.local_addr.sa, &parent_parameters.local_addr.sa, parsed_parameters.local_addr.sa.sa_len)) {
7113 NECPLOG0(LOG_INFO, "necp_client_add, parent/child local address mismatch");
7114 break;
7115 }
7116 if (memcmp(&parsed_parameters.remote_addr.sa, &parent_parameters.remote_addr.sa, parsed_parameters.remote_addr.sa.sa_len)) {
7117 NECPLOG0(LOG_INFO, "necp_client_add, parent/child remote address mismatch");
7118 break;
7119 }
7120 if (parsed_parameters.local_addr.sin.sin_port != parent_parameters.local_addr.sin.sin_port) {
7121 NECPLOG0(LOG_INFO, "necp_client_add, parent/child local port mismatch");
7122 break;
7123 }
7124 if (parsed_parameters.remote_addr.sin.sin_port != parent_parameters.remote_addr.sin.sin_port) {
7125 NECPLOG0(LOG_INFO, "necp_client_add, parent/child remote port mismatch");
7126 break;
7127 }
7128 client->validated_parent = 1;
7129 uuid_copy(client->parent_client_id, parent_client_id);
7130 } while (false);
7131 }
7132
7133 #endif /* !SKYWALK */
7134
7135 necp_client_update_observer_add(client);
7136
7137 NECP_FD_LOCK(fd_data);
7138 RB_INSERT(_necp_client_tree, &fd_data->clients, client);
7139 OSIncrementAtomic(&necp_client_count);
7140 NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
7141 RB_INSERT(_necp_client_global_tree, &necp_client_global_tree, client);
7142 NECP_CLIENT_TREE_UNLOCK();
7143
7144 // Prime the client result
7145 NECP_CLIENT_LOCK(client);
7146 (void)necp_update_client_result(current_proc(), fd_data, client, NULL);
7147 necp_client_retain_locked(client);
7148 NECP_CLIENT_UNLOCK(client);
7149 NECP_FD_UNLOCK(fd_data);
7150 // Now everything is set, it's safe to plumb this in to NetworkStatistics
7151 uint32_t ntstat_properties = 0;
7152 necp_find_conn_netstat_data(client, &ntstat_properties, NULL, NULL, NULL);
7153
7154 client->nstat_context = nstat_provider_stats_open((nstat_provider_context)client,
7155 NSTAT_PROVIDER_CONN_USERLAND, (u_int64_t)ntstat_properties, necp_request_conn_netstats, necp_find_conn_extension_info);
7156 necp_client_release(client);
7157 done:
7158 if (error != 0 && client != NULL) {
7159 necp_client_free(client);
7160 client = NULL;
7161 }
7162 *retval = error;
7163
7164 return error;
7165 }
7166
7167 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_claim(struct proc * p,struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)7168 necp_client_claim(struct proc *p, struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7169 {
7170 int error = 0;
7171 uuid_t client_id = {};
7172 struct necp_client *client = NULL;
7173
7174 if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
7175 error = EINVAL;
7176 goto done;
7177 }
7178
7179 error = copyin(uap->client_id, client_id, sizeof(uuid_t));
7180 if (error) {
7181 NECPLOG(LOG_ERR, "necp_client_claim copyin client_id error (%d)", error);
7182 goto done;
7183 }
7184
7185 if (necp_client_id_is_flow(client_id)) {
7186 NECPLOG0(LOG_ERR, "necp_client_claim cannot claim from flow UUID");
7187 error = EINVAL;
7188 goto done;
7189 }
7190
7191 u_int64_t upid = proc_uniqueid(p);
7192
7193 NECP_FD_LIST_LOCK_SHARED();
7194
7195 struct necp_fd_data *find_fd = NULL;
7196 LIST_FOREACH(find_fd, &necp_fd_list, chain) {
7197 NECP_FD_LOCK(find_fd);
7198 struct necp_client *find_client = necp_client_fd_find_client_and_lock(find_fd, client_id);
7199 if (find_client != NULL) {
7200 if (find_client->delegated_upid == upid) {
7201 // Matched the client to claim; remove from the old fd
7202 client = find_client;
7203 RB_REMOVE(_necp_client_tree, &find_fd->clients, client);
7204 necp_client_retain_locked(client);
7205 }
7206 NECP_CLIENT_UNLOCK(find_client);
7207 }
7208 NECP_FD_UNLOCK(find_fd);
7209
7210 if (client != NULL) {
7211 break;
7212 }
7213 }
7214
7215 NECP_FD_LIST_UNLOCK();
7216
7217 if (client == NULL) {
7218 error = ENOENT;
7219 goto done;
7220 }
7221
7222 client->proc_pid = fd_data->proc_pid; // Transfer client to claiming pid
7223 client->agent_handle = (void *)fd_data;
7224 client->platform_binary = ((csproc_get_platform_binary(p) == 0) ? 0 : 1);
7225
7226 NECP_CLIENT_LOG(client, "Claiming client");
7227
7228 // Add matched client to our fd and re-run result
7229 NECP_FD_LOCK(fd_data);
7230 RB_INSERT(_necp_client_tree, &fd_data->clients, client);
7231 NECP_CLIENT_LOCK(client);
7232 (void)necp_update_client_result(current_proc(), fd_data, client, NULL);
7233 NECP_CLIENT_UNLOCK(client);
7234 NECP_FD_UNLOCK(fd_data);
7235
7236 necp_client_release(client);
7237
7238 done:
7239 *retval = error;
7240
7241 return error;
7242 }
7243
7244 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_remove(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)7245 necp_client_remove(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7246 {
7247 int error = 0;
7248 uuid_t client_id = {};
7249 struct ifnet_stats_per_flow flow_ifnet_stats = {};
7250 const size_t buffer_size = uap->buffer_size;
7251
7252 if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
7253 error = EINVAL;
7254 goto done;
7255 }
7256
7257 error = copyin(uap->client_id, client_id, sizeof(uuid_t));
7258 if (error) {
7259 NECPLOG(LOG_ERR, "necp_client_remove copyin client_id error (%d)", error);
7260 goto done;
7261 }
7262
7263 if (uap->buffer != 0 && buffer_size == sizeof(flow_ifnet_stats)) {
7264 error = copyin(uap->buffer, &flow_ifnet_stats, buffer_size);
7265 if (error) {
7266 NECPLOG(LOG_ERR, "necp_client_remove flow_ifnet_stats copyin error (%d)", error);
7267 // Not fatal; make sure to zero-out stats in case of partial copy
7268 memset(&flow_ifnet_stats, 0, sizeof(flow_ifnet_stats));
7269 error = 0;
7270 }
7271 } else if (uap->buffer != 0) {
7272 NECPLOG(LOG_ERR, "necp_client_remove unexpected parameters length (%zu)", buffer_size);
7273 }
7274
7275 NECP_FD_LOCK(fd_data);
7276
7277 pid_t pid = fd_data->proc_pid;
7278 struct necp_client *client = necp_client_fd_find_client_unlocked(fd_data, client_id);
7279
7280 NECP_CLIENT_LOG(client, "Removing client");
7281
7282 if (client != NULL) {
7283 // Remove any flow registrations that match
7284 struct necp_client_flow_registration *flow_registration = NULL;
7285 struct necp_client_flow_registration *temp_flow_registration = NULL;
7286 RB_FOREACH_SAFE(flow_registration, _necp_fd_flow_tree, &fd_data->flows, temp_flow_registration) {
7287 if (flow_registration->client == client) {
7288 #if SKYWALK
7289 necp_destroy_flow_stats(fd_data, flow_registration, NULL, TRUE);
7290 #endif /* SKYWALK */
7291 NECP_FLOW_TREE_LOCK_EXCLUSIVE();
7292 RB_REMOVE(_necp_client_flow_global_tree, &necp_client_flow_global_tree, flow_registration);
7293 NECP_FLOW_TREE_UNLOCK();
7294 RB_REMOVE(_necp_fd_flow_tree, &fd_data->flows, flow_registration);
7295 }
7296 }
7297 #if SKYWALK
7298 if (client->nstat_context != NULL) {
7299 // Main path, we expect stats to be in existance at this point
7300 nstat_provider_stats_close(client->nstat_context);
7301 client->nstat_context = NULL;
7302 } else {
7303 NECPLOG0(LOG_ERR, "necp_client_remove ntstat shutdown finds nstat_context NULL");
7304 }
7305 #endif /* SKYWALK */
7306 // Remove client from lists
7307 NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
7308 RB_REMOVE(_necp_client_global_tree, &necp_client_global_tree, client);
7309 NECP_CLIENT_TREE_UNLOCK();
7310 RB_REMOVE(_necp_client_tree, &fd_data->clients, client);
7311 }
7312
7313 #if SKYWALK
7314 // If the currently-active arena is idle (has no more flows referring to it), or if there are defunct
7315 // arenas lingering in the list, schedule a threadcall to do the clean up. The idle check is done
7316 // by checking if the reference count is 3: one held by this client (will be released below when we
7317 // destroy it) when it's non-NULL; the rest held by stats_arena_{active,list}.
7318 if ((fd_data->stats_arena_active != NULL && fd_data->stats_arena_active->nai_use_count == 3) ||
7319 (fd_data->stats_arena_active == NULL && !LIST_EMPTY(&fd_data->stats_arena_list))) {
7320 uint64_t deadline = 0;
7321 uint64_t leeway = 0;
7322 clock_interval_to_deadline(necp_close_arenas_timeout_microseconds, NSEC_PER_USEC, &deadline);
7323 clock_interval_to_absolutetime_interval(necp_close_arenas_timeout_leeway_microseconds, NSEC_PER_USEC, &leeway);
7324
7325 thread_call_enter_delayed_with_leeway(necp_close_empty_arenas_tcall, NULL,
7326 deadline, leeway, THREAD_CALL_DELAY_LEEWAY);
7327 }
7328 #endif /* SKYWALK */
7329
7330 NECP_FD_UNLOCK(fd_data);
7331
7332 if (client != NULL) {
7333 ASSERT(error == 0);
7334 necp_destroy_client(client, pid, true);
7335 } else {
7336 error = ENOENT;
7337 NECPLOG(LOG_ERR, "necp_client_remove invalid client_id (%d)", error);
7338 }
7339 done:
7340 *retval = error;
7341
7342 return error;
7343 }
7344
7345 static struct necp_client_flow_registration *
necp_client_fd_find_flow(struct necp_fd_data * client_fd,uuid_t flow_id)7346 necp_client_fd_find_flow(struct necp_fd_data *client_fd, uuid_t flow_id)
7347 {
7348 NECP_FD_ASSERT_LOCKED(client_fd);
7349 struct necp_client_flow_registration *flow = NULL;
7350
7351 if (necp_client_id_is_flow(flow_id)) {
7352 struct necp_client_flow_registration find;
7353 uuid_copy(find.registration_id, flow_id);
7354 flow = RB_FIND(_necp_fd_flow_tree, &client_fd->flows, &find);
7355 }
7356
7357 return flow;
7358 }
7359
7360 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_remove_flow(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)7361 necp_client_remove_flow(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7362 {
7363 int error = 0;
7364 uuid_t flow_id = {};
7365 struct ifnet_stats_per_flow flow_ifnet_stats = {};
7366 const size_t buffer_size = uap->buffer_size;
7367
7368 if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
7369 error = EINVAL;
7370 NECPLOG(LOG_ERR, "necp_client_remove_flow invalid client_id (length %zu)", (size_t)uap->client_id_len);
7371 goto done;
7372 }
7373
7374 error = copyin(uap->client_id, flow_id, sizeof(uuid_t));
7375 if (error) {
7376 NECPLOG(LOG_ERR, "necp_client_remove_flow copyin client_id error (%d)", error);
7377 goto done;
7378 }
7379
7380 if (uap->buffer != 0 && buffer_size == sizeof(flow_ifnet_stats)) {
7381 error = copyin(uap->buffer, &flow_ifnet_stats, buffer_size);
7382 if (error) {
7383 NECPLOG(LOG_ERR, "necp_client_remove flow_ifnet_stats copyin error (%d)", error);
7384 // Not fatal
7385 }
7386 } else if (uap->buffer != 0) {
7387 NECPLOG(LOG_ERR, "necp_client_remove unexpected parameters length (%zu)", buffer_size);
7388 }
7389
7390 NECP_FD_LOCK(fd_data);
7391 struct necp_client *client = NULL;
7392 struct necp_client_flow_registration *flow_registration = necp_client_fd_find_flow(fd_data, flow_id);
7393 if (flow_registration != NULL) {
7394 #if SKYWALK
7395 // Cleanup stats per flow
7396 necp_destroy_flow_stats(fd_data, flow_registration, &flow_ifnet_stats, TRUE);
7397 #endif /* SKYWALK */
7398 NECP_FLOW_TREE_LOCK_EXCLUSIVE();
7399 RB_REMOVE(_necp_client_flow_global_tree, &necp_client_flow_global_tree, flow_registration);
7400 NECP_FLOW_TREE_UNLOCK();
7401 RB_REMOVE(_necp_fd_flow_tree, &fd_data->flows, flow_registration);
7402
7403 client = flow_registration->client;
7404 if (client != NULL) {
7405 necp_client_retain(client);
7406 }
7407 }
7408 NECP_FD_UNLOCK(fd_data);
7409
7410 NECP_CLIENT_FLOW_LOG(client, flow_registration, "removing flow");
7411
7412 if (flow_registration != NULL && client != NULL) {
7413 NECP_CLIENT_LOCK(client);
7414 if (flow_registration->client == client) {
7415 necp_destroy_client_flow_registration(client, flow_registration, fd_data->proc_pid, false);
7416 }
7417 necp_client_release_locked(client);
7418 NECP_CLIENT_UNLOCK(client);
7419 }
7420
7421 done:
7422 *retval = error;
7423 if (error != 0) {
7424 NECPLOG(LOG_ERR, "Remove flow error (%d)", error);
7425 }
7426
7427 return error;
7428 }
7429
7430 // Don't inline the function since it includes necp_client_parsed_parameters on the stack
7431 static __attribute__((noinline)) int
necp_client_check_tcp_heuristics(struct necp_client * client,struct necp_client_flow * flow,u_int32_t * flags,u_int8_t * tfo_cookie,u_int8_t * tfo_cookie_len)7432 necp_client_check_tcp_heuristics(struct necp_client *client, struct necp_client_flow *flow, u_int32_t *flags, u_int8_t *tfo_cookie, u_int8_t *tfo_cookie_len)
7433 {
7434 struct necp_client_parsed_parameters parsed_parameters;
7435 int error = 0;
7436
7437 error = necp_client_parse_parameters(client, client->parameters,
7438 (u_int32_t)client->parameters_length,
7439 &parsed_parameters);
7440 if (error) {
7441 NECPLOG(LOG_ERR, "necp_client_parse_parameters error (%d)", error);
7442 return error;
7443 }
7444
7445 if ((flow->remote_addr.sa.sa_family != AF_INET &&
7446 flow->remote_addr.sa.sa_family != AF_INET6) ||
7447 (flow->local_addr.sa.sa_family != AF_INET &&
7448 flow->local_addr.sa.sa_family != AF_INET6)) {
7449 return EINVAL;
7450 }
7451
7452 NECP_CLIENT_ROUTE_LOCK(client);
7453
7454 if (client->current_route == NULL) {
7455 error = ENOENT;
7456 goto do_unlock;
7457 }
7458
7459 bool check_ecn = false;
7460 do {
7461 if ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_ECN_ENABLE) ==
7462 NECP_CLIENT_PARAMETER_FLAG_ECN_ENABLE) {
7463 check_ecn = true;
7464 break;
7465 }
7466
7467 if ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_ECN_DISABLE) ==
7468 NECP_CLIENT_PARAMETER_FLAG_ECN_DISABLE) {
7469 break;
7470 }
7471
7472 if (client->current_route != NULL) {
7473 if (client->current_route->rt_ifp->if_eflags & IFEF_ECN_ENABLE) {
7474 check_ecn = true;
7475 break;
7476 }
7477 if (client->current_route->rt_ifp->if_eflags & IFEF_ECN_DISABLE) {
7478 break;
7479 }
7480 }
7481
7482 bool inbound = ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) == 0);
7483 if ((inbound && tcp_ecn_inbound == 1) ||
7484 (!inbound && tcp_ecn_outbound == 1)) {
7485 check_ecn = true;
7486 }
7487 } while (false);
7488
7489 if (check_ecn) {
7490 if (tcp_heuristic_do_ecn_with_address(client->current_route->rt_ifp,
7491 (union sockaddr_in_4_6 *)&flow->local_addr)) {
7492 *flags |= NECP_CLIENT_RESULT_FLAG_ECN_ENABLED;
7493 }
7494 }
7495
7496 if ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_TFO_ENABLE) ==
7497 NECP_CLIENT_PARAMETER_FLAG_TFO_ENABLE) {
7498 if (!tcp_heuristic_do_tfo_with_address(client->current_route->rt_ifp,
7499 (union sockaddr_in_4_6 *)&flow->local_addr,
7500 (union sockaddr_in_4_6 *)&flow->remote_addr,
7501 tfo_cookie, tfo_cookie_len)) {
7502 *flags |= NECP_CLIENT_RESULT_FLAG_FAST_OPEN_BLOCKED;
7503 *tfo_cookie_len = 0;
7504 }
7505 } else {
7506 *flags |= NECP_CLIENT_RESULT_FLAG_FAST_OPEN_BLOCKED;
7507 *tfo_cookie_len = 0;
7508 }
7509 do_unlock:
7510 NECP_CLIENT_ROUTE_UNLOCK(client);
7511
7512 return error;
7513 }
7514
7515 static size_t
necp_client_calculate_flow_tlv_size(struct necp_client_flow_registration * flow_registration)7516 necp_client_calculate_flow_tlv_size(struct necp_client_flow_registration *flow_registration)
7517 {
7518 size_t assigned_results_size = 0;
7519 struct necp_client_flow *flow = NULL;
7520 LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
7521 if (flow->assigned) {
7522 size_t header_length = 0;
7523 if (flow->nexus) {
7524 header_length = sizeof(struct necp_client_nexus_flow_header);
7525 } else {
7526 header_length = sizeof(struct necp_client_flow_header);
7527 }
7528 assigned_results_size += (header_length + flow->assigned_results_length);
7529
7530 if (flow->has_protoctl_event) {
7531 assigned_results_size += sizeof(struct necp_client_flow_protoctl_event_header);
7532 }
7533 }
7534 }
7535 return assigned_results_size;
7536 }
7537
7538 static int
necp_client_fillout_flow_tlvs(struct necp_client * client,bool client_is_observed,struct necp_client_flow_registration * flow_registration,struct necp_client_action_args * uap,size_t * assigned_results_cursor)7539 necp_client_fillout_flow_tlvs(struct necp_client *client,
7540 bool client_is_observed,
7541 struct necp_client_flow_registration *flow_registration,
7542 struct necp_client_action_args *uap,
7543 size_t *assigned_results_cursor)
7544 {
7545 int error = 0;
7546 struct necp_client_flow *flow = NULL;
7547 LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
7548 if (flow->assigned) {
7549 // Write TLV headers
7550 struct necp_client_nexus_flow_header header = {};
7551 u_int32_t length = 0;
7552 u_int32_t flags = 0;
7553 u_int8_t tfo_cookie_len = 0;
7554 u_int8_t type = 0;
7555
7556 type = NECP_CLIENT_RESULT_FLOW_ID;
7557 length = sizeof(header.flow_header.flow_id);
7558 memcpy(&header.flow_header.flow_id_tlv_header.type, &type, sizeof(type));
7559 memcpy(&header.flow_header.flow_id_tlv_header.length, &length, sizeof(length));
7560 uuid_copy(header.flow_header.flow_id, flow_registration->registration_id);
7561
7562 if (flow->nexus) {
7563 if (flow->check_tcp_heuristics) {
7564 u_int8_t tfo_cookie[NECP_TFO_COOKIE_LEN_MAX];
7565 tfo_cookie_len = NECP_TFO_COOKIE_LEN_MAX;
7566
7567 if (necp_client_check_tcp_heuristics(client, flow, &flags,
7568 tfo_cookie, &tfo_cookie_len) != 0) {
7569 tfo_cookie_len = 0;
7570 } else {
7571 flow->check_tcp_heuristics = FALSE;
7572
7573 if (tfo_cookie_len != 0) {
7574 type = NECP_CLIENT_RESULT_TFO_COOKIE;
7575 length = tfo_cookie_len;
7576 memcpy(&header.tfo_cookie_tlv_header.type, &type, sizeof(type));
7577 memcpy(&header.tfo_cookie_tlv_header.length, &length, sizeof(length));
7578 memcpy(&header.tfo_cookie_value, tfo_cookie, tfo_cookie_len);
7579 }
7580 }
7581 }
7582 }
7583
7584 size_t header_length = 0;
7585 if (flow->nexus) {
7586 if (tfo_cookie_len != 0) {
7587 header_length = sizeof(struct necp_client_nexus_flow_header) - (NECP_TFO_COOKIE_LEN_MAX - tfo_cookie_len);
7588 } else {
7589 header_length = sizeof(struct necp_client_nexus_flow_header) - sizeof(struct necp_tlv_header) - NECP_TFO_COOKIE_LEN_MAX;
7590 }
7591 } else {
7592 header_length = sizeof(struct necp_client_flow_header);
7593 }
7594
7595 type = NECP_CLIENT_RESULT_FLAGS;
7596 length = sizeof(header.flow_header.flags_value);
7597 memcpy(&header.flow_header.flags_tlv_header.type, &type, sizeof(type));
7598 memcpy(&header.flow_header.flags_tlv_header.length, &length, sizeof(length));
7599 if (flow->assigned) {
7600 flags |= NECP_CLIENT_RESULT_FLAG_FLOW_ASSIGNED;
7601 }
7602 if (flow->viable) {
7603 flags |= NECP_CLIENT_RESULT_FLAG_FLOW_VIABLE;
7604 }
7605 if (flow_registration->defunct) {
7606 flags |= NECP_CLIENT_RESULT_FLAG_DEFUNCT;
7607 }
7608 flags |= flow->necp_flow_flags;
7609 memcpy(&header.flow_header.flags_value, &flags, sizeof(flags));
7610
7611 type = NECP_CLIENT_RESULT_INTERFACE;
7612 length = sizeof(header.flow_header.interface_value);
7613 memcpy(&header.flow_header.interface_tlv_header.type, &type, sizeof(type));
7614 memcpy(&header.flow_header.interface_tlv_header.length, &length, sizeof(length));
7615
7616 struct necp_client_result_interface interface_struct;
7617 interface_struct.generation = 0;
7618 interface_struct.index = flow->interface_index;
7619
7620 memcpy(&header.flow_header.interface_value, &interface_struct, sizeof(interface_struct));
7621 if (flow->nexus) {
7622 type = NECP_CLIENT_RESULT_NETAGENT;
7623 length = sizeof(header.agent_value);
7624 memcpy(&header.agent_tlv_header.type, &type, sizeof(type));
7625 memcpy(&header.agent_tlv_header.length, &length, sizeof(length));
7626
7627 struct necp_client_result_netagent agent_struct;
7628 uuid_copy(agent_struct.netagent_uuid, flow->u.nexus_agent);
7629 agent_struct.generation = netagent_get_generation(agent_struct.netagent_uuid);
7630
7631 memcpy(&header.agent_value, &agent_struct, sizeof(agent_struct));
7632 }
7633
7634 // Don't include outer TLV header in length field
7635 type = NECP_CLIENT_RESULT_FLOW;
7636 length = (header_length - sizeof(struct necp_tlv_header) + flow->assigned_results_length);
7637 if (flow->has_protoctl_event) {
7638 length += sizeof(struct necp_client_flow_protoctl_event_header);
7639 }
7640 memcpy(&header.flow_header.outer_header.type, &type, sizeof(type));
7641 memcpy(&header.flow_header.outer_header.length, &length, sizeof(length));
7642
7643 error = copyout(&header, uap->buffer + client->result_length + *assigned_results_cursor, header_length);
7644 if (error) {
7645 NECPLOG(LOG_ERR, "necp_client_copy assigned results tlv_header copyout error (%d)", error);
7646 return error;
7647 }
7648 *assigned_results_cursor += header_length;
7649
7650 if (flow->assigned_results && flow->assigned_results_length) {
7651 // Write inner TLVs
7652 error = copyout(flow->assigned_results, uap->buffer + client->result_length + *assigned_results_cursor,
7653 flow->assigned_results_length);
7654 if (error) {
7655 NECPLOG(LOG_ERR, "necp_client_copy assigned results copyout error (%d)", error);
7656 return error;
7657 }
7658 }
7659 *assigned_results_cursor += flow->assigned_results_length;
7660
7661 /* Read the protocol event and reset it */
7662 if (flow->has_protoctl_event) {
7663 struct necp_client_flow_protoctl_event_header protoctl_event_header = {};
7664
7665 type = NECP_CLIENT_RESULT_PROTO_CTL_EVENT;
7666 length = sizeof(protoctl_event_header.protoctl_event);
7667
7668 memcpy(&protoctl_event_header.protoctl_tlv_header.type, &type, sizeof(type));
7669 memcpy(&protoctl_event_header.protoctl_tlv_header.length, &length, sizeof(length));
7670 memcpy(&protoctl_event_header.protoctl_event, &flow->protoctl_event,
7671 sizeof(flow->protoctl_event));
7672
7673 error = copyout(&protoctl_event_header, uap->buffer + client->result_length + *assigned_results_cursor,
7674 sizeof(protoctl_event_header));
7675
7676 if (error) {
7677 NECPLOG(LOG_ERR, "necp_client_copy protocol control event results"
7678 " tlv_header copyout error (%d)", error);
7679 return error;
7680 }
7681 *assigned_results_cursor += sizeof(protoctl_event_header);
7682 flow->has_protoctl_event = FALSE;
7683 flow->protoctl_event.protoctl_event_code = 0;
7684 flow->protoctl_event.protoctl_event_val = 0;
7685 flow->protoctl_event.protoctl_event_tcp_seq_num = 0;
7686 }
7687 }
7688 }
7689 if (!client_is_observed) {
7690 flow_registration->flow_result_read = TRUE;
7691 }
7692 return 0;
7693 }
7694
7695 static int
necp_client_copy_internal(struct necp_client * client,uuid_t client_id,bool client_is_observed,struct necp_client_action_args * uap,int * retval)7696 necp_client_copy_internal(struct necp_client *client, uuid_t client_id, bool client_is_observed, struct necp_client_action_args *uap, int *retval)
7697 {
7698 NECP_CLIENT_ASSERT_LOCKED(client);
7699 int error = 0;
7700 // Copy results out
7701 if (uap->action == NECP_CLIENT_ACTION_COPY_PARAMETERS) {
7702 if (uap->buffer_size < client->parameters_length) {
7703 return EINVAL;
7704 }
7705 error = copyout(client->parameters, uap->buffer, client->parameters_length);
7706 if (error) {
7707 NECPLOG(LOG_ERR, "necp_client_copy parameters copyout error (%d)", error);
7708 return error;
7709 }
7710 *retval = client->parameters_length;
7711 } else if (uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT &&
7712 client->result_read && client->group_members_read && !necp_client_has_unread_flows(client)) {
7713 // Copy updates only, but nothing to read
7714 // Just return 0 for bytes read
7715 *retval = 0;
7716 } else if (uap->action == NECP_CLIENT_ACTION_COPY_RESULT ||
7717 uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT) {
7718 size_t assigned_results_size = client->assigned_group_members_length;
7719
7720 bool some_flow_is_defunct = false;
7721 struct necp_client_flow_registration *single_flow_registration = NULL;
7722 if (necp_client_id_is_flow(client_id)) {
7723 single_flow_registration = necp_client_find_flow(client, client_id);
7724 if (single_flow_registration != NULL) {
7725 assigned_results_size += necp_client_calculate_flow_tlv_size(single_flow_registration);
7726 }
7727 } else {
7728 // This request is for the client, so copy everything
7729 struct necp_client_flow_registration *flow_registration = NULL;
7730 RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
7731 if (flow_registration->defunct) {
7732 some_flow_is_defunct = true;
7733 }
7734 assigned_results_size += necp_client_calculate_flow_tlv_size(flow_registration);
7735 }
7736 }
7737 if (uap->buffer_size < (client->result_length + assigned_results_size)) {
7738 return EINVAL;
7739 }
7740
7741 u_int32_t original_flags = 0;
7742 bool flags_updated = false;
7743 if (some_flow_is_defunct && client->legacy_client_is_flow) {
7744 // If our client expects the defunct flag in the client, add it now
7745 u_int32_t client_flags = 0;
7746 u_int32_t value_size = 0;
7747 u_int8_t *flags_pointer = necp_buffer_get_tlv_value(client->result, 0, &value_size);
7748 if (flags_pointer != NULL && value_size == sizeof(client_flags)) {
7749 memcpy(&client_flags, flags_pointer, value_size);
7750 original_flags = client_flags;
7751 client_flags |= NECP_CLIENT_RESULT_FLAG_DEFUNCT;
7752 (void)necp_buffer_write_tlv_if_different(client->result, NECP_CLIENT_RESULT_FLAGS,
7753 sizeof(client_flags), &client_flags, &flags_updated,
7754 client->result, sizeof(client->result));
7755 }
7756 }
7757
7758 error = copyout(client->result, uap->buffer, client->result_length);
7759
7760 if (flags_updated) {
7761 // Revert stored flags
7762 (void)necp_buffer_write_tlv_if_different(client->result, NECP_CLIENT_RESULT_FLAGS,
7763 sizeof(original_flags), &original_flags, &flags_updated,
7764 client->result, sizeof(client->result));
7765 }
7766
7767 if (error != 0) {
7768 NECPLOG(LOG_ERR, "necp_client_copy result copyout error (%d)", error);
7769 return error;
7770 }
7771
7772 if (client->assigned_group_members != NULL && client->assigned_group_members_length > 0) {
7773 error = copyout(client->assigned_group_members, uap->buffer + client->result_length, client->assigned_group_members_length);
7774 if (error != 0) {
7775 NECPLOG(LOG_ERR, "necp_client_copy group members copyout error (%d)", error);
7776 return error;
7777 }
7778 }
7779
7780 size_t assigned_results_cursor = client->assigned_group_members_length; // Start with an offset based on the group members
7781 if (necp_client_id_is_flow(client_id)) {
7782 if (single_flow_registration != NULL) {
7783 error = necp_client_fillout_flow_tlvs(client, client_is_observed, single_flow_registration, uap, &assigned_results_cursor);
7784 if (error != 0) {
7785 return error;
7786 }
7787 }
7788 } else {
7789 // This request is for the client, so copy everything
7790 struct necp_client_flow_registration *flow_registration = NULL;
7791 RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
7792 error = necp_client_fillout_flow_tlvs(client, client_is_observed, flow_registration, uap, &assigned_results_cursor);
7793 if (error != 0) {
7794 return error;
7795 }
7796 }
7797 }
7798
7799 *retval = client->result_length + assigned_results_cursor;
7800
7801 if (!client_is_observed) {
7802 client->result_read = TRUE;
7803 client->group_members_read = TRUE;
7804 }
7805 }
7806
7807 return 0;
7808 }
7809
7810 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_copy(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)7811 necp_client_copy(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7812 {
7813 int error = 0;
7814 struct necp_client *client = NULL;
7815 uuid_t client_id;
7816 uuid_clear(client_id);
7817
7818 *retval = 0;
7819
7820 if (uap->buffer_size == 0 || uap->buffer == 0) {
7821 return EINVAL;
7822 }
7823
7824 if (uap->action != NECP_CLIENT_ACTION_COPY_PARAMETERS &&
7825 uap->action != NECP_CLIENT_ACTION_COPY_RESULT &&
7826 uap->action != NECP_CLIENT_ACTION_COPY_UPDATED_RESULT) {
7827 return EINVAL;
7828 }
7829
7830 if (uap->client_id) {
7831 if (uap->client_id_len != sizeof(uuid_t)) {
7832 NECPLOG(LOG_ERR, "Incorrect length (got %zu, expected %zu)", (size_t)uap->client_id_len, sizeof(uuid_t));
7833 return ERANGE;
7834 }
7835
7836 error = copyin(uap->client_id, client_id, sizeof(uuid_t));
7837 if (error) {
7838 NECPLOG(LOG_ERR, "necp_client_copy client_id copyin error (%d)", error);
7839 return error;
7840 }
7841 }
7842
7843 const bool is_wildcard = (bool)uuid_is_null(client_id);
7844
7845 NECP_FD_LOCK(fd_data);
7846
7847 if (is_wildcard) {
7848 if (uap->action == NECP_CLIENT_ACTION_COPY_RESULT || uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT) {
7849 struct necp_client *find_client = NULL;
7850 RB_FOREACH(find_client, _necp_client_tree, &fd_data->clients) {
7851 NECP_CLIENT_LOCK(find_client);
7852 if (!find_client->result_read || !find_client->group_members_read || necp_client_has_unread_flows(find_client)) {
7853 client = find_client;
7854 // Leave the client locked, and break
7855 break;
7856 }
7857 NECP_CLIENT_UNLOCK(find_client);
7858 }
7859 }
7860 } else {
7861 client = necp_client_fd_find_client_and_lock(fd_data, client_id);
7862 }
7863
7864 if (client != NULL) {
7865 // If client is set, it is locked
7866 error = necp_client_copy_internal(client, client_id, FALSE, uap, retval);
7867 NECP_CLIENT_UNLOCK(client);
7868 }
7869
7870 // Unlock our own fd before moving on or returning
7871 NECP_FD_UNLOCK(fd_data);
7872
7873 if (client == NULL) {
7874 if (fd_data->flags & NECP_OPEN_FLAG_OBSERVER) {
7875 // Observers are allowed to lookup clients on other fds
7876
7877 // Lock tree
7878 NECP_CLIENT_TREE_LOCK_SHARED();
7879
7880 bool found_client = FALSE;
7881
7882 client = necp_find_client_and_lock(client_id);
7883 if (client != NULL) {
7884 // Matched, copy out data
7885 found_client = TRUE;
7886 error = necp_client_copy_internal(client, client_id, TRUE, uap, retval);
7887 NECP_CLIENT_UNLOCK(client);
7888 }
7889
7890 // Unlock tree
7891 NECP_CLIENT_TREE_UNLOCK();
7892
7893 // No client found, fail
7894 if (!found_client) {
7895 return ENOENT;
7896 }
7897 } else {
7898 // No client found, and not allowed to search other fds, fail
7899 return ENOENT;
7900 }
7901 }
7902
7903 return error;
7904 }
7905
7906 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_copy_client_update(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)7907 necp_client_copy_client_update(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7908 {
7909 int error = 0;
7910
7911 *retval = 0;
7912
7913 if (!(fd_data->flags & NECP_OPEN_FLAG_PUSH_OBSERVER)) {
7914 NECPLOG0(LOG_ERR, "NECP fd is not observer, cannot copy client update");
7915 return EINVAL;
7916 }
7917
7918 if (uap->client_id_len != sizeof(uuid_t) || uap->client_id == 0) {
7919 NECPLOG0(LOG_ERR, "Client id invalid, cannot copy client update");
7920 return EINVAL;
7921 }
7922
7923 if (uap->buffer_size == 0 || uap->buffer == 0) {
7924 NECPLOG0(LOG_ERR, "Buffer invalid, cannot copy client update");
7925 return EINVAL;
7926 }
7927
7928 NECP_FD_LOCK(fd_data);
7929 struct necp_client_update *client_update = TAILQ_FIRST(&fd_data->update_list);
7930 if (client_update != NULL) {
7931 TAILQ_REMOVE(&fd_data->update_list, client_update, chain);
7932 VERIFY(fd_data->update_count > 0);
7933 fd_data->update_count--;
7934 }
7935 NECP_FD_UNLOCK(fd_data);
7936
7937 if (client_update != NULL) {
7938 error = copyout(client_update->client_id, uap->client_id, sizeof(uuid_t));
7939 if (error) {
7940 NECPLOG(LOG_ERR, "Copy client update copyout client id error (%d)", error);
7941 } else {
7942 if (uap->buffer_size < client_update->update_length) {
7943 NECPLOG(LOG_ERR, "Buffer size cannot hold update (%zu < %zu)", (size_t)uap->buffer_size, client_update->update_length);
7944 error = EINVAL;
7945 } else {
7946 error = copyout(client_update->update, uap->buffer, client_update->update_length);
7947 if (error) {
7948 NECPLOG(LOG_ERR, "Copy client update copyout error (%d)", error);
7949 } else {
7950 *retval = client_update->update_length;
7951 }
7952 }
7953 }
7954
7955 necp_client_update_free(client_update);
7956 client_update = NULL;
7957 } else {
7958 error = ENOENT;
7959 }
7960
7961 return error;
7962 }
7963
7964 static int
necp_client_copy_parameters_locked(struct necp_client * client,struct necp_client_nexus_parameters * parameters)7965 necp_client_copy_parameters_locked(struct necp_client *client,
7966 struct necp_client_nexus_parameters *parameters)
7967 {
7968 VERIFY(parameters != NULL);
7969
7970 struct necp_client_parsed_parameters parsed_parameters = {};
7971 int error = necp_client_parse_parameters(client, client->parameters, (u_int32_t)client->parameters_length, &parsed_parameters);
7972
7973 parameters->pid = client->proc_pid;
7974 if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID) {
7975 parameters->epid = parsed_parameters.effective_pid;
7976 } else {
7977 parameters->epid = parameters->pid;
7978 }
7979 #if SKYWALK
7980 parameters->port_reservation = client->port_reservation;
7981 #endif /* !SKYWALK */
7982 memcpy(¶meters->local_addr, &parsed_parameters.local_addr, sizeof(parameters->local_addr));
7983 memcpy(¶meters->remote_addr, &parsed_parameters.remote_addr, sizeof(parameters->remote_addr));
7984 parameters->ip_protocol = parsed_parameters.ip_protocol;
7985 if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_TRANSPORT_PROTOCOL) {
7986 parameters->transport_protocol = parsed_parameters.transport_protocol;
7987 } else {
7988 parameters->transport_protocol = parsed_parameters.ip_protocol;
7989 }
7990 parameters->ethertype = parsed_parameters.ethertype;
7991 parameters->traffic_class = parsed_parameters.traffic_class;
7992 if (uuid_is_null(client->override_euuid)) {
7993 uuid_copy(parameters->euuid, parsed_parameters.effective_uuid);
7994 } else {
7995 uuid_copy(parameters->euuid, client->override_euuid);
7996 }
7997 parameters->is_listener = (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) ? 1 : 0;
7998 parameters->is_interpose = (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_INTERPOSE) ? 1 : 0;
7999 parameters->is_custom_ether = (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_CUSTOM_ETHER) ? 1 : 0;
8000 parameters->policy_id = client->policy_id;
8001
8002 // parse client result flag
8003 u_int32_t client_result_flags = 0;
8004 u_int32_t value_size = 0;
8005 u_int8_t *flags_pointer = NULL;
8006 flags_pointer = necp_buffer_get_tlv_value(client->result, 0, &value_size);
8007 if (flags_pointer && value_size == sizeof(client_result_flags)) {
8008 memcpy(&client_result_flags, flags_pointer, value_size);
8009 }
8010 parameters->allow_qos_marking = (client_result_flags & NECP_CLIENT_RESULT_FLAG_ALLOW_QOS_MARKING) ? 1 : 0;
8011
8012 if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR_PREFERENCE) {
8013 if (parsed_parameters.local_address_preference == NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_DEFAULT) {
8014 parameters->override_address_selection = false;
8015 } else if (parsed_parameters.local_address_preference == NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_TEMPORARY) {
8016 parameters->override_address_selection = true;
8017 parameters->use_stable_address = false;
8018 } else if (parsed_parameters.local_address_preference == NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_STABLE) {
8019 parameters->override_address_selection = true;
8020 parameters->use_stable_address = true;
8021 }
8022 } else {
8023 parameters->override_address_selection = false;
8024 }
8025
8026 if ((parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) &&
8027 (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_NO_WAKE_FROM_SLEEP)) {
8028 parameters->no_wake_from_sleep = true;
8029 }
8030
8031 if ((parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) &&
8032 (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_REUSE_LOCAL)) {
8033 parameters->reuse_port = true;
8034 }
8035
8036 #if SKYWALK
8037 if (!parameters->is_listener) {
8038 if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLOW_DEMUX_PATTERN) {
8039 if (parsed_parameters.demux_patterns[0].len == 0) {
8040 parameters->is_demuxable_parent = 1;
8041 } else {
8042 if (client->validated_parent) {
8043 ASSERT(!uuid_is_null(client->parent_client_id));
8044
8045 NECP_CLIENT_TREE_LOCK_SHARED();
8046 struct necp_client *parent = necp_find_client_and_lock(client->parent_client_id);
8047 if (parent != NULL) {
8048 struct necp_client_flow_registration *parent_flow_registration = NULL;
8049 RB_FOREACH(parent_flow_registration, _necp_client_flow_tree, &parent->flow_registrations) {
8050 uuid_copy(parameters->parent_flow_uuid, parent_flow_registration->registration_id);
8051 break;
8052 }
8053
8054 NECP_CLIENT_UNLOCK(parent);
8055 }
8056 NECP_CLIENT_TREE_UNLOCK();
8057
8058 if (parsed_parameters.demux_pattern_count > 0) {
8059 for (int i = 0; i < parsed_parameters.demux_pattern_count; i++) {
8060 memcpy(¶meters->demux_patterns[i], &parsed_parameters.demux_patterns[i], sizeof(struct necp_demux_pattern));
8061 }
8062 parameters->demux_pattern_count = parsed_parameters.demux_pattern_count;
8063 }
8064 }
8065 }
8066 }
8067 }
8068 #endif // SKYWALK
8069
8070 return error;
8071 }
8072
8073 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_list(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8074 necp_client_list(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8075 {
8076 int error = 0;
8077 struct necp_client *find_client = NULL;
8078 uuid_t *list = NULL;
8079 u_int32_t requested_client_count = 0;
8080 u_int32_t client_count = 0;
8081 size_t copy_buffer_size = 0;
8082
8083 if (uap->buffer_size < sizeof(requested_client_count) || uap->buffer == 0) {
8084 error = EINVAL;
8085 goto done;
8086 }
8087
8088 if (!(fd_data->flags & NECP_OPEN_FLAG_OBSERVER)) {
8089 NECPLOG0(LOG_ERR, "Client does not hold necessary entitlement to list other NECP clients");
8090 error = EACCES;
8091 goto done;
8092 }
8093
8094 error = copyin(uap->buffer, &requested_client_count, sizeof(requested_client_count));
8095 if (error) {
8096 goto done;
8097 }
8098
8099 if (os_mul_overflow(sizeof(uuid_t), requested_client_count, ©_buffer_size)) {
8100 error = ERANGE;
8101 goto done;
8102 }
8103
8104 if (uap->buffer_size - sizeof(requested_client_count) != copy_buffer_size) {
8105 error = EINVAL;
8106 goto done;
8107 }
8108
8109 if (copy_buffer_size > NECP_MAX_CLIENT_LIST_SIZE) {
8110 error = EINVAL;
8111 goto done;
8112 }
8113
8114 if (requested_client_count > 0) {
8115 if ((list = (uuid_t*)kalloc_data(copy_buffer_size, Z_WAITOK | Z_ZERO)) == NULL) {
8116 error = ENOMEM;
8117 goto done;
8118 }
8119 }
8120
8121 // Lock tree
8122 NECP_CLIENT_TREE_LOCK_SHARED();
8123
8124 find_client = NULL;
8125 RB_FOREACH(find_client, _necp_client_global_tree, &necp_client_global_tree) {
8126 NECP_CLIENT_LOCK(find_client);
8127 if (!uuid_is_null(find_client->client_id)) {
8128 if (client_count < requested_client_count) {
8129 uuid_copy(list[client_count], find_client->client_id);
8130 }
8131 client_count++;
8132 }
8133 NECP_CLIENT_UNLOCK(find_client);
8134 }
8135
8136 // Unlock tree
8137 NECP_CLIENT_TREE_UNLOCK();
8138
8139 error = copyout(&client_count, uap->buffer, sizeof(client_count));
8140 if (error) {
8141 NECPLOG(LOG_ERR, "necp_client_list buffer copyout error (%d)", error);
8142 goto done;
8143 }
8144
8145 if (requested_client_count > 0 &&
8146 client_count > 0 &&
8147 list != NULL) {
8148 error = copyout(list, uap->buffer + sizeof(client_count), copy_buffer_size);
8149 if (error) {
8150 NECPLOG(LOG_ERR, "necp_client_list client count copyout error (%d)", error);
8151 goto done;
8152 }
8153 }
8154 done:
8155 if (list != NULL) {
8156 kfree_data(list, copy_buffer_size);
8157 }
8158 *retval = error;
8159
8160 return error;
8161 }
8162
8163 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_add_flow(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8164 necp_client_add_flow(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8165 {
8166 int error = 0;
8167 struct necp_client *client = NULL;
8168 uuid_t client_id;
8169 struct necp_client_nexus_parameters parameters = {};
8170 struct proc *proc = PROC_NULL;
8171 struct necp_client_add_flow *add_request = NULL;
8172 struct necp_client_add_flow *allocated_add_request = NULL;
8173 struct necp_client_add_flow_default default_add_request = {};
8174 const size_t buffer_size = uap->buffer_size;
8175
8176 if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
8177 error = EINVAL;
8178 NECPLOG(LOG_ERR, "necp_client_add_flow invalid client_id (length %zu)", (size_t)uap->client_id_len);
8179 goto done;
8180 }
8181
8182 if (uap->buffer == 0 || buffer_size < sizeof(struct necp_client_add_flow) ||
8183 buffer_size > sizeof(struct necp_client_add_flow_default) * 4) {
8184 error = EINVAL;
8185 NECPLOG(LOG_ERR, "necp_client_add_flow invalid buffer (length %zu)", buffer_size);
8186 goto done;
8187 }
8188
8189 error = copyin(uap->client_id, client_id, sizeof(uuid_t));
8190 if (error) {
8191 NECPLOG(LOG_ERR, "necp_client_add_flow copyin client_id error (%d)", error);
8192 goto done;
8193 }
8194
8195 if (buffer_size <= sizeof(struct necp_client_add_flow_default)) {
8196 // Fits in default size
8197 error = copyin(uap->buffer, &default_add_request, buffer_size);
8198 if (error) {
8199 NECPLOG(LOG_ERR, "necp_client_add_flow copyin default_add_request error (%d)", error);
8200 goto done;
8201 }
8202
8203 add_request = (struct necp_client_add_flow *)&default_add_request;
8204 } else {
8205 allocated_add_request = (struct necp_client_add_flow *)kalloc_data(buffer_size, Z_WAITOK | Z_ZERO);
8206 if (allocated_add_request == NULL) {
8207 error = ENOMEM;
8208 goto done;
8209 }
8210
8211 error = copyin(uap->buffer, allocated_add_request, buffer_size);
8212 if (error) {
8213 NECPLOG(LOG_ERR, "necp_client_add_flow copyin default_add_request error (%d)", error);
8214 goto done;
8215 }
8216
8217 add_request = allocated_add_request;
8218 }
8219
8220 NECP_FD_LOCK(fd_data);
8221 pid_t pid = fd_data->proc_pid;
8222 proc = proc_find(pid);
8223 if (proc == PROC_NULL) {
8224 NECP_FD_UNLOCK(fd_data);
8225 NECPLOG(LOG_ERR, "necp_client_add_flow process not found for pid %d error (%d)", pid, error);
8226 error = ESRCH;
8227 goto done;
8228 }
8229
8230 client = necp_client_fd_find_client_and_lock(fd_data, client_id);
8231 if (client == NULL) {
8232 error = ENOENT;
8233 NECP_FD_UNLOCK(fd_data);
8234 goto done;
8235 }
8236
8237 // Using ADD_FLOW indicates that the client supports multiple flows per client
8238 client->legacy_client_is_flow = false;
8239
8240 necp_client_retain_locked(client);
8241 necp_client_copy_parameters_locked(client, ¶meters);
8242
8243 struct necp_client_flow_registration *new_registration = necp_client_create_flow_registration(fd_data, client);
8244 if (new_registration == NULL) {
8245 error = ENOMEM;
8246 NECP_CLIENT_UNLOCK(client);
8247 NECP_FD_UNLOCK(fd_data);
8248 NECPLOG0(LOG_ERR, "Failed to allocate flow registration");
8249 goto done;
8250 }
8251
8252 new_registration->flags = add_request->flags;
8253
8254 // Copy new ID out to caller
8255 uuid_copy(add_request->registration_id, new_registration->registration_id);
8256
8257 NECP_CLIENT_FLOW_LOG(client, new_registration, "adding flow");
8258
8259 // Copy override address
8260 if (add_request->flags & NECP_CLIENT_FLOW_FLAGS_OVERRIDE_ADDRESS) {
8261 size_t offset_of_address = (sizeof(struct necp_client_add_flow) +
8262 add_request->stats_request_count * sizeof(struct necp_client_flow_stats));
8263 if (buffer_size >= offset_of_address + sizeof(struct sockaddr_in)) {
8264 struct sockaddr *override_address = (struct sockaddr *)(((uint8_t *)add_request) + offset_of_address);
8265 if (buffer_size >= offset_of_address + override_address->sa_len &&
8266 override_address->sa_len <= sizeof(parameters.remote_addr)) {
8267 memcpy(¶meters.remote_addr, override_address, override_address->sa_len);
8268 }
8269 }
8270 }
8271
8272 #if SKYWALK
8273 if (add_request->flags & NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS) {
8274 void *assigned_results = NULL;
8275 size_t assigned_results_length = 0;
8276 uint32_t interface_index = 0;
8277
8278 // Validate that the nexus UUID is assigned
8279 bool found_nexus = false;
8280 for (u_int32_t option_i = 0; option_i < client->interface_option_count; option_i++) {
8281 if (option_i < NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT) {
8282 struct necp_client_interface_option *option = &client->interface_options[option_i];
8283 if (uuid_compare(option->nexus_agent, add_request->agent_uuid) == 0) {
8284 interface_index = option->interface_index;
8285 found_nexus = true;
8286 break;
8287 }
8288 } else {
8289 struct necp_client_interface_option *option = &client->extra_interface_options[option_i - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT];
8290 if (uuid_compare(option->nexus_agent, add_request->agent_uuid) == 0) {
8291 interface_index = option->interface_index;
8292 found_nexus = true;
8293 break;
8294 }
8295 }
8296 }
8297
8298 if (!found_nexus) {
8299 NECPLOG0(LOG_ERR, "Requested nexus not found");
8300 } else {
8301 necp_client_add_nexus_flow_if_needed(new_registration, add_request->agent_uuid, interface_index);
8302
8303 error = netagent_client_message_with_params(add_request->agent_uuid,
8304 ((new_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
8305 client->client_id :
8306 new_registration->registration_id),
8307 pid, client->agent_handle,
8308 NETAGENT_MESSAGE_TYPE_REQUEST_NEXUS,
8309 (struct necp_client_agent_parameters *)¶meters,
8310 &assigned_results, &assigned_results_length);
8311 if (error != 0) {
8312 VERIFY(assigned_results == NULL);
8313 VERIFY(assigned_results_length == 0);
8314 NECPLOG(LOG_ERR, "netagent_client_message error (%d)", error);
8315 } else if (assigned_results != NULL) {
8316 if (!necp_assign_client_result_locked(proc, fd_data, client, new_registration, add_request->agent_uuid,
8317 assigned_results, assigned_results_length, false, false)) {
8318 kfree_data(assigned_results, assigned_results_length);
8319 }
8320 }
8321 }
8322 }
8323
8324 // Don't request stats if nexus creation fails
8325 if (error == 0 && add_request->stats_request_count > 0 && necp_arena_initialize(fd_data, true) == 0) {
8326 struct necp_client_flow_stats *stats_request = (struct necp_client_flow_stats *)&add_request->stats_requests[0];
8327 struct necp_stats_bufreq bufreq = {};
8328
8329 NECP_CLIENT_FLOW_LOG(client, new_registration, "Initializing stats");
8330
8331 bufreq.necp_stats_bufreq_id = NECP_CLIENT_STATISTICS_BUFREQ_ID;
8332 bufreq.necp_stats_bufreq_type = stats_request->stats_type;
8333 bufreq.necp_stats_bufreq_ver = stats_request->stats_version;
8334 bufreq.necp_stats_bufreq_size = stats_request->stats_size;
8335 bufreq.necp_stats_bufreq_uaddr = stats_request->stats_addr;
8336 (void)necp_stats_initialize(fd_data, client, new_registration, &bufreq);
8337 stats_request->stats_type = bufreq.necp_stats_bufreq_type;
8338 stats_request->stats_version = bufreq.necp_stats_bufreq_ver;
8339 stats_request->stats_size = bufreq.necp_stats_bufreq_size;
8340 stats_request->stats_addr = bufreq.necp_stats_bufreq_uaddr;
8341 }
8342 #endif /* !SKYWALK */
8343
8344 if (error == 0 &&
8345 (add_request->flags & NECP_CLIENT_FLOW_FLAGS_BROWSE ||
8346 add_request->flags & NECP_CLIENT_FLOW_FLAGS_RESOLVE)) {
8347 uint32_t interface_index = IFSCOPE_NONE;
8348 ifnet_head_lock_shared();
8349 struct ifnet *interface = NULL;
8350 TAILQ_FOREACH(interface, &ifnet_head, if_link) {
8351 ifnet_lock_shared(interface);
8352 if (interface->if_agentids != NULL) {
8353 for (u_int32_t i = 0; i < interface->if_agentcount; i++) {
8354 if (uuid_compare(interface->if_agentids[i], add_request->agent_uuid) == 0) {
8355 interface_index = interface->if_index;
8356 break;
8357 }
8358 }
8359 }
8360 ifnet_lock_done(interface);
8361 if (interface_index != IFSCOPE_NONE) {
8362 break;
8363 }
8364 }
8365 ifnet_head_done();
8366
8367 necp_client_add_nexus_flow_if_needed(new_registration, add_request->agent_uuid, interface_index);
8368
8369 error = netagent_client_message_with_params(add_request->agent_uuid,
8370 ((new_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
8371 client->client_id :
8372 new_registration->registration_id),
8373 pid, client->agent_handle,
8374 NETAGENT_MESSAGE_TYPE_CLIENT_ASSERT,
8375 (struct necp_client_agent_parameters *)¶meters,
8376 NULL, NULL);
8377 if (error != 0) {
8378 NECPLOG(LOG_ERR, "netagent_client_message error (%d)", error);
8379 }
8380 }
8381
8382 if (error != 0) {
8383 // Encountered an error in adding the flow, destroy the flow registration
8384 #if SKYWALK
8385 necp_destroy_flow_stats(fd_data, new_registration, NULL, false);
8386 #endif /* SKYWALK */
8387 NECP_FLOW_TREE_LOCK_EXCLUSIVE();
8388 RB_REMOVE(_necp_client_flow_global_tree, &necp_client_flow_global_tree, new_registration);
8389 NECP_FLOW_TREE_UNLOCK();
8390 RB_REMOVE(_necp_fd_flow_tree, &fd_data->flows, new_registration);
8391 necp_destroy_client_flow_registration(client, new_registration, fd_data->proc_pid, true);
8392 new_registration = NULL;
8393 }
8394
8395 NECP_CLIENT_UNLOCK(client);
8396 NECP_FD_UNLOCK(fd_data);
8397
8398 necp_client_release(client);
8399
8400 if (error != 0) {
8401 goto done;
8402 }
8403
8404 // Copy the request back out to the caller with assigned fields
8405 error = copyout(add_request, uap->buffer, buffer_size);
8406 if (error != 0) {
8407 NECPLOG(LOG_ERR, "necp_client_add_flow copyout add_request error (%d)", error);
8408 }
8409
8410 done:
8411 *retval = error;
8412 if (error != 0) {
8413 NECPLOG(LOG_ERR, "Add flow error (%d)", error);
8414 }
8415
8416 if (allocated_add_request != NULL) {
8417 kfree_data(allocated_add_request, buffer_size);
8418 }
8419
8420 if (proc != PROC_NULL) {
8421 proc_rele(proc);
8422 }
8423 return error;
8424 }
8425
8426 #if SKYWALK
8427
8428 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_request_nexus(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8429 necp_client_request_nexus(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8430 {
8431 int error = 0;
8432 struct necp_client *client = NULL;
8433 uuid_t client_id;
8434 struct necp_client_nexus_parameters parameters = {};
8435 struct proc *proc = PROC_NULL;
8436 const size_t buffer_size = uap->buffer_size;
8437
8438 if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
8439 error = EINVAL;
8440 goto done;
8441 }
8442
8443 error = copyin(uap->client_id, client_id, sizeof(uuid_t));
8444 if (error) {
8445 NECPLOG(LOG_ERR, "necp_client_request_nexus copyin client_id error (%d)", error);
8446 goto done;
8447 }
8448
8449 NECP_FD_LOCK(fd_data);
8450 pid_t pid = fd_data->proc_pid;
8451 proc = proc_find(pid);
8452 if (proc == PROC_NULL) {
8453 NECP_FD_UNLOCK(fd_data);
8454 NECPLOG(LOG_ERR, "necp_client_request_nexus process not found for pid %d error (%d)", pid, error);
8455 error = ESRCH;
8456 goto done;
8457 }
8458
8459 client = necp_client_fd_find_client_and_lock(fd_data, client_id);
8460 if (client == NULL) {
8461 NECP_FD_UNLOCK(fd_data);
8462 error = ENOENT;
8463 goto done;
8464 }
8465
8466 // Using REQUEST_NEXUS indicates that the client only supports one flow per client
8467 client->legacy_client_is_flow = true;
8468
8469 necp_client_retain_locked(client);
8470 necp_client_copy_parameters_locked(client, ¶meters);
8471
8472 do {
8473 void *assigned_results = NULL;
8474 size_t assigned_results_length = 0;
8475 uuid_t nexus_uuid;
8476 uint32_t interface_index = 0;
8477
8478 // Validate that the nexus UUID is assigned
8479 bool found_nexus = false;
8480 for (u_int32_t option_i = 0; option_i < client->interface_option_count; option_i++) {
8481 if (option_i < NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT) {
8482 struct necp_client_interface_option *option = &client->interface_options[option_i];
8483 if (!uuid_is_null(option->nexus_agent)) {
8484 uuid_copy(nexus_uuid, option->nexus_agent);
8485 interface_index = option->interface_index;
8486 found_nexus = true;
8487 break;
8488 }
8489 } else {
8490 struct necp_client_interface_option *option = &client->extra_interface_options[option_i - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT];
8491 if (!uuid_is_null(option->nexus_agent)) {
8492 uuid_copy(nexus_uuid, option->nexus_agent);
8493 interface_index = option->interface_index;
8494 found_nexus = true;
8495 break;
8496 }
8497 }
8498 }
8499
8500 if (!found_nexus) {
8501 NECP_CLIENT_UNLOCK(client);
8502 NECP_FD_UNLOCK(fd_data);
8503 necp_client_release(client);
8504 // Break the loop
8505 error = ENETDOWN;
8506 goto done;
8507 }
8508
8509 struct necp_client_flow_registration *new_registration = necp_client_create_flow_registration(fd_data, client);
8510 if (new_registration == NULL) {
8511 error = ENOMEM;
8512 NECP_CLIENT_UNLOCK(client);
8513 NECP_FD_UNLOCK(fd_data);
8514 necp_client_release(client);
8515 NECPLOG0(LOG_ERR, "Failed to allocate flow registration");
8516 goto done;
8517 }
8518
8519 new_registration->flags = (NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS | NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID);
8520
8521 necp_client_add_nexus_flow_if_needed(new_registration, nexus_uuid, interface_index);
8522
8523 // Note: Any clients using "request_nexus" are not flow-registration aware.
8524 // Register the Client ID rather than the Registration ID with the nexus, since
8525 // the client will send traffic based on the client ID.
8526 error = netagent_client_message_with_params(nexus_uuid,
8527 ((new_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
8528 client->client_id :
8529 new_registration->registration_id),
8530 pid, client->agent_handle,
8531 NETAGENT_MESSAGE_TYPE_REQUEST_NEXUS,
8532 (struct necp_client_agent_parameters *)¶meters,
8533 &assigned_results, &assigned_results_length);
8534 if (error) {
8535 NECP_CLIENT_UNLOCK(client);
8536 NECP_FD_UNLOCK(fd_data);
8537 necp_client_release(client);
8538 VERIFY(assigned_results == NULL);
8539 VERIFY(assigned_results_length == 0);
8540 NECPLOG(LOG_ERR, "netagent_client_message error (%d)", error);
8541 goto done;
8542 }
8543
8544 if (assigned_results != NULL) {
8545 if (!necp_assign_client_result_locked(proc, fd_data, client, new_registration, nexus_uuid,
8546 assigned_results, assigned_results_length, false, false)) {
8547 kfree_data(assigned_results, assigned_results_length);
8548 }
8549 }
8550
8551 if (uap->buffer != 0 && buffer_size == sizeof(struct necp_stats_bufreq) &&
8552 necp_arena_initialize(fd_data, true) == 0) {
8553 struct necp_stats_bufreq bufreq = {};
8554 int copy_error = copyin(uap->buffer, &bufreq, buffer_size);
8555 if (copy_error) {
8556 NECPLOG(LOG_ERR, "necp_client_request_nexus copyin bufreq error (%d)", copy_error);
8557 } else {
8558 (void)necp_stats_initialize(fd_data, client, new_registration, &bufreq);
8559 copy_error = copyout(&bufreq, uap->buffer, buffer_size);
8560 if (copy_error != 0) {
8561 NECPLOG(LOG_ERR, "necp_client_request_nexus copyout bufreq error (%d)", copy_error);
8562 }
8563 }
8564 }
8565 } while (false);
8566
8567 NECP_CLIENT_UNLOCK(client);
8568 NECP_FD_UNLOCK(fd_data);
8569
8570 necp_client_release(client);
8571
8572 done:
8573 *retval = error;
8574 if (error != 0) {
8575 NECPLOG(LOG_ERR, "Request nexus error (%d)", error);
8576 }
8577
8578 if (proc != PROC_NULL) {
8579 proc_rele(proc);
8580 }
8581 return error;
8582 }
8583 #endif /* !SKYWALK */
8584
8585 static void
necp_client_add_assertion(struct necp_client * client,uuid_t netagent_uuid)8586 necp_client_add_assertion(struct necp_client *client, uuid_t netagent_uuid)
8587 {
8588 struct necp_client_assertion *new_assertion = NULL;
8589
8590 new_assertion = kalloc_type(struct necp_client_assertion,
8591 Z_WAITOK | Z_NOFAIL);
8592
8593 uuid_copy(new_assertion->asserted_netagent, netagent_uuid);
8594
8595 LIST_INSERT_HEAD(&client->assertion_list, new_assertion, assertion_chain);
8596 }
8597
8598 static bool
necp_client_remove_assertion(struct necp_client * client,uuid_t netagent_uuid)8599 necp_client_remove_assertion(struct necp_client *client, uuid_t netagent_uuid)
8600 {
8601 struct necp_client_assertion *found_assertion = NULL;
8602 struct necp_client_assertion *search_assertion = NULL;
8603 LIST_FOREACH(search_assertion, &client->assertion_list, assertion_chain) {
8604 if (uuid_compare(search_assertion->asserted_netagent, netagent_uuid) == 0) {
8605 found_assertion = search_assertion;
8606 break;
8607 }
8608 }
8609
8610 if (found_assertion == NULL) {
8611 NECPLOG0(LOG_ERR, "Netagent uuid not previously asserted");
8612 return false;
8613 }
8614
8615 LIST_REMOVE(found_assertion, assertion_chain);
8616 kfree_type(struct necp_client_assertion, found_assertion);
8617 return true;
8618 }
8619
8620 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_agent_action(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8621 necp_client_agent_action(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8622 {
8623 int error = 0;
8624 struct necp_client *client = NULL;
8625 uuid_t client_id;
8626 bool acted_on_agent = FALSE;
8627 u_int8_t *parameters = NULL;
8628 const size_t buffer_size = uap->buffer_size;
8629
8630 if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
8631 buffer_size == 0 || uap->buffer == 0) {
8632 NECPLOG0(LOG_ERR, "necp_client_agent_action invalid parameters");
8633 error = EINVAL;
8634 goto done;
8635 }
8636
8637 error = copyin(uap->client_id, client_id, sizeof(uuid_t));
8638 if (error) {
8639 NECPLOG(LOG_ERR, "necp_client_agent_action copyin client_id error (%d)", error);
8640 goto done;
8641 }
8642
8643 if (buffer_size > NECP_MAX_AGENT_ACTION_SIZE) {
8644 NECPLOG(LOG_ERR, "necp_client_agent_action invalid buffer size (>%u)", NECP_MAX_AGENT_ACTION_SIZE);
8645 error = EINVAL;
8646 goto done;
8647 }
8648
8649 if ((parameters = (u_int8_t *)kalloc_data(buffer_size, Z_WAITOK | Z_ZERO)) == NULL) {
8650 NECPLOG0(LOG_ERR, "necp_client_agent_action malloc failed");
8651 error = ENOMEM;
8652 goto done;
8653 }
8654
8655 error = copyin(uap->buffer, parameters, buffer_size);
8656 if (error) {
8657 NECPLOG(LOG_ERR, "necp_client_agent_action parameters copyin error (%d)", error);
8658 goto done;
8659 }
8660
8661 NECP_FD_LOCK(fd_data);
8662 client = necp_client_fd_find_client_and_lock(fd_data, client_id);
8663 if (client != NULL) {
8664 size_t offset = 0;
8665 while ((offset + sizeof(struct necp_tlv_header)) <= buffer_size) {
8666 u_int8_t type = necp_buffer_get_tlv_type(parameters, offset);
8667 u_int32_t length = necp_buffer_get_tlv_length(parameters, offset);
8668
8669 if (length > (buffer_size - (offset + sizeof(struct necp_tlv_header)))) {
8670 // If the length is larger than what can fit in the remaining parameters size, bail
8671 NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
8672 break;
8673 }
8674
8675 if (length >= sizeof(uuid_t)) {
8676 u_int8_t *value = necp_buffer_get_tlv_value(parameters, offset, NULL);
8677 if (value == NULL) {
8678 NECPLOG0(LOG_ERR, "Invalid TLV value");
8679 break;
8680 }
8681 if (type == NECP_CLIENT_PARAMETER_TRIGGER_AGENT ||
8682 type == NECP_CLIENT_PARAMETER_ASSERT_AGENT ||
8683 type == NECP_CLIENT_PARAMETER_UNASSERT_AGENT) {
8684 uuid_t agent_uuid;
8685 uuid_copy(agent_uuid, value);
8686 u_int8_t netagent_message_type = 0;
8687 if (type == NECP_CLIENT_PARAMETER_TRIGGER_AGENT) {
8688 netagent_message_type = NETAGENT_MESSAGE_TYPE_CLIENT_TRIGGER;
8689 } else if (type == NECP_CLIENT_PARAMETER_ASSERT_AGENT) {
8690 netagent_message_type = NETAGENT_MESSAGE_TYPE_CLIENT_ASSERT;
8691 } else if (type == NECP_CLIENT_PARAMETER_UNASSERT_AGENT) {
8692 netagent_message_type = NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT;
8693 }
8694
8695 // Before unasserting, verify that the assertion was already taken
8696 if (type == NECP_CLIENT_PARAMETER_UNASSERT_AGENT) {
8697 if (!necp_client_remove_assertion(client, agent_uuid)) {
8698 error = ENOENT;
8699 break;
8700 }
8701 }
8702
8703 struct necp_client_nexus_parameters parsed_parameters = {};
8704 necp_client_copy_parameters_locked(client, &parsed_parameters);
8705
8706 error = netagent_client_message_with_params(agent_uuid,
8707 client_id,
8708 fd_data->proc_pid,
8709 client->agent_handle,
8710 netagent_message_type,
8711 (struct necp_client_agent_parameters *)&parsed_parameters,
8712 NULL, NULL);
8713 if (error == 0) {
8714 acted_on_agent = TRUE;
8715 } else {
8716 break;
8717 }
8718
8719 // Only save the assertion if the action succeeded
8720 if (type == NECP_CLIENT_PARAMETER_ASSERT_AGENT) {
8721 necp_client_add_assertion(client, agent_uuid);
8722 }
8723 } else if (type == NECP_CLIENT_PARAMETER_AGENT_ADD_GROUP_MEMBERS ||
8724 type == NECP_CLIENT_PARAMETER_AGENT_REMOVE_GROUP_MEMBERS) {
8725 uuid_t agent_uuid;
8726 uuid_copy(agent_uuid, value);
8727 u_int8_t netagent_message_type = 0;
8728 if (type == NECP_CLIENT_PARAMETER_AGENT_ADD_GROUP_MEMBERS) {
8729 netagent_message_type = NETAGENT_MESSAGE_TYPE_ADD_GROUP_MEMBERS;
8730 } else if (type == NECP_CLIENT_PARAMETER_AGENT_REMOVE_GROUP_MEMBERS) {
8731 netagent_message_type = NETAGENT_MESSAGE_TYPE_REMOVE_GROUP_MEMBERS;
8732 }
8733
8734 struct necp_client_group_members group_members = {};
8735 group_members.group_members_length = (length - sizeof(uuid_t));
8736 group_members.group_members = (value + sizeof(uuid_t));
8737 error = netagent_client_message_with_params(agent_uuid,
8738 client_id,
8739 fd_data->proc_pid,
8740 client->agent_handle,
8741 netagent_message_type,
8742 (struct necp_client_agent_parameters *)&group_members,
8743 NULL, NULL);
8744 if (error == 0) {
8745 acted_on_agent = TRUE;
8746 } else {
8747 break;
8748 }
8749 } else if (type == NECP_CLIENT_PARAMETER_REPORT_AGENT_ERROR) {
8750 uuid_t agent_uuid;
8751 uuid_copy(agent_uuid, value);
8752 struct necp_client_agent_parameters agent_params = {};
8753 if ((length - sizeof(uuid_t)) >= sizeof(agent_params.u.error)) {
8754 memcpy(&agent_params.u.error,
8755 (value + sizeof(uuid_t)),
8756 sizeof(agent_params.u.error));
8757 }
8758 error = netagent_client_message_with_params(agent_uuid,
8759 client_id,
8760 fd_data->proc_pid,
8761 client->agent_handle,
8762 NETAGENT_MESSAGE_TYPE_CLIENT_ERROR,
8763 &agent_params,
8764 NULL, NULL);
8765 if (error == 0) {
8766 acted_on_agent = TRUE;
8767 } else {
8768 break;
8769 }
8770 }
8771 }
8772
8773 offset += sizeof(struct necp_tlv_header) + length;
8774 }
8775
8776 NECP_CLIENT_UNLOCK(client);
8777 }
8778 NECP_FD_UNLOCK(fd_data);
8779
8780 if (!acted_on_agent &&
8781 error == 0) {
8782 error = ENOENT;
8783 }
8784 done:
8785 *retval = error;
8786 if (parameters != NULL) {
8787 kfree_data(parameters, buffer_size);
8788 parameters = NULL;
8789 }
8790
8791 return error;
8792 }
8793
8794 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_copy_agent(__unused struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8795 necp_client_copy_agent(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8796 {
8797 int error = 0;
8798 uuid_t agent_uuid;
8799 const size_t buffer_size = uap->buffer_size;
8800
8801 if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
8802 buffer_size == 0 || uap->buffer == 0) {
8803 NECPLOG0(LOG_ERR, "necp_client_copy_agent bad input");
8804 error = EINVAL;
8805 goto done;
8806 }
8807
8808 error = copyin(uap->client_id, agent_uuid, sizeof(uuid_t));
8809 if (error) {
8810 NECPLOG(LOG_ERR, "necp_client_copy_agent copyin agent_uuid error (%d)", error);
8811 goto done;
8812 }
8813
8814 error = netagent_copyout(agent_uuid, uap->buffer, buffer_size);
8815 if (error) {
8816 // netagent_copyout already logs appropriate errors
8817 goto done;
8818 }
8819 done:
8820 *retval = error;
8821
8822 return error;
8823 }
8824
8825 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_agent_use(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8826 necp_client_agent_use(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8827 {
8828 int error = 0;
8829 struct necp_client *client = NULL;
8830 uuid_t client_id;
8831 struct necp_agent_use_parameters parameters = {};
8832 const size_t buffer_size = uap->buffer_size;
8833
8834 if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
8835 buffer_size != sizeof(parameters) || uap->buffer == 0) {
8836 error = EINVAL;
8837 goto done;
8838 }
8839
8840 error = copyin(uap->client_id, client_id, sizeof(uuid_t));
8841 if (error) {
8842 NECPLOG(LOG_ERR, "Copyin client_id error (%d)", error);
8843 goto done;
8844 }
8845
8846 error = copyin(uap->buffer, ¶meters, buffer_size);
8847 if (error) {
8848 NECPLOG(LOG_ERR, "Parameters copyin error (%d)", error);
8849 goto done;
8850 }
8851
8852 NECP_FD_LOCK(fd_data);
8853 client = necp_client_fd_find_client_and_lock(fd_data, client_id);
8854 if (client != NULL) {
8855 error = netagent_use(parameters.agent_uuid, ¶meters.out_use_count);
8856 NECP_CLIENT_UNLOCK(client);
8857 } else {
8858 error = ENOENT;
8859 }
8860
8861 NECP_FD_UNLOCK(fd_data);
8862
8863 if (error == 0) {
8864 error = copyout(¶meters, uap->buffer, buffer_size);
8865 if (error) {
8866 NECPLOG(LOG_ERR, "Parameters copyout error (%d)", error);
8867 goto done;
8868 }
8869 }
8870
8871 done:
8872 *retval = error;
8873
8874 return error;
8875 }
8876
8877 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_acquire_agent_token(__unused struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8878 necp_client_acquire_agent_token(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8879 {
8880 int error = 0;
8881 uuid_t agent_uuid = {};
8882 const size_t buffer_size = uap->buffer_size;
8883
8884 *retval = 0;
8885
8886 if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
8887 buffer_size == 0 || uap->buffer == 0) {
8888 NECPLOG0(LOG_ERR, "necp_client_copy_agent bad input");
8889 error = EINVAL;
8890 goto done;
8891 }
8892
8893 error = copyin(uap->client_id, agent_uuid, sizeof(uuid_t));
8894 if (error) {
8895 NECPLOG(LOG_ERR, "necp_client_copy_agent copyin agent_uuid error (%d)", error);
8896 goto done;
8897 }
8898
8899 error = netagent_acquire_token(agent_uuid, uap->buffer, buffer_size, retval);
8900 done:
8901 return error;
8902 }
8903
8904 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_copy_interface(__unused struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8905 necp_client_copy_interface(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8906 {
8907 int error = 0;
8908 u_int32_t interface_index = 0;
8909 struct necp_interface_details interface_details = {};
8910
8911 if (uap->client_id == 0 || uap->client_id_len != sizeof(u_int32_t) ||
8912 uap->buffer_size < sizeof(interface_details) ||
8913 uap->buffer == 0) {
8914 NECPLOG0(LOG_ERR, "necp_client_copy_interface bad input");
8915 error = EINVAL;
8916 goto done;
8917 }
8918
8919 error = copyin(uap->client_id, &interface_index, sizeof(u_int32_t));
8920 if (error) {
8921 NECPLOG(LOG_ERR, "necp_client_copy_interface copyin interface_index error (%d)", error);
8922 goto done;
8923 }
8924
8925 if (interface_index == 0) {
8926 error = ENOENT;
8927 NECPLOG(LOG_ERR, "necp_client_copy_interface bad interface_index (%d)", interface_index);
8928 goto done;
8929 }
8930
8931 lck_mtx_lock(rnh_lock);
8932 ifnet_head_lock_shared();
8933 ifnet_t interface = NULL;
8934 if (interface_index != IFSCOPE_NONE && interface_index <= (u_int32_t)if_index) {
8935 interface = ifindex2ifnet[interface_index];
8936 }
8937
8938 if (interface != NULL) {
8939 if (interface->if_xname != NULL) {
8940 strlcpy((char *)&interface_details.name, interface->if_xname, sizeof(interface_details.name));
8941 }
8942 interface_details.index = interface->if_index;
8943 interface_details.generation = ifnet_get_generation(interface);
8944 if (interface->if_delegated.ifp != NULL) {
8945 interface_details.delegate_index = interface->if_delegated.ifp->if_index;
8946 }
8947 interface_details.functional_type = if_functional_type(interface, TRUE);
8948 if (IFNET_IS_EXPENSIVE(interface)) {
8949 interface_details.flags |= NECP_INTERFACE_FLAG_EXPENSIVE;
8950 }
8951 if (IFNET_IS_CONSTRAINED(interface)) {
8952 interface_details.flags |= NECP_INTERFACE_FLAG_CONSTRAINED;
8953 }
8954 if ((interface->if_eflags & IFEF_TXSTART) == IFEF_TXSTART) {
8955 interface_details.flags |= NECP_INTERFACE_FLAG_TXSTART;
8956 }
8957 if ((interface->if_eflags & IFEF_NOACKPRI) == IFEF_NOACKPRI) {
8958 interface_details.flags |= NECP_INTERFACE_FLAG_NOACKPRI;
8959 }
8960 if ((interface->if_eflags & IFEF_3CA) == IFEF_3CA) {
8961 interface_details.flags |= NECP_INTERFACE_FLAG_3CARRIERAGG;
8962 }
8963 if (IFNET_IS_LOW_POWER(interface)) {
8964 interface_details.flags |= NECP_INTERFACE_FLAG_IS_LOW_POWER;
8965 }
8966 if (interface->if_xflags & IFXF_MPK_LOG) {
8967 interface_details.flags |= NECP_INTERFACE_FLAG_MPK_LOG;
8968 }
8969 if (interface->if_flags & IFF_MULTICAST) {
8970 interface_details.flags |= NECP_INTERFACE_FLAG_SUPPORTS_MULTICAST;
8971 }
8972 if (IS_INTF_CLAT46(interface)) {
8973 interface_details.flags |= NECP_INTERFACE_FLAG_HAS_NAT64;
8974 }
8975 interface_details.mtu = interface->if_mtu;
8976 #if SKYWALK
8977 nx_netif_get_interface_tso_capabilities(interface, &interface_details.tso_max_segment_size_v4, &interface_details.tso_max_segment_size_v6);
8978 #endif /* SKYWALK */
8979
8980 u_int8_t ipv4_signature_len = sizeof(interface_details.ipv4_signature.signature);
8981 u_int16_t ipv4_signature_flags;
8982 if (ifnet_get_netsignature(interface, AF_INET, &ipv4_signature_len, &ipv4_signature_flags,
8983 (u_int8_t *)&interface_details.ipv4_signature) != 0) {
8984 ipv4_signature_len = 0;
8985 }
8986 interface_details.ipv4_signature.signature_len = ipv4_signature_len;
8987
8988 // Check for default scoped routes for IPv4 and IPv6
8989 union necp_sockaddr_union default_address;
8990 struct rtentry *v4Route = NULL;
8991 memset(&default_address, 0, sizeof(default_address));
8992 default_address.sa.sa_family = AF_INET;
8993 default_address.sa.sa_len = sizeof(struct sockaddr_in);
8994 v4Route = rtalloc1_scoped_locked((struct sockaddr *)&default_address, 0, 0,
8995 interface->if_index);
8996 if (v4Route != NULL) {
8997 if (v4Route->rt_ifp != NULL && !IS_INTF_CLAT46(v4Route->rt_ifp)) {
8998 interface_details.flags |= NECP_INTERFACE_FLAG_IPV4_ROUTABLE;
8999 }
9000 rtfree_locked(v4Route);
9001 v4Route = NULL;
9002 }
9003
9004 struct rtentry *v6Route = NULL;
9005 memset(&default_address, 0, sizeof(default_address));
9006 default_address.sa.sa_family = AF_INET6;
9007 default_address.sa.sa_len = sizeof(struct sockaddr_in6);
9008 v6Route = rtalloc1_scoped_locked((struct sockaddr *)&default_address, 0, 0,
9009 interface->if_index);
9010 if (v6Route != NULL) {
9011 if (v6Route->rt_ifp != NULL) {
9012 interface_details.flags |= NECP_INTERFACE_FLAG_IPV6_ROUTABLE;
9013 }
9014 rtfree_locked(v6Route);
9015 v6Route = NULL;
9016 }
9017
9018 u_int8_t ipv6_signature_len = sizeof(interface_details.ipv6_signature.signature);
9019 u_int16_t ipv6_signature_flags;
9020 if (ifnet_get_netsignature(interface, AF_INET6, &ipv6_signature_len, &ipv6_signature_flags,
9021 (u_int8_t *)&interface_details.ipv6_signature) != 0) {
9022 ipv6_signature_len = 0;
9023 }
9024 interface_details.ipv6_signature.signature_len = ipv6_signature_len;
9025
9026 ifnet_lock_shared(interface);
9027 struct ifaddr *ifa = NULL;
9028 TAILQ_FOREACH(ifa, &interface->if_addrhead, ifa_link) {
9029 IFA_LOCK(ifa);
9030 if (ifa->ifa_addr->sa_family == AF_INET) {
9031 interface_details.flags |= NECP_INTERFACE_FLAG_HAS_NETMASK;
9032 interface_details.ipv4_netmask = ((struct in_ifaddr *)ifa)->ia_sockmask.sin_addr.s_addr;
9033 if (interface->if_flags & IFF_BROADCAST) {
9034 interface_details.flags |= NECP_INTERFACE_FLAG_HAS_BROADCAST;
9035 interface_details.ipv4_broadcast = ((struct in_ifaddr *)ifa)->ia_broadaddr.sin_addr.s_addr;
9036 }
9037 }
9038 IFA_UNLOCK(ifa);
9039 }
9040
9041 interface_details.radio_type = interface->if_radio_type;
9042 if (interface_details.radio_type == 0 && interface->if_delegated.ifp) {
9043 interface_details.radio_type = interface->if_delegated.ifp->if_radio_type;
9044 }
9045 ifnet_lock_done(interface);
9046 }
9047
9048 ifnet_head_done();
9049 lck_mtx_unlock(rnh_lock);
9050
9051 // If the client is using an older version of the struct, copy that length
9052 error = copyout(&interface_details, uap->buffer, sizeof(interface_details));
9053 if (error) {
9054 NECPLOG(LOG_ERR, "necp_client_copy_interface copyout error (%d)", error);
9055 goto done;
9056 }
9057 done:
9058 *retval = error;
9059
9060 return error;
9061 }
9062
9063 #if SKYWALK
9064
9065 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_get_interface_address(__unused struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)9066 necp_client_get_interface_address(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9067 {
9068 int error = 0;
9069 u_int32_t interface_index = IFSCOPE_NONE;
9070 struct sockaddr_storage address = {};
9071 const size_t buffer_size = uap->buffer_size;
9072
9073 if (uap->client_id == 0 || uap->client_id_len != sizeof(u_int32_t) ||
9074 buffer_size < sizeof(struct sockaddr_in) ||
9075 buffer_size > sizeof(struct sockaddr_storage) ||
9076 uap->buffer == 0) {
9077 NECPLOG0(LOG_ERR, "necp_client_get_interface_address bad input");
9078 error = EINVAL;
9079 goto done;
9080 }
9081
9082 error = copyin(uap->client_id, &interface_index, sizeof(u_int32_t));
9083 if (error) {
9084 NECPLOG(LOG_ERR, "necp_client_get_interface_address copyin interface_index error (%d)", error);
9085 goto done;
9086 }
9087
9088 if (interface_index == IFSCOPE_NONE) {
9089 error = ENOENT;
9090 NECPLOG(LOG_ERR, "necp_client_get_interface_address bad interface_index (%d)", interface_index);
9091 goto done;
9092 }
9093
9094 error = copyin(uap->buffer, &address, buffer_size);
9095 if (error) {
9096 NECPLOG(LOG_ERR, "necp_client_get_interface_address copyin address error (%d)", error);
9097 goto done;
9098 }
9099
9100 if (address.ss_family != AF_INET && address.ss_family != AF_INET6) {
9101 error = EINVAL;
9102 NECPLOG(LOG_ERR, "necp_client_get_interface_address invalid address family (%u)", address.ss_family);
9103 goto done;
9104 }
9105
9106 if (address.ss_len != buffer_size) {
9107 error = EINVAL;
9108 NECPLOG(LOG_ERR, "necp_client_get_interface_address invalid address length (%u)", address.ss_len);
9109 goto done;
9110 }
9111
9112 ifnet_head_lock_shared();
9113 ifnet_t ifp = NULL;
9114 if (interface_index != IFSCOPE_NONE && interface_index <= (u_int32_t)if_index) {
9115 ifp = ifindex2ifnet[interface_index];
9116 }
9117 ifnet_head_done();
9118 if (ifp == NULL) {
9119 error = ENOENT;
9120 NECPLOG0(LOG_ERR, "necp_client_get_interface_address no matching interface found");
9121 goto done;
9122 }
9123
9124 struct rtentry *rt = rtalloc1_scoped((struct sockaddr *)&address, 0, 0, interface_index);
9125 if (rt == NULL) {
9126 error = EINVAL;
9127 NECPLOG0(LOG_ERR, "necp_client_get_interface_address route lookup failed");
9128 goto done;
9129 }
9130
9131 uint32_t gencount = 0;
9132 struct sockaddr_storage local_address = {};
9133 error = flow_route_select_laddr((union sockaddr_in_4_6 *)&local_address,
9134 (union sockaddr_in_4_6 *)&address, ifp, rt, &gencount, 1);
9135 rtfree(rt);
9136 rt = NULL;
9137
9138 if (error) {
9139 NECPLOG(LOG_ERR, "necp_client_get_interface_address local address selection failed (%d)", error);
9140 goto done;
9141 }
9142
9143 if (local_address.ss_len > buffer_size) {
9144 error = EMSGSIZE;
9145 NECPLOG(LOG_ERR, "necp_client_get_interface_address local address too long for buffer (%u)",
9146 local_address.ss_len);
9147 goto done;
9148 }
9149
9150 error = copyout(&local_address, uap->buffer, local_address.ss_len);
9151 if (error) {
9152 NECPLOG(LOG_ERR, "necp_client_get_interface_address copyout error (%d)", error);
9153 goto done;
9154 }
9155 done:
9156 *retval = error;
9157
9158 return error;
9159 }
9160
9161 extern char *proc_name_address(void *p);
9162
9163 int
necp_stats_ctor(struct skmem_obj_info * oi,struct skmem_obj_info * oim,void * arg,uint32_t skmflag)9164 necp_stats_ctor(struct skmem_obj_info *oi, struct skmem_obj_info *oim,
9165 void *arg, uint32_t skmflag)
9166 {
9167 #pragma unused(arg, skmflag)
9168 struct necp_all_kstats *kstats = SKMEM_OBJ_ADDR(oi);
9169
9170 ASSERT(oim != NULL && SKMEM_OBJ_ADDR(oim) != NULL);
9171 ASSERT(SKMEM_OBJ_SIZE(oi) == SKMEM_OBJ_SIZE(oim));
9172
9173 kstats->necp_stats_ustats = SKMEM_OBJ_ADDR(oim);
9174
9175 return 0;
9176 }
9177
9178 int
necp_stats_dtor(void * addr,void * arg)9179 necp_stats_dtor(void *addr, void *arg)
9180 {
9181 #pragma unused(addr, arg)
9182 struct necp_all_kstats *kstats = addr;
9183
9184 kstats->necp_stats_ustats = NULL;
9185
9186 return 0;
9187 }
9188
9189 static void
necp_fd_insert_stats_arena(struct necp_fd_data * fd_data,struct necp_arena_info * nai)9190 necp_fd_insert_stats_arena(struct necp_fd_data *fd_data, struct necp_arena_info *nai)
9191 {
9192 NECP_FD_ASSERT_LOCKED(fd_data);
9193 VERIFY(!(nai->nai_flags & NAIF_ATTACHED));
9194 VERIFY(nai->nai_chain.le_next == NULL && nai->nai_chain.le_prev == NULL);
9195
9196 LIST_INSERT_HEAD(&fd_data->stats_arena_list, nai, nai_chain);
9197 nai->nai_flags |= NAIF_ATTACHED;
9198 necp_arena_info_retain(nai); // for the list
9199 }
9200
9201 static void
necp_fd_remove_stats_arena(struct necp_fd_data * fd_data,struct necp_arena_info * nai)9202 necp_fd_remove_stats_arena(struct necp_fd_data *fd_data, struct necp_arena_info *nai)
9203 {
9204 #pragma unused(fd_data)
9205 NECP_FD_ASSERT_LOCKED(fd_data);
9206 VERIFY(nai->nai_flags & NAIF_ATTACHED);
9207 VERIFY(nai->nai_use_count >= 1);
9208
9209 LIST_REMOVE(nai, nai_chain);
9210 nai->nai_flags &= ~NAIF_ATTACHED;
9211 nai->nai_chain.le_next = NULL;
9212 nai->nai_chain.le_prev = NULL;
9213 necp_arena_info_release(nai); // for the list
9214 }
9215
9216 static struct necp_arena_info *
necp_fd_mredirect_stats_arena(struct necp_fd_data * fd_data,struct proc * proc)9217 necp_fd_mredirect_stats_arena(struct necp_fd_data *fd_data, struct proc *proc)
9218 {
9219 struct necp_arena_info *nai, *nai_ret = NULL;
9220
9221 NECP_FD_ASSERT_LOCKED(fd_data);
9222
9223 // Redirect currently-active stats arena and remove it from the active state;
9224 // upon process resumption, new flow request would trigger the creation of
9225 // another active arena.
9226 if ((nai = fd_data->stats_arena_active) != NULL) {
9227 boolean_t need_defunct = FALSE;
9228
9229 ASSERT(!(nai->nai_flags & (NAIF_REDIRECT | NAIF_DEFUNCT)));
9230 VERIFY(nai->nai_use_count >= 2);
9231 ASSERT(nai->nai_arena != NULL);
9232 ASSERT(nai->nai_mmap.ami_mapref != NULL);
9233
9234 int err = skmem_arena_mredirect(nai->nai_arena, &nai->nai_mmap, proc, &need_defunct);
9235 VERIFY(err == 0);
9236 // must be TRUE since we don't mmap the arena more than once
9237 VERIFY(need_defunct == TRUE);
9238
9239 nai->nai_flags |= NAIF_REDIRECT;
9240 nai_ret = nai; // return to caller
9241
9242 necp_arena_info_release(nai); // for fd_data
9243 fd_data->stats_arena_active = nai = NULL;
9244 }
9245
9246 #if (DEVELOPMENT || DEBUG)
9247 // make sure this list now contains nothing but redirected/defunct arenas
9248 LIST_FOREACH(nai, &fd_data->stats_arena_list, nai_chain) {
9249 ASSERT(nai->nai_use_count >= 1);
9250 ASSERT(nai->nai_flags & (NAIF_REDIRECT | NAIF_DEFUNCT));
9251 }
9252 #endif /* (DEVELOPMENT || DEBUG) */
9253
9254 return nai_ret;
9255 }
9256
9257 static void
necp_arena_info_retain(struct necp_arena_info * nai)9258 necp_arena_info_retain(struct necp_arena_info *nai)
9259 {
9260 nai->nai_use_count++;
9261 VERIFY(nai->nai_use_count != 0);
9262 }
9263
9264 static void
necp_arena_info_release(struct necp_arena_info * nai)9265 necp_arena_info_release(struct necp_arena_info *nai)
9266 {
9267 VERIFY(nai->nai_use_count > 0);
9268 if (--nai->nai_use_count == 0) {
9269 necp_arena_info_free(nai);
9270 }
9271 }
9272
9273 static struct necp_arena_info *
necp_arena_info_alloc(void)9274 necp_arena_info_alloc(void)
9275 {
9276 return zalloc_flags(necp_arena_info_zone, Z_WAITOK | Z_ZERO);
9277 }
9278
9279 static void
necp_arena_info_free(struct necp_arena_info * nai)9280 necp_arena_info_free(struct necp_arena_info *nai)
9281 {
9282 VERIFY(nai->nai_chain.le_next == NULL && nai->nai_chain.le_prev == NULL);
9283 VERIFY(nai->nai_use_count == 0);
9284
9285 // NOTE: destroying the arena requires that all outstanding objects
9286 // that were allocated have been freed, else it will assert.
9287 if (nai->nai_arena != NULL) {
9288 skmem_arena_munmap(nai->nai_arena, &nai->nai_mmap);
9289 skmem_arena_release(nai->nai_arena);
9290 OSDecrementAtomic(&necp_arena_count);
9291 nai->nai_arena = NULL;
9292 nai->nai_roff = 0;
9293 }
9294
9295 ASSERT(nai->nai_arena == NULL);
9296 ASSERT(nai->nai_mmap.ami_mapref == NULL);
9297 ASSERT(nai->nai_mmap.ami_arena == NULL);
9298 ASSERT(nai->nai_mmap.ami_maptask == TASK_NULL);
9299
9300 zfree(necp_arena_info_zone, nai);
9301 }
9302
9303 static int
necp_arena_create(struct necp_fd_data * fd_data,size_t obj_size,size_t obj_cnt,struct proc * p)9304 necp_arena_create(struct necp_fd_data *fd_data, size_t obj_size, size_t obj_cnt, struct proc *p)
9305 {
9306 struct skmem_region_params srp_ustats = {};
9307 struct skmem_region_params srp_kstats = {};
9308 struct necp_arena_info *nai;
9309 char name[32];
9310 int error = 0;
9311
9312 NECP_FD_ASSERT_LOCKED(fd_data);
9313 ASSERT(fd_data->stats_arena_active == NULL);
9314 ASSERT(p != PROC_NULL);
9315 ASSERT(proc_pid(p) == fd_data->proc_pid);
9316
9317 // inherit the default parameters for the stats region
9318 srp_ustats = *skmem_get_default(SKMEM_REGION_USTATS);
9319 srp_kstats = *skmem_get_default(SKMEM_REGION_KSTATS);
9320
9321 // enable multi-segment mode
9322 srp_ustats.srp_cflags &= ~SKMEM_REGION_CR_MONOLITHIC;
9323 srp_kstats.srp_cflags &= ~SKMEM_REGION_CR_MONOLITHIC;
9324
9325 // configure and adjust the region parameters
9326 srp_ustats.srp_r_obj_cnt = srp_kstats.srp_r_obj_cnt = obj_cnt;
9327 srp_ustats.srp_r_obj_size = srp_kstats.srp_r_obj_size = obj_size;
9328 skmem_region_params_config(&srp_ustats);
9329 skmem_region_params_config(&srp_kstats);
9330
9331 nai = necp_arena_info_alloc();
9332
9333 nai->nai_proc_pid = fd_data->proc_pid;
9334 (void) snprintf(name, sizeof(name), "stats-%u.%s.%d", fd_data->stats_arena_gencnt, proc_name_address(p), fd_data->proc_pid);
9335 nai->nai_arena = skmem_arena_create_for_necp(name, &srp_ustats, &srp_kstats, &error);
9336 ASSERT(nai->nai_arena != NULL || error != 0);
9337 if (error != 0) {
9338 NECPLOG(LOG_ERR, "failed to create stats arena for pid %d\n", fd_data->proc_pid);
9339 } else {
9340 OSIncrementAtomic(&necp_arena_count);
9341
9342 // Get region offsets from base of mmap span; the arena
9343 // doesn't need to be mmap'd at this point, since we simply
9344 // compute the relative offset.
9345 nai->nai_roff = skmem_arena_get_region_offset(nai->nai_arena, SKMEM_REGION_USTATS);
9346
9347 // map to the task/process; upon success, the base address of the region
9348 // will be returned in nai_mmap.ami_mapaddr; this can be communicated to
9349 // the process.
9350 error = skmem_arena_mmap(nai->nai_arena, p, &nai->nai_mmap);
9351 if (error != 0) {
9352 NECPLOG(LOG_ERR, "failed to map stats arena for pid %d\n", fd_data->proc_pid);
9353 }
9354 }
9355
9356 if (error == 0) {
9357 fd_data->stats_arena_active = nai;
9358 necp_arena_info_retain(nai); // for fd_data
9359 necp_fd_insert_stats_arena(fd_data, nai);
9360 ++fd_data->stats_arena_gencnt;
9361 } else {
9362 necp_arena_info_free(nai);
9363 }
9364
9365 return error;
9366 }
9367
9368 static int
necp_arena_stats_obj_alloc(struct necp_fd_data * fd_data,mach_vm_offset_t * off,struct necp_arena_info ** stats_arena,void ** kstats_kaddr,boolean_t cansleep)9369 necp_arena_stats_obj_alloc(struct necp_fd_data *fd_data,
9370 mach_vm_offset_t *off,
9371 struct necp_arena_info **stats_arena,
9372 void **kstats_kaddr,
9373 boolean_t cansleep)
9374 {
9375 struct skmem_cache *kstats_cp = NULL;
9376 void *ustats_obj = NULL;
9377 void *kstats_obj = NULL;
9378 struct necp_all_kstats *kstats = NULL;
9379 struct skmem_obj_info kstats_oi = {};
9380
9381 ASSERT(off != NULL);
9382 ASSERT(stats_arena != NULL && *stats_arena == NULL);
9383 ASSERT(kstats_kaddr != NULL && *kstats_kaddr == NULL);
9384
9385 NECP_FD_ASSERT_LOCKED(fd_data);
9386 ASSERT(fd_data->stats_arena_active != NULL);
9387 ASSERT(fd_data->stats_arena_active->nai_arena != NULL);
9388
9389 kstats_cp = skmem_arena_necp(fd_data->stats_arena_active->nai_arena)->arc_kstats_cache;
9390 if ((kstats_obj = skmem_cache_alloc(kstats_cp, (cansleep ? SKMEM_SLEEP : SKMEM_NOSLEEP))) == NULL) {
9391 return ENOMEM;
9392 }
9393
9394 kstats = (struct necp_all_kstats*)kstats_obj;
9395 ustats_obj = kstats->necp_stats_ustats;
9396
9397 skmem_cache_get_obj_info(kstats_cp, kstats_obj, &kstats_oi, NULL);
9398 ASSERT(SKMEM_OBJ_SIZE(&kstats_oi) >= sizeof(struct necp_all_stats));
9399 // reset all stats counters
9400 bzero(ustats_obj, SKMEM_OBJ_SIZE(&kstats_oi));
9401 bzero(&kstats->necp_stats_comm, sizeof(struct necp_all_stats));
9402 *stats_arena = fd_data->stats_arena_active;
9403 *kstats_kaddr = kstats_obj;
9404 // kstats and ustats are mirrored and have the same offset
9405 *off = fd_data->stats_arena_active->nai_roff + SKMEM_OBJ_ROFF(&kstats_oi);
9406
9407 return 0;
9408 }
9409
9410 static void
necp_arena_stats_obj_free(struct necp_fd_data * fd_data,struct necp_arena_info * stats_arena,void ** kstats_kaddr,mach_vm_address_t * ustats_uaddr)9411 necp_arena_stats_obj_free(struct necp_fd_data *fd_data, struct necp_arena_info *stats_arena, void **kstats_kaddr, mach_vm_address_t *ustats_uaddr)
9412 {
9413 #pragma unused(fd_data)
9414 NECP_FD_ASSERT_LOCKED(fd_data);
9415
9416 ASSERT(stats_arena != NULL);
9417 ASSERT(stats_arena->nai_arena != NULL);
9418 ASSERT(kstats_kaddr != NULL && *kstats_kaddr != NULL);
9419 ASSERT(ustats_uaddr != NULL);
9420
9421 skmem_cache_free(skmem_arena_necp(stats_arena->nai_arena)->arc_kstats_cache, *kstats_kaddr);
9422 *kstats_kaddr = NULL;
9423 *ustats_uaddr = 0;
9424 }
9425
9426 // This routine returns the KVA of the sysctls object, as well as the
9427 // offset of that object relative to the mmap base address for the
9428 // task/process.
9429 static void *
necp_arena_sysctls_obj(struct necp_fd_data * fd_data,mach_vm_offset_t * off,size_t * size)9430 necp_arena_sysctls_obj(struct necp_fd_data *fd_data, mach_vm_offset_t *off, size_t *size)
9431 {
9432 void *objaddr;
9433
9434 NECP_FD_ASSERT_LOCKED(fd_data);
9435 ASSERT(fd_data->sysctl_arena != NULL);
9436
9437 // kernel virtual address of the sysctls object
9438 objaddr = skmem_arena_system_sysctls_obj_addr(fd_data->sysctl_arena);
9439 ASSERT(objaddr != NULL);
9440
9441 // Return the relative offset of the sysctls object; there is
9442 // only 1 object in the entire sysctls region, and therefore the
9443 // object's offset is simply the region's offset in the arena.
9444 // (sysctl_mmap.ami_mapaddr + offset) is the address of this object
9445 // in the task/process.
9446 if (off != NULL) {
9447 *off = fd_data->system_sysctls_roff;
9448 }
9449
9450 if (size != NULL) {
9451 *size = skmem_arena_system_sysctls_obj_size(fd_data->sysctl_arena);
9452 ASSERT(*size != 0);
9453 }
9454
9455 return objaddr;
9456 }
9457
9458 static void
necp_stats_arenas_destroy(struct necp_fd_data * fd_data,boolean_t closing)9459 necp_stats_arenas_destroy(struct necp_fd_data *fd_data, boolean_t closing)
9460 {
9461 struct necp_arena_info *nai, *nai_tmp;
9462
9463 NECP_FD_ASSERT_LOCKED(fd_data);
9464
9465 // If reaping (not closing), release reference only for idle active arena; the reference
9466 // count must be 2 by now, when it's not being referred to by any clients/flows.
9467 if ((nai = fd_data->stats_arena_active) != NULL && (closing || nai->nai_use_count == 2)) {
9468 VERIFY(nai->nai_use_count >= 2);
9469 necp_arena_info_release(nai); // for fd_data
9470 fd_data->stats_arena_active = NULL;
9471 }
9472
9473 // clean up any defunct arenas left in the list
9474 LIST_FOREACH_SAFE(nai, &fd_data->stats_arena_list, nai_chain, nai_tmp) {
9475 // If reaping, release reference if the list holds the last one
9476 if (closing || nai->nai_use_count == 1) {
9477 VERIFY(nai->nai_use_count >= 1);
9478 // callee unchains nai (and may free it)
9479 necp_fd_remove_stats_arena(fd_data, nai);
9480 }
9481 }
9482 }
9483
9484 static void
necp_sysctl_arena_destroy(struct necp_fd_data * fd_data)9485 necp_sysctl_arena_destroy(struct necp_fd_data *fd_data)
9486 {
9487 NECP_FD_ASSERT_LOCKED(fd_data);
9488
9489 // NOTE: destroying the arena requires that all outstanding objects
9490 // that were allocated have been freed, else it will assert.
9491 if (fd_data->sysctl_arena != NULL) {
9492 skmem_arena_munmap(fd_data->sysctl_arena, &fd_data->sysctl_mmap);
9493 skmem_arena_release(fd_data->sysctl_arena);
9494 OSDecrementAtomic(&necp_sysctl_arena_count);
9495 fd_data->sysctl_arena = NULL;
9496 fd_data->system_sysctls_roff = 0;
9497 }
9498 }
9499
9500 static int
necp_arena_initialize(struct necp_fd_data * fd_data,bool locked)9501 necp_arena_initialize(struct necp_fd_data *fd_data, bool locked)
9502 {
9503 int error = 0;
9504 size_t stats_obj_size = MAX(sizeof(struct necp_all_stats), sizeof(struct necp_all_kstats));
9505
9506 if (!locked) {
9507 NECP_FD_LOCK(fd_data);
9508 }
9509 if (fd_data->stats_arena_active == NULL) {
9510 error = necp_arena_create(fd_data, stats_obj_size,
9511 NECP_MAX_PER_PROCESS_CLIENT_STATISTICS_STRUCTS,
9512 current_proc());
9513 }
9514 if (!locked) {
9515 NECP_FD_UNLOCK(fd_data);
9516 }
9517
9518 return error;
9519 }
9520
9521 static int
necp_sysctl_arena_initialize(struct necp_fd_data * fd_data,bool locked)9522 necp_sysctl_arena_initialize(struct necp_fd_data *fd_data, bool locked)
9523 {
9524 int error = 0;
9525
9526 if (!locked) {
9527 NECP_FD_LOCK(fd_data);
9528 }
9529
9530 NECP_FD_ASSERT_LOCKED(fd_data);
9531
9532 if (fd_data->sysctl_arena == NULL) {
9533 char name[32];
9534 struct proc *p = current_proc();
9535
9536 ASSERT(p != PROC_NULL);
9537 ASSERT(proc_pid(p) == fd_data->proc_pid);
9538
9539 (void) snprintf(name, sizeof(name), "sysctl.%s.%d", proc_name_address(p), fd_data->proc_pid);
9540 fd_data->sysctl_arena = skmem_arena_create_for_system(name, &error);
9541 ASSERT(fd_data->sysctl_arena != NULL || error != 0);
9542 if (error != 0) {
9543 NECPLOG(LOG_ERR, "failed to create arena for pid %d\n", fd_data->proc_pid);
9544 } else {
9545 OSIncrementAtomic(&necp_sysctl_arena_count);
9546
9547 // Get region offsets from base of mmap span; the arena
9548 // doesn't need to be mmap'd at this point, since we simply
9549 // compute the relative offset.
9550 fd_data->system_sysctls_roff = skmem_arena_get_region_offset(fd_data->sysctl_arena, SKMEM_REGION_SYSCTLS);
9551
9552 // map to the task/process; upon success, the base address of the region
9553 // will be returned in nai_mmap.ami_mapaddr; this can be communicated to
9554 // the process.
9555 error = skmem_arena_mmap(fd_data->sysctl_arena, p, &fd_data->sysctl_mmap);
9556 if (error != 0) {
9557 NECPLOG(LOG_ERR, "failed to map sysctl arena for pid %d\n", fd_data->proc_pid);
9558 necp_sysctl_arena_destroy(fd_data);
9559 }
9560 }
9561 }
9562
9563 if (!locked) {
9564 NECP_FD_UNLOCK(fd_data);
9565 }
9566
9567 return error;
9568 }
9569
9570 static int
necp_client_stats_bufreq(struct necp_fd_data * fd_data,struct necp_client * client,struct necp_client_flow_registration * flow_registration,struct necp_stats_bufreq * bufreq,struct necp_stats_hdr * out_header)9571 necp_client_stats_bufreq(struct necp_fd_data *fd_data,
9572 struct necp_client *client,
9573 struct necp_client_flow_registration *flow_registration,
9574 struct necp_stats_bufreq *bufreq,
9575 struct necp_stats_hdr *out_header)
9576 {
9577 int error = 0;
9578 NECP_CLIENT_ASSERT_LOCKED(client);
9579 NECP_FD_ASSERT_LOCKED(fd_data);
9580
9581 if ((bufreq->necp_stats_bufreq_id == NECP_CLIENT_STATISTICS_BUFREQ_ID) &&
9582 ((bufreq->necp_stats_bufreq_type == NECP_CLIENT_STATISTICS_TYPE_TCP &&
9583 bufreq->necp_stats_bufreq_ver == NECP_CLIENT_STATISTICS_TYPE_TCP_CURRENT_VER) ||
9584 (bufreq->necp_stats_bufreq_type == NECP_CLIENT_STATISTICS_TYPE_UDP &&
9585 bufreq->necp_stats_bufreq_ver == NECP_CLIENT_STATISTICS_TYPE_UDP_CURRENT_VER) ||
9586 (bufreq->necp_stats_bufreq_type == NECP_CLIENT_STATISTICS_TYPE_QUIC &&
9587 bufreq->necp_stats_bufreq_ver == NECP_CLIENT_STATISTICS_TYPE_QUIC_CURRENT_VER)) &&
9588 (bufreq->necp_stats_bufreq_size == sizeof(struct necp_all_stats))) {
9589 // There should be one and only one stats allocation per client.
9590 // If asked more than once, we just repeat ourselves.
9591 if (flow_registration->ustats_uaddr == 0) {
9592 mach_vm_offset_t off;
9593 ASSERT(flow_registration->stats_arena == NULL);
9594 ASSERT(flow_registration->kstats_kaddr == NULL);
9595 ASSERT(flow_registration->ustats_uaddr == 0);
9596 error = necp_arena_stats_obj_alloc(fd_data, &off, &flow_registration->stats_arena, &flow_registration->kstats_kaddr, FALSE);
9597 if (error == 0) {
9598 // upon success, hold a reference for the client; this is released when the client is removed/closed
9599 ASSERT(flow_registration->stats_arena != NULL);
9600 necp_arena_info_retain(flow_registration->stats_arena);
9601
9602 // compute user address based on mapping info and object offset
9603 flow_registration->ustats_uaddr = flow_registration->stats_arena->nai_mmap.ami_mapaddr + off;
9604
9605 // add to collect_stats list
9606 NECP_STATS_LIST_LOCK_EXCLUSIVE();
9607 necp_client_retain_locked(client); // Add a reference to the client
9608 LIST_INSERT_HEAD(&necp_collect_stats_flow_list, flow_registration, collect_stats_chain);
9609 NECP_STATS_LIST_UNLOCK();
9610 necp_schedule_collect_stats_clients(FALSE);
9611 } else {
9612 ASSERT(flow_registration->stats_arena == NULL);
9613 ASSERT(flow_registration->kstats_kaddr == NULL);
9614 }
9615 }
9616 if (flow_registration->ustats_uaddr != 0) {
9617 ASSERT(error == 0);
9618 ASSERT(flow_registration->stats_arena != NULL);
9619 ASSERT(flow_registration->kstats_kaddr != NULL);
9620
9621 struct necp_all_kstats *kstats = (struct necp_all_kstats *)flow_registration->kstats_kaddr;
9622 kstats->necp_stats_ustats->all_stats_u.tcp_stats.necp_tcp_hdr.necp_stats_type = bufreq->necp_stats_bufreq_type;
9623 kstats->necp_stats_ustats->all_stats_u.tcp_stats.necp_tcp_hdr.necp_stats_ver = bufreq->necp_stats_bufreq_ver;
9624
9625 if (out_header) {
9626 out_header->necp_stats_type = bufreq->necp_stats_bufreq_type;
9627 out_header->necp_stats_ver = bufreq->necp_stats_bufreq_ver;
9628 }
9629
9630 bufreq->necp_stats_bufreq_uaddr = flow_registration->ustats_uaddr;
9631 }
9632 } else {
9633 error = EINVAL;
9634 }
9635
9636 return error;
9637 }
9638
9639 static int
necp_client_stats_initial(struct necp_client_flow_registration * flow_registration,uint32_t stats_type,uint32_t stats_ver)9640 necp_client_stats_initial(struct necp_client_flow_registration *flow_registration, uint32_t stats_type, uint32_t stats_ver)
9641 {
9642 // An attempted create
9643 assert(flow_registration->stats_handler_context == NULL);
9644 assert(flow_registration->stats_arena);
9645 assert(flow_registration->ustats_uaddr);
9646 assert(flow_registration->kstats_kaddr);
9647
9648 int error = 0;
9649
9650 switch (stats_type) {
9651 case NECP_CLIENT_STATISTICS_TYPE_TCP: {
9652 if (stats_ver == NECP_CLIENT_STATISTICS_TYPE_TCP_VER_1) {
9653 flow_registration->stats_handler_context = ntstat_userland_stats_open((userland_stats_provider_context *)flow_registration,
9654 NSTAT_PROVIDER_TCP_USERLAND, 0, necp_request_tcp_netstats, necp_find_extension_info);
9655 if (flow_registration->stats_handler_context == NULL) {
9656 error = EIO;
9657 }
9658 } else {
9659 error = ENOTSUP;
9660 }
9661 break;
9662 }
9663 case NECP_CLIENT_STATISTICS_TYPE_UDP: {
9664 if (stats_ver == NECP_CLIENT_STATISTICS_TYPE_UDP_VER_1) {
9665 flow_registration->stats_handler_context = ntstat_userland_stats_open((userland_stats_provider_context *)flow_registration,
9666 NSTAT_PROVIDER_UDP_USERLAND, 0, necp_request_udp_netstats, necp_find_extension_info);
9667 if (flow_registration->stats_handler_context == NULL) {
9668 error = EIO;
9669 }
9670 } else {
9671 error = ENOTSUP;
9672 }
9673 break;
9674 }
9675 case NECP_CLIENT_STATISTICS_TYPE_QUIC: {
9676 if (stats_ver == NECP_CLIENT_STATISTICS_TYPE_QUIC_VER_1 && flow_registration->flags & NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS) {
9677 flow_registration->stats_handler_context = ntstat_userland_stats_open((userland_stats_provider_context *)flow_registration,
9678 NSTAT_PROVIDER_QUIC_USERLAND, 0, necp_request_quic_netstats, necp_find_extension_info);
9679 if (flow_registration->stats_handler_context == NULL) {
9680 error = EIO;
9681 }
9682 } else {
9683 error = ENOTSUP;
9684 }
9685 break;
9686 }
9687 default: {
9688 error = ENOTSUP;
9689 break;
9690 }
9691 }
9692 return error;
9693 }
9694
9695 static int
necp_stats_initialize(struct necp_fd_data * fd_data,struct necp_client * client,struct necp_client_flow_registration * flow_registration,struct necp_stats_bufreq * bufreq)9696 necp_stats_initialize(struct necp_fd_data *fd_data,
9697 struct necp_client *client,
9698 struct necp_client_flow_registration *flow_registration,
9699 struct necp_stats_bufreq *bufreq)
9700 {
9701 int error = 0;
9702 struct necp_stats_hdr stats_hdr = {};
9703
9704 NECP_CLIENT_ASSERT_LOCKED(client);
9705 NECP_FD_ASSERT_LOCKED(fd_data);
9706 VERIFY(fd_data->stats_arena_active != NULL);
9707 VERIFY(fd_data->stats_arena_active->nai_arena != NULL);
9708 VERIFY(!(fd_data->stats_arena_active->nai_flags & (NAIF_REDIRECT | NAIF_DEFUNCT)));
9709
9710 if (bufreq == NULL) {
9711 return EINVAL;
9712 }
9713
9714 // Setup stats region
9715 error = necp_client_stats_bufreq(fd_data, client, flow_registration, bufreq, &stats_hdr);
9716 if (error) {
9717 return error;
9718 }
9719 // Notify ntstat about new flow
9720 if (flow_registration->stats_handler_context == NULL) {
9721 error = necp_client_stats_initial(flow_registration, stats_hdr.necp_stats_type, stats_hdr.necp_stats_ver);
9722 if (flow_registration->stats_handler_context != NULL) {
9723 ntstat_userland_stats_event(flow_registration->stats_handler_context, NECP_CLIENT_STATISTICS_EVENT_INIT);
9724 }
9725 NECP_CLIENT_FLOW_LOG(client, flow_registration, "Initialized stats <error %d>", error);
9726 }
9727
9728 return error;
9729 }
9730
9731 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_map_sysctls(__unused struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)9732 necp_client_map_sysctls(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9733 {
9734 int result = 0;
9735 if (!retval) {
9736 retval = &result;
9737 }
9738
9739 do {
9740 mach_vm_address_t uaddr = 0;
9741 if (uap->buffer_size != sizeof(uaddr)) {
9742 *retval = EINVAL;
9743 break;
9744 }
9745
9746 *retval = necp_sysctl_arena_initialize(fd_data, false);
9747 if (*retval != 0) {
9748 break;
9749 }
9750
9751 mach_vm_offset_t off = 0;
9752 void *location = NULL;
9753 NECP_FD_LOCK(fd_data);
9754 location = necp_arena_sysctls_obj(fd_data, &off, NULL);
9755 NECP_FD_UNLOCK(fd_data);
9756
9757 if (location == NULL) {
9758 *retval = ENOENT;
9759 break;
9760 }
9761
9762 uaddr = fd_data->sysctl_mmap.ami_mapaddr + off;
9763 *retval = copyout(&uaddr, uap->buffer, sizeof(uaddr));
9764 } while (false);
9765
9766 return *retval;
9767 }
9768
9769 #endif /* !SKYWALK */
9770
9771 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_copy_route_statistics(__unused struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)9772 necp_client_copy_route_statistics(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9773 {
9774 int error = 0;
9775 struct necp_client *client = NULL;
9776 uuid_t client_id;
9777
9778 if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
9779 uap->buffer_size < sizeof(struct necp_stat_counts) || uap->buffer == 0) {
9780 NECPLOG0(LOG_ERR, "necp_client_copy_route_statistics bad input");
9781 error = EINVAL;
9782 goto done;
9783 }
9784
9785 error = copyin(uap->client_id, client_id, sizeof(uuid_t));
9786 if (error) {
9787 NECPLOG(LOG_ERR, "necp_client_copy_route_statistics copyin client_id error (%d)", error);
9788 goto done;
9789 }
9790
9791 // Lock
9792 NECP_FD_LOCK(fd_data);
9793 client = necp_client_fd_find_client_and_lock(fd_data, client_id);
9794 if (client != NULL) {
9795 NECP_CLIENT_ROUTE_LOCK(client);
9796 struct necp_stat_counts route_stats = {};
9797 if (client->current_route != NULL && client->current_route->rt_stats != NULL) {
9798 struct nstat_counts *rt_stats = client->current_route->rt_stats;
9799 atomic_get_64(route_stats.necp_stat_rxpackets, &rt_stats->nstat_rxpackets);
9800 atomic_get_64(route_stats.necp_stat_rxbytes, &rt_stats->nstat_rxbytes);
9801 atomic_get_64(route_stats.necp_stat_txpackets, &rt_stats->nstat_txpackets);
9802 atomic_get_64(route_stats.necp_stat_txbytes, &rt_stats->nstat_txbytes);
9803 route_stats.necp_stat_rxduplicatebytes = rt_stats->nstat_rxduplicatebytes;
9804 route_stats.necp_stat_rxoutoforderbytes = rt_stats->nstat_rxoutoforderbytes;
9805 route_stats.necp_stat_txretransmit = rt_stats->nstat_txretransmit;
9806 route_stats.necp_stat_connectattempts = rt_stats->nstat_connectattempts;
9807 route_stats.necp_stat_connectsuccesses = rt_stats->nstat_connectsuccesses;
9808 route_stats.necp_stat_min_rtt = rt_stats->nstat_min_rtt;
9809 route_stats.necp_stat_avg_rtt = rt_stats->nstat_avg_rtt;
9810 route_stats.necp_stat_var_rtt = rt_stats->nstat_var_rtt;
9811 route_stats.necp_stat_route_flags = client->current_route->rt_flags;
9812 }
9813
9814 // Unlock before copying out
9815 NECP_CLIENT_ROUTE_UNLOCK(client);
9816 NECP_CLIENT_UNLOCK(client);
9817 NECP_FD_UNLOCK(fd_data);
9818
9819 error = copyout(&route_stats, uap->buffer, sizeof(route_stats));
9820 if (error) {
9821 NECPLOG(LOG_ERR, "necp_client_copy_route_statistics copyout error (%d)", error);
9822 }
9823 } else {
9824 // Unlock
9825 NECP_FD_UNLOCK(fd_data);
9826 error = ENOENT;
9827 }
9828
9829
9830 done:
9831 *retval = error;
9832 return error;
9833 }
9834
9835 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_update_cache(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)9836 necp_client_update_cache(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9837 {
9838 int error = 0;
9839 struct necp_client *client = NULL;
9840 uuid_t client_id;
9841
9842 if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
9843 error = EINVAL;
9844 goto done;
9845 }
9846
9847 error = copyin(uap->client_id, client_id, sizeof(uuid_t));
9848 if (error) {
9849 NECPLOG(LOG_ERR, "necp_client_update_cache copyin client_id error (%d)", error);
9850 goto done;
9851 }
9852
9853 NECP_FD_LOCK(fd_data);
9854 client = necp_client_fd_find_client_and_lock(fd_data, client_id);
9855 if (client == NULL) {
9856 NECP_FD_UNLOCK(fd_data);
9857 error = ENOENT;
9858 goto done;
9859 }
9860
9861 struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
9862 if (flow_registration == NULL) {
9863 NECP_CLIENT_UNLOCK(client);
9864 NECP_FD_UNLOCK(fd_data);
9865 error = ENOENT;
9866 goto done;
9867 }
9868
9869 NECP_CLIENT_ROUTE_LOCK(client);
9870 // This needs to be changed when TFO/ECN is supported by multiple flows
9871 struct necp_client_flow *flow = LIST_FIRST(&flow_registration->flow_list);
9872 if (flow == NULL ||
9873 (flow->remote_addr.sa.sa_family != AF_INET &&
9874 flow->remote_addr.sa.sa_family != AF_INET6) ||
9875 (flow->local_addr.sa.sa_family != AF_INET &&
9876 flow->local_addr.sa.sa_family != AF_INET6)) {
9877 error = EINVAL;
9878 NECPLOG(LOG_ERR, "necp_client_update_cache no flow error (%d)", error);
9879 goto done_unlock;
9880 }
9881
9882 necp_cache_buffer cache_buffer;
9883 memset(&cache_buffer, 0, sizeof(cache_buffer));
9884
9885 if (uap->buffer_size != sizeof(necp_cache_buffer) ||
9886 uap->buffer == USER_ADDR_NULL) {
9887 error = EINVAL;
9888 goto done_unlock;
9889 }
9890
9891 error = copyin(uap->buffer, &cache_buffer, sizeof(cache_buffer));
9892 if (error) {
9893 NECPLOG(LOG_ERR, "necp_client_update_cache copyin cache buffer error (%d)", error);
9894 goto done_unlock;
9895 }
9896
9897 if (cache_buffer.necp_cache_buf_type == NECP_CLIENT_CACHE_TYPE_ECN &&
9898 cache_buffer.necp_cache_buf_ver == NECP_CLIENT_CACHE_TYPE_ECN_VER_1) {
9899 if (cache_buffer.necp_cache_buf_size != sizeof(necp_tcp_ecn_cache) ||
9900 cache_buffer.necp_cache_buf_addr == USER_ADDR_NULL) {
9901 error = EINVAL;
9902 goto done_unlock;
9903 }
9904
9905 necp_tcp_ecn_cache ecn_cache_buffer;
9906 memset(&ecn_cache_buffer, 0, sizeof(ecn_cache_buffer));
9907
9908 error = copyin(cache_buffer.necp_cache_buf_addr, &ecn_cache_buffer, sizeof(necp_tcp_ecn_cache));
9909 if (error) {
9910 NECPLOG(LOG_ERR, "necp_client_update_cache copyin ecn cache buffer error (%d)", error);
9911 goto done_unlock;
9912 }
9913
9914 if (client->current_route != NULL && client->current_route->rt_ifp != NULL) {
9915 if (!client->platform_binary) {
9916 ecn_cache_buffer.necp_tcp_ecn_heuristics_success = 0;
9917 }
9918 tcp_heuristics_ecn_update(&ecn_cache_buffer, client->current_route->rt_ifp,
9919 (union sockaddr_in_4_6 *)&flow->local_addr);
9920 }
9921 } else if (cache_buffer.necp_cache_buf_type == NECP_CLIENT_CACHE_TYPE_TFO &&
9922 cache_buffer.necp_cache_buf_ver == NECP_CLIENT_CACHE_TYPE_TFO_VER_1) {
9923 if (cache_buffer.necp_cache_buf_size != sizeof(necp_tcp_tfo_cache) ||
9924 cache_buffer.necp_cache_buf_addr == USER_ADDR_NULL) {
9925 error = EINVAL;
9926 goto done_unlock;
9927 }
9928
9929 necp_tcp_tfo_cache tfo_cache_buffer;
9930 memset(&tfo_cache_buffer, 0, sizeof(tfo_cache_buffer));
9931
9932 error = copyin(cache_buffer.necp_cache_buf_addr, &tfo_cache_buffer, sizeof(necp_tcp_tfo_cache));
9933 if (error) {
9934 NECPLOG(LOG_ERR, "necp_client_update_cache copyin tfo cache buffer error (%d)", error);
9935 goto done_unlock;
9936 }
9937
9938 if (client->current_route != NULL && client->current_route->rt_ifp != NULL) {
9939 if (!client->platform_binary) {
9940 tfo_cache_buffer.necp_tcp_tfo_heuristics_success = 0;
9941 }
9942 tcp_heuristics_tfo_update(&tfo_cache_buffer, client->current_route->rt_ifp,
9943 (union sockaddr_in_4_6 *)&flow->local_addr,
9944 (union sockaddr_in_4_6 *)&flow->remote_addr);
9945 }
9946 } else {
9947 error = EINVAL;
9948 }
9949 done_unlock:
9950 NECP_CLIENT_ROUTE_UNLOCK(client);
9951 NECP_CLIENT_UNLOCK(client);
9952 NECP_FD_UNLOCK(fd_data);
9953 done:
9954 *retval = error;
9955 return error;
9956 }
9957
9958 // Most results will fit into this size
9959 struct necp_client_signable_default {
9960 uuid_t client_id;
9961 u_int32_t sign_type;
9962 u_int8_t signable_data[NECP_CLIENT_ACTION_SIGN_DEFAULT_DATA_LENGTH];
9963 } __attribute__((__packed__));
9964
9965 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_sign(__unused struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)9966 necp_client_sign(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9967 {
9968 int error = 0;
9969 u_int8_t tag[NECP_CLIENT_ACTION_SIGN_TAG_LENGTH] = {};
9970 struct necp_client_signable *signable = NULL;
9971 struct necp_client_signable *allocated_signable = NULL;
9972 struct necp_client_signable_default default_signable = {};
9973 size_t tag_size = sizeof(tag);
9974
9975 const size_t signable_length = uap->client_id_len;
9976 const size_t return_tag_length = uap->buffer_size;
9977
9978 *retval = 0;
9979
9980 const bool has_resolver_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_VALIDATED_RESOLVER, 0) == 0);
9981 if (!has_resolver_entitlement) {
9982 NECPLOG0(LOG_ERR, "Process does not hold the necessary entitlement to sign resolver answers");
9983 error = EPERM;
9984 goto done;
9985 }
9986
9987 if (uap->client_id == 0 || signable_length < sizeof(*signable) || signable_length > NECP_CLIENT_ACTION_SIGN_MAX_TOTAL_LENGTH) {
9988 error = EINVAL;
9989 goto done;
9990 }
9991
9992 if (uap->buffer == 0 || return_tag_length != NECP_CLIENT_ACTION_SIGN_TAG_LENGTH) {
9993 error = EINVAL;
9994 goto done;
9995 }
9996
9997 if (signable_length <= sizeof(default_signable)) {
9998 signable = (struct necp_client_signable *)&default_signable;
9999 } else {
10000 if ((allocated_signable = (struct necp_client_signable *)kalloc_data(signable_length, Z_WAITOK | Z_ZERO)) == NULL) {
10001 NECPLOG(LOG_ERR, "necp_client_sign allocate signable %zu failed", signable_length);
10002 error = ENOMEM;
10003 goto done;
10004 }
10005 signable = allocated_signable;
10006 }
10007
10008 error = copyin(uap->client_id, signable, signable_length);
10009 if (error) {
10010 NECPLOG(LOG_ERR, "necp_client_sign copyin signable error (%d)", error);
10011 goto done;
10012 }
10013
10014 size_t data_length = 0;
10015 switch (signable->sign_type) {
10016 case NECP_CLIENT_SIGN_TYPE_RESOLVER_ANSWER:
10017 case NECP_CLIENT_SIGN_TYPE_SYSTEM_RESOLVER_ANSWER: {
10018 data_length = (sizeof(struct necp_client_host_resolver_answer) - sizeof(struct necp_client_signable));
10019 if (signable_length < (sizeof(struct necp_client_signable) + data_length)) {
10020 error = EINVAL;
10021 goto done;
10022 }
10023 struct necp_client_host_resolver_answer *signable_struct = (struct necp_client_host_resolver_answer *)signable;
10024 if (signable_struct->hostname_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10025 signable_length != (sizeof(struct necp_client_signable) + data_length + signable_struct->hostname_length)) {
10026 error = EINVAL;
10027 goto done;
10028 }
10029 data_length += signable_struct->hostname_length;
10030 break;
10031 }
10032 case NECP_CLIENT_SIGN_TYPE_BROWSE_RESULT:
10033 case NECP_CLIENT_SIGN_TYPE_SYSTEM_BROWSE_RESULT: {
10034 data_length = (sizeof(struct necp_client_browse_result) - sizeof(struct necp_client_signable));
10035 if (signable_length < (sizeof(struct necp_client_signable) + data_length)) {
10036 error = EINVAL;
10037 goto done;
10038 }
10039 struct necp_client_browse_result *signable_struct = (struct necp_client_browse_result *)signable;
10040 if (signable_struct->service_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10041 signable_length != (sizeof(struct necp_client_signable) + data_length + signable_struct->service_length)) {
10042 error = EINVAL;
10043 goto done;
10044 }
10045 data_length += signable_struct->service_length;
10046 break;
10047 }
10048 case NECP_CLIENT_SIGN_TYPE_SERVICE_RESOLVER_ANSWER:
10049 case NECP_CLIENT_SIGN_TYPE_SYSTEM_SERVICE_RESOLVER_ANSWER: {
10050 data_length = (sizeof(struct necp_client_service_resolver_answer) - sizeof(struct necp_client_signable));
10051 if (signable_length < (sizeof(struct necp_client_signable) + data_length)) {
10052 error = EINVAL;
10053 goto done;
10054 }
10055 struct necp_client_service_resolver_answer *signable_struct = (struct necp_client_service_resolver_answer *)signable;
10056 if (signable_struct->service_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10057 signable_struct->hostname_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10058 signable_length != (sizeof(struct necp_client_signable) + data_length + signable_struct->service_length + signable_struct->hostname_length)) {
10059 error = EINVAL;
10060 goto done;
10061 }
10062 data_length += signable_struct->service_length;
10063 data_length += signable_struct->hostname_length;
10064 break;
10065 }
10066 default: {
10067 NECPLOG(LOG_ERR, "necp_client_sign unknown signable type (%u)", signable->sign_type);
10068 error = EINVAL;
10069 goto done;
10070 }
10071 }
10072
10073 error = necp_sign_resolver_answer(signable->client_id, signable->sign_type,
10074 signable->signable_data, data_length,
10075 tag, &tag_size);
10076 if (tag_size != sizeof(tag)) {
10077 NECPLOG(LOG_ERR, "necp_client_sign unexpected tag size %zu", tag_size);
10078 error = EINVAL;
10079 goto done;
10080 }
10081 error = copyout(tag, uap->buffer, tag_size);
10082 if (error) {
10083 NECPLOG(LOG_ERR, "necp_client_sign copyout error (%d)", error);
10084 goto done;
10085 }
10086
10087 done:
10088 if (allocated_signable != NULL) {
10089 kfree_data(allocated_signable, signable_length);
10090 allocated_signable = NULL;
10091 }
10092 *retval = error;
10093 return error;
10094 }
10095
10096 // Most results will fit into this size
10097 struct necp_client_validatable_default {
10098 struct necp_client_signature signature;
10099 struct necp_client_signable_default signable;
10100 } __attribute__((__packed__));
10101
10102 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_validate(__unused struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)10103 necp_client_validate(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10104 {
10105 int error = 0;
10106 struct necp_client_validatable *validatable = NULL;
10107 struct necp_client_validatable *allocated_validatable = NULL;
10108 struct necp_client_validatable_default default_validatable = {};
10109
10110 const size_t validatable_length = uap->client_id_len;
10111
10112 *retval = 0;
10113
10114 const bool has_resolver_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_VALIDATED_RESOLVER, 0) == 0);
10115 if (!has_resolver_entitlement) {
10116 NECPLOG0(LOG_ERR, "Process does not hold the necessary entitlement to directly validate resolver answers");
10117 error = EPERM;
10118 goto done;
10119 }
10120
10121 if (uap->client_id == 0 || validatable_length < sizeof(*validatable) ||
10122 validatable_length > (NECP_CLIENT_ACTION_SIGN_MAX_TOTAL_LENGTH + NECP_CLIENT_ACTION_SIGN_TAG_LENGTH)) {
10123 error = EINVAL;
10124 goto done;
10125 }
10126
10127 if (validatable_length <= sizeof(default_validatable)) {
10128 validatable = (struct necp_client_validatable *)&default_validatable;
10129 } else {
10130 if ((allocated_validatable = (struct necp_client_validatable *)kalloc_data(validatable_length, Z_WAITOK | Z_ZERO)) == NULL) {
10131 NECPLOG(LOG_ERR, "necp_client_validate allocate struct %zu failed", validatable_length);
10132 error = ENOMEM;
10133 goto done;
10134 }
10135 validatable = allocated_validatable;
10136 }
10137
10138 error = copyin(uap->client_id, validatable, validatable_length);
10139 if (error) {
10140 NECPLOG(LOG_ERR, "necp_client_validate copyin error (%d)", error);
10141 goto done;
10142 }
10143
10144 const bool validated = necp_validate_resolver_answer(validatable->signable.client_id, validatable->signable.sign_type,
10145 validatable->signable.signable_data, validatable_length - sizeof(struct necp_client_validatable),
10146 validatable->signature.signed_tag, sizeof(validatable->signature.signed_tag));
10147 if (!validated) {
10148 // Return EAUTH to indicate that the signature failed
10149 error = EAUTH;
10150 }
10151
10152 done:
10153 if (allocated_validatable != NULL) {
10154 kfree_data(allocated_validatable, validatable_length);
10155 allocated_validatable = NULL;
10156 }
10157 *retval = error;
10158 return error;
10159 }
10160
10161 int
necp_client_action(struct proc * p,struct necp_client_action_args * uap,int * retval)10162 necp_client_action(struct proc *p, struct necp_client_action_args *uap, int *retval)
10163 {
10164 struct fileproc *fp;
10165 int error = 0;
10166 int return_value = 0;
10167 struct necp_fd_data *fd_data = NULL;
10168
10169 error = necp_find_fd_data(p, uap->necp_fd, &fp, &fd_data);
10170 if (error != 0) {
10171 NECPLOG(LOG_ERR, "necp_client_action find fd error (%d)", error);
10172 return error;
10173 }
10174
10175 u_int32_t action = uap->action;
10176
10177 #if CONFIG_MACF
10178 error = mac_necp_check_client_action(p, fp->fp_glob, action);
10179 if (error) {
10180 return_value = error;
10181 goto done;
10182 }
10183 #endif /* MACF */
10184
10185 switch (action) {
10186 case NECP_CLIENT_ACTION_ADD: {
10187 return_value = necp_client_add(p, fd_data, uap, retval);
10188 break;
10189 }
10190 case NECP_CLIENT_ACTION_CLAIM: {
10191 return_value = necp_client_claim(p, fd_data, uap, retval);
10192 break;
10193 }
10194 case NECP_CLIENT_ACTION_REMOVE: {
10195 return_value = necp_client_remove(fd_data, uap, retval);
10196 break;
10197 }
10198 case NECP_CLIENT_ACTION_COPY_PARAMETERS:
10199 case NECP_CLIENT_ACTION_COPY_RESULT:
10200 case NECP_CLIENT_ACTION_COPY_UPDATED_RESULT: {
10201 return_value = necp_client_copy(fd_data, uap, retval);
10202 break;
10203 }
10204 case NECP_CLIENT_ACTION_COPY_LIST: {
10205 return_value = necp_client_list(fd_data, uap, retval);
10206 break;
10207 }
10208 case NECP_CLIENT_ACTION_ADD_FLOW: {
10209 return_value = necp_client_add_flow(fd_data, uap, retval);
10210 break;
10211 }
10212 case NECP_CLIENT_ACTION_REMOVE_FLOW: {
10213 return_value = necp_client_remove_flow(fd_data, uap, retval);
10214 break;
10215 }
10216 #if SKYWALK
10217 case NECP_CLIENT_ACTION_REQUEST_NEXUS_INSTANCE: {
10218 return_value = necp_client_request_nexus(fd_data, uap, retval);
10219 break;
10220 }
10221 #endif /* !SKYWALK */
10222 case NECP_CLIENT_ACTION_AGENT: {
10223 return_value = necp_client_agent_action(fd_data, uap, retval);
10224 break;
10225 }
10226 case NECP_CLIENT_ACTION_COPY_AGENT: {
10227 return_value = necp_client_copy_agent(fd_data, uap, retval);
10228 break;
10229 }
10230 case NECP_CLIENT_ACTION_AGENT_USE: {
10231 return_value = necp_client_agent_use(fd_data, uap, retval);
10232 break;
10233 }
10234 case NECP_CLIENT_ACTION_ACQUIRE_AGENT_TOKEN: {
10235 return_value = necp_client_acquire_agent_token(fd_data, uap, retval);
10236 break;
10237 }
10238 case NECP_CLIENT_ACTION_COPY_INTERFACE: {
10239 return_value = necp_client_copy_interface(fd_data, uap, retval);
10240 break;
10241 }
10242 #if SKYWALK
10243 case NECP_CLIENT_ACTION_GET_INTERFACE_ADDRESS: {
10244 return_value = necp_client_get_interface_address(fd_data, uap, retval);
10245 break;
10246 }
10247 case NECP_CLIENT_ACTION_SET_STATISTICS: {
10248 return_value = ENOTSUP;
10249 break;
10250 }
10251 case NECP_CLIENT_ACTION_MAP_SYSCTLS: {
10252 return_value = necp_client_map_sysctls(fd_data, uap, retval);
10253 break;
10254 }
10255 #endif /* !SKYWALK */
10256 case NECP_CLIENT_ACTION_COPY_ROUTE_STATISTICS: {
10257 return_value = necp_client_copy_route_statistics(fd_data, uap, retval);
10258 break;
10259 }
10260 case NECP_CLIENT_ACTION_UPDATE_CACHE: {
10261 return_value = necp_client_update_cache(fd_data, uap, retval);
10262 break;
10263 }
10264 case NECP_CLIENT_ACTION_COPY_CLIENT_UPDATE: {
10265 return_value = necp_client_copy_client_update(fd_data, uap, retval);
10266 break;
10267 }
10268 case NECP_CLIENT_ACTION_SIGN: {
10269 return_value = necp_client_sign(fd_data, uap, retval);
10270 break;
10271 }
10272 case NECP_CLIENT_ACTION_VALIDATE: {
10273 return_value = necp_client_validate(fd_data, uap, retval);
10274 break;
10275 }
10276 default: {
10277 NECPLOG(LOG_ERR, "necp_client_action unknown action (%u)", action);
10278 return_value = EINVAL;
10279 break;
10280 }
10281 }
10282
10283 done:
10284 fp_drop(p, uap->necp_fd, fp, 0);
10285 return return_value;
10286 }
10287
10288 #define NECP_MAX_MATCH_POLICY_PARAMETER_SIZE 1024
10289
10290 int
necp_match_policy(struct proc * p,struct necp_match_policy_args * uap,int32_t * retval)10291 necp_match_policy(struct proc *p, struct necp_match_policy_args *uap, int32_t *retval)
10292 {
10293 #pragma unused(retval)
10294 u_int8_t *parameters = NULL;
10295 struct necp_aggregate_result returned_result;
10296 int error = 0;
10297
10298 if (uap == NULL) {
10299 error = EINVAL;
10300 goto done;
10301 }
10302
10303 if (uap->parameters == 0 || uap->parameters_size == 0 || uap->parameters_size > NECP_MAX_MATCH_POLICY_PARAMETER_SIZE || uap->returned_result == 0) {
10304 error = EINVAL;
10305 goto done;
10306 }
10307
10308 parameters = (u_int8_t *)kalloc_data(uap->parameters_size, Z_WAITOK | Z_ZERO);
10309 if (parameters == NULL) {
10310 error = ENOMEM;
10311 goto done;
10312 }
10313 // Copy parameters in
10314 error = copyin(uap->parameters, parameters, uap->parameters_size);
10315 if (error) {
10316 goto done;
10317 }
10318
10319 error = necp_application_find_policy_match_internal(p, parameters, uap->parameters_size,
10320 &returned_result, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, false, false, NULL);
10321 if (error) {
10322 goto done;
10323 }
10324
10325 // Copy return value back
10326 error = copyout(&returned_result, uap->returned_result, sizeof(struct necp_aggregate_result));
10327 if (error) {
10328 goto done;
10329 }
10330 done:
10331 if (parameters != NULL) {
10332 kfree_data(parameters, uap->parameters_size);
10333 }
10334 return error;
10335 }
10336
10337 /// Socket operations
10338
10339 static errno_t
necp_set_socket_attribute(u_int8_t * buffer,size_t buffer_length,u_int8_t type,char ** buffer_p,bool * single_tlv)10340 necp_set_socket_attribute(u_int8_t *buffer, size_t buffer_length, u_int8_t type, char **buffer_p, bool *single_tlv)
10341 {
10342 int error = 0;
10343 int cursor = 0;
10344 size_t string_size = 0;
10345 char *local_string = NULL;
10346 u_int8_t *value = NULL;
10347 char *buffer_to_free = NULL;
10348
10349 cursor = necp_buffer_find_tlv(buffer, buffer_length, 0, type, NULL, 0);
10350 if (cursor < 0) {
10351 // This will clear out the parameter
10352 goto done;
10353 }
10354
10355 string_size = necp_buffer_get_tlv_length(buffer, cursor);
10356 if (single_tlv != NULL && (buffer_length == sizeof(struct necp_tlv_header) + string_size)) {
10357 *single_tlv = true;
10358 }
10359 if (string_size == 0 || string_size > NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) {
10360 // This will clear out the parameter
10361 goto done;
10362 }
10363
10364 local_string = (char *)kalloc_data(string_size + 1, Z_WAITOK | Z_ZERO);
10365 if (local_string == NULL) {
10366 NECPLOG(LOG_ERR, "Failed to allocate a socket attribute buffer (size %zu)", string_size);
10367 goto fail;
10368 }
10369
10370 value = necp_buffer_get_tlv_value(buffer, cursor, NULL);
10371 if (value == NULL) {
10372 NECPLOG0(LOG_ERR, "Failed to get socket attribute");
10373 goto fail;
10374 }
10375
10376 memcpy(local_string, value, string_size);
10377 local_string[string_size] = 0;
10378
10379 done:
10380 buffer_to_free = *buffer_p;
10381
10382 // Protect switching of buffer pointer
10383 necp_lock_socket_attributes();
10384 *buffer_p = local_string;
10385 necp_unlock_socket_attributes();
10386
10387 if (buffer_to_free != NULL) {
10388 kfree_data_addr(buffer_to_free);
10389 }
10390 return 0;
10391 fail:
10392 if (local_string != NULL) {
10393 kfree_data(local_string, string_size + 1);
10394 }
10395 return error;
10396 }
10397
10398 errno_t
necp_set_socket_attributes(struct inp_necp_attributes * attributes,struct sockopt * sopt)10399 necp_set_socket_attributes(struct inp_necp_attributes *attributes, struct sockopt *sopt)
10400 {
10401 int error = 0;
10402 u_int8_t *buffer = NULL;
10403 bool single_tlv = false;
10404 size_t valsize = sopt->sopt_valsize;
10405 if (valsize == 0 ||
10406 valsize > ((sizeof(struct necp_tlv_header) + NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) * 4)) {
10407 goto done;
10408 }
10409
10410 buffer = (u_int8_t *)kalloc_data(valsize, Z_WAITOK | Z_ZERO);
10411 if (buffer == NULL) {
10412 goto done;
10413 }
10414
10415 error = sooptcopyin(sopt, buffer, valsize, 0);
10416 if (error) {
10417 goto done;
10418 }
10419
10420 // If NECP_TLV_ATTRIBUTE_DOMAIN_CONTEXT is being set/cleared separately from the other attributes,
10421 // do not clear other attributes.
10422 error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_DOMAIN_CONTEXT, &attributes->inp_domain_context, &single_tlv);
10423 if (error) {
10424 NECPLOG0(LOG_ERR, "Could not set domain context TLV for socket attributes");
10425 goto done;
10426 }
10427 if (single_tlv == true) {
10428 goto done;
10429 }
10430
10431 error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_DOMAIN, &attributes->inp_domain, NULL);
10432 if (error) {
10433 NECPLOG0(LOG_ERR, "Could not set domain TLV for socket attributes");
10434 goto done;
10435 }
10436
10437 error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_DOMAIN_OWNER, &attributes->inp_domain_owner, NULL);
10438 if (error) {
10439 NECPLOG0(LOG_ERR, "Could not set domain owner TLV for socket attributes");
10440 goto done;
10441 }
10442
10443 error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_TRACKER_DOMAIN, &attributes->inp_tracker_domain, NULL);
10444 if (error) {
10445 NECPLOG0(LOG_ERR, "Could not set tracker domain TLV for socket attributes");
10446 goto done;
10447 }
10448
10449 error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_ACCOUNT, &attributes->inp_account, NULL);
10450 if (error) {
10451 NECPLOG0(LOG_ERR, "Could not set account TLV for socket attributes");
10452 goto done;
10453 }
10454
10455 done:
10456 NECP_SOCKET_ATTRIBUTE_LOG("NECP ATTRIBUTES SOCKET - domain <%s> owner <%s> context <%s> tracker domain <%s> account <%s>",
10457 attributes->inp_domain,
10458 attributes->inp_domain_owner,
10459 attributes->inp_domain_context,
10460 attributes->inp_tracker_domain,
10461 attributes->inp_account);
10462
10463 if (necp_debug) {
10464 NECPLOG(LOG_DEBUG, "Set on socket: Domain %s, Domain owner %s, Domain context %s, Tracker domain %s, Account %s",
10465 attributes->inp_domain,
10466 attributes->inp_domain_owner,
10467 attributes->inp_domain_context,
10468 attributes->inp_tracker_domain,
10469 attributes->inp_account);
10470 }
10471
10472 if (buffer != NULL) {
10473 kfree_data(buffer, valsize);
10474 }
10475
10476 return error;
10477 }
10478
10479 errno_t
necp_get_socket_attributes(struct inp_necp_attributes * attributes,struct sockopt * sopt)10480 necp_get_socket_attributes(struct inp_necp_attributes *attributes, struct sockopt *sopt)
10481 {
10482 int error = 0;
10483 u_int8_t *buffer = NULL;
10484 u_int8_t *cursor = NULL;
10485 size_t valsize = 0;
10486
10487 if (attributes->inp_domain != NULL) {
10488 valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_domain);
10489 }
10490 if (attributes->inp_domain_owner != NULL) {
10491 valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_domain_owner);
10492 }
10493 if (attributes->inp_domain_context != NULL) {
10494 valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_domain_context);
10495 }
10496 if (attributes->inp_tracker_domain != NULL) {
10497 valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_tracker_domain);
10498 }
10499 if (attributes->inp_account != NULL) {
10500 valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_account);
10501 }
10502 if (valsize == 0) {
10503 goto done;
10504 }
10505
10506 buffer = (u_int8_t *)kalloc_data(valsize, Z_WAITOK | Z_ZERO);
10507 if (buffer == NULL) {
10508 goto done;
10509 }
10510
10511 cursor = buffer;
10512 if (attributes->inp_domain != NULL) {
10513 cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_DOMAIN, strlen(attributes->inp_domain), attributes->inp_domain,
10514 buffer, valsize);
10515 }
10516
10517 if (attributes->inp_domain_owner != NULL) {
10518 cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_DOMAIN_OWNER, strlen(attributes->inp_domain_owner), attributes->inp_domain_owner,
10519 buffer, valsize);
10520 }
10521
10522 if (attributes->inp_domain_context != NULL) {
10523 cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_DOMAIN_CONTEXT, strlen(attributes->inp_domain_context), attributes->inp_domain_context,
10524 buffer, valsize);
10525 }
10526
10527 if (attributes->inp_tracker_domain != NULL) {
10528 cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_TRACKER_DOMAIN, strlen(attributes->inp_tracker_domain), attributes->inp_tracker_domain,
10529 buffer, valsize);
10530 }
10531
10532 if (attributes->inp_account != NULL) {
10533 cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_ACCOUNT, strlen(attributes->inp_account), attributes->inp_account,
10534 buffer, valsize);
10535 }
10536
10537 error = sooptcopyout(sopt, buffer, valsize);
10538 if (error) {
10539 goto done;
10540 }
10541 done:
10542 if (buffer != NULL) {
10543 kfree_data(buffer, valsize);
10544 }
10545
10546 return error;
10547 }
10548
10549 int
necp_set_socket_resolver_signature(struct inpcb * inp,struct sockopt * sopt)10550 necp_set_socket_resolver_signature(struct inpcb *inp, struct sockopt *sopt)
10551 {
10552 const size_t valsize = sopt->sopt_valsize;
10553 if (valsize > NECP_CLIENT_ACTION_SIGN_MAX_TOTAL_LENGTH + NECP_CLIENT_ACTION_SIGN_TAG_LENGTH) {
10554 return EINVAL;
10555 }
10556
10557 necp_lock_socket_attributes();
10558 if (inp->inp_resolver_signature != NULL) {
10559 kfree_data(inp->inp_resolver_signature, inp->inp_resolver_signature_length);
10560 }
10561 inp->inp_resolver_signature_length = 0;
10562
10563 int error = 0;
10564 if (valsize > 0) {
10565 inp->inp_resolver_signature = kalloc_data(valsize, Z_WAITOK | Z_ZERO);
10566 if ((error = sooptcopyin(sopt, inp->inp_resolver_signature, valsize,
10567 valsize)) != 0) {
10568 // Free the signature buffer if the copyin failed
10569 kfree_data(inp->inp_resolver_signature, valsize);
10570 } else {
10571 inp->inp_resolver_signature_length = valsize;
10572 }
10573 }
10574 necp_unlock_socket_attributes();
10575
10576 return error;
10577 }
10578
10579 int
necp_get_socket_resolver_signature(struct inpcb * inp,struct sockopt * sopt)10580 necp_get_socket_resolver_signature(struct inpcb *inp, struct sockopt *sopt)
10581 {
10582 int error = 0;
10583 necp_lock_socket_attributes();
10584 if (inp->inp_resolver_signature == NULL ||
10585 inp->inp_resolver_signature_length == 0) {
10586 error = ENOENT;
10587 } else {
10588 error = sooptcopyout(sopt, inp->inp_resolver_signature,
10589 inp->inp_resolver_signature_length);
10590 }
10591 necp_unlock_socket_attributes();
10592 return error;
10593 }
10594
10595 bool
necp_socket_has_resolver_signature(struct inpcb * inp)10596 necp_socket_has_resolver_signature(struct inpcb *inp)
10597 {
10598 necp_lock_socket_attributes();
10599 bool has_signature = (inp->inp_resolver_signature != NULL && inp->inp_resolver_signature_length != 0);
10600 necp_unlock_socket_attributes();
10601 return has_signature;
10602 }
10603
10604 bool
necp_socket_resolver_signature_matches_address(struct inpcb * inp,union necp_sockaddr_union * address)10605 necp_socket_resolver_signature_matches_address(struct inpcb *inp, union necp_sockaddr_union *address)
10606 {
10607 bool matches_address = false;
10608 necp_lock_socket_attributes();
10609 if (inp->inp_resolver_signature != NULL && inp->inp_resolver_signature_length > 0 && address->sa.sa_len > 0) {
10610 struct necp_client_validatable *validatable = (struct necp_client_validatable *)inp->inp_resolver_signature;
10611 if (inp->inp_resolver_signature_length > sizeof(struct necp_client_validatable) &&
10612 validatable->signable.sign_type == NECP_CLIENT_SIGN_TYPE_SYSTEM_RESOLVER_ANSWER) {
10613 size_t data_length = inp->inp_resolver_signature_length - sizeof(struct necp_client_validatable);
10614 if (data_length >= (sizeof(struct necp_client_host_resolver_answer) - sizeof(struct necp_client_signable))) {
10615 struct necp_client_host_resolver_answer *answer_struct = (struct necp_client_host_resolver_answer *)&validatable->signable;
10616 if (data_length == (sizeof(struct necp_client_host_resolver_answer) + answer_struct->hostname_length - sizeof(struct necp_client_signable)) &&
10617 answer_struct->address_answer.sa.sa_family == address->sa.sa_family &&
10618 answer_struct->address_answer.sa.sa_len == address->sa.sa_len &&
10619 (answer_struct->address_answer.sin.sin_port == 0 ||
10620 answer_struct->address_answer.sin.sin_port == address->sin.sin_port) &&
10621 ((answer_struct->address_answer.sa.sa_family == AF_INET &&
10622 answer_struct->address_answer.sin.sin_addr.s_addr == address->sin.sin_addr.s_addr) ||
10623 (answer_struct->address_answer.sa.sa_family == AF_INET6 &&
10624 memcmp(&answer_struct->address_answer.sin6.sin6_addr, &address->sin6.sin6_addr, sizeof(struct in6_addr)) == 0))) {
10625 // Address matches
10626 const bool validated = necp_validate_resolver_answer(validatable->signable.client_id,
10627 validatable->signable.sign_type,
10628 validatable->signable.signable_data, data_length,
10629 validatable->signature.signed_tag, sizeof(validatable->signature.signed_tag));
10630 if (validated) {
10631 // Answer is validated
10632 matches_address = true;
10633 }
10634 }
10635 }
10636 }
10637 }
10638 necp_unlock_socket_attributes();
10639 return matches_address;
10640 }
10641
10642 /*
10643 * necp_set_socket_domain_attributes
10644 * Called from soconnectlock/soconnectxlock to directly set the tracker domain and owner for
10645 * a newly marked tracker socket.
10646 */
10647 errno_t
necp_set_socket_domain_attributes(struct socket * so,const char * domain,const char * domain_owner)10648 necp_set_socket_domain_attributes(struct socket *so, const char *domain, const char *domain_owner)
10649 {
10650 int error = 0;
10651 struct inpcb *inp = NULL;
10652 u_int8_t *buffer = NULL;
10653 size_t valsize = 0;
10654 char *buffer_to_free = NULL;
10655
10656 if (SOCK_DOM(so) != PF_INET && SOCK_DOM(so) != PF_INET6) {
10657 error = EINVAL;
10658 goto fail;
10659 }
10660
10661 // Set domain (required)
10662
10663 valsize = strlen(domain);
10664 if (valsize == 0 || valsize > NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) {
10665 error = EINVAL;
10666 goto fail;
10667 }
10668
10669 buffer = (u_int8_t *)kalloc_data(valsize + 1, Z_WAITOK | Z_ZERO);
10670 if (buffer == NULL) {
10671 error = ENOMEM;
10672 goto fail;
10673 }
10674 bcopy(domain, buffer, valsize);
10675 buffer[valsize] = 0;
10676
10677 inp = sotoinpcb(so);
10678 // Do not overwrite a previously set domain if tracker domain is different.
10679 if (inp->inp_necp_attributes.inp_domain != NULL) {
10680 if (strlen(inp->inp_necp_attributes.inp_domain) != strlen(domain) ||
10681 strncmp(inp->inp_necp_attributes.inp_domain, domain, strlen(domain)) != 0) {
10682 buffer_to_free = inp->inp_necp_attributes.inp_tracker_domain;
10683 // Protect switching of buffer pointer
10684 necp_lock_socket_attributes();
10685 inp->inp_necp_attributes.inp_tracker_domain = (char *)buffer;
10686 necp_unlock_socket_attributes();
10687 if (buffer_to_free != NULL) {
10688 kfree_data_addr(buffer_to_free);
10689 }
10690 } else {
10691 kfree_data_addr(buffer);
10692 }
10693 } else {
10694 // Protect switching of buffer pointer
10695 necp_lock_socket_attributes();
10696 inp->inp_necp_attributes.inp_domain = (char *)buffer;
10697 necp_unlock_socket_attributes();
10698 }
10699 buffer = NULL;
10700
10701 // set domain_owner (required only for tracker)
10702 if (!(so->so_flags1 & SOF1_KNOWN_TRACKER)) {
10703 goto done;
10704 }
10705
10706 valsize = strlen(domain_owner);
10707 if (valsize == 0 || valsize > NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) {
10708 error = EINVAL;
10709 goto fail;
10710 }
10711
10712 buffer = (u_int8_t *)kalloc_data(valsize + 1, Z_WAITOK | Z_ZERO);
10713 if (buffer == NULL) {
10714 error = ENOMEM;
10715 goto fail;
10716 }
10717 bcopy(domain_owner, buffer, valsize);
10718 buffer[valsize] = 0;
10719
10720 inp = sotoinpcb(so);
10721
10722 buffer_to_free = inp->inp_necp_attributes.inp_domain_owner;
10723 // Protect switching of buffer pointer
10724 necp_lock_socket_attributes();
10725 inp->inp_necp_attributes.inp_domain_owner = (char *)buffer;
10726 necp_unlock_socket_attributes();
10727 buffer = NULL;
10728
10729 if (buffer_to_free != NULL) {
10730 kfree_data_addr(buffer_to_free);
10731 }
10732
10733 done:
10734 // Log if it is a known tracker
10735 if (so->so_flags1 & SOF1_KNOWN_TRACKER) {
10736 NECP_CLIENT_TRACKER_LOG(NECP_SOCKET_PID(so),
10737 "NECP ATTRIBUTES SOCKET - domain <%s> owner <%s> context <%s> tracker domain <%s> account <%s> "
10738 "<so flags - is_tracker %X non-app-initiated %X app-approved-domain %X",
10739 inp->inp_necp_attributes.inp_domain ? "present" : "not set",
10740 inp->inp_necp_attributes.inp_domain_owner ? "present" : "not set",
10741 inp->inp_necp_attributes.inp_domain_context ? "present" : "not set",
10742 inp->inp_necp_attributes.inp_tracker_domain ? "present" : "not set",
10743 inp->inp_necp_attributes.inp_account ? "present" : "not set",
10744 so->so_flags1 & SOF1_KNOWN_TRACKER,
10745 so->so_flags1 & SOF1_TRACKER_NON_APP_INITIATED,
10746 so->so_flags1 & SOF1_APPROVED_APP_DOMAIN);
10747 }
10748
10749 NECP_SOCKET_PARAMS_LOG(so, "NECP ATTRIBUTES SOCKET - domain <%s> owner <%s> context <%s> tracker domain <%s> account <%s> "
10750 "<so flags - is_tracker %X non-app-initiated %X app-approved-domain %X",
10751 inp->inp_necp_attributes.inp_domain,
10752 inp->inp_necp_attributes.inp_domain_owner,
10753 inp->inp_necp_attributes.inp_domain_context,
10754 inp->inp_necp_attributes.inp_tracker_domain,
10755 inp->inp_necp_attributes.inp_account,
10756 so->so_flags1 & SOF1_KNOWN_TRACKER,
10757 so->so_flags1 & SOF1_TRACKER_NON_APP_INITIATED,
10758 so->so_flags1 & SOF1_APPROVED_APP_DOMAIN);
10759
10760 if (necp_debug) {
10761 NECPLOG(LOG_DEBUG, "Set on socket: Domain <%s> Domain owner <%s> Domain context <%s> Tracker domain <%s> Account <%s> ",
10762 inp->inp_necp_attributes.inp_domain,
10763 inp->inp_necp_attributes.inp_domain_owner,
10764 inp->inp_necp_attributes.inp_domain_context,
10765 inp->inp_necp_attributes.inp_tracker_domain,
10766 inp->inp_necp_attributes.inp_account);
10767 }
10768 fail:
10769 if (buffer != NULL) {
10770 kfree_data(buffer, valsize + 1);
10771 }
10772 return error;
10773 }
10774
10775 void *
necp_create_nexus_assign_message(uuid_t nexus_instance,nexus_port_t nexus_port,void * key,uint32_t key_length,struct necp_client_endpoint * local_endpoint,struct necp_client_endpoint * remote_endpoint,struct ether_addr * local_ether_addr,u_int32_t flow_adv_index,void * flow_stats,size_t * message_length)10776 necp_create_nexus_assign_message(uuid_t nexus_instance, nexus_port_t nexus_port, void *key, uint32_t key_length,
10777 struct necp_client_endpoint *local_endpoint, struct necp_client_endpoint *remote_endpoint, struct ether_addr *local_ether_addr,
10778 u_int32_t flow_adv_index, void *flow_stats, size_t *message_length)
10779 {
10780 u_int8_t *buffer = NULL;
10781 u_int8_t *cursor = NULL;
10782 size_t valsize = 0;
10783 bool has_nexus_assignment = FALSE;
10784
10785 if (!uuid_is_null(nexus_instance)) {
10786 has_nexus_assignment = TRUE;
10787 valsize += sizeof(struct necp_tlv_header) + sizeof(uuid_t);
10788 valsize += sizeof(struct necp_tlv_header) + sizeof(nexus_port_t);
10789 }
10790 if (flow_adv_index != NECP_FLOWADV_IDX_INVALID) {
10791 valsize += sizeof(struct necp_tlv_header) + sizeof(u_int32_t);
10792 }
10793 if (key != NULL && key_length > 0) {
10794 valsize += sizeof(struct necp_tlv_header) + key_length;
10795 }
10796 if (local_endpoint != NULL) {
10797 valsize += sizeof(struct necp_tlv_header) + sizeof(struct necp_client_endpoint);
10798 }
10799 if (remote_endpoint != NULL) {
10800 valsize += sizeof(struct necp_tlv_header) + sizeof(struct necp_client_endpoint);
10801 }
10802 if (local_ether_addr != NULL) {
10803 valsize += sizeof(struct necp_tlv_header) + sizeof(struct ether_addr);
10804 }
10805 if (flow_stats != NULL) {
10806 valsize += sizeof(struct necp_tlv_header) + sizeof(void *);
10807 }
10808 if (valsize == 0) {
10809 return NULL;
10810 }
10811
10812 buffer = kalloc_data(valsize, Z_WAITOK | Z_ZERO);
10813 if (buffer == NULL) {
10814 return NULL;
10815 }
10816
10817 cursor = buffer;
10818 if (has_nexus_assignment) {
10819 cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_INSTANCE, sizeof(uuid_t), nexus_instance, buffer, valsize);
10820 cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_PORT, sizeof(nexus_port_t), &nexus_port, buffer, valsize);
10821 }
10822 if (flow_adv_index != NECP_FLOWADV_IDX_INVALID) {
10823 cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_PORT_FLOW_INDEX, sizeof(u_int32_t), &flow_adv_index, buffer, valsize);
10824 }
10825 if (key != NULL && key_length > 0) {
10826 cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_PARAMETER_NEXUS_KEY, key_length, key, buffer, valsize);
10827 }
10828 if (local_endpoint != NULL) {
10829 cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_LOCAL_ENDPOINT, sizeof(struct necp_client_endpoint), local_endpoint, buffer, valsize);
10830 }
10831 if (remote_endpoint != NULL) {
10832 cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_REMOTE_ENDPOINT, sizeof(struct necp_client_endpoint), remote_endpoint, buffer, valsize);
10833 }
10834 if (local_ether_addr != NULL) {
10835 cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_LOCAL_ETHER_ADDR, sizeof(struct ether_addr), local_ether_addr, buffer, valsize);
10836 }
10837 if (flow_stats != NULL) {
10838 cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_FLOW_STATS, sizeof(void *), &flow_stats, buffer, valsize);
10839 }
10840
10841 *message_length = valsize;
10842
10843 return buffer;
10844 }
10845
10846 void
necp_inpcb_remove_cb(struct inpcb * inp)10847 necp_inpcb_remove_cb(struct inpcb *inp)
10848 {
10849 if (!uuid_is_null(inp->necp_client_uuid)) {
10850 necp_client_unregister_socket_flow(inp->necp_client_uuid, inp);
10851 uuid_clear(inp->necp_client_uuid);
10852 }
10853 }
10854
10855 void
necp_inpcb_dispose(struct inpcb * inp)10856 necp_inpcb_dispose(struct inpcb *inp)
10857 {
10858 necp_inpcb_remove_cb(inp); // Clear out socket registrations if not yet done
10859 if (inp->inp_necp_attributes.inp_domain != NULL) {
10860 kfree_data_addr(inp->inp_necp_attributes.inp_domain);
10861 inp->inp_necp_attributes.inp_domain = NULL;
10862 }
10863 if (inp->inp_necp_attributes.inp_account != NULL) {
10864 kfree_data_addr(inp->inp_necp_attributes.inp_account);
10865 inp->inp_necp_attributes.inp_account = NULL;
10866 }
10867 if (inp->inp_necp_attributes.inp_domain_owner != NULL) {
10868 kfree_data_addr(inp->inp_necp_attributes.inp_domain_owner);
10869 inp->inp_necp_attributes.inp_domain_owner = NULL;
10870 }
10871 if (inp->inp_necp_attributes.inp_domain_context != NULL) {
10872 kfree_data_addr(inp->inp_necp_attributes.inp_domain_context);
10873 inp->inp_necp_attributes.inp_domain_context = NULL;
10874 }
10875 if (inp->inp_necp_attributes.inp_tracker_domain != NULL) {
10876 kfree_data_addr(inp->inp_necp_attributes.inp_tracker_domain);
10877 inp->inp_necp_attributes.inp_tracker_domain = NULL;
10878 }
10879 if (inp->inp_resolver_signature != NULL) {
10880 kfree_data(inp->inp_resolver_signature, inp->inp_resolver_signature_length);
10881 }
10882 inp->inp_resolver_signature_length = 0;
10883 }
10884
10885 void
necp_mppcb_dispose(struct mppcb * mpp)10886 necp_mppcb_dispose(struct mppcb *mpp)
10887 {
10888 if (!uuid_is_null(mpp->necp_client_uuid)) {
10889 necp_client_unregister_multipath_cb(mpp->necp_client_uuid, mpp);
10890 uuid_clear(mpp->necp_client_uuid);
10891 }
10892
10893 if (mpp->inp_necp_attributes.inp_domain != NULL) {
10894 kfree_data_addr(mpp->inp_necp_attributes.inp_domain);
10895 mpp->inp_necp_attributes.inp_domain = NULL;
10896 }
10897 if (mpp->inp_necp_attributes.inp_account != NULL) {
10898 kfree_data_addr(mpp->inp_necp_attributes.inp_account);
10899 mpp->inp_necp_attributes.inp_account = NULL;
10900 }
10901 if (mpp->inp_necp_attributes.inp_domain_owner != NULL) {
10902 kfree_data_addr(mpp->inp_necp_attributes.inp_domain_owner);
10903 mpp->inp_necp_attributes.inp_domain_owner = NULL;
10904 }
10905 if (mpp->inp_necp_attributes.inp_tracker_domain != NULL) {
10906 kfree_data_addr(mpp->inp_necp_attributes.inp_tracker_domain);
10907 mpp->inp_necp_attributes.inp_tracker_domain = NULL;
10908 }
10909 }
10910
10911 /// Module init
10912
10913 void
necp_client_init(void)10914 necp_client_init(void)
10915 {
10916 necp_client_update_tcall = thread_call_allocate_with_options(necp_update_all_clients_callout, NULL,
10917 THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
10918 VERIFY(necp_client_update_tcall != NULL);
10919 #if SKYWALK
10920
10921 necp_client_collect_stats_tcall = thread_call_allocate_with_options(necp_collect_stats_client_callout, NULL,
10922 THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
10923 VERIFY(necp_client_collect_stats_tcall != NULL);
10924
10925 necp_close_empty_arenas_tcall = thread_call_allocate_with_options(necp_close_empty_arenas_callout, NULL,
10926 THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
10927 VERIFY(necp_close_empty_arenas_tcall != NULL);
10928 #endif /* SKYWALK */
10929
10930 LIST_INIT(&necp_fd_list);
10931 LIST_INIT(&necp_fd_observer_list);
10932 LIST_INIT(&necp_collect_stats_flow_list);
10933
10934 RB_INIT(&necp_client_global_tree);
10935 RB_INIT(&necp_client_flow_global_tree);
10936 }
10937
10938 #if SKYWALK
10939 pid_t
necp_client_get_proc_pid_from_arena_info(struct skmem_arena_mmap_info * arena_info)10940 necp_client_get_proc_pid_from_arena_info(struct skmem_arena_mmap_info *arena_info)
10941 {
10942 ASSERT((arena_info->ami_arena->ar_type == SKMEM_ARENA_TYPE_NECP) || (arena_info->ami_arena->ar_type == SKMEM_ARENA_TYPE_SYSTEM));
10943
10944 if (arena_info->ami_arena->ar_type == SKMEM_ARENA_TYPE_NECP) {
10945 struct necp_arena_info *nai = container_of(arena_info, struct necp_arena_info, nai_mmap);
10946 return nai->nai_proc_pid;
10947 } else {
10948 struct necp_fd_data *fd_data = container_of(arena_info, struct necp_fd_data, sysctl_mmap);
10949 return fd_data->proc_pid;
10950 }
10951 }
10952 #endif /* !SKYWALK */
10953