xref: /xnu-11215.61.5/bsd/net/necp_client.c (revision 4f1223e81cd707a65cc109d0b8ad6653699da3c4)
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 && ifp->if_agentids != NULL) {
2585 		for (u_int32_t i = 0; i < ifp->if_agentcount; i++) {
2586 			if (uuid_is_null(ifp->if_agentids[i])) {
2587 				continue;
2588 			}
2589 			// Relies on the side effect that nexus agents that apply will create flows
2590 			(void)necp_netagent_applies_to_client(client, parsed_parameters, &ifp->if_agentids[i], TRUE,
2591 			    ifp->if_index, ifnet_get_generation(ifp));
2592 		}
2593 	}
2594 }
2595 
2596 static void
necp_client_add_browse_interface_options(struct necp_client * client,const struct necp_client_parsed_parameters * parsed_parameters,ifnet_t ifp)2597 necp_client_add_browse_interface_options(struct necp_client *client,
2598     const struct necp_client_parsed_parameters *parsed_parameters,
2599     ifnet_t ifp)
2600 {
2601 	if (ifp != NULL && ifp->if_agentids != NULL) {
2602 		for (u_int32_t i = 0; i < ifp->if_agentcount; i++) {
2603 			if (uuid_is_null(ifp->if_agentids[i])) {
2604 				continue;
2605 			}
2606 
2607 			u_int32_t flags = netagent_get_flags(ifp->if_agentids[i]);
2608 			if ((flags & NETAGENT_FLAG_REGISTERED) &&
2609 			    (flags & NETAGENT_FLAG_ACTIVE) &&
2610 			    (flags & NETAGENT_FLAG_SUPPORTS_BROWSE) &&
2611 			    (!(flags & NETAGENT_FLAG_SPECIFIC_USE_ONLY) ||
2612 			    necp_netagent_is_requested(parsed_parameters, &ifp->if_agentids[i]))) {
2613 				necp_client_add_interface_option_if_needed(client, ifp->if_index, ifnet_get_generation(ifp), &ifp->if_agentids[i], (flags & NETAGENT_FLAG_NETWORK_PROVIDER));
2614 
2615 				// Finding one is enough
2616 				break;
2617 			}
2618 		}
2619 	}
2620 }
2621 
2622 static inline bool
_necp_client_address_is_valid(struct sockaddr * address)2623 _necp_client_address_is_valid(struct sockaddr *address)
2624 {
2625 	if (address->sa_family == AF_INET) {
2626 		return address->sa_len == sizeof(struct sockaddr_in);
2627 	} else if (address->sa_family == AF_INET6) {
2628 		return address->sa_len == sizeof(struct sockaddr_in6);
2629 	} else {
2630 		return FALSE;
2631 	}
2632 }
2633 
2634 #define necp_client_address_is_valid(S) _necp_client_address_is_valid(SA(S))
2635 
2636 static inline bool
necp_client_endpoint_is_unspecified(struct necp_client_endpoint * endpoint)2637 necp_client_endpoint_is_unspecified(struct necp_client_endpoint *endpoint)
2638 {
2639 	if (necp_client_address_is_valid(&endpoint->u.sa)) {
2640 		if (endpoint->u.sa.sa_family == AF_INET) {
2641 			return endpoint->u.sin.sin_addr.s_addr == INADDR_ANY;
2642 		} else if (endpoint->u.sa.sa_family == AF_INET6) {
2643 			return IN6_IS_ADDR_UNSPECIFIED(&endpoint->u.sin6.sin6_addr);
2644 		} else {
2645 			return TRUE;
2646 		}
2647 	} else {
2648 		return TRUE;
2649 	}
2650 }
2651 
2652 #if SKYWALK
2653 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)2654 necp_client_update_local_port_parameters(u_int8_t * __sized_by(parameters_size)parameters,
2655     u_int32_t parameters_size,
2656     uint16_t local_port)
2657 {
2658 	size_t offset = 0;
2659 	while ((offset + sizeof(struct necp_tlv_header)) <= parameters_size) {
2660 		u_int8_t type = necp_buffer_get_tlv_type(parameters, parameters_size, offset);
2661 		u_int32_t length = necp_buffer_get_tlv_length(parameters, parameters_size, offset);
2662 
2663 		if (length > (parameters_size - (offset + sizeof(struct necp_tlv_header)))) {
2664 			// If the length is larger than what can fit in the remaining parameters size, bail
2665 			NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
2666 			break;
2667 		}
2668 
2669 		if (length > 0) {
2670 			u_int8_t * __indexable value = necp_buffer_get_tlv_value(parameters, parameters_size, offset, NULL);
2671 			if (value != NULL) {
2672 				switch (type) {
2673 				case NECP_CLIENT_PARAMETER_LOCAL_ADDRESS: {
2674 					if (length >= sizeof(struct necp_policy_condition_addr)) {
2675 						struct necp_policy_condition_addr *address_struct = (struct necp_policy_condition_addr *)(void *)value;
2676 						if (necp_client_address_is_valid(&address_struct->address.sa)) {
2677 							if (address_struct->address.sa.sa_family == AF_INET) {
2678 								address_struct->address.sin.sin_port = local_port;
2679 							} else if (address_struct->address.sa.sa_family == AF_INET6) {
2680 								address_struct->address.sin6.sin6_port = local_port;
2681 							}
2682 						}
2683 					}
2684 					break;
2685 				}
2686 				case NECP_CLIENT_PARAMETER_LOCAL_ENDPOINT: {
2687 					if (length >= sizeof(struct necp_client_endpoint)) {
2688 						struct necp_client_endpoint *endpoint = (struct necp_client_endpoint *)(void *)value;
2689 						if (necp_client_address_is_valid(&endpoint->u.sa)) {
2690 							if (endpoint->u.sa.sa_family == AF_INET) {
2691 								endpoint->u.sin.sin_port = local_port;
2692 							} else if (endpoint->u.sa.sa_family == AF_INET6) {
2693 								endpoint->u.sin6.sin6_port = local_port;
2694 							}
2695 						}
2696 					}
2697 					break;
2698 				}
2699 				default: {
2700 					break;
2701 				}
2702 				}
2703 			}
2704 		}
2705 
2706 		offset += sizeof(struct necp_tlv_header) + length;
2707 	}
2708 }
2709 #endif /* !SKYWALK */
2710 
2711 #define NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH 253
2712 
2713 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)2714 necp_client_trace_parameter_parsing(struct necp_client *client, u_int8_t type, u_int8_t * __sized_by(length)value, u_int32_t length)
2715 {
2716 	uint64_t num = 0;
2717 	uint16_t shortBuf;
2718 	uint32_t intBuf;
2719 	char buffer[NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH + 1];
2720 
2721 	if (value != NULL && length > 0) {
2722 		switch (length) {
2723 		case 1:
2724 			num = *value;
2725 			break;
2726 		case 2:
2727 			memcpy(&shortBuf, value, sizeof(shortBuf));
2728 			num = shortBuf;
2729 			break;
2730 		case 4:
2731 			memcpy(&intBuf, value, sizeof(intBuf));
2732 			num = intBuf;
2733 			break;
2734 		case 8:
2735 			memcpy(&num, value, sizeof(num));
2736 			break;
2737 		default:
2738 			num = 0;
2739 			break;
2740 		}
2741 		int len = NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH < length ? NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH : length;
2742 		memcpy(buffer, value, len);
2743 		buffer[len] = 0;
2744 		NECP_CLIENT_PARAMS_LOG(client, "Parsing param - type %d length %d value <%llu (%llX)> %s", type, length, num, num, buffer);
2745 	} else {
2746 		NECP_CLIENT_PARAMS_LOG(client, "Parsing param - type %d length %d", type, length);
2747 	}
2748 }
2749 
2750 static void
necp_client_trace_parsed_parameters(struct necp_client * client,struct necp_client_parsed_parameters * parsed_parameters)2751 necp_client_trace_parsed_parameters(struct necp_client *client, struct necp_client_parsed_parameters *parsed_parameters)
2752 {
2753 	int i;
2754 	char local_buffer[64] = { };
2755 	char remote_buffer[64] = { };
2756 	uuid_string_t uuid_str = { };
2757 	uuid_unparse_lower(parsed_parameters->effective_uuid, uuid_str);
2758 
2759 	switch (parsed_parameters->local_addr.sa.sa_family) {
2760 	case AF_INET:
2761 		if (parsed_parameters->local_addr.sa.sa_len == sizeof(struct sockaddr_in)) {
2762 			struct sockaddr_in *addr = &parsed_parameters->local_addr.sin;
2763 			inet_ntop(AF_INET, &(addr->sin_addr), local_buffer, sizeof(local_buffer));
2764 		}
2765 		break;
2766 	case AF_INET6:
2767 		if (parsed_parameters->local_addr.sa.sa_len == sizeof(struct sockaddr_in6)) {
2768 			struct sockaddr_in6 *addr6 = &parsed_parameters->local_addr.sin6;
2769 			inet_ntop(AF_INET6, &(addr6->sin6_addr), local_buffer, sizeof(local_buffer));
2770 		}
2771 		break;
2772 	default:
2773 		break;
2774 	}
2775 
2776 	switch (parsed_parameters->remote_addr.sa.sa_family) {
2777 	case AF_INET:
2778 		if (parsed_parameters->remote_addr.sa.sa_len == sizeof(struct sockaddr_in)) {
2779 			struct sockaddr_in *addr = &parsed_parameters->remote_addr.sin;
2780 			inet_ntop(AF_INET, &(addr->sin_addr), remote_buffer, sizeof(remote_buffer));
2781 		}
2782 		break;
2783 	case AF_INET6:
2784 		if (parsed_parameters->remote_addr.sa.sa_len == sizeof(struct sockaddr_in6)) {
2785 			struct sockaddr_in6 *addr6 = &parsed_parameters->remote_addr.sin6;
2786 			inet_ntop(AF_INET6, &(addr6->sin6_addr), remote_buffer, sizeof(remote_buffer));
2787 		}
2788 		break;
2789 	default:
2790 		break;
2791 	}
2792 
2793 	NECP_CLIENT_PARAMS_LOG(client, "Parsed params - valid_fields %X flags %X delegated_upid %llu local_addr %s remote_addr %s "
2794 	    "required_interface_index %u required_interface_type %d local_address_preference %d "
2795 	    "ip_protocol %d transport_protocol %d ethertype %d effective_pid %d effective_uuid %s uid %d persona_id %d traffic_class %d",
2796 	    parsed_parameters->valid_fields,
2797 	    parsed_parameters->flags,
2798 	    parsed_parameters->delegated_upid,
2799 	    local_buffer, remote_buffer,
2800 	    parsed_parameters->required_interface_index,
2801 	    parsed_parameters->required_interface_type,
2802 	    parsed_parameters->local_address_preference,
2803 	    parsed_parameters->ip_protocol,
2804 	    parsed_parameters->transport_protocol,
2805 	    parsed_parameters->ethertype,
2806 	    parsed_parameters->effective_pid,
2807 	    uuid_str,
2808 	    parsed_parameters->uid,
2809 	    parsed_parameters->persona_id,
2810 	    parsed_parameters->traffic_class);
2811 
2812 	NECP_CLIENT_PARAMS_LOG(client, "Parsed params - tracker flags <known-tracker %X> <non-app-initiated %X> <silent %X> <app-approved %X>",
2813 	    parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_KNOWN_TRACKER,
2814 	    parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_NON_APP_INITIATED,
2815 	    parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_SILENT,
2816 	    parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_APPROVED_APP_DOMAIN);
2817 
2818 	for (i = 0; i < NECP_MAX_INTERFACE_PARAMETERS && parsed_parameters->prohibited_interfaces[i][0]; i++) {
2819 		NECP_CLIENT_PARAMS_LOG(client, "Parsed prohibited_interfaces[%d] <%s>", i, parsed_parameters->prohibited_interfaces[i]);
2820 	}
2821 
2822 	for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && parsed_parameters->required_netagent_types[i].netagent_domain[0]; i++) {
2823 		NECP_CLIENT_PARAMS_LOG(client, "Parsed required_netagent_types[%d] <%s> <%s>", i,
2824 		    parsed_parameters->required_netagent_types[i].netagent_domain,
2825 		    parsed_parameters->required_netagent_types[i].netagent_type);
2826 	}
2827 	for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && parsed_parameters->prohibited_netagent_types[i].netagent_domain[0]; i++) {
2828 		NECP_CLIENT_PARAMS_LOG(client, "Parsed prohibited_netagent_types[%d] <%s> <%s>", i,
2829 		    parsed_parameters->prohibited_netagent_types[i].netagent_domain,
2830 		    parsed_parameters->prohibited_netagent_types[i].netagent_type);
2831 	}
2832 	for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && parsed_parameters->preferred_netagent_types[i].netagent_domain[0]; i++) {
2833 		NECP_CLIENT_PARAMS_LOG(client, "Parsed preferred_netagent_types[%d] <%s> <%s>", i,
2834 		    parsed_parameters->preferred_netagent_types[i].netagent_domain,
2835 		    parsed_parameters->preferred_netagent_types[i].netagent_type);
2836 	}
2837 	for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && parsed_parameters->avoided_netagent_types[i].netagent_domain[0]; i++) {
2838 		NECP_CLIENT_PARAMS_LOG(client, "Parsed avoided_netagent_types[%d] <%s> <%s>", i,
2839 		    parsed_parameters->avoided_netagent_types[i].netagent_domain,
2840 		    parsed_parameters->avoided_netagent_types[i].netagent_type);
2841 	}
2842 
2843 	for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && !uuid_is_null(parsed_parameters->required_netagents[i]); i++) {
2844 		uuid_unparse_lower(parsed_parameters->required_netagents[i], uuid_str);
2845 		NECP_CLIENT_PARAMS_LOG(client, "Parsed required_netagents[%d] <%s>", i, uuid_str);
2846 	}
2847 	for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && !uuid_is_null(parsed_parameters->prohibited_netagents[i]); i++) {
2848 		uuid_unparse_lower(parsed_parameters->prohibited_netagents[i], uuid_str);
2849 		NECP_CLIENT_PARAMS_LOG(client, "Parsed prohibited_netagents[%d] <%s>", i, uuid_str);
2850 	}
2851 	for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && !uuid_is_null(parsed_parameters->preferred_netagents[i]); i++) {
2852 		uuid_unparse_lower(parsed_parameters->preferred_netagents[i], uuid_str);
2853 		NECP_CLIENT_PARAMS_LOG(client, "Parsed preferred_netagents[%d] <%s>", i, uuid_str);
2854 	}
2855 	for (i = 0; i < NECP_MAX_AGENT_PARAMETERS && !uuid_is_null(parsed_parameters->avoided_netagents[i]); i++) {
2856 		uuid_unparse_lower(parsed_parameters->avoided_netagents[i], uuid_str);
2857 		NECP_CLIENT_PARAMS_LOG(client, "Parsed avoided_netagents[%d] <%s>", i, uuid_str);
2858 	}
2859 }
2860 
2861 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)2862 necp_client_strings_are_equal(const char * __sized_by(string1_length)string1, size_t string1_length,
2863     const char * __sized_by(string2_length)string2, size_t string2_length)
2864 {
2865 	if (string1 == NULL || string2 == NULL) {
2866 		return false;
2867 	}
2868 	const size_t string1_actual_length = strnlen(string1, string1_length);
2869 	const size_t string2_actual_length = strnlen(string2, string2_length);
2870 	if (string1_actual_length != string2_actual_length) {
2871 		return false;
2872 	}
2873 	return strbufcmp(string1, string1_actual_length, string2, string2_actual_length) == 0;
2874 }
2875 
2876 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)2877 necp_client_parse_parameters(struct necp_client *client, u_int8_t * __sized_by(parameters_size)parameters,
2878     u_int32_t parameters_size,
2879     struct necp_client_parsed_parameters *parsed_parameters)
2880 {
2881 	int error = 0;
2882 	size_t offset = 0;
2883 
2884 	u_int32_t num_prohibited_interfaces = 0;
2885 	u_int32_t num_prohibited_interface_types = 0;
2886 	u_int32_t num_required_agents = 0;
2887 	u_int32_t num_prohibited_agents = 0;
2888 	u_int32_t num_preferred_agents = 0;
2889 	u_int32_t num_avoided_agents = 0;
2890 	u_int32_t num_required_agent_types = 0;
2891 	u_int32_t num_prohibited_agent_types = 0;
2892 	u_int32_t num_preferred_agent_types = 0;
2893 	u_int32_t num_avoided_agent_types = 0;
2894 	u_int32_t resolver_tag_length = 0;
2895 	u_int8_t * __sized_by(resolver_tag_length) resolver_tag = NULL;
2896 	u_int32_t hostname_length = 0;
2897 	u_int8_t * __sized_by(hostname_length) client_hostname = NULL;
2898 	uuid_t parent_id = {};
2899 
2900 	if (parsed_parameters == NULL) {
2901 		return EINVAL;
2902 	}
2903 
2904 	memset(parsed_parameters, 0, sizeof(struct necp_client_parsed_parameters));
2905 
2906 	while ((offset + sizeof(struct necp_tlv_header)) <= parameters_size) {
2907 		u_int8_t type = necp_buffer_get_tlv_type(parameters, parameters_size, offset);
2908 		u_int32_t length = necp_buffer_get_tlv_length(parameters, parameters_size, offset);
2909 
2910 		if (length > (parameters_size - (offset + sizeof(struct necp_tlv_header)))) {
2911 			// If the length is larger than what can fit in the remaining parameters size, bail
2912 			NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
2913 			break;
2914 		}
2915 
2916 		if (length > 0) {
2917 			u_int8_t * __indexable value = necp_buffer_get_tlv_value(parameters, parameters_size, offset, NULL);
2918 			if (value != NULL) {
2919 				switch (type) {
2920 				case NECP_CLIENT_PARAMETER_BOUND_INTERFACE: {
2921 					if (length <= IFXNAMSIZ && length > 0) {
2922 						ifnet_t __single bound_interface = NULL;
2923 						char interface_name[IFXNAMSIZ];
2924 						memcpy(interface_name, value, length);
2925 						interface_name[length - 1] = 0;         // Make sure the string is NULL terminated
2926 						if (ifnet_find_by_name(__unsafe_null_terminated_from_indexable(interface_name, &interface_name[length - 1]), &bound_interface) == 0) {
2927 							parsed_parameters->required_interface_index = bound_interface->if_index;
2928 							parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF;
2929 							ifnet_release(bound_interface);
2930 						}
2931 					}
2932 					break;
2933 				}
2934 				case NECP_CLIENT_PARAMETER_LOCAL_ADDRESS: {
2935 					if (length >= sizeof(struct necp_policy_condition_addr)) {
2936 						struct necp_policy_condition_addr *address_struct = (struct necp_policy_condition_addr *)(void *)value;
2937 						if (necp_client_address_is_valid(&address_struct->address.sa)) {
2938 							parsed_parameters->local_addr.sin6 = address_struct->address.sin6;
2939 							if (!necp_address_is_wildcard(&parsed_parameters->local_addr)) {
2940 								parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR;
2941 							}
2942 							if ((parsed_parameters->local_addr.sa.sa_family == AF_INET && parsed_parameters->local_addr.sin.sin_port) ||
2943 							    (parsed_parameters->local_addr.sa.sa_family == AF_INET6 && parsed_parameters->local_addr.sin6.sin6_port)) {
2944 								parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_LOCAL_PORT;
2945 							}
2946 						}
2947 					}
2948 					break;
2949 				}
2950 				case NECP_CLIENT_PARAMETER_LOCAL_ENDPOINT: {
2951 					if (length >= sizeof(struct necp_client_endpoint)) {
2952 						struct necp_client_endpoint *endpoint = (struct necp_client_endpoint *)(void *)value;
2953 						if (necp_client_address_is_valid(&endpoint->u.sa)) {
2954 							parsed_parameters->local_addr.sin6 = endpoint->u.sin6;
2955 							if (!necp_address_is_wildcard(&parsed_parameters->local_addr)) {
2956 								parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR;
2957 							}
2958 							if ((parsed_parameters->local_addr.sa.sa_family == AF_INET && parsed_parameters->local_addr.sin.sin_port) ||
2959 							    (parsed_parameters->local_addr.sa.sa_family == AF_INET6 && parsed_parameters->local_addr.sin6.sin6_port)) {
2960 								parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_LOCAL_PORT;
2961 							}
2962 						}
2963 					}
2964 					break;
2965 				}
2966 				case NECP_CLIENT_PARAMETER_REMOTE_ADDRESS: {
2967 					if (length >= sizeof(struct necp_policy_condition_addr)) {
2968 						struct necp_policy_condition_addr *address_struct = (struct necp_policy_condition_addr *)(void *)value;
2969 						if (necp_client_address_is_valid(&address_struct->address.sa)) {
2970 							parsed_parameters->remote_addr.sin6 = address_struct->address.sin6;
2971 							parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_REMOTE_ADDR;
2972 						}
2973 					}
2974 					break;
2975 				}
2976 				case NECP_CLIENT_PARAMETER_REMOTE_ENDPOINT: {
2977 					if (length >= sizeof(struct necp_client_endpoint)) {
2978 						struct necp_client_endpoint *endpoint = (struct necp_client_endpoint *)(void *)value;
2979 						if (necp_client_address_is_valid(&endpoint->u.sa)) {
2980 							parsed_parameters->remote_addr.sin6 = endpoint->u.sin6;
2981 							parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_REMOTE_ADDR;
2982 						}
2983 					}
2984 					break;
2985 				}
2986 				case NECP_CLIENT_PARAMETER_PROHIBIT_INTERFACE: {
2987 					if (num_prohibited_interfaces >= NECP_MAX_INTERFACE_PARAMETERS) {
2988 						break;
2989 					}
2990 					if (length <= IFXNAMSIZ && length > 0) {
2991 						memcpy(parsed_parameters->prohibited_interfaces[num_prohibited_interfaces], value, length);
2992 						parsed_parameters->prohibited_interfaces[num_prohibited_interfaces][length - 1] = 0;         // Make sure the string is NULL terminated
2993 						num_prohibited_interfaces++;
2994 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IF;
2995 					}
2996 					break;
2997 				}
2998 				case NECP_CLIENT_PARAMETER_REQUIRE_IF_TYPE: {
2999 					if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE) {
3000 						break;
3001 					}
3002 					if (length >= sizeof(u_int8_t)) {
3003 						memcpy(&parsed_parameters->required_interface_type, value, sizeof(u_int8_t));
3004 						if (parsed_parameters->required_interface_type) {
3005 							parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE;
3006 						}
3007 					}
3008 					break;
3009 				}
3010 				case NECP_CLIENT_PARAMETER_PROHIBIT_IF_TYPE: {
3011 					if (num_prohibited_interface_types >= NECP_MAX_INTERFACE_PARAMETERS) {
3012 						break;
3013 					}
3014 					if (length >= sizeof(u_int8_t)) {
3015 						memcpy(&parsed_parameters->prohibited_interface_types[num_prohibited_interface_types], value, sizeof(u_int8_t));
3016 						num_prohibited_interface_types++;
3017 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IFTYPE;
3018 					}
3019 					break;
3020 				}
3021 				case NECP_CLIENT_PARAMETER_REQUIRE_AGENT: {
3022 					if (num_required_agents >= NECP_MAX_AGENT_PARAMETERS) {
3023 						break;
3024 					}
3025 					if (length >= sizeof(uuid_t)) {
3026 						memcpy(&parsed_parameters->required_netagents[num_required_agents], value, sizeof(uuid_t));
3027 						num_required_agents++;
3028 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT;
3029 					}
3030 					break;
3031 				}
3032 				case NECP_CLIENT_PARAMETER_PROHIBIT_AGENT: {
3033 					if (num_prohibited_agents >= NECP_MAX_AGENT_PARAMETERS) {
3034 						break;
3035 					}
3036 					if (length >= sizeof(uuid_t)) {
3037 						memcpy(&parsed_parameters->prohibited_netagents[num_prohibited_agents], value, sizeof(uuid_t));
3038 						num_prohibited_agents++;
3039 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT;
3040 					}
3041 					break;
3042 				}
3043 				case NECP_CLIENT_PARAMETER_PREFER_AGENT: {
3044 					if (num_preferred_agents >= NECP_MAX_AGENT_PARAMETERS) {
3045 						break;
3046 					}
3047 					if (length >= sizeof(uuid_t)) {
3048 						memcpy(&parsed_parameters->preferred_netagents[num_preferred_agents], value, sizeof(uuid_t));
3049 						num_preferred_agents++;
3050 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT;
3051 					}
3052 					break;
3053 				}
3054 				case NECP_CLIENT_PARAMETER_AVOID_AGENT: {
3055 					if (num_avoided_agents >= NECP_MAX_AGENT_PARAMETERS) {
3056 						break;
3057 					}
3058 					if (length >= sizeof(uuid_t)) {
3059 						memcpy(&parsed_parameters->avoided_netagents[num_avoided_agents], value, sizeof(uuid_t));
3060 						num_avoided_agents++;
3061 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT;
3062 					}
3063 					break;
3064 				}
3065 				case NECP_CLIENT_PARAMETER_REQUIRE_AGENT_TYPE: {
3066 					if (num_required_agent_types >= NECP_MAX_AGENT_PARAMETERS) {
3067 						break;
3068 					}
3069 					if (length >= sizeof(struct necp_client_parameter_netagent_type)) {
3070 						memcpy(&parsed_parameters->required_netagent_types[num_required_agent_types], value, sizeof(struct necp_client_parameter_netagent_type));
3071 						num_required_agent_types++;
3072 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE;
3073 					}
3074 					break;
3075 				}
3076 				case NECP_CLIENT_PARAMETER_PROHIBIT_AGENT_TYPE: {
3077 					if (num_prohibited_agent_types >= NECP_MAX_AGENT_PARAMETERS) {
3078 						break;
3079 					}
3080 					if (length >= sizeof(struct necp_client_parameter_netagent_type)) {
3081 						memcpy(&parsed_parameters->prohibited_netagent_types[num_prohibited_agent_types], value, sizeof(struct necp_client_parameter_netagent_type));
3082 						num_prohibited_agent_types++;
3083 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT_TYPE;
3084 					}
3085 					break;
3086 				}
3087 				case NECP_CLIENT_PARAMETER_PREFER_AGENT_TYPE: {
3088 					if (num_preferred_agent_types >= NECP_MAX_AGENT_PARAMETERS) {
3089 						break;
3090 					}
3091 					if (length >= sizeof(struct necp_client_parameter_netagent_type)) {
3092 						memcpy(&parsed_parameters->preferred_netagent_types[num_preferred_agent_types], value, sizeof(struct necp_client_parameter_netagent_type));
3093 						num_preferred_agent_types++;
3094 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE;
3095 					}
3096 					break;
3097 				}
3098 				case NECP_CLIENT_PARAMETER_AVOID_AGENT_TYPE: {
3099 					if (num_avoided_agent_types >= NECP_MAX_AGENT_PARAMETERS) {
3100 						break;
3101 					}
3102 					if (length >= sizeof(struct necp_client_parameter_netagent_type)) {
3103 						memcpy(&parsed_parameters->avoided_netagent_types[num_avoided_agent_types], value, sizeof(struct necp_client_parameter_netagent_type));
3104 						num_avoided_agent_types++;
3105 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE;
3106 					}
3107 					break;
3108 				}
3109 				case NECP_CLIENT_PARAMETER_FLAGS: {
3110 					if (length >= sizeof(u_int32_t)) {
3111 						memcpy(&parsed_parameters->flags, value, sizeof(parsed_parameters->flags));
3112 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_FLAGS;
3113 					}
3114 					break;
3115 				}
3116 				case NECP_CLIENT_PARAMETER_IP_PROTOCOL: {
3117 					if (length == sizeof(u_int16_t)) {
3118 						u_int16_t large_ip_protocol = 0;
3119 						memcpy(&large_ip_protocol, value, sizeof(large_ip_protocol));
3120 						parsed_parameters->ip_protocol = (u_int8_t)large_ip_protocol;
3121 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_IP_PROTOCOL;
3122 					} else if (length >= sizeof(parsed_parameters->ip_protocol)) {
3123 						memcpy(&parsed_parameters->ip_protocol, value, sizeof(parsed_parameters->ip_protocol));
3124 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_IP_PROTOCOL;
3125 					}
3126 					break;
3127 				}
3128 				case NECP_CLIENT_PARAMETER_TRANSPORT_PROTOCOL: {
3129 					if (length >= sizeof(parsed_parameters->transport_protocol)) {
3130 						memcpy(&parsed_parameters->transport_protocol, value, sizeof(parsed_parameters->transport_protocol));
3131 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_TRANSPORT_PROTOCOL;
3132 					}
3133 					break;
3134 				}
3135 				case NECP_CLIENT_PARAMETER_PID: {
3136 					if (length >= sizeof(parsed_parameters->effective_pid)) {
3137 						memcpy(&parsed_parameters->effective_pid, value, sizeof(parsed_parameters->effective_pid));
3138 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID;
3139 					}
3140 					break;
3141 				}
3142 				case NECP_CLIENT_PARAMETER_DELEGATED_UPID: {
3143 					if (length >= sizeof(parsed_parameters->delegated_upid)) {
3144 						memcpy(&parsed_parameters->delegated_upid, value, sizeof(parsed_parameters->delegated_upid));
3145 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_DELEGATED_UPID;
3146 					}
3147 					break;
3148 				}
3149 				case NECP_CLIENT_PARAMETER_ETHERTYPE: {
3150 					if (length >= sizeof(parsed_parameters->ethertype)) {
3151 						memcpy(&parsed_parameters->ethertype, value, sizeof(parsed_parameters->ethertype));
3152 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_ETHERTYPE;
3153 					}
3154 					break;
3155 				}
3156 				case NECP_CLIENT_PARAMETER_APPLICATION: {
3157 					if (length >= sizeof(parsed_parameters->effective_uuid)) {
3158 						memcpy(&parsed_parameters->effective_uuid, value, sizeof(parsed_parameters->effective_uuid));
3159 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_UUID;
3160 					}
3161 					break;
3162 				}
3163 				case NECP_CLIENT_PARAMETER_TRAFFIC_CLASS: {
3164 					if (length >= sizeof(parsed_parameters->traffic_class)) {
3165 						memcpy(&parsed_parameters->traffic_class, value, sizeof(parsed_parameters->traffic_class));
3166 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_TRAFFIC_CLASS;
3167 					}
3168 					break;
3169 				}
3170 				case NECP_CLIENT_PARAMETER_RESOLVER_TAG: {
3171 					if (length > 0) {
3172 						if (resolver_tag != NULL) {
3173 							// Multiple resolver tags is invalid
3174 							NECPLOG0(LOG_ERR, "Multiple resolver tags are not supported");
3175 							error = EINVAL;
3176 						} else {
3177 							resolver_tag = (u_int8_t *)value;
3178 							resolver_tag_length = length;
3179 						}
3180 					}
3181 					break;
3182 				}
3183 				case NECP_CLIENT_PARAMETER_DOMAIN: {
3184 					if (length > 0) {
3185 						client_hostname = (u_int8_t *)value;
3186 						hostname_length = length;
3187 					}
3188 					break;
3189 				}
3190 				case NECP_CLIENT_PARAMETER_PARENT_ID: {
3191 					if (length == sizeof(parent_id)) {
3192 						uuid_copy(parent_id, value);
3193 						memcpy(&parsed_parameters->parent_uuid, value, sizeof(parsed_parameters->parent_uuid));
3194 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PARENT_UUID;
3195 					}
3196 					break;
3197 				}
3198 				case NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE: {
3199 					if (length >= sizeof(parsed_parameters->local_address_preference)) {
3200 						memcpy(&parsed_parameters->local_address_preference, value, sizeof(parsed_parameters->local_address_preference));
3201 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR_PREFERENCE;
3202 					}
3203 					break;
3204 				}
3205 				case NECP_CLIENT_PARAMETER_ATTRIBUTED_BUNDLE_IDENTIFIER: {
3206 					if (length > 0) {
3207 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_ATTRIBUTED_BUNDLE_IDENTIFIER;
3208 					}
3209 					break;
3210 				}
3211 				case NECP_CLIENT_PARAMETER_FLOW_DEMUX_PATTERN: {
3212 					if (parsed_parameters->demux_pattern_count >= NECP_MAX_DEMUX_PATTERNS) {
3213 						break;
3214 					}
3215 					if (length >= sizeof(struct necp_demux_pattern)) {
3216 						memcpy(&parsed_parameters->demux_patterns[parsed_parameters->demux_pattern_count], value, sizeof(struct necp_demux_pattern));
3217 						parsed_parameters->demux_pattern_count++;
3218 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_FLOW_DEMUX_PATTERN;
3219 					}
3220 					break;
3221 				}
3222 				case NECP_CLIENT_PARAMETER_APPLICATION_ID: {
3223 					if (length >= sizeof(necp_application_id_t)) {
3224 						necp_application_id_t *application_id = (necp_application_id_t *)(void *)value;
3225 						// UID
3226 						parsed_parameters->uid = application_id->uid;
3227 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_UID;
3228 						// EUUID
3229 						uuid_copy(parsed_parameters->effective_uuid, application_id->effective_uuid);
3230 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_UUID;
3231 						// PERSONA
3232 						parsed_parameters->persona_id = application_id->persona_id;
3233 						parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_PERSONA_ID;
3234 					}
3235 					break;
3236 				}
3237 				default: {
3238 					break;
3239 				}
3240 				}
3241 			}
3242 
3243 			if (NECP_ENABLE_CLIENT_TRACE(NECP_CLIENT_TRACE_LEVEL_PARAMS)) {
3244 				necp_client_trace_parameter_parsing(client, type, value, length);
3245 			}
3246 		}
3247 
3248 		offset += sizeof(struct necp_tlv_header) + length;
3249 	}
3250 
3251 	if (resolver_tag != NULL) {
3252 		struct necp_client_validatable *validatable = (struct necp_client_validatable *)resolver_tag;
3253 		if (resolver_tag_length <= sizeof(struct necp_client_validatable)) {
3254 			error = EINVAL;
3255 			NECPLOG(LOG_ERR, "Resolver tag length too short: %u", resolver_tag_length);
3256 		} else {
3257 			bool matches = true;
3258 
3259 			// Check the client UUID for client-specific results
3260 			if (validatable->signable.sign_type == NECP_CLIENT_SIGN_TYPE_RESOLVER_ANSWER ||
3261 			    validatable->signable.sign_type == NECP_CLIENT_SIGN_TYPE_BROWSE_RESULT ||
3262 			    validatable->signable.sign_type == NECP_CLIENT_SIGN_TYPE_SERVICE_RESOLVER_ANSWER) {
3263 				if (uuid_compare(parent_id, validatable->signable.client_id) != 0 &&
3264 				    uuid_compare(client->client_id, validatable->signable.client_id) != 0) {
3265 					NECPLOG0(LOG_ERR, "Resolver tag invalid client ID");
3266 					matches = false;
3267 				}
3268 			}
3269 
3270 			size_t data_length = resolver_tag_length - sizeof(struct necp_client_validatable);
3271 			switch (validatable->signable.sign_type) {
3272 			case NECP_CLIENT_SIGN_TYPE_RESOLVER_ANSWER:
3273 			case NECP_CLIENT_SIGN_TYPE_SYSTEM_RESOLVER_ANSWER: {
3274 				if (data_length < (sizeof(struct necp_client_host_resolver_answer) - sizeof(struct necp_client_signable))) {
3275 					NECPLOG0(LOG_ERR, "Resolver tag invalid length for resolver answer");
3276 					matches = false;
3277 				} else {
3278 					struct necp_client_host_resolver_answer * __single answer_struct = (struct necp_client_host_resolver_answer *)&validatable->signable;
3279 					if (data_length != (sizeof(struct necp_client_host_resolver_answer) + answer_struct->hostname_length - sizeof(struct necp_client_signable))) {
3280 						NECPLOG0(LOG_ERR, "Resolver tag invalid length for resolver answer");
3281 						matches = false;
3282 					} else {
3283 						struct sockaddr_in6 sin6 = answer_struct->address_answer.sin6;
3284 						if (answer_struct->hostname_length != 0 && // If the hostname on the signed answer is empty, ignore
3285 						    !necp_client_strings_are_equal((const char *)client_hostname, hostname_length,
3286 						    necp_answer_get_hostname(answer_struct, answer_struct->hostname_length), answer_struct->hostname_length)) {
3287 							NECPLOG0(LOG_ERR, "Resolver tag hostname does not match");
3288 							matches = false;
3289 						} else if (answer_struct->address_answer.sa.sa_family != parsed_parameters->remote_addr.sa.sa_family ||
3290 						    answer_struct->address_answer.sa.sa_len != parsed_parameters->remote_addr.sa.sa_len) {
3291 							NECPLOG0(LOG_ERR, "Resolver tag address type does not match");
3292 							matches = false;
3293 						} else if (answer_struct->address_answer.sin.sin_port != 0 && // If the port on the signed answer is empty, ignore
3294 						    answer_struct->address_answer.sin.sin_port != parsed_parameters->remote_addr.sin.sin_port) {
3295 							NECPLOG0(LOG_ERR, "Resolver tag port does not match");
3296 							matches = false;
3297 						} else if ((answer_struct->address_answer.sa.sa_family == AF_INET &&
3298 						    answer_struct->address_answer.sin.sin_addr.s_addr != parsed_parameters->remote_addr.sin.sin_addr.s_addr) ||
3299 						    (answer_struct->address_answer.sa.sa_family == AF_INET6 &&
3300 						    memcmp(&sin6.sin6_addr, &parsed_parameters->remote_addr.sin6.sin6_addr, sizeof(struct in6_addr)) != 0)) {
3301 							NECPLOG0(LOG_ERR, "Resolver tag address does not match");
3302 							matches = false;
3303 						}
3304 					}
3305 				}
3306 				break;
3307 			}
3308 			case NECP_CLIENT_SIGN_TYPE_BROWSE_RESULT:
3309 			case NECP_CLIENT_SIGN_TYPE_SYSTEM_BROWSE_RESULT: {
3310 				if (data_length < (sizeof(struct necp_client_browse_result) - sizeof(struct necp_client_signable))) {
3311 					NECPLOG0(LOG_ERR, "Resolver tag invalid length for browse result");
3312 					matches = false;
3313 				} else {
3314 					struct necp_client_browse_result * __single answer_struct = (struct necp_client_browse_result *)&validatable->signable;
3315 					if (data_length != (sizeof(struct necp_client_browse_result) + answer_struct->service_length - sizeof(struct necp_client_signable))) {
3316 						NECPLOG0(LOG_ERR, "Resolver tag invalid length for browse result");
3317 						matches = false;
3318 					}
3319 				}
3320 				break;
3321 			}
3322 			case NECP_CLIENT_SIGN_TYPE_SERVICE_RESOLVER_ANSWER:
3323 			case NECP_CLIENT_SIGN_TYPE_SYSTEM_SERVICE_RESOLVER_ANSWER: {
3324 				if (data_length < (sizeof(struct necp_client_service_resolver_answer) - sizeof(struct necp_client_signable))) {
3325 					NECPLOG0(LOG_ERR, "Resolver tag invalid length for service resolver answer");
3326 					matches = false;
3327 				} else {
3328 					struct necp_client_service_resolver_answer * __single answer_struct = (struct necp_client_service_resolver_answer *)&validatable->signable;
3329 					if (data_length != (sizeof(struct necp_client_service_resolver_answer) + answer_struct->service_length + answer_struct->hostname_length - sizeof(struct necp_client_signable))) {
3330 						NECPLOG0(LOG_ERR, "Resolver tag invalid length for service resolver answer");
3331 						matches = false;
3332 					}
3333 				}
3334 				break;
3335 			}
3336 			default: {
3337 				NECPLOG(LOG_ERR, "Resolver tag unknown sign type: %u", validatable->signable.sign_type);
3338 				matches = false;
3339 				break;
3340 			}
3341 			}
3342 			if (!matches) {
3343 				error = EAUTH;
3344 			} else {
3345 				const bool validated = necp_validate_resolver_answer(validatable->signable.client_id,
3346 				    validatable->signable.sign_type,
3347 				    signable_get_data(&validatable->signable, data_length), data_length,
3348 				    validatable->signature.signed_tag, sizeof(validatable->signature.signed_tag));
3349 				if (!validated) {
3350 					error = EAUTH;
3351 					NECPLOG0(LOG_ERR, "Failed to validate resolve answer");
3352 				}
3353 			}
3354 		}
3355 	}
3356 
3357 	if (NECP_ENABLE_CLIENT_TRACE(NECP_CLIENT_TRACE_LEVEL_PARAMS)) {
3358 		necp_client_trace_parsed_parameters(client, parsed_parameters);
3359 	}
3360 
3361 	return error;
3362 }
3363 
3364 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)3365 necp_client_parse_result(u_int8_t * __indexable result,
3366     u_int32_t result_size,
3367     union necp_sockaddr_union *local_address,
3368     union necp_sockaddr_union *remote_address,
3369     void **flow_stats)
3370 {
3371 #pragma unused(flow_stats)
3372 	int error = 0;
3373 	size_t offset = 0;
3374 
3375 	while ((offset + sizeof(struct necp_tlv_header)) <= result_size) {
3376 		u_int8_t type = necp_buffer_get_tlv_type(result, result_size, offset);
3377 		u_int32_t length = necp_buffer_get_tlv_length(result, result_size, offset);
3378 
3379 		if (length > 0 && (offset + sizeof(struct necp_tlv_header) + length) <= result_size) {
3380 			u_int8_t * __indexable value = necp_buffer_get_tlv_value(result, result_size, offset, NULL);
3381 			if (value != NULL) {
3382 				switch (type) {
3383 				case NECP_CLIENT_RESULT_LOCAL_ENDPOINT: {
3384 					if (length >= sizeof(struct necp_client_endpoint)) {
3385 						struct necp_client_endpoint *endpoint = (struct necp_client_endpoint *)(void *)value;
3386 						if (local_address != NULL && necp_client_address_is_valid(&endpoint->u.sa)) {
3387 							local_address->sin6 = endpoint->u.sin6;
3388 						}
3389 					}
3390 					break;
3391 				}
3392 				case NECP_CLIENT_RESULT_REMOTE_ENDPOINT: {
3393 					if (length >= sizeof(struct necp_client_endpoint)) {
3394 						struct necp_client_endpoint *endpoint = (struct necp_client_endpoint *)(void *)value;
3395 						if (remote_address != NULL && necp_client_address_is_valid(&endpoint->u.sa)) {
3396 							remote_address->sin6 = endpoint->u.sin6;
3397 						}
3398 					}
3399 					break;
3400 				}
3401 #if SKYWALK
3402 				case NECP_CLIENT_RESULT_NEXUS_FLOW_STATS: {
3403 					// this TLV contains flow_stats pointer which is refcnt'ed.
3404 					if (flow_stats != NULL && length >= sizeof(struct sk_stats_flow *)) {
3405 						struct flow_stats * __single fs = *(void **)(void *)value;
3406 						// transfer the refcnt to flow_stats pointer
3407 						*flow_stats = fs;
3408 					}
3409 					memset(value, 0, length); // nullify TLV always
3410 					break;
3411 				}
3412 #endif /* SKYWALK */
3413 				default: {
3414 					break;
3415 				}
3416 				}
3417 			}
3418 		}
3419 
3420 		offset += sizeof(struct necp_tlv_header) + length;
3421 	}
3422 
3423 	return error;
3424 }
3425 
3426 static struct necp_client_flow_registration *
necp_client_create_flow_registration(struct necp_fd_data * fd_data,struct necp_client * client)3427 necp_client_create_flow_registration(struct necp_fd_data *fd_data, struct necp_client *client)
3428 {
3429 	NECP_FD_ASSERT_LOCKED(fd_data);
3430 	NECP_CLIENT_ASSERT_LOCKED(client);
3431 
3432 	struct necp_client_flow_registration *new_registration = kalloc_type(struct necp_client_flow_registration, Z_WAITOK | Z_ZERO | Z_NOFAIL);
3433 
3434 	new_registration->last_interface_details = combine_interface_details(IFSCOPE_NONE, NSTAT_IFNET_IS_UNKNOWN_TYPE);
3435 
3436 	necp_generate_client_id(new_registration->registration_id, true);
3437 	LIST_INIT(&new_registration->flow_list);
3438 
3439 	// Add registration to client list
3440 	RB_INSERT(_necp_client_flow_tree, &client->flow_registrations, new_registration);
3441 
3442 	// Add registration to fd list
3443 	RB_INSERT(_necp_fd_flow_tree, &fd_data->flows, new_registration);
3444 
3445 	// Add registration to global tree for lookup
3446 	NECP_FLOW_TREE_LOCK_EXCLUSIVE();
3447 	RB_INSERT(_necp_client_flow_global_tree, &necp_client_flow_global_tree, new_registration);
3448 	NECP_FLOW_TREE_UNLOCK();
3449 
3450 	new_registration->client = client;
3451 
3452 #if SKYWALK
3453 	{
3454 		// The uuid caching here is something of a hack, but saves a dynamic lookup with attendant lock hierarchy issues
3455 		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;
3456 		uuid_copy(client->latest_flow_registration_id, new_registration->registration_id);
3457 
3458 		// With the flow uuid known, push a new statistics update to ensure the uuid gets known by any clients before the flow can close
3459 		if (client->nstat_context != NULL) {
3460 			nstat_provider_stats_event(client->nstat_context, stats_event_type);
3461 		}
3462 	}
3463 #endif /* !SKYWALK */
3464 
3465 	// Start out assuming there is nothing to read from the flow
3466 	new_registration->flow_result_read = true;
3467 
3468 	return new_registration;
3469 }
3470 
3471 static void
necp_client_add_socket_flow(struct necp_client_flow_registration * flow_registration,struct inpcb * inp)3472 necp_client_add_socket_flow(struct necp_client_flow_registration *flow_registration,
3473     struct inpcb *inp)
3474 {
3475 	struct necp_client_flow *new_flow = kalloc_type(struct necp_client_flow, Z_WAITOK | Z_ZERO | Z_NOFAIL);
3476 
3477 	new_flow->socket = TRUE;
3478 	new_flow->u.socket_handle = inp;
3479 	new_flow->u.cb = inp->necp_cb;
3480 
3481 	OSIncrementAtomic(&necp_socket_flow_count);
3482 
3483 	LIST_INSERT_HEAD(&flow_registration->flow_list, new_flow, flow_chain);
3484 }
3485 
3486 static int
necp_client_register_socket_inner(pid_t pid,uuid_t client_id,struct inpcb * inp,bool is_listener)3487 necp_client_register_socket_inner(pid_t pid, uuid_t client_id, struct inpcb *inp, bool is_listener)
3488 {
3489 	int error = 0;
3490 	struct necp_fd_data *client_fd = NULL;
3491 	bool found_client = FALSE;
3492 
3493 	NECP_FD_LIST_LOCK_SHARED();
3494 	LIST_FOREACH(client_fd, &necp_fd_list, chain) {
3495 		NECP_FD_LOCK(client_fd);
3496 		struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
3497 		if (client != NULL) {
3498 			if (!pid || client->proc_pid == pid) {
3499 				if (is_listener) {
3500 					found_client = TRUE;
3501 #if SKYWALK
3502 					// Check netns token for registration
3503 					if (!NETNS_TOKEN_VALID(&client->port_reservation)) {
3504 						error = EINVAL;
3505 					}
3506 #endif /* !SKYWALK */
3507 				} else {
3508 					// Find client flow and assign from socket
3509 					struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
3510 					if (flow_registration != NULL) {
3511 						// Found the right client and flow registration, add a new flow
3512 						found_client = TRUE;
3513 						necp_client_add_socket_flow(flow_registration, inp);
3514 					} else if (RB_EMPTY(&client->flow_registrations) && !necp_client_id_is_flow(client_id)) {
3515 						// No flows yet on this client, add a new registration
3516 						flow_registration = necp_client_create_flow_registration(client_fd, client);
3517 						if (flow_registration == NULL) {
3518 							error = ENOMEM;
3519 						} else {
3520 							// Add a new flow
3521 							found_client = TRUE;
3522 							necp_client_add_socket_flow(flow_registration, inp);
3523 						}
3524 					}
3525 				}
3526 			}
3527 
3528 			NECP_CLIENT_UNLOCK(client);
3529 		}
3530 		NECP_FD_UNLOCK(client_fd);
3531 
3532 		if (found_client) {
3533 			break;
3534 		}
3535 	}
3536 	NECP_FD_LIST_UNLOCK();
3537 
3538 	if (!found_client) {
3539 		error = ENOENT;
3540 	} else {
3541 		// Count the sockets that have the NECP client UUID set
3542 		struct socket *so = inp->inp_socket;
3543 		if (!(so->so_flags1 & SOF1_HAS_NECP_CLIENT_UUID)) {
3544 			so->so_flags1 |= SOF1_HAS_NECP_CLIENT_UUID;
3545 			INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_necp_clientuuid_total);
3546 		}
3547 	}
3548 
3549 	return error;
3550 }
3551 
3552 int
necp_client_register_socket_flow(pid_t pid,uuid_t client_id,struct inpcb * inp)3553 necp_client_register_socket_flow(pid_t pid, uuid_t client_id, struct inpcb *inp)
3554 {
3555 	return necp_client_register_socket_inner(pid, client_id, inp, false);
3556 }
3557 
3558 int
necp_client_register_socket_listener(pid_t pid,uuid_t client_id,struct inpcb * inp)3559 necp_client_register_socket_listener(pid_t pid, uuid_t client_id, struct inpcb *inp)
3560 {
3561 	return necp_client_register_socket_inner(pid, client_id, inp, true);
3562 }
3563 
3564 #if SKYWALK
3565 int
necp_client_get_netns_flow_info(uuid_t client_id,struct ns_flow_info * flow_info)3566 necp_client_get_netns_flow_info(uuid_t client_id, struct ns_flow_info *flow_info)
3567 {
3568 	int error = 0;
3569 	struct necp_fd_data *client_fd = NULL;
3570 	bool found_client = FALSE;
3571 
3572 	NECP_FD_LIST_LOCK_SHARED();
3573 	LIST_FOREACH(client_fd, &necp_fd_list, chain) {
3574 		NECP_FD_LOCK(client_fd);
3575 		struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
3576 		if (client != NULL) {
3577 			found_client = TRUE;
3578 			if (!NETNS_TOKEN_VALID(&client->port_reservation)) {
3579 				error = EINVAL;
3580 			} else {
3581 				error = netns_get_flow_info(&client->port_reservation, flow_info);
3582 			}
3583 
3584 			NECP_CLIENT_UNLOCK(client);
3585 		}
3586 		NECP_FD_UNLOCK(client_fd);
3587 
3588 		if (found_client) {
3589 			break;
3590 		}
3591 	}
3592 	NECP_FD_LIST_UNLOCK();
3593 
3594 	if (!found_client) {
3595 		error = ENOENT;
3596 	}
3597 
3598 	return error;
3599 }
3600 #endif /* !SKYWALK */
3601 
3602 static void
necp_client_add_multipath_interface_flows(struct necp_client_flow_registration * flow_registration,struct necp_client * client,struct mppcb * mpp)3603 necp_client_add_multipath_interface_flows(struct necp_client_flow_registration *flow_registration,
3604     struct necp_client *client,
3605     struct mppcb *mpp)
3606 {
3607 	flow_registration->interface_handle = mpp;
3608 	flow_registration->interface_cb = mpp->necp_cb;
3609 
3610 	proc_t proc = proc_find(client->proc_pid);
3611 	if (proc == PROC_NULL) {
3612 		return;
3613 	}
3614 
3615 	// Traverse all interfaces and add a tracking flow if needed
3616 	necp_flow_add_interface_flows(proc, client, flow_registration, true);
3617 
3618 	proc_rele(proc);
3619 	proc = PROC_NULL;
3620 }
3621 
3622 int
necp_client_register_multipath_cb(pid_t pid,uuid_t client_id,struct mppcb * mpp)3623 necp_client_register_multipath_cb(pid_t pid, uuid_t client_id, struct mppcb *mpp)
3624 {
3625 	int error = 0;
3626 	struct necp_fd_data *client_fd = NULL;
3627 	bool found_client = FALSE;
3628 
3629 	NECP_FD_LIST_LOCK_SHARED();
3630 	LIST_FOREACH(client_fd, &necp_fd_list, chain) {
3631 		NECP_FD_LOCK(client_fd);
3632 		struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
3633 		if (client != NULL) {
3634 			if (!pid || client->proc_pid == pid) {
3635 				struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
3636 				if (flow_registration != NULL) {
3637 					// Found the right client and flow registration, add a new flow
3638 					found_client = TRUE;
3639 					necp_client_add_multipath_interface_flows(flow_registration, client, mpp);
3640 				} else if (RB_EMPTY(&client->flow_registrations) && !necp_client_id_is_flow(client_id)) {
3641 					// No flows yet on this client, add a new registration
3642 					flow_registration = necp_client_create_flow_registration(client_fd, client);
3643 					if (flow_registration == NULL) {
3644 						error = ENOMEM;
3645 					} else {
3646 						// Add a new flow
3647 						found_client = TRUE;
3648 						necp_client_add_multipath_interface_flows(flow_registration, client, mpp);
3649 					}
3650 				}
3651 			}
3652 
3653 			NECP_CLIENT_UNLOCK(client);
3654 		}
3655 		NECP_FD_UNLOCK(client_fd);
3656 
3657 		if (found_client) {
3658 			break;
3659 		}
3660 	}
3661 	NECP_FD_LIST_UNLOCK();
3662 
3663 	if (!found_client && error == 0) {
3664 		error = ENOENT;
3665 	}
3666 
3667 	return error;
3668 }
3669 
3670 #define NETAGENT_DOMAIN_RADIO_MANAGER   "WirelessRadioManager"
3671 #define NETAGENT_TYPE_RADIO_MANAGER     "WirelessRadioManager:BB Manager"
3672 
3673 static int
necp_client_lookup_bb_radio_manager(struct necp_client * client,uuid_t netagent_uuid)3674 necp_client_lookup_bb_radio_manager(struct necp_client *client,
3675     uuid_t netagent_uuid)
3676 {
3677 	char netagent_domain[NETAGENT_DOMAINSIZE];
3678 	char netagent_type[NETAGENT_TYPESIZE];
3679 	struct necp_aggregate_result result;
3680 	proc_t proc;
3681 	int error;
3682 
3683 	proc = proc_find(client->proc_pid);
3684 	if (proc == PROC_NULL) {
3685 		return ESRCH;
3686 	}
3687 
3688 	error = necp_application_find_policy_match_internal(proc, client->parameters, (u_int32_t)client->parameters_length,
3689 	    &result, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, true, true, NULL);
3690 
3691 	proc_rele(proc);
3692 	proc = PROC_NULL;
3693 
3694 	if (error) {
3695 		return error;
3696 	}
3697 
3698 	for (int i = 0; i < NECP_MAX_NETAGENTS; i++) {
3699 		if (uuid_is_null(result.netagents[i])) {
3700 			// Passed end of valid agents
3701 			break;
3702 		}
3703 
3704 		memset(&netagent_domain, 0, NETAGENT_DOMAINSIZE);
3705 		memset(&netagent_type, 0, NETAGENT_TYPESIZE);
3706 		if (netagent_get_agent_domain_and_type(result.netagents[i], netagent_domain, netagent_type) == FALSE) {
3707 			continue;
3708 		}
3709 
3710 		if (strlcmp(netagent_domain, NETAGENT_DOMAIN_RADIO_MANAGER, NETAGENT_DOMAINSIZE) != 0) {
3711 			continue;
3712 		}
3713 
3714 		if (strlcmp(netagent_type, NETAGENT_TYPE_RADIO_MANAGER, NETAGENT_TYPESIZE) != 0) {
3715 			continue;
3716 		}
3717 
3718 		uuid_copy(netagent_uuid, result.netagents[i]);
3719 
3720 		break;
3721 	}
3722 
3723 	return 0;
3724 }
3725 
3726 static int
necp_client_assert_bb_radio_manager_common(struct necp_client * client,bool assert)3727 necp_client_assert_bb_radio_manager_common(struct necp_client *client, bool assert)
3728 {
3729 	uuid_t netagent_uuid;
3730 	uint8_t assert_type;
3731 	int error;
3732 
3733 	error = necp_client_lookup_bb_radio_manager(client, netagent_uuid);
3734 	if (error) {
3735 		NECPLOG0(LOG_ERR, "BB radio manager agent not found");
3736 		return error;
3737 	}
3738 
3739 	// Before unasserting, verify that the assertion was already taken
3740 	if (assert == FALSE) {
3741 		assert_type = NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT;
3742 
3743 		if (!necp_client_remove_assertion(client, netagent_uuid)) {
3744 			return EINVAL;
3745 		}
3746 	} else {
3747 		assert_type = NETAGENT_MESSAGE_TYPE_CLIENT_ASSERT;
3748 	}
3749 
3750 	error = netagent_client_message(netagent_uuid, client->client_id, client->proc_pid, client->agent_handle, assert_type);
3751 	if (error) {
3752 		NECPLOG0(LOG_ERR, "netagent_client_message failed");
3753 		return error;
3754 	}
3755 
3756 	// Only save the assertion if the action succeeded
3757 	if (assert == TRUE) {
3758 		necp_client_add_assertion(client, netagent_uuid);
3759 	}
3760 
3761 	return 0;
3762 }
3763 
3764 int
necp_client_assert_bb_radio_manager(uuid_t client_id,bool assert)3765 necp_client_assert_bb_radio_manager(uuid_t client_id, bool assert)
3766 {
3767 	struct necp_client *client;
3768 	int error = 0;
3769 
3770 	NECP_CLIENT_TREE_LOCK_SHARED();
3771 
3772 	client = necp_find_client_and_lock(client_id);
3773 
3774 	if (client) {
3775 		// Found the right client!
3776 		error = necp_client_assert_bb_radio_manager_common(client, assert);
3777 
3778 		NECP_CLIENT_UNLOCK(client);
3779 	} else {
3780 		NECPLOG0(LOG_ERR, "Couldn't find client");
3781 		error = ENOENT;
3782 	}
3783 
3784 	NECP_CLIENT_TREE_UNLOCK();
3785 
3786 	return error;
3787 }
3788 
3789 static int
necp_client_unregister_socket_flow(uuid_t client_id,void * handle)3790 necp_client_unregister_socket_flow(uuid_t client_id, void *handle)
3791 {
3792 	int error = 0;
3793 	struct necp_fd_data *client_fd = NULL;
3794 	bool found_client = FALSE;
3795 	bool client_updated = FALSE;
3796 
3797 	NECP_FD_LIST_LOCK_SHARED();
3798 	LIST_FOREACH(client_fd, &necp_fd_list, chain) {
3799 		NECP_FD_LOCK(client_fd);
3800 
3801 		struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
3802 		if (client != NULL) {
3803 			struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
3804 			if (flow_registration != NULL) {
3805 				// Found the right client and flow!
3806 				found_client = TRUE;
3807 
3808 				// Remove flow assignment
3809 				struct necp_client_flow * __single search_flow = NULL;
3810 				struct necp_client_flow *temp_flow = NULL;
3811 				LIST_FOREACH_SAFE(search_flow, &flow_registration->flow_list, flow_chain, temp_flow) {
3812 					if (search_flow->socket && search_flow->u.socket_handle == handle) {
3813 						if (search_flow->assigned_results != NULL) {
3814 							kfree_data_counted_by(search_flow->assigned_results, search_flow->assigned_results_length);
3815 						}
3816 						client_updated = TRUE;
3817 						flow_registration->flow_result_read = FALSE;
3818 						LIST_REMOVE(search_flow, flow_chain);
3819 						OSDecrementAtomic(&necp_socket_flow_count);
3820 						kfree_type(struct necp_client_flow, search_flow);
3821 					}
3822 				}
3823 			}
3824 
3825 			NECP_CLIENT_UNLOCK(client);
3826 		}
3827 
3828 		if (client_updated) {
3829 			necp_fd_notify(client_fd, true);
3830 		}
3831 		NECP_FD_UNLOCK(client_fd);
3832 
3833 		if (found_client) {
3834 			break;
3835 		}
3836 	}
3837 	NECP_FD_LIST_UNLOCK();
3838 
3839 	if (!found_client) {
3840 		error = ENOENT;
3841 	}
3842 
3843 	return error;
3844 }
3845 
3846 static int
necp_client_unregister_multipath_cb(uuid_t client_id,void * handle)3847 necp_client_unregister_multipath_cb(uuid_t client_id, void *handle)
3848 {
3849 	int error = 0;
3850 	bool found_client = FALSE;
3851 
3852 	NECP_CLIENT_TREE_LOCK_SHARED();
3853 
3854 	struct necp_client *client = necp_find_client_and_lock(client_id);
3855 	if (client != NULL) {
3856 		struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
3857 		if (flow_registration != NULL) {
3858 			// Found the right client and flow!
3859 			found_client = TRUE;
3860 
3861 			// Remove flow assignment
3862 			struct necp_client_flow *search_flow = NULL;
3863 			struct necp_client_flow *temp_flow = NULL;
3864 			LIST_FOREACH_SAFE(search_flow, &flow_registration->flow_list, flow_chain, temp_flow) {
3865 				if (!search_flow->socket && !search_flow->nexus &&
3866 				    search_flow->u.socket_handle == handle) {
3867 					search_flow->u.socket_handle = NULL;
3868 					search_flow->u.cb = NULL;
3869 				}
3870 			}
3871 
3872 			flow_registration->interface_handle = NULL;
3873 			flow_registration->interface_cb = NULL;
3874 		}
3875 
3876 		NECP_CLIENT_UNLOCK(client);
3877 	}
3878 
3879 	NECP_CLIENT_TREE_UNLOCK();
3880 
3881 	if (!found_client) {
3882 		error = ENOENT;
3883 	}
3884 
3885 	return error;
3886 }
3887 
3888 int
necp_client_assign_from_socket(pid_t pid,uuid_t client_id,struct inpcb * inp)3889 necp_client_assign_from_socket(pid_t pid, uuid_t client_id, struct inpcb *inp)
3890 {
3891 	int error = 0;
3892 	struct necp_fd_data *client_fd = NULL;
3893 	bool found_client = FALSE;
3894 	bool client_updated = FALSE;
3895 
3896 	NECP_FD_LIST_LOCK_SHARED();
3897 	LIST_FOREACH(client_fd, &necp_fd_list, chain) {
3898 		if (pid && client_fd->proc_pid != pid) {
3899 			continue;
3900 		}
3901 
3902 		proc_t proc = proc_find(client_fd->proc_pid);
3903 		if (proc == PROC_NULL) {
3904 			continue;
3905 		}
3906 
3907 		NECP_FD_LOCK(client_fd);
3908 
3909 		struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
3910 		if (client != NULL) {
3911 			struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
3912 			if (flow_registration == NULL && RB_EMPTY(&client->flow_registrations) && !necp_client_id_is_flow(client_id)) {
3913 				// No flows yet on this client, add a new registration
3914 				flow_registration = necp_client_create_flow_registration(client_fd, client);
3915 				if (flow_registration == NULL) {
3916 					error = ENOMEM;
3917 				}
3918 			}
3919 			if (flow_registration != NULL) {
3920 				// Found the right client and flow!
3921 				found_client = TRUE;
3922 
3923 				struct necp_client_flow *flow = NULL;
3924 				LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
3925 					if (flow->socket && flow->u.socket_handle == inp) {
3926 						// Release prior results and route
3927 						if (flow->assigned_results != NULL) {
3928 							kfree_data_counted_by(flow->assigned_results, flow->assigned_results_length);
3929 						}
3930 
3931 						ifnet_t ifp = NULL;
3932 						if ((inp->inp_flags & INP_BOUND_IF) && inp->inp_boundifp) {
3933 							ifp = inp->inp_boundifp;
3934 						} else {
3935 							ifp = inp->inp_last_outifp;
3936 						}
3937 
3938 						if (ifp != NULL) {
3939 							flow->interface_index = ifp->if_index;
3940 						} else {
3941 							flow->interface_index = IFSCOPE_NONE;
3942 						}
3943 
3944 						if (inp->inp_vflag & INP_IPV4) {
3945 							flow->local_addr.sin.sin_family = AF_INET;
3946 							flow->local_addr.sin.sin_len = sizeof(struct sockaddr_in);
3947 							flow->local_addr.sin.sin_port = inp->inp_lport;
3948 							memcpy(&flow->local_addr.sin.sin_addr, &inp->inp_laddr, sizeof(struct in_addr));
3949 
3950 							flow->remote_addr.sin.sin_family = AF_INET;
3951 							flow->remote_addr.sin.sin_len = sizeof(struct sockaddr_in);
3952 							flow->remote_addr.sin.sin_port = inp->inp_fport;
3953 							memcpy(&flow->remote_addr.sin.sin_addr, &inp->inp_faddr, sizeof(struct in_addr));
3954 						} else if (inp->inp_vflag & INP_IPV6) {
3955 							in6_ip6_to_sockaddr(&inp->in6p_laddr, inp->inp_lport, inp->inp_lifscope, &flow->local_addr.sin6, sizeof(flow->local_addr));
3956 							in6_ip6_to_sockaddr(&inp->in6p_faddr, inp->inp_fport, inp->inp_fifscope, &flow->remote_addr.sin6, sizeof(flow->remote_addr));
3957 						}
3958 
3959 						flow->viable = necp_client_flow_is_viable(proc, client, flow);
3960 
3961 						uuid_t empty_uuid;
3962 						uuid_clear(empty_uuid);
3963 						flow->assigned = TRUE;
3964 
3965 						size_t message_length;
3966 						void *message = necp_create_nexus_assign_message(empty_uuid, 0, NULL, 0,
3967 						    (struct necp_client_endpoint *)&flow->local_addr,
3968 						    (struct necp_client_endpoint *)&flow->remote_addr,
3969 						    NULL, 0, NULL, &message_length);
3970 						flow->assigned_results = message;
3971 						flow->assigned_results_length = message_length;
3972 						flow_registration->flow_result_read = FALSE;
3973 						client_updated = TRUE;
3974 						break;
3975 					}
3976 				}
3977 			}
3978 
3979 			NECP_CLIENT_UNLOCK(client);
3980 		}
3981 		if (client_updated) {
3982 			necp_fd_notify(client_fd, true);
3983 		}
3984 		NECP_FD_UNLOCK(client_fd);
3985 
3986 		proc_rele(proc);
3987 		proc = PROC_NULL;
3988 
3989 		if (found_client) {
3990 			break;
3991 		}
3992 	}
3993 	NECP_FD_LIST_UNLOCK();
3994 
3995 	if (error == 0) {
3996 		if (!found_client) {
3997 			error = ENOENT;
3998 		} else if (!client_updated) {
3999 			error = EINVAL;
4000 		}
4001 	}
4002 
4003 	return error;
4004 }
4005 
4006 bool
necp_socket_is_allowed_to_recv_on_interface(struct inpcb * inp,ifnet_t interface)4007 necp_socket_is_allowed_to_recv_on_interface(struct inpcb *inp, ifnet_t interface)
4008 {
4009 	if (interface == NULL ||
4010 	    inp == NULL ||
4011 	    !(inp->inp_flags2 & INP2_EXTERNAL_PORT) ||
4012 	    uuid_is_null(inp->necp_client_uuid)) {
4013 		// If there's no interface or client ID to check,
4014 		// or if this is not a listener, pass.
4015 		// Outbound connections will have already been
4016 		// validated for policy.
4017 		return TRUE;
4018 	}
4019 
4020 	// Only filter out listener sockets (no remote address specified)
4021 	if ((inp->inp_vflag & INP_IPV4) &&
4022 	    inp->inp_faddr.s_addr != INADDR_ANY) {
4023 		return TRUE;
4024 	}
4025 	if ((inp->inp_vflag & INP_IPV6) &&
4026 	    !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
4027 		return TRUE;
4028 	}
4029 
4030 	bool allowed = TRUE;
4031 
4032 	NECP_CLIENT_TREE_LOCK_SHARED();
4033 
4034 	struct necp_client *client = necp_find_client_and_lock(inp->necp_client_uuid);
4035 	if (client != NULL) {
4036 		struct necp_client_parsed_parameters * __single parsed_parameters = NULL;
4037 
4038 		parsed_parameters = kalloc_type(struct necp_client_parsed_parameters,
4039 		    Z_WAITOK | Z_ZERO | Z_NOFAIL);
4040 		int error = necp_client_parse_parameters(client, client->parameters, (u_int32_t)client->parameters_length, parsed_parameters);
4041 		if (error == 0) {
4042 			if (!necp_ifnet_matches_parameters(interface, parsed_parameters, 0, NULL, true, false)) {
4043 				allowed = FALSE;
4044 			}
4045 		}
4046 		kfree_type(struct necp_client_parsed_parameters, parsed_parameters);
4047 
4048 		NECP_CLIENT_UNLOCK(client);
4049 	}
4050 
4051 	NECP_CLIENT_TREE_UNLOCK();
4052 
4053 	return allowed;
4054 }
4055 
4056 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)4057 necp_update_flow_protoctl_event(uuid_t netagent_uuid, uuid_t client_id,
4058     uint32_t protoctl_event_code, uint32_t protoctl_event_val,
4059     uint32_t protoctl_event_tcp_seq_number)
4060 {
4061 	int error = 0;
4062 	struct necp_fd_data *client_fd = NULL;
4063 	bool found_client = FALSE;
4064 	bool client_updated = FALSE;
4065 
4066 	NECP_FD_LIST_LOCK_SHARED();
4067 	LIST_FOREACH(client_fd, &necp_fd_list, chain) {
4068 		proc_t proc = proc_find(client_fd->proc_pid);
4069 		if (proc == PROC_NULL) {
4070 			continue;
4071 		}
4072 
4073 		NECP_FD_LOCK(client_fd);
4074 
4075 		struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
4076 		if (client != NULL) {
4077 			struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
4078 			if (flow_registration != NULL) {
4079 				// Found the right client and flow!
4080 				found_client = TRUE;
4081 
4082 				struct necp_client_flow *flow = NULL;
4083 				LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
4084 					// Verify that the client nexus agent matches
4085 					if ((flow->nexus && uuid_compare(flow->u.nexus_agent, netagent_uuid) == 0) ||
4086 					    flow->socket) {
4087 						flow->has_protoctl_event = TRUE;
4088 						flow->protoctl_event.protoctl_event_code = protoctl_event_code;
4089 						flow->protoctl_event.protoctl_event_val = protoctl_event_val;
4090 						flow->protoctl_event.protoctl_event_tcp_seq_num = protoctl_event_tcp_seq_number;
4091 						flow_registration->flow_result_read = FALSE;
4092 						client_updated = TRUE;
4093 						break;
4094 					}
4095 				}
4096 			}
4097 
4098 			NECP_CLIENT_UNLOCK(client);
4099 		}
4100 
4101 		if (client_updated) {
4102 			necp_fd_notify(client_fd, true);
4103 		}
4104 
4105 		NECP_FD_UNLOCK(client_fd);
4106 		proc_rele(proc);
4107 		proc = PROC_NULL;
4108 
4109 		if (found_client) {
4110 			break;
4111 		}
4112 	}
4113 	NECP_FD_LIST_UNLOCK();
4114 
4115 	if (!found_client) {
4116 		error = ENOENT;
4117 	} else if (!client_updated) {
4118 		error = EINVAL;
4119 	}
4120 	return error;
4121 }
4122 
4123 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)4124 necp_assign_client_result_locked(struct proc *proc,
4125     struct necp_fd_data *client_fd,
4126     struct necp_client *client,
4127     struct necp_client_flow_registration *flow_registration,
4128     uuid_t netagent_uuid,
4129     u_int8_t * __indexable assigned_results,
4130     size_t assigned_results_length,
4131     bool notify_fd,
4132     bool assigned_from_userspace_agent)
4133 {
4134 	bool client_updated = FALSE;
4135 
4136 	NECP_FD_ASSERT_LOCKED(client_fd);
4137 	NECP_CLIENT_ASSERT_LOCKED(client);
4138 
4139 	struct necp_client_flow *flow = NULL;
4140 	LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
4141 		// Verify that the client nexus agent matches
4142 		if (flow->nexus &&
4143 		    uuid_compare(flow->u.nexus_agent, netagent_uuid) == 0) {
4144 			// Release prior results and route
4145 			if (flow->assigned_results != NULL) {
4146 				kfree_data_counted_by(flow->assigned_results, flow->assigned_results_length);
4147 			}
4148 
4149 			void * __single nexus_stats = NULL;
4150 			if (assigned_results != NULL && assigned_results_length > 0) {
4151 				int error = necp_client_parse_result(assigned_results, (u_int32_t)assigned_results_length,
4152 				    &flow->local_addr, &flow->remote_addr,
4153 				    assigned_from_userspace_agent ? NULL : &nexus_stats); // Only assign stats from kernel agents
4154 				VERIFY(error == 0);
4155 			}
4156 
4157 			flow->viable = necp_client_flow_is_viable(proc, client, flow);
4158 
4159 			flow->assigned = TRUE;
4160 			flow->assigned_results = assigned_results;
4161 			flow->assigned_results_length = assigned_results_length;
4162 			flow_registration->flow_result_read = FALSE;
4163 #if SKYWALK
4164 			if (nexus_stats != NULL) {
4165 				if (flow_registration->nexus_stats != NULL) {
4166 					flow_stats_release(flow_registration->nexus_stats);
4167 				}
4168 				flow_registration->nexus_stats = nexus_stats;
4169 			}
4170 #endif /* SKYWALK */
4171 			client_updated = TRUE;
4172 			break;
4173 		}
4174 	}
4175 
4176 	if (client_updated && notify_fd) {
4177 		necp_fd_notify(client_fd, true);
4178 	}
4179 
4180 	// if not updated, client must free assigned_results
4181 	return client_updated;
4182 }
4183 
4184 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)4185 necp_assign_client_result(uuid_t netagent_uuid, uuid_t client_id,
4186     u_int8_t * __sized_by(assigned_results_length)assigned_results, size_t assigned_results_length)
4187 {
4188 	int error = 0;
4189 	struct necp_fd_data *client_fd = NULL;
4190 	bool found_client = FALSE;
4191 	bool client_updated = FALSE;
4192 
4193 	NECP_FD_LIST_LOCK_SHARED();
4194 
4195 	LIST_FOREACH(client_fd, &necp_fd_list, chain) {
4196 		proc_t proc = proc_find(client_fd->proc_pid);
4197 		if (proc == PROC_NULL) {
4198 			continue;
4199 		}
4200 
4201 		NECP_FD_LOCK(client_fd);
4202 		struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
4203 		if (client != NULL) {
4204 			struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
4205 			if (flow_registration != NULL) {
4206 				// Found the right client and flow!
4207 				found_client = TRUE;
4208 				if (necp_assign_client_result_locked(proc, client_fd, client, flow_registration, netagent_uuid,
4209 				    assigned_results, assigned_results_length, true, true)) {
4210 					client_updated = TRUE;
4211 				}
4212 			}
4213 
4214 			NECP_CLIENT_UNLOCK(client);
4215 		}
4216 		NECP_FD_UNLOCK(client_fd);
4217 
4218 		proc_rele(proc);
4219 		proc = PROC_NULL;
4220 
4221 		if (found_client) {
4222 			break;
4223 		}
4224 	}
4225 
4226 	NECP_FD_LIST_UNLOCK();
4227 
4228 	// upon error, client must free assigned_results
4229 	if (!found_client) {
4230 		error = ENOENT;
4231 	} else if (!client_updated) {
4232 		error = EINVAL;
4233 	}
4234 
4235 	return error;
4236 }
4237 
4238 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)4239 necp_assign_client_group_members(uuid_t netagent_uuid, uuid_t client_id,
4240     u_int8_t *__counted_by(assigned_group_members_length) assigned_group_members,
4241     size_t assigned_group_members_length)
4242 {
4243 #pragma unused(netagent_uuid)
4244 	int error = 0;
4245 	struct necp_fd_data *client_fd = NULL;
4246 	bool found_client = false;
4247 	bool client_updated = false;
4248 
4249 	NECP_FD_LIST_LOCK_SHARED();
4250 
4251 	LIST_FOREACH(client_fd, &necp_fd_list, chain) {
4252 		proc_t proc = proc_find(client_fd->proc_pid);
4253 		if (proc == PROC_NULL) {
4254 			continue;
4255 		}
4256 
4257 		NECP_FD_LOCK(client_fd);
4258 		struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
4259 		if (client != NULL) {
4260 			found_client = true;
4261 			// Release prior results
4262 			if (client->assigned_group_members != NULL) {
4263 				kfree_data_counted_by(client->assigned_group_members, client->assigned_group_members_length);
4264 			}
4265 
4266 			// Save new results
4267 			client->assigned_group_members = assigned_group_members;
4268 			client->assigned_group_members_length = assigned_group_members_length;
4269 			client->group_members_read = false;
4270 
4271 			client_updated = true;
4272 			necp_fd_notify(client_fd, true);
4273 
4274 			NECP_CLIENT_UNLOCK(client);
4275 		}
4276 		NECP_FD_UNLOCK(client_fd);
4277 
4278 		proc_rele(proc);
4279 		proc = PROC_NULL;
4280 
4281 		if (found_client) {
4282 			break;
4283 		}
4284 	}
4285 
4286 	NECP_FD_LIST_UNLOCK();
4287 
4288 	// upon error, client must free assigned_results
4289 	if (!found_client) {
4290 		error = ENOENT;
4291 	} else if (!client_updated) {
4292 		error = EINVAL;
4293 	}
4294 
4295 	return error;
4296 }
4297 
4298 /// Client updating
4299 
4300 static bool
necp_update_parsed_parameters(struct necp_client_parsed_parameters * parsed_parameters,struct necp_aggregate_result * result)4301 necp_update_parsed_parameters(struct necp_client_parsed_parameters *parsed_parameters,
4302     struct necp_aggregate_result *result)
4303 {
4304 	if (parsed_parameters == NULL ||
4305 	    result == NULL) {
4306 		return false;
4307 	}
4308 
4309 	bool updated = false;
4310 	for (int i = 0; i < NECP_MAX_NETAGENTS; i++) {
4311 		if (uuid_is_null(result->netagents[i])) {
4312 			// Passed end of valid agents
4313 			break;
4314 		}
4315 
4316 		if (!(result->netagent_use_flags[i] & NECP_AGENT_USE_FLAG_SCOPE)) {
4317 			// Not a scoped agent, ignore
4318 			continue;
4319 		}
4320 
4321 		// This is a scoped agent. Add it to the required agents.
4322 		if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT) {
4323 			// Already some required agents, add this at the end
4324 			for (int j = 0; j < NECP_MAX_AGENT_PARAMETERS; j++) {
4325 				if (uuid_compare(parsed_parameters->required_netagents[j], result->netagents[i]) == 0) {
4326 					// Already required, break
4327 					break;
4328 				}
4329 				if (uuid_is_null(parsed_parameters->required_netagents[j])) {
4330 					// Add here
4331 					memcpy(&parsed_parameters->required_netagents[j], result->netagents[i], sizeof(uuid_t));
4332 					updated = true;
4333 					break;
4334 				}
4335 			}
4336 		} else {
4337 			// No required agents yet, add this one
4338 			parsed_parameters->valid_fields |= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT;
4339 			memcpy(&parsed_parameters->required_netagents[0], result->netagents[i], sizeof(uuid_t));
4340 			updated = true;
4341 		}
4342 
4343 		// Remove requirements for agents of the same type
4344 		if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE) {
4345 			char remove_agent_domain[NETAGENT_DOMAINSIZE] = { 0 };
4346 			char remove_agent_type[NETAGENT_TYPESIZE] = { 0 };
4347 			if (netagent_get_agent_domain_and_type(result->netagents[i], remove_agent_domain, remove_agent_type)) {
4348 				for (int j = 0; j < NECP_MAX_AGENT_PARAMETERS; j++) {
4349 					if (strbuflen(parsed_parameters->required_netagent_types[j].netagent_domain, sizeof(parsed_parameters->required_netagent_types[j].netagent_domain)) == 0 &&
4350 					    strbuflen(parsed_parameters->required_netagent_types[j].netagent_type, sizeof(parsed_parameters->required_netagent_types[j].netagent_type)) == 0) {
4351 						break;
4352 					}
4353 
4354 					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 &&
4355 					    strbufcmp(parsed_parameters->required_netagent_types[j].netagent_type, sizeof(parsed_parameters->required_netagent_types[j].netagent_type), remove_agent_type, NETAGENT_TYPESIZE) == 0) {
4356 						updated = true;
4357 
4358 						if (j == NECP_MAX_AGENT_PARAMETERS - 1) {
4359 							// Last field, just clear and break
4360 							memset(&parsed_parameters->required_netagent_types[NECP_MAX_AGENT_PARAMETERS - 1], 0, sizeof(struct necp_client_parameter_netagent_type));
4361 							break;
4362 						} else {
4363 							// Move the parameters down, clear the last entry
4364 							memmove(&parsed_parameters->required_netagent_types[j],
4365 							    &parsed_parameters->required_netagent_types[j + 1],
4366 							    sizeof(struct necp_client_parameter_netagent_type) * (NECP_MAX_AGENT_PARAMETERS - (j + 1)));
4367 							memset(&parsed_parameters->required_netagent_types[NECP_MAX_AGENT_PARAMETERS - 1], 0, sizeof(struct necp_client_parameter_netagent_type));
4368 							// Continue, don't increment but look at the new shifted item instead
4369 							continue;
4370 						}
4371 					}
4372 
4373 					// Increment j to look at the next agent type parameter
4374 					j++;
4375 				}
4376 			}
4377 		}
4378 	}
4379 
4380 	if (updated &&
4381 	    parsed_parameters->required_interface_index != IFSCOPE_NONE &&
4382 	    (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF) == 0) {
4383 		// A required interface index was added after the fact. Clear it.
4384 		parsed_parameters->required_interface_index = IFSCOPE_NONE;
4385 	}
4386 
4387 
4388 	return updated;
4389 }
4390 
4391 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)4392 necp_agent_types_match(const char * __sized_by(NETAGENT_DOMAINSIZE)agent_domain1, const char * __sized_by(NETAGENT_TYPESIZE)agent_type1,
4393     const char * __sized_by(NETAGENT_DOMAINSIZE)agent_domain2, const char * __sized_by(NETAGENT_TYPESIZE)agent_type2)
4394 {
4395 	return (strbuflen(agent_domain1, NETAGENT_DOMAINSIZE) == 0 ||
4396 	       strbufcmp(agent_domain2, NETAGENT_DOMAINSIZE, agent_domain1, NETAGENT_DOMAINSIZE) == 0) &&
4397 	       (strbuflen(agent_type1, NETAGENT_TYPESIZE) == 0 ||
4398 	       strbufcmp(agent_type2, NETAGENT_TYPESIZE, agent_type1, NETAGENT_TYPESIZE) == 0);
4399 }
4400 
4401 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)4402 necp_calculate_client_result(proc_t proc,
4403     struct necp_client *client,
4404     struct necp_client_parsed_parameters *parsed_parameters,
4405     struct necp_aggregate_result *result,
4406     u_int32_t *flags,
4407     u_int32_t *reason,
4408     struct necp_client_endpoint *v4_gateway,
4409     struct necp_client_endpoint *v6_gateway,
4410     uuid_t *override_euuid)
4411 {
4412 	struct rtentry * __single route = NULL;
4413 
4414 	// Check parameters to find best interface
4415 	bool validate_agents = false;
4416 	u_int matching_if_index = 0;
4417 	if (necp_find_matching_interface_index(parsed_parameters, &matching_if_index, &validate_agents)) {
4418 		if (matching_if_index != 0) {
4419 			parsed_parameters->required_interface_index = matching_if_index;
4420 		}
4421 		// Interface found or not needed, match policy.
4422 		memset(result, 0, sizeof(*result));
4423 		int error = necp_application_find_policy_match_internal(proc, client->parameters,
4424 		    (u_int32_t)client->parameters_length,
4425 		    result, flags, reason, matching_if_index,
4426 		    NULL, NULL,
4427 		    v4_gateway, v6_gateway,
4428 		    &route, false, true,
4429 		    override_euuid);
4430 		if (error != 0) {
4431 			if (route != NULL) {
4432 				rtfree(route);
4433 			}
4434 			return FALSE;
4435 		}
4436 
4437 		if (validate_agents) {
4438 			bool requirement_failed = FALSE;
4439 			if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT) {
4440 				for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
4441 					if (uuid_is_null(parsed_parameters->required_netagents[i])) {
4442 						break;
4443 					}
4444 
4445 					bool requirement_found = FALSE;
4446 					for (int j = 0; j < NECP_MAX_NETAGENTS; j++) {
4447 						if (uuid_is_null(result->netagents[j])) {
4448 							break;
4449 						}
4450 
4451 						if (result->netagent_use_flags[j] & NECP_AGENT_USE_FLAG_REMOVE) {
4452 							// A removed agent, ignore
4453 							continue;
4454 						}
4455 
4456 						if (uuid_compare(parsed_parameters->required_netagents[i], result->netagents[j]) == 0) {
4457 							requirement_found = TRUE;
4458 							break;
4459 						}
4460 					}
4461 
4462 					if (!requirement_found) {
4463 						requirement_failed = TRUE;
4464 						break;
4465 					}
4466 				}
4467 			}
4468 
4469 			if (!requirement_failed && parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE) {
4470 				for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
4471 					if (strbuflen(parsed_parameters->required_netagent_types[i].netagent_domain, sizeof(parsed_parameters->required_netagent_types[i].netagent_domain)) == 0 &&
4472 					    strbuflen(parsed_parameters->required_netagent_types[i].netagent_type, sizeof(parsed_parameters->required_netagent_types[i].netagent_type)) == 0) {
4473 						break;
4474 					}
4475 
4476 					bool requirement_found = FALSE;
4477 					for (int j = 0; j < NECP_MAX_NETAGENTS; j++) {
4478 						if (uuid_is_null(result->netagents[j])) {
4479 							break;
4480 						}
4481 
4482 						if (result->netagent_use_flags[j] & NECP_AGENT_USE_FLAG_REMOVE) {
4483 							// A removed agent, ignore
4484 							continue;
4485 						}
4486 
4487 						char policy_agent_domain[NETAGENT_DOMAINSIZE] = { 0 };
4488 						char policy_agent_type[NETAGENT_TYPESIZE] = { 0 };
4489 
4490 						if (netagent_get_agent_domain_and_type(result->netagents[j], policy_agent_domain, policy_agent_type)) {
4491 							if (necp_agent_types_match(parsed_parameters->required_netagent_types[i].netagent_domain,
4492 							    parsed_parameters->required_netagent_types[i].netagent_type,
4493 							    policy_agent_domain, policy_agent_type)) {
4494 								requirement_found = TRUE;
4495 								break;
4496 							}
4497 						}
4498 					}
4499 
4500 					if (!requirement_found) {
4501 						requirement_failed = TRUE;
4502 						break;
4503 					}
4504 				}
4505 			}
4506 
4507 			if (requirement_failed) {
4508 				// Agent requirement failed. Clear out the whole result, make everything fail.
4509 				memset(result, 0, sizeof(*result));
4510 				if (route != NULL) {
4511 					rtfree(route);
4512 				}
4513 				return TRUE;
4514 			}
4515 		}
4516 
4517 		// Reset current route
4518 		NECP_CLIENT_ROUTE_LOCK(client);
4519 		if (client->current_route != NULL) {
4520 			rtfree(client->current_route);
4521 		}
4522 		client->current_route = route;
4523 		NECP_CLIENT_ROUTE_UNLOCK(client);
4524 	} else {
4525 		// Interface not found. Clear out the whole result, make everything fail.
4526 		memset(result, 0, sizeof(*result));
4527 	}
4528 
4529 	return TRUE;
4530 }
4531 
4532 #define NECP_PARSED_PARAMETERS_REQUIRED_FIELDS (NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF |             \
4533 	                                                                                        NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE |          \
4534 	                                                                                        NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT |           \
4535 	                                                                                        NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE)
4536 
4537 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)4538 necp_update_client_result(proc_t proc,
4539     struct necp_fd_data *client_fd,
4540     struct necp_client *client,
4541     struct _necp_flow_defunct_list *defunct_list)
4542 {
4543 	struct necp_client_result_netagent netagent;
4544 	struct necp_aggregate_result result;
4545 	struct necp_client_parsed_parameters * __single parsed_parameters = NULL;
4546 	u_int32_t flags = 0;
4547 	u_int32_t reason = 0;
4548 
4549 	NECP_CLIENT_ASSERT_LOCKED(client);
4550 
4551 	parsed_parameters = kalloc_type(struct necp_client_parsed_parameters,
4552 	    Z_WAITOK | Z_ZERO | Z_NOFAIL);
4553 
4554 	// Nexus flows will be brought back if they are still valid
4555 	necp_client_mark_all_nonsocket_flows_as_invalid(client);
4556 
4557 	int error = necp_client_parse_parameters(client, client->parameters, (u_int32_t)client->parameters_length, parsed_parameters);
4558 	if (error != 0) {
4559 		kfree_type(struct necp_client_parsed_parameters, parsed_parameters);
4560 		return FALSE;
4561 	}
4562 	bool originally_scoped = (parsed_parameters->required_interface_index != IFSCOPE_NONE);
4563 
4564 	// Update saved IP protocol
4565 	client->ip_protocol = parsed_parameters->ip_protocol;
4566 
4567 	// Calculate the policy result
4568 	struct necp_client_endpoint v4_gateway = {};
4569 	struct necp_client_endpoint v6_gateway = {};
4570 	uuid_t override_euuid;
4571 	uuid_clear(override_euuid);
4572 	if (!necp_calculate_client_result(proc, client, parsed_parameters, &result, &flags, &reason, &v4_gateway, &v6_gateway, &override_euuid)) {
4573 		kfree_type(struct necp_client_parsed_parameters, parsed_parameters);
4574 		return FALSE;
4575 	}
4576 
4577 	if (necp_update_parsed_parameters(parsed_parameters, &result)) {
4578 		// Changed the parameters based on result, try again (only once)
4579 		if (!necp_calculate_client_result(proc, client, parsed_parameters, &result, &flags, &reason, &v4_gateway, &v6_gateway, &override_euuid)) {
4580 			kfree_type(struct necp_client_parsed_parameters, parsed_parameters);
4581 			return FALSE;
4582 		}
4583 	}
4584 
4585 	if ((parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) &&
4586 	    parsed_parameters->required_interface_index != IFSCOPE_NONE &&
4587 	    (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF) == 0) {
4588 		// Listener should not apply required interface index if
4589 		parsed_parameters->required_interface_index = IFSCOPE_NONE;
4590 	}
4591 
4592 	// Save the last policy id on the client
4593 	client->policy_id = result.policy_id;
4594 	client->skip_policy_id = result.skip_policy_id;
4595 	uuid_copy(client->override_euuid, override_euuid);
4596 
4597 	if ((parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_MULTIPATH) ||
4598 	    (parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_BROWSE) ||
4599 	    ((parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) &&
4600 	    result.routing_result != NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED)) {
4601 		client->allow_multiple_flows = TRUE;
4602 	} else {
4603 		client->allow_multiple_flows = FALSE;
4604 	}
4605 
4606 	// If the original request was scoped, and the policy result matches, make sure the result is scoped
4607 	if ((result.routing_result == NECP_KERNEL_POLICY_RESULT_NONE ||
4608 	    result.routing_result == NECP_KERNEL_POLICY_RESULT_PASS) &&
4609 	    result.routed_interface_index != IFSCOPE_NONE &&
4610 	    parsed_parameters->required_interface_index == result.routed_interface_index) {
4611 		result.routing_result = NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED;
4612 		result.routing_result_parameter.scoped_interface_index = result.routed_interface_index;
4613 	}
4614 
4615 	if (defunct_list != NULL &&
4616 	    result.routing_result == NECP_KERNEL_POLICY_RESULT_DROP) {
4617 		// If we are forced to drop the client, defunct it if it has flows
4618 		necp_defunct_client_for_policy(client, defunct_list);
4619 	}
4620 
4621 	// Recalculate flags
4622 	if (parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) {
4623 		// Listeners are valid as long as they aren't dropped
4624 		if (result.routing_result != NECP_KERNEL_POLICY_RESULT_DROP) {
4625 			flags |= NECP_CLIENT_RESULT_FLAG_SATISFIED;
4626 		}
4627 	} else if (result.routed_interface_index != 0) {
4628 		// Clients without flows determine viability based on having some routable interface
4629 		flags |= NECP_CLIENT_RESULT_FLAG_SATISFIED;
4630 	}
4631 
4632 	bool updated = FALSE;
4633 	u_int8_t * __indexable cursor = client->result;
4634 	cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_FLAGS, sizeof(flags), &flags, &updated, client->result, sizeof(client->result));
4635 	if (reason != 0) {
4636 		cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_REASON, sizeof(reason), &reason, &updated, client->result, sizeof(client->result));
4637 	}
4638 	cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_CLIENT_ID, sizeof(uuid_t), client->client_id, &updated,
4639 	    client->result, sizeof(client->result));
4640 	cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_POLICY_RESULT, sizeof(result.routing_result), &result.routing_result, &updated,
4641 	    client->result, sizeof(client->result));
4642 	if (result.routing_result_parameter.tunnel_interface_index != 0) {
4643 		cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_POLICY_RESULT_PARAMETER,
4644 		    sizeof(result.routing_result_parameter), &result.routing_result_parameter, &updated,
4645 		    client->result, sizeof(client->result));
4646 	}
4647 	if (result.filter_control_unit != 0) {
4648 		cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_FILTER_CONTROL_UNIT,
4649 		    sizeof(result.filter_control_unit), &result.filter_control_unit, &updated,
4650 		    client->result, sizeof(client->result));
4651 	}
4652 	if (result.flow_divert_aggregate_unit != 0) {
4653 		cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_FLOW_DIVERT_AGGREGATE_UNIT,
4654 		    sizeof(result.flow_divert_aggregate_unit), &result.flow_divert_aggregate_unit, &updated,
4655 		    client->result, sizeof(client->result));
4656 	}
4657 	if (result.routed_interface_index != 0) {
4658 		u_int routed_interface_index = result.routed_interface_index;
4659 		if (result.routing_result == NECP_KERNEL_POLICY_RESULT_IP_TUNNEL &&
4660 		    (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_REQUIRED_FIELDS) &&
4661 		    parsed_parameters->required_interface_index != IFSCOPE_NONE &&
4662 		    parsed_parameters->required_interface_index != result.routed_interface_index) {
4663 			routed_interface_index = parsed_parameters->required_interface_index;
4664 		}
4665 
4666 		cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_INTERFACE_INDEX,
4667 		    sizeof(routed_interface_index), &routed_interface_index, &updated,
4668 		    client->result, sizeof(client->result));
4669 	}
4670 	if (client_fd && client_fd->flags & NECP_OPEN_FLAG_BACKGROUND) {
4671 		u_int32_t effective_traffic_class = SO_TC_BK_SYS;
4672 		cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_EFFECTIVE_TRAFFIC_CLASS,
4673 		    sizeof(effective_traffic_class), &effective_traffic_class, &updated,
4674 		    client->result, sizeof(client->result));
4675 	}
4676 
4677 	if (client_fd->background) {
4678 		bool has_assigned_flow = FALSE;
4679 		struct necp_client_flow_registration *flow_registration = NULL;
4680 		struct necp_client_flow *search_flow = NULL;
4681 		RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
4682 			LIST_FOREACH(search_flow, &flow_registration->flow_list, flow_chain) {
4683 				if (search_flow->assigned) {
4684 					has_assigned_flow = TRUE;
4685 					break;
4686 				}
4687 			}
4688 		}
4689 
4690 		if (has_assigned_flow) {
4691 			u_int32_t background = client_fd->background;
4692 			cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_TRAFFIC_MGMT_BG,
4693 			    sizeof(background), &background, &updated,
4694 			    client->result, sizeof(client->result));
4695 		}
4696 	}
4697 
4698 	bool write_v4_gateway = !necp_client_endpoint_is_unspecified(&v4_gateway);
4699 	bool write_v6_gateway = !necp_client_endpoint_is_unspecified(&v6_gateway);
4700 
4701 	NECP_CLIENT_ROUTE_LOCK(client);
4702 	if (client->current_route != NULL) {
4703 		const u_int32_t route_mtu = get_maxmtu(client->current_route);
4704 		if (route_mtu != 0) {
4705 			cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_EFFECTIVE_MTU,
4706 			    sizeof(route_mtu), &route_mtu, &updated,
4707 			    client->result, sizeof(client->result));
4708 		}
4709 		bool has_remote_addr = parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REMOTE_ADDR;
4710 		if (has_remote_addr && client->current_route->rt_gateway != NULL) {
4711 			if (client->current_route->rt_gateway->sa_family == AF_INET) {
4712 				write_v6_gateway = false;
4713 			} else if (client->current_route->rt_gateway->sa_family == AF_INET6) {
4714 				write_v4_gateway = false;
4715 			}
4716 		}
4717 	}
4718 	NECP_CLIENT_ROUTE_UNLOCK(client);
4719 
4720 	if (write_v4_gateway) {
4721 		cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_GATEWAY,
4722 		    sizeof(struct necp_client_endpoint), (uint8_t *)(struct necp_client_endpoint * __bidi_indexable)&v4_gateway, &updated,
4723 		    client->result, sizeof(client->result));
4724 	}
4725 
4726 	if (write_v6_gateway) {
4727 		cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_GATEWAY,
4728 		    sizeof(struct necp_client_endpoint), (uint8_t *)(struct necp_client_endpoint * __bidi_indexable)&v6_gateway, &updated,
4729 		    client->result, sizeof(client->result));
4730 	}
4731 
4732 	for (int i = 0; i < NAT64_MAX_NUM_PREFIXES; i++) {
4733 		if (result.nat64_prefixes[i].prefix_len != 0) {
4734 			cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_NAT64,
4735 			    sizeof(result.nat64_prefixes), result.nat64_prefixes, &updated,
4736 			    client->result, sizeof(client->result));
4737 			break;
4738 		}
4739 	}
4740 
4741 	if (result.mss_recommended != 0) {
4742 		cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_RECOMMENDED_MSS,
4743 		    sizeof(result.mss_recommended), &result.mss_recommended, &updated,
4744 		    client->result, sizeof(client->result));
4745 	}
4746 
4747 	for (int i = 0; i < NECP_MAX_NETAGENTS; i++) {
4748 		if (uuid_is_null(result.netagents[i])) {
4749 			break;
4750 		}
4751 		if (result.netagent_use_flags[i] & NECP_AGENT_USE_FLAG_REMOVE) {
4752 			// A removed agent, ignore
4753 			continue;
4754 		}
4755 		uuid_copy(netagent.netagent_uuid, result.netagents[i]);
4756 		netagent.generation = netagent_get_generation(netagent.netagent_uuid);
4757 		if (necp_netagent_applies_to_client(client, parsed_parameters, &netagent.netagent_uuid, TRUE, 0, 0)) {
4758 			cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_NETAGENT, sizeof(netagent), &netagent, &updated,
4759 			    client->result, sizeof(client->result));
4760 		}
4761 	}
4762 
4763 	ifnet_head_lock_shared();
4764 	ifnet_t direct_interface = NULL;
4765 	ifnet_t delegate_interface = NULL;
4766 	ifnet_t original_scoped_interface = NULL;
4767 
4768 	if (result.routed_interface_index != IFSCOPE_NONE && result.routed_interface_index <= (u_int32_t)if_index) {
4769 		direct_interface = ifindex2ifnet[result.routed_interface_index];
4770 	} else if (parsed_parameters->required_interface_index != IFSCOPE_NONE &&
4771 	    parsed_parameters->required_interface_index <= (u_int32_t)if_index) {
4772 		// If the request was scoped, but the route didn't match, still grab the agents
4773 		direct_interface = ifindex2ifnet[parsed_parameters->required_interface_index];
4774 	} else if (result.routed_interface_index == IFSCOPE_NONE &&
4775 	    result.routing_result == NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED &&
4776 	    result.routing_result_parameter.scoped_interface_index != IFSCOPE_NONE) {
4777 		direct_interface = ifindex2ifnet[result.routing_result_parameter.scoped_interface_index];
4778 	}
4779 	if (direct_interface != NULL) {
4780 		delegate_interface = direct_interface->if_delegated.ifp;
4781 	}
4782 	if (result.routing_result == NECP_KERNEL_POLICY_RESULT_IP_TUNNEL &&
4783 	    (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_REQUIRED_FIELDS) &&
4784 	    parsed_parameters->required_interface_index != IFSCOPE_NONE &&
4785 	    parsed_parameters->required_interface_index != result.routing_result_parameter.tunnel_interface_index &&
4786 	    parsed_parameters->required_interface_index <= (u_int32_t)if_index) {
4787 		original_scoped_interface = ifindex2ifnet[parsed_parameters->required_interface_index];
4788 	}
4789 	// Add interfaces
4790 	if (original_scoped_interface != NULL) {
4791 		struct necp_client_result_interface interface_struct;
4792 		interface_struct.index = original_scoped_interface->if_index;
4793 		interface_struct.generation = ifnet_get_generation(original_scoped_interface);
4794 		cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_INTERFACE, sizeof(interface_struct), &interface_struct, &updated,
4795 		    client->result, sizeof(client->result));
4796 	}
4797 	if (direct_interface != NULL) {
4798 		struct necp_client_result_interface interface_struct;
4799 		interface_struct.index = direct_interface->if_index;
4800 		interface_struct.generation = ifnet_get_generation(direct_interface);
4801 		cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_INTERFACE, sizeof(interface_struct), &interface_struct, &updated,
4802 		    client->result, sizeof(client->result));
4803 
4804 		// Set the delta time since interface up/down
4805 		struct timeval updown_delta = {};
4806 		if (ifnet_updown_delta(direct_interface, &updown_delta) == 0) {
4807 			u_int32_t delta = updown_delta.tv_sec;
4808 			bool ignore_updated = FALSE;
4809 			cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_INTERFACE_TIME_DELTA,
4810 			    sizeof(delta), &delta, &ignore_updated,
4811 			    client->result, sizeof(client->result));
4812 		}
4813 	}
4814 	if (delegate_interface != NULL) {
4815 		struct necp_client_result_interface interface_struct;
4816 		interface_struct.index = delegate_interface->if_index;
4817 		interface_struct.generation = ifnet_get_generation(delegate_interface);
4818 		cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_INTERFACE, sizeof(interface_struct), &interface_struct, &updated,
4819 		    client->result, sizeof(client->result));
4820 	}
4821 
4822 	// Update multipath/listener interface flows
4823 	if (parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_MULTIPATH && !(parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_BROWSE)) {
4824 		// Add the interface option for the routed interface first
4825 		if (direct_interface != NULL) {
4826 			// Add nexus agent
4827 			necp_client_add_agent_interface_options(client, parsed_parameters, direct_interface);
4828 
4829 			// Add interface option in case it is not a nexus
4830 			necp_client_add_interface_option_if_needed(client, direct_interface->if_index,
4831 			    ifnet_get_generation(direct_interface), NULL, false);
4832 		}
4833 		if (parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_INBOUND) {
4834 			// For inbound multipath, add from the global list (like a listener)
4835 			struct ifnet *multi_interface = NULL;
4836 			TAILQ_FOREACH(multi_interface, &ifnet_head, if_link) {
4837 				if ((multi_interface->if_flags & (IFF_UP | IFF_RUNNING)) &&
4838 				    necp_ifnet_matches_parameters(multi_interface, parsed_parameters, 0, NULL, true, false)) {
4839 					// Add nexus agents for inbound multipath
4840 					necp_client_add_agent_interface_options(client, parsed_parameters, multi_interface);
4841 				}
4842 			}
4843 		} else {
4844 			// Get other multipath interface options from ordered list
4845 			struct ifnet *multi_interface = NULL;
4846 			TAILQ_FOREACH(multi_interface, &ifnet_ordered_head, if_ordered_link) {
4847 				if (multi_interface != direct_interface &&
4848 				    necp_ifnet_matches_parameters(multi_interface, parsed_parameters, 0, NULL, true, false)) {
4849 					// Add nexus agents for multipath
4850 					necp_client_add_agent_interface_options(client, parsed_parameters, multi_interface);
4851 
4852 					// Add multipath interface flows for kernel MPTCP
4853 					necp_client_add_interface_option_if_needed(client, multi_interface->if_index,
4854 					    ifnet_get_generation(multi_interface), NULL, false);
4855 				}
4856 			}
4857 		}
4858 	} else if (parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) {
4859 		if (result.routing_result == NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED) {
4860 			if (direct_interface != NULL) {
4861 				// If scoped, only listen on that interface
4862 				// Add nexus agents for listeners
4863 				necp_client_add_agent_interface_options(client, parsed_parameters, direct_interface);
4864 
4865 				// Add interface option in case it is not a nexus
4866 				necp_client_add_interface_option_if_needed(client, direct_interface->if_index,
4867 				    ifnet_get_generation(direct_interface), NULL, false);
4868 			}
4869 		} else {
4870 			// Get listener interface options from global list
4871 			struct ifnet *listen_interface = NULL;
4872 			TAILQ_FOREACH(listen_interface, &ifnet_head, if_link) {
4873 				if ((listen_interface->if_flags & (IFF_UP | IFF_RUNNING)) &&
4874 				    necp_ifnet_matches_parameters(listen_interface, parsed_parameters, 0, NULL, true, false)) {
4875 					// Add nexus agents for listeners
4876 					necp_client_add_agent_interface_options(client, parsed_parameters, listen_interface);
4877 				}
4878 			}
4879 		}
4880 	} else if (parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_BROWSE) {
4881 		if (result.routing_result == NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED && originally_scoped) {
4882 			if (direct_interface != NULL) {
4883 				// Add browse option if it has an agent
4884 				necp_client_add_browse_interface_options(client, parsed_parameters, direct_interface);
4885 			}
4886 		} else {
4887 			// Get browse interface options from global list
4888 			struct ifnet *browse_interface = NULL;
4889 			TAILQ_FOREACH(browse_interface, &ifnet_head, if_link) {
4890 				if (necp_ifnet_matches_parameters(browse_interface, parsed_parameters, 0, NULL, true, false)) {
4891 					necp_client_add_browse_interface_options(client, parsed_parameters, browse_interface);
4892 				}
4893 			}
4894 		}
4895 	}
4896 
4897 	struct necp_client_result_estimated_throughput throughput = {
4898 		.up = 0,
4899 		.down = 0,
4900 	};
4901 
4902 	// Add agents
4903 	if (original_scoped_interface != NULL) {
4904 		ifnet_lock_shared(original_scoped_interface);
4905 		if (original_scoped_interface->if_agentids != NULL) {
4906 			for (u_int32_t i = 0; i < original_scoped_interface->if_agentcount; i++) {
4907 				if (uuid_is_null(original_scoped_interface->if_agentids[i])) {
4908 					continue;
4909 				}
4910 				bool skip_agent = false;
4911 				for (int j = 0; j < NECP_MAX_NETAGENTS; j++) {
4912 					if (uuid_is_null(result.netagents[j])) {
4913 						break;
4914 					}
4915 					if ((result.netagent_use_flags[j] & NECP_AGENT_USE_FLAG_REMOVE) &&
4916 					    uuid_compare(original_scoped_interface->if_agentids[i], result.netagents[j]) == 0) {
4917 						skip_agent = true;
4918 						break;
4919 					}
4920 				}
4921 				if (skip_agent) {
4922 					continue;
4923 				}
4924 				uuid_copy(netagent.netagent_uuid, original_scoped_interface->if_agentids[i]);
4925 				netagent.generation = netagent_get_generation(netagent.netagent_uuid);
4926 				if (necp_netagent_applies_to_client(client, parsed_parameters, &netagent.netagent_uuid, FALSE,
4927 				    original_scoped_interface->if_index, ifnet_get_generation(original_scoped_interface))) {
4928 					cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_NETAGENT, sizeof(netagent), &netagent, &updated,
4929 					    client->result, sizeof(client->result));
4930 				}
4931 			}
4932 		}
4933 		ifnet_lock_done(original_scoped_interface);
4934 	}
4935 	if (direct_interface != NULL) {
4936 		ifnet_lock_shared(direct_interface);
4937 		throughput.up = direct_interface->if_estimated_up_bucket;
4938 		throughput.down = direct_interface->if_estimated_down_bucket;
4939 		if (direct_interface->if_agentids != NULL) {
4940 			for (u_int32_t i = 0; i < direct_interface->if_agentcount; i++) {
4941 				if (uuid_is_null(direct_interface->if_agentids[i])) {
4942 					continue;
4943 				}
4944 				bool skip_agent = false;
4945 				for (int j = 0; j < NECP_MAX_NETAGENTS; j++) {
4946 					if (uuid_is_null(result.netagents[j])) {
4947 						break;
4948 					}
4949 					if ((result.netagent_use_flags[j] & NECP_AGENT_USE_FLAG_REMOVE) &&
4950 					    uuid_compare(direct_interface->if_agentids[i], result.netagents[j]) == 0) {
4951 						skip_agent = true;
4952 						break;
4953 					}
4954 				}
4955 				if (skip_agent) {
4956 					continue;
4957 				}
4958 				uuid_copy(netagent.netagent_uuid, direct_interface->if_agentids[i]);
4959 				netagent.generation = netagent_get_generation(netagent.netagent_uuid);
4960 				if (necp_netagent_applies_to_client(client, parsed_parameters, &netagent.netagent_uuid, TRUE,
4961 				    direct_interface->if_index, ifnet_get_generation(direct_interface))) {
4962 					cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_NETAGENT, sizeof(netagent), &netagent, &updated,
4963 					    client->result, sizeof(client->result));
4964 				}
4965 			}
4966 		}
4967 		ifnet_lock_done(direct_interface);
4968 	}
4969 	if (delegate_interface != NULL) {
4970 		ifnet_lock_shared(delegate_interface);
4971 		if (throughput.up == 0 && throughput.down == 0) {
4972 			throughput.up = delegate_interface->if_estimated_up_bucket;
4973 			throughput.down = delegate_interface->if_estimated_down_bucket;
4974 		}
4975 		if (delegate_interface->if_agentids != NULL) {
4976 			for (u_int32_t i = 0; i < delegate_interface->if_agentcount; i++) {
4977 				if (uuid_is_null(delegate_interface->if_agentids[i])) {
4978 					continue;
4979 				}
4980 				bool skip_agent = false;
4981 				for (int j = 0; j < NECP_MAX_NETAGENTS; j++) {
4982 					if (uuid_is_null(result.netagents[j])) {
4983 						break;
4984 					}
4985 					if ((result.netagent_use_flags[j] & NECP_AGENT_USE_FLAG_REMOVE) &&
4986 					    uuid_compare(delegate_interface->if_agentids[i], result.netagents[j]) == 0) {
4987 						skip_agent = true;
4988 						break;
4989 					}
4990 				}
4991 				if (skip_agent) {
4992 					continue;
4993 				}
4994 				uuid_copy(netagent.netagent_uuid, delegate_interface->if_agentids[i]);
4995 				netagent.generation = netagent_get_generation(netagent.netagent_uuid);
4996 				if (necp_netagent_applies_to_client(client, parsed_parameters, &netagent.netagent_uuid, FALSE,
4997 				    delegate_interface->if_index, ifnet_get_generation(delegate_interface))) {
4998 					cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_NETAGENT, sizeof(netagent), &netagent, &updated,
4999 					    client->result, sizeof(client->result));
5000 				}
5001 			}
5002 		}
5003 		ifnet_lock_done(delegate_interface);
5004 	}
5005 	ifnet_head_done();
5006 
5007 	if (throughput.up != 0 || throughput.down != 0) {
5008 		cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_ESTIMATED_THROUGHPUT,
5009 		    sizeof(throughput), &throughput, &updated, client->result, sizeof(client->result));
5010 	}
5011 
5012 	// Add interface options
5013 	for (u_int32_t option_i = 0; option_i < client->interface_option_count; option_i++) {
5014 		if (option_i < NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT) {
5015 			struct necp_client_interface_option *option = &client->interface_options[option_i];
5016 			cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_INTERFACE_OPTION, sizeof(*option), option, &updated,
5017 			    client->result, sizeof(client->result));
5018 		} else {
5019 			struct necp_client_interface_option *option = &client->extra_interface_options[option_i - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT];
5020 			cursor = necp_buffer_write_tlv_if_different(cursor, NECP_CLIENT_RESULT_INTERFACE_OPTION, sizeof(*option), option, &updated,
5021 			    client->result, sizeof(client->result));
5022 		}
5023 	}
5024 
5025 	size_t new_result_length = (cursor - client->result);
5026 	if (new_result_length != client->result_length) {
5027 		client->result_length = new_result_length;
5028 		updated = TRUE;
5029 	}
5030 
5031 	// Update flow viability/flags
5032 	if (necp_client_update_flows(proc, client, defunct_list)) {
5033 		updated = TRUE;
5034 	}
5035 
5036 	if (updated) {
5037 		client->result_read = FALSE;
5038 		necp_client_update_observer_update(client);
5039 	}
5040 
5041 	kfree_type(struct necp_client_parsed_parameters, parsed_parameters);
5042 	return updated;
5043 }
5044 
5045 static bool
necp_defunct_client_fd_locked_inner(struct necp_fd_data * client_fd,struct _necp_flow_defunct_list * defunct_list,bool destroy_stats)5046 necp_defunct_client_fd_locked_inner(struct necp_fd_data *client_fd, struct _necp_flow_defunct_list *defunct_list, bool destroy_stats)
5047 {
5048 	bool updated_result = FALSE;
5049 	struct necp_client *client = NULL;
5050 
5051 	NECP_FD_ASSERT_LOCKED(client_fd);
5052 
5053 	RB_FOREACH(client, _necp_client_tree, &client_fd->clients) {
5054 		struct necp_client_flow_registration *flow_registration = NULL;
5055 
5056 		NECP_CLIENT_LOCK(client);
5057 
5058 		// Prepare close events to be sent to the nexus to effectively remove the flows
5059 		struct necp_client_flow *search_flow = NULL;
5060 		RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
5061 			LIST_FOREACH(search_flow, &flow_registration->flow_list, flow_chain) {
5062 				if (search_flow->nexus &&
5063 				    !uuid_is_null(search_flow->u.nexus_agent)) {
5064 					// Sleeping alloc won't fail; copy only what's necessary
5065 					struct necp_flow_defunct *flow_defunct = kalloc_type(struct necp_flow_defunct, Z_WAITOK | Z_ZERO);
5066 					uuid_copy(flow_defunct->nexus_agent, search_flow->u.nexus_agent);
5067 					uuid_copy(flow_defunct->flow_id, ((flow_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
5068 					    client->client_id :
5069 					    flow_registration->registration_id));
5070 					flow_defunct->proc_pid = client->proc_pid;
5071 					flow_defunct->agent_handle = client->agent_handle;
5072 					flow_defunct->flags = flow_registration->flags;
5073 #if SKYWALK
5074 					if (flow_registration->kstats_kaddr != NULL) {
5075 						struct necp_all_stats *ustats_kaddr = ((struct necp_all_kstats *)flow_registration->kstats_kaddr)->necp_stats_ustats;
5076 						struct necp_quic_stats *quicstats = (struct necp_quic_stats *)ustats_kaddr;
5077 						if (quicstats != NULL &&
5078 						    quicstats->necp_quic_udp_stats.necp_udp_hdr.necp_stats_type == NECP_CLIENT_STATISTICS_TYPE_QUIC) {
5079 							memcpy(flow_defunct->close_parameters.u.close_token, quicstats->necp_quic_extra.ssr_token, sizeof(flow_defunct->close_parameters.u.close_token));
5080 							flow_defunct->has_close_parameters = true;
5081 						}
5082 					}
5083 #endif /* SKYWALK */
5084 					// Add to the list provided by caller
5085 					LIST_INSERT_HEAD(defunct_list, flow_defunct, chain);
5086 
5087 					flow_registration->defunct = true;
5088 					flow_registration->flow_result_read = false;
5089 					updated_result = true;
5090 				}
5091 			}
5092 		}
5093 		if (destroy_stats) {
5094 #if SKYWALK
5095 			// Free any remaining stats objects back to the arena where they came from;
5096 			// do this independent of the above defunct check, as the client may have
5097 			// been marked as defunct separately via necp_defunct_client_for_policy().
5098 			RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
5099 				necp_destroy_flow_stats(client_fd, flow_registration, NULL, FALSE);
5100 			}
5101 #endif /* SKYWALK */
5102 		}
5103 		NECP_CLIENT_UNLOCK(client);
5104 	}
5105 
5106 	return updated_result;
5107 }
5108 
5109 static inline void
necp_defunct_client_fd_locked(struct necp_fd_data * client_fd,struct _necp_flow_defunct_list * defunct_list,struct proc * proc)5110 necp_defunct_client_fd_locked(struct necp_fd_data *client_fd, struct _necp_flow_defunct_list *defunct_list, struct proc *proc)
5111 {
5112 #pragma unused(proc)
5113 	bool updated_result = FALSE;
5114 
5115 	NECP_FD_ASSERT_LOCKED(client_fd);
5116 #if SKYWALK
5117 	// redirect regions of currently-active stats arena to zero-filled pages
5118 	struct necp_arena_info *nai = necp_fd_mredirect_stats_arena(client_fd, proc);
5119 #endif /* SKYWALK */
5120 
5121 	updated_result = necp_defunct_client_fd_locked_inner(client_fd, defunct_list, true);
5122 
5123 #if SKYWALK
5124 	// and tear down the currently-active arena's regions now that the redirection and freeing are done
5125 	if (nai != NULL) {
5126 		ASSERT((nai->nai_flags & (NAIF_REDIRECT | NAIF_DEFUNCT)) == NAIF_REDIRECT);
5127 		ASSERT(nai->nai_arena != NULL);
5128 		ASSERT(nai->nai_mmap.ami_mapref != NULL);
5129 
5130 		int err = skmem_arena_defunct(nai->nai_arena);
5131 		VERIFY(err == 0);
5132 
5133 		nai->nai_flags |= NAIF_DEFUNCT;
5134 	}
5135 #endif /* SKYWALK */
5136 
5137 	if (updated_result) {
5138 		necp_fd_notify(client_fd, true);
5139 	}
5140 }
5141 
5142 static inline void
necp_update_client_fd_locked(struct necp_fd_data * client_fd,proc_t proc,struct _necp_flow_defunct_list * defunct_list)5143 necp_update_client_fd_locked(struct necp_fd_data *client_fd,
5144     proc_t proc,
5145     struct _necp_flow_defunct_list *defunct_list)
5146 {
5147 	struct necp_client *client = NULL;
5148 	bool updated_result = FALSE;
5149 	NECP_FD_ASSERT_LOCKED(client_fd);
5150 	RB_FOREACH(client, _necp_client_tree, &client_fd->clients) {
5151 		NECP_CLIENT_LOCK(client);
5152 		if (necp_update_client_result(proc, client_fd, client, defunct_list)) {
5153 			updated_result = TRUE;
5154 		}
5155 		NECP_CLIENT_UNLOCK(client);
5156 	}
5157 
5158 	// Check if this PID needs to request in-process flow divert
5159 	NECP_FD_LIST_ASSERT_LOCKED();
5160 	for (int i = 0; i < NECP_MAX_FLOW_DIVERT_NEEDED_PIDS; i++) {
5161 		if (necp_flow_divert_needed_pids[i] == 0) {
5162 			break;
5163 		}
5164 		if (necp_flow_divert_needed_pids[i] == client_fd->proc_pid) {
5165 			client_fd->request_in_process_flow_divert = true;
5166 			break;
5167 		}
5168 	}
5169 
5170 	if (updated_result || client_fd->request_in_process_flow_divert) {
5171 		necp_fd_notify(client_fd, true);
5172 	}
5173 }
5174 
5175 #if SKYWALK
5176 static void
necp_close_empty_arenas_callout(__unused thread_call_param_t dummy,__unused thread_call_param_t arg)5177 necp_close_empty_arenas_callout(__unused thread_call_param_t dummy,
5178     __unused thread_call_param_t arg)
5179 {
5180 	struct necp_fd_data *client_fd = NULL;
5181 
5182 	NECP_FD_LIST_LOCK_SHARED();
5183 
5184 	LIST_FOREACH(client_fd, &necp_fd_list, chain) {
5185 		NECP_FD_LOCK(client_fd);
5186 		necp_stats_arenas_destroy(client_fd, FALSE);
5187 		NECP_FD_UNLOCK(client_fd);
5188 	}
5189 
5190 	NECP_FD_LIST_UNLOCK();
5191 }
5192 #endif /* SKYWALK */
5193 
5194 static void
necp_update_all_clients_callout(__unused thread_call_param_t dummy,__unused thread_call_param_t arg)5195 necp_update_all_clients_callout(__unused thread_call_param_t dummy,
5196     __unused thread_call_param_t arg)
5197 {
5198 	struct necp_fd_data *client_fd = NULL;
5199 
5200 	NECP_UPDATE_ALL_CLIENTS_LOCK_EXCLUSIVE();
5201 	uint32_t count = necp_update_all_clients_sched_cnt;
5202 	necp_update_all_clients_sched_cnt = 0;
5203 	necp_update_all_clients_sched_abstime = 0;
5204 	NECP_UPDATE_ALL_CLIENTS_UNLOCK();
5205 
5206 	if (necp_debug > 0) {
5207 		NECPLOG(LOG_DEBUG,
5208 		    "necp_update_all_clients_callout running for coalesced %u updates",
5209 		    count);
5210 	}
5211 
5212 	struct _necp_flow_defunct_list defunct_list;
5213 	LIST_INIT(&defunct_list);
5214 
5215 	NECP_FD_LIST_LOCK_SHARED();
5216 
5217 	LIST_FOREACH(client_fd, &necp_fd_list, chain) {
5218 		proc_t proc = proc_find(client_fd->proc_pid);
5219 		if (proc == PROC_NULL) {
5220 			continue;
5221 		}
5222 
5223 		// Update all clients on one fd
5224 		NECP_FD_LOCK(client_fd);
5225 		necp_update_client_fd_locked(client_fd, proc, &defunct_list);
5226 		NECP_FD_UNLOCK(client_fd);
5227 
5228 		proc_rele(proc);
5229 		proc = PROC_NULL;
5230 	}
5231 
5232 	// Reset the necp_flow_divert_needed_pids list
5233 	for (int i = 0; i < NECP_MAX_FLOW_DIVERT_NEEDED_PIDS; i++) {
5234 		necp_flow_divert_needed_pids[i] = 0;
5235 	}
5236 
5237 	NECP_FD_LIST_UNLOCK();
5238 
5239 	// Handle the case in which some clients became newly defunct
5240 	necp_process_defunct_list(&defunct_list);
5241 }
5242 
5243 void
necp_update_all_clients(void)5244 necp_update_all_clients(void)
5245 {
5246 	necp_update_all_clients_immediately_if_needed(false);
5247 }
5248 
5249 void
necp_update_all_clients_immediately_if_needed(bool should_update_immediately)5250 necp_update_all_clients_immediately_if_needed(bool should_update_immediately)
5251 {
5252 	if (necp_client_update_tcall == NULL) {
5253 		// Don't try to update clients if the module is not initialized
5254 		return;
5255 	}
5256 
5257 	uint64_t deadline = 0;
5258 	uint64_t leeway = 0;
5259 
5260 	uint32_t timeout_to_use = necp_timeout_microseconds;
5261 	uint32_t leeway_to_use = necp_timeout_leeway_microseconds;
5262 	if (should_update_immediately) {
5263 		timeout_to_use = 1000 * 10; // 10ms
5264 		leeway_to_use = 1000 * 10; // 10ms;
5265 	}
5266 
5267 	clock_interval_to_deadline(timeout_to_use, NSEC_PER_USEC, &deadline);
5268 	clock_interval_to_absolutetime_interval(leeway_to_use, NSEC_PER_USEC, &leeway);
5269 
5270 	NECP_UPDATE_ALL_CLIENTS_LOCK_EXCLUSIVE();
5271 	bool need_cancel = false;
5272 	bool need_schedule = true;
5273 	uint64_t sched_abstime;
5274 
5275 	clock_absolutetime_interval_to_deadline(deadline + leeway, &sched_abstime);
5276 
5277 	/*
5278 	 * Do not push the timer if it is already scheduled
5279 	 */
5280 	if (necp_update_all_clients_sched_abstime != 0) {
5281 		need_schedule = false;
5282 
5283 		if (should_update_immediately) {
5284 			/*
5285 			 * To update immediately we may have to cancel the current timer
5286 			 * if it's scheduled too far out.
5287 			 */
5288 			if (necp_update_all_clients_sched_abstime > sched_abstime) {
5289 				need_cancel = true;
5290 				need_schedule = true;
5291 			}
5292 		}
5293 	}
5294 
5295 	/*
5296 	 * Record the time of the deadline with leeway
5297 	 */
5298 	if (need_schedule) {
5299 		necp_update_all_clients_sched_abstime = sched_abstime;
5300 	}
5301 
5302 	necp_update_all_clients_sched_cnt += 1;
5303 	uint32_t count = necp_update_all_clients_sched_cnt;
5304 	NECP_UPDATE_ALL_CLIENTS_UNLOCK();
5305 
5306 	if (need_schedule) {
5307 		/*
5308 		 * Wait if the thread call is currently executing to make sure the
5309 		 * next update will be delivered to all clients
5310 		 */
5311 		if (need_cancel) {
5312 			(void) thread_call_cancel_wait(necp_client_update_tcall);
5313 		}
5314 
5315 		(void) thread_call_enter_delayed_with_leeway(necp_client_update_tcall, NULL,
5316 		    deadline, leeway, THREAD_CALL_DELAY_LEEWAY);
5317 	}
5318 	if (necp_debug > 0) {
5319 		NECPLOG(LOG_DEBUG,
5320 		    "necp_update_all_clients immediate %s update %u",
5321 		    should_update_immediately ? "true" : "false", count);
5322 	}
5323 }
5324 
5325 bool
necp_set_client_as_background(proc_t proc,struct fileproc * fp,bool background)5326 necp_set_client_as_background(proc_t proc,
5327     struct fileproc *fp,
5328     bool background)
5329 {
5330 	if (proc == PROC_NULL) {
5331 		NECPLOG0(LOG_ERR, "NULL proc");
5332 		return FALSE;
5333 	}
5334 
5335 	if (fp == NULL) {
5336 		NECPLOG0(LOG_ERR, "NULL fp");
5337 		return FALSE;
5338 	}
5339 
5340 	struct necp_fd_data *client_fd = (struct necp_fd_data *)fp_get_data(fp);
5341 	if (client_fd == NULL) {
5342 		NECPLOG0(LOG_ERR, "Could not find client structure for backgrounded client");
5343 		return FALSE;
5344 	}
5345 
5346 	if (client_fd->necp_fd_type != necp_fd_type_client) {
5347 		// Not a client fd, ignore
5348 		NECPLOG0(LOG_ERR, "Not a client fd, ignore");
5349 		return FALSE;
5350 	}
5351 
5352 	client_fd->background = background;
5353 
5354 	return TRUE;
5355 }
5356 
5357 void
necp_fd_memstatus(proc_t proc,uint32_t status,struct necp_fd_data * client_fd)5358 necp_fd_memstatus(proc_t proc, uint32_t status,
5359     struct necp_fd_data *client_fd)
5360 {
5361 #pragma unused(proc, status, client_fd)
5362 	ASSERT(proc != PROC_NULL);
5363 	ASSERT(client_fd != NULL);
5364 
5365 	// Nothing to reap for the process or client for now,
5366 	// but this is where we would trigger that in future.
5367 }
5368 
5369 void
necp_fd_defunct(proc_t proc,struct necp_fd_data * client_fd)5370 necp_fd_defunct(proc_t proc, struct necp_fd_data *client_fd)
5371 {
5372 	struct _necp_flow_defunct_list defunct_list;
5373 
5374 	ASSERT(proc != PROC_NULL);
5375 	ASSERT(client_fd != NULL);
5376 
5377 	if (client_fd->necp_fd_type != necp_fd_type_client) {
5378 		// Not a client fd, ignore
5379 		return;
5380 	}
5381 
5382 	// Our local temporary list
5383 	LIST_INIT(&defunct_list);
5384 
5385 	// Need to hold lock so ntstats defunct the same set of clients
5386 	NECP_FD_LOCK(client_fd);
5387 #if SKYWALK
5388 	// Shut down statistics
5389 	nstats_userland_stats_defunct_for_process(proc_getpid(proc));
5390 #endif /* SKYWALK */
5391 	necp_defunct_client_fd_locked(client_fd, &defunct_list, proc);
5392 	NECP_FD_UNLOCK(client_fd);
5393 
5394 	necp_process_defunct_list(&defunct_list);
5395 }
5396 
5397 void
necp_client_request_in_process_flow_divert(pid_t pid)5398 necp_client_request_in_process_flow_divert(pid_t pid)
5399 {
5400 	if (pid == 0) {
5401 		return;
5402 	}
5403 
5404 	// Add to the list of pids that should get an update. These will
5405 	// get picked up on the next thread call to update client paths.
5406 	NECP_FD_LIST_LOCK_SHARED();
5407 	for (int i = 0; i < NECP_MAX_FLOW_DIVERT_NEEDED_PIDS; i++) {
5408 		if (necp_flow_divert_needed_pids[i] == 0) {
5409 			necp_flow_divert_needed_pids[i] = pid;
5410 			break;
5411 		}
5412 	}
5413 	NECP_FD_LIST_UNLOCK();
5414 }
5415 
5416 static void
necp_client_remove_agent_from_result(struct necp_client * client,uuid_t netagent_uuid)5417 necp_client_remove_agent_from_result(struct necp_client *client, uuid_t netagent_uuid)
5418 {
5419 	size_t offset = 0;
5420 
5421 	u_int8_t *result_buffer = client->result;
5422 	while ((offset + sizeof(struct necp_tlv_header)) <= client->result_length) {
5423 		u_int8_t type = necp_buffer_get_tlv_type(result_buffer, client->result_length, offset);
5424 		u_int32_t length = necp_buffer_get_tlv_length(result_buffer, client->result_length, offset);
5425 
5426 		size_t tlv_total_length = (sizeof(struct necp_tlv_header) + length);
5427 		if (type == NECP_CLIENT_RESULT_NETAGENT &&
5428 		    length == sizeof(struct necp_client_result_netagent) &&
5429 		    (offset + tlv_total_length) <= client->result_length) {
5430 			struct necp_client_result_netagent *value = ((struct necp_client_result_netagent *)(void *)
5431 			    necp_buffer_get_tlv_value(result_buffer, client->result_length, offset, NULL));
5432 			if (uuid_compare(value->netagent_uuid, netagent_uuid) == 0) {
5433 				// Found a netagent to remove
5434 				// Shift bytes down to remove the tlv, and adjust total length
5435 				// Don't adjust the current offset
5436 				memmove(result_buffer + offset,
5437 				    result_buffer + offset + tlv_total_length,
5438 				    client->result_length - (offset + tlv_total_length));
5439 				client->result_length -= tlv_total_length;
5440 				memset(result_buffer + client->result_length, 0, sizeof(client->result) - client->result_length);
5441 				continue;
5442 			}
5443 		}
5444 
5445 		offset += tlv_total_length;
5446 	}
5447 }
5448 
5449 void
necp_force_update_client(uuid_t client_id,uuid_t remove_netagent_uuid,u_int32_t agent_generation)5450 necp_force_update_client(uuid_t client_id, uuid_t remove_netagent_uuid, u_int32_t agent_generation)
5451 {
5452 	struct necp_fd_data *client_fd = NULL;
5453 
5454 	NECP_FD_LIST_LOCK_SHARED();
5455 
5456 	LIST_FOREACH(client_fd, &necp_fd_list, chain) {
5457 		bool updated_result = FALSE;
5458 		NECP_FD_LOCK(client_fd);
5459 		struct necp_client *client = necp_client_fd_find_client_and_lock(client_fd, client_id);
5460 		if (client != NULL) {
5461 			client->failed_trigger_agent.generation = agent_generation;
5462 			uuid_copy(client->failed_trigger_agent.netagent_uuid, remove_netagent_uuid);
5463 			if (!uuid_is_null(remove_netagent_uuid)) {
5464 				necp_client_remove_agent_from_result(client, remove_netagent_uuid);
5465 			}
5466 			client->result_read = FALSE;
5467 			// Found the client, break
5468 			updated_result = TRUE;
5469 			NECP_CLIENT_UNLOCK(client);
5470 		}
5471 		if (updated_result) {
5472 			necp_fd_notify(client_fd, true);
5473 		}
5474 		NECP_FD_UNLOCK(client_fd);
5475 		if (updated_result) {
5476 			// Found the client, break
5477 			break;
5478 		}
5479 	}
5480 
5481 	NECP_FD_LIST_UNLOCK();
5482 }
5483 
5484 #if SKYWALK
5485 void
necp_client_early_close(uuid_t client_id)5486 necp_client_early_close(uuid_t client_id)
5487 {
5488 	NECP_CLIENT_TREE_LOCK_SHARED();
5489 
5490 	struct necp_client *client = necp_find_client_and_lock(client_id);
5491 	if (client != NULL) {
5492 		struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
5493 		if (flow_registration != NULL) {
5494 			// Found the right client and flow, mark the stats as over
5495 			if (flow_registration->stats_handler_context != NULL) {
5496 				ntstat_userland_stats_event(flow_registration->stats_handler_context,
5497 				    NECP_CLIENT_STATISTICS_EVENT_TIME_WAIT);
5498 			}
5499 		}
5500 		NECP_CLIENT_UNLOCK(client);
5501 	}
5502 
5503 	NECP_CLIENT_TREE_UNLOCK();
5504 }
5505 #endif /* SKYWALK */
5506 
5507 /// Interface matching
5508 
5509 #define NECP_PARSED_PARAMETERS_INTERESTING_IFNET_FIELDS (NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR |                              \
5510 	                                                                                                         NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IF |                   \
5511 	                                                                                                         NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE |                 \
5512 	                                                                                                         NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IFTYPE |               \
5513 	                                                                                                         NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT |                  \
5514 	                                                                                                         NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT |                \
5515 	                                                                                                         NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT |                 \
5516 	                                                                                                         NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT |                   \
5517 	                                                                                                         NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE |             \
5518 	                                                                                                         NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT_TYPE |   \
5519 	                                                                                                         NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE |    \
5520 	                                                                                                         NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE)
5521 
5522 #define NECP_PARSED_PARAMETERS_SCOPED_FIELDS (NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR |                 \
5523 	                                                                                  NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE |                \
5524 	                                                                                  NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT |         \
5525 	                                                                                  NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT |                \
5526 	                                                                                  NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE |    \
5527 	                                                                                  NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE)
5528 
5529 #define NECP_PARSED_PARAMETERS_SCOPED_IFNET_FIELDS (NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR |           \
5530 	                                                                                                NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE)
5531 
5532 #define NECP_PARSED_PARAMETERS_PREFERRED_FIELDS (NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT |                 \
5533 	                                                                                         NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT |                   \
5534 	                                                                                         NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE |    \
5535 	                                                                                         NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE)
5536 
5537 static bool
necp_ifnet_matches_type(struct ifnet * ifp,u_int8_t interface_type,bool check_delegates)5538 necp_ifnet_matches_type(struct ifnet *ifp, u_int8_t interface_type, bool check_delegates)
5539 {
5540 	struct ifnet *check_ifp = ifp;
5541 	while (check_ifp) {
5542 		if (if_functional_type(check_ifp, TRUE) == interface_type) {
5543 			return TRUE;
5544 		}
5545 		if (!check_delegates) {
5546 			break;
5547 		}
5548 		check_ifp = check_ifp->if_delegated.ifp;
5549 	}
5550 	return FALSE;
5551 }
5552 
5553 static bool
necp_ifnet_matches_name(struct ifnet * ifp,const char * __sized_by (IFXNAMSIZ)interface_name,bool check_delegates)5554 necp_ifnet_matches_name(struct ifnet *ifp, const char * __sized_by(IFXNAMSIZ)interface_name, bool check_delegates)
5555 {
5556 	struct ifnet *check_ifp = ifp;
5557 	while (check_ifp) {
5558 		if (strlcmp(interface_name, check_ifp->if_xname, IFXNAMSIZ) == 0) {
5559 			return TRUE;
5560 		}
5561 		if (!check_delegates) {
5562 			break;
5563 		}
5564 		check_ifp = check_ifp->if_delegated.ifp;
5565 	}
5566 	return FALSE;
5567 }
5568 
5569 static bool
necp_ifnet_matches_agent(struct ifnet * ifp,uuid_t * agent_uuid,bool check_delegates)5570 necp_ifnet_matches_agent(struct ifnet *ifp, uuid_t *agent_uuid, bool check_delegates)
5571 {
5572 	struct ifnet *check_ifp = ifp;
5573 
5574 	while (check_ifp != NULL) {
5575 		ifnet_lock_shared(check_ifp);
5576 		if (check_ifp->if_agentids != NULL) {
5577 			for (u_int32_t index = 0; index < check_ifp->if_agentcount; index++) {
5578 				if (uuid_compare(check_ifp->if_agentids[index], *agent_uuid) == 0) {
5579 					ifnet_lock_done(check_ifp);
5580 					return TRUE;
5581 				}
5582 			}
5583 		}
5584 		ifnet_lock_done(check_ifp);
5585 
5586 		if (!check_delegates) {
5587 			break;
5588 		}
5589 		check_ifp = check_ifp->if_delegated.ifp;
5590 	}
5591 	return FALSE;
5592 }
5593 
5594 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)5595 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)
5596 {
5597 	struct ifnet *check_ifp = ifp;
5598 
5599 	while (check_ifp != NULL) {
5600 		ifnet_lock_shared(check_ifp);
5601 		if (check_ifp->if_agentids != NULL) {
5602 			for (u_int32_t index = 0; index < check_ifp->if_agentcount; index++) {
5603 				if (uuid_is_null(check_ifp->if_agentids[index])) {
5604 					continue;
5605 				}
5606 
5607 				char if_agent_domain[NETAGENT_DOMAINSIZE] = { 0 };
5608 				char if_agent_type[NETAGENT_TYPESIZE] = { 0 };
5609 
5610 				if (netagent_get_agent_domain_and_type(check_ifp->if_agentids[index], if_agent_domain, if_agent_type)) {
5611 					if (necp_agent_types_match(agent_domain, agent_type, if_agent_domain, if_agent_type)) {
5612 						ifnet_lock_done(check_ifp);
5613 						return TRUE;
5614 					}
5615 				}
5616 			}
5617 		}
5618 		ifnet_lock_done(check_ifp);
5619 
5620 		if (!check_delegates) {
5621 			break;
5622 		}
5623 		check_ifp = check_ifp->if_delegated.ifp;
5624 	}
5625 	return FALSE;
5626 }
5627 
5628 static bool
necp_ifnet_matches_local_address(struct ifnet * ifp,struct sockaddr * sa)5629 necp_ifnet_matches_local_address(struct ifnet *ifp, struct sockaddr *sa)
5630 {
5631 	struct ifaddr *ifa = NULL;
5632 	bool matched_local_address = FALSE;
5633 
5634 	// Transform sa into the ifaddr form
5635 	// IPv6 Scope IDs are always embedded in the ifaddr list
5636 	struct sockaddr_storage address;
5637 	u_int ifscope = IFSCOPE_NONE;
5638 	(void)sa_copy(sa, &address, &ifscope);
5639 	SIN(&address)->sin_port = 0;
5640 	if (address.ss_family == AF_INET6) {
5641 		if (in6_embedded_scope ||
5642 		    !IN6_IS_SCOPE_EMBED(&SIN6(&address)->sin6_addr)) {
5643 			SIN6(&address)->sin6_scope_id = 0;
5644 		}
5645 	}
5646 
5647 	ifa = ifa_ifwithaddr_scoped_locked(SA(&address), ifp->if_index);
5648 	matched_local_address = (ifa != NULL);
5649 
5650 	if (ifa) {
5651 		ifaddr_release(ifa);
5652 	}
5653 
5654 	return matched_local_address;
5655 }
5656 
5657 static bool
necp_interface_type_should_match_unranked_interfaces(u_int8_t interface_type)5658 necp_interface_type_should_match_unranked_interfaces(u_int8_t interface_type)
5659 {
5660 	switch (interface_type) {
5661 	// These are the interface types we allow a client to request even if the matching
5662 	// interface isn't currently eligible to be primary (has default route, dns, etc)
5663 	case IFRTYPE_FUNCTIONAL_WIFI_AWDL:
5664 	case IFRTYPE_FUNCTIONAL_INTCOPROC:
5665 	case IFRTYPE_FUNCTIONAL_COMPANIONLINK:
5666 		return true;
5667 	default:
5668 		break;
5669 	}
5670 	return false;
5671 }
5672 
5673 #define NECP_IFP_IS_ON_ORDERED_LIST(_ifp) ((_ifp)->if_ordered_link.tqe_next != NULL || (_ifp)->if_ordered_link.tqe_prev != NULL)
5674 
5675 // Secondary interface flag indicates that the interface is being
5676 // used for multipath or a listener as an extra path
5677 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)5678 necp_ifnet_matches_parameters(struct ifnet *ifp,
5679     struct necp_client_parsed_parameters *parsed_parameters,
5680     u_int32_t override_flags,
5681     u_int32_t *preferred_count,
5682     bool secondary_interface,
5683     bool require_scoped_field)
5684 {
5685 	bool matched_some_scoped_field = FALSE;
5686 
5687 	if (preferred_count) {
5688 		*preferred_count = 0;
5689 	}
5690 
5691 	if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF) {
5692 		if (parsed_parameters->required_interface_index != ifp->if_index) {
5693 			return FALSE;
5694 		}
5695 	}
5696 #if SKYWALK
5697 	else {
5698 		if (ifnet_is_low_latency(ifp)) {
5699 			return FALSE;
5700 		}
5701 	}
5702 #endif /* SKYWALK */
5703 
5704 	if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR) {
5705 		if (!necp_ifnet_matches_local_address(ifp, SA(&parsed_parameters->local_addr.sa))) {
5706 			return FALSE;
5707 		}
5708 		if (require_scoped_field) {
5709 			matched_some_scoped_field = TRUE;
5710 		}
5711 	}
5712 
5713 	if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) {
5714 		if (override_flags != 0) {
5715 			if ((override_flags & NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_EXPENSIVE) &&
5716 			    IFNET_IS_EXPENSIVE(ifp)) {
5717 				return FALSE;
5718 			}
5719 			if ((override_flags & NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_CONSTRAINED) &&
5720 			    IFNET_IS_CONSTRAINED(ifp)) {
5721 				return FALSE;
5722 			}
5723 			if (!(override_flags & NECP_CLIENT_PARAMETER_FLAG_ALLOW_ULTRA_CONSTRAINED) &&
5724 			    IFNET_IS_ULTRA_CONSTRAINED(ifp)) {
5725 				return FALSE;
5726 			}
5727 		} else {
5728 			if ((parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_EXPENSIVE) &&
5729 			    IFNET_IS_EXPENSIVE(ifp)) {
5730 				return FALSE;
5731 			}
5732 			if ((parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_CONSTRAINED) &&
5733 			    IFNET_IS_CONSTRAINED(ifp)) {
5734 				return FALSE;
5735 			}
5736 			if (!(parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_ALLOW_ULTRA_CONSTRAINED) &&
5737 			    IFNET_IS_ULTRA_CONSTRAINED(ifp)) {
5738 				return FALSE;
5739 			}
5740 		}
5741 	}
5742 
5743 	if ((!secondary_interface || // Enforce interface type if this is the primary interface
5744 	    !(parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) ||      // or if there are no flags
5745 	    !(parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_ONLY_PRIMARY_REQUIRES_TYPE)) &&      // or if the flags don't give an exception
5746 	    (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE) &&
5747 	    !necp_ifnet_matches_type(ifp, parsed_parameters->required_interface_type, FALSE)) {
5748 		return FALSE;
5749 	}
5750 
5751 	if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE) {
5752 		if (require_scoped_field) {
5753 			matched_some_scoped_field = TRUE;
5754 		}
5755 	}
5756 
5757 	if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IFTYPE) {
5758 		for (int i = 0; i < NECP_MAX_INTERFACE_PARAMETERS; i++) {
5759 			if (parsed_parameters->prohibited_interface_types[i] == 0) {
5760 				break;
5761 			}
5762 
5763 			if (necp_ifnet_matches_type(ifp, parsed_parameters->prohibited_interface_types[i], TRUE)) {
5764 				return FALSE;
5765 			}
5766 		}
5767 	}
5768 
5769 	if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IF) {
5770 		for (int i = 0; i < NECP_MAX_INTERFACE_PARAMETERS; i++) {
5771 			if (strbuflen(parsed_parameters->prohibited_interfaces[i], sizeof(parsed_parameters->prohibited_interfaces[i])) == 0) {
5772 				break;
5773 			}
5774 
5775 			if (necp_ifnet_matches_name(ifp, parsed_parameters->prohibited_interfaces[i], TRUE)) {
5776 				return FALSE;
5777 			}
5778 		}
5779 	}
5780 
5781 	if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT) {
5782 		for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5783 			if (uuid_is_null(parsed_parameters->required_netagents[i])) {
5784 				break;
5785 			}
5786 
5787 			if (!necp_ifnet_matches_agent(ifp, &parsed_parameters->required_netagents[i], FALSE)) {
5788 				return FALSE;
5789 			}
5790 
5791 			if (require_scoped_field) {
5792 				matched_some_scoped_field = TRUE;
5793 			}
5794 		}
5795 	}
5796 
5797 	if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT) {
5798 		for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5799 			if (uuid_is_null(parsed_parameters->prohibited_netagents[i])) {
5800 				break;
5801 			}
5802 
5803 			if (necp_ifnet_matches_agent(ifp, &parsed_parameters->prohibited_netagents[i], TRUE)) {
5804 				return FALSE;
5805 			}
5806 		}
5807 	}
5808 
5809 	if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE) {
5810 		for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5811 			if (strbuflen(parsed_parameters->required_netagent_types[i].netagent_domain, sizeof(parsed_parameters->required_netagent_types[i].netagent_domain)) == 0 &&
5812 			    strbuflen(parsed_parameters->required_netagent_types[i].netagent_type, sizeof(parsed_parameters->required_netagent_types[i].netagent_type)) == 0) {
5813 				break;
5814 			}
5815 
5816 			if (!necp_ifnet_matches_agent_type(ifp, parsed_parameters->required_netagent_types[i].netagent_domain, parsed_parameters->required_netagent_types[i].netagent_type, FALSE)) {
5817 				return FALSE;
5818 			}
5819 
5820 			if (require_scoped_field) {
5821 				matched_some_scoped_field = TRUE;
5822 			}
5823 		}
5824 	}
5825 
5826 	if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT_TYPE) {
5827 		for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5828 			if (strbuflen(parsed_parameters->prohibited_netagent_types[i].netagent_domain, sizeof(parsed_parameters->prohibited_netagent_types[i].netagent_domain)) == 0 &&
5829 			    strbuflen(parsed_parameters->prohibited_netagent_types[i].netagent_type, sizeof(parsed_parameters->prohibited_netagent_types[i].netagent_type)) == 0) {
5830 				break;
5831 			}
5832 
5833 			if (necp_ifnet_matches_agent_type(ifp, parsed_parameters->prohibited_netagent_types[i].netagent_domain, parsed_parameters->prohibited_netagent_types[i].netagent_type, TRUE)) {
5834 				return FALSE;
5835 			}
5836 		}
5837 	}
5838 
5839 	// Checked preferred properties
5840 	if (preferred_count) {
5841 		if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT) {
5842 			for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5843 				if (uuid_is_null(parsed_parameters->preferred_netagents[i])) {
5844 					break;
5845 				}
5846 
5847 				if (necp_ifnet_matches_agent(ifp, &parsed_parameters->preferred_netagents[i], TRUE)) {
5848 					(*preferred_count)++;
5849 					if (require_scoped_field) {
5850 						matched_some_scoped_field = TRUE;
5851 					}
5852 				}
5853 			}
5854 		}
5855 
5856 		if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE) {
5857 			for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5858 				if (strbuflen(parsed_parameters->preferred_netagent_types[i].netagent_domain, sizeof(parsed_parameters->preferred_netagent_types[i].netagent_domain)) == 0 &&
5859 				    strbuflen(parsed_parameters->preferred_netagent_types[i].netagent_type, sizeof(parsed_parameters->preferred_netagent_types[i].netagent_type)) == 0) {
5860 					break;
5861 				}
5862 
5863 				if (necp_ifnet_matches_agent_type(ifp, parsed_parameters->preferred_netagent_types[i].netagent_domain, parsed_parameters->preferred_netagent_types[i].netagent_type, TRUE)) {
5864 					(*preferred_count)++;
5865 					if (require_scoped_field) {
5866 						matched_some_scoped_field = TRUE;
5867 					}
5868 				}
5869 			}
5870 		}
5871 
5872 		if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT) {
5873 			for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5874 				if (uuid_is_null(parsed_parameters->avoided_netagents[i])) {
5875 					break;
5876 				}
5877 
5878 				if (!necp_ifnet_matches_agent(ifp, &parsed_parameters->avoided_netagents[i], TRUE)) {
5879 					(*preferred_count)++;
5880 				}
5881 			}
5882 		}
5883 
5884 		if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE) {
5885 			for (int i = 0; i < NECP_MAX_AGENT_PARAMETERS; i++) {
5886 				if (strbuflen(parsed_parameters->avoided_netagent_types[i].netagent_domain, sizeof(parsed_parameters->avoided_netagent_types[i].netagent_domain)) == 0 &&
5887 				    strbuflen(parsed_parameters->avoided_netagent_types[i].netagent_type, sizeof(parsed_parameters->avoided_netagent_types[i].netagent_type)) == 0) {
5888 					break;
5889 				}
5890 
5891 				if (!necp_ifnet_matches_agent_type(ifp, parsed_parameters->avoided_netagent_types[i].netagent_domain,
5892 				    parsed_parameters->avoided_netagent_types[i].netagent_type, TRUE)) {
5893 					(*preferred_count)++;
5894 				}
5895 			}
5896 		}
5897 	}
5898 
5899 	if (require_scoped_field) {
5900 		return matched_some_scoped_field;
5901 	}
5902 
5903 	return TRUE;
5904 }
5905 
5906 static bool
necp_find_matching_interface_index(struct necp_client_parsed_parameters * parsed_parameters,u_int * return_ifindex,bool * validate_agents)5907 necp_find_matching_interface_index(struct necp_client_parsed_parameters *parsed_parameters,
5908     u_int *return_ifindex, bool *validate_agents)
5909 {
5910 	struct ifnet *ifp = NULL;
5911 	u_int32_t best_preferred_count = 0;
5912 	bool has_preferred_fields = FALSE;
5913 	*return_ifindex = 0;
5914 
5915 	if (parsed_parameters->required_interface_index != 0) {
5916 		*return_ifindex = parsed_parameters->required_interface_index;
5917 		return TRUE;
5918 	}
5919 
5920 	// Check and save off flags
5921 	u_int32_t flags = 0;
5922 	bool has_prohibit_flags = FALSE;
5923 	if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) {
5924 		flags = parsed_parameters->flags;
5925 		has_prohibit_flags = (parsed_parameters->flags &
5926 		    (NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_EXPENSIVE |
5927 		    NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_CONSTRAINED));
5928 	}
5929 
5930 	if (!(parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_INTERESTING_IFNET_FIELDS) &&
5931 	    !has_prohibit_flags) {
5932 		return TRUE;
5933 	}
5934 
5935 	has_preferred_fields = (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_PREFERRED_FIELDS);
5936 
5937 	// We have interesting parameters to parse and find a matching interface
5938 	ifnet_head_lock_shared();
5939 
5940 	if (!(parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_SCOPED_FIELDS) &&
5941 	    !has_preferred_fields) {
5942 		// We do have fields to match, but they are only prohibitory
5943 		// If the first interface in the list matches, or there are no ordered interfaces, we don't need to scope
5944 		ifp = TAILQ_FIRST(&ifnet_ordered_head);
5945 		if (ifp == NULL || necp_ifnet_matches_parameters(ifp, parsed_parameters, 0, NULL, false, false)) {
5946 			// Don't set return_ifindex, so the client doesn't need to scope
5947 			ifnet_head_done();
5948 			return TRUE;
5949 		}
5950 
5951 		if (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REMOTE_ADDR &&
5952 		    parsed_parameters->remote_addr.sin6.sin6_family == AF_INET6 &&
5953 		    parsed_parameters->remote_addr.sin6.sin6_scope_id != IFSCOPE_NONE &&
5954 		    parsed_parameters->remote_addr.sin6.sin6_scope_id <= (u_int32_t)if_index) {
5955 			ifp = ifindex2ifnet[parsed_parameters->remote_addr.sin6.sin6_scope_id];
5956 			if (ifp != NULL && necp_ifnet_matches_parameters(ifp, parsed_parameters, 0, NULL, false, false)) {
5957 				// Don't set return_ifindex, so the client doesn't need to scope since the v6 scope ID will
5958 				// already route to the correct interface
5959 				ifnet_head_done();
5960 				return TRUE;
5961 			}
5962 		}
5963 	}
5964 
5965 	// First check the ordered interface list
5966 	TAILQ_FOREACH(ifp, &ifnet_ordered_head, if_ordered_link) {
5967 		u_int32_t preferred_count = 0;
5968 		if (necp_ifnet_matches_parameters(ifp, parsed_parameters, flags, &preferred_count, false, false)) {
5969 			if (preferred_count > best_preferred_count ||
5970 			    *return_ifindex == 0) {
5971 				// Everything matched, and is most preferred. Return this interface.
5972 				*return_ifindex = ifp->if_index;
5973 				best_preferred_count = preferred_count;
5974 
5975 				if (!has_preferred_fields) {
5976 					break;
5977 				}
5978 			}
5979 		}
5980 
5981 		if (has_prohibit_flags &&
5982 		    ifp == TAILQ_FIRST(&ifnet_ordered_head)) {
5983 			// This was the first interface. From here on, if the
5984 			// client prohibited either expensive or constrained,
5985 			// don't allow either as a secondary interface option.
5986 			flags |= (NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_EXPENSIVE |
5987 			    NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_CONSTRAINED);
5988 		}
5989 	}
5990 
5991 	bool is_listener = ((parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) &&
5992 	    (parsed_parameters->flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER));
5993 
5994 	// Then check the remaining interfaces
5995 	if ((parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_SCOPED_FIELDS) &&
5996 	    ((!(parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE)) ||
5997 	    necp_interface_type_should_match_unranked_interfaces(parsed_parameters->required_interface_type) ||
5998 	    (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR) ||
5999 	    is_listener) &&
6000 	    (*return_ifindex == 0 || has_preferred_fields)) {
6001 		TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
6002 			u_int32_t preferred_count = 0;
6003 			if (NECP_IFP_IS_ON_ORDERED_LIST(ifp)) {
6004 				// This interface was in the ordered list, skip
6005 				continue;
6006 			}
6007 			if (necp_ifnet_matches_parameters(ifp, parsed_parameters, flags, &preferred_count, false, true)) {
6008 				if (preferred_count > best_preferred_count ||
6009 				    *return_ifindex == 0) {
6010 					// Everything matched, and is most preferred. Return this interface.
6011 					*return_ifindex = ifp->if_index;
6012 					best_preferred_count = preferred_count;
6013 
6014 					if (!has_preferred_fields) {
6015 						break;
6016 					}
6017 				}
6018 			}
6019 		}
6020 	}
6021 
6022 	ifnet_head_done();
6023 
6024 	if (has_preferred_fields && best_preferred_count == 0 &&
6025 	    ((parsed_parameters->valid_fields & (NECP_PARSED_PARAMETERS_SCOPED_FIELDS | NECP_PARSED_PARAMETERS_PREFERRED_FIELDS)) ==
6026 	    (parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_PREFERRED_FIELDS))) {
6027 		// If only has preferred ifnet fields, and nothing was found, clear the interface index and return TRUE
6028 		*return_ifindex = 0;
6029 		return TRUE;
6030 	}
6031 
6032 	if (*return_ifindex == 0 &&
6033 	    !(parsed_parameters->valid_fields & NECP_PARSED_PARAMETERS_SCOPED_IFNET_FIELDS)) {
6034 		// Has required fields, but not including specific interface fields. Pass for now, and check
6035 		// to see if agents are satisfied by policy.
6036 		*validate_agents = TRUE;
6037 		return TRUE;
6038 	}
6039 
6040 	return *return_ifindex != 0;
6041 }
6042 
6043 void
necp_copy_inp_domain_info(struct inpcb * inp,struct socket * so,nstat_domain_info * domain_info)6044 necp_copy_inp_domain_info(struct inpcb *inp, struct socket *so, nstat_domain_info *domain_info)
6045 {
6046 	if (inp == NULL || so == NULL || domain_info == NULL) {
6047 		return;
6048 	}
6049 
6050 	necp_lock_socket_attributes();
6051 
6052 	domain_info->is_silent = !!(so->so_flags1 & SOF1_DOMAIN_INFO_SILENT);
6053 	if (!domain_info->is_silent) {
6054 		domain_info->is_tracker = !!(so->so_flags1 & SOF1_KNOWN_TRACKER);
6055 		domain_info->is_non_app_initiated = !!(so->so_flags1 & SOF1_TRACKER_NON_APP_INITIATED);
6056 		if (domain_info->is_tracker &&
6057 		    inp->inp_necp_attributes.inp_tracker_domain != NULL) {
6058 			strlcpy(domain_info->domain_name, inp->inp_necp_attributes.inp_tracker_domain,
6059 			    sizeof(domain_info->domain_name));
6060 		} else if (inp->inp_necp_attributes.inp_domain != NULL) {
6061 			strlcpy(domain_info->domain_name, inp->inp_necp_attributes.inp_domain,
6062 			    sizeof(domain_info->domain_name));
6063 		}
6064 		if (inp->inp_necp_attributes.inp_domain_owner != NULL) {
6065 			strlcpy(domain_info->domain_owner, inp->inp_necp_attributes.inp_domain_owner,
6066 			    sizeof(domain_info->domain_owner));
6067 		}
6068 		if (inp->inp_necp_attributes.inp_domain_context != NULL) {
6069 			strlcpy(domain_info->domain_tracker_ctxt, inp->inp_necp_attributes.inp_domain_context,
6070 			    sizeof(domain_info->domain_tracker_ctxt));
6071 		}
6072 	}
6073 
6074 	necp_unlock_socket_attributes();
6075 }
6076 
6077 void
necp_with_inp_domain_name(struct socket * so,void * ctx,void (* with_func)(char * domain_name __null_terminated,void * ctx))6078 necp_with_inp_domain_name(struct socket *so, void *ctx, void (*with_func)(char *domain_name __null_terminated, void *ctx))
6079 {
6080 	struct inpcb *inp = NULL;
6081 
6082 	if (so == NULL || with_func == NULL) {
6083 		return;
6084 	}
6085 
6086 	inp = (struct inpcb *)so->so_pcb;
6087 	if (inp == NULL) {
6088 		return;
6089 	}
6090 
6091 	necp_lock_socket_attributes();
6092 	with_func(inp->inp_necp_attributes.inp_domain, ctx);
6093 	necp_unlock_socket_attributes();
6094 }
6095 
6096 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)6097 necp_find_domain_info_common(struct necp_client *client,
6098     u_int8_t * __sized_by(parameters_size)parameters,
6099     size_t parameters_size,
6100     struct necp_client_flow_registration *flow_registration,    /* For logging purposes only */
6101     nstat_domain_info *domain_info)
6102 {
6103 	if (client == NULL) {
6104 		return 0;
6105 	}
6106 	if (domain_info == NULL) {
6107 		return sizeof(nstat_domain_info);
6108 	}
6109 
6110 	size_t offset = 0;
6111 	u_int32_t flags = 0;
6112 	u_int8_t *tracker_domain = NULL;
6113 	u_int8_t *domain = NULL;
6114 	size_t tracker_domain_length = 0;
6115 	size_t domain_length = 0;
6116 
6117 	NECP_CLIENT_FLOW_LOG(client, flow_registration, "Collecting stats");
6118 
6119 	while ((offset + sizeof(struct necp_tlv_header)) <= parameters_size) {
6120 		u_int8_t type = necp_buffer_get_tlv_type(parameters, parameters_size, offset);
6121 		u_int32_t length = necp_buffer_get_tlv_length(parameters, parameters_size, offset);
6122 
6123 		if (length > (parameters_size - (offset + sizeof(struct necp_tlv_header)))) {
6124 			// If the length is larger than what can fit in the remaining parameters size, bail
6125 			NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
6126 			break;
6127 		}
6128 
6129 		if (length > 0) {
6130 			u_int8_t * __indexable value = necp_buffer_get_tlv_value(parameters, parameters_size, offset, NULL);
6131 			if (value != NULL) {
6132 				switch (type) {
6133 				case NECP_CLIENT_PARAMETER_FLAGS: {
6134 					if (length >= sizeof(u_int32_t)) {
6135 						memcpy(&flags, value, sizeof(u_int32_t));
6136 					}
6137 
6138 					domain_info->is_tracker =
6139 					    !!(flags & NECP_CLIENT_PARAMETER_FLAG_KNOWN_TRACKER);
6140 					domain_info->is_non_app_initiated =
6141 					    !!(flags & NECP_CLIENT_PARAMETER_FLAG_NON_APP_INITIATED);
6142 					domain_info->is_silent =
6143 					    !!(flags & NECP_CLIENT_PARAMETER_FLAG_SILENT);
6144 					break;
6145 				}
6146 				case NECP_CLIENT_PARAMETER_TRACKER_DOMAIN: {
6147 					tracker_domain_length = length;
6148 					tracker_domain = value;
6149 					break;
6150 				}
6151 				case NECP_CLIENT_PARAMETER_DOMAIN: {
6152 					domain_length = length;
6153 					domain = value;
6154 					break;
6155 				}
6156 				case NECP_CLIENT_PARAMETER_DOMAIN_OWNER: {
6157 					size_t length_to_copy = MIN(length, sizeof(domain_info->domain_owner));
6158 					strbufcpy(domain_info->domain_owner, sizeof(domain_info->domain_owner), (const char *)value, length_to_copy);
6159 					break;
6160 				}
6161 				case NECP_CLIENT_PARAMETER_DOMAIN_CONTEXT: {
6162 					size_t length_to_copy = MIN(length, sizeof(domain_info->domain_tracker_ctxt));
6163 					strbufcpy(domain_info->domain_tracker_ctxt, sizeof(domain_info->domain_tracker_ctxt), (const char *)value, length_to_copy);
6164 					break;
6165 				}
6166 				case NECP_CLIENT_PARAMETER_ATTRIBUTED_BUNDLE_IDENTIFIER: {
6167 					size_t length_to_copy = MIN(length, sizeof(domain_info->domain_attributed_bundle_id));
6168 					strbufcpy(domain_info->domain_attributed_bundle_id, sizeof(domain_info->domain_attributed_bundle_id), (const char *)value, length_to_copy);
6169 					break;
6170 				}
6171 				case NECP_CLIENT_PARAMETER_REMOTE_ADDRESS: {
6172 					if (length >= sizeof(struct necp_policy_condition_addr)) {
6173 						struct necp_policy_condition_addr *address_struct = (struct necp_policy_condition_addr *)(void *)value;
6174 						if (necp_client_address_is_valid(&address_struct->address.sa)) {
6175 							domain_info->remote.v6 = address_struct->address.sin6;
6176 						}
6177 					}
6178 					break;
6179 				}
6180 				default: {
6181 					break;
6182 				}
6183 				}
6184 			}
6185 		}
6186 		offset += sizeof(struct necp_tlv_header) + length;
6187 	}
6188 
6189 	if (domain_info->is_silent) {
6190 		memset(domain_info, 0, sizeof(*domain_info));
6191 		domain_info->is_silent = true;
6192 	} else if (domain_info->is_tracker && tracker_domain != NULL && tracker_domain_length > 0) {
6193 		size_t length_to_copy = MIN(tracker_domain_length, sizeof(domain_info->domain_name));
6194 		strbufcpy(domain_info->domain_name, sizeof(domain_info->domain_name), (const char *)tracker_domain, length_to_copy);
6195 	} else if (domain != NULL && domain_length > 0) {
6196 		size_t length_to_copy = MIN(domain_length, sizeof(domain_info->domain_name));
6197 		strbufcpy(domain_info->domain_name, sizeof(domain_info->domain_name), (const char *)domain, length_to_copy);
6198 	}
6199 
6200 	NECP_CLIENT_FLOW_LOG(client, flow_registration,
6201 	    "Collected stats - domain <%s> owner <%s> ctxt <%s> bundle id <%s> "
6202 	    "is_tracker %d is_non_app_initiated %d is_silent %d",
6203 	    domain_info->domain_name,
6204 	    domain_info->domain_owner,
6205 	    domain_info->domain_tracker_ctxt,
6206 	    domain_info->domain_attributed_bundle_id,
6207 	    domain_info->is_tracker,
6208 	    domain_info->is_non_app_initiated,
6209 	    domain_info->is_silent);
6210 
6211 	return sizeof(nstat_domain_info);
6212 }
6213 
6214 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)6215 necp_find_conn_extension_info(nstat_provider_context ctx,
6216     int requested_extension,    /* The extension to be returned */
6217     void * __sized_by(buf_size)buf,  /* If not NULL, the address for extensions to be returned in */
6218     size_t buf_size)            /* The size of the buffer space, typically matching the return from a previous call with a NULL buf pointer */
6219 {
6220 	// Note, the caller has guaranteed that any buffer has been zeroed, there is no need to clear it again
6221 
6222 	if (ctx == NULL) {
6223 		return 0;
6224 	}
6225 	struct necp_client *client = (struct necp_client *)ctx;
6226 	switch (requested_extension) {
6227 	case NSTAT_EXTENDED_UPDATE_TYPE_DOMAIN:
6228 		// This is for completeness.  The intent is that domain information can be extracted at user level from the TLV parameters
6229 		if (buf == NULL) {
6230 			return sizeof(nstat_domain_info);
6231 		}
6232 		if (buf_size < sizeof(nstat_domain_info)) {
6233 			return 0;
6234 		}
6235 		return necp_find_domain_info_common(client, client->parameters, client->parameters_length, NULL, (nstat_domain_info *)buf);
6236 
6237 	case NSTAT_EXTENDED_UPDATE_TYPE_NECP_TLV: {
6238 		size_t parameters_length = client->parameters_length;
6239 		if (buf == NULL) {
6240 			return parameters_length;
6241 		}
6242 		if (buf_size < parameters_length) {
6243 			return 0;
6244 		}
6245 		memcpy(buf, client->parameters, parameters_length);
6246 		return parameters_length;
6247 	}
6248 	case NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_NECP_TLV:
6249 		if (buf == NULL) {
6250 			return (client->original_parameters_source != NULL) ? client->original_parameters_source->parameters_length : 0;
6251 		}
6252 		if ((client->original_parameters_source == NULL) || (buf_size < client->original_parameters_source->parameters_length)) {
6253 			return 0;
6254 		}
6255 		memcpy(buf, client->original_parameters_source->parameters, client->original_parameters_source->parameters_length);
6256 		return client->original_parameters_source->parameters_length;
6257 
6258 	case NSTAT_EXTENDED_UPDATE_TYPE_ORIGINAL_DOMAIN:
6259 		if (buf == NULL) {
6260 			return (client->original_parameters_source != NULL) ? sizeof(nstat_domain_info) : 0;
6261 		}
6262 		if ((buf_size < sizeof(nstat_domain_info)) || (client->original_parameters_source == NULL)) {
6263 			return 0;
6264 		}
6265 		return necp_find_domain_info_common(client, client->original_parameters_source->parameters, client->original_parameters_source->parameters_length,
6266 		           NULL, (nstat_domain_info *)buf);
6267 
6268 	default:
6269 		return 0;
6270 	}
6271 }
6272 
6273 #if SKYWALK
6274 
6275 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)6276 necp_find_extension_info(userland_stats_provider_context *ctx,
6277     int requested_extension,    /* The extension to be returned */
6278     void * __sized_by(buf_size)buf,  /* If not NULL, the address for extensions to be returned in */
6279     size_t buf_size)            /* The size of the buffer space, typically matching the return from a previous call with a NULL buf pointer */
6280 {
6281 	if (ctx == NULL) {
6282 		return 0;
6283 	}
6284 	struct necp_client_flow_registration * __single flow_registration = (struct necp_client_flow_registration *)(void *)ctx;
6285 	struct necp_client *client = flow_registration->client;
6286 
6287 	switch (requested_extension) {
6288 	case NSTAT_EXTENDED_UPDATE_TYPE_DOMAIN:
6289 		if (buf == NULL) {
6290 			return sizeof(nstat_domain_info);
6291 		}
6292 		if (buf_size < sizeof(nstat_domain_info)) {
6293 			return 0;
6294 		}
6295 		return necp_find_domain_info_common(client, client->parameters, client->parameters_length, flow_registration, (nstat_domain_info *)buf);
6296 
6297 	case NSTAT_EXTENDED_UPDATE_TYPE_NECP_TLV:
6298 		if (buf == NULL) {
6299 			return client->parameters_length;
6300 		}
6301 		if (buf_size < client->parameters_length) {
6302 			return 0;
6303 		}
6304 		memcpy(buf, client->parameters, client->parameters_length);
6305 		return client->parameters_length;
6306 
6307 	case NSTAT_EXTENDED_UPDATE_TYPE_FUUID:
6308 		if (buf == NULL) {
6309 			return sizeof(uuid_t);
6310 		}
6311 		if (buf_size < sizeof(uuid_t)) {
6312 			return 0;
6313 		}
6314 		uuid_copy(buf, flow_registration->registration_id);
6315 		return sizeof(uuid_t);
6316 
6317 	case NSTAT_EXTENDED_UPDATE_TYPE_BLUETOOTH_COUNTS: {
6318 		// Retrieve details from the last time the assigned flows were updated
6319 		u_int32_t route_ifindex = IFSCOPE_NONE;
6320 		u_int32_t route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6321 		u_int64_t combined_interface_details = 0;
6322 
6323 		combined_interface_details = os_atomic_load(&flow_registration->last_interface_details, relaxed);
6324 		split_interface_details(combined_interface_details, &route_ifindex, &route_ifflags);
6325 		bool is_companionlink_bluetooth = (route_ifflags & NSTAT_IFNET_IS_COMPANIONLINK_BT);
6326 
6327 		if (buf == NULL) {
6328 			return (is_companionlink_bluetooth ||
6329 			       (route_ifflags & NSTAT_IFNET_PEEREGRESSINTERFACE_IS_CELLULAR)) ? sizeof(nstat_interface_counts):0;
6330 		}
6331 		if (buf_size < sizeof(nstat_interface_counts)) {
6332 			return 0;
6333 		}
6334 
6335 		const struct sk_stats_flow *sf = &flow_registration->nexus_stats->fs_stats;
6336 		if ((sf != NULL) &&
6337 		    (is_companionlink_bluetooth || (route_ifflags & NSTAT_IFNET_PEEREGRESSINTERFACE_IS_CELLULAR))) {
6338 			nstat_interface_counts *bt_counts = (nstat_interface_counts *)buf;
6339 			bt_counts->nstat_rxbytes = sf->sf_ibytes;
6340 			bt_counts->nstat_txbytes = sf->sf_obytes;
6341 			return sizeof(nstat_interface_counts);
6342 		} else {
6343 			return 0;
6344 		}
6345 	}
6346 
6347 	default:
6348 		return 0;
6349 	}
6350 }
6351 
6352 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)6353 necp_find_netstat_data(struct necp_client *client,
6354     union necp_sockaddr_union *remote,
6355     pid_t *effective_pid,
6356     uid_t *uid,
6357     uuid_t euuid,
6358     uid_t *persona_id,
6359     u_int32_t *traffic_class,
6360     u_int8_t *fallback_mode)
6361 {
6362 	bool have_set_euuid = false;
6363 	size_t offset = 0;
6364 	u_int8_t *parameters;
6365 	u_int32_t parameters_size;
6366 
6367 	parameters = client->parameters;
6368 	parameters_size = (u_int32_t)client->parameters_length;
6369 
6370 	while ((offset + sizeof(struct necp_tlv_header)) <= parameters_size) {
6371 		u_int8_t type = necp_buffer_get_tlv_type(parameters, parameters_size, offset);
6372 		u_int32_t length = necp_buffer_get_tlv_length(parameters, parameters_size, offset);
6373 
6374 		if (length > (parameters_size - (offset + sizeof(struct necp_tlv_header)))) {
6375 			// If the length is larger than what can fit in the remaining parameters size, bail
6376 			NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
6377 			break;
6378 		}
6379 
6380 		if (length > 0) {
6381 			u_int8_t * __indexable value = necp_buffer_get_tlv_value(parameters, parameters_size, offset, NULL);
6382 			if (value != NULL) {
6383 				switch (type) {
6384 				case NECP_CLIENT_PARAMETER_APPLICATION: {
6385 					if (length >= sizeof(uuid_t)) {
6386 						uuid_copy(euuid, value);
6387 					}
6388 					break;
6389 				}
6390 				case NECP_CLIENT_PARAMETER_PID: {
6391 					if (length >= sizeof(pid_t)) {
6392 						memcpy(effective_pid, value, sizeof(pid_t));
6393 					}
6394 					break;
6395 				}
6396 				case NECP_CLIENT_PARAMETER_TRAFFIC_CLASS: {
6397 					if (length >= sizeof(u_int32_t)) {
6398 						memcpy(traffic_class, value, sizeof(u_int32_t));
6399 					}
6400 					break;
6401 				}
6402 				case NECP_CLIENT_PARAMETER_FALLBACK_MODE: {
6403 					if (length >= sizeof(u_int8_t)) {
6404 						memcpy(fallback_mode, value, sizeof(u_int8_t));
6405 					}
6406 					break;
6407 				}
6408 				// It is an implementation quirk that the remote address can be found in the necp parameters
6409 				// while the local address must be retrieved from the flowswitch
6410 				case NECP_CLIENT_PARAMETER_REMOTE_ADDRESS: {
6411 					if (length >= sizeof(struct necp_policy_condition_addr)) {
6412 						struct necp_policy_condition_addr *address_struct = (struct necp_policy_condition_addr *)(void *)value;
6413 						if (necp_client_address_is_valid(&address_struct->address.sa)) {
6414 							remote->sin6 = address_struct->address.sin6;
6415 						}
6416 					}
6417 					break;
6418 				}
6419 				case NECP_CLIENT_PARAMETER_APPLICATION_ID: {
6420 					if (length >= sizeof(necp_application_id_t) && uid && persona_id) {
6421 						necp_application_id_t *application_id = (necp_application_id_t *)(void *)value;
6422 						memcpy(uid, &application_id->uid, sizeof(uid_t));
6423 						uuid_copy(euuid, application_id->effective_uuid);
6424 						memcpy(persona_id, &application_id->persona_id, sizeof(uid_t));
6425 						have_set_euuid = true;
6426 					}
6427 					break;
6428 				}
6429 				default: {
6430 					break;
6431 				}
6432 				}
6433 			}
6434 		}
6435 		offset += sizeof(struct necp_tlv_header) + length;
6436 	}
6437 
6438 	if (!have_set_euuid) {
6439 		proc_t proc = proc_find(client->proc_pid);
6440 		if (proc != PROC_NULL) {
6441 			uuid_t responsible_uuid = { 0 };
6442 			proc_getresponsibleuuid(proc, responsible_uuid, sizeof(responsible_uuid));
6443 			proc_rele(proc);
6444 			if (!uuid_is_null(responsible_uuid)) {
6445 				uuid_copy(euuid, responsible_uuid);
6446 			}
6447 		}
6448 	}
6449 }
6450 
6451 static u_int64_t
necp_find_netstat_initial_properties(struct necp_client * client)6452 necp_find_netstat_initial_properties(struct necp_client *client)
6453 {
6454 	size_t offset = 0;
6455 	u_int64_t retval = 0;
6456 	u_int8_t *parameters;
6457 	u_int32_t parameters_size;
6458 
6459 	parameters = client->parameters;
6460 	parameters_size = (u_int32_t)client->parameters_length;
6461 
6462 	while ((offset + sizeof(struct necp_tlv_header)) <= parameters_size) {
6463 		u_int8_t type = necp_buffer_get_tlv_type(parameters, parameters_size, offset);
6464 		u_int32_t length = necp_buffer_get_tlv_length(parameters, parameters_size, offset);
6465 
6466 		if (length > (parameters_size - (offset + sizeof(struct necp_tlv_header)))) {
6467 			// If the length is larger than what can fit in the remaining parameters size, bail
6468 			NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
6469 			break;
6470 		}
6471 
6472 		if (type == NECP_CLIENT_PARAMETER_FLAGS) {
6473 			u_int32_t policy_condition_client_flags;
6474 			u_int8_t * __indexable value = necp_buffer_get_tlv_value(parameters, parameters_size, offset, NULL);
6475 			if ((value != NULL) && (length >= sizeof(policy_condition_client_flags))) {
6476 				memcpy(&policy_condition_client_flags, value, sizeof(policy_condition_client_flags));
6477 				if (policy_condition_client_flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) {
6478 					retval |= NSTAT_SOURCE_IS_LISTENER;
6479 				}
6480 				if (policy_condition_client_flags & NECP_CLIENT_PARAMETER_FLAG_INBOUND) {
6481 					retval |= NSTAT_SOURCE_IS_INBOUND;
6482 				}
6483 			}
6484 			break;
6485 		}
6486 		offset += sizeof(struct necp_tlv_header) + length;
6487 	}
6488 	if (retval == 0) {
6489 		retval = NSTAT_SOURCE_IS_OUTBOUND;
6490 	}
6491 	return retval;
6492 }
6493 
6494 // Called from NetworkStatistics when it wishes to collect latest information for a TCP flow.
6495 // It is a responsibility of NetworkStatistics to have previously zeroed any supplied memory.
6496 static bool
necp_request_tcp_netstats(userland_stats_provider_context * ctx,u_int32_t * ifflagsp,nstat_progress_digest * digestp,nstat_counts * countsp,void * metadatap)6497 necp_request_tcp_netstats(userland_stats_provider_context *ctx,
6498     u_int32_t *ifflagsp,
6499     nstat_progress_digest *digestp,
6500     nstat_counts *countsp,
6501     void *metadatap)
6502 {
6503 	if (ctx == NULL) {
6504 		return false;
6505 	}
6506 
6507 	struct necp_client_flow_registration * __single flow_registration = (struct necp_client_flow_registration *)(void *)ctx;
6508 	struct necp_client *client = flow_registration->client;
6509 	struct necp_all_stats *ustats_kaddr = ((struct necp_all_kstats *)flow_registration->kstats_kaddr)->necp_stats_ustats;
6510 	struct necp_tcp_stats *tcpstats = (struct necp_tcp_stats *)ustats_kaddr;
6511 	ASSERT(tcpstats != NULL);
6512 
6513 	u_int32_t nstat_diagnostic_flags = 0;
6514 
6515 	// Retrieve details from the last time the assigned flows were updated
6516 	u_int32_t route_ifindex = IFSCOPE_NONE;
6517 	u_int32_t route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6518 	u_int64_t combined_interface_details = 0;
6519 
6520 	combined_interface_details = os_atomic_load(&flow_registration->last_interface_details, relaxed);
6521 	split_interface_details(combined_interface_details, &route_ifindex, &route_ifflags);
6522 
6523 	if (route_ifindex == IFSCOPE_NONE) {
6524 		// Mark no interface
6525 		nstat_diagnostic_flags |= NSTAT_IFNET_ROUTE_VALUE_UNOBTAINABLE;
6526 		route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6527 		NECPLOG(LOG_INFO, "req tcp stats, failed to get route details for pid %d curproc %d %s\n",
6528 		    client->proc_pid, proc_pid(current_proc()), proc_best_name(current_proc()));
6529 	}
6530 
6531 	const struct sk_stats_flow *sf = &flow_registration->nexus_stats->fs_stats;
6532 	if (sf == NULL) {
6533 		nstat_diagnostic_flags |= NSTAT_IFNET_FLOWSWITCH_VALUE_UNOBTAINABLE;
6534 		char namebuf[MAXCOMLEN + 1];
6535 		(void) strlcpy(namebuf, "unknown", sizeof(namebuf));
6536 		proc_name(client->proc_pid, namebuf, sizeof(namebuf));
6537 		NECPLOG(LOG_ERR, "req tcp stats, necp_client flow_registration flow_stats missing for pid %d %s curproc %d %s\n",
6538 		    client->proc_pid, namebuf, proc_pid(current_proc()), proc_best_name(current_proc()));
6539 		sf = &ntstat_sk_stats_zero;
6540 	}
6541 
6542 	if (ifflagsp) {
6543 		*ifflagsp = route_ifflags | nstat_diagnostic_flags;
6544 		*ifflagsp |= (sf->sf_flags & SFLOWF_ONLINK) ? NSTAT_IFNET_IS_LOCAL : NSTAT_IFNET_IS_NON_LOCAL;
6545 		if (tcpstats->necp_tcp_extra.flags1 & SOF1_CELLFALLBACK) {
6546 			*ifflagsp |= NSTAT_IFNET_VIA_CELLFALLBACK;
6547 		}
6548 		if ((digestp == NULL) && (countsp == NULL) && (metadatap == NULL)) {
6549 			return true;
6550 		}
6551 	}
6552 
6553 	if (digestp) {
6554 		// The digest is intended to give information that may help give insight into the state of the link
6555 		digestp->rxbytes = tcpstats->necp_tcp_counts.necp_stat_rxbytes;
6556 		digestp->txbytes = tcpstats->necp_tcp_counts.necp_stat_txbytes;
6557 		digestp->rxduplicatebytes = tcpstats->necp_tcp_counts.necp_stat_rxduplicatebytes;
6558 		digestp->rxoutoforderbytes = tcpstats->necp_tcp_counts.necp_stat_rxoutoforderbytes;
6559 		digestp->txretransmit = tcpstats->necp_tcp_counts.necp_stat_txretransmit;
6560 		digestp->ifindex = route_ifindex;
6561 		digestp->state = tcpstats->necp_tcp_extra.state;
6562 		digestp->txunacked = tcpstats->necp_tcp_extra.txunacked;
6563 		digestp->txwindow = tcpstats->necp_tcp_extra.txwindow;
6564 		digestp->connstatus.probe_activated    = tcpstats->necp_tcp_extra.probestatus.probe_activated;
6565 		digestp->connstatus.write_probe_failed = tcpstats->necp_tcp_extra.probestatus.write_probe_failed;
6566 		digestp->connstatus.read_probe_failed  = tcpstats->necp_tcp_extra.probestatus.read_probe_failed;
6567 		digestp->connstatus.conn_probe_failed  = tcpstats->necp_tcp_extra.probestatus.conn_probe_failed;
6568 
6569 		if ((countsp == NULL) && (metadatap == NULL)) {
6570 			return true;
6571 		}
6572 	}
6573 
6574 	if (countsp) {
6575 		countsp->nstat_rxbytes = tcpstats->necp_tcp_counts.necp_stat_rxbytes;
6576 		countsp->nstat_txbytes = tcpstats->necp_tcp_counts.necp_stat_txbytes;
6577 
6578 		countsp->nstat_rxduplicatebytes = tcpstats->necp_tcp_counts.necp_stat_rxduplicatebytes;
6579 		countsp->nstat_rxoutoforderbytes = tcpstats->necp_tcp_counts.necp_stat_rxoutoforderbytes;
6580 		countsp->nstat_txretransmit = tcpstats->necp_tcp_counts.necp_stat_txretransmit;
6581 
6582 		countsp->nstat_min_rtt = tcpstats->necp_tcp_counts.necp_stat_min_rtt;
6583 		countsp->nstat_avg_rtt = tcpstats->necp_tcp_counts.necp_stat_avg_rtt;
6584 		countsp->nstat_var_rtt = tcpstats->necp_tcp_counts.necp_stat_var_rtt;
6585 
6586 		countsp->nstat_connectattempts = tcpstats->necp_tcp_extra.state >= TCPS_SYN_SENT ? 1 : 0;
6587 		countsp->nstat_connectsuccesses = tcpstats->necp_tcp_extra.state >= TCPS_ESTABLISHED ? 1 : 0;
6588 
6589 		// Supplement what the user level has told us with what we know from the flowswitch
6590 		countsp->nstat_rxpackets = sf->sf_ipackets;
6591 		countsp->nstat_txpackets = sf->sf_opackets;
6592 		if (route_ifflags & NSTAT_IFNET_IS_CELLULAR) {
6593 			countsp->nstat_cell_rxbytes = sf->sf_ibytes;
6594 			countsp->nstat_cell_txbytes = sf->sf_obytes;
6595 		} else if (route_ifflags & NSTAT_IFNET_IS_WIFI) {
6596 			countsp->nstat_wifi_rxbytes = sf->sf_ibytes;
6597 			countsp->nstat_wifi_txbytes = sf->sf_obytes;
6598 		} else if (route_ifflags & NSTAT_IFNET_IS_WIRED) {
6599 			countsp->nstat_wired_rxbytes = sf->sf_ibytes;
6600 			countsp->nstat_wired_txbytes = sf->sf_obytes;
6601 		}
6602 	}
6603 
6604 	if (metadatap) {
6605 		nstat_tcp_descriptor *desc = (nstat_tcp_descriptor *)metadatap;
6606 		memset(desc, 0, sizeof(*desc));
6607 
6608 		// Metadata from the flow registration
6609 		uuid_copy(desc->fuuid, flow_registration->registration_id);
6610 
6611 		// Metadata that the necp client should have in TLV format.
6612 		pid_t effective_pid = client->proc_pid;
6613 		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);
6614 		desc->epid = (u_int32_t)effective_pid;
6615 
6616 		// Metadata from the flow registration
6617 		// This needs to revisited if multiple flows are created from one flow registration
6618 		struct necp_client_flow *flow = NULL;
6619 		LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
6620 			memcpy(&desc->local, &flow->local_addr, sizeof(desc->local));
6621 			break;
6622 		}
6623 
6624 		// Metadata from the route
6625 		desc->ifindex = route_ifindex;
6626 		desc->ifnet_properties = route_ifflags | nstat_diagnostic_flags;
6627 		desc->ifnet_properties |= (sf->sf_flags & SFLOWF_ONLINK) ? NSTAT_IFNET_IS_LOCAL : NSTAT_IFNET_IS_NON_LOCAL;
6628 		if (tcpstats->necp_tcp_extra.flags1 & SOF1_CELLFALLBACK) {
6629 			desc->ifnet_properties |= NSTAT_IFNET_VIA_CELLFALLBACK;
6630 		}
6631 
6632 		// Basic metadata from userland
6633 		desc->rcvbufsize = tcpstats->necp_tcp_basic.rcvbufsize;
6634 		desc->rcvbufused = tcpstats->necp_tcp_basic.rcvbufused;
6635 
6636 		// Additional TCP specific data
6637 		desc->sndbufsize = tcpstats->necp_tcp_extra.sndbufsize;
6638 		desc->sndbufused = tcpstats->necp_tcp_extra.sndbufused;
6639 		desc->txunacked = tcpstats->necp_tcp_extra.txunacked;
6640 		desc->txwindow = tcpstats->necp_tcp_extra.txwindow;
6641 		desc->txcwindow = tcpstats->necp_tcp_extra.txcwindow;
6642 		desc->traffic_mgt_flags = tcpstats->necp_tcp_extra.traffic_mgt_flags;
6643 		desc->state = tcpstats->necp_tcp_extra.state;
6644 
6645 		u_int32_t cc_alg_index = tcpstats->necp_tcp_extra.cc_alg_index;
6646 		if (cc_alg_index < TCP_CC_ALGO_COUNT) {
6647 			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));
6648 		} else {
6649 			strlcpy(desc->cc_algo, "unknown", sizeof(desc->cc_algo));
6650 		}
6651 
6652 		desc->connstatus.probe_activated        = tcpstats->necp_tcp_extra.probestatus.probe_activated;
6653 		desc->connstatus.write_probe_failed     = tcpstats->necp_tcp_extra.probestatus.write_probe_failed;
6654 		desc->connstatus.read_probe_failed      = tcpstats->necp_tcp_extra.probestatus.read_probe_failed;
6655 		desc->connstatus.conn_probe_failed      = tcpstats->necp_tcp_extra.probestatus.conn_probe_failed;
6656 
6657 		memcpy(&desc->activity_bitmap, &sf->sf_activity, sizeof(sf->sf_activity));
6658 
6659 		if (NECP_ENABLE_CLIENT_TRACE(NECP_CLIENT_TRACE_LEVEL_FLOW)) {
6660 			uuid_string_t euuid_str = { 0 };
6661 			uuid_unparse(desc->euuid, euuid_str);
6662 			NECPLOG(LOG_NOTICE, "Collected stats - TCP - epid %d uid %d euuid %s persona id %d", desc->epid, desc->uid, euuid_str, desc->persona_id);
6663 		}
6664 	}
6665 
6666 	return true;
6667 }
6668 
6669 // Called from NetworkStatistics when it wishes to collect latest information for a UDP flow.
6670 static bool
necp_request_udp_netstats(userland_stats_provider_context * ctx,u_int32_t * ifflagsp,nstat_progress_digest * digestp,nstat_counts * countsp,void * metadatap)6671 necp_request_udp_netstats(userland_stats_provider_context *ctx,
6672     u_int32_t *ifflagsp,
6673     nstat_progress_digest *digestp,
6674     nstat_counts *countsp,
6675     void *metadatap)
6676 {
6677 #pragma unused(digestp)
6678 
6679 	if (ctx == NULL) {
6680 		return false;
6681 	}
6682 
6683 	struct necp_client_flow_registration * __single flow_registration = (struct necp_client_flow_registration *)(void *)ctx;
6684 	struct necp_client *client = flow_registration->client;
6685 	struct necp_all_stats *ustats_kaddr = ((struct necp_all_kstats *)flow_registration->kstats_kaddr)->necp_stats_ustats;
6686 	struct necp_udp_stats *udpstats = (struct necp_udp_stats *)ustats_kaddr;
6687 	ASSERT(udpstats != NULL);
6688 
6689 	u_int32_t nstat_diagnostic_flags = 0;
6690 
6691 	// Retrieve details from the last time the assigned flows were updated
6692 	u_int32_t route_ifindex = IFSCOPE_NONE;
6693 	u_int32_t route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6694 	u_int64_t combined_interface_details = 0;
6695 
6696 	combined_interface_details = os_atomic_load(&flow_registration->last_interface_details, relaxed);
6697 	split_interface_details(combined_interface_details, &route_ifindex, &route_ifflags);
6698 
6699 	if (route_ifindex == IFSCOPE_NONE) {
6700 		// Mark no interface
6701 		nstat_diagnostic_flags |= NSTAT_IFNET_ROUTE_VALUE_UNOBTAINABLE;
6702 		route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6703 		NECPLOG(LOG_INFO, "req udp stats, failed to get route details for pid %d curproc %d %s\n",
6704 		    client->proc_pid, proc_pid(current_proc()), proc_best_name(current_proc()));
6705 	}
6706 
6707 	const struct sk_stats_flow *sf = &flow_registration->nexus_stats->fs_stats;
6708 	if (sf == NULL) {
6709 		nstat_diagnostic_flags |= NSTAT_IFNET_FLOWSWITCH_VALUE_UNOBTAINABLE;
6710 		char namebuf[MAXCOMLEN + 1];
6711 		(void) strlcpy(namebuf, "unknown", sizeof(namebuf));
6712 		proc_name(client->proc_pid, namebuf, sizeof(namebuf));
6713 		NECPLOG(LOG_ERR, "req udp stats, necp_client flow_registration flow_stats missing for pid %d %s curproc %d %s\n",
6714 		    client->proc_pid, namebuf, proc_pid(current_proc()), proc_best_name(current_proc()));
6715 		sf = &ntstat_sk_stats_zero;
6716 	}
6717 
6718 	if (ifflagsp) {
6719 		*ifflagsp = route_ifflags | nstat_diagnostic_flags;
6720 		*ifflagsp |= (sf->sf_flags & SFLOWF_ONLINK) ? NSTAT_IFNET_IS_LOCAL : NSTAT_IFNET_IS_NON_LOCAL;
6721 		if ((countsp == NULL) && (metadatap == NULL)) {
6722 			return true;
6723 		}
6724 	}
6725 
6726 	if (countsp) {
6727 		countsp->nstat_rxbytes = udpstats->necp_udp_counts.necp_stat_rxbytes;
6728 		countsp->nstat_txbytes = udpstats->necp_udp_counts.necp_stat_txbytes;
6729 
6730 		countsp->nstat_rxduplicatebytes = udpstats->necp_udp_counts.necp_stat_rxduplicatebytes;
6731 		countsp->nstat_rxoutoforderbytes = udpstats->necp_udp_counts.necp_stat_rxoutoforderbytes;
6732 		countsp->nstat_txretransmit = udpstats->necp_udp_counts.necp_stat_txretransmit;
6733 
6734 		countsp->nstat_min_rtt = udpstats->necp_udp_counts.necp_stat_min_rtt;
6735 		countsp->nstat_avg_rtt = udpstats->necp_udp_counts.necp_stat_avg_rtt;
6736 		countsp->nstat_var_rtt = udpstats->necp_udp_counts.necp_stat_var_rtt;
6737 
6738 		// Supplement what the user level has told us with what we know from the flowswitch
6739 		countsp->nstat_rxpackets = sf->sf_ipackets;
6740 		countsp->nstat_txpackets = sf->sf_opackets;
6741 		if (route_ifflags & NSTAT_IFNET_IS_CELLULAR) {
6742 			countsp->nstat_cell_rxbytes = sf->sf_ibytes;
6743 			countsp->nstat_cell_txbytes = sf->sf_obytes;
6744 		} else if (route_ifflags & NSTAT_IFNET_IS_WIFI) {
6745 			countsp->nstat_wifi_rxbytes = sf->sf_ibytes;
6746 			countsp->nstat_wifi_txbytes = sf->sf_obytes;
6747 		} else if (route_ifflags & NSTAT_IFNET_IS_WIRED) {
6748 			countsp->nstat_wired_rxbytes = sf->sf_ibytes;
6749 			countsp->nstat_wired_txbytes = sf->sf_obytes;
6750 		}
6751 	}
6752 
6753 	if (metadatap) {
6754 		nstat_udp_descriptor *desc = (nstat_udp_descriptor *)metadatap;
6755 		memset(desc, 0, sizeof(*desc));
6756 
6757 		// Metadata from the flow registration
6758 		uuid_copy(desc->fuuid, flow_registration->registration_id);
6759 
6760 		// Metadata that the necp client should have in TLV format.
6761 		pid_t effective_pid = client->proc_pid;
6762 		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);
6763 		desc->epid = (u_int32_t)effective_pid;
6764 
6765 		// Metadata from the flow registration
6766 		// This needs to revisited if multiple flows are created from one flow registration
6767 		struct necp_client_flow *flow = NULL;
6768 		LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
6769 			memcpy(&desc->local, &flow->local_addr, sizeof(desc->local));
6770 			break;
6771 		}
6772 
6773 		// Metadata from the route
6774 		desc->ifindex = route_ifindex;
6775 		desc->ifnet_properties = route_ifflags | nstat_diagnostic_flags;
6776 		desc->ifnet_properties |= (sf->sf_flags & SFLOWF_ONLINK) ? NSTAT_IFNET_IS_LOCAL : NSTAT_IFNET_IS_NON_LOCAL;
6777 
6778 		// Basic metadata is all that is required for UDP
6779 		desc->rcvbufsize = udpstats->necp_udp_basic.rcvbufsize;
6780 		desc->rcvbufused = udpstats->necp_udp_basic.rcvbufused;
6781 
6782 		memcpy(&desc->activity_bitmap, &sf->sf_activity, sizeof(sf->sf_activity));
6783 
6784 		if (NECP_ENABLE_CLIENT_TRACE(NECP_CLIENT_TRACE_LEVEL_FLOW)) {
6785 			uuid_string_t euuid_str = { 0 };
6786 			uuid_unparse(desc->euuid, euuid_str);
6787 			NECPLOG(LOG_NOTICE, "Collected stats - UDP - epid %d uid %d euuid %s persona id %d", desc->epid, desc->uid, euuid_str, desc->persona_id);
6788 		}
6789 	}
6790 
6791 	return true;
6792 }
6793 
6794 // Called from NetworkStatistics when it wishes to collect latest information for a QUIC flow.
6795 //
6796 // TODO: For now it is an exact implementation as that of TCP.
6797 // Still to keep the logic separate for future divergence, keeping the routines separate.
6798 // It also seems there are lots of common code between existing implementations and
6799 // it would be good to refactor this logic at some point.
6800 static bool
necp_request_quic_netstats(userland_stats_provider_context * ctx,u_int32_t * ifflagsp,nstat_progress_digest * digestp,nstat_counts * countsp,void * metadatap)6801 necp_request_quic_netstats(userland_stats_provider_context *ctx,
6802     u_int32_t *ifflagsp,
6803     nstat_progress_digest *digestp,
6804     nstat_counts *countsp,
6805     void *metadatap)
6806 {
6807 	if (ctx == NULL) {
6808 		return false;
6809 	}
6810 
6811 	struct necp_client_flow_registration * __single flow_registration = (struct necp_client_flow_registration *)(void *)ctx;
6812 	struct necp_client *client = flow_registration->client;
6813 	struct necp_all_stats *ustats_kaddr = ((struct necp_all_kstats *)flow_registration->kstats_kaddr)->necp_stats_ustats;
6814 	struct necp_quic_stats *quicstats = (struct necp_quic_stats *)ustats_kaddr;
6815 	ASSERT(quicstats != NULL);
6816 
6817 	u_int32_t nstat_diagnostic_flags = 0;
6818 
6819 	// Retrieve details from the last time the assigned flows were updated
6820 	u_int32_t route_ifindex = IFSCOPE_NONE;
6821 	u_int32_t route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6822 	u_int64_t combined_interface_details = 0;
6823 
6824 	combined_interface_details = os_atomic_load(&flow_registration->last_interface_details, relaxed);
6825 	split_interface_details(combined_interface_details, &route_ifindex, &route_ifflags);
6826 
6827 	if (route_ifindex == IFSCOPE_NONE) {
6828 		// Mark no interface
6829 		nstat_diagnostic_flags |= NSTAT_IFNET_ROUTE_VALUE_UNOBTAINABLE;
6830 		route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6831 		NECPLOG(LOG_INFO, "req quic stats, failed to get route details for pid %d curproc %d %s\n",
6832 		    client->proc_pid, proc_pid(current_proc()), proc_best_name(current_proc()));
6833 	}
6834 
6835 	const struct sk_stats_flow *sf = &flow_registration->nexus_stats->fs_stats;
6836 	if (sf == NULL) {
6837 		nstat_diagnostic_flags |= NSTAT_IFNET_FLOWSWITCH_VALUE_UNOBTAINABLE;
6838 		char namebuf[MAXCOMLEN + 1];
6839 		(void) strlcpy(namebuf, "unknown", sizeof(namebuf));
6840 		proc_name(client->proc_pid, namebuf, sizeof(namebuf));
6841 		NECPLOG(LOG_ERR, "req quic stats, necp_client flow_registration flow_stats missing for pid %d %s curproc %d %s\n",
6842 		    client->proc_pid, namebuf, proc_pid(current_proc()), proc_best_name(current_proc()));
6843 		sf = &ntstat_sk_stats_zero;
6844 	}
6845 
6846 	if (ifflagsp) {
6847 		*ifflagsp = route_ifflags | nstat_diagnostic_flags;
6848 		*ifflagsp |= (sf->sf_flags & SFLOWF_ONLINK) ? NSTAT_IFNET_IS_LOCAL : NSTAT_IFNET_IS_NON_LOCAL;
6849 		if ((digestp == NULL) && (countsp == NULL) && (metadatap == NULL)) {
6850 			return true;
6851 		}
6852 	}
6853 
6854 	if (digestp) {
6855 		// The digest is intended to give information that may help give insight into the state of the link
6856 		digestp->rxbytes = quicstats->necp_quic_counts.necp_stat_rxbytes;
6857 		digestp->txbytes = quicstats->necp_quic_counts.necp_stat_txbytes;
6858 		digestp->rxduplicatebytes = quicstats->necp_quic_counts.necp_stat_rxduplicatebytes;
6859 		digestp->rxoutoforderbytes = quicstats->necp_quic_counts.necp_stat_rxoutoforderbytes;
6860 		digestp->txretransmit = quicstats->necp_quic_counts.necp_stat_txretransmit;
6861 		digestp->ifindex = route_ifindex;
6862 		digestp->state = quicstats->necp_quic_extra.state;
6863 		digestp->txunacked = quicstats->necp_quic_extra.txunacked;
6864 		digestp->txwindow = quicstats->necp_quic_extra.txwindow;
6865 		digestp->connstatus.probe_activated    = quicstats->necp_quic_extra.probestatus.probe_activated;
6866 		digestp->connstatus.write_probe_failed = quicstats->necp_quic_extra.probestatus.write_probe_failed;
6867 		digestp->connstatus.read_probe_failed  = quicstats->necp_quic_extra.probestatus.read_probe_failed;
6868 		digestp->connstatus.conn_probe_failed  = quicstats->necp_quic_extra.probestatus.conn_probe_failed;
6869 
6870 		if ((countsp == NULL) && (metadatap == NULL)) {
6871 			return true;
6872 		}
6873 	}
6874 
6875 	if (countsp) {
6876 		countsp->nstat_rxbytes = quicstats->necp_quic_counts.necp_stat_rxbytes;
6877 		countsp->nstat_txbytes = quicstats->necp_quic_counts.necp_stat_txbytes;
6878 
6879 		countsp->nstat_rxduplicatebytes = quicstats->necp_quic_counts.necp_stat_rxduplicatebytes;
6880 		countsp->nstat_rxoutoforderbytes = quicstats->necp_quic_counts.necp_stat_rxoutoforderbytes;
6881 		countsp->nstat_txretransmit = quicstats->necp_quic_counts.necp_stat_txretransmit;
6882 
6883 		countsp->nstat_min_rtt = quicstats->necp_quic_counts.necp_stat_min_rtt;
6884 		countsp->nstat_avg_rtt = quicstats->necp_quic_counts.necp_stat_avg_rtt;
6885 		countsp->nstat_var_rtt = quicstats->necp_quic_counts.necp_stat_var_rtt;
6886 
6887 		// TODO: It would be good to expose QUIC stats for CH/SH retransmission and connection state
6888 		// Supplement what the user level has told us with what we know from the flowswitch
6889 		countsp->nstat_rxpackets = sf->sf_ipackets;
6890 		countsp->nstat_txpackets = sf->sf_opackets;
6891 		if (route_ifflags & NSTAT_IFNET_IS_CELLULAR) {
6892 			countsp->nstat_cell_rxbytes = sf->sf_ibytes;
6893 			countsp->nstat_cell_txbytes = sf->sf_obytes;
6894 		} else if (route_ifflags & NSTAT_IFNET_IS_WIFI) {
6895 			countsp->nstat_wifi_rxbytes = sf->sf_ibytes;
6896 			countsp->nstat_wifi_txbytes = sf->sf_obytes;
6897 		} else if (route_ifflags & NSTAT_IFNET_IS_WIRED) {
6898 			countsp->nstat_wired_rxbytes = sf->sf_ibytes;
6899 			countsp->nstat_wired_txbytes = sf->sf_obytes;
6900 		}
6901 	}
6902 
6903 	if (metadatap) {
6904 		nstat_quic_descriptor *desc = (nstat_quic_descriptor *)metadatap;
6905 		memset(desc, 0, sizeof(*desc));
6906 
6907 		// Metadata from the flow registration
6908 		uuid_copy(desc->fuuid, flow_registration->registration_id);
6909 
6910 		// Metadata, that the necp client should have, in TLV format.
6911 		pid_t effective_pid = client->proc_pid;
6912 		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);
6913 		desc->epid = (u_int32_t)effective_pid;
6914 
6915 		// Metadata from the flow registration
6916 		// This needs to revisited if multiple flows are created from one flow registration
6917 		struct necp_client_flow *flow = NULL;
6918 		LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
6919 			memcpy(&desc->local, &flow->local_addr, sizeof(desc->local));
6920 			break;
6921 		}
6922 
6923 		// Metadata from the route
6924 		desc->ifindex = route_ifindex;
6925 		desc->ifnet_properties = route_ifflags | nstat_diagnostic_flags;
6926 		desc->ifnet_properties |= (sf->sf_flags & SFLOWF_ONLINK) ? NSTAT_IFNET_IS_LOCAL : NSTAT_IFNET_IS_NON_LOCAL;
6927 
6928 		// Basic metadata from userland
6929 		desc->rcvbufsize = quicstats->necp_quic_basic.rcvbufsize;
6930 		desc->rcvbufused = quicstats->necp_quic_basic.rcvbufused;
6931 
6932 		// Additional QUIC specific data
6933 		desc->sndbufsize = quicstats->necp_quic_extra.sndbufsize;
6934 		desc->sndbufused = quicstats->necp_quic_extra.sndbufused;
6935 		desc->txunacked = quicstats->necp_quic_extra.txunacked;
6936 		desc->txwindow = quicstats->necp_quic_extra.txwindow;
6937 		desc->txcwindow = quicstats->necp_quic_extra.txcwindow;
6938 		desc->traffic_mgt_flags = quicstats->necp_quic_extra.traffic_mgt_flags;
6939 		desc->state = quicstats->necp_quic_extra.state;
6940 
6941 		// TODO: CC algo defines should be named agnostic of the protocol
6942 		u_int32_t cc_alg_index = quicstats->necp_quic_extra.cc_alg_index;
6943 		if (cc_alg_index < TCP_CC_ALGO_COUNT) {
6944 			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));
6945 		} else {
6946 			strlcpy(desc->cc_algo, "unknown", sizeof(desc->cc_algo));
6947 		}
6948 
6949 		memcpy(&desc->activity_bitmap, &sf->sf_activity, sizeof(sf->sf_activity));
6950 
6951 		desc->connstatus.probe_activated        = quicstats->necp_quic_extra.probestatus.probe_activated;
6952 		desc->connstatus.write_probe_failed     = quicstats->necp_quic_extra.probestatus.write_probe_failed;
6953 		desc->connstatus.read_probe_failed      = quicstats->necp_quic_extra.probestatus.read_probe_failed;
6954 		desc->connstatus.conn_probe_failed      = quicstats->necp_quic_extra.probestatus.conn_probe_failed;
6955 
6956 		if (NECP_ENABLE_CLIENT_TRACE(NECP_CLIENT_TRACE_LEVEL_FLOW)) {
6957 			uuid_string_t euuid_str = { 0 };
6958 			uuid_unparse(desc->euuid, euuid_str);
6959 			NECPLOG(LOG_NOTICE, "Collected stats - QUIC - epid %d uid %d euuid %s persona id %d", desc->epid, desc->uid, euuid_str, desc->persona_id);
6960 		}
6961 	}
6962 	return true;
6963 }
6964 
6965 #endif /* SKYWALK */
6966 
6967 // Support functions for NetworkStatistics support for necp_client connections
6968 
6969 static void
necp_client_inherit_from_parent(struct necp_client * client,struct necp_client * parent)6970 necp_client_inherit_from_parent(
6971 	struct necp_client *client,
6972 	struct necp_client *parent)
6973 {
6974 	assert(client->original_parameters_source == NULL);
6975 
6976 	if (parent->original_parameters_source != NULL) {
6977 		client->original_parameters_source = parent->original_parameters_source;
6978 	} else {
6979 		client->original_parameters_source = parent;
6980 	}
6981 	necp_client_retain(client->original_parameters_source);
6982 }
6983 
6984 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)6985 necp_find_conn_netstat_data(struct necp_client *client,
6986     u_int32_t *ntstat_flags,
6987     pid_t *effective_pid,
6988     uuid_t *puuid,
6989     uid_t *uid,
6990     uuid_t *euuid,
6991     uid_t *persona_id)
6992 {
6993 	bool has_remote_address = false;
6994 	bool has_ip_protocol = false;
6995 	bool has_transport_protocol = false;
6996 	size_t offset = 0;
6997 	u_int8_t *parameters;
6998 	u_int32_t parameters_size;
6999 
7000 
7001 	parameters = client->parameters;
7002 	parameters_size = (u_int32_t)client->parameters_length;
7003 
7004 	while ((offset + sizeof(struct necp_tlv_header)) <= parameters_size) {
7005 		u_int8_t type = necp_buffer_get_tlv_type(parameters, parameters_size, offset);
7006 		u_int32_t length = necp_buffer_get_tlv_length(parameters, parameters_size, offset);
7007 
7008 		if (length > (parameters_size - (offset + sizeof(struct necp_tlv_header)))) {
7009 			// If the length is larger than what can fit in the remaining parameters size, bail
7010 			NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
7011 			break;
7012 		}
7013 
7014 		if (length > 0) {
7015 			u_int8_t * __indexable value = necp_buffer_get_tlv_value(parameters, parameters_size, offset, NULL);
7016 			if (value != NULL) {
7017 				switch (type) {
7018 				case NECP_CLIENT_PARAMETER_APPLICATION: {
7019 					if ((euuid) && (length >= sizeof(uuid_t))) {
7020 						uuid_copy(*euuid, value);
7021 					}
7022 					break;
7023 				}
7024 				case NECP_CLIENT_PARAMETER_IP_PROTOCOL: {
7025 					if (length >= 1) {
7026 						has_ip_protocol = true;
7027 					}
7028 					break;
7029 				}
7030 				case NECP_CLIENT_PARAMETER_PID: {
7031 					if ((effective_pid) && length >= sizeof(pid_t)) {
7032 						memcpy(effective_pid, value, sizeof(pid_t));
7033 					}
7034 					break;
7035 				}
7036 				case NECP_CLIENT_PARAMETER_PARENT_ID: {
7037 					if ((puuid) && (length == sizeof(uuid_t))) {
7038 						uuid_copy(*puuid, value);
7039 					}
7040 					break;
7041 				}
7042 				// It is an implementation quirk that the remote address can be found in the necp parameters
7043 				case NECP_CLIENT_PARAMETER_REMOTE_ADDRESS: {
7044 					if (length >= sizeof(struct necp_policy_condition_addr)) {
7045 						struct necp_policy_condition_addr *address_struct = (struct necp_policy_condition_addr *)(void *)value;
7046 						if (necp_client_address_is_valid(&address_struct->address.sa)) {
7047 							has_remote_address = true;
7048 						}
7049 					}
7050 					break;
7051 				}
7052 				case NECP_CLIENT_PARAMETER_TRANSPORT_PROTOCOL: {
7053 					if (length >= 1) {
7054 						has_transport_protocol = true;
7055 					}
7056 					break;
7057 				}
7058 				case NECP_CLIENT_PARAMETER_APPLICATION_ID: {
7059 					if (length >= sizeof(necp_application_id_t) && uid && persona_id) {
7060 						necp_application_id_t *application_id = (necp_application_id_t *)(void *)value;
7061 						memcpy(uid, &application_id->uid, sizeof(uid_t));
7062 						uuid_copy(*euuid, application_id->effective_uuid);
7063 						memcpy(persona_id, &application_id->persona_id, sizeof(uid_t));
7064 					}
7065 					break;
7066 				}
7067 				default: {
7068 					break;
7069 				}
7070 				}
7071 			}
7072 		}
7073 		offset += sizeof(struct necp_tlv_header) + length;
7074 	}
7075 	if (ntstat_flags) {
7076 		*ntstat_flags = (has_remote_address && has_ip_protocol && has_transport_protocol)? NSTAT_NECP_CONN_HAS_NET_ACCESS: 0;
7077 	}
7078 }
7079 
7080 static bool
necp_request_conn_netstats(nstat_provider_context ctx,u_int32_t * ifflagsp,nstat_counts * countsp,void * metadatap)7081 necp_request_conn_netstats(nstat_provider_context ctx,
7082     u_int32_t *ifflagsp,
7083     nstat_counts *countsp,
7084     void *metadatap)
7085 {
7086 	if (ctx == NULL) {
7087 		return false;
7088 	}
7089 	struct necp_client * __single client = (struct necp_client *)(void *)ctx;
7090 	nstat_connection_descriptor *desc = (nstat_connection_descriptor *)metadatap;
7091 
7092 	if (ifflagsp) {
7093 		necp_find_conn_netstat_data(client, ifflagsp, NULL, NULL, NULL, NULL, NULL);
7094 	}
7095 	if (countsp) {
7096 		memset(countsp, 0, sizeof(*countsp));
7097 	}
7098 	if (desc) {
7099 		memset(desc, 0, sizeof(*desc));
7100 		// Metadata, that the necp client should have, in TLV format.
7101 		pid_t effective_pid = client->proc_pid;
7102 		necp_find_conn_netstat_data(client, &desc->ifnet_properties, &effective_pid, &desc->puuid, &desc->uid, &desc->euuid, &desc->persona_id);
7103 		desc->epid = (u_int32_t)effective_pid;
7104 
7105 		// User level should obtain almost all connection information from an extension
7106 		// leaving little to do here
7107 		uuid_copy(desc->fuuid, client->latest_flow_registration_id);
7108 		uuid_copy(desc->cuuid, client->client_id);
7109 	}
7110 	return true;
7111 }
7112 
7113 static int
necp_skywalk_priv_check_cred(proc_t p,kauth_cred_t cred)7114 necp_skywalk_priv_check_cred(proc_t p, kauth_cred_t cred)
7115 {
7116 #pragma unused(p, cred)
7117 #if SKYWALK
7118 	/* This includes Nexus controller and Skywalk observer privs */
7119 	return skywalk_nxctl_check_privileges(p, cred);
7120 #else /* !SKYWALK */
7121 	return 0;
7122 #endif /* !SKYWALK */
7123 }
7124 
7125 /// System calls
7126 
7127 int
necp_open(struct proc * p,struct necp_open_args * uap,int * retval)7128 necp_open(struct proc *p, struct necp_open_args *uap, int *retval)
7129 {
7130 #pragma unused(retval)
7131 	int error = 0;
7132 	struct necp_fd_data * __single fd_data = NULL;
7133 	struct fileproc * __single fp = NULL;
7134 	int fd = -1;
7135 
7136 	if (uap->flags & NECP_OPEN_FLAG_OBSERVER ||
7137 	    uap->flags & NECP_OPEN_FLAG_PUSH_OBSERVER) {
7138 		if (necp_skywalk_priv_check_cred(p, kauth_cred_get()) != 0 &&
7139 		    priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_NETWORK_STATISTICS, 0) != 0) {
7140 			NECPLOG0(LOG_ERR, "Client does not hold necessary entitlement to observe other NECP clients");
7141 			error = EACCES;
7142 			goto done;
7143 		}
7144 	}
7145 
7146 #if CONFIG_MACF
7147 	error = mac_necp_check_open(p, uap->flags);
7148 	if (error) {
7149 		goto done;
7150 	}
7151 #endif /* MACF */
7152 
7153 	error = falloc(p, &fp, &fd);
7154 	if (error != 0) {
7155 		goto done;
7156 	}
7157 
7158 	fd_data = kalloc_type(struct necp_fd_data, Z_WAITOK | Z_ZERO | Z_NOFAIL);
7159 
7160 	fd_data->necp_fd_type = necp_fd_type_client;
7161 	fd_data->flags = uap->flags;
7162 	RB_INIT(&fd_data->clients);
7163 	RB_INIT(&fd_data->flows);
7164 	TAILQ_INIT(&fd_data->update_list);
7165 	lck_mtx_init(&fd_data->fd_lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
7166 	klist_init(&fd_data->si.si_note);
7167 	fd_data->proc_pid = proc_pid(p);
7168 #if SKYWALK
7169 	LIST_INIT(&fd_data->stats_arena_list);
7170 #endif /* !SKYWALK */
7171 
7172 	fp->fp_flags |= FP_CLOEXEC | FP_CLOFORK;
7173 	fp->fp_glob->fg_flag = FREAD;
7174 	fp->fp_glob->fg_ops = &necp_fd_ops;
7175 	fp_set_data(fp, fd_data);
7176 
7177 	proc_fdlock(p);
7178 
7179 	procfdtbl_releasefd(p, fd, NULL);
7180 	fp_drop(p, fd, fp, 1);
7181 
7182 	*retval = fd;
7183 
7184 	if (fd_data->flags & NECP_OPEN_FLAG_PUSH_OBSERVER) {
7185 		NECP_OBSERVER_LIST_LOCK_EXCLUSIVE();
7186 		LIST_INSERT_HEAD(&necp_fd_observer_list, fd_data, chain);
7187 		OSIncrementAtomic(&necp_observer_fd_count);
7188 		NECP_OBSERVER_LIST_UNLOCK();
7189 
7190 		// Walk all existing clients and add them
7191 		NECP_CLIENT_TREE_LOCK_SHARED();
7192 		struct necp_client *existing_client = NULL;
7193 		RB_FOREACH(existing_client, _necp_client_global_tree, &necp_client_global_tree) {
7194 			NECP_CLIENT_LOCK(existing_client);
7195 			necp_client_update_observer_add_internal(fd_data, existing_client);
7196 			necp_client_update_observer_update_internal(fd_data, existing_client);
7197 			NECP_CLIENT_UNLOCK(existing_client);
7198 		}
7199 		NECP_CLIENT_TREE_UNLOCK();
7200 	} else {
7201 		NECP_FD_LIST_LOCK_EXCLUSIVE();
7202 		LIST_INSERT_HEAD(&necp_fd_list, fd_data, chain);
7203 		OSIncrementAtomic(&necp_client_fd_count);
7204 		NECP_FD_LIST_UNLOCK();
7205 	}
7206 
7207 	proc_fdunlock(p);
7208 
7209 done:
7210 	if (error != 0) {
7211 		if (fp != NULL) {
7212 			fp_free(p, fd, fp);
7213 			fp = NULL;
7214 		}
7215 		if (fd_data != NULL) {
7216 			kfree_type(struct necp_fd_data, fd_data);
7217 		}
7218 	}
7219 
7220 	return error;
7221 }
7222 
7223 // All functions called directly from necp_client_action() to handle one of the
7224 // types should be marked with NECP_CLIENT_ACTION_FUNCTION. This ensures that
7225 // necp_client_action() does not inline all the actions into a single function.
7226 #define NECP_CLIENT_ACTION_FUNCTION __attribute__((noinline))
7227 
7228 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)7229 necp_client_add(struct proc *p, struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7230 {
7231 	int error = 0;
7232 	struct necp_client * __single client = NULL;
7233 	const size_t buffer_size = uap->buffer_size;
7234 
7235 	if (fd_data->flags & NECP_OPEN_FLAG_PUSH_OBSERVER) {
7236 		NECPLOG0(LOG_ERR, "NECP client observers with push enabled may not add their own clients");
7237 		return EINVAL;
7238 	}
7239 
7240 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
7241 	    buffer_size == 0 || buffer_size > NECP_MAX_CLIENT_PARAMETERS_SIZE || uap->buffer == 0) {
7242 		return EINVAL;
7243 	}
7244 
7245 	client = kalloc_type(struct necp_client, Z_WAITOK | Z_ZERO | Z_NOFAIL);
7246 	client->parameters = kalloc_data(buffer_size, Z_WAITOK | Z_NOFAIL);
7247 	client->parameters_length = buffer_size;
7248 	lck_mtx_init(&client->lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
7249 	lck_mtx_init(&client->route_lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
7250 
7251 	error = copyin(uap->buffer, client->parameters, buffer_size);
7252 	if (error) {
7253 		NECPLOG(LOG_ERR, "necp_client_add parameters copyin error (%d)", error);
7254 		goto done;
7255 	}
7256 
7257 	os_ref_init(&client->reference_count, &necp_client_refgrp); // Hold our reference until close
7258 
7259 	client->proc_pid = fd_data->proc_pid; // Save off proc pid in case the client will persist past fd
7260 	client->agent_handle = (void *)fd_data;
7261 	client->platform_binary = ((csproc_get_platform_binary(p) == 0) ? 0 : 1);
7262 
7263 	necp_generate_client_id(client->client_id, false);
7264 	LIST_INIT(&client->assertion_list);
7265 	RB_INIT(&client->flow_registrations);
7266 
7267 	NECP_CLIENT_LOG(client, "Adding client");
7268 
7269 	error = copyout(client->client_id, uap->client_id, sizeof(uuid_t));
7270 	if (error) {
7271 		NECPLOG(LOG_ERR, "necp_client_add client_id copyout error (%d)", error);
7272 		goto done;
7273 	}
7274 
7275 #if SKYWALK
7276 	struct necp_client_parsed_parameters parsed_parameters = {};
7277 	int parse_error = necp_client_parse_parameters(client, client->parameters, (u_int32_t)client->parameters_length, &parsed_parameters);
7278 
7279 	if (parse_error == 0 &&
7280 	    ((parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_DELEGATED_UPID) ||
7281 	    (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_ATTRIBUTED_BUNDLE_IDENTIFIER))) {
7282 		bool has_delegation_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_SOCKET_DELEGATE, 0) == 0);
7283 		if (!has_delegation_entitlement) {
7284 			if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_DELEGATED_UPID) {
7285 				NECPLOG(LOG_ERR, "%s(%d) does not hold the necessary entitlement to delegate network traffic for other processes by upid",
7286 				    proc_name_address(p), proc_pid(p));
7287 			}
7288 			if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_ATTRIBUTED_BUNDLE_IDENTIFIER) {
7289 				NECPLOG(LOG_ERR, "%s(%d) does not hold the necessary entitlement to set attributed bundle identifier",
7290 				    proc_name_address(p), proc_pid(p));
7291 			}
7292 			error = EPERM;
7293 			goto done;
7294 		}
7295 
7296 		if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_DELEGATED_UPID) {
7297 			// Save off delegated unique PID
7298 			client->delegated_upid = parsed_parameters.delegated_upid;
7299 		}
7300 	}
7301 
7302 	if (parse_error == 0 && parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_INTERPOSE) {
7303 		bool has_nexus_entitlement = (necp_skywalk_priv_check_cred(p, kauth_cred_get()) == 0);
7304 		if (!has_nexus_entitlement) {
7305 			NECPLOG(LOG_ERR, "%s(%d) does not hold the necessary entitlement to open a custom nexus client",
7306 			    proc_name_address(p), proc_pid(p));
7307 			error = EPERM;
7308 			goto done;
7309 		}
7310 	}
7311 
7312 	if (parse_error == 0 && (parsed_parameters.flags &
7313 	    (NECP_CLIENT_PARAMETER_FLAG_CUSTOM_ETHER | NECP_CLIENT_PARAMETER_FLAG_CUSTOM_IP))) {
7314 		bool has_custom_protocol_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_CUSTOM_PROTOCOL, 0) == 0);
7315 		if (!has_custom_protocol_entitlement) {
7316 			NECPLOG(LOG_ERR, "%s(%d) does not hold the necessary entitlement for custom protocol APIs",
7317 			    proc_name_address(p), proc_pid(p));
7318 			error = EPERM;
7319 			goto done;
7320 		}
7321 	}
7322 
7323 	if (parse_error == 0 && parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER &&
7324 	    (parsed_parameters.ip_protocol == IPPROTO_TCP || parsed_parameters.ip_protocol == IPPROTO_UDP)) {
7325 		uint32_t *netns_addr = NULL;
7326 		uint8_t netns_addr_len = 0;
7327 		struct ns_flow_info flow_info = {};
7328 		uint32_t netns_flags = NETNS_LISTENER;
7329 		uuid_copy(flow_info.nfi_flow_uuid, client->client_id);
7330 		flow_info.nfi_protocol = parsed_parameters.ip_protocol;
7331 		flow_info.nfi_owner_pid = client->proc_pid;
7332 		if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID) {
7333 			flow_info.nfi_effective_pid = parsed_parameters.effective_pid;
7334 		} else {
7335 			flow_info.nfi_effective_pid = flow_info.nfi_owner_pid;
7336 		}
7337 		proc_name(flow_info.nfi_owner_pid, flow_info.nfi_owner_name, MAXCOMLEN);
7338 		proc_name(flow_info.nfi_effective_pid, flow_info.nfi_effective_name, MAXCOMLEN);
7339 
7340 		if (parsed_parameters.local_addr.sa.sa_family == AF_UNSPEC) {
7341 			// Treat no local address as a wildcard IPv6
7342 			// parsed_parameters is already initialized to all zeros
7343 			parsed_parameters.local_addr.sin6.sin6_family = AF_INET6;
7344 			parsed_parameters.local_addr.sin6.sin6_len = sizeof(struct sockaddr_in6);
7345 		}
7346 
7347 		switch (parsed_parameters.local_addr.sa.sa_family) {
7348 		case AF_INET: {
7349 			memcpy(&flow_info.nfi_laddr, &parsed_parameters.local_addr.sa, parsed_parameters.local_addr.sa.sa_len);
7350 			netns_addr = (uint32_t *)&parsed_parameters.local_addr.sin.sin_addr;
7351 			netns_addr_len = 4;
7352 			break;
7353 		}
7354 		case AF_INET6: {
7355 			memcpy(&flow_info.nfi_laddr.sin6, &parsed_parameters.local_addr.sin6, parsed_parameters.local_addr.sa.sa_len);
7356 			netns_addr = (uint32_t *)&parsed_parameters.local_addr.sin6.sin6_addr;
7357 			netns_addr_len = 16;
7358 			break;
7359 		}
7360 
7361 		default: {
7362 			NECPLOG(LOG_ERR, "necp_client_add listener invalid address family (%d)", parsed_parameters.local_addr.sa.sa_family);
7363 			error = EINVAL;
7364 			goto done;
7365 		}
7366 		}
7367 		if ((parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) &&
7368 		    (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_REUSE_LOCAL)) {
7369 			netns_flags |= NETNS_REUSEPORT;
7370 		}
7371 		if (parsed_parameters.local_addr.sin.sin_port == 0) {
7372 			error = netns_reserve_ephemeral(&client->port_reservation, netns_addr, netns_addr_len, parsed_parameters.ip_protocol,
7373 			    &parsed_parameters.local_addr.sin.sin_port, netns_flags, &flow_info);
7374 			if (error) {
7375 				NECPLOG(LOG_ERR, "necp_client_add netns_reserve_ephemeral error (%d)", error);
7376 				goto done;
7377 			}
7378 
7379 			// Update the parameter TLVs with the assigned port
7380 			necp_client_update_local_port_parameters(client->parameters, (u_int32_t)client->parameters_length, parsed_parameters.local_addr.sin.sin_port);
7381 		} else {
7382 			error = netns_reserve(&client->port_reservation, netns_addr, netns_addr_len, parsed_parameters.ip_protocol,
7383 			    parsed_parameters.local_addr.sin.sin_port, netns_flags, &flow_info);
7384 			if (error) {
7385 				NECPLOG(LOG_ERR, "necp_client_add netns_reserve error (%d)", error);
7386 				goto done;
7387 			}
7388 		}
7389 	}
7390 
7391 	struct necp_client *parent = NULL;
7392 	uuid_t parent_client_id;
7393 	uuid_clear(parent_client_id);
7394 	struct necp_client_nexus_parameters parent_parameters = {};
7395 	uint16_t num_flow_regs = 0;
7396 	if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_PARENT_UUID) {
7397 		// The parent "should" be found on fd_data without having to search across the whole necp_fd_list
7398 		// It would be nice to do this a little further down where there's another instance of NECP_FD_LOCK
7399 		// but the logic here depends on the parse paramters
7400 		NECP_FD_LOCK(fd_data);
7401 		parent = necp_client_fd_find_client_unlocked(fd_data, parsed_parameters.parent_uuid);
7402 		if (parent != NULL) {
7403 			necp_client_inherit_from_parent(client, parent);
7404 			necp_client_copy_parameters_locked(client, &parent_parameters);
7405 			uuid_copy(parent_client_id, parsed_parameters.parent_uuid);
7406 			struct necp_client_flow_registration *flow_registration = NULL;
7407 			RB_FOREACH(flow_registration, _necp_client_flow_tree, &parent->flow_registrations) {
7408 				num_flow_regs++;
7409 			}
7410 		}
7411 		NECP_FD_UNLOCK(fd_data);
7412 		if (parent == NULL) {
7413 			NECPLOG0(LOG_ERR, "necp_client_add, no necp_client_inherit_from_parent as can't find parent on fd_data");
7414 		}
7415 	}
7416 	if (parse_error == 0 && parent != NULL && parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLOW_DEMUX_PATTERN) {
7417 		do {
7418 			if (parsed_parameters.demux_patterns[0].len == 0) {
7419 				NECPLOG0(LOG_INFO, "necp_client_add, child does not have a demux pattern");
7420 				break;
7421 			}
7422 
7423 			if (uuid_is_null(parent_client_id)) {
7424 				NECPLOG0(LOG_INFO, "necp_client_add, parent ID is null");
7425 				break;
7426 			}
7427 
7428 			if (num_flow_regs > 1) {
7429 				NECPLOG0(LOG_INFO, "necp_client_add, multiple parent flows not supported");
7430 				break;
7431 			}
7432 			if (parsed_parameters.ip_protocol != IPPROTO_UDP) {
7433 				NECPLOG(LOG_INFO, "necp_client_add, flow demux pattern not supported for %d protocol",
7434 				    parsed_parameters.ip_protocol);
7435 				break;
7436 			}
7437 			if (parsed_parameters.ip_protocol != parent_parameters.ip_protocol) {
7438 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child ip protocol mismatch");
7439 				break;
7440 			}
7441 			if (parsed_parameters.local_addr.sa.sa_family != AF_INET && parsed_parameters.local_addr.sa.sa_family != AF_INET6) {
7442 				NECPLOG(LOG_INFO, "necp_client_add, flow demux pattern not supported for %d family",
7443 				    parsed_parameters.local_addr.sa.sa_family);
7444 				break;
7445 			}
7446 			if (parsed_parameters.local_addr.sa.sa_family != parsed_parameters.remote_addr.sa.sa_family) {
7447 				NECPLOG0(LOG_INFO, "necp_client_add, local/remote address family mismatch");
7448 				break;
7449 			}
7450 			if (parsed_parameters.local_addr.sa.sa_family != parent_parameters.local_addr.sa.sa_family) {
7451 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child address family mismatch");
7452 				break;
7453 			}
7454 			if (SOCKADDR_CMP(&parsed_parameters.local_addr.sa, &parent_parameters.local_addr.sa, parsed_parameters.local_addr.sa.sa_len)) {
7455 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child local address mismatch");
7456 				break;
7457 			}
7458 			if (SOCKADDR_CMP(&parsed_parameters.remote_addr.sa, &parent_parameters.remote_addr.sa, parsed_parameters.remote_addr.sa.sa_len)) {
7459 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child remote address mismatch");
7460 				break;
7461 			}
7462 			if (parsed_parameters.local_addr.sin.sin_port != parent_parameters.local_addr.sin.sin_port) {
7463 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child local port mismatch");
7464 				break;
7465 			}
7466 			if (parsed_parameters.remote_addr.sin.sin_port != parent_parameters.remote_addr.sin.sin_port) {
7467 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child remote port mismatch");
7468 				break;
7469 			}
7470 			client->validated_parent = 1;
7471 			uuid_copy(client->parent_client_id, parent_client_id);
7472 		} while (false);
7473 	}
7474 
7475 #endif /* !SKYWALK */
7476 
7477 	necp_client_update_observer_add(client);
7478 
7479 	NECP_FD_LOCK(fd_data);
7480 	RB_INSERT(_necp_client_tree, &fd_data->clients, client);
7481 	OSIncrementAtomic(&necp_client_count);
7482 	NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
7483 	RB_INSERT(_necp_client_global_tree, &necp_client_global_tree, client);
7484 	NECP_CLIENT_TREE_UNLOCK();
7485 
7486 	// Prime the client result
7487 	NECP_CLIENT_LOCK(client);
7488 	(void)necp_update_client_result(current_proc(), fd_data, client, NULL);
7489 	necp_client_retain_locked(client);
7490 	NECP_CLIENT_UNLOCK(client);
7491 	NECP_FD_UNLOCK(fd_data);
7492 	// Now everything is set, it's safe to plumb this in to NetworkStatistics
7493 	uint32_t ntstat_properties = 0;
7494 	necp_find_conn_netstat_data(client, &ntstat_properties, NULL, NULL, NULL, NULL, NULL);
7495 
7496 	client->nstat_context = nstat_provider_stats_open((nstat_provider_context)client,
7497 	    NSTAT_PROVIDER_CONN_USERLAND, (u_int64_t)ntstat_properties, necp_request_conn_netstats, necp_find_conn_extension_info);
7498 	necp_client_release(client);
7499 done:
7500 	if (error != 0 && client != NULL) {
7501 		necp_client_free(client);
7502 		client = NULL;
7503 	}
7504 	*retval = error;
7505 
7506 	return error;
7507 }
7508 
7509 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)7510 necp_client_claim(struct proc *p, struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7511 {
7512 	int error = 0;
7513 	uuid_t client_id = {};
7514 	struct necp_client *client = NULL;
7515 
7516 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
7517 		error = EINVAL;
7518 		goto done;
7519 	}
7520 
7521 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
7522 	if (error) {
7523 		NECPLOG(LOG_ERR, "necp_client_claim copyin client_id error (%d)", error);
7524 		goto done;
7525 	}
7526 
7527 	if (necp_client_id_is_flow(client_id)) {
7528 		NECPLOG0(LOG_ERR, "necp_client_claim cannot claim from flow UUID");
7529 		error = EINVAL;
7530 		goto done;
7531 	}
7532 
7533 	u_int64_t upid = proc_uniqueid(p);
7534 
7535 	NECP_FD_LIST_LOCK_SHARED();
7536 
7537 	struct necp_fd_data *find_fd = NULL;
7538 	LIST_FOREACH(find_fd, &necp_fd_list, chain) {
7539 		NECP_FD_LOCK(find_fd);
7540 		struct necp_client *find_client = necp_client_fd_find_client_and_lock(find_fd, client_id);
7541 		if (find_client != NULL) {
7542 			if (find_client->delegated_upid == upid &&
7543 			    RB_EMPTY(&find_client->flow_registrations)) {
7544 				// Matched the client to claim; remove from the old fd
7545 				client = find_client;
7546 				RB_REMOVE(_necp_client_tree, &find_fd->clients, client);
7547 				necp_client_retain_locked(client);
7548 			}
7549 			NECP_CLIENT_UNLOCK(find_client);
7550 		}
7551 		NECP_FD_UNLOCK(find_fd);
7552 
7553 		if (client != NULL) {
7554 			break;
7555 		}
7556 	}
7557 
7558 	NECP_FD_LIST_UNLOCK();
7559 
7560 	if (client == NULL) {
7561 		error = ENOENT;
7562 		goto done;
7563 	}
7564 
7565 	client->proc_pid = fd_data->proc_pid; // Transfer client to claiming pid
7566 	client->agent_handle = (void *)fd_data;
7567 	client->platform_binary = ((csproc_get_platform_binary(p) == 0) ? 0 : 1);
7568 
7569 	NECP_CLIENT_LOG(client, "Claiming client");
7570 
7571 	// Add matched client to our fd and re-run result
7572 	NECP_FD_LOCK(fd_data);
7573 	RB_INSERT(_necp_client_tree, &fd_data->clients, client);
7574 	NECP_CLIENT_LOCK(client);
7575 	(void)necp_update_client_result(current_proc(), fd_data, client, NULL);
7576 	NECP_CLIENT_UNLOCK(client);
7577 	NECP_FD_UNLOCK(fd_data);
7578 
7579 	necp_client_release(client);
7580 
7581 done:
7582 	*retval = error;
7583 
7584 	return error;
7585 }
7586 
7587 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_remove(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)7588 necp_client_remove(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7589 {
7590 	int error = 0;
7591 	uuid_t client_id = {};
7592 	struct ifnet_stats_per_flow flow_ifnet_stats = {};
7593 	const size_t buffer_size = uap->buffer_size;
7594 
7595 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
7596 		error = EINVAL;
7597 		goto done;
7598 	}
7599 
7600 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
7601 	if (error) {
7602 		NECPLOG(LOG_ERR, "necp_client_remove copyin client_id error (%d)", error);
7603 		goto done;
7604 	}
7605 
7606 	if (uap->buffer != 0 && buffer_size == sizeof(flow_ifnet_stats)) {
7607 		error = copyin(uap->buffer, &flow_ifnet_stats, buffer_size);
7608 		if (error) {
7609 			NECPLOG(LOG_ERR, "necp_client_remove flow_ifnet_stats copyin error (%d)", error);
7610 			// Not fatal; make sure to zero-out stats in case of partial copy
7611 			memset(&flow_ifnet_stats, 0, sizeof(flow_ifnet_stats));
7612 			error = 0;
7613 		}
7614 	} else if (uap->buffer != 0) {
7615 		NECPLOG(LOG_ERR, "necp_client_remove unexpected parameters length (%zu)", buffer_size);
7616 	}
7617 
7618 	NECP_FD_LOCK(fd_data);
7619 
7620 	pid_t pid = fd_data->proc_pid;
7621 	struct necp_client *client = necp_client_fd_find_client_unlocked(fd_data, client_id);
7622 
7623 	NECP_CLIENT_LOG(client, "Removing client");
7624 
7625 	if (client != NULL) {
7626 		// Remove any flow registrations that match
7627 		struct necp_client_flow_registration *flow_registration = NULL;
7628 		struct necp_client_flow_registration *temp_flow_registration = NULL;
7629 		RB_FOREACH_SAFE(flow_registration, _necp_fd_flow_tree, &fd_data->flows, temp_flow_registration) {
7630 			if (flow_registration->client == client) {
7631 #if SKYWALK
7632 				necp_destroy_flow_stats(fd_data, flow_registration, NULL, TRUE);
7633 #endif /* SKYWALK */
7634 				NECP_FLOW_TREE_LOCK_EXCLUSIVE();
7635 				RB_REMOVE(_necp_client_flow_global_tree, &necp_client_flow_global_tree, flow_registration);
7636 				NECP_FLOW_TREE_UNLOCK();
7637 				RB_REMOVE(_necp_fd_flow_tree, &fd_data->flows, flow_registration);
7638 			}
7639 		}
7640 #if SKYWALK
7641 		if (client->nstat_context != NULL) {
7642 			// Main path, we expect stats to be in existance at this point
7643 			nstat_provider_stats_close(client->nstat_context);
7644 			client->nstat_context = NULL;
7645 		} else {
7646 			NECPLOG0(LOG_ERR, "necp_client_remove ntstat shutdown finds nstat_context NULL");
7647 		}
7648 #endif /* SKYWALK */
7649 		// Remove client from lists
7650 		NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
7651 		RB_REMOVE(_necp_client_global_tree, &necp_client_global_tree, client);
7652 		NECP_CLIENT_TREE_UNLOCK();
7653 		RB_REMOVE(_necp_client_tree, &fd_data->clients, client);
7654 	}
7655 
7656 #if SKYWALK
7657 	// If the currently-active arena is idle (has no more flows referring to it), or if there are defunct
7658 	// arenas lingering in the list, schedule a threadcall to do the clean up.  The idle check is done
7659 	// by checking if the reference count is 3: one held by this client (will be released below when we
7660 	// destroy it) when it's non-NULL; the rest held by stats_arena_{active,list}.
7661 	if ((fd_data->stats_arena_active != NULL && fd_data->stats_arena_active->nai_use_count == 3) ||
7662 	    (fd_data->stats_arena_active == NULL && !LIST_EMPTY(&fd_data->stats_arena_list))) {
7663 		uint64_t deadline = 0;
7664 		uint64_t leeway = 0;
7665 		clock_interval_to_deadline(necp_close_arenas_timeout_microseconds, NSEC_PER_USEC, &deadline);
7666 		clock_interval_to_absolutetime_interval(necp_close_arenas_timeout_leeway_microseconds, NSEC_PER_USEC, &leeway);
7667 
7668 		thread_call_enter_delayed_with_leeway(necp_close_empty_arenas_tcall, NULL,
7669 		    deadline, leeway, THREAD_CALL_DELAY_LEEWAY);
7670 	}
7671 #endif /* SKYWALK */
7672 
7673 	NECP_FD_UNLOCK(fd_data);
7674 
7675 	if (client != NULL) {
7676 		ASSERT(error == 0);
7677 		necp_destroy_client(client, pid, true);
7678 	} else {
7679 		error = ENOENT;
7680 		NECPLOG(LOG_ERR, "necp_client_remove invalid client_id (%d)", error);
7681 	}
7682 done:
7683 	*retval = error;
7684 
7685 	return error;
7686 }
7687 
7688 static struct necp_client_flow_registration *
necp_client_fd_find_flow(struct necp_fd_data * client_fd,uuid_t flow_id)7689 necp_client_fd_find_flow(struct necp_fd_data *client_fd, uuid_t flow_id)
7690 {
7691 	NECP_FD_ASSERT_LOCKED(client_fd);
7692 	struct necp_client_flow_registration *flow = NULL;
7693 
7694 	if (necp_client_id_is_flow(flow_id)) {
7695 		struct necp_client_flow_registration find;
7696 		uuid_copy(find.registration_id, flow_id);
7697 		flow = RB_FIND(_necp_fd_flow_tree, &client_fd->flows, &find);
7698 	}
7699 
7700 	return flow;
7701 }
7702 
7703 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_remove_flow(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)7704 necp_client_remove_flow(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7705 {
7706 	int error = 0;
7707 	uuid_t flow_id = {};
7708 	struct ifnet_stats_per_flow flow_ifnet_stats = {};
7709 	const size_t buffer_size = uap->buffer_size;
7710 
7711 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
7712 		error = EINVAL;
7713 		NECPLOG(LOG_ERR, "necp_client_remove_flow invalid client_id (length %zu)", (size_t)uap->client_id_len);
7714 		goto done;
7715 	}
7716 
7717 	error = copyin(uap->client_id, flow_id, sizeof(uuid_t));
7718 	if (error) {
7719 		NECPLOG(LOG_ERR, "necp_client_remove_flow copyin client_id error (%d)", error);
7720 		goto done;
7721 	}
7722 
7723 	if (uap->buffer != 0 && buffer_size == sizeof(flow_ifnet_stats)) {
7724 		error = copyin(uap->buffer, &flow_ifnet_stats, buffer_size);
7725 		if (error) {
7726 			NECPLOG(LOG_ERR, "necp_client_remove flow_ifnet_stats copyin error (%d)", error);
7727 			// Not fatal
7728 		}
7729 	} else if (uap->buffer != 0) {
7730 		NECPLOG(LOG_ERR, "necp_client_remove unexpected parameters length (%zu)", buffer_size);
7731 	}
7732 
7733 	NECP_FD_LOCK(fd_data);
7734 	struct necp_client *client = NULL;
7735 	struct necp_client_flow_registration *flow_registration = necp_client_fd_find_flow(fd_data, flow_id);
7736 	if (flow_registration != NULL) {
7737 #if SKYWALK
7738 		// Cleanup stats per flow
7739 		necp_destroy_flow_stats(fd_data, flow_registration, &flow_ifnet_stats, TRUE);
7740 #endif /* SKYWALK */
7741 		NECP_FLOW_TREE_LOCK_EXCLUSIVE();
7742 		RB_REMOVE(_necp_client_flow_global_tree, &necp_client_flow_global_tree, flow_registration);
7743 		NECP_FLOW_TREE_UNLOCK();
7744 		RB_REMOVE(_necp_fd_flow_tree, &fd_data->flows, flow_registration);
7745 
7746 		client = flow_registration->client;
7747 		if (client != NULL) {
7748 			necp_client_retain(client);
7749 		}
7750 	}
7751 	NECP_FD_UNLOCK(fd_data);
7752 
7753 	NECP_CLIENT_FLOW_LOG(client, flow_registration, "removing flow");
7754 
7755 	if (flow_registration != NULL && client != NULL) {
7756 		NECP_CLIENT_LOCK(client);
7757 		if (flow_registration->client == client) {
7758 			necp_destroy_client_flow_registration(client, flow_registration, fd_data->proc_pid, false);
7759 		}
7760 		necp_client_release_locked(client);
7761 		NECP_CLIENT_UNLOCK(client);
7762 	}
7763 
7764 done:
7765 	*retval = error;
7766 	if (error != 0) {
7767 		NECPLOG(LOG_ERR, "Remove flow error (%d)", error);
7768 	}
7769 
7770 	return error;
7771 }
7772 
7773 // Don't inline the function since it includes necp_client_parsed_parameters on the stack
7774 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)7775 necp_client_check_tcp_heuristics(struct necp_client *client, struct necp_client_flow *flow,
7776     u_int32_t *flags, u_int8_t *__counted_by(tfo_cookie_maxlen) tfo_cookie, u_int8_t tfo_cookie_maxlen,
7777     u_int8_t *tfo_cookie_len)
7778 {
7779 	struct necp_client_parsed_parameters parsed_parameters;
7780 	int error = 0;
7781 
7782 	error = necp_client_parse_parameters(client, client->parameters,
7783 	    (u_int32_t)client->parameters_length,
7784 	    &parsed_parameters);
7785 	if (error) {
7786 		NECPLOG(LOG_ERR, "necp_client_parse_parameters error (%d)", error);
7787 		return error;
7788 	}
7789 
7790 	if ((flow->remote_addr.sa.sa_family != AF_INET &&
7791 	    flow->remote_addr.sa.sa_family != AF_INET6) ||
7792 	    (flow->local_addr.sa.sa_family != AF_INET &&
7793 	    flow->local_addr.sa.sa_family != AF_INET6)) {
7794 		return EINVAL;
7795 	}
7796 
7797 	NECP_CLIENT_ROUTE_LOCK(client);
7798 
7799 	if (client->current_route == NULL) {
7800 		error = ENOENT;
7801 		goto do_unlock;
7802 	}
7803 
7804 	bool check_ecn = false;
7805 	do {
7806 		if ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_ECN_ENABLE) ==
7807 		    NECP_CLIENT_PARAMETER_FLAG_ECN_ENABLE) {
7808 			check_ecn = true;
7809 			break;
7810 		}
7811 
7812 		if ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_ECN_DISABLE) ==
7813 		    NECP_CLIENT_PARAMETER_FLAG_ECN_DISABLE) {
7814 			break;
7815 		}
7816 
7817 		if (client->current_route != NULL) {
7818 			if (client->current_route->rt_ifp->if_eflags & IFEF_ECN_ENABLE) {
7819 				check_ecn = true;
7820 				break;
7821 			}
7822 			if (client->current_route->rt_ifp->if_eflags & IFEF_ECN_DISABLE) {
7823 				break;
7824 			}
7825 		}
7826 
7827 		bool inbound = ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) == 0);
7828 		if ((inbound && tcp_ecn_inbound == 1) ||
7829 		    (!inbound && tcp_ecn_outbound == 1)) {
7830 			check_ecn = true;
7831 		}
7832 	} while (false);
7833 
7834 	if (check_ecn) {
7835 		if (tcp_heuristic_do_ecn_with_address(client->current_route->rt_ifp,
7836 		    (union sockaddr_in_4_6 *)&flow->local_addr)) {
7837 			*flags |= NECP_CLIENT_RESULT_FLAG_ECN_ENABLED;
7838 		}
7839 	}
7840 
7841 	if ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_TFO_ENABLE) ==
7842 	    NECP_CLIENT_PARAMETER_FLAG_TFO_ENABLE) {
7843 		if (!tcp_heuristic_do_tfo_with_address(client->current_route->rt_ifp,
7844 		    (union sockaddr_in_4_6 *)&flow->local_addr,
7845 		    (union sockaddr_in_4_6 *)&flow->remote_addr,
7846 		    tfo_cookie, tfo_cookie_maxlen, tfo_cookie_len)) {
7847 			*flags |= NECP_CLIENT_RESULT_FLAG_FAST_OPEN_BLOCKED;
7848 			*tfo_cookie_len = 0;
7849 		}
7850 	} else {
7851 		*flags |= NECP_CLIENT_RESULT_FLAG_FAST_OPEN_BLOCKED;
7852 		*tfo_cookie_len = 0;
7853 	}
7854 do_unlock:
7855 	NECP_CLIENT_ROUTE_UNLOCK(client);
7856 
7857 	return error;
7858 }
7859 
7860 static size_t
necp_client_calculate_flow_tlv_size(struct necp_client_flow_registration * flow_registration)7861 necp_client_calculate_flow_tlv_size(struct necp_client_flow_registration *flow_registration)
7862 {
7863 	size_t assigned_results_size = 0;
7864 	struct necp_client_flow *flow = NULL;
7865 	LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
7866 		if (flow->assigned || !necp_client_endpoint_is_unspecified((struct necp_client_endpoint *)&flow->remote_addr)) {
7867 			size_t header_length = 0;
7868 			if (flow->nexus) {
7869 				header_length = sizeof(struct necp_client_nexus_flow_header);
7870 			} else {
7871 				header_length = sizeof(struct necp_client_flow_header);
7872 			}
7873 			assigned_results_size += (header_length + flow->assigned_results_length);
7874 
7875 			if (flow->has_protoctl_event) {
7876 				assigned_results_size += sizeof(struct necp_client_flow_protoctl_event_header);
7877 			}
7878 		}
7879 	}
7880 	return assigned_results_size;
7881 }
7882 
7883 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)7884 necp_client_fillout_flow_tlvs(struct necp_client *client,
7885     bool client_is_observed,
7886     struct necp_client_flow_registration *flow_registration,
7887     struct necp_client_action_args *uap,
7888     size_t *assigned_results_cursor)
7889 {
7890 	int error = 0;
7891 	struct necp_client_flow *flow = NULL;
7892 	LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
7893 		if (flow->assigned || !necp_client_endpoint_is_unspecified((struct necp_client_endpoint *)&flow->remote_addr)) {
7894 			// Write TLV headers
7895 			struct necp_client_nexus_flow_header header = {};
7896 			u_int32_t length = 0;
7897 			u_int32_t flags = 0;
7898 			u_int8_t tfo_cookie_len = 0;
7899 			u_int8_t type = 0;
7900 
7901 			type = NECP_CLIENT_RESULT_FLOW_ID;
7902 			length = sizeof(header.flow_header.flow_id);
7903 			header.flow_header.flow_id_tlv_header.type = type;
7904 			header.flow_header.flow_id_tlv_header.length = length;
7905 			uuid_copy(header.flow_header.flow_id, flow_registration->registration_id);
7906 
7907 			if (flow->nexus) {
7908 				if (flow->check_tcp_heuristics) {
7909 					u_int8_t tfo_cookie[NECP_TFO_COOKIE_LEN_MAX];
7910 					tfo_cookie_len = NECP_TFO_COOKIE_LEN_MAX;
7911 
7912 					if (necp_client_check_tcp_heuristics(client, flow, &flags,
7913 					    tfo_cookie, tfo_cookie_len, &tfo_cookie_len) != 0) {
7914 						tfo_cookie_len = 0;
7915 					} else {
7916 						flow->check_tcp_heuristics = FALSE;
7917 
7918 						if (tfo_cookie_len != 0) {
7919 							type = NECP_CLIENT_RESULT_TFO_COOKIE;
7920 							length = tfo_cookie_len;
7921 							header.tfo_cookie_tlv_header.type = type;
7922 							header.tfo_cookie_tlv_header.length = length;
7923 							memcpy(&header.tfo_cookie_value, tfo_cookie, tfo_cookie_len);
7924 						}
7925 					}
7926 				}
7927 			}
7928 
7929 			size_t header_length = 0;
7930 			if (flow->nexus) {
7931 				if (tfo_cookie_len != 0) {
7932 					header_length = sizeof(struct necp_client_nexus_flow_header) - (NECP_TFO_COOKIE_LEN_MAX - tfo_cookie_len);
7933 				} else {
7934 					header_length = sizeof(struct necp_client_nexus_flow_header) - sizeof(struct necp_tlv_header) - NECP_TFO_COOKIE_LEN_MAX;
7935 				}
7936 			} else {
7937 				header_length = sizeof(struct necp_client_flow_header);
7938 			}
7939 
7940 			type = NECP_CLIENT_RESULT_FLAGS;
7941 			length = sizeof(header.flow_header.flags_value);
7942 			header.flow_header.flags_tlv_header.type = type;
7943 			header.flow_header.flags_tlv_header.length = length;
7944 			if (flow->assigned) {
7945 				flags |= NECP_CLIENT_RESULT_FLAG_FLOW_ASSIGNED;
7946 			}
7947 			if (flow->viable) {
7948 				flags |= NECP_CLIENT_RESULT_FLAG_FLOW_VIABLE;
7949 			}
7950 			if (flow_registration->defunct) {
7951 				flags |= NECP_CLIENT_RESULT_FLAG_DEFUNCT;
7952 			}
7953 			flags |= flow->necp_flow_flags;
7954 			header.flow_header.flags_value = flags;
7955 
7956 			type = NECP_CLIENT_RESULT_INTERFACE;
7957 			length = sizeof(header.flow_header.interface_value);
7958 			header.flow_header.interface_tlv_header.type = type;
7959 			header.flow_header.interface_tlv_header.length = length;
7960 
7961 			struct necp_client_result_interface interface_struct;
7962 			interface_struct.generation = 0;
7963 			interface_struct.index = flow->interface_index;
7964 
7965 			header.flow_header.interface_value = interface_struct;
7966 			if (flow->nexus) {
7967 				type = NECP_CLIENT_RESULT_NETAGENT;
7968 				length = sizeof(header.agent_value);
7969 				header.agent_tlv_header.type = type;
7970 				header.agent_tlv_header.length = length;
7971 
7972 				struct necp_client_result_netagent agent_struct;
7973 				uuid_copy(agent_struct.netagent_uuid, flow->u.nexus_agent);
7974 				agent_struct.generation = netagent_get_generation(agent_struct.netagent_uuid);
7975 
7976 				header.agent_value = agent_struct;
7977 			}
7978 
7979 			// Don't include outer TLV header in length field
7980 			type = NECP_CLIENT_RESULT_FLOW;
7981 			length = (header_length - sizeof(struct necp_tlv_header) + flow->assigned_results_length);
7982 			if (flow->has_protoctl_event) {
7983 				length += sizeof(struct necp_client_flow_protoctl_event_header);
7984 			}
7985 			header.flow_header.outer_header.type = type;
7986 			header.flow_header.outer_header.length = length;
7987 
7988 			error = copyout(&header, uap->buffer + client->result_length + *assigned_results_cursor, header_length);
7989 			if (error) {
7990 				NECPLOG(LOG_ERR, "necp_client_copy assigned results tlv_header copyout error (%d)", error);
7991 				return error;
7992 			}
7993 			*assigned_results_cursor += header_length;
7994 
7995 			if (flow->assigned_results && flow->assigned_results_length) {
7996 				// Write inner TLVs
7997 				error = copyout(flow->assigned_results, uap->buffer + client->result_length + *assigned_results_cursor,
7998 				    flow->assigned_results_length);
7999 				if (error) {
8000 					NECPLOG(LOG_ERR, "necp_client_copy assigned results copyout error (%d)", error);
8001 					return error;
8002 				}
8003 			}
8004 			*assigned_results_cursor += flow->assigned_results_length;
8005 
8006 			/* Read the protocol event and reset it */
8007 			if (flow->has_protoctl_event) {
8008 				struct necp_client_flow_protoctl_event_header protoctl_event_header = {};
8009 
8010 				type = NECP_CLIENT_RESULT_PROTO_CTL_EVENT;
8011 				length = sizeof(protoctl_event_header.protoctl_event);
8012 
8013 				protoctl_event_header.protoctl_tlv_header.type = type;
8014 				protoctl_event_header.protoctl_tlv_header.length = length;
8015 				protoctl_event_header.protoctl_event = flow->protoctl_event;
8016 
8017 				error = copyout(&protoctl_event_header, uap->buffer + client->result_length + *assigned_results_cursor,
8018 				    sizeof(protoctl_event_header));
8019 
8020 				if (error) {
8021 					NECPLOG(LOG_ERR, "necp_client_copy protocol control event results"
8022 					    " tlv_header copyout error (%d)", error);
8023 					return error;
8024 				}
8025 				*assigned_results_cursor += sizeof(protoctl_event_header);
8026 				flow->has_protoctl_event = FALSE;
8027 				flow->protoctl_event.protoctl_event_code = 0;
8028 				flow->protoctl_event.protoctl_event_val = 0;
8029 				flow->protoctl_event.protoctl_event_tcp_seq_num = 0;
8030 			}
8031 		}
8032 	}
8033 	if (!client_is_observed) {
8034 		flow_registration->flow_result_read = TRUE;
8035 	}
8036 	return 0;
8037 }
8038 
8039 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)8040 necp_client_copy_internal(struct necp_client *client, uuid_t client_id, bool client_is_observed, struct necp_client_action_args *uap, int *retval)
8041 {
8042 	NECP_CLIENT_ASSERT_LOCKED(client);
8043 	int error = 0;
8044 	// Copy results out
8045 	if (uap->action == NECP_CLIENT_ACTION_COPY_PARAMETERS) {
8046 		if (uap->buffer_size < client->parameters_length) {
8047 			return EINVAL;
8048 		}
8049 		error = copyout(client->parameters, uap->buffer, client->parameters_length);
8050 		if (error) {
8051 			NECPLOG(LOG_ERR, "necp_client_copy parameters copyout error (%d)", error);
8052 			return error;
8053 		}
8054 		*retval = client->parameters_length;
8055 	} else if ((uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT || uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT_FINAL) &&
8056 	    client->result_read && client->group_members_read && !necp_client_has_unread_flows(client)) {
8057 		// Copy updates only, but nothing to read
8058 		// Just return 0 for bytes read
8059 		*retval = 0;
8060 	} else if (uap->action == NECP_CLIENT_ACTION_COPY_RESULT ||
8061 	    uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT ||
8062 	    uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT_FINAL) {
8063 		size_t assigned_results_size = client->assigned_group_members_length;
8064 
8065 		bool some_flow_is_defunct = false;
8066 		struct necp_client_flow_registration *single_flow_registration = NULL;
8067 		if (necp_client_id_is_flow(client_id)) {
8068 			single_flow_registration = necp_client_find_flow(client, client_id);
8069 			if (single_flow_registration != NULL) {
8070 				assigned_results_size += necp_client_calculate_flow_tlv_size(single_flow_registration);
8071 			}
8072 		} else {
8073 			// This request is for the client, so copy everything
8074 			struct necp_client_flow_registration *flow_registration = NULL;
8075 			RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
8076 				if (flow_registration->defunct) {
8077 					some_flow_is_defunct = true;
8078 				}
8079 				assigned_results_size += necp_client_calculate_flow_tlv_size(flow_registration);
8080 			}
8081 		}
8082 		if (uap->buffer_size < (client->result_length + assigned_results_size)) {
8083 			if (uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT_FINAL) {
8084 				// Mark the client and all flows as read to prevent looping
8085 				client->result_read = true;
8086 				struct necp_client_flow_registration *flow_registration = NULL;
8087 				RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
8088 					flow_registration->flow_result_read = true;
8089 				}
8090 			}
8091 			return EINVAL;
8092 		}
8093 
8094 		u_int32_t original_flags = 0;
8095 		bool flags_updated = false;
8096 		if (some_flow_is_defunct && client->legacy_client_is_flow) {
8097 			// If our client expects the defunct flag in the client, add it now
8098 			u_int32_t client_flags = 0;
8099 			u_int32_t value_size = 0;
8100 			u_int8_t *flags_pointer = necp_buffer_get_tlv_value(client->result, client->result_length, 0, &value_size);
8101 			if (flags_pointer != NULL && value_size == sizeof(client_flags)) {
8102 				memcpy(&client_flags, flags_pointer, value_size);
8103 				original_flags = client_flags;
8104 				client_flags |= NECP_CLIENT_RESULT_FLAG_DEFUNCT;
8105 				(void)necp_buffer_write_tlv_if_different(client->result, NECP_CLIENT_RESULT_FLAGS,
8106 				    sizeof(client_flags), &client_flags, &flags_updated,
8107 				    client->result, sizeof(client->result));
8108 			}
8109 		}
8110 
8111 		error = copyout(client->result, uap->buffer, client->result_length);
8112 
8113 		if (flags_updated) {
8114 			// Revert stored flags
8115 			(void)necp_buffer_write_tlv_if_different(client->result, NECP_CLIENT_RESULT_FLAGS,
8116 			    sizeof(original_flags), &original_flags, &flags_updated,
8117 			    client->result, sizeof(client->result));
8118 		}
8119 
8120 		if (error != 0) {
8121 			NECPLOG(LOG_ERR, "necp_client_copy result copyout error (%d)", error);
8122 			return error;
8123 		}
8124 
8125 		if (client->assigned_group_members != NULL && client->assigned_group_members_length > 0) {
8126 			error = copyout(client->assigned_group_members, uap->buffer + client->result_length, client->assigned_group_members_length);
8127 			if (error != 0) {
8128 				NECPLOG(LOG_ERR, "necp_client_copy group members copyout error (%d)", error);
8129 				return error;
8130 			}
8131 		}
8132 
8133 		size_t assigned_results_cursor = client->assigned_group_members_length; // Start with an offset based on the group members
8134 		if (necp_client_id_is_flow(client_id)) {
8135 			if (single_flow_registration != NULL) {
8136 				error = necp_client_fillout_flow_tlvs(client, client_is_observed, single_flow_registration, uap, &assigned_results_cursor);
8137 				if (error != 0) {
8138 					return error;
8139 				}
8140 			}
8141 		} else {
8142 			// This request is for the client, so copy everything
8143 			struct necp_client_flow_registration *flow_registration = NULL;
8144 			RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
8145 				error = necp_client_fillout_flow_tlvs(client, client_is_observed, flow_registration, uap, &assigned_results_cursor);
8146 				if (error != 0) {
8147 					return error;
8148 				}
8149 			}
8150 		}
8151 
8152 		*retval = client->result_length + assigned_results_cursor;
8153 
8154 		if (!client_is_observed) {
8155 			client->result_read = TRUE;
8156 			client->group_members_read = TRUE;
8157 		}
8158 	}
8159 
8160 	return 0;
8161 }
8162 
8163 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_copy(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8164 necp_client_copy(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8165 {
8166 	int error = 0;
8167 	struct necp_client *client = NULL;
8168 	uuid_t client_id;
8169 	uuid_clear(client_id);
8170 
8171 	*retval = 0;
8172 
8173 	if (uap->buffer_size == 0 || uap->buffer == 0) {
8174 		return EINVAL;
8175 	}
8176 
8177 	if (uap->action != NECP_CLIENT_ACTION_COPY_PARAMETERS &&
8178 	    uap->action != NECP_CLIENT_ACTION_COPY_RESULT &&
8179 	    uap->action != NECP_CLIENT_ACTION_COPY_UPDATED_RESULT &&
8180 	    uap->action != NECP_CLIENT_ACTION_COPY_UPDATED_RESULT_FINAL) {
8181 		return EINVAL;
8182 	}
8183 
8184 	if (uap->client_id) {
8185 		if (uap->client_id_len != sizeof(uuid_t)) {
8186 			NECPLOG(LOG_ERR, "Incorrect length (got %zu, expected %zu)", (size_t)uap->client_id_len, sizeof(uuid_t));
8187 			return ERANGE;
8188 		}
8189 
8190 		error = copyin(uap->client_id, client_id, sizeof(uuid_t));
8191 		if (error) {
8192 			NECPLOG(LOG_ERR, "necp_client_copy client_id copyin error (%d)", error);
8193 			return error;
8194 		}
8195 	}
8196 
8197 	const bool is_wildcard = (bool)uuid_is_null(client_id);
8198 
8199 	NECP_FD_LOCK(fd_data);
8200 
8201 	bool send_in_process_flow_divert_message = false;
8202 	if (is_wildcard) {
8203 		if (uap->action == NECP_CLIENT_ACTION_COPY_RESULT ||
8204 		    uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT ||
8205 		    uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT_FINAL) {
8206 			struct necp_client *find_client = NULL;
8207 			RB_FOREACH(find_client, _necp_client_tree, &fd_data->clients) {
8208 				NECP_CLIENT_LOCK(find_client);
8209 				if (!find_client->result_read || !find_client->group_members_read || necp_client_has_unread_flows(find_client)) {
8210 					client = find_client;
8211 					// Leave the client locked, and break
8212 					break;
8213 				}
8214 				NECP_CLIENT_UNLOCK(find_client);
8215 			}
8216 
8217 			if (client == NULL && fd_data->request_in_process_flow_divert) {
8218 				// No client found that needs update. Check for an event requesting in-process flow divert.
8219 				send_in_process_flow_divert_message = true;
8220 			}
8221 		}
8222 	} else {
8223 		client = necp_client_fd_find_client_and_lock(fd_data, client_id);
8224 	}
8225 
8226 	if (client != NULL) {
8227 		if (!send_in_process_flow_divert_message) {
8228 			// If client is set, it is locked
8229 			error = necp_client_copy_internal(client, client_id, FALSE, uap, retval);
8230 		}
8231 		NECP_CLIENT_UNLOCK(client);
8232 	}
8233 
8234 	if (send_in_process_flow_divert_message) {
8235 		fd_data->request_in_process_flow_divert = false;
8236 
8237 		struct necp_tlv_header request_tlv = {
8238 			.type = NECP_CLIENT_RESULT_REQUEST_IN_PROCESS_FLOW_DIVERT,
8239 			.length = 0,
8240 		};
8241 		if (uap->buffer_size < sizeof(request_tlv)) {
8242 			error = EINVAL;
8243 		} else {
8244 			error = copyout(&request_tlv, uap->buffer, sizeof(request_tlv));
8245 			if (error) {
8246 				NECPLOG(LOG_ERR, "necp_client_copy request flow divert TLV copyout error (%d)", error);
8247 			} else {
8248 				*retval = sizeof(request_tlv);
8249 			}
8250 		}
8251 	}
8252 
8253 	// Unlock our own fd before moving on or returning
8254 	NECP_FD_UNLOCK(fd_data);
8255 
8256 	if (client == NULL && !send_in_process_flow_divert_message) {
8257 		if (fd_data->flags & NECP_OPEN_FLAG_OBSERVER) {
8258 			// Observers are allowed to lookup clients on other fds
8259 
8260 			// Lock tree
8261 			NECP_CLIENT_TREE_LOCK_SHARED();
8262 
8263 			bool found_client = FALSE;
8264 
8265 			client = necp_find_client_and_lock(client_id);
8266 			if (client != NULL) {
8267 				// Matched, copy out data
8268 				found_client = TRUE;
8269 				error = necp_client_copy_internal(client, client_id, TRUE, uap, retval);
8270 				NECP_CLIENT_UNLOCK(client);
8271 			}
8272 
8273 			// Unlock tree
8274 			NECP_CLIENT_TREE_UNLOCK();
8275 
8276 			// No client found, fail
8277 			if (!found_client) {
8278 				return ENOENT;
8279 			}
8280 		} else {
8281 			// No client found, and not allowed to search other fds, fail
8282 			return ENOENT;
8283 		}
8284 	}
8285 
8286 	return error;
8287 }
8288 
8289 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)8290 necp_client_copy_client_update(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8291 {
8292 	int error = 0;
8293 
8294 	*retval = 0;
8295 
8296 	if (!(fd_data->flags & NECP_OPEN_FLAG_PUSH_OBSERVER)) {
8297 		NECPLOG0(LOG_ERR, "NECP fd is not observer, cannot copy client update");
8298 		return EINVAL;
8299 	}
8300 
8301 	if (uap->client_id_len != sizeof(uuid_t) || uap->client_id == 0) {
8302 		NECPLOG0(LOG_ERR, "Client id invalid, cannot copy client update");
8303 		return EINVAL;
8304 	}
8305 
8306 	if (uap->buffer_size == 0 || uap->buffer == 0) {
8307 		NECPLOG0(LOG_ERR, "Buffer invalid, cannot copy client update");
8308 		return EINVAL;
8309 	}
8310 
8311 	NECP_FD_LOCK(fd_data);
8312 	struct necp_client_update *client_update = TAILQ_FIRST(&fd_data->update_list);
8313 	if (client_update != NULL) {
8314 		TAILQ_REMOVE(&fd_data->update_list, client_update, chain);
8315 		VERIFY(fd_data->update_count > 0);
8316 		fd_data->update_count--;
8317 	}
8318 	NECP_FD_UNLOCK(fd_data);
8319 
8320 	if (client_update != NULL) {
8321 		error = copyout(client_update->client_id, uap->client_id, sizeof(uuid_t));
8322 		if (error) {
8323 			NECPLOG(LOG_ERR, "Copy client update copyout client id error (%d)", error);
8324 		} else {
8325 			if (uap->buffer_size < client_update->update_length) {
8326 				NECPLOG(LOG_ERR, "Buffer size cannot hold update (%zu < %zu)", (size_t)uap->buffer_size, client_update->update_length);
8327 				error = EINVAL;
8328 			} else {
8329 				error = copyout(client_update->update, uap->buffer, client_update->update_length);
8330 				if (error) {
8331 					NECPLOG(LOG_ERR, "Copy client update copyout error (%d)", error);
8332 				} else {
8333 					*retval = client_update->update_length;
8334 				}
8335 			}
8336 		}
8337 
8338 		necp_client_update_free(client_update);
8339 		client_update = NULL;
8340 	} else {
8341 		error = ENOENT;
8342 	}
8343 
8344 	return error;
8345 }
8346 
8347 static int
necp_client_copy_parameters_locked(struct necp_client * client,struct necp_client_nexus_parameters * parameters)8348 necp_client_copy_parameters_locked(struct necp_client *client,
8349     struct necp_client_nexus_parameters *parameters)
8350 {
8351 	VERIFY(parameters != NULL);
8352 
8353 	struct necp_client_parsed_parameters parsed_parameters = {};
8354 	int error = necp_client_parse_parameters(client, client->parameters, (u_int32_t)client->parameters_length, &parsed_parameters);
8355 
8356 	parameters->pid = client->proc_pid;
8357 	if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID) {
8358 		parameters->epid = parsed_parameters.effective_pid;
8359 	} else {
8360 		parameters->epid = parameters->pid;
8361 	}
8362 #if SKYWALK
8363 	parameters->port_reservation = client->port_reservation;
8364 #endif /* !SKYWALK */
8365 	memcpy(&parameters->local_addr, &parsed_parameters.local_addr, sizeof(parameters->local_addr));
8366 	memcpy(&parameters->remote_addr, &parsed_parameters.remote_addr, sizeof(parameters->remote_addr));
8367 	parameters->ip_protocol = parsed_parameters.ip_protocol;
8368 	if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_TRANSPORT_PROTOCOL) {
8369 		parameters->transport_protocol = parsed_parameters.transport_protocol;
8370 	} else {
8371 		parameters->transport_protocol = parsed_parameters.ip_protocol;
8372 	}
8373 	parameters->ethertype = parsed_parameters.ethertype;
8374 	parameters->traffic_class = parsed_parameters.traffic_class;
8375 	if (uuid_is_null(client->override_euuid)) {
8376 		uuid_copy(parameters->euuid, parsed_parameters.effective_uuid);
8377 	} else {
8378 		uuid_copy(parameters->euuid, client->override_euuid);
8379 	}
8380 	parameters->is_listener = (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) ? 1 : 0;
8381 	parameters->is_interpose = (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_INTERPOSE) ? 1 : 0;
8382 	parameters->is_custom_ether = (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_CUSTOM_ETHER) ? 1 : 0;
8383 	parameters->policy_id = client->policy_id;
8384 	parameters->skip_policy_id = client->skip_policy_id;
8385 
8386 	// parse client result flag
8387 	u_int32_t client_result_flags = 0;
8388 	u_int32_t value_size = 0;
8389 	u_int8_t *flags_pointer = NULL;
8390 	flags_pointer = necp_buffer_get_tlv_value(client->result, client->result_length, 0, &value_size);
8391 	if (flags_pointer && value_size == sizeof(client_result_flags)) {
8392 		memcpy(&client_result_flags, flags_pointer, value_size);
8393 	}
8394 	parameters->allow_qos_marking = (client_result_flags & NECP_CLIENT_RESULT_FLAG_ALLOW_QOS_MARKING) ? 1 : 0;
8395 
8396 	if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR_PREFERENCE) {
8397 		if (parsed_parameters.local_address_preference == NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_DEFAULT) {
8398 			parameters->override_address_selection = false;
8399 		} else if (parsed_parameters.local_address_preference == NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_TEMPORARY) {
8400 			parameters->override_address_selection = true;
8401 			parameters->use_stable_address = false;
8402 		} else if (parsed_parameters.local_address_preference == NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_STABLE) {
8403 			parameters->override_address_selection = true;
8404 			parameters->use_stable_address = true;
8405 		}
8406 	} else {
8407 		parameters->override_address_selection = false;
8408 	}
8409 
8410 	if ((parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) &&
8411 	    (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_NO_WAKE_FROM_SLEEP)) {
8412 		parameters->no_wake_from_sleep = true;
8413 	}
8414 
8415 	if ((parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) &&
8416 	    (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_REUSE_LOCAL)) {
8417 		parameters->reuse_port = true;
8418 	}
8419 
8420 #if SKYWALK
8421 	if (!parameters->is_listener) {
8422 		if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLOW_DEMUX_PATTERN) {
8423 			if (parsed_parameters.demux_patterns[0].len == 0) {
8424 				parameters->is_demuxable_parent = 1;
8425 			} else {
8426 				if (client->validated_parent) {
8427 					ASSERT(!uuid_is_null(client->parent_client_id));
8428 
8429 					NECP_CLIENT_TREE_LOCK_SHARED();
8430 					struct necp_client *parent = necp_find_client_and_lock(client->parent_client_id);
8431 					if (parent != NULL) {
8432 						struct necp_client_flow_registration *parent_flow_registration = NULL;
8433 						RB_FOREACH(parent_flow_registration, _necp_client_flow_tree, &parent->flow_registrations) {
8434 							uuid_copy(parameters->parent_flow_uuid, parent_flow_registration->registration_id);
8435 							break;
8436 						}
8437 
8438 						NECP_CLIENT_UNLOCK(parent);
8439 					}
8440 					NECP_CLIENT_TREE_UNLOCK();
8441 
8442 					if (parsed_parameters.demux_pattern_count > 0) {
8443 						for (int i = 0; i < parsed_parameters.demux_pattern_count; i++) {
8444 							memcpy(&parameters->demux_patterns[i], &parsed_parameters.demux_patterns[i], sizeof(struct necp_demux_pattern));
8445 						}
8446 						parameters->demux_pattern_count = parsed_parameters.demux_pattern_count;
8447 					}
8448 				}
8449 			}
8450 		}
8451 	}
8452 #endif // SKYWALK
8453 
8454 	return error;
8455 }
8456 
8457 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_list(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8458 necp_client_list(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8459 {
8460 	int error = 0;
8461 	struct necp_client *find_client = NULL;
8462 	size_t copy_buffer_size = 0;
8463 	uuid_t *list = NULL;
8464 	u_int32_t requested_client_count = 0;
8465 	u_int32_t client_count = 0;
8466 
8467 	if (uap->buffer_size < sizeof(requested_client_count) || uap->buffer == 0) {
8468 		error = EINVAL;
8469 		goto done;
8470 	}
8471 
8472 	if (!(fd_data->flags & NECP_OPEN_FLAG_OBSERVER)) {
8473 		NECPLOG0(LOG_ERR, "Client does not hold necessary entitlement to list other NECP clients");
8474 		error = EACCES;
8475 		goto done;
8476 	}
8477 
8478 	error = copyin(uap->buffer, &requested_client_count, sizeof(requested_client_count));
8479 	if (error) {
8480 		goto done;
8481 	}
8482 
8483 	if (os_mul_overflow(sizeof(uuid_t), requested_client_count, &copy_buffer_size)) {
8484 		error = ERANGE;
8485 		goto done;
8486 	}
8487 
8488 	if (uap->buffer_size - sizeof(requested_client_count) != copy_buffer_size) {
8489 		error = EINVAL;
8490 		goto done;
8491 	}
8492 
8493 	if (copy_buffer_size > NECP_MAX_CLIENT_LIST_SIZE) {
8494 		error = EINVAL;
8495 		goto done;
8496 	}
8497 
8498 	if (requested_client_count > 0) {
8499 		list = (uuid_t*)kalloc_data(copy_buffer_size, Z_WAITOK | Z_ZERO);
8500 		if (list == NULL) {
8501 			error = ENOMEM;
8502 			goto done;
8503 		}
8504 	}
8505 
8506 	// Lock tree
8507 	NECP_CLIENT_TREE_LOCK_SHARED();
8508 
8509 	find_client = NULL;
8510 	RB_FOREACH(find_client, _necp_client_global_tree, &necp_client_global_tree) {
8511 		NECP_CLIENT_LOCK(find_client);
8512 		if (!uuid_is_null(find_client->client_id)) {
8513 			if (client_count < requested_client_count) {
8514 				uuid_copy(list[client_count], find_client->client_id);
8515 			}
8516 			client_count++;
8517 		}
8518 		NECP_CLIENT_UNLOCK(find_client);
8519 	}
8520 
8521 	// Unlock tree
8522 	NECP_CLIENT_TREE_UNLOCK();
8523 
8524 	error = copyout(&client_count, uap->buffer, sizeof(client_count));
8525 	if (error) {
8526 		NECPLOG(LOG_ERR, "necp_client_list buffer copyout error (%d)", error);
8527 		goto done;
8528 	}
8529 
8530 	if (requested_client_count > 0 &&
8531 	    client_count > 0 &&
8532 	    list != NULL) {
8533 		error = copyout(list, uap->buffer + sizeof(client_count), copy_buffer_size);
8534 		if (error) {
8535 			NECPLOG(LOG_ERR, "necp_client_list client count copyout error (%d)", error);
8536 			goto done;
8537 		}
8538 	}
8539 done:
8540 	if (list != NULL) {
8541 		kfree_data(list, copy_buffer_size);
8542 	}
8543 	*retval = error;
8544 
8545 	return error;
8546 }
8547 
8548 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_add_flow(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8549 necp_client_add_flow(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8550 {
8551 	int error = 0;
8552 	struct necp_client *client = NULL;
8553 	uuid_t client_id;
8554 	struct necp_client_nexus_parameters parameters = {};
8555 	struct proc *proc = PROC_NULL;
8556 	struct necp_client_add_flow * __indexable add_request = NULL;
8557 	struct necp_client_add_flow * __indexable allocated_add_request = NULL;
8558 	struct necp_client_add_flow_default default_add_request = {};
8559 	const size_t buffer_size = uap->buffer_size;
8560 
8561 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
8562 		error = EINVAL;
8563 		NECPLOG(LOG_ERR, "necp_client_add_flow invalid client_id (length %zu)", (size_t)uap->client_id_len);
8564 		goto done;
8565 	}
8566 
8567 	if (uap->buffer == 0 || buffer_size < sizeof(struct necp_client_add_flow) ||
8568 	    buffer_size > sizeof(struct necp_client_add_flow_default) * 4) {
8569 		error = EINVAL;
8570 		NECPLOG(LOG_ERR, "necp_client_add_flow invalid buffer (length %zu)", buffer_size);
8571 		goto done;
8572 	}
8573 
8574 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
8575 	if (error) {
8576 		NECPLOG(LOG_ERR, "necp_client_add_flow copyin client_id error (%d)", error);
8577 		goto done;
8578 	}
8579 
8580 	if (buffer_size <= sizeof(struct necp_client_add_flow_default)) {
8581 		// Fits in default size
8582 		error = copyin(uap->buffer, &default_add_request, buffer_size);
8583 		if (error) {
8584 			NECPLOG(LOG_ERR, "necp_client_add_flow copyin default_add_request error (%d)", error);
8585 			goto done;
8586 		}
8587 
8588 		add_request = (struct necp_client_add_flow *)&default_add_request;
8589 	} else {
8590 		allocated_add_request = (struct necp_client_add_flow *)kalloc_data(buffer_size, Z_WAITOK | Z_ZERO);
8591 		if (allocated_add_request == NULL) {
8592 			error = ENOMEM;
8593 			goto done;
8594 		}
8595 
8596 		error = copyin(uap->buffer, allocated_add_request, buffer_size);
8597 		if (error) {
8598 			NECPLOG(LOG_ERR, "necp_client_add_flow copyin default_add_request error (%d)", error);
8599 			goto done;
8600 		}
8601 
8602 		add_request = allocated_add_request;
8603 	}
8604 
8605 	NECP_FD_LOCK(fd_data);
8606 	pid_t pid = fd_data->proc_pid;
8607 	proc = proc_find(pid);
8608 	if (proc == PROC_NULL) {
8609 		NECP_FD_UNLOCK(fd_data);
8610 		NECPLOG(LOG_ERR, "necp_client_add_flow process not found for pid %d error (%d)", pid, error);
8611 		error = ESRCH;
8612 		goto done;
8613 	}
8614 
8615 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
8616 	if (client == NULL) {
8617 		error = ENOENT;
8618 		NECP_FD_UNLOCK(fd_data);
8619 		goto done;
8620 	}
8621 
8622 	// Using ADD_FLOW indicates that the client supports multiple flows per client
8623 	client->legacy_client_is_flow = false;
8624 
8625 	necp_client_retain_locked(client);
8626 	necp_client_copy_parameters_locked(client, &parameters);
8627 
8628 	struct necp_client_flow_registration *new_registration = necp_client_create_flow_registration(fd_data, client);
8629 	if (new_registration == NULL) {
8630 		error = ENOMEM;
8631 		NECP_CLIENT_UNLOCK(client);
8632 		NECP_FD_UNLOCK(fd_data);
8633 		NECPLOG0(LOG_ERR, "Failed to allocate flow registration");
8634 		goto done;
8635 	}
8636 
8637 	new_registration->flags = add_request->flags;
8638 
8639 	// Copy new ID out to caller
8640 	uuid_copy(add_request->registration_id, new_registration->registration_id);
8641 
8642 	NECP_CLIENT_FLOW_LOG(client, new_registration, "adding flow");
8643 
8644 	size_t trailer_offset = (sizeof(struct necp_client_add_flow) +
8645 	    add_request->stats_request_count * sizeof(struct necp_client_flow_stats));
8646 
8647 	// Copy override address
8648 	struct sockaddr * __single override_address = NULL;
8649 	if (add_request->flags & NECP_CLIENT_FLOW_FLAGS_OVERRIDE_ADDRESS) {
8650 		size_t offset_of_address = trailer_offset;
8651 		if (buffer_size >= offset_of_address + sizeof(struct sockaddr_in)) {
8652 			override_address = flow_req_get_address(add_request, offset_of_address);
8653 			if (buffer_size >= offset_of_address + override_address->sa_len &&
8654 			    override_address->sa_len <= sizeof(parameters.remote_addr)) {
8655 				SOCKADDR_COPY(override_address, &parameters.remote_addr, override_address->sa_len);
8656 				trailer_offset += override_address->sa_len;
8657 			} else {
8658 				override_address = NULL;
8659 			}
8660 		}
8661 	}
8662 
8663 	// Copy override IP protocol
8664 	if (add_request->flags & NECP_CLIENT_FLOW_FLAGS_OVERRIDE_IP_PROTOCOL) {
8665 		size_t offset_of_ip_protocol = trailer_offset;
8666 		if (buffer_size >= offset_of_ip_protocol + sizeof(uint8_t)) {
8667 			uint8_t * __single ip_protocol_p = flow_req_get_proto(add_request, offset_of_ip_protocol);
8668 			memcpy(&parameters.ip_protocol, ip_protocol_p, sizeof(uint8_t));
8669 		}
8670 	}
8671 
8672 #if SKYWALK
8673 	if (add_request->flags & NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS) {
8674 		size_t assigned_results_length = 0;
8675 		void * __sized_by(assigned_results_length) assigned_results = NULL;
8676 		uint32_t interface_index = 0;
8677 
8678 		// Validate that the nexus UUID is assigned
8679 		bool found_nexus = false;
8680 		for (u_int32_t option_i = 0; option_i < client->interface_option_count; option_i++) {
8681 			if (option_i < NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT) {
8682 				struct necp_client_interface_option *option = &client->interface_options[option_i];
8683 				if (uuid_compare(option->nexus_agent, add_request->agent_uuid) == 0) {
8684 					interface_index = option->interface_index;
8685 					found_nexus = true;
8686 					break;
8687 				}
8688 			} else {
8689 				struct necp_client_interface_option *option = &client->extra_interface_options[option_i - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT];
8690 				if (uuid_compare(option->nexus_agent, add_request->agent_uuid) == 0) {
8691 					interface_index = option->interface_index;
8692 					found_nexus = true;
8693 					break;
8694 				}
8695 			}
8696 		}
8697 
8698 		if (!found_nexus) {
8699 			NECPLOG0(LOG_ERR, "Requested nexus not found");
8700 		} else {
8701 			necp_client_add_nexus_flow_if_needed(new_registration, add_request->agent_uuid, interface_index);
8702 
8703 			error = netagent_client_message_with_params(add_request->agent_uuid,
8704 			    ((new_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
8705 			    client->client_id :
8706 			    new_registration->registration_id),
8707 			    pid, client->agent_handle,
8708 			    NETAGENT_MESSAGE_TYPE_REQUEST_NEXUS,
8709 			    (struct necp_client_agent_parameters *)&parameters,
8710 			    &assigned_results, &assigned_results_length);
8711 			if (error != 0) {
8712 				VERIFY(assigned_results == NULL);
8713 				VERIFY(assigned_results_length == 0);
8714 				NECPLOG(LOG_ERR, "netagent_client_message error (%d)", error);
8715 			} else if (assigned_results != NULL) {
8716 				if (!necp_assign_client_result_locked(proc, fd_data, client, new_registration, add_request->agent_uuid,
8717 				    assigned_results, assigned_results_length, false, false)) {
8718 					kfree_data_sized_by(assigned_results, assigned_results_length);
8719 				}
8720 			} else if (override_address != NULL) {
8721 				// Save the overridden address in the flow. Find the correct flow,
8722 				// and assign just the address TLV. Don't set the assigned flag.
8723 				struct necp_client_flow *flow = NULL;
8724 				LIST_FOREACH(flow, &new_registration->flow_list, flow_chain) {
8725 					if (flow->nexus &&
8726 					    uuid_compare(flow->u.nexus_agent, add_request->agent_uuid) == 0) {
8727 						if (flow->assigned_results == NULL) {
8728 							SOCKADDR_COPY(override_address, &flow->remote_addr, override_address->sa_len);
8729 							uuid_t empty_uuid;
8730 							uuid_clear(empty_uuid);
8731 							size_t message_length;
8732 							void *message = necp_create_nexus_assign_message(empty_uuid, 0, NULL, 0,
8733 							    (struct necp_client_endpoint *)&flow->local_addr,
8734 							    (struct necp_client_endpoint *)&flow->remote_addr,
8735 							    NULL, 0, NULL, &message_length);
8736 							flow->assigned_results = message;
8737 							flow->assigned_results_length = message_length;
8738 						}
8739 						break;
8740 					}
8741 				}
8742 			}
8743 		}
8744 	}
8745 
8746 	// Don't request stats if nexus creation fails
8747 	if (error == 0 && add_request->stats_request_count > 0 && necp_arena_initialize(fd_data, true) == 0) {
8748 		struct necp_client_flow_stats * __single stats_request = &(necp_client_get_flow_stats(add_request))[0];
8749 		struct necp_stats_bufreq bufreq = {};
8750 
8751 		NECP_CLIENT_FLOW_LOG(client, new_registration, "Initializing stats");
8752 
8753 		bufreq.necp_stats_bufreq_id = NECP_CLIENT_STATISTICS_BUFREQ_ID;
8754 		bufreq.necp_stats_bufreq_type = stats_request->stats_type;
8755 		bufreq.necp_stats_bufreq_ver = stats_request->stats_version;
8756 		bufreq.necp_stats_bufreq_size = stats_request->stats_size;
8757 		bufreq.necp_stats_bufreq_uaddr = stats_request->stats_addr;
8758 		(void)necp_stats_initialize(fd_data, client, new_registration, &bufreq);
8759 		stats_request->stats_type = bufreq.necp_stats_bufreq_type;
8760 		stats_request->stats_version = bufreq.necp_stats_bufreq_ver;
8761 		stats_request->stats_size = bufreq.necp_stats_bufreq_size;
8762 		stats_request->stats_addr = bufreq.necp_stats_bufreq_uaddr;
8763 	}
8764 #endif /* !SKYWALK */
8765 
8766 	if (error == 0 &&
8767 	    (add_request->flags & NECP_CLIENT_FLOW_FLAGS_BROWSE ||
8768 	    add_request->flags & NECP_CLIENT_FLOW_FLAGS_RESOLVE)) {
8769 		uint32_t interface_index = IFSCOPE_NONE;
8770 		ifnet_head_lock_shared();
8771 		struct ifnet *interface = NULL;
8772 		TAILQ_FOREACH(interface, &ifnet_head, if_link) {
8773 			ifnet_lock_shared(interface);
8774 			if (interface->if_agentids != NULL) {
8775 				for (u_int32_t i = 0; i < interface->if_agentcount; i++) {
8776 					if (uuid_compare(interface->if_agentids[i], add_request->agent_uuid) == 0) {
8777 						interface_index = interface->if_index;
8778 						break;
8779 					}
8780 				}
8781 			}
8782 			ifnet_lock_done(interface);
8783 			if (interface_index != IFSCOPE_NONE) {
8784 				break;
8785 			}
8786 		}
8787 		ifnet_head_done();
8788 
8789 		necp_client_add_nexus_flow_if_needed(new_registration, add_request->agent_uuid, interface_index);
8790 
8791 		size_t dummy_length = 0;
8792 		void * __sized_by(dummy_length) dummy_results = NULL;
8793 		error = netagent_client_message_with_params(add_request->agent_uuid,
8794 		    ((new_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
8795 		    client->client_id :
8796 		    new_registration->registration_id),
8797 		    pid, client->agent_handle,
8798 		    NETAGENT_MESSAGE_TYPE_CLIENT_ASSERT,
8799 		    (struct necp_client_agent_parameters *)&parameters,
8800 		    &dummy_results, &dummy_length);
8801 		if (error != 0) {
8802 			NECPLOG(LOG_ERR, "netagent_client_message error (%d)", error);
8803 		}
8804 	}
8805 
8806 	if (error != 0) {
8807 		// Encountered an error in adding the flow, destroy the flow registration
8808 #if SKYWALK
8809 		necp_destroy_flow_stats(fd_data, new_registration, NULL, false);
8810 #endif /* SKYWALK */
8811 		NECP_FLOW_TREE_LOCK_EXCLUSIVE();
8812 		RB_REMOVE(_necp_client_flow_global_tree, &necp_client_flow_global_tree, new_registration);
8813 		NECP_FLOW_TREE_UNLOCK();
8814 		RB_REMOVE(_necp_fd_flow_tree, &fd_data->flows, new_registration);
8815 		necp_destroy_client_flow_registration(client, new_registration, fd_data->proc_pid, true);
8816 		new_registration = NULL;
8817 	}
8818 
8819 	NECP_CLIENT_UNLOCK(client);
8820 	NECP_FD_UNLOCK(fd_data);
8821 
8822 	necp_client_release(client);
8823 
8824 	if (error != 0) {
8825 		goto done;
8826 	}
8827 
8828 	// Copy the request back out to the caller with assigned fields
8829 	error = copyout(add_request, uap->buffer, buffer_size);
8830 	if (error != 0) {
8831 		NECPLOG(LOG_ERR, "necp_client_add_flow copyout add_request error (%d)", error);
8832 	}
8833 
8834 done:
8835 	*retval = error;
8836 	if (error != 0) {
8837 		NECPLOG(LOG_ERR, "Add flow error (%d)", error);
8838 	}
8839 
8840 	if (allocated_add_request != NULL) {
8841 		kfree_data(allocated_add_request, buffer_size);
8842 	}
8843 
8844 	if (proc != PROC_NULL) {
8845 		proc_rele(proc);
8846 	}
8847 	return error;
8848 }
8849 
8850 #if SKYWALK
8851 
8852 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_request_nexus(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8853 necp_client_request_nexus(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8854 {
8855 	int error = 0;
8856 	struct necp_client *client = NULL;
8857 	uuid_t client_id;
8858 	struct necp_client_nexus_parameters parameters = {};
8859 	struct proc *proc = PROC_NULL;
8860 	const size_t buffer_size = uap->buffer_size;
8861 
8862 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
8863 		error = EINVAL;
8864 		goto done;
8865 	}
8866 
8867 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
8868 	if (error) {
8869 		NECPLOG(LOG_ERR, "necp_client_request_nexus copyin client_id error (%d)", error);
8870 		goto done;
8871 	}
8872 
8873 	NECP_FD_LOCK(fd_data);
8874 	pid_t pid = fd_data->proc_pid;
8875 	proc = proc_find(pid);
8876 	if (proc == PROC_NULL) {
8877 		NECP_FD_UNLOCK(fd_data);
8878 		NECPLOG(LOG_ERR, "necp_client_request_nexus process not found for pid %d error (%d)", pid, error);
8879 		error = ESRCH;
8880 		goto done;
8881 	}
8882 
8883 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
8884 	if (client == NULL) {
8885 		NECP_FD_UNLOCK(fd_data);
8886 		error = ENOENT;
8887 		goto done;
8888 	}
8889 
8890 	// Using REQUEST_NEXUS indicates that the client only supports one flow per client
8891 	client->legacy_client_is_flow = true;
8892 
8893 	necp_client_retain_locked(client);
8894 	necp_client_copy_parameters_locked(client, &parameters);
8895 
8896 	do {
8897 		size_t assigned_results_length = 0;
8898 		void * __sized_by(assigned_results_length) assigned_results = NULL;
8899 		uuid_t nexus_uuid;
8900 		uint32_t interface_index = 0;
8901 
8902 		// Validate that the nexus UUID is assigned
8903 		bool found_nexus = false;
8904 		for (u_int32_t option_i = 0; option_i < client->interface_option_count; option_i++) {
8905 			if (option_i < NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT) {
8906 				struct necp_client_interface_option *option = &client->interface_options[option_i];
8907 				if (!uuid_is_null(option->nexus_agent)) {
8908 					uuid_copy(nexus_uuid, option->nexus_agent);
8909 					interface_index = option->interface_index;
8910 					found_nexus = true;
8911 					break;
8912 				}
8913 			} else {
8914 				struct necp_client_interface_option *option = &client->extra_interface_options[option_i - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT];
8915 				if (!uuid_is_null(option->nexus_agent)) {
8916 					uuid_copy(nexus_uuid, option->nexus_agent);
8917 					interface_index = option->interface_index;
8918 					found_nexus = true;
8919 					break;
8920 				}
8921 			}
8922 		}
8923 
8924 		if (!found_nexus) {
8925 			NECP_CLIENT_UNLOCK(client);
8926 			NECP_FD_UNLOCK(fd_data);
8927 			necp_client_release(client);
8928 			// Break the loop
8929 			error = ENETDOWN;
8930 			goto done;
8931 		}
8932 
8933 		struct necp_client_flow_registration *new_registration = necp_client_create_flow_registration(fd_data, client);
8934 		if (new_registration == NULL) {
8935 			error = ENOMEM;
8936 			NECP_CLIENT_UNLOCK(client);
8937 			NECP_FD_UNLOCK(fd_data);
8938 			necp_client_release(client);
8939 			NECPLOG0(LOG_ERR, "Failed to allocate flow registration");
8940 			goto done;
8941 		}
8942 
8943 		new_registration->flags = (NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS | NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID);
8944 
8945 		necp_client_add_nexus_flow_if_needed(new_registration, nexus_uuid, interface_index);
8946 
8947 		// Note: Any clients using "request_nexus" are not flow-registration aware.
8948 		// Register the Client ID rather than the Registration ID with the nexus, since
8949 		// the client will send traffic based on the client ID.
8950 		error = netagent_client_message_with_params(nexus_uuid,
8951 		    ((new_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
8952 		    client->client_id :
8953 		    new_registration->registration_id),
8954 		    pid, client->agent_handle,
8955 		    NETAGENT_MESSAGE_TYPE_REQUEST_NEXUS,
8956 		    (struct necp_client_agent_parameters *)&parameters,
8957 		    &assigned_results, &assigned_results_length);
8958 		if (error) {
8959 			NECP_CLIENT_UNLOCK(client);
8960 			NECP_FD_UNLOCK(fd_data);
8961 			necp_client_release(client);
8962 			VERIFY(assigned_results == NULL);
8963 			VERIFY(assigned_results_length == 0);
8964 			NECPLOG(LOG_ERR, "netagent_client_message error (%d)", error);
8965 			goto done;
8966 		}
8967 
8968 		if (assigned_results != NULL) {
8969 			if (!necp_assign_client_result_locked(proc, fd_data, client, new_registration, nexus_uuid,
8970 			    assigned_results, assigned_results_length, false, false)) {
8971 				kfree_data_sized_by(assigned_results, assigned_results_length);
8972 			}
8973 		}
8974 
8975 		if (uap->buffer != 0 && buffer_size == sizeof(struct necp_stats_bufreq) &&
8976 		    necp_arena_initialize(fd_data, true) == 0) {
8977 			struct necp_stats_bufreq bufreq = {};
8978 			int copy_error = copyin(uap->buffer, &bufreq, buffer_size);
8979 			if (copy_error) {
8980 				NECPLOG(LOG_ERR, "necp_client_request_nexus copyin bufreq error (%d)", copy_error);
8981 			} else {
8982 				(void)necp_stats_initialize(fd_data, client, new_registration, &bufreq);
8983 				copy_error = copyout(&bufreq, uap->buffer, buffer_size);
8984 				if (copy_error != 0) {
8985 					NECPLOG(LOG_ERR, "necp_client_request_nexus copyout bufreq error (%d)", copy_error);
8986 				}
8987 			}
8988 		}
8989 	} while (false);
8990 
8991 	NECP_CLIENT_UNLOCK(client);
8992 	NECP_FD_UNLOCK(fd_data);
8993 
8994 	necp_client_release(client);
8995 
8996 done:
8997 	*retval = error;
8998 	if (error != 0) {
8999 		NECPLOG(LOG_ERR, "Request nexus error (%d)", error);
9000 	}
9001 
9002 	if (proc != PROC_NULL) {
9003 		proc_rele(proc);
9004 	}
9005 	return error;
9006 }
9007 #endif /* !SKYWALK */
9008 
9009 static void
necp_client_add_assertion(struct necp_client * client,uuid_t netagent_uuid)9010 necp_client_add_assertion(struct necp_client *client, uuid_t netagent_uuid)
9011 {
9012 	struct necp_client_assertion *new_assertion = NULL;
9013 
9014 	new_assertion = kalloc_type(struct necp_client_assertion,
9015 	    Z_WAITOK | Z_NOFAIL);
9016 
9017 	uuid_copy(new_assertion->asserted_netagent, netagent_uuid);
9018 
9019 	LIST_INSERT_HEAD(&client->assertion_list, new_assertion, assertion_chain);
9020 }
9021 
9022 static bool
necp_client_remove_assertion(struct necp_client * client,uuid_t netagent_uuid)9023 necp_client_remove_assertion(struct necp_client *client, uuid_t netagent_uuid)
9024 {
9025 	struct necp_client_assertion * __single found_assertion = NULL;
9026 	struct necp_client_assertion *search_assertion = NULL;
9027 	LIST_FOREACH(search_assertion, &client->assertion_list, assertion_chain) {
9028 		if (uuid_compare(search_assertion->asserted_netagent, netagent_uuid) == 0) {
9029 			found_assertion = search_assertion;
9030 			break;
9031 		}
9032 	}
9033 
9034 	if (found_assertion == NULL) {
9035 		NECPLOG0(LOG_ERR, "Netagent uuid not previously asserted");
9036 		return false;
9037 	}
9038 
9039 	LIST_REMOVE(found_assertion, assertion_chain);
9040 	kfree_type(struct necp_client_assertion, found_assertion);
9041 	return true;
9042 }
9043 
9044 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_agent_action(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)9045 necp_client_agent_action(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9046 {
9047 	int error = 0;
9048 	struct necp_client *client = NULL;
9049 	uuid_t client_id;
9050 	bool acted_on_agent = FALSE;
9051 	u_int8_t *parameters = NULL;
9052 	const size_t buffer_size = uap->buffer_size;
9053 
9054 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
9055 	    buffer_size == 0 || uap->buffer == 0) {
9056 		NECPLOG0(LOG_ERR, "necp_client_agent_action invalid parameters");
9057 		error = EINVAL;
9058 		goto done;
9059 	}
9060 
9061 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
9062 	if (error) {
9063 		NECPLOG(LOG_ERR, "necp_client_agent_action copyin client_id error (%d)", error);
9064 		goto done;
9065 	}
9066 
9067 	if (buffer_size > NECP_MAX_AGENT_ACTION_SIZE) {
9068 		NECPLOG(LOG_ERR, "necp_client_agent_action invalid buffer size (>%u)", NECP_MAX_AGENT_ACTION_SIZE);
9069 		error = EINVAL;
9070 		goto done;
9071 	}
9072 
9073 	parameters = (u_int8_t *)kalloc_data(buffer_size, Z_WAITOK | Z_ZERO);
9074 	if (parameters == NULL) {
9075 		error = ENOMEM;
9076 		goto done;
9077 	}
9078 
9079 	error = copyin(uap->buffer, parameters, buffer_size);
9080 	if (error) {
9081 		NECPLOG(LOG_ERR, "necp_client_agent_action parameters copyin error (%d)", error);
9082 		goto done;
9083 	}
9084 
9085 	NECP_FD_LOCK(fd_data);
9086 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
9087 	if (client != NULL) {
9088 		size_t offset = 0;
9089 		while ((offset + sizeof(struct necp_tlv_header)) <= buffer_size) {
9090 			u_int8_t type = necp_buffer_get_tlv_type(parameters, buffer_size, offset);
9091 			u_int32_t length = necp_buffer_get_tlv_length(parameters, buffer_size, offset);
9092 
9093 			if (length > (buffer_size - (offset + sizeof(struct necp_tlv_header)))) {
9094 				// If the length is larger than what can fit in the remaining parameters size, bail
9095 				NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
9096 				break;
9097 			}
9098 
9099 			if (length >= sizeof(uuid_t)) {
9100 				u_int8_t * __indexable value = necp_buffer_get_tlv_value(parameters, buffer_size, offset, NULL);
9101 				if (value == NULL) {
9102 					NECPLOG0(LOG_ERR, "Invalid TLV value");
9103 					break;
9104 				}
9105 				if (type == NECP_CLIENT_PARAMETER_TRIGGER_AGENT ||
9106 				    type == NECP_CLIENT_PARAMETER_ASSERT_AGENT ||
9107 				    type == NECP_CLIENT_PARAMETER_UNASSERT_AGENT) {
9108 					uuid_t agent_uuid;
9109 					uuid_copy(agent_uuid, value);
9110 					u_int8_t netagent_message_type = 0;
9111 					if (type == NECP_CLIENT_PARAMETER_TRIGGER_AGENT) {
9112 						netagent_message_type = NETAGENT_MESSAGE_TYPE_CLIENT_TRIGGER;
9113 					} else if (type == NECP_CLIENT_PARAMETER_ASSERT_AGENT) {
9114 						netagent_message_type = NETAGENT_MESSAGE_TYPE_CLIENT_ASSERT;
9115 					} else if (type == NECP_CLIENT_PARAMETER_UNASSERT_AGENT) {
9116 						netagent_message_type = NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT;
9117 					}
9118 
9119 					// Before unasserting, verify that the assertion was already taken
9120 					if (type == NECP_CLIENT_PARAMETER_UNASSERT_AGENT) {
9121 						if (!necp_client_remove_assertion(client, agent_uuid)) {
9122 							error = ENOENT;
9123 							break;
9124 						}
9125 					}
9126 
9127 					struct necp_client_nexus_parameters parsed_parameters = {};
9128 					necp_client_copy_parameters_locked(client, &parsed_parameters);
9129 					size_t dummy_length = 0;
9130 					void * __sized_by(dummy_length) dummy_results = NULL;
9131 
9132 					error = netagent_client_message_with_params(agent_uuid,
9133 					    client_id,
9134 					    fd_data->proc_pid,
9135 					    client->agent_handle,
9136 					    netagent_message_type,
9137 					    (struct necp_client_agent_parameters *)&parsed_parameters,
9138 					    &dummy_results, &dummy_length);
9139 					if (error == 0) {
9140 						acted_on_agent = TRUE;
9141 					} else {
9142 						break;
9143 					}
9144 
9145 					// Only save the assertion if the action succeeded
9146 					if (type == NECP_CLIENT_PARAMETER_ASSERT_AGENT) {
9147 						necp_client_add_assertion(client, agent_uuid);
9148 					}
9149 				} else if (type == NECP_CLIENT_PARAMETER_AGENT_ADD_GROUP_MEMBERS ||
9150 				    type == NECP_CLIENT_PARAMETER_AGENT_REMOVE_GROUP_MEMBERS) {
9151 					uuid_t agent_uuid;
9152 					uuid_copy(agent_uuid, value);
9153 					u_int8_t netagent_message_type = 0;
9154 					if (type == NECP_CLIENT_PARAMETER_AGENT_ADD_GROUP_MEMBERS) {
9155 						netagent_message_type = NETAGENT_MESSAGE_TYPE_ADD_GROUP_MEMBERS;
9156 					} else if (type == NECP_CLIENT_PARAMETER_AGENT_REMOVE_GROUP_MEMBERS) {
9157 						netagent_message_type = NETAGENT_MESSAGE_TYPE_REMOVE_GROUP_MEMBERS;
9158 					}
9159 
9160 					struct necp_client_group_members group_members = {};
9161 					group_members.group_members_length = (length - sizeof(uuid_t));
9162 					group_members.group_members = (value + sizeof(uuid_t));
9163 					size_t dummy_length = 0;
9164 					void * __sized_by(dummy_length) dummy_results = NULL;
9165 					error = netagent_client_message_with_params(agent_uuid,
9166 					    client_id,
9167 					    fd_data->proc_pid,
9168 					    client->agent_handle,
9169 					    netagent_message_type,
9170 					    (struct necp_client_agent_parameters *)&group_members,
9171 					    &dummy_results, &dummy_length);
9172 					if (error == 0) {
9173 						acted_on_agent = TRUE;
9174 					} else {
9175 						break;
9176 					}
9177 				} else if (type == NECP_CLIENT_PARAMETER_REPORT_AGENT_ERROR) {
9178 					uuid_t agent_uuid;
9179 					uuid_copy(agent_uuid, value);
9180 					struct necp_client_agent_parameters agent_params = {};
9181 					if ((length - sizeof(uuid_t)) >= sizeof(agent_params.u.error.error)) {
9182 						memcpy(&agent_params.u.error.error,
9183 						    (value + sizeof(uuid_t)),
9184 						    sizeof(agent_params.u.error.error));
9185 					}
9186 					bool agent_reported = false;
9187 					for (int agent_i = 0; agent_i < NECP_FD_REPORTED_AGENT_COUNT; agent_i++) {
9188 						if (uuid_compare(agent_uuid, fd_data->reported_agents.agent_uuid[agent_i]) == 0) {
9189 							// Found a match, already reported
9190 							agent_reported = true;
9191 							break;
9192 						}
9193 					}
9194 					agent_params.u.error.force_report = !agent_reported;
9195 					if (!agent_reported) {
9196 						// Save this agent as having been reported
9197 						bool saved_agent_uuid = false;
9198 						for (int agent_i = 0; agent_i < NECP_FD_REPORTED_AGENT_COUNT; agent_i++) {
9199 							if (uuid_is_null(fd_data->reported_agents.agent_uuid[agent_i])) {
9200 								uuid_copy(fd_data->reported_agents.agent_uuid[agent_i], agent_uuid);
9201 								saved_agent_uuid = true;
9202 								break;
9203 							}
9204 						}
9205 						if (!saved_agent_uuid) {
9206 							// Reported agent UUIDs full, move over and insert at the end
9207 							for (int agent_i = 0; agent_i < NECP_FD_REPORTED_AGENT_COUNT; agent_i++) {
9208 								if (agent_i + 1 < NECP_FD_REPORTED_AGENT_COUNT) {
9209 									uuid_copy(fd_data->reported_agents.agent_uuid[agent_i], fd_data->reported_agents.agent_uuid[agent_i + 1]);
9210 								} else {
9211 									uuid_copy(fd_data->reported_agents.agent_uuid[agent_i], agent_uuid);
9212 								}
9213 							}
9214 						}
9215 					}
9216 					size_t dummy_length = 0;
9217 					void * __sized_by(dummy_length) dummy_results = NULL;
9218 					error = netagent_client_message_with_params(agent_uuid,
9219 					    client_id,
9220 					    fd_data->proc_pid,
9221 					    client->agent_handle,
9222 					    NETAGENT_MESSAGE_TYPE_CLIENT_ERROR,
9223 					    &agent_params,
9224 					    &dummy_results, &dummy_length);
9225 					if (error == 0) {
9226 						acted_on_agent = TRUE;
9227 					} else {
9228 						break;
9229 					}
9230 				}
9231 			}
9232 
9233 			offset += sizeof(struct necp_tlv_header) + length;
9234 		}
9235 
9236 		NECP_CLIENT_UNLOCK(client);
9237 	}
9238 	NECP_FD_UNLOCK(fd_data);
9239 
9240 	if (!acted_on_agent &&
9241 	    error == 0) {
9242 		error = ENOENT;
9243 	}
9244 done:
9245 	*retval = error;
9246 	if (parameters != NULL) {
9247 		kfree_data(parameters, buffer_size);
9248 		parameters = NULL;
9249 	}
9250 
9251 	return error;
9252 }
9253 
9254 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)9255 necp_client_copy_agent(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9256 {
9257 	int error = 0;
9258 	uuid_t agent_uuid;
9259 	const size_t buffer_size = uap->buffer_size;
9260 
9261 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
9262 	    buffer_size == 0 || uap->buffer == 0) {
9263 		NECPLOG0(LOG_ERR, "necp_client_copy_agent bad input");
9264 		error = EINVAL;
9265 		goto done;
9266 	}
9267 
9268 	error = copyin(uap->client_id, agent_uuid, sizeof(uuid_t));
9269 	if (error) {
9270 		NECPLOG(LOG_ERR, "necp_client_copy_agent copyin agent_uuid error (%d)", error);
9271 		goto done;
9272 	}
9273 
9274 	error = netagent_copyout(agent_uuid, uap->buffer, buffer_size);
9275 	if (error) {
9276 		// netagent_copyout already logs appropriate errors
9277 		goto done;
9278 	}
9279 done:
9280 	*retval = error;
9281 
9282 	return error;
9283 }
9284 
9285 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_agent_use(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)9286 necp_client_agent_use(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9287 {
9288 	int error = 0;
9289 	struct necp_client *client = NULL;
9290 	uuid_t client_id;
9291 	struct necp_agent_use_parameters parameters = {};
9292 	const size_t buffer_size = uap->buffer_size;
9293 
9294 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
9295 	    buffer_size != sizeof(parameters) || uap->buffer == 0) {
9296 		error = EINVAL;
9297 		goto done;
9298 	}
9299 
9300 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
9301 	if (error) {
9302 		NECPLOG(LOG_ERR, "Copyin client_id error (%d)", error);
9303 		goto done;
9304 	}
9305 
9306 	error = copyin(uap->buffer, &parameters, buffer_size);
9307 	if (error) {
9308 		NECPLOG(LOG_ERR, "Parameters copyin error (%d)", error);
9309 		goto done;
9310 	}
9311 
9312 	NECP_FD_LOCK(fd_data);
9313 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
9314 	if (client != NULL) {
9315 		error = netagent_use(parameters.agent_uuid, &parameters.out_use_count);
9316 		NECP_CLIENT_UNLOCK(client);
9317 	} else {
9318 		error = ENOENT;
9319 	}
9320 
9321 	NECP_FD_UNLOCK(fd_data);
9322 
9323 	if (error == 0) {
9324 		error = copyout(&parameters, uap->buffer, buffer_size);
9325 		if (error) {
9326 			NECPLOG(LOG_ERR, "Parameters copyout error (%d)", error);
9327 			goto done;
9328 		}
9329 	}
9330 
9331 done:
9332 	*retval = error;
9333 
9334 	return error;
9335 }
9336 
9337 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)9338 necp_client_acquire_agent_token(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9339 {
9340 	int error = 0;
9341 	uuid_t agent_uuid = {};
9342 	const size_t buffer_size = uap->buffer_size;
9343 
9344 	*retval = 0;
9345 
9346 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
9347 	    buffer_size == 0 || uap->buffer == 0) {
9348 		NECPLOG0(LOG_ERR, "necp_client_copy_agent bad input");
9349 		error = EINVAL;
9350 		goto done;
9351 	}
9352 
9353 	error = copyin(uap->client_id, agent_uuid, sizeof(uuid_t));
9354 	if (error) {
9355 		NECPLOG(LOG_ERR, "necp_client_copy_agent copyin agent_uuid error (%d)", error);
9356 		goto done;
9357 	}
9358 
9359 	error = netagent_acquire_token(agent_uuid, uap->buffer, buffer_size, retval);
9360 done:
9361 	return error;
9362 }
9363 
9364 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)9365 necp_client_copy_interface(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9366 {
9367 	int error = 0;
9368 	u_int32_t interface_index = 0;
9369 	struct necp_interface_details interface_details = {};
9370 
9371 	if (uap->client_id == 0 || uap->client_id_len != sizeof(u_int32_t) ||
9372 	    uap->buffer_size < sizeof(interface_details) ||
9373 	    uap->buffer == 0) {
9374 		NECPLOG0(LOG_ERR, "necp_client_copy_interface bad input");
9375 		error = EINVAL;
9376 		goto done;
9377 	}
9378 
9379 	error = copyin(uap->client_id, &interface_index, sizeof(u_int32_t));
9380 	if (error) {
9381 		NECPLOG(LOG_ERR, "necp_client_copy_interface copyin interface_index error (%d)", error);
9382 		goto done;
9383 	}
9384 
9385 	if (interface_index == 0) {
9386 		error = ENOENT;
9387 		NECPLOG(LOG_ERR, "necp_client_copy_interface bad interface_index (%d)", interface_index);
9388 		goto done;
9389 	}
9390 
9391 	lck_mtx_lock(rnh_lock);
9392 	ifnet_head_lock_shared();
9393 	ifnet_t interface = NULL;
9394 	if (interface_index != IFSCOPE_NONE && interface_index <= (u_int32_t)if_index) {
9395 		interface = ifindex2ifnet[interface_index];
9396 	}
9397 
9398 	if (interface != NULL) {
9399 		if (interface->if_xname != NULL) {
9400 			strlcpy((char *)&interface_details.name, interface->if_xname, sizeof(interface_details.name));
9401 		}
9402 		interface_details.index = interface->if_index;
9403 		interface_details.generation = ifnet_get_generation(interface);
9404 		if (interface->if_delegated.ifp != NULL) {
9405 			interface_details.delegate_index = interface->if_delegated.ifp->if_index;
9406 		}
9407 		interface_details.functional_type = if_functional_type(interface, TRUE);
9408 		if (IFNET_IS_EXPENSIVE(interface)) {
9409 			interface_details.flags |= NECP_INTERFACE_FLAG_EXPENSIVE;
9410 		}
9411 		if (IFNET_IS_CONSTRAINED(interface)) {
9412 			interface_details.flags |= NECP_INTERFACE_FLAG_CONSTRAINED;
9413 		}
9414 		if (IFNET_IS_ULTRA_CONSTRAINED(interface)) {
9415 			interface_details.flags |= NECP_INTERFACE_FLAG_ULTRA_CONSTRAINED;
9416 		}
9417 		if ((interface->if_eflags & IFEF_TXSTART) == IFEF_TXSTART) {
9418 			interface_details.flags |= NECP_INTERFACE_FLAG_TXSTART;
9419 		}
9420 		if ((interface->if_eflags & IFEF_NOACKPRI) == IFEF_NOACKPRI) {
9421 			interface_details.flags |= NECP_INTERFACE_FLAG_NOACKPRI;
9422 		}
9423 		if ((interface->if_eflags & IFEF_3CA) == IFEF_3CA) {
9424 			interface_details.flags |= NECP_INTERFACE_FLAG_3CARRIERAGG;
9425 		}
9426 		if (IFNET_IS_LOW_POWER(interface)) {
9427 			interface_details.flags |= NECP_INTERFACE_FLAG_IS_LOW_POWER;
9428 		}
9429 		if (interface->if_xflags & IFXF_MPK_LOG) {
9430 			interface_details.flags |= NECP_INTERFACE_FLAG_MPK_LOG;
9431 		}
9432 		if (interface->if_flags & IFF_MULTICAST) {
9433 			interface_details.flags |= NECP_INTERFACE_FLAG_SUPPORTS_MULTICAST;
9434 		}
9435 		if (IS_INTF_CLAT46(interface)) {
9436 			interface_details.flags |= NECP_INTERFACE_FLAG_HAS_NAT64;
9437 		}
9438 		interface_details.mtu = interface->if_mtu;
9439 #if SKYWALK
9440 		fsw_get_tso_capabilities(interface, &interface_details.tso_max_segment_size_v4,
9441 		    &interface_details.tso_max_segment_size_v6);
9442 
9443 		interface_details.hwcsum_flags = interface->if_hwassist & IFNET_CHECKSUMF;
9444 #endif /* SKYWALK */
9445 
9446 		u_int8_t ipv4_signature_len = sizeof(interface_details.ipv4_signature.signature);
9447 		u_int16_t ipv4_signature_flags;
9448 		if (ifnet_get_netsignature(interface, AF_INET, &ipv4_signature_len, &ipv4_signature_flags,
9449 		    (u_int8_t *)&interface_details.ipv4_signature) != 0) {
9450 			ipv4_signature_len = 0;
9451 		}
9452 		interface_details.ipv4_signature.signature_len = ipv4_signature_len;
9453 
9454 		// Check for default scoped routes for IPv4 and IPv6
9455 		union necp_sockaddr_union default_address;
9456 		struct rtentry *v4Route = NULL;
9457 		memset(&default_address, 0, sizeof(default_address));
9458 		default_address.sa.sa_family = AF_INET;
9459 		default_address.sa.sa_len = sizeof(struct sockaddr_in);
9460 		v4Route = rtalloc1_scoped_locked(SA(&default_address), 0, 0,
9461 		    interface->if_index);
9462 		if (v4Route != NULL) {
9463 			if (v4Route->rt_ifp != NULL && !IS_INTF_CLAT46(v4Route->rt_ifp)) {
9464 				interface_details.flags |= NECP_INTERFACE_FLAG_IPV4_ROUTABLE;
9465 			}
9466 			rtfree_locked(v4Route);
9467 			v4Route = NULL;
9468 		}
9469 
9470 		struct rtentry *v6Route = NULL;
9471 		memset(&default_address, 0, sizeof(default_address));
9472 		default_address.sa.sa_family = AF_INET6;
9473 		default_address.sa.sa_len = sizeof(struct sockaddr_in6);
9474 		v6Route = rtalloc1_scoped_locked(SA(&default_address), 0, 0,
9475 		    interface->if_index);
9476 		if (v6Route != NULL) {
9477 			if (v6Route->rt_ifp != NULL) {
9478 				interface_details.flags |= NECP_INTERFACE_FLAG_IPV6_ROUTABLE;
9479 			}
9480 			rtfree_locked(v6Route);
9481 			v6Route = NULL;
9482 		}
9483 
9484 		u_int8_t ipv6_signature_len = sizeof(interface_details.ipv6_signature.signature);
9485 		u_int16_t ipv6_signature_flags;
9486 		if (ifnet_get_netsignature(interface, AF_INET6, &ipv6_signature_len, &ipv6_signature_flags,
9487 		    (u_int8_t *)&interface_details.ipv6_signature) != 0) {
9488 			ipv6_signature_len = 0;
9489 		}
9490 		interface_details.ipv6_signature.signature_len = ipv6_signature_len;
9491 
9492 		ifnet_lock_shared(interface);
9493 		struct ifaddr * __single ifa = NULL;
9494 		TAILQ_FOREACH(ifa, &interface->if_addrhead, ifa_link) {
9495 			IFA_LOCK(ifa);
9496 			if (ifa->ifa_addr->sa_family == AF_INET) {
9497 				interface_details.flags |= NECP_INTERFACE_FLAG_HAS_NETMASK;
9498 				interface_details.ipv4_netmask = (ifatoia(ifa))->ia_sockmask.sin_addr.s_addr;
9499 				if (interface->if_flags & IFF_BROADCAST) {
9500 					interface_details.flags |= NECP_INTERFACE_FLAG_HAS_BROADCAST;
9501 					interface_details.ipv4_broadcast = (ifatoia(ifa))->ia_broadaddr.sin_addr.s_addr;
9502 				}
9503 			}
9504 			IFA_UNLOCK(ifa);
9505 		}
9506 
9507 		interface_details.radio_type = interface->if_radio_type;
9508 		if (interface_details.radio_type == 0 && interface->if_delegated.ifp) {
9509 			interface_details.radio_type = interface->if_delegated.ifp->if_radio_type;
9510 		}
9511 		ifnet_lock_done(interface);
9512 	}
9513 
9514 	ifnet_head_done();
9515 	lck_mtx_unlock(rnh_lock);
9516 
9517 	// If the client is using an older version of the struct, copy that length
9518 	error = copyout(&interface_details, uap->buffer, sizeof(interface_details));
9519 	if (error) {
9520 		NECPLOG(LOG_ERR, "necp_client_copy_interface copyout error (%d)", error);
9521 		goto done;
9522 	}
9523 done:
9524 	*retval = error;
9525 
9526 	return error;
9527 }
9528 
9529 #if SKYWALK
9530 
9531 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)9532 necp_client_get_interface_address(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9533 {
9534 	int error = 0;
9535 	u_int32_t interface_index = IFSCOPE_NONE;
9536 	struct sockaddr_storage address = {};
9537 	const size_t buffer_size = uap->buffer_size;
9538 
9539 	if (uap->client_id == 0 || uap->client_id_len != sizeof(u_int32_t) ||
9540 	    buffer_size < sizeof(struct sockaddr_in) ||
9541 	    buffer_size > sizeof(struct sockaddr_storage) ||
9542 	    uap->buffer == 0) {
9543 		NECPLOG0(LOG_ERR, "necp_client_get_interface_address bad input");
9544 		error = EINVAL;
9545 		goto done;
9546 	}
9547 
9548 	error = copyin(uap->client_id, &interface_index, sizeof(u_int32_t));
9549 	if (error) {
9550 		NECPLOG(LOG_ERR, "necp_client_get_interface_address copyin interface_index error (%d)", error);
9551 		goto done;
9552 	}
9553 
9554 	if (interface_index == IFSCOPE_NONE) {
9555 		error = ENOENT;
9556 		NECPLOG(LOG_ERR, "necp_client_get_interface_address bad interface_index (%d)", interface_index);
9557 		goto done;
9558 	}
9559 
9560 	error = copyin(uap->buffer, &address, buffer_size);
9561 	if (error) {
9562 		NECPLOG(LOG_ERR, "necp_client_get_interface_address copyin address error (%d)", error);
9563 		goto done;
9564 	}
9565 
9566 	if (address.ss_family != AF_INET && address.ss_family != AF_INET6) {
9567 		error = EINVAL;
9568 		NECPLOG(LOG_ERR, "necp_client_get_interface_address invalid address family (%u)", address.ss_family);
9569 		goto done;
9570 	}
9571 
9572 	if (address.ss_len != buffer_size) {
9573 		error = EINVAL;
9574 		NECPLOG(LOG_ERR, "necp_client_get_interface_address invalid address length (%u)", address.ss_len);
9575 		goto done;
9576 	}
9577 
9578 	ifnet_head_lock_shared();
9579 	ifnet_t ifp = NULL;
9580 	if (interface_index != IFSCOPE_NONE && interface_index <= (u_int32_t)if_index) {
9581 		ifp = ifindex2ifnet[interface_index];
9582 	}
9583 	ifnet_head_done();
9584 	if (ifp == NULL) {
9585 		error = ENOENT;
9586 		NECPLOG0(LOG_ERR, "necp_client_get_interface_address no matching interface found");
9587 		goto done;
9588 	}
9589 
9590 	struct rtentry *rt = rtalloc1_scoped(SA(&address), 0, 0, interface_index);
9591 	if (rt == NULL) {
9592 		error = EINVAL;
9593 		NECPLOG0(LOG_ERR, "necp_client_get_interface_address route lookup failed");
9594 		goto done;
9595 	}
9596 
9597 	uint32_t gencount = 0;
9598 	struct sockaddr_storage local_address = {};
9599 	error = flow_route_select_laddr((union sockaddr_in_4_6 *)&local_address,
9600 	    (union sockaddr_in_4_6 *)&address, ifp, rt, &gencount, 1);
9601 	rtfree(rt);
9602 	rt = NULL;
9603 
9604 	if (error) {
9605 		NECPLOG(LOG_ERR, "necp_client_get_interface_address local address selection failed (%d)", error);
9606 		goto done;
9607 	}
9608 
9609 	if (local_address.ss_len > buffer_size) {
9610 		error = EMSGSIZE;
9611 		NECPLOG(LOG_ERR, "necp_client_get_interface_address local address too long for buffer (%u)",
9612 		    local_address.ss_len);
9613 		goto done;
9614 	}
9615 
9616 	error = copyout(&local_address, uap->buffer, local_address.ss_len);
9617 	if (error) {
9618 		NECPLOG(LOG_ERR, "necp_client_get_interface_address copyout error (%d)", error);
9619 		goto done;
9620 	}
9621 done:
9622 	*retval = error;
9623 
9624 	return error;
9625 }
9626 
9627 extern const char *proc_name_address(void *p);
9628 
9629 int
necp_stats_ctor(struct skmem_obj_info * oi,struct skmem_obj_info * oim,void * arg,uint32_t skmflag)9630 necp_stats_ctor(struct skmem_obj_info *oi, struct skmem_obj_info *oim,
9631     void *arg, uint32_t skmflag)
9632 {
9633 #pragma unused(arg, skmflag)
9634 	struct necp_all_kstats * __single kstats = SKMEM_OBJ_ADDR(oi);
9635 
9636 	ASSERT(oim != NULL && SKMEM_OBJ_ADDR(oim) != NULL);
9637 	ASSERT(SKMEM_OBJ_SIZE(oi) == SKMEM_OBJ_SIZE(oim));
9638 
9639 	kstats->necp_stats_ustats = SKMEM_OBJ_ADDR(oim);
9640 
9641 	return 0;
9642 }
9643 
9644 int
necp_stats_dtor(void * addr,void * arg)9645 necp_stats_dtor(void *addr, void *arg)
9646 {
9647 #pragma unused(addr, arg)
9648 	struct necp_all_kstats * __single kstats = addr;
9649 
9650 	kstats->necp_stats_ustats = NULL;
9651 
9652 	return 0;
9653 }
9654 
9655 static void
necp_fd_insert_stats_arena(struct necp_fd_data * fd_data,struct necp_arena_info * nai)9656 necp_fd_insert_stats_arena(struct necp_fd_data *fd_data, struct necp_arena_info *nai)
9657 {
9658 	NECP_FD_ASSERT_LOCKED(fd_data);
9659 	VERIFY(!(nai->nai_flags & NAIF_ATTACHED));
9660 	VERIFY(nai->nai_chain.le_next == NULL && nai->nai_chain.le_prev == NULL);
9661 
9662 	LIST_INSERT_HEAD(&fd_data->stats_arena_list, nai, nai_chain);
9663 	nai->nai_flags |= NAIF_ATTACHED;
9664 	necp_arena_info_retain(nai);    // for the list
9665 }
9666 
9667 static void
necp_fd_remove_stats_arena(struct necp_fd_data * fd_data,struct necp_arena_info * nai)9668 necp_fd_remove_stats_arena(struct necp_fd_data *fd_data, struct necp_arena_info *nai)
9669 {
9670 #pragma unused(fd_data)
9671 	NECP_FD_ASSERT_LOCKED(fd_data);
9672 	VERIFY(nai->nai_flags & NAIF_ATTACHED);
9673 	VERIFY(nai->nai_use_count >= 1);
9674 
9675 	LIST_REMOVE(nai, nai_chain);
9676 	nai->nai_flags &= ~NAIF_ATTACHED;
9677 	nai->nai_chain.le_next = NULL;
9678 	nai->nai_chain.le_prev = NULL;
9679 	necp_arena_info_release(nai);   // for the list
9680 }
9681 
9682 static struct necp_arena_info *
necp_fd_mredirect_stats_arena(struct necp_fd_data * fd_data,struct proc * proc)9683 necp_fd_mredirect_stats_arena(struct necp_fd_data *fd_data, struct proc *proc)
9684 {
9685 	struct necp_arena_info *nai, *nai_ret = NULL;
9686 
9687 	NECP_FD_ASSERT_LOCKED(fd_data);
9688 
9689 	// Redirect currently-active stats arena and remove it from the active state;
9690 	// upon process resumption, new flow request would trigger the creation of
9691 	// another active arena.
9692 	if ((nai = fd_data->stats_arena_active) != NULL) {
9693 		boolean_t need_defunct = FALSE;
9694 
9695 		ASSERT(!(nai->nai_flags & (NAIF_REDIRECT | NAIF_DEFUNCT)));
9696 		VERIFY(nai->nai_use_count >= 2);
9697 		ASSERT(nai->nai_arena != NULL);
9698 		ASSERT(nai->nai_mmap.ami_mapref != NULL);
9699 
9700 		int err = skmem_arena_mredirect(nai->nai_arena, &nai->nai_mmap, proc, &need_defunct);
9701 		VERIFY(err == 0);
9702 		// must be TRUE since we don't mmap the arena more than once
9703 		VERIFY(need_defunct == TRUE);
9704 
9705 		nai->nai_flags |= NAIF_REDIRECT;
9706 		nai_ret = nai;  // return to caller
9707 
9708 		necp_arena_info_release(nai);   // for fd_data
9709 		fd_data->stats_arena_active = nai = NULL;
9710 	}
9711 
9712 #if (DEVELOPMENT || DEBUG)
9713 	// make sure this list now contains nothing but redirected/defunct arenas
9714 	LIST_FOREACH(nai, &fd_data->stats_arena_list, nai_chain) {
9715 		ASSERT(nai->nai_use_count >= 1);
9716 		ASSERT(nai->nai_flags & (NAIF_REDIRECT | NAIF_DEFUNCT));
9717 	}
9718 #endif /* (DEVELOPMENT || DEBUG) */
9719 
9720 	return nai_ret;
9721 }
9722 
9723 static void
necp_arena_info_retain(struct necp_arena_info * nai)9724 necp_arena_info_retain(struct necp_arena_info *nai)
9725 {
9726 	nai->nai_use_count++;
9727 	VERIFY(nai->nai_use_count != 0);
9728 }
9729 
9730 static void
necp_arena_info_release(struct necp_arena_info * nai)9731 necp_arena_info_release(struct necp_arena_info *nai)
9732 {
9733 	VERIFY(nai->nai_use_count > 0);
9734 	if (--nai->nai_use_count == 0) {
9735 		necp_arena_info_free(nai);
9736 	}
9737 }
9738 
9739 static struct necp_arena_info *
necp_arena_info_alloc(void)9740 necp_arena_info_alloc(void)
9741 {
9742 	return zalloc_flags(necp_arena_info_zone, Z_WAITOK | Z_ZERO);
9743 }
9744 
9745 static void
necp_arena_info_free(struct necp_arena_info * nai)9746 necp_arena_info_free(struct necp_arena_info *nai)
9747 {
9748 	VERIFY(nai->nai_chain.le_next == NULL && nai->nai_chain.le_prev == NULL);
9749 	VERIFY(nai->nai_use_count == 0);
9750 
9751 	// NOTE: destroying the arena requires that all outstanding objects
9752 	// that were allocated have been freed, else it will assert.
9753 	if (nai->nai_arena != NULL) {
9754 		skmem_arena_munmap(nai->nai_arena, &nai->nai_mmap);
9755 		skmem_arena_release(nai->nai_arena);
9756 		OSDecrementAtomic(&necp_arena_count);
9757 		nai->nai_arena = NULL;
9758 		nai->nai_roff = 0;
9759 	}
9760 
9761 	ASSERT(nai->nai_arena == NULL);
9762 	ASSERT(nai->nai_mmap.ami_mapref == NULL);
9763 	ASSERT(nai->nai_mmap.ami_arena == NULL);
9764 	ASSERT(nai->nai_mmap.ami_maptask == TASK_NULL);
9765 
9766 	zfree(necp_arena_info_zone, nai);
9767 }
9768 
9769 static int
necp_arena_create(struct necp_fd_data * fd_data,size_t obj_size,size_t obj_cnt,struct proc * p)9770 necp_arena_create(struct necp_fd_data *fd_data, size_t obj_size, size_t obj_cnt, struct proc *p)
9771 {
9772 	struct skmem_region_params srp_ustats = {};
9773 	struct skmem_region_params srp_kstats = {};
9774 	struct necp_arena_info *nai;
9775 	char name[32];
9776 	const char *__null_terminated name_ptr = NULL;
9777 	int error = 0;
9778 
9779 	NECP_FD_ASSERT_LOCKED(fd_data);
9780 	ASSERT(fd_data->stats_arena_active == NULL);
9781 	ASSERT(p != PROC_NULL);
9782 	ASSERT(proc_pid(p) == fd_data->proc_pid);
9783 
9784 	// inherit the default parameters for the stats region
9785 	srp_ustats = *skmem_get_default(SKMEM_REGION_USTATS);
9786 	srp_kstats = *skmem_get_default(SKMEM_REGION_KSTATS);
9787 
9788 	// enable multi-segment mode
9789 	srp_ustats.srp_cflags &= ~SKMEM_REGION_CR_MONOLITHIC;
9790 	srp_kstats.srp_cflags &= ~SKMEM_REGION_CR_MONOLITHIC;
9791 
9792 	// configure and adjust the region parameters
9793 	srp_ustats.srp_r_obj_cnt = srp_kstats.srp_r_obj_cnt = obj_cnt;
9794 	srp_ustats.srp_r_obj_size = srp_kstats.srp_r_obj_size = obj_size;
9795 	skmem_region_params_config(&srp_ustats);
9796 	skmem_region_params_config(&srp_kstats);
9797 
9798 	nai = necp_arena_info_alloc();
9799 
9800 	nai->nai_proc_pid = fd_data->proc_pid;
9801 	name_ptr = tsnprintf(name, sizeof(name), "stats-%u.%s.%d", fd_data->stats_arena_gencnt, proc_name_address(p), fd_data->proc_pid);
9802 	nai->nai_arena = skmem_arena_create_for_necp(name_ptr, &srp_ustats, &srp_kstats, &error);
9803 	ASSERT(nai->nai_arena != NULL || error != 0);
9804 	if (error != 0) {
9805 		NECPLOG(LOG_ERR, "failed to create stats arena for pid %d\n", fd_data->proc_pid);
9806 	} else {
9807 		OSIncrementAtomic(&necp_arena_count);
9808 
9809 		// Get region offsets from base of mmap span; the arena
9810 		// doesn't need to be mmap'd at this point, since we simply
9811 		// compute the relative offset.
9812 		nai->nai_roff = skmem_arena_get_region_offset(nai->nai_arena, SKMEM_REGION_USTATS);
9813 
9814 		// map to the task/process; upon success, the base address of the region
9815 		// will be returned in nai_mmap.ami_mapaddr; this can be communicated to
9816 		// the process.
9817 		error = skmem_arena_mmap(nai->nai_arena, p, &nai->nai_mmap);
9818 		if (error != 0) {
9819 			NECPLOG(LOG_ERR, "failed to map stats arena for pid %d\n", fd_data->proc_pid);
9820 		}
9821 	}
9822 
9823 	if (error == 0) {
9824 		fd_data->stats_arena_active = nai;
9825 		necp_arena_info_retain(nai);    // for fd_data
9826 		necp_fd_insert_stats_arena(fd_data, nai);
9827 		++fd_data->stats_arena_gencnt;
9828 	} else {
9829 		necp_arena_info_free(nai);
9830 	}
9831 
9832 	return error;
9833 }
9834 
9835 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)9836 necp_arena_stats_obj_alloc(struct necp_fd_data *fd_data,
9837     mach_vm_offset_t *off,
9838     struct necp_arena_info **stats_arena,
9839     void **kstats_kaddr,
9840     boolean_t cansleep)
9841 {
9842 	struct skmem_cache *kstats_cp = NULL;
9843 	struct skmem_obj_info kstats_oi = {};
9844 	uint32_t ustats_obj_sz = 0;
9845 	void *__sized_by(ustats_obj_sz) ustats_obj = NULL;
9846 	uint32_t kstats_obj_sz = 0;
9847 	void *__sized_by(kstats_obj_sz) kstats_obj = NULL;
9848 	void * __indexable kstats_obj_tmp = NULL;
9849 	struct necp_all_kstats * __single kstats = NULL;
9850 
9851 	ASSERT(off != NULL);
9852 	ASSERT(stats_arena != NULL && *stats_arena == NULL);
9853 	ASSERT(kstats_kaddr != NULL && *kstats_kaddr == NULL);
9854 
9855 	NECP_FD_ASSERT_LOCKED(fd_data);
9856 	ASSERT(fd_data->stats_arena_active != NULL);
9857 	ASSERT(fd_data->stats_arena_active->nai_arena != NULL);
9858 
9859 	kstats_cp = skmem_arena_necp(fd_data->stats_arena_active->nai_arena)->arc_kstats_cache;
9860 	if ((kstats_obj_tmp = skmem_cache_alloc(kstats_cp, (cansleep ? SKMEM_SLEEP : SKMEM_NOSLEEP))) == NULL) {
9861 		return ENOMEM;
9862 	}
9863 	skmem_cache_get_obj_info(kstats_cp, kstats_obj_tmp, &kstats_oi, NULL);
9864 	ASSERT(SKMEM_OBJ_SIZE(&kstats_oi) >= sizeof(struct necp_all_stats));
9865 	kstats_obj = kstats_obj_tmp;
9866 	kstats_obj_sz = SKMEM_OBJ_SIZE(&kstats_oi);
9867 
9868 	kstats = (struct necp_all_kstats*)kstats_obj;
9869 	ustats_obj = __unsafe_forge_bidi_indexable(uint8_t *, kstats->necp_stats_ustats, kstats_obj_sz);
9870 	ustats_obj_sz = kstats_obj_sz;
9871 
9872 	bzero(ustats_obj, ustats_obj_sz);
9873 	bzero(&kstats->necp_stats_comm, sizeof(struct necp_all_stats));
9874 	*stats_arena = fd_data->stats_arena_active;
9875 	*kstats_kaddr = kstats_obj;
9876 	// kstats and ustats are mirrored and have the same offset
9877 	*off = fd_data->stats_arena_active->nai_roff + SKMEM_OBJ_ROFF(&kstats_oi);
9878 
9879 	return 0;
9880 }
9881 
9882 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)9883 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)
9884 {
9885 #pragma unused(fd_data)
9886 	NECP_FD_ASSERT_LOCKED(fd_data);
9887 
9888 	ASSERT(stats_arena != NULL);
9889 	ASSERT(stats_arena->nai_arena != NULL);
9890 	ASSERT(kstats_kaddr != NULL && *kstats_kaddr != NULL);
9891 	ASSERT(ustats_uaddr != NULL);
9892 
9893 	skmem_cache_free(skmem_arena_necp(stats_arena->nai_arena)->arc_kstats_cache, *kstats_kaddr);
9894 	*kstats_kaddr = NULL;
9895 	*ustats_uaddr = 0;
9896 }
9897 
9898 // This routine returns the KVA of the sysctls object, as well as the
9899 // offset of that object relative to the mmap base address for the
9900 // task/process.
9901 static void *
necp_arena_sysctls_obj(struct necp_fd_data * fd_data,mach_vm_offset_t * off,size_t * size)9902 necp_arena_sysctls_obj(struct necp_fd_data *fd_data, mach_vm_offset_t *off, size_t *size)
9903 {
9904 	void * __single objaddr;
9905 
9906 	NECP_FD_ASSERT_LOCKED(fd_data);
9907 	ASSERT(fd_data->sysctl_arena != NULL);
9908 
9909 	// kernel virtual address of the sysctls object
9910 	objaddr = skmem_arena_system_sysctls_obj_addr(fd_data->sysctl_arena);
9911 	ASSERT(objaddr != NULL);
9912 
9913 	// Return the relative offset of the sysctls object; there is
9914 	// only 1 object in the entire sysctls region, and therefore the
9915 	// object's offset is simply the region's offset in the arena.
9916 	// (sysctl_mmap.ami_mapaddr + offset) is the address of this object
9917 	// in the task/process.
9918 	if (off != NULL) {
9919 		*off = fd_data->system_sysctls_roff;
9920 	}
9921 
9922 	if (size != NULL) {
9923 		*size = skmem_arena_system_sysctls_obj_size(fd_data->sysctl_arena);
9924 		ASSERT(*size != 0);
9925 	}
9926 
9927 	return objaddr;
9928 }
9929 
9930 static void
necp_stats_arenas_destroy(struct necp_fd_data * fd_data,boolean_t closing)9931 necp_stats_arenas_destroy(struct necp_fd_data *fd_data, boolean_t closing)
9932 {
9933 	struct necp_arena_info *nai, *nai_tmp;
9934 
9935 	NECP_FD_ASSERT_LOCKED(fd_data);
9936 
9937 	// If reaping (not closing), release reference only for idle active arena; the reference
9938 	// count must be 2 by now, when it's not being referred to by any clients/flows.
9939 	if ((nai = fd_data->stats_arena_active) != NULL && (closing || nai->nai_use_count == 2)) {
9940 		VERIFY(nai->nai_use_count >= 2);
9941 		necp_arena_info_release(nai);   // for fd_data
9942 		fd_data->stats_arena_active = NULL;
9943 	}
9944 
9945 	// clean up any defunct arenas left in the list
9946 	LIST_FOREACH_SAFE(nai, &fd_data->stats_arena_list, nai_chain, nai_tmp) {
9947 		// If reaping, release reference if the list holds the last one
9948 		if (closing || nai->nai_use_count == 1) {
9949 			VERIFY(nai->nai_use_count >= 1);
9950 			// callee unchains nai (and may free it)
9951 			necp_fd_remove_stats_arena(fd_data, nai);
9952 		}
9953 	}
9954 }
9955 
9956 static void
necp_sysctl_arena_destroy(struct necp_fd_data * fd_data)9957 necp_sysctl_arena_destroy(struct necp_fd_data *fd_data)
9958 {
9959 	NECP_FD_ASSERT_LOCKED(fd_data);
9960 
9961 	// NOTE: destroying the arena requires that all outstanding objects
9962 	// that were allocated have been freed, else it will assert.
9963 	if (fd_data->sysctl_arena != NULL) {
9964 		skmem_arena_munmap(fd_data->sysctl_arena, &fd_data->sysctl_mmap);
9965 		skmem_arena_release(fd_data->sysctl_arena);
9966 		OSDecrementAtomic(&necp_sysctl_arena_count);
9967 		fd_data->sysctl_arena = NULL;
9968 		fd_data->system_sysctls_roff = 0;
9969 	}
9970 }
9971 
9972 static int
necp_arena_initialize(struct necp_fd_data * fd_data,bool locked)9973 necp_arena_initialize(struct necp_fd_data *fd_data, bool locked)
9974 {
9975 	int error = 0;
9976 	size_t stats_obj_size = MAX(sizeof(struct necp_all_stats), sizeof(struct necp_all_kstats));
9977 
9978 	if (!locked) {
9979 		NECP_FD_LOCK(fd_data);
9980 	}
9981 	if (fd_data->stats_arena_active == NULL) {
9982 		error = necp_arena_create(fd_data, stats_obj_size,
9983 		    NECP_MAX_PER_PROCESS_CLIENT_STATISTICS_STRUCTS,
9984 		    current_proc());
9985 	}
9986 	if (!locked) {
9987 		NECP_FD_UNLOCK(fd_data);
9988 	}
9989 
9990 	return error;
9991 }
9992 
9993 static int
necp_sysctl_arena_initialize(struct necp_fd_data * fd_data,bool locked)9994 necp_sysctl_arena_initialize(struct necp_fd_data *fd_data, bool locked)
9995 {
9996 	int error = 0;
9997 
9998 	if (!locked) {
9999 		NECP_FD_LOCK(fd_data);
10000 	}
10001 
10002 	NECP_FD_ASSERT_LOCKED(fd_data);
10003 
10004 	if (fd_data->sysctl_arena == NULL) {
10005 		char name[32];
10006 		const char *__null_terminated name_ptr = NULL;
10007 		struct proc *p = current_proc();
10008 
10009 		ASSERT(p != PROC_NULL);
10010 		ASSERT(proc_pid(p) == fd_data->proc_pid);
10011 
10012 		name_ptr = tsnprintf(name, sizeof(name), "sysctl.%s.%d", proc_name_address(p), fd_data->proc_pid);
10013 		fd_data->sysctl_arena = skmem_arena_create_for_system(name_ptr, &error);
10014 		ASSERT(fd_data->sysctl_arena != NULL || error != 0);
10015 		if (error != 0) {
10016 			NECPLOG(LOG_ERR, "failed to create arena for pid %d\n", fd_data->proc_pid);
10017 		} else {
10018 			OSIncrementAtomic(&necp_sysctl_arena_count);
10019 
10020 			// Get region offsets from base of mmap span; the arena
10021 			// doesn't need to be mmap'd at this point, since we simply
10022 			// compute the relative offset.
10023 			fd_data->system_sysctls_roff = skmem_arena_get_region_offset(fd_data->sysctl_arena, SKMEM_REGION_SYSCTLS);
10024 
10025 			// map to the task/process; upon success, the base address of the region
10026 			// will be returned in nai_mmap.ami_mapaddr; this can be communicated to
10027 			// the process.
10028 			error = skmem_arena_mmap(fd_data->sysctl_arena, p, &fd_data->sysctl_mmap);
10029 			if (error != 0) {
10030 				NECPLOG(LOG_ERR, "failed to map sysctl arena for pid %d\n", fd_data->proc_pid);
10031 				necp_sysctl_arena_destroy(fd_data);
10032 			}
10033 		}
10034 	}
10035 
10036 	if (!locked) {
10037 		NECP_FD_UNLOCK(fd_data);
10038 	}
10039 
10040 	return error;
10041 }
10042 
10043 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)10044 necp_client_stats_bufreq(struct necp_fd_data *fd_data,
10045     struct necp_client *client,
10046     struct necp_client_flow_registration *flow_registration,
10047     struct necp_stats_bufreq *bufreq,
10048     struct necp_stats_hdr *out_header)
10049 {
10050 	int error = 0;
10051 	NECP_CLIENT_ASSERT_LOCKED(client);
10052 	NECP_FD_ASSERT_LOCKED(fd_data);
10053 
10054 	if ((bufreq->necp_stats_bufreq_id == NECP_CLIENT_STATISTICS_BUFREQ_ID) &&
10055 	    ((bufreq->necp_stats_bufreq_type == NECP_CLIENT_STATISTICS_TYPE_TCP &&
10056 	    bufreq->necp_stats_bufreq_ver == NECP_CLIENT_STATISTICS_TYPE_TCP_CURRENT_VER) ||
10057 	    (bufreq->necp_stats_bufreq_type == NECP_CLIENT_STATISTICS_TYPE_UDP &&
10058 	    bufreq->necp_stats_bufreq_ver == NECP_CLIENT_STATISTICS_TYPE_UDP_CURRENT_VER) ||
10059 	    (bufreq->necp_stats_bufreq_type == NECP_CLIENT_STATISTICS_TYPE_QUIC &&
10060 	    bufreq->necp_stats_bufreq_ver == NECP_CLIENT_STATISTICS_TYPE_QUIC_CURRENT_VER)) &&
10061 	    (bufreq->necp_stats_bufreq_size == sizeof(struct necp_all_stats))) {
10062 		// There should be one and only one stats allocation per client.
10063 		// If asked more than once, we just repeat ourselves.
10064 		if (flow_registration->ustats_uaddr == 0) {
10065 			mach_vm_offset_t off;
10066 			ASSERT(flow_registration->stats_arena == NULL);
10067 			ASSERT(flow_registration->kstats_kaddr == NULL);
10068 			ASSERT(flow_registration->ustats_uaddr == 0);
10069 			error = necp_arena_stats_obj_alloc(fd_data, &off, &flow_registration->stats_arena, &flow_registration->kstats_kaddr, FALSE);
10070 			if (error == 0) {
10071 				// upon success, hold a reference for the client; this is released when the client is removed/closed
10072 				ASSERT(flow_registration->stats_arena != NULL);
10073 				necp_arena_info_retain(flow_registration->stats_arena);
10074 
10075 				// compute user address based on mapping info and object offset
10076 				flow_registration->ustats_uaddr = flow_registration->stats_arena->nai_mmap.ami_mapaddr + off;
10077 
10078 				// add to collect_stats list
10079 				NECP_STATS_LIST_LOCK_EXCLUSIVE();
10080 				necp_client_retain_locked(client); // Add a reference to the client
10081 				LIST_INSERT_HEAD(&necp_collect_stats_flow_list, flow_registration, collect_stats_chain);
10082 				NECP_STATS_LIST_UNLOCK();
10083 				necp_schedule_collect_stats_clients(FALSE);
10084 			} else {
10085 				ASSERT(flow_registration->stats_arena == NULL);
10086 				ASSERT(flow_registration->kstats_kaddr == NULL);
10087 			}
10088 		}
10089 		if (flow_registration->ustats_uaddr != 0) {
10090 			ASSERT(error == 0);
10091 			ASSERT(flow_registration->stats_arena != NULL);
10092 			ASSERT(flow_registration->kstats_kaddr != NULL);
10093 
10094 			struct necp_all_kstats *kstats = (struct necp_all_kstats *)flow_registration->kstats_kaddr;
10095 			kstats->necp_stats_ustats->all_stats_u.tcp_stats.necp_tcp_hdr.necp_stats_type = bufreq->necp_stats_bufreq_type;
10096 			kstats->necp_stats_ustats->all_stats_u.tcp_stats.necp_tcp_hdr.necp_stats_ver = bufreq->necp_stats_bufreq_ver;
10097 
10098 			if (out_header) {
10099 				out_header->necp_stats_type = bufreq->necp_stats_bufreq_type;
10100 				out_header->necp_stats_ver = bufreq->necp_stats_bufreq_ver;
10101 			}
10102 
10103 			bufreq->necp_stats_bufreq_uaddr = flow_registration->ustats_uaddr;
10104 		}
10105 	} else {
10106 		error = EINVAL;
10107 	}
10108 
10109 	return error;
10110 }
10111 
10112 static int
necp_client_stats_initial(struct necp_client_flow_registration * flow_registration,uint32_t stats_type,uint32_t stats_ver)10113 necp_client_stats_initial(struct necp_client_flow_registration *flow_registration, uint32_t stats_type, uint32_t stats_ver)
10114 {
10115 	// An attempted create
10116 	assert(flow_registration->stats_handler_context == NULL);
10117 	assert(flow_registration->stats_arena);
10118 	assert(flow_registration->ustats_uaddr);
10119 	assert(flow_registration->kstats_kaddr);
10120 
10121 	int error = 0;
10122 	uint64_t ntstat_properties = necp_find_netstat_initial_properties(flow_registration->client);
10123 
10124 	switch (stats_type) {
10125 	case NECP_CLIENT_STATISTICS_TYPE_TCP: {
10126 		if (stats_ver == NECP_CLIENT_STATISTICS_TYPE_TCP_VER_1) {
10127 			flow_registration->stats_handler_context = ntstat_userland_stats_open((userland_stats_provider_context *)flow_registration,
10128 			    NSTAT_PROVIDER_TCP_USERLAND, ntstat_properties, necp_request_tcp_netstats, necp_find_extension_info);
10129 			if (flow_registration->stats_handler_context == NULL) {
10130 				error = EIO;
10131 			}
10132 		} else {
10133 			error = ENOTSUP;
10134 		}
10135 		break;
10136 	}
10137 	case NECP_CLIENT_STATISTICS_TYPE_UDP: {
10138 		if (stats_ver == NECP_CLIENT_STATISTICS_TYPE_UDP_VER_1) {
10139 			flow_registration->stats_handler_context = ntstat_userland_stats_open((userland_stats_provider_context *)flow_registration,
10140 			    NSTAT_PROVIDER_UDP_USERLAND, ntstat_properties, necp_request_udp_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_QUIC: {
10150 		if (stats_ver == NECP_CLIENT_STATISTICS_TYPE_QUIC_VER_1 && flow_registration->flags & NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS) {
10151 			flow_registration->stats_handler_context = ntstat_userland_stats_open((userland_stats_provider_context *)flow_registration,
10152 			    NSTAT_PROVIDER_QUIC_USERLAND, ntstat_properties, necp_request_quic_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 	default: {
10162 		error = ENOTSUP;
10163 		break;
10164 	}
10165 	}
10166 	return error;
10167 }
10168 
10169 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)10170 necp_stats_initialize(struct necp_fd_data *fd_data,
10171     struct necp_client *client,
10172     struct necp_client_flow_registration *flow_registration,
10173     struct necp_stats_bufreq *bufreq)
10174 {
10175 	int error = 0;
10176 	struct necp_stats_hdr stats_hdr = {};
10177 
10178 	NECP_CLIENT_ASSERT_LOCKED(client);
10179 	NECP_FD_ASSERT_LOCKED(fd_data);
10180 	VERIFY(fd_data->stats_arena_active != NULL);
10181 	VERIFY(fd_data->stats_arena_active->nai_arena != NULL);
10182 	VERIFY(!(fd_data->stats_arena_active->nai_flags & (NAIF_REDIRECT | NAIF_DEFUNCT)));
10183 
10184 	if (bufreq == NULL) {
10185 		return EINVAL;
10186 	}
10187 
10188 	// Setup stats region
10189 	error = necp_client_stats_bufreq(fd_data, client, flow_registration, bufreq, &stats_hdr);
10190 	if (error) {
10191 		return error;
10192 	}
10193 	// Notify ntstat about new flow
10194 	if (flow_registration->stats_handler_context == NULL) {
10195 		error = necp_client_stats_initial(flow_registration, stats_hdr.necp_stats_type, stats_hdr.necp_stats_ver);
10196 		if (flow_registration->stats_handler_context != NULL) {
10197 			ntstat_userland_stats_event(flow_registration->stats_handler_context, NECP_CLIENT_STATISTICS_EVENT_INIT);
10198 		}
10199 		NECP_CLIENT_FLOW_LOG(client, flow_registration, "Initialized stats <error %d>", error);
10200 	}
10201 
10202 	return error;
10203 }
10204 
10205 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)10206 necp_client_map_sysctls(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10207 {
10208 	int result = 0;
10209 	if (!retval) {
10210 		retval = &result;
10211 	}
10212 
10213 	do {
10214 		mach_vm_address_t uaddr = 0;
10215 		if (uap->buffer_size != sizeof(uaddr)) {
10216 			*retval = EINVAL;
10217 			break;
10218 		}
10219 
10220 		*retval = necp_sysctl_arena_initialize(fd_data, false);
10221 		if (*retval != 0) {
10222 			break;
10223 		}
10224 
10225 		mach_vm_offset_t off = 0;
10226 		void * __single location = NULL;
10227 		NECP_FD_LOCK(fd_data);
10228 		location = necp_arena_sysctls_obj(fd_data, &off, NULL);
10229 		NECP_FD_UNLOCK(fd_data);
10230 
10231 		if (location == NULL) {
10232 			*retval = ENOENT;
10233 			break;
10234 		}
10235 
10236 		uaddr = fd_data->sysctl_mmap.ami_mapaddr + off;
10237 		*retval = copyout(&uaddr, uap->buffer, sizeof(uaddr));
10238 	} while (false);
10239 
10240 	return *retval;
10241 }
10242 
10243 #endif /* !SKYWALK */
10244 
10245 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)10246 necp_client_copy_route_statistics(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10247 {
10248 	int error = 0;
10249 	struct necp_client *client = NULL;
10250 	uuid_t client_id;
10251 
10252 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
10253 	    uap->buffer_size < sizeof(struct necp_stat_counts) || uap->buffer == 0) {
10254 		NECPLOG0(LOG_ERR, "necp_client_copy_route_statistics bad input");
10255 		error = EINVAL;
10256 		goto done;
10257 	}
10258 
10259 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
10260 	if (error) {
10261 		NECPLOG(LOG_ERR, "necp_client_copy_route_statistics copyin client_id error (%d)", error);
10262 		goto done;
10263 	}
10264 
10265 	// Lock
10266 	NECP_FD_LOCK(fd_data);
10267 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
10268 	if (client != NULL) {
10269 		NECP_CLIENT_ROUTE_LOCK(client);
10270 		struct necp_stat_counts route_stats = {};
10271 		if (client->current_route != NULL && client->current_route->rt_stats != NULL) {
10272 			struct nstat_counts     *rt_stats = client->current_route->rt_stats;
10273 			route_stats.necp_stat_rxpackets = os_atomic_load(&rt_stats->nstat_rxpackets, relaxed);
10274 			route_stats.necp_stat_rxbytes = os_atomic_load(&rt_stats->nstat_rxbytes, relaxed);
10275 			route_stats.necp_stat_txpackets = os_atomic_load(&rt_stats->nstat_txpackets, relaxed);
10276 			route_stats.necp_stat_txbytes = os_atomic_load(&rt_stats->nstat_txbytes, relaxed);
10277 			route_stats.necp_stat_rxduplicatebytes = rt_stats->nstat_rxduplicatebytes;
10278 			route_stats.necp_stat_rxoutoforderbytes = rt_stats->nstat_rxoutoforderbytes;
10279 			route_stats.necp_stat_txretransmit = rt_stats->nstat_txretransmit;
10280 			route_stats.necp_stat_connectattempts = rt_stats->nstat_connectattempts;
10281 			route_stats.necp_stat_connectsuccesses = rt_stats->nstat_connectsuccesses;
10282 			route_stats.necp_stat_min_rtt = rt_stats->nstat_min_rtt;
10283 			route_stats.necp_stat_avg_rtt = rt_stats->nstat_avg_rtt;
10284 			route_stats.necp_stat_var_rtt = rt_stats->nstat_var_rtt;
10285 			route_stats.necp_stat_route_flags = client->current_route->rt_flags;
10286 		}
10287 
10288 		// Unlock before copying out
10289 		NECP_CLIENT_ROUTE_UNLOCK(client);
10290 		NECP_CLIENT_UNLOCK(client);
10291 		NECP_FD_UNLOCK(fd_data);
10292 
10293 		error = copyout(&route_stats, uap->buffer, sizeof(route_stats));
10294 		if (error) {
10295 			NECPLOG(LOG_ERR, "necp_client_copy_route_statistics copyout error (%d)", error);
10296 		}
10297 	} else {
10298 		// Unlock
10299 		NECP_FD_UNLOCK(fd_data);
10300 		error = ENOENT;
10301 	}
10302 
10303 
10304 done:
10305 	*retval = error;
10306 	return error;
10307 }
10308 
10309 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_update_cache(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)10310 necp_client_update_cache(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10311 {
10312 	int error = 0;
10313 	struct necp_client *client = NULL;
10314 	uuid_t client_id;
10315 
10316 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
10317 		error = EINVAL;
10318 		goto done;
10319 	}
10320 
10321 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
10322 	if (error) {
10323 		NECPLOG(LOG_ERR, "necp_client_update_cache copyin client_id error (%d)", error);
10324 		goto done;
10325 	}
10326 
10327 	NECP_FD_LOCK(fd_data);
10328 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
10329 	if (client == NULL) {
10330 		NECP_FD_UNLOCK(fd_data);
10331 		error = ENOENT;
10332 		goto done;
10333 	}
10334 
10335 	struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
10336 	if (flow_registration == NULL) {
10337 		NECP_CLIENT_UNLOCK(client);
10338 		NECP_FD_UNLOCK(fd_data);
10339 		error = ENOENT;
10340 		goto done;
10341 	}
10342 
10343 	NECP_CLIENT_ROUTE_LOCK(client);
10344 	// This needs to be changed when TFO/ECN is supported by multiple flows
10345 	struct necp_client_flow *flow = LIST_FIRST(&flow_registration->flow_list);
10346 	if (flow == NULL ||
10347 	    (flow->remote_addr.sa.sa_family != AF_INET &&
10348 	    flow->remote_addr.sa.sa_family != AF_INET6) ||
10349 	    (flow->local_addr.sa.sa_family != AF_INET &&
10350 	    flow->local_addr.sa.sa_family != AF_INET6)) {
10351 		error = EINVAL;
10352 		NECPLOG(LOG_ERR, "necp_client_update_cache no flow error (%d)", error);
10353 		goto done_unlock;
10354 	}
10355 
10356 	necp_cache_buffer cache_buffer;
10357 	memset(&cache_buffer, 0, sizeof(cache_buffer));
10358 
10359 	if (uap->buffer_size != sizeof(necp_cache_buffer) ||
10360 	    uap->buffer == USER_ADDR_NULL) {
10361 		error = EINVAL;
10362 		goto done_unlock;
10363 	}
10364 
10365 	error = copyin(uap->buffer, &cache_buffer, sizeof(cache_buffer));
10366 	if (error) {
10367 		NECPLOG(LOG_ERR, "necp_client_update_cache copyin cache buffer error (%d)", error);
10368 		goto done_unlock;
10369 	}
10370 
10371 	if (cache_buffer.necp_cache_buf_type == NECP_CLIENT_CACHE_TYPE_ECN &&
10372 	    cache_buffer.necp_cache_buf_ver == NECP_CLIENT_CACHE_TYPE_ECN_VER_1) {
10373 		if (cache_buffer.necp_cache_buf_size != sizeof(necp_tcp_ecn_cache) ||
10374 		    cache_buffer.necp_cache_buf_addr == USER_ADDR_NULL) {
10375 			error = EINVAL;
10376 			goto done_unlock;
10377 		}
10378 
10379 		necp_tcp_ecn_cache ecn_cache_buffer;
10380 		memset(&ecn_cache_buffer, 0, sizeof(ecn_cache_buffer));
10381 
10382 		error = copyin(cache_buffer.necp_cache_buf_addr, &ecn_cache_buffer, sizeof(necp_tcp_ecn_cache));
10383 		if (error) {
10384 			NECPLOG(LOG_ERR, "necp_client_update_cache copyin ecn cache buffer error (%d)", error);
10385 			goto done_unlock;
10386 		}
10387 
10388 		if (client->current_route != NULL && client->current_route->rt_ifp != NULL) {
10389 			if (!client->platform_binary) {
10390 				ecn_cache_buffer.necp_tcp_ecn_heuristics_success = 0;
10391 			}
10392 			tcp_heuristics_ecn_update(&ecn_cache_buffer, client->current_route->rt_ifp,
10393 			    (union sockaddr_in_4_6 *)&flow->local_addr);
10394 		}
10395 	} else if (cache_buffer.necp_cache_buf_type == NECP_CLIENT_CACHE_TYPE_TFO &&
10396 	    cache_buffer.necp_cache_buf_ver == NECP_CLIENT_CACHE_TYPE_TFO_VER_1) {
10397 		if (cache_buffer.necp_cache_buf_size != sizeof(necp_tcp_tfo_cache) ||
10398 		    cache_buffer.necp_cache_buf_addr == USER_ADDR_NULL) {
10399 			error = EINVAL;
10400 			goto done_unlock;
10401 		}
10402 
10403 		necp_tcp_tfo_cache tfo_cache_buffer;
10404 		memset(&tfo_cache_buffer, 0, sizeof(tfo_cache_buffer));
10405 
10406 		error = copyin(cache_buffer.necp_cache_buf_addr, &tfo_cache_buffer, sizeof(necp_tcp_tfo_cache));
10407 		if (error) {
10408 			NECPLOG(LOG_ERR, "necp_client_update_cache copyin tfo cache buffer error (%d)", error);
10409 			goto done_unlock;
10410 		}
10411 
10412 		if (client->current_route != NULL && client->current_route->rt_ifp != NULL) {
10413 			if (!client->platform_binary) {
10414 				tfo_cache_buffer.necp_tcp_tfo_heuristics_success = 0;
10415 			}
10416 			tcp_heuristics_tfo_update(&tfo_cache_buffer, client->current_route->rt_ifp,
10417 			    (union sockaddr_in_4_6 *)&flow->local_addr,
10418 			    (union sockaddr_in_4_6 *)&flow->remote_addr);
10419 		}
10420 	} else {
10421 		error = EINVAL;
10422 	}
10423 done_unlock:
10424 	NECP_CLIENT_ROUTE_UNLOCK(client);
10425 	NECP_CLIENT_UNLOCK(client);
10426 	NECP_FD_UNLOCK(fd_data);
10427 done:
10428 	*retval = error;
10429 	return error;
10430 }
10431 
10432 // Most results will fit into this size
10433 struct necp_client_signable_default {
10434 	uuid_t client_id;
10435 	u_int32_t sign_type;
10436 	u_int8_t signable_data[NECP_CLIENT_ACTION_SIGN_DEFAULT_DATA_LENGTH];
10437 } __attribute__((__packed__));
10438 
10439 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_sign(__unused struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)10440 necp_client_sign(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10441 {
10442 	int error = 0;
10443 	u_int8_t tag[NECP_CLIENT_ACTION_SIGN_TAG_LENGTH] = {};
10444 	struct necp_client_signable * __indexable signable = NULL;
10445 	struct necp_client_signable * __indexable allocated_signable = NULL;
10446 	struct necp_client_signable_default default_signable = {};
10447 	size_t tag_size = sizeof(tag);
10448 
10449 	const size_t signable_length = uap->client_id_len;
10450 	const size_t return_tag_length = uap->buffer_size;
10451 
10452 	*retval = 0;
10453 
10454 	const bool has_resolver_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_VALIDATED_RESOLVER, 0) == 0);
10455 	if (!has_resolver_entitlement) {
10456 		NECPLOG0(LOG_ERR, "Process does not hold the necessary entitlement to sign resolver answers");
10457 		error = EPERM;
10458 		goto done;
10459 	}
10460 
10461 	if (uap->client_id == 0 || signable_length < sizeof(*signable) || signable_length > NECP_CLIENT_ACTION_SIGN_MAX_TOTAL_LENGTH) {
10462 		error = EINVAL;
10463 		goto done;
10464 	}
10465 
10466 	if (uap->buffer == 0 || return_tag_length != NECP_CLIENT_ACTION_SIGN_TAG_LENGTH) {
10467 		error = EINVAL;
10468 		goto done;
10469 	}
10470 
10471 	if (signable_length <= sizeof(default_signable)) {
10472 		signable = (struct necp_client_signable *)&default_signable;
10473 	} else {
10474 		if ((allocated_signable = (struct necp_client_signable *)kalloc_data(signable_length, Z_WAITOK | Z_ZERO)) == NULL) {
10475 			NECPLOG(LOG_ERR, "necp_client_sign allocate signable %zu failed", signable_length);
10476 			error = ENOMEM;
10477 			goto done;
10478 		}
10479 		signable = allocated_signable;
10480 	}
10481 
10482 	error = copyin(uap->client_id, signable, signable_length);
10483 	if (error) {
10484 		NECPLOG(LOG_ERR, "necp_client_sign copyin signable error (%d)", error);
10485 		goto done;
10486 	}
10487 
10488 	size_t data_length = 0;
10489 	switch (signable->sign_type) {
10490 	case NECP_CLIENT_SIGN_TYPE_RESOLVER_ANSWER:
10491 	case NECP_CLIENT_SIGN_TYPE_SYSTEM_RESOLVER_ANSWER: {
10492 		data_length = (sizeof(struct necp_client_host_resolver_answer) - sizeof(struct necp_client_signable));
10493 		if (signable_length < (sizeof(struct necp_client_signable) + data_length)) {
10494 			error = EINVAL;
10495 			goto done;
10496 		}
10497 		struct necp_client_host_resolver_answer * __single signable_struct = (struct necp_client_host_resolver_answer *)signable;
10498 		if (signable_struct->hostname_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10499 		    signable_length != (sizeof(struct necp_client_signable) + data_length + signable_struct->hostname_length)) {
10500 			error = EINVAL;
10501 			goto done;
10502 		}
10503 		data_length += signable_struct->hostname_length;
10504 		break;
10505 	}
10506 	case NECP_CLIENT_SIGN_TYPE_BROWSE_RESULT:
10507 	case NECP_CLIENT_SIGN_TYPE_SYSTEM_BROWSE_RESULT: {
10508 		data_length = (sizeof(struct necp_client_browse_result) - sizeof(struct necp_client_signable));
10509 		if (signable_length < (sizeof(struct necp_client_signable) + data_length)) {
10510 			error = EINVAL;
10511 			goto done;
10512 		}
10513 		struct necp_client_browse_result *signable_struct = (struct necp_client_browse_result *)signable;
10514 		if (signable_struct->service_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10515 		    signable_length != (sizeof(struct necp_client_signable) + data_length + signable_struct->service_length)) {
10516 			error = EINVAL;
10517 			goto done;
10518 		}
10519 		data_length += signable_struct->service_length;
10520 		break;
10521 	}
10522 	case NECP_CLIENT_SIGN_TYPE_SERVICE_RESOLVER_ANSWER:
10523 	case NECP_CLIENT_SIGN_TYPE_SYSTEM_SERVICE_RESOLVER_ANSWER: {
10524 		data_length = (sizeof(struct necp_client_service_resolver_answer) - sizeof(struct necp_client_signable));
10525 		if (signable_length < (sizeof(struct necp_client_signable) + data_length)) {
10526 			error = EINVAL;
10527 			goto done;
10528 		}
10529 		struct necp_client_service_resolver_answer * __single signable_struct = (struct necp_client_service_resolver_answer *)signable;
10530 		if (signable_struct->service_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10531 		    signable_struct->hostname_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10532 		    signable_length != (sizeof(struct necp_client_signable) + data_length + signable_struct->service_length + signable_struct->hostname_length)) {
10533 			error = EINVAL;
10534 			goto done;
10535 		}
10536 		data_length += signable_struct->service_length;
10537 		data_length += signable_struct->hostname_length;
10538 		break;
10539 	}
10540 	default: {
10541 		NECPLOG(LOG_ERR, "necp_client_sign unknown signable type (%u)", signable->sign_type);
10542 		error = EINVAL;
10543 		goto done;
10544 	}
10545 	}
10546 
10547 	error = necp_sign_resolver_answer(signable->client_id, signable->sign_type,
10548 	    signable_get_data(signable, data_length), data_length,
10549 	    tag, &tag_size);
10550 	if (tag_size != sizeof(tag)) {
10551 		NECPLOG(LOG_ERR, "necp_client_sign unexpected tag size %zu", tag_size);
10552 		error = EINVAL;
10553 		goto done;
10554 	}
10555 	error = copyout(tag, uap->buffer, tag_size);
10556 	if (error) {
10557 		NECPLOG(LOG_ERR, "necp_client_sign copyout error (%d)", error);
10558 		goto done;
10559 	}
10560 
10561 done:
10562 	if (allocated_signable != NULL) {
10563 		kfree_data(allocated_signable, signable_length);
10564 		allocated_signable = NULL;
10565 	}
10566 	*retval = error;
10567 	return error;
10568 }
10569 
10570 // Most results will fit into this size
10571 struct necp_client_validatable_default {
10572 	struct necp_client_signature signature;
10573 	struct necp_client_signable_default signable;
10574 } __attribute__((__packed__));
10575 
10576 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_validate(__unused struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)10577 necp_client_validate(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10578 {
10579 	int error = 0;
10580 	struct necp_client_validatable *validatable = NULL;
10581 	struct necp_client_validatable * __single allocated_validatable = NULL;
10582 	struct necp_client_validatable_default default_validatable = {};
10583 
10584 	const size_t validatable_length = uap->client_id_len;
10585 
10586 	*retval = 0;
10587 
10588 	const bool has_resolver_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_VALIDATED_RESOLVER, 0) == 0);
10589 	if (!has_resolver_entitlement) {
10590 		NECPLOG0(LOG_ERR, "Process does not hold the necessary entitlement to directly validate resolver answers");
10591 		error = EPERM;
10592 		goto done;
10593 	}
10594 
10595 	if (uap->client_id == 0 || validatable_length < sizeof(*validatable) ||
10596 	    validatable_length > (NECP_CLIENT_ACTION_SIGN_MAX_TOTAL_LENGTH + NECP_CLIENT_ACTION_SIGN_TAG_LENGTH)) {
10597 		error = EINVAL;
10598 		goto done;
10599 	}
10600 
10601 	if (validatable_length <= sizeof(default_validatable)) {
10602 		validatable = (struct necp_client_validatable *)&default_validatable;
10603 	} else {
10604 		if ((allocated_validatable = (struct necp_client_validatable *)kalloc_data(validatable_length, Z_WAITOK | Z_ZERO)) == NULL) {
10605 			NECPLOG(LOG_ERR, "necp_client_validate allocate struct %zu failed", validatable_length);
10606 			error = ENOMEM;
10607 			goto done;
10608 		}
10609 		validatable = allocated_validatable;
10610 	}
10611 
10612 	error = copyin(uap->client_id, validatable, validatable_length);
10613 	if (error) {
10614 		NECPLOG(LOG_ERR, "necp_client_validate copyin error (%d)", error);
10615 		goto done;
10616 	}
10617 
10618 	size_t signable_data_len = validatable_length - sizeof(struct necp_client_validatable);
10619 	const bool validated = necp_validate_resolver_answer(validatable->signable.client_id, validatable->signable.sign_type,
10620 	    signable_get_data(&validatable->signable, signable_data_len), signable_data_len,
10621 	    validatable->signature.signed_tag, sizeof(validatable->signature.signed_tag));
10622 	if (!validated) {
10623 		// Return EAUTH to indicate that the signature failed
10624 		error = EAUTH;
10625 	}
10626 
10627 done:
10628 	if (allocated_validatable != NULL) {
10629 		kfree_data(allocated_validatable, validatable_length);
10630 		allocated_validatable = NULL;
10631 	}
10632 	*retval = error;
10633 	return error;
10634 }
10635 
10636 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)10637 necp_client_get_signed_client_id(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10638 {
10639 	int error = 0;
10640 	*retval = 0;
10641 	u_int32_t request_type = 0;
10642 	struct necp_client_signed_client_id_uuid client_id = { 0 };
10643 	const size_t buffer_size = uap->buffer_size;
10644 	u_int8_t tag[NECP_CLIENT_ACTION_SIGN_TAG_LENGTH] = {};
10645 	size_t tag_size = sizeof(tag);
10646 	proc_t proc = current_proc();
10647 	if (uap->client_id == 0 || uap->client_id_len != sizeof(u_int32_t) ||
10648 	    buffer_size < sizeof(struct necp_client_signed_client_id_uuid) ||
10649 	    uap->buffer == 0) {
10650 		NECPLOG0(LOG_ERR, "necp_client_get_signed_client_id bad input");
10651 		error = EINVAL;
10652 		goto done;
10653 	}
10654 
10655 	error = copyin(uap->client_id, &request_type, sizeof(u_int32_t));
10656 	if (error) {
10657 		NECPLOG(LOG_ERR, "necp_client_get_signed_client_id copyin request_type error (%d)", error);
10658 		goto done;
10659 	}
10660 
10661 	if (request_type != NECP_CLIENT_SIGNED_CLIENT_ID_TYPE_UUID) {
10662 		error = ENOENT;
10663 		NECPLOG(LOG_ERR, "necp_client_get_signed_client_id bad request_type (%d)", request_type);
10664 		goto done;
10665 	}
10666 
10667 	uuid_t application_uuid;
10668 	uuid_clear(application_uuid);
10669 	proc_getexecutableuuid(proc, application_uuid, sizeof(application_uuid));
10670 
10671 	error = necp_sign_application_id(application_uuid,
10672 	    NECP_CLIENT_SIGNED_CLIENT_ID_TYPE_UUID,
10673 	    tag, &tag_size);
10674 	if (tag_size != sizeof(tag)) {
10675 		NECPLOG(LOG_ERR, "necp_client_get_signed_client_id unexpected tag size %zu", tag_size);
10676 		error = EINVAL;
10677 		goto done;
10678 	}
10679 	uuid_copy(client_id.client_id, application_uuid);
10680 	client_id.signature_length = tag_size;
10681 	memcpy(client_id.signature_data, tag, tag_size);
10682 
10683 	error = copyout(&client_id, uap->buffer, sizeof(client_id));
10684 	if (error != 0) {
10685 		NECPLOG(LOG_ERR, "necp_client_get_signed_client_id copyout error (%d)", error);
10686 		goto done;
10687 	}
10688 
10689 done:
10690 	*retval = error;
10691 	return error;
10692 }
10693 
10694 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)10695 necp_client_set_signed_client_id(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10696 {
10697 	int error = 0;
10698 	*retval = 0;
10699 	u_int32_t request_type = 0;
10700 	struct necp_client_signed_client_id_uuid client_id = { 0 };
10701 	const size_t buffer_size = uap->buffer_size;
10702 
10703 	// Only allow entitled processes to set the client ID.
10704 	proc_t proc = current_proc();
10705 	task_t __single task = proc_task(proc);
10706 	bool has_delegation_entitlement = task != NULL && IOTaskHasEntitlement(task, kCSWebBrowserNetworkEntitlement);
10707 	if (!has_delegation_entitlement) {
10708 		has_delegation_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_SOCKET_DELEGATE, 0) == 0);
10709 	}
10710 	if (!has_delegation_entitlement) {
10711 		NECPLOG0(LOG_ERR, "necp_client_set_signed_client_id client lacks the necessary entitlement");
10712 		error = EAUTH;
10713 		goto done;
10714 	}
10715 
10716 	if (uap->client_id == 0 || uap->client_id_len != sizeof(u_int32_t) ||
10717 	    buffer_size < sizeof(struct necp_client_signed_client_id_uuid) ||
10718 	    uap->buffer == 0) {
10719 		NECPLOG0(LOG_ERR, "necp_client_set_signed_client_id bad input");
10720 		error = EINVAL;
10721 		goto done;
10722 	}
10723 
10724 	error = copyin(uap->client_id, &request_type, sizeof(u_int32_t));
10725 	if (error) {
10726 		NECPLOG(LOG_ERR, "necp_client_set_signed_client_id copyin request_type error (%d)", error);
10727 		goto done;
10728 	}
10729 
10730 	if (request_type != NECP_CLIENT_SIGNED_CLIENT_ID_TYPE_UUID) {
10731 		error = ENOENT;
10732 		NECPLOG(LOG_ERR, "necp_client_set_signed_client_id bad request_type (%d)", request_type);
10733 		goto done;
10734 	}
10735 
10736 	error = copyin(uap->buffer, &client_id, sizeof(struct necp_client_signed_client_id_uuid));
10737 	if (error) {
10738 		NECPLOG(LOG_ERR, "necp_client_set_signed_client_id copyin request error (%d)", error);
10739 		goto done;
10740 	}
10741 
10742 	const bool validated = necp_validate_application_id(client_id.client_id,
10743 	    NECP_CLIENT_SIGNED_CLIENT_ID_TYPE_UUID,
10744 	    client_id.signature_data, sizeof(client_id.signature_data));
10745 	if (!validated) {
10746 		// Return EAUTH to indicate that the signature failed
10747 		error = EAUTH;
10748 		NECPLOG(LOG_ERR, "necp_client_set_signed_client_id signature validation failed (%d)", error);
10749 		goto done;
10750 	}
10751 
10752 	proc_setresponsibleuuid(proc, client_id.client_id, sizeof(client_id.client_id));
10753 
10754 done:
10755 	*retval = error;
10756 	return error;
10757 }
10758 
10759 int
necp_client_action(struct proc * p,struct necp_client_action_args * uap,int * retval)10760 necp_client_action(struct proc *p, struct necp_client_action_args *uap, int *retval)
10761 {
10762 	struct fileproc * __single fp;
10763 	int error = 0;
10764 	int return_value = 0;
10765 	struct necp_fd_data * __single fd_data = NULL;
10766 
10767 	error = necp_find_fd_data(p, uap->necp_fd, &fp, &fd_data);
10768 	if (error != 0) {
10769 		NECPLOG(LOG_ERR, "necp_client_action find fd error (%d)", error);
10770 		return error;
10771 	}
10772 
10773 	u_int32_t action = uap->action;
10774 
10775 #if CONFIG_MACF
10776 	error = mac_necp_check_client_action(p, fp->fp_glob, action);
10777 	if (error) {
10778 		return_value = error;
10779 		goto done;
10780 	}
10781 #endif /* MACF */
10782 
10783 	switch (action) {
10784 	case NECP_CLIENT_ACTION_ADD: {
10785 		return_value = necp_client_add(p, fd_data, uap, retval);
10786 		break;
10787 	}
10788 	case NECP_CLIENT_ACTION_CLAIM: {
10789 		return_value = necp_client_claim(p, fd_data, uap, retval);
10790 		break;
10791 	}
10792 	case NECP_CLIENT_ACTION_REMOVE: {
10793 		return_value = necp_client_remove(fd_data, uap, retval);
10794 		break;
10795 	}
10796 	case NECP_CLIENT_ACTION_COPY_PARAMETERS:
10797 	case NECP_CLIENT_ACTION_COPY_RESULT:
10798 	case NECP_CLIENT_ACTION_COPY_UPDATED_RESULT:
10799 	case NECP_CLIENT_ACTION_COPY_UPDATED_RESULT_FINAL: {
10800 		return_value = necp_client_copy(fd_data, uap, retval);
10801 		break;
10802 	}
10803 	case NECP_CLIENT_ACTION_COPY_LIST: {
10804 		return_value = necp_client_list(fd_data, uap, retval);
10805 		break;
10806 	}
10807 	case NECP_CLIENT_ACTION_ADD_FLOW: {
10808 		return_value = necp_client_add_flow(fd_data, uap, retval);
10809 		break;
10810 	}
10811 	case NECP_CLIENT_ACTION_REMOVE_FLOW: {
10812 		return_value = necp_client_remove_flow(fd_data, uap, retval);
10813 		break;
10814 	}
10815 #if SKYWALK
10816 	case NECP_CLIENT_ACTION_REQUEST_NEXUS_INSTANCE: {
10817 		return_value = necp_client_request_nexus(fd_data, uap, retval);
10818 		break;
10819 	}
10820 #endif /* !SKYWALK */
10821 	case NECP_CLIENT_ACTION_AGENT: {
10822 		return_value = necp_client_agent_action(fd_data, uap, retval);
10823 		break;
10824 	}
10825 	case NECP_CLIENT_ACTION_COPY_AGENT: {
10826 		return_value = necp_client_copy_agent(fd_data, uap, retval);
10827 		break;
10828 	}
10829 	case NECP_CLIENT_ACTION_AGENT_USE: {
10830 		return_value = necp_client_agent_use(fd_data, uap, retval);
10831 		break;
10832 	}
10833 	case NECP_CLIENT_ACTION_ACQUIRE_AGENT_TOKEN: {
10834 		return_value = necp_client_acquire_agent_token(fd_data, uap, retval);
10835 		break;
10836 	}
10837 	case NECP_CLIENT_ACTION_COPY_INTERFACE: {
10838 		return_value = necp_client_copy_interface(fd_data, uap, retval);
10839 		break;
10840 	}
10841 #if SKYWALK
10842 	case NECP_CLIENT_ACTION_GET_INTERFACE_ADDRESS: {
10843 		return_value = necp_client_get_interface_address(fd_data, uap, retval);
10844 		break;
10845 	}
10846 	case NECP_CLIENT_ACTION_SET_STATISTICS: {
10847 		return_value = ENOTSUP;
10848 		break;
10849 	}
10850 	case NECP_CLIENT_ACTION_MAP_SYSCTLS: {
10851 		return_value = necp_client_map_sysctls(fd_data, uap, retval);
10852 		break;
10853 	}
10854 #endif /* !SKYWALK */
10855 	case NECP_CLIENT_ACTION_COPY_ROUTE_STATISTICS: {
10856 		return_value = necp_client_copy_route_statistics(fd_data, uap, retval);
10857 		break;
10858 	}
10859 	case NECP_CLIENT_ACTION_UPDATE_CACHE: {
10860 		return_value = necp_client_update_cache(fd_data, uap, retval);
10861 		break;
10862 	}
10863 	case NECP_CLIENT_ACTION_COPY_CLIENT_UPDATE: {
10864 		return_value = necp_client_copy_client_update(fd_data, uap, retval);
10865 		break;
10866 	}
10867 	case NECP_CLIENT_ACTION_SIGN: {
10868 		return_value = necp_client_sign(fd_data, uap, retval);
10869 		break;
10870 	}
10871 	case NECP_CLIENT_ACTION_VALIDATE: {
10872 		return_value = necp_client_validate(fd_data, uap, retval);
10873 		break;
10874 	}
10875 	case NECP_CLIENT_ACTION_GET_SIGNED_CLIENT_ID: {
10876 		return_value = necp_client_get_signed_client_id(fd_data, uap, retval);
10877 		break;
10878 	}
10879 	case NECP_CLIENT_ACTION_SET_SIGNED_CLIENT_ID: {
10880 		return_value = necp_client_set_signed_client_id(fd_data, uap, retval);
10881 		break;
10882 	}
10883 	default: {
10884 		NECPLOG(LOG_ERR, "necp_client_action unknown action (%u)", action);
10885 		return_value = EINVAL;
10886 		break;
10887 	}
10888 	}
10889 
10890 done:
10891 	fp_drop(p, uap->necp_fd, fp, 0);
10892 	return return_value;
10893 }
10894 
10895 #define NECP_MAX_MATCH_POLICY_PARAMETER_SIZE 1024
10896 
10897 int
necp_match_policy(struct proc * p,struct necp_match_policy_args * uap,int32_t * retval)10898 necp_match_policy(struct proc *p, struct necp_match_policy_args *uap, int32_t *retval)
10899 {
10900 #pragma unused(retval)
10901 	size_t buffer_size = 0;
10902 	u_int8_t * __sized_by(buffer_size) parameters = NULL;
10903 	struct necp_aggregate_result returned_result;
10904 	int error = 0;
10905 
10906 	if (uap == NULL) {
10907 		error = EINVAL;
10908 		goto done;
10909 	}
10910 
10911 	if (uap->parameters == 0 || uap->parameters_size == 0 || uap->parameters_size > NECP_MAX_MATCH_POLICY_PARAMETER_SIZE || uap->returned_result == 0) {
10912 		error = EINVAL;
10913 		goto done;
10914 	}
10915 
10916 	parameters = (u_int8_t *)kalloc_data(uap->parameters_size, Z_WAITOK | Z_ZERO);
10917 	buffer_size = uap->parameters_size;
10918 	if (parameters == NULL) {
10919 		error = ENOMEM;
10920 		goto done;
10921 	}
10922 	// Copy parameters in
10923 	error = copyin(uap->parameters, parameters, buffer_size);
10924 	if (error) {
10925 		goto done;
10926 	}
10927 
10928 	error = necp_application_find_policy_match_internal(p, parameters, buffer_size,
10929 	    &returned_result, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, false, false, NULL);
10930 	if (error) {
10931 		goto done;
10932 	}
10933 
10934 	// Copy return value back
10935 	error = copyout(&returned_result, uap->returned_result, sizeof(struct necp_aggregate_result));
10936 	if (error) {
10937 		goto done;
10938 	}
10939 done:
10940 	if (parameters != NULL) {
10941 		kfree_data_sized_by(parameters, buffer_size);
10942 	}
10943 	return error;
10944 }
10945 
10946 /// Socket operations
10947 
10948 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)10949 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)
10950 {
10951 	int error = 0;
10952 	int cursor = 0;
10953 	size_t string_size = 0;
10954 	size_t local_string_length = 0;
10955 	char * __sized_by(local_string_length) local_string = NULL;
10956 	u_int8_t * __indexable value = NULL;
10957 	char * __indexable buffer_to_free = NULL;
10958 
10959 	cursor = necp_buffer_find_tlv(buffer, buffer_length, 0, type, NULL, 0);
10960 	if (cursor < 0) {
10961 		// This will clear out the parameter
10962 		goto done;
10963 	}
10964 
10965 	string_size = necp_buffer_get_tlv_length(buffer, buffer_length, cursor);
10966 	if (single_tlv != NULL && (buffer_length == sizeof(struct necp_tlv_header) + string_size)) {
10967 		*single_tlv = true;
10968 	}
10969 	if (string_size == 0 || string_size > NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) {
10970 		// This will clear out the parameter
10971 		goto done;
10972 	}
10973 
10974 	local_string = (char *)kalloc_data(string_size + 1, Z_WAITOK | Z_ZERO);
10975 	local_string_length = string_size + 1;
10976 	if (local_string == NULL) {
10977 		NECPLOG(LOG_ERR, "Failed to allocate a socket attribute buffer (size %zu)", string_size);
10978 		goto fail;
10979 	}
10980 
10981 	value = necp_buffer_get_tlv_value(buffer, buffer_length, cursor, NULL);
10982 	if (value == NULL) {
10983 		NECPLOG0(LOG_ERR, "Failed to get socket attribute");
10984 		goto fail;
10985 	}
10986 
10987 	memcpy(local_string, value, string_size);
10988 	local_string[string_size] = 0;
10989 
10990 done:
10991 	if (*buffer_p != NULL) {
10992 		buffer_to_free = __unsafe_null_terminated_to_indexable(*buffer_p);
10993 	}
10994 
10995 	// Protect switching of buffer pointer
10996 	necp_lock_socket_attributes();
10997 	if (local_string != NULL) {
10998 		*buffer_p = __unsafe_null_terminated_from_indexable(local_string, &local_string[string_size]);
10999 	} else {
11000 		*buffer_p = NULL;
11001 	}
11002 	necp_unlock_socket_attributes();
11003 
11004 	if (buffer_to_free != NULL) {
11005 		kfree_data_addr(buffer_to_free);
11006 	}
11007 	return 0;
11008 fail:
11009 	if (local_string != NULL) {
11010 		kfree_data_sized_by(local_string, local_string_length);
11011 	}
11012 	return error;
11013 }
11014 
11015 errno_t
necp_set_socket_attributes(struct inp_necp_attributes * attributes,struct sockopt * sopt)11016 necp_set_socket_attributes(struct inp_necp_attributes *attributes, struct sockopt *sopt)
11017 {
11018 	int error = 0;
11019 	u_int8_t *buffer = NULL;
11020 	bool single_tlv = false;
11021 	size_t valsize = sopt->sopt_valsize;
11022 	if (valsize == 0 ||
11023 	    valsize > ((sizeof(struct necp_tlv_header) + NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) * 4)) {
11024 		goto done;
11025 	}
11026 
11027 	buffer = (u_int8_t *)kalloc_data(valsize, Z_WAITOK | Z_ZERO);
11028 	if (buffer == NULL) {
11029 		goto done;
11030 	}
11031 
11032 	error = sooptcopyin(sopt, buffer, valsize, 0);
11033 	if (error) {
11034 		goto done;
11035 	}
11036 
11037 	// If NECP_TLV_ATTRIBUTE_DOMAIN_CONTEXT is being set/cleared separately from the other attributes,
11038 	// do not clear other attributes.
11039 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_DOMAIN_CONTEXT, &attributes->inp_domain_context, &single_tlv);
11040 	if (error) {
11041 		NECPLOG0(LOG_ERR, "Could not set domain context TLV for socket attributes");
11042 		goto done;
11043 	}
11044 	if (single_tlv == true) {
11045 		goto done;
11046 	}
11047 
11048 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_DOMAIN, &attributes->inp_domain, NULL);
11049 	if (error) {
11050 		NECPLOG0(LOG_ERR, "Could not set domain TLV for socket attributes");
11051 		goto done;
11052 	}
11053 
11054 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_DOMAIN_OWNER, &attributes->inp_domain_owner, NULL);
11055 	if (error) {
11056 		NECPLOG0(LOG_ERR, "Could not set domain owner TLV for socket attributes");
11057 		goto done;
11058 	}
11059 
11060 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_TRACKER_DOMAIN, &attributes->inp_tracker_domain, NULL);
11061 	if (error) {
11062 		NECPLOG0(LOG_ERR, "Could not set tracker domain TLV for socket attributes");
11063 		goto done;
11064 	}
11065 
11066 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_ACCOUNT, &attributes->inp_account, NULL);
11067 	if (error) {
11068 		NECPLOG0(LOG_ERR, "Could not set account TLV for socket attributes");
11069 		goto done;
11070 	}
11071 
11072 done:
11073 	NECP_SOCKET_ATTRIBUTE_LOG("NECP ATTRIBUTES SOCKET - domain <%s> owner <%s> context <%s> tracker domain <%s> account <%s>",
11074 	    attributes->inp_domain,
11075 	    attributes->inp_domain_owner,
11076 	    attributes->inp_domain_context,
11077 	    attributes->inp_tracker_domain,
11078 	    attributes->inp_account);
11079 
11080 	if (necp_debug) {
11081 		NECPLOG(LOG_DEBUG, "Set on socket: Domain %s, Domain owner %s, Domain context %s, Tracker domain %s, Account %s",
11082 		    attributes->inp_domain,
11083 		    attributes->inp_domain_owner,
11084 		    attributes->inp_domain_context,
11085 		    attributes->inp_tracker_domain,
11086 		    attributes->inp_account);
11087 	}
11088 
11089 	if (buffer != NULL) {
11090 		kfree_data(buffer, valsize);
11091 	}
11092 
11093 	return error;
11094 }
11095 
11096 errno_t
necp_get_socket_attributes(struct inp_necp_attributes * attributes,struct sockopt * sopt)11097 necp_get_socket_attributes(struct inp_necp_attributes *attributes, struct sockopt *sopt)
11098 {
11099 	int error = 0;
11100 	size_t valsize = 0;
11101 	u_int8_t *buffer = NULL;
11102 	u_int8_t * __indexable cursor = NULL;
11103 
11104 	if (attributes->inp_domain != NULL) {
11105 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_domain);
11106 	}
11107 	if (attributes->inp_domain_owner != NULL) {
11108 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_domain_owner);
11109 	}
11110 	if (attributes->inp_domain_context != NULL) {
11111 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_domain_context);
11112 	}
11113 	if (attributes->inp_tracker_domain != NULL) {
11114 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_tracker_domain);
11115 	}
11116 	if (attributes->inp_account != NULL) {
11117 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_account);
11118 	}
11119 	if (valsize == 0) {
11120 		goto done;
11121 	}
11122 
11123 	buffer = (u_int8_t *)kalloc_data(valsize, Z_WAITOK | Z_ZERO);
11124 	if (buffer == NULL) {
11125 		goto done;
11126 	}
11127 
11128 	cursor = buffer;
11129 	if (attributes->inp_domain != NULL) {
11130 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_DOMAIN, strlen(attributes->inp_domain), __terminated_by_to_indexable(attributes->inp_domain),
11131 		    buffer, valsize);
11132 	}
11133 
11134 	if (attributes->inp_domain_owner != NULL) {
11135 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_DOMAIN_OWNER, strlen(attributes->inp_domain_owner), __terminated_by_to_indexable(attributes->inp_domain_owner),
11136 		    buffer, valsize);
11137 	}
11138 
11139 	if (attributes->inp_domain_context != NULL) {
11140 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_DOMAIN_CONTEXT, strlen(attributes->inp_domain_context), __terminated_by_to_indexable(attributes->inp_domain_context),
11141 		    buffer, valsize);
11142 	}
11143 
11144 	if (attributes->inp_tracker_domain != NULL) {
11145 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_TRACKER_DOMAIN, strlen(attributes->inp_tracker_domain), __terminated_by_to_indexable(attributes->inp_tracker_domain),
11146 		    buffer, valsize);
11147 	}
11148 
11149 	if (attributes->inp_account != NULL) {
11150 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_ACCOUNT, strlen(attributes->inp_account), __terminated_by_to_indexable(attributes->inp_account),
11151 		    buffer, valsize);
11152 	}
11153 
11154 	error = sooptcopyout(sopt, buffer, valsize);
11155 	if (error) {
11156 		goto done;
11157 	}
11158 done:
11159 	if (buffer != NULL) {
11160 		kfree_data(buffer, valsize);
11161 	}
11162 
11163 	return error;
11164 }
11165 
11166 int
necp_set_socket_resolver_signature(struct inpcb * inp,struct sockopt * sopt)11167 necp_set_socket_resolver_signature(struct inpcb *inp, struct sockopt *sopt)
11168 {
11169 	const size_t valsize = sopt->sopt_valsize;
11170 	if (valsize > NECP_CLIENT_ACTION_SIGN_MAX_TOTAL_LENGTH + NECP_CLIENT_ACTION_SIGN_TAG_LENGTH) {
11171 		return EINVAL;
11172 	}
11173 
11174 	necp_lock_socket_attributes();
11175 	if (inp->inp_resolver_signature != NULL) {
11176 		kfree_data_sized_by(inp->inp_resolver_signature, inp->inp_resolver_signature_length);
11177 	}
11178 
11179 	int error = 0;
11180 	if (valsize > 0) {
11181 		inp->inp_resolver_signature = kalloc_data(valsize, Z_WAITOK | Z_ZERO);
11182 		inp->inp_resolver_signature_length = valsize;
11183 		if ((error = sooptcopyin(sopt, inp->inp_resolver_signature, valsize,
11184 		    valsize)) != 0) {
11185 			// Free the signature buffer if the copyin failed
11186 			kfree_data_sized_by(inp->inp_resolver_signature, inp->inp_resolver_signature_length);
11187 		}
11188 	}
11189 	necp_unlock_socket_attributes();
11190 
11191 	return error;
11192 }
11193 
11194 int
necp_get_socket_resolver_signature(struct inpcb * inp,struct sockopt * sopt)11195 necp_get_socket_resolver_signature(struct inpcb *inp, struct sockopt *sopt)
11196 {
11197 	int error = 0;
11198 	necp_lock_socket_attributes();
11199 	if (inp->inp_resolver_signature == NULL ||
11200 	    inp->inp_resolver_signature_length == 0) {
11201 		error = ENOENT;
11202 	} else {
11203 		error = sooptcopyout(sopt, inp->inp_resolver_signature,
11204 		    inp->inp_resolver_signature_length);
11205 	}
11206 	necp_unlock_socket_attributes();
11207 	return error;
11208 }
11209 
11210 bool
necp_socket_has_resolver_signature(struct inpcb * inp)11211 necp_socket_has_resolver_signature(struct inpcb *inp)
11212 {
11213 	necp_lock_socket_attributes();
11214 	bool has_signature = (inp->inp_resolver_signature != NULL && inp->inp_resolver_signature_length != 0);
11215 	necp_unlock_socket_attributes();
11216 	return has_signature;
11217 }
11218 
11219 bool
necp_socket_resolver_signature_matches_address(struct inpcb * inp,union necp_sockaddr_union * address)11220 necp_socket_resolver_signature_matches_address(struct inpcb *inp, union necp_sockaddr_union *address)
11221 {
11222 	bool matches_address = false;
11223 	necp_lock_socket_attributes();
11224 	if (inp->inp_resolver_signature != NULL && inp->inp_resolver_signature_length > 0 && address->sa.sa_len > 0) {
11225 		struct necp_client_validatable *validatable = (struct necp_client_validatable *)inp->inp_resolver_signature;
11226 		if (inp->inp_resolver_signature_length > sizeof(struct necp_client_validatable) &&
11227 		    validatable->signable.sign_type == NECP_CLIENT_SIGN_TYPE_SYSTEM_RESOLVER_ANSWER) {
11228 			size_t data_length = inp->inp_resolver_signature_length - sizeof(struct necp_client_validatable);
11229 			if (data_length >= (sizeof(struct necp_client_host_resolver_answer) - sizeof(struct necp_client_signable))) {
11230 				struct necp_client_host_resolver_answer * __single answer_struct = (struct necp_client_host_resolver_answer *)&validatable->signable;
11231 				struct sockaddr_in6 sin6 = answer_struct->address_answer.sin6;
11232 				if (data_length == (sizeof(struct necp_client_host_resolver_answer) + answer_struct->hostname_length - sizeof(struct necp_client_signable)) &&
11233 				    answer_struct->address_answer.sa.sa_family == address->sa.sa_family &&
11234 				    answer_struct->address_answer.sa.sa_len == address->sa.sa_len &&
11235 				    (answer_struct->address_answer.sin.sin_port == 0 ||
11236 				    answer_struct->address_answer.sin.sin_port == address->sin.sin_port) &&
11237 				    ((answer_struct->address_answer.sa.sa_family == AF_INET &&
11238 				    answer_struct->address_answer.sin.sin_addr.s_addr == address->sin.sin_addr.s_addr) ||
11239 				    (answer_struct->address_answer.sa.sa_family == AF_INET6 &&
11240 				    memcmp(&sin6.sin6_addr, &address->sin6.sin6_addr, sizeof(struct in6_addr)) == 0))) {
11241 					// Address matches
11242 					const bool validated = necp_validate_resolver_answer(validatable->signable.client_id,
11243 					    validatable->signable.sign_type,
11244 					    signable_get_data(&validatable->signable, data_length), data_length,
11245 					    validatable->signature.signed_tag, sizeof(validatable->signature.signed_tag));
11246 					if (validated) {
11247 						// Answer is validated
11248 						matches_address = true;
11249 					}
11250 				}
11251 			}
11252 		}
11253 	}
11254 	necp_unlock_socket_attributes();
11255 	return matches_address;
11256 }
11257 
11258 /*
11259  * necp_set_socket_domain_attributes
11260  * Called from soconnectlock/soconnectxlock to directly set the tracker domain and owner for
11261  * a newly marked tracker socket.
11262  */
11263 errno_t
necp_set_socket_domain_attributes(struct socket * so,const char * domain __null_terminated,const char * domain_owner __null_terminated)11264 necp_set_socket_domain_attributes(struct socket *so, const char *domain __null_terminated, const char *domain_owner __null_terminated)
11265 {
11266 	int error = 0;
11267 	struct inpcb * __single inp = NULL;
11268 	size_t valsize = 0;
11269 	size_t buffer_size = 0;
11270 	u_int8_t * __sized_by(buffer_size) buffer = NULL;
11271 	char * __indexable buffer_to_free = NULL;
11272 
11273 	if (SOCK_DOM(so) != PF_INET && SOCK_DOM(so) != PF_INET6) {
11274 		error = EINVAL;
11275 		goto fail;
11276 	}
11277 
11278 	// Set domain (required)
11279 
11280 	valsize = strlen(domain);
11281 	if (valsize == 0 || valsize > NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) {
11282 		error = EINVAL;
11283 		goto fail;
11284 	}
11285 
11286 	buffer = (u_int8_t *)kalloc_data(valsize + 1, Z_WAITOK | Z_ZERO);
11287 	buffer_size = valsize + 1;
11288 	if (buffer == NULL) {
11289 		error = ENOMEM;
11290 		goto fail;
11291 	}
11292 	strlcpy((char *)buffer, domain, buffer_size);
11293 	buffer[valsize] = 0;
11294 
11295 	inp = sotoinpcb(so);
11296 	// Do not overwrite a previously set domain if tracker domain is different.
11297 	if (inp->inp_necp_attributes.inp_domain != NULL) {
11298 		if (strlen(inp->inp_necp_attributes.inp_domain) != strlen(domain) ||
11299 		    strcmp(inp->inp_necp_attributes.inp_domain, domain) != 0) {
11300 			buffer_to_free = (inp->inp_necp_attributes.inp_tracker_domain != NULL) ? __unsafe_null_terminated_to_indexable(inp->inp_necp_attributes.inp_tracker_domain) : NULL;
11301 			// Protect switching of buffer pointer
11302 			necp_lock_socket_attributes();
11303 			inp->inp_necp_attributes.inp_tracker_domain = __unsafe_null_terminated_from_indexable((char *)buffer, (char *)&buffer[valsize]);
11304 			necp_unlock_socket_attributes();
11305 			if (buffer_to_free != NULL) {
11306 				kfree_data_addr(buffer_to_free);
11307 			}
11308 		} else {
11309 			kfree_data_sized_by(buffer, buffer_size);
11310 		}
11311 	} else {
11312 		// Protect switching of buffer pointer
11313 		necp_lock_socket_attributes();
11314 		inp->inp_necp_attributes.inp_domain = __unsafe_null_terminated_from_indexable((char *)buffer, (char *)&buffer[valsize]);
11315 		necp_unlock_socket_attributes();
11316 	}
11317 	buffer = NULL;
11318 	buffer_size = 0;
11319 
11320 	// set domain_owner (required only for tracker)
11321 	if (!(so->so_flags1 & SOF1_KNOWN_TRACKER)) {
11322 		goto done;
11323 	}
11324 
11325 	valsize = strlen(domain_owner);
11326 	if (valsize == 0 || valsize > NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) {
11327 		error = EINVAL;
11328 		goto fail;
11329 	}
11330 
11331 	buffer = (u_int8_t *)kalloc_data(valsize + 1, Z_WAITOK | Z_ZERO);
11332 	buffer_size = valsize + 1;
11333 	if (buffer == NULL) {
11334 		error = ENOMEM;
11335 		goto fail;
11336 	}
11337 	strlcpy((char *)buffer, domain_owner, buffer_size);
11338 	buffer[valsize] = 0;
11339 
11340 	inp = sotoinpcb(so);
11341 
11342 	buffer_to_free = (inp->inp_necp_attributes.inp_domain_owner != NULL) ? __unsafe_null_terminated_to_indexable(inp->inp_necp_attributes.inp_domain_owner) : NULL;
11343 	// Protect switching of buffer pointer
11344 	necp_lock_socket_attributes();
11345 	inp->inp_necp_attributes.inp_domain_owner = __unsafe_null_terminated_from_indexable((char *)buffer, (char *)&buffer[valsize]);
11346 	necp_unlock_socket_attributes();
11347 	buffer = NULL;
11348 	buffer_size = 0;
11349 
11350 	if (buffer_to_free != NULL) {
11351 		kfree_data_addr(buffer_to_free);
11352 	}
11353 
11354 done:
11355 	NECP_SOCKET_PARAMS_LOG(so, "NECP ATTRIBUTES SOCKET - domain <%s> owner <%s> context <%s> tracker domain <%s> account <%s> "
11356 	    "<so flags - is_tracker %X non-app-initiated %X app-approved-domain %X",
11357 	    inp->inp_necp_attributes.inp_domain,
11358 	    inp->inp_necp_attributes.inp_domain_owner,
11359 	    inp->inp_necp_attributes.inp_domain_context,
11360 	    inp->inp_necp_attributes.inp_tracker_domain,
11361 	    inp->inp_necp_attributes.inp_account,
11362 	    so->so_flags1 & SOF1_KNOWN_TRACKER,
11363 	    so->so_flags1 & SOF1_TRACKER_NON_APP_INITIATED,
11364 	    so->so_flags1 & SOF1_APPROVED_APP_DOMAIN);
11365 
11366 	if (necp_debug) {
11367 		NECPLOG(LOG_DEBUG, "Set on socket: Domain <%s> Domain owner <%s> Domain context <%s> Tracker domain <%s> Account <%s> ",
11368 		    inp->inp_necp_attributes.inp_domain,
11369 		    inp->inp_necp_attributes.inp_domain_owner,
11370 		    inp->inp_necp_attributes.inp_domain_context,
11371 		    inp->inp_necp_attributes.inp_tracker_domain,
11372 		    inp->inp_necp_attributes.inp_account);
11373 	}
11374 fail:
11375 	if (buffer != NULL) {
11376 		kfree_data_sized_by(buffer, buffer_size);
11377 	}
11378 	return error;
11379 }
11380 
11381 void *
11382 __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)11383 necp_create_nexus_assign_message(uuid_t nexus_instance, nexus_port_t nexus_port, void * __sized_by(key_length) key, uint32_t key_length,
11384     struct necp_client_endpoint *local_endpoint, struct necp_client_endpoint *remote_endpoint, struct ether_addr *local_ether_addr,
11385     u_int32_t flow_adv_index, void *flow_stats, size_t *message_length)
11386 {
11387 	u_int8_t * __indexable buffer = NULL;
11388 	u_int8_t * __indexable cursor = NULL;
11389 	size_t valsize = 0;
11390 	bool has_nexus_assignment = FALSE;
11391 
11392 	if (!uuid_is_null(nexus_instance)) {
11393 		has_nexus_assignment = TRUE;
11394 		valsize += sizeof(struct necp_tlv_header) + sizeof(uuid_t);
11395 		valsize += sizeof(struct necp_tlv_header) + sizeof(nexus_port_t);
11396 	}
11397 	if (flow_adv_index != NECP_FLOWADV_IDX_INVALID) {
11398 		valsize += sizeof(struct necp_tlv_header) + sizeof(u_int32_t);
11399 	}
11400 	if (key != NULL && key_length > 0) {
11401 		valsize += sizeof(struct necp_tlv_header) + key_length;
11402 	}
11403 	if (local_endpoint != NULL) {
11404 		valsize += sizeof(struct necp_tlv_header) + sizeof(struct necp_client_endpoint);
11405 	}
11406 	if (remote_endpoint != NULL) {
11407 		valsize += sizeof(struct necp_tlv_header) + sizeof(struct necp_client_endpoint);
11408 	}
11409 	if (local_ether_addr != NULL) {
11410 		valsize += sizeof(struct necp_tlv_header) + sizeof(struct ether_addr);
11411 	}
11412 	if (flow_stats != NULL) {
11413 		valsize += sizeof(struct necp_tlv_header) + sizeof(void *);
11414 	}
11415 	if (valsize == 0) {
11416 		*message_length = 0;
11417 		return NULL;
11418 	}
11419 
11420 	buffer = kalloc_data(valsize, Z_WAITOK | Z_ZERO);
11421 	if (buffer == NULL) {
11422 		*message_length = 0;
11423 		return NULL;
11424 	}
11425 
11426 	cursor = buffer;
11427 	if (has_nexus_assignment) {
11428 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_INSTANCE, sizeof(uuid_t), nexus_instance, buffer, valsize);
11429 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_PORT, sizeof(nexus_port_t), &nexus_port, buffer, valsize);
11430 	}
11431 	if (flow_adv_index != NECP_FLOWADV_IDX_INVALID) {
11432 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_PORT_FLOW_INDEX, sizeof(u_int32_t), &flow_adv_index, buffer, valsize);
11433 	}
11434 	if (key != NULL && key_length > 0) {
11435 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_PARAMETER_NEXUS_KEY, key_length, key, buffer, valsize);
11436 	}
11437 	if (local_endpoint != NULL) {
11438 		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);
11439 	}
11440 	if (remote_endpoint != NULL) {
11441 		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);
11442 	}
11443 	if (local_ether_addr != NULL) {
11444 		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);
11445 	}
11446 	if (flow_stats != NULL) {
11447 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_FLOW_STATS, sizeof(void *), &flow_stats, buffer, valsize);
11448 	}
11449 
11450 	*message_length = valsize;
11451 
11452 	return buffer;
11453 }
11454 
11455 void
necp_inpcb_remove_cb(struct inpcb * inp)11456 necp_inpcb_remove_cb(struct inpcb *inp)
11457 {
11458 	if (!uuid_is_null(inp->necp_client_uuid)) {
11459 		necp_client_unregister_socket_flow(inp->necp_client_uuid, inp);
11460 		uuid_clear(inp->necp_client_uuid);
11461 	}
11462 }
11463 
11464 void
necp_inpcb_dispose(struct inpcb * inp)11465 necp_inpcb_dispose(struct inpcb *inp)
11466 {
11467 	char * __indexable buffer = NULL;
11468 
11469 	necp_inpcb_remove_cb(inp); // Clear out socket registrations if not yet done
11470 	if (inp->inp_necp_attributes.inp_domain != NULL) {
11471 		buffer = __unsafe_null_terminated_to_indexable(inp->inp_necp_attributes.inp_domain);
11472 		kfree_data_addr(buffer);
11473 		inp->inp_necp_attributes.inp_domain = NULL;
11474 	}
11475 	if (inp->inp_necp_attributes.inp_account != NULL) {
11476 		buffer = __unsafe_null_terminated_to_indexable(inp->inp_necp_attributes.inp_account);
11477 		kfree_data_addr(buffer);
11478 		inp->inp_necp_attributes.inp_account = NULL;
11479 	}
11480 	if (inp->inp_necp_attributes.inp_domain_owner != NULL) {
11481 		buffer = __unsafe_null_terminated_to_indexable(inp->inp_necp_attributes.inp_domain_owner);
11482 		kfree_data_addr(buffer);
11483 		inp->inp_necp_attributes.inp_domain_owner = NULL;
11484 	}
11485 	if (inp->inp_necp_attributes.inp_domain_context != NULL) {
11486 		buffer = __unsafe_null_terminated_to_indexable(inp->inp_necp_attributes.inp_domain_context);
11487 		kfree_data_addr(buffer);
11488 		inp->inp_necp_attributes.inp_domain_context = NULL;
11489 	}
11490 	if (inp->inp_necp_attributes.inp_tracker_domain != NULL) {
11491 		buffer = __unsafe_null_terminated_to_indexable(inp->inp_necp_attributes.inp_tracker_domain);
11492 		kfree_data_addr(buffer);
11493 		inp->inp_necp_attributes.inp_tracker_domain = NULL;
11494 	}
11495 	if (inp->inp_resolver_signature != NULL) {
11496 		kfree_data_sized_by(inp->inp_resolver_signature, inp->inp_resolver_signature_length);
11497 	}
11498 }
11499 
11500 void
necp_mppcb_dispose(struct mppcb * mpp)11501 necp_mppcb_dispose(struct mppcb *mpp)
11502 {
11503 	char * __indexable buffer = NULL;
11504 
11505 	if (!uuid_is_null(mpp->necp_client_uuid)) {
11506 		necp_client_unregister_multipath_cb(mpp->necp_client_uuid, mpp);
11507 		uuid_clear(mpp->necp_client_uuid);
11508 	}
11509 
11510 	if (mpp->inp_necp_attributes.inp_domain != NULL) {
11511 		buffer = __unsafe_null_terminated_to_indexable(mpp->inp_necp_attributes.inp_domain);
11512 		kfree_data_addr(buffer);
11513 		mpp->inp_necp_attributes.inp_domain = NULL;
11514 	}
11515 	if (mpp->inp_necp_attributes.inp_account != NULL) {
11516 		buffer = __unsafe_null_terminated_to_indexable(mpp->inp_necp_attributes.inp_account);
11517 		kfree_data_addr(buffer);
11518 		mpp->inp_necp_attributes.inp_account = NULL;
11519 	}
11520 	if (mpp->inp_necp_attributes.inp_domain_owner != NULL) {
11521 		buffer = __unsafe_null_terminated_to_indexable(mpp->inp_necp_attributes.inp_domain_owner);
11522 		kfree_data_addr(buffer);
11523 		mpp->inp_necp_attributes.inp_domain_owner = NULL;
11524 	}
11525 	if (mpp->inp_necp_attributes.inp_tracker_domain != NULL) {
11526 		buffer = __unsafe_null_terminated_to_indexable(mpp->inp_necp_attributes.inp_tracker_domain);
11527 		kfree_data_addr(buffer);
11528 		mpp->inp_necp_attributes.inp_tracker_domain = NULL;
11529 	}
11530 	if (mpp->inp_necp_attributes.inp_domain_context != NULL) {
11531 		buffer = __unsafe_null_terminated_to_indexable(mpp->inp_necp_attributes.inp_domain_context);
11532 		kfree_data_addr(buffer);
11533 		mpp->inp_necp_attributes.inp_domain_context = NULL;
11534 	}
11535 }
11536 
11537 /// Module init
11538 
11539 void
necp_client_init(void)11540 necp_client_init(void)
11541 {
11542 	necp_client_update_tcall = thread_call_allocate_with_options(necp_update_all_clients_callout, NULL,
11543 	    THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
11544 	VERIFY(necp_client_update_tcall != NULL);
11545 #if SKYWALK
11546 
11547 	necp_client_collect_stats_tcall = thread_call_allocate_with_options(necp_collect_stats_client_callout, NULL,
11548 	    THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
11549 	VERIFY(necp_client_collect_stats_tcall != NULL);
11550 
11551 	necp_close_empty_arenas_tcall = thread_call_allocate_with_options(necp_close_empty_arenas_callout, NULL,
11552 	    THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
11553 	VERIFY(necp_close_empty_arenas_tcall != NULL);
11554 #endif /* SKYWALK */
11555 
11556 	LIST_INIT(&necp_fd_list);
11557 	LIST_INIT(&necp_fd_observer_list);
11558 	LIST_INIT(&necp_collect_stats_flow_list);
11559 
11560 	RB_INIT(&necp_client_global_tree);
11561 	RB_INIT(&necp_client_flow_global_tree);
11562 }
11563 
11564 #if SKYWALK
11565 pid_t
necp_client_get_proc_pid_from_arena_info(struct skmem_arena_mmap_info * arena_info)11566 necp_client_get_proc_pid_from_arena_info(struct skmem_arena_mmap_info *arena_info)
11567 {
11568 	ASSERT((arena_info->ami_arena->ar_type == SKMEM_ARENA_TYPE_NECP) || (arena_info->ami_arena->ar_type == SKMEM_ARENA_TYPE_SYSTEM));
11569 
11570 	if (arena_info->ami_arena->ar_type == SKMEM_ARENA_TYPE_NECP) {
11571 		struct necp_arena_info * __single nai = __unsafe_forge_single(struct necp_arena_info *, container_of(arena_info, struct necp_arena_info, nai_mmap));
11572 		return nai->nai_proc_pid;
11573 	} else {
11574 		struct necp_fd_data * __single fd_data = __unsafe_forge_single(struct necp_fd_data *, container_of(arena_info, struct necp_fd_data, sysctl_mmap));
11575 		return fd_data->proc_pid;
11576 	}
11577 }
11578 #endif /* !SKYWALK */
11579