xref: /xnu-8792.41.9/bsd/net/necp_client.c (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
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 	lck_mtx_init(&client->lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
6906 	lck_mtx_init(&client->route_lock, &necp_fd_mtx_grp, &necp_fd_mtx_attr);
6907 
6908 	error = copyin(uap->buffer, client->parameters, buffer_size);
6909 	if (error) {
6910 		NECPLOG(LOG_ERR, "necp_client_add parameters copyin error (%d)", error);
6911 		goto done;
6912 	}
6913 
6914 	os_ref_init(&client->reference_count, &necp_client_refgrp); // Hold our reference until close
6915 
6916 	client->parameters_length = buffer_size;
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 		uuid_copy(flow_info.nfi_flow_uuid, client->client_id);
6987 		flow_info.nfi_protocol = parsed_parameters.ip_protocol;
6988 		flow_info.nfi_owner_pid = client->proc_pid;
6989 		if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID) {
6990 			flow_info.nfi_effective_pid = parsed_parameters.effective_pid;
6991 		} else {
6992 			flow_info.nfi_effective_pid = flow_info.nfi_owner_pid;
6993 		}
6994 		proc_name(flow_info.nfi_owner_pid, flow_info.nfi_owner_name, MAXCOMLEN);
6995 		proc_name(flow_info.nfi_effective_pid, flow_info.nfi_effective_name, MAXCOMLEN);
6996 
6997 		if (parsed_parameters.local_addr.sa.sa_family == AF_UNSPEC) {
6998 			// Treat no local address as a wildcard IPv6
6999 			// parsed_parameters is already initialized to all zeros
7000 			parsed_parameters.local_addr.sin6.sin6_family = AF_INET6;
7001 			parsed_parameters.local_addr.sin6.sin6_len = sizeof(struct sockaddr_in6);
7002 		}
7003 
7004 		switch (parsed_parameters.local_addr.sa.sa_family) {
7005 		case AF_INET: {
7006 			memcpy(&flow_info.nfi_laddr, &parsed_parameters.local_addr.sa, parsed_parameters.local_addr.sa.sa_len);
7007 			netns_addr = (uint32_t *)&parsed_parameters.local_addr.sin.sin_addr;
7008 			netns_addr_len = 4;
7009 			break;
7010 		}
7011 		case AF_INET6: {
7012 			memcpy(&flow_info.nfi_laddr, &parsed_parameters.local_addr.sa, parsed_parameters.local_addr.sa.sa_len);
7013 			netns_addr = (uint32_t *)&parsed_parameters.local_addr.sin6.sin6_addr;
7014 			netns_addr_len = 16;
7015 			break;
7016 		}
7017 
7018 		default: {
7019 			NECPLOG(LOG_ERR, "necp_client_add listener invalid address family (%d)", parsed_parameters.local_addr.sa.sa_family);
7020 			error = EINVAL;
7021 			goto done;
7022 		}
7023 		}
7024 		if (parsed_parameters.local_addr.sin.sin_port == 0) {
7025 			error = netns_reserve_ephemeral(&client->port_reservation, netns_addr, netns_addr_len, parsed_parameters.ip_protocol,
7026 			    &parsed_parameters.local_addr.sin.sin_port, NETNS_LISTENER, &flow_info);
7027 			if (error) {
7028 				NECPLOG(LOG_ERR, "necp_client_add netns_reserve_ephemeral error (%d)", error);
7029 				goto done;
7030 			}
7031 
7032 			// Update the parameter TLVs with the assigned port
7033 			necp_client_update_local_port_parameters(client->parameters, (u_int32_t)client->parameters_length, parsed_parameters.local_addr.sin.sin_port);
7034 		} else {
7035 			error = netns_reserve(&client->port_reservation, netns_addr, netns_addr_len, parsed_parameters.ip_protocol,
7036 			    parsed_parameters.local_addr.sin.sin_port, NETNS_LISTENER, &flow_info);
7037 			if (error) {
7038 				NECPLOG(LOG_ERR, "necp_client_add netns_reserve error (%d)", error);
7039 				goto done;
7040 			}
7041 		}
7042 	}
7043 
7044 	struct necp_client *parent = NULL;
7045 	uuid_t parent_client_id;
7046 	uuid_clear(parent_client_id);
7047 	struct necp_client_nexus_parameters parent_parameters = {};
7048 	uint16_t num_flow_regs = 0;
7049 	if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_PARENT_UUID) {
7050 		// The parent "should" be found on fd_data without having to search across the whole necp_fd_list
7051 		// It would be nice to do this a little further down where there's another instance of NECP_FD_LOCK
7052 		// but the logic here depends on the parse paramters
7053 		NECP_FD_LOCK(fd_data);
7054 		parent = necp_client_fd_find_client_unlocked(fd_data, parsed_parameters.parent_uuid);
7055 		if (parent != NULL) {
7056 			necp_client_inherit_from_parent(client, parent);
7057 			necp_client_copy_parameters_locked(client, &parent_parameters);
7058 			uuid_copy(parent_client_id, parsed_parameters.parent_uuid);
7059 			struct necp_client_flow_registration *flow_registration = NULL;
7060 			RB_FOREACH(flow_registration, _necp_client_flow_tree, &parent->flow_registrations) {
7061 				num_flow_regs++;
7062 			}
7063 		}
7064 		NECP_FD_UNLOCK(fd_data);
7065 		if (parent == NULL) {
7066 			NECPLOG0(LOG_ERR, "necp_client_add, no necp_client_inherit_from_parent as can't find parent on fd_data");
7067 		}
7068 	}
7069 	if (parse_error == 0 && parent != NULL && parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLOW_DEMUX_PATTERN) {
7070 		do {
7071 			if (parsed_parameters.demux_patterns[0].len == 0) {
7072 				NECPLOG0(LOG_INFO, "necp_client_add, child does not have a demux pattern");
7073 				break;
7074 			}
7075 
7076 			if (uuid_is_null(parent_client_id)) {
7077 				NECPLOG0(LOG_INFO, "necp_client_add, parent ID is null");
7078 				break;
7079 			}
7080 
7081 			if (num_flow_regs > 1) {
7082 				NECPLOG0(LOG_INFO, "necp_client_add, multiple parent flows not supported");
7083 				break;
7084 			}
7085 			if (parsed_parameters.ip_protocol != IPPROTO_UDP) {
7086 				NECPLOG(LOG_INFO, "necp_client_add, flow demux pattern not supported for %d protocol",
7087 				    parsed_parameters.ip_protocol);
7088 				break;
7089 			}
7090 			if (parsed_parameters.ip_protocol != parent_parameters.transport_protocol) {
7091 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child ip protocol mismatch");
7092 				break;
7093 			}
7094 			if (parsed_parameters.local_addr.sa.sa_family != AF_INET && parsed_parameters.local_addr.sa.sa_family != AF_INET6) {
7095 				NECPLOG(LOG_INFO, "necp_client_add, flow demux pattern not supported for %d family",
7096 				    parsed_parameters.local_addr.sa.sa_family);
7097 				break;
7098 			}
7099 			if (parsed_parameters.local_addr.sa.sa_family != parsed_parameters.remote_addr.sa.sa_family) {
7100 				NECPLOG0(LOG_INFO, "necp_client_add, local/remote address family mismatch");
7101 				break;
7102 			}
7103 			if (parsed_parameters.local_addr.sa.sa_family != parent_parameters.local_addr.sa.sa_family) {
7104 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child address family mismatch");
7105 				break;
7106 			}
7107 			if (memcmp(&parsed_parameters.local_addr.sa, &parent_parameters.local_addr.sa, parsed_parameters.local_addr.sa.sa_len)) {
7108 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child local address mismatch");
7109 				break;
7110 			}
7111 			if (memcmp(&parsed_parameters.remote_addr.sa, &parent_parameters.remote_addr.sa, parsed_parameters.remote_addr.sa.sa_len)) {
7112 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child remote address mismatch");
7113 				break;
7114 			}
7115 			if (parsed_parameters.local_addr.sin.sin_port != parent_parameters.local_addr.sin.sin_port) {
7116 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child local port mismatch");
7117 				break;
7118 			}
7119 			if (parsed_parameters.remote_addr.sin.sin_port != parent_parameters.remote_addr.sin.sin_port) {
7120 				NECPLOG0(LOG_INFO, "necp_client_add, parent/child remote port mismatch");
7121 				break;
7122 			}
7123 			client->validated_parent = 1;
7124 			uuid_copy(client->parent_client_id, parent_client_id);
7125 		} while (false);
7126 	}
7127 
7128 #endif /* !SKYWALK */
7129 
7130 	necp_client_update_observer_add(client);
7131 
7132 	NECP_FD_LOCK(fd_data);
7133 	RB_INSERT(_necp_client_tree, &fd_data->clients, client);
7134 	OSIncrementAtomic(&necp_client_count);
7135 	NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
7136 	RB_INSERT(_necp_client_global_tree, &necp_client_global_tree, client);
7137 	NECP_CLIENT_TREE_UNLOCK();
7138 
7139 	// Prime the client result
7140 	NECP_CLIENT_LOCK(client);
7141 	(void)necp_update_client_result(current_proc(), fd_data, client, NULL);
7142 	necp_client_retain_locked(client);
7143 	NECP_CLIENT_UNLOCK(client);
7144 	NECP_FD_UNLOCK(fd_data);
7145 	// Now everything is set, it's safe to plumb this in to NetworkStatistics
7146 	uint32_t ntstat_properties = 0;
7147 	necp_find_conn_netstat_data(client, &ntstat_properties, NULL, NULL, NULL);
7148 
7149 	client->nstat_context = nstat_provider_stats_open((nstat_provider_context)client,
7150 	    NSTAT_PROVIDER_CONN_USERLAND, (u_int64_t)ntstat_properties, necp_request_conn_netstats, necp_find_conn_extension_info);
7151 	necp_client_release(client);
7152 done:
7153 	if (error != 0 && client != NULL) {
7154 		necp_client_free(client);
7155 		client = NULL;
7156 	}
7157 	*retval = error;
7158 
7159 	return error;
7160 }
7161 
7162 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)7163 necp_client_claim(struct proc *p, struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7164 {
7165 	int error = 0;
7166 	uuid_t client_id = {};
7167 	struct necp_client *client = NULL;
7168 
7169 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
7170 		error = EINVAL;
7171 		goto done;
7172 	}
7173 
7174 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
7175 	if (error) {
7176 		NECPLOG(LOG_ERR, "necp_client_claim copyin client_id error (%d)", error);
7177 		goto done;
7178 	}
7179 
7180 	u_int64_t upid = proc_uniqueid(p);
7181 
7182 	NECP_FD_LIST_LOCK_SHARED();
7183 
7184 	struct necp_fd_data *find_fd = NULL;
7185 	LIST_FOREACH(find_fd, &necp_fd_list, chain) {
7186 		NECP_FD_LOCK(find_fd);
7187 		struct necp_client *find_client = necp_client_fd_find_client_and_lock(find_fd, client_id);
7188 		if (find_client != NULL) {
7189 			if (find_client->delegated_upid == upid) {
7190 				// Matched the client to claim; remove from the old fd
7191 				client = find_client;
7192 				RB_REMOVE(_necp_client_tree, &find_fd->clients, client);
7193 				necp_client_retain_locked(client);
7194 			}
7195 			NECP_CLIENT_UNLOCK(find_client);
7196 		}
7197 		NECP_FD_UNLOCK(find_fd);
7198 
7199 		if (client != NULL) {
7200 			break;
7201 		}
7202 	}
7203 
7204 	NECP_FD_LIST_UNLOCK();
7205 
7206 	if (client == NULL) {
7207 		error = ENOENT;
7208 		goto done;
7209 	}
7210 
7211 	client->proc_pid = fd_data->proc_pid; // Transfer client to claiming pid
7212 	client->agent_handle = (void *)fd_data;
7213 	client->platform_binary = ((csproc_get_platform_binary(p) == 0) ? 0 : 1);
7214 
7215 	NECP_CLIENT_LOG(client, "Claiming client");
7216 
7217 	// Add matched client to our fd and re-run result
7218 	NECP_FD_LOCK(fd_data);
7219 	RB_INSERT(_necp_client_tree, &fd_data->clients, client);
7220 	NECP_CLIENT_LOCK(client);
7221 	(void)necp_update_client_result(current_proc(), fd_data, client, NULL);
7222 	NECP_CLIENT_UNLOCK(client);
7223 	NECP_FD_UNLOCK(fd_data);
7224 
7225 	necp_client_release(client);
7226 
7227 done:
7228 	*retval = error;
7229 
7230 	return error;
7231 }
7232 
7233 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_remove(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)7234 necp_client_remove(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7235 {
7236 	int error = 0;
7237 	uuid_t client_id = {};
7238 	struct ifnet_stats_per_flow flow_ifnet_stats = {};
7239 	const size_t buffer_size = uap->buffer_size;
7240 
7241 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
7242 		error = EINVAL;
7243 		goto done;
7244 	}
7245 
7246 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
7247 	if (error) {
7248 		NECPLOG(LOG_ERR, "necp_client_remove copyin client_id error (%d)", error);
7249 		goto done;
7250 	}
7251 
7252 	if (uap->buffer != 0 && buffer_size == sizeof(flow_ifnet_stats)) {
7253 		error = copyin(uap->buffer, &flow_ifnet_stats, buffer_size);
7254 		if (error) {
7255 			NECPLOG(LOG_ERR, "necp_client_remove flow_ifnet_stats copyin error (%d)", error);
7256 			// Not fatal; make sure to zero-out stats in case of partial copy
7257 			memset(&flow_ifnet_stats, 0, sizeof(flow_ifnet_stats));
7258 			error = 0;
7259 		}
7260 	} else if (uap->buffer != 0) {
7261 		NECPLOG(LOG_ERR, "necp_client_remove unexpected parameters length (%zu)", buffer_size);
7262 	}
7263 
7264 	NECP_FD_LOCK(fd_data);
7265 
7266 	pid_t pid = fd_data->proc_pid;
7267 	struct necp_client *client = necp_client_fd_find_client_unlocked(fd_data, client_id);
7268 
7269 	NECP_CLIENT_LOG(client, "Removing client");
7270 
7271 	if (client != NULL) {
7272 		// Remove any flow registrations that match
7273 		struct necp_client_flow_registration *flow_registration = NULL;
7274 		struct necp_client_flow_registration *temp_flow_registration = NULL;
7275 		RB_FOREACH_SAFE(flow_registration, _necp_fd_flow_tree, &fd_data->flows, temp_flow_registration) {
7276 			if (flow_registration->client == client) {
7277 #if SKYWALK
7278 				necp_destroy_flow_stats(fd_data, flow_registration, NULL, TRUE);
7279 #endif /* SKYWALK */
7280 				NECP_FLOW_TREE_LOCK_EXCLUSIVE();
7281 				RB_REMOVE(_necp_client_flow_global_tree, &necp_client_flow_global_tree, flow_registration);
7282 				NECP_FLOW_TREE_UNLOCK();
7283 				RB_REMOVE(_necp_fd_flow_tree, &fd_data->flows, flow_registration);
7284 			}
7285 		}
7286 #if SKYWALK
7287 		if (client->nstat_context != NULL) {
7288 			// Main path, we expect stats to be in existance at this point
7289 			nstat_provider_stats_close(client->nstat_context);
7290 			client->nstat_context = NULL;
7291 		} else {
7292 			NECPLOG0(LOG_ERR, "necp_client_remove ntstat shutdown finds nstat_context NULL");
7293 		}
7294 #endif /* SKYWALK */
7295 		// Remove client from lists
7296 		NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
7297 		RB_REMOVE(_necp_client_global_tree, &necp_client_global_tree, client);
7298 		NECP_CLIENT_TREE_UNLOCK();
7299 		RB_REMOVE(_necp_client_tree, &fd_data->clients, client);
7300 	}
7301 
7302 #if SKYWALK
7303 	// If the currently-active arena is idle (has no more flows referring to it), or if there are defunct
7304 	// arenas lingering in the list, schedule a threadcall to do the clean up.  The idle check is done
7305 	// by checking if the reference count is 3: one held by this client (will be released below when we
7306 	// destroy it) when it's non-NULL; the rest held by stats_arena_{active,list}.
7307 	if ((fd_data->stats_arena_active != NULL && fd_data->stats_arena_active->nai_use_count == 3) ||
7308 	    (fd_data->stats_arena_active == NULL && !LIST_EMPTY(&fd_data->stats_arena_list))) {
7309 		uint64_t deadline = 0;
7310 		uint64_t leeway = 0;
7311 		clock_interval_to_deadline(necp_close_arenas_timeout_microseconds, NSEC_PER_USEC, &deadline);
7312 		clock_interval_to_absolutetime_interval(necp_close_arenas_timeout_leeway_microseconds, NSEC_PER_USEC, &leeway);
7313 
7314 		thread_call_enter_delayed_with_leeway(necp_close_empty_arenas_tcall, NULL,
7315 		    deadline, leeway, THREAD_CALL_DELAY_LEEWAY);
7316 	}
7317 #endif /* SKYWALK */
7318 
7319 	NECP_FD_UNLOCK(fd_data);
7320 
7321 	if (client != NULL) {
7322 		ASSERT(error == 0);
7323 		necp_destroy_client(client, pid, true);
7324 	} else {
7325 		error = ENOENT;
7326 		NECPLOG(LOG_ERR, "necp_client_remove invalid client_id (%d)", error);
7327 	}
7328 done:
7329 	*retval = error;
7330 
7331 	return error;
7332 }
7333 
7334 static struct necp_client_flow_registration *
necp_client_fd_find_flow(struct necp_fd_data * client_fd,uuid_t flow_id)7335 necp_client_fd_find_flow(struct necp_fd_data *client_fd, uuid_t flow_id)
7336 {
7337 	NECP_FD_ASSERT_LOCKED(client_fd);
7338 	struct necp_client_flow_registration *flow = NULL;
7339 
7340 	if (necp_client_id_is_flow(flow_id)) {
7341 		struct necp_client_flow_registration find;
7342 		uuid_copy(find.registration_id, flow_id);
7343 		flow = RB_FIND(_necp_fd_flow_tree, &client_fd->flows, &find);
7344 	}
7345 
7346 	return flow;
7347 }
7348 
7349 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_remove_flow(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)7350 necp_client_remove_flow(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7351 {
7352 	int error = 0;
7353 	uuid_t flow_id = {};
7354 	struct ifnet_stats_per_flow flow_ifnet_stats = {};
7355 	const size_t buffer_size = uap->buffer_size;
7356 
7357 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
7358 		error = EINVAL;
7359 		NECPLOG(LOG_ERR, "necp_client_remove_flow invalid client_id (length %zu)", (size_t)uap->client_id_len);
7360 		goto done;
7361 	}
7362 
7363 	error = copyin(uap->client_id, flow_id, sizeof(uuid_t));
7364 	if (error) {
7365 		NECPLOG(LOG_ERR, "necp_client_remove_flow copyin client_id error (%d)", error);
7366 		goto done;
7367 	}
7368 
7369 	if (uap->buffer != 0 && buffer_size == sizeof(flow_ifnet_stats)) {
7370 		error = copyin(uap->buffer, &flow_ifnet_stats, buffer_size);
7371 		if (error) {
7372 			NECPLOG(LOG_ERR, "necp_client_remove flow_ifnet_stats copyin error (%d)", error);
7373 			// Not fatal
7374 		}
7375 	} else if (uap->buffer != 0) {
7376 		NECPLOG(LOG_ERR, "necp_client_remove unexpected parameters length (%zu)", buffer_size);
7377 	}
7378 
7379 	NECP_FD_LOCK(fd_data);
7380 	struct necp_client *client = NULL;
7381 	struct necp_client_flow_registration *flow_registration = necp_client_fd_find_flow(fd_data, flow_id);
7382 	if (flow_registration != NULL) {
7383 #if SKYWALK
7384 		// Cleanup stats per flow
7385 		necp_destroy_flow_stats(fd_data, flow_registration, &flow_ifnet_stats, TRUE);
7386 #endif /* SKYWALK */
7387 		NECP_FLOW_TREE_LOCK_EXCLUSIVE();
7388 		RB_REMOVE(_necp_client_flow_global_tree, &necp_client_flow_global_tree, flow_registration);
7389 		NECP_FLOW_TREE_UNLOCK();
7390 		RB_REMOVE(_necp_fd_flow_tree, &fd_data->flows, flow_registration);
7391 
7392 		client = flow_registration->client;
7393 		if (client != NULL) {
7394 			necp_client_retain(client);
7395 		}
7396 	}
7397 	NECP_FD_UNLOCK(fd_data);
7398 
7399 	NECP_CLIENT_FLOW_LOG(client, flow_registration, "removing flow");
7400 
7401 	if (flow_registration != NULL && client != NULL) {
7402 		NECP_CLIENT_LOCK(client);
7403 		if (flow_registration->client == client) {
7404 			necp_destroy_client_flow_registration(client, flow_registration, fd_data->proc_pid, false);
7405 		}
7406 		necp_client_release_locked(client);
7407 		NECP_CLIENT_UNLOCK(client);
7408 	}
7409 
7410 done:
7411 	*retval = error;
7412 	if (error != 0) {
7413 		NECPLOG(LOG_ERR, "Remove flow error (%d)", error);
7414 	}
7415 
7416 	return error;
7417 }
7418 
7419 // Don't inline the function since it includes necp_client_parsed_parameters on the stack
7420 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)7421 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)
7422 {
7423 	struct necp_client_parsed_parameters parsed_parameters;
7424 	int error = 0;
7425 
7426 	error = necp_client_parse_parameters(client, client->parameters,
7427 	    (u_int32_t)client->parameters_length,
7428 	    &parsed_parameters);
7429 	if (error) {
7430 		NECPLOG(LOG_ERR, "necp_client_parse_parameters error (%d)", error);
7431 		return error;
7432 	}
7433 
7434 	if ((flow->remote_addr.sa.sa_family != AF_INET &&
7435 	    flow->remote_addr.sa.sa_family != AF_INET6) ||
7436 	    (flow->local_addr.sa.sa_family != AF_INET &&
7437 	    flow->local_addr.sa.sa_family != AF_INET6)) {
7438 		return EINVAL;
7439 	}
7440 
7441 	NECP_CLIENT_ROUTE_LOCK(client);
7442 
7443 	if (client->current_route == NULL) {
7444 		error = ENOENT;
7445 		goto do_unlock;
7446 	}
7447 
7448 	bool check_ecn = false;
7449 	do {
7450 		if ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_ECN_ENABLE) ==
7451 		    NECP_CLIENT_PARAMETER_FLAG_ECN_ENABLE) {
7452 			check_ecn = true;
7453 			break;
7454 		}
7455 
7456 		if ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_ECN_DISABLE) ==
7457 		    NECP_CLIENT_PARAMETER_FLAG_ECN_DISABLE) {
7458 			break;
7459 		}
7460 
7461 		if (client->current_route != NULL) {
7462 			if (client->current_route->rt_ifp->if_eflags & IFEF_ECN_ENABLE) {
7463 				check_ecn = true;
7464 				break;
7465 			}
7466 			if (client->current_route->rt_ifp->if_eflags & IFEF_ECN_DISABLE) {
7467 				break;
7468 			}
7469 		}
7470 
7471 		bool inbound = ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) == 0);
7472 		if ((inbound && tcp_ecn_inbound == 1) ||
7473 		    (!inbound && tcp_ecn_outbound == 1)) {
7474 			check_ecn = true;
7475 		}
7476 	} while (false);
7477 
7478 	if (check_ecn) {
7479 		if (tcp_heuristic_do_ecn_with_address(client->current_route->rt_ifp,
7480 		    (union sockaddr_in_4_6 *)&flow->local_addr)) {
7481 			*flags |= NECP_CLIENT_RESULT_FLAG_ECN_ENABLED;
7482 		}
7483 	}
7484 
7485 	if ((parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_TFO_ENABLE) ==
7486 	    NECP_CLIENT_PARAMETER_FLAG_TFO_ENABLE) {
7487 		if (!tcp_heuristic_do_tfo_with_address(client->current_route->rt_ifp,
7488 		    (union sockaddr_in_4_6 *)&flow->local_addr,
7489 		    (union sockaddr_in_4_6 *)&flow->remote_addr,
7490 		    tfo_cookie, tfo_cookie_len)) {
7491 			*flags |= NECP_CLIENT_RESULT_FLAG_FAST_OPEN_BLOCKED;
7492 			*tfo_cookie_len = 0;
7493 		}
7494 	} else {
7495 		*flags |= NECP_CLIENT_RESULT_FLAG_FAST_OPEN_BLOCKED;
7496 		*tfo_cookie_len = 0;
7497 	}
7498 do_unlock:
7499 	NECP_CLIENT_ROUTE_UNLOCK(client);
7500 
7501 	return error;
7502 }
7503 
7504 static size_t
necp_client_calculate_flow_tlv_size(struct necp_client_flow_registration * flow_registration)7505 necp_client_calculate_flow_tlv_size(struct necp_client_flow_registration *flow_registration)
7506 {
7507 	size_t assigned_results_size = 0;
7508 	struct necp_client_flow *flow = NULL;
7509 	LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
7510 		if (flow->assigned) {
7511 			size_t header_length = 0;
7512 			if (flow->nexus) {
7513 				header_length = sizeof(struct necp_client_nexus_flow_header);
7514 			} else {
7515 				header_length = sizeof(struct necp_client_flow_header);
7516 			}
7517 			assigned_results_size += (header_length + flow->assigned_results_length);
7518 
7519 			if (flow->has_protoctl_event) {
7520 				assigned_results_size += sizeof(struct necp_client_flow_protoctl_event_header);
7521 			}
7522 		}
7523 	}
7524 	return assigned_results_size;
7525 }
7526 
7527 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)7528 necp_client_fillout_flow_tlvs(struct necp_client *client,
7529     bool client_is_observed,
7530     struct necp_client_flow_registration *flow_registration,
7531     struct necp_client_action_args *uap,
7532     size_t *assigned_results_cursor)
7533 {
7534 	int error = 0;
7535 	struct necp_client_flow *flow = NULL;
7536 	LIST_FOREACH(flow, &flow_registration->flow_list, flow_chain) {
7537 		if (flow->assigned) {
7538 			// Write TLV headers
7539 			struct necp_client_nexus_flow_header header = {};
7540 			u_int32_t length = 0;
7541 			u_int32_t flags = 0;
7542 			u_int8_t tfo_cookie_len = 0;
7543 			u_int8_t type = 0;
7544 
7545 			type = NECP_CLIENT_RESULT_FLOW_ID;
7546 			length = sizeof(header.flow_header.flow_id);
7547 			memcpy(&header.flow_header.flow_id_tlv_header.type, &type, sizeof(type));
7548 			memcpy(&header.flow_header.flow_id_tlv_header.length, &length, sizeof(length));
7549 			uuid_copy(header.flow_header.flow_id, flow_registration->registration_id);
7550 
7551 			if (flow->nexus) {
7552 				if (flow->check_tcp_heuristics) {
7553 					u_int8_t tfo_cookie[NECP_TFO_COOKIE_LEN_MAX];
7554 					tfo_cookie_len = NECP_TFO_COOKIE_LEN_MAX;
7555 
7556 					if (necp_client_check_tcp_heuristics(client, flow, &flags,
7557 					    tfo_cookie, &tfo_cookie_len) != 0) {
7558 						tfo_cookie_len = 0;
7559 					} else {
7560 						flow->check_tcp_heuristics = FALSE;
7561 
7562 						if (tfo_cookie_len != 0) {
7563 							type = NECP_CLIENT_RESULT_TFO_COOKIE;
7564 							length = tfo_cookie_len;
7565 							memcpy(&header.tfo_cookie_tlv_header.type, &type, sizeof(type));
7566 							memcpy(&header.tfo_cookie_tlv_header.length, &length, sizeof(length));
7567 							memcpy(&header.tfo_cookie_value, tfo_cookie, tfo_cookie_len);
7568 						}
7569 					}
7570 				}
7571 			}
7572 
7573 			size_t header_length = 0;
7574 			if (flow->nexus) {
7575 				if (tfo_cookie_len != 0) {
7576 					header_length = sizeof(struct necp_client_nexus_flow_header) - (NECP_TFO_COOKIE_LEN_MAX - tfo_cookie_len);
7577 				} else {
7578 					header_length = sizeof(struct necp_client_nexus_flow_header) - sizeof(struct necp_tlv_header) - NECP_TFO_COOKIE_LEN_MAX;
7579 				}
7580 			} else {
7581 				header_length = sizeof(struct necp_client_flow_header);
7582 			}
7583 
7584 			type = NECP_CLIENT_RESULT_FLAGS;
7585 			length = sizeof(header.flow_header.flags_value);
7586 			memcpy(&header.flow_header.flags_tlv_header.type, &type, sizeof(type));
7587 			memcpy(&header.flow_header.flags_tlv_header.length, &length, sizeof(length));
7588 			if (flow->assigned) {
7589 				flags |= NECP_CLIENT_RESULT_FLAG_FLOW_ASSIGNED;
7590 			}
7591 			if (flow->viable) {
7592 				flags |= NECP_CLIENT_RESULT_FLAG_FLOW_VIABLE;
7593 			}
7594 			if (flow_registration->defunct) {
7595 				flags |= NECP_CLIENT_RESULT_FLAG_DEFUNCT;
7596 			}
7597 			flags |= flow->necp_flow_flags;
7598 			memcpy(&header.flow_header.flags_value, &flags, sizeof(flags));
7599 
7600 			type = NECP_CLIENT_RESULT_INTERFACE;
7601 			length = sizeof(header.flow_header.interface_value);
7602 			memcpy(&header.flow_header.interface_tlv_header.type, &type, sizeof(type));
7603 			memcpy(&header.flow_header.interface_tlv_header.length, &length, sizeof(length));
7604 
7605 			struct necp_client_result_interface interface_struct;
7606 			interface_struct.generation = 0;
7607 			interface_struct.index = flow->interface_index;
7608 
7609 			memcpy(&header.flow_header.interface_value, &interface_struct, sizeof(interface_struct));
7610 			if (flow->nexus) {
7611 				type = NECP_CLIENT_RESULT_NETAGENT;
7612 				length = sizeof(header.agent_value);
7613 				memcpy(&header.agent_tlv_header.type, &type, sizeof(type));
7614 				memcpy(&header.agent_tlv_header.length, &length, sizeof(length));
7615 
7616 				struct necp_client_result_netagent agent_struct;
7617 				uuid_copy(agent_struct.netagent_uuid, flow->u.nexus_agent);
7618 				agent_struct.generation = netagent_get_generation(agent_struct.netagent_uuid);
7619 
7620 				memcpy(&header.agent_value, &agent_struct, sizeof(agent_struct));
7621 			}
7622 
7623 			// Don't include outer TLV header in length field
7624 			type = NECP_CLIENT_RESULT_FLOW;
7625 			length = (header_length - sizeof(struct necp_tlv_header) + flow->assigned_results_length);
7626 			if (flow->has_protoctl_event) {
7627 				length += sizeof(struct necp_client_flow_protoctl_event_header);
7628 			}
7629 			memcpy(&header.flow_header.outer_header.type, &type, sizeof(type));
7630 			memcpy(&header.flow_header.outer_header.length, &length, sizeof(length));
7631 
7632 			error = copyout(&header, uap->buffer + client->result_length + *assigned_results_cursor, header_length);
7633 			if (error) {
7634 				NECPLOG(LOG_ERR, "necp_client_copy assigned results tlv_header copyout error (%d)", error);
7635 				return error;
7636 			}
7637 			*assigned_results_cursor += header_length;
7638 
7639 			if (flow->assigned_results && flow->assigned_results_length) {
7640 				// Write inner TLVs
7641 				error = copyout(flow->assigned_results, uap->buffer + client->result_length + *assigned_results_cursor,
7642 				    flow->assigned_results_length);
7643 				if (error) {
7644 					NECPLOG(LOG_ERR, "necp_client_copy assigned results copyout error (%d)", error);
7645 					return error;
7646 				}
7647 			}
7648 			*assigned_results_cursor += flow->assigned_results_length;
7649 
7650 			/* Read the protocol event and reset it */
7651 			if (flow->has_protoctl_event) {
7652 				struct necp_client_flow_protoctl_event_header protoctl_event_header = {};
7653 
7654 				type = NECP_CLIENT_RESULT_PROTO_CTL_EVENT;
7655 				length = sizeof(protoctl_event_header.protoctl_event);
7656 
7657 				memcpy(&protoctl_event_header.protoctl_tlv_header.type, &type, sizeof(type));
7658 				memcpy(&protoctl_event_header.protoctl_tlv_header.length, &length, sizeof(length));
7659 				memcpy(&protoctl_event_header.protoctl_event, &flow->protoctl_event,
7660 				    sizeof(flow->protoctl_event));
7661 
7662 				error = copyout(&protoctl_event_header, uap->buffer + client->result_length + *assigned_results_cursor,
7663 				    sizeof(protoctl_event_header));
7664 
7665 				if (error) {
7666 					NECPLOG(LOG_ERR, "necp_client_copy protocol control event results"
7667 					    " tlv_header copyout error (%d)", error);
7668 					return error;
7669 				}
7670 				*assigned_results_cursor += sizeof(protoctl_event_header);
7671 				flow->has_protoctl_event = FALSE;
7672 				flow->protoctl_event.protoctl_event_code = 0;
7673 				flow->protoctl_event.protoctl_event_val = 0;
7674 				flow->protoctl_event.protoctl_event_tcp_seq_num = 0;
7675 			}
7676 		}
7677 	}
7678 	if (!client_is_observed) {
7679 		flow_registration->flow_result_read = TRUE;
7680 	}
7681 	return 0;
7682 }
7683 
7684 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)7685 necp_client_copy_internal(struct necp_client *client, uuid_t client_id, bool client_is_observed, struct necp_client_action_args *uap, int *retval)
7686 {
7687 	NECP_CLIENT_ASSERT_LOCKED(client);
7688 	int error = 0;
7689 	// Copy results out
7690 	if (uap->action == NECP_CLIENT_ACTION_COPY_PARAMETERS) {
7691 		if (uap->buffer_size < client->parameters_length) {
7692 			return EINVAL;
7693 		}
7694 		error = copyout(client->parameters, uap->buffer, client->parameters_length);
7695 		if (error) {
7696 			NECPLOG(LOG_ERR, "necp_client_copy parameters copyout error (%d)", error);
7697 			return error;
7698 		}
7699 		*retval = client->parameters_length;
7700 	} else if (uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT &&
7701 	    client->result_read && client->group_members_read && !necp_client_has_unread_flows(client)) {
7702 		// Copy updates only, but nothing to read
7703 		// Just return 0 for bytes read
7704 		*retval = 0;
7705 	} else if (uap->action == NECP_CLIENT_ACTION_COPY_RESULT ||
7706 	    uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT) {
7707 		size_t assigned_results_size = client->assigned_group_members_length;
7708 
7709 		bool some_flow_is_defunct = false;
7710 		struct necp_client_flow_registration *single_flow_registration = NULL;
7711 		if (necp_client_id_is_flow(client_id)) {
7712 			single_flow_registration = necp_client_find_flow(client, client_id);
7713 			if (single_flow_registration != NULL) {
7714 				assigned_results_size += necp_client_calculate_flow_tlv_size(single_flow_registration);
7715 			}
7716 		} else {
7717 			// This request is for the client, so copy everything
7718 			struct necp_client_flow_registration *flow_registration = NULL;
7719 			RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
7720 				if (flow_registration->defunct) {
7721 					some_flow_is_defunct = true;
7722 				}
7723 				assigned_results_size += necp_client_calculate_flow_tlv_size(flow_registration);
7724 			}
7725 		}
7726 		if (uap->buffer_size < (client->result_length + assigned_results_size)) {
7727 			return EINVAL;
7728 		}
7729 
7730 		u_int32_t original_flags = 0;
7731 		bool flags_updated = false;
7732 		if (some_flow_is_defunct && client->legacy_client_is_flow) {
7733 			// If our client expects the defunct flag in the client, add it now
7734 			u_int32_t client_flags = 0;
7735 			u_int32_t value_size = 0;
7736 			u_int8_t *flags_pointer = necp_buffer_get_tlv_value(client->result, 0, &value_size);
7737 			if (flags_pointer != NULL && value_size == sizeof(client_flags)) {
7738 				memcpy(&client_flags, flags_pointer, value_size);
7739 				original_flags = client_flags;
7740 				client_flags |= NECP_CLIENT_RESULT_FLAG_DEFUNCT;
7741 				(void)necp_buffer_write_tlv_if_different(client->result, NECP_CLIENT_RESULT_FLAGS,
7742 				    sizeof(client_flags), &client_flags, &flags_updated,
7743 				    client->result, sizeof(client->result));
7744 			}
7745 		}
7746 
7747 		error = copyout(client->result, uap->buffer, client->result_length);
7748 
7749 		if (flags_updated) {
7750 			// Revert stored flags
7751 			(void)necp_buffer_write_tlv_if_different(client->result, NECP_CLIENT_RESULT_FLAGS,
7752 			    sizeof(original_flags), &original_flags, &flags_updated,
7753 			    client->result, sizeof(client->result));
7754 		}
7755 
7756 		if (error != 0) {
7757 			NECPLOG(LOG_ERR, "necp_client_copy result copyout error (%d)", error);
7758 			return error;
7759 		}
7760 
7761 		if (client->assigned_group_members != NULL && client->assigned_group_members_length > 0) {
7762 			error = copyout(client->assigned_group_members, uap->buffer + client->result_length, client->assigned_group_members_length);
7763 			if (error != 0) {
7764 				NECPLOG(LOG_ERR, "necp_client_copy group members copyout error (%d)", error);
7765 				return error;
7766 			}
7767 		}
7768 
7769 		size_t assigned_results_cursor = client->assigned_group_members_length; // Start with an offset based on the group members
7770 		if (necp_client_id_is_flow(client_id)) {
7771 			if (single_flow_registration != NULL) {
7772 				error = necp_client_fillout_flow_tlvs(client, client_is_observed, single_flow_registration, uap, &assigned_results_cursor);
7773 				if (error != 0) {
7774 					return error;
7775 				}
7776 			}
7777 		} else {
7778 			// This request is for the client, so copy everything
7779 			struct necp_client_flow_registration *flow_registration = NULL;
7780 			RB_FOREACH(flow_registration, _necp_client_flow_tree, &client->flow_registrations) {
7781 				error = necp_client_fillout_flow_tlvs(client, client_is_observed, flow_registration, uap, &assigned_results_cursor);
7782 				if (error != 0) {
7783 					return error;
7784 				}
7785 			}
7786 		}
7787 
7788 		*retval = client->result_length + assigned_results_cursor;
7789 
7790 		if (!client_is_observed) {
7791 			client->result_read = TRUE;
7792 			client->group_members_read = TRUE;
7793 		}
7794 	}
7795 
7796 	return 0;
7797 }
7798 
7799 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_copy(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)7800 necp_client_copy(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7801 {
7802 	int error = 0;
7803 	struct necp_client *client = NULL;
7804 	uuid_t client_id;
7805 	uuid_clear(client_id);
7806 
7807 	*retval = 0;
7808 
7809 	if (uap->buffer_size == 0 || uap->buffer == 0) {
7810 		return EINVAL;
7811 	}
7812 
7813 	if (uap->action != NECP_CLIENT_ACTION_COPY_PARAMETERS &&
7814 	    uap->action != NECP_CLIENT_ACTION_COPY_RESULT &&
7815 	    uap->action != NECP_CLIENT_ACTION_COPY_UPDATED_RESULT) {
7816 		return EINVAL;
7817 	}
7818 
7819 	if (uap->client_id) {
7820 		if (uap->client_id_len != sizeof(uuid_t)) {
7821 			NECPLOG(LOG_ERR, "Incorrect length (got %zu, expected %zu)", (size_t)uap->client_id_len, sizeof(uuid_t));
7822 			return ERANGE;
7823 		}
7824 
7825 		error = copyin(uap->client_id, client_id, sizeof(uuid_t));
7826 		if (error) {
7827 			NECPLOG(LOG_ERR, "necp_client_copy client_id copyin error (%d)", error);
7828 			return error;
7829 		}
7830 	}
7831 
7832 	const bool is_wildcard = (bool)uuid_is_null(client_id);
7833 
7834 	NECP_FD_LOCK(fd_data);
7835 
7836 	if (is_wildcard) {
7837 		if (uap->action == NECP_CLIENT_ACTION_COPY_RESULT || uap->action == NECP_CLIENT_ACTION_COPY_UPDATED_RESULT) {
7838 			struct necp_client *find_client = NULL;
7839 			RB_FOREACH(find_client, _necp_client_tree, &fd_data->clients) {
7840 				NECP_CLIENT_LOCK(find_client);
7841 				if (!find_client->result_read || !find_client->group_members_read || necp_client_has_unread_flows(find_client)) {
7842 					client = find_client;
7843 					// Leave the client locked, and break
7844 					break;
7845 				}
7846 				NECP_CLIENT_UNLOCK(find_client);
7847 			}
7848 		}
7849 	} else {
7850 		client = necp_client_fd_find_client_and_lock(fd_data, client_id);
7851 	}
7852 
7853 	if (client != NULL) {
7854 		// If client is set, it is locked
7855 		error = necp_client_copy_internal(client, client_id, FALSE, uap, retval);
7856 		NECP_CLIENT_UNLOCK(client);
7857 	}
7858 
7859 	// Unlock our own fd before moving on or returning
7860 	NECP_FD_UNLOCK(fd_data);
7861 
7862 	if (client == NULL) {
7863 		if (fd_data->flags & NECP_OPEN_FLAG_OBSERVER) {
7864 			// Observers are allowed to lookup clients on other fds
7865 
7866 			// Lock tree
7867 			NECP_CLIENT_TREE_LOCK_SHARED();
7868 
7869 			bool found_client = FALSE;
7870 
7871 			client = necp_find_client_and_lock(client_id);
7872 			if (client != NULL) {
7873 				// Matched, copy out data
7874 				found_client = TRUE;
7875 				error = necp_client_copy_internal(client, client_id, TRUE, uap, retval);
7876 				NECP_CLIENT_UNLOCK(client);
7877 			}
7878 
7879 			// Unlock tree
7880 			NECP_CLIENT_TREE_UNLOCK();
7881 
7882 			// No client found, fail
7883 			if (!found_client) {
7884 				return ENOENT;
7885 			}
7886 		} else {
7887 			// No client found, and not allowed to search other fds, fail
7888 			return ENOENT;
7889 		}
7890 	}
7891 
7892 	return error;
7893 }
7894 
7895 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)7896 necp_client_copy_client_update(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
7897 {
7898 	int error = 0;
7899 
7900 	*retval = 0;
7901 
7902 	if (!(fd_data->flags & NECP_OPEN_FLAG_PUSH_OBSERVER)) {
7903 		NECPLOG0(LOG_ERR, "NECP fd is not observer, cannot copy client update");
7904 		return EINVAL;
7905 	}
7906 
7907 	if (uap->client_id_len != sizeof(uuid_t) || uap->client_id == 0) {
7908 		NECPLOG0(LOG_ERR, "Client id invalid, cannot copy client update");
7909 		return EINVAL;
7910 	}
7911 
7912 	if (uap->buffer_size == 0 || uap->buffer == 0) {
7913 		NECPLOG0(LOG_ERR, "Buffer invalid, cannot copy client update");
7914 		return EINVAL;
7915 	}
7916 
7917 	NECP_FD_LOCK(fd_data);
7918 	struct necp_client_update *client_update = TAILQ_FIRST(&fd_data->update_list);
7919 	if (client_update != NULL) {
7920 		TAILQ_REMOVE(&fd_data->update_list, client_update, chain);
7921 		VERIFY(fd_data->update_count > 0);
7922 		fd_data->update_count--;
7923 	}
7924 	NECP_FD_UNLOCK(fd_data);
7925 
7926 	if (client_update != NULL) {
7927 		error = copyout(client_update->client_id, uap->client_id, sizeof(uuid_t));
7928 		if (error) {
7929 			NECPLOG(LOG_ERR, "Copy client update copyout client id error (%d)", error);
7930 		} else {
7931 			if (uap->buffer_size < client_update->update_length) {
7932 				NECPLOG(LOG_ERR, "Buffer size cannot hold update (%zu < %zu)", (size_t)uap->buffer_size, client_update->update_length);
7933 				error = EINVAL;
7934 			} else {
7935 				error = copyout(client_update->update, uap->buffer, client_update->update_length);
7936 				if (error) {
7937 					NECPLOG(LOG_ERR, "Copy client update copyout error (%d)", error);
7938 				} else {
7939 					*retval = client_update->update_length;
7940 				}
7941 			}
7942 		}
7943 
7944 		necp_client_update_free(client_update);
7945 		client_update = NULL;
7946 	} else {
7947 		error = ENOENT;
7948 	}
7949 
7950 	return error;
7951 }
7952 
7953 static int
necp_client_copy_parameters_locked(struct necp_client * client,struct necp_client_nexus_parameters * parameters)7954 necp_client_copy_parameters_locked(struct necp_client *client,
7955     struct necp_client_nexus_parameters *parameters)
7956 {
7957 	VERIFY(parameters != NULL);
7958 
7959 	struct necp_client_parsed_parameters parsed_parameters = {};
7960 	int error = necp_client_parse_parameters(client, client->parameters, (u_int32_t)client->parameters_length, &parsed_parameters);
7961 
7962 	parameters->pid = client->proc_pid;
7963 	if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID) {
7964 		parameters->epid = parsed_parameters.effective_pid;
7965 	} else {
7966 		parameters->epid = parameters->pid;
7967 	}
7968 #if SKYWALK
7969 	parameters->port_reservation = client->port_reservation;
7970 #endif /* !SKYWALK */
7971 	memcpy(&parameters->local_addr, &parsed_parameters.local_addr, sizeof(parameters->local_addr));
7972 	memcpy(&parameters->remote_addr, &parsed_parameters.remote_addr, sizeof(parameters->remote_addr));
7973 	parameters->ip_protocol = parsed_parameters.ip_protocol;
7974 	if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_TRANSPORT_PROTOCOL) {
7975 		parameters->transport_protocol = parsed_parameters.transport_protocol;
7976 	} else {
7977 		parameters->transport_protocol = parsed_parameters.ip_protocol;
7978 	}
7979 	parameters->ethertype = parsed_parameters.ethertype;
7980 	parameters->traffic_class = parsed_parameters.traffic_class;
7981 	if (uuid_is_null(client->override_euuid)) {
7982 		uuid_copy(parameters->euuid, parsed_parameters.effective_uuid);
7983 	} else {
7984 		uuid_copy(parameters->euuid, client->override_euuid);
7985 	}
7986 	parameters->is_listener = (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_LISTENER) ? 1 : 0;
7987 	parameters->is_interpose = (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_INTERPOSE) ? 1 : 0;
7988 	parameters->is_custom_ether = (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_CUSTOM_ETHER) ? 1 : 0;
7989 	parameters->policy_id = client->policy_id;
7990 
7991 	// parse client result flag
7992 	u_int32_t client_result_flags = 0;
7993 	u_int32_t value_size = 0;
7994 	u_int8_t *flags_pointer = NULL;
7995 	flags_pointer = necp_buffer_get_tlv_value(client->result, 0, &value_size);
7996 	if (flags_pointer && value_size == sizeof(client_result_flags)) {
7997 		memcpy(&client_result_flags, flags_pointer, value_size);
7998 	}
7999 	parameters->allow_qos_marking = (client_result_flags & NECP_CLIENT_RESULT_FLAG_ALLOW_QOS_MARKING) ? 1 : 0;
8000 
8001 	if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR_PREFERENCE) {
8002 		if (parsed_parameters.local_address_preference == NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_DEFAULT) {
8003 			parameters->override_address_selection = false;
8004 		} else if (parsed_parameters.local_address_preference == NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_TEMPORARY) {
8005 			parameters->override_address_selection = true;
8006 			parameters->use_stable_address = false;
8007 		} else if (parsed_parameters.local_address_preference == NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_STABLE) {
8008 			parameters->override_address_selection = true;
8009 			parameters->use_stable_address = true;
8010 		}
8011 	} else {
8012 		parameters->override_address_selection = false;
8013 	}
8014 
8015 	if ((parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) &&
8016 	    (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_NO_WAKE_FROM_SLEEP)) {
8017 		parameters->no_wake_from_sleep = true;
8018 	}
8019 
8020 	if ((parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLAGS) &&
8021 	    (parsed_parameters.flags & NECP_CLIENT_PARAMETER_FLAG_REUSE_LOCAL)) {
8022 		parameters->reuse_port = true;
8023 	}
8024 
8025 #if SKYWALK
8026 	if (!parameters->is_listener) {
8027 		if (parsed_parameters.valid_fields & NECP_PARSED_PARAMETERS_FIELD_FLOW_DEMUX_PATTERN) {
8028 			if (parsed_parameters.demux_patterns[0].len == 0) {
8029 				parameters->is_demuxable_parent = 1;
8030 			} else {
8031 				if (client->validated_parent) {
8032 					ASSERT(!uuid_is_null(client->parent_client_id));
8033 
8034 					NECP_CLIENT_TREE_LOCK_SHARED();
8035 					struct necp_client *parent = necp_find_client_and_lock(client->parent_client_id);
8036 					if (parent != NULL) {
8037 						struct necp_client_flow_registration *parent_flow_registration = NULL;
8038 						RB_FOREACH(parent_flow_registration, _necp_client_flow_tree, &parent->flow_registrations) {
8039 							uuid_copy(parameters->parent_flow_uuid, parent_flow_registration->registration_id);
8040 							break;
8041 						}
8042 
8043 						NECP_CLIENT_UNLOCK(parent);
8044 					}
8045 					NECP_CLIENT_TREE_UNLOCK();
8046 
8047 					if (parsed_parameters.demux_pattern_count > 0) {
8048 						for (int i = 0; i < parsed_parameters.demux_pattern_count; i++) {
8049 							memcpy(&parameters->demux_patterns[i], &parsed_parameters.demux_patterns[i], sizeof(struct necp_demux_pattern));
8050 						}
8051 						parameters->demux_pattern_count = parsed_parameters.demux_pattern_count;
8052 					}
8053 				}
8054 			}
8055 		}
8056 	}
8057 #endif // SKYWALK
8058 
8059 	return error;
8060 }
8061 
8062 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_list(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8063 necp_client_list(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8064 {
8065 	int error = 0;
8066 	struct necp_client *find_client = NULL;
8067 	uuid_t *list = NULL;
8068 	u_int32_t requested_client_count = 0;
8069 	u_int32_t client_count = 0;
8070 	size_t copy_buffer_size = 0;
8071 
8072 	if (uap->buffer_size < sizeof(requested_client_count) || uap->buffer == 0) {
8073 		error = EINVAL;
8074 		goto done;
8075 	}
8076 
8077 	if (!(fd_data->flags & NECP_OPEN_FLAG_OBSERVER)) {
8078 		NECPLOG0(LOG_ERR, "Client does not hold necessary entitlement to list other NECP clients");
8079 		error = EACCES;
8080 		goto done;
8081 	}
8082 
8083 	error = copyin(uap->buffer, &requested_client_count, sizeof(requested_client_count));
8084 	if (error) {
8085 		goto done;
8086 	}
8087 
8088 	if (os_mul_overflow(sizeof(uuid_t), requested_client_count, &copy_buffer_size)) {
8089 		error = ERANGE;
8090 		goto done;
8091 	}
8092 
8093 	if (uap->buffer_size - sizeof(requested_client_count) != copy_buffer_size) {
8094 		error = EINVAL;
8095 		goto done;
8096 	}
8097 
8098 	if (copy_buffer_size > NECP_MAX_CLIENT_LIST_SIZE) {
8099 		error = EINVAL;
8100 		goto done;
8101 	}
8102 
8103 	if (requested_client_count > 0) {
8104 		if ((list = (uuid_t*)kalloc_data(copy_buffer_size, Z_WAITOK | Z_ZERO)) == NULL) {
8105 			error = ENOMEM;
8106 			goto done;
8107 		}
8108 	}
8109 
8110 	// Lock tree
8111 	NECP_CLIENT_TREE_LOCK_SHARED();
8112 
8113 	find_client = NULL;
8114 	RB_FOREACH(find_client, _necp_client_global_tree, &necp_client_global_tree) {
8115 		NECP_CLIENT_LOCK(find_client);
8116 		if (!uuid_is_null(find_client->client_id)) {
8117 			if (client_count < requested_client_count) {
8118 				uuid_copy(list[client_count], find_client->client_id);
8119 			}
8120 			client_count++;
8121 		}
8122 		NECP_CLIENT_UNLOCK(find_client);
8123 	}
8124 
8125 	// Unlock tree
8126 	NECP_CLIENT_TREE_UNLOCK();
8127 
8128 	error = copyout(&client_count, uap->buffer, sizeof(client_count));
8129 	if (error) {
8130 		NECPLOG(LOG_ERR, "necp_client_list buffer copyout error (%d)", error);
8131 		goto done;
8132 	}
8133 
8134 	if (requested_client_count > 0 &&
8135 	    client_count > 0 &&
8136 	    list != NULL) {
8137 		error = copyout(list, uap->buffer + sizeof(client_count), copy_buffer_size);
8138 		if (error) {
8139 			NECPLOG(LOG_ERR, "necp_client_list client count copyout error (%d)", error);
8140 			goto done;
8141 		}
8142 	}
8143 done:
8144 	if (list != NULL) {
8145 		kfree_data(list, copy_buffer_size);
8146 	}
8147 	*retval = error;
8148 
8149 	return error;
8150 }
8151 
8152 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_add_flow(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8153 necp_client_add_flow(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8154 {
8155 	int error = 0;
8156 	struct necp_client *client = NULL;
8157 	uuid_t client_id;
8158 	struct necp_client_nexus_parameters parameters = {};
8159 	struct proc *proc = PROC_NULL;
8160 	struct necp_client_add_flow *add_request = NULL;
8161 	struct necp_client_add_flow *allocated_add_request = NULL;
8162 	struct necp_client_add_flow_default default_add_request = {};
8163 	const size_t buffer_size = uap->buffer_size;
8164 
8165 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
8166 		error = EINVAL;
8167 		NECPLOG(LOG_ERR, "necp_client_add_flow invalid client_id (length %zu)", (size_t)uap->client_id_len);
8168 		goto done;
8169 	}
8170 
8171 	if (uap->buffer == 0 || buffer_size < sizeof(struct necp_client_add_flow) ||
8172 	    buffer_size > sizeof(struct necp_client_add_flow_default) * 4) {
8173 		error = EINVAL;
8174 		NECPLOG(LOG_ERR, "necp_client_add_flow invalid buffer (length %zu)", buffer_size);
8175 		goto done;
8176 	}
8177 
8178 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
8179 	if (error) {
8180 		NECPLOG(LOG_ERR, "necp_client_add_flow copyin client_id error (%d)", error);
8181 		goto done;
8182 	}
8183 
8184 	if (buffer_size <= sizeof(struct necp_client_add_flow_default)) {
8185 		// Fits in default size
8186 		error = copyin(uap->buffer, &default_add_request, buffer_size);
8187 		if (error) {
8188 			NECPLOG(LOG_ERR, "necp_client_add_flow copyin default_add_request error (%d)", error);
8189 			goto done;
8190 		}
8191 
8192 		add_request = (struct necp_client_add_flow *)&default_add_request;
8193 	} else {
8194 		allocated_add_request = (struct necp_client_add_flow *)kalloc_data(buffer_size, Z_WAITOK | Z_ZERO);
8195 		if (allocated_add_request == NULL) {
8196 			error = ENOMEM;
8197 			goto done;
8198 		}
8199 
8200 		error = copyin(uap->buffer, allocated_add_request, buffer_size);
8201 		if (error) {
8202 			NECPLOG(LOG_ERR, "necp_client_add_flow copyin default_add_request error (%d)", error);
8203 			goto done;
8204 		}
8205 
8206 		add_request = allocated_add_request;
8207 	}
8208 
8209 	NECP_FD_LOCK(fd_data);
8210 	pid_t pid = fd_data->proc_pid;
8211 	proc = proc_find(pid);
8212 	if (proc == PROC_NULL) {
8213 		NECP_FD_UNLOCK(fd_data);
8214 		NECPLOG(LOG_ERR, "necp_client_add_flow process not found for pid %d error (%d)", pid, error);
8215 		error = ESRCH;
8216 		goto done;
8217 	}
8218 
8219 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
8220 	if (client == NULL) {
8221 		error = ENOENT;
8222 		NECP_FD_UNLOCK(fd_data);
8223 		goto done;
8224 	}
8225 
8226 	// Using ADD_FLOW indicates that the client supports multiple flows per client
8227 	client->legacy_client_is_flow = false;
8228 
8229 	necp_client_retain_locked(client);
8230 	necp_client_copy_parameters_locked(client, &parameters);
8231 
8232 	struct necp_client_flow_registration *new_registration = necp_client_create_flow_registration(fd_data, client);
8233 	if (new_registration == NULL) {
8234 		error = ENOMEM;
8235 		NECP_CLIENT_UNLOCK(client);
8236 		NECP_FD_UNLOCK(fd_data);
8237 		NECPLOG0(LOG_ERR, "Failed to allocate flow registration");
8238 		goto done;
8239 	}
8240 
8241 	new_registration->flags = add_request->flags;
8242 
8243 	// Copy new ID out to caller
8244 	uuid_copy(add_request->registration_id, new_registration->registration_id);
8245 
8246 	NECP_CLIENT_FLOW_LOG(client, new_registration, "adding flow");
8247 
8248 	// Copy override address
8249 	if (add_request->flags & NECP_CLIENT_FLOW_FLAGS_OVERRIDE_ADDRESS) {
8250 		size_t offset_of_address = (sizeof(struct necp_client_add_flow) +
8251 		    add_request->stats_request_count * sizeof(struct necp_client_flow_stats));
8252 		if (buffer_size >= offset_of_address + sizeof(struct sockaddr_in)) {
8253 			struct sockaddr *override_address = (struct sockaddr *)(((uint8_t *)add_request) + offset_of_address);
8254 			if (buffer_size >= offset_of_address + override_address->sa_len &&
8255 			    override_address->sa_len <= sizeof(parameters.remote_addr)) {
8256 				memcpy(&parameters.remote_addr, override_address, override_address->sa_len);
8257 			}
8258 		}
8259 	}
8260 
8261 #if SKYWALK
8262 	if (add_request->flags & NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS) {
8263 		void *assigned_results = NULL;
8264 		size_t assigned_results_length = 0;
8265 		uint32_t interface_index = 0;
8266 
8267 		// Validate that the nexus UUID is assigned
8268 		bool found_nexus = false;
8269 		for (u_int32_t option_i = 0; option_i < client->interface_option_count; option_i++) {
8270 			if (option_i < NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT) {
8271 				struct necp_client_interface_option *option = &client->interface_options[option_i];
8272 				if (uuid_compare(option->nexus_agent, add_request->agent_uuid) == 0) {
8273 					interface_index = option->interface_index;
8274 					found_nexus = true;
8275 					break;
8276 				}
8277 			} else {
8278 				struct necp_client_interface_option *option = &client->extra_interface_options[option_i - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT];
8279 				if (uuid_compare(option->nexus_agent, add_request->agent_uuid) == 0) {
8280 					interface_index = option->interface_index;
8281 					found_nexus = true;
8282 					break;
8283 				}
8284 			}
8285 		}
8286 
8287 		if (!found_nexus) {
8288 			NECPLOG0(LOG_ERR, "Requested nexus not found");
8289 		} else {
8290 			necp_client_add_nexus_flow_if_needed(new_registration, add_request->agent_uuid, interface_index);
8291 
8292 			error = netagent_client_message_with_params(add_request->agent_uuid,
8293 			    ((new_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
8294 			    client->client_id :
8295 			    new_registration->registration_id),
8296 			    pid, client->agent_handle,
8297 			    NETAGENT_MESSAGE_TYPE_REQUEST_NEXUS,
8298 			    (struct necp_client_agent_parameters *)&parameters,
8299 			    &assigned_results, &assigned_results_length);
8300 			if (error != 0) {
8301 				VERIFY(assigned_results == NULL);
8302 				VERIFY(assigned_results_length == 0);
8303 				NECPLOG(LOG_ERR, "netagent_client_message error (%d)", error);
8304 			} else if (assigned_results != NULL) {
8305 				if (!necp_assign_client_result_locked(proc, fd_data, client, new_registration, add_request->agent_uuid,
8306 				    assigned_results, assigned_results_length, false, false)) {
8307 					kfree_data(assigned_results, assigned_results_length);
8308 				}
8309 			}
8310 		}
8311 	}
8312 
8313 	// Don't request stats if nexus creation fails
8314 	if (error == 0 && add_request->stats_request_count > 0 && necp_arena_initialize(fd_data, true) == 0) {
8315 		struct necp_client_flow_stats *stats_request = (struct necp_client_flow_stats *)&add_request->stats_requests[0];
8316 		struct necp_stats_bufreq bufreq = {};
8317 
8318 		NECP_CLIENT_FLOW_LOG(client, new_registration, "Initializing stats");
8319 
8320 		bufreq.necp_stats_bufreq_id = NECP_CLIENT_STATISTICS_BUFREQ_ID;
8321 		bufreq.necp_stats_bufreq_type = stats_request->stats_type;
8322 		bufreq.necp_stats_bufreq_ver = stats_request->stats_version;
8323 		bufreq.necp_stats_bufreq_size = stats_request->stats_size;
8324 		bufreq.necp_stats_bufreq_uaddr = stats_request->stats_addr;
8325 		(void)necp_stats_initialize(fd_data, client, new_registration, &bufreq);
8326 		stats_request->stats_type = bufreq.necp_stats_bufreq_type;
8327 		stats_request->stats_version = bufreq.necp_stats_bufreq_ver;
8328 		stats_request->stats_size = bufreq.necp_stats_bufreq_size;
8329 		stats_request->stats_addr = bufreq.necp_stats_bufreq_uaddr;
8330 	}
8331 #endif /* !SKYWALK */
8332 
8333 	if (error == 0 &&
8334 	    (add_request->flags & NECP_CLIENT_FLOW_FLAGS_BROWSE ||
8335 	    add_request->flags & NECP_CLIENT_FLOW_FLAGS_RESOLVE)) {
8336 		uint32_t interface_index = IFSCOPE_NONE;
8337 		ifnet_head_lock_shared();
8338 		struct ifnet *interface = NULL;
8339 		TAILQ_FOREACH(interface, &ifnet_head, if_link) {
8340 			ifnet_lock_shared(interface);
8341 			if (interface->if_agentids != NULL) {
8342 				for (u_int32_t i = 0; i < interface->if_agentcount; i++) {
8343 					if (uuid_compare(interface->if_agentids[i], add_request->agent_uuid) == 0) {
8344 						interface_index = interface->if_index;
8345 						break;
8346 					}
8347 				}
8348 			}
8349 			ifnet_lock_done(interface);
8350 			if (interface_index != IFSCOPE_NONE) {
8351 				break;
8352 			}
8353 		}
8354 		ifnet_head_done();
8355 
8356 		necp_client_add_nexus_flow_if_needed(new_registration, add_request->agent_uuid, interface_index);
8357 
8358 		error = netagent_client_message_with_params(add_request->agent_uuid,
8359 		    ((new_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
8360 		    client->client_id :
8361 		    new_registration->registration_id),
8362 		    pid, client->agent_handle,
8363 		    NETAGENT_MESSAGE_TYPE_CLIENT_ASSERT,
8364 		    (struct necp_client_agent_parameters *)&parameters,
8365 		    NULL, NULL);
8366 		if (error != 0) {
8367 			NECPLOG(LOG_ERR, "netagent_client_message error (%d)", error);
8368 		}
8369 	}
8370 
8371 	if (error != 0) {
8372 		// Encountered an error in adding the flow, destroy the flow registration
8373 #if SKYWALK
8374 		necp_destroy_flow_stats(fd_data, new_registration, NULL, false);
8375 #endif /* SKYWALK */
8376 		NECP_FLOW_TREE_LOCK_EXCLUSIVE();
8377 		RB_REMOVE(_necp_client_flow_global_tree, &necp_client_flow_global_tree, new_registration);
8378 		NECP_FLOW_TREE_UNLOCK();
8379 		RB_REMOVE(_necp_fd_flow_tree, &fd_data->flows, new_registration);
8380 		necp_destroy_client_flow_registration(client, new_registration, fd_data->proc_pid, true);
8381 		new_registration = NULL;
8382 	}
8383 
8384 	NECP_CLIENT_UNLOCK(client);
8385 	NECP_FD_UNLOCK(fd_data);
8386 
8387 	necp_client_release(client);
8388 
8389 	if (error != 0) {
8390 		goto done;
8391 	}
8392 
8393 	// Copy the request back out to the caller with assigned fields
8394 	error = copyout(add_request, uap->buffer, buffer_size);
8395 	if (error != 0) {
8396 		NECPLOG(LOG_ERR, "necp_client_add_flow copyout add_request error (%d)", error);
8397 	}
8398 
8399 done:
8400 	*retval = error;
8401 	if (error != 0) {
8402 		NECPLOG(LOG_ERR, "Add flow error (%d)", error);
8403 	}
8404 
8405 	if (allocated_add_request != NULL) {
8406 		kfree_data(allocated_add_request, buffer_size);
8407 	}
8408 
8409 	if (proc != PROC_NULL) {
8410 		proc_rele(proc);
8411 	}
8412 	return error;
8413 }
8414 
8415 #if SKYWALK
8416 
8417 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_request_nexus(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8418 necp_client_request_nexus(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8419 {
8420 	int error = 0;
8421 	struct necp_client *client = NULL;
8422 	uuid_t client_id;
8423 	struct necp_client_nexus_parameters parameters = {};
8424 	struct proc *proc = PROC_NULL;
8425 	const size_t buffer_size = uap->buffer_size;
8426 
8427 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
8428 		error = EINVAL;
8429 		goto done;
8430 	}
8431 
8432 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
8433 	if (error) {
8434 		NECPLOG(LOG_ERR, "necp_client_request_nexus copyin client_id error (%d)", error);
8435 		goto done;
8436 	}
8437 
8438 	NECP_FD_LOCK(fd_data);
8439 	pid_t pid = fd_data->proc_pid;
8440 	proc = proc_find(pid);
8441 	if (proc == PROC_NULL) {
8442 		NECP_FD_UNLOCK(fd_data);
8443 		NECPLOG(LOG_ERR, "necp_client_request_nexus process not found for pid %d error (%d)", pid, error);
8444 		error = ESRCH;
8445 		goto done;
8446 	}
8447 
8448 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
8449 	if (client == NULL) {
8450 		NECP_FD_UNLOCK(fd_data);
8451 		error = ENOENT;
8452 		goto done;
8453 	}
8454 
8455 	// Using REQUEST_NEXUS indicates that the client only supports one flow per client
8456 	client->legacy_client_is_flow = true;
8457 
8458 	necp_client_retain_locked(client);
8459 	necp_client_copy_parameters_locked(client, &parameters);
8460 
8461 	do {
8462 		void *assigned_results = NULL;
8463 		size_t assigned_results_length = 0;
8464 		uuid_t nexus_uuid;
8465 		uint32_t interface_index = 0;
8466 
8467 		// Validate that the nexus UUID is assigned
8468 		bool found_nexus = false;
8469 		for (u_int32_t option_i = 0; option_i < client->interface_option_count; option_i++) {
8470 			if (option_i < NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT) {
8471 				struct necp_client_interface_option *option = &client->interface_options[option_i];
8472 				if (!uuid_is_null(option->nexus_agent)) {
8473 					uuid_copy(nexus_uuid, option->nexus_agent);
8474 					interface_index = option->interface_index;
8475 					found_nexus = true;
8476 					break;
8477 				}
8478 			} else {
8479 				struct necp_client_interface_option *option = &client->extra_interface_options[option_i - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT];
8480 				if (!uuid_is_null(option->nexus_agent)) {
8481 					uuid_copy(nexus_uuid, option->nexus_agent);
8482 					interface_index = option->interface_index;
8483 					found_nexus = true;
8484 					break;
8485 				}
8486 			}
8487 		}
8488 
8489 		if (!found_nexus) {
8490 			NECP_CLIENT_UNLOCK(client);
8491 			NECP_FD_UNLOCK(fd_data);
8492 			necp_client_release(client);
8493 			// Break the loop
8494 			error = ENETDOWN;
8495 			goto done;
8496 		}
8497 
8498 		struct necp_client_flow_registration *new_registration = necp_client_create_flow_registration(fd_data, client);
8499 		if (new_registration == NULL) {
8500 			error = ENOMEM;
8501 			NECP_CLIENT_UNLOCK(client);
8502 			NECP_FD_UNLOCK(fd_data);
8503 			necp_client_release(client);
8504 			NECPLOG0(LOG_ERR, "Failed to allocate flow registration");
8505 			goto done;
8506 		}
8507 
8508 		new_registration->flags = (NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS | NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID);
8509 
8510 		necp_client_add_nexus_flow_if_needed(new_registration, nexus_uuid, interface_index);
8511 
8512 		// Note: Any clients using "request_nexus" are not flow-registration aware.
8513 		// Register the Client ID rather than the Registration ID with the nexus, since
8514 		// the client will send traffic based on the client ID.
8515 		error = netagent_client_message_with_params(nexus_uuid,
8516 		    ((new_registration->flags & NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID) ?
8517 		    client->client_id :
8518 		    new_registration->registration_id),
8519 		    pid, client->agent_handle,
8520 		    NETAGENT_MESSAGE_TYPE_REQUEST_NEXUS,
8521 		    (struct necp_client_agent_parameters *)&parameters,
8522 		    &assigned_results, &assigned_results_length);
8523 		if (error) {
8524 			NECP_CLIENT_UNLOCK(client);
8525 			NECP_FD_UNLOCK(fd_data);
8526 			necp_client_release(client);
8527 			VERIFY(assigned_results == NULL);
8528 			VERIFY(assigned_results_length == 0);
8529 			NECPLOG(LOG_ERR, "netagent_client_message error (%d)", error);
8530 			goto done;
8531 		}
8532 
8533 		if (assigned_results != NULL) {
8534 			if (!necp_assign_client_result_locked(proc, fd_data, client, new_registration, nexus_uuid,
8535 			    assigned_results, assigned_results_length, false, false)) {
8536 				kfree_data(assigned_results, assigned_results_length);
8537 			}
8538 		}
8539 
8540 		if (uap->buffer != 0 && buffer_size == sizeof(struct necp_stats_bufreq) &&
8541 		    necp_arena_initialize(fd_data, true) == 0) {
8542 			struct necp_stats_bufreq bufreq = {};
8543 			int copy_error = copyin(uap->buffer, &bufreq, buffer_size);
8544 			if (copy_error) {
8545 				NECPLOG(LOG_ERR, "necp_client_request_nexus copyin bufreq error (%d)", copy_error);
8546 			} else {
8547 				(void)necp_stats_initialize(fd_data, client, new_registration, &bufreq);
8548 				copy_error = copyout(&bufreq, uap->buffer, buffer_size);
8549 				if (copy_error != 0) {
8550 					NECPLOG(LOG_ERR, "necp_client_request_nexus copyout bufreq error (%d)", copy_error);
8551 				}
8552 			}
8553 		}
8554 	} while (false);
8555 
8556 	NECP_CLIENT_UNLOCK(client);
8557 	NECP_FD_UNLOCK(fd_data);
8558 
8559 	necp_client_release(client);
8560 
8561 done:
8562 	*retval = error;
8563 	if (error != 0) {
8564 		NECPLOG(LOG_ERR, "Request nexus error (%d)", error);
8565 	}
8566 
8567 	if (proc != PROC_NULL) {
8568 		proc_rele(proc);
8569 	}
8570 	return error;
8571 }
8572 #endif /* !SKYWALK */
8573 
8574 static void
necp_client_add_assertion(struct necp_client * client,uuid_t netagent_uuid)8575 necp_client_add_assertion(struct necp_client *client, uuid_t netagent_uuid)
8576 {
8577 	struct necp_client_assertion *new_assertion = NULL;
8578 
8579 	new_assertion = kalloc_type(struct necp_client_assertion,
8580 	    Z_WAITOK | Z_NOFAIL);
8581 
8582 	uuid_copy(new_assertion->asserted_netagent, netagent_uuid);
8583 
8584 	LIST_INSERT_HEAD(&client->assertion_list, new_assertion, assertion_chain);
8585 }
8586 
8587 static bool
necp_client_remove_assertion(struct necp_client * client,uuid_t netagent_uuid)8588 necp_client_remove_assertion(struct necp_client *client, uuid_t netagent_uuid)
8589 {
8590 	struct necp_client_assertion *found_assertion = NULL;
8591 	struct necp_client_assertion *search_assertion = NULL;
8592 	LIST_FOREACH(search_assertion, &client->assertion_list, assertion_chain) {
8593 		if (uuid_compare(search_assertion->asserted_netagent, netagent_uuid) == 0) {
8594 			found_assertion = search_assertion;
8595 			break;
8596 		}
8597 	}
8598 
8599 	if (found_assertion == NULL) {
8600 		NECPLOG0(LOG_ERR, "Netagent uuid not previously asserted");
8601 		return false;
8602 	}
8603 
8604 	LIST_REMOVE(found_assertion, assertion_chain);
8605 	kfree_type(struct necp_client_assertion, found_assertion);
8606 	return true;
8607 }
8608 
8609 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_agent_action(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8610 necp_client_agent_action(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8611 {
8612 	int error = 0;
8613 	struct necp_client *client = NULL;
8614 	uuid_t client_id;
8615 	bool acted_on_agent = FALSE;
8616 	u_int8_t *parameters = NULL;
8617 	const size_t buffer_size = uap->buffer_size;
8618 
8619 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
8620 	    buffer_size == 0 || uap->buffer == 0) {
8621 		NECPLOG0(LOG_ERR, "necp_client_agent_action invalid parameters");
8622 		error = EINVAL;
8623 		goto done;
8624 	}
8625 
8626 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
8627 	if (error) {
8628 		NECPLOG(LOG_ERR, "necp_client_agent_action copyin client_id error (%d)", error);
8629 		goto done;
8630 	}
8631 
8632 	if (buffer_size > NECP_MAX_AGENT_ACTION_SIZE) {
8633 		NECPLOG(LOG_ERR, "necp_client_agent_action invalid buffer size (>%u)", NECP_MAX_AGENT_ACTION_SIZE);
8634 		error = EINVAL;
8635 		goto done;
8636 	}
8637 
8638 	if ((parameters = (u_int8_t *)kalloc_data(buffer_size, Z_WAITOK | Z_ZERO)) == NULL) {
8639 		NECPLOG0(LOG_ERR, "necp_client_agent_action malloc failed");
8640 		error = ENOMEM;
8641 		goto done;
8642 	}
8643 
8644 	error = copyin(uap->buffer, parameters, buffer_size);
8645 	if (error) {
8646 		NECPLOG(LOG_ERR, "necp_client_agent_action parameters copyin error (%d)", error);
8647 		goto done;
8648 	}
8649 
8650 	NECP_FD_LOCK(fd_data);
8651 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
8652 	if (client != NULL) {
8653 		size_t offset = 0;
8654 		while ((offset + sizeof(struct necp_tlv_header)) <= buffer_size) {
8655 			u_int8_t type = necp_buffer_get_tlv_type(parameters, offset);
8656 			u_int32_t length = necp_buffer_get_tlv_length(parameters, offset);
8657 
8658 			if (length > (buffer_size - (offset + sizeof(struct necp_tlv_header)))) {
8659 				// If the length is larger than what can fit in the remaining parameters size, bail
8660 				NECPLOG(LOG_ERR, "Invalid TLV length (%u)", length);
8661 				break;
8662 			}
8663 
8664 			if (length >= sizeof(uuid_t)) {
8665 				u_int8_t *value = necp_buffer_get_tlv_value(parameters, offset, NULL);
8666 				if (value == NULL) {
8667 					NECPLOG0(LOG_ERR, "Invalid TLV value");
8668 					break;
8669 				}
8670 				if (type == NECP_CLIENT_PARAMETER_TRIGGER_AGENT ||
8671 				    type == NECP_CLIENT_PARAMETER_ASSERT_AGENT ||
8672 				    type == NECP_CLIENT_PARAMETER_UNASSERT_AGENT) {
8673 					uuid_t agent_uuid;
8674 					uuid_copy(agent_uuid, value);
8675 					u_int8_t netagent_message_type = 0;
8676 					if (type == NECP_CLIENT_PARAMETER_TRIGGER_AGENT) {
8677 						netagent_message_type = NETAGENT_MESSAGE_TYPE_CLIENT_TRIGGER;
8678 					} else if (type == NECP_CLIENT_PARAMETER_ASSERT_AGENT) {
8679 						netagent_message_type = NETAGENT_MESSAGE_TYPE_CLIENT_ASSERT;
8680 					} else if (type == NECP_CLIENT_PARAMETER_UNASSERT_AGENT) {
8681 						netagent_message_type = NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT;
8682 					}
8683 
8684 					// Before unasserting, verify that the assertion was already taken
8685 					if (type == NECP_CLIENT_PARAMETER_UNASSERT_AGENT) {
8686 						if (!necp_client_remove_assertion(client, agent_uuid)) {
8687 							error = ENOENT;
8688 							break;
8689 						}
8690 					}
8691 
8692 					struct necp_client_nexus_parameters parsed_parameters = {};
8693 					necp_client_copy_parameters_locked(client, &parsed_parameters);
8694 
8695 					error = netagent_client_message_with_params(agent_uuid,
8696 					    client_id,
8697 					    fd_data->proc_pid,
8698 					    client->agent_handle,
8699 					    netagent_message_type,
8700 					    (struct necp_client_agent_parameters *)&parsed_parameters,
8701 					    NULL, NULL);
8702 					if (error == 0) {
8703 						acted_on_agent = TRUE;
8704 					} else {
8705 						break;
8706 					}
8707 
8708 					// Only save the assertion if the action succeeded
8709 					if (type == NECP_CLIENT_PARAMETER_ASSERT_AGENT) {
8710 						necp_client_add_assertion(client, agent_uuid);
8711 					}
8712 				} else if (type == NECP_CLIENT_PARAMETER_AGENT_ADD_GROUP_MEMBERS ||
8713 				    type == NECP_CLIENT_PARAMETER_AGENT_REMOVE_GROUP_MEMBERS) {
8714 					uuid_t agent_uuid;
8715 					uuid_copy(agent_uuid, value);
8716 					u_int8_t netagent_message_type = 0;
8717 					if (type == NECP_CLIENT_PARAMETER_AGENT_ADD_GROUP_MEMBERS) {
8718 						netagent_message_type = NETAGENT_MESSAGE_TYPE_ADD_GROUP_MEMBERS;
8719 					} else if (type == NECP_CLIENT_PARAMETER_AGENT_REMOVE_GROUP_MEMBERS) {
8720 						netagent_message_type = NETAGENT_MESSAGE_TYPE_REMOVE_GROUP_MEMBERS;
8721 					}
8722 
8723 					struct necp_client_group_members group_members = {};
8724 					group_members.group_members_length = (length - sizeof(uuid_t));
8725 					group_members.group_members = (value + sizeof(uuid_t));
8726 					error = netagent_client_message_with_params(agent_uuid,
8727 					    client_id,
8728 					    fd_data->proc_pid,
8729 					    client->agent_handle,
8730 					    netagent_message_type,
8731 					    (struct necp_client_agent_parameters *)&group_members,
8732 					    NULL, NULL);
8733 					if (error == 0) {
8734 						acted_on_agent = TRUE;
8735 					} else {
8736 						break;
8737 					}
8738 				} else if (type == NECP_CLIENT_PARAMETER_REPORT_AGENT_ERROR) {
8739 					uuid_t agent_uuid;
8740 					uuid_copy(agent_uuid, value);
8741 					struct necp_client_agent_parameters agent_params = {};
8742 					if ((length - sizeof(uuid_t)) >= sizeof(agent_params.u.error)) {
8743 						memcpy(&agent_params.u.error,
8744 						    (value + sizeof(uuid_t)),
8745 						    sizeof(agent_params.u.error));
8746 					}
8747 					error = netagent_client_message_with_params(agent_uuid,
8748 					    client_id,
8749 					    fd_data->proc_pid,
8750 					    client->agent_handle,
8751 					    NETAGENT_MESSAGE_TYPE_CLIENT_ERROR,
8752 					    &agent_params,
8753 					    NULL, NULL);
8754 					if (error == 0) {
8755 						acted_on_agent = TRUE;
8756 					} else {
8757 						break;
8758 					}
8759 				}
8760 			}
8761 
8762 			offset += sizeof(struct necp_tlv_header) + length;
8763 		}
8764 
8765 		NECP_CLIENT_UNLOCK(client);
8766 	}
8767 	NECP_FD_UNLOCK(fd_data);
8768 
8769 	if (!acted_on_agent &&
8770 	    error == 0) {
8771 		error = ENOENT;
8772 	}
8773 done:
8774 	*retval = error;
8775 	if (parameters != NULL) {
8776 		kfree_data(parameters, buffer_size);
8777 		parameters = NULL;
8778 	}
8779 
8780 	return error;
8781 }
8782 
8783 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)8784 necp_client_copy_agent(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8785 {
8786 	int error = 0;
8787 	uuid_t agent_uuid;
8788 	const size_t buffer_size = uap->buffer_size;
8789 
8790 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
8791 	    buffer_size == 0 || uap->buffer == 0) {
8792 		NECPLOG0(LOG_ERR, "necp_client_copy_agent bad input");
8793 		error = EINVAL;
8794 		goto done;
8795 	}
8796 
8797 	error = copyin(uap->client_id, agent_uuid, sizeof(uuid_t));
8798 	if (error) {
8799 		NECPLOG(LOG_ERR, "necp_client_copy_agent copyin agent_uuid error (%d)", error);
8800 		goto done;
8801 	}
8802 
8803 	error = netagent_copyout(agent_uuid, uap->buffer, buffer_size);
8804 	if (error) {
8805 		// netagent_copyout already logs appropriate errors
8806 		goto done;
8807 	}
8808 done:
8809 	*retval = error;
8810 
8811 	return error;
8812 }
8813 
8814 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_agent_use(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)8815 necp_client_agent_use(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8816 {
8817 	int error = 0;
8818 	struct necp_client *client = NULL;
8819 	uuid_t client_id;
8820 	struct necp_agent_use_parameters parameters = {};
8821 	const size_t buffer_size = uap->buffer_size;
8822 
8823 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
8824 	    buffer_size != sizeof(parameters) || uap->buffer == 0) {
8825 		error = EINVAL;
8826 		goto done;
8827 	}
8828 
8829 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
8830 	if (error) {
8831 		NECPLOG(LOG_ERR, "Copyin client_id error (%d)", error);
8832 		goto done;
8833 	}
8834 
8835 	error = copyin(uap->buffer, &parameters, buffer_size);
8836 	if (error) {
8837 		NECPLOG(LOG_ERR, "Parameters copyin error (%d)", error);
8838 		goto done;
8839 	}
8840 
8841 	NECP_FD_LOCK(fd_data);
8842 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
8843 	if (client != NULL) {
8844 		error = netagent_use(parameters.agent_uuid, &parameters.out_use_count);
8845 		NECP_CLIENT_UNLOCK(client);
8846 	} else {
8847 		error = ENOENT;
8848 	}
8849 
8850 	NECP_FD_UNLOCK(fd_data);
8851 
8852 	if (error == 0) {
8853 		error = copyout(&parameters, uap->buffer, buffer_size);
8854 		if (error) {
8855 			NECPLOG(LOG_ERR, "Parameters copyout error (%d)", error);
8856 			goto done;
8857 		}
8858 	}
8859 
8860 done:
8861 	*retval = error;
8862 
8863 	return error;
8864 }
8865 
8866 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)8867 necp_client_acquire_agent_token(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8868 {
8869 	int error = 0;
8870 	uuid_t agent_uuid = {};
8871 	const size_t buffer_size = uap->buffer_size;
8872 
8873 	*retval = 0;
8874 
8875 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
8876 	    buffer_size == 0 || uap->buffer == 0) {
8877 		NECPLOG0(LOG_ERR, "necp_client_copy_agent bad input");
8878 		error = EINVAL;
8879 		goto done;
8880 	}
8881 
8882 	error = copyin(uap->client_id, agent_uuid, sizeof(uuid_t));
8883 	if (error) {
8884 		NECPLOG(LOG_ERR, "necp_client_copy_agent copyin agent_uuid error (%d)", error);
8885 		goto done;
8886 	}
8887 
8888 	error = netagent_acquire_token(agent_uuid, uap->buffer, buffer_size, retval);
8889 done:
8890 	return error;
8891 }
8892 
8893 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)8894 necp_client_copy_interface(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
8895 {
8896 	int error = 0;
8897 	u_int32_t interface_index = 0;
8898 	struct necp_interface_details interface_details = {};
8899 
8900 	if (uap->client_id == 0 || uap->client_id_len != sizeof(u_int32_t) ||
8901 	    uap->buffer_size < sizeof(interface_details) ||
8902 	    uap->buffer == 0) {
8903 		NECPLOG0(LOG_ERR, "necp_client_copy_interface bad input");
8904 		error = EINVAL;
8905 		goto done;
8906 	}
8907 
8908 	error = copyin(uap->client_id, &interface_index, sizeof(u_int32_t));
8909 	if (error) {
8910 		NECPLOG(LOG_ERR, "necp_client_copy_interface copyin interface_index error (%d)", error);
8911 		goto done;
8912 	}
8913 
8914 	if (interface_index == 0) {
8915 		error = ENOENT;
8916 		NECPLOG(LOG_ERR, "necp_client_copy_interface bad interface_index (%d)", interface_index);
8917 		goto done;
8918 	}
8919 
8920 	lck_mtx_lock(rnh_lock);
8921 	ifnet_head_lock_shared();
8922 	ifnet_t interface = NULL;
8923 	if (interface_index != IFSCOPE_NONE && interface_index <= (u_int32_t)if_index) {
8924 		interface = ifindex2ifnet[interface_index];
8925 	}
8926 
8927 	if (interface != NULL) {
8928 		if (interface->if_xname != NULL) {
8929 			strlcpy((char *)&interface_details.name, interface->if_xname, sizeof(interface_details.name));
8930 		}
8931 		interface_details.index = interface->if_index;
8932 		interface_details.generation = ifnet_get_generation(interface);
8933 		if (interface->if_delegated.ifp != NULL) {
8934 			interface_details.delegate_index = interface->if_delegated.ifp->if_index;
8935 		}
8936 		interface_details.functional_type = if_functional_type(interface, TRUE);
8937 		if (IFNET_IS_EXPENSIVE(interface)) {
8938 			interface_details.flags |= NECP_INTERFACE_FLAG_EXPENSIVE;
8939 		}
8940 		if (IFNET_IS_CONSTRAINED(interface)) {
8941 			interface_details.flags |= NECP_INTERFACE_FLAG_CONSTRAINED;
8942 		}
8943 		if ((interface->if_eflags & IFEF_TXSTART) == IFEF_TXSTART) {
8944 			interface_details.flags |= NECP_INTERFACE_FLAG_TXSTART;
8945 		}
8946 		if ((interface->if_eflags & IFEF_NOACKPRI) == IFEF_NOACKPRI) {
8947 			interface_details.flags |= NECP_INTERFACE_FLAG_NOACKPRI;
8948 		}
8949 		if ((interface->if_eflags & IFEF_3CA) == IFEF_3CA) {
8950 			interface_details.flags |= NECP_INTERFACE_FLAG_3CARRIERAGG;
8951 		}
8952 		if (IFNET_IS_LOW_POWER(interface)) {
8953 			interface_details.flags |= NECP_INTERFACE_FLAG_IS_LOW_POWER;
8954 		}
8955 		if (interface->if_xflags & IFXF_MPK_LOG) {
8956 			interface_details.flags |= NECP_INTERFACE_FLAG_MPK_LOG;
8957 		}
8958 		if (interface->if_flags & IFF_MULTICAST) {
8959 			interface_details.flags |= NECP_INTERFACE_FLAG_SUPPORTS_MULTICAST;
8960 		}
8961 		if (IS_INTF_CLAT46(interface)) {
8962 			interface_details.flags |= NECP_INTERFACE_FLAG_HAS_NAT64;
8963 		}
8964 		interface_details.mtu = interface->if_mtu;
8965 #if SKYWALK
8966 		nx_netif_get_interface_tso_capabilities(interface, &interface_details.tso_max_segment_size_v4, &interface_details.tso_max_segment_size_v6);
8967 #endif /* SKYWALK */
8968 
8969 		u_int8_t ipv4_signature_len = sizeof(interface_details.ipv4_signature.signature);
8970 		u_int16_t ipv4_signature_flags;
8971 		if (ifnet_get_netsignature(interface, AF_INET, &ipv4_signature_len, &ipv4_signature_flags,
8972 		    (u_int8_t *)&interface_details.ipv4_signature) != 0) {
8973 			ipv4_signature_len = 0;
8974 		}
8975 		interface_details.ipv4_signature.signature_len = ipv4_signature_len;
8976 
8977 		// Check for default scoped routes for IPv4 and IPv6
8978 		union necp_sockaddr_union default_address;
8979 		struct rtentry *v4Route = NULL;
8980 		memset(&default_address, 0, sizeof(default_address));
8981 		default_address.sa.sa_family = AF_INET;
8982 		default_address.sa.sa_len = sizeof(struct sockaddr_in);
8983 		v4Route = rtalloc1_scoped_locked((struct sockaddr *)&default_address, 0, 0,
8984 		    interface->if_index);
8985 		if (v4Route != NULL) {
8986 			if (v4Route->rt_ifp != NULL && !IS_INTF_CLAT46(v4Route->rt_ifp)) {
8987 				interface_details.flags |= NECP_INTERFACE_FLAG_IPV4_ROUTABLE;
8988 			}
8989 			rtfree_locked(v4Route);
8990 			v4Route = NULL;
8991 		}
8992 
8993 		struct rtentry *v6Route = NULL;
8994 		memset(&default_address, 0, sizeof(default_address));
8995 		default_address.sa.sa_family = AF_INET6;
8996 		default_address.sa.sa_len = sizeof(struct sockaddr_in6);
8997 		v6Route = rtalloc1_scoped_locked((struct sockaddr *)&default_address, 0, 0,
8998 		    interface->if_index);
8999 		if (v6Route != NULL) {
9000 			if (v6Route->rt_ifp != NULL) {
9001 				interface_details.flags |= NECP_INTERFACE_FLAG_IPV6_ROUTABLE;
9002 			}
9003 			rtfree_locked(v6Route);
9004 			v6Route = NULL;
9005 		}
9006 
9007 		u_int8_t ipv6_signature_len = sizeof(interface_details.ipv6_signature.signature);
9008 		u_int16_t ipv6_signature_flags;
9009 		if (ifnet_get_netsignature(interface, AF_INET6, &ipv6_signature_len, &ipv6_signature_flags,
9010 		    (u_int8_t *)&interface_details.ipv6_signature) != 0) {
9011 			ipv6_signature_len = 0;
9012 		}
9013 		interface_details.ipv6_signature.signature_len = ipv6_signature_len;
9014 
9015 		ifnet_lock_shared(interface);
9016 		struct ifaddr *ifa = NULL;
9017 		TAILQ_FOREACH(ifa, &interface->if_addrhead, ifa_link) {
9018 			IFA_LOCK(ifa);
9019 			if (ifa->ifa_addr->sa_family == AF_INET) {
9020 				interface_details.flags |= NECP_INTERFACE_FLAG_HAS_NETMASK;
9021 				interface_details.ipv4_netmask = ((struct in_ifaddr *)ifa)->ia_sockmask.sin_addr.s_addr;
9022 				if (interface->if_flags & IFF_BROADCAST) {
9023 					interface_details.flags |= NECP_INTERFACE_FLAG_HAS_BROADCAST;
9024 					interface_details.ipv4_broadcast = ((struct in_ifaddr *)ifa)->ia_broadaddr.sin_addr.s_addr;
9025 				}
9026 			}
9027 			IFA_UNLOCK(ifa);
9028 		}
9029 
9030 		interface_details.radio_type = interface->if_radio_type;
9031 		if (interface_details.radio_type == 0 && interface->if_delegated.ifp) {
9032 			interface_details.radio_type = interface->if_delegated.ifp->if_radio_type;
9033 		}
9034 		ifnet_lock_done(interface);
9035 	}
9036 
9037 	ifnet_head_done();
9038 	lck_mtx_unlock(rnh_lock);
9039 
9040 	// If the client is using an older version of the struct, copy that length
9041 	error = copyout(&interface_details, uap->buffer, sizeof(interface_details));
9042 	if (error) {
9043 		NECPLOG(LOG_ERR, "necp_client_copy_interface copyout error (%d)", error);
9044 		goto done;
9045 	}
9046 done:
9047 	*retval = error;
9048 
9049 	return error;
9050 }
9051 
9052 #if SKYWALK
9053 
9054 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)9055 necp_client_get_interface_address(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9056 {
9057 	int error = 0;
9058 	u_int32_t interface_index = IFSCOPE_NONE;
9059 	struct sockaddr_storage address = {};
9060 	const size_t buffer_size = uap->buffer_size;
9061 
9062 	if (uap->client_id == 0 || uap->client_id_len != sizeof(u_int32_t) ||
9063 	    buffer_size < sizeof(struct sockaddr_in) ||
9064 	    buffer_size > sizeof(struct sockaddr_storage) ||
9065 	    uap->buffer == 0) {
9066 		NECPLOG0(LOG_ERR, "necp_client_get_interface_address bad input");
9067 		error = EINVAL;
9068 		goto done;
9069 	}
9070 
9071 	error = copyin(uap->client_id, &interface_index, sizeof(u_int32_t));
9072 	if (error) {
9073 		NECPLOG(LOG_ERR, "necp_client_get_interface_address copyin interface_index error (%d)", error);
9074 		goto done;
9075 	}
9076 
9077 	if (interface_index == IFSCOPE_NONE) {
9078 		error = ENOENT;
9079 		NECPLOG(LOG_ERR, "necp_client_get_interface_address bad interface_index (%d)", interface_index);
9080 		goto done;
9081 	}
9082 
9083 	error = copyin(uap->buffer, &address, buffer_size);
9084 	if (error) {
9085 		NECPLOG(LOG_ERR, "necp_client_get_interface_address copyin address error (%d)", error);
9086 		goto done;
9087 	}
9088 
9089 	if (address.ss_family != AF_INET && address.ss_family != AF_INET6) {
9090 		error = EINVAL;
9091 		NECPLOG(LOG_ERR, "necp_client_get_interface_address invalid address family (%u)", address.ss_family);
9092 		goto done;
9093 	}
9094 
9095 	if (address.ss_len != buffer_size) {
9096 		error = EINVAL;
9097 		NECPLOG(LOG_ERR, "necp_client_get_interface_address invalid address length (%u)", address.ss_len);
9098 		goto done;
9099 	}
9100 
9101 	ifnet_head_lock_shared();
9102 	ifnet_t ifp = NULL;
9103 	if (interface_index != IFSCOPE_NONE && interface_index <= (u_int32_t)if_index) {
9104 		ifp = ifindex2ifnet[interface_index];
9105 	}
9106 	ifnet_head_done();
9107 	if (ifp == NULL) {
9108 		error = ENOENT;
9109 		NECPLOG0(LOG_ERR, "necp_client_get_interface_address no matching interface found");
9110 		goto done;
9111 	}
9112 
9113 	struct rtentry *rt = rtalloc1_scoped((struct sockaddr *)&address, 0, 0, interface_index);
9114 	if (rt == NULL) {
9115 		error = EINVAL;
9116 		NECPLOG0(LOG_ERR, "necp_client_get_interface_address route lookup failed");
9117 		goto done;
9118 	}
9119 
9120 	uint32_t gencount = 0;
9121 	struct sockaddr_storage local_address = {};
9122 	error = flow_route_select_laddr((union sockaddr_in_4_6 *)&local_address,
9123 	    (union sockaddr_in_4_6 *)&address, ifp, rt, &gencount, 1);
9124 	rtfree(rt);
9125 	rt = NULL;
9126 
9127 	if (error) {
9128 		NECPLOG(LOG_ERR, "necp_client_get_interface_address local address selection failed (%d)", error);
9129 		goto done;
9130 	}
9131 
9132 	if (local_address.ss_len > buffer_size) {
9133 		error = EMSGSIZE;
9134 		NECPLOG(LOG_ERR, "necp_client_get_interface_address local address too long for buffer (%u)",
9135 		    local_address.ss_len);
9136 		goto done;
9137 	}
9138 
9139 	error = copyout(&local_address, uap->buffer, local_address.ss_len);
9140 	if (error) {
9141 		NECPLOG(LOG_ERR, "necp_client_get_interface_address copyout error (%d)", error);
9142 		goto done;
9143 	}
9144 done:
9145 	*retval = error;
9146 
9147 	return error;
9148 }
9149 
9150 extern char *proc_name_address(void *p);
9151 
9152 int
necp_stats_ctor(struct skmem_obj_info * oi,struct skmem_obj_info * oim,void * arg,uint32_t skmflag)9153 necp_stats_ctor(struct skmem_obj_info *oi, struct skmem_obj_info *oim,
9154     void *arg, uint32_t skmflag)
9155 {
9156 #pragma unused(arg, skmflag)
9157 	struct necp_all_kstats *kstats = SKMEM_OBJ_ADDR(oi);
9158 
9159 	ASSERT(oim != NULL && SKMEM_OBJ_ADDR(oim) != NULL);
9160 	ASSERT(SKMEM_OBJ_SIZE(oi) == SKMEM_OBJ_SIZE(oim));
9161 
9162 	kstats->necp_stats_ustats = SKMEM_OBJ_ADDR(oim);
9163 
9164 	return 0;
9165 }
9166 
9167 int
necp_stats_dtor(void * addr,void * arg)9168 necp_stats_dtor(void *addr, void *arg)
9169 {
9170 #pragma unused(addr, arg)
9171 	struct necp_all_kstats *kstats = addr;
9172 
9173 	kstats->necp_stats_ustats = NULL;
9174 
9175 	return 0;
9176 }
9177 
9178 static void
necp_fd_insert_stats_arena(struct necp_fd_data * fd_data,struct necp_arena_info * nai)9179 necp_fd_insert_stats_arena(struct necp_fd_data *fd_data, struct necp_arena_info *nai)
9180 {
9181 	NECP_FD_ASSERT_LOCKED(fd_data);
9182 	VERIFY(!(nai->nai_flags & NAIF_ATTACHED));
9183 	VERIFY(nai->nai_chain.le_next == NULL && nai->nai_chain.le_prev == NULL);
9184 
9185 	LIST_INSERT_HEAD(&fd_data->stats_arena_list, nai, nai_chain);
9186 	nai->nai_flags |= NAIF_ATTACHED;
9187 	necp_arena_info_retain(nai);    // for the list
9188 }
9189 
9190 static void
necp_fd_remove_stats_arena(struct necp_fd_data * fd_data,struct necp_arena_info * nai)9191 necp_fd_remove_stats_arena(struct necp_fd_data *fd_data, struct necp_arena_info *nai)
9192 {
9193 #pragma unused(fd_data)
9194 	NECP_FD_ASSERT_LOCKED(fd_data);
9195 	VERIFY(nai->nai_flags & NAIF_ATTACHED);
9196 	VERIFY(nai->nai_use_count >= 1);
9197 
9198 	LIST_REMOVE(nai, nai_chain);
9199 	nai->nai_flags &= ~NAIF_ATTACHED;
9200 	nai->nai_chain.le_next = NULL;
9201 	nai->nai_chain.le_prev = NULL;
9202 	necp_arena_info_release(nai);   // for the list
9203 }
9204 
9205 static struct necp_arena_info *
necp_fd_mredirect_stats_arena(struct necp_fd_data * fd_data,struct proc * proc)9206 necp_fd_mredirect_stats_arena(struct necp_fd_data *fd_data, struct proc *proc)
9207 {
9208 	struct necp_arena_info *nai, *nai_ret = NULL;
9209 
9210 	NECP_FD_ASSERT_LOCKED(fd_data);
9211 
9212 	// Redirect currently-active stats arena and remove it from the active state;
9213 	// upon process resumption, new flow request would trigger the creation of
9214 	// another active arena.
9215 	if ((nai = fd_data->stats_arena_active) != NULL) {
9216 		boolean_t need_defunct = FALSE;
9217 
9218 		ASSERT(!(nai->nai_flags & (NAIF_REDIRECT | NAIF_DEFUNCT)));
9219 		VERIFY(nai->nai_use_count >= 2);
9220 		ASSERT(nai->nai_arena != NULL);
9221 		ASSERT(nai->nai_mmap.ami_mapref != NULL);
9222 
9223 		int err = skmem_arena_mredirect(nai->nai_arena, &nai->nai_mmap, proc, &need_defunct);
9224 		VERIFY(err == 0);
9225 		// must be TRUE since we don't mmap the arena more than once
9226 		VERIFY(need_defunct == TRUE);
9227 
9228 		nai->nai_flags |= NAIF_REDIRECT;
9229 		nai_ret = nai;  // return to caller
9230 
9231 		necp_arena_info_release(nai);   // for fd_data
9232 		fd_data->stats_arena_active = nai = NULL;
9233 	}
9234 
9235 #if (DEVELOPMENT || DEBUG)
9236 	// make sure this list now contains nothing but redirected/defunct arenas
9237 	LIST_FOREACH(nai, &fd_data->stats_arena_list, nai_chain) {
9238 		ASSERT(nai->nai_use_count >= 1);
9239 		ASSERT(nai->nai_flags & (NAIF_REDIRECT | NAIF_DEFUNCT));
9240 	}
9241 #endif /* (DEVELOPMENT || DEBUG) */
9242 
9243 	return nai_ret;
9244 }
9245 
9246 static void
necp_arena_info_retain(struct necp_arena_info * nai)9247 necp_arena_info_retain(struct necp_arena_info *nai)
9248 {
9249 	nai->nai_use_count++;
9250 	VERIFY(nai->nai_use_count != 0);
9251 }
9252 
9253 static void
necp_arena_info_release(struct necp_arena_info * nai)9254 necp_arena_info_release(struct necp_arena_info *nai)
9255 {
9256 	VERIFY(nai->nai_use_count > 0);
9257 	if (--nai->nai_use_count == 0) {
9258 		necp_arena_info_free(nai);
9259 	}
9260 }
9261 
9262 static struct necp_arena_info *
necp_arena_info_alloc(void)9263 necp_arena_info_alloc(void)
9264 {
9265 	return zalloc_flags(necp_arena_info_zone, Z_WAITOK | Z_ZERO);
9266 }
9267 
9268 static void
necp_arena_info_free(struct necp_arena_info * nai)9269 necp_arena_info_free(struct necp_arena_info *nai)
9270 {
9271 	VERIFY(nai->nai_chain.le_next == NULL && nai->nai_chain.le_prev == NULL);
9272 	VERIFY(nai->nai_use_count == 0);
9273 
9274 	// NOTE: destroying the arena requires that all outstanding objects
9275 	// that were allocated have been freed, else it will assert.
9276 	if (nai->nai_arena != NULL) {
9277 		skmem_arena_munmap(nai->nai_arena, &nai->nai_mmap);
9278 		skmem_arena_release(nai->nai_arena);
9279 		OSDecrementAtomic(&necp_arena_count);
9280 		nai->nai_arena = NULL;
9281 		nai->nai_roff = 0;
9282 	}
9283 
9284 	ASSERT(nai->nai_arena == NULL);
9285 	ASSERT(nai->nai_mmap.ami_mapref == NULL);
9286 	ASSERT(nai->nai_mmap.ami_arena == NULL);
9287 	ASSERT(nai->nai_mmap.ami_maptask == TASK_NULL);
9288 
9289 	zfree(necp_arena_info_zone, nai);
9290 }
9291 
9292 static int
necp_arena_create(struct necp_fd_data * fd_data,size_t obj_size,size_t obj_cnt,struct proc * p)9293 necp_arena_create(struct necp_fd_data *fd_data, size_t obj_size, size_t obj_cnt, struct proc *p)
9294 {
9295 	struct skmem_region_params srp_ustats = {};
9296 	struct skmem_region_params srp_kstats = {};
9297 	struct necp_arena_info *nai;
9298 	char name[32];
9299 	int error = 0;
9300 
9301 	NECP_FD_ASSERT_LOCKED(fd_data);
9302 	ASSERT(fd_data->stats_arena_active == NULL);
9303 	ASSERT(p != PROC_NULL);
9304 	ASSERT(proc_pid(p) == fd_data->proc_pid);
9305 
9306 	// inherit the default parameters for the stats region
9307 	srp_ustats = *skmem_get_default(SKMEM_REGION_USTATS);
9308 	srp_kstats = *skmem_get_default(SKMEM_REGION_KSTATS);
9309 
9310 	// enable multi-segment mode
9311 	srp_ustats.srp_cflags &= ~SKMEM_REGION_CR_MONOLITHIC;
9312 	srp_kstats.srp_cflags &= ~SKMEM_REGION_CR_MONOLITHIC;
9313 
9314 	// configure and adjust the region parameters
9315 	srp_ustats.srp_r_obj_cnt = srp_kstats.srp_r_obj_cnt = obj_cnt;
9316 	srp_ustats.srp_r_obj_size = srp_kstats.srp_r_obj_size = obj_size;
9317 	skmem_region_params_config(&srp_ustats);
9318 	skmem_region_params_config(&srp_kstats);
9319 
9320 	nai = necp_arena_info_alloc();
9321 
9322 	nai->nai_proc_pid = fd_data->proc_pid;
9323 	(void) snprintf(name, sizeof(name), "stats-%u.%s.%d", fd_data->stats_arena_gencnt, proc_name_address(p), fd_data->proc_pid);
9324 	nai->nai_arena = skmem_arena_create_for_necp(name, &srp_ustats, &srp_kstats, &error);
9325 	ASSERT(nai->nai_arena != NULL || error != 0);
9326 	if (error != 0) {
9327 		NECPLOG(LOG_ERR, "failed to create stats arena for pid %d\n", fd_data->proc_pid);
9328 	} else {
9329 		OSIncrementAtomic(&necp_arena_count);
9330 
9331 		// Get region offsets from base of mmap span; the arena
9332 		// doesn't need to be mmap'd at this point, since we simply
9333 		// compute the relative offset.
9334 		nai->nai_roff = skmem_arena_get_region_offset(nai->nai_arena, SKMEM_REGION_USTATS);
9335 
9336 		// map to the task/process; upon success, the base address of the region
9337 		// will be returned in nai_mmap.ami_mapaddr; this can be communicated to
9338 		// the process.
9339 		error = skmem_arena_mmap(nai->nai_arena, p, &nai->nai_mmap);
9340 		if (error != 0) {
9341 			NECPLOG(LOG_ERR, "failed to map stats arena for pid %d\n", fd_data->proc_pid);
9342 		}
9343 	}
9344 
9345 	if (error == 0) {
9346 		fd_data->stats_arena_active = nai;
9347 		necp_arena_info_retain(nai);    // for fd_data
9348 		necp_fd_insert_stats_arena(fd_data, nai);
9349 		++fd_data->stats_arena_gencnt;
9350 	} else {
9351 		necp_arena_info_free(nai);
9352 	}
9353 
9354 	return error;
9355 }
9356 
9357 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)9358 necp_arena_stats_obj_alloc(struct necp_fd_data *fd_data,
9359     mach_vm_offset_t *off,
9360     struct necp_arena_info **stats_arena,
9361     void **kstats_kaddr,
9362     boolean_t cansleep)
9363 {
9364 	struct skmem_cache *kstats_cp = NULL;
9365 	void *ustats_obj = NULL;
9366 	void *kstats_obj = NULL;
9367 	struct necp_all_kstats *kstats = NULL;
9368 	struct skmem_obj_info kstats_oi = {};
9369 
9370 	ASSERT(off != NULL);
9371 	ASSERT(stats_arena != NULL && *stats_arena == NULL);
9372 	ASSERT(kstats_kaddr != NULL && *kstats_kaddr == NULL);
9373 
9374 	NECP_FD_ASSERT_LOCKED(fd_data);
9375 	ASSERT(fd_data->stats_arena_active != NULL);
9376 	ASSERT(fd_data->stats_arena_active->nai_arena != NULL);
9377 
9378 	kstats_cp = skmem_arena_necp(fd_data->stats_arena_active->nai_arena)->arc_kstats_cache;
9379 	if ((kstats_obj = skmem_cache_alloc(kstats_cp, (cansleep ? SKMEM_SLEEP : SKMEM_NOSLEEP))) == NULL) {
9380 		return ENOMEM;
9381 	}
9382 
9383 	kstats = (struct necp_all_kstats*)kstats_obj;
9384 	ustats_obj = kstats->necp_stats_ustats;
9385 
9386 	skmem_cache_get_obj_info(kstats_cp, kstats_obj, &kstats_oi, NULL);
9387 	ASSERT(SKMEM_OBJ_SIZE(&kstats_oi) >= sizeof(struct necp_all_stats));
9388 	// reset all stats counters
9389 	bzero(ustats_obj, SKMEM_OBJ_SIZE(&kstats_oi));
9390 	bzero(&kstats->necp_stats_comm, sizeof(struct necp_all_stats));
9391 	*stats_arena = fd_data->stats_arena_active;
9392 	*kstats_kaddr = kstats_obj;
9393 	// kstats and ustats are mirrored and have the same offset
9394 	*off = fd_data->stats_arena_active->nai_roff + SKMEM_OBJ_ROFF(&kstats_oi);
9395 
9396 	return 0;
9397 }
9398 
9399 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)9400 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)
9401 {
9402 #pragma unused(fd_data)
9403 	NECP_FD_ASSERT_LOCKED(fd_data);
9404 
9405 	ASSERT(stats_arena != NULL);
9406 	ASSERT(stats_arena->nai_arena != NULL);
9407 	ASSERT(kstats_kaddr != NULL && *kstats_kaddr != NULL);
9408 	ASSERT(ustats_uaddr != NULL);
9409 
9410 	skmem_cache_free(skmem_arena_necp(stats_arena->nai_arena)->arc_kstats_cache, *kstats_kaddr);
9411 	*kstats_kaddr = NULL;
9412 	*ustats_uaddr = 0;
9413 }
9414 
9415 // This routine returns the KVA of the sysctls object, as well as the
9416 // offset of that object relative to the mmap base address for the
9417 // task/process.
9418 static void *
necp_arena_sysctls_obj(struct necp_fd_data * fd_data,mach_vm_offset_t * off,size_t * size)9419 necp_arena_sysctls_obj(struct necp_fd_data *fd_data, mach_vm_offset_t *off, size_t *size)
9420 {
9421 	void *objaddr;
9422 
9423 	NECP_FD_ASSERT_LOCKED(fd_data);
9424 	ASSERT(fd_data->sysctl_arena != NULL);
9425 
9426 	// kernel virtual address of the sysctls object
9427 	objaddr = skmem_arena_system_sysctls_obj_addr(fd_data->sysctl_arena);
9428 	ASSERT(objaddr != NULL);
9429 
9430 	// Return the relative offset of the sysctls object; there is
9431 	// only 1 object in the entire sysctls region, and therefore the
9432 	// object's offset is simply the region's offset in the arena.
9433 	// (sysctl_mmap.ami_mapaddr + offset) is the address of this object
9434 	// in the task/process.
9435 	if (off != NULL) {
9436 		*off = fd_data->system_sysctls_roff;
9437 	}
9438 
9439 	if (size != NULL) {
9440 		*size = skmem_arena_system_sysctls_obj_size(fd_data->sysctl_arena);
9441 		ASSERT(*size != 0);
9442 	}
9443 
9444 	return objaddr;
9445 }
9446 
9447 static void
necp_stats_arenas_destroy(struct necp_fd_data * fd_data,boolean_t closing)9448 necp_stats_arenas_destroy(struct necp_fd_data *fd_data, boolean_t closing)
9449 {
9450 	struct necp_arena_info *nai, *nai_tmp;
9451 
9452 	NECP_FD_ASSERT_LOCKED(fd_data);
9453 
9454 	// If reaping (not closing), release reference only for idle active arena; the reference
9455 	// count must be 2 by now, when it's not being referred to by any clients/flows.
9456 	if ((nai = fd_data->stats_arena_active) != NULL && (closing || nai->nai_use_count == 2)) {
9457 		VERIFY(nai->nai_use_count >= 2);
9458 		necp_arena_info_release(nai);   // for fd_data
9459 		fd_data->stats_arena_active = NULL;
9460 	}
9461 
9462 	// clean up any defunct arenas left in the list
9463 	LIST_FOREACH_SAFE(nai, &fd_data->stats_arena_list, nai_chain, nai_tmp) {
9464 		// If reaping, release reference if the list holds the last one
9465 		if (closing || nai->nai_use_count == 1) {
9466 			VERIFY(nai->nai_use_count >= 1);
9467 			// callee unchains nai (and may free it)
9468 			necp_fd_remove_stats_arena(fd_data, nai);
9469 		}
9470 	}
9471 }
9472 
9473 static void
necp_sysctl_arena_destroy(struct necp_fd_data * fd_data)9474 necp_sysctl_arena_destroy(struct necp_fd_data *fd_data)
9475 {
9476 	NECP_FD_ASSERT_LOCKED(fd_data);
9477 
9478 	// NOTE: destroying the arena requires that all outstanding objects
9479 	// that were allocated have been freed, else it will assert.
9480 	if (fd_data->sysctl_arena != NULL) {
9481 		skmem_arena_munmap(fd_data->sysctl_arena, &fd_data->sysctl_mmap);
9482 		skmem_arena_release(fd_data->sysctl_arena);
9483 		OSDecrementAtomic(&necp_sysctl_arena_count);
9484 		fd_data->sysctl_arena = NULL;
9485 		fd_data->system_sysctls_roff = 0;
9486 	}
9487 }
9488 
9489 static int
necp_arena_initialize(struct necp_fd_data * fd_data,bool locked)9490 necp_arena_initialize(struct necp_fd_data *fd_data, bool locked)
9491 {
9492 	int error = 0;
9493 	size_t stats_obj_size = MAX(sizeof(struct necp_all_stats), sizeof(struct necp_all_kstats));
9494 
9495 	if (!locked) {
9496 		NECP_FD_LOCK(fd_data);
9497 	}
9498 	if (fd_data->stats_arena_active == NULL) {
9499 		error = necp_arena_create(fd_data, stats_obj_size,
9500 		    NECP_MAX_PER_PROCESS_CLIENT_STATISTICS_STRUCTS,
9501 		    current_proc());
9502 	}
9503 	if (!locked) {
9504 		NECP_FD_UNLOCK(fd_data);
9505 	}
9506 
9507 	return error;
9508 }
9509 
9510 static int
necp_sysctl_arena_initialize(struct necp_fd_data * fd_data,bool locked)9511 necp_sysctl_arena_initialize(struct necp_fd_data *fd_data, bool locked)
9512 {
9513 	int error = 0;
9514 
9515 	if (!locked) {
9516 		NECP_FD_LOCK(fd_data);
9517 	}
9518 
9519 	NECP_FD_ASSERT_LOCKED(fd_data);
9520 
9521 	if (fd_data->sysctl_arena == NULL) {
9522 		char name[32];
9523 		struct proc *p = current_proc();
9524 
9525 		ASSERT(p != PROC_NULL);
9526 		ASSERT(proc_pid(p) == fd_data->proc_pid);
9527 
9528 		(void) snprintf(name, sizeof(name), "sysctl.%s.%d", proc_name_address(p), fd_data->proc_pid);
9529 		fd_data->sysctl_arena = skmem_arena_create_for_system(name, &error);
9530 		ASSERT(fd_data->sysctl_arena != NULL || error != 0);
9531 		if (error != 0) {
9532 			NECPLOG(LOG_ERR, "failed to create arena for pid %d\n", fd_data->proc_pid);
9533 		} else {
9534 			OSIncrementAtomic(&necp_sysctl_arena_count);
9535 
9536 			// Get region offsets from base of mmap span; the arena
9537 			// doesn't need to be mmap'd at this point, since we simply
9538 			// compute the relative offset.
9539 			fd_data->system_sysctls_roff = skmem_arena_get_region_offset(fd_data->sysctl_arena, SKMEM_REGION_SYSCTLS);
9540 
9541 			// map to the task/process; upon success, the base address of the region
9542 			// will be returned in nai_mmap.ami_mapaddr; this can be communicated to
9543 			// the process.
9544 			error = skmem_arena_mmap(fd_data->sysctl_arena, p, &fd_data->sysctl_mmap);
9545 			if (error != 0) {
9546 				NECPLOG(LOG_ERR, "failed to map sysctl arena for pid %d\n", fd_data->proc_pid);
9547 				necp_sysctl_arena_destroy(fd_data);
9548 			}
9549 		}
9550 	}
9551 
9552 	if (!locked) {
9553 		NECP_FD_UNLOCK(fd_data);
9554 	}
9555 
9556 	return error;
9557 }
9558 
9559 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)9560 necp_client_stats_bufreq(struct necp_fd_data *fd_data,
9561     struct necp_client *client,
9562     struct necp_client_flow_registration *flow_registration,
9563     struct necp_stats_bufreq *bufreq,
9564     struct necp_stats_hdr *out_header)
9565 {
9566 	int error = 0;
9567 	NECP_CLIENT_ASSERT_LOCKED(client);
9568 	NECP_FD_ASSERT_LOCKED(fd_data);
9569 
9570 	if ((bufreq->necp_stats_bufreq_id == NECP_CLIENT_STATISTICS_BUFREQ_ID) &&
9571 	    ((bufreq->necp_stats_bufreq_type == NECP_CLIENT_STATISTICS_TYPE_TCP &&
9572 	    bufreq->necp_stats_bufreq_ver == NECP_CLIENT_STATISTICS_TYPE_TCP_CURRENT_VER) ||
9573 	    (bufreq->necp_stats_bufreq_type == NECP_CLIENT_STATISTICS_TYPE_UDP &&
9574 	    bufreq->necp_stats_bufreq_ver == NECP_CLIENT_STATISTICS_TYPE_UDP_CURRENT_VER) ||
9575 	    (bufreq->necp_stats_bufreq_type == NECP_CLIENT_STATISTICS_TYPE_QUIC &&
9576 	    bufreq->necp_stats_bufreq_ver == NECP_CLIENT_STATISTICS_TYPE_QUIC_CURRENT_VER)) &&
9577 	    (bufreq->necp_stats_bufreq_size == sizeof(struct necp_all_stats))) {
9578 		// There should be one and only one stats allocation per client.
9579 		// If asked more than once, we just repeat ourselves.
9580 		if (flow_registration->ustats_uaddr == 0) {
9581 			mach_vm_offset_t off;
9582 			ASSERT(flow_registration->stats_arena == NULL);
9583 			ASSERT(flow_registration->kstats_kaddr == NULL);
9584 			ASSERT(flow_registration->ustats_uaddr == 0);
9585 			error = necp_arena_stats_obj_alloc(fd_data, &off, &flow_registration->stats_arena, &flow_registration->kstats_kaddr, FALSE);
9586 			if (error == 0) {
9587 				// upon success, hold a reference for the client; this is released when the client is removed/closed
9588 				ASSERT(flow_registration->stats_arena != NULL);
9589 				necp_arena_info_retain(flow_registration->stats_arena);
9590 
9591 				// compute user address based on mapping info and object offset
9592 				flow_registration->ustats_uaddr = flow_registration->stats_arena->nai_mmap.ami_mapaddr + off;
9593 
9594 				// add to collect_stats list
9595 				NECP_STATS_LIST_LOCK_EXCLUSIVE();
9596 				necp_client_retain_locked(client); // Add a reference to the client
9597 				LIST_INSERT_HEAD(&necp_collect_stats_flow_list, flow_registration, collect_stats_chain);
9598 				NECP_STATS_LIST_UNLOCK();
9599 				necp_schedule_collect_stats_clients(FALSE);
9600 			} else {
9601 				ASSERT(flow_registration->stats_arena == NULL);
9602 				ASSERT(flow_registration->kstats_kaddr == NULL);
9603 			}
9604 		}
9605 		if (flow_registration->ustats_uaddr != 0) {
9606 			ASSERT(error == 0);
9607 			ASSERT(flow_registration->stats_arena != NULL);
9608 			ASSERT(flow_registration->kstats_kaddr != NULL);
9609 
9610 			struct necp_all_kstats *kstats = (struct necp_all_kstats *)flow_registration->kstats_kaddr;
9611 			kstats->necp_stats_ustats->all_stats_u.tcp_stats.necp_tcp_hdr.necp_stats_type = bufreq->necp_stats_bufreq_type;
9612 			kstats->necp_stats_ustats->all_stats_u.tcp_stats.necp_tcp_hdr.necp_stats_ver = bufreq->necp_stats_bufreq_ver;
9613 
9614 			if (out_header) {
9615 				out_header->necp_stats_type = bufreq->necp_stats_bufreq_type;
9616 				out_header->necp_stats_ver = bufreq->necp_stats_bufreq_ver;
9617 			}
9618 
9619 			bufreq->necp_stats_bufreq_uaddr = flow_registration->ustats_uaddr;
9620 		}
9621 	} else {
9622 		error = EINVAL;
9623 	}
9624 
9625 	return error;
9626 }
9627 
9628 static int
necp_client_stats_initial(struct necp_client_flow_registration * flow_registration,uint32_t stats_type,uint32_t stats_ver)9629 necp_client_stats_initial(struct necp_client_flow_registration *flow_registration, uint32_t stats_type, uint32_t stats_ver)
9630 {
9631 	// An attempted create
9632 	assert(flow_registration->stats_handler_context == NULL);
9633 	assert(flow_registration->stats_arena);
9634 	assert(flow_registration->ustats_uaddr);
9635 	assert(flow_registration->kstats_kaddr);
9636 
9637 	int error = 0;
9638 
9639 	switch (stats_type) {
9640 	case NECP_CLIENT_STATISTICS_TYPE_TCP: {
9641 		if (stats_ver == NECP_CLIENT_STATISTICS_TYPE_TCP_VER_1) {
9642 			flow_registration->stats_handler_context = ntstat_userland_stats_open((userland_stats_provider_context *)flow_registration,
9643 			    NSTAT_PROVIDER_TCP_USERLAND, 0, necp_request_tcp_netstats, necp_find_extension_info);
9644 			if (flow_registration->stats_handler_context == NULL) {
9645 				error = EIO;
9646 			}
9647 		} else {
9648 			error = ENOTSUP;
9649 		}
9650 		break;
9651 	}
9652 	case NECP_CLIENT_STATISTICS_TYPE_UDP: {
9653 		if (stats_ver == NECP_CLIENT_STATISTICS_TYPE_UDP_VER_1) {
9654 			flow_registration->stats_handler_context = ntstat_userland_stats_open((userland_stats_provider_context *)flow_registration,
9655 			    NSTAT_PROVIDER_UDP_USERLAND, 0, necp_request_udp_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_QUIC: {
9665 		if (stats_ver == NECP_CLIENT_STATISTICS_TYPE_QUIC_VER_1 && flow_registration->flags & NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS) {
9666 			flow_registration->stats_handler_context = ntstat_userland_stats_open((userland_stats_provider_context *)flow_registration,
9667 			    NSTAT_PROVIDER_QUIC_USERLAND, 0, necp_request_quic_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 	default: {
9677 		error = ENOTSUP;
9678 		break;
9679 	}
9680 	}
9681 	return error;
9682 }
9683 
9684 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)9685 necp_stats_initialize(struct necp_fd_data *fd_data,
9686     struct necp_client *client,
9687     struct necp_client_flow_registration *flow_registration,
9688     struct necp_stats_bufreq *bufreq)
9689 {
9690 	int error = 0;
9691 	struct necp_stats_hdr stats_hdr = {};
9692 
9693 	NECP_CLIENT_ASSERT_LOCKED(client);
9694 	NECP_FD_ASSERT_LOCKED(fd_data);
9695 	VERIFY(fd_data->stats_arena_active != NULL);
9696 	VERIFY(fd_data->stats_arena_active->nai_arena != NULL);
9697 	VERIFY(!(fd_data->stats_arena_active->nai_flags & (NAIF_REDIRECT | NAIF_DEFUNCT)));
9698 
9699 	if (bufreq == NULL) {
9700 		return EINVAL;
9701 	}
9702 
9703 	// Setup stats region
9704 	error = necp_client_stats_bufreq(fd_data, client, flow_registration, bufreq, &stats_hdr);
9705 	if (error) {
9706 		return error;
9707 	}
9708 	// Notify ntstat about new flow
9709 	if (flow_registration->stats_handler_context == NULL) {
9710 		error = necp_client_stats_initial(flow_registration, stats_hdr.necp_stats_type, stats_hdr.necp_stats_ver);
9711 		if (flow_registration->stats_handler_context != NULL) {
9712 			ntstat_userland_stats_event(flow_registration->stats_handler_context, NECP_CLIENT_STATISTICS_EVENT_INIT);
9713 		}
9714 		NECP_CLIENT_FLOW_LOG(client, flow_registration, "Initialized stats <error %d>", error);
9715 	}
9716 
9717 	return error;
9718 }
9719 
9720 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)9721 necp_client_map_sysctls(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9722 {
9723 	int result = 0;
9724 	if (!retval) {
9725 		retval = &result;
9726 	}
9727 
9728 	do {
9729 		mach_vm_address_t uaddr = 0;
9730 		if (uap->buffer_size != sizeof(uaddr)) {
9731 			*retval = EINVAL;
9732 			break;
9733 		}
9734 
9735 		*retval = necp_sysctl_arena_initialize(fd_data, false);
9736 		if (*retval != 0) {
9737 			break;
9738 		}
9739 
9740 		mach_vm_offset_t off = 0;
9741 		void *location = NULL;
9742 		NECP_FD_LOCK(fd_data);
9743 		location = necp_arena_sysctls_obj(fd_data, &off, NULL);
9744 		NECP_FD_UNLOCK(fd_data);
9745 
9746 		if (location == NULL) {
9747 			*retval = ENOENT;
9748 			break;
9749 		}
9750 
9751 		uaddr = fd_data->sysctl_mmap.ami_mapaddr + off;
9752 		*retval = copyout(&uaddr, uap->buffer, sizeof(uaddr));
9753 	} while (false);
9754 
9755 	return *retval;
9756 }
9757 
9758 #endif /* !SKYWALK */
9759 
9760 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)9761 necp_client_copy_route_statistics(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9762 {
9763 	int error = 0;
9764 	struct necp_client *client = NULL;
9765 	uuid_t client_id;
9766 
9767 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t) ||
9768 	    uap->buffer_size < sizeof(struct necp_stat_counts) || uap->buffer == 0) {
9769 		NECPLOG0(LOG_ERR, "necp_client_copy_route_statistics bad input");
9770 		error = EINVAL;
9771 		goto done;
9772 	}
9773 
9774 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
9775 	if (error) {
9776 		NECPLOG(LOG_ERR, "necp_client_copy_route_statistics copyin client_id error (%d)", error);
9777 		goto done;
9778 	}
9779 
9780 	// Lock
9781 	NECP_FD_LOCK(fd_data);
9782 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
9783 	if (client != NULL) {
9784 		NECP_CLIENT_ROUTE_LOCK(client);
9785 		struct necp_stat_counts route_stats = {};
9786 		if (client->current_route != NULL && client->current_route->rt_stats != NULL) {
9787 			struct nstat_counts     *rt_stats = client->current_route->rt_stats;
9788 			atomic_get_64(route_stats.necp_stat_rxpackets, &rt_stats->nstat_rxpackets);
9789 			atomic_get_64(route_stats.necp_stat_rxbytes, &rt_stats->nstat_rxbytes);
9790 			atomic_get_64(route_stats.necp_stat_txpackets, &rt_stats->nstat_txpackets);
9791 			atomic_get_64(route_stats.necp_stat_txbytes, &rt_stats->nstat_txbytes);
9792 			route_stats.necp_stat_rxduplicatebytes = rt_stats->nstat_rxduplicatebytes;
9793 			route_stats.necp_stat_rxoutoforderbytes = rt_stats->nstat_rxoutoforderbytes;
9794 			route_stats.necp_stat_txretransmit = rt_stats->nstat_txretransmit;
9795 			route_stats.necp_stat_connectattempts = rt_stats->nstat_connectattempts;
9796 			route_stats.necp_stat_connectsuccesses = rt_stats->nstat_connectsuccesses;
9797 			route_stats.necp_stat_min_rtt = rt_stats->nstat_min_rtt;
9798 			route_stats.necp_stat_avg_rtt = rt_stats->nstat_avg_rtt;
9799 			route_stats.necp_stat_var_rtt = rt_stats->nstat_var_rtt;
9800 			route_stats.necp_stat_route_flags = client->current_route->rt_flags;
9801 		}
9802 
9803 		// Unlock before copying out
9804 		NECP_CLIENT_ROUTE_UNLOCK(client);
9805 		NECP_CLIENT_UNLOCK(client);
9806 		NECP_FD_UNLOCK(fd_data);
9807 
9808 		error = copyout(&route_stats, uap->buffer, sizeof(route_stats));
9809 		if (error) {
9810 			NECPLOG(LOG_ERR, "necp_client_copy_route_statistics copyout error (%d)", error);
9811 		}
9812 	} else {
9813 		// Unlock
9814 		NECP_FD_UNLOCK(fd_data);
9815 		error = ENOENT;
9816 	}
9817 
9818 
9819 done:
9820 	*retval = error;
9821 	return error;
9822 }
9823 
9824 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_update_cache(struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)9825 necp_client_update_cache(struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9826 {
9827 	int error = 0;
9828 	struct necp_client *client = NULL;
9829 	uuid_t client_id;
9830 
9831 	if (uap->client_id == 0 || uap->client_id_len != sizeof(uuid_t)) {
9832 		error = EINVAL;
9833 		goto done;
9834 	}
9835 
9836 	error = copyin(uap->client_id, client_id, sizeof(uuid_t));
9837 	if (error) {
9838 		NECPLOG(LOG_ERR, "necp_client_update_cache copyin client_id error (%d)", error);
9839 		goto done;
9840 	}
9841 
9842 	NECP_FD_LOCK(fd_data);
9843 	client = necp_client_fd_find_client_and_lock(fd_data, client_id);
9844 	if (client == NULL) {
9845 		NECP_FD_UNLOCK(fd_data);
9846 		error = ENOENT;
9847 		goto done;
9848 	}
9849 
9850 	struct necp_client_flow_registration *flow_registration = necp_client_find_flow(client, client_id);
9851 	if (flow_registration == NULL) {
9852 		NECP_CLIENT_UNLOCK(client);
9853 		NECP_FD_UNLOCK(fd_data);
9854 		error = ENOENT;
9855 		goto done;
9856 	}
9857 
9858 	NECP_CLIENT_ROUTE_LOCK(client);
9859 	// This needs to be changed when TFO/ECN is supported by multiple flows
9860 	struct necp_client_flow *flow = LIST_FIRST(&flow_registration->flow_list);
9861 	if (flow == NULL ||
9862 	    (flow->remote_addr.sa.sa_family != AF_INET &&
9863 	    flow->remote_addr.sa.sa_family != AF_INET6) ||
9864 	    (flow->local_addr.sa.sa_family != AF_INET &&
9865 	    flow->local_addr.sa.sa_family != AF_INET6)) {
9866 		error = EINVAL;
9867 		NECPLOG(LOG_ERR, "necp_client_update_cache no flow error (%d)", error);
9868 		goto done_unlock;
9869 	}
9870 
9871 	necp_cache_buffer cache_buffer;
9872 	memset(&cache_buffer, 0, sizeof(cache_buffer));
9873 
9874 	if (uap->buffer_size != sizeof(necp_cache_buffer) ||
9875 	    uap->buffer == USER_ADDR_NULL) {
9876 		error = EINVAL;
9877 		goto done_unlock;
9878 	}
9879 
9880 	error = copyin(uap->buffer, &cache_buffer, sizeof(cache_buffer));
9881 	if (error) {
9882 		NECPLOG(LOG_ERR, "necp_client_update_cache copyin cache buffer error (%d)", error);
9883 		goto done_unlock;
9884 	}
9885 
9886 	if (cache_buffer.necp_cache_buf_type == NECP_CLIENT_CACHE_TYPE_ECN &&
9887 	    cache_buffer.necp_cache_buf_ver == NECP_CLIENT_CACHE_TYPE_ECN_VER_1) {
9888 		if (cache_buffer.necp_cache_buf_size != sizeof(necp_tcp_ecn_cache) ||
9889 		    cache_buffer.necp_cache_buf_addr == USER_ADDR_NULL) {
9890 			error = EINVAL;
9891 			goto done_unlock;
9892 		}
9893 
9894 		necp_tcp_ecn_cache ecn_cache_buffer;
9895 		memset(&ecn_cache_buffer, 0, sizeof(ecn_cache_buffer));
9896 
9897 		error = copyin(cache_buffer.necp_cache_buf_addr, &ecn_cache_buffer, sizeof(necp_tcp_ecn_cache));
9898 		if (error) {
9899 			NECPLOG(LOG_ERR, "necp_client_update_cache copyin ecn cache buffer error (%d)", error);
9900 			goto done_unlock;
9901 		}
9902 
9903 		if (client->current_route != NULL && client->current_route->rt_ifp != NULL) {
9904 			if (!client->platform_binary) {
9905 				ecn_cache_buffer.necp_tcp_ecn_heuristics_success = 0;
9906 			}
9907 			tcp_heuristics_ecn_update(&ecn_cache_buffer, client->current_route->rt_ifp,
9908 			    (union sockaddr_in_4_6 *)&flow->local_addr);
9909 		}
9910 	} else if (cache_buffer.necp_cache_buf_type == NECP_CLIENT_CACHE_TYPE_TFO &&
9911 	    cache_buffer.necp_cache_buf_ver == NECP_CLIENT_CACHE_TYPE_TFO_VER_1) {
9912 		if (cache_buffer.necp_cache_buf_size != sizeof(necp_tcp_tfo_cache) ||
9913 		    cache_buffer.necp_cache_buf_addr == USER_ADDR_NULL) {
9914 			error = EINVAL;
9915 			goto done_unlock;
9916 		}
9917 
9918 		necp_tcp_tfo_cache tfo_cache_buffer;
9919 		memset(&tfo_cache_buffer, 0, sizeof(tfo_cache_buffer));
9920 
9921 		error = copyin(cache_buffer.necp_cache_buf_addr, &tfo_cache_buffer, sizeof(necp_tcp_tfo_cache));
9922 		if (error) {
9923 			NECPLOG(LOG_ERR, "necp_client_update_cache copyin tfo cache buffer error (%d)", error);
9924 			goto done_unlock;
9925 		}
9926 
9927 		if (client->current_route != NULL && client->current_route->rt_ifp != NULL) {
9928 			if (!client->platform_binary) {
9929 				tfo_cache_buffer.necp_tcp_tfo_heuristics_success = 0;
9930 			}
9931 			tcp_heuristics_tfo_update(&tfo_cache_buffer, client->current_route->rt_ifp,
9932 			    (union sockaddr_in_4_6 *)&flow->local_addr,
9933 			    (union sockaddr_in_4_6 *)&flow->remote_addr);
9934 		}
9935 	} else {
9936 		error = EINVAL;
9937 	}
9938 done_unlock:
9939 	NECP_CLIENT_ROUTE_UNLOCK(client);
9940 	NECP_CLIENT_UNLOCK(client);
9941 	NECP_FD_UNLOCK(fd_data);
9942 done:
9943 	*retval = error;
9944 	return error;
9945 }
9946 
9947 // Most results will fit into this size
9948 struct necp_client_signable_default {
9949 	uuid_t client_id;
9950 	u_int32_t sign_type;
9951 	u_int8_t signable_data[NECP_CLIENT_ACTION_SIGN_DEFAULT_DATA_LENGTH];
9952 } __attribute__((__packed__));
9953 
9954 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_sign(__unused struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)9955 necp_client_sign(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
9956 {
9957 	int error = 0;
9958 	u_int8_t tag[NECP_CLIENT_ACTION_SIGN_TAG_LENGTH] = {};
9959 	struct necp_client_signable *signable = NULL;
9960 	struct necp_client_signable *allocated_signable = NULL;
9961 	struct necp_client_signable_default default_signable = {};
9962 	size_t tag_size = sizeof(tag);
9963 
9964 	const size_t signable_length = uap->client_id_len;
9965 	const size_t return_tag_length = uap->buffer_size;
9966 
9967 	*retval = 0;
9968 
9969 	const bool has_resolver_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_VALIDATED_RESOLVER, 0) == 0);
9970 	if (!has_resolver_entitlement) {
9971 		NECPLOG0(LOG_ERR, "Process does not hold the necessary entitlement to sign resolver answers");
9972 		error = EPERM;
9973 		goto done;
9974 	}
9975 
9976 	if (uap->client_id == 0 || signable_length < sizeof(*signable) || signable_length > NECP_CLIENT_ACTION_SIGN_MAX_TOTAL_LENGTH) {
9977 		error = EINVAL;
9978 		goto done;
9979 	}
9980 
9981 	if (uap->buffer == 0 || return_tag_length != NECP_CLIENT_ACTION_SIGN_TAG_LENGTH) {
9982 		error = EINVAL;
9983 		goto done;
9984 	}
9985 
9986 	if (signable_length <= sizeof(default_signable)) {
9987 		signable = (struct necp_client_signable *)&default_signable;
9988 	} else {
9989 		if ((allocated_signable = (struct necp_client_signable *)kalloc_data(signable_length, Z_WAITOK | Z_ZERO)) == NULL) {
9990 			NECPLOG(LOG_ERR, "necp_client_sign allocate signable %zu failed", signable_length);
9991 			error = ENOMEM;
9992 			goto done;
9993 		}
9994 		signable = allocated_signable;
9995 	}
9996 
9997 	error = copyin(uap->client_id, signable, signable_length);
9998 	if (error) {
9999 		NECPLOG(LOG_ERR, "necp_client_sign copyin signable error (%d)", error);
10000 		goto done;
10001 	}
10002 
10003 	size_t data_length = 0;
10004 	switch (signable->sign_type) {
10005 	case NECP_CLIENT_SIGN_TYPE_RESOLVER_ANSWER:
10006 	case NECP_CLIENT_SIGN_TYPE_SYSTEM_RESOLVER_ANSWER: {
10007 		data_length = (sizeof(struct necp_client_host_resolver_answer) - sizeof(struct necp_client_signable));
10008 		if (signable_length < (sizeof(struct necp_client_signable) + data_length)) {
10009 			error = EINVAL;
10010 			goto done;
10011 		}
10012 		struct necp_client_host_resolver_answer *signable_struct = (struct necp_client_host_resolver_answer *)signable;
10013 		if (signable_struct->hostname_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10014 		    signable_length != (sizeof(struct necp_client_signable) + data_length + signable_struct->hostname_length)) {
10015 			error = EINVAL;
10016 			goto done;
10017 		}
10018 		data_length += signable_struct->hostname_length;
10019 		break;
10020 	}
10021 	case NECP_CLIENT_SIGN_TYPE_BROWSE_RESULT:
10022 	case NECP_CLIENT_SIGN_TYPE_SYSTEM_BROWSE_RESULT: {
10023 		data_length = (sizeof(struct necp_client_browse_result) - sizeof(struct necp_client_signable));
10024 		if (signable_length < (sizeof(struct necp_client_signable) + data_length)) {
10025 			error = EINVAL;
10026 			goto done;
10027 		}
10028 		struct necp_client_browse_result *signable_struct = (struct necp_client_browse_result *)signable;
10029 		if (signable_struct->service_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10030 		    signable_length != (sizeof(struct necp_client_signable) + data_length + signable_struct->service_length)) {
10031 			error = EINVAL;
10032 			goto done;
10033 		}
10034 		data_length += signable_struct->service_length;
10035 		break;
10036 	}
10037 	case NECP_CLIENT_SIGN_TYPE_SERVICE_RESOLVER_ANSWER:
10038 	case NECP_CLIENT_SIGN_TYPE_SYSTEM_SERVICE_RESOLVER_ANSWER: {
10039 		data_length = (sizeof(struct necp_client_service_resolver_answer) - sizeof(struct necp_client_signable));
10040 		if (signable_length < (sizeof(struct necp_client_signable) + data_length)) {
10041 			error = EINVAL;
10042 			goto done;
10043 		}
10044 		struct necp_client_service_resolver_answer *signable_struct = (struct necp_client_service_resolver_answer *)signable;
10045 		if (signable_struct->service_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10046 		    signable_struct->hostname_length > NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH ||
10047 		    signable_length != (sizeof(struct necp_client_signable) + data_length + signable_struct->service_length + signable_struct->hostname_length)) {
10048 			error = EINVAL;
10049 			goto done;
10050 		}
10051 		data_length += signable_struct->service_length;
10052 		data_length += signable_struct->hostname_length;
10053 		break;
10054 	}
10055 	default: {
10056 		NECPLOG(LOG_ERR, "necp_client_sign unknown signable type (%u)", signable->sign_type);
10057 		error = EINVAL;
10058 		goto done;
10059 	}
10060 	}
10061 
10062 	error = necp_sign_resolver_answer(signable->client_id, signable->sign_type,
10063 	    signable->signable_data, data_length,
10064 	    tag, &tag_size);
10065 	if (tag_size != sizeof(tag)) {
10066 		NECPLOG(LOG_ERR, "necp_client_sign unexpected tag size %zu", tag_size);
10067 		error = EINVAL;
10068 		goto done;
10069 	}
10070 	error = copyout(tag, uap->buffer, tag_size);
10071 	if (error) {
10072 		NECPLOG(LOG_ERR, "necp_client_sign copyout error (%d)", error);
10073 		goto done;
10074 	}
10075 
10076 done:
10077 	if (allocated_signable != NULL) {
10078 		kfree_data(allocated_signable, signable_length);
10079 		allocated_signable = NULL;
10080 	}
10081 	*retval = error;
10082 	return error;
10083 }
10084 
10085 // Most results will fit into this size
10086 struct necp_client_validatable_default {
10087 	struct necp_client_signature signature;
10088 	struct necp_client_signable_default signable;
10089 } __attribute__((__packed__));
10090 
10091 static NECP_CLIENT_ACTION_FUNCTION int
necp_client_validate(__unused struct necp_fd_data * fd_data,struct necp_client_action_args * uap,int * retval)10092 necp_client_validate(__unused struct necp_fd_data *fd_data, struct necp_client_action_args *uap, int *retval)
10093 {
10094 	int error = 0;
10095 	struct necp_client_validatable *validatable = NULL;
10096 	struct necp_client_validatable *allocated_validatable = NULL;
10097 	struct necp_client_validatable_default default_validatable = {};
10098 
10099 	const size_t validatable_length = uap->client_id_len;
10100 
10101 	*retval = 0;
10102 
10103 	const bool has_resolver_entitlement = (priv_check_cred(kauth_cred_get(), PRIV_NET_VALIDATED_RESOLVER, 0) == 0);
10104 	if (!has_resolver_entitlement) {
10105 		NECPLOG0(LOG_ERR, "Process does not hold the necessary entitlement to directly validate resolver answers");
10106 		error = EPERM;
10107 		goto done;
10108 	}
10109 
10110 	if (uap->client_id == 0 || validatable_length < sizeof(*validatable) ||
10111 	    validatable_length > (NECP_CLIENT_ACTION_SIGN_MAX_TOTAL_LENGTH + NECP_CLIENT_ACTION_SIGN_TAG_LENGTH)) {
10112 		error = EINVAL;
10113 		goto done;
10114 	}
10115 
10116 	if (validatable_length <= sizeof(default_validatable)) {
10117 		validatable = (struct necp_client_validatable *)&default_validatable;
10118 	} else {
10119 		if ((allocated_validatable = (struct necp_client_validatable *)kalloc_data(validatable_length, Z_WAITOK | Z_ZERO)) == NULL) {
10120 			NECPLOG(LOG_ERR, "necp_client_validate allocate struct %zu failed", validatable_length);
10121 			error = ENOMEM;
10122 			goto done;
10123 		}
10124 		validatable = allocated_validatable;
10125 	}
10126 
10127 	error = copyin(uap->client_id, validatable, validatable_length);
10128 	if (error) {
10129 		NECPLOG(LOG_ERR, "necp_client_validate copyin error (%d)", error);
10130 		goto done;
10131 	}
10132 
10133 	const bool validated = necp_validate_resolver_answer(validatable->signable.client_id, validatable->signable.sign_type,
10134 	    validatable->signable.signable_data, validatable_length - sizeof(struct necp_client_validatable),
10135 	    validatable->signature.signed_tag, sizeof(validatable->signature.signed_tag));
10136 	if (!validated) {
10137 		// Return EAUTH to indicate that the signature failed
10138 		error = EAUTH;
10139 	}
10140 
10141 done:
10142 	if (allocated_validatable != NULL) {
10143 		kfree_data(allocated_validatable, validatable_length);
10144 		allocated_validatable = NULL;
10145 	}
10146 	*retval = error;
10147 	return error;
10148 }
10149 
10150 int
necp_client_action(struct proc * p,struct necp_client_action_args * uap,int * retval)10151 necp_client_action(struct proc *p, struct necp_client_action_args *uap, int *retval)
10152 {
10153 	struct fileproc *fp;
10154 	int error = 0;
10155 	int return_value = 0;
10156 	struct necp_fd_data *fd_data = NULL;
10157 
10158 	error = necp_find_fd_data(p, uap->necp_fd, &fp, &fd_data);
10159 	if (error != 0) {
10160 		NECPLOG(LOG_ERR, "necp_client_action find fd error (%d)", error);
10161 		return error;
10162 	}
10163 
10164 	u_int32_t action = uap->action;
10165 
10166 #if CONFIG_MACF
10167 	error = mac_necp_check_client_action(p, fp->fp_glob, action);
10168 	if (error) {
10169 		return_value = error;
10170 		goto done;
10171 	}
10172 #endif /* MACF */
10173 
10174 	switch (action) {
10175 	case NECP_CLIENT_ACTION_ADD: {
10176 		return_value = necp_client_add(p, fd_data, uap, retval);
10177 		break;
10178 	}
10179 	case NECP_CLIENT_ACTION_CLAIM: {
10180 		return_value = necp_client_claim(p, fd_data, uap, retval);
10181 		break;
10182 	}
10183 	case NECP_CLIENT_ACTION_REMOVE: {
10184 		return_value = necp_client_remove(fd_data, uap, retval);
10185 		break;
10186 	}
10187 	case NECP_CLIENT_ACTION_COPY_PARAMETERS:
10188 	case NECP_CLIENT_ACTION_COPY_RESULT:
10189 	case NECP_CLIENT_ACTION_COPY_UPDATED_RESULT: {
10190 		return_value = necp_client_copy(fd_data, uap, retval);
10191 		break;
10192 	}
10193 	case NECP_CLIENT_ACTION_COPY_LIST: {
10194 		return_value = necp_client_list(fd_data, uap, retval);
10195 		break;
10196 	}
10197 	case NECP_CLIENT_ACTION_ADD_FLOW: {
10198 		return_value = necp_client_add_flow(fd_data, uap, retval);
10199 		break;
10200 	}
10201 	case NECP_CLIENT_ACTION_REMOVE_FLOW: {
10202 		return_value = necp_client_remove_flow(fd_data, uap, retval);
10203 		break;
10204 	}
10205 #if SKYWALK
10206 	case NECP_CLIENT_ACTION_REQUEST_NEXUS_INSTANCE: {
10207 		return_value = necp_client_request_nexus(fd_data, uap, retval);
10208 		break;
10209 	}
10210 #endif /* !SKYWALK */
10211 	case NECP_CLIENT_ACTION_AGENT: {
10212 		return_value = necp_client_agent_action(fd_data, uap, retval);
10213 		break;
10214 	}
10215 	case NECP_CLIENT_ACTION_COPY_AGENT: {
10216 		return_value = necp_client_copy_agent(fd_data, uap, retval);
10217 		break;
10218 	}
10219 	case NECP_CLIENT_ACTION_AGENT_USE: {
10220 		return_value = necp_client_agent_use(fd_data, uap, retval);
10221 		break;
10222 	}
10223 	case NECP_CLIENT_ACTION_ACQUIRE_AGENT_TOKEN: {
10224 		return_value = necp_client_acquire_agent_token(fd_data, uap, retval);
10225 		break;
10226 	}
10227 	case NECP_CLIENT_ACTION_COPY_INTERFACE: {
10228 		return_value = necp_client_copy_interface(fd_data, uap, retval);
10229 		break;
10230 	}
10231 #if SKYWALK
10232 	case NECP_CLIENT_ACTION_GET_INTERFACE_ADDRESS: {
10233 		return_value = necp_client_get_interface_address(fd_data, uap, retval);
10234 		break;
10235 	}
10236 	case NECP_CLIENT_ACTION_SET_STATISTICS: {
10237 		return_value = ENOTSUP;
10238 		break;
10239 	}
10240 	case NECP_CLIENT_ACTION_MAP_SYSCTLS: {
10241 		return_value = necp_client_map_sysctls(fd_data, uap, retval);
10242 		break;
10243 	}
10244 #endif /* !SKYWALK */
10245 	case NECP_CLIENT_ACTION_COPY_ROUTE_STATISTICS: {
10246 		return_value = necp_client_copy_route_statistics(fd_data, uap, retval);
10247 		break;
10248 	}
10249 	case NECP_CLIENT_ACTION_UPDATE_CACHE: {
10250 		return_value = necp_client_update_cache(fd_data, uap, retval);
10251 		break;
10252 	}
10253 	case NECP_CLIENT_ACTION_COPY_CLIENT_UPDATE: {
10254 		return_value = necp_client_copy_client_update(fd_data, uap, retval);
10255 		break;
10256 	}
10257 	case NECP_CLIENT_ACTION_SIGN: {
10258 		return_value = necp_client_sign(fd_data, uap, retval);
10259 		break;
10260 	}
10261 	case NECP_CLIENT_ACTION_VALIDATE: {
10262 		return_value = necp_client_validate(fd_data, uap, retval);
10263 		break;
10264 	}
10265 	default: {
10266 		NECPLOG(LOG_ERR, "necp_client_action unknown action (%u)", action);
10267 		return_value = EINVAL;
10268 		break;
10269 	}
10270 	}
10271 
10272 done:
10273 	fp_drop(p, uap->necp_fd, fp, 0);
10274 	return return_value;
10275 }
10276 
10277 #define NECP_MAX_MATCH_POLICY_PARAMETER_SIZE 1024
10278 
10279 int
necp_match_policy(struct proc * p,struct necp_match_policy_args * uap,int32_t * retval)10280 necp_match_policy(struct proc *p, struct necp_match_policy_args *uap, int32_t *retval)
10281 {
10282 #pragma unused(retval)
10283 	u_int8_t *parameters = NULL;
10284 	struct necp_aggregate_result returned_result;
10285 	int error = 0;
10286 
10287 	if (uap == NULL) {
10288 		error = EINVAL;
10289 		goto done;
10290 	}
10291 
10292 	if (uap->parameters == 0 || uap->parameters_size == 0 || uap->parameters_size > NECP_MAX_MATCH_POLICY_PARAMETER_SIZE || uap->returned_result == 0) {
10293 		error = EINVAL;
10294 		goto done;
10295 	}
10296 
10297 	parameters = (u_int8_t *)kalloc_data(uap->parameters_size, Z_WAITOK | Z_ZERO);
10298 	if (parameters == NULL) {
10299 		error = ENOMEM;
10300 		goto done;
10301 	}
10302 	// Copy parameters in
10303 	error = copyin(uap->parameters, parameters, uap->parameters_size);
10304 	if (error) {
10305 		goto done;
10306 	}
10307 
10308 	error = necp_application_find_policy_match_internal(p, parameters, uap->parameters_size,
10309 	    &returned_result, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, false, false, NULL);
10310 	if (error) {
10311 		goto done;
10312 	}
10313 
10314 	// Copy return value back
10315 	error = copyout(&returned_result, uap->returned_result, sizeof(struct necp_aggregate_result));
10316 	if (error) {
10317 		goto done;
10318 	}
10319 done:
10320 	if (parameters != NULL) {
10321 		kfree_data(parameters, uap->parameters_size);
10322 	}
10323 	return error;
10324 }
10325 
10326 /// Socket operations
10327 
10328 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)10329 necp_set_socket_attribute(u_int8_t *buffer, size_t buffer_length, u_int8_t type, char **buffer_p, bool *single_tlv)
10330 {
10331 	int error = 0;
10332 	int cursor = 0;
10333 	size_t string_size = 0;
10334 	char *local_string = NULL;
10335 	u_int8_t *value = NULL;
10336 	char *buffer_to_free = NULL;
10337 
10338 	cursor = necp_buffer_find_tlv(buffer, buffer_length, 0, type, NULL, 0);
10339 	if (cursor < 0) {
10340 		// This will clear out the parameter
10341 		goto done;
10342 	}
10343 
10344 	string_size = necp_buffer_get_tlv_length(buffer, cursor);
10345 	if (single_tlv != NULL && (buffer_length == sizeof(struct necp_tlv_header) + string_size)) {
10346 		*single_tlv = true;
10347 	}
10348 	if (string_size == 0 || string_size > NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) {
10349 		// This will clear out the parameter
10350 		goto done;
10351 	}
10352 
10353 	local_string = (char *)kalloc_data(string_size + 1, Z_WAITOK | Z_ZERO);
10354 	if (local_string == NULL) {
10355 		NECPLOG(LOG_ERR, "Failed to allocate a socket attribute buffer (size %zu)", string_size);
10356 		goto fail;
10357 	}
10358 
10359 	value = necp_buffer_get_tlv_value(buffer, cursor, NULL);
10360 	if (value == NULL) {
10361 		NECPLOG0(LOG_ERR, "Failed to get socket attribute");
10362 		goto fail;
10363 	}
10364 
10365 	memcpy(local_string, value, string_size);
10366 	local_string[string_size] = 0;
10367 
10368 done:
10369 	buffer_to_free = *buffer_p;
10370 
10371 	// Protect switching of buffer pointer
10372 	necp_lock_socket_attributes();
10373 	*buffer_p = local_string;
10374 	necp_unlock_socket_attributes();
10375 
10376 	if (buffer_to_free != NULL) {
10377 		kfree_data_addr(buffer_to_free);
10378 	}
10379 	return 0;
10380 fail:
10381 	if (local_string != NULL) {
10382 		kfree_data(local_string, string_size + 1);
10383 	}
10384 	return error;
10385 }
10386 
10387 errno_t
necp_set_socket_attributes(struct inp_necp_attributes * attributes,struct sockopt * sopt)10388 necp_set_socket_attributes(struct inp_necp_attributes *attributes, struct sockopt *sopt)
10389 {
10390 	int error = 0;
10391 	u_int8_t *buffer = NULL;
10392 	bool single_tlv = false;
10393 	size_t valsize = sopt->sopt_valsize;
10394 	if (valsize == 0 ||
10395 	    valsize > ((sizeof(struct necp_tlv_header) + NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) * 4)) {
10396 		goto done;
10397 	}
10398 
10399 	buffer = (u_int8_t *)kalloc_data(valsize, Z_WAITOK | Z_ZERO);
10400 	if (buffer == NULL) {
10401 		goto done;
10402 	}
10403 
10404 	error = sooptcopyin(sopt, buffer, valsize, 0);
10405 	if (error) {
10406 		goto done;
10407 	}
10408 
10409 	// If NECP_TLV_ATTRIBUTE_DOMAIN_CONTEXT is being set/cleared separately from the other attributes,
10410 	// do not clear other attributes.
10411 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_DOMAIN_CONTEXT, &attributes->inp_domain_context, &single_tlv);
10412 	if (error) {
10413 		NECPLOG0(LOG_ERR, "Could not set domain context TLV for socket attributes");
10414 		goto done;
10415 	}
10416 	if (single_tlv == true) {
10417 		goto done;
10418 	}
10419 
10420 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_DOMAIN, &attributes->inp_domain, NULL);
10421 	if (error) {
10422 		NECPLOG0(LOG_ERR, "Could not set domain TLV for socket attributes");
10423 		goto done;
10424 	}
10425 
10426 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_DOMAIN_OWNER, &attributes->inp_domain_owner, NULL);
10427 	if (error) {
10428 		NECPLOG0(LOG_ERR, "Could not set domain owner TLV for socket attributes");
10429 		goto done;
10430 	}
10431 
10432 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_TRACKER_DOMAIN, &attributes->inp_tracker_domain, NULL);
10433 	if (error) {
10434 		NECPLOG0(LOG_ERR, "Could not set tracker domain TLV for socket attributes");
10435 		goto done;
10436 	}
10437 
10438 	error = necp_set_socket_attribute(buffer, valsize, NECP_TLV_ATTRIBUTE_ACCOUNT, &attributes->inp_account, NULL);
10439 	if (error) {
10440 		NECPLOG0(LOG_ERR, "Could not set account TLV for socket attributes");
10441 		goto done;
10442 	}
10443 
10444 done:
10445 	NECP_SOCKET_ATTRIBUTE_LOG("NECP ATTRIBUTES SOCKET - domain <%s> owner <%s> context <%s> tracker domain <%s> account <%s>",
10446 	    attributes->inp_domain,
10447 	    attributes->inp_domain_owner,
10448 	    attributes->inp_domain_context,
10449 	    attributes->inp_tracker_domain,
10450 	    attributes->inp_account);
10451 
10452 	if (necp_debug) {
10453 		NECPLOG(LOG_DEBUG, "Set on socket: Domain %s, Domain owner %s, Domain context %s, Tracker domain %s, Account %s",
10454 		    attributes->inp_domain,
10455 		    attributes->inp_domain_owner,
10456 		    attributes->inp_domain_context,
10457 		    attributes->inp_tracker_domain,
10458 		    attributes->inp_account);
10459 	}
10460 
10461 	if (buffer != NULL) {
10462 		kfree_data(buffer, valsize);
10463 	}
10464 
10465 	return error;
10466 }
10467 
10468 errno_t
necp_get_socket_attributes(struct inp_necp_attributes * attributes,struct sockopt * sopt)10469 necp_get_socket_attributes(struct inp_necp_attributes *attributes, struct sockopt *sopt)
10470 {
10471 	int error = 0;
10472 	u_int8_t *buffer = NULL;
10473 	u_int8_t *cursor = NULL;
10474 	size_t valsize = 0;
10475 
10476 	if (attributes->inp_domain != NULL) {
10477 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_domain);
10478 	}
10479 	if (attributes->inp_domain_owner != NULL) {
10480 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_domain_owner);
10481 	}
10482 	if (attributes->inp_domain_context != NULL) {
10483 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_domain_context);
10484 	}
10485 	if (attributes->inp_tracker_domain != NULL) {
10486 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_tracker_domain);
10487 	}
10488 	if (attributes->inp_account != NULL) {
10489 		valsize += sizeof(struct necp_tlv_header) + strlen(attributes->inp_account);
10490 	}
10491 	if (valsize == 0) {
10492 		goto done;
10493 	}
10494 
10495 	buffer = (u_int8_t *)kalloc_data(valsize, Z_WAITOK | Z_ZERO);
10496 	if (buffer == NULL) {
10497 		goto done;
10498 	}
10499 
10500 	cursor = buffer;
10501 	if (attributes->inp_domain != NULL) {
10502 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_DOMAIN, strlen(attributes->inp_domain), attributes->inp_domain,
10503 		    buffer, valsize);
10504 	}
10505 
10506 	if (attributes->inp_domain_owner != NULL) {
10507 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_DOMAIN_OWNER, strlen(attributes->inp_domain_owner), attributes->inp_domain_owner,
10508 		    buffer, valsize);
10509 	}
10510 
10511 	if (attributes->inp_domain_context != NULL) {
10512 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_DOMAIN_CONTEXT, strlen(attributes->inp_domain_context), attributes->inp_domain_context,
10513 		    buffer, valsize);
10514 	}
10515 
10516 	if (attributes->inp_tracker_domain != NULL) {
10517 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_TRACKER_DOMAIN, strlen(attributes->inp_tracker_domain), attributes->inp_tracker_domain,
10518 		    buffer, valsize);
10519 	}
10520 
10521 	if (attributes->inp_account != NULL) {
10522 		cursor = necp_buffer_write_tlv(cursor, NECP_TLV_ATTRIBUTE_ACCOUNT, strlen(attributes->inp_account), attributes->inp_account,
10523 		    buffer, valsize);
10524 	}
10525 
10526 	error = sooptcopyout(sopt, buffer, valsize);
10527 	if (error) {
10528 		goto done;
10529 	}
10530 done:
10531 	if (buffer != NULL) {
10532 		kfree_data(buffer, valsize);
10533 	}
10534 
10535 	return error;
10536 }
10537 
10538 int
necp_set_socket_resolver_signature(struct inpcb * inp,struct sockopt * sopt)10539 necp_set_socket_resolver_signature(struct inpcb *inp, struct sockopt *sopt)
10540 {
10541 	const size_t valsize = sopt->sopt_valsize;
10542 	if (valsize > NECP_CLIENT_ACTION_SIGN_MAX_TOTAL_LENGTH + NECP_CLIENT_ACTION_SIGN_TAG_LENGTH) {
10543 		return EINVAL;
10544 	}
10545 
10546 	necp_lock_socket_attributes();
10547 	if (inp->inp_resolver_signature != NULL) {
10548 		kfree_data(inp->inp_resolver_signature, inp->inp_resolver_signature_length);
10549 	}
10550 	inp->inp_resolver_signature_length = 0;
10551 
10552 	int error = 0;
10553 	if (valsize > 0) {
10554 		inp->inp_resolver_signature = kalloc_data(valsize, Z_WAITOK | Z_ZERO);
10555 		if ((error = sooptcopyin(sopt, inp->inp_resolver_signature, valsize,
10556 		    valsize)) != 0) {
10557 			// Free the signature buffer if the copyin failed
10558 			kfree_data(inp->inp_resolver_signature, valsize);
10559 		} else {
10560 			inp->inp_resolver_signature_length = valsize;
10561 		}
10562 	}
10563 	necp_unlock_socket_attributes();
10564 
10565 	return error;
10566 }
10567 
10568 int
necp_get_socket_resolver_signature(struct inpcb * inp,struct sockopt * sopt)10569 necp_get_socket_resolver_signature(struct inpcb *inp, struct sockopt *sopt)
10570 {
10571 	int error = 0;
10572 	necp_lock_socket_attributes();
10573 	if (inp->inp_resolver_signature == NULL ||
10574 	    inp->inp_resolver_signature_length == 0) {
10575 		error = ENOENT;
10576 	} else {
10577 		error = sooptcopyout(sopt, inp->inp_resolver_signature,
10578 		    inp->inp_resolver_signature_length);
10579 	}
10580 	necp_unlock_socket_attributes();
10581 	return error;
10582 }
10583 
10584 bool
necp_socket_has_resolver_signature(struct inpcb * inp)10585 necp_socket_has_resolver_signature(struct inpcb *inp)
10586 {
10587 	necp_lock_socket_attributes();
10588 	bool has_signature = (inp->inp_resolver_signature != NULL && inp->inp_resolver_signature_length != 0);
10589 	necp_unlock_socket_attributes();
10590 	return has_signature;
10591 }
10592 
10593 bool
necp_socket_resolver_signature_matches_address(struct inpcb * inp,union necp_sockaddr_union * address)10594 necp_socket_resolver_signature_matches_address(struct inpcb *inp, union necp_sockaddr_union *address)
10595 {
10596 	bool matches_address = false;
10597 	necp_lock_socket_attributes();
10598 	if (inp->inp_resolver_signature != NULL && inp->inp_resolver_signature_length > 0 && address->sa.sa_len > 0) {
10599 		struct necp_client_validatable *validatable = (struct necp_client_validatable *)inp->inp_resolver_signature;
10600 		if (inp->inp_resolver_signature_length > sizeof(struct necp_client_validatable) &&
10601 		    validatable->signable.sign_type == NECP_CLIENT_SIGN_TYPE_SYSTEM_RESOLVER_ANSWER) {
10602 			size_t data_length = inp->inp_resolver_signature_length - sizeof(struct necp_client_validatable);
10603 			if (data_length >= (sizeof(struct necp_client_host_resolver_answer) - sizeof(struct necp_client_signable))) {
10604 				struct necp_client_host_resolver_answer *answer_struct = (struct necp_client_host_resolver_answer *)&validatable->signable;
10605 				if (data_length == (sizeof(struct necp_client_host_resolver_answer) + answer_struct->hostname_length - sizeof(struct necp_client_signable)) &&
10606 				    answer_struct->address_answer.sa.sa_family == address->sa.sa_family &&
10607 				    answer_struct->address_answer.sa.sa_len == address->sa.sa_len &&
10608 				    (answer_struct->address_answer.sin.sin_port == 0 ||
10609 				    answer_struct->address_answer.sin.sin_port == address->sin.sin_port) &&
10610 				    ((answer_struct->address_answer.sa.sa_family == AF_INET &&
10611 				    answer_struct->address_answer.sin.sin_addr.s_addr == address->sin.sin_addr.s_addr) ||
10612 				    (answer_struct->address_answer.sa.sa_family == AF_INET6 &&
10613 				    memcmp(&answer_struct->address_answer.sin6.sin6_addr, &address->sin6.sin6_addr, sizeof(struct in6_addr)) == 0))) {
10614 					// Address matches
10615 					const bool validated = necp_validate_resolver_answer(validatable->signable.client_id,
10616 					    validatable->signable.sign_type,
10617 					    validatable->signable.signable_data, data_length,
10618 					    validatable->signature.signed_tag, sizeof(validatable->signature.signed_tag));
10619 					if (validated) {
10620 						// Answer is validated
10621 						matches_address = true;
10622 					}
10623 				}
10624 			}
10625 		}
10626 	}
10627 	necp_unlock_socket_attributes();
10628 	return matches_address;
10629 }
10630 
10631 /*
10632  * necp_set_socket_domain_attributes
10633  * Called from soconnectlock/soconnectxlock to directly set the tracker domain and owner for
10634  * a newly marked tracker socket.
10635  */
10636 errno_t
necp_set_socket_domain_attributes(struct socket * so,const char * domain,const char * domain_owner)10637 necp_set_socket_domain_attributes(struct socket *so, const char *domain, const char *domain_owner)
10638 {
10639 	int error = 0;
10640 	struct inpcb *inp = NULL;
10641 	u_int8_t *buffer = NULL;
10642 	size_t valsize = 0;
10643 	char *buffer_to_free = NULL;
10644 
10645 	if (SOCK_DOM(so) != PF_INET && SOCK_DOM(so) != PF_INET6) {
10646 		error = EINVAL;
10647 		goto fail;
10648 	}
10649 
10650 	// Set domain (required)
10651 
10652 	valsize = strlen(domain);
10653 	if (valsize == 0 || valsize > NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) {
10654 		error = EINVAL;
10655 		goto fail;
10656 	}
10657 
10658 	buffer = (u_int8_t *)kalloc_data(valsize + 1, Z_WAITOK | Z_ZERO);
10659 	if (buffer == NULL) {
10660 		error = ENOMEM;
10661 		goto fail;
10662 	}
10663 	bcopy(domain, buffer, valsize);
10664 	buffer[valsize] = 0;
10665 
10666 	inp = sotoinpcb(so);
10667 	// Do not overwrite a previously set domain if tracker domain is different.
10668 	if (inp->inp_necp_attributes.inp_domain != NULL) {
10669 		if (strlen(inp->inp_necp_attributes.inp_domain) != strlen(domain) ||
10670 		    strncmp(inp->inp_necp_attributes.inp_domain, domain, strlen(domain)) != 0) {
10671 			buffer_to_free = inp->inp_necp_attributes.inp_tracker_domain;
10672 			// Protect switching of buffer pointer
10673 			necp_lock_socket_attributes();
10674 			inp->inp_necp_attributes.inp_tracker_domain = (char *)buffer;
10675 			necp_unlock_socket_attributes();
10676 			if (buffer_to_free != NULL) {
10677 				kfree_data_addr(buffer_to_free);
10678 			}
10679 		} else {
10680 			kfree_data_addr(buffer);
10681 		}
10682 	} else {
10683 		// Protect switching of buffer pointer
10684 		necp_lock_socket_attributes();
10685 		inp->inp_necp_attributes.inp_domain = (char *)buffer;
10686 		necp_unlock_socket_attributes();
10687 	}
10688 	buffer = NULL;
10689 
10690 	// set domain_owner (required only for tracker)
10691 	if (!(so->so_flags1 & SOF1_KNOWN_TRACKER)) {
10692 		goto done;
10693 	}
10694 
10695 	valsize = strlen(domain_owner);
10696 	if (valsize == 0 || valsize > NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH) {
10697 		error = EINVAL;
10698 		goto fail;
10699 	}
10700 
10701 	buffer = (u_int8_t *)kalloc_data(valsize + 1, Z_WAITOK | Z_ZERO);
10702 	if (buffer == NULL) {
10703 		error = ENOMEM;
10704 		goto fail;
10705 	}
10706 	bcopy(domain_owner, buffer, valsize);
10707 	buffer[valsize] = 0;
10708 
10709 	inp = sotoinpcb(so);
10710 
10711 	buffer_to_free = inp->inp_necp_attributes.inp_domain_owner;
10712 	// Protect switching of buffer pointer
10713 	necp_lock_socket_attributes();
10714 	inp->inp_necp_attributes.inp_domain_owner = (char *)buffer;
10715 	necp_unlock_socket_attributes();
10716 	buffer = NULL;
10717 
10718 	if (buffer_to_free != NULL) {
10719 		kfree_data_addr(buffer_to_free);
10720 	}
10721 
10722 done:
10723 	// Log if it is a known tracker
10724 	if (so->so_flags1 & SOF1_KNOWN_TRACKER) {
10725 		NECP_CLIENT_TRACKER_LOG(NECP_SOCKET_PID(so),
10726 		    "NECP ATTRIBUTES SOCKET - domain <%s> owner <%s> context <%s> tracker domain <%s> account <%s> "
10727 		    "<so flags - is_tracker %X non-app-initiated %X app-approved-domain %X",
10728 		    inp->inp_necp_attributes.inp_domain ? "present" : "not set",
10729 		    inp->inp_necp_attributes.inp_domain_owner ? "present" : "not set",
10730 		    inp->inp_necp_attributes.inp_domain_context ? "present" : "not set",
10731 		    inp->inp_necp_attributes.inp_tracker_domain ? "present" : "not set",
10732 		    inp->inp_necp_attributes.inp_account ? "present" : "not set",
10733 		    so->so_flags1 & SOF1_KNOWN_TRACKER,
10734 		    so->so_flags1 & SOF1_TRACKER_NON_APP_INITIATED,
10735 		    so->so_flags1 & SOF1_APPROVED_APP_DOMAIN);
10736 	}
10737 
10738 	NECP_SOCKET_PARAMS_LOG(so, "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,
10741 	    inp->inp_necp_attributes.inp_domain_owner,
10742 	    inp->inp_necp_attributes.inp_domain_context,
10743 	    inp->inp_necp_attributes.inp_tracker_domain,
10744 	    inp->inp_necp_attributes.inp_account,
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 	if (necp_debug) {
10750 		NECPLOG(LOG_DEBUG, "Set on socket: Domain <%s> Domain owner <%s> Domain context <%s> Tracker domain <%s> Account <%s> ",
10751 		    inp->inp_necp_attributes.inp_domain,
10752 		    inp->inp_necp_attributes.inp_domain_owner,
10753 		    inp->inp_necp_attributes.inp_domain_context,
10754 		    inp->inp_necp_attributes.inp_tracker_domain,
10755 		    inp->inp_necp_attributes.inp_account);
10756 	}
10757 fail:
10758 	if (buffer != NULL) {
10759 		kfree_data(buffer, valsize + 1);
10760 	}
10761 	return error;
10762 }
10763 
10764 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)10765 necp_create_nexus_assign_message(uuid_t nexus_instance, nexus_port_t nexus_port, void *key, uint32_t key_length,
10766     struct necp_client_endpoint *local_endpoint, struct necp_client_endpoint *remote_endpoint, struct ether_addr *local_ether_addr,
10767     u_int32_t flow_adv_index, void *flow_stats, size_t *message_length)
10768 {
10769 	u_int8_t *buffer = NULL;
10770 	u_int8_t *cursor = NULL;
10771 	size_t valsize = 0;
10772 	bool has_nexus_assignment = FALSE;
10773 
10774 	if (!uuid_is_null(nexus_instance)) {
10775 		has_nexus_assignment = TRUE;
10776 		valsize += sizeof(struct necp_tlv_header) + sizeof(uuid_t);
10777 		valsize += sizeof(struct necp_tlv_header) + sizeof(nexus_port_t);
10778 	}
10779 	if (flow_adv_index != NECP_FLOWADV_IDX_INVALID) {
10780 		valsize += sizeof(struct necp_tlv_header) + sizeof(u_int32_t);
10781 	}
10782 	if (key != NULL && key_length > 0) {
10783 		valsize += sizeof(struct necp_tlv_header) + key_length;
10784 	}
10785 	if (local_endpoint != NULL) {
10786 		valsize += sizeof(struct necp_tlv_header) + sizeof(struct necp_client_endpoint);
10787 	}
10788 	if (remote_endpoint != NULL) {
10789 		valsize += sizeof(struct necp_tlv_header) + sizeof(struct necp_client_endpoint);
10790 	}
10791 	if (local_ether_addr != NULL) {
10792 		valsize += sizeof(struct necp_tlv_header) + sizeof(struct ether_addr);
10793 	}
10794 	if (flow_stats != NULL) {
10795 		valsize += sizeof(struct necp_tlv_header) + sizeof(void *);
10796 	}
10797 	if (valsize == 0) {
10798 		return NULL;
10799 	}
10800 
10801 	buffer = kalloc_data(valsize, Z_WAITOK | Z_ZERO);
10802 	if (buffer == NULL) {
10803 		return NULL;
10804 	}
10805 
10806 	cursor = buffer;
10807 	if (has_nexus_assignment) {
10808 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_INSTANCE, sizeof(uuid_t), nexus_instance, buffer, valsize);
10809 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_PORT, sizeof(nexus_port_t), &nexus_port, buffer, valsize);
10810 	}
10811 	if (flow_adv_index != NECP_FLOWADV_IDX_INVALID) {
10812 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_PORT_FLOW_INDEX, sizeof(u_int32_t), &flow_adv_index, buffer, valsize);
10813 	}
10814 	if (key != NULL && key_length > 0) {
10815 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_PARAMETER_NEXUS_KEY, key_length, key, buffer, valsize);
10816 	}
10817 	if (local_endpoint != NULL) {
10818 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_LOCAL_ENDPOINT, sizeof(struct necp_client_endpoint), local_endpoint, buffer, valsize);
10819 	}
10820 	if (remote_endpoint != NULL) {
10821 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_REMOTE_ENDPOINT, sizeof(struct necp_client_endpoint), remote_endpoint, buffer, valsize);
10822 	}
10823 	if (local_ether_addr != NULL) {
10824 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_LOCAL_ETHER_ADDR, sizeof(struct ether_addr), local_ether_addr, buffer, valsize);
10825 	}
10826 	if (flow_stats != NULL) {
10827 		cursor = necp_buffer_write_tlv(cursor, NECP_CLIENT_RESULT_NEXUS_FLOW_STATS, sizeof(void *), &flow_stats, buffer, valsize);
10828 	}
10829 
10830 	*message_length = valsize;
10831 
10832 	return buffer;
10833 }
10834 
10835 void
necp_inpcb_remove_cb(struct inpcb * inp)10836 necp_inpcb_remove_cb(struct inpcb *inp)
10837 {
10838 	if (!uuid_is_null(inp->necp_client_uuid)) {
10839 		necp_client_unregister_socket_flow(inp->necp_client_uuid, inp);
10840 		uuid_clear(inp->necp_client_uuid);
10841 	}
10842 }
10843 
10844 void
necp_inpcb_dispose(struct inpcb * inp)10845 necp_inpcb_dispose(struct inpcb *inp)
10846 {
10847 	necp_inpcb_remove_cb(inp); // Clear out socket registrations if not yet done
10848 	if (inp->inp_necp_attributes.inp_domain != NULL) {
10849 		kfree_data_addr(inp->inp_necp_attributes.inp_domain);
10850 		inp->inp_necp_attributes.inp_domain = NULL;
10851 	}
10852 	if (inp->inp_necp_attributes.inp_account != NULL) {
10853 		kfree_data_addr(inp->inp_necp_attributes.inp_account);
10854 		inp->inp_necp_attributes.inp_account = NULL;
10855 	}
10856 	if (inp->inp_necp_attributes.inp_domain_owner != NULL) {
10857 		kfree_data_addr(inp->inp_necp_attributes.inp_domain_owner);
10858 		inp->inp_necp_attributes.inp_domain_owner = NULL;
10859 	}
10860 	if (inp->inp_necp_attributes.inp_domain_context != NULL) {
10861 		kfree_data_addr(inp->inp_necp_attributes.inp_domain_context);
10862 		inp->inp_necp_attributes.inp_domain_context = NULL;
10863 	}
10864 	if (inp->inp_necp_attributes.inp_tracker_domain != NULL) {
10865 		kfree_data_addr(inp->inp_necp_attributes.inp_tracker_domain);
10866 		inp->inp_necp_attributes.inp_tracker_domain = NULL;
10867 	}
10868 	if (inp->inp_resolver_signature != NULL) {
10869 		kfree_data(inp->inp_resolver_signature, inp->inp_resolver_signature_length);
10870 	}
10871 	inp->inp_resolver_signature_length = 0;
10872 }
10873 
10874 void
necp_mppcb_dispose(struct mppcb * mpp)10875 necp_mppcb_dispose(struct mppcb *mpp)
10876 {
10877 	if (!uuid_is_null(mpp->necp_client_uuid)) {
10878 		necp_client_unregister_multipath_cb(mpp->necp_client_uuid, mpp);
10879 		uuid_clear(mpp->necp_client_uuid);
10880 	}
10881 
10882 	if (mpp->inp_necp_attributes.inp_domain != NULL) {
10883 		kfree_data_addr(mpp->inp_necp_attributes.inp_domain);
10884 		mpp->inp_necp_attributes.inp_domain = NULL;
10885 	}
10886 	if (mpp->inp_necp_attributes.inp_account != NULL) {
10887 		kfree_data_addr(mpp->inp_necp_attributes.inp_account);
10888 		mpp->inp_necp_attributes.inp_account = NULL;
10889 	}
10890 	if (mpp->inp_necp_attributes.inp_domain_owner != NULL) {
10891 		kfree_data_addr(mpp->inp_necp_attributes.inp_domain_owner);
10892 		mpp->inp_necp_attributes.inp_domain_owner = NULL;
10893 	}
10894 	if (mpp->inp_necp_attributes.inp_tracker_domain != NULL) {
10895 		kfree_data_addr(mpp->inp_necp_attributes.inp_tracker_domain);
10896 		mpp->inp_necp_attributes.inp_tracker_domain = NULL;
10897 	}
10898 }
10899 
10900 /// Module init
10901 
10902 void
necp_client_init(void)10903 necp_client_init(void)
10904 {
10905 	necp_client_update_tcall = thread_call_allocate_with_options(necp_update_all_clients_callout, NULL,
10906 	    THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
10907 	VERIFY(necp_client_update_tcall != NULL);
10908 #if SKYWALK
10909 
10910 	necp_client_collect_stats_tcall = thread_call_allocate_with_options(necp_collect_stats_client_callout, NULL,
10911 	    THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
10912 	VERIFY(necp_client_collect_stats_tcall != NULL);
10913 
10914 	necp_close_empty_arenas_tcall = thread_call_allocate_with_options(necp_close_empty_arenas_callout, NULL,
10915 	    THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
10916 	VERIFY(necp_close_empty_arenas_tcall != NULL);
10917 #endif /* SKYWALK */
10918 
10919 	LIST_INIT(&necp_fd_list);
10920 	LIST_INIT(&necp_fd_observer_list);
10921 	LIST_INIT(&necp_collect_stats_flow_list);
10922 
10923 	RB_INIT(&necp_client_global_tree);
10924 	RB_INIT(&necp_client_flow_global_tree);
10925 }
10926 
10927 #if SKYWALK
10928 pid_t
necp_client_get_proc_pid_from_arena_info(struct skmem_arena_mmap_info * arena_info)10929 necp_client_get_proc_pid_from_arena_info(struct skmem_arena_mmap_info *arena_info)
10930 {
10931 	ASSERT((arena_info->ami_arena->ar_type == SKMEM_ARENA_TYPE_NECP) || (arena_info->ami_arena->ar_type == SKMEM_ARENA_TYPE_SYSTEM));
10932 
10933 	if (arena_info->ami_arena->ar_type == SKMEM_ARENA_TYPE_NECP) {
10934 		struct necp_arena_info *nai = container_of(arena_info, struct necp_arena_info, nai_mmap);
10935 		return nai->nai_proc_pid;
10936 	} else {
10937 		struct necp_fd_data *fd_data = container_of(arena_info, struct necp_fd_data, sysctl_mmap);
10938 		return fd_data->proc_pid;
10939 	}
10940 }
10941 #endif /* !SKYWALK */
10942