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