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