2 * Copyright (c) 2015-2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 #include <kern/thread_call.h>
32 #include <kern/zalloc.h>
34 #include <libkern/OSMalloc.h>
37 #include <net/if_var.h>
38 #include <net/net_api_stats.h>
40 #include <net/network_agent.h>
41 #include <net/ntstat.h>
43 #include <netinet/in_pcb.h>
44 #include <netinet/in_var.h>
45 #include <netinet/ip.h>
46 #include <netinet/ip6.h>
47 #include <netinet/mp_pcb.h>
48 #include <netinet/tcp_cc.h>
49 #include <netinet/tcp_fsm.h>
50 #include <netinet/tcp_cache.h>
51 #include <netinet6/in6_var.h>
53 #include <sys/domain.h>
54 #include <sys/file_internal.h>
55 #include <sys/kauth.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
60 #include <sys/protosw.h>
61 #include <sys/queue.h>
62 #include <sys/socket.h>
63 #include <sys/socketvar.h>
64 #include <sys/sysproto.h>
65 #include <sys/systm.h>
66 #include <sys/types.h>
67 #include <sys/codesign.h>
68 #include <libkern/section_keywords.h>
70 #include <os/refcnt.h>
74 * NECP Client Architecture
75 * ------------------------------------------------
76 * See <net/necp.c> for a discussion on NECP database architecture.
78 * Each client of NECP provides a set of parameters for a connection or network state
79 * evaluation, on which NECP policy evaluation is run. This produces a policy result
80 * which can be accessed by the originating process, along with events for when policies
81 * results have changed.
83 * ------------------------------------------------
85 * ------------------------------------------------
86 * A process opens an NECP file descriptor using necp_open(). This is a very simple
87 * file descriptor, upon which the process may do the following operations:
88 * - necp_client_action(...), to add/remove/query clients
89 * - kqueue, to watch for readable events
90 * - close(), to close the client session and release all clients
92 * Client objects are allocated structures that hang off of the file descriptor. Each
94 * - Client ID, a UUID that references the client across the system
95 * - Parameters, a buffer of TLVs that describe the client's connection parameters,
96 * such as the remote and local endpoints, interface requirements, etc.
97 * - Result, a buffer of TLVs containing the current policy evaluation for the client.
98 * This result will be updated whenever a network change occurs that impacts the
99 * policy result for that client.
105 * ==================================
107 * +--------------+ +--------------+ +--------------+
108 * | Client ID | | Client ID | | Client ID |
109 * | ---- | | ---- | | ---- |
110 * | Parameters | | Parameters | | Parameters |
111 * | ---- | | ---- | | ---- |
112 * | Result | | Result | | Result |
113 * +--------------+ +--------------+ +--------------+
115 * ------------------------------------------------
117 * ------------------------------------------------
118 * - Add. Input parameters as a buffer of TLVs, and output a client ID. Allocates a
119 * new client structure on the file descriptor.
120 * - Remove. Input a client ID. Removes a client structure from the file descriptor.
121 * - Copy Parameters. Input a client ID, and output parameter TLVs.
122 * - Copy Result. Input a client ID, and output result TLVs. Alternatively, input empty
123 * client ID and get next unread client result.
124 * - Copy List. List all client IDs.
126 * ------------------------------------------------
127 * Client Policy Evaluation
128 * ------------------------------------------------
129 * Policies are evaluated for clients upon client creation, and upon update events,
130 * which are network/agent/policy changes coalesced by a timer.
132 * The policy evaluation goes through the following steps:
133 * 1. Parse client parameters.
134 * 2. Select a scoped interface if applicable. This involves using require/prohibit
135 * parameters, along with the local address, to select the most appropriate interface
136 * if not explicitly set by the client parameters.
137 * 3. Run NECP application-level policy evalution
138 * 4. Set policy result into client result buffer.
140 * ------------------------------------------------
142 * ------------------------------------------------
143 * If necp_open() is called with the NECP_OPEN_FLAG_OBSERVER flag, and the process
144 * passes the necessary privilege check, the fd is allowed to use necp_client_action()
145 * to copy client state attached to the file descriptors of other processes, and to
146 * list all client IDs on the system.
149 extern u_int32_t necp_debug
;
151 static int necpop_select(struct fileproc
*, int, void *, vfs_context_t
);
152 static int necpop_close(struct fileglob
*, vfs_context_t
);
153 static int necpop_kqfilter(struct fileproc
*, struct knote
*, struct kevent_qos_s
*);
156 static int necp_timeout_microseconds
= 1000 * 100; // 100ms
157 static int necp_timeout_leeway_microseconds
= 1000 * 500; // 500ms
159 static int necp_client_fd_count
= 0;
160 static int necp_observer_fd_count
= 0;
161 static int necp_client_count
= 0;
162 static int necp_socket_flow_count
= 0;
163 static int necp_if_flow_count
= 0;
164 static int necp_observer_message_limit
= 256;
166 os_refgrp_decl(static, necp_client_refgrp
, "NECPClientRefGroup", NULL
);
168 SYSCTL_INT(_net_necp
, NECPCTL_CLIENT_FD_COUNT
, client_fd_count
, CTLFLAG_LOCKED
| CTLFLAG_RD
, &necp_client_fd_count
, 0, "");
169 SYSCTL_INT(_net_necp
, NECPCTL_OBSERVER_FD_COUNT
, observer_fd_count
, CTLFLAG_LOCKED
| CTLFLAG_RD
, &necp_observer_fd_count
, 0, "");
170 SYSCTL_INT(_net_necp
, NECPCTL_CLIENT_COUNT
, client_count
, CTLFLAG_LOCKED
| CTLFLAG_RD
, &necp_client_count
, 0, "");
171 SYSCTL_INT(_net_necp
, NECPCTL_SOCKET_FLOW_COUNT
, socket_flow_count
, CTLFLAG_LOCKED
| CTLFLAG_RD
, &necp_socket_flow_count
, 0, "");
172 SYSCTL_INT(_net_necp
, NECPCTL_IF_FLOW_COUNT
, if_flow_count
, CTLFLAG_LOCKED
| CTLFLAG_RD
, &necp_if_flow_count
, 0, "");
173 SYSCTL_INT(_net_necp
, NECPCTL_OBSERVER_MESSAGE_LIMIT
, observer_message_limit
, CTLFLAG_LOCKED
| CTLFLAG_RW
, &necp_observer_message_limit
, 256, "");
175 #define NECP_MAX_CLIENT_LIST_SIZE 1024 * 1024 // 1MB
176 #define NECP_MAX_AGENT_ACTION_SIZE 256
178 extern int tvtohz(struct timeval
*);
179 extern unsigned int get_maxmtu(struct rtentry
*);
182 #define NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR 0x00001
183 #define NECP_PARSED_PARAMETERS_FIELD_REMOTE_ADDR 0x00002
184 #define NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF 0x00004
185 #define NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IF 0x00008
186 #define NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE 0x00010
187 #define NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IFTYPE 0x00020
188 #define NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT 0x00040
189 #define NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT 0x00080
190 #define NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT 0x00100
191 #define NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT 0x00200
192 #define NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE 0x00400
193 #define NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT_TYPE 0x00800
194 #define NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE 0x01000
195 #define NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE 0x02000
196 #define NECP_PARSED_PARAMETERS_FIELD_FLAGS 0x04000
197 #define NECP_PARSED_PARAMETERS_FIELD_IP_PROTOCOL 0x08000
198 #define NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID 0x10000
199 #define NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_UUID 0x20000
200 #define NECP_PARSED_PARAMETERS_FIELD_TRAFFIC_CLASS 0x40000
201 #define NECP_PARSED_PARAMETERS_FIELD_LOCAL_PORT 0x80000
202 #define NECP_PARSED_PARAMETERS_FIELD_DELEGATED_UPID 0x100000
203 #define NECP_PARSED_PARAMETERS_FIELD_ETHERTYPE 0x200000
204 #define NECP_PARSED_PARAMETERS_FIELD_TRANSPORT_PROTOCOL 0x400000
205 #define NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR_PREFERENCE 0x800000
208 #define NECP_MAX_INTERFACE_PARAMETERS 16
209 #define NECP_MAX_AGENT_PARAMETERS 4
210 struct necp_client_parsed_parameters
{
211 u_int32_t valid_fields
;
213 u_int64_t delegated_upid
;
214 union necp_sockaddr_union local_addr
;
215 union necp_sockaddr_union remote_addr
;
216 u_int32_t required_interface_index
;
217 char prohibited_interfaces
[NECP_MAX_INTERFACE_PARAMETERS
][IFXNAMSIZ
];
218 u_int8_t required_interface_type
;
219 u_int8_t local_address_preference
;
220 u_int8_t prohibited_interface_types
[NECP_MAX_INTERFACE_PARAMETERS
];
221 struct necp_client_parameter_netagent_type required_netagent_types
[NECP_MAX_AGENT_PARAMETERS
];
222 struct necp_client_parameter_netagent_type prohibited_netagent_types
[NECP_MAX_AGENT_PARAMETERS
];
223 struct necp_client_parameter_netagent_type preferred_netagent_types
[NECP_MAX_AGENT_PARAMETERS
];
224 struct necp_client_parameter_netagent_type avoided_netagent_types
[NECP_MAX_AGENT_PARAMETERS
];
225 uuid_t required_netagents
[NECP_MAX_AGENT_PARAMETERS
];
226 uuid_t prohibited_netagents
[NECP_MAX_AGENT_PARAMETERS
];
227 uuid_t preferred_netagents
[NECP_MAX_AGENT_PARAMETERS
];
228 uuid_t avoided_netagents
[NECP_MAX_AGENT_PARAMETERS
];
229 u_int8_t ip_protocol
;
230 u_int8_t transport_protocol
;
233 uuid_t effective_uuid
;
234 u_int32_t traffic_class
;
238 necp_find_matching_interface_index(struct necp_client_parsed_parameters
*parsed_parameters
,
239 u_int
*return_ifindex
, bool *validate_agents
);
242 necp_ifnet_matches_local_address(struct ifnet
*ifp
, struct sockaddr
*sa
);
245 necp_ifnet_matches_parameters(struct ifnet
*ifp
,
246 struct necp_client_parsed_parameters
*parsed_parameters
,
247 u_int32_t override_flags
,
248 u_int32_t
*preferred_count
,
249 bool secondary_interface
,
250 bool require_scoped_field
);
252 static const struct fileops necp_fd_ops
= {
253 .fo_type
= DTYPE_NETPOLICY
,
254 .fo_read
= fo_no_read
,
255 .fo_write
= fo_no_write
,
256 .fo_ioctl
= fo_no_ioctl
,
257 .fo_select
= necpop_select
,
258 .fo_close
= necpop_close
,
259 .fo_drain
= fo_no_drain
,
260 .fo_kqfilter
= necpop_kqfilter
,
263 struct necp_client_assertion
{
264 LIST_ENTRY(necp_client_assertion
) assertion_chain
;
265 uuid_t asserted_netagent
;
268 struct necp_client_flow_header
{
269 struct necp_tlv_header outer_header
;
270 struct necp_tlv_header flow_id_tlv_header
;
272 struct necp_tlv_header flags_tlv_header
;
273 u_int32_t flags_value
;
274 struct necp_tlv_header interface_tlv_header
;
275 struct necp_client_result_interface interface_value
;
276 } __attribute__((__packed__
));
278 struct necp_client_flow_protoctl_event_header
{
279 struct necp_tlv_header protoctl_tlv_header
;
280 struct necp_client_flow_protoctl_event protoctl_event
;
281 } __attribute__((__packed__
));
283 struct necp_client_nexus_flow_header
{
284 struct necp_client_flow_header flow_header
;
285 struct necp_tlv_header agent_tlv_header
;
286 struct necp_client_result_netagent agent_value
;
287 struct necp_tlv_header tfo_cookie_tlv_header
;
288 u_int8_t tfo_cookie_value
[NECP_TFO_COOKIE_LEN_MAX
];
289 } __attribute__((__packed__
));
292 struct necp_client_flow
{
293 LIST_ENTRY(necp_client_flow
) flow_chain
;
294 unsigned invalid
: 1;
295 unsigned nexus
: 1; // If true, flow is a nexus; if false, flow is attached to socket
298 unsigned assigned
: 1;
299 unsigned has_protoctl_event
: 1;
300 unsigned check_tcp_heuristics
: 1;
301 unsigned _reserved
: 1;
306 necp_client_flow_cb cb
;
309 uint32_t interface_index
;
310 uint16_t interface_flags
;
311 uint32_t necp_flow_flags
;
312 struct necp_client_flow_protoctl_event protoctl_event
;
313 union necp_sockaddr_union local_addr
;
314 union necp_sockaddr_union remote_addr
;
316 size_t assigned_results_length
;
317 u_int8_t
*assigned_results
;
320 struct necp_client_flow_registration
{
321 RB_ENTRY(necp_client_flow_registration
) fd_link
;
322 RB_ENTRY(necp_client_flow_registration
) global_link
;
323 RB_ENTRY(necp_client_flow_registration
) client_link
;
324 LIST_ENTRY(necp_client_flow_registration
) collect_stats_chain
;
325 uuid_t registration_id
;
327 unsigned flow_result_read
: 1;
328 unsigned defunct
: 1;
329 void *interface_handle
;
330 necp_client_flow_cb interface_cb
;
331 struct necp_client
*client
;
332 LIST_HEAD(_necp_registration_flow_list
, necp_client_flow
) flow_list
;
333 u_int64_t last_interface_details
__attribute__((aligned(sizeof(u_int64_t
))));
336 static int necp_client_flow_id_cmp(struct necp_client_flow_registration
*flow0
, struct necp_client_flow_registration
*flow1
);
338 RB_HEAD(_necp_client_flow_tree
, necp_client_flow_registration
);
339 RB_PROTOTYPE_PREV(_necp_client_flow_tree
, necp_client_flow_registration
, client_link
, necp_client_flow_id_cmp
);
340 RB_GENERATE_PREV(_necp_client_flow_tree
, necp_client_flow_registration
, client_link
, necp_client_flow_id_cmp
);
342 #define NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT 4
343 #define NECP_CLIENT_MAX_INTERFACE_OPTIONS 16
345 #define NECP_CLIENT_INTERFACE_OPTION_EXTRA_COUNT (NECP_CLIENT_MAX_INTERFACE_OPTIONS - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT)
348 RB_ENTRY(necp_client
) link
;
349 RB_ENTRY(necp_client
) global_link
;
351 decl_lck_mtx_data(, lock
);
352 decl_lck_mtx_data(, route_lock
);
353 os_refcnt_t reference_count
;
356 unsigned result_read
: 1;
357 unsigned allow_multiple_flows
: 1;
358 unsigned legacy_client_is_flow
: 1;
360 unsigned platform_binary
: 1;
362 size_t result_length
;
363 u_int8_t result
[NECP_MAX_CLIENT_RESULT_SIZE
];
365 necp_policy_id policy_id
;
367 u_int8_t ip_protocol
;
370 u_int64_t delegated_upid
;
372 struct _necp_client_flow_tree flow_registrations
;
373 LIST_HEAD(_necp_client_assertion_list
, necp_client_assertion
) assertion_list
;
375 struct rtentry
*current_route
;
377 struct necp_client_interface_option interface_options
[NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
];
378 struct necp_client_interface_option
*extra_interface_options
;
379 u_int8_t interface_option_count
; // Number in interface_options + extra_interface_options
381 struct necp_client_result_netagent failed_trigger_agent
;
386 size_t parameters_length
;
387 u_int8_t parameters
[0];
390 #define NECP_CLIENT_LOCK(_c) lck_mtx_lock(&_c->lock)
391 #define NECP_CLIENT_UNLOCK(_c) lck_mtx_unlock(&_c->lock)
392 #define NECP_CLIENT_ASSERT_LOCKED(_c) LCK_MTX_ASSERT(&_c->lock, LCK_MTX_ASSERT_OWNED)
393 #define NECP_CLIENT_ASSERT_UNLOCKED(_c) LCK_MTX_ASSERT(&_c->lock, LCK_MTX_ASSERT_NOTOWNED)
395 #define NECP_CLIENT_ROUTE_LOCK(_c) lck_mtx_lock(&_c->route_lock)
396 #define NECP_CLIENT_ROUTE_UNLOCK(_c) lck_mtx_unlock(&_c->route_lock)
398 static void necp_client_retain_locked(struct necp_client
*client
);
400 static bool necp_client_release_locked(struct necp_client
*client
);
401 static bool necp_client_release(struct necp_client
*client
);
404 necp_client_add_assertion(struct necp_client
*client
, uuid_t netagent_uuid
);
407 necp_client_remove_assertion(struct necp_client
*client
, uuid_t netagent_uuid
);
409 LIST_HEAD(_necp_flow_registration_list
, necp_client_flow_registration
);
410 static struct _necp_flow_registration_list necp_collect_stats_flow_list
;
412 struct necp_flow_defunct
{
413 LIST_ENTRY(necp_flow_defunct
) chain
;
420 struct necp_client_agent_parameters close_parameters
;
421 bool has_close_parameters
;
424 LIST_HEAD(_necp_flow_defunct_list
, necp_flow_defunct
);
426 static int necp_client_id_cmp(struct necp_client
*client0
, struct necp_client
*client1
);
428 RB_HEAD(_necp_client_tree
, necp_client
);
429 RB_PROTOTYPE_PREV(_necp_client_tree
, necp_client
, link
, necp_client_id_cmp
);
430 RB_GENERATE_PREV(_necp_client_tree
, necp_client
, link
, necp_client_id_cmp
);
432 RB_HEAD(_necp_client_global_tree
, necp_client
);
433 RB_PROTOTYPE_PREV(_necp_client_global_tree
, necp_client
, global_link
, necp_client_id_cmp
);
434 RB_GENERATE_PREV(_necp_client_global_tree
, necp_client
, global_link
, necp_client_id_cmp
);
436 RB_HEAD(_necp_fd_flow_tree
, necp_client_flow_registration
);
437 RB_PROTOTYPE_PREV(_necp_fd_flow_tree
, necp_client_flow_registration
, fd_link
, necp_client_flow_id_cmp
);
438 RB_GENERATE_PREV(_necp_fd_flow_tree
, necp_client_flow_registration
, fd_link
, necp_client_flow_id_cmp
);
440 RB_HEAD(_necp_client_flow_global_tree
, necp_client_flow_registration
);
441 RB_PROTOTYPE_PREV(_necp_client_flow_global_tree
, necp_client_flow_registration
, global_link
, necp_client_flow_id_cmp
);
442 RB_GENERATE_PREV(_necp_client_flow_global_tree
, necp_client_flow_registration
, global_link
, necp_client_flow_id_cmp
);
444 static struct _necp_client_global_tree necp_client_global_tree
;
445 static struct _necp_client_flow_global_tree necp_client_flow_global_tree
;
447 struct necp_client_update
{
448 TAILQ_ENTRY(necp_client_update
) chain
;
452 size_t update_length
;
453 struct necp_client_observer_update update
;
457 #define NAIF_ATTACHED 0x1 // arena is attached to list
458 #define NAIF_REDIRECT 0x2 // arena mmap has been redirected
459 #define NAIF_DEFUNCT 0x4 // arena is now defunct
461 struct necp_fd_data
{
462 u_int8_t necp_fd_type
;
463 LIST_ENTRY(necp_fd_data
) chain
;
464 struct _necp_client_tree clients
;
465 struct _necp_fd_flow_tree flows
;
466 TAILQ_HEAD(_necp_client_update_list
, necp_client_update
) update_list
;
470 unsigned background
: 1;
473 decl_lck_mtx_data(, fd_lock
);
477 #define NECP_FD_LOCK(_f) lck_mtx_lock(&_f->fd_lock)
478 #define NECP_FD_UNLOCK(_f) lck_mtx_unlock(&_f->fd_lock)
479 #define NECP_FD_ASSERT_LOCKED(_f) LCK_MTX_ASSERT(&_f->fd_lock, LCK_MTX_ASSERT_OWNED)
480 #define NECP_FD_ASSERT_UNLOCKED(_f) LCK_MTX_ASSERT(&_f->fd_lock, LCK_MTX_ASSERT_NOTOWNED)
482 static LIST_HEAD(_necp_fd_list
, necp_fd_data
) necp_fd_list
;
483 static LIST_HEAD(_necp_fd_observer_list
, necp_fd_data
) necp_fd_observer_list
;
485 #define NECP_CLIENT_FD_ZONE_MAX 128
486 #define NECP_CLIENT_FD_ZONE_NAME "necp.clientfd"
488 static unsigned int necp_client_fd_size
; /* size of zone element */
489 static struct zone
*necp_client_fd_zone
; /* zone for necp_fd_data */
491 #define NECP_FLOW_ZONE_NAME "necp.flow"
492 #define NECP_FLOW_REGISTRATION_ZONE_NAME "necp.flowregistration"
494 static unsigned int necp_flow_size
; /* size of necp_client_flow */
495 static struct mcache
*necp_flow_cache
; /* cache for necp_client_flow */
497 static unsigned int necp_flow_registration_size
; /* size of necp_client_flow_registration */
498 static struct mcache
*necp_flow_registration_cache
; /* cache for necp_client_flow_registration */
500 #define NECP_ARENA_INFO_ZONE_MAX 128
501 #define NECP_ARENA_INFO_ZONE_NAME "necp.arenainfo"
504 static lck_grp_attr_t
*necp_fd_grp_attr
= NULL
;
505 static lck_attr_t
*necp_fd_mtx_attr
= NULL
;
506 static lck_grp_t
*necp_fd_mtx_grp
= NULL
;
508 decl_lck_rw_data(static, necp_fd_lock
);
509 decl_lck_rw_data(static, necp_observer_lock
);
510 decl_lck_rw_data(static, necp_client_tree_lock
);
511 decl_lck_rw_data(static, necp_flow_tree_lock
);
512 decl_lck_rw_data(static, necp_collect_stats_list_lock
);
514 #define NECP_STATS_LIST_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_collect_stats_list_lock)
515 #define NECP_STATS_LIST_LOCK_SHARED() lck_rw_lock_shared(&necp_collect_stats_list_lock)
516 #define NECP_STATS_LIST_UNLOCK() lck_rw_done(&necp_collect_stats_list_lock)
518 #define NECP_CLIENT_TREE_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_client_tree_lock)
519 #define NECP_CLIENT_TREE_LOCK_SHARED() lck_rw_lock_shared(&necp_client_tree_lock)
520 #define NECP_CLIENT_TREE_UNLOCK() lck_rw_done(&necp_client_tree_lock)
521 #define NECP_CLIENT_TREE_ASSERT_LOCKED() LCK_RW_ASSERT(&necp_client_tree_lock, LCK_RW_ASSERT_HELD)
523 #define NECP_FLOW_TREE_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_flow_tree_lock)
524 #define NECP_FLOW_TREE_LOCK_SHARED() lck_rw_lock_shared(&necp_flow_tree_lock)
525 #define NECP_FLOW_TREE_UNLOCK() lck_rw_done(&necp_flow_tree_lock)
526 #define NECP_FLOW_TREE_ASSERT_LOCKED() LCK_RW_ASSERT(&necp_flow_tree_lock, LCK_RW_ASSERT_HELD)
528 #define NECP_FD_LIST_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_fd_lock)
529 #define NECP_FD_LIST_LOCK_SHARED() lck_rw_lock_shared(&necp_fd_lock)
530 #define NECP_FD_LIST_UNLOCK() lck_rw_done(&necp_fd_lock)
532 #define NECP_OBSERVER_LIST_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_observer_lock)
533 #define NECP_OBSERVER_LIST_LOCK_SHARED() lck_rw_lock_shared(&necp_observer_lock)
534 #define NECP_OBSERVER_LIST_UNLOCK() lck_rw_done(&necp_observer_lock)
538 // Take NECP_FD_LIST_LOCK when accessing or modifying the necp_fd_list
539 // Take NECP_CLIENT_TREE_LOCK when accessing or modifying the necp_client_global_tree
540 // Take NECP_FLOW_TREE_LOCK when accessing or modifying the necp_client_flow_global_tree
541 // Take NECP_STATS_LIST_LOCK when accessing or modifying the necp_collect_stats_flow_list
542 // Take NECP_FD_LOCK when accessing or modifying an necp_fd_data entry
543 // Take NECP_CLIENT_LOCK when accessing or modifying a single necp_client
544 // Take NECP_CLIENT_ROUTE_LOCK when accessing or modifying a client's route
546 // Precedence, where 1 is the first lock that must be taken
547 // 1. NECP_FD_LIST_LOCK
548 // 2. NECP_FD_LOCK (any)
549 // 3. NECP_CLIENT_TREE_LOCK
550 // 4. NECP_CLIENT_LOCK (any)
551 // 5. NECP_FLOW_TREE_LOCK
552 // 6. NECP_STATS_LIST_LOCK
553 // 7. NECP_CLIENT_ROUTE_LOCK (any)
555 static thread_call_t necp_client_update_tcall
;
558 /// NECP file descriptor functions
561 necp_fd_notify(struct necp_fd_data
*fd_data
, bool locked
)
563 struct selinfo
*si
= &fd_data
->si
;
566 NECP_FD_LOCK(fd_data
);
571 // use a non-zero hint to tell the notification from the
572 // call done in kqueue_scan() which uses 0
573 KNOTE(&si
->si_note
, 1); // notification
576 NECP_FD_UNLOCK(fd_data
);
581 necp_client_has_unread_flows(struct necp_client
*client
)
583 NECP_CLIENT_ASSERT_LOCKED(client
);
584 struct necp_client_flow_registration
*flow_registration
= NULL
;
585 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
586 if (!flow_registration
->flow_result_read
) {
594 necp_fd_poll(struct necp_fd_data
*fd_data
, int events
, void *wql
, struct proc
*p
, int is_kevent
)
596 #pragma unused(wql, p, is_kevent)
599 u_int want_rx
= events
& (POLLIN
| POLLRDNORM
);
601 if (fd_data
->flags
& NECP_OPEN_FLAG_PUSH_OBSERVER
) {
602 // Push-mode observers are readable when they have a new update
603 if (!TAILQ_EMPTY(&fd_data
->update_list
)) {
607 // Standard fds are readable when some client is unread
608 struct necp_client
*client
= NULL
;
609 bool has_unread_clients
= FALSE
;
610 RB_FOREACH(client
, _necp_client_tree
, &fd_data
->clients
) {
611 NECP_CLIENT_LOCK(client
);
612 if (!client
->result_read
|| necp_client_has_unread_flows(client
)) {
613 has_unread_clients
= TRUE
;
615 NECP_CLIENT_UNLOCK(client
);
616 if (has_unread_clients
) {
621 if (has_unread_clients
) {
631 necp_generate_client_id(uuid_t client_id
, bool is_flow
)
633 uuid_generate_random(client_id
);
636 client_id
[9] |= 0x01;
638 client_id
[9] &= ~0x01;
643 necp_client_id_is_flow(uuid_t client_id
)
645 return client_id
[9] & 0x01;
648 static struct necp_client
*
649 necp_find_client_and_lock(uuid_t client_id
)
651 NECP_CLIENT_TREE_ASSERT_LOCKED();
653 struct necp_client
*client
= NULL
;
655 if (necp_client_id_is_flow(client_id
)) {
656 NECP_FLOW_TREE_LOCK_SHARED();
657 struct necp_client_flow_registration find
;
658 uuid_copy(find
.registration_id
, client_id
);
659 struct necp_client_flow_registration
*flow
= RB_FIND(_necp_client_flow_global_tree
, &necp_client_flow_global_tree
, &find
);
661 client
= flow
->client
;
663 NECP_FLOW_TREE_UNLOCK();
665 struct necp_client find
;
666 uuid_copy(find
.client_id
, client_id
);
667 client
= RB_FIND(_necp_client_global_tree
, &necp_client_global_tree
, &find
);
670 if (client
!= NULL
) {
671 NECP_CLIENT_LOCK(client
);
677 static struct necp_client_flow_registration
*
678 necp_client_find_flow(struct necp_client
*client
, uuid_t flow_id
)
680 NECP_CLIENT_ASSERT_LOCKED(client
);
681 struct necp_client_flow_registration
*flow
= NULL
;
683 if (necp_client_id_is_flow(flow_id
)) {
684 struct necp_client_flow_registration find
;
685 uuid_copy(find
.registration_id
, flow_id
);
686 flow
= RB_FIND(_necp_client_flow_tree
, &client
->flow_registrations
, &find
);
688 flow
= RB_ROOT(&client
->flow_registrations
);
694 static struct necp_client
*
695 necp_client_fd_find_client_unlocked(struct necp_fd_data
*client_fd
, uuid_t client_id
)
697 NECP_FD_ASSERT_LOCKED(client_fd
);
698 struct necp_client
*client
= NULL
;
700 if (necp_client_id_is_flow(client_id
)) {
701 struct necp_client_flow_registration find
;
702 uuid_copy(find
.registration_id
, client_id
);
703 struct necp_client_flow_registration
*flow
= RB_FIND(_necp_fd_flow_tree
, &client_fd
->flows
, &find
);
705 client
= flow
->client
;
708 struct necp_client find
;
709 uuid_copy(find
.client_id
, client_id
);
710 client
= RB_FIND(_necp_client_tree
, &client_fd
->clients
, &find
);
716 static struct necp_client
*
717 necp_client_fd_find_client_and_lock(struct necp_fd_data
*client_fd
, uuid_t client_id
)
719 struct necp_client
*client
= necp_client_fd_find_client_unlocked(client_fd
, client_id
);
720 if (client
!= NULL
) {
721 NECP_CLIENT_LOCK(client
);
728 necp_client_id_cmp(struct necp_client
*client0
, struct necp_client
*client1
)
730 return uuid_compare(client0
->client_id
, client1
->client_id
);
734 necp_client_flow_id_cmp(struct necp_client_flow_registration
*flow0
, struct necp_client_flow_registration
*flow1
)
736 return uuid_compare(flow0
->registration_id
, flow1
->registration_id
);
740 necpop_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
742 #pragma unused(fp, which, wql, ctx)
744 struct necp_fd_data
*fd_data
= NULL
;
749 fd_data
= (struct necp_fd_data
*)fp
->f_fglob
->fg_data
;
750 if (fd_data
== NULL
) {
754 procp
= vfs_context_proc(ctx
);
767 NECP_FD_LOCK(fd_data
);
768 revents
= necp_fd_poll(fd_data
, events
, wql
, procp
, 0);
769 NECP_FD_UNLOCK(fd_data
);
771 return (events
& revents
) ? 1 : 0;
775 necp_fd_knrdetach(struct knote
*kn
)
777 struct necp_fd_data
*fd_data
= (struct necp_fd_data
*)kn
->kn_hook
;
778 struct selinfo
*si
= &fd_data
->si
;
780 NECP_FD_LOCK(fd_data
);
781 KNOTE_DETACH(&si
->si_note
, kn
);
782 NECP_FD_UNLOCK(fd_data
);
786 necp_fd_knread(struct knote
*kn
, long hint
)
788 #pragma unused(kn, hint)
789 return 1; /* assume we are ready */
793 necp_fd_knrprocess(struct knote
*kn
, struct kevent_qos_s
*kev
)
795 struct necp_fd_data
*fd_data
;
799 fd_data
= (struct necp_fd_data
*)kn
->kn_hook
;
801 NECP_FD_LOCK(fd_data
);
802 revents
= necp_fd_poll(fd_data
, POLLIN
, NULL
, current_proc(), 1);
803 res
= ((revents
& POLLIN
) != 0);
805 knote_fill_kevent(kn
, kev
, 0);
807 NECP_FD_UNLOCK(fd_data
);
812 necp_fd_knrtouch(struct knote
*kn
, struct kevent_qos_s
*kev
)
815 struct necp_fd_data
*fd_data
;
818 fd_data
= (struct necp_fd_data
*)kn
->kn_hook
;
820 NECP_FD_LOCK(fd_data
);
821 revents
= necp_fd_poll(fd_data
, POLLIN
, NULL
, current_proc(), 1);
822 NECP_FD_UNLOCK(fd_data
);
824 return (revents
& POLLIN
) != 0;
827 SECURITY_READ_ONLY_EARLY(struct filterops
) necp_fd_rfiltops
= {
829 .f_detach
= necp_fd_knrdetach
,
830 .f_event
= necp_fd_knread
,
831 .f_touch
= necp_fd_knrtouch
,
832 .f_process
= necp_fd_knrprocess
,
836 necpop_kqfilter(struct fileproc
*fp
, struct knote
*kn
,
837 __unused
struct kevent_qos_s
*kev
)
839 struct necp_fd_data
*fd_data
= NULL
;
842 if (kn
->kn_filter
!= EVFILT_READ
) {
843 NECPLOG(LOG_ERR
, "bad filter request %d", kn
->kn_filter
);
844 knote_set_error(kn
, EINVAL
);
848 fd_data
= (struct necp_fd_data
*)fp
->f_fglob
->fg_data
;
849 if (fd_data
== NULL
) {
850 NECPLOG0(LOG_ERR
, "No channel for kqfilter");
851 knote_set_error(kn
, ENOENT
);
855 NECP_FD_LOCK(fd_data
);
856 kn
->kn_filtid
= EVFILTID_NECP_FD
;
857 kn
->kn_hook
= fd_data
;
858 KNOTE_ATTACH(&fd_data
->si
.si_note
, kn
);
860 revents
= necp_fd_poll(fd_data
, POLLIN
, NULL
, current_proc(), 1);
862 NECP_FD_UNLOCK(fd_data
);
864 return (revents
& POLLIN
) != 0;
867 #define INTERFACE_FLAGS_SHIFT 32
868 #define INTERFACE_FLAGS_MASK 0xffff
869 #define INTERFACE_INDEX_SHIFT 0
870 #define INTERFACE_INDEX_MASK 0xffffffff
873 combine_interface_details(uint32_t interface_index
, uint16_t interface_flags
)
875 return ((uint64_t)interface_flags
& INTERFACE_FLAGS_MASK
) << INTERFACE_FLAGS_SHIFT
|
876 ((uint64_t)interface_index
& INTERFACE_INDEX_MASK
) << INTERFACE_INDEX_SHIFT
;
881 necp_defunct_flow_registration(struct necp_client
*client
,
882 struct necp_client_flow_registration
*flow_registration
,
883 struct _necp_flow_defunct_list
*defunct_list
)
885 NECP_CLIENT_ASSERT_LOCKED(client
);
887 if (!flow_registration
->defunct
) {
888 bool needs_defunct
= false;
889 struct necp_client_flow
*search_flow
= NULL
;
890 LIST_FOREACH(search_flow
, &flow_registration
->flow_list
, flow_chain
) {
891 if (search_flow
->nexus
&&
892 !uuid_is_null(search_flow
->u
.nexus_agent
)) {
893 // Save defunct values for the nexus
894 if (defunct_list
!= NULL
) {
895 // Sleeping alloc won't fail; copy only what's necessary
896 struct necp_flow_defunct
*flow_defunct
= _MALLOC(sizeof(struct necp_flow_defunct
),
897 M_NECP
, M_WAITOK
| M_ZERO
);
898 uuid_copy(flow_defunct
->nexus_agent
, search_flow
->u
.nexus_agent
);
899 uuid_copy(flow_defunct
->flow_id
, ((flow_registration
->flags
& NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID
) ?
901 flow_registration
->registration_id
));
902 flow_defunct
->proc_pid
= client
->proc_pid
;
903 flow_defunct
->agent_handle
= client
->agent_handle
;
904 flow_defunct
->flags
= flow_registration
->flags
;
905 // Add to the list provided by caller
906 LIST_INSERT_HEAD(defunct_list
, flow_defunct
, chain
);
909 needs_defunct
= true;
915 // Only set defunct if there was some assigned flow
916 flow_registration
->defunct
= true;
922 necp_defunct_client_for_policy(struct necp_client
*client
,
923 struct _necp_flow_defunct_list
*defunct_list
)
925 NECP_CLIENT_ASSERT_LOCKED(client
);
927 struct necp_client_flow_registration
*flow_registration
= NULL
;
928 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
929 necp_defunct_flow_registration(client
, flow_registration
, defunct_list
);
934 necp_client_free(struct necp_client
*client
)
936 NECP_CLIENT_ASSERT_LOCKED(client
);
938 NECP_CLIENT_UNLOCK(client
);
940 FREE(client
->extra_interface_options
, M_NECP
);
941 client
->extra_interface_options
= NULL
;
943 lck_mtx_destroy(&client
->route_lock
, necp_fd_mtx_grp
);
944 lck_mtx_destroy(&client
->lock
, necp_fd_mtx_grp
);
946 FREE(client
, M_NECP
);
950 necp_client_retain_locked(struct necp_client
*client
)
952 NECP_CLIENT_ASSERT_LOCKED(client
);
954 os_ref_retain_locked(&client
->reference_count
);
959 necp_client_release_locked(struct necp_client
*client
)
961 NECP_CLIENT_ASSERT_LOCKED(client
);
963 os_ref_count_t count
= os_ref_release_locked(&client
->reference_count
);
965 necp_client_free(client
);
972 necp_client_release(struct necp_client
*client
)
976 NECP_CLIENT_LOCK(client
);
977 if (!(last_ref
= necp_client_release_locked(client
))) {
978 NECP_CLIENT_UNLOCK(client
);
985 necp_client_update_observer_add_internal(struct necp_fd_data
*observer_fd
, struct necp_client
*client
)
987 NECP_FD_LOCK(observer_fd
);
989 if (observer_fd
->update_count
>= necp_observer_message_limit
) {
990 NECP_FD_UNLOCK(observer_fd
);
994 struct necp_client_update
*client_update
= _MALLOC(sizeof(struct necp_client_update
) + client
->parameters_length
,
995 M_NECP
, M_WAITOK
| M_ZERO
);
996 if (client_update
!= NULL
) {
997 client_update
->update_length
= sizeof(struct necp_client_observer_update
) + client
->parameters_length
;
998 uuid_copy(client_update
->client_id
, client
->client_id
);
999 client_update
->update
.update_type
= NECP_CLIENT_UPDATE_TYPE_PARAMETERS
;
1000 memcpy(client_update
->update
.tlv_buffer
, client
->parameters
, client
->parameters_length
);
1001 TAILQ_INSERT_TAIL(&observer_fd
->update_list
, client_update
, chain
);
1002 observer_fd
->update_count
++;
1004 necp_fd_notify(observer_fd
, true);
1007 NECP_FD_UNLOCK(observer_fd
);
1011 necp_client_update_observer_update_internal(struct necp_fd_data
*observer_fd
, struct necp_client
*client
)
1013 NECP_FD_LOCK(observer_fd
);
1015 if (observer_fd
->update_count
>= necp_observer_message_limit
) {
1016 NECP_FD_UNLOCK(observer_fd
);
1020 struct necp_client_update
*client_update
= _MALLOC(sizeof(struct necp_client_update
) + client
->result_length
,
1021 M_NECP
, M_WAITOK
| M_ZERO
);
1022 if (client_update
!= NULL
) {
1023 client_update
->update_length
= sizeof(struct necp_client_observer_update
) + client
->result_length
;
1024 uuid_copy(client_update
->client_id
, client
->client_id
);
1025 client_update
->update
.update_type
= NECP_CLIENT_UPDATE_TYPE_RESULT
;
1026 memcpy(client_update
->update
.tlv_buffer
, client
->result
, client
->result_length
);
1027 TAILQ_INSERT_TAIL(&observer_fd
->update_list
, client_update
, chain
);
1028 observer_fd
->update_count
++;
1030 necp_fd_notify(observer_fd
, true);
1033 NECP_FD_UNLOCK(observer_fd
);
1037 necp_client_update_observer_remove_internal(struct necp_fd_data
*observer_fd
, struct necp_client
*client
)
1039 NECP_FD_LOCK(observer_fd
);
1041 if (observer_fd
->update_count
>= necp_observer_message_limit
) {
1042 NECP_FD_UNLOCK(observer_fd
);
1046 struct necp_client_update
*client_update
= _MALLOC(sizeof(struct necp_client_update
),
1047 M_NECP
, M_WAITOK
| M_ZERO
);
1048 if (client_update
!= NULL
) {
1049 client_update
->update_length
= sizeof(struct necp_client_observer_update
);
1050 uuid_copy(client_update
->client_id
, client
->client_id
);
1051 client_update
->update
.update_type
= NECP_CLIENT_UPDATE_TYPE_REMOVE
;
1052 TAILQ_INSERT_TAIL(&observer_fd
->update_list
, client_update
, chain
);
1053 observer_fd
->update_count
++;
1055 necp_fd_notify(observer_fd
, true);
1058 NECP_FD_UNLOCK(observer_fd
);
1062 necp_client_update_observer_add(struct necp_client
*client
)
1064 NECP_OBSERVER_LIST_LOCK_SHARED();
1066 if (LIST_EMPTY(&necp_fd_observer_list
)) {
1067 // No observers, bail
1068 NECP_OBSERVER_LIST_UNLOCK();
1072 struct necp_fd_data
*observer_fd
= NULL
;
1073 LIST_FOREACH(observer_fd
, &necp_fd_observer_list
, chain
) {
1074 necp_client_update_observer_add_internal(observer_fd
, client
);
1077 NECP_OBSERVER_LIST_UNLOCK();
1081 necp_client_update_observer_update(struct necp_client
*client
)
1083 NECP_OBSERVER_LIST_LOCK_SHARED();
1085 if (LIST_EMPTY(&necp_fd_observer_list
)) {
1086 // No observers, bail
1087 NECP_OBSERVER_LIST_UNLOCK();
1091 struct necp_fd_data
*observer_fd
= NULL
;
1092 LIST_FOREACH(observer_fd
, &necp_fd_observer_list
, chain
) {
1093 necp_client_update_observer_update_internal(observer_fd
, client
);
1096 NECP_OBSERVER_LIST_UNLOCK();
1100 necp_client_update_observer_remove(struct necp_client
*client
)
1102 NECP_OBSERVER_LIST_LOCK_SHARED();
1104 if (LIST_EMPTY(&necp_fd_observer_list
)) {
1105 // No observers, bail
1106 NECP_OBSERVER_LIST_UNLOCK();
1110 struct necp_fd_data
*observer_fd
= NULL
;
1111 LIST_FOREACH(observer_fd
, &necp_fd_observer_list
, chain
) {
1112 necp_client_update_observer_remove_internal(observer_fd
, client
);
1115 NECP_OBSERVER_LIST_UNLOCK();
1119 necp_destroy_client_flow_registration(struct necp_client
*client
,
1120 struct necp_client_flow_registration
*flow_registration
,
1121 pid_t pid
, bool abort
)
1123 NECP_CLIENT_ASSERT_LOCKED(client
);
1125 bool has_close_parameters
= false;
1126 struct necp_client_agent_parameters close_parameters
= {};
1127 memset(close_parameters
.u
.close_token
, 0, sizeof(close_parameters
.u
.close_token
));
1129 struct necp_client_flow
*search_flow
= NULL
;
1130 struct necp_client_flow
*temp_flow
= NULL
;
1131 LIST_FOREACH_SAFE(search_flow
, &flow_registration
->flow_list
, flow_chain
, temp_flow
) {
1132 if (search_flow
->nexus
&&
1133 !uuid_is_null(search_flow
->u
.nexus_agent
)) {
1134 // Note that if we had defuncted the client earlier, this would result in a harmless ENOENT
1135 u_int8_t message_type
= (abort
? NETAGENT_MESSAGE_TYPE_ABORT_NEXUS
:
1136 NETAGENT_MESSAGE_TYPE_CLOSE_NEXUS
);
1137 if (((flow_registration
->flags
& NECP_CLIENT_FLOW_FLAGS_BROWSE
) ||
1138 (flow_registration
->flags
& NECP_CLIENT_FLOW_FLAGS_RESOLVE
)) &&
1139 !(flow_registration
->flags
& NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS
)) {
1140 message_type
= NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT
;
1142 int netagent_error
= netagent_client_message_with_params(search_flow
->u
.nexus_agent
,
1143 ((flow_registration
->flags
& NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID
) ?
1145 flow_registration
->registration_id
),
1146 pid
, client
->agent_handle
,
1148 has_close_parameters
? &close_parameters
: NULL
,
1150 if (netagent_error
!= 0 && netagent_error
!= ENOENT
) {
1151 NECPLOG(LOG_ERR
, "necp_client_remove close nexus error (%d) MESSAGE TYPE %u", netagent_error
, message_type
);
1153 uuid_clear(search_flow
->u
.nexus_agent
);
1155 if (search_flow
->assigned_results
!= NULL
) {
1156 FREE(search_flow
->assigned_results
, M_NETAGENT
);
1157 search_flow
->assigned_results
= NULL
;
1159 LIST_REMOVE(search_flow
, flow_chain
);
1160 if (search_flow
->socket
) {
1161 OSDecrementAtomic(&necp_socket_flow_count
);
1163 OSDecrementAtomic(&necp_if_flow_count
);
1165 mcache_free(necp_flow_cache
, search_flow
);
1168 RB_REMOVE(_necp_client_flow_tree
, &client
->flow_registrations
, flow_registration
);
1169 flow_registration
->client
= NULL
;
1171 mcache_free(necp_flow_registration_cache
, flow_registration
);
1175 necp_destroy_client(struct necp_client
*client
, pid_t pid
, bool abort
)
1177 NECP_CLIENT_ASSERT_UNLOCKED(client
);
1179 necp_client_update_observer_remove(client
);
1181 NECP_CLIENT_LOCK(client
);
1184 NECP_CLIENT_ROUTE_LOCK(client
);
1185 if (client
->current_route
!= NULL
) {
1186 rtfree(client
->current_route
);
1187 client
->current_route
= NULL
;
1189 NECP_CLIENT_ROUTE_UNLOCK(client
);
1191 // Remove flow assignments
1192 struct necp_client_flow_registration
*flow_registration
= NULL
;
1193 struct necp_client_flow_registration
*temp_flow_registration
= NULL
;
1194 RB_FOREACH_SAFE(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
, temp_flow_registration
) {
1195 necp_destroy_client_flow_registration(client
, flow_registration
, pid
, abort
);
1199 // Remove agent assertions
1200 struct necp_client_assertion
*search_assertion
= NULL
;
1201 struct necp_client_assertion
*temp_assertion
= NULL
;
1202 LIST_FOREACH_SAFE(search_assertion
, &client
->assertion_list
, assertion_chain
, temp_assertion
) {
1203 int netagent_error
= netagent_client_message(search_assertion
->asserted_netagent
, client
->client_id
, pid
,
1204 client
->agent_handle
, NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT
);
1205 if (netagent_error
!= 0) {
1206 NECPLOG((netagent_error
== ENOENT
? LOG_DEBUG
: LOG_ERR
),
1207 "necp_client_remove unassert agent error (%d)", netagent_error
);
1209 LIST_REMOVE(search_assertion
, assertion_chain
);
1210 FREE(search_assertion
, M_NECP
);
1213 if (!necp_client_release_locked(client
)) {
1214 NECP_CLIENT_UNLOCK(client
);
1217 OSDecrementAtomic(&necp_client_count
);
1221 necpop_close(struct fileglob
*fg
, vfs_context_t ctx
)
1224 struct necp_fd_data
*fd_data
= NULL
;
1227 fd_data
= (struct necp_fd_data
*)fg
->fg_data
;
1230 if (fd_data
!= NULL
) {
1231 struct _necp_client_tree clients_to_close
;
1232 RB_INIT(&clients_to_close
);
1234 // Remove from list quickly
1235 if (fd_data
->flags
& NECP_OPEN_FLAG_PUSH_OBSERVER
) {
1236 NECP_OBSERVER_LIST_LOCK_EXCLUSIVE();
1237 LIST_REMOVE(fd_data
, chain
);
1238 NECP_OBSERVER_LIST_UNLOCK();
1240 NECP_FD_LIST_LOCK_EXCLUSIVE();
1241 LIST_REMOVE(fd_data
, chain
);
1242 NECP_FD_LIST_UNLOCK();
1245 NECP_FD_LOCK(fd_data
);
1246 pid_t pid
= fd_data
->proc_pid
;
1248 struct necp_client_flow_registration
*flow_registration
= NULL
;
1249 struct necp_client_flow_registration
*temp_flow_registration
= NULL
;
1250 RB_FOREACH_SAFE(flow_registration
, _necp_fd_flow_tree
, &fd_data
->flows
, temp_flow_registration
) {
1251 NECP_FLOW_TREE_LOCK_EXCLUSIVE();
1252 RB_REMOVE(_necp_client_flow_global_tree
, &necp_client_flow_global_tree
, flow_registration
);
1253 NECP_FLOW_TREE_UNLOCK();
1254 RB_REMOVE(_necp_fd_flow_tree
, &fd_data
->flows
, flow_registration
);
1257 struct necp_client
*client
= NULL
;
1258 struct necp_client
*temp_client
= NULL
;
1259 RB_FOREACH_SAFE(client
, _necp_client_tree
, &fd_data
->clients
, temp_client
) {
1260 NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
1261 RB_REMOVE(_necp_client_global_tree
, &necp_client_global_tree
, client
);
1262 NECP_CLIENT_TREE_UNLOCK();
1263 RB_REMOVE(_necp_client_tree
, &fd_data
->clients
, client
);
1264 RB_INSERT(_necp_client_tree
, &clients_to_close
, client
);
1267 struct necp_client_update
*client_update
= NULL
;
1268 struct necp_client_update
*temp_update
= NULL
;
1269 TAILQ_FOREACH_SAFE(client_update
, &fd_data
->update_list
, chain
, temp_update
) {
1270 // Flush pending updates
1271 TAILQ_REMOVE(&fd_data
->update_list
, client_update
, chain
);
1272 FREE(client_update
, M_NECP
);
1274 fd_data
->update_count
= 0;
1277 NECP_FD_UNLOCK(fd_data
);
1279 selthreadclear(&fd_data
->si
);
1281 lck_mtx_destroy(&fd_data
->fd_lock
, necp_fd_mtx_grp
);
1283 if (fd_data
->flags
& NECP_OPEN_FLAG_PUSH_OBSERVER
) {
1284 OSDecrementAtomic(&necp_observer_fd_count
);
1286 OSDecrementAtomic(&necp_client_fd_count
);
1289 zfree(necp_client_fd_zone
, fd_data
);
1292 RB_FOREACH_SAFE(client
, _necp_client_tree
, &clients_to_close
, temp_client
) {
1293 RB_REMOVE(_necp_client_tree
, &clients_to_close
, client
);
1294 necp_destroy_client(client
, pid
, true);
1301 /// NECP client utilities
1304 necp_address_is_wildcard(const union necp_sockaddr_union
* const addr
)
1306 return (addr
->sa
.sa_family
== AF_INET
&& addr
->sin
.sin_addr
.s_addr
== INADDR_ANY
) ||
1307 (addr
->sa
.sa_family
== AF_INET6
&& IN6_IS_ADDR_UNSPECIFIED(&addr
->sin6
.sin6_addr
));
1311 necp_find_fd_data(int fd
, struct necp_fd_data
**fd_data
)
1313 proc_t p
= current_proc();
1314 struct fileproc
*fp
= NULL
;
1317 proc_fdlock_spin(p
);
1318 if ((error
= fp_lookup(p
, fd
, &fp
, 1)) != 0) {
1321 if (fp
->f_fglob
->fg_ops
->fo_type
!= DTYPE_NETPOLICY
) {
1322 fp_drop(p
, fd
, fp
, 1);
1326 *fd_data
= (struct necp_fd_data
*)fp
->f_fglob
->fg_data
;
1328 if ((*fd_data
)->necp_fd_type
!= necp_fd_type_client
) {
1329 // Not a client fd, ignore
1330 fp_drop(p
, fd
, fp
, 1);
1341 static struct necp_client_flow
*
1342 necp_client_add_interface_flow(struct necp_client_flow_registration
*flow_registration
,
1343 uint32_t interface_index
)
1345 struct necp_client_flow
*new_flow
= mcache_alloc(necp_flow_cache
, MCR_SLEEP
);
1346 if (new_flow
== NULL
) {
1347 NECPLOG0(LOG_ERR
, "Failed to allocate interface flow");
1351 memset(new_flow
, 0, sizeof(*new_flow
));
1353 // Neither nexus nor socket
1354 new_flow
->interface_index
= interface_index
;
1355 new_flow
->u
.socket_handle
= flow_registration
->interface_handle
;
1356 new_flow
->u
.cb
= flow_registration
->interface_cb
;
1358 OSIncrementAtomic(&necp_if_flow_count
);
1360 LIST_INSERT_HEAD(&flow_registration
->flow_list
, new_flow
, flow_chain
);
1365 static struct necp_client_flow
*
1366 necp_client_add_interface_flow_if_needed(struct necp_client
*client
,
1367 struct necp_client_flow_registration
*flow_registration
,
1368 uint32_t interface_index
)
1370 if (!client
->allow_multiple_flows
||
1371 interface_index
== IFSCOPE_NONE
) {
1372 // Interface not set, or client not allowed to use this mode
1376 struct necp_client_flow
*flow
= NULL
;
1377 LIST_FOREACH(flow
, &flow_registration
->flow_list
, flow_chain
) {
1378 if (!flow
->nexus
&& !flow
->socket
&& flow
->interface_index
== interface_index
) {
1379 // Already have the flow
1380 flow
->invalid
= FALSE
;
1381 flow
->u
.socket_handle
= flow_registration
->interface_handle
;
1382 flow
->u
.cb
= flow_registration
->interface_cb
;
1386 return necp_client_add_interface_flow(flow_registration
, interface_index
);
1390 necp_client_add_interface_option_if_needed(struct necp_client
*client
,
1391 uint32_t interface_index
,
1392 uint32_t interface_generation
,
1393 uuid_t
*nexus_agent
)
1395 if (interface_index
== IFSCOPE_NONE
||
1396 (client
->interface_option_count
!= 0 && !client
->allow_multiple_flows
)) {
1397 // Interface not set, or client not allowed to use this mode
1401 if (client
->interface_option_count
>= NECP_CLIENT_MAX_INTERFACE_OPTIONS
) {
1402 // Cannot take any more interface options
1406 // Check if already present
1407 for (u_int32_t option_i
= 0; option_i
< client
->interface_option_count
; option_i
++) {
1408 if (option_i
< NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
) {
1409 struct necp_client_interface_option
*option
= &client
->interface_options
[option_i
];
1410 if (option
->interface_index
== interface_index
) {
1411 if (nexus_agent
== NULL
) {
1414 if (uuid_compare(option
->nexus_agent
, *nexus_agent
) == 0) {
1417 if (uuid_is_null(option
->nexus_agent
)) {
1418 uuid_copy(option
->nexus_agent
, *nexus_agent
);
1421 // If we get to this point, this is a new nexus flow
1424 struct necp_client_interface_option
*option
= &client
->extra_interface_options
[option_i
- NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
];
1425 if (option
->interface_index
== interface_index
) {
1426 if (nexus_agent
== NULL
) {
1429 if (uuid_compare(option
->nexus_agent
, *nexus_agent
) == 0) {
1432 if (uuid_is_null(option
->nexus_agent
)) {
1433 uuid_copy(option
->nexus_agent
, *nexus_agent
);
1436 // If we get to this point, this is a new nexus flow
1442 if (client
->interface_option_count
< NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
) {
1444 struct necp_client_interface_option
*option
= &client
->interface_options
[client
->interface_option_count
];
1445 option
->interface_index
= interface_index
;
1446 option
->interface_generation
= interface_generation
;
1447 if (nexus_agent
!= NULL
) {
1448 uuid_copy(option
->nexus_agent
, *nexus_agent
);
1450 uuid_clear(option
->nexus_agent
);
1452 client
->interface_option_count
++;
1455 if (client
->extra_interface_options
== NULL
) {
1456 client
->extra_interface_options
= _MALLOC(sizeof(struct necp_client_interface_option
) * NECP_CLIENT_INTERFACE_OPTION_EXTRA_COUNT
, M_NECP
, M_WAITOK
| M_ZERO
);
1458 if (client
->extra_interface_options
!= NULL
) {
1459 struct necp_client_interface_option
*option
= &client
->extra_interface_options
[client
->interface_option_count
- NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
];
1460 option
->interface_index
= interface_index
;
1461 option
->interface_generation
= interface_generation
;
1462 if (nexus_agent
!= NULL
) {
1463 uuid_copy(option
->nexus_agent
, *nexus_agent
);
1465 uuid_clear(option
->nexus_agent
);
1467 client
->interface_option_count
++;
1473 necp_client_flow_is_viable(proc_t proc
, struct necp_client
*client
,
1474 struct necp_client_flow
*flow
)
1476 struct necp_aggregate_result result
;
1477 bool ignore_address
= (client
->allow_multiple_flows
&& !flow
->nexus
&& !flow
->socket
);
1479 flow
->necp_flow_flags
= 0;
1480 int error
= necp_application_find_policy_match_internal(proc
, client
->parameters
,
1481 (u_int32_t
)client
->parameters_length
,
1482 &result
, &flow
->necp_flow_flags
, NULL
,
1483 flow
->interface_index
,
1484 &flow
->local_addr
, &flow
->remote_addr
, NULL
, NULL
,
1485 NULL
, ignore_address
, true);
1487 // Check for blocking agents
1488 for (int i
= 0; i
< NECP_MAX_NETAGENTS
; i
++) {
1489 if (uuid_is_null(result
.netagents
[i
])) {
1490 // Passed end of valid agents
1494 u_int32_t flags
= netagent_get_flags(result
.netagents
[i
]);
1495 if ((flags
& NETAGENT_FLAG_REGISTERED
) &&
1496 !(flags
& NETAGENT_FLAG_VOLUNTARY
) &&
1497 !(flags
& NETAGENT_FLAG_ACTIVE
) &&
1498 !(flags
& NETAGENT_FLAG_SPECIFIC_USE_ONLY
)) {
1499 // A required agent is not active, cause the flow to be marked non-viable
1504 return error
== 0 &&
1505 result
.routed_interface_index
!= IFSCOPE_NONE
&&
1506 result
.routing_result
!= NECP_KERNEL_POLICY_RESULT_DROP
;
1510 necp_flow_add_interface_flows(proc_t proc
,
1511 struct necp_client
*client
,
1512 struct necp_client_flow_registration
*flow_registration
,
1515 // Traverse all interfaces and add a tracking flow if needed
1516 for (u_int32_t option_i
= 0; option_i
< client
->interface_option_count
; option_i
++) {
1517 if (option_i
< NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
) {
1518 struct necp_client_interface_option
*option
= &client
->interface_options
[option_i
];
1519 struct necp_client_flow
*flow
= necp_client_add_interface_flow_if_needed(client
, flow_registration
, option
->interface_index
);
1520 if (flow
!= NULL
&& send_initial
) {
1521 flow
->viable
= necp_client_flow_is_viable(proc
, client
, flow
);
1522 if (flow
->viable
&& flow
->u
.cb
) {
1523 bool viable
= flow
->viable
;
1524 flow
->u
.cb(flow_registration
->interface_handle
, NECP_CLIENT_CBACTION_INITIAL
, flow
->interface_index
, flow
->necp_flow_flags
, &viable
);
1525 flow
->viable
= viable
;
1529 struct necp_client_interface_option
*option
= &client
->extra_interface_options
[option_i
- NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
];
1530 struct necp_client_flow
*flow
= necp_client_add_interface_flow_if_needed(client
, flow_registration
, option
->interface_index
);
1531 if (flow
!= NULL
&& send_initial
) {
1532 flow
->viable
= necp_client_flow_is_viable(proc
, client
, flow
);
1533 if (flow
->viable
&& flow
->u
.cb
) {
1534 bool viable
= flow
->viable
;
1535 flow
->u
.cb(flow_registration
->interface_handle
, NECP_CLIENT_CBACTION_INITIAL
, flow
->interface_index
, flow
->necp_flow_flags
, &viable
);
1536 flow
->viable
= viable
;
1544 necp_client_update_flows(proc_t proc
,
1545 struct necp_client
*client
,
1546 struct _necp_flow_defunct_list
*defunct_list
)
1548 NECP_CLIENT_ASSERT_LOCKED(client
);
1550 bool client_updated
= FALSE
;
1551 struct necp_client_flow
*flow
= NULL
;
1552 struct necp_client_flow
*temp_flow
= NULL
;
1553 struct necp_client_flow_registration
*flow_registration
= NULL
;
1554 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
1555 if (flow_registration
->interface_cb
!= NULL
) {
1556 // Add any interface flows that are not already tracked
1557 necp_flow_add_interface_flows(proc
, client
, flow_registration
, false);
1560 LIST_FOREACH_SAFE(flow
, &flow_registration
->flow_list
, flow_chain
, temp_flow
) {
1561 // Check policy result for flow
1562 int old_flags
= flow
->necp_flow_flags
;
1563 bool viable
= necp_client_flow_is_viable(proc
, client
, flow
);
1565 // TODO: Defunct nexus flows that are blocked by policy
1567 if (flow
->viable
!= viable
) {
1568 flow
->viable
= viable
;
1569 client_updated
= TRUE
;
1572 if ((old_flags
& NECP_CLIENT_RESULT_FLAG_FORCE_UPDATE
) !=
1573 (flow
->necp_flow_flags
& NECP_CLIENT_RESULT_FLAG_FORCE_UPDATE
)) {
1574 client_updated
= TRUE
;
1577 if (flow
->viable
&& client_updated
&& (flow
->socket
|| (!flow
->socket
&& !flow
->nexus
)) && flow
->u
.cb
) {
1578 bool flow_viable
= flow
->viable
;
1579 flow
->u
.cb(flow
->u
.socket_handle
, NECP_CLIENT_CBACTION_VIABLE
, flow
->interface_index
, flow
->necp_flow_flags
, &flow_viable
);
1580 flow
->viable
= flow_viable
;
1583 if (!flow
->viable
|| flow
->invalid
) {
1584 if (client_updated
&& (flow
->socket
|| (!flow
->socket
&& !flow
->nexus
)) && flow
->u
.cb
) {
1585 bool flow_viable
= flow
->viable
;
1586 flow
->u
.cb(flow
->u
.socket_handle
, NECP_CLIENT_CBACTION_NONVIABLE
, flow
->interface_index
, flow
->necp_flow_flags
, &flow_viable
);
1587 flow
->viable
= flow_viable
;
1589 // The callback might change the viable-flag of the
1590 // flow depending on its policy. Thus, we need to
1591 // check the flags again after the callback.
1596 // Handle flows that no longer match
1597 if (!flow
->viable
|| flow
->invalid
) {
1598 // Drop them as long as they aren't assigned data
1599 if (!flow
->nexus
&& !flow
->assigned
) {
1600 if (flow
->assigned_results
!= NULL
) {
1601 FREE(flow
->assigned_results
, M_NETAGENT
);
1602 flow
->assigned_results
= NULL
;
1603 client_updated
= TRUE
;
1605 LIST_REMOVE(flow
, flow_chain
);
1607 OSDecrementAtomic(&necp_socket_flow_count
);
1609 OSDecrementAtomic(&necp_if_flow_count
);
1611 mcache_free(necp_flow_cache
, flow
);
1617 return client_updated
;
1621 necp_client_mark_all_nonsocket_flows_as_invalid(struct necp_client
*client
)
1623 struct necp_client_flow_registration
*flow_registration
= NULL
;
1624 struct necp_client_flow
*flow
= NULL
;
1625 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
1626 LIST_FOREACH(flow
, &flow_registration
->flow_list
, flow_chain
) {
1627 if (!flow
->socket
) { // Socket flows are not marked as invalid
1628 flow
->invalid
= TRUE
;
1633 // Reset option count every update
1634 client
->interface_option_count
= 0;
1638 necp_netagent_applies_to_client(struct necp_client
*client
,
1639 const struct necp_client_parsed_parameters
*parameters
,
1640 uuid_t
*netagent_uuid
, bool allow_nexus
,
1641 uint32_t interface_index
, uint32_t interface_generation
)
1643 #pragma unused(interface_index, interface_generation)
1644 bool applies
= FALSE
;
1645 u_int32_t flags
= netagent_get_flags(*netagent_uuid
);
1646 if (!(flags
& NETAGENT_FLAG_REGISTERED
)) {
1647 // Unregistered agents never apply
1651 const bool is_nexus_agent
= ((flags
& NETAGENT_FLAG_NEXUS_PROVIDER
) ||
1652 (flags
& NETAGENT_FLAG_NEXUS_LISTENER
) ||
1653 (flags
& NETAGENT_FLAG_CUSTOM_ETHER_NEXUS
) ||
1654 (flags
& NETAGENT_FLAG_CUSTOM_IP_NEXUS
) ||
1655 (flags
& NETAGENT_FLAG_INTERPOSE_NEXUS
));
1656 if (is_nexus_agent
) {
1658 // Hide nexus providers unless allowed
1659 // Direct interfaces and direct policies are allowed to use a nexus
1660 // Delegate interfaces or re-scoped interfaces are not allowed
1664 if ((parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_CUSTOM_ETHER
) &&
1665 !(flags
& NETAGENT_FLAG_CUSTOM_ETHER_NEXUS
)) {
1666 // Client requested a custom ether nexus, but this nexus isn't one
1670 if ((parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_CUSTOM_IP
) &&
1671 !(flags
& NETAGENT_FLAG_CUSTOM_IP_NEXUS
)) {
1672 // Client requested a custom IP nexus, but this nexus isn't one
1676 if ((parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_INTERPOSE
) &&
1677 !(flags
& NETAGENT_FLAG_INTERPOSE_NEXUS
)) {
1678 // Client requested an interpose nexus, but this nexus isn't one
1682 if (!(parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_CUSTOM_ETHER
) &&
1683 !(parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_CUSTOM_IP
) &&
1684 !(parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_INTERPOSE
) &&
1685 !(flags
& NETAGENT_FLAG_NEXUS_PROVIDER
)) {
1686 // Client requested default parameters, but this nexus isn't generic
1691 if (uuid_compare(client
->failed_trigger_agent
.netagent_uuid
, *netagent_uuid
) == 0) {
1692 if (client
->failed_trigger_agent
.generation
== netagent_get_generation(*netagent_uuid
)) {
1693 // If this agent was triggered, and failed, and hasn't changed, keep hiding it
1696 // Mismatch generation, clear out old trigger
1697 uuid_clear(client
->failed_trigger_agent
.netagent_uuid
);
1698 client
->failed_trigger_agent
.generation
= 0;
1702 if (flags
& NETAGENT_FLAG_SPECIFIC_USE_ONLY
) {
1703 // Specific use agents only apply when required
1704 bool required
= FALSE
;
1705 if (parameters
!= NULL
) {
1706 // Check required agent UUIDs
1707 for (int i
= 0; i
< NECP_MAX_AGENT_PARAMETERS
; i
++) {
1708 if (uuid_is_null(parameters
->required_netagents
[i
])) {
1711 if (uuid_compare(parameters
->required_netagents
[i
], *netagent_uuid
) == 0) {
1718 // Check required agent types
1719 bool fetched_type
= FALSE
;
1720 char netagent_domain
[NETAGENT_DOMAINSIZE
];
1721 char netagent_type
[NETAGENT_TYPESIZE
];
1722 memset(&netagent_domain
, 0, NETAGENT_DOMAINSIZE
);
1723 memset(&netagent_type
, 0, NETAGENT_TYPESIZE
);
1725 for (int i
= 0; i
< NECP_MAX_AGENT_PARAMETERS
; i
++) {
1726 if (strlen(parameters
->required_netagent_types
[i
].netagent_domain
) == 0 ||
1727 strlen(parameters
->required_netagent_types
[i
].netagent_type
) == 0) {
1731 if (!fetched_type
) {
1732 if (netagent_get_agent_domain_and_type(*netagent_uuid
, netagent_domain
, netagent_type
)) {
1733 fetched_type
= TRUE
;
1739 if ((strlen(parameters
->required_netagent_types
[i
].netagent_domain
) == 0 ||
1740 strncmp(netagent_domain
, parameters
->required_netagent_types
[i
].netagent_domain
, NETAGENT_DOMAINSIZE
) == 0) &&
1741 (strlen(parameters
->required_netagent_types
[i
].netagent_type
) == 0 ||
1742 strncmp(netagent_type
, parameters
->required_netagent_types
[i
].netagent_type
, NETAGENT_TYPESIZE
) == 0)) {
1760 necp_client_add_agent_interface_options(struct necp_client
*client
,
1761 const struct necp_client_parsed_parameters
*parsed_parameters
,
1764 if (ifp
!= NULL
&& ifp
->if_agentids
!= NULL
) {
1765 for (u_int32_t i
= 0; i
< ifp
->if_agentcount
; i
++) {
1766 if (uuid_is_null(ifp
->if_agentids
[i
])) {
1769 // Relies on the side effect that nexus agents that apply will create flows
1770 (void)necp_netagent_applies_to_client(client
, parsed_parameters
, &ifp
->if_agentids
[i
], TRUE
,
1771 ifp
->if_index
, ifnet_get_generation(ifp
));
1777 necp_client_address_is_valid(struct sockaddr
*address
)
1779 if (address
->sa_family
== AF_INET
) {
1780 return address
->sa_len
== sizeof(struct sockaddr_in
);
1781 } else if (address
->sa_family
== AF_INET6
) {
1782 return address
->sa_len
== sizeof(struct sockaddr_in6
);
1789 necp_client_endpoint_is_unspecified(struct necp_client_endpoint
*endpoint
)
1791 if (necp_client_address_is_valid(&endpoint
->u
.sa
)) {
1792 if (endpoint
->u
.sa
.sa_family
== AF_INET
) {
1793 return endpoint
->u
.sin
.sin_addr
.s_addr
== INADDR_ANY
;
1794 } else if (endpoint
->u
.sa
.sa_family
== AF_INET6
) {
1795 return IN6_IS_ADDR_UNSPECIFIED(&endpoint
->u
.sin6
.sin6_addr
);
1806 necp_client_parse_parameters(u_int8_t
*parameters
,
1807 u_int32_t parameters_size
,
1808 struct necp_client_parsed_parameters
*parsed_parameters
)
1813 u_int32_t num_prohibited_interfaces
= 0;
1814 u_int32_t num_prohibited_interface_types
= 0;
1815 u_int32_t num_required_agents
= 0;
1816 u_int32_t num_prohibited_agents
= 0;
1817 u_int32_t num_preferred_agents
= 0;
1818 u_int32_t num_avoided_agents
= 0;
1819 u_int32_t num_required_agent_types
= 0;
1820 u_int32_t num_prohibited_agent_types
= 0;
1821 u_int32_t num_preferred_agent_types
= 0;
1822 u_int32_t num_avoided_agent_types
= 0;
1823 u_int8_t
*resolver_tag
= NULL
;
1824 u_int32_t resolver_tag_length
= 0;
1825 u_int8_t
*client_hostname
= NULL
;
1826 u_int32_t hostname_length
= 0;
1827 uuid_t parent_id
= {};
1829 if (parsed_parameters
== NULL
) {
1833 memset(parsed_parameters
, 0, sizeof(struct necp_client_parsed_parameters
));
1835 while ((offset
+ sizeof(struct necp_tlv_header
)) <= parameters_size
) {
1836 u_int8_t type
= necp_buffer_get_tlv_type(parameters
, offset
);
1837 u_int32_t length
= necp_buffer_get_tlv_length(parameters
, offset
);
1839 if (length
> (parameters_size
- (offset
+ sizeof(struct necp_tlv_header
)))) {
1840 // If the length is larger than what can fit in the remaining parameters size, bail
1841 NECPLOG(LOG_ERR
, "Invalid TLV length (%u)", length
);
1846 u_int8_t
*value
= necp_buffer_get_tlv_value(parameters
, offset
, NULL
);
1847 if (value
!= NULL
) {
1849 case NECP_CLIENT_PARAMETER_BOUND_INTERFACE
: {
1850 if (length
<= IFXNAMSIZ
&& length
> 0) {
1851 ifnet_t bound_interface
= NULL
;
1852 char interface_name
[IFXNAMSIZ
];
1853 memcpy(interface_name
, value
, length
);
1854 interface_name
[length
- 1] = 0; // Make sure the string is NULL terminated
1855 if (ifnet_find_by_name(interface_name
, &bound_interface
) == 0) {
1856 parsed_parameters
->required_interface_index
= bound_interface
->if_index
;
1857 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF
;
1858 ifnet_release(bound_interface
);
1863 case NECP_CLIENT_PARAMETER_LOCAL_ADDRESS
: {
1864 if (length
>= sizeof(struct necp_policy_condition_addr
)) {
1865 struct necp_policy_condition_addr
*address_struct
= (struct necp_policy_condition_addr
*)(void *)value
;
1866 if (necp_client_address_is_valid(&address_struct
->address
.sa
)) {
1867 memcpy(&parsed_parameters
->local_addr
, &address_struct
->address
, sizeof(address_struct
->address
));
1868 if (!necp_address_is_wildcard(&parsed_parameters
->local_addr
)) {
1869 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR
;
1871 if ((parsed_parameters
->local_addr
.sa
.sa_family
== AF_INET
&& parsed_parameters
->local_addr
.sin
.sin_port
) ||
1872 (parsed_parameters
->local_addr
.sa
.sa_family
== AF_INET6
&& parsed_parameters
->local_addr
.sin6
.sin6_port
)) {
1873 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_LOCAL_PORT
;
1879 case NECP_CLIENT_PARAMETER_LOCAL_ENDPOINT
: {
1880 if (length
>= sizeof(struct necp_client_endpoint
)) {
1881 struct necp_client_endpoint
*endpoint
= (struct necp_client_endpoint
*)(void *)value
;
1882 if (necp_client_address_is_valid(&endpoint
->u
.sa
)) {
1883 memcpy(&parsed_parameters
->local_addr
, &endpoint
->u
.sa
, sizeof(union necp_sockaddr_union
));
1884 if (!necp_address_is_wildcard(&parsed_parameters
->local_addr
)) {
1885 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR
;
1887 if ((parsed_parameters
->local_addr
.sa
.sa_family
== AF_INET
&& parsed_parameters
->local_addr
.sin
.sin_port
) ||
1888 (parsed_parameters
->local_addr
.sa
.sa_family
== AF_INET6
&& parsed_parameters
->local_addr
.sin6
.sin6_port
)) {
1889 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_LOCAL_PORT
;
1895 case NECP_CLIENT_PARAMETER_REMOTE_ADDRESS
: {
1896 if (length
>= sizeof(struct necp_policy_condition_addr
)) {
1897 struct necp_policy_condition_addr
*address_struct
= (struct necp_policy_condition_addr
*)(void *)value
;
1898 if (necp_client_address_is_valid(&address_struct
->address
.sa
)) {
1899 memcpy(&parsed_parameters
->remote_addr
, &address_struct
->address
, sizeof(address_struct
->address
));
1900 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_REMOTE_ADDR
;
1905 case NECP_CLIENT_PARAMETER_REMOTE_ENDPOINT
: {
1906 if (length
>= sizeof(struct necp_client_endpoint
)) {
1907 struct necp_client_endpoint
*endpoint
= (struct necp_client_endpoint
*)(void *)value
;
1908 if (necp_client_address_is_valid(&endpoint
->u
.sa
)) {
1909 memcpy(&parsed_parameters
->remote_addr
, &endpoint
->u
.sa
, sizeof(union necp_sockaddr_union
));
1910 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_REMOTE_ADDR
;
1915 case NECP_CLIENT_PARAMETER_PROHIBIT_INTERFACE
: {
1916 if (num_prohibited_interfaces
>= NECP_MAX_INTERFACE_PARAMETERS
) {
1919 if (length
<= IFXNAMSIZ
&& length
> 0) {
1920 memcpy(parsed_parameters
->prohibited_interfaces
[num_prohibited_interfaces
], value
, length
);
1921 parsed_parameters
->prohibited_interfaces
[num_prohibited_interfaces
][length
- 1] = 0; // Make sure the string is NULL terminated
1922 num_prohibited_interfaces
++;
1923 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IF
;
1927 case NECP_CLIENT_PARAMETER_REQUIRE_IF_TYPE
: {
1928 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE
) {
1931 if (length
>= sizeof(u_int8_t
)) {
1932 memcpy(&parsed_parameters
->required_interface_type
, value
, sizeof(u_int8_t
));
1933 if (parsed_parameters
->required_interface_type
) {
1934 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE
;
1939 case NECP_CLIENT_PARAMETER_PROHIBIT_IF_TYPE
: {
1940 if (num_prohibited_interface_types
>= NECP_MAX_INTERFACE_PARAMETERS
) {
1943 if (length
>= sizeof(u_int8_t
)) {
1944 memcpy(&parsed_parameters
->prohibited_interface_types
[num_prohibited_interface_types
], value
, sizeof(u_int8_t
));
1945 num_prohibited_interface_types
++;
1946 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IFTYPE
;
1950 case NECP_CLIENT_PARAMETER_REQUIRE_AGENT
: {
1951 if (num_required_agents
>= NECP_MAX_AGENT_PARAMETERS
) {
1954 if (length
>= sizeof(uuid_t
)) {
1955 memcpy(&parsed_parameters
->required_netagents
[num_required_agents
], value
, sizeof(uuid_t
));
1956 num_required_agents
++;
1957 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT
;
1961 case NECP_CLIENT_PARAMETER_PROHIBIT_AGENT
: {
1962 if (num_prohibited_agents
>= NECP_MAX_AGENT_PARAMETERS
) {
1965 if (length
>= sizeof(uuid_t
)) {
1966 memcpy(&parsed_parameters
->prohibited_netagents
[num_prohibited_agents
], value
, sizeof(uuid_t
));
1967 num_prohibited_agents
++;
1968 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT
;
1972 case NECP_CLIENT_PARAMETER_PREFER_AGENT
: {
1973 if (num_preferred_agents
>= NECP_MAX_AGENT_PARAMETERS
) {
1976 if (length
>= sizeof(uuid_t
)) {
1977 memcpy(&parsed_parameters
->preferred_netagents
[num_preferred_agents
], value
, sizeof(uuid_t
));
1978 num_preferred_agents
++;
1979 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT
;
1983 case NECP_CLIENT_PARAMETER_AVOID_AGENT
: {
1984 if (num_avoided_agents
>= NECP_MAX_AGENT_PARAMETERS
) {
1987 if (length
>= sizeof(uuid_t
)) {
1988 memcpy(&parsed_parameters
->avoided_netagents
[num_avoided_agents
], value
, sizeof(uuid_t
));
1989 num_avoided_agents
++;
1990 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT
;
1994 case NECP_CLIENT_PARAMETER_REQUIRE_AGENT_TYPE
: {
1995 if (num_required_agent_types
>= NECP_MAX_AGENT_PARAMETERS
) {
1998 if (length
>= sizeof(struct necp_client_parameter_netagent_type
)) {
1999 memcpy(&parsed_parameters
->required_netagent_types
[num_required_agent_types
], value
, sizeof(struct necp_client_parameter_netagent_type
));
2000 num_required_agent_types
++;
2001 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE
;
2005 case NECP_CLIENT_PARAMETER_PROHIBIT_AGENT_TYPE
: {
2006 if (num_prohibited_agent_types
>= NECP_MAX_AGENT_PARAMETERS
) {
2009 if (length
>= sizeof(struct necp_client_parameter_netagent_type
)) {
2010 memcpy(&parsed_parameters
->prohibited_netagent_types
[num_prohibited_agent_types
], value
, sizeof(struct necp_client_parameter_netagent_type
));
2011 num_prohibited_agent_types
++;
2012 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT_TYPE
;
2016 case NECP_CLIENT_PARAMETER_PREFER_AGENT_TYPE
: {
2017 if (num_preferred_agent_types
>= NECP_MAX_AGENT_PARAMETERS
) {
2020 if (length
>= sizeof(struct necp_client_parameter_netagent_type
)) {
2021 memcpy(&parsed_parameters
->preferred_netagent_types
[num_preferred_agent_types
], value
, sizeof(struct necp_client_parameter_netagent_type
));
2022 num_preferred_agent_types
++;
2023 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE
;
2027 case NECP_CLIENT_PARAMETER_AVOID_AGENT_TYPE
: {
2028 if (num_avoided_agent_types
>= NECP_MAX_AGENT_PARAMETERS
) {
2031 if (length
>= sizeof(struct necp_client_parameter_netagent_type
)) {
2032 memcpy(&parsed_parameters
->avoided_netagent_types
[num_avoided_agent_types
], value
, sizeof(struct necp_client_parameter_netagent_type
));
2033 num_avoided_agent_types
++;
2034 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE
;
2038 case NECP_CLIENT_PARAMETER_FLAGS
: {
2039 if (length
>= sizeof(u_int32_t
)) {
2040 memcpy(&parsed_parameters
->flags
, value
, sizeof(parsed_parameters
->flags
));
2041 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_FLAGS
;
2045 case NECP_CLIENT_PARAMETER_IP_PROTOCOL
: {
2046 if (length
== sizeof(u_int16_t
)) {
2047 u_int16_t large_ip_protocol
= 0;
2048 memcpy(&large_ip_protocol
, value
, sizeof(large_ip_protocol
));
2049 parsed_parameters
->ip_protocol
= (u_int8_t
)large_ip_protocol
;
2050 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_IP_PROTOCOL
;
2051 } else if (length
>= sizeof(parsed_parameters
->ip_protocol
)) {
2052 memcpy(&parsed_parameters
->ip_protocol
, value
, sizeof(parsed_parameters
->ip_protocol
));
2053 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_IP_PROTOCOL
;
2057 case NECP_CLIENT_PARAMETER_TRANSPORT_PROTOCOL
: {
2058 if (length
>= sizeof(parsed_parameters
->transport_protocol
)) {
2059 memcpy(&parsed_parameters
->transport_protocol
, value
, sizeof(parsed_parameters
->transport_protocol
));
2060 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_TRANSPORT_PROTOCOL
;
2064 case NECP_CLIENT_PARAMETER_PID
: {
2065 if (length
>= sizeof(parsed_parameters
->effective_pid
)) {
2066 memcpy(&parsed_parameters
->effective_pid
, value
, sizeof(parsed_parameters
->effective_pid
));
2067 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID
;
2071 case NECP_CLIENT_PARAMETER_DELEGATED_UPID
: {
2072 if (length
>= sizeof(parsed_parameters
->delegated_upid
)) {
2073 memcpy(&parsed_parameters
->delegated_upid
, value
, sizeof(parsed_parameters
->delegated_upid
));
2074 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_DELEGATED_UPID
;
2078 case NECP_CLIENT_PARAMETER_ETHERTYPE
: {
2079 if (length
>= sizeof(parsed_parameters
->ethertype
)) {
2080 memcpy(&parsed_parameters
->ethertype
, value
, sizeof(parsed_parameters
->ethertype
));
2081 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_ETHERTYPE
;
2085 case NECP_CLIENT_PARAMETER_APPLICATION
: {
2086 if (length
>= sizeof(parsed_parameters
->effective_uuid
)) {
2087 memcpy(&parsed_parameters
->effective_uuid
, value
, sizeof(parsed_parameters
->effective_uuid
));
2088 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_UUID
;
2092 case NECP_CLIENT_PARAMETER_TRAFFIC_CLASS
: {
2093 if (length
>= sizeof(parsed_parameters
->traffic_class
)) {
2094 memcpy(&parsed_parameters
->traffic_class
, value
, sizeof(parsed_parameters
->traffic_class
));
2095 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_TRAFFIC_CLASS
;
2099 case NECP_CLIENT_PARAMETER_RESOLVER_TAG
: {
2101 resolver_tag
= (u_int8_t
*)value
;
2102 resolver_tag_length
= length
;
2106 case NECP_CLIENT_PARAMETER_DOMAIN
: {
2108 client_hostname
= (u_int8_t
*)value
;
2109 hostname_length
= length
;
2113 case NECP_CLIENT_PARAMETER_PARENT_ID
: {
2114 if (length
== sizeof(parent_id
)) {
2115 uuid_copy(parent_id
, value
);
2119 case NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE
: {
2120 if (length
>= sizeof(parsed_parameters
->local_address_preference
)) {
2121 memcpy(&parsed_parameters
->local_address_preference
, value
, sizeof(parsed_parameters
->local_address_preference
));
2122 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR_PREFERENCE
;
2133 offset
+= sizeof(struct necp_tlv_header
) + length
;
2136 if (resolver_tag
!= NULL
) {
2137 union necp_sockaddr_union remote_addr
;
2138 memcpy(&remote_addr
, &parsed_parameters
->remote_addr
, sizeof(remote_addr
));
2139 remote_addr
.sin
.sin_port
= 0;
2140 const bool validated
= necp_validate_resolver_answer(parent_id
,
2141 client_hostname
, hostname_length
,
2142 (u_int8_t
*)&remote_addr
, sizeof(remote_addr
),
2143 resolver_tag
, resolver_tag_length
);
2146 NECPLOG(LOG_ERR
, "Failed to validate answer for hostname %s", client_hostname
);
2154 necp_client_parse_result(u_int8_t
*result
,
2155 u_int32_t result_size
,
2156 union necp_sockaddr_union
*local_address
,
2157 union necp_sockaddr_union
*remote_address
,
2160 #pragma unused(flow_stats)
2164 while ((offset
+ sizeof(struct necp_tlv_header
)) <= result_size
) {
2165 u_int8_t type
= necp_buffer_get_tlv_type(result
, offset
);
2166 u_int32_t length
= necp_buffer_get_tlv_length(result
, offset
);
2168 if (length
> 0 && (offset
+ sizeof(struct necp_tlv_header
) + length
) <= result_size
) {
2169 u_int8_t
*value
= necp_buffer_get_tlv_value(result
, offset
, NULL
);
2170 if (value
!= NULL
) {
2172 case NECP_CLIENT_RESULT_LOCAL_ENDPOINT
: {
2173 if (length
>= sizeof(struct necp_client_endpoint
)) {
2174 struct necp_client_endpoint
*endpoint
= (struct necp_client_endpoint
*)(void *)value
;
2175 if (local_address
!= NULL
&& necp_client_address_is_valid(&endpoint
->u
.sa
)) {
2176 memcpy(local_address
, &endpoint
->u
.sa
, endpoint
->u
.sa
.sa_len
);
2181 case NECP_CLIENT_RESULT_REMOTE_ENDPOINT
: {
2182 if (length
>= sizeof(struct necp_client_endpoint
)) {
2183 struct necp_client_endpoint
*endpoint
= (struct necp_client_endpoint
*)(void *)value
;
2184 if (remote_address
!= NULL
&& necp_client_address_is_valid(&endpoint
->u
.sa
)) {
2185 memcpy(remote_address
, &endpoint
->u
.sa
, endpoint
->u
.sa
.sa_len
);
2197 offset
+= sizeof(struct necp_tlv_header
) + length
;
2203 static struct necp_client_flow_registration
*
2204 necp_client_create_flow_registration(struct necp_fd_data
*fd_data
, struct necp_client
*client
)
2206 NECP_FD_ASSERT_LOCKED(fd_data
);
2207 NECP_CLIENT_ASSERT_LOCKED(client
);
2209 struct necp_client_flow_registration
*new_registration
= mcache_alloc(necp_flow_registration_cache
, MCR_SLEEP
);
2210 if (new_registration
== NULL
) {
2214 memset(new_registration
, 0, sizeof(*new_registration
));
2216 new_registration
->last_interface_details
= combine_interface_details(IFSCOPE_NONE
, NSTAT_IFNET_IS_UNKNOWN_TYPE
);
2218 necp_generate_client_id(new_registration
->registration_id
, true);
2219 LIST_INIT(&new_registration
->flow_list
);
2221 // Add registration to client list
2222 RB_INSERT(_necp_client_flow_tree
, &client
->flow_registrations
, new_registration
);
2224 // Add registration to fd list
2225 RB_INSERT(_necp_fd_flow_tree
, &fd_data
->flows
, new_registration
);
2227 // Add registration to global tree for lookup
2228 NECP_FLOW_TREE_LOCK_EXCLUSIVE();
2229 RB_INSERT(_necp_client_flow_global_tree
, &necp_client_flow_global_tree
, new_registration
);
2230 NECP_FLOW_TREE_UNLOCK();
2232 new_registration
->client
= client
;
2234 // Start out assuming there is nothing to read from the flow
2235 new_registration
->flow_result_read
= true;
2237 return new_registration
;
2241 necp_client_add_socket_flow(struct necp_client_flow_registration
*flow_registration
,
2244 struct necp_client_flow
*new_flow
= mcache_alloc(necp_flow_cache
, MCR_SLEEP
);
2245 if (new_flow
== NULL
) {
2246 NECPLOG0(LOG_ERR
, "Failed to allocate socket flow");
2250 memset(new_flow
, 0, sizeof(*new_flow
));
2252 new_flow
->socket
= TRUE
;
2253 new_flow
->u
.socket_handle
= inp
;
2254 new_flow
->u
.cb
= inp
->necp_cb
;
2256 OSIncrementAtomic(&necp_socket_flow_count
);
2258 LIST_INSERT_HEAD(&flow_registration
->flow_list
, new_flow
, flow_chain
);
2262 necp_client_register_socket_inner(pid_t pid
, uuid_t client_id
, struct inpcb
*inp
, bool is_listener
)
2265 struct necp_fd_data
*client_fd
= NULL
;
2266 bool found_client
= FALSE
;
2268 NECP_FD_LIST_LOCK_SHARED();
2269 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
2270 NECP_FD_LOCK(client_fd
);
2271 struct necp_client
*client
= necp_client_fd_find_client_and_lock(client_fd
, client_id
);
2272 if (client
!= NULL
) {
2273 if (!pid
|| client
->proc_pid
== pid
) {
2275 found_client
= TRUE
;
2277 // Find client flow and assign from socket
2278 struct necp_client_flow_registration
*flow_registration
= necp_client_find_flow(client
, client_id
);
2279 if (flow_registration
!= NULL
) {
2280 // Found the right client and flow registration, add a new flow
2281 found_client
= TRUE
;
2282 necp_client_add_socket_flow(flow_registration
, inp
);
2283 } else if (RB_EMPTY(&client
->flow_registrations
) && !necp_client_id_is_flow(client_id
)) {
2284 // No flows yet on this client, add a new registration
2285 flow_registration
= necp_client_create_flow_registration(client_fd
, client
);
2286 if (flow_registration
== NULL
) {
2290 found_client
= TRUE
;
2291 necp_client_add_socket_flow(flow_registration
, inp
);
2297 NECP_CLIENT_UNLOCK(client
);
2299 NECP_FD_UNLOCK(client_fd
);
2305 NECP_FD_LIST_UNLOCK();
2307 if (!found_client
) {
2310 // Count the sockets that have the NECP client UUID set
2311 struct socket
*so
= inp
->inp_socket
;
2312 if (!(so
->so_flags1
& SOF1_HAS_NECP_CLIENT_UUID
)) {
2313 so
->so_flags1
|= SOF1_HAS_NECP_CLIENT_UUID
;
2314 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_necp_clientuuid_total
);
2322 necp_client_register_socket_flow(pid_t pid
, uuid_t client_id
, struct inpcb
*inp
)
2324 return necp_client_register_socket_inner(pid
, client_id
, inp
, false);
2328 necp_client_register_socket_listener(pid_t pid
, uuid_t client_id
, struct inpcb
*inp
)
2330 return necp_client_register_socket_inner(pid
, client_id
, inp
, true);
2335 necp_client_add_multipath_interface_flows(struct necp_client_flow_registration
*flow_registration
,
2336 struct necp_client
*client
,
2339 flow_registration
->interface_handle
= mpp
;
2340 flow_registration
->interface_cb
= mpp
->necp_cb
;
2342 proc_t proc
= proc_find(client
->proc_pid
);
2343 if (proc
== PROC_NULL
) {
2347 // Traverse all interfaces and add a tracking flow if needed
2348 necp_flow_add_interface_flows(proc
, client
, flow_registration
, true);
2355 necp_client_register_multipath_cb(pid_t pid
, uuid_t client_id
, struct mppcb
*mpp
)
2358 struct necp_fd_data
*client_fd
= NULL
;
2359 bool found_client
= FALSE
;
2361 NECP_FD_LIST_LOCK_SHARED();
2362 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
2363 NECP_FD_LOCK(client_fd
);
2364 struct necp_client
*client
= necp_client_fd_find_client_and_lock(client_fd
, client_id
);
2365 if (client
!= NULL
) {
2366 if (!pid
|| client
->proc_pid
== pid
) {
2367 struct necp_client_flow_registration
*flow_registration
= necp_client_find_flow(client
, client_id
);
2368 if (flow_registration
!= NULL
) {
2369 // Found the right client and flow registration, add a new flow
2370 found_client
= TRUE
;
2371 necp_client_add_multipath_interface_flows(flow_registration
, client
, mpp
);
2372 } else if (RB_EMPTY(&client
->flow_registrations
) && !necp_client_id_is_flow(client_id
)) {
2373 // No flows yet on this client, add a new registration
2374 flow_registration
= necp_client_create_flow_registration(client_fd
, client
);
2375 if (flow_registration
== NULL
) {
2379 found_client
= TRUE
;
2380 necp_client_add_multipath_interface_flows(flow_registration
, client
, mpp
);
2385 NECP_CLIENT_UNLOCK(client
);
2387 NECP_FD_UNLOCK(client_fd
);
2393 NECP_FD_LIST_UNLOCK();
2395 if (!found_client
&& error
== 0) {
2402 #define NETAGENT_DOMAIN_RADIO_MANAGER "WirelessRadioManager"
2403 #define NETAGENT_TYPE_RADIO_MANAGER "WirelessRadioManager:BB Manager"
2406 necp_client_lookup_bb_radio_manager(struct necp_client
*client
,
2407 uuid_t netagent_uuid
)
2409 char netagent_domain
[NETAGENT_DOMAINSIZE
];
2410 char netagent_type
[NETAGENT_TYPESIZE
];
2411 struct necp_aggregate_result result
;
2415 proc
= proc_find(client
->proc_pid
);
2416 if (proc
== PROC_NULL
) {
2420 error
= necp_application_find_policy_match_internal(proc
, client
->parameters
, (u_int32_t
)client
->parameters_length
,
2421 &result
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
, NULL
, true, true);
2430 for (int i
= 0; i
< NECP_MAX_NETAGENTS
; i
++) {
2431 if (uuid_is_null(result
.netagents
[i
])) {
2432 // Passed end of valid agents
2436 memset(&netagent_domain
, 0, NETAGENT_DOMAINSIZE
);
2437 memset(&netagent_type
, 0, NETAGENT_TYPESIZE
);
2438 if (netagent_get_agent_domain_and_type(result
.netagents
[i
], netagent_domain
, netagent_type
) == FALSE
) {
2442 if (strncmp(netagent_domain
, NETAGENT_DOMAIN_RADIO_MANAGER
, NETAGENT_DOMAINSIZE
) != 0) {
2446 if (strncmp(netagent_type
, NETAGENT_TYPE_RADIO_MANAGER
, NETAGENT_TYPESIZE
) != 0) {
2450 uuid_copy(netagent_uuid
, result
.netagents
[i
]);
2459 necp_client_assert_bb_radio_manager_common(struct necp_client
*client
, bool assert)
2461 uuid_t netagent_uuid
;
2462 uint8_t assert_type
;
2465 error
= necp_client_lookup_bb_radio_manager(client
, netagent_uuid
);
2467 NECPLOG0(LOG_ERR
, "BB radio manager agent not found");
2471 // Before unasserting, verify that the assertion was already taken
2472 if (assert == FALSE
) {
2473 assert_type
= NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT
;
2475 if (!necp_client_remove_assertion(client
, netagent_uuid
)) {
2479 assert_type
= NETAGENT_MESSAGE_TYPE_CLIENT_ASSERT
;
2482 error
= netagent_client_message(netagent_uuid
, client
->client_id
, client
->proc_pid
, client
->agent_handle
, assert_type
);
2484 NECPLOG0(LOG_ERR
, "netagent_client_message failed");
2488 // Only save the assertion if the action succeeded
2489 if (assert == TRUE
) {
2490 necp_client_add_assertion(client
, netagent_uuid
);
2497 necp_client_assert_bb_radio_manager(uuid_t client_id
, bool assert)
2499 struct necp_client
*client
;
2502 NECP_CLIENT_TREE_LOCK_SHARED();
2504 client
= necp_find_client_and_lock(client_id
);
2507 // Found the right client!
2508 error
= necp_client_assert_bb_radio_manager_common(client
, assert);
2510 NECP_CLIENT_UNLOCK(client
);
2512 NECPLOG0(LOG_ERR
, "Couldn't find client");
2516 NECP_CLIENT_TREE_UNLOCK();
2522 necp_client_unregister_socket_flow(uuid_t client_id
, void *handle
)
2525 struct necp_fd_data
*client_fd
= NULL
;
2526 bool found_client
= FALSE
;
2527 bool client_updated
= FALSE
;
2529 NECP_FD_LIST_LOCK_SHARED();
2530 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
2531 NECP_FD_LOCK(client_fd
);
2533 struct necp_client
*client
= necp_client_fd_find_client_and_lock(client_fd
, client_id
);
2534 if (client
!= NULL
) {
2535 struct necp_client_flow_registration
*flow_registration
= necp_client_find_flow(client
, client_id
);
2536 if (flow_registration
!= NULL
) {
2537 // Found the right client and flow!
2538 found_client
= TRUE
;
2540 // Remove flow assignment
2541 struct necp_client_flow
*search_flow
= NULL
;
2542 struct necp_client_flow
*temp_flow
= NULL
;
2543 LIST_FOREACH_SAFE(search_flow
, &flow_registration
->flow_list
, flow_chain
, temp_flow
) {
2544 if (search_flow
->socket
&& search_flow
->u
.socket_handle
== handle
) {
2545 if (search_flow
->assigned_results
!= NULL
) {
2546 FREE(search_flow
->assigned_results
, M_NETAGENT
);
2547 search_flow
->assigned_results
= NULL
;
2549 client_updated
= TRUE
;
2550 flow_registration
->flow_result_read
= FALSE
;
2551 LIST_REMOVE(search_flow
, flow_chain
);
2552 OSDecrementAtomic(&necp_socket_flow_count
);
2553 mcache_free(necp_flow_cache
, search_flow
);
2558 NECP_CLIENT_UNLOCK(client
);
2561 if (client_updated
) {
2562 necp_fd_notify(client_fd
, true);
2564 NECP_FD_UNLOCK(client_fd
);
2570 NECP_FD_LIST_UNLOCK();
2572 if (!found_client
) {
2580 necp_client_unregister_multipath_cb(uuid_t client_id
, void *handle
)
2583 bool found_client
= FALSE
;
2585 NECP_CLIENT_TREE_LOCK_SHARED();
2587 struct necp_client
*client
= necp_find_client_and_lock(client_id
);
2588 if (client
!= NULL
) {
2589 struct necp_client_flow_registration
*flow_registration
= necp_client_find_flow(client
, client_id
);
2590 if (flow_registration
!= NULL
) {
2591 // Found the right client and flow!
2592 found_client
= TRUE
;
2594 // Remove flow assignment
2595 struct necp_client_flow
*search_flow
= NULL
;
2596 struct necp_client_flow
*temp_flow
= NULL
;
2597 LIST_FOREACH_SAFE(search_flow
, &flow_registration
->flow_list
, flow_chain
, temp_flow
) {
2598 if (!search_flow
->socket
&& !search_flow
->nexus
&&
2599 search_flow
->u
.socket_handle
== handle
) {
2600 search_flow
->u
.socket_handle
= NULL
;
2601 search_flow
->u
.cb
= NULL
;
2605 flow_registration
->interface_handle
= NULL
;
2606 flow_registration
->interface_cb
= NULL
;
2609 NECP_CLIENT_UNLOCK(client
);
2612 NECP_CLIENT_TREE_UNLOCK();
2614 if (!found_client
) {
2622 necp_client_assign_from_socket(pid_t pid
, uuid_t client_id
, struct inpcb
*inp
)
2625 struct necp_fd_data
*client_fd
= NULL
;
2626 bool found_client
= FALSE
;
2627 bool client_updated
= FALSE
;
2629 NECP_FD_LIST_LOCK_SHARED();
2630 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
2631 if (pid
&& client_fd
->proc_pid
!= pid
) {
2635 proc_t proc
= proc_find(client_fd
->proc_pid
);
2636 if (proc
== PROC_NULL
) {
2640 NECP_FD_LOCK(client_fd
);
2642 struct necp_client
*client
= necp_client_fd_find_client_and_lock(client_fd
, client_id
);
2643 if (client
!= NULL
) {
2644 struct necp_client_flow_registration
*flow_registration
= necp_client_find_flow(client
, client_id
);
2645 if (flow_registration
== NULL
&& RB_EMPTY(&client
->flow_registrations
) && !necp_client_id_is_flow(client_id
)) {
2646 // No flows yet on this client, add a new registration
2647 flow_registration
= necp_client_create_flow_registration(client_fd
, client
);
2648 if (flow_registration
== NULL
) {
2652 if (flow_registration
!= NULL
) {
2653 // Found the right client and flow!
2654 found_client
= TRUE
;
2656 struct necp_client_flow
*flow
= NULL
;
2657 LIST_FOREACH(flow
, &flow_registration
->flow_list
, flow_chain
) {
2658 if (flow
->socket
&& flow
->u
.socket_handle
== inp
) {
2659 // Release prior results and route
2660 if (flow
->assigned_results
!= NULL
) {
2661 FREE(flow
->assigned_results
, M_NETAGENT
);
2662 flow
->assigned_results
= NULL
;
2666 if ((inp
->inp_flags
& INP_BOUND_IF
) && inp
->inp_boundifp
) {
2667 ifp
= inp
->inp_boundifp
;
2669 ifp
= inp
->inp_last_outifp
;
2673 flow
->interface_index
= ifp
->if_index
;
2675 flow
->interface_index
= IFSCOPE_NONE
;
2678 if (inp
->inp_vflag
& INP_IPV4
) {
2679 flow
->local_addr
.sin
.sin_family
= AF_INET
;
2680 flow
->local_addr
.sin
.sin_len
= sizeof(struct sockaddr_in
);
2681 flow
->local_addr
.sin
.sin_port
= inp
->inp_lport
;
2682 memcpy(&flow
->local_addr
.sin
.sin_addr
, &inp
->inp_laddr
, sizeof(struct in_addr
));
2684 flow
->remote_addr
.sin
.sin_family
= AF_INET
;
2685 flow
->remote_addr
.sin
.sin_len
= sizeof(struct sockaddr_in
);
2686 flow
->remote_addr
.sin
.sin_port
= inp
->inp_fport
;
2687 memcpy(&flow
->remote_addr
.sin
.sin_addr
, &inp
->inp_faddr
, sizeof(struct in_addr
));
2688 } else if (inp
->inp_vflag
& INP_IPV6
) {
2689 in6_ip6_to_sockaddr(&inp
->in6p_laddr
, inp
->inp_lport
, &flow
->local_addr
.sin6
, sizeof(flow
->local_addr
));
2690 in6_ip6_to_sockaddr(&inp
->in6p_faddr
, inp
->inp_fport
, &flow
->remote_addr
.sin6
, sizeof(flow
->remote_addr
));
2693 flow
->viable
= necp_client_flow_is_viable(proc
, client
, flow
);
2696 uuid_clear(empty_uuid
);
2697 flow
->assigned
= TRUE
;
2698 flow
->assigned_results
= necp_create_nexus_assign_message(empty_uuid
, 0, NULL
, 0,
2699 (struct necp_client_endpoint
*)&flow
->local_addr
,
2700 (struct necp_client_endpoint
*)&flow
->remote_addr
,
2701 NULL
, 0, NULL
, &flow
->assigned_results_length
);
2702 flow_registration
->flow_result_read
= FALSE
;
2703 client_updated
= TRUE
;
2709 NECP_CLIENT_UNLOCK(client
);
2711 if (client_updated
) {
2712 necp_fd_notify(client_fd
, true);
2714 NECP_FD_UNLOCK(client_fd
);
2723 NECP_FD_LIST_UNLOCK();
2726 if (!found_client
) {
2728 } else if (!client_updated
) {
2737 necp_socket_is_allowed_to_recv_on_interface(struct inpcb
*inp
, ifnet_t interface
)
2739 if (interface
== NULL
||
2741 !(inp
->inp_flags2
& INP2_EXTERNAL_PORT
) ||
2742 uuid_is_null(inp
->necp_client_uuid
)) {
2743 // If there's no interface or client ID to check,
2744 // or if this is not a listener, pass.
2745 // Outbound connections will have already been
2746 // validated for policy.
2750 // Only filter out listener sockets (no remote address specified)
2751 if ((inp
->inp_vflag
& INP_IPV4
) &&
2752 inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
2755 if ((inp
->inp_vflag
& INP_IPV6
) &&
2756 !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
)) {
2760 bool allowed
= TRUE
;
2762 NECP_CLIENT_TREE_LOCK_SHARED();
2764 struct necp_client
*client
= necp_find_client_and_lock(inp
->necp_client_uuid
);
2765 if (client
!= NULL
) {
2766 struct necp_client_parsed_parameters
*parsed_parameters
= NULL
;
2768 MALLOC(parsed_parameters
, struct necp_client_parsed_parameters
*, sizeof(*parsed_parameters
), M_NECP
, (M_WAITOK
| M_ZERO
));
2769 if (parsed_parameters
!= NULL
) {
2770 int error
= necp_client_parse_parameters(client
->parameters
, (u_int32_t
)client
->parameters_length
, parsed_parameters
);
2772 if (!necp_ifnet_matches_parameters(interface
, parsed_parameters
, 0, NULL
, true, false)) {
2776 FREE(parsed_parameters
, M_NECP
);
2779 NECP_CLIENT_UNLOCK(client
);
2782 NECP_CLIENT_TREE_UNLOCK();
2788 necp_update_flow_protoctl_event(uuid_t netagent_uuid
, uuid_t client_id
,
2789 uint32_t protoctl_event_code
, uint32_t protoctl_event_val
,
2790 uint32_t protoctl_event_tcp_seq_number
)
2793 struct necp_fd_data
*client_fd
= NULL
;
2794 bool found_client
= FALSE
;
2795 bool client_updated
= FALSE
;
2797 NECP_FD_LIST_LOCK_SHARED();
2798 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
2799 proc_t proc
= proc_find(client_fd
->proc_pid
);
2800 if (proc
== PROC_NULL
) {
2804 NECP_FD_LOCK(client_fd
);
2806 struct necp_client
*client
= necp_client_fd_find_client_and_lock(client_fd
, client_id
);
2807 if (client
!= NULL
) {
2808 struct necp_client_flow_registration
*flow_registration
= necp_client_find_flow(client
, client_id
);
2809 if (flow_registration
!= NULL
) {
2810 // Found the right client and flow!
2811 found_client
= TRUE
;
2813 struct necp_client_flow
*flow
= NULL
;
2814 LIST_FOREACH(flow
, &flow_registration
->flow_list
, flow_chain
) {
2815 // Verify that the client nexus agent matches
2817 uuid_compare(flow
->u
.nexus_agent
,
2818 netagent_uuid
) == 0) {
2819 flow
->has_protoctl_event
= TRUE
;
2820 flow
->protoctl_event
.protoctl_event_code
= protoctl_event_code
;
2821 flow
->protoctl_event
.protoctl_event_val
= protoctl_event_val
;
2822 flow
->protoctl_event
.protoctl_event_tcp_seq_num
= protoctl_event_tcp_seq_number
;
2823 flow_registration
->flow_result_read
= FALSE
;
2824 client_updated
= TRUE
;
2830 NECP_CLIENT_UNLOCK(client
);
2833 if (client_updated
) {
2834 necp_fd_notify(client_fd
, true);
2837 NECP_FD_UNLOCK(client_fd
);
2845 NECP_FD_LIST_UNLOCK();
2847 if (!found_client
) {
2849 } else if (!client_updated
) {
2856 necp_assign_client_result_locked(struct proc
*proc
,
2857 struct necp_fd_data
*client_fd
,
2858 struct necp_client
*client
,
2859 struct necp_client_flow_registration
*flow_registration
,
2860 uuid_t netagent_uuid
,
2861 u_int8_t
*assigned_results
,
2862 size_t assigned_results_length
,
2865 bool client_updated
= FALSE
;
2867 NECP_FD_ASSERT_LOCKED(client_fd
);
2868 NECP_CLIENT_ASSERT_LOCKED(client
);
2870 struct necp_client_flow
*flow
= NULL
;
2871 LIST_FOREACH(flow
, &flow_registration
->flow_list
, flow_chain
) {
2872 // Verify that the client nexus agent matches
2874 uuid_compare(flow
->u
.nexus_agent
, netagent_uuid
) == 0) {
2875 // Release prior results and route
2876 if (flow
->assigned_results
!= NULL
) {
2877 FREE(flow
->assigned_results
, M_NETAGENT
);
2878 flow
->assigned_results
= NULL
;
2881 void *nexus_stats
= NULL
;
2882 if (assigned_results
!= NULL
&& assigned_results_length
> 0) {
2883 int error
= necp_client_parse_result(assigned_results
, (u_int32_t
)assigned_results_length
,
2884 &flow
->local_addr
, &flow
->remote_addr
, &nexus_stats
);
2888 flow
->viable
= necp_client_flow_is_viable(proc
, client
, flow
);
2890 flow
->assigned
= TRUE
;
2891 flow
->assigned_results
= assigned_results
;
2892 flow
->assigned_results_length
= assigned_results_length
;
2893 flow_registration
->flow_result_read
= FALSE
;
2894 client_updated
= TRUE
;
2899 if (client_updated
&& notify_fd
) {
2900 necp_fd_notify(client_fd
, true);
2903 // if not updated, client must free assigned_results
2904 return client_updated
;
2908 necp_assign_client_result(uuid_t netagent_uuid
, uuid_t client_id
,
2909 u_int8_t
*assigned_results
, size_t assigned_results_length
)
2912 struct necp_fd_data
*client_fd
= NULL
;
2913 bool found_client
= FALSE
;
2914 bool client_updated
= FALSE
;
2916 NECP_FD_LIST_LOCK_SHARED();
2918 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
2919 proc_t proc
= proc_find(client_fd
->proc_pid
);
2920 if (proc
== PROC_NULL
) {
2924 NECP_FD_LOCK(client_fd
);
2925 struct necp_client
*client
= necp_client_fd_find_client_and_lock(client_fd
, client_id
);
2926 if (client
!= NULL
) {
2927 struct necp_client_flow_registration
*flow_registration
= necp_client_find_flow(client
, client_id
);
2928 if (flow_registration
!= NULL
) {
2929 // Found the right client and flow!
2930 found_client
= TRUE
;
2931 if (necp_assign_client_result_locked(proc
, client_fd
, client
, flow_registration
, netagent_uuid
,
2932 assigned_results
, assigned_results_length
, true)) {
2933 client_updated
= TRUE
;
2937 NECP_CLIENT_UNLOCK(client
);
2939 NECP_FD_UNLOCK(client_fd
);
2949 NECP_FD_LIST_UNLOCK();
2951 // upon error, client must free assigned_results
2952 if (!found_client
) {
2954 } else if (!client_updated
) {
2964 necp_update_parsed_parameters(struct necp_client_parsed_parameters
*parsed_parameters
,
2965 struct necp_aggregate_result
*result
)
2967 if (parsed_parameters
== NULL
||
2972 bool updated
= false;
2973 for (int i
= 0; i
< NECP_MAX_NETAGENTS
; i
++) {
2974 if (uuid_is_null(result
->netagents
[i
])) {
2975 // Passed end of valid agents
2979 if (!(result
->netagent_use_flags
[i
] & NECP_AGENT_USE_FLAG_SCOPE
)) {
2980 // Not a scoped agent, ignore
2984 // This is a scoped agent. Add it to the required agents.
2985 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT
) {
2986 // Already some required agents, add this at the end
2987 for (int j
= 0; j
< NECP_MAX_AGENT_PARAMETERS
; j
++) {
2988 if (uuid_compare(parsed_parameters
->required_netagents
[j
], result
->netagents
[i
]) == 0) {
2989 // Already required, break
2992 if (uuid_is_null(parsed_parameters
->required_netagents
[j
])) {
2994 memcpy(&parsed_parameters
->required_netagents
[j
], result
->netagents
[i
], sizeof(uuid_t
));
3000 // No required agents yet, add this one
3001 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT
;
3002 memcpy(&parsed_parameters
->required_netagents
[0], result
->netagents
[i
], sizeof(uuid_t
));
3006 // Remove requirements for agents of the same type
3007 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE
) {
3008 char remove_agent_domain
[NETAGENT_DOMAINSIZE
] = { 0 };
3009 char remove_agent_type
[NETAGENT_TYPESIZE
] = { 0 };
3010 if (netagent_get_agent_domain_and_type(result
->netagents
[i
], remove_agent_domain
, remove_agent_type
)) {
3011 for (int j
= 0; j
< NECP_MAX_AGENT_PARAMETERS
; j
++) {
3012 if (strlen(parsed_parameters
->required_netagent_types
[j
].netagent_domain
) == 0 &&
3013 strlen(parsed_parameters
->required_netagent_types
[j
].netagent_type
) == 0) {
3017 if (strncmp(parsed_parameters
->required_netagent_types
[j
].netagent_domain
, remove_agent_domain
, NETAGENT_DOMAINSIZE
) == 0 &&
3018 strncmp(parsed_parameters
->required_netagent_types
[j
].netagent_type
, remove_agent_type
, NETAGENT_TYPESIZE
) == 0) {
3021 if (j
== NECP_MAX_AGENT_PARAMETERS
- 1) {
3022 // Last field, just clear and break
3023 memset(&parsed_parameters
->required_netagent_types
[NECP_MAX_AGENT_PARAMETERS
- 1], 0, sizeof(struct necp_client_parameter_netagent_type
));
3026 // Move the parameters down, clear the last entry
3027 memmove(&parsed_parameters
->required_netagent_types
[j
],
3028 &parsed_parameters
->required_netagent_types
[j
+ 1],
3029 sizeof(struct necp_client_parameter_netagent_type
) * (NECP_MAX_AGENT_PARAMETERS
- (j
+ 1)));
3030 memset(&parsed_parameters
->required_netagent_types
[NECP_MAX_AGENT_PARAMETERS
- 1], 0, sizeof(struct necp_client_parameter_netagent_type
));
3031 // Continue, don't increment but look at the new shifted item instead
3036 // Increment j to look at the next agent type parameter
3044 parsed_parameters
->required_interface_index
!= IFSCOPE_NONE
&&
3045 (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF
) == 0) {
3046 // A required interface index was added after the fact. Clear it.
3047 parsed_parameters
->required_interface_index
= IFSCOPE_NONE
;
3055 necp_agent_types_match(const char *agent_domain1
, const char *agent_type1
,
3056 const char *agent_domain2
, const char *agent_type2
)
3058 return (strlen(agent_domain1
) == 0 ||
3059 strncmp(agent_domain2
, agent_domain1
, NETAGENT_DOMAINSIZE
) == 0) &&
3060 (strlen(agent_type1
) == 0 ||
3061 strncmp(agent_type2
, agent_type1
, NETAGENT_TYPESIZE
) == 0);
3065 necp_calculate_client_result(proc_t proc
,
3066 struct necp_client
*client
,
3067 struct necp_client_parsed_parameters
*parsed_parameters
,
3068 struct necp_aggregate_result
*result
,
3071 struct necp_client_endpoint
*v4_gateway
,
3072 struct necp_client_endpoint
*v6_gateway
)
3074 struct rtentry
*route
= NULL
;
3076 // Check parameters to find best interface
3077 bool validate_agents
= false;
3078 u_int matching_if_index
= 0;
3079 if (necp_find_matching_interface_index(parsed_parameters
, &matching_if_index
, &validate_agents
)) {
3080 if (matching_if_index
!= 0) {
3081 parsed_parameters
->required_interface_index
= matching_if_index
;
3083 // Interface found or not needed, match policy.
3084 memset(result
, 0, sizeof(*result
));
3085 int error
= necp_application_find_policy_match_internal(proc
, client
->parameters
,
3086 (u_int32_t
)client
->parameters_length
,
3087 result
, flags
, reason
, matching_if_index
,
3089 v4_gateway
, v6_gateway
,
3090 &route
, false, true);
3092 if (route
!= NULL
) {
3098 if (validate_agents
) {
3099 bool requirement_failed
= FALSE
;
3100 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT
) {
3101 for (int i
= 0; i
< NECP_MAX_AGENT_PARAMETERS
; i
++) {
3102 if (uuid_is_null(parsed_parameters
->required_netagents
[i
])) {
3106 bool requirement_found
= FALSE
;
3107 for (int j
= 0; j
< NECP_MAX_NETAGENTS
; j
++) {
3108 if (uuid_is_null(result
->netagents
[j
])) {
3112 if (uuid_compare(parsed_parameters
->required_netagents
[i
], result
->netagents
[j
]) == 0) {
3113 requirement_found
= TRUE
;
3118 if (!requirement_found
) {
3119 requirement_failed
= TRUE
;
3125 if (!requirement_failed
&& parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE
) {
3126 for (int i
= 0; i
< NECP_MAX_AGENT_PARAMETERS
; i
++) {
3127 if (strlen(parsed_parameters
->required_netagent_types
[i
].netagent_domain
) == 0 &&
3128 strlen(parsed_parameters
->required_netagent_types
[i
].netagent_type
) == 0) {
3132 bool requirement_found
= FALSE
;
3133 for (int j
= 0; j
< NECP_MAX_NETAGENTS
; j
++) {
3134 if (uuid_is_null(result
->netagents
[j
])) {
3138 char policy_agent_domain
[NETAGENT_DOMAINSIZE
] = { 0 };
3139 char policy_agent_type
[NETAGENT_TYPESIZE
] = { 0 };
3141 if (netagent_get_agent_domain_and_type(result
->netagents
[j
], policy_agent_domain
, policy_agent_type
)) {
3142 if (necp_agent_types_match(parsed_parameters
->required_netagent_types
[i
].netagent_domain
,
3143 parsed_parameters
->required_netagent_types
[i
].netagent_type
,
3144 policy_agent_domain
, policy_agent_type
)) {
3145 requirement_found
= TRUE
;
3151 if (!requirement_found
) {
3152 requirement_failed
= TRUE
;
3158 if (requirement_failed
) {
3159 // Agent requirement failed. Clear out the whole result, make everything fail.
3160 memset(result
, 0, sizeof(*result
));
3161 if (route
!= NULL
) {
3168 // Reset current route
3169 NECP_CLIENT_ROUTE_LOCK(client
);
3170 if (client
->current_route
!= NULL
) {
3171 rtfree(client
->current_route
);
3173 client
->current_route
= route
;
3174 NECP_CLIENT_ROUTE_UNLOCK(client
);
3176 // Interface not found. Clear out the whole result, make everything fail.
3177 memset(result
, 0, sizeof(*result
));
3183 #define NECP_PARSED_PARAMETERS_REQUIRED_FIELDS (NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF | \
3184 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE | \
3185 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT | \
3186 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE)
3189 necp_update_client_result(proc_t proc
,
3190 struct necp_fd_data
*client_fd
,
3191 struct necp_client
*client
,
3192 struct _necp_flow_defunct_list
*defunct_list
)
3194 struct necp_client_result_netagent netagent
;
3195 struct necp_aggregate_result result
;
3196 struct necp_client_parsed_parameters
*parsed_parameters
= NULL
;
3197 u_int32_t flags
= 0;
3198 u_int32_t reason
= 0;
3200 NECP_CLIENT_ASSERT_LOCKED(client
);
3202 MALLOC(parsed_parameters
, struct necp_client_parsed_parameters
*, sizeof(*parsed_parameters
), M_NECP
, (M_WAITOK
| M_ZERO
));
3203 if (parsed_parameters
== NULL
) {
3204 NECPLOG0(LOG_ERR
, "Failed to allocate parsed parameters");
3208 // Nexus flows will be brought back if they are still valid
3209 necp_client_mark_all_nonsocket_flows_as_invalid(client
);
3211 int error
= necp_client_parse_parameters(client
->parameters
, (u_int32_t
)client
->parameters_length
, parsed_parameters
);
3213 FREE(parsed_parameters
, M_NECP
);
3217 // Update saved IP protocol
3218 client
->ip_protocol
= parsed_parameters
->ip_protocol
;
3220 // Calculate the policy result
3221 struct necp_client_endpoint v4_gateway
= {};
3222 struct necp_client_endpoint v6_gateway
= {};
3223 if (!necp_calculate_client_result(proc
, client
, parsed_parameters
, &result
, &flags
, &reason
, &v4_gateway
, &v6_gateway
)) {
3224 FREE(parsed_parameters
, M_NECP
);
3228 if (necp_update_parsed_parameters(parsed_parameters
, &result
)) {
3229 // Changed the parameters based on result, try again (only once)
3230 if (!necp_calculate_client_result(proc
, client
, parsed_parameters
, &result
, &flags
, &reason
, &v4_gateway
, &v6_gateway
)) {
3231 FREE(parsed_parameters
, M_NECP
);
3236 if ((parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_LISTENER
) &&
3237 parsed_parameters
->required_interface_index
!= IFSCOPE_NONE
&&
3238 (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF
) == 0) {
3239 // Listener should not apply required interface index if
3240 parsed_parameters
->required_interface_index
= IFSCOPE_NONE
;
3243 // Save the last policy id on the client
3244 client
->policy_id
= result
.policy_id
;
3246 if ((parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_MULTIPATH
) ||
3247 ((parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_LISTENER
) &&
3248 result
.routing_result
!= NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED
)) {
3249 client
->allow_multiple_flows
= TRUE
;
3251 client
->allow_multiple_flows
= FALSE
;
3254 // If the original request was scoped, and the policy result matches, make sure the result is scoped
3255 if ((result
.routing_result
== NECP_KERNEL_POLICY_RESULT_NONE
||
3256 result
.routing_result
== NECP_KERNEL_POLICY_RESULT_PASS
) &&
3257 result
.routed_interface_index
!= IFSCOPE_NONE
&&
3258 parsed_parameters
->required_interface_index
== result
.routed_interface_index
) {
3259 result
.routing_result
= NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED
;
3260 result
.routing_result_parameter
.scoped_interface_index
= result
.routed_interface_index
;
3263 if (defunct_list
!= NULL
&&
3264 result
.routing_result
== NECP_KERNEL_POLICY_RESULT_DROP
) {
3265 // If we are forced to drop the client, defunct it if it has flows
3266 necp_defunct_client_for_policy(client
, defunct_list
);
3269 // Recalculate flags
3270 if (parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_LISTENER
) {
3271 // Listeners are valid as long as they aren't dropped
3272 if (result
.routing_result
!= NECP_KERNEL_POLICY_RESULT_DROP
) {
3273 flags
|= NECP_CLIENT_RESULT_FLAG_SATISFIED
;
3275 } else if (result
.routed_interface_index
!= 0) {
3276 // Clients without flows determine viability based on having some routable interface
3277 flags
|= NECP_CLIENT_RESULT_FLAG_SATISFIED
;
3280 bool updated
= FALSE
;
3281 u_int8_t
*cursor
= client
->result
;
3282 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_FLAGS
, sizeof(flags
), &flags
, &updated
, client
->result
, sizeof(client
->result
));
3284 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_REASON
, sizeof(reason
), &reason
, &updated
, client
->result
, sizeof(client
->result
));
3286 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_CLIENT_ID
, sizeof(uuid_t
), client
->client_id
, &updated
,
3287 client
->result
, sizeof(client
->result
));
3288 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_POLICY_RESULT
, sizeof(result
.routing_result
), &result
.routing_result
, &updated
,
3289 client
->result
, sizeof(client
->result
));
3290 if (result
.routing_result_parameter
.tunnel_interface_index
!= 0) {
3291 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_POLICY_RESULT_PARAMETER
,
3292 sizeof(result
.routing_result_parameter
), &result
.routing_result_parameter
, &updated
,
3293 client
->result
, sizeof(client
->result
));
3295 if (result
.filter_control_unit
!= 0) {
3296 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_FILTER_CONTROL_UNIT
,
3297 sizeof(result
.filter_control_unit
), &result
.filter_control_unit
, &updated
,
3298 client
->result
, sizeof(client
->result
));
3300 if (result
.routed_interface_index
!= 0) {
3301 u_int routed_interface_index
= result
.routed_interface_index
;
3302 if (result
.routing_result
== NECP_KERNEL_POLICY_RESULT_IP_TUNNEL
&&
3303 (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_REQUIRED_FIELDS
) &&
3304 parsed_parameters
->required_interface_index
!= IFSCOPE_NONE
&&
3305 parsed_parameters
->required_interface_index
!= result
.routed_interface_index
) {
3306 routed_interface_index
= parsed_parameters
->required_interface_index
;
3309 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_INTERFACE_INDEX
,
3310 sizeof(routed_interface_index
), &routed_interface_index
, &updated
,
3311 client
->result
, sizeof(client
->result
));
3313 if (client_fd
&& client_fd
->flags
& NECP_OPEN_FLAG_BACKGROUND
) {
3314 u_int32_t effective_traffic_class
= SO_TC_BK_SYS
;
3315 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_EFFECTIVE_TRAFFIC_CLASS
,
3316 sizeof(effective_traffic_class
), &effective_traffic_class
, &updated
,
3317 client
->result
, sizeof(client
->result
));
3320 if (client_fd
->background
) {
3321 bool has_assigned_flow
= FALSE
;
3322 struct necp_client_flow_registration
*flow_registration
= NULL
;
3323 struct necp_client_flow
*search_flow
= NULL
;
3324 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
3325 LIST_FOREACH(search_flow
, &flow_registration
->flow_list
, flow_chain
) {
3326 if (search_flow
->assigned
) {
3327 has_assigned_flow
= TRUE
;
3333 if (has_assigned_flow
) {
3334 u_int32_t background
= client_fd
->background
;
3335 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_TRAFFIC_MGMT_BG
,
3336 sizeof(background
), &background
, &updated
,
3337 client
->result
, sizeof(client
->result
));
3341 bool write_v4_gateway
= !necp_client_endpoint_is_unspecified(&v4_gateway
);
3342 bool write_v6_gateway
= !necp_client_endpoint_is_unspecified(&v6_gateway
);
3344 NECP_CLIENT_ROUTE_LOCK(client
);
3345 if (client
->current_route
!= NULL
) {
3346 const u_int32_t route_mtu
= get_maxmtu(client
->current_route
);
3347 if (route_mtu
!= 0) {
3348 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_EFFECTIVE_MTU
,
3349 sizeof(route_mtu
), &route_mtu
, &updated
,
3350 client
->result
, sizeof(client
->result
));
3352 bool has_remote_addr
= parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REMOTE_ADDR
;
3353 if (has_remote_addr
&& client
->current_route
->rt_gateway
!= NULL
) {
3354 if (client
->current_route
->rt_gateway
->sa_family
== AF_INET
) {
3355 write_v6_gateway
= false;
3356 } else if (client
->current_route
->rt_gateway
->sa_family
== AF_INET6
) {
3357 write_v4_gateway
= false;
3361 NECP_CLIENT_ROUTE_UNLOCK(client
);
3363 if (write_v4_gateway
) {
3364 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_GATEWAY
,
3365 sizeof(struct necp_client_endpoint
), &v4_gateway
, &updated
,
3366 client
->result
, sizeof(client
->result
));
3369 if (write_v6_gateway
) {
3370 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_GATEWAY
,
3371 sizeof(struct necp_client_endpoint
), &v6_gateway
, &updated
,
3372 client
->result
, sizeof(client
->result
));
3375 if (result
.mss_recommended
!= 0) {
3376 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_RECOMMENDED_MSS
,
3377 sizeof(result
.mss_recommended
), &result
.mss_recommended
, &updated
,
3378 client
->result
, sizeof(client
->result
));
3381 for (int i
= 0; i
< NECP_MAX_NETAGENTS
; i
++) {
3382 if (uuid_is_null(result
.netagents
[i
])) {
3385 uuid_copy(netagent
.netagent_uuid
, result
.netagents
[i
]);
3386 netagent
.generation
= netagent_get_generation(netagent
.netagent_uuid
);
3387 if (necp_netagent_applies_to_client(client
, parsed_parameters
, &netagent
.netagent_uuid
, TRUE
, 0, 0)) {
3388 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_NETAGENT
, sizeof(netagent
), &netagent
, &updated
,
3389 client
->result
, sizeof(client
->result
));
3393 ifnet_head_lock_shared();
3394 ifnet_t direct_interface
= NULL
;
3395 ifnet_t delegate_interface
= NULL
;
3396 ifnet_t original_scoped_interface
= NULL
;
3398 if (result
.routed_interface_index
!= IFSCOPE_NONE
&& result
.routed_interface_index
<= (u_int32_t
)if_index
) {
3399 direct_interface
= ifindex2ifnet
[result
.routed_interface_index
];
3400 } else if (parsed_parameters
->required_interface_index
!= IFSCOPE_NONE
&&
3401 parsed_parameters
->required_interface_index
<= (u_int32_t
)if_index
) {
3402 // If the request was scoped, but the route didn't match, still grab the agents
3403 direct_interface
= ifindex2ifnet
[parsed_parameters
->required_interface_index
];
3404 } else if (result
.routed_interface_index
== IFSCOPE_NONE
&&
3405 result
.routing_result
== NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED
&&
3406 result
.routing_result_parameter
.scoped_interface_index
!= IFSCOPE_NONE
) {
3407 direct_interface
= ifindex2ifnet
[result
.routing_result_parameter
.scoped_interface_index
];
3409 if (direct_interface
!= NULL
) {
3410 delegate_interface
= direct_interface
->if_delegated
.ifp
;
3412 if (result
.routing_result
== NECP_KERNEL_POLICY_RESULT_IP_TUNNEL
&&
3413 (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_REQUIRED_FIELDS
) &&
3414 parsed_parameters
->required_interface_index
!= IFSCOPE_NONE
&&
3415 parsed_parameters
->required_interface_index
!= result
.routing_result_parameter
.tunnel_interface_index
&&
3416 parsed_parameters
->required_interface_index
<= (u_int32_t
)if_index
) {
3417 original_scoped_interface
= ifindex2ifnet
[parsed_parameters
->required_interface_index
];
3420 if (original_scoped_interface
!= NULL
) {
3421 struct necp_client_result_interface interface_struct
;
3422 interface_struct
.index
= original_scoped_interface
->if_index
;
3423 interface_struct
.generation
= ifnet_get_generation(original_scoped_interface
);
3424 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_INTERFACE
, sizeof(interface_struct
), &interface_struct
, &updated
,
3425 client
->result
, sizeof(client
->result
));
3427 if (direct_interface
!= NULL
) {
3428 struct necp_client_result_interface interface_struct
;
3429 interface_struct
.index
= direct_interface
->if_index
;
3430 interface_struct
.generation
= ifnet_get_generation(direct_interface
);
3431 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_INTERFACE
, sizeof(interface_struct
), &interface_struct
, &updated
,
3432 client
->result
, sizeof(client
->result
));
3434 // Set the delta time since interface up/down
3435 struct timeval updown_delta
= {};
3436 if (ifnet_updown_delta(direct_interface
, &updown_delta
) == 0) {
3437 u_int32_t delta
= updown_delta
.tv_sec
;
3438 bool ignore_updated
= FALSE
;
3439 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_INTERFACE_TIME_DELTA
,
3440 sizeof(delta
), &delta
, &ignore_updated
,
3441 client
->result
, sizeof(client
->result
));
3444 if (delegate_interface
!= NULL
) {
3445 struct necp_client_result_interface interface_struct
;
3446 interface_struct
.index
= delegate_interface
->if_index
;
3447 interface_struct
.generation
= ifnet_get_generation(delegate_interface
);
3448 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_INTERFACE
, sizeof(interface_struct
), &interface_struct
, &updated
,
3449 client
->result
, sizeof(client
->result
));
3452 // Update multipath/listener interface flows
3453 if (parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_MULTIPATH
) {
3454 // Get multipath interface options from ordered list
3455 struct ifnet
*multi_interface
= NULL
;
3456 TAILQ_FOREACH(multi_interface
, &ifnet_ordered_head
, if_ordered_link
) {
3457 if (necp_ifnet_matches_parameters(multi_interface
, parsed_parameters
, 0, NULL
, true, false)) {
3458 // Add multipath interface flows for kernel MPTCP
3459 necp_client_add_interface_option_if_needed(client
, multi_interface
->if_index
,
3460 ifnet_get_generation(multi_interface
), NULL
);
3462 // Add nexus agents for multipath
3463 necp_client_add_agent_interface_options(client
, parsed_parameters
, multi_interface
);
3466 } else if (parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_LISTENER
) {
3467 if (result
.routing_result
== NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED
) {
3468 if (direct_interface
!= NULL
) {
3469 // If scoped, only listen on that interface
3470 // Add nexus agents for listeners
3471 necp_client_add_agent_interface_options(client
, parsed_parameters
, direct_interface
);
3473 // Add interface option in case it is not a nexus
3474 necp_client_add_interface_option_if_needed(client
, direct_interface
->if_index
,
3475 ifnet_get_generation(direct_interface
), NULL
);
3478 // Get listener interface options from global list
3479 struct ifnet
*listen_interface
= NULL
;
3480 TAILQ_FOREACH(listen_interface
, &ifnet_head
, if_link
) {
3481 if ((listen_interface
->if_flags
& (IFF_UP
| IFF_RUNNING
)) &&
3482 necp_ifnet_matches_parameters(listen_interface
, parsed_parameters
, 0, NULL
, true, false)) {
3483 // Add nexus agents for listeners
3484 necp_client_add_agent_interface_options(client
, parsed_parameters
, listen_interface
);
3491 if (original_scoped_interface
!= NULL
) {
3492 ifnet_lock_shared(original_scoped_interface
);
3493 if (original_scoped_interface
->if_agentids
!= NULL
) {
3494 for (u_int32_t i
= 0; i
< original_scoped_interface
->if_agentcount
; i
++) {
3495 if (uuid_is_null(original_scoped_interface
->if_agentids
[i
])) {
3498 uuid_copy(netagent
.netagent_uuid
, original_scoped_interface
->if_agentids
[i
]);
3499 netagent
.generation
= netagent_get_generation(netagent
.netagent_uuid
);
3500 if (necp_netagent_applies_to_client(client
, parsed_parameters
, &netagent
.netagent_uuid
, FALSE
,
3501 original_scoped_interface
->if_index
, ifnet_get_generation(original_scoped_interface
))) {
3502 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_NETAGENT
, sizeof(netagent
), &netagent
, &updated
,
3503 client
->result
, sizeof(client
->result
));
3507 ifnet_lock_done(original_scoped_interface
);
3509 if (direct_interface
!= NULL
) {
3510 ifnet_lock_shared(direct_interface
);
3511 if (direct_interface
->if_agentids
!= NULL
) {
3512 for (u_int32_t i
= 0; i
< direct_interface
->if_agentcount
; i
++) {
3513 if (uuid_is_null(direct_interface
->if_agentids
[i
])) {
3516 uuid_copy(netagent
.netagent_uuid
, direct_interface
->if_agentids
[i
]);
3517 netagent
.generation
= netagent_get_generation(netagent
.netagent_uuid
);
3518 if (necp_netagent_applies_to_client(client
, parsed_parameters
, &netagent
.netagent_uuid
, TRUE
,
3519 direct_interface
->if_index
, ifnet_get_generation(direct_interface
))) {
3520 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_NETAGENT
, sizeof(netagent
), &netagent
, &updated
,
3521 client
->result
, sizeof(client
->result
));
3525 ifnet_lock_done(direct_interface
);
3527 if (delegate_interface
!= NULL
) {
3528 ifnet_lock_shared(delegate_interface
);
3529 if (delegate_interface
->if_agentids
!= NULL
) {
3530 for (u_int32_t i
= 0; i
< delegate_interface
->if_agentcount
; i
++) {
3531 if (uuid_is_null(delegate_interface
->if_agentids
[i
])) {
3534 uuid_copy(netagent
.netagent_uuid
, delegate_interface
->if_agentids
[i
]);
3535 netagent
.generation
= netagent_get_generation(netagent
.netagent_uuid
);
3536 if (necp_netagent_applies_to_client(client
, parsed_parameters
, &netagent
.netagent_uuid
, FALSE
,
3537 delegate_interface
->if_index
, ifnet_get_generation(delegate_interface
))) {
3538 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_NETAGENT
, sizeof(netagent
), &netagent
, &updated
,
3539 client
->result
, sizeof(client
->result
));
3543 ifnet_lock_done(delegate_interface
);
3547 // Add interface options
3548 for (u_int32_t option_i
= 0; option_i
< client
->interface_option_count
; option_i
++) {
3549 if (option_i
< NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
) {
3550 struct necp_client_interface_option
*option
= &client
->interface_options
[option_i
];
3551 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_INTERFACE_OPTION
, sizeof(*option
), option
, &updated
,
3552 client
->result
, sizeof(client
->result
));
3554 struct necp_client_interface_option
*option
= &client
->extra_interface_options
[option_i
- NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
];
3555 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_INTERFACE_OPTION
, sizeof(*option
), option
, &updated
,
3556 client
->result
, sizeof(client
->result
));
3560 size_t new_result_length
= (cursor
- client
->result
);
3561 if (new_result_length
!= client
->result_length
) {
3562 client
->result_length
= new_result_length
;
3566 // Update flow viability/flags
3567 if (necp_client_update_flows(proc
, client
, defunct_list
)) {
3572 client
->result_read
= FALSE
;
3573 necp_client_update_observer_update(client
);
3576 FREE(parsed_parameters
, M_NECP
);
3581 necp_defunct_client_fd_locked(struct necp_fd_data
*client_fd
, struct _necp_flow_defunct_list
*defunct_list
, struct proc
*proc
)
3583 #pragma unused(proc)
3584 bool updated_result
= FALSE
;
3585 struct necp_client
*client
= NULL
;
3587 NECP_FD_ASSERT_LOCKED(client_fd
);
3589 RB_FOREACH(client
, _necp_client_tree
, &client_fd
->clients
) {
3590 struct necp_client_flow_registration
*flow_registration
= NULL
;
3592 NECP_CLIENT_LOCK(client
);
3594 // Prepare close events to be sent to the nexus to effectively remove the flows
3595 struct necp_client_flow
*search_flow
= NULL
;
3596 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
3597 LIST_FOREACH(search_flow
, &flow_registration
->flow_list
, flow_chain
) {
3598 if (search_flow
->nexus
&&
3599 !uuid_is_null(search_flow
->u
.nexus_agent
)) {
3600 // Sleeping alloc won't fail; copy only what's necessary
3601 struct necp_flow_defunct
*flow_defunct
= _MALLOC(sizeof(struct necp_flow_defunct
), M_NECP
, M_WAITOK
| M_ZERO
);
3602 uuid_copy(flow_defunct
->nexus_agent
, search_flow
->u
.nexus_agent
);
3603 uuid_copy(flow_defunct
->flow_id
, ((flow_registration
->flags
& NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID
) ?
3605 flow_registration
->registration_id
));
3606 flow_defunct
->proc_pid
= client
->proc_pid
;
3607 flow_defunct
->agent_handle
= client
->agent_handle
;
3608 flow_defunct
->flags
= flow_registration
->flags
;
3609 // Add to the list provided by caller
3610 LIST_INSERT_HEAD(defunct_list
, flow_defunct
, chain
);
3612 flow_registration
->defunct
= true;
3613 flow_registration
->flow_result_read
= false;
3614 updated_result
= true;
3618 NECP_CLIENT_UNLOCK(client
);
3622 if (updated_result
) {
3623 necp_fd_notify(client_fd
, true);
3628 necp_update_client_fd_locked(struct necp_fd_data
*client_fd
,
3630 struct _necp_flow_defunct_list
*defunct_list
)
3632 struct necp_client
*client
= NULL
;
3633 bool updated_result
= FALSE
;
3634 NECP_FD_ASSERT_LOCKED(client_fd
);
3635 RB_FOREACH(client
, _necp_client_tree
, &client_fd
->clients
) {
3636 NECP_CLIENT_LOCK(client
);
3637 if (necp_update_client_result(proc
, client_fd
, client
, defunct_list
)) {
3638 updated_result
= TRUE
;
3640 NECP_CLIENT_UNLOCK(client
);
3642 if (updated_result
) {
3643 necp_fd_notify(client_fd
, true);
3649 necp_update_all_clients_callout(__unused thread_call_param_t dummy
,
3650 __unused thread_call_param_t arg
)
3652 struct necp_fd_data
*client_fd
= NULL
;
3654 struct _necp_flow_defunct_list defunct_list
;
3655 LIST_INIT(&defunct_list
);
3657 NECP_FD_LIST_LOCK_SHARED();
3659 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
3660 proc_t proc
= proc_find(client_fd
->proc_pid
);
3661 if (proc
== PROC_NULL
) {
3665 // Update all clients on one fd
3666 NECP_FD_LOCK(client_fd
);
3667 necp_update_client_fd_locked(client_fd
, proc
, &defunct_list
);
3668 NECP_FD_UNLOCK(client_fd
);
3674 NECP_FD_LIST_UNLOCK();
3676 // Handle the case in which some clients became newly defunct
3677 if (!LIST_EMPTY(&defunct_list
)) {
3678 struct necp_flow_defunct
*flow_defunct
= NULL
;
3679 struct necp_flow_defunct
*temp_flow_defunct
= NULL
;
3681 // For each newly defunct client, send a message to the nexus to remove the flow
3682 LIST_FOREACH_SAFE(flow_defunct
, &defunct_list
, chain
, temp_flow_defunct
) {
3683 if (!uuid_is_null(flow_defunct
->nexus_agent
)) {
3684 u_int8_t message_type
= NETAGENT_MESSAGE_TYPE_ABORT_NEXUS
;
3685 if (((flow_defunct
->flags
& NECP_CLIENT_FLOW_FLAGS_BROWSE
) ||
3686 (flow_defunct
->flags
& NECP_CLIENT_FLOW_FLAGS_RESOLVE
)) &&
3687 !(flow_defunct
->flags
& NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS
)) {
3688 message_type
= NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT
;
3690 int netagent_error
= netagent_client_message_with_params(flow_defunct
->nexus_agent
,
3691 flow_defunct
->flow_id
,
3692 flow_defunct
->proc_pid
,
3693 flow_defunct
->agent_handle
,
3695 flow_defunct
->has_close_parameters
? &flow_defunct
->close_parameters
: NULL
,
3697 if (netagent_error
!= 0) {
3698 char namebuf
[MAXCOMLEN
+ 1];
3699 (void) strlcpy(namebuf
, "unknown", sizeof(namebuf
));
3700 proc_name(flow_defunct
->proc_pid
, namebuf
, sizeof(namebuf
));
3701 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
);
3704 LIST_REMOVE(flow_defunct
, chain
);
3705 FREE(flow_defunct
, M_NECP
);
3708 ASSERT(LIST_EMPTY(&defunct_list
));
3712 necp_update_all_clients(void)
3714 necp_update_all_clients_immediately_if_needed(false);
3718 necp_update_all_clients_immediately_if_needed(bool should_update_immediately
)
3720 if (necp_client_update_tcall
== NULL
) {
3721 // Don't try to update clients if the module is not initialized
3725 uint64_t deadline
= 0;
3726 uint64_t leeway
= 0;
3728 uint32_t timeout_to_use
= necp_timeout_microseconds
;
3729 uint32_t leeway_to_use
= necp_timeout_leeway_microseconds
;
3730 if (should_update_immediately
) {
3731 timeout_to_use
= 1000 * 10; // 10ms
3732 leeway_to_use
= 1000 * 10; // 10ms;
3735 clock_interval_to_deadline(timeout_to_use
, NSEC_PER_USEC
, &deadline
);
3736 clock_interval_to_absolutetime_interval(leeway_to_use
, NSEC_PER_USEC
, &leeway
);
3738 thread_call_enter_delayed_with_leeway(necp_client_update_tcall
, NULL
,
3739 deadline
, leeway
, THREAD_CALL_DELAY_LEEWAY
);
3743 necp_set_client_as_background(proc_t proc
,
3744 struct fileproc
*fp
,
3747 if (proc
== PROC_NULL
) {
3748 NECPLOG0(LOG_ERR
, "NULL proc");
3753 NECPLOG0(LOG_ERR
, "NULL fp");
3757 struct necp_fd_data
*client_fd
= (struct necp_fd_data
*)fp
->f_fglob
->fg_data
;
3758 if (client_fd
== NULL
) {
3759 NECPLOG0(LOG_ERR
, "Could not find client structure for backgrounded client");
3763 if (client_fd
->necp_fd_type
!= necp_fd_type_client
) {
3764 // Not a client fd, ignore
3765 NECPLOG0(LOG_ERR
, "Not a client fd, ignore");
3769 client_fd
->background
= background
;
3775 necp_fd_memstatus(proc_t proc
, uint32_t status
,
3776 struct necp_fd_data
*client_fd
)
3778 #pragma unused(proc, status, client_fd)
3779 ASSERT(proc
!= PROC_NULL
);
3780 ASSERT(client_fd
!= NULL
);
3782 // Nothing to reap for the process or client for now,
3783 // but this is where we would trigger that in future.
3787 necp_fd_defunct(proc_t proc
, struct necp_fd_data
*client_fd
)
3789 struct _necp_flow_defunct_list defunct_list
;
3791 ASSERT(proc
!= PROC_NULL
);
3792 ASSERT(client_fd
!= NULL
);
3794 if (client_fd
->necp_fd_type
!= necp_fd_type_client
) {
3795 // Not a client fd, ignore
3799 // Our local temporary list
3800 LIST_INIT(&defunct_list
);
3802 // Need to hold lock so ntstats defunct the same set of clients
3803 NECP_FD_LOCK(client_fd
);
3804 necp_defunct_client_fd_locked(client_fd
, &defunct_list
, proc
);
3805 NECP_FD_UNLOCK(client_fd
);
3807 if (!LIST_EMPTY(&defunct_list
)) {
3808 struct necp_flow_defunct
*flow_defunct
= NULL
;
3809 struct necp_flow_defunct
*temp_flow_defunct
= NULL
;
3811 // For each defunct client, remove flow from the nexus
3812 LIST_FOREACH_SAFE(flow_defunct
, &defunct_list
, chain
, temp_flow_defunct
) {
3813 if (!uuid_is_null(flow_defunct
->nexus_agent
)) {
3814 u_int8_t message_type
= NETAGENT_MESSAGE_TYPE_ABORT_NEXUS
;
3815 if (((flow_defunct
->flags
& NECP_CLIENT_FLOW_FLAGS_BROWSE
) ||
3816 (flow_defunct
->flags
& NECP_CLIENT_FLOW_FLAGS_RESOLVE
)) &&
3817 !(flow_defunct
->flags
& NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS
)) {
3818 message_type
= NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT
;
3820 int netagent_error
= netagent_client_message_with_params(flow_defunct
->nexus_agent
,
3821 flow_defunct
->flow_id
,
3822 flow_defunct
->proc_pid
,
3823 flow_defunct
->agent_handle
,
3825 flow_defunct
->has_close_parameters
? &flow_defunct
->close_parameters
: NULL
,
3827 if (netagent_error
!= 0) {
3828 NECPLOG((netagent_error
== ENOENT
? LOG_DEBUG
: LOG_ERR
), "necp_defunct_client abort nexus error (%d)", netagent_error
);
3831 LIST_REMOVE(flow_defunct
, chain
);
3832 FREE(flow_defunct
, M_NECP
);
3835 ASSERT(LIST_EMPTY(&defunct_list
));
3839 necp_client_remove_agent_from_result(struct necp_client
*client
, uuid_t netagent_uuid
)
3843 u_int8_t
*result_buffer
= client
->result
;
3844 while ((offset
+ sizeof(struct necp_tlv_header
)) <= client
->result_length
) {
3845 u_int8_t type
= necp_buffer_get_tlv_type(result_buffer
, offset
);
3846 u_int32_t length
= necp_buffer_get_tlv_length(result_buffer
, offset
);
3848 size_t tlv_total_length
= (sizeof(struct necp_tlv_header
) + length
);
3849 if (type
== NECP_CLIENT_RESULT_NETAGENT
&&
3850 length
== sizeof(struct necp_client_result_netagent
) &&
3851 (offset
+ tlv_total_length
) <= client
->result_length
) {
3852 struct necp_client_result_netagent
*value
= ((struct necp_client_result_netagent
*)(void *)
3853 necp_buffer_get_tlv_value(result_buffer
, offset
, NULL
));
3854 if (uuid_compare(value
->netagent_uuid
, netagent_uuid
) == 0) {
3855 // Found a netagent to remove
3856 // Shift bytes down to remove the tlv, and adjust total length
3857 // Don't adjust the current offset
3858 memmove(result_buffer
+ offset
,
3859 result_buffer
+ offset
+ tlv_total_length
,
3860 client
->result_length
- (offset
+ tlv_total_length
));
3861 client
->result_length
-= tlv_total_length
;
3862 memset(result_buffer
+ client
->result_length
, 0, sizeof(client
->result
) - client
->result_length
);
3867 offset
+= tlv_total_length
;
3872 necp_force_update_client(uuid_t client_id
, uuid_t remove_netagent_uuid
, u_int32_t agent_generation
)
3874 struct necp_fd_data
*client_fd
= NULL
;
3876 NECP_FD_LIST_LOCK_SHARED();
3878 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
3879 bool updated_result
= FALSE
;
3880 NECP_FD_LOCK(client_fd
);
3881 struct necp_client
*client
= necp_client_fd_find_client_and_lock(client_fd
, client_id
);
3882 if (client
!= NULL
) {
3883 client
->failed_trigger_agent
.generation
= agent_generation
;
3884 uuid_copy(client
->failed_trigger_agent
.netagent_uuid
, remove_netagent_uuid
);
3885 if (!uuid_is_null(remove_netagent_uuid
)) {
3886 necp_client_remove_agent_from_result(client
, remove_netagent_uuid
);
3888 client
->result_read
= FALSE
;
3889 // Found the client, break
3890 updated_result
= TRUE
;
3891 NECP_CLIENT_UNLOCK(client
);
3893 if (updated_result
) {
3894 necp_fd_notify(client_fd
, true);
3896 NECP_FD_UNLOCK(client_fd
);
3897 if (updated_result
) {
3898 // Found the client, break
3903 NECP_FD_LIST_UNLOCK();
3907 /// Interface matching
3909 #define NECP_PARSED_PARAMETERS_INTERESTING_IFNET_FIELDS (NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR | \
3910 NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IF | \
3911 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE | \
3912 NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IFTYPE | \
3913 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT | \
3914 NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT | \
3915 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT | \
3916 NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT | \
3917 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE | \
3918 NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT_TYPE | \
3919 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE | \
3920 NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE)
3922 #define NECP_PARSED_PARAMETERS_SCOPED_FIELDS (NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR | \
3923 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE | \
3924 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT | \
3925 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT | \
3926 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE | \
3927 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE)
3929 #define NECP_PARSED_PARAMETERS_SCOPED_IFNET_FIELDS (NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR | \
3930 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE)
3932 #define NECP_PARSED_PARAMETERS_PREFERRED_FIELDS (NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT | \
3933 NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT | \
3934 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE | \
3935 NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE)
3938 necp_ifnet_matches_type(struct ifnet
*ifp
, u_int8_t interface_type
, bool check_delegates
)
3940 struct ifnet
*check_ifp
= ifp
;
3942 if (if_functional_type(check_ifp
, TRUE
) == interface_type
) {
3945 if (!check_delegates
) {
3948 check_ifp
= check_ifp
->if_delegated
.ifp
;
3954 necp_ifnet_matches_name(struct ifnet
*ifp
, const char *interface_name
, bool check_delegates
)
3956 struct ifnet
*check_ifp
= ifp
;
3958 if (strncmp(check_ifp
->if_xname
, interface_name
, IFXNAMSIZ
) == 0) {
3961 if (!check_delegates
) {
3964 check_ifp
= check_ifp
->if_delegated
.ifp
;
3970 necp_ifnet_matches_agent(struct ifnet
*ifp
, uuid_t
*agent_uuid
, bool check_delegates
)
3972 struct ifnet
*check_ifp
= ifp
;
3974 while (check_ifp
!= NULL
) {
3975 ifnet_lock_shared(check_ifp
);
3976 if (check_ifp
->if_agentids
!= NULL
) {
3977 for (u_int32_t index
= 0; index
< check_ifp
->if_agentcount
; index
++) {
3978 if (uuid_compare(check_ifp
->if_agentids
[index
], *agent_uuid
) == 0) {
3979 ifnet_lock_done(check_ifp
);
3984 ifnet_lock_done(check_ifp
);
3986 if (!check_delegates
) {
3989 check_ifp
= check_ifp
->if_delegated
.ifp
;
3995 necp_ifnet_matches_agent_type(struct ifnet
*ifp
, const char *agent_domain
, const char *agent_type
, bool check_delegates
)
3997 struct ifnet
*check_ifp
= ifp
;
3999 while (check_ifp
!= NULL
) {
4000 ifnet_lock_shared(check_ifp
);
4001 if (check_ifp
->if_agentids
!= NULL
) {
4002 for (u_int32_t index
= 0; index
< check_ifp
->if_agentcount
; index
++) {
4003 if (uuid_is_null(check_ifp
->if_agentids
[index
])) {
4007 char if_agent_domain
[NETAGENT_DOMAINSIZE
] = { 0 };
4008 char if_agent_type
[NETAGENT_TYPESIZE
] = { 0 };
4010 if (netagent_get_agent_domain_and_type(check_ifp
->if_agentids
[index
], if_agent_domain
, if_agent_type
)) {
4011 if (necp_agent_types_match(agent_domain
, agent_type
, if_agent_domain
, if_agent_type
)) {
4012 ifnet_lock_done(check_ifp
);
4018 ifnet_lock_done(check_ifp
);
4020 if (!check_delegates
) {
4023 check_ifp
= check_ifp
->if_delegated
.ifp
;
4029 necp_ifnet_matches_local_address(struct ifnet
*ifp
, struct sockaddr
*sa
)
4031 struct ifaddr
*ifa
= NULL
;
4032 bool matched_local_address
= FALSE
;
4034 // Transform sa into the ifaddr form
4035 // IPv6 Scope IDs are always embedded in the ifaddr list
4036 struct sockaddr_storage address
;
4037 u_int ifscope
= IFSCOPE_NONE
;
4038 (void)sa_copy(sa
, &address
, &ifscope
);
4039 SIN(&address
)->sin_port
= 0;
4040 if (address
.ss_family
== AF_INET6
) {
4041 SIN6(&address
)->sin6_scope_id
= 0;
4044 ifa
= ifa_ifwithaddr_scoped_locked((struct sockaddr
*)&address
, ifp
->if_index
);
4045 matched_local_address
= (ifa
!= NULL
);
4048 ifaddr_release(ifa
);
4051 return matched_local_address
;
4055 necp_interface_type_is_primary_eligible(u_int8_t interface_type
)
4057 switch (interface_type
) {
4058 // These types can never be primary, so a client requesting these types is allowed
4059 // to match an interface that isn't currently eligible to be primary (has default
4061 case IFRTYPE_FUNCTIONAL_WIFI_AWDL
:
4062 case IFRTYPE_FUNCTIONAL_INTCOPROC
:
4070 #define NECP_IFP_IS_ON_ORDERED_LIST(_ifp) ((_ifp)->if_ordered_link.tqe_next != NULL || (_ifp)->if_ordered_link.tqe_prev != NULL)
4072 // Secondary interface flag indicates that the interface is being
4073 // used for multipath or a listener as an extra path
4075 necp_ifnet_matches_parameters(struct ifnet
*ifp
,
4076 struct necp_client_parsed_parameters
*parsed_parameters
,
4077 u_int32_t override_flags
,
4078 u_int32_t
*preferred_count
,
4079 bool secondary_interface
,
4080 bool require_scoped_field
)
4082 bool matched_some_scoped_field
= FALSE
;
4084 if (preferred_count
) {
4085 *preferred_count
= 0;
4088 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF
) {
4089 if (parsed_parameters
->required_interface_index
!= ifp
->if_index
) {
4094 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR
) {
4095 if (!necp_ifnet_matches_local_address(ifp
, &parsed_parameters
->local_addr
.sa
)) {
4098 if (require_scoped_field
) {
4099 matched_some_scoped_field
= TRUE
;
4103 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_FLAGS
) {
4104 if (override_flags
!= 0) {
4105 if ((override_flags
& NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_EXPENSIVE
) &&
4106 IFNET_IS_EXPENSIVE(ifp
)) {
4109 if ((override_flags
& NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_CONSTRAINED
) &&
4110 IFNET_IS_CONSTRAINED(ifp
)) {
4114 if ((parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_EXPENSIVE
) &&
4115 IFNET_IS_EXPENSIVE(ifp
)) {
4118 if ((parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_CONSTRAINED
) &&
4119 IFNET_IS_CONSTRAINED(ifp
)) {
4125 if ((!secondary_interface
|| // Enforce interface type if this is the primary interface
4126 !(parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_FLAGS
) || // or if there are no flags
4127 !(parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_ONLY_PRIMARY_REQUIRES_TYPE
)) && // or if the flags don't give an exception
4128 (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE
) &&
4129 !necp_ifnet_matches_type(ifp
, parsed_parameters
->required_interface_type
, FALSE
)) {
4133 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE
) {
4134 if (require_scoped_field
) {
4135 matched_some_scoped_field
= TRUE
;
4139 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IFTYPE
) {
4140 for (int i
= 0; i
< NECP_MAX_INTERFACE_PARAMETERS
; i
++) {
4141 if (parsed_parameters
->prohibited_interface_types
[i
] == 0) {
4145 if (necp_ifnet_matches_type(ifp
, parsed_parameters
->prohibited_interface_types
[i
], TRUE
)) {
4151 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IF
) {
4152 for (int i
= 0; i
< NECP_MAX_INTERFACE_PARAMETERS
; i
++) {
4153 if (strlen(parsed_parameters
->prohibited_interfaces
[i
]) == 0) {
4157 if (necp_ifnet_matches_name(ifp
, parsed_parameters
->prohibited_interfaces
[i
], TRUE
)) {
4163 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT
) {
4164 for (int i
= 0; i
< NECP_MAX_AGENT_PARAMETERS
; i
++) {
4165 if (uuid_is_null(parsed_parameters
->required_netagents
[i
])) {
4169 if (!necp_ifnet_matches_agent(ifp
, &parsed_parameters
->required_netagents
[i
], FALSE
)) {
4173 if (require_scoped_field
) {
4174 matched_some_scoped_field
= TRUE
;
4179 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT
) {
4180 for (int i
= 0; i
< NECP_MAX_AGENT_PARAMETERS
; i
++) {
4181 if (uuid_is_null(parsed_parameters
->prohibited_netagents
[i
])) {
4185 if (necp_ifnet_matches_agent(ifp
, &parsed_parameters
->prohibited_netagents
[i
], TRUE
)) {
4191 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE
) {
4192 for (int i
= 0; i
< NECP_MAX_AGENT_PARAMETERS
; i
++) {
4193 if (strlen(parsed_parameters
->required_netagent_types
[i
].netagent_domain
) == 0 &&
4194 strlen(parsed_parameters
->required_netagent_types
[i
].netagent_type
) == 0) {
4198 if (!necp_ifnet_matches_agent_type(ifp
, parsed_parameters
->required_netagent_types
[i
].netagent_domain
, parsed_parameters
->required_netagent_types
[i
].netagent_type
, FALSE
)) {
4202 if (require_scoped_field
) {
4203 matched_some_scoped_field
= TRUE
;
4208 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT_TYPE
) {
4209 for (int i
= 0; i
< NECP_MAX_AGENT_PARAMETERS
; i
++) {
4210 if (strlen(parsed_parameters
->prohibited_netagent_types
[i
].netagent_domain
) == 0 &&
4211 strlen(parsed_parameters
->prohibited_netagent_types
[i
].netagent_type
) == 0) {
4215 if (necp_ifnet_matches_agent_type(ifp
, parsed_parameters
->prohibited_netagent_types
[i
].netagent_domain
, parsed_parameters
->prohibited_netagent_types
[i
].netagent_type
, TRUE
)) {
4221 // Checked preferred properties
4222 if (preferred_count
) {
4223 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT
) {
4224 for (int i
= 0; i
< NECP_MAX_AGENT_PARAMETERS
; i
++) {
4225 if (uuid_is_null(parsed_parameters
->preferred_netagents
[i
])) {
4229 if (necp_ifnet_matches_agent(ifp
, &parsed_parameters
->preferred_netagents
[i
], TRUE
)) {
4230 (*preferred_count
)++;
4231 if (require_scoped_field
) {
4232 matched_some_scoped_field
= TRUE
;
4238 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE
) {
4239 for (int i
= 0; i
< NECP_MAX_AGENT_PARAMETERS
; i
++) {
4240 if (strlen(parsed_parameters
->preferred_netagent_types
[i
].netagent_domain
) == 0 &&
4241 strlen(parsed_parameters
->preferred_netagent_types
[i
].netagent_type
) == 0) {
4245 if (necp_ifnet_matches_agent_type(ifp
, parsed_parameters
->preferred_netagent_types
[i
].netagent_domain
, parsed_parameters
->preferred_netagent_types
[i
].netagent_type
, TRUE
)) {
4246 (*preferred_count
)++;
4247 if (require_scoped_field
) {
4248 matched_some_scoped_field
= TRUE
;
4254 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT
) {
4255 for (int i
= 0; i
< NECP_MAX_AGENT_PARAMETERS
; i
++) {
4256 if (uuid_is_null(parsed_parameters
->avoided_netagents
[i
])) {
4260 if (!necp_ifnet_matches_agent(ifp
, &parsed_parameters
->avoided_netagents
[i
], TRUE
)) {
4261 (*preferred_count
)++;
4266 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE
) {
4267 for (int i
= 0; i
< NECP_MAX_AGENT_PARAMETERS
; i
++) {
4268 if (strlen(parsed_parameters
->avoided_netagent_types
[i
].netagent_domain
) == 0 &&
4269 strlen(parsed_parameters
->avoided_netagent_types
[i
].netagent_type
) == 0) {
4273 if (!necp_ifnet_matches_agent_type(ifp
, parsed_parameters
->avoided_netagent_types
[i
].netagent_domain
,
4274 parsed_parameters
->avoided_netagent_types
[i
].netagent_type
, TRUE
)) {
4275 (*preferred_count
)++;
4281 if (require_scoped_field
) {
4282 return matched_some_scoped_field
;
4289 necp_find_matching_interface_index(struct necp_client_parsed_parameters
*parsed_parameters
,
4290 u_int
*return_ifindex
, bool *validate_agents
)
4292 struct ifnet
*ifp
= NULL
;
4293 u_int32_t best_preferred_count
= 0;
4294 bool has_preferred_fields
= FALSE
;
4295 *return_ifindex
= 0;
4297 if (parsed_parameters
->required_interface_index
!= 0) {
4298 *return_ifindex
= parsed_parameters
->required_interface_index
;
4302 // Check and save off flags
4303 u_int32_t flags
= 0;
4304 bool has_prohibit_flags
= FALSE
;
4305 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_FLAGS
) {
4306 flags
= parsed_parameters
->flags
;
4307 has_prohibit_flags
= (parsed_parameters
->flags
&
4308 (NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_EXPENSIVE
|
4309 NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_CONSTRAINED
));
4312 if (!(parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_INTERESTING_IFNET_FIELDS
) &&
4313 !has_prohibit_flags
) {
4317 has_preferred_fields
= (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_PREFERRED_FIELDS
);
4319 // We have interesting parameters to parse and find a matching interface
4320 ifnet_head_lock_shared();
4322 if (!(parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_SCOPED_FIELDS
) &&
4323 !has_preferred_fields
) {
4324 // We do have fields to match, but they are only prohibitory
4325 // If the first interface in the list matches, or there are no ordered interfaces, we don't need to scope
4326 ifp
= TAILQ_FIRST(&ifnet_ordered_head
);
4327 if (ifp
== NULL
|| necp_ifnet_matches_parameters(ifp
, parsed_parameters
, 0, NULL
, false, false)) {
4328 // Don't set return_ifindex, so the client doesn't need to scope
4334 // First check the ordered interface list
4335 TAILQ_FOREACH(ifp
, &ifnet_ordered_head
, if_ordered_link
) {
4336 u_int32_t preferred_count
= 0;
4337 if (necp_ifnet_matches_parameters(ifp
, parsed_parameters
, flags
, &preferred_count
, false, false)) {
4338 if (preferred_count
> best_preferred_count
||
4339 *return_ifindex
== 0) {
4340 // Everything matched, and is most preferred. Return this interface.
4341 *return_ifindex
= ifp
->if_index
;
4342 best_preferred_count
= preferred_count
;
4344 if (!has_preferred_fields
) {
4350 if (has_prohibit_flags
&&
4351 ifp
== TAILQ_FIRST(&ifnet_ordered_head
)) {
4352 // This was the first interface. From here on, if the
4353 // client prohibited either expensive or constrained,
4354 // don't allow either as a secondary interface option.
4355 flags
|= (NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_EXPENSIVE
|
4356 NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_CONSTRAINED
);
4360 bool is_listener
= ((parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_FLAGS
) &&
4361 (parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_LISTENER
));
4363 // Then check the remaining interfaces
4364 if ((parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_SCOPED_FIELDS
) &&
4365 ((!(parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE
)) ||
4366 !necp_interface_type_is_primary_eligible(parsed_parameters
->required_interface_type
) ||
4367 (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR
) ||
4369 (*return_ifindex
== 0 || has_preferred_fields
)) {
4370 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
4371 u_int32_t preferred_count
= 0;
4372 if (NECP_IFP_IS_ON_ORDERED_LIST(ifp
)) {
4373 // This interface was in the ordered list, skip
4376 if (necp_ifnet_matches_parameters(ifp
, parsed_parameters
, flags
, &preferred_count
, false, true)) {
4377 if (preferred_count
> best_preferred_count
||
4378 *return_ifindex
== 0) {
4379 // Everything matched, and is most preferred. Return this interface.
4380 *return_ifindex
= ifp
->if_index
;
4381 best_preferred_count
= preferred_count
;
4383 if (!has_preferred_fields
) {
4393 if ((parsed_parameters
->valid_fields
== (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_PREFERRED_FIELDS
)) &&
4394 best_preferred_count
== 0) {
4395 // If only has preferred fields, and nothing was found, clear the interface index and return TRUE
4396 *return_ifindex
= 0;
4400 if (*return_ifindex
== 0 &&
4401 !(parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_SCOPED_IFNET_FIELDS
)) {
4402 // Has required fields, but not including specific interface fields. Pass for now, and check
4403 // to see if agents are satisfied by policy.
4404 *validate_agents
= TRUE
;
4408 return *return_ifindex
!= 0;
4413 necp_skywalk_priv_check_cred(proc_t p
, kauth_cred_t cred
)
4415 #pragma unused(p, cred)
4422 necp_open(struct proc
*p
, struct necp_open_args
*uap
, int *retval
)
4424 #pragma unused(retval)
4426 struct necp_fd_data
*fd_data
= NULL
;
4427 struct fileproc
*fp
= NULL
;
4430 if (uap
->flags
& NECP_OPEN_FLAG_OBSERVER
||
4431 uap
->flags
& NECP_OPEN_FLAG_PUSH_OBSERVER
) {
4432 if (necp_skywalk_priv_check_cred(p
, kauth_cred_get()) != 0 &&
4433 priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_NETWORK_STATISTICS
, 0) != 0) {
4434 NECPLOG0(LOG_ERR
, "Client does not hold necessary entitlement to observe other NECP clients");
4440 error
= falloc(p
, &fp
, &fd
, vfs_context_current());
4445 if ((fd_data
= zalloc(necp_client_fd_zone
)) == NULL
) {
4450 memset(fd_data
, 0, sizeof(*fd_data
));
4452 fd_data
->necp_fd_type
= necp_fd_type_client
;
4453 fd_data
->flags
= uap
->flags
;
4454 RB_INIT(&fd_data
->clients
);
4455 RB_INIT(&fd_data
->flows
);
4456 TAILQ_INIT(&fd_data
->update_list
);
4457 lck_mtx_init(&fd_data
->fd_lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
4458 klist_init(&fd_data
->si
.si_note
);
4459 fd_data
->proc_pid
= proc_pid(p
);
4461 fp
->f_fglob
->fg_flag
= FREAD
;
4462 fp
->f_fglob
->fg_ops
= &necp_fd_ops
;
4463 fp
->f_fglob
->fg_data
= fd_data
;
4467 *fdflags(p
, fd
) |= (UF_EXCLOSE
| UF_FORKCLOSE
);
4468 procfdtbl_releasefd(p
, fd
, NULL
);
4469 fp_drop(p
, fd
, fp
, 1);
4473 if (fd_data
->flags
& NECP_OPEN_FLAG_PUSH_OBSERVER
) {
4474 NECP_OBSERVER_LIST_LOCK_EXCLUSIVE();
4475 LIST_INSERT_HEAD(&necp_fd_observer_list
, fd_data
, chain
);
4476 OSIncrementAtomic(&necp_observer_fd_count
);
4477 NECP_OBSERVER_LIST_UNLOCK();
4479 // Walk all existing clients and add them
4480 NECP_CLIENT_TREE_LOCK_SHARED();
4481 struct necp_client
*existing_client
= NULL
;
4482 RB_FOREACH(existing_client
, _necp_client_global_tree
, &necp_client_global_tree
) {
4483 NECP_CLIENT_LOCK(existing_client
);
4484 necp_client_update_observer_add_internal(fd_data
, existing_client
);
4485 necp_client_update_observer_update_internal(fd_data
, existing_client
);
4486 NECP_CLIENT_UNLOCK(existing_client
);
4488 NECP_CLIENT_TREE_UNLOCK();
4490 NECP_FD_LIST_LOCK_EXCLUSIVE();
4491 LIST_INSERT_HEAD(&necp_fd_list
, fd_data
, chain
);
4492 OSIncrementAtomic(&necp_client_fd_count
);
4493 NECP_FD_LIST_UNLOCK();
4504 if (fd_data
!= NULL
) {
4505 zfree(necp_client_fd_zone
, fd_data
);
4513 // All functions called directly from necp_client_action() to handle one of the
4514 // types should be marked with NECP_CLIENT_ACTION_FUNCTION. This ensures that
4515 // necp_client_action() does not inline all the actions into a single function.
4516 #define NECP_CLIENT_ACTION_FUNCTION __attribute__((noinline))
4518 static NECP_CLIENT_ACTION_FUNCTION
int
4519 necp_client_add(struct proc
*p
, struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
4522 struct necp_client
*client
= NULL
;
4524 if (fd_data
->flags
& NECP_OPEN_FLAG_PUSH_OBSERVER
) {
4525 NECPLOG0(LOG_ERR
, "NECP client observers with push enabled may not add their own clients");
4529 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
) ||
4530 uap
->buffer_size
== 0 || uap
->buffer_size
> NECP_MAX_CLIENT_PARAMETERS_SIZE
|| uap
->buffer
== 0) {
4534 if ((client
= _MALLOC(sizeof(struct necp_client
) + uap
->buffer_size
, M_NECP
,
4535 M_WAITOK
| M_ZERO
)) == NULL
) {
4540 error
= copyin(uap
->buffer
, client
->parameters
, uap
->buffer_size
);
4542 NECPLOG(LOG_ERR
, "necp_client_add parameters copyin error (%d)", error
);
4546 lck_mtx_init(&client
->lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
4547 lck_mtx_init(&client
->route_lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
4549 os_ref_init(&client
->reference_count
, &necp_client_refgrp
); // Hold our reference until close
4551 client
->parameters_length
= uap
->buffer_size
;
4552 client
->proc_pid
= fd_data
->proc_pid
; // Save off proc pid in case the client will persist past fd
4553 client
->agent_handle
= (void *)fd_data
;
4554 client
->platform_binary
= ((csproc_get_platform_binary(p
) == 0) ? 0 : 1);
4556 necp_generate_client_id(client
->client_id
, false);
4557 LIST_INIT(&client
->assertion_list
);
4558 RB_INIT(&client
->flow_registrations
);
4560 error
= copyout(client
->client_id
, uap
->client_id
, sizeof(uuid_t
));
4562 NECPLOG(LOG_ERR
, "necp_client_add client_id copyout error (%d)", error
);
4567 necp_client_update_observer_add(client
);
4569 NECP_FD_LOCK(fd_data
);
4570 RB_INSERT(_necp_client_tree
, &fd_data
->clients
, client
);
4571 OSIncrementAtomic(&necp_client_count
);
4572 NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
4573 RB_INSERT(_necp_client_global_tree
, &necp_client_global_tree
, client
);
4574 NECP_CLIENT_TREE_UNLOCK();
4576 // Prime the client result
4577 NECP_CLIENT_LOCK(client
);
4578 (void)necp_update_client_result(current_proc(), fd_data
, client
, NULL
);
4579 NECP_CLIENT_UNLOCK(client
);
4580 NECP_FD_UNLOCK(fd_data
);
4583 if (client
!= NULL
) {
4584 FREE(client
, M_NECP
);
4593 static NECP_CLIENT_ACTION_FUNCTION
int
4594 necp_client_claim(struct proc
*p
, struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
4597 uuid_t client_id
= {};
4598 struct necp_client
*client
= NULL
;
4600 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
)) {
4605 error
= copyin(uap
->client_id
, client_id
, sizeof(uuid_t
));
4607 NECPLOG(LOG_ERR
, "necp_client_claim copyin client_id error (%d)", error
);
4611 u_int64_t upid
= proc_uniqueid(p
);
4613 NECP_FD_LIST_LOCK_SHARED();
4615 struct necp_fd_data
*find_fd
= NULL
;
4616 LIST_FOREACH(find_fd
, &necp_fd_list
, chain
) {
4617 NECP_FD_LOCK(find_fd
);
4618 struct necp_client
*find_client
= necp_client_fd_find_client_and_lock(find_fd
, client_id
);
4619 if (find_client
!= NULL
) {
4620 if (find_client
->delegated_upid
== upid
) {
4621 // Matched the client to claim; remove from the old fd
4622 client
= find_client
;
4623 RB_REMOVE(_necp_client_tree
, &find_fd
->clients
, client
);
4624 necp_client_retain_locked(client
);
4626 NECP_CLIENT_UNLOCK(find_client
);
4628 NECP_FD_UNLOCK(find_fd
);
4630 if (client
!= NULL
) {
4635 NECP_FD_LIST_UNLOCK();
4637 if (client
== NULL
) {
4642 client
->proc_pid
= fd_data
->proc_pid
; // Transfer client to claiming pid
4644 // Add matched client to our fd and re-run result
4645 NECP_FD_LOCK(fd_data
);
4646 RB_INSERT(_necp_client_tree
, &fd_data
->clients
, client
);
4647 NECP_CLIENT_LOCK(client
);
4648 (void)necp_update_client_result(current_proc(), fd_data
, client
, NULL
);
4649 NECP_CLIENT_UNLOCK(client
);
4650 NECP_FD_UNLOCK(fd_data
);
4652 necp_client_release(client
);
4660 static NECP_CLIENT_ACTION_FUNCTION
int
4661 necp_client_remove(struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
4664 uuid_t client_id
= {};
4665 struct ifnet_stats_per_flow flow_ifnet_stats
= {};
4667 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
)) {
4672 error
= copyin(uap
->client_id
, client_id
, sizeof(uuid_t
));
4674 NECPLOG(LOG_ERR
, "necp_client_remove copyin client_id error (%d)", error
);
4678 if (uap
->buffer
!= 0 && uap
->buffer_size
== sizeof(flow_ifnet_stats
)) {
4679 error
= copyin(uap
->buffer
, &flow_ifnet_stats
, uap
->buffer_size
);
4681 NECPLOG(LOG_ERR
, "necp_client_remove flow_ifnet_stats copyin error (%d)", error
);
4682 // Not fatal; make sure to zero-out stats in case of partial copy
4683 memset(&flow_ifnet_stats
, 0, sizeof(flow_ifnet_stats
));
4686 } else if (uap
->buffer
!= 0) {
4687 NECPLOG(LOG_ERR
, "necp_client_remove unexpected parameters length (%zu)", uap
->buffer_size
);
4690 NECP_FD_LOCK(fd_data
);
4692 pid_t pid
= fd_data
->proc_pid
;
4693 struct necp_client
*client
= necp_client_fd_find_client_unlocked(fd_data
, client_id
);
4694 if (client
!= NULL
) {
4695 // Remove any flow registrations that match
4696 struct necp_client_flow_registration
*flow_registration
= NULL
;
4697 struct necp_client_flow_registration
*temp_flow_registration
= NULL
;
4698 RB_FOREACH_SAFE(flow_registration
, _necp_fd_flow_tree
, &fd_data
->flows
, temp_flow_registration
) {
4699 if (flow_registration
->client
== client
) {
4700 NECP_FLOW_TREE_LOCK_EXCLUSIVE();
4701 RB_REMOVE(_necp_client_flow_global_tree
, &necp_client_flow_global_tree
, flow_registration
);
4702 NECP_FLOW_TREE_UNLOCK();
4703 RB_REMOVE(_necp_fd_flow_tree
, &fd_data
->flows
, flow_registration
);
4706 // Remove client from lists
4707 NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
4708 RB_REMOVE(_necp_client_global_tree
, &necp_client_global_tree
, client
);
4709 NECP_CLIENT_TREE_UNLOCK();
4710 RB_REMOVE(_necp_client_tree
, &fd_data
->clients
, client
);
4714 NECP_FD_UNLOCK(fd_data
);
4716 if (client
!= NULL
) {
4718 necp_destroy_client(client
, pid
, true);
4721 NECPLOG(LOG_ERR
, "necp_client_remove invalid client_id (%d)", error
);
4730 // Don't inline the function since it includes necp_client_parsed_parameters on the stack
4731 static __attribute__((noinline
)) int
4732 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
)
4734 struct necp_client_parsed_parameters parsed_parameters
;
4737 error
= necp_client_parse_parameters(client
->parameters
,
4738 (u_int32_t
)client
->parameters_length
,
4739 &parsed_parameters
);
4741 NECPLOG(LOG_ERR
, "necp_client_parse_parameters error (%d)", error
);
4745 if ((flow
->remote_addr
.sa
.sa_family
!= AF_INET
&&
4746 flow
->remote_addr
.sa
.sa_family
!= AF_INET6
) ||
4747 (flow
->local_addr
.sa
.sa_family
!= AF_INET
&&
4748 flow
->local_addr
.sa
.sa_family
!= AF_INET6
)) {
4752 NECP_CLIENT_ROUTE_LOCK(client
);
4754 if (client
->current_route
== NULL
) {
4759 bool check_ecn
= false;
4761 if ((parsed_parameters
.flags
& NECP_CLIENT_PARAMETER_FLAG_ECN_ENABLE
) ==
4762 NECP_CLIENT_PARAMETER_FLAG_ECN_ENABLE
) {
4767 if ((parsed_parameters
.flags
& NECP_CLIENT_PARAMETER_FLAG_ECN_DISABLE
) ==
4768 NECP_CLIENT_PARAMETER_FLAG_ECN_DISABLE
) {
4772 if (client
->current_route
!= NULL
) {
4773 if (client
->current_route
->rt_ifp
->if_eflags
& IFEF_ECN_ENABLE
) {
4777 if (client
->current_route
->rt_ifp
->if_eflags
& IFEF_ECN_DISABLE
) {
4782 bool inbound
= ((parsed_parameters
.flags
& NECP_CLIENT_PARAMETER_FLAG_LISTENER
) == 0);
4783 if ((inbound
&& tcp_ecn_inbound
== 1) ||
4784 (!inbound
&& tcp_ecn_outbound
== 1)) {
4790 if (tcp_heuristic_do_ecn_with_address(client
->current_route
->rt_ifp
,
4791 (union sockaddr_in_4_6
*)&flow
->local_addr
)) {
4792 *flags
|= NECP_CLIENT_RESULT_FLAG_ECN_ENABLED
;
4796 if ((parsed_parameters
.flags
& NECP_CLIENT_PARAMETER_FLAG_TFO_ENABLE
) ==
4797 NECP_CLIENT_PARAMETER_FLAG_TFO_ENABLE
) {
4798 if (!tcp_heuristic_do_tfo_with_address(client
->current_route
->rt_ifp
,
4799 (union sockaddr_in_4_6
*)&flow
->local_addr
,
4800 (union sockaddr_in_4_6
*)&flow
->remote_addr
,
4801 tfo_cookie
, tfo_cookie_len
)) {
4802 *flags
|= NECP_CLIENT_RESULT_FLAG_FAST_OPEN_BLOCKED
;
4803 *tfo_cookie_len
= 0;
4806 *flags
|= NECP_CLIENT_RESULT_FLAG_FAST_OPEN_BLOCKED
;
4807 *tfo_cookie_len
= 0;
4810 NECP_CLIENT_ROUTE_UNLOCK(client
);
4816 necp_client_calculate_flow_tlv_size(struct necp_client_flow_registration
*flow_registration
)
4818 size_t assigned_results_size
= 0;
4819 struct necp_client_flow
*flow
= NULL
;
4820 LIST_FOREACH(flow
, &flow_registration
->flow_list
, flow_chain
) {
4821 if (flow
->assigned
) {
4822 size_t header_length
= 0;
4824 header_length
= sizeof(struct necp_client_nexus_flow_header
);
4826 header_length
= sizeof(struct necp_client_flow_header
);
4828 assigned_results_size
+= (header_length
+ flow
->assigned_results_length
);
4830 if (flow
->has_protoctl_event
) {
4831 assigned_results_size
+= sizeof(struct necp_client_flow_protoctl_event_header
);
4835 return assigned_results_size
;
4839 necp_client_fillout_flow_tlvs(struct necp_client
*client
,
4840 bool client_is_observed
,
4841 struct necp_client_flow_registration
*flow_registration
,
4842 struct necp_client_action_args
*uap
,
4843 size_t *assigned_results_cursor
)
4846 struct necp_client_flow
*flow
= NULL
;
4847 LIST_FOREACH(flow
, &flow_registration
->flow_list
, flow_chain
) {
4848 if (flow
->assigned
) {
4849 // Write TLV headers
4850 struct necp_client_nexus_flow_header header
= {};
4851 u_int32_t length
= 0;
4852 u_int32_t flags
= 0;
4853 u_int8_t tfo_cookie_len
= 0;
4856 type
= NECP_CLIENT_RESULT_FLOW_ID
;
4857 length
= sizeof(header
.flow_header
.flow_id
);
4858 memcpy(&header
.flow_header
.flow_id_tlv_header
.type
, &type
, sizeof(type
));
4859 memcpy(&header
.flow_header
.flow_id_tlv_header
.length
, &length
, sizeof(length
));
4860 uuid_copy(header
.flow_header
.flow_id
, flow_registration
->registration_id
);
4863 if (flow
->check_tcp_heuristics
) {
4864 u_int8_t tfo_cookie
[NECP_TFO_COOKIE_LEN_MAX
];
4865 tfo_cookie_len
= NECP_TFO_COOKIE_LEN_MAX
;
4867 if (necp_client_check_tcp_heuristics(client
, flow
, &flags
,
4868 tfo_cookie
, &tfo_cookie_len
) != 0) {
4871 flow
->check_tcp_heuristics
= FALSE
;
4873 if (tfo_cookie_len
!= 0) {
4874 type
= NECP_CLIENT_RESULT_TFO_COOKIE
;
4875 length
= tfo_cookie_len
;
4876 memcpy(&header
.tfo_cookie_tlv_header
.type
, &type
, sizeof(type
));
4877 memcpy(&header
.tfo_cookie_tlv_header
.length
, &length
, sizeof(length
));
4878 memcpy(&header
.tfo_cookie_value
, tfo_cookie
, tfo_cookie_len
);
4884 size_t header_length
= 0;
4886 if (tfo_cookie_len
!= 0) {
4887 header_length
= sizeof(struct necp_client_nexus_flow_header
) - (NECP_TFO_COOKIE_LEN_MAX
- tfo_cookie_len
);
4889 header_length
= sizeof(struct necp_client_nexus_flow_header
) - sizeof(struct necp_tlv_header
) - NECP_TFO_COOKIE_LEN_MAX
;
4892 header_length
= sizeof(struct necp_client_flow_header
);
4895 type
= NECP_CLIENT_RESULT_FLAGS
;
4896 length
= sizeof(header
.flow_header
.flags_value
);
4897 memcpy(&header
.flow_header
.flags_tlv_header
.type
, &type
, sizeof(type
));
4898 memcpy(&header
.flow_header
.flags_tlv_header
.length
, &length
, sizeof(length
));
4899 if (flow
->assigned
) {
4900 flags
|= NECP_CLIENT_RESULT_FLAG_FLOW_ASSIGNED
;
4903 flags
|= NECP_CLIENT_RESULT_FLAG_FLOW_VIABLE
;
4905 if (flow_registration
->defunct
) {
4906 flags
|= NECP_CLIENT_RESULT_FLAG_DEFUNCT
;
4908 flags
|= flow
->necp_flow_flags
;
4909 memcpy(&header
.flow_header
.flags_value
, &flags
, sizeof(flags
));
4911 type
= NECP_CLIENT_RESULT_INTERFACE
;
4912 length
= sizeof(header
.flow_header
.interface_value
);
4913 memcpy(&header
.flow_header
.interface_tlv_header
.type
, &type
, sizeof(type
));
4914 memcpy(&header
.flow_header
.interface_tlv_header
.length
, &length
, sizeof(length
));
4916 struct necp_client_result_interface interface_struct
;
4917 interface_struct
.generation
= 0;
4918 interface_struct
.index
= flow
->interface_index
;
4920 memcpy(&header
.flow_header
.interface_value
, &interface_struct
, sizeof(interface_struct
));
4922 type
= NECP_CLIENT_RESULT_NETAGENT
;
4923 length
= sizeof(header
.agent_value
);
4924 memcpy(&header
.agent_tlv_header
.type
, &type
, sizeof(type
));
4925 memcpy(&header
.agent_tlv_header
.length
, &length
, sizeof(length
));
4927 struct necp_client_result_netagent agent_struct
;
4928 agent_struct
.generation
= 0;
4929 uuid_copy(agent_struct
.netagent_uuid
, flow
->u
.nexus_agent
);
4931 memcpy(&header
.agent_value
, &agent_struct
, sizeof(agent_struct
));
4934 // Don't include outer TLV header in length field
4935 type
= NECP_CLIENT_RESULT_FLOW
;
4936 length
= (header_length
- sizeof(struct necp_tlv_header
) + flow
->assigned_results_length
);
4937 if (flow
->has_protoctl_event
) {
4938 length
+= sizeof(struct necp_client_flow_protoctl_event_header
);
4940 memcpy(&header
.flow_header
.outer_header
.type
, &type
, sizeof(type
));
4941 memcpy(&header
.flow_header
.outer_header
.length
, &length
, sizeof(length
));
4943 error
= copyout(&header
, uap
->buffer
+ client
->result_length
+ *assigned_results_cursor
, header_length
);
4945 NECPLOG(LOG_ERR
, "necp_client_copy assigned results tlv_header copyout error (%d)", error
);
4948 *assigned_results_cursor
+= header_length
;
4950 if (flow
->assigned_results
&& flow
->assigned_results_length
) {
4952 error
= copyout(flow
->assigned_results
, uap
->buffer
+ client
->result_length
+ *assigned_results_cursor
,
4953 flow
->assigned_results_length
);
4955 NECPLOG(LOG_ERR
, "necp_client_copy assigned results copyout error (%d)", error
);
4959 *assigned_results_cursor
+= flow
->assigned_results_length
;
4961 /* Read the protocol event and reset it */
4962 if (flow
->has_protoctl_event
) {
4963 struct necp_client_flow_protoctl_event_header protoctl_event_header
= {};
4965 type
= NECP_CLIENT_RESULT_PROTO_CTL_EVENT
;
4966 length
= sizeof(protoctl_event_header
.protoctl_event
);
4968 memcpy(&protoctl_event_header
.protoctl_tlv_header
.type
, &type
, sizeof(type
));
4969 memcpy(&protoctl_event_header
.protoctl_tlv_header
.length
, &length
, sizeof(length
));
4970 memcpy(&protoctl_event_header
.protoctl_event
, &flow
->protoctl_event
,
4971 sizeof(flow
->protoctl_event
));
4973 error
= copyout(&protoctl_event_header
, uap
->buffer
+ client
->result_length
+ *assigned_results_cursor
,
4974 sizeof(protoctl_event_header
));
4977 NECPLOG(LOG_ERR
, "necp_client_copy protocol control event results"
4978 " tlv_header copyout error (%d)", error
);
4981 *assigned_results_cursor
+= sizeof(protoctl_event_header
);
4982 flow
->has_protoctl_event
= FALSE
;
4983 flow
->protoctl_event
.protoctl_event_code
= 0;
4984 flow
->protoctl_event
.protoctl_event_val
= 0;
4985 flow
->protoctl_event
.protoctl_event_tcp_seq_num
= 0;
4989 if (!client_is_observed
) {
4990 flow_registration
->flow_result_read
= TRUE
;
4996 necp_client_copy_internal(struct necp_client
*client
, uuid_t client_id
, bool client_is_observed
, struct necp_client_action_args
*uap
, int *retval
)
4998 NECP_CLIENT_ASSERT_LOCKED(client
);
5001 if (uap
->action
== NECP_CLIENT_ACTION_COPY_PARAMETERS
) {
5002 if (uap
->buffer_size
< client
->parameters_length
) {
5005 error
= copyout(client
->parameters
, uap
->buffer
, client
->parameters_length
);
5007 NECPLOG(LOG_ERR
, "necp_client_copy parameters copyout error (%d)", error
);
5010 *retval
= client
->parameters_length
;
5011 } else if (uap
->action
== NECP_CLIENT_ACTION_COPY_UPDATED_RESULT
&&
5012 client
->result_read
&& !necp_client_has_unread_flows(client
)) {
5013 // Copy updates only, but nothing to read
5014 // Just return 0 for bytes read
5016 } else if (uap
->action
== NECP_CLIENT_ACTION_COPY_RESULT
||
5017 uap
->action
== NECP_CLIENT_ACTION_COPY_UPDATED_RESULT
) {
5018 size_t assigned_results_size
= 0;
5020 bool some_flow_is_defunct
= false;
5021 struct necp_client_flow_registration
*single_flow_registration
= NULL
;
5022 if (necp_client_id_is_flow(client_id
)) {
5023 single_flow_registration
= necp_client_find_flow(client
, client_id
);
5024 if (single_flow_registration
!= NULL
) {
5025 assigned_results_size
+= necp_client_calculate_flow_tlv_size(single_flow_registration
);
5028 // This request is for the client, so copy everything
5029 struct necp_client_flow_registration
*flow_registration
= NULL
;
5030 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
5031 if (flow_registration
->defunct
) {
5032 some_flow_is_defunct
= true;
5034 assigned_results_size
+= necp_client_calculate_flow_tlv_size(flow_registration
);
5037 if (uap
->buffer_size
< (client
->result_length
+ assigned_results_size
)) {
5041 u_int32_t original_flags
= 0;
5042 bool flags_updated
= false;
5043 if (some_flow_is_defunct
&& client
->legacy_client_is_flow
) {
5044 // If our client expects the defunct flag in the client, add it now
5045 u_int32_t client_flags
= 0;
5046 u_int32_t value_size
= 0;
5047 u_int8_t
*flags_pointer
= necp_buffer_get_tlv_value(client
->result
, 0, &value_size
);
5048 if (flags_pointer
!= NULL
&& value_size
== sizeof(client_flags
)) {
5049 memcpy(&client_flags
, flags_pointer
, value_size
);
5050 original_flags
= client_flags
;
5051 client_flags
|= NECP_CLIENT_RESULT_FLAG_DEFUNCT
;
5052 (void)necp_buffer_write_tlv_if_different(client
->result
, NECP_CLIENT_RESULT_FLAGS
,
5053 sizeof(client_flags
), &client_flags
, &flags_updated
,
5054 client
->result
, sizeof(client
->result
));
5058 error
= copyout(client
->result
, uap
->buffer
, client
->result_length
);
5060 if (flags_updated
) {
5061 // Revert stored flags
5062 (void)necp_buffer_write_tlv_if_different(client
->result
, NECP_CLIENT_RESULT_FLAGS
,
5063 sizeof(original_flags
), &original_flags
, &flags_updated
,
5064 client
->result
, sizeof(client
->result
));
5068 NECPLOG(LOG_ERR
, "necp_client_copy result copyout error (%d)", error
);
5072 size_t assigned_results_cursor
= 0;
5073 if (necp_client_id_is_flow(client_id
)) {
5074 if (single_flow_registration
!= NULL
) {
5075 error
= necp_client_fillout_flow_tlvs(client
, client_is_observed
, single_flow_registration
, uap
, &assigned_results_cursor
);
5081 // This request is for the client, so copy everything
5082 struct necp_client_flow_registration
*flow_registration
= NULL
;
5083 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
5084 error
= necp_client_fillout_flow_tlvs(client
, client_is_observed
, flow_registration
, uap
, &assigned_results_cursor
);
5091 *retval
= client
->result_length
+ assigned_results_cursor
;
5093 if (!client_is_observed
) {
5094 client
->result_read
= TRUE
;
5101 static NECP_CLIENT_ACTION_FUNCTION
int
5102 necp_client_copy(struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5105 struct necp_client
*client
= NULL
;
5107 uuid_clear(client_id
);
5111 if (uap
->buffer_size
== 0 || uap
->buffer
== 0) {
5115 if (uap
->action
!= NECP_CLIENT_ACTION_COPY_PARAMETERS
&&
5116 uap
->action
!= NECP_CLIENT_ACTION_COPY_RESULT
&&
5117 uap
->action
!= NECP_CLIENT_ACTION_COPY_UPDATED_RESULT
) {
5121 if (uap
->client_id
) {
5122 if (uap
->client_id_len
!= sizeof(uuid_t
)) {
5123 NECPLOG(LOG_ERR
, "Incorrect length (got %d, expected %d)", uap
->client_id_len
, sizeof(uuid_t
));
5127 error
= copyin(uap
->client_id
, client_id
, sizeof(uuid_t
));
5129 NECPLOG(LOG_ERR
, "necp_client_copy client_id copyin error (%d)", error
);
5134 const bool is_wildcard
= (bool)uuid_is_null(client_id
);
5136 NECP_FD_LOCK(fd_data
);
5139 if (uap
->action
== NECP_CLIENT_ACTION_COPY_RESULT
|| uap
->action
== NECP_CLIENT_ACTION_COPY_UPDATED_RESULT
) {
5140 struct necp_client
*find_client
= NULL
;
5141 RB_FOREACH(find_client
, _necp_client_tree
, &fd_data
->clients
) {
5142 NECP_CLIENT_LOCK(find_client
);
5143 if (!find_client
->result_read
|| necp_client_has_unread_flows(find_client
)) {
5144 client
= find_client
;
5145 // Leave the client locked, and break
5148 NECP_CLIENT_UNLOCK(find_client
);
5152 client
= necp_client_fd_find_client_and_lock(fd_data
, client_id
);
5155 if (client
!= NULL
) {
5156 // If client is set, it is locked
5157 error
= necp_client_copy_internal(client
, client_id
, FALSE
, uap
, retval
);
5158 NECP_CLIENT_UNLOCK(client
);
5161 // Unlock our own fd before moving on or returning
5162 NECP_FD_UNLOCK(fd_data
);
5164 if (client
== NULL
) {
5165 if (fd_data
->flags
& NECP_OPEN_FLAG_OBSERVER
) {
5166 // Observers are allowed to lookup clients on other fds
5169 NECP_CLIENT_TREE_LOCK_SHARED();
5171 bool found_client
= FALSE
;
5173 client
= necp_find_client_and_lock(client_id
);
5174 if (client
!= NULL
) {
5175 // Matched, copy out data
5176 found_client
= TRUE
;
5177 error
= necp_client_copy_internal(client
, client_id
, TRUE
, uap
, retval
);
5178 NECP_CLIENT_UNLOCK(client
);
5182 NECP_CLIENT_TREE_UNLOCK();
5184 // No client found, fail
5185 if (!found_client
) {
5189 // No client found, and not allowed to search other fds, fail
5197 static NECP_CLIENT_ACTION_FUNCTION
int
5198 necp_client_copy_client_update(struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5204 if (!(fd_data
->flags
& NECP_OPEN_FLAG_PUSH_OBSERVER
)) {
5205 NECPLOG0(LOG_ERR
, "NECP fd is not observer, cannot copy client update");
5209 if (uap
->client_id_len
!= sizeof(uuid_t
) || uap
->client_id
== 0) {
5210 NECPLOG0(LOG_ERR
, "Client id invalid, cannot copy client update");
5214 if (uap
->buffer_size
== 0 || uap
->buffer
== 0) {
5215 NECPLOG0(LOG_ERR
, "Buffer invalid, cannot copy client update");
5219 NECP_FD_LOCK(fd_data
);
5220 struct necp_client_update
*client_update
= TAILQ_FIRST(&fd_data
->update_list
);
5221 if (client_update
!= NULL
) {
5222 TAILQ_REMOVE(&fd_data
->update_list
, client_update
, chain
);
5223 VERIFY(fd_data
->update_count
> 0);
5224 fd_data
->update_count
--;
5226 NECP_FD_UNLOCK(fd_data
);
5228 if (client_update
!= NULL
) {
5229 error
= copyout(client_update
->client_id
, uap
->client_id
, sizeof(uuid_t
));
5231 NECPLOG(LOG_ERR
, "Copy client update copyout client id error (%d)", error
);
5233 if (uap
->buffer_size
< client_update
->update_length
) {
5234 NECPLOG(LOG_ERR
, "Buffer size cannot hold update (%zu < %zu)", uap
->buffer_size
, client_update
->update_length
);
5237 error
= copyout(&client_update
->update
, uap
->buffer
, client_update
->update_length
);
5239 NECPLOG(LOG_ERR
, "Copy client update copyout error (%d)", error
);
5241 *retval
= client_update
->update_length
;
5246 FREE(client_update
, M_NECP
);
5247 client_update
= NULL
;
5256 necp_client_copy_parameters_locked(struct necp_client
*client
,
5257 struct necp_client_nexus_parameters
*parameters
)
5259 VERIFY(parameters
!= NULL
);
5261 struct necp_client_parsed_parameters parsed_parameters
= {};
5262 int error
= necp_client_parse_parameters(client
->parameters
, (u_int32_t
)client
->parameters_length
, &parsed_parameters
);
5264 parameters
->pid
= client
->proc_pid
;
5265 if (parsed_parameters
.valid_fields
& NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID
) {
5266 parameters
->epid
= parsed_parameters
.effective_pid
;
5268 parameters
->epid
= parameters
->pid
;
5270 memcpy(¶meters
->local_addr
, &parsed_parameters
.local_addr
, sizeof(parameters
->local_addr
));
5271 memcpy(¶meters
->remote_addr
, &parsed_parameters
.remote_addr
, sizeof(parameters
->remote_addr
));
5272 parameters
->ip_protocol
= parsed_parameters
.ip_protocol
;
5273 if (parsed_parameters
.valid_fields
& NECP_PARSED_PARAMETERS_FIELD_TRANSPORT_PROTOCOL
) {
5274 parameters
->transport_protocol
= parsed_parameters
.transport_protocol
;
5276 parameters
->transport_protocol
= parsed_parameters
.ip_protocol
;
5278 parameters
->ethertype
= parsed_parameters
.ethertype
;
5279 parameters
->traffic_class
= parsed_parameters
.traffic_class
;
5280 uuid_copy(parameters
->euuid
, parsed_parameters
.effective_uuid
);
5281 parameters
->is_listener
= (parsed_parameters
.flags
& NECP_CLIENT_PARAMETER_FLAG_LISTENER
) ? 1 : 0;
5282 parameters
->is_interpose
= (parsed_parameters
.flags
& NECP_CLIENT_PARAMETER_FLAG_INTERPOSE
) ? 1 : 0;
5283 parameters
->is_custom_ether
= (parsed_parameters
.flags
& NECP_CLIENT_PARAMETER_FLAG_CUSTOM_ETHER
) ? 1 : 0;
5284 parameters
->policy_id
= client
->policy_id
;
5286 // parse client result flag
5287 u_int32_t client_result_flags
= 0;
5288 u_int32_t value_size
= 0;
5289 u_int8_t
*flags_pointer
= NULL
;
5290 flags_pointer
= necp_buffer_get_tlv_value(client
->result
, 0, &value_size
);
5291 if (flags_pointer
&& value_size
== sizeof(client_result_flags
)) {
5292 memcpy(&client_result_flags
, flags_pointer
, value_size
);
5294 parameters
->allow_qos_marking
= (client_result_flags
& NECP_CLIENT_RESULT_FLAG_ALLOW_QOS_MARKING
) ? 1 : 0;
5296 if (parsed_parameters
.valid_fields
& NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR_PREFERENCE
) {
5297 if (parsed_parameters
.local_address_preference
== NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_DEFAULT
) {
5298 parameters
->override_address_selection
= false;
5299 } else if (parsed_parameters
.local_address_preference
== NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_TEMPORARY
) {
5300 parameters
->override_address_selection
= true;
5301 parameters
->use_stable_address
= false;
5302 } else if (parsed_parameters
.local_address_preference
== NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_STABLE
) {
5303 parameters
->override_address_selection
= true;
5304 parameters
->use_stable_address
= true;
5307 parameters
->override_address_selection
= false;
5313 static NECP_CLIENT_ACTION_FUNCTION
int
5314 necp_client_list(struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5317 struct necp_client
*find_client
= NULL
;
5318 uuid_t
*list
= NULL
;
5319 u_int32_t requested_client_count
= 0;
5320 u_int32_t client_count
= 0;
5321 size_t copy_buffer_size
= 0;
5323 if (uap
->buffer_size
< sizeof(requested_client_count
) || uap
->buffer
== 0) {
5328 if (!(fd_data
->flags
& NECP_OPEN_FLAG_OBSERVER
)) {
5329 NECPLOG0(LOG_ERR
, "Client does not hold necessary entitlement to list other NECP clients");
5334 error
= copyin(uap
->buffer
, &requested_client_count
, sizeof(requested_client_count
));
5339 if (os_mul_overflow(sizeof(uuid_t
), requested_client_count
, ©_buffer_size
)) {
5344 if (uap
->buffer_size
- sizeof(requested_client_count
) != copy_buffer_size
) {
5349 if (copy_buffer_size
> NECP_MAX_CLIENT_LIST_SIZE
) {
5354 if (requested_client_count
> 0) {
5355 if ((list
= _MALLOC(copy_buffer_size
, M_NECP
, M_WAITOK
| M_ZERO
)) == NULL
) {
5362 NECP_CLIENT_TREE_LOCK_SHARED();
5365 RB_FOREACH(find_client
, _necp_client_global_tree
, &necp_client_global_tree
) {
5366 NECP_CLIENT_LOCK(find_client
);
5367 if (!uuid_is_null(find_client
->client_id
)) {
5368 if (client_count
< requested_client_count
) {
5369 uuid_copy(list
[client_count
], find_client
->client_id
);
5373 NECP_CLIENT_UNLOCK(find_client
);
5377 NECP_CLIENT_TREE_UNLOCK();
5379 error
= copyout(&client_count
, uap
->buffer
, sizeof(client_count
));
5381 NECPLOG(LOG_ERR
, "necp_client_list buffer copyout error (%d)", error
);
5385 if (requested_client_count
> 0 &&
5388 error
= copyout(list
, uap
->buffer
+ sizeof(client_count
), copy_buffer_size
);
5390 NECPLOG(LOG_ERR
, "necp_client_list client count copyout error (%d)", error
);
5405 necp_client_add_assertion(struct necp_client
*client
, uuid_t netagent_uuid
)
5407 struct necp_client_assertion
*new_assertion
= NULL
;
5409 MALLOC(new_assertion
, struct necp_client_assertion
*, sizeof(*new_assertion
), M_NECP
, M_WAITOK
);
5410 if (new_assertion
== NULL
) {
5411 NECPLOG0(LOG_ERR
, "Failed to allocate assertion");
5415 uuid_copy(new_assertion
->asserted_netagent
, netagent_uuid
);
5417 LIST_INSERT_HEAD(&client
->assertion_list
, new_assertion
, assertion_chain
);
5421 necp_client_remove_assertion(struct necp_client
*client
, uuid_t netagent_uuid
)
5423 struct necp_client_assertion
*found_assertion
= NULL
;
5424 struct necp_client_assertion
*search_assertion
= NULL
;
5425 LIST_FOREACH(search_assertion
, &client
->assertion_list
, assertion_chain
) {
5426 if (uuid_compare(search_assertion
->asserted_netagent
, netagent_uuid
) == 0) {
5427 found_assertion
= search_assertion
;
5432 if (found_assertion
== NULL
) {
5433 NECPLOG0(LOG_ERR
, "Netagent uuid not previously asserted");
5437 LIST_REMOVE(found_assertion
, assertion_chain
);
5438 FREE(found_assertion
, M_NECP
);
5442 static NECP_CLIENT_ACTION_FUNCTION
int
5443 necp_client_agent_action(struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5446 struct necp_client
*client
= NULL
;
5448 bool acted_on_agent
= FALSE
;
5449 u_int8_t
*parameters
= NULL
;
5450 size_t parameters_size
= uap
->buffer_size
;
5452 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
) ||
5453 uap
->buffer_size
== 0 || uap
->buffer
== 0) {
5454 NECPLOG0(LOG_ERR
, "necp_client_agent_action invalid parameters");
5459 error
= copyin(uap
->client_id
, client_id
, sizeof(uuid_t
));
5461 NECPLOG(LOG_ERR
, "necp_client_agent_action copyin client_id error (%d)", error
);
5465 if (uap
->buffer_size
> NECP_MAX_AGENT_ACTION_SIZE
) {
5466 NECPLOG(LOG_ERR
, "necp_client_agent_action invalid buffer size (>%u)", NECP_MAX_AGENT_ACTION_SIZE
);
5471 if ((parameters
= _MALLOC(uap
->buffer_size
, M_NECP
, M_WAITOK
| M_ZERO
)) == NULL
) {
5472 NECPLOG0(LOG_ERR
, "necp_client_agent_action malloc failed");
5477 error
= copyin(uap
->buffer
, parameters
, uap
->buffer_size
);
5479 NECPLOG(LOG_ERR
, "necp_client_agent_action parameters copyin error (%d)", error
);
5483 NECP_FD_LOCK(fd_data
);
5484 client
= necp_client_fd_find_client_and_lock(fd_data
, client_id
);
5485 if (client
!= NULL
) {
5487 while ((offset
+ sizeof(struct necp_tlv_header
)) <= parameters_size
) {
5488 u_int8_t type
= necp_buffer_get_tlv_type(parameters
, offset
);
5489 u_int32_t length
= necp_buffer_get_tlv_length(parameters
, offset
);
5491 if (length
> (parameters_size
- (offset
+ sizeof(struct necp_tlv_header
)))) {
5492 // If the length is larger than what can fit in the remaining parameters size, bail
5493 NECPLOG(LOG_ERR
, "Invalid TLV length (%u)", length
);
5498 u_int8_t
*value
= necp_buffer_get_tlv_value(parameters
, offset
, NULL
);
5499 if (length
>= sizeof(uuid_t
) &&
5501 (type
== NECP_CLIENT_PARAMETER_TRIGGER_AGENT
||
5502 type
== NECP_CLIENT_PARAMETER_ASSERT_AGENT
||
5503 type
== NECP_CLIENT_PARAMETER_UNASSERT_AGENT
)) {
5505 uuid_copy(agent_uuid
, value
);
5506 u_int8_t netagent_message_type
= 0;
5507 if (type
== NECP_CLIENT_PARAMETER_TRIGGER_AGENT
) {
5508 netagent_message_type
= NETAGENT_MESSAGE_TYPE_CLIENT_TRIGGER
;
5509 } else if (type
== NECP_CLIENT_PARAMETER_ASSERT_AGENT
) {
5510 netagent_message_type
= NETAGENT_MESSAGE_TYPE_CLIENT_ASSERT
;
5511 } else if (type
== NECP_CLIENT_PARAMETER_UNASSERT_AGENT
) {
5512 netagent_message_type
= NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT
;
5515 // Before unasserting, verify that the assertion was already taken
5516 if (type
== NECP_CLIENT_PARAMETER_UNASSERT_AGENT
) {
5517 if (!necp_client_remove_assertion(client
, agent_uuid
)) {
5523 struct necp_client_nexus_parameters parsed_parameters
= {};
5524 necp_client_copy_parameters_locked(client
, &parsed_parameters
);
5526 error
= netagent_client_message_with_params(agent_uuid
,
5529 client
->agent_handle
,
5530 netagent_message_type
,
5531 (struct necp_client_agent_parameters
*)&parsed_parameters
,
5534 acted_on_agent
= TRUE
;
5539 // Only save the assertion if the action succeeded
5540 if (type
== NECP_CLIENT_PARAMETER_ASSERT_AGENT
) {
5541 necp_client_add_assertion(client
, agent_uuid
);
5546 offset
+= sizeof(struct necp_tlv_header
) + length
;
5549 NECP_CLIENT_UNLOCK(client
);
5551 NECP_FD_UNLOCK(fd_data
);
5553 if (!acted_on_agent
&&
5559 if (parameters
!= NULL
) {
5560 FREE(parameters
, M_NECP
);
5567 static NECP_CLIENT_ACTION_FUNCTION
int
5568 necp_client_copy_agent(__unused
struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5573 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
) ||
5574 uap
->buffer_size
== 0 || uap
->buffer
== 0) {
5575 NECPLOG0(LOG_ERR
, "necp_client_copy_agent bad input");
5580 error
= copyin(uap
->client_id
, agent_uuid
, sizeof(uuid_t
));
5582 NECPLOG(LOG_ERR
, "necp_client_copy_agent copyin agent_uuid error (%d)", error
);
5586 error
= netagent_copyout(agent_uuid
, uap
->buffer
, uap
->buffer_size
);
5588 // netagent_copyout already logs appropriate errors
5597 static NECP_CLIENT_ACTION_FUNCTION
int
5598 necp_client_agent_use(struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5601 struct necp_client
*client
= NULL
;
5603 struct necp_agent_use_parameters parameters
;
5605 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
) ||
5606 uap
->buffer_size
!= sizeof(parameters
) || uap
->buffer
== 0) {
5611 error
= copyin(uap
->client_id
, client_id
, sizeof(uuid_t
));
5613 NECPLOG(LOG_ERR
, "Copyin client_id error (%d)", error
);
5617 error
= copyin(uap
->buffer
, ¶meters
, uap
->buffer_size
);
5619 NECPLOG(LOG_ERR
, "Parameters copyin error (%d)", error
);
5623 NECP_FD_LOCK(fd_data
);
5624 client
= necp_client_fd_find_client_and_lock(fd_data
, client_id
);
5625 if (client
!= NULL
) {
5626 error
= netagent_use(parameters
.agent_uuid
, ¶meters
.out_use_count
);
5627 NECP_CLIENT_UNLOCK(client
);
5632 NECP_FD_UNLOCK(fd_data
);
5635 error
= copyout(¶meters
, uap
->buffer
, uap
->buffer_size
);
5637 NECPLOG(LOG_ERR
, "Parameters copyout error (%d)", error
);
5648 struct necp_interface_details_legacy
{
5649 char name
[IFXNAMSIZ
];
5651 u_int32_t generation
;
5652 u_int32_t functional_type
;
5653 u_int32_t delegate_index
;
5654 u_int32_t flags
; // see NECP_INTERFACE_FLAG_*
5656 struct necp_interface_signature ipv4_signature
;
5657 struct necp_interface_signature ipv6_signature
;
5660 static NECP_CLIENT_ACTION_FUNCTION
int
5661 necp_client_copy_interface(__unused
struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5664 u_int32_t interface_index
= 0;
5665 struct necp_interface_details interface_details
= {};
5667 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(u_int32_t
) ||
5668 uap
->buffer_size
< sizeof(struct necp_interface_details_legacy
) ||
5670 NECPLOG0(LOG_ERR
, "necp_client_copy_interface bad input");
5675 error
= copyin(uap
->client_id
, &interface_index
, sizeof(u_int32_t
));
5677 NECPLOG(LOG_ERR
, "necp_client_copy_interface copyin interface_index error (%d)", error
);
5681 if (interface_index
== 0) {
5683 NECPLOG(LOG_ERR
, "necp_client_copy_interface bad interface_index (%d)", interface_index
);
5687 ifnet_head_lock_shared();
5688 ifnet_t interface
= NULL
;
5689 if (interface_index
!= IFSCOPE_NONE
&& interface_index
<= (u_int32_t
)if_index
) {
5690 interface
= ifindex2ifnet
[interface_index
];
5693 if (interface
!= NULL
) {
5694 if (interface
->if_xname
!= NULL
) {
5695 strlcpy((char *)&interface_details
.name
, interface
->if_xname
, sizeof(interface_details
.name
));
5697 interface_details
.index
= interface
->if_index
;
5698 interface_details
.generation
= ifnet_get_generation(interface
);
5699 if (interface
->if_delegated
.ifp
!= NULL
) {
5700 interface_details
.delegate_index
= interface
->if_delegated
.ifp
->if_index
;
5702 interface_details
.functional_type
= if_functional_type(interface
, TRUE
);
5703 if (IFNET_IS_EXPENSIVE(interface
)) {
5704 interface_details
.flags
|= NECP_INTERFACE_FLAG_EXPENSIVE
;
5706 if (IFNET_IS_CONSTRAINED(interface
)) {
5707 interface_details
.flags
|= NECP_INTERFACE_FLAG_CONSTRAINED
;
5709 if ((interface
->if_eflags
& IFEF_TXSTART
) == IFEF_TXSTART
) {
5710 interface_details
.flags
|= NECP_INTERFACE_FLAG_TXSTART
;
5712 if ((interface
->if_eflags
& IFEF_NOACKPRI
) == IFEF_NOACKPRI
) {
5713 interface_details
.flags
|= NECP_INTERFACE_FLAG_NOACKPRI
;
5715 if ((interface
->if_eflags
& IFEF_3CA
) == IFEF_3CA
) {
5716 interface_details
.flags
|= NECP_INTERFACE_FLAG_3CARRIERAGG
;
5718 if (IFNET_IS_LOW_POWER(interface
)) {
5719 interface_details
.flags
|= NECP_INTERFACE_FLAG_IS_LOW_POWER
;
5721 if (interface
->if_xflags
& IFXF_MPK_LOG
) {
5722 interface_details
.flags
|= NECP_INTERFACE_FLAG_MPK_LOG
;
5724 interface_details
.mtu
= interface
->if_mtu
;
5726 u_int8_t ipv4_signature_len
= sizeof(interface_details
.ipv4_signature
.signature
);
5727 u_int16_t ipv4_signature_flags
;
5728 if (ifnet_get_netsignature(interface
, AF_INET
, &ipv4_signature_len
, &ipv4_signature_flags
,
5729 (u_int8_t
*)&interface_details
.ipv4_signature
) != 0) {
5730 ipv4_signature_len
= 0;
5732 interface_details
.ipv4_signature
.signature_len
= ipv4_signature_len
;
5734 u_int8_t ipv6_signature_len
= sizeof(interface_details
.ipv6_signature
.signature
);
5735 u_int16_t ipv6_signature_flags
;
5736 if (ifnet_get_netsignature(interface
, AF_INET6
, &ipv6_signature_len
, &ipv6_signature_flags
,
5737 (u_int8_t
*)&interface_details
.ipv6_signature
) != 0) {
5738 ipv6_signature_len
= 0;
5740 interface_details
.ipv6_signature
.signature_len
= ipv6_signature_len
;
5742 ifnet_lock_shared(interface
);
5743 struct ifaddr
*ifa
= NULL
;
5744 TAILQ_FOREACH(ifa
, &interface
->if_addrhead
, ifa_link
) {
5746 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
5747 interface_details
.flags
|= NECP_INTERFACE_FLAG_HAS_NETMASK
;
5748 interface_details
.ipv4_netmask
= ((struct in_ifaddr
*)ifa
)->ia_sockmask
.sin_addr
.s_addr
;
5749 if (interface
->if_flags
& IFF_BROADCAST
) {
5750 interface_details
.flags
|= NECP_INTERFACE_FLAG_HAS_BROADCAST
;
5751 interface_details
.ipv4_broadcast
= ((struct in_ifaddr
*)ifa
)->ia_broadaddr
.sin_addr
.s_addr
;
5756 ifnet_lock_done(interface
);
5761 // If the client is using an older version of the struct, copy that length
5762 size_t copy_length
= sizeof(interface_details
);
5763 if (uap
->buffer_size
< sizeof(struct necp_interface_details_legacy
)) {
5764 copy_length
= sizeof(struct necp_interface_details_legacy
);
5766 error
= copyout(&interface_details
, uap
->buffer
, copy_length
);
5768 NECPLOG(LOG_ERR
, "necp_client_copy_interface copyout error (%d)", error
);
5778 static NECP_CLIENT_ACTION_FUNCTION
int
5779 necp_client_copy_route_statistics(__unused
struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5782 struct necp_client
*client
= NULL
;
5785 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
) ||
5786 uap
->buffer_size
< sizeof(struct necp_stat_counts
) || uap
->buffer
== 0) {
5787 NECPLOG0(LOG_ERR
, "necp_client_copy_route_statistics bad input");
5792 error
= copyin(uap
->client_id
, client_id
, sizeof(uuid_t
));
5794 NECPLOG(LOG_ERR
, "necp_client_copy_route_statistics copyin client_id error (%d)", error
);
5799 NECP_FD_LOCK(fd_data
);
5800 client
= necp_client_fd_find_client_and_lock(fd_data
, client_id
);
5801 if (client
!= NULL
) {
5802 NECP_CLIENT_ROUTE_LOCK(client
);
5803 struct necp_stat_counts route_stats
= {};
5804 if (client
->current_route
!= NULL
&& client
->current_route
->rt_stats
!= NULL
) {
5805 struct nstat_counts
*rt_stats
= client
->current_route
->rt_stats
;
5806 atomic_get_64(route_stats
.necp_stat_rxpackets
, &rt_stats
->nstat_rxpackets
);
5807 atomic_get_64(route_stats
.necp_stat_rxbytes
, &rt_stats
->nstat_rxbytes
);
5808 atomic_get_64(route_stats
.necp_stat_txpackets
, &rt_stats
->nstat_txpackets
);
5809 atomic_get_64(route_stats
.necp_stat_txbytes
, &rt_stats
->nstat_txbytes
);
5810 route_stats
.necp_stat_rxduplicatebytes
= rt_stats
->nstat_rxduplicatebytes
;
5811 route_stats
.necp_stat_rxoutoforderbytes
= rt_stats
->nstat_rxoutoforderbytes
;
5812 route_stats
.necp_stat_txretransmit
= rt_stats
->nstat_txretransmit
;
5813 route_stats
.necp_stat_connectattempts
= rt_stats
->nstat_connectattempts
;
5814 route_stats
.necp_stat_connectsuccesses
= rt_stats
->nstat_connectsuccesses
;
5815 route_stats
.necp_stat_min_rtt
= rt_stats
->nstat_min_rtt
;
5816 route_stats
.necp_stat_avg_rtt
= rt_stats
->nstat_avg_rtt
;
5817 route_stats
.necp_stat_var_rtt
= rt_stats
->nstat_var_rtt
;
5818 route_stats
.necp_stat_route_flags
= client
->current_route
->rt_flags
;
5821 // Unlock before copying out
5822 NECP_CLIENT_ROUTE_UNLOCK(client
);
5823 NECP_CLIENT_UNLOCK(client
);
5824 NECP_FD_UNLOCK(fd_data
);
5826 error
= copyout(&route_stats
, uap
->buffer
, sizeof(route_stats
));
5828 NECPLOG(LOG_ERR
, "necp_client_copy_route_statistics copyout error (%d)", error
);
5832 NECP_FD_UNLOCK(fd_data
);
5842 static NECP_CLIENT_ACTION_FUNCTION
int
5843 necp_client_update_cache(struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5846 struct necp_client
*client
= NULL
;
5849 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
)) {
5854 error
= copyin(uap
->client_id
, client_id
, sizeof(uuid_t
));
5856 NECPLOG(LOG_ERR
, "necp_client_update_cache copyin client_id error (%d)", error
);
5860 NECP_FD_LOCK(fd_data
);
5861 client
= necp_client_fd_find_client_and_lock(fd_data
, client_id
);
5862 if (client
== NULL
) {
5863 NECP_FD_UNLOCK(fd_data
);
5868 struct necp_client_flow_registration
*flow_registration
= necp_client_find_flow(client
, client_id
);
5869 if (flow_registration
== NULL
) {
5870 NECP_CLIENT_UNLOCK(client
);
5871 NECP_FD_UNLOCK(fd_data
);
5876 NECP_CLIENT_ROUTE_LOCK(client
);
5877 // This needs to be changed when TFO/ECN is supported by multiple flows
5878 struct necp_client_flow
*flow
= LIST_FIRST(&flow_registration
->flow_list
);
5880 (flow
->remote_addr
.sa
.sa_family
!= AF_INET
&&
5881 flow
->remote_addr
.sa
.sa_family
!= AF_INET6
) ||
5882 (flow
->local_addr
.sa
.sa_family
!= AF_INET
&&
5883 flow
->local_addr
.sa
.sa_family
!= AF_INET6
)) {
5885 NECPLOG(LOG_ERR
, "necp_client_update_cache no flow error (%d)", error
);
5889 necp_cache_buffer cache_buffer
;
5890 memset(&cache_buffer
, 0, sizeof(cache_buffer
));
5892 if (uap
->buffer_size
!= sizeof(necp_cache_buffer
) ||
5893 uap
->buffer
== USER_ADDR_NULL
) {
5898 error
= copyin(uap
->buffer
, &cache_buffer
, sizeof(cache_buffer
));
5900 NECPLOG(LOG_ERR
, "necp_client_update_cache copyin cache buffer error (%d)", error
);
5904 if (cache_buffer
.necp_cache_buf_type
== NECP_CLIENT_CACHE_TYPE_ECN
&&
5905 cache_buffer
.necp_cache_buf_ver
== NECP_CLIENT_CACHE_TYPE_ECN_VER_1
) {
5906 if (cache_buffer
.necp_cache_buf_size
!= sizeof(necp_tcp_ecn_cache
) ||
5907 cache_buffer
.necp_cache_buf_addr
== USER_ADDR_NULL
) {
5912 necp_tcp_ecn_cache ecn_cache_buffer
;
5913 memset(&ecn_cache_buffer
, 0, sizeof(ecn_cache_buffer
));
5915 error
= copyin(cache_buffer
.necp_cache_buf_addr
, &ecn_cache_buffer
, sizeof(necp_tcp_ecn_cache
));
5917 NECPLOG(LOG_ERR
, "necp_client_update_cache copyin ecn cache buffer error (%d)", error
);
5921 if (client
->current_route
!= NULL
&& client
->current_route
->rt_ifp
!= NULL
) {
5922 if (!client
->platform_binary
) {
5923 ecn_cache_buffer
.necp_tcp_ecn_heuristics_success
= 0;
5925 tcp_heuristics_ecn_update(&ecn_cache_buffer
, client
->current_route
->rt_ifp
,
5926 (union sockaddr_in_4_6
*)&flow
->local_addr
);
5928 } else if (cache_buffer
.necp_cache_buf_type
== NECP_CLIENT_CACHE_TYPE_TFO
&&
5929 cache_buffer
.necp_cache_buf_ver
== NECP_CLIENT_CACHE_TYPE_TFO_VER_1
) {
5930 if (cache_buffer
.necp_cache_buf_size
!= sizeof(necp_tcp_tfo_cache
) ||
5931 cache_buffer
.necp_cache_buf_addr
== USER_ADDR_NULL
) {
5936 necp_tcp_tfo_cache tfo_cache_buffer
;
5937 memset(&tfo_cache_buffer
, 0, sizeof(tfo_cache_buffer
));
5939 error
= copyin(cache_buffer
.necp_cache_buf_addr
, &tfo_cache_buffer
, sizeof(necp_tcp_tfo_cache
));
5941 NECPLOG(LOG_ERR
, "necp_client_update_cache copyin tfo cache buffer error (%d)", error
);
5945 if (client
->current_route
!= NULL
&& client
->current_route
->rt_ifp
!= NULL
) {
5946 if (!client
->platform_binary
) {
5947 tfo_cache_buffer
.necp_tcp_tfo_heuristics_success
= 0;
5949 tcp_heuristics_tfo_update(&tfo_cache_buffer
, client
->current_route
->rt_ifp
,
5950 (union sockaddr_in_4_6
*)&flow
->local_addr
,
5951 (union sockaddr_in_4_6
*)&flow
->remote_addr
);
5957 NECP_CLIENT_ROUTE_UNLOCK(client
);
5958 NECP_CLIENT_UNLOCK(client
);
5959 NECP_FD_UNLOCK(fd_data
);
5965 #define NECP_CLIENT_ACTION_SIGN_DEFAULT_HOSTNAME_LENGTH 64
5966 #define NECP_CLIENT_ACTION_SIGN_MAX_HOSTNAME_LENGTH 1024
5968 #define NECP_CLIENT_ACTION_SIGN_TAG_LENGTH 32
5970 static NECP_CLIENT_ACTION_FUNCTION
int
5971 necp_client_sign(__unused
struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5974 u_int32_t hostname_length
= 0;
5975 u_int8_t tag
[NECP_CLIENT_ACTION_SIGN_TAG_LENGTH
] = {};
5976 struct necp_client_signable signable
= {};
5977 union necp_sockaddr_union address_answer
= {};
5978 u_int8_t
*client_hostname
= NULL
;
5979 u_int8_t
*allocated_hostname
= NULL
;
5980 u_int8_t default_hostname
[NECP_CLIENT_ACTION_SIGN_DEFAULT_HOSTNAME_LENGTH
] = "";
5981 uint32_t tag_size
= sizeof(tag
);
5985 const bool has_resolver_entitlement
= (priv_check_cred(kauth_cred_get(), PRIV_NET_VALIDATED_RESOLVER
, 0) == 0);
5986 if (!has_resolver_entitlement
) {
5987 NECPLOG0(LOG_ERR
, "Process does not hold the necessary entitlement to sign resolver answers");
5992 if (uap
->client_id
== 0 || uap
->client_id_len
< sizeof(struct necp_client_signable
)) {
5997 if (uap
->buffer
== 0 || uap
->buffer_size
!= NECP_CLIENT_ACTION_SIGN_TAG_LENGTH
) {
6002 error
= copyin(uap
->client_id
, &signable
, sizeof(signable
));
6004 NECPLOG(LOG_ERR
, "necp_client_sign copyin signable error (%d)", error
);
6008 if (signable
.sign_type
!= NECP_CLIENT_SIGN_TYPE_RESOLVER_ANSWER
) {
6009 NECPLOG(LOG_ERR
, "necp_client_sign unknown signable type (%u)", signable
.sign_type
);
6014 if (uap
->client_id_len
< sizeof(struct necp_client_resolver_answer
)) {
6019 error
= copyin(uap
->client_id
+ sizeof(signable
), &address_answer
, sizeof(address_answer
));
6021 NECPLOG(LOG_ERR
, "necp_client_sign copyin address_answer error (%d)", error
);
6025 error
= copyin(uap
->client_id
+ sizeof(signable
) + sizeof(address_answer
), &hostname_length
, sizeof(hostname_length
));
6027 NECPLOG(LOG_ERR
, "necp_client_sign copyin hostname_length error (%d)", error
);
6031 if (hostname_length
> NECP_CLIENT_ACTION_SIGN_MAX_HOSTNAME_LENGTH
) {
6036 if (hostname_length
> NECP_CLIENT_ACTION_SIGN_DEFAULT_HOSTNAME_LENGTH
) {
6037 if ((allocated_hostname
= _MALLOC(hostname_length
, M_NECP
, M_WAITOK
| M_ZERO
)) == NULL
) {
6038 NECPLOG(LOG_ERR
, "necp_client_sign malloc hostname %u failed", hostname_length
);
6043 client_hostname
= allocated_hostname
;
6045 client_hostname
= default_hostname
;
6048 error
= copyin(uap
->client_id
+ sizeof(signable
) + sizeof(address_answer
) + sizeof(hostname_length
), client_hostname
, hostname_length
);
6050 NECPLOG(LOG_ERR
, "necp_client_sign copyin hostname error (%d)", error
);
6054 address_answer
.sin
.sin_port
= 0;
6055 error
= necp_sign_resolver_answer(signable
.client_id
, client_hostname
, hostname_length
,
6056 (u_int8_t
*)&address_answer
, sizeof(address_answer
),
6058 if (tag_size
!= sizeof(tag
)) {
6059 NECPLOG(LOG_ERR
, "necp_client_sign unexpected tag size %u", tag_size
);
6063 error
= copyout(tag
, uap
->buffer
, tag_size
);
6065 NECPLOG(LOG_ERR
, "necp_client_sign copyout error (%d)", error
);
6070 if (allocated_hostname
!= NULL
) {
6071 FREE(allocated_hostname
, M_NECP
);
6072 allocated_hostname
= NULL
;
6079 necp_client_action(struct proc
*p
, struct necp_client_action_args
*uap
, int *retval
)
6083 int return_value
= 0;
6084 struct necp_fd_data
*fd_data
= NULL
;
6085 error
= necp_find_fd_data(uap
->necp_fd
, &fd_data
);
6087 NECPLOG(LOG_ERR
, "necp_client_action find fd error (%d)", error
);
6091 u_int32_t action
= uap
->action
;
6093 case NECP_CLIENT_ACTION_ADD
: {
6094 return_value
= necp_client_add(p
, fd_data
, uap
, retval
);
6097 case NECP_CLIENT_ACTION_CLAIM
: {
6098 return_value
= necp_client_claim(p
, fd_data
, uap
, retval
);
6101 case NECP_CLIENT_ACTION_REMOVE
: {
6102 return_value
= necp_client_remove(fd_data
, uap
, retval
);
6105 case NECP_CLIENT_ACTION_COPY_PARAMETERS
:
6106 case NECP_CLIENT_ACTION_COPY_RESULT
:
6107 case NECP_CLIENT_ACTION_COPY_UPDATED_RESULT
: {
6108 return_value
= necp_client_copy(fd_data
, uap
, retval
);
6111 case NECP_CLIENT_ACTION_COPY_LIST
: {
6112 return_value
= necp_client_list(fd_data
, uap
, retval
);
6115 case NECP_CLIENT_ACTION_AGENT
: {
6116 return_value
= necp_client_agent_action(fd_data
, uap
, retval
);
6119 case NECP_CLIENT_ACTION_COPY_AGENT
: {
6120 return_value
= necp_client_copy_agent(fd_data
, uap
, retval
);
6123 case NECP_CLIENT_ACTION_AGENT_USE
: {
6124 return_value
= necp_client_agent_use(fd_data
, uap
, retval
);
6127 case NECP_CLIENT_ACTION_COPY_INTERFACE
: {
6128 return_value
= necp_client_copy_interface(fd_data
, uap
, retval
);
6131 case NECP_CLIENT_ACTION_COPY_ROUTE_STATISTICS
: {
6132 return_value
= necp_client_copy_route_statistics(fd_data
, uap
, retval
);
6135 case NECP_CLIENT_ACTION_UPDATE_CACHE
: {
6136 return_value
= necp_client_update_cache(fd_data
, uap
, retval
);
6139 case NECP_CLIENT_ACTION_COPY_CLIENT_UPDATE
: {
6140 return_value
= necp_client_copy_client_update(fd_data
, uap
, retval
);
6143 case NECP_CLIENT_ACTION_SIGN
: {
6144 return_value
= necp_client_sign(fd_data
, uap
, retval
);
6148 NECPLOG(LOG_ERR
, "necp_client_action unknown action (%u)", action
);
6149 return_value
= EINVAL
;
6154 file_drop(uap
->necp_fd
);
6156 return return_value
;
6159 #define NECP_MAX_MATCH_POLICY_PARAMETER_SIZE 1024
6162 necp_match_policy(struct proc
*p
, struct necp_match_policy_args
*uap
, int32_t *retval
)
6164 #pragma unused(retval)
6165 u_int8_t
*parameters
= NULL
;
6166 struct necp_aggregate_result returned_result
;
6174 if (uap
->parameters
== 0 || uap
->parameters_size
== 0 || uap
->parameters_size
> NECP_MAX_MATCH_POLICY_PARAMETER_SIZE
|| uap
->returned_result
== 0) {
6179 MALLOC(parameters
, u_int8_t
*, uap
->parameters_size
, M_NECP
, M_WAITOK
| M_ZERO
);
6180 if (parameters
== NULL
) {
6184 // Copy parameters in
6185 error
= copyin(uap
->parameters
, parameters
, uap
->parameters_size
);
6190 error
= necp_application_find_policy_match_internal(p
, parameters
, uap
->parameters_size
,
6191 &returned_result
, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
, NULL
, false, false);
6196 // Copy return value back
6197 error
= copyout(&returned_result
, uap
->returned_result
, sizeof(struct necp_aggregate_result
));
6202 if (parameters
!= NULL
) {
6203 FREE(parameters
, M_NECP
);
6208 /// Socket operations
6209 #define NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH 253
6212 necp_set_socket_attribute(u_int8_t
*buffer
, size_t buffer_length
, u_int8_t type
, char **buffer_p
)
6216 size_t string_size
= 0;
6217 char *local_string
= NULL
;
6218 u_int8_t
*value
= NULL
;
6220 cursor
= necp_buffer_find_tlv(buffer
, buffer_length
, 0, type
, NULL
, 0);
6222 // This will clear out the parameter
6226 string_size
= necp_buffer_get_tlv_length(buffer
, cursor
);
6227 if (string_size
== 0 || string_size
> NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH
) {
6228 // This will clear out the parameter
6232 MALLOC(local_string
, char *, string_size
+ 1, M_NECP
, M_WAITOK
| M_ZERO
);
6233 if (local_string
== NULL
) {
6234 NECPLOG(LOG_ERR
, "Failed to allocate a socket attribute buffer (size %d)", string_size
);
6238 value
= necp_buffer_get_tlv_value(buffer
, cursor
, NULL
);
6239 if (value
== NULL
) {
6240 NECPLOG0(LOG_ERR
, "Failed to get socket attribute");
6244 memcpy(local_string
, value
, string_size
);
6245 local_string
[string_size
] = 0;
6248 if (*buffer_p
!= NULL
) {
6249 FREE(*buffer_p
, M_NECP
);
6253 *buffer_p
= local_string
;
6256 if (local_string
!= NULL
) {
6257 FREE(local_string
, M_NECP
);
6263 necp_set_socket_attributes(struct socket
*so
, struct sockopt
*sopt
)
6266 u_int8_t
*buffer
= NULL
;
6267 struct inpcb
*inp
= NULL
;
6269 if ((SOCK_DOM(so
) != PF_INET
6271 && SOCK_DOM(so
) != PF_INET6
6278 inp
= sotoinpcb(so
);
6280 size_t valsize
= sopt
->sopt_valsize
;
6282 valsize
> ((sizeof(struct necp_tlv_header
) + NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH
) * 2)) {
6286 MALLOC(buffer
, u_int8_t
*, valsize
, M_NECP
, M_WAITOK
| M_ZERO
);
6287 if (buffer
== NULL
) {
6291 error
= sooptcopyin(sopt
, buffer
, valsize
, 0);
6296 error
= necp_set_socket_attribute(buffer
, valsize
, NECP_TLV_ATTRIBUTE_DOMAIN
, &inp
->inp_necp_attributes
.inp_domain
);
6298 NECPLOG0(LOG_ERR
, "Could not set domain TLV for socket attributes");
6302 error
= necp_set_socket_attribute(buffer
, valsize
, NECP_TLV_ATTRIBUTE_ACCOUNT
, &inp
->inp_necp_attributes
.inp_account
);
6304 NECPLOG0(LOG_ERR
, "Could not set account TLV for socket attributes");
6309 NECPLOG(LOG_DEBUG
, "Set on socket: Domain %s, Account %s", inp
->inp_necp_attributes
.inp_domain
, inp
->inp_necp_attributes
.inp_account
);
6312 if (buffer
!= NULL
) {
6313 FREE(buffer
, M_NECP
);
6320 necp_get_socket_attributes(struct socket
*so
, struct sockopt
*sopt
)
6323 u_int8_t
*buffer
= NULL
;
6324 u_int8_t
*cursor
= NULL
;
6326 struct inpcb
*inp
= NULL
;
6328 if ((SOCK_DOM(so
) != PF_INET
6330 && SOCK_DOM(so
) != PF_INET6
6337 inp
= sotoinpcb(so
);
6338 if (inp
->inp_necp_attributes
.inp_domain
!= NULL
) {
6339 valsize
+= sizeof(struct necp_tlv_header
) + strlen(inp
->inp_necp_attributes
.inp_domain
);
6341 if (inp
->inp_necp_attributes
.inp_account
!= NULL
) {
6342 valsize
+= sizeof(struct necp_tlv_header
) + strlen(inp
->inp_necp_attributes
.inp_account
);
6348 MALLOC(buffer
, u_int8_t
*, valsize
, M_NECP
, M_WAITOK
| M_ZERO
);
6349 if (buffer
== NULL
) {
6354 if (inp
->inp_necp_attributes
.inp_domain
!= NULL
) {
6355 cursor
= necp_buffer_write_tlv(cursor
, NECP_TLV_ATTRIBUTE_DOMAIN
, strlen(inp
->inp_necp_attributes
.inp_domain
), inp
->inp_necp_attributes
.inp_domain
,
6359 if (inp
->inp_necp_attributes
.inp_account
!= NULL
) {
6360 cursor
= necp_buffer_write_tlv(cursor
, NECP_TLV_ATTRIBUTE_ACCOUNT
, strlen(inp
->inp_necp_attributes
.inp_account
), inp
->inp_necp_attributes
.inp_account
,
6364 error
= sooptcopyout(sopt
, buffer
, valsize
);
6369 if (buffer
!= NULL
) {
6370 FREE(buffer
, M_NECP
);
6377 necp_create_nexus_assign_message(uuid_t nexus_instance
, u_int32_t nexus_port
, void *key
, uint32_t key_length
,
6378 struct necp_client_endpoint
*local_endpoint
, struct necp_client_endpoint
*remote_endpoint
, struct ether_addr
*local_ether_addr
,
6379 u_int32_t flow_adv_index
, void *flow_stats
, size_t *message_length
)
6381 u_int8_t
*buffer
= NULL
;
6382 u_int8_t
*cursor
= NULL
;
6384 bool has_nexus_assignment
= FALSE
;
6386 if (!uuid_is_null(nexus_instance
)) {
6387 has_nexus_assignment
= TRUE
;
6388 valsize
+= sizeof(struct necp_tlv_header
) + sizeof(uuid_t
);
6389 valsize
+= sizeof(struct necp_tlv_header
) + sizeof(u_int32_t
);
6391 if (flow_adv_index
!= NECP_FLOWADV_IDX_INVALID
) {
6392 valsize
+= sizeof(struct necp_tlv_header
) + sizeof(u_int32_t
);
6394 if (key
!= NULL
&& key_length
> 0) {
6395 valsize
+= sizeof(struct necp_tlv_header
) + key_length
;
6397 if (local_endpoint
!= NULL
) {
6398 valsize
+= sizeof(struct necp_tlv_header
) + sizeof(struct necp_client_endpoint
);
6400 if (remote_endpoint
!= NULL
) {
6401 valsize
+= sizeof(struct necp_tlv_header
) + sizeof(struct necp_client_endpoint
);
6403 if (local_ether_addr
!= NULL
) {
6404 valsize
+= sizeof(struct necp_tlv_header
) + sizeof(struct ether_addr
);
6406 if (flow_stats
!= NULL
) {
6407 valsize
+= sizeof(struct necp_tlv_header
) + sizeof(void *);
6413 MALLOC(buffer
, u_int8_t
*, valsize
, M_NETAGENT
, M_WAITOK
| M_ZERO
); // Use M_NETAGENT area, since it is expected upon free
6414 if (buffer
== NULL
) {
6419 if (has_nexus_assignment
) {
6420 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_RESULT_NEXUS_INSTANCE
, sizeof(uuid_t
), nexus_instance
, buffer
, valsize
);
6421 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_RESULT_NEXUS_PORT
, sizeof(u_int32_t
), &nexus_port
, buffer
, valsize
);
6423 if (flow_adv_index
!= NECP_FLOWADV_IDX_INVALID
) {
6424 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_RESULT_NEXUS_PORT_FLOW_INDEX
, sizeof(u_int32_t
), &flow_adv_index
, buffer
, valsize
);
6426 if (key
!= NULL
&& key_length
> 0) {
6427 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_PARAMETER_NEXUS_KEY
, key_length
, key
, buffer
, valsize
);
6429 if (local_endpoint
!= NULL
) {
6430 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_RESULT_LOCAL_ENDPOINT
, sizeof(struct necp_client_endpoint
), local_endpoint
, buffer
, valsize
);
6432 if (remote_endpoint
!= NULL
) {
6433 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_RESULT_REMOTE_ENDPOINT
, sizeof(struct necp_client_endpoint
), remote_endpoint
, buffer
, valsize
);
6435 if (local_ether_addr
!= NULL
) {
6436 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_RESULT_LOCAL_ETHER_ADDR
, sizeof(struct ether_addr
), local_ether_addr
, buffer
, valsize
);
6438 if (flow_stats
!= NULL
) {
6439 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_RESULT_NEXUS_FLOW_STATS
, sizeof(void *), &flow_stats
, buffer
, valsize
);
6442 *message_length
= valsize
;
6448 necp_inpcb_remove_cb(struct inpcb
*inp
)
6450 if (!uuid_is_null(inp
->necp_client_uuid
)) {
6451 necp_client_unregister_socket_flow(inp
->necp_client_uuid
, inp
);
6452 uuid_clear(inp
->necp_client_uuid
);
6457 necp_inpcb_dispose(struct inpcb
*inp
)
6459 necp_inpcb_remove_cb(inp
); // Clear out socket registrations if not yet done
6460 if (inp
->inp_necp_attributes
.inp_domain
!= NULL
) {
6461 FREE(inp
->inp_necp_attributes
.inp_domain
, M_NECP
);
6462 inp
->inp_necp_attributes
.inp_domain
= NULL
;
6464 if (inp
->inp_necp_attributes
.inp_account
!= NULL
) {
6465 FREE(inp
->inp_necp_attributes
.inp_account
, M_NECP
);
6466 inp
->inp_necp_attributes
.inp_account
= NULL
;
6471 necp_mppcb_dispose(struct mppcb
*mpp
)
6473 if (!uuid_is_null(mpp
->necp_client_uuid
)) {
6474 necp_client_unregister_multipath_cb(mpp
->necp_client_uuid
, mpp
);
6475 uuid_clear(mpp
->necp_client_uuid
);
6482 necp_client_init(void)
6484 necp_fd_grp_attr
= lck_grp_attr_alloc_init();
6485 if (necp_fd_grp_attr
== NULL
) {
6486 panic("lck_grp_attr_alloc_init failed\n");
6490 necp_fd_mtx_grp
= lck_grp_alloc_init("necp_fd", necp_fd_grp_attr
);
6491 if (necp_fd_mtx_grp
== NULL
) {
6492 panic("lck_grp_alloc_init failed\n");
6496 necp_fd_mtx_attr
= lck_attr_alloc_init();
6497 if (necp_fd_mtx_attr
== NULL
) {
6498 panic("lck_attr_alloc_init failed\n");
6502 necp_client_fd_size
= sizeof(struct necp_fd_data
);
6503 necp_client_fd_zone
= zinit(necp_client_fd_size
,
6504 NECP_CLIENT_FD_ZONE_MAX
* necp_client_fd_size
,
6505 0, NECP_CLIENT_FD_ZONE_NAME
);
6506 if (necp_client_fd_zone
== NULL
) {
6507 panic("zinit(necp_client_fd) failed\n");
6511 necp_flow_size
= sizeof(struct necp_client_flow
);
6512 necp_flow_cache
= mcache_create(NECP_FLOW_ZONE_NAME
, necp_flow_size
, sizeof(uint64_t), 0, MCR_SLEEP
);
6513 if (necp_flow_cache
== NULL
) {
6514 panic("mcache_create(necp_flow_cache) failed\n");
6518 necp_flow_registration_size
= sizeof(struct necp_client_flow_registration
);
6519 necp_flow_registration_cache
= mcache_create(NECP_FLOW_REGISTRATION_ZONE_NAME
, necp_flow_registration_size
, sizeof(uint64_t), 0, MCR_SLEEP
);
6520 if (necp_flow_registration_cache
== NULL
) {
6521 panic("mcache_create(necp_client_flow_registration) failed\n");
6525 necp_client_update_tcall
= thread_call_allocate_with_options(necp_update_all_clients_callout
, NULL
,
6526 THREAD_CALL_PRIORITY_KERNEL
, THREAD_CALL_OPTIONS_ONCE
);
6527 VERIFY(necp_client_update_tcall
!= NULL
);
6529 lck_rw_init(&necp_fd_lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
6530 lck_rw_init(&necp_observer_lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
6531 lck_rw_init(&necp_client_tree_lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
6532 lck_rw_init(&necp_flow_tree_lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
6533 lck_rw_init(&necp_collect_stats_list_lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
6535 LIST_INIT(&necp_fd_list
);
6536 LIST_INIT(&necp_fd_observer_list
);
6537 LIST_INIT(&necp_collect_stats_flow_list
);
6539 RB_INIT(&necp_client_global_tree
);
6540 RB_INIT(&necp_client_flow_global_tree
);
6546 necp_client_reap_caches(boolean_t purge
)
6548 mcache_reap_now(necp_flow_cache
, purge
);
6549 mcache_reap_now(necp_flow_registration_cache
, purge
);