xref: /xnu-11215.41.3/bsd/net/necp_client.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
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_counted_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 	if (ifflagsp) {
6532 		*ifflagsp = route_ifflags | nstat_diagnostic_flags;
6533 		if (tcpstats->necp_tcp_extra.flags1 & SOF1_CELLFALLBACK) {
6534 			*ifflagsp |= NSTAT_IFNET_VIA_CELLFALLBACK;
6535 		}
6536 		if ((digestp == NULL) && (countsp == NULL) && (metadatap == NULL)) {
6537 			return true;
6538 		}
6539 	}
6540 
6541 	if (digestp) {
6542 		// The digest is intended to give information that may help give insight into the state of the link
6543 		// while avoiding the need to do the relatively expensive flowswitch lookup
6544 		digestp->rxbytes = tcpstats->necp_tcp_counts.necp_stat_rxbytes;
6545 		digestp->txbytes = tcpstats->necp_tcp_counts.necp_stat_txbytes;
6546 		digestp->rxduplicatebytes = tcpstats->necp_tcp_counts.necp_stat_rxduplicatebytes;
6547 		digestp->rxoutoforderbytes = tcpstats->necp_tcp_counts.necp_stat_rxoutoforderbytes;
6548 		digestp->txretransmit = tcpstats->necp_tcp_counts.necp_stat_txretransmit;
6549 		digestp->ifindex = route_ifindex;
6550 		digestp->state = tcpstats->necp_tcp_extra.state;
6551 		digestp->txunacked = tcpstats->necp_tcp_extra.txunacked;
6552 		digestp->txwindow = tcpstats->necp_tcp_extra.txwindow;
6553 		digestp->connstatus.probe_activated    = tcpstats->necp_tcp_extra.probestatus.probe_activated;
6554 		digestp->connstatus.write_probe_failed = tcpstats->necp_tcp_extra.probestatus.write_probe_failed;
6555 		digestp->connstatus.read_probe_failed  = tcpstats->necp_tcp_extra.probestatus.read_probe_failed;
6556 		digestp->connstatus.conn_probe_failed  = tcpstats->necp_tcp_extra.probestatus.conn_probe_failed;
6557 
6558 		if ((countsp == NULL) && (metadatap == NULL)) {
6559 			return true;
6560 		}
6561 	}
6562 
6563 	const struct sk_stats_flow *sf = &flow_registration->nexus_stats->fs_stats;
6564 	if (sf == NULL) {
6565 		nstat_diagnostic_flags |= NSTAT_IFNET_FLOWSWITCH_VALUE_UNOBTAINABLE;
6566 		char namebuf[MAXCOMLEN + 1];
6567 		(void) strlcpy(namebuf, "unknown", sizeof(namebuf));
6568 		proc_name(client->proc_pid, namebuf, sizeof(namebuf));
6569 		NECPLOG(LOG_ERR, "req tcp stats, necp_client flow_registration flow_stats missing for pid %d %s curproc %d %s\n",
6570 		    client->proc_pid, namebuf, proc_pid(current_proc()), proc_best_name(current_proc()));
6571 		sf = &ntstat_sk_stats_zero;
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 	if (ifflagsp) {
6708 		*ifflagsp = route_ifflags | nstat_diagnostic_flags;
6709 		if ((countsp == NULL) && (metadatap == NULL)) {
6710 			return true;
6711 		}
6712 	}
6713 	const struct sk_stats_flow *sf = &flow_registration->nexus_stats->fs_stats;
6714 	if (sf == NULL) {
6715 		nstat_diagnostic_flags |= NSTAT_IFNET_FLOWSWITCH_VALUE_UNOBTAINABLE;
6716 		char namebuf[MAXCOMLEN + 1];
6717 		(void) strlcpy(namebuf, "unknown", sizeof(namebuf));
6718 		proc_name(client->proc_pid, namebuf, sizeof(namebuf));
6719 		NECPLOG(LOG_ERR, "req udp stats, necp_client flow_registration flow_stats missing for pid %d %s curproc %d %s\n",
6720 		    client->proc_pid, namebuf, proc_pid(current_proc()), proc_best_name(current_proc()));
6721 		sf = &ntstat_sk_stats_zero;
6722 	}
6723 
6724 	if (countsp) {
6725 		countsp->nstat_rxbytes = udpstats->necp_udp_counts.necp_stat_rxbytes;
6726 		countsp->nstat_txbytes = udpstats->necp_udp_counts.necp_stat_txbytes;
6727 
6728 		countsp->nstat_rxduplicatebytes = udpstats->necp_udp_counts.necp_stat_rxduplicatebytes;
6729 		countsp->nstat_rxoutoforderbytes = udpstats->necp_udp_counts.necp_stat_rxoutoforderbytes;
6730 		countsp->nstat_txretransmit = udpstats->necp_udp_counts.necp_stat_txretransmit;
6731 
6732 		countsp->nstat_min_rtt = udpstats->necp_udp_counts.necp_stat_min_rtt;
6733 		countsp->nstat_avg_rtt = udpstats->necp_udp_counts.necp_stat_avg_rtt;
6734 		countsp->nstat_var_rtt = udpstats->necp_udp_counts.necp_stat_var_rtt;
6735 
6736 		// Supplement what the user level has told us with what we know from the flowswitch
6737 		countsp->nstat_rxpackets = sf->sf_ipackets;
6738 		countsp->nstat_txpackets = sf->sf_opackets;
6739 		if (route_ifflags & NSTAT_IFNET_IS_CELLULAR) {
6740 			countsp->nstat_cell_rxbytes = sf->sf_ibytes;
6741 			countsp->nstat_cell_txbytes = sf->sf_obytes;
6742 		} else if (route_ifflags & NSTAT_IFNET_IS_WIFI) {
6743 			countsp->nstat_wifi_rxbytes = sf->sf_ibytes;
6744 			countsp->nstat_wifi_txbytes = sf->sf_obytes;
6745 		} else if (route_ifflags & NSTAT_IFNET_IS_WIRED) {
6746 			countsp->nstat_wired_rxbytes = sf->sf_ibytes;
6747 			countsp->nstat_wired_txbytes = sf->sf_obytes;
6748 		}
6749 	}
6750 
6751 	if (metadatap) {
6752 		nstat_udp_descriptor *desc = (nstat_udp_descriptor *)metadatap;
6753 		memset(desc, 0, sizeof(*desc));
6754 
6755 		// Metadata from the flow registration
6756 		uuid_copy(desc->fuuid, flow_registration->registration_id);
6757 
6758 		// Metadata that the necp client should have in TLV format.
6759 		pid_t effective_pid = client->proc_pid;
6760 		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);
6761 		desc->epid = (u_int32_t)effective_pid;
6762 
6763 		// Metadata from the flow registration
6764 		// This needs to revisited if multiple flows are created from one flow registration
6765 		struct necp_client_flow *flow = NULL;
6766 		LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
6767 			memcpy(&desc->local, &flow->local_addr, sizeof(desc->local));
6768 			break;
6769 		}
6770 
6771 		// Metadata from the route
6772 		desc->ifindex = route_ifindex;
6773 		desc->ifnet_properties = route_ifflags | nstat_diagnostic_flags;
6774 		desc->ifnet_properties |= (sf->sf_flags & SFLOWF_ONLINK) ? NSTAT_IFNET_IS_LOCAL : NSTAT_IFNET_IS_NON_LOCAL;
6775 
6776 		// Basic metadata is all that is required for UDP
6777 		desc->rcvbufsize = udpstats->necp_udp_basic.rcvbufsize;
6778 		desc->rcvbufused = udpstats->necp_udp_basic.rcvbufused;
6779 
6780 		memcpy(&desc->activity_bitmap, &sf->sf_activity, sizeof(sf->sf_activity));
6781 
6782 		if (NECP_ENABLE_CLIENT_TRACE(NECP_CLIENT_TRACE_LEVEL_FLOW)) {
6783 			uuid_string_t euuid_str = { 0 };
6784 			uuid_unparse(desc->euuid, euuid_str);
6785 			NECPLOG(LOG_NOTICE, "Collected stats - UDP - epid %d uid %d euuid %s persona id %d", desc->epid, desc->uid, euuid_str, desc->persona_id);
6786 		}
6787 	}
6788 
6789 	return true;
6790 }
6791 
6792 // Called from NetworkStatistics when it wishes to collect latest information for a QUIC flow.
6793 //
6794 // TODO: For now it is an exact implementation as that of TCP.
6795 // Still to keep the logic separate for future divergence, keeping the routines separate.
6796 // It also seems there are lots of common code between existing implementations and
6797 // it would be good to refactor this logic at some point.
6798 static bool
necp_request_quic_netstats(userland_stats_provider_context * ctx,u_int32_t * ifflagsp,nstat_progress_digest * digestp,nstat_counts * countsp,void * metadatap)6799 necp_request_quic_netstats(userland_stats_provider_context *ctx,
6800     u_int32_t *ifflagsp,
6801     nstat_progress_digest *digestp,
6802     nstat_counts *countsp,
6803     void *metadatap)
6804 {
6805 	if (ctx == NULL) {
6806 		return false;
6807 	}
6808 
6809 	struct necp_client_flow_registration * __single flow_registration = (struct necp_client_flow_registration *)(void *)ctx;
6810 	struct necp_client *client = flow_registration->client;
6811 	struct necp_all_stats *ustats_kaddr = ((struct necp_all_kstats *)flow_registration->kstats_kaddr)->necp_stats_ustats;
6812 	struct necp_quic_stats *quicstats = (struct necp_quic_stats *)ustats_kaddr;
6813 	ASSERT(quicstats != NULL);
6814 
6815 	u_int32_t nstat_diagnostic_flags = 0;
6816 
6817 	// Retrieve details from the last time the assigned flows were updated
6818 	u_int32_t route_ifindex = IFSCOPE_NONE;
6819 	u_int32_t route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6820 	u_int64_t combined_interface_details = 0;
6821 
6822 	combined_interface_details = os_atomic_load(&flow_registration->last_interface_details, relaxed);
6823 	split_interface_details(combined_interface_details, &route_ifindex, &route_ifflags);
6824 
6825 	if (route_ifindex == IFSCOPE_NONE) {
6826 		// Mark no interface
6827 		nstat_diagnostic_flags |= NSTAT_IFNET_ROUTE_VALUE_UNOBTAINABLE;
6828 		route_ifflags = NSTAT_IFNET_IS_UNKNOWN_TYPE;
6829 		NECPLOG(LOG_INFO, "req quic stats, failed to get route details for pid %d curproc %d %s\n",
6830 		    client->proc_pid, proc_pid(current_proc()), proc_best_name(current_proc()));
6831 	}
6832 
6833 	if (ifflagsp) {
6834 		*ifflagsp = route_ifflags | nstat_diagnostic_flags;
6835 		if ((digestp == NULL) && (countsp == NULL) && (metadatap == NULL)) {
6836 			return true;
6837 		}
6838 	}
6839 
6840 	if (digestp) {
6841 		// The digest is intended to give information that may help give insight into the state of the link
6842 		// while avoiding the need to do the relatively expensive flowswitch lookup
6843 		digestp->rxbytes = quicstats->necp_quic_counts.necp_stat_rxbytes;
6844 		digestp->txbytes = quicstats->necp_quic_counts.necp_stat_txbytes;
6845 		digestp->rxduplicatebytes = quicstats->necp_quic_counts.necp_stat_rxduplicatebytes;
6846 		digestp->rxoutoforderbytes = quicstats->necp_quic_counts.necp_stat_rxoutoforderbytes;
6847 		digestp->txretransmit = quicstats->necp_quic_counts.necp_stat_txretransmit;
6848 		digestp->ifindex = route_ifindex;
6849 		digestp->state = quicstats->necp_quic_extra.state;
6850 		digestp->txunacked = quicstats->necp_quic_extra.txunacked;
6851 		digestp->txwindow = quicstats->necp_quic_extra.txwindow;
6852 		digestp->connstatus.probe_activated    = quicstats->necp_quic_extra.probestatus.probe_activated;
6853 		digestp->connstatus.write_probe_failed = quicstats->necp_quic_extra.probestatus.write_probe_failed;
6854 		digestp->connstatus.read_probe_failed  = quicstats->necp_quic_extra.probestatus.read_probe_failed;
6855 		digestp->connstatus.conn_probe_failed  = quicstats->necp_quic_extra.probestatus.conn_probe_failed;
6856 
6857 		if ((countsp == NULL) && (metadatap == NULL)) {
6858 			return true;
6859 		}
6860 	}
6861 
6862 	const struct sk_stats_flow *sf = &flow_registration->nexus_stats->fs_stats;
6863 	if (sf == NULL) {
6864 		nstat_diagnostic_flags |= NSTAT_IFNET_FLOWSWITCH_VALUE_UNOBTAINABLE;
6865 		char namebuf[MAXCOMLEN + 1];
6866 		(void) strlcpy(namebuf, "unknown", sizeof(namebuf));
6867 		proc_name(client->proc_pid, namebuf, sizeof(namebuf));
6868 		NECPLOG(LOG_ERR, "req quic stats, necp_client flow_registration flow_stats missing for pid %d %s curproc %d %s\n",
6869 		    client->proc_pid, namebuf, proc_pid(current_proc()), proc_best_name(current_proc()));
6870 		sf = &ntstat_sk_stats_zero;
6871 	}
6872 
6873 	if (countsp) {
6874 		countsp->nstat_rxbytes = quicstats->necp_quic_counts.necp_stat_rxbytes;
6875 		countsp->nstat_txbytes = quicstats->necp_quic_counts.necp_stat_txbytes;
6876 
6877 		countsp->nstat_rxduplicatebytes = quicstats->necp_quic_counts.necp_stat_rxduplicatebytes;
6878 		countsp->nstat_rxoutoforderbytes = quicstats->necp_quic_counts.necp_stat_rxoutoforderbytes;
6879 		countsp->nstat_txretransmit = quicstats->necp_quic_counts.necp_stat_txretransmit;
6880 
6881 		countsp->nstat_min_rtt = quicstats->necp_quic_counts.necp_stat_min_rtt;
6882 		countsp->nstat_avg_rtt = quicstats->necp_quic_counts.necp_stat_avg_rtt;
6883 		countsp->nstat_var_rtt = quicstats->necp_quic_counts.necp_stat_var_rtt;
6884 
6885 		// TODO: It would be good to expose QUIC stats for CH/SH retransmission and connection state
6886 		// Supplement what the user level has told us with what we know from the flowswitch
6887 		countsp->nstat_rxpackets = sf->sf_ipackets;
6888 		countsp->nstat_txpackets = sf->sf_opackets;
6889 		if (route_ifflags & NSTAT_IFNET_IS_CELLULAR) {
6890 			countsp->nstat_cell_rxbytes = sf->sf_ibytes;
6891 			countsp->nstat_cell_txbytes = sf->sf_obytes;
6892 		} else if (route_ifflags & NSTAT_IFNET_IS_WIFI) {
6893 			countsp->nstat_wifi_rxbytes = sf->sf_ibytes;
6894 			countsp->nstat_wifi_txbytes = sf->sf_obytes;
6895 		} else if (route_ifflags & NSTAT_IFNET_IS_WIRED) {
6896 			countsp->nstat_wired_rxbytes = sf->sf_ibytes;
6897 			countsp->nstat_wired_txbytes = sf->sf_obytes;
6898 		}
6899 	}
6900 
6901 	if (metadatap) {
6902 		nstat_quic_descriptor *desc = (nstat_quic_descriptor *)metadatap;
6903 		memset(desc, 0, sizeof(*desc));
6904 
6905 		// Metadata from the flow registration
6906 		uuid_copy(desc->fuuid, flow_registration->registration_id);
6907 
6908 		// Metadata, that the necp client should have, in TLV format.
6909 		pid_t effective_pid = client->proc_pid;
6910 		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);
6911 		desc->epid = (u_int32_t)effective_pid;
6912 
6913 		// Metadata from the flow registration
6914 		// This needs to revisited if multiple flows are created from one flow registration
6915 		struct necp_client_flow *flow = NULL;
6916 		LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
6917 			memcpy(&desc->local, &flow->local_addr, sizeof(desc->local));
6918 			break;
6919 		}
6920 
6921 		// Metadata from the route
6922 		desc->ifindex = route_ifindex;
6923 		desc->ifnet_properties = route_ifflags | nstat_diagnostic_flags;
6924 		desc->ifnet_properties |= (sf->sf_flags & SFLOWF_ONLINK) ? NSTAT_IFNET_IS_LOCAL : NSTAT_IFNET_IS_NON_LOCAL;
6925 
6926 		// Basic metadata from userland
6927 		desc->rcvbufsize = quicstats->necp_quic_basic.rcvbufsize;
6928 		desc->rcvbufused = quicstats->necp_quic_basic.rcvbufused;
6929 
6930 		// Additional QUIC specific data
6931 		desc->sndbufsize = quicstats->necp_quic_extra.sndbufsize;
6932 		desc->sndbufused = quicstats->necp_quic_extra.sndbufused;
6933 		desc->txunacked = quicstats->necp_quic_extra.txunacked;
6934 		desc->txwindow = quicstats->necp_quic_extra.txwindow;
6935 		desc->txcwindow = quicstats->necp_quic_extra.txcwindow;
6936 		desc->traffic_mgt_flags = quicstats->necp_quic_extra.traffic_mgt_flags;
6937 		desc->state = quicstats->necp_quic_extra.state;
6938 
6939 		// TODO: CC algo defines should be named agnostic of the protocol
6940 		u_int32_t cc_alg_index = quicstats->necp_quic_extra.cc_alg_index;
6941 		if (cc_alg_index < TCP_CC_ALGO_COUNT) {
6942 			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));
6943 		} else {
6944 			strlcpy(desc->cc_algo, "unknown", sizeof(desc->cc_algo));
6945 		}
6946 
6947 		memcpy(&desc->activity_bitmap, &sf->sf_activity, sizeof(sf->sf_activity));
6948 
6949 		desc->connstatus.probe_activated        = quicstats->necp_quic_extra.probestatus.probe_activated;
6950 		desc->connstatus.write_probe_failed     = quicstats->necp_quic_extra.probestatus.write_probe_failed;
6951 		desc->connstatus.read_probe_failed      = quicstats->necp_quic_extra.probestatus.read_probe_failed;
6952 		desc->connstatus.conn_probe_failed      = quicstats->necp_quic_extra.probestatus.conn_probe_failed;
6953 
6954 		if (NECP_ENABLE_CLIENT_TRACE(NECP_CLIENT_TRACE_LEVEL_FLOW)) {
6955 			uuid_string_t euuid_str = { 0 };
6956 			uuid_unparse(desc->euuid, euuid_str);
6957 			NECPLOG(LOG_NOTICE, "Collected stats - QUIC - epid %d uid %d euuid %s persona id %d", desc->epid, desc->uid, euuid_str, desc->persona_id);
6958 		}
6959 	}
6960 	return true;
6961 }
6962 
6963 #endif /* SKYWALK */
6964 
6965 // Support functions for NetworkStatistics support for necp_client connections
6966 
6967 static void
necp_client_inherit_from_parent(struct necp_client * client,struct necp_client * parent)6968 necp_client_inherit_from_parent(
6969 	struct necp_client *client,
6970 	struct necp_client *parent)
6971 {
6972 	assert(client->original_parameters_source == NULL);
6973 
6974 	if (parent->original_parameters_source != NULL) {
6975 		client->original_parameters_source = parent->original_parameters_source;
6976 	} else {
6977 		client->original_parameters_source = parent;
6978 	}
6979 	necp_client_retain(client->original_parameters_source);
6980 }
6981 
6982 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)6983 necp_find_conn_netstat_data(struct necp_client *client,
6984     u_int32_t *ntstat_flags,
6985     pid_t *effective_pid,
6986     uuid_t *puuid,
6987     uid_t *uid,
6988     uuid_t *euuid,
6989     uid_t *persona_id)
6990 {
6991 	bool has_remote_address = false;
6992 	bool has_ip_protocol = false;
6993 	bool has_transport_protocol = false;
6994 	size_t offset = 0;
6995 	u_int8_t *parameters;
6996 	u_int32_t parameters_size;
6997 
6998 
6999 	parameters = client->parameters;
7000 	parameters_size = (u_int32_t)client->parameters_length;
7001 
7002 	while ((offset + sizeof(struct necp_tlv_header)) <= parameters_size) {
7003 		u_int8_t type = necp_buffer_get_tlv_type(parameters, parameters_size, offset);
7004 		u_int32_t length = necp_buffer_get_tlv_length(parameters, parameters_size, offset);
7005 
7006 		if (length > (parameters_size - (offset + sizeof(struct necp_tlv_header)))) {
7007 			// If the length is larger than what can fit in the remaining parameters size, bail
7008 			NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
7009 			break;
7010 		}
7011 
7012 		if (length > 0) {
7013 			u_int8_t * __indexable value = necp_buffer_get_tlv_value(parameters, parameters_size, offset, NULL);
7014 			if (value != NULL) {
7015 				switch (type) {
7016 				case NECP_CLIENT_PARAMETER_APPLICATION: {
7017 					if ((euuid) && (length >= sizeof(uuid_t))) {
7018 						uuid_copy(*euuid, value);
7019 					}
7020 					break;
7021 				}
7022 				case NECP_CLIENT_PARAMETER_IP_PROTOCOL: {
7023 					if (length >= 1) {
7024 						has_ip_protocol = true;
7025 					}
7026 					break;
7027 				}
7028 				case NECP_CLIENT_PARAMETER_PID: {
7029 					if ((effective_pid) && length >= sizeof(pid_t)) {
7030 						memcpy(effective_pid, value, sizeof(pid_t));
7031 					}
7032 					break;
7033 				}
7034 				case NECP_CLIENT_PARAMETER_PARENT_ID: {
7035 					if ((puuid) && (length == sizeof(uuid_t))) {
7036 						uuid_copy(*puuid, value);
7037 					}
7038 					break;
7039 				}
7040 				// It is an implementation quirk that the remote address can be found in the necp parameters
7041 				case NECP_CLIENT_PARAMETER_REMOTE_ADDRESS: {
7042 					if (length >= sizeof(struct necp_policy_condition_addr)) {
7043 						struct necp_policy_condition_addr *address_struct = (struct necp_policy_condition_addr *)(void *)value;
7044 						if (necp_client_address_is_valid(&address_struct->address.sa)) {
7045 							has_remote_address = true;
7046 						}
7047 					}
7048 					break;
7049 				}
7050 				case NECP_CLIENT_PARAMETER_TRANSPORT_PROTOCOL: {
7051 					if (length >= 1) {
7052 						has_transport_protocol = true;
7053 					}
7054 					break;
7055 				}
7056 				case NECP_CLIENT_PARAMETER_APPLICATION_ID: {
7057 					if (length >= sizeof(necp_application_id_t) && uid && persona_id) {
7058 						necp_application_id_t *application_id = (necp_application_id_t *)(void *)value;
7059 						memcpy(uid, &application_id->uid, sizeof(uid_t));
7060 						uuid_copy(*euuid, application_id->effective_uuid);
7061 						memcpy(persona_id, &application_id->persona_id, sizeof(uid_t));
7062 					}
7063 					break;
7064 				}
7065 				default: {
7066 					break;
7067 				}
7068 				}
7069 			}
7070 		}
7071 		offset += sizeof(struct necp_tlv_header) + length;
7072 	}
7073 	if (ntstat_flags) {
7074 		*ntstat_flags = (has_remote_address && has_ip_protocol && has_transport_protocol)? NSTAT_NECP_CONN_HAS_NET_ACCESS: 0;
7075 	}
7076 }
7077 
7078 static bool
necp_request_conn_netstats(nstat_provider_context ctx,u_int32_t * ifflagsp,nstat_counts * countsp,void * metadatap)7079 necp_request_conn_netstats(nstat_provider_context ctx,
7080     u_int32_t *ifflagsp,
7081     nstat_counts *countsp,
7082     void *metadatap)
7083 {
7084 	if (ctx == NULL) {
7085 		return false;
7086 	}
7087 	struct necp_client * __single client = (struct necp_client *)(void *)ctx;
7088 	nstat_connection_descriptor *desc = (nstat_connection_descriptor *)metadatap;
7089 
7090 	if (ifflagsp) {
7091 		necp_find_conn_netstat_data(client, ifflagsp, NULL, NULL, NULL, NULL, NULL);
7092 	}
7093 	if (countsp) {
7094 		memset(countsp, 0, sizeof(*countsp));
7095 	}
7096 	if (desc) {
7097 		memset(desc, 0, sizeof(*desc));
7098 		// Metadata, that the necp client should have, in TLV format.
7099 		pid_t effective_pid = client->proc_pid;
7100 		necp_find_conn_netstat_data(client, &desc->ifnet_properties, &effective_pid, &desc->puuid, &desc->uid, &desc->euuid, &desc->persona_id);
7101 		desc->epid = (u_int32_t)effective_pid;
7102 
7103 		// User level should obtain almost all connection information from an extension
7104 		// leaving little to do here
7105 		uuid_copy(desc->fuuid, client->latest_flow_registration_id);
7106 		uuid_copy(desc->cuuid, client->client_id);
7107 	}
7108 	return true;
7109 }
7110 
7111 static int
necp_skywalk_priv_check_cred(proc_t p,kauth_cred_t cred)7112 necp_skywalk_priv_check_cred(proc_t p, kauth_cred_t cred)
7113 {
7114 #pragma unused(p, cred)
7115 #if SKYWALK
7116 	/* This includes Nexus controller and Skywalk observer privs */
7117 	return skywalk_nxctl_check_privileges(p, cred);
7118 #else /* !SKYWALK */
7119 	return 0;
7120 #endif /* !SKYWALK */
7121 }
7122 
7123 /// System calls
7124 
7125 int
necp_open(struct proc * p,struct necp_open_args * uap,int * retval)7126 necp_open(struct proc *p, struct necp_open_args *uap, int *retval)
7127 {
7128 #pragma unused(retval)
7129 	int error = 0;
7130 	struct necp_fd_data * __single fd_data = NULL;
7131 	struct fileproc * __single fp = NULL;
7132 	int fd = -1;
7133 
7134 	if (uap->flags & NECP_OPEN_FLAG_OBSERVER ||
7135 	    uap->flags & NECP_OPEN_FLAG_PUSH_OBSERVER) {
7136 		if (necp_skywalk_priv_check_cred(p, kauth_cred_get()) != 0 &&
7137 		    priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_NETWORK_STATISTICS, 0) != 0) {
7138 			NECPLOG0(LOG_ERR, "Client does not hold necessary entitlement to observe other NECP clients");
7139 			error = EACCES;
7140 			goto done;
7141 		}
7142 	}
7143 
7144 #if CONFIG_MACF
7145 	error = mac_necp_check_open(p, uap->flags);
7146 	if (error) {
7147 		goto done;
7148 	}
7149 #endif /* MACF */
7150 
7151 	error = falloc(p, &fp, &fd);
7152 	if (error != 0) {
7153 		goto done;
7154 	}
7155 
7156 	fd_data = kalloc_type(struct necp_fd_data, Z_WAITOK | Z_ZERO | Z_NOFAIL);
7157 
7158 	fd_data->necp_fd_type = necp_fd_type_client;
7159 	fd_data->flags = uap->flags;
7160 	RB_INIT(&fd_data->clients);
7161 	RB_INIT(&fd_data->flows);
7162 	TAILQ_INIT(&fd_data->update_list);
7163 	lck_mtx_init(&fd_data->fd_lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
7164 	klist_init(&fd_data->si.si_note);
7165 	fd_data->proc_pid = proc_pid(p);
7166 #if SKYWALK
7167 	LIST_INIT(&fd_data->stats_arena_list);
7168 #endif /* !SKYWALK */
7169 
7170 	fp->fp_flags |= FP_CLOEXEC | FP_CLOFORK;
7171 	fp->fp_glob->fg_flag = FREAD;
7172 	fp->fp_glob->fg_ops = &necp_fd_ops;
7173 	fp_set_data(fp, fd_data);
7174 
7175 	proc_fdlock(p);
7176 
7177 	procfdtbl_releasefd(p, fd, NULL);
7178 	fp_drop(p, fd, fp, 1);
7179 
7180 	*retval = fd;
7181 
7182 	if (fd_data->flags & NECP_OPEN_FLAG_PUSH_OBSERVER) {
7183 		NECP_OBSERVER_LIST_LOCK_EXCLUSIVE();
7184 		LIST_INSERT_HEAD(&necp_fd_observer_list, fd_data, chain);
7185 		OSIncrementAtomic(&necp_observer_fd_count);
7186 		NECP_OBSERVER_LIST_UNLOCK();
7187 
7188 		// Walk all existing clients and add them
7189 		NECP_CLIENT_TREE_LOCK_SHARED();
7190 		struct necp_client *existing_client = NULL;
7191 		RB_FOREACH(existing_client, _necp_client_global_tree, &necp_client_global_tree) {
7192 			NECP_CLIENT_LOCK(existing_client);
7193 			necp_client_update_observer_add_internal(fd_data, existing_client);
7194 			necp_client_update_observer_update_internal(fd_data, existing_client);
7195 			NECP_CLIENT_UNLOCK(existing_client);
7196 		}
7197 		NECP_CLIENT_TREE_UNLOCK();
7198 	} else {
7199 		NECP_FD_LIST_LOCK_EXCLUSIVE();
7200 		LIST_INSERT_HEAD(&necp_fd_list, fd_data, chain);
7201 		OSIncrementAtomic(&necp_client_fd_count);
7202 		NECP_FD_LIST_UNLOCK();
7203 	}
7204 
7205 	proc_fdunlock(p);
7206 
7207 done:
7208 	if (error != 0) {
7209 		if (fp != NULL) {
7210 			fp_free(p, fd, fp);
7211 			fp = NULL;
7212 		}
7213 		if (fd_data != NULL) {
7214 			kfree_type(struct necp_fd_data, fd_data);
7215 		}
7216 	}
7217 
7218 	return error;
7219 }
7220 
7221 // All functions called directly from necp_client_action() to handle one of the
7222 // types should be marked with NECP_CLIENT_ACTION_FUNCTION. This ensures that
7223 // necp_client_action() does not inline all the actions into a single function.
7224 #define NECP_CLIENT_ACTION_FUNCTION __attribute__((noinline))
7225 
7226 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)7227 necp_client_add(struct proc *p, struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7228 {
7229 	int error = 0;
7230 	struct necp_client * __single client = NULL;
7231 	const size_t buffer_size = uap->buffer_size;
7232 
7233 	if (fd_data->flags & NECP_OPEN_FLAG_PUSH_OBSERVER) {
7234 		NECPLOG0(LOG_ERR, "NECP client observers with push enabled may not add their own clients");
7235 		return EINVAL;
7236 	}
7237 
7238 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
7239 	    buffer_size == 0 || buffer_size > NECP_MAX_CLIENT_PARAMETERS_SIZE || uap->buffer == 0) {
7240 		return EINVAL;
7241 	}
7242 
7243 	client = kalloc_type(struct necp_client, Z_WAITOK | Z_ZERO | Z_NOFAIL);
7244 	client->parameters = kalloc_data(buffer_size, Z_WAITOK | Z_NOFAIL);
7245 	client->parameters_length = buffer_size;
7246 	lck_mtx_init(&client->lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
7247 	lck_mtx_init(&client->route_lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
7248 
7249 	error = copyin(uap->buffer, client->parameters, buffer_size);
7250 	if (error) {
7251 		NECPLOG(LOG_ERR, "necp_client_add parameters copyin error (%d)", error);
7252 		goto done;
7253 	}
7254 
7255 	os_ref_init(&client->reference_count, &necp_client_refgrp); // Hold our reference until close
7256 
7257 	client->proc_pid = fd_data->proc_pid; // Save off proc pid in case the client will persist past fd
7258 	client->agent_handle = (void *)fd_data;
7259 	client->platform_binary = ((csproc_get_platform_binary(p) == 0) ? 0 : 1);
7260 
7261 	necp_generate_client_id(client->client_id, false);
7262 	LIST_INIT(&client->assertion_list);
7263 	RB_INIT(&client->flow_registrations);
7264 
7265 	NECP_CLIENT_LOG(client, "Adding client");
7266 
7267 	error = copyout(client->client_id, uap->client_id, sizeof(uuid_t));
7268 	if (error) {
7269 		NECPLOG(LOG_ERR, "necp_client_add client_id copyout error (%d)", error);
7270 		goto done;
7271 	}
7272 
7273 #if SKYWALK
7274 	struct necp_client_parsed_parameters parsed_parameters = {};
7275 	int parse_error = necp_client_parse_parameters(client, client->parameters, (u_int32_t)client->parameters_length, &parsed_parameters);
7276 
7277 	if (parse_error == 0 &&
7278 	    ((parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_DELEGATED_UPID) ||
7279 	    (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_ATTRIBUTED_BUNDLE_IDENTIFIER))) {
7280 		bool has_delegation_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_SOCKET_DELEGATE, 0) == 0);
7281 		if (!has_delegation_entitlement) {
7282 			if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_DELEGATED_UPID) {
7283 				NECPLOG(LOG_ERR, "%s(%d) does not hold the necessary entitlement to delegate network traffic for other processes by upid",
7284 				    proc_name_address(p), proc_pid(p));
7285 			}
7286 			if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_ATTRIBUTED_BUNDLE_IDENTIFIER) {
7287 				NECPLOG(LOG_ERR, "%s(%d) does not hold the necessary entitlement to set attributed bundle identifier",
7288 				    proc_name_address(p), proc_pid(p));
7289 			}
7290 			error = EPERM;
7291 			goto done;
7292 		}
7293 
7294 		if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_DELEGATED_UPID) {
7295 			// Save off delegated unique PID
7296 			client->delegated_upid = parsed_parameters.delegated_upid;
7297 		}
7298 	}
7299 
7300 	if (parse_error == 0 && parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_INTERPOSE) {
7301 		bool has_nexus_entitlement = (necp_skywalk_priv_check_cred(p, kauth_cred_get()) == 0);
7302 		if (!has_nexus_entitlement) {
7303 			NECPLOG(LOG_ERR, "%s(%d) does not hold the necessary entitlement to open a custom nexus client",
7304 			    proc_name_address(p), proc_pid(p));
7305 			error = EPERM;
7306 			goto done;
7307 		}
7308 	}
7309 
7310 	if (parse_error == 0 && (parsed_parameters.flags &
7311 	    (NECP_CLIENT_PARAMETER_FLAG_CUSTOM_ETHER | NECP_CLIENT_PARAMETER_FLAG_CUSTOM_IP))) {
7312 		bool has_custom_protocol_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_CUSTOM_PROTOCOL, 0) == 0);
7313 		if (!has_custom_protocol_entitlement) {
7314 			NECPLOG(LOG_ERR, "%s(%d) does not hold the necessary entitlement for custom protocol APIs",
7315 			    proc_name_address(p), proc_pid(p));
7316 			error = EPERM;
7317 			goto done;
7318 		}
7319 	}
7320 
7321 	if (parse_error == 0 && parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER &&
7322 	    (parsed_parameters.ip_protocol == IPPROTO_TCP || parsed_parameters.ip_protocol == IPPROTO_UDP)) {
7323 		uint32_t *netns_addr = NULL;
7324 		uint8_t netns_addr_len = 0;
7325 		struct ns_flow_info flow_info = {};
7326 		uint32_t netns_flags = NETNS_LISTENER;
7327 		uuid_copy(flow_info.nfi_flow_uuid, client->client_id);
7328 		flow_info.nfi_protocol = parsed_parameters.ip_protocol;
7329 		flow_info.nfi_owner_pid = client->proc_pid;
7330 		if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID) {
7331 			flow_info.nfi_effective_pid = parsed_parameters.effective_pid;
7332 		} else {
7333 			flow_info.nfi_effective_pid = flow_info.nfi_owner_pid;
7334 		}
7335 		proc_name(flow_info.nfi_owner_pid, flow_info.nfi_owner_name, MAXCOMLEN);
7336 		proc_name(flow_info.nfi_effective_pid, flow_info.nfi_effective_name, MAXCOMLEN);
7337 
7338 		if (parsed_parameters.local_addr.sa.sa_family == AF_UNSPEC) {
7339 			// Treat no local address as a wildcard IPv6
7340 			// parsed_parameters is already initialized to all zeros
7341 			parsed_parameters.local_addr.sin6.sin6_family = AF_INET6;
7342 			parsed_parameters.local_addr.sin6.sin6_len = sizeof(struct sockaddr_in6);
7343 		}
7344 
7345 		switch (parsed_parameters.local_addr.sa.sa_family) {
7346 		case AF_INET: {
7347 			memcpy(&flow_info.nfi_laddr, &parsed_parameters.local_addr.sa, parsed_parameters.local_addr.sa.sa_len);
7348 			netns_addr = (uint32_t *)&parsed_parameters.local_addr.sin.sin_addr;
7349 			netns_addr_len = 4;
7350 			break;
7351 		}
7352 		case AF_INET6: {
7353 			memcpy(&flow_info.nfi_laddr.sin6, &parsed_parameters.local_addr.sin6, parsed_parameters.local_addr.sa.sa_len);
7354 			netns_addr = (uint32_t *)&parsed_parameters.local_addr.sin6.sin6_addr;
7355 			netns_addr_len = 16;
7356 			break;
7357 		}
7358 
7359 		default: {
7360 			NECPLOG(LOG_ERR, "necp_client_add listener invalid address family (%d)", parsed_parameters.local_addr.sa.sa_family);
7361 			error = EINVAL;
7362 			goto done;
7363 		}
7364 		}
7365 		if ((parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) &&
7366 		    (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_REUSE_LOCAL)) {
7367 			netns_flags |= NETNS_REUSEPORT;
7368 		}
7369 		if (parsed_parameters.local_addr.sin.sin_port == 0) {
7370 			error = netns_reserve_ephemeral(&client->port_reservation, netns_addr, netns_addr_len, parsed_parameters.ip_protocol,
7371 			    &parsed_parameters.local_addr.sin.sin_port, netns_flags, &flow_info);
7372 			if (error) {
7373 				NECPLOG(LOG_ERR, "necp_client_add netns_reserve_ephemeral error (%d)", error);
7374 				goto done;
7375 			}
7376 
7377 			// Update the parameter TLVs with the assigned port
7378 			necp_client_update_local_port_parameters(client->parameters, (u_int32_t)client->parameters_length, parsed_parameters.local_addr.sin.sin_port);
7379 		} else {
7380 			error = netns_reserve(&client->port_reservation, netns_addr, netns_addr_len, parsed_parameters.ip_protocol,
7381 			    parsed_parameters.local_addr.sin.sin_port, netns_flags, &flow_info);
7382 			if (error) {
7383 				NECPLOG(LOG_ERR, "necp_client_add netns_reserve error (%d)", error);
7384 				goto done;
7385 			}
7386 		}
7387 	}
7388 
7389 	struct necp_client *parent = NULL;
7390 	uuid_t parent_client_id;
7391 	uuid_clear(parent_client_id);
7392 	struct necp_client_nexus_parameters parent_parameters = {};
7393 	uint16_t num_flow_regs = 0;
7394 	if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_PARENT_UUID) {
7395 		// The parent "should" be found on fd_data without having to search across the whole necp_fd_list
7396 		// It would be nice to do this a little further down where there's another instance of NECP_FD_LOCK
7397 		// but the logic here depends on the parse paramters
7398 		NECP_FD_LOCK(fd_data);
7399 		parent = necp_client_fd_find_client_unlocked(fd_data, parsed_parameters.parent_uuid);
7400 		if (parent != NULL) {
7401 			necp_client_inherit_from_parent(client, parent);
7402 			necp_client_copy_parameters_locked(client, &parent_parameters);
7403 			uuid_copy(parent_client_id, parsed_parameters.parent_uuid);
7404 			struct necp_client_flow_registration *flow_registration = NULL;
7405 			RB_FOREACH(flow_registration, _necp_client_flow_tree, &parent->flow_registrations) {
7406 				num_flow_regs++;
7407 			}
7408 		}
7409 		NECP_FD_UNLOCK(fd_data);
7410 		if (parent == NULL) {
7411 			NECPLOG0(LOG_ERR, "necp_client_add, no necp_client_inherit_from_parent as can't find parent on fd_data");
7412 		}
7413 	}
7414 	if (parse_error == 0 && parent != NULL && parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLOW_DEMUX_PATTERN) {
7415 		do {
7416 			if (parsed_parameters.demux_patterns[0].len == 0) {
7417 				NECPLOG0(LOG_INFO, "necp_client_add, child does not have a demux pattern");
7418 				break;
7419 			}
7420 
7421 			if (uuid_is_null(parent_client_id)) {
7422 				NECPLOG0(LOG_INFO, "necp_client_add, parent ID is null");
7423 				break;
7424 			}
7425 
7426 			if (num_flow_regs > 1) {
7427 				NECPLOG0(LOG_INFO, "necp_client_add, multiple parent flows not supported");
7428 				break;
7429 			}
7430 			if (parsed_parameters.ip_protocol != IPPROTO_UDP) {
7431 				NECPLOG(LOG_INFO, "necp_client_add, flow demux pattern not supported for %d protocol",
7432 				    parsed_parameters.ip_protocol);
7433 				break;
7434 			}
7435 			if (parsed_parameters.ip_protocol != parent_parameters.ip_protocol) {
7436 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child ip protocol mismatch");
7437 				break;
7438 			}
7439 			if (parsed_parameters.local_addr.sa.sa_family != AF_INET && parsed_parameters.local_addr.sa.sa_family != AF_INET6) {
7440 				NECPLOG(LOG_INFO, "necp_client_add, flow demux pattern not supported for %d family",
7441 				    parsed_parameters.local_addr.sa.sa_family);
7442 				break;
7443 			}
7444 			if (parsed_parameters.local_addr.sa.sa_family != parsed_parameters.remote_addr.sa.sa_family) {
7445 				NECPLOG0(LOG_INFO, "necp_client_add, local/remote address family mismatch");
7446 				break;
7447 			}
7448 			if (parsed_parameters.local_addr.sa.sa_family != parent_parameters.local_addr.sa.sa_family) {
7449 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child address family mismatch");
7450 				break;
7451 			}
7452 			if (SOCKADDR_CMP(&parsed_parameters.local_addr.sa, &parent_parameters.local_addr.sa, parsed_parameters.local_addr.sa.sa_len)) {
7453 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child local address mismatch");
7454 				break;
7455 			}
7456 			if (SOCKADDR_CMP(&parsed_parameters.remote_addr.sa, &parent_parameters.remote_addr.sa, parsed_parameters.remote_addr.sa.sa_len)) {
7457 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child remote address mismatch");
7458 				break;
7459 			}
7460 			if (parsed_parameters.local_addr.sin.sin_port != parent_parameters.local_addr.sin.sin_port) {
7461 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child local port mismatch");
7462 				break;
7463 			}
7464 			if (parsed_parameters.remote_addr.sin.sin_port != parent_parameters.remote_addr.sin.sin_port) {
7465 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child remote port mismatch");
7466 				break;
7467 			}
7468 			client->validated_parent = 1;
7469 			uuid_copy(client->parent_client_id, parent_client_id);
7470 		} while (false);
7471 	}
7472 
7473 #endif /* !SKYWALK */
7474 
7475 	necp_client_update_observer_add(client);
7476 
7477 	NECP_FD_LOCK(fd_data);
7478 	RB_INSERT(_necp_client_tree, &fd_data->clients, client);
7479 	OSIncrementAtomic(&necp_client_count);
7480 	NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
7481 	RB_INSERT(_necp_client_global_tree, &necp_client_global_tree, client);
7482 	NECP_CLIENT_TREE_UNLOCK();
7483 
7484 	// Prime the client result
7485 	NECP_CLIENT_LOCK(client);
7486 	(void)necp_update_client_result(current_proc(), fd_data, client, NULL);
7487 	necp_client_retain_locked(client);
7488 	NECP_CLIENT_UNLOCK(client);
7489 	NECP_FD_UNLOCK(fd_data);
7490 	// Now everything is set, it's safe to plumb this in to NetworkStatistics
7491 	uint32_t ntstat_properties = 0;
7492 	necp_find_conn_netstat_data(client, &ntstat_properties, NULL, NULL, NULL, NULL, NULL);
7493 
7494 	client->nstat_context = nstat_provider_stats_open((nstat_provider_context)client,
7495 	    NSTAT_PROVIDER_CONN_USERLAND, (u_int64_t)ntstat_properties, necp_request_conn_netstats, necp_find_conn_extension_info);
7496 	necp_client_release(client);
7497 done:
7498 	if (error != 0 && client != NULL) {
7499 		necp_client_free(client);
7500 		client = NULL;
7501 	}
7502 	*retval = error;
7503 
7504 	return error;
7505 }
7506 
7507 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)7508 necp_client_claim(struct proc *p, struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7509 {
7510 	int error = 0;
7511 	uuid_t client_id = {};
7512 	struct necp_client *client = NULL;
7513 
7514 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
7515 		error = EINVAL;
7516 		goto done;
7517 	}
7518 
7519 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
7520 	if (error) {
7521 		NECPLOG(LOG_ERR, "necp_client_claim copyin client_id error (%d)", error);
7522 		goto done;
7523 	}
7524 
7525 	if (necp_client_id_is_flow(client_id)) {
7526 		NECPLOG0(LOG_ERR, "necp_client_claim cannot claim from flow UUID");
7527 		error = EINVAL;
7528 		goto done;
7529 	}
7530 
7531 	u_int64_t upid = proc_uniqueid(p);
7532 
7533 	NECP_FD_LIST_LOCK_SHARED();
7534 
7535 	struct necp_fd_data *find_fd = NULL;
7536 	LIST_FOREACH(find_fd, &necp_fd_list, chain) {
7537 		NECP_FD_LOCK(find_fd);
7538 		struct necp_client *find_client = necp_client_fd_find_client_and_lock(find_fd, client_id);
7539 		if (find_client != NULL) {
7540 			if (find_client->delegated_upid == upid &&
7541 			    RB_EMPTY(&find_client->flow_registrations)) {
7542 				// Matched the client to claim; remove from the old fd
7543 				client = find_client;
7544 				RB_REMOVE(_necp_client_tree, &find_fd->clients, client);
7545 				necp_client_retain_locked(client);
7546 			}
7547 			NECP_CLIENT_UNLOCK(find_client);
7548 		}
7549 		NECP_FD_UNLOCK(find_fd);
7550 
7551 		if (client != NULL) {
7552 			break;
7553 		}
7554 	}
7555 
7556 	NECP_FD_LIST_UNLOCK();
7557 
7558 	if (client == NULL) {
7559 		error = ENOENT;
7560 		goto done;
7561 	}
7562 
7563 	client->proc_pid = fd_data->proc_pid; // Transfer client to claiming pid
7564 	client->agent_handle = (void *)fd_data;
7565 	client->platform_binary = ((csproc_get_platform_binary(p) == 0) ? 0 : 1);
7566 
7567 	NECP_CLIENT_LOG(client, "Claiming client");
7568 
7569 	// Add matched client to our fd and re-run result
7570 	NECP_FD_LOCK(fd_data);
7571 	RB_INSERT(_necp_client_tree, &fd_data->clients, client);
7572 	NECP_CLIENT_LOCK(client);
7573 	(void)necp_update_client_result(current_proc(), fd_data, client, NULL);
7574 	NECP_CLIENT_UNLOCK(client);
7575 	NECP_FD_UNLOCK(fd_data);
7576 
7577 	necp_client_release(client);
7578 
7579 done:
7580 	*retval = error;
7581 
7582 	return error;
7583 }
7584 
7585 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_remove(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)7586 necp_client_remove(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7587 {
7588 	int error = 0;
7589 	uuid_t client_id = {};
7590 	struct ifnet_stats_per_flow flow_ifnet_stats = {};
7591 	const size_t buffer_size = uap->buffer_size;
7592 
7593 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
7594 		error = EINVAL;
7595 		goto done;
7596 	}
7597 
7598 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
7599 	if (error) {
7600 		NECPLOG(LOG_ERR, "necp_client_remove copyin client_id error (%d)", error);
7601 		goto done;
7602 	}
7603 
7604 	if (uap->buffer != 0 && buffer_size == sizeof(flow_ifnet_stats)) {
7605 		error = copyin(uap->buffer, &flow_ifnet_stats, buffer_size);
7606 		if (error) {
7607 			NECPLOG(LOG_ERR, "necp_client_remove flow_ifnet_stats copyin error (%d)", error);
7608 			// Not fatal; make sure to zero-out stats in case of partial copy
7609 			memset(&flow_ifnet_stats, 0, sizeof(flow_ifnet_stats));
7610 			error = 0;
7611 		}
7612 	} else if (uap->buffer != 0) {
7613 		NECPLOG(LOG_ERR, "necp_client_remove unexpected parameters length (%zu)", buffer_size);
7614 	}
7615 
7616 	NECP_FD_LOCK(fd_data);
7617 
7618 	pid_t pid = fd_data->proc_pid;
7619 	struct necp_client *client = necp_client_fd_find_client_unlocked(fd_data, client_id);
7620 
7621 	NECP_CLIENT_LOG(client, "Removing client");
7622 
7623 	if (client != NULL) {
7624 		// Remove any flow registrations that match
7625 		struct necp_client_flow_registration *flow_registration = NULL;
7626 		struct necp_client_flow_registration *temp_flow_registration = NULL;
7627 		RB_FOREACH_SAFE(flow_registration, _necp_fd_flow_tree, &fd_data->flows, temp_flow_registration) {
7628 			if (flow_registration->client == client) {
7629 #if SKYWALK
7630 				necp_destroy_flow_stats(fd_data, flow_registration, NULL, TRUE);
7631 #endif /* SKYWALK */
7632 				NECP_FLOW_TREE_LOCK_EXCLUSIVE();
7633 				RB_REMOVE(_necp_client_flow_global_tree, &necp_client_flow_global_tree, flow_registration);
7634 				NECP_FLOW_TREE_UNLOCK();
7635 				RB_REMOVE(_necp_fd_flow_tree, &fd_data->flows, flow_registration);
7636 			}
7637 		}
7638 #if SKYWALK
7639 		if (client->nstat_context != NULL) {
7640 			// Main path, we expect stats to be in existance at this point
7641 			nstat_provider_stats_close(client->nstat_context);
7642 			client->nstat_context = NULL;
7643 		} else {
7644 			NECPLOG0(LOG_ERR, "necp_client_remove ntstat shutdown finds nstat_context NULL");
7645 		}
7646 #endif /* SKYWALK */
7647 		// Remove client from lists
7648 		NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
7649 		RB_REMOVE(_necp_client_global_tree, &necp_client_global_tree, client);
7650 		NECP_CLIENT_TREE_UNLOCK();
7651 		RB_REMOVE(_necp_client_tree, &fd_data->clients, client);
7652 	}
7653 
7654 #if SKYWALK
7655 	// If the currently-active arena is idle (has no more flows referring to it), or if there are defunct
7656 	// arenas lingering in the list, schedule a threadcall to do the clean up.  The idle check is done
7657 	// by checking if the reference count is 3: one held by this client (will be released below when we
7658 	// destroy it) when it's non-NULL; the rest held by stats_arena_{active,list}.
7659 	if ((fd_data->stats_arena_active != NULL && fd_data->stats_arena_active->nai_use_count == 3) ||
7660 	    (fd_data->stats_arena_active == NULL && !LIST_EMPTY(&fd_data->stats_arena_list))) {
7661 		uint64_t deadline = 0;
7662 		uint64_t leeway = 0;
7663 		clock_interval_to_deadline(necp_close_arenas_timeout_microseconds, NSEC_PER_USEC, &deadline);
7664 		clock_interval_to_absolutetime_interval(necp_close_arenas_timeout_leeway_microseconds, NSEC_PER_USEC, &leeway);
7665 
7666 		thread_call_enter_delayed_with_leeway(necp_close_empty_arenas_tcall, NULL,
7667 		    deadline, leeway, THREAD_CALL_DELAY_LEEWAY);
7668 	}
7669 #endif /* SKYWALK */
7670 
7671 	NECP_FD_UNLOCK(fd_data);
7672 
7673 	if (client != NULL) {
7674 		ASSERT(error == 0);
7675 		necp_destroy_client(client, pid, true);
7676 	} else {
7677 		error = ENOENT;
7678 		NECPLOG(LOG_ERR, "necp_client_remove invalid client_id (%d)", error);
7679 	}
7680 done:
7681 	*retval = error;
7682 
7683 	return error;
7684 }
7685 
7686 static struct necp_client_flow_registration *
necp_client_fd_find_flow(struct necp_fd_data * client_fd,uuid_t flow_id)7687 necp_client_fd_find_flow(struct necp_fd_data *client_fd, uuid_t flow_id)
7688 {
7689 	NECP_FD_ASSERT_LOCKED(client_fd);
7690 	struct necp_client_flow_registration *flow = NULL;
7691 
7692 	if (necp_client_id_is_flow(flow_id)) {
7693 		struct necp_client_flow_registration find;
7694 		uuid_copy(find.registration_id, flow_id);
7695 		flow = RB_FIND(_necp_fd_flow_tree, &client_fd->flows, &find);
7696 	}
7697 
7698 	return flow;
7699 }
7700 
7701 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_remove_flow(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)7702 necp_client_remove_flow(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7703 {
7704 	int error = 0;
7705 	uuid_t flow_id = {};
7706 	struct ifnet_stats_per_flow flow_ifnet_stats = {};
7707 	const size_t buffer_size = uap->buffer_size;
7708 
7709 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
7710 		error = EINVAL;
7711 		NECPLOG(LOG_ERR, "necp_client_remove_flow invalid client_id (length %zu)", (size_t)uap->client_id_len);
7712 		goto done;
7713 	}
7714 
7715 	error = copyin(uap->client_id, flow_id, sizeof(uuid_t));
7716 	if (error) {
7717 		NECPLOG(LOG_ERR, "necp_client_remove_flow copyin client_id error (%d)", error);
7718 		goto done;
7719 	}
7720 
7721 	if (uap->buffer != 0 && buffer_size == sizeof(flow_ifnet_stats)) {
7722 		error = copyin(uap->buffer, &flow_ifnet_stats, buffer_size);
7723 		if (error) {
7724 			NECPLOG(LOG_ERR, "necp_client_remove flow_ifnet_stats copyin error (%d)", error);
7725 			// Not fatal
7726 		}
7727 	} else if (uap->buffer != 0) {
7728 		NECPLOG(LOG_ERR, "necp_client_remove unexpected parameters length (%zu)", buffer_size);
7729 	}
7730 
7731 	NECP_FD_LOCK(fd_data);
7732 	struct necp_client *client = NULL;
7733 	struct necp_client_flow_registration *flow_registration = necp_client_fd_find_flow(fd_data, flow_id);
7734 	if (flow_registration != NULL) {
7735 #if SKYWALK
7736 		// Cleanup stats per flow
7737 		necp_destroy_flow_stats(fd_data, flow_registration, &flow_ifnet_stats, TRUE);
7738 #endif /* SKYWALK */
7739 		NECP_FLOW_TREE_LOCK_EXCLUSIVE();
7740 		RB_REMOVE(_necp_client_flow_global_tree, &necp_client_flow_global_tree, flow_registration);
7741 		NECP_FLOW_TREE_UNLOCK();
7742 		RB_REMOVE(_necp_fd_flow_tree, &fd_data->flows, flow_registration);
7743 
7744 		client = flow_registration->client;
7745 		if (client != NULL) {
7746 			necp_client_retain(client);
7747 		}
7748 	}
7749 	NECP_FD_UNLOCK(fd_data);
7750 
7751 	NECP_CLIENT_FLOW_LOG(client, flow_registration, "removing flow");
7752 
7753 	if (flow_registration != NULL && client != NULL) {
7754 		NECP_CLIENT_LOCK(client);
7755 		if (flow_registration->client == client) {
7756 			necp_destroy_client_flow_registration(client, flow_registration, fd_data->proc_pid, false);
7757 		}
7758 		necp_client_release_locked(client);
7759 		NECP_CLIENT_UNLOCK(client);
7760 	}
7761 
7762 done:
7763 	*retval = error;
7764 	if (error != 0) {
7765 		NECPLOG(LOG_ERR, "Remove flow error (%d)", error);
7766 	}
7767 
7768 	return error;
7769 }
7770 
7771 // Don't inline the function since it includes necp_client_parsed_parameters on the stack
7772 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)7773 necp_client_check_tcp_heuristics(struct necp_client *client, struct necp_client_flow *flow,
7774     u_int32_t *flags, u_int8_t *__counted_by(tfo_cookie_maxlen) tfo_cookie, u_int8_t tfo_cookie_maxlen,
7775     u_int8_t *tfo_cookie_len)
7776 {
7777 	struct necp_client_parsed_parameters parsed_parameters;
7778 	int error = 0;
7779 
7780 	error = necp_client_parse_parameters(client, client->parameters,
7781 	    (u_int32_t)client->parameters_length,
7782 	    &parsed_parameters);
7783 	if (error) {
7784 		NECPLOG(LOG_ERR, "necp_client_parse_parameters error (%d)", error);
7785 		return error;
7786 	}
7787 
7788 	if ((flow->remote_addr.sa.sa_family != AF_INET &&
7789 	    flow->remote_addr.sa.sa_family != AF_INET6) ||
7790 	    (flow->local_addr.sa.sa_family != AF_INET &&
7791 	    flow->local_addr.sa.sa_family != AF_INET6)) {
7792 		return EINVAL;
7793 	}
7794 
7795 	NECP_CLIENT_ROUTE_LOCK(client);
7796 
7797 	if (client->current_route == NULL) {
7798 		error = ENOENT;
7799 		goto do_unlock;
7800 	}
7801 
7802 	bool check_ecn = false;
7803 	do {
7804 		if ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_ECN_ENABLE) ==
7805 		    NECP_CLIENT_PARAMETER_FLAG_ECN_ENABLE) {
7806 			check_ecn = true;
7807 			break;
7808 		}
7809 
7810 		if ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_ECN_DISABLE) ==
7811 		    NECP_CLIENT_PARAMETER_FLAG_ECN_DISABLE) {
7812 			break;
7813 		}
7814 
7815 		if (client->current_route != NULL) {
7816 			if (client->current_route->rt_ifp->if_eflags & IFEF_ECN_ENABLE) {
7817 				check_ecn = true;
7818 				break;
7819 			}
7820 			if (client->current_route->rt_ifp->if_eflags & IFEF_ECN_DISABLE) {
7821 				break;
7822 			}
7823 		}
7824 
7825 		bool inbound = ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) == 0);
7826 		if ((inbound && tcp_ecn_inbound == 1) ||
7827 		    (!inbound && tcp_ecn_outbound == 1)) {
7828 			check_ecn = true;
7829 		}
7830 	} while (false);
7831 
7832 	if (check_ecn) {
7833 		if (tcp_heuristic_do_ecn_with_address(client->current_route->rt_ifp,
7834 		    (union sockaddr_in_4_6 *)&flow->local_addr)) {
7835 			*flags |= NECP_CLIENT_RESULT_FLAG_ECN_ENABLED;
7836 		}
7837 	}
7838 
7839 	if ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_TFO_ENABLE) ==
7840 	    NECP_CLIENT_PARAMETER_FLAG_TFO_ENABLE) {
7841 		if (!tcp_heuristic_do_tfo_with_address(client->current_route->rt_ifp,
7842 		    (union sockaddr_in_4_6 *)&flow->local_addr,
7843 		    (union sockaddr_in_4_6 *)&flow->remote_addr,
7844 		    tfo_cookie, tfo_cookie_maxlen, tfo_cookie_len)) {
7845 			*flags |= NECP_CLIENT_RESULT_FLAG_FAST_OPEN_BLOCKED;
7846 			*tfo_cookie_len = 0;
7847 		}
7848 	} else {
7849 		*flags |= NECP_CLIENT_RESULT_FLAG_FAST_OPEN_BLOCKED;
7850 		*tfo_cookie_len = 0;
7851 	}
7852 do_unlock:
7853 	NECP_CLIENT_ROUTE_UNLOCK(client);
7854 
7855 	return error;
7856 }
7857 
7858 static size_t
necp_client_calculate_flow_tlv_size(struct necp_client_flow_registration * flow_registration)7859 necp_client_calculate_flow_tlv_size(struct necp_client_flow_registration *flow_registration)
7860 {
7861 	size_t assigned_results_size = 0;
7862 	struct necp_client_flow *flow = NULL;
7863 	LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
7864 		if (flow->assigned || !necp_client_endpoint_is_unspecified((struct necp_client_endpoint *)&flow->remote_addr)) {
7865 			size_t header_length = 0;
7866 			if (flow->nexus) {
7867 				header_length = sizeof(struct necp_client_nexus_flow_header);
7868 			} else {
7869 				header_length = sizeof(struct necp_client_flow_header);
7870 			}
7871 			assigned_results_size += (header_length + flow->assigned_results_length);
7872 
7873 			if (flow->has_protoctl_event) {
7874 				assigned_results_size += sizeof(struct necp_client_flow_protoctl_event_header);
7875 			}
7876 		}
7877 	}
7878 	return assigned_results_size;
7879 }
7880 
7881 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)7882 necp_client_fillout_flow_tlvs(struct necp_client *client,
7883     bool client_is_observed,
7884     struct necp_client_flow_registration *flow_registration,
7885     struct necp_client_action_args *uap,
7886     size_t *assigned_results_cursor)
7887 {
7888 	int error = 0;
7889 	struct necp_client_flow *flow = NULL;
7890 	LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
7891 		if (flow->assigned || !necp_client_endpoint_is_unspecified((struct necp_client_endpoint *)&flow->remote_addr)) {
7892 			// Write TLV headers
7893 			struct necp_client_nexus_flow_header header = {};
7894 			u_int32_t length = 0;
7895 			u_int32_t flags = 0;
7896 			u_int8_t tfo_cookie_len = 0;
7897 			u_int8_t type = 0;
7898 
7899 			type = NECP_CLIENT_RESULT_FLOW_ID;
7900 			length = sizeof(header.flow_header.flow_id);
7901 			header.flow_header.flow_id_tlv_header.type = type;
7902 			header.flow_header.flow_id_tlv_header.length = length;
7903 			uuid_copy(header.flow_header.flow_id, flow_registration->registration_id);
7904 
7905 			if (flow->nexus) {
7906 				if (flow->check_tcp_heuristics) {
7907 					u_int8_t tfo_cookie[NECP_TFO_COOKIE_LEN_MAX];
7908 					tfo_cookie_len = NECP_TFO_COOKIE_LEN_MAX;
7909 
7910 					if (necp_client_check_tcp_heuristics(client, flow, &flags,
7911 					    tfo_cookie, tfo_cookie_len, &tfo_cookie_len) != 0) {
7912 						tfo_cookie_len = 0;
7913 					} else {
7914 						flow->check_tcp_heuristics = FALSE;
7915 
7916 						if (tfo_cookie_len != 0) {
7917 							type = NECP_CLIENT_RESULT_TFO_COOKIE;
7918 							length = tfo_cookie_len;
7919 							header.tfo_cookie_tlv_header.type = type;
7920 							header.tfo_cookie_tlv_header.length = length;
7921 							memcpy(&header.tfo_cookie_value, tfo_cookie, tfo_cookie_len);
7922 						}
7923 					}
7924 				}
7925 			}
7926 
7927 			size_t header_length = 0;
7928 			if (flow->nexus) {
7929 				if (tfo_cookie_len != 0) {
7930 					header_length = sizeof(struct necp_client_nexus_flow_header) - (NECP_TFO_COOKIE_LEN_MAX - tfo_cookie_len);
7931 				} else {
7932 					header_length = sizeof(struct necp_client_nexus_flow_header) - sizeof(struct necp_tlv_header) - NECP_TFO_COOKIE_LEN_MAX;
7933 				}
7934 			} else {
7935 				header_length = sizeof(struct necp_client_flow_header);
7936 			}
7937 
7938 			type = NECP_CLIENT_RESULT_FLAGS;
7939 			length = sizeof(header.flow_header.flags_value);
7940 			header.flow_header.flags_tlv_header.type = type;
7941 			header.flow_header.flags_tlv_header.length = length;
7942 			if (flow->assigned) {
7943 				flags |= NECP_CLIENT_RESULT_FLAG_FLOW_ASSIGNED;
7944 			}
7945 			if (flow->viable) {
7946 				flags |= NECP_CLIENT_RESULT_FLAG_FLOW_VIABLE;
7947 			}
7948 			if (flow_registration->defunct) {
7949 				flags |= NECP_CLIENT_RESULT_FLAG_DEFUNCT;
7950 			}
7951 			flags |= flow->necp_flow_flags;
7952 			header.flow_header.flags_value = flags;
7953 
7954 			type = NECP_CLIENT_RESULT_INTERFACE;
7955 			length = sizeof(header.flow_header.interface_value);
7956 			header.flow_header.interface_tlv_header.type = type;
7957 			header.flow_header.interface_tlv_header.length = length;
7958 
7959 			struct necp_client_result_interface interface_struct;
7960 			interface_struct.generation = 0;
7961 			interface_struct.index = flow->interface_index;
7962 
7963 			header.flow_header.interface_value = interface_struct;
7964 			if (flow->nexus) {
7965 				type = NECP_CLIENT_RESULT_NETAGENT;
7966 				length = sizeof(header.agent_value);
7967 				header.agent_tlv_header.type = type;
7968 				header.agent_tlv_header.length = length;
7969 
7970 				struct necp_client_result_netagent agent_struct;
7971 				uuid_copy(agent_struct.netagent_uuid, flow->u.nexus_agent);
7972 				agent_struct.generation = netagent_get_generation(agent_struct.netagent_uuid);
7973 
7974 				header.agent_value = agent_struct;
7975 			}
7976 
7977 			// Don't include outer TLV header in length field
7978 			type = NECP_CLIENT_RESULT_FLOW;
7979 			length = (header_length - sizeof(struct necp_tlv_header) + flow->assigned_results_length);
7980 			if (flow->has_protoctl_event) {
7981 				length += sizeof(struct necp_client_flow_protoctl_event_header);
7982 			}
7983 			header.flow_header.outer_header.type = type;
7984 			header.flow_header.outer_header.length = length;
7985 
7986 			error = copyout(&header, uap->buffer + client->result_length + *assigned_results_cursor, header_length);
7987 			if (error) {
7988 				NECPLOG(LOG_ERR, "necp_client_copy assigned results tlv_header copyout error (%d)", error);
7989 				return error;
7990 			}
7991 			*assigned_results_cursor += header_length;
7992 
7993 			if (flow->assigned_results && flow->assigned_results_length) {
7994 				// Write inner TLVs
7995 				error = copyout(flow->assigned_results, uap->buffer + client->result_length + *assigned_results_cursor,
7996 				    flow->assigned_results_length);
7997 				if (error) {
7998 					NECPLOG(LOG_ERR, "necp_client_copy assigned results copyout error (%d)", error);
7999 					return error;
8000 				}
8001 			}
8002 			*assigned_results_cursor += flow->assigned_results_length;
8003 
8004 			/* Read the protocol event and reset it */
8005 			if (flow->has_protoctl_event) {
8006 				struct necp_client_flow_protoctl_event_header protoctl_event_header = {};
8007 
8008 				type = NECP_CLIENT_RESULT_PROTO_CTL_EVENT;
8009 				length = sizeof(protoctl_event_header.protoctl_event);
8010 
8011 				protoctl_event_header.protoctl_tlv_header.type = type;
8012 				protoctl_event_header.protoctl_tlv_header.length = length;
8013 				protoctl_event_header.protoctl_event = flow->protoctl_event;
8014 
8015 				error = copyout(&protoctl_event_header, uap->buffer + client->result_length + *assigned_results_cursor,
8016 				    sizeof(protoctl_event_header));
8017 
8018 				if (error) {
8019 					NECPLOG(LOG_ERR, "necp_client_copy protocol control event results"
8020 					    " tlv_header copyout error (%d)", error);
8021 					return error;
8022 				}
8023 				*assigned_results_cursor += sizeof(protoctl_event_header);
8024 				flow->has_protoctl_event = FALSE;
8025 				flow->protoctl_event.protoctl_event_code = 0;
8026 				flow->protoctl_event.protoctl_event_val = 0;
8027 				flow->protoctl_event.protoctl_event_tcp_seq_num = 0;
8028 			}
8029 		}
8030 	}
8031 	if (!client_is_observed) {
8032 		flow_registration->flow_result_read = TRUE;
8033 	}
8034 	return 0;
8035 }
8036 
8037 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)8038 necp_client_copy_internal(struct necp_client *client, uuid_t client_id, bool client_is_observed, struct necp_client_action_args *uap, int *retval)
8039 {
8040 	NECP_CLIENT_ASSERT_LOCKED(client);
8041 	int error = 0;
8042 	// Copy results out
8043 	if (uap->action == NECP_CLIENT_ACTION_COPY_PARAMETERS) {
8044 		if (uap->buffer_size < client->parameters_length) {
8045 			return EINVAL;
8046 		}
8047 		error = copyout(client->parameters, uap->buffer, client->parameters_length);
8048 		if (error) {
8049 			NECPLOG(LOG_ERR, "necp_client_copy parameters copyout error (%d)", error);
8050 			return error;
8051 		}
8052 		*retval = client->parameters_length;
8053 	} else if ((uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT || uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT_FINAL) &&
8054 	    client->result_read && client->group_members_read && !necp_client_has_unread_flows(client)) {
8055 		// Copy updates only, but nothing to read
8056 		// Just return 0 for bytes read
8057 		*retval = 0;
8058 	} else if (uap->action == NECP_CLIENT_ACTION_COPY_RESULT ||
8059 	    uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT ||
8060 	    uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT_FINAL) {
8061 		size_t assigned_results_size = client->assigned_group_members_length;
8062 
8063 		bool some_flow_is_defunct = false;
8064 		struct necp_client_flow_registration *single_flow_registration = NULL;
8065 		if (necp_client_id_is_flow(client_id)) {
8066 			single_flow_registration = necp_client_find_flow(client, client_id);
8067 			if (single_flow_registration != NULL) {
8068 				assigned_results_size += necp_client_calculate_flow_tlv_size(single_flow_registration);
8069 			}
8070 		} else {
8071 			// This request is for the client, so copy everything
8072 			struct necp_client_flow_registration *flow_registration = NULL;
8073 			RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
8074 				if (flow_registration->defunct) {
8075 					some_flow_is_defunct = true;
8076 				}
8077 				assigned_results_size += necp_client_calculate_flow_tlv_size(flow_registration);
8078 			}
8079 		}
8080 		if (uap->buffer_size < (client->result_length + assigned_results_size)) {
8081 			if (uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT_FINAL) {
8082 				// Mark the client and all flows as read to prevent looping
8083 				client->result_read = true;
8084 				struct necp_client_flow_registration *flow_registration = NULL;
8085 				RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
8086 					flow_registration->flow_result_read = true;
8087 				}
8088 			}
8089 			return EINVAL;
8090 		}
8091 
8092 		u_int32_t original_flags = 0;
8093 		bool flags_updated = false;
8094 		if (some_flow_is_defunct && client->legacy_client_is_flow) {
8095 			// If our client expects the defunct flag in the client, add it now
8096 			u_int32_t client_flags = 0;
8097 			u_int32_t value_size = 0;
8098 			u_int8_t *flags_pointer = necp_buffer_get_tlv_value(client->result, client->result_length, 0, &value_size);
8099 			if (flags_pointer != NULL && value_size == sizeof(client_flags)) {
8100 				memcpy(&client_flags, flags_pointer, value_size);
8101 				original_flags = client_flags;
8102 				client_flags |= NECP_CLIENT_RESULT_FLAG_DEFUNCT;
8103 				(void)necp_buffer_write_tlv_if_different(client->result, NECP_CLIENT_RESULT_FLAGS,
8104 				    sizeof(client_flags), &client_flags, &flags_updated,
8105 				    client->result, sizeof(client->result));
8106 			}
8107 		}
8108 
8109 		error = copyout(client->result, uap->buffer, client->result_length);
8110 
8111 		if (flags_updated) {
8112 			// Revert stored flags
8113 			(void)necp_buffer_write_tlv_if_different(client->result, NECP_CLIENT_RESULT_FLAGS,
8114 			    sizeof(original_flags), &original_flags, &flags_updated,
8115 			    client->result, sizeof(client->result));
8116 		}
8117 
8118 		if (error != 0) {
8119 			NECPLOG(LOG_ERR, "necp_client_copy result copyout error (%d)", error);
8120 			return error;
8121 		}
8122 
8123 		if (client->assigned_group_members != NULL && client->assigned_group_members_length > 0) {
8124 			error = copyout(client->assigned_group_members, uap->buffer + client->result_length, client->assigned_group_members_length);
8125 			if (error != 0) {
8126 				NECPLOG(LOG_ERR, "necp_client_copy group members copyout error (%d)", error);
8127 				return error;
8128 			}
8129 		}
8130 
8131 		size_t assigned_results_cursor = client->assigned_group_members_length; // Start with an offset based on the group members
8132 		if (necp_client_id_is_flow(client_id)) {
8133 			if (single_flow_registration != NULL) {
8134 				error = necp_client_fillout_flow_tlvs(client, client_is_observed, single_flow_registration, uap, &assigned_results_cursor);
8135 				if (error != 0) {
8136 					return error;
8137 				}
8138 			}
8139 		} else {
8140 			// This request is for the client, so copy everything
8141 			struct necp_client_flow_registration *flow_registration = NULL;
8142 			RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
8143 				error = necp_client_fillout_flow_tlvs(client, client_is_observed, flow_registration, uap, &assigned_results_cursor);
8144 				if (error != 0) {
8145 					return error;
8146 				}
8147 			}
8148 		}
8149 
8150 		*retval = client->result_length + assigned_results_cursor;
8151 
8152 		if (!client_is_observed) {
8153 			client->result_read = TRUE;
8154 			client->group_members_read = TRUE;
8155 		}
8156 	}
8157 
8158 	return 0;
8159 }
8160 
8161 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_copy(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8162 necp_client_copy(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8163 {
8164 	int error = 0;
8165 	struct necp_client *client = NULL;
8166 	uuid_t client_id;
8167 	uuid_clear(client_id);
8168 
8169 	*retval = 0;
8170 
8171 	if (uap->buffer_size == 0 || uap->buffer == 0) {
8172 		return EINVAL;
8173 	}
8174 
8175 	if (uap->action != NECP_CLIENT_ACTION_COPY_PARAMETERS &&
8176 	    uap->action != NECP_CLIENT_ACTION_COPY_RESULT &&
8177 	    uap->action != NECP_CLIENT_ACTION_COPY_UPDATED_RESULT &&
8178 	    uap->action != NECP_CLIENT_ACTION_COPY_UPDATED_RESULT_FINAL) {
8179 		return EINVAL;
8180 	}
8181 
8182 	if (uap->client_id) {
8183 		if (uap->client_id_len != sizeof(uuid_t)) {
8184 			NECPLOG(LOG_ERR, "Incorrect length (got %zu, expected %zu)", (size_t)uap->client_id_len, sizeof(uuid_t));
8185 			return ERANGE;
8186 		}
8187 
8188 		error = copyin(uap->client_id, client_id, sizeof(uuid_t));
8189 		if (error) {
8190 			NECPLOG(LOG_ERR, "necp_client_copy client_id copyin error (%d)", error);
8191 			return error;
8192 		}
8193 	}
8194 
8195 	const bool is_wildcard = (bool)uuid_is_null(client_id);
8196 
8197 	NECP_FD_LOCK(fd_data);
8198 
8199 	bool send_in_process_flow_divert_message = false;
8200 	if (is_wildcard) {
8201 		if (uap->action == NECP_CLIENT_ACTION_COPY_RESULT ||
8202 		    uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT ||
8203 		    uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT_FINAL) {
8204 			struct necp_client *find_client = NULL;
8205 			RB_FOREACH(find_client, _necp_client_tree, &fd_data->clients) {
8206 				NECP_CLIENT_LOCK(find_client);
8207 				if (!find_client->result_read || !find_client->group_members_read || necp_client_has_unread_flows(find_client)) {
8208 					client = find_client;
8209 					// Leave the client locked, and break
8210 					break;
8211 				}
8212 				NECP_CLIENT_UNLOCK(find_client);
8213 			}
8214 
8215 			if (client == NULL && fd_data->request_in_process_flow_divert) {
8216 				// No client found that needs update. Check for an event requesting in-process flow divert.
8217 				send_in_process_flow_divert_message = true;
8218 			}
8219 		}
8220 	} else {
8221 		client = necp_client_fd_find_client_and_lock(fd_data, client_id);
8222 	}
8223 
8224 	if (client != NULL) {
8225 		if (!send_in_process_flow_divert_message) {
8226 			// If client is set, it is locked
8227 			error = necp_client_copy_internal(client, client_id, FALSE, uap, retval);
8228 		}
8229 		NECP_CLIENT_UNLOCK(client);
8230 	}
8231 
8232 	if (send_in_process_flow_divert_message) {
8233 		fd_data->request_in_process_flow_divert = false;
8234 
8235 		struct necp_tlv_header request_tlv = {
8236 			.type = NECP_CLIENT_RESULT_REQUEST_IN_PROCESS_FLOW_DIVERT,
8237 			.length = 0,
8238 		};
8239 		if (uap->buffer_size < sizeof(request_tlv)) {
8240 			error = EINVAL;
8241 		} else {
8242 			error = copyout(&request_tlv, uap->buffer, sizeof(request_tlv));
8243 			if (error) {
8244 				NECPLOG(LOG_ERR, "necp_client_copy request flow divert TLV copyout error (%d)", error);
8245 			} else {
8246 				*retval = sizeof(request_tlv);
8247 			}
8248 		}
8249 	}
8250 
8251 	// Unlock our own fd before moving on or returning
8252 	NECP_FD_UNLOCK(fd_data);
8253 
8254 	if (client == NULL && !send_in_process_flow_divert_message) {
8255 		if (fd_data->flags & NECP_OPEN_FLAG_OBSERVER) {
8256 			// Observers are allowed to lookup clients on other fds
8257 
8258 			// Lock tree
8259 			NECP_CLIENT_TREE_LOCK_SHARED();
8260 
8261 			bool found_client = FALSE;
8262 
8263 			client = necp_find_client_and_lock(client_id);
8264 			if (client != NULL) {
8265 				// Matched, copy out data
8266 				found_client = TRUE;
8267 				error = necp_client_copy_internal(client, client_id, TRUE, uap, retval);
8268 				NECP_CLIENT_UNLOCK(client);
8269 			}
8270 
8271 			// Unlock tree
8272 			NECP_CLIENT_TREE_UNLOCK();
8273 
8274 			// No client found, fail
8275 			if (!found_client) {
8276 				return ENOENT;
8277 			}
8278 		} else {
8279 			// No client found, and not allowed to search other fds, fail
8280 			return ENOENT;
8281 		}
8282 	}
8283 
8284 	return error;
8285 }
8286 
8287 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)8288 necp_client_copy_client_update(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8289 {
8290 	int error = 0;
8291 
8292 	*retval = 0;
8293 
8294 	if (!(fd_data->flags & NECP_OPEN_FLAG_PUSH_OBSERVER)) {
8295 		NECPLOG0(LOG_ERR, "NECP fd is not observer, cannot copy client update");
8296 		return EINVAL;
8297 	}
8298 
8299 	if (uap->client_id_len != sizeof(uuid_t) || uap->client_id == 0) {
8300 		NECPLOG0(LOG_ERR, "Client id invalid, cannot copy client update");
8301 		return EINVAL;
8302 	}
8303 
8304 	if (uap->buffer_size == 0 || uap->buffer == 0) {
8305 		NECPLOG0(LOG_ERR, "Buffer invalid, cannot copy client update");
8306 		return EINVAL;
8307 	}
8308 
8309 	NECP_FD_LOCK(fd_data);
8310 	struct necp_client_update *client_update = TAILQ_FIRST(&fd_data->update_list);
8311 	if (client_update != NULL) {
8312 		TAILQ_REMOVE(&fd_data->update_list, client_update, chain);
8313 		VERIFY(fd_data->update_count > 0);
8314 		fd_data->update_count--;
8315 	}
8316 	NECP_FD_UNLOCK(fd_data);
8317 
8318 	if (client_update != NULL) {
8319 		error = copyout(client_update->client_id, uap->client_id, sizeof(uuid_t));
8320 		if (error) {
8321 			NECPLOG(LOG_ERR, "Copy client update copyout client id error (%d)", error);
8322 		} else {
8323 			if (uap->buffer_size < client_update->update_length) {
8324 				NECPLOG(LOG_ERR, "Buffer size cannot hold update (%zu < %zu)", (size_t)uap->buffer_size, client_update->update_length);
8325 				error = EINVAL;
8326 			} else {
8327 				error = copyout(client_update->update, uap->buffer, client_update->update_length);
8328 				if (error) {
8329 					NECPLOG(LOG_ERR, "Copy client update copyout error (%d)", error);
8330 				} else {
8331 					*retval = client_update->update_length;
8332 				}
8333 			}
8334 		}
8335 
8336 		necp_client_update_free(client_update);
8337 		client_update = NULL;
8338 	} else {
8339 		error = ENOENT;
8340 	}
8341 
8342 	return error;
8343 }
8344 
8345 static int
necp_client_copy_parameters_locked(struct necp_client * client,struct necp_client_nexus_parameters * parameters)8346 necp_client_copy_parameters_locked(struct necp_client *client,
8347     struct necp_client_nexus_parameters *parameters)
8348 {
8349 	VERIFY(parameters != NULL);
8350 
8351 	struct necp_client_parsed_parameters parsed_parameters = {};
8352 	int error = necp_client_parse_parameters(client, client->parameters, (u_int32_t)client->parameters_length, &parsed_parameters);
8353 
8354 	parameters->pid = client->proc_pid;
8355 	if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID) {
8356 		parameters->epid = parsed_parameters.effective_pid;
8357 	} else {
8358 		parameters->epid = parameters->pid;
8359 	}
8360 #if SKYWALK
8361 	parameters->port_reservation = client->port_reservation;
8362 #endif /* !SKYWALK */
8363 	memcpy(&parameters->local_addr, &parsed_parameters.local_addr, sizeof(parameters->local_addr));
8364 	memcpy(&parameters->remote_addr, &parsed_parameters.remote_addr, sizeof(parameters->remote_addr));
8365 	parameters->ip_protocol = parsed_parameters.ip_protocol;
8366 	if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_TRANSPORT_PROTOCOL) {
8367 		parameters->transport_protocol = parsed_parameters.transport_protocol;
8368 	} else {
8369 		parameters->transport_protocol = parsed_parameters.ip_protocol;
8370 	}
8371 	parameters->ethertype = parsed_parameters.ethertype;
8372 	parameters->traffic_class = parsed_parameters.traffic_class;
8373 	if (uuid_is_null(client->override_euuid)) {
8374 		uuid_copy(parameters->euuid, parsed_parameters.effective_uuid);
8375 	} else {
8376 		uuid_copy(parameters->euuid, client->override_euuid);
8377 	}
8378 	parameters->is_listener = (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) ? 1 : 0;
8379 	parameters->is_interpose = (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_INTERPOSE) ? 1 : 0;
8380 	parameters->is_custom_ether = (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_CUSTOM_ETHER) ? 1 : 0;
8381 	parameters->policy_id = client->policy_id;
8382 	parameters->skip_policy_id = client->skip_policy_id;
8383 
8384 	// parse client result flag
8385 	u_int32_t client_result_flags = 0;
8386 	u_int32_t value_size = 0;
8387 	u_int8_t *flags_pointer = NULL;
8388 	flags_pointer = necp_buffer_get_tlv_value(client->result, client->result_length, 0, &value_size);
8389 	if (flags_pointer && value_size == sizeof(client_result_flags)) {
8390 		memcpy(&client_result_flags, flags_pointer, value_size);
8391 	}
8392 	parameters->allow_qos_marking = (client_result_flags & NECP_CLIENT_RESULT_FLAG_ALLOW_QOS_MARKING) ? 1 : 0;
8393 
8394 	if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR_PREFERENCE) {
8395 		if (parsed_parameters.local_address_preference == NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_DEFAULT) {
8396 			parameters->override_address_selection = false;
8397 		} else if (parsed_parameters.local_address_preference == NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_TEMPORARY) {
8398 			parameters->override_address_selection = true;
8399 			parameters->use_stable_address = false;
8400 		} else if (parsed_parameters.local_address_preference == NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_STABLE) {
8401 			parameters->override_address_selection = true;
8402 			parameters->use_stable_address = true;
8403 		}
8404 	} else {
8405 		parameters->override_address_selection = false;
8406 	}
8407 
8408 	if ((parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) &&
8409 	    (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_NO_WAKE_FROM_SLEEP)) {
8410 		parameters->no_wake_from_sleep = true;
8411 	}
8412 
8413 	if ((parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) &&
8414 	    (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_REUSE_LOCAL)) {
8415 		parameters->reuse_port = true;
8416 	}
8417 
8418 #if SKYWALK
8419 	if (!parameters->is_listener) {
8420 		if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLOW_DEMUX_PATTERN) {
8421 			if (parsed_parameters.demux_patterns[0].len == 0) {
8422 				parameters->is_demuxable_parent = 1;
8423 			} else {
8424 				if (client->validated_parent) {
8425 					ASSERT(!uuid_is_null(client->parent_client_id));
8426 
8427 					NECP_CLIENT_TREE_LOCK_SHARED();
8428 					struct necp_client *parent = necp_find_client_and_lock(client->parent_client_id);
8429 					if (parent != NULL) {
8430 						struct necp_client_flow_registration *parent_flow_registration = NULL;
8431 						RB_FOREACH(parent_flow_registration, _necp_client_flow_tree, &parent->flow_registrations) {
8432 							uuid_copy(parameters->parent_flow_uuid, parent_flow_registration->registration_id);
8433 							break;
8434 						}
8435 
8436 						NECP_CLIENT_UNLOCK(parent);
8437 					}
8438 					NECP_CLIENT_TREE_UNLOCK();
8439 
8440 					if (parsed_parameters.demux_pattern_count > 0) {
8441 						for (int i = 0; i < parsed_parameters.demux_pattern_count; i++) {
8442 							memcpy(&parameters->demux_patterns[i], &parsed_parameters.demux_patterns[i], sizeof(struct necp_demux_pattern));
8443 						}
8444 						parameters->demux_pattern_count = parsed_parameters.demux_pattern_count;
8445 					}
8446 				}
8447 			}
8448 		}
8449 	}
8450 #endif // SKYWALK
8451 
8452 	return error;
8453 }
8454 
8455 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_list(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8456 necp_client_list(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8457 {
8458 	int error = 0;
8459 	struct necp_client *find_client = NULL;
8460 	size_t copy_buffer_size = 0;
8461 	uuid_t *list = NULL;
8462 	u_int32_t requested_client_count = 0;
8463 	u_int32_t client_count = 0;
8464 
8465 	if (uap->buffer_size < sizeof(requested_client_count) || uap->buffer == 0) {
8466 		error = EINVAL;
8467 		goto done;
8468 	}
8469 
8470 	if (!(fd_data->flags & NECP_OPEN_FLAG_OBSERVER)) {
8471 		NECPLOG0(LOG_ERR, "Client does not hold necessary entitlement to list other NECP clients");
8472 		error = EACCES;
8473 		goto done;
8474 	}
8475 
8476 	error = copyin(uap->buffer, &requested_client_count, sizeof(requested_client_count));
8477 	if (error) {
8478 		goto done;
8479 	}
8480 
8481 	if (os_mul_overflow(sizeof(uuid_t), requested_client_count, &copy_buffer_size)) {
8482 		error = ERANGE;
8483 		goto done;
8484 	}
8485 
8486 	if (uap->buffer_size - sizeof(requested_client_count) != copy_buffer_size) {
8487 		error = EINVAL;
8488 		goto done;
8489 	}
8490 
8491 	if (copy_buffer_size > NECP_MAX_CLIENT_LIST_SIZE) {
8492 		error = EINVAL;
8493 		goto done;
8494 	}
8495 
8496 	if (requested_client_count > 0) {
8497 		list = (uuid_t*)kalloc_data(copy_buffer_size, Z_WAITOK | Z_ZERO);
8498 		if (list == NULL) {
8499 			error = ENOMEM;
8500 			goto done;
8501 		}
8502 	}
8503 
8504 	// Lock tree
8505 	NECP_CLIENT_TREE_LOCK_SHARED();
8506 
8507 	find_client = NULL;
8508 	RB_FOREACH(find_client, _necp_client_global_tree, &necp_client_global_tree) {
8509 		NECP_CLIENT_LOCK(find_client);
8510 		if (!uuid_is_null(find_client->client_id)) {
8511 			if (client_count < requested_client_count) {
8512 				uuid_copy(list[client_count], find_client->client_id);
8513 			}
8514 			client_count++;
8515 		}
8516 		NECP_CLIENT_UNLOCK(find_client);
8517 	}
8518 
8519 	// Unlock tree
8520 	NECP_CLIENT_TREE_UNLOCK();
8521 
8522 	error = copyout(&client_count, uap->buffer, sizeof(client_count));
8523 	if (error) {
8524 		NECPLOG(LOG_ERR, "necp_client_list buffer copyout error (%d)", error);
8525 		goto done;
8526 	}
8527 
8528 	if (requested_client_count > 0 &&
8529 	    client_count > 0 &&
8530 	    list != NULL) {
8531 		error = copyout(list, uap->buffer + sizeof(client_count), copy_buffer_size);
8532 		if (error) {
8533 			NECPLOG(LOG_ERR, "necp_client_list client count copyout error (%d)", error);
8534 			goto done;
8535 		}
8536 	}
8537 done:
8538 	if (list != NULL) {
8539 		kfree_data(list, copy_buffer_size);
8540 	}
8541 	*retval = error;
8542 
8543 	return error;
8544 }
8545 
8546 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_add_flow(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8547 necp_client_add_flow(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8548 {
8549 	int error = 0;
8550 	struct necp_client *client = NULL;
8551 	uuid_t client_id;
8552 	struct necp_client_nexus_parameters parameters = {};
8553 	struct proc *proc = PROC_NULL;
8554 	struct necp_client_add_flow * __indexable add_request = NULL;
8555 	struct necp_client_add_flow * __indexable allocated_add_request = NULL;
8556 	struct necp_client_add_flow_default default_add_request = {};
8557 	const size_t buffer_size = uap->buffer_size;
8558 
8559 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
8560 		error = EINVAL;
8561 		NECPLOG(LOG_ERR, "necp_client_add_flow invalid client_id (length %zu)", (size_t)uap->client_id_len);
8562 		goto done;
8563 	}
8564 
8565 	if (uap->buffer == 0 || buffer_size < sizeof(struct necp_client_add_flow) ||
8566 	    buffer_size > sizeof(struct necp_client_add_flow_default) * 4) {
8567 		error = EINVAL;
8568 		NECPLOG(LOG_ERR, "necp_client_add_flow invalid buffer (length %zu)", buffer_size);
8569 		goto done;
8570 	}
8571 
8572 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
8573 	if (error) {
8574 		NECPLOG(LOG_ERR, "necp_client_add_flow copyin client_id error (%d)", error);
8575 		goto done;
8576 	}
8577 
8578 	if (buffer_size <= sizeof(struct necp_client_add_flow_default)) {
8579 		// Fits in default size
8580 		error = copyin(uap->buffer, &default_add_request, buffer_size);
8581 		if (error) {
8582 			NECPLOG(LOG_ERR, "necp_client_add_flow copyin default_add_request error (%d)", error);
8583 			goto done;
8584 		}
8585 
8586 		add_request = (struct necp_client_add_flow *)&default_add_request;
8587 	} else {
8588 		allocated_add_request = (struct necp_client_add_flow *)kalloc_data(buffer_size, Z_WAITOK | Z_ZERO);
8589 		if (allocated_add_request == NULL) {
8590 			error = ENOMEM;
8591 			goto done;
8592 		}
8593 
8594 		error = copyin(uap->buffer, allocated_add_request, buffer_size);
8595 		if (error) {
8596 			NECPLOG(LOG_ERR, "necp_client_add_flow copyin default_add_request error (%d)", error);
8597 			goto done;
8598 		}
8599 
8600 		add_request = allocated_add_request;
8601 	}
8602 
8603 	NECP_FD_LOCK(fd_data);
8604 	pid_t pid = fd_data->proc_pid;
8605 	proc = proc_find(pid);
8606 	if (proc == PROC_NULL) {
8607 		NECP_FD_UNLOCK(fd_data);
8608 		NECPLOG(LOG_ERR, "necp_client_add_flow process not found for pid %d error (%d)", pid, error);
8609 		error = ESRCH;
8610 		goto done;
8611 	}
8612 
8613 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
8614 	if (client == NULL) {
8615 		error = ENOENT;
8616 		NECP_FD_UNLOCK(fd_data);
8617 		goto done;
8618 	}
8619 
8620 	// Using ADD_FLOW indicates that the client supports multiple flows per client
8621 	client->legacy_client_is_flow = false;
8622 
8623 	necp_client_retain_locked(client);
8624 	necp_client_copy_parameters_locked(client, &parameters);
8625 
8626 	struct necp_client_flow_registration *new_registration = necp_client_create_flow_registration(fd_data, client);
8627 	if (new_registration == NULL) {
8628 		error = ENOMEM;
8629 		NECP_CLIENT_UNLOCK(client);
8630 		NECP_FD_UNLOCK(fd_data);
8631 		NECPLOG0(LOG_ERR, "Failed to allocate flow registration");
8632 		goto done;
8633 	}
8634 
8635 	new_registration->flags = add_request->flags;
8636 
8637 	// Copy new ID out to caller
8638 	uuid_copy(add_request->registration_id, new_registration->registration_id);
8639 
8640 	NECP_CLIENT_FLOW_LOG(client, new_registration, "adding flow");
8641 
8642 	size_t trailer_offset = (sizeof(struct necp_client_add_flow) +
8643 	    add_request->stats_request_count * sizeof(struct necp_client_flow_stats));
8644 
8645 	// Copy override address
8646 	struct sockaddr * __single override_address = NULL;
8647 	if (add_request->flags & NECP_CLIENT_FLOW_FLAGS_OVERRIDE_ADDRESS) {
8648 		size_t offset_of_address = trailer_offset;
8649 		if (buffer_size >= offset_of_address + sizeof(struct sockaddr_in)) {
8650 			override_address = flow_req_get_address(add_request, offset_of_address);
8651 			if (buffer_size >= offset_of_address + override_address->sa_len &&
8652 			    override_address->sa_len <= sizeof(parameters.remote_addr)) {
8653 				SOCKADDR_COPY(override_address, &parameters.remote_addr, override_address->sa_len);
8654 				trailer_offset += override_address->sa_len;
8655 			} else {
8656 				override_address = NULL;
8657 			}
8658 		}
8659 	}
8660 
8661 	// Copy override IP protocol
8662 	if (add_request->flags & NECP_CLIENT_FLOW_FLAGS_OVERRIDE_IP_PROTOCOL) {
8663 		size_t offset_of_ip_protocol = trailer_offset;
8664 		if (buffer_size >= offset_of_ip_protocol + sizeof(uint8_t)) {
8665 			uint8_t * __single ip_protocol_p = flow_req_get_proto(add_request, offset_of_ip_protocol);
8666 			memcpy(&parameters.ip_protocol, ip_protocol_p, sizeof(uint8_t));
8667 		}
8668 	}
8669 
8670 #if SKYWALK
8671 	if (add_request->flags & NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS) {
8672 		size_t assigned_results_length = 0;
8673 		void * __sized_by(assigned_results_length) assigned_results = NULL;
8674 		uint32_t interface_index = 0;
8675 
8676 		// Validate that the nexus UUID is assigned
8677 		bool found_nexus = false;
8678 		for (u_int32_t option_i = 0; option_i < client->interface_option_count; option_i++) {
8679 			if (option_i < NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT) {
8680 				struct necp_client_interface_option *option = &client->interface_options[option_i];
8681 				if (uuid_compare(option->nexus_agent, add_request->agent_uuid) == 0) {
8682 					interface_index = option->interface_index;
8683 					found_nexus = true;
8684 					break;
8685 				}
8686 			} else {
8687 				struct necp_client_interface_option *option = &client->extra_interface_options[option_i - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT];
8688 				if (uuid_compare(option->nexus_agent, add_request->agent_uuid) == 0) {
8689 					interface_index = option->interface_index;
8690 					found_nexus = true;
8691 					break;
8692 				}
8693 			}
8694 		}
8695 
8696 		if (!found_nexus) {
8697 			NECPLOG0(LOG_ERR, "Requested nexus not found");
8698 		} else {
8699 			necp_client_add_nexus_flow_if_needed(new_registration, add_request->agent_uuid, interface_index);
8700 
8701 			error = netagent_client_message_with_params(add_request->agent_uuid,
8702 			    ((new_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
8703 			    client->client_id :
8704 			    new_registration->registration_id),
8705 			    pid, client->agent_handle,
8706 			    NETAGENT_MESSAGE_TYPE_REQUEST_NEXUS,
8707 			    (struct necp_client_agent_parameters *)&parameters,
8708 			    &assigned_results, &assigned_results_length);
8709 			if (error != 0) {
8710 				VERIFY(assigned_results == NULL);
8711 				VERIFY(assigned_results_length == 0);
8712 				NECPLOG(LOG_ERR, "netagent_client_message error (%d)", error);
8713 			} else if (assigned_results != NULL) {
8714 				if (!necp_assign_client_result_locked(proc, fd_data, client, new_registration, add_request->agent_uuid,
8715 				    assigned_results, assigned_results_length, false, false)) {
8716 					kfree_data_sized_by(assigned_results, assigned_results_length);
8717 				}
8718 			} else if (override_address != NULL) {
8719 				// Save the overridden address in the flow. Find the correct flow,
8720 				// and assign just the address TLV. Don't set the assigned flag.
8721 				struct necp_client_flow *flow = NULL;
8722 				LIST_FOREACH(flow, &new_registration->flow_list, flow_chain) {
8723 					if (flow->nexus &&
8724 					    uuid_compare(flow->u.nexus_agent, add_request->agent_uuid) == 0) {
8725 						if (flow->assigned_results == NULL) {
8726 							SOCKADDR_COPY(override_address, &flow->remote_addr, override_address->sa_len);
8727 							uuid_t empty_uuid;
8728 							uuid_clear(empty_uuid);
8729 							size_t message_length;
8730 							void *message = necp_create_nexus_assign_message(empty_uuid, 0, NULL, 0,
8731 							    (struct necp_client_endpoint *)&flow->local_addr,
8732 							    (struct necp_client_endpoint *)&flow->remote_addr,
8733 							    NULL, 0, NULL, &message_length);
8734 							flow->assigned_results = message;
8735 							flow->assigned_results_length = message_length;
8736 						}
8737 						break;
8738 					}
8739 				}
8740 			}
8741 		}
8742 	}
8743 
8744 	// Don't request stats if nexus creation fails
8745 	if (error == 0 && add_request->stats_request_count > 0 && necp_arena_initialize(fd_data, true) == 0) {
8746 		struct necp_client_flow_stats * __single stats_request = &(necp_client_get_flow_stats(add_request))[0];
8747 		struct necp_stats_bufreq bufreq = {};
8748 
8749 		NECP_CLIENT_FLOW_LOG(client, new_registration, "Initializing stats");
8750 
8751 		bufreq.necp_stats_bufreq_id = NECP_CLIENT_STATISTICS_BUFREQ_ID;
8752 		bufreq.necp_stats_bufreq_type = stats_request->stats_type;
8753 		bufreq.necp_stats_bufreq_ver = stats_request->stats_version;
8754 		bufreq.necp_stats_bufreq_size = stats_request->stats_size;
8755 		bufreq.necp_stats_bufreq_uaddr = stats_request->stats_addr;
8756 		(void)necp_stats_initialize(fd_data, client, new_registration, &bufreq);
8757 		stats_request->stats_type = bufreq.necp_stats_bufreq_type;
8758 		stats_request->stats_version = bufreq.necp_stats_bufreq_ver;
8759 		stats_request->stats_size = bufreq.necp_stats_bufreq_size;
8760 		stats_request->stats_addr = bufreq.necp_stats_bufreq_uaddr;
8761 	}
8762 #endif /* !SKYWALK */
8763 
8764 	if (error == 0 &&
8765 	    (add_request->flags & NECP_CLIENT_FLOW_FLAGS_BROWSE ||
8766 	    add_request->flags & NECP_CLIENT_FLOW_FLAGS_RESOLVE)) {
8767 		uint32_t interface_index = IFSCOPE_NONE;
8768 		ifnet_head_lock_shared();
8769 		struct ifnet *interface = NULL;
8770 		TAILQ_FOREACH(interface, &ifnet_head, if_link) {
8771 			ifnet_lock_shared(interface);
8772 			if (interface->if_agentids != NULL) {
8773 				for (u_int32_t i = 0; i < interface->if_agentcount; i++) {
8774 					if (uuid_compare(interface->if_agentids[i], add_request->agent_uuid) == 0) {
8775 						interface_index = interface->if_index;
8776 						break;
8777 					}
8778 				}
8779 			}
8780 			ifnet_lock_done(interface);
8781 			if (interface_index != IFSCOPE_NONE) {
8782 				break;
8783 			}
8784 		}
8785 		ifnet_head_done();
8786 
8787 		necp_client_add_nexus_flow_if_needed(new_registration, add_request->agent_uuid, interface_index);
8788 
8789 		size_t dummy_length = 0;
8790 		void * __sized_by(dummy_length) dummy_results = NULL;
8791 		error = netagent_client_message_with_params(add_request->agent_uuid,
8792 		    ((new_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
8793 		    client->client_id :
8794 		    new_registration->registration_id),
8795 		    pid, client->agent_handle,
8796 		    NETAGENT_MESSAGE_TYPE_CLIENT_ASSERT,
8797 		    (struct necp_client_agent_parameters *)&parameters,
8798 		    &dummy_results, &dummy_length);
8799 		if (error != 0) {
8800 			NECPLOG(LOG_ERR, "netagent_client_message error (%d)", error);
8801 		}
8802 	}
8803 
8804 	if (error != 0) {
8805 		// Encountered an error in adding the flow, destroy the flow registration
8806 #if SKYWALK
8807 		necp_destroy_flow_stats(fd_data, new_registration, NULL, false);
8808 #endif /* SKYWALK */
8809 		NECP_FLOW_TREE_LOCK_EXCLUSIVE();
8810 		RB_REMOVE(_necp_client_flow_global_tree, &necp_client_flow_global_tree, new_registration);
8811 		NECP_FLOW_TREE_UNLOCK();
8812 		RB_REMOVE(_necp_fd_flow_tree, &fd_data->flows, new_registration);
8813 		necp_destroy_client_flow_registration(client, new_registration, fd_data->proc_pid, true);
8814 		new_registration = NULL;
8815 	}
8816 
8817 	NECP_CLIENT_UNLOCK(client);
8818 	NECP_FD_UNLOCK(fd_data);
8819 
8820 	necp_client_release(client);
8821 
8822 	if (error != 0) {
8823 		goto done;
8824 	}
8825 
8826 	// Copy the request back out to the caller with assigned fields
8827 	error = copyout(add_request, uap->buffer, buffer_size);
8828 	if (error != 0) {
8829 		NECPLOG(LOG_ERR, "necp_client_add_flow copyout add_request error (%d)", error);
8830 	}
8831 
8832 done:
8833 	*retval = error;
8834 	if (error != 0) {
8835 		NECPLOG(LOG_ERR, "Add flow error (%d)", error);
8836 	}
8837 
8838 	if (allocated_add_request != NULL) {
8839 		kfree_data(allocated_add_request, buffer_size);
8840 	}
8841 
8842 	if (proc != PROC_NULL) {
8843 		proc_rele(proc);
8844 	}
8845 	return error;
8846 }
8847 
8848 #if SKYWALK
8849 
8850 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_request_nexus(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8851 necp_client_request_nexus(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8852 {
8853 	int error = 0;
8854 	struct necp_client *client = NULL;
8855 	uuid_t client_id;
8856 	struct necp_client_nexus_parameters parameters = {};
8857 	struct proc *proc = PROC_NULL;
8858 	const size_t buffer_size = uap->buffer_size;
8859 
8860 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
8861 		error = EINVAL;
8862 		goto done;
8863 	}
8864 
8865 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
8866 	if (error) {
8867 		NECPLOG(LOG_ERR, "necp_client_request_nexus copyin client_id error (%d)", error);
8868 		goto done;
8869 	}
8870 
8871 	NECP_FD_LOCK(fd_data);
8872 	pid_t pid = fd_data->proc_pid;
8873 	proc = proc_find(pid);
8874 	if (proc == PROC_NULL) {
8875 		NECP_FD_UNLOCK(fd_data);
8876 		NECPLOG(LOG_ERR, "necp_client_request_nexus process not found for pid %d error (%d)", pid, error);
8877 		error = ESRCH;
8878 		goto done;
8879 	}
8880 
8881 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
8882 	if (client == NULL) {
8883 		NECP_FD_UNLOCK(fd_data);
8884 		error = ENOENT;
8885 		goto done;
8886 	}
8887 
8888 	// Using REQUEST_NEXUS indicates that the client only supports one flow per client
8889 	client->legacy_client_is_flow = true;
8890 
8891 	necp_client_retain_locked(client);
8892 	necp_client_copy_parameters_locked(client, &parameters);
8893 
8894 	do {
8895 		size_t assigned_results_length = 0;
8896 		void * __sized_by(assigned_results_length) assigned_results = NULL;
8897 		uuid_t nexus_uuid;
8898 		uint32_t interface_index = 0;
8899 
8900 		// Validate that the nexus UUID is assigned
8901 		bool found_nexus = false;
8902 		for (u_int32_t option_i = 0; option_i < client->interface_option_count; option_i++) {
8903 			if (option_i < NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT) {
8904 				struct necp_client_interface_option *option = &client->interface_options[option_i];
8905 				if (!uuid_is_null(option->nexus_agent)) {
8906 					uuid_copy(nexus_uuid, option->nexus_agent);
8907 					interface_index = option->interface_index;
8908 					found_nexus = true;
8909 					break;
8910 				}
8911 			} else {
8912 				struct necp_client_interface_option *option = &client->extra_interface_options[option_i - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT];
8913 				if (!uuid_is_null(option->nexus_agent)) {
8914 					uuid_copy(nexus_uuid, option->nexus_agent);
8915 					interface_index = option->interface_index;
8916 					found_nexus = true;
8917 					break;
8918 				}
8919 			}
8920 		}
8921 
8922 		if (!found_nexus) {
8923 			NECP_CLIENT_UNLOCK(client);
8924 			NECP_FD_UNLOCK(fd_data);
8925 			necp_client_release(client);
8926 			// Break the loop
8927 			error = ENETDOWN;
8928 			goto done;
8929 		}
8930 
8931 		struct necp_client_flow_registration *new_registration = necp_client_create_flow_registration(fd_data, client);
8932 		if (new_registration == NULL) {
8933 			error = ENOMEM;
8934 			NECP_CLIENT_UNLOCK(client);
8935 			NECP_FD_UNLOCK(fd_data);
8936 			necp_client_release(client);
8937 			NECPLOG0(LOG_ERR, "Failed to allocate flow registration");
8938 			goto done;
8939 		}
8940 
8941 		new_registration->flags = (NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS | NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID);
8942 
8943 		necp_client_add_nexus_flow_if_needed(new_registration, nexus_uuid, interface_index);
8944 
8945 		// Note: Any clients using "request_nexus" are not flow-registration aware.
8946 		// Register the Client ID rather than the Registration ID with the nexus, since
8947 		// the client will send traffic based on the client ID.
8948 		error = netagent_client_message_with_params(nexus_uuid,
8949 		    ((new_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
8950 		    client->client_id :
8951 		    new_registration->registration_id),
8952 		    pid, client->agent_handle,
8953 		    NETAGENT_MESSAGE_TYPE_REQUEST_NEXUS,
8954 		    (struct necp_client_agent_parameters *)&parameters,
8955 		    &assigned_results, &assigned_results_length);
8956 		if (error) {
8957 			NECP_CLIENT_UNLOCK(client);
8958 			NECP_FD_UNLOCK(fd_data);
8959 			necp_client_release(client);
8960 			VERIFY(assigned_results == NULL);
8961 			VERIFY(assigned_results_length == 0);
8962 			NECPLOG(LOG_ERR, "netagent_client_message error (%d)", error);
8963 			goto done;
8964 		}
8965 
8966 		if (assigned_results != NULL) {
8967 			if (!necp_assign_client_result_locked(proc, fd_data, client, new_registration, nexus_uuid,
8968 			    assigned_results, assigned_results_length, false, false)) {
8969 				kfree_data_sized_by(assigned_results, assigned_results_length);
8970 			}
8971 		}
8972 
8973 		if (uap->buffer != 0 && buffer_size == sizeof(struct necp_stats_bufreq) &&
8974 		    necp_arena_initialize(fd_data, true) == 0) {
8975 			struct necp_stats_bufreq bufreq = {};
8976 			int copy_error = copyin(uap->buffer, &bufreq, buffer_size);
8977 			if (copy_error) {
8978 				NECPLOG(LOG_ERR, "necp_client_request_nexus copyin bufreq error (%d)", copy_error);
8979 			} else {
8980 				(void)necp_stats_initialize(fd_data, client, new_registration, &bufreq);
8981 				copy_error = copyout(&bufreq, uap->buffer, buffer_size);
8982 				if (copy_error != 0) {
8983 					NECPLOG(LOG_ERR, "necp_client_request_nexus copyout bufreq error (%d)", copy_error);
8984 				}
8985 			}
8986 		}
8987 	} while (false);
8988 
8989 	NECP_CLIENT_UNLOCK(client);
8990 	NECP_FD_UNLOCK(fd_data);
8991 
8992 	necp_client_release(client);
8993 
8994 done:
8995 	*retval = error;
8996 	if (error != 0) {
8997 		NECPLOG(LOG_ERR, "Request nexus error (%d)", error);
8998 	}
8999 
9000 	if (proc != PROC_NULL) {
9001 		proc_rele(proc);
9002 	}
9003 	return error;
9004 }
9005 #endif /* !SKYWALK */
9006 
9007 static void
necp_client_add_assertion(struct necp_client * client,uuid_t netagent_uuid)9008 necp_client_add_assertion(struct necp_client *client, uuid_t netagent_uuid)
9009 {
9010 	struct necp_client_assertion *new_assertion = NULL;
9011 
9012 	new_assertion = kalloc_type(struct necp_client_assertion,
9013 	    Z_WAITOK | Z_NOFAIL);
9014 
9015 	uuid_copy(new_assertion->asserted_netagent, netagent_uuid);
9016 
9017 	LIST_INSERT_HEAD(&client->assertion_list, new_assertion, assertion_chain);
9018 }
9019 
9020 static bool
necp_client_remove_assertion(struct necp_client * client,uuid_t netagent_uuid)9021 necp_client_remove_assertion(struct necp_client *client, uuid_t netagent_uuid)
9022 {
9023 	struct necp_client_assertion * __single found_assertion = NULL;
9024 	struct necp_client_assertion *search_assertion = NULL;
9025 	LIST_FOREACH(search_assertion, &client->assertion_list, assertion_chain) {
9026 		if (uuid_compare(search_assertion->asserted_netagent, netagent_uuid) == 0) {
9027 			found_assertion = search_assertion;
9028 			break;
9029 		}
9030 	}
9031 
9032 	if (found_assertion == NULL) {
9033 		NECPLOG0(LOG_ERR, "Netagent uuid not previously asserted");
9034 		return false;
9035 	}
9036 
9037 	LIST_REMOVE(found_assertion, assertion_chain);
9038 	kfree_type(struct necp_client_assertion, found_assertion);
9039 	return true;
9040 }
9041 
9042 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_agent_action(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)9043 necp_client_agent_action(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9044 {
9045 	int error = 0;
9046 	struct necp_client *client = NULL;
9047 	uuid_t client_id;
9048 	bool acted_on_agent = FALSE;
9049 	u_int8_t *parameters = NULL;
9050 	const size_t buffer_size = uap->buffer_size;
9051 
9052 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
9053 	    buffer_size == 0 || uap->buffer == 0) {
9054 		NECPLOG0(LOG_ERR, "necp_client_agent_action invalid parameters");
9055 		error = EINVAL;
9056 		goto done;
9057 	}
9058 
9059 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
9060 	if (error) {
9061 		NECPLOG(LOG_ERR, "necp_client_agent_action copyin client_id error (%d)", error);
9062 		goto done;
9063 	}
9064 
9065 	if (buffer_size > NECP_MAX_AGENT_ACTION_SIZE) {
9066 		NECPLOG(LOG_ERR, "necp_client_agent_action invalid buffer size (>%u)", NECP_MAX_AGENT_ACTION_SIZE);
9067 		error = EINVAL;
9068 		goto done;
9069 	}
9070 
9071 	parameters = (u_int8_t *)kalloc_data(buffer_size, Z_WAITOK | Z_ZERO);
9072 	if (parameters == NULL) {
9073 		error = ENOMEM;
9074 		goto done;
9075 	}
9076 
9077 	error = copyin(uap->buffer, parameters, buffer_size);
9078 	if (error) {
9079 		NECPLOG(LOG_ERR, "necp_client_agent_action parameters copyin error (%d)", error);
9080 		goto done;
9081 	}
9082 
9083 	NECP_FD_LOCK(fd_data);
9084 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
9085 	if (client != NULL) {
9086 		size_t offset = 0;
9087 		while ((offset + sizeof(struct necp_tlv_header)) <= buffer_size) {
9088 			u_int8_t type = necp_buffer_get_tlv_type(parameters, buffer_size, offset);
9089 			u_int32_t length = necp_buffer_get_tlv_length(parameters, buffer_size, offset);
9090 
9091 			if (length > (buffer_size - (offset + sizeof(struct necp_tlv_header)))) {
9092 				// If the length is larger than what can fit in the remaining parameters size, bail
9093 				NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
9094 				break;
9095 			}
9096 
9097 			if (length >= sizeof(uuid_t)) {
9098 				u_int8_t * __indexable value = necp_buffer_get_tlv_value(parameters, buffer_size, offset, NULL);
9099 				if (value == NULL) {
9100 					NECPLOG0(LOG_ERR, "Invalid TLV value");
9101 					break;
9102 				}
9103 				if (type == NECP_CLIENT_PARAMETER_TRIGGER_AGENT ||
9104 				    type == NECP_CLIENT_PARAMETER_ASSERT_AGENT ||
9105 				    type == NECP_CLIENT_PARAMETER_UNASSERT_AGENT) {
9106 					uuid_t agent_uuid;
9107 					uuid_copy(agent_uuid, value);
9108 					u_int8_t netagent_message_type = 0;
9109 					if (type == NECP_CLIENT_PARAMETER_TRIGGER_AGENT) {
9110 						netagent_message_type = NETAGENT_MESSAGE_TYPE_CLIENT_TRIGGER;
9111 					} else if (type == NECP_CLIENT_PARAMETER_ASSERT_AGENT) {
9112 						netagent_message_type = NETAGENT_MESSAGE_TYPE_CLIENT_ASSERT;
9113 					} else if (type == NECP_CLIENT_PARAMETER_UNASSERT_AGENT) {
9114 						netagent_message_type = NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT;
9115 					}
9116 
9117 					// Before unasserting, verify that the assertion was already taken
9118 					if (type == NECP_CLIENT_PARAMETER_UNASSERT_AGENT) {
9119 						if (!necp_client_remove_assertion(client, agent_uuid)) {
9120 							error = ENOENT;
9121 							break;
9122 						}
9123 					}
9124 
9125 					struct necp_client_nexus_parameters parsed_parameters = {};
9126 					necp_client_copy_parameters_locked(client, &parsed_parameters);
9127 					size_t dummy_length = 0;
9128 					void * __sized_by(dummy_length) dummy_results = NULL;
9129 
9130 					error = netagent_client_message_with_params(agent_uuid,
9131 					    client_id,
9132 					    fd_data->proc_pid,
9133 					    client->agent_handle,
9134 					    netagent_message_type,
9135 					    (struct necp_client_agent_parameters *)&parsed_parameters,
9136 					    &dummy_results, &dummy_length);
9137 					if (error == 0) {
9138 						acted_on_agent = TRUE;
9139 					} else {
9140 						break;
9141 					}
9142 
9143 					// Only save the assertion if the action succeeded
9144 					if (type == NECP_CLIENT_PARAMETER_ASSERT_AGENT) {
9145 						necp_client_add_assertion(client, agent_uuid);
9146 					}
9147 				} else if (type == NECP_CLIENT_PARAMETER_AGENT_ADD_GROUP_MEMBERS ||
9148 				    type == NECP_CLIENT_PARAMETER_AGENT_REMOVE_GROUP_MEMBERS) {
9149 					uuid_t agent_uuid;
9150 					uuid_copy(agent_uuid, value);
9151 					u_int8_t netagent_message_type = 0;
9152 					if (type == NECP_CLIENT_PARAMETER_AGENT_ADD_GROUP_MEMBERS) {
9153 						netagent_message_type = NETAGENT_MESSAGE_TYPE_ADD_GROUP_MEMBERS;
9154 					} else if (type == NECP_CLIENT_PARAMETER_AGENT_REMOVE_GROUP_MEMBERS) {
9155 						netagent_message_type = NETAGENT_MESSAGE_TYPE_REMOVE_GROUP_MEMBERS;
9156 					}
9157 
9158 					struct necp_client_group_members group_members = {};
9159 					group_members.group_members_length = (length - sizeof(uuid_t));
9160 					group_members.group_members = (value + sizeof(uuid_t));
9161 					size_t dummy_length = 0;
9162 					void * __sized_by(dummy_length) dummy_results = NULL;
9163 					error = netagent_client_message_with_params(agent_uuid,
9164 					    client_id,
9165 					    fd_data->proc_pid,
9166 					    client->agent_handle,
9167 					    netagent_message_type,
9168 					    (struct necp_client_agent_parameters *)&group_members,
9169 					    &dummy_results, &dummy_length);
9170 					if (error == 0) {
9171 						acted_on_agent = TRUE;
9172 					} else {
9173 						break;
9174 					}
9175 				} else if (type == NECP_CLIENT_PARAMETER_REPORT_AGENT_ERROR) {
9176 					uuid_t agent_uuid;
9177 					uuid_copy(agent_uuid, value);
9178 					struct necp_client_agent_parameters agent_params = {};
9179 					if ((length - sizeof(uuid_t)) >= sizeof(agent_params.u.error.error)) {
9180 						memcpy(&agent_params.u.error.error,
9181 						    (value + sizeof(uuid_t)),
9182 						    sizeof(agent_params.u.error.error));
9183 					}
9184 					bool agent_reported = false;
9185 					for (int agent_i = 0; agent_i < NECP_FD_REPORTED_AGENT_COUNT; agent_i++) {
9186 						if (uuid_compare(agent_uuid, fd_data->reported_agents.agent_uuid[agent_i]) == 0) {
9187 							// Found a match, already reported
9188 							agent_reported = true;
9189 							break;
9190 						}
9191 					}
9192 					agent_params.u.error.force_report = !agent_reported;
9193 					if (!agent_reported) {
9194 						// Save this agent as having been reported
9195 						bool saved_agent_uuid = false;
9196 						for (int agent_i = 0; agent_i < NECP_FD_REPORTED_AGENT_COUNT; agent_i++) {
9197 							if (uuid_is_null(fd_data->reported_agents.agent_uuid[agent_i])) {
9198 								uuid_copy(fd_data->reported_agents.agent_uuid[agent_i], agent_uuid);
9199 								saved_agent_uuid = true;
9200 								break;
9201 							}
9202 						}
9203 						if (!saved_agent_uuid) {
9204 							// Reported agent UUIDs full, move over and insert at the end
9205 							for (int agent_i = 0; agent_i < NECP_FD_REPORTED_AGENT_COUNT; agent_i++) {
9206 								if (agent_i + 1 < NECP_FD_REPORTED_AGENT_COUNT) {
9207 									uuid_copy(fd_data->reported_agents.agent_uuid[agent_i], fd_data->reported_agents.agent_uuid[agent_i + 1]);
9208 								} else {
9209 									uuid_copy(fd_data->reported_agents.agent_uuid[agent_i], agent_uuid);
9210 								}
9211 							}
9212 						}
9213 					}
9214 					size_t dummy_length = 0;
9215 					void * __sized_by(dummy_length) dummy_results = NULL;
9216 					error = netagent_client_message_with_params(agent_uuid,
9217 					    client_id,
9218 					    fd_data->proc_pid,
9219 					    client->agent_handle,
9220 					    NETAGENT_MESSAGE_TYPE_CLIENT_ERROR,
9221 					    &agent_params,
9222 					    &dummy_results, &dummy_length);
9223 					if (error == 0) {
9224 						acted_on_agent = TRUE;
9225 					} else {
9226 						break;
9227 					}
9228 				}
9229 			}
9230 
9231 			offset += sizeof(struct necp_tlv_header) + length;
9232 		}
9233 
9234 		NECP_CLIENT_UNLOCK(client);
9235 	}
9236 	NECP_FD_UNLOCK(fd_data);
9237 
9238 	if (!acted_on_agent &&
9239 	    error == 0) {
9240 		error = ENOENT;
9241 	}
9242 done:
9243 	*retval = error;
9244 	if (parameters != NULL) {
9245 		kfree_data(parameters, buffer_size);
9246 		parameters = NULL;
9247 	}
9248 
9249 	return error;
9250 }
9251 
9252 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)9253 necp_client_copy_agent(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9254 {
9255 	int error = 0;
9256 	uuid_t agent_uuid;
9257 	const size_t buffer_size = uap->buffer_size;
9258 
9259 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
9260 	    buffer_size == 0 || uap->buffer == 0) {
9261 		NECPLOG0(LOG_ERR, "necp_client_copy_agent bad input");
9262 		error = EINVAL;
9263 		goto done;
9264 	}
9265 
9266 	error = copyin(uap->client_id, agent_uuid, sizeof(uuid_t));
9267 	if (error) {
9268 		NECPLOG(LOG_ERR, "necp_client_copy_agent copyin agent_uuid error (%d)", error);
9269 		goto done;
9270 	}
9271 
9272 	error = netagent_copyout(agent_uuid, uap->buffer, buffer_size);
9273 	if (error) {
9274 		// netagent_copyout already logs appropriate errors
9275 		goto done;
9276 	}
9277 done:
9278 	*retval = error;
9279 
9280 	return error;
9281 }
9282 
9283 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_agent_use(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)9284 necp_client_agent_use(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9285 {
9286 	int error = 0;
9287 	struct necp_client *client = NULL;
9288 	uuid_t client_id;
9289 	struct necp_agent_use_parameters parameters = {};
9290 	const size_t buffer_size = uap->buffer_size;
9291 
9292 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
9293 	    buffer_size != sizeof(parameters) || uap->buffer == 0) {
9294 		error = EINVAL;
9295 		goto done;
9296 	}
9297 
9298 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
9299 	if (error) {
9300 		NECPLOG(LOG_ERR, "Copyin client_id error (%d)", error);
9301 		goto done;
9302 	}
9303 
9304 	error = copyin(uap->buffer, &parameters, buffer_size);
9305 	if (error) {
9306 		NECPLOG(LOG_ERR, "Parameters copyin error (%d)", error);
9307 		goto done;
9308 	}
9309 
9310 	NECP_FD_LOCK(fd_data);
9311 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
9312 	if (client != NULL) {
9313 		error = netagent_use(parameters.agent_uuid, &parameters.out_use_count);
9314 		NECP_CLIENT_UNLOCK(client);
9315 	} else {
9316 		error = ENOENT;
9317 	}
9318 
9319 	NECP_FD_UNLOCK(fd_data);
9320 
9321 	if (error == 0) {
9322 		error = copyout(&parameters, uap->buffer, buffer_size);
9323 		if (error) {
9324 			NECPLOG(LOG_ERR, "Parameters copyout error (%d)", error);
9325 			goto done;
9326 		}
9327 	}
9328 
9329 done:
9330 	*retval = error;
9331 
9332 	return error;
9333 }
9334 
9335 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)9336 necp_client_acquire_agent_token(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9337 {
9338 	int error = 0;
9339 	uuid_t agent_uuid = {};
9340 	const size_t buffer_size = uap->buffer_size;
9341 
9342 	*retval = 0;
9343 
9344 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
9345 	    buffer_size == 0 || uap->buffer == 0) {
9346 		NECPLOG0(LOG_ERR, "necp_client_copy_agent bad input");
9347 		error = EINVAL;
9348 		goto done;
9349 	}
9350 
9351 	error = copyin(uap->client_id, agent_uuid, sizeof(uuid_t));
9352 	if (error) {
9353 		NECPLOG(LOG_ERR, "necp_client_copy_agent copyin agent_uuid error (%d)", error);
9354 		goto done;
9355 	}
9356 
9357 	error = netagent_acquire_token(agent_uuid, uap->buffer, buffer_size, retval);
9358 done:
9359 	return error;
9360 }
9361 
9362 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)9363 necp_client_copy_interface(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9364 {
9365 	int error = 0;
9366 	u_int32_t interface_index = 0;
9367 	struct necp_interface_details interface_details = {};
9368 
9369 	if (uap->client_id == 0 || uap->client_id_len != sizeof(u_int32_t) ||
9370 	    uap->buffer_size < sizeof(interface_details) ||
9371 	    uap->buffer == 0) {
9372 		NECPLOG0(LOG_ERR, "necp_client_copy_interface bad input");
9373 		error = EINVAL;
9374 		goto done;
9375 	}
9376 
9377 	error = copyin(uap->client_id, &interface_index, sizeof(u_int32_t));
9378 	if (error) {
9379 		NECPLOG(LOG_ERR, "necp_client_copy_interface copyin interface_index error (%d)", error);
9380 		goto done;
9381 	}
9382 
9383 	if (interface_index == 0) {
9384 		error = ENOENT;
9385 		NECPLOG(LOG_ERR, "necp_client_copy_interface bad interface_index (%d)", interface_index);
9386 		goto done;
9387 	}
9388 
9389 	lck_mtx_lock(rnh_lock);
9390 	ifnet_head_lock_shared();
9391 	ifnet_t interface = NULL;
9392 	if (interface_index != IFSCOPE_NONE && interface_index <= (u_int32_t)if_index) {
9393 		interface = ifindex2ifnet[interface_index];
9394 	}
9395 
9396 	if (interface != NULL) {
9397 		if (interface->if_xname != NULL) {
9398 			strlcpy((char *)&interface_details.name, interface->if_xname, sizeof(interface_details.name));
9399 		}
9400 		interface_details.index = interface->if_index;
9401 		interface_details.generation = ifnet_get_generation(interface);
9402 		if (interface->if_delegated.ifp != NULL) {
9403 			interface_details.delegate_index = interface->if_delegated.ifp->if_index;
9404 		}
9405 		interface_details.functional_type = if_functional_type(interface, TRUE);
9406 		if (IFNET_IS_EXPENSIVE(interface)) {
9407 			interface_details.flags |= NECP_INTERFACE_FLAG_EXPENSIVE;
9408 		}
9409 		if (IFNET_IS_CONSTRAINED(interface)) {
9410 			interface_details.flags |= NECP_INTERFACE_FLAG_CONSTRAINED;
9411 		}
9412 		if (IFNET_IS_ULTRA_CONSTRAINED(interface)) {
9413 			interface_details.flags |= NECP_INTERFACE_FLAG_ULTRA_CONSTRAINED;
9414 		}
9415 		if ((interface->if_eflags & IFEF_TXSTART) == IFEF_TXSTART) {
9416 			interface_details.flags |= NECP_INTERFACE_FLAG_TXSTART;
9417 		}
9418 		if ((interface->if_eflags & IFEF_NOACKPRI) == IFEF_NOACKPRI) {
9419 			interface_details.flags |= NECP_INTERFACE_FLAG_NOACKPRI;
9420 		}
9421 		if ((interface->if_eflags & IFEF_3CA) == IFEF_3CA) {
9422 			interface_details.flags |= NECP_INTERFACE_FLAG_3CARRIERAGG;
9423 		}
9424 		if (IFNET_IS_LOW_POWER(interface)) {
9425 			interface_details.flags |= NECP_INTERFACE_FLAG_IS_LOW_POWER;
9426 		}
9427 		if (interface->if_xflags & IFXF_MPK_LOG) {
9428 			interface_details.flags |= NECP_INTERFACE_FLAG_MPK_LOG;
9429 		}
9430 		if (interface->if_flags & IFF_MULTICAST) {
9431 			interface_details.flags |= NECP_INTERFACE_FLAG_SUPPORTS_MULTICAST;
9432 		}
9433 		if (IS_INTF_CLAT46(interface)) {
9434 			interface_details.flags |= NECP_INTERFACE_FLAG_HAS_NAT64;
9435 		}
9436 		interface_details.mtu = interface->if_mtu;
9437 #if SKYWALK
9438 		fsw_get_tso_capabilities(interface, &interface_details.tso_max_segment_size_v4,
9439 		    &interface_details.tso_max_segment_size_v6);
9440 
9441 		interface_details.hwcsum_flags = interface->if_hwassist & IFNET_CHECKSUMF;
9442 #endif /* SKYWALK */
9443 
9444 		u_int8_t ipv4_signature_len = sizeof(interface_details.ipv4_signature.signature);
9445 		u_int16_t ipv4_signature_flags;
9446 		if (ifnet_get_netsignature(interface, AF_INET, &ipv4_signature_len, &ipv4_signature_flags,
9447 		    (u_int8_t *)&interface_details.ipv4_signature) != 0) {
9448 			ipv4_signature_len = 0;
9449 		}
9450 		interface_details.ipv4_signature.signature_len = ipv4_signature_len;
9451 
9452 		// Check for default scoped routes for IPv4 and IPv6
9453 		union necp_sockaddr_union default_address;
9454 		struct rtentry *v4Route = NULL;
9455 		memset(&default_address, 0, sizeof(default_address));
9456 		default_address.sa.sa_family = AF_INET;
9457 		default_address.sa.sa_len = sizeof(struct sockaddr_in);
9458 		v4Route = rtalloc1_scoped_locked(SA(&default_address), 0, 0,
9459 		    interface->if_index);
9460 		if (v4Route != NULL) {
9461 			if (v4Route->rt_ifp != NULL && !IS_INTF_CLAT46(v4Route->rt_ifp)) {
9462 				interface_details.flags |= NECP_INTERFACE_FLAG_IPV4_ROUTABLE;
9463 			}
9464 			rtfree_locked(v4Route);
9465 			v4Route = NULL;
9466 		}
9467 
9468 		struct rtentry *v6Route = NULL;
9469 		memset(&default_address, 0, sizeof(default_address));
9470 		default_address.sa.sa_family = AF_INET6;
9471 		default_address.sa.sa_len = sizeof(struct sockaddr_in6);
9472 		v6Route = rtalloc1_scoped_locked(SA(&default_address), 0, 0,
9473 		    interface->if_index);
9474 		if (v6Route != NULL) {
9475 			if (v6Route->rt_ifp != NULL) {
9476 				interface_details.flags |= NECP_INTERFACE_FLAG_IPV6_ROUTABLE;
9477 			}
9478 			rtfree_locked(v6Route);
9479 			v6Route = NULL;
9480 		}
9481 
9482 		u_int8_t ipv6_signature_len = sizeof(interface_details.ipv6_signature.signature);
9483 		u_int16_t ipv6_signature_flags;
9484 		if (ifnet_get_netsignature(interface, AF_INET6, &ipv6_signature_len, &ipv6_signature_flags,
9485 		    (u_int8_t *)&interface_details.ipv6_signature) != 0) {
9486 			ipv6_signature_len = 0;
9487 		}
9488 		interface_details.ipv6_signature.signature_len = ipv6_signature_len;
9489 
9490 		ifnet_lock_shared(interface);
9491 		struct ifaddr * __single ifa = NULL;
9492 		TAILQ_FOREACH(ifa, &interface->if_addrhead, ifa_link) {
9493 			IFA_LOCK(ifa);
9494 			if (ifa->ifa_addr->sa_family == AF_INET) {
9495 				interface_details.flags |= NECP_INTERFACE_FLAG_HAS_NETMASK;
9496 				interface_details.ipv4_netmask = (ifatoia(ifa))->ia_sockmask.sin_addr.s_addr;
9497 				if (interface->if_flags & IFF_BROADCAST) {
9498 					interface_details.flags |= NECP_INTERFACE_FLAG_HAS_BROADCAST;
9499 					interface_details.ipv4_broadcast = (ifatoia(ifa))->ia_broadaddr.sin_addr.s_addr;
9500 				}
9501 			}
9502 			IFA_UNLOCK(ifa);
9503 		}
9504 
9505 		interface_details.radio_type = interface->if_radio_type;
9506 		if (interface_details.radio_type == 0 && interface->if_delegated.ifp) {
9507 			interface_details.radio_type = interface->if_delegated.ifp->if_radio_type;
9508 		}
9509 		ifnet_lock_done(interface);
9510 	}
9511 
9512 	ifnet_head_done();
9513 	lck_mtx_unlock(rnh_lock);
9514 
9515 	// If the client is using an older version of the struct, copy that length
9516 	error = copyout(&interface_details, uap->buffer, sizeof(interface_details));
9517 	if (error) {
9518 		NECPLOG(LOG_ERR, "necp_client_copy_interface copyout error (%d)", error);
9519 		goto done;
9520 	}
9521 done:
9522 	*retval = error;
9523 
9524 	return error;
9525 }
9526 
9527 #if SKYWALK
9528 
9529 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)9530 necp_client_get_interface_address(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9531 {
9532 	int error = 0;
9533 	u_int32_t interface_index = IFSCOPE_NONE;
9534 	struct sockaddr_storage address = {};
9535 	const size_t buffer_size = uap->buffer_size;
9536 
9537 	if (uap->client_id == 0 || uap->client_id_len != sizeof(u_int32_t) ||
9538 	    buffer_size < sizeof(struct sockaddr_in) ||
9539 	    buffer_size > sizeof(struct sockaddr_storage) ||
9540 	    uap->buffer == 0) {
9541 		NECPLOG0(LOG_ERR, "necp_client_get_interface_address bad input");
9542 		error = EINVAL;
9543 		goto done;
9544 	}
9545 
9546 	error = copyin(uap->client_id, &interface_index, sizeof(u_int32_t));
9547 	if (error) {
9548 		NECPLOG(LOG_ERR, "necp_client_get_interface_address copyin interface_index error (%d)", error);
9549 		goto done;
9550 	}
9551 
9552 	if (interface_index == IFSCOPE_NONE) {
9553 		error = ENOENT;
9554 		NECPLOG(LOG_ERR, "necp_client_get_interface_address bad interface_index (%d)", interface_index);
9555 		goto done;
9556 	}
9557 
9558 	error = copyin(uap->buffer, &address, buffer_size);
9559 	if (error) {
9560 		NECPLOG(LOG_ERR, "necp_client_get_interface_address copyin address error (%d)", error);
9561 		goto done;
9562 	}
9563 
9564 	if (address.ss_family != AF_INET && address.ss_family != AF_INET6) {
9565 		error = EINVAL;
9566 		NECPLOG(LOG_ERR, "necp_client_get_interface_address invalid address family (%u)", address.ss_family);
9567 		goto done;
9568 	}
9569 
9570 	if (address.ss_len != buffer_size) {
9571 		error = EINVAL;
9572 		NECPLOG(LOG_ERR, "necp_client_get_interface_address invalid address length (%u)", address.ss_len);
9573 		goto done;
9574 	}
9575 
9576 	ifnet_head_lock_shared();
9577 	ifnet_t ifp = NULL;
9578 	if (interface_index != IFSCOPE_NONE && interface_index <= (u_int32_t)if_index) {
9579 		ifp = ifindex2ifnet[interface_index];
9580 	}
9581 	ifnet_head_done();
9582 	if (ifp == NULL) {
9583 		error = ENOENT;
9584 		NECPLOG0(LOG_ERR, "necp_client_get_interface_address no matching interface found");
9585 		goto done;
9586 	}
9587 
9588 	struct rtentry *rt = rtalloc1_scoped(SA(&address), 0, 0, interface_index);
9589 	if (rt == NULL) {
9590 		error = EINVAL;
9591 		NECPLOG0(LOG_ERR, "necp_client_get_interface_address route lookup failed");
9592 		goto done;
9593 	}
9594 
9595 	uint32_t gencount = 0;
9596 	struct sockaddr_storage local_address = {};
9597 	error = flow_route_select_laddr((union sockaddr_in_4_6 *)&local_address,
9598 	    (union sockaddr_in_4_6 *)&address, ifp, rt, &gencount, 1);
9599 	rtfree(rt);
9600 	rt = NULL;
9601 
9602 	if (error) {
9603 		NECPLOG(LOG_ERR, "necp_client_get_interface_address local address selection failed (%d)", error);
9604 		goto done;
9605 	}
9606 
9607 	if (local_address.ss_len > buffer_size) {
9608 		error = EMSGSIZE;
9609 		NECPLOG(LOG_ERR, "necp_client_get_interface_address local address too long for buffer (%u)",
9610 		    local_address.ss_len);
9611 		goto done;
9612 	}
9613 
9614 	error = copyout(&local_address, uap->buffer, local_address.ss_len);
9615 	if (error) {
9616 		NECPLOG(LOG_ERR, "necp_client_get_interface_address copyout error (%d)", error);
9617 		goto done;
9618 	}
9619 done:
9620 	*retval = error;
9621 
9622 	return error;
9623 }
9624 
9625 extern const char *proc_name_address(void *p);
9626 
9627 int
necp_stats_ctor(struct skmem_obj_info * oi,struct skmem_obj_info * oim,void * arg,uint32_t skmflag)9628 necp_stats_ctor(struct skmem_obj_info *oi, struct skmem_obj_info *oim,
9629     void *arg, uint32_t skmflag)
9630 {
9631 #pragma unused(arg, skmflag)
9632 	struct necp_all_kstats * __single kstats = SKMEM_OBJ_ADDR(oi);
9633 
9634 	ASSERT(oim != NULL && SKMEM_OBJ_ADDR(oim) != NULL);
9635 	ASSERT(SKMEM_OBJ_SIZE(oi) == SKMEM_OBJ_SIZE(oim));
9636 
9637 	kstats->necp_stats_ustats = SKMEM_OBJ_ADDR(oim);
9638 
9639 	return 0;
9640 }
9641 
9642 int
necp_stats_dtor(void * addr,void * arg)9643 necp_stats_dtor(void *addr, void *arg)
9644 {
9645 #pragma unused(addr, arg)
9646 	struct necp_all_kstats * __single kstats = addr;
9647 
9648 	kstats->necp_stats_ustats = NULL;
9649 
9650 	return 0;
9651 }
9652 
9653 static void
necp_fd_insert_stats_arena(struct necp_fd_data * fd_data,struct necp_arena_info * nai)9654 necp_fd_insert_stats_arena(struct necp_fd_data *fd_data, struct necp_arena_info *nai)
9655 {
9656 	NECP_FD_ASSERT_LOCKED(fd_data);
9657 	VERIFY(!(nai->nai_flags & NAIF_ATTACHED));
9658 	VERIFY(nai->nai_chain.le_next == NULL && nai->nai_chain.le_prev == NULL);
9659 
9660 	LIST_INSERT_HEAD(&fd_data->stats_arena_list, nai, nai_chain);
9661 	nai->nai_flags |= NAIF_ATTACHED;
9662 	necp_arena_info_retain(nai);    // for the list
9663 }
9664 
9665 static void
necp_fd_remove_stats_arena(struct necp_fd_data * fd_data,struct necp_arena_info * nai)9666 necp_fd_remove_stats_arena(struct necp_fd_data *fd_data, struct necp_arena_info *nai)
9667 {
9668 #pragma unused(fd_data)
9669 	NECP_FD_ASSERT_LOCKED(fd_data);
9670 	VERIFY(nai->nai_flags & NAIF_ATTACHED);
9671 	VERIFY(nai->nai_use_count >= 1);
9672 
9673 	LIST_REMOVE(nai, nai_chain);
9674 	nai->nai_flags &= ~NAIF_ATTACHED;
9675 	nai->nai_chain.le_next = NULL;
9676 	nai->nai_chain.le_prev = NULL;
9677 	necp_arena_info_release(nai);   // for the list
9678 }
9679 
9680 static struct necp_arena_info *
necp_fd_mredirect_stats_arena(struct necp_fd_data * fd_data,struct proc * proc)9681 necp_fd_mredirect_stats_arena(struct necp_fd_data *fd_data, struct proc *proc)
9682 {
9683 	struct necp_arena_info *nai, *nai_ret = NULL;
9684 
9685 	NECP_FD_ASSERT_LOCKED(fd_data);
9686 
9687 	// Redirect currently-active stats arena and remove it from the active state;
9688 	// upon process resumption, new flow request would trigger the creation of
9689 	// another active arena.
9690 	if ((nai = fd_data->stats_arena_active) != NULL) {
9691 		boolean_t need_defunct = FALSE;
9692 
9693 		ASSERT(!(nai->nai_flags & (NAIF_REDIRECT | NAIF_DEFUNCT)));
9694 		VERIFY(nai->nai_use_count >= 2);
9695 		ASSERT(nai->nai_arena != NULL);
9696 		ASSERT(nai->nai_mmap.ami_mapref != NULL);
9697 
9698 		int err = skmem_arena_mredirect(nai->nai_arena, &nai->nai_mmap, proc, &need_defunct);
9699 		VERIFY(err == 0);
9700 		// must be TRUE since we don't mmap the arena more than once
9701 		VERIFY(need_defunct == TRUE);
9702 
9703 		nai->nai_flags |= NAIF_REDIRECT;
9704 		nai_ret = nai;  // return to caller
9705 
9706 		necp_arena_info_release(nai);   // for fd_data
9707 		fd_data->stats_arena_active = nai = NULL;
9708 	}
9709 
9710 #if (DEVELOPMENT || DEBUG)
9711 	// make sure this list now contains nothing but redirected/defunct arenas
9712 	LIST_FOREACH(nai, &fd_data->stats_arena_list, nai_chain) {
9713 		ASSERT(nai->nai_use_count >= 1);
9714 		ASSERT(nai->nai_flags & (NAIF_REDIRECT | NAIF_DEFUNCT));
9715 	}
9716 #endif /* (DEVELOPMENT || DEBUG) */
9717 
9718 	return nai_ret;
9719 }
9720 
9721 static void
necp_arena_info_retain(struct necp_arena_info * nai)9722 necp_arena_info_retain(struct necp_arena_info *nai)
9723 {
9724 	nai->nai_use_count++;
9725 	VERIFY(nai->nai_use_count != 0);
9726 }
9727 
9728 static void
necp_arena_info_release(struct necp_arena_info * nai)9729 necp_arena_info_release(struct necp_arena_info *nai)
9730 {
9731 	VERIFY(nai->nai_use_count > 0);
9732 	if (--nai->nai_use_count == 0) {
9733 		necp_arena_info_free(nai);
9734 	}
9735 }
9736 
9737 static struct necp_arena_info *
necp_arena_info_alloc(void)9738 necp_arena_info_alloc(void)
9739 {
9740 	return zalloc_flags(necp_arena_info_zone, Z_WAITOK | Z_ZERO);
9741 }
9742 
9743 static void
necp_arena_info_free(struct necp_arena_info * nai)9744 necp_arena_info_free(struct necp_arena_info *nai)
9745 {
9746 	VERIFY(nai->nai_chain.le_next == NULL && nai->nai_chain.le_prev == NULL);
9747 	VERIFY(nai->nai_use_count == 0);
9748 
9749 	// NOTE: destroying the arena requires that all outstanding objects
9750 	// that were allocated have been freed, else it will assert.
9751 	if (nai->nai_arena != NULL) {
9752 		skmem_arena_munmap(nai->nai_arena, &nai->nai_mmap);
9753 		skmem_arena_release(nai->nai_arena);
9754 		OSDecrementAtomic(&necp_arena_count);
9755 		nai->nai_arena = NULL;
9756 		nai->nai_roff = 0;
9757 	}
9758 
9759 	ASSERT(nai->nai_arena == NULL);
9760 	ASSERT(nai->nai_mmap.ami_mapref == NULL);
9761 	ASSERT(nai->nai_mmap.ami_arena == NULL);
9762 	ASSERT(nai->nai_mmap.ami_maptask == TASK_NULL);
9763 
9764 	zfree(necp_arena_info_zone, nai);
9765 }
9766 
9767 static int
necp_arena_create(struct necp_fd_data * fd_data,size_t obj_size,size_t obj_cnt,struct proc * p)9768 necp_arena_create(struct necp_fd_data *fd_data, size_t obj_size, size_t obj_cnt, struct proc *p)
9769 {
9770 	struct skmem_region_params srp_ustats = {};
9771 	struct skmem_region_params srp_kstats = {};
9772 	struct necp_arena_info *nai;
9773 	char name[32];
9774 	const char *__null_terminated name_ptr = NULL;
9775 	int error = 0;
9776 
9777 	NECP_FD_ASSERT_LOCKED(fd_data);
9778 	ASSERT(fd_data->stats_arena_active == NULL);
9779 	ASSERT(p != PROC_NULL);
9780 	ASSERT(proc_pid(p) == fd_data->proc_pid);
9781 
9782 	// inherit the default parameters for the stats region
9783 	srp_ustats = *skmem_get_default(SKMEM_REGION_USTATS);
9784 	srp_kstats = *skmem_get_default(SKMEM_REGION_KSTATS);
9785 
9786 	// enable multi-segment mode
9787 	srp_ustats.srp_cflags &= ~SKMEM_REGION_CR_MONOLITHIC;
9788 	srp_kstats.srp_cflags &= ~SKMEM_REGION_CR_MONOLITHIC;
9789 
9790 	// configure and adjust the region parameters
9791 	srp_ustats.srp_r_obj_cnt = srp_kstats.srp_r_obj_cnt = obj_cnt;
9792 	srp_ustats.srp_r_obj_size = srp_kstats.srp_r_obj_size = obj_size;
9793 	skmem_region_params_config(&srp_ustats);
9794 	skmem_region_params_config(&srp_kstats);
9795 
9796 	nai = necp_arena_info_alloc();
9797 
9798 	nai->nai_proc_pid = fd_data->proc_pid;
9799 	name_ptr = tsnprintf(name, sizeof(name), "stats-%u.%s.%d", fd_data->stats_arena_gencnt, proc_name_address(p), fd_data->proc_pid);
9800 	nai->nai_arena = skmem_arena_create_for_necp(name_ptr, &srp_ustats, &srp_kstats, &error);
9801 	ASSERT(nai->nai_arena != NULL || error != 0);
9802 	if (error != 0) {
9803 		NECPLOG(LOG_ERR, "failed to create stats arena for pid %d\n", fd_data->proc_pid);
9804 	} else {
9805 		OSIncrementAtomic(&necp_arena_count);
9806 
9807 		// Get region offsets from base of mmap span; the arena
9808 		// doesn't need to be mmap'd at this point, since we simply
9809 		// compute the relative offset.
9810 		nai->nai_roff = skmem_arena_get_region_offset(nai->nai_arena, SKMEM_REGION_USTATS);
9811 
9812 		// map to the task/process; upon success, the base address of the region
9813 		// will be returned in nai_mmap.ami_mapaddr; this can be communicated to
9814 		// the process.
9815 		error = skmem_arena_mmap(nai->nai_arena, p, &nai->nai_mmap);
9816 		if (error != 0) {
9817 			NECPLOG(LOG_ERR, "failed to map stats arena for pid %d\n", fd_data->proc_pid);
9818 		}
9819 	}
9820 
9821 	if (error == 0) {
9822 		fd_data->stats_arena_active = nai;
9823 		necp_arena_info_retain(nai);    // for fd_data
9824 		necp_fd_insert_stats_arena(fd_data, nai);
9825 		++fd_data->stats_arena_gencnt;
9826 	} else {
9827 		necp_arena_info_free(nai);
9828 	}
9829 
9830 	return error;
9831 }
9832 
9833 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)9834 necp_arena_stats_obj_alloc(struct necp_fd_data *fd_data,
9835     mach_vm_offset_t *off,
9836     struct necp_arena_info **stats_arena,
9837     void **kstats_kaddr,
9838     boolean_t cansleep)
9839 {
9840 	struct skmem_cache *kstats_cp = NULL;
9841 	struct skmem_obj_info kstats_oi = {};
9842 	uint32_t ustats_obj_sz = 0;
9843 	void *__sized_by(ustats_obj_sz) ustats_obj = NULL;
9844 	uint32_t kstats_obj_sz = 0;
9845 	void *__sized_by(kstats_obj_sz) kstats_obj = NULL;
9846 	void * __indexable kstats_obj_tmp = NULL;
9847 	struct necp_all_kstats * __single kstats = NULL;
9848 
9849 	ASSERT(off != NULL);
9850 	ASSERT(stats_arena != NULL && *stats_arena == NULL);
9851 	ASSERT(kstats_kaddr != NULL && *kstats_kaddr == NULL);
9852 
9853 	NECP_FD_ASSERT_LOCKED(fd_data);
9854 	ASSERT(fd_data->stats_arena_active != NULL);
9855 	ASSERT(fd_data->stats_arena_active->nai_arena != NULL);
9856 
9857 	kstats_cp = skmem_arena_necp(fd_data->stats_arena_active->nai_arena)->arc_kstats_cache;
9858 	if ((kstats_obj_tmp = skmem_cache_alloc(kstats_cp, (cansleep ? SKMEM_SLEEP : SKMEM_NOSLEEP))) == NULL) {
9859 		return ENOMEM;
9860 	}
9861 	skmem_cache_get_obj_info(kstats_cp, kstats_obj_tmp, &kstats_oi, NULL);
9862 	ASSERT(SKMEM_OBJ_SIZE(&kstats_oi) >= sizeof(struct necp_all_stats));
9863 	kstats_obj = kstats_obj_tmp;
9864 	kstats_obj_sz = SKMEM_OBJ_SIZE(&kstats_oi);
9865 
9866 	kstats = (struct necp_all_kstats*)kstats_obj;
9867 	ustats_obj = __unsafe_forge_bidi_indexable(uint8_t *, kstats->necp_stats_ustats, kstats_obj_sz);
9868 	ustats_obj_sz = kstats_obj_sz;
9869 
9870 	bzero(ustats_obj, ustats_obj_sz);
9871 	bzero(&kstats->necp_stats_comm, sizeof(struct necp_all_stats));
9872 	*stats_arena = fd_data->stats_arena_active;
9873 	*kstats_kaddr = kstats_obj;
9874 	// kstats and ustats are mirrored and have the same offset
9875 	*off = fd_data->stats_arena_active->nai_roff + SKMEM_OBJ_ROFF(&kstats_oi);
9876 
9877 	return 0;
9878 }
9879 
9880 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)9881 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)
9882 {
9883 #pragma unused(fd_data)
9884 	NECP_FD_ASSERT_LOCKED(fd_data);
9885 
9886 	ASSERT(stats_arena != NULL);
9887 	ASSERT(stats_arena->nai_arena != NULL);
9888 	ASSERT(kstats_kaddr != NULL && *kstats_kaddr != NULL);
9889 	ASSERT(ustats_uaddr != NULL);
9890 
9891 	skmem_cache_free(skmem_arena_necp(stats_arena->nai_arena)->arc_kstats_cache, *kstats_kaddr);
9892 	*kstats_kaddr = NULL;
9893 	*ustats_uaddr = 0;
9894 }
9895 
9896 // This routine returns the KVA of the sysctls object, as well as the
9897 // offset of that object relative to the mmap base address for the
9898 // task/process.
9899 static void *
necp_arena_sysctls_obj(struct necp_fd_data * fd_data,mach_vm_offset_t * off,size_t * size)9900 necp_arena_sysctls_obj(struct necp_fd_data *fd_data, mach_vm_offset_t *off, size_t *size)
9901 {
9902 	void * __single objaddr;
9903 
9904 	NECP_FD_ASSERT_LOCKED(fd_data);
9905 	ASSERT(fd_data->sysctl_arena != NULL);
9906 
9907 	// kernel virtual address of the sysctls object
9908 	objaddr = skmem_arena_system_sysctls_obj_addr(fd_data->sysctl_arena);
9909 	ASSERT(objaddr != NULL);
9910 
9911 	// Return the relative offset of the sysctls object; there is
9912 	// only 1 object in the entire sysctls region, and therefore the
9913 	// object's offset is simply the region's offset in the arena.
9914 	// (sysctl_mmap.ami_mapaddr + offset) is the address of this object
9915 	// in the task/process.
9916 	if (off != NULL) {
9917 		*off = fd_data->system_sysctls_roff;
9918 	}
9919 
9920 	if (size != NULL) {
9921 		*size = skmem_arena_system_sysctls_obj_size(fd_data->sysctl_arena);
9922 		ASSERT(*size != 0);
9923 	}
9924 
9925 	return objaddr;
9926 }
9927 
9928 static void
necp_stats_arenas_destroy(struct necp_fd_data * fd_data,boolean_t closing)9929 necp_stats_arenas_destroy(struct necp_fd_data *fd_data, boolean_t closing)
9930 {
9931 	struct necp_arena_info *nai, *nai_tmp;
9932 
9933 	NECP_FD_ASSERT_LOCKED(fd_data);
9934 
9935 	// If reaping (not closing), release reference only for idle active arena; the reference
9936 	// count must be 2 by now, when it's not being referred to by any clients/flows.
9937 	if ((nai = fd_data->stats_arena_active) != NULL && (closing || nai->nai_use_count == 2)) {
9938 		VERIFY(nai->nai_use_count >= 2);
9939 		necp_arena_info_release(nai);   // for fd_data
9940 		fd_data->stats_arena_active = NULL;
9941 	}
9942 
9943 	// clean up any defunct arenas left in the list
9944 	LIST_FOREACH_SAFE(nai, &fd_data->stats_arena_list, nai_chain, nai_tmp) {
9945 		// If reaping, release reference if the list holds the last one
9946 		if (closing || nai->nai_use_count == 1) {
9947 			VERIFY(nai->nai_use_count >= 1);
9948 			// callee unchains nai (and may free it)
9949 			necp_fd_remove_stats_arena(fd_data, nai);
9950 		}
9951 	}
9952 }
9953 
9954 static void
necp_sysctl_arena_destroy(struct necp_fd_data * fd_data)9955 necp_sysctl_arena_destroy(struct necp_fd_data *fd_data)
9956 {
9957 	NECP_FD_ASSERT_LOCKED(fd_data);
9958 
9959 	// NOTE: destroying the arena requires that all outstanding objects
9960 	// that were allocated have been freed, else it will assert.
9961 	if (fd_data->sysctl_arena != NULL) {
9962 		skmem_arena_munmap(fd_data->sysctl_arena, &fd_data->sysctl_mmap);
9963 		skmem_arena_release(fd_data->sysctl_arena);
9964 		OSDecrementAtomic(&necp_sysctl_arena_count);
9965 		fd_data->sysctl_arena = NULL;
9966 		fd_data->system_sysctls_roff = 0;
9967 	}
9968 }
9969 
9970 static int
necp_arena_initialize(struct necp_fd_data * fd_data,bool locked)9971 necp_arena_initialize(struct necp_fd_data *fd_data, bool locked)
9972 {
9973 	int error = 0;
9974 	size_t stats_obj_size = MAX(sizeof(struct necp_all_stats), sizeof(struct necp_all_kstats));
9975 
9976 	if (!locked) {
9977 		NECP_FD_LOCK(fd_data);
9978 	}
9979 	if (fd_data->stats_arena_active == NULL) {
9980 		error = necp_arena_create(fd_data, stats_obj_size,
9981 		    NECP_MAX_PER_PROCESS_CLIENT_STATISTICS_STRUCTS,
9982 		    current_proc());
9983 	}
9984 	if (!locked) {
9985 		NECP_FD_UNLOCK(fd_data);
9986 	}
9987 
9988 	return error;
9989 }
9990 
9991 static int
necp_sysctl_arena_initialize(struct necp_fd_data * fd_data,bool locked)9992 necp_sysctl_arena_initialize(struct necp_fd_data *fd_data, bool locked)
9993 {
9994 	int error = 0;
9995 
9996 	if (!locked) {
9997 		NECP_FD_LOCK(fd_data);
9998 	}
9999 
10000 	NECP_FD_ASSERT_LOCKED(fd_data);
10001 
10002 	if (fd_data->sysctl_arena == NULL) {
10003 		char name[32];
10004 		const char *__null_terminated name_ptr = NULL;
10005 		struct proc *p = current_proc();
10006 
10007 		ASSERT(p != PROC_NULL);
10008 		ASSERT(proc_pid(p) == fd_data->proc_pid);
10009 
10010 		name_ptr = tsnprintf(name, sizeof(name), "sysctl.%s.%d", proc_name_address(p), fd_data->proc_pid);
10011 		fd_data->sysctl_arena = skmem_arena_create_for_system(name_ptr, &error);
10012 		ASSERT(fd_data->sysctl_arena != NULL || error != 0);
10013 		if (error != 0) {
10014 			NECPLOG(LOG_ERR, "failed to create arena for pid %d\n", fd_data->proc_pid);
10015 		} else {
10016 			OSIncrementAtomic(&necp_sysctl_arena_count);
10017 
10018 			// Get region offsets from base of mmap span; the arena
10019 			// doesn't need to be mmap'd at this point, since we simply
10020 			// compute the relative offset.
10021 			fd_data->system_sysctls_roff = skmem_arena_get_region_offset(fd_data->sysctl_arena, SKMEM_REGION_SYSCTLS);
10022 
10023 			// map to the task/process; upon success, the base address of the region
10024 			// will be returned in nai_mmap.ami_mapaddr; this can be communicated to
10025 			// the process.
10026 			error = skmem_arena_mmap(fd_data->sysctl_arena, p, &fd_data->sysctl_mmap);
10027 			if (error != 0) {
10028 				NECPLOG(LOG_ERR, "failed to map sysctl arena for pid %d\n", fd_data->proc_pid);
10029 				necp_sysctl_arena_destroy(fd_data);
10030 			}
10031 		}
10032 	}
10033 
10034 	if (!locked) {
10035 		NECP_FD_UNLOCK(fd_data);
10036 	}
10037 
10038 	return error;
10039 }
10040 
10041 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)10042 necp_client_stats_bufreq(struct necp_fd_data *fd_data,
10043     struct necp_client *client,
10044     struct necp_client_flow_registration *flow_registration,
10045     struct necp_stats_bufreq *bufreq,
10046     struct necp_stats_hdr *out_header)
10047 {
10048 	int error = 0;
10049 	NECP_CLIENT_ASSERT_LOCKED(client);
10050 	NECP_FD_ASSERT_LOCKED(fd_data);
10051 
10052 	if ((bufreq->necp_stats_bufreq_id == NECP_CLIENT_STATISTICS_BUFREQ_ID) &&
10053 	    ((bufreq->necp_stats_bufreq_type == NECP_CLIENT_STATISTICS_TYPE_TCP &&
10054 	    bufreq->necp_stats_bufreq_ver == NECP_CLIENT_STATISTICS_TYPE_TCP_CURRENT_VER) ||
10055 	    (bufreq->necp_stats_bufreq_type == NECP_CLIENT_STATISTICS_TYPE_UDP &&
10056 	    bufreq->necp_stats_bufreq_ver == NECP_CLIENT_STATISTICS_TYPE_UDP_CURRENT_VER) ||
10057 	    (bufreq->necp_stats_bufreq_type == NECP_CLIENT_STATISTICS_TYPE_QUIC &&
10058 	    bufreq->necp_stats_bufreq_ver == NECP_CLIENT_STATISTICS_TYPE_QUIC_CURRENT_VER)) &&
10059 	    (bufreq->necp_stats_bufreq_size == sizeof(struct necp_all_stats))) {
10060 		// There should be one and only one stats allocation per client.
10061 		// If asked more than once, we just repeat ourselves.
10062 		if (flow_registration->ustats_uaddr == 0) {
10063 			mach_vm_offset_t off;
10064 			ASSERT(flow_registration->stats_arena == NULL);
10065 			ASSERT(flow_registration->kstats_kaddr == NULL);
10066 			ASSERT(flow_registration->ustats_uaddr == 0);
10067 			error = necp_arena_stats_obj_alloc(fd_data, &off, &flow_registration->stats_arena, &flow_registration->kstats_kaddr, FALSE);
10068 			if (error == 0) {
10069 				// upon success, hold a reference for the client; this is released when the client is removed/closed
10070 				ASSERT(flow_registration->stats_arena != NULL);
10071 				necp_arena_info_retain(flow_registration->stats_arena);
10072 
10073 				// compute user address based on mapping info and object offset
10074 				flow_registration->ustats_uaddr = flow_registration->stats_arena->nai_mmap.ami_mapaddr + off;
10075 
10076 				// add to collect_stats list
10077 				NECP_STATS_LIST_LOCK_EXCLUSIVE();
10078 				necp_client_retain_locked(client); // Add a reference to the client
10079 				LIST_INSERT_HEAD(&necp_collect_stats_flow_list, flow_registration, collect_stats_chain);
10080 				NECP_STATS_LIST_UNLOCK();
10081 				necp_schedule_collect_stats_clients(FALSE);
10082 			} else {
10083 				ASSERT(flow_registration->stats_arena == NULL);
10084 				ASSERT(flow_registration->kstats_kaddr == NULL);
10085 			}
10086 		}
10087 		if (flow_registration->ustats_uaddr != 0) {
10088 			ASSERT(error == 0);
10089 			ASSERT(flow_registration->stats_arena != NULL);
10090 			ASSERT(flow_registration->kstats_kaddr != NULL);
10091 
10092 			struct necp_all_kstats *kstats = (struct necp_all_kstats *)flow_registration->kstats_kaddr;
10093 			kstats->necp_stats_ustats->all_stats_u.tcp_stats.necp_tcp_hdr.necp_stats_type = bufreq->necp_stats_bufreq_type;
10094 			kstats->necp_stats_ustats->all_stats_u.tcp_stats.necp_tcp_hdr.necp_stats_ver = bufreq->necp_stats_bufreq_ver;
10095 
10096 			if (out_header) {
10097 				out_header->necp_stats_type = bufreq->necp_stats_bufreq_type;
10098 				out_header->necp_stats_ver = bufreq->necp_stats_bufreq_ver;
10099 			}
10100 
10101 			bufreq->necp_stats_bufreq_uaddr = flow_registration->ustats_uaddr;
10102 		}
10103 	} else {
10104 		error = EINVAL;
10105 	}
10106 
10107 	return error;
10108 }
10109 
10110 static int
necp_client_stats_initial(struct necp_client_flow_registration * flow_registration,uint32_t stats_type,uint32_t stats_ver)10111 necp_client_stats_initial(struct necp_client_flow_registration *flow_registration, uint32_t stats_type, uint32_t stats_ver)
10112 {
10113 	// An attempted create
10114 	assert(flow_registration->stats_handler_context == NULL);
10115 	assert(flow_registration->stats_arena);
10116 	assert(flow_registration->ustats_uaddr);
10117 	assert(flow_registration->kstats_kaddr);
10118 
10119 	int error = 0;
10120 	uint64_t ntstat_properties = necp_find_netstat_initial_properties(flow_registration->client);
10121 
10122 	switch (stats_type) {
10123 	case NECP_CLIENT_STATISTICS_TYPE_TCP: {
10124 		if (stats_ver == NECP_CLIENT_STATISTICS_TYPE_TCP_VER_1) {
10125 			flow_registration->stats_handler_context = ntstat_userland_stats_open((userland_stats_provider_context *)flow_registration,
10126 			    NSTAT_PROVIDER_TCP_USERLAND, ntstat_properties, necp_request_tcp_netstats, necp_find_extension_info);
10127 			if (flow_registration->stats_handler_context == NULL) {
10128 				error = EIO;
10129 			}
10130 		} else {
10131 			error = ENOTSUP;
10132 		}
10133 		break;
10134 	}
10135 	case NECP_CLIENT_STATISTICS_TYPE_UDP: {
10136 		if (stats_ver == NECP_CLIENT_STATISTICS_TYPE_UDP_VER_1) {
10137 			flow_registration->stats_handler_context = ntstat_userland_stats_open((userland_stats_provider_context *)flow_registration,
10138 			    NSTAT_PROVIDER_UDP_USERLAND, ntstat_properties, necp_request_udp_netstats, necp_find_extension_info);
10139 			if (flow_registration->stats_handler_context == NULL) {
10140 				error = EIO;
10141 			}
10142 		} else {
10143 			error = ENOTSUP;
10144 		}
10145 		break;
10146 	}
10147 	case NECP_CLIENT_STATISTICS_TYPE_QUIC: {
10148 		if (stats_ver == NECP_CLIENT_STATISTICS_TYPE_QUIC_VER_1 && flow_registration->flags & NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS) {
10149 			flow_registration->stats_handler_context = ntstat_userland_stats_open((userland_stats_provider_context *)flow_registration,
10150 			    NSTAT_PROVIDER_QUIC_USERLAND, ntstat_properties, necp_request_quic_netstats, necp_find_extension_info);
10151 			if (flow_registration->stats_handler_context == NULL) {
10152 				error = EIO;
10153 			}
10154 		} else {
10155 			error = ENOTSUP;
10156 		}
10157 		break;
10158 	}
10159 	default: {
10160 		error = ENOTSUP;
10161 		break;
10162 	}
10163 	}
10164 	return error;
10165 }
10166 
10167 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)10168 necp_stats_initialize(struct necp_fd_data *fd_data,
10169     struct necp_client *client,
10170     struct necp_client_flow_registration *flow_registration,
10171     struct necp_stats_bufreq *bufreq)
10172 {
10173 	int error = 0;
10174 	struct necp_stats_hdr stats_hdr = {};
10175 
10176 	NECP_CLIENT_ASSERT_LOCKED(client);
10177 	NECP_FD_ASSERT_LOCKED(fd_data);
10178 	VERIFY(fd_data->stats_arena_active != NULL);
10179 	VERIFY(fd_data->stats_arena_active->nai_arena != NULL);
10180 	VERIFY(!(fd_data->stats_arena_active->nai_flags & (NAIF_REDIRECT | NAIF_DEFUNCT)));
10181 
10182 	if (bufreq == NULL) {
10183 		return EINVAL;
10184 	}
10185 
10186 	// Setup stats region
10187 	error = necp_client_stats_bufreq(fd_data, client, flow_registration, bufreq, &stats_hdr);
10188 	if (error) {
10189 		return error;
10190 	}
10191 	// Notify ntstat about new flow
10192 	if (flow_registration->stats_handler_context == NULL) {
10193 		error = necp_client_stats_initial(flow_registration, stats_hdr.necp_stats_type, stats_hdr.necp_stats_ver);
10194 		if (flow_registration->stats_handler_context != NULL) {
10195 			ntstat_userland_stats_event(flow_registration->stats_handler_context, NECP_CLIENT_STATISTICS_EVENT_INIT);
10196 		}
10197 		NECP_CLIENT_FLOW_LOG(client, flow_registration, "Initialized stats <error %d>", error);
10198 	}
10199 
10200 	return error;
10201 }
10202 
10203 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)10204 necp_client_map_sysctls(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10205 {
10206 	int result = 0;
10207 	if (!retval) {
10208 		retval = &result;
10209 	}
10210 
10211 	do {
10212 		mach_vm_address_t uaddr = 0;
10213 		if (uap->buffer_size != sizeof(uaddr)) {
10214 			*retval = EINVAL;
10215 			break;
10216 		}
10217 
10218 		*retval = necp_sysctl_arena_initialize(fd_data, false);
10219 		if (*retval != 0) {
10220 			break;
10221 		}
10222 
10223 		mach_vm_offset_t off = 0;
10224 		void * __single location = NULL;
10225 		NECP_FD_LOCK(fd_data);
10226 		location = necp_arena_sysctls_obj(fd_data, &off, NULL);
10227 		NECP_FD_UNLOCK(fd_data);
10228 
10229 		if (location == NULL) {
10230 			*retval = ENOENT;
10231 			break;
10232 		}
10233 
10234 		uaddr = fd_data->sysctl_mmap.ami_mapaddr + off;
10235 		*retval = copyout(&uaddr, uap->buffer, sizeof(uaddr));
10236 	} while (false);
10237 
10238 	return *retval;
10239 }
10240 
10241 #endif /* !SKYWALK */
10242 
10243 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)10244 necp_client_copy_route_statistics(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10245 {
10246 	int error = 0;
10247 	struct necp_client *client = NULL;
10248 	uuid_t client_id;
10249 
10250 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
10251 	    uap->buffer_size < sizeof(struct necp_stat_counts) || uap->buffer == 0) {
10252 		NECPLOG0(LOG_ERR, "necp_client_copy_route_statistics bad input");
10253 		error = EINVAL;
10254 		goto done;
10255 	}
10256 
10257 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
10258 	if (error) {
10259 		NECPLOG(LOG_ERR, "necp_client_copy_route_statistics copyin client_id error (%d)", error);
10260 		goto done;
10261 	}
10262 
10263 	// Lock
10264 	NECP_FD_LOCK(fd_data);
10265 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
10266 	if (client != NULL) {
10267 		NECP_CLIENT_ROUTE_LOCK(client);
10268 		struct necp_stat_counts route_stats = {};
10269 		if (client->current_route != NULL && client->current_route->rt_stats != NULL) {
10270 			struct nstat_counts     *rt_stats = client->current_route->rt_stats;
10271 			route_stats.necp_stat_rxpackets = os_atomic_load(&rt_stats->nstat_rxpackets, relaxed);
10272 			route_stats.necp_stat_rxbytes = os_atomic_load(&rt_stats->nstat_rxbytes, relaxed);
10273 			route_stats.necp_stat_txpackets = os_atomic_load(&rt_stats->nstat_txpackets, relaxed);
10274 			route_stats.necp_stat_txbytes = os_atomic_load(&rt_stats->nstat_txbytes, relaxed);
10275 			route_stats.necp_stat_rxduplicatebytes = rt_stats->nstat_rxduplicatebytes;
10276 			route_stats.necp_stat_rxoutoforderbytes = rt_stats->nstat_rxoutoforderbytes;
10277 			route_stats.necp_stat_txretransmit = rt_stats->nstat_txretransmit;
10278 			route_stats.necp_stat_connectattempts = rt_stats->nstat_connectattempts;
10279 			route_stats.necp_stat_connectsuccesses = rt_stats->nstat_connectsuccesses;
10280 			route_stats.necp_stat_min_rtt = rt_stats->nstat_min_rtt;
10281 			route_stats.necp_stat_avg_rtt = rt_stats->nstat_avg_rtt;
10282 			route_stats.necp_stat_var_rtt = rt_stats->nstat_var_rtt;
10283 			route_stats.necp_stat_route_flags = client->current_route->rt_flags;
10284 		}
10285 
10286 		// Unlock before copying out
10287 		NECP_CLIENT_ROUTE_UNLOCK(client);
10288 		NECP_CLIENT_UNLOCK(client);
10289 		NECP_FD_UNLOCK(fd_data);
10290 
10291 		error = copyout(&route_stats, uap->buffer, sizeof(route_stats));
10292 		if (error) {
10293 			NECPLOG(LOG_ERR, "necp_client_copy_route_statistics copyout error (%d)", error);
10294 		}
10295 	} else {
10296 		// Unlock
10297 		NECP_FD_UNLOCK(fd_data);
10298 		error = ENOENT;
10299 	}
10300 
10301 
10302 done:
10303 	*retval = error;
10304 	return error;
10305 }
10306 
10307 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_update_cache(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)10308 necp_client_update_cache(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10309 {
10310 	int error = 0;
10311 	struct necp_client *client = NULL;
10312 	uuid_t client_id;
10313 
10314 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
10315 		error = EINVAL;
10316 		goto done;
10317 	}
10318 
10319 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
10320 	if (error) {
10321 		NECPLOG(LOG_ERR, "necp_client_update_cache copyin client_id error (%d)", error);
10322 		goto done;
10323 	}
10324 
10325 	NECP_FD_LOCK(fd_data);
10326 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
10327 	if (client == NULL) {
10328 		NECP_FD_UNLOCK(fd_data);
10329 		error = ENOENT;
10330 		goto done;
10331 	}
10332 
10333 	struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
10334 	if (flow_registration == NULL) {
10335 		NECP_CLIENT_UNLOCK(client);
10336 		NECP_FD_UNLOCK(fd_data);
10337 		error = ENOENT;
10338 		goto done;
10339 	}
10340 
10341 	NECP_CLIENT_ROUTE_LOCK(client);
10342 	// This needs to be changed when TFO/ECN is supported by multiple flows
10343 	struct necp_client_flow *flow = LIST_FIRST(&flow_registration->flow_list);
10344 	if (flow == NULL ||
10345 	    (flow->remote_addr.sa.sa_family != AF_INET &&
10346 	    flow->remote_addr.sa.sa_family != AF_INET6) ||
10347 	    (flow->local_addr.sa.sa_family != AF_INET &&
10348 	    flow->local_addr.sa.sa_family != AF_INET6)) {
10349 		error = EINVAL;
10350 		NECPLOG(LOG_ERR, "necp_client_update_cache no flow error (%d)", error);
10351 		goto done_unlock;
10352 	}
10353 
10354 	necp_cache_buffer cache_buffer;
10355 	memset(&cache_buffer, 0, sizeof(cache_buffer));
10356 
10357 	if (uap->buffer_size != sizeof(necp_cache_buffer) ||
10358 	    uap->buffer == USER_ADDR_NULL) {
10359 		error = EINVAL;
10360 		goto done_unlock;
10361 	}
10362 
10363 	error = copyin(uap->buffer, &cache_buffer, sizeof(cache_buffer));
10364 	if (error) {
10365 		NECPLOG(LOG_ERR, "necp_client_update_cache copyin cache buffer error (%d)", error);
10366 		goto done_unlock;
10367 	}
10368 
10369 	if (cache_buffer.necp_cache_buf_type == NECP_CLIENT_CACHE_TYPE_ECN &&
10370 	    cache_buffer.necp_cache_buf_ver == NECP_CLIENT_CACHE_TYPE_ECN_VER_1) {
10371 		if (cache_buffer.necp_cache_buf_size != sizeof(necp_tcp_ecn_cache) ||
10372 		    cache_buffer.necp_cache_buf_addr == USER_ADDR_NULL) {
10373 			error = EINVAL;
10374 			goto done_unlock;
10375 		}
10376 
10377 		necp_tcp_ecn_cache ecn_cache_buffer;
10378 		memset(&ecn_cache_buffer, 0, sizeof(ecn_cache_buffer));
10379 
10380 		error = copyin(cache_buffer.necp_cache_buf_addr, &ecn_cache_buffer, sizeof(necp_tcp_ecn_cache));
10381 		if (error) {
10382 			NECPLOG(LOG_ERR, "necp_client_update_cache copyin ecn cache buffer error (%d)", error);
10383 			goto done_unlock;
10384 		}
10385 
10386 		if (client->current_route != NULL && client->current_route->rt_ifp != NULL) {
10387 			if (!client->platform_binary) {
10388 				ecn_cache_buffer.necp_tcp_ecn_heuristics_success = 0;
10389 			}
10390 			tcp_heuristics_ecn_update(&ecn_cache_buffer, client->current_route->rt_ifp,
10391 			    (union sockaddr_in_4_6 *)&flow->local_addr);
10392 		}
10393 	} else if (cache_buffer.necp_cache_buf_type == NECP_CLIENT_CACHE_TYPE_TFO &&
10394 	    cache_buffer.necp_cache_buf_ver == NECP_CLIENT_CACHE_TYPE_TFO_VER_1) {
10395 		if (cache_buffer.necp_cache_buf_size != sizeof(necp_tcp_tfo_cache) ||
10396 		    cache_buffer.necp_cache_buf_addr == USER_ADDR_NULL) {
10397 			error = EINVAL;
10398 			goto done_unlock;
10399 		}
10400 
10401 		necp_tcp_tfo_cache tfo_cache_buffer;
10402 		memset(&tfo_cache_buffer, 0, sizeof(tfo_cache_buffer));
10403 
10404 		error = copyin(cache_buffer.necp_cache_buf_addr, &tfo_cache_buffer, sizeof(necp_tcp_tfo_cache));
10405 		if (error) {
10406 			NECPLOG(LOG_ERR, "necp_client_update_cache copyin tfo cache buffer error (%d)", error);
10407 			goto done_unlock;
10408 		}
10409 
10410 		if (client->current_route != NULL && client->current_route->rt_ifp != NULL) {
10411 			if (!client->platform_binary) {
10412 				tfo_cache_buffer.necp_tcp_tfo_heuristics_success = 0;
10413 			}
10414 			tcp_heuristics_tfo_update(&tfo_cache_buffer, client->current_route->rt_ifp,
10415 			    (union sockaddr_in_4_6 *)&flow->local_addr,
10416 			    (union sockaddr_in_4_6 *)&flow->remote_addr);
10417 		}
10418 	} else {
10419 		error = EINVAL;
10420 	}
10421 done_unlock:
10422 	NECP_CLIENT_ROUTE_UNLOCK(client);
10423 	NECP_CLIENT_UNLOCK(client);
10424 	NECP_FD_UNLOCK(fd_data);
10425 done:
10426 	*retval = error;
10427 	return error;
10428 }
10429 
10430 // Most results will fit into this size
10431 struct necp_client_signable_default {
10432 	uuid_t client_id;
10433 	u_int32_t sign_type;
10434 	u_int8_t signable_data[NECP_CLIENT_ACTION_SIGN_DEFAULT_DATA_LENGTH];
10435 } __attribute__((__packed__));
10436 
10437 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_sign(__unused struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)10438 necp_client_sign(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10439 {
10440 	int error = 0;
10441 	u_int8_t tag[NECP_CLIENT_ACTION_SIGN_TAG_LENGTH] = {};
10442 	struct necp_client_signable * __indexable signable = NULL;
10443 	struct necp_client_signable * __indexable allocated_signable = NULL;
10444 	struct necp_client_signable_default default_signable = {};
10445 	size_t tag_size = sizeof(tag);
10446 
10447 	const size_t signable_length = uap->client_id_len;
10448 	const size_t return_tag_length = uap->buffer_size;
10449 
10450 	*retval = 0;
10451 
10452 	const bool has_resolver_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_VALIDATED_RESOLVER, 0) == 0);
10453 	if (!has_resolver_entitlement) {
10454 		NECPLOG0(LOG_ERR, "Process does not hold the necessary entitlement to sign resolver answers");
10455 		error = EPERM;
10456 		goto done;
10457 	}
10458 
10459 	if (uap->client_id == 0 || signable_length < sizeof(*signable) || signable_length > NECP_CLIENT_ACTION_SIGN_MAX_TOTAL_LENGTH) {
10460 		error = EINVAL;
10461 		goto done;
10462 	}
10463 
10464 	if (uap->buffer == 0 || return_tag_length != NECP_CLIENT_ACTION_SIGN_TAG_LENGTH) {
10465 		error = EINVAL;
10466 		goto done;
10467 	}
10468 
10469 	if (signable_length <= sizeof(default_signable)) {
10470 		signable = (struct necp_client_signable *)&default_signable;
10471 	} else {
10472 		if ((allocated_signable = (struct necp_client_signable *)kalloc_data(signable_length, Z_WAITOK | Z_ZERO)) == NULL) {
10473 			NECPLOG(LOG_ERR, "necp_client_sign allocate signable %zu failed", signable_length);
10474 			error = ENOMEM;
10475 			goto done;
10476 		}
10477 		signable = allocated_signable;
10478 	}
10479 
10480 	error = copyin(uap->client_id, signable, signable_length);
10481 	if (error) {
10482 		NECPLOG(LOG_ERR, "necp_client_sign copyin signable error (%d)", error);
10483 		goto done;
10484 	}
10485 
10486 	size_t data_length = 0;
10487 	switch (signable->sign_type) {
10488 	case NECP_CLIENT_SIGN_TYPE_RESOLVER_ANSWER:
10489 	case NECP_CLIENT_SIGN_TYPE_SYSTEM_RESOLVER_ANSWER: {
10490 		data_length = (sizeof(struct necp_client_host_resolver_answer) - sizeof(struct necp_client_signable));
10491 		if (signable_length < (sizeof(struct necp_client_signable) + data_length)) {
10492 			error = EINVAL;
10493 			goto done;
10494 		}
10495 		struct necp_client_host_resolver_answer * __single signable_struct = (struct necp_client_host_resolver_answer *)signable;
10496 		if (signable_struct->hostname_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10497 		    signable_length != (sizeof(struct necp_client_signable) + data_length + signable_struct->hostname_length)) {
10498 			error = EINVAL;
10499 			goto done;
10500 		}
10501 		data_length += signable_struct->hostname_length;
10502 		break;
10503 	}
10504 	case NECP_CLIENT_SIGN_TYPE_BROWSE_RESULT:
10505 	case NECP_CLIENT_SIGN_TYPE_SYSTEM_BROWSE_RESULT: {
10506 		data_length = (sizeof(struct necp_client_browse_result) - sizeof(struct necp_client_signable));
10507 		if (signable_length < (sizeof(struct necp_client_signable) + data_length)) {
10508 			error = EINVAL;
10509 			goto done;
10510 		}
10511 		struct necp_client_browse_result *signable_struct = (struct necp_client_browse_result *)signable;
10512 		if (signable_struct->service_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10513 		    signable_length != (sizeof(struct necp_client_signable) + data_length + signable_struct->service_length)) {
10514 			error = EINVAL;
10515 			goto done;
10516 		}
10517 		data_length += signable_struct->service_length;
10518 		break;
10519 	}
10520 	case NECP_CLIENT_SIGN_TYPE_SERVICE_RESOLVER_ANSWER:
10521 	case NECP_CLIENT_SIGN_TYPE_SYSTEM_SERVICE_RESOLVER_ANSWER: {
10522 		data_length = (sizeof(struct necp_client_service_resolver_answer) - sizeof(struct necp_client_signable));
10523 		if (signable_length < (sizeof(struct necp_client_signable) + data_length)) {
10524 			error = EINVAL;
10525 			goto done;
10526 		}
10527 		struct necp_client_service_resolver_answer * __single signable_struct = (struct necp_client_service_resolver_answer *)signable;
10528 		if (signable_struct->service_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10529 		    signable_struct->hostname_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10530 		    signable_length != (sizeof(struct necp_client_signable) + data_length + signable_struct->service_length + signable_struct->hostname_length)) {
10531 			error = EINVAL;
10532 			goto done;
10533 		}
10534 		data_length += signable_struct->service_length;
10535 		data_length += signable_struct->hostname_length;
10536 		break;
10537 	}
10538 	default: {
10539 		NECPLOG(LOG_ERR, "necp_client_sign unknown signable type (%u)", signable->sign_type);
10540 		error = EINVAL;
10541 		goto done;
10542 	}
10543 	}
10544 
10545 	error = necp_sign_resolver_answer(signable->client_id, signable->sign_type,
10546 	    signable_get_data(signable, data_length), data_length,
10547 	    tag, &tag_size);
10548 	if (tag_size != sizeof(tag)) {
10549 		NECPLOG(LOG_ERR, "necp_client_sign unexpected tag size %zu", tag_size);
10550 		error = EINVAL;
10551 		goto done;
10552 	}
10553 	error = copyout(tag, uap->buffer, tag_size);
10554 	if (error) {
10555 		NECPLOG(LOG_ERR, "necp_client_sign copyout error (%d)", error);
10556 		goto done;
10557 	}
10558 
10559 done:
10560 	if (allocated_signable != NULL) {
10561 		kfree_data(allocated_signable, signable_length);
10562 		allocated_signable = NULL;
10563 	}
10564 	*retval = error;
10565 	return error;
10566 }
10567 
10568 // Most results will fit into this size
10569 struct necp_client_validatable_default {
10570 	struct necp_client_signature signature;
10571 	struct necp_client_signable_default signable;
10572 } __attribute__((__packed__));
10573 
10574 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_validate(__unused struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)10575 necp_client_validate(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10576 {
10577 	int error = 0;
10578 	struct necp_client_validatable *validatable = NULL;
10579 	struct necp_client_validatable * __single allocated_validatable = NULL;
10580 	struct necp_client_validatable_default default_validatable = {};
10581 
10582 	const size_t validatable_length = uap->client_id_len;
10583 
10584 	*retval = 0;
10585 
10586 	const bool has_resolver_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_VALIDATED_RESOLVER, 0) == 0);
10587 	if (!has_resolver_entitlement) {
10588 		NECPLOG0(LOG_ERR, "Process does not hold the necessary entitlement to directly validate resolver answers");
10589 		error = EPERM;
10590 		goto done;
10591 	}
10592 
10593 	if (uap->client_id == 0 || validatable_length < sizeof(*validatable) ||
10594 	    validatable_length > (NECP_CLIENT_ACTION_SIGN_MAX_TOTAL_LENGTH + NECP_CLIENT_ACTION_SIGN_TAG_LENGTH)) {
10595 		error = EINVAL;
10596 		goto done;
10597 	}
10598 
10599 	if (validatable_length <= sizeof(default_validatable)) {
10600 		validatable = (struct necp_client_validatable *)&default_validatable;
10601 	} else {
10602 		if ((allocated_validatable = (struct necp_client_validatable *)kalloc_data(validatable_length, Z_WAITOK | Z_ZERO)) == NULL) {
10603 			NECPLOG(LOG_ERR, "necp_client_validate allocate struct %zu failed", validatable_length);
10604 			error = ENOMEM;
10605 			goto done;
10606 		}
10607 		validatable = allocated_validatable;
10608 	}
10609 
10610 	error = copyin(uap->client_id, validatable, validatable_length);
10611 	if (error) {
10612 		NECPLOG(LOG_ERR, "necp_client_validate copyin error (%d)", error);
10613 		goto done;
10614 	}
10615 
10616 	size_t signable_data_len = validatable_length - sizeof(struct necp_client_validatable);
10617 	const bool validated = necp_validate_resolver_answer(validatable->signable.client_id, validatable->signable.sign_type,
10618 	    signable_get_data(&validatable->signable, signable_data_len), signable_data_len,
10619 	    validatable->signature.signed_tag, sizeof(validatable->signature.signed_tag));
10620 	if (!validated) {
10621 		// Return EAUTH to indicate that the signature failed
10622 		error = EAUTH;
10623 	}
10624 
10625 done:
10626 	if (allocated_validatable != NULL) {
10627 		kfree_data(allocated_validatable, validatable_length);
10628 		allocated_validatable = NULL;
10629 	}
10630 	*retval = error;
10631 	return error;
10632 }
10633 
10634 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)10635 necp_client_get_signed_client_id(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10636 {
10637 	int error = 0;
10638 	*retval = 0;
10639 	u_int32_t request_type = 0;
10640 	struct necp_client_signed_client_id_uuid client_id = { 0 };
10641 	const size_t buffer_size = uap->buffer_size;
10642 	u_int8_t tag[NECP_CLIENT_ACTION_SIGN_TAG_LENGTH] = {};
10643 	size_t tag_size = sizeof(tag);
10644 	proc_t proc = current_proc();
10645 	if (uap->client_id == 0 || uap->client_id_len != sizeof(u_int32_t) ||
10646 	    buffer_size < sizeof(struct necp_client_signed_client_id_uuid) ||
10647 	    uap->buffer == 0) {
10648 		NECPLOG0(LOG_ERR, "necp_client_get_signed_client_id bad input");
10649 		error = EINVAL;
10650 		goto done;
10651 	}
10652 
10653 	error = copyin(uap->client_id, &request_type, sizeof(u_int32_t));
10654 	if (error) {
10655 		NECPLOG(LOG_ERR, "necp_client_get_signed_client_id copyin request_type error (%d)", error);
10656 		goto done;
10657 	}
10658 
10659 	if (request_type != NECP_CLIENT_SIGNED_CLIENT_ID_TYPE_UUID) {
10660 		error = ENOENT;
10661 		NECPLOG(LOG_ERR, "necp_client_get_signed_client_id bad request_type (%d)", request_type);
10662 		goto done;
10663 	}
10664 
10665 	uuid_t application_uuid;
10666 	uuid_clear(application_uuid);
10667 	proc_getexecutableuuid(proc, application_uuid, sizeof(application_uuid));
10668 
10669 	error = necp_sign_application_id(application_uuid,
10670 	    NECP_CLIENT_SIGNED_CLIENT_ID_TYPE_UUID,
10671 	    tag, &tag_size);
10672 	if (tag_size != sizeof(tag)) {
10673 		NECPLOG(LOG_ERR, "necp_client_get_signed_client_id unexpected tag size %zu", tag_size);
10674 		error = EINVAL;
10675 		goto done;
10676 	}
10677 	uuid_copy(client_id.client_id, application_uuid);
10678 	client_id.signature_length = tag_size;
10679 	memcpy(client_id.signature_data, tag, tag_size);
10680 
10681 	error = copyout(&client_id, uap->buffer, sizeof(client_id));
10682 	if (error != 0) {
10683 		NECPLOG(LOG_ERR, "necp_client_get_signed_client_id copyout error (%d)", error);
10684 		goto done;
10685 	}
10686 
10687 done:
10688 	*retval = error;
10689 	return error;
10690 }
10691 
10692 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)10693 necp_client_set_signed_client_id(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10694 {
10695 	int error = 0;
10696 	*retval = 0;
10697 	u_int32_t request_type = 0;
10698 	struct necp_client_signed_client_id_uuid client_id = { 0 };
10699 	const size_t buffer_size = uap->buffer_size;
10700 
10701 	// Only allow entitled processes to set the client ID.
10702 	proc_t proc = current_proc();
10703 	task_t __single task = proc_task(proc);
10704 	bool has_delegation_entitlement = task != NULL && IOTaskHasEntitlement(task, kCSWebBrowserNetworkEntitlement);
10705 	if (!has_delegation_entitlement) {
10706 		has_delegation_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_SOCKET_DELEGATE, 0) == 0);
10707 	}
10708 	if (!has_delegation_entitlement) {
10709 		NECPLOG0(LOG_ERR, "necp_client_set_signed_client_id client lacks the necessary entitlement");
10710 		error = EAUTH;
10711 		goto done;
10712 	}
10713 
10714 	if (uap->client_id == 0 || uap->client_id_len != sizeof(u_int32_t) ||
10715 	    buffer_size < sizeof(struct necp_client_signed_client_id_uuid) ||
10716 	    uap->buffer == 0) {
10717 		NECPLOG0(LOG_ERR, "necp_client_set_signed_client_id bad input");
10718 		error = EINVAL;
10719 		goto done;
10720 	}
10721 
10722 	error = copyin(uap->client_id, &request_type, sizeof(u_int32_t));
10723 	if (error) {
10724 		NECPLOG(LOG_ERR, "necp_client_set_signed_client_id copyin request_type error (%d)", error);
10725 		goto done;
10726 	}
10727 
10728 	if (request_type != NECP_CLIENT_SIGNED_CLIENT_ID_TYPE_UUID) {
10729 		error = ENOENT;
10730 		NECPLOG(LOG_ERR, "necp_client_set_signed_client_id bad request_type (%d)", request_type);
10731 		goto done;
10732 	}
10733 
10734 	error = copyin(uap->buffer, &client_id, sizeof(struct necp_client_signed_client_id_uuid));
10735 	if (error) {
10736 		NECPLOG(LOG_ERR, "necp_client_set_signed_client_id copyin request error (%d)", error);
10737 		goto done;
10738 	}
10739 
10740 	const bool validated = necp_validate_application_id(client_id.client_id,
10741 	    NECP_CLIENT_SIGNED_CLIENT_ID_TYPE_UUID,
10742 	    client_id.signature_data, sizeof(client_id.signature_data));
10743 	if (!validated) {
10744 		// Return EAUTH to indicate that the signature failed
10745 		error = EAUTH;
10746 		NECPLOG(LOG_ERR, "necp_client_set_signed_client_id signature validation failed (%d)", error);
10747 		goto done;
10748 	}
10749 
10750 	proc_setresponsibleuuid(proc, client_id.client_id, sizeof(client_id.client_id));
10751 
10752 done:
10753 	*retval = error;
10754 	return error;
10755 }
10756 
10757 int
necp_client_action(struct proc * p,struct necp_client_action_args * uap,int * retval)10758 necp_client_action(struct proc *p, struct necp_client_action_args *uap, int *retval)
10759 {
10760 	struct fileproc * __single fp;
10761 	int error = 0;
10762 	int return_value = 0;
10763 	struct necp_fd_data * __single fd_data = NULL;
10764 
10765 	error = necp_find_fd_data(p, uap->necp_fd, &fp, &fd_data);
10766 	if (error != 0) {
10767 		NECPLOG(LOG_ERR, "necp_client_action find fd error (%d)", error);
10768 		return error;
10769 	}
10770 
10771 	u_int32_t action = uap->action;
10772 
10773 #if CONFIG_MACF
10774 	error = mac_necp_check_client_action(p, fp->fp_glob, action);
10775 	if (error) {
10776 		return_value = error;
10777 		goto done;
10778 	}
10779 #endif /* MACF */
10780 
10781 	switch (action) {
10782 	case NECP_CLIENT_ACTION_ADD: {
10783 		return_value = necp_client_add(p, fd_data, uap, retval);
10784 		break;
10785 	}
10786 	case NECP_CLIENT_ACTION_CLAIM: {
10787 		return_value = necp_client_claim(p, fd_data, uap, retval);
10788 		break;
10789 	}
10790 	case NECP_CLIENT_ACTION_REMOVE: {
10791 		return_value = necp_client_remove(fd_data, uap, retval);
10792 		break;
10793 	}
10794 	case NECP_CLIENT_ACTION_COPY_PARAMETERS:
10795 	case NECP_CLIENT_ACTION_COPY_RESULT:
10796 	case NECP_CLIENT_ACTION_COPY_UPDATED_RESULT:
10797 	case NECP_CLIENT_ACTION_COPY_UPDATED_RESULT_FINAL: {
10798 		return_value = necp_client_copy(fd_data, uap, retval);
10799 		break;
10800 	}
10801 	case NECP_CLIENT_ACTION_COPY_LIST: {
10802 		return_value = necp_client_list(fd_data, uap, retval);
10803 		break;
10804 	}
10805 	case NECP_CLIENT_ACTION_ADD_FLOW: {
10806 		return_value = necp_client_add_flow(fd_data, uap, retval);
10807 		break;
10808 	}
10809 	case NECP_CLIENT_ACTION_REMOVE_FLOW: {
10810 		return_value = necp_client_remove_flow(fd_data, uap, retval);
10811 		break;
10812 	}
10813 #if SKYWALK
10814 	case NECP_CLIENT_ACTION_REQUEST_NEXUS_INSTANCE: {
10815 		return_value = necp_client_request_nexus(fd_data, uap, retval);
10816 		break;
10817 	}
10818 #endif /* !SKYWALK */
10819 	case NECP_CLIENT_ACTION_AGENT: {
10820 		return_value = necp_client_agent_action(fd_data, uap, retval);
10821 		break;
10822 	}
10823 	case NECP_CLIENT_ACTION_COPY_AGENT: {
10824 		return_value = necp_client_copy_agent(fd_data, uap, retval);
10825 		break;
10826 	}
10827 	case NECP_CLIENT_ACTION_AGENT_USE: {
10828 		return_value = necp_client_agent_use(fd_data, uap, retval);
10829 		break;
10830 	}
10831 	case NECP_CLIENT_ACTION_ACQUIRE_AGENT_TOKEN: {
10832 		return_value = necp_client_acquire_agent_token(fd_data, uap, retval);
10833 		break;
10834 	}
10835 	case NECP_CLIENT_ACTION_COPY_INTERFACE: {
10836 		return_value = necp_client_copy_interface(fd_data, uap, retval);
10837 		break;
10838 	}
10839 #if SKYWALK
10840 	case NECP_CLIENT_ACTION_GET_INTERFACE_ADDRESS: {
10841 		return_value = necp_client_get_interface_address(fd_data, uap, retval);
10842 		break;
10843 	}
10844 	case NECP_CLIENT_ACTION_SET_STATISTICS: {
10845 		return_value = ENOTSUP;
10846 		break;
10847 	}
10848 	case NECP_CLIENT_ACTION_MAP_SYSCTLS: {
10849 		return_value = necp_client_map_sysctls(fd_data, uap, retval);
10850 		break;
10851 	}
10852 #endif /* !SKYWALK */
10853 	case NECP_CLIENT_ACTION_COPY_ROUTE_STATISTICS: {
10854 		return_value = necp_client_copy_route_statistics(fd_data, uap, retval);
10855 		break;
10856 	}
10857 	case NECP_CLIENT_ACTION_UPDATE_CACHE: {
10858 		return_value = necp_client_update_cache(fd_data, uap, retval);
10859 		break;
10860 	}
10861 	case NECP_CLIENT_ACTION_COPY_CLIENT_UPDATE: {
10862 		return_value = necp_client_copy_client_update(fd_data, uap, retval);
10863 		break;
10864 	}
10865 	case NECP_CLIENT_ACTION_SIGN: {
10866 		return_value = necp_client_sign(fd_data, uap, retval);
10867 		break;
10868 	}
10869 	case NECP_CLIENT_ACTION_VALIDATE: {
10870 		return_value = necp_client_validate(fd_data, uap, retval);
10871 		break;
10872 	}
10873 	case NECP_CLIENT_ACTION_GET_SIGNED_CLIENT_ID: {
10874 		return_value = necp_client_get_signed_client_id(fd_data, uap, retval);
10875 		break;
10876 	}
10877 	case NECP_CLIENT_ACTION_SET_SIGNED_CLIENT_ID: {
10878 		return_value = necp_client_set_signed_client_id(fd_data, uap, retval);
10879 		break;
10880 	}
10881 	default: {
10882 		NECPLOG(LOG_ERR, "necp_client_action unknown action (%u)", action);
10883 		return_value = EINVAL;
10884 		break;
10885 	}
10886 	}
10887 
10888 done:
10889 	fp_drop(p, uap->necp_fd, fp, 0);
10890 	return return_value;
10891 }
10892 
10893 #define NECP_MAX_MATCH_POLICY_PARAMETER_SIZE 1024
10894 
10895 int
necp_match_policy(struct proc * p,struct necp_match_policy_args * uap,int32_t * retval)10896 necp_match_policy(struct proc *p, struct necp_match_policy_args *uap, int32_t *retval)
10897 {
10898 #pragma unused(retval)
10899 	size_t buffer_size = 0;
10900 	u_int8_t * __sized_by(buffer_size) parameters = NULL;
10901 	struct necp_aggregate_result returned_result;
10902 	int error = 0;
10903 
10904 	if (uap == NULL) {
10905 		error = EINVAL;
10906 		goto done;
10907 	}
10908 
10909 	if (uap->parameters == 0 || uap->parameters_size == 0 || uap->parameters_size > NECP_MAX_MATCH_POLICY_PARAMETER_SIZE || uap->returned_result == 0) {
10910 		error = EINVAL;
10911 		goto done;
10912 	}
10913 
10914 	parameters = (u_int8_t *)kalloc_data(uap->parameters_size, Z_WAITOK | Z_ZERO);
10915 	buffer_size = uap->parameters_size;
10916 	if (parameters == NULL) {
10917 		error = ENOMEM;
10918 		goto done;
10919 	}
10920 	// Copy parameters in
10921 	error = copyin(uap->parameters, parameters, buffer_size);
10922 	if (error) {
10923 		goto done;
10924 	}
10925 
10926 	error = necp_application_find_policy_match_internal(p, parameters, buffer_size,
10927 	    &returned_result, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, false, false, NULL);
10928 	if (error) {
10929 		goto done;
10930 	}
10931 
10932 	// Copy return value back
10933 	error = copyout(&returned_result, uap->returned_result, sizeof(struct necp_aggregate_result));
10934 	if (error) {
10935 		goto done;
10936 	}
10937 done:
10938 	if (parameters != NULL) {
10939 		kfree_data_sized_by(parameters, buffer_size);
10940 	}
10941 	return error;
10942 }
10943 
10944 /// Socket operations
10945 
10946 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)10947 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)
10948 {
10949 	int error = 0;
10950 	int cursor = 0;
10951 	size_t string_size = 0;
10952 	size_t local_string_length = 0;
10953 	char * __sized_by(local_string_length) local_string = NULL;
10954 	u_int8_t * __indexable value = NULL;
10955 	char * __indexable buffer_to_free = NULL;
10956 
10957 	cursor = necp_buffer_find_tlv(buffer, buffer_length, 0, type, NULL, 0);
10958 	if (cursor < 0) {
10959 		// This will clear out the parameter
10960 		goto done;
10961 	}
10962 
10963 	string_size = necp_buffer_get_tlv_length(buffer, buffer_length, cursor);
10964 	if (single_tlv != NULL && (buffer_length == sizeof(struct necp_tlv_header) + string_size)) {
10965 		*single_tlv = true;
10966 	}
10967 	if (string_size == 0 || string_size > NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) {
10968 		// This will clear out the parameter
10969 		goto done;
10970 	}
10971 
10972 	local_string = (char *)kalloc_data(string_size + 1, Z_WAITOK | Z_ZERO);
10973 	local_string_length = string_size + 1;
10974 	if (local_string == NULL) {
10975 		NECPLOG(LOG_ERR, "Failed to allocate a socket attribute buffer (size %zu)", string_size);
10976 		goto fail;
10977 	}
10978 
10979 	value = necp_buffer_get_tlv_value(buffer, buffer_length, cursor, NULL);
10980 	if (value == NULL) {
10981 		NECPLOG0(LOG_ERR, "Failed to get socket attribute");
10982 		goto fail;
10983 	}
10984 
10985 	memcpy(local_string, value, string_size);
10986 	local_string[string_size] = 0;
10987 
10988 done:
10989 	if (*buffer_p != NULL) {
10990 		buffer_to_free = __unsafe_null_terminated_to_indexable(*buffer_p);
10991 	}
10992 
10993 	// Protect switching of buffer pointer
10994 	necp_lock_socket_attributes();
10995 	if (local_string != NULL) {
10996 		*buffer_p = __unsafe_null_terminated_from_indexable(local_string, &local_string[string_size]);
10997 	} else {
10998 		*buffer_p = NULL;
10999 	}
11000 	necp_unlock_socket_attributes();
11001 
11002 	if (buffer_to_free != NULL) {
11003 		kfree_data_addr(buffer_to_free);
11004 	}
11005 	return 0;
11006 fail:
11007 	if (local_string != NULL) {
11008 		kfree_data_sized_by(local_string, local_string_length);
11009 	}
11010 	return error;
11011 }
11012 
11013 errno_t
necp_set_socket_attributes(struct inp_necp_attributes * attributes,struct sockopt * sopt)11014 necp_set_socket_attributes(struct inp_necp_attributes *attributes, struct sockopt *sopt)
11015 {
11016 	int error = 0;
11017 	u_int8_t *buffer = NULL;
11018 	bool single_tlv = false;
11019 	size_t valsize = sopt->sopt_valsize;
11020 	if (valsize == 0 ||
11021 	    valsize > ((sizeof(struct necp_tlv_header) + NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) * 4)) {
11022 		goto done;
11023 	}
11024 
11025 	buffer = (u_int8_t *)kalloc_data(valsize, Z_WAITOK | Z_ZERO);
11026 	if (buffer == NULL) {
11027 		goto done;
11028 	}
11029 
11030 	error = sooptcopyin(sopt, buffer, valsize, 0);
11031 	if (error) {
11032 		goto done;
11033 	}
11034 
11035 	// If NECP_TLV_ATTRIBUTE_DOMAIN_CONTEXT is being set/cleared separately from the other attributes,
11036 	// do not clear other attributes.
11037 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_DOMAIN_CONTEXT, &attributes->inp_domain_context, &single_tlv);
11038 	if (error) {
11039 		NECPLOG0(LOG_ERR, "Could not set domain context TLV for socket attributes");
11040 		goto done;
11041 	}
11042 	if (single_tlv == true) {
11043 		goto done;
11044 	}
11045 
11046 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_DOMAIN, &attributes->inp_domain, NULL);
11047 	if (error) {
11048 		NECPLOG0(LOG_ERR, "Could not set domain TLV for socket attributes");
11049 		goto done;
11050 	}
11051 
11052 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_DOMAIN_OWNER, &attributes->inp_domain_owner, NULL);
11053 	if (error) {
11054 		NECPLOG0(LOG_ERR, "Could not set domain owner TLV for socket attributes");
11055 		goto done;
11056 	}
11057 
11058 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_TRACKER_DOMAIN, &attributes->inp_tracker_domain, NULL);
11059 	if (error) {
11060 		NECPLOG0(LOG_ERR, "Could not set tracker domain TLV for socket attributes");
11061 		goto done;
11062 	}
11063 
11064 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_ACCOUNT, &attributes->inp_account, NULL);
11065 	if (error) {
11066 		NECPLOG0(LOG_ERR, "Could not set account TLV for socket attributes");
11067 		goto done;
11068 	}
11069 
11070 done:
11071 	NECP_SOCKET_ATTRIBUTE_LOG("NECP ATTRIBUTES SOCKET - domain <%s> owner <%s> context <%s> tracker domain <%s> account <%s>",
11072 	    attributes->inp_domain,
11073 	    attributes->inp_domain_owner,
11074 	    attributes->inp_domain_context,
11075 	    attributes->inp_tracker_domain,
11076 	    attributes->inp_account);
11077 
11078 	if (necp_debug) {
11079 		NECPLOG(LOG_DEBUG, "Set on socket: Domain %s, Domain owner %s, Domain context %s, Tracker domain %s, Account %s",
11080 		    attributes->inp_domain,
11081 		    attributes->inp_domain_owner,
11082 		    attributes->inp_domain_context,
11083 		    attributes->inp_tracker_domain,
11084 		    attributes->inp_account);
11085 	}
11086 
11087 	if (buffer != NULL) {
11088 		kfree_data(buffer, valsize);
11089 	}
11090 
11091 	return error;
11092 }
11093 
11094 errno_t
necp_get_socket_attributes(struct inp_necp_attributes * attributes,struct sockopt * sopt)11095 necp_get_socket_attributes(struct inp_necp_attributes *attributes, struct sockopt *sopt)
11096 {
11097 	int error = 0;
11098 	size_t valsize = 0;
11099 	u_int8_t *buffer = NULL;
11100 	u_int8_t * __indexable cursor = NULL;
11101 
11102 	if (attributes->inp_domain != NULL) {
11103 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_domain);
11104 	}
11105 	if (attributes->inp_domain_owner != NULL) {
11106 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_domain_owner);
11107 	}
11108 	if (attributes->inp_domain_context != NULL) {
11109 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_domain_context);
11110 	}
11111 	if (attributes->inp_tracker_domain != NULL) {
11112 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_tracker_domain);
11113 	}
11114 	if (attributes->inp_account != NULL) {
11115 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_account);
11116 	}
11117 	if (valsize == 0) {
11118 		goto done;
11119 	}
11120 
11121 	buffer = (u_int8_t *)kalloc_data(valsize, Z_WAITOK | Z_ZERO);
11122 	if (buffer == NULL) {
11123 		goto done;
11124 	}
11125 
11126 	cursor = buffer;
11127 	if (attributes->inp_domain != NULL) {
11128 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_DOMAIN, strlen(attributes->inp_domain), __terminated_by_to_indexable(attributes->inp_domain),
11129 		    buffer, valsize);
11130 	}
11131 
11132 	if (attributes->inp_domain_owner != NULL) {
11133 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_DOMAIN_OWNER, strlen(attributes->inp_domain_owner), __terminated_by_to_indexable(attributes->inp_domain_owner),
11134 		    buffer, valsize);
11135 	}
11136 
11137 	if (attributes->inp_domain_context != NULL) {
11138 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_DOMAIN_CONTEXT, strlen(attributes->inp_domain_context), __terminated_by_to_indexable(attributes->inp_domain_context),
11139 		    buffer, valsize);
11140 	}
11141 
11142 	if (attributes->inp_tracker_domain != NULL) {
11143 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_TRACKER_DOMAIN, strlen(attributes->inp_tracker_domain), __terminated_by_to_indexable(attributes->inp_tracker_domain),
11144 		    buffer, valsize);
11145 	}
11146 
11147 	if (attributes->inp_account != NULL) {
11148 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_ACCOUNT, strlen(attributes->inp_account), __terminated_by_to_indexable(attributes->inp_account),
11149 		    buffer, valsize);
11150 	}
11151 
11152 	error = sooptcopyout(sopt, buffer, valsize);
11153 	if (error) {
11154 		goto done;
11155 	}
11156 done:
11157 	if (buffer != NULL) {
11158 		kfree_data(buffer, valsize);
11159 	}
11160 
11161 	return error;
11162 }
11163 
11164 int
necp_set_socket_resolver_signature(struct inpcb * inp,struct sockopt * sopt)11165 necp_set_socket_resolver_signature(struct inpcb *inp, struct sockopt *sopt)
11166 {
11167 	const size_t valsize = sopt->sopt_valsize;
11168 	if (valsize > NECP_CLIENT_ACTION_SIGN_MAX_TOTAL_LENGTH + NECP_CLIENT_ACTION_SIGN_TAG_LENGTH) {
11169 		return EINVAL;
11170 	}
11171 
11172 	necp_lock_socket_attributes();
11173 	if (inp->inp_resolver_signature != NULL) {
11174 		kfree_data_sized_by(inp->inp_resolver_signature, inp->inp_resolver_signature_length);
11175 	}
11176 
11177 	int error = 0;
11178 	if (valsize > 0) {
11179 		inp->inp_resolver_signature = kalloc_data(valsize, Z_WAITOK | Z_ZERO);
11180 		inp->inp_resolver_signature_length = valsize;
11181 		if ((error = sooptcopyin(sopt, inp->inp_resolver_signature, valsize,
11182 		    valsize)) != 0) {
11183 			// Free the signature buffer if the copyin failed
11184 			kfree_data_sized_by(inp->inp_resolver_signature, inp->inp_resolver_signature_length);
11185 		}
11186 	}
11187 	necp_unlock_socket_attributes();
11188 
11189 	return error;
11190 }
11191 
11192 int
necp_get_socket_resolver_signature(struct inpcb * inp,struct sockopt * sopt)11193 necp_get_socket_resolver_signature(struct inpcb *inp, struct sockopt *sopt)
11194 {
11195 	int error = 0;
11196 	necp_lock_socket_attributes();
11197 	if (inp->inp_resolver_signature == NULL ||
11198 	    inp->inp_resolver_signature_length == 0) {
11199 		error = ENOENT;
11200 	} else {
11201 		error = sooptcopyout(sopt, inp->inp_resolver_signature,
11202 		    inp->inp_resolver_signature_length);
11203 	}
11204 	necp_unlock_socket_attributes();
11205 	return error;
11206 }
11207 
11208 bool
necp_socket_has_resolver_signature(struct inpcb * inp)11209 necp_socket_has_resolver_signature(struct inpcb *inp)
11210 {
11211 	necp_lock_socket_attributes();
11212 	bool has_signature = (inp->inp_resolver_signature != NULL && inp->inp_resolver_signature_length != 0);
11213 	necp_unlock_socket_attributes();
11214 	return has_signature;
11215 }
11216 
11217 bool
necp_socket_resolver_signature_matches_address(struct inpcb * inp,union necp_sockaddr_union * address)11218 necp_socket_resolver_signature_matches_address(struct inpcb *inp, union necp_sockaddr_union *address)
11219 {
11220 	bool matches_address = false;
11221 	necp_lock_socket_attributes();
11222 	if (inp->inp_resolver_signature != NULL && inp->inp_resolver_signature_length > 0 && address->sa.sa_len > 0) {
11223 		struct necp_client_validatable *validatable = (struct necp_client_validatable *)inp->inp_resolver_signature;
11224 		if (inp->inp_resolver_signature_length > sizeof(struct necp_client_validatable) &&
11225 		    validatable->signable.sign_type == NECP_CLIENT_SIGN_TYPE_SYSTEM_RESOLVER_ANSWER) {
11226 			size_t data_length = inp->inp_resolver_signature_length - sizeof(struct necp_client_validatable);
11227 			if (data_length >= (sizeof(struct necp_client_host_resolver_answer) - sizeof(struct necp_client_signable))) {
11228 				struct necp_client_host_resolver_answer * __single answer_struct = (struct necp_client_host_resolver_answer *)&validatable->signable;
11229 				struct sockaddr_in6 sin6 = answer_struct->address_answer.sin6;
11230 				if (data_length == (sizeof(struct necp_client_host_resolver_answer) + answer_struct->hostname_length - sizeof(struct necp_client_signable)) &&
11231 				    answer_struct->address_answer.sa.sa_family == address->sa.sa_family &&
11232 				    answer_struct->address_answer.sa.sa_len == address->sa.sa_len &&
11233 				    (answer_struct->address_answer.sin.sin_port == 0 ||
11234 				    answer_struct->address_answer.sin.sin_port == address->sin.sin_port) &&
11235 				    ((answer_struct->address_answer.sa.sa_family == AF_INET &&
11236 				    answer_struct->address_answer.sin.sin_addr.s_addr == address->sin.sin_addr.s_addr) ||
11237 				    (answer_struct->address_answer.sa.sa_family == AF_INET6 &&
11238 				    memcmp(&sin6.sin6_addr, &address->sin6.sin6_addr, sizeof(struct in6_addr)) == 0))) {
11239 					// Address matches
11240 					const bool validated = necp_validate_resolver_answer(validatable->signable.client_id,
11241 					    validatable->signable.sign_type,
11242 					    signable_get_data(&validatable->signable, data_length), data_length,
11243 					    validatable->signature.signed_tag, sizeof(validatable->signature.signed_tag));
11244 					if (validated) {
11245 						// Answer is validated
11246 						matches_address = true;
11247 					}
11248 				}
11249 			}
11250 		}
11251 	}
11252 	necp_unlock_socket_attributes();
11253 	return matches_address;
11254 }
11255 
11256 /*
11257  * necp_set_socket_domain_attributes
11258  * Called from soconnectlock/soconnectxlock to directly set the tracker domain and owner for
11259  * a newly marked tracker socket.
11260  */
11261 errno_t
necp_set_socket_domain_attributes(struct socket * so,const char * domain __null_terminated,const char * domain_owner __null_terminated)11262 necp_set_socket_domain_attributes(struct socket *so, const char *domain __null_terminated, const char *domain_owner __null_terminated)
11263 {
11264 	int error = 0;
11265 	struct inpcb * __single inp = NULL;
11266 	size_t valsize = 0;
11267 	size_t buffer_size = 0;
11268 	u_int8_t * __sized_by(buffer_size) buffer = NULL;
11269 	char * __indexable buffer_to_free = NULL;
11270 
11271 	if (SOCK_DOM(so) != PF_INET && SOCK_DOM(so) != PF_INET6) {
11272 		error = EINVAL;
11273 		goto fail;
11274 	}
11275 
11276 	// Set domain (required)
11277 
11278 	valsize = strlen(domain);
11279 	if (valsize == 0 || valsize > NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) {
11280 		error = EINVAL;
11281 		goto fail;
11282 	}
11283 
11284 	buffer = (u_int8_t *)kalloc_data(valsize + 1, Z_WAITOK | Z_ZERO);
11285 	buffer_size = valsize + 1;
11286 	if (buffer == NULL) {
11287 		error = ENOMEM;
11288 		goto fail;
11289 	}
11290 	strlcpy((char *)buffer, domain, buffer_size);
11291 	buffer[valsize] = 0;
11292 
11293 	inp = sotoinpcb(so);
11294 	// Do not overwrite a previously set domain if tracker domain is different.
11295 	if (inp->inp_necp_attributes.inp_domain != NULL) {
11296 		if (strlen(inp->inp_necp_attributes.inp_domain) != strlen(domain) ||
11297 		    strcmp(inp->inp_necp_attributes.inp_domain, domain) != 0) {
11298 			buffer_to_free = (inp->inp_necp_attributes.inp_tracker_domain != NULL) ? __unsafe_null_terminated_to_indexable(inp->inp_necp_attributes.inp_tracker_domain) : NULL;
11299 			// Protect switching of buffer pointer
11300 			necp_lock_socket_attributes();
11301 			inp->inp_necp_attributes.inp_tracker_domain = __unsafe_null_terminated_from_indexable((char *)buffer, (char *)&buffer[valsize]);
11302 			necp_unlock_socket_attributes();
11303 			if (buffer_to_free != NULL) {
11304 				kfree_data_addr(buffer_to_free);
11305 			}
11306 		} else {
11307 			kfree_data_sized_by(buffer, buffer_size);
11308 		}
11309 	} else {
11310 		// Protect switching of buffer pointer
11311 		necp_lock_socket_attributes();
11312 		inp->inp_necp_attributes.inp_domain = __unsafe_null_terminated_from_indexable((char *)buffer, (char *)&buffer[valsize]);
11313 		necp_unlock_socket_attributes();
11314 	}
11315 	buffer = NULL;
11316 	buffer_size = 0;
11317 
11318 	// set domain_owner (required only for tracker)
11319 	if (!(so->so_flags1 & SOF1_KNOWN_TRACKER)) {
11320 		goto done;
11321 	}
11322 
11323 	valsize = strlen(domain_owner);
11324 	if (valsize == 0 || valsize > NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) {
11325 		error = EINVAL;
11326 		goto fail;
11327 	}
11328 
11329 	buffer = (u_int8_t *)kalloc_data(valsize + 1, Z_WAITOK | Z_ZERO);
11330 	buffer_size = valsize + 1;
11331 	if (buffer == NULL) {
11332 		error = ENOMEM;
11333 		goto fail;
11334 	}
11335 	strlcpy((char *)buffer, domain_owner, buffer_size);
11336 	buffer[valsize] = 0;
11337 
11338 	inp = sotoinpcb(so);
11339 
11340 	buffer_to_free = (inp->inp_necp_attributes.inp_domain_owner != NULL) ? __unsafe_null_terminated_to_indexable(inp->inp_necp_attributes.inp_domain_owner) : NULL;
11341 	// Protect switching of buffer pointer
11342 	necp_lock_socket_attributes();
11343 	inp->inp_necp_attributes.inp_domain_owner = __unsafe_null_terminated_from_indexable((char *)buffer, (char *)&buffer[valsize]);
11344 	necp_unlock_socket_attributes();
11345 	buffer = NULL;
11346 	buffer_size = 0;
11347 
11348 	if (buffer_to_free != NULL) {
11349 		kfree_data_addr(buffer_to_free);
11350 	}
11351 
11352 done:
11353 	NECP_SOCKET_PARAMS_LOG(so, "NECP ATTRIBUTES SOCKET - domain <%s> owner <%s> context <%s> tracker domain <%s> account <%s> "
11354 	    "<so flags - is_tracker %X non-app-initiated %X app-approved-domain %X",
11355 	    inp->inp_necp_attributes.inp_domain,
11356 	    inp->inp_necp_attributes.inp_domain_owner,
11357 	    inp->inp_necp_attributes.inp_domain_context,
11358 	    inp->inp_necp_attributes.inp_tracker_domain,
11359 	    inp->inp_necp_attributes.inp_account,
11360 	    so->so_flags1 & SOF1_KNOWN_TRACKER,
11361 	    so->so_flags1 & SOF1_TRACKER_NON_APP_INITIATED,
11362 	    so->so_flags1 & SOF1_APPROVED_APP_DOMAIN);
11363 
11364 	if (necp_debug) {
11365 		NECPLOG(LOG_DEBUG, "Set on socket: Domain <%s> Domain owner <%s> Domain context <%s> Tracker domain <%s> Account <%s> ",
11366 		    inp->inp_necp_attributes.inp_domain,
11367 		    inp->inp_necp_attributes.inp_domain_owner,
11368 		    inp->inp_necp_attributes.inp_domain_context,
11369 		    inp->inp_necp_attributes.inp_tracker_domain,
11370 		    inp->inp_necp_attributes.inp_account);
11371 	}
11372 fail:
11373 	if (buffer != NULL) {
11374 		kfree_data_sized_by(buffer, buffer_size);
11375 	}
11376 	return error;
11377 }
11378 
11379 void *
11380 __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)11381 necp_create_nexus_assign_message(uuid_t nexus_instance, nexus_port_t nexus_port, void * __sized_by(key_length) key, uint32_t key_length,
11382     struct necp_client_endpoint *local_endpoint, struct necp_client_endpoint *remote_endpoint, struct ether_addr *local_ether_addr,
11383     u_int32_t flow_adv_index, void *flow_stats, size_t *message_length)
11384 {
11385 	u_int8_t * __indexable buffer = NULL;
11386 	u_int8_t * __indexable cursor = NULL;
11387 	size_t valsize = 0;
11388 	bool has_nexus_assignment = FALSE;
11389 
11390 	if (!uuid_is_null(nexus_instance)) {
11391 		has_nexus_assignment = TRUE;
11392 		valsize += sizeof(struct necp_tlv_header) + sizeof(uuid_t);
11393 		valsize += sizeof(struct necp_tlv_header) + sizeof(nexus_port_t);
11394 	}
11395 	if (flow_adv_index != NECP_FLOWADV_IDX_INVALID) {
11396 		valsize += sizeof(struct necp_tlv_header) + sizeof(u_int32_t);
11397 	}
11398 	if (key != NULL && key_length > 0) {
11399 		valsize += sizeof(struct necp_tlv_header) + key_length;
11400 	}
11401 	if (local_endpoint != NULL) {
11402 		valsize += sizeof(struct necp_tlv_header) + sizeof(struct necp_client_endpoint);
11403 	}
11404 	if (remote_endpoint != NULL) {
11405 		valsize += sizeof(struct necp_tlv_header) + sizeof(struct necp_client_endpoint);
11406 	}
11407 	if (local_ether_addr != NULL) {
11408 		valsize += sizeof(struct necp_tlv_header) + sizeof(struct ether_addr);
11409 	}
11410 	if (flow_stats != NULL) {
11411 		valsize += sizeof(struct necp_tlv_header) + sizeof(void *);
11412 	}
11413 	if (valsize == 0) {
11414 		*message_length = 0;
11415 		return NULL;
11416 	}
11417 
11418 	buffer = kalloc_data(valsize, Z_WAITOK | Z_ZERO);
11419 	if (buffer == NULL) {
11420 		*message_length = 0;
11421 		return NULL;
11422 	}
11423 
11424 	cursor = buffer;
11425 	if (has_nexus_assignment) {
11426 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_INSTANCE, sizeof(uuid_t), nexus_instance, buffer, valsize);
11427 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_PORT, sizeof(nexus_port_t), &nexus_port, buffer, valsize);
11428 	}
11429 	if (flow_adv_index != NECP_FLOWADV_IDX_INVALID) {
11430 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_PORT_FLOW_INDEX, sizeof(u_int32_t), &flow_adv_index, buffer, valsize);
11431 	}
11432 	if (key != NULL && key_length > 0) {
11433 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_PARAMETER_NEXUS_KEY, key_length, key, buffer, valsize);
11434 	}
11435 	if (local_endpoint != NULL) {
11436 		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);
11437 	}
11438 	if (remote_endpoint != NULL) {
11439 		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);
11440 	}
11441 	if (local_ether_addr != NULL) {
11442 		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);
11443 	}
11444 	if (flow_stats != NULL) {
11445 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_FLOW_STATS, sizeof(void *), &flow_stats, buffer, valsize);
11446 	}
11447 
11448 	*message_length = valsize;
11449 
11450 	return buffer;
11451 }
11452 
11453 void
necp_inpcb_remove_cb(struct inpcb * inp)11454 necp_inpcb_remove_cb(struct inpcb *inp)
11455 {
11456 	if (!uuid_is_null(inp->necp_client_uuid)) {
11457 		necp_client_unregister_socket_flow(inp->necp_client_uuid, inp);
11458 		uuid_clear(inp->necp_client_uuid);
11459 	}
11460 }
11461 
11462 void
necp_inpcb_dispose(struct inpcb * inp)11463 necp_inpcb_dispose(struct inpcb *inp)
11464 {
11465 	char * __indexable buffer = NULL;
11466 
11467 	necp_inpcb_remove_cb(inp); // Clear out socket registrations if not yet done
11468 	if (inp->inp_necp_attributes.inp_domain != NULL) {
11469 		buffer = __unsafe_null_terminated_to_indexable(inp->inp_necp_attributes.inp_domain);
11470 		kfree_data_addr(buffer);
11471 		inp->inp_necp_attributes.inp_domain = NULL;
11472 	}
11473 	if (inp->inp_necp_attributes.inp_account != NULL) {
11474 		buffer = __unsafe_null_terminated_to_indexable(inp->inp_necp_attributes.inp_account);
11475 		kfree_data_addr(buffer);
11476 		inp->inp_necp_attributes.inp_account = NULL;
11477 	}
11478 	if (inp->inp_necp_attributes.inp_domain_owner != NULL) {
11479 		buffer = __unsafe_null_terminated_to_indexable(inp->inp_necp_attributes.inp_domain_owner);
11480 		kfree_data_addr(buffer);
11481 		inp->inp_necp_attributes.inp_domain_owner = NULL;
11482 	}
11483 	if (inp->inp_necp_attributes.inp_domain_context != NULL) {
11484 		buffer = __unsafe_null_terminated_to_indexable(inp->inp_necp_attributes.inp_domain_context);
11485 		kfree_data_addr(buffer);
11486 		inp->inp_necp_attributes.inp_domain_context = NULL;
11487 	}
11488 	if (inp->inp_necp_attributes.inp_tracker_domain != NULL) {
11489 		buffer = __unsafe_null_terminated_to_indexable(inp->inp_necp_attributes.inp_tracker_domain);
11490 		kfree_data_addr(buffer);
11491 		inp->inp_necp_attributes.inp_tracker_domain = NULL;
11492 	}
11493 	if (inp->inp_resolver_signature != NULL) {
11494 		kfree_data_sized_by(inp->inp_resolver_signature, inp->inp_resolver_signature_length);
11495 	}
11496 }
11497 
11498 void
necp_mppcb_dispose(struct mppcb * mpp)11499 necp_mppcb_dispose(struct mppcb *mpp)
11500 {
11501 	char * __indexable buffer = NULL;
11502 
11503 	if (!uuid_is_null(mpp->necp_client_uuid)) {
11504 		necp_client_unregister_multipath_cb(mpp->necp_client_uuid, mpp);
11505 		uuid_clear(mpp->necp_client_uuid);
11506 	}
11507 
11508 	if (mpp->inp_necp_attributes.inp_domain != NULL) {
11509 		buffer = __unsafe_null_terminated_to_indexable(mpp->inp_necp_attributes.inp_domain);
11510 		kfree_data_addr(buffer);
11511 		mpp->inp_necp_attributes.inp_domain = NULL;
11512 	}
11513 	if (mpp->inp_necp_attributes.inp_account != NULL) {
11514 		buffer = __unsafe_null_terminated_to_indexable(mpp->inp_necp_attributes.inp_account);
11515 		kfree_data_addr(buffer);
11516 		mpp->inp_necp_attributes.inp_account = NULL;
11517 	}
11518 	if (mpp->inp_necp_attributes.inp_domain_owner != NULL) {
11519 		buffer = __unsafe_null_terminated_to_indexable(mpp->inp_necp_attributes.inp_domain_owner);
11520 		kfree_data_addr(buffer);
11521 		mpp->inp_necp_attributes.inp_domain_owner = NULL;
11522 	}
11523 	if (mpp->inp_necp_attributes.inp_tracker_domain != NULL) {
11524 		buffer = __unsafe_null_terminated_to_indexable(mpp->inp_necp_attributes.inp_tracker_domain);
11525 		kfree_data_addr(buffer);
11526 		mpp->inp_necp_attributes.inp_tracker_domain = NULL;
11527 	}
11528 	if (mpp->inp_necp_attributes.inp_domain_context != NULL) {
11529 		buffer = __unsafe_null_terminated_to_indexable(mpp->inp_necp_attributes.inp_domain_context);
11530 		kfree_data_addr(buffer);
11531 		mpp->inp_necp_attributes.inp_domain_context = NULL;
11532 	}
11533 }
11534 
11535 /// Module init
11536 
11537 void
necp_client_init(void)11538 necp_client_init(void)
11539 {
11540 	necp_client_update_tcall = thread_call_allocate_with_options(necp_update_all_clients_callout, NULL,
11541 	    THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
11542 	VERIFY(necp_client_update_tcall != NULL);
11543 #if SKYWALK
11544 
11545 	necp_client_collect_stats_tcall = thread_call_allocate_with_options(necp_collect_stats_client_callout, NULL,
11546 	    THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
11547 	VERIFY(necp_client_collect_stats_tcall != NULL);
11548 
11549 	necp_close_empty_arenas_tcall = thread_call_allocate_with_options(necp_close_empty_arenas_callout, NULL,
11550 	    THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
11551 	VERIFY(necp_close_empty_arenas_tcall != NULL);
11552 #endif /* SKYWALK */
11553 
11554 	LIST_INIT(&necp_fd_list);
11555 	LIST_INIT(&necp_fd_observer_list);
11556 	LIST_INIT(&necp_collect_stats_flow_list);
11557 
11558 	RB_INIT(&necp_client_global_tree);
11559 	RB_INIT(&necp_client_flow_global_tree);
11560 }
11561 
11562 #if SKYWALK
11563 pid_t
necp_client_get_proc_pid_from_arena_info(struct skmem_arena_mmap_info * arena_info)11564 necp_client_get_proc_pid_from_arena_info(struct skmem_arena_mmap_info *arena_info)
11565 {
11566 	ASSERT((arena_info->ami_arena->ar_type == SKMEM_ARENA_TYPE_NECP) || (arena_info->ami_arena->ar_type == SKMEM_ARENA_TYPE_SYSTEM));
11567 
11568 	if (arena_info->ami_arena->ar_type == SKMEM_ARENA_TYPE_NECP) {
11569 		struct necp_arena_info * __single nai = __unsafe_forge_single(struct necp_arena_info *, container_of(arena_info, struct necp_arena_info, nai_mmap));
11570 		return nai->nai_proc_pid;
11571 	} else {
11572 		struct necp_fd_data * __single fd_data = __unsafe_forge_single(struct necp_fd_data *, container_of(arena_info, struct necp_fd_data, sysctl_mmap));
11573 		return fd_data->proc_pid;
11574 	}
11575 }
11576 #endif /* !SKYWALK */
11577