2 * Copyright (c) 2015-2018 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/ip.h>
45 #include <netinet/ip6.h>
46 #include <netinet/mp_pcb.h>
47 #include <netinet/tcp_cc.h>
48 #include <netinet/tcp_fsm.h>
49 #include <netinet/tcp_cache.h>
50 #include <netinet6/in6_var.h>
52 #include <sys/domain.h>
53 #include <sys/file_internal.h>
54 #include <sys/kauth.h>
55 #include <sys/kernel.h>
56 #include <sys/malloc.h>
59 #include <sys/protosw.h>
60 #include <sys/queue.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/sysproto.h>
64 #include <sys/systm.h>
65 #include <sys/types.h>
66 #include <sys/codesign.h>
67 #include <libkern/section_keywords.h>
71 * NECP Client Architecture
72 * ------------------------------------------------
73 * See <net/necp.c> for a discussion on NECP database architecture.
75 * Each client of NECP provides a set of parameters for a connection or network state
76 * evaluation, on which NECP policy evaluation is run. This produces a policy result
77 * which can be accessed by the originating process, along with events for when policies
78 * results have changed.
80 * ------------------------------------------------
82 * ------------------------------------------------
83 * A process opens an NECP file descriptor using necp_open(). This is a very simple
84 * file descriptor, upon which the process may do the following operations:
85 * - necp_client_action(...), to add/remove/query clients
86 * - kqueue, to watch for readable events
87 * - close(), to close the client session and release all clients
89 * Client objects are allocated structures that hang off of the file descriptor. Each
91 * - Client ID, a UUID that references the client across the system
92 * - Parameters, a buffer of TLVs that describe the client's connection parameters,
93 * such as the remote and local endpoints, interface requirements, etc.
94 * - Result, a buffer of TLVs containing the current policy evaluation for the client.
95 * This result will be updated whenever a network change occurs that impacts the
96 * policy result for that client.
102 * ==================================
104 * +--------------+ +--------------+ +--------------+
105 * | Client ID | | Client ID | | Client ID |
106 * | ---- | | ---- | | ---- |
107 * | Parameters | | Parameters | | Parameters |
108 * | ---- | | ---- | | ---- |
109 * | Result | | Result | | Result |
110 * +--------------+ +--------------+ +--------------+
112 * ------------------------------------------------
114 * ------------------------------------------------
115 * - Add. Input parameters as a buffer of TLVs, and output a client ID. Allocates a
116 * new client structure on the file descriptor.
117 * - Remove. Input a client ID. Removes a client structure from the file descriptor.
118 * - Copy Parameters. Input a client ID, and output parameter TLVs.
119 * - Copy Result. Input a client ID, and output result TLVs. Alternatively, input empty
120 * client ID and get next unread client result.
121 * - Copy List. List all client IDs.
123 * ------------------------------------------------
124 * Client Policy Evaluation
125 * ------------------------------------------------
126 * Policies are evaluated for clients upon client creation, and upon update events,
127 * which are network/agent/policy changes coalesced by a timer.
129 * The policy evaluation goes through the following steps:
130 * 1. Parse client parameters.
131 * 2. Select a scoped interface if applicable. This involves using require/prohibit
132 * parameters, along with the local address, to select the most appropriate interface
133 * if not explicitly set by the client parameters.
134 * 3. Run NECP application-level policy evalution
135 * 4. Set policy result into client result buffer.
137 * ------------------------------------------------
139 * ------------------------------------------------
140 * If necp_open() is called with the NECP_OPEN_FLAG_OBSERVER flag, and the process
141 * passes the necessary privilege check, the fd is allowed to use necp_client_action()
142 * to copy client state attached to the file descriptors of other processes, and to
143 * list all client IDs on the system.
146 extern u_int32_t necp_debug
;
148 static int noop_read(struct fileproc
*, struct uio
*, int, vfs_context_t
);
149 static int noop_write(struct fileproc
*, struct uio
*, int, vfs_context_t
);
150 static int noop_ioctl(struct fileproc
*, unsigned long, caddr_t
,
152 static int necpop_select(struct fileproc
*, int, void *, vfs_context_t
);
153 static int necpop_close(struct fileglob
*, vfs_context_t
);
154 static int necpop_kqfilter(struct fileproc
*, struct knote
*,
155 struct kevent_internal_s
*kev
, vfs_context_t
);
158 static int necp_timeout_microseconds
= 1000 * 100; // 100ms
159 static int necp_timeout_leeway_microseconds
= 1000 * 500; // 500ms
161 static int necp_client_fd_count
= 0;
162 static int necp_observer_fd_count
= 0;
163 static int necp_client_count
= 0;
164 static int necp_socket_flow_count
= 0;
165 static int necp_if_flow_count
= 0;
166 static int necp_observer_message_limit
= 256;
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
203 #define NECP_MAX_PARSED_PARAMETERS 16
204 struct necp_client_parsed_parameters
{
205 u_int32_t valid_fields
;
207 union necp_sockaddr_union local_addr
;
208 union necp_sockaddr_union remote_addr
;
209 u_int32_t required_interface_index
;
210 char prohibited_interfaces
[IFXNAMSIZ
][NECP_MAX_PARSED_PARAMETERS
];
211 u_int8_t required_interface_type
;
212 u_int8_t prohibited_interface_types
[NECP_MAX_PARSED_PARAMETERS
];
213 struct necp_client_parameter_netagent_type required_netagent_types
[NECP_MAX_PARSED_PARAMETERS
];
214 struct necp_client_parameter_netagent_type prohibited_netagent_types
[NECP_MAX_PARSED_PARAMETERS
];
215 struct necp_client_parameter_netagent_type preferred_netagent_types
[NECP_MAX_PARSED_PARAMETERS
];
216 struct necp_client_parameter_netagent_type avoided_netagent_types
[NECP_MAX_PARSED_PARAMETERS
];
217 uuid_t required_netagents
[NECP_MAX_PARSED_PARAMETERS
];
218 uuid_t prohibited_netagents
[NECP_MAX_PARSED_PARAMETERS
];
219 uuid_t preferred_netagents
[NECP_MAX_PARSED_PARAMETERS
];
220 uuid_t avoided_netagents
[NECP_MAX_PARSED_PARAMETERS
];
221 u_int16_t ip_protocol
;
223 uuid_t effective_uuid
;
224 u_int32_t traffic_class
;
228 necp_find_matching_interface_index(struct necp_client_parsed_parameters
*parsed_parameters
,
229 u_int
*return_ifindex
, bool *validate_agents
);
232 necp_ifnet_matches_local_address(struct ifnet
*ifp
, struct sockaddr
*sa
);
235 necp_ifnet_matches_parameters(struct ifnet
*ifp
,
236 struct necp_client_parsed_parameters
*parsed_parameters
,
237 u_int32_t
*preferred_count
,
238 bool secondary_interface
);
240 static const struct fileops necp_fd_ops
= {
241 .fo_type
= DTYPE_NETPOLICY
,
242 .fo_read
= noop_read
,
243 .fo_write
= noop_write
,
244 .fo_ioctl
= noop_ioctl
,
245 .fo_select
= necpop_select
,
246 .fo_close
= necpop_close
,
247 .fo_kqfilter
= necpop_kqfilter
,
251 struct necp_client_assertion
{
252 LIST_ENTRY(necp_client_assertion
) assertion_chain
;
253 uuid_t asserted_netagent
;
256 struct necp_client_flow_header
{
257 struct necp_tlv_header outer_header
;
258 struct necp_tlv_header flow_id_tlv_header
;
260 struct necp_tlv_header flags_tlv_header
;
261 u_int32_t flags_value
;
262 struct necp_tlv_header interface_tlv_header
;
263 struct necp_client_result_interface interface_value
;
264 } __attribute__((__packed__
));
266 struct necp_client_flow_protoctl_event_header
{
267 struct necp_tlv_header protoctl_tlv_header
;
268 struct necp_client_flow_protoctl_event protoctl_event
;
269 } __attribute__((__packed__
));
271 struct necp_client_nexus_flow_header
{
272 struct necp_client_flow_header flow_header
;
273 struct necp_tlv_header agent_tlv_header
;
274 struct necp_client_result_netagent agent_value
;
275 struct necp_tlv_header tfo_cookie_tlv_header
;
276 u_int8_t tfo_cookie_value
[NECP_TFO_COOKIE_LEN_MAX
];
277 } __attribute__((__packed__
));
280 struct necp_client_flow
{
281 LIST_ENTRY(necp_client_flow
) flow_chain
;
282 unsigned invalid
: 1;
283 unsigned nexus
: 1; // If true, flow is a nexus; if false, flow is attached to socket
286 unsigned assigned
: 1;
287 unsigned has_protoctl_event
: 1;
288 unsigned check_tcp_heuristics
: 1;
289 unsigned _reserved
: 1;
294 necp_client_flow_cb cb
;
297 uint32_t interface_index
;
298 uint16_t interface_flags
;
299 uint32_t necp_flow_flags
;
300 struct necp_client_flow_protoctl_event protoctl_event
;
301 union necp_sockaddr_union local_addr
;
302 union necp_sockaddr_union remote_addr
;
304 size_t assigned_results_length
;
305 u_int8_t
*assigned_results
;
308 struct necp_client_flow_registration
{
309 RB_ENTRY(necp_client_flow_registration
) fd_link
;
310 RB_ENTRY(necp_client_flow_registration
) global_link
;
311 RB_ENTRY(necp_client_flow_registration
) client_link
;
312 LIST_ENTRY(necp_client_flow_registration
) collect_stats_chain
;
313 uuid_t registration_id
;
315 unsigned flow_result_read
: 1;
316 unsigned defunct
: 1;
317 void *interface_handle
;
318 necp_client_flow_cb interface_cb
;
319 struct necp_client
*client
;
320 LIST_HEAD(_necp_registration_flow_list
, necp_client_flow
) flow_list
;
321 u_int64_t last_interface_details
__attribute__((aligned(sizeof(u_int64_t
))));
324 static int necp_client_flow_id_cmp(struct necp_client_flow_registration
*flow0
, struct necp_client_flow_registration
*flow1
);
326 RB_HEAD(_necp_client_flow_tree
, necp_client_flow_registration
);
327 RB_PROTOTYPE_PREV(_necp_client_flow_tree
, necp_client_flow_registration
, client_link
, necp_client_flow_id_cmp
);
328 RB_GENERATE_PREV(_necp_client_flow_tree
, necp_client_flow_registration
, client_link
, necp_client_flow_id_cmp
);
330 #define NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT 4
331 #define NECP_CLIENT_MAX_INTERFACE_OPTIONS 16
333 #define NECP_CLIENT_INTERFACE_OPTION_EXTRA_COUNT (NECP_CLIENT_MAX_INTERFACE_OPTIONS - NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT)
336 RB_ENTRY(necp_client
) link
;
337 RB_ENTRY(necp_client
) global_link
;
339 decl_lck_mtx_data(, lock
);
340 decl_lck_mtx_data(, route_lock
);
341 uint32_t reference_count
;
344 unsigned result_read
: 1;
345 unsigned allow_multiple_flows
: 1;
346 unsigned legacy_client_is_flow
: 1;
348 unsigned background
: 1;
349 unsigned background_update
: 1;
350 unsigned platform_binary
: 1;
352 size_t result_length
;
353 u_int8_t result
[NECP_MAX_CLIENT_RESULT_SIZE
];
355 necp_policy_id policy_id
;
357 u_int16_t ip_protocol
;
360 struct _necp_client_flow_tree flow_registrations
;
361 LIST_HEAD(_necp_client_assertion_list
, necp_client_assertion
) assertion_list
;
363 struct rtentry
*current_route
;
365 struct necp_client_interface_option interface_options
[NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
];
366 struct necp_client_interface_option
*extra_interface_options
;
367 u_int8_t interface_option_count
; // Number in interface_options + extra_interface_options
369 struct necp_client_result_netagent failed_trigger_agent
;
373 size_t parameters_length
;
374 u_int8_t parameters
[0];
377 #define NECP_CLIENT_LOCK(_c) lck_mtx_lock(&_c->lock)
378 #define NECP_CLIENT_UNLOCK(_c) lck_mtx_unlock(&_c->lock)
379 #define NECP_CLIENT_ASSERT_LOCKED(_c) LCK_MTX_ASSERT(&_c->lock, LCK_MTX_ASSERT_OWNED)
380 #define NECP_CLIENT_ASSERT_UNLOCKED(_c) LCK_MTX_ASSERT(&_c->lock, LCK_MTX_ASSERT_NOTOWNED)
382 #define NECP_CLIENT_ROUTE_LOCK(_c) lck_mtx_lock(&_c->route_lock)
383 #define NECP_CLIENT_ROUTE_UNLOCK(_c) lck_mtx_unlock(&_c->route_lock)
385 static void necp_client_retain_locked(struct necp_client
*client
);
386 static void necp_client_retain(struct necp_client
*client
);
387 static bool necp_client_release_locked(struct necp_client
*client
);
390 necp_client_add_assertion(struct necp_client
*client
, uuid_t netagent_uuid
);
393 necp_client_remove_assertion(struct necp_client
*client
, uuid_t netagent_uuid
);
395 LIST_HEAD(_necp_flow_registration_list
, necp_client_flow_registration
);
396 static struct _necp_flow_registration_list necp_collect_stats_flow_list
;
398 struct necp_flow_defunct
{
399 LIST_ENTRY(necp_flow_defunct
) chain
;
407 LIST_HEAD(_necp_flow_defunct_list
, necp_flow_defunct
);
409 static int necp_client_id_cmp(struct necp_client
*client0
, struct necp_client
*client1
);
411 RB_HEAD(_necp_client_tree
, necp_client
);
412 RB_PROTOTYPE_PREV(_necp_client_tree
, necp_client
, link
, necp_client_id_cmp
);
413 RB_GENERATE_PREV(_necp_client_tree
, necp_client
, link
, necp_client_id_cmp
);
415 RB_HEAD(_necp_client_global_tree
, necp_client
);
416 RB_PROTOTYPE_PREV(_necp_client_global_tree
, necp_client
, global_link
, necp_client_id_cmp
);
417 RB_GENERATE_PREV(_necp_client_global_tree
, necp_client
, global_link
, necp_client_id_cmp
);
419 RB_HEAD(_necp_fd_flow_tree
, necp_client_flow_registration
);
420 RB_PROTOTYPE_PREV(_necp_fd_flow_tree
, necp_client_flow_registration
, fd_link
, necp_client_flow_id_cmp
);
421 RB_GENERATE_PREV(_necp_fd_flow_tree
, necp_client_flow_registration
, fd_link
, necp_client_flow_id_cmp
);
423 RB_HEAD(_necp_client_flow_global_tree
, necp_client_flow_registration
);
424 RB_PROTOTYPE_PREV(_necp_client_flow_global_tree
, necp_client_flow_registration
, global_link
, necp_client_flow_id_cmp
);
425 RB_GENERATE_PREV(_necp_client_flow_global_tree
, necp_client_flow_registration
, global_link
, necp_client_flow_id_cmp
);
427 static struct _necp_client_global_tree necp_client_global_tree
;
428 static struct _necp_client_flow_global_tree necp_client_flow_global_tree
;
430 struct necp_client_update
{
431 TAILQ_ENTRY(necp_client_update
) chain
;
435 size_t update_length
;
436 struct necp_client_observer_update update
;
440 #define NAIF_ATTACHED 0x1 // arena is attached to list
441 #define NAIF_REDIRECT 0x2 // arena mmap has been redirected
442 #define NAIF_DEFUNCT 0x4 // arena is now defunct
444 struct necp_fd_data
{
445 u_int8_t necp_fd_type
;
446 LIST_ENTRY(necp_fd_data
) chain
;
447 struct _necp_client_tree clients
;
448 struct _necp_fd_flow_tree flows
;
449 TAILQ_HEAD(_necp_client_update_list
, necp_client_update
) update_list
;
453 decl_lck_mtx_data(, fd_lock
);
457 #define NECP_FD_LOCK(_f) lck_mtx_lock(&_f->fd_lock)
458 #define NECP_FD_UNLOCK(_f) lck_mtx_unlock(&_f->fd_lock)
459 #define NECP_FD_ASSERT_LOCKED(_f) LCK_MTX_ASSERT(&_f->fd_lock, LCK_MTX_ASSERT_OWNED)
460 #define NECP_FD_ASSERT_UNLOCKED(_f) LCK_MTX_ASSERT(&_f->fd_lock, LCK_MTX_ASSERT_NOTOWNED)
462 static LIST_HEAD(_necp_fd_list
, necp_fd_data
) necp_fd_list
;
463 static LIST_HEAD(_necp_fd_observer_list
, necp_fd_data
) necp_fd_observer_list
;
465 #define NECP_CLIENT_FD_ZONE_MAX 128
466 #define NECP_CLIENT_FD_ZONE_NAME "necp.clientfd"
468 static unsigned int necp_client_fd_size
; /* size of zone element */
469 static struct zone
*necp_client_fd_zone
; /* zone for necp_fd_data */
471 #define NECP_FLOW_ZONE_NAME "necp.flow"
472 #define NECP_FLOW_REGISTRATION_ZONE_NAME "necp.flowregistration"
474 static unsigned int necp_flow_size
; /* size of necp_client_flow */
475 static struct mcache
*necp_flow_cache
; /* cache for necp_client_flow */
477 static unsigned int necp_flow_registration_size
; /* size of necp_client_flow_registration */
478 static struct mcache
*necp_flow_registration_cache
; /* cache for necp_client_flow_registration */
480 #define NECP_ARENA_INFO_ZONE_MAX 128
481 #define NECP_ARENA_INFO_ZONE_NAME "necp.arenainfo"
484 static lck_grp_attr_t
*necp_fd_grp_attr
= NULL
;
485 static lck_attr_t
*necp_fd_mtx_attr
= NULL
;
486 static lck_grp_t
*necp_fd_mtx_grp
= NULL
;
488 decl_lck_rw_data(static, necp_fd_lock
);
489 decl_lck_rw_data(static, necp_observer_lock
);
490 decl_lck_rw_data(static, necp_client_tree_lock
);
491 decl_lck_rw_data(static, necp_flow_tree_lock
);
492 decl_lck_rw_data(static, necp_collect_stats_list_lock
);
494 #define NECP_STATS_LIST_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_collect_stats_list_lock)
495 #define NECP_STATS_LIST_LOCK_SHARED() lck_rw_lock_shared(&necp_collect_stats_list_lock)
496 #define NECP_STATS_LIST_UNLOCK() lck_rw_done(&necp_collect_stats_list_lock)
498 #define NECP_CLIENT_TREE_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_client_tree_lock)
499 #define NECP_CLIENT_TREE_LOCK_SHARED() lck_rw_lock_shared(&necp_client_tree_lock)
500 #define NECP_CLIENT_TREE_UNLOCK() lck_rw_done(&necp_client_tree_lock)
501 #define NECP_CLIENT_TREE_ASSERT_LOCKED() LCK_RW_ASSERT(&necp_client_tree_lock, LCK_RW_ASSERT_HELD)
503 #define NECP_FLOW_TREE_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_flow_tree_lock)
504 #define NECP_FLOW_TREE_LOCK_SHARED() lck_rw_lock_shared(&necp_flow_tree_lock)
505 #define NECP_FLOW_TREE_UNLOCK() lck_rw_done(&necp_flow_tree_lock)
506 #define NECP_FLOW_TREE_ASSERT_LOCKED() LCK_RW_ASSERT(&necp_flow_tree_lock, LCK_RW_ASSERT_HELD)
508 #define NECP_FD_LIST_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_fd_lock)
509 #define NECP_FD_LIST_LOCK_SHARED() lck_rw_lock_shared(&necp_fd_lock)
510 #define NECP_FD_LIST_UNLOCK() lck_rw_done(&necp_fd_lock)
512 #define NECP_OBSERVER_LIST_LOCK_EXCLUSIVE() lck_rw_lock_exclusive(&necp_observer_lock)
513 #define NECP_OBSERVER_LIST_LOCK_SHARED() lck_rw_lock_shared(&necp_observer_lock)
514 #define NECP_OBSERVER_LIST_UNLOCK() lck_rw_done(&necp_observer_lock)
518 // Take NECP_FD_LIST_LOCK when accessing or modifying the necp_fd_list
519 // Take NECP_CLIENT_TREE_LOCK when accessing or modifying the necp_client_global_tree
520 // Take NECP_FLOW_TREE_LOCK when accessing or modifying the necp_client_flow_global_tree
521 // Take NECP_STATS_LIST_LOCK when accessing or modifying the necp_collect_stats_flow_list
522 // Take NECP_FD_LOCK when accessing or modifying an necp_fd_data entry
523 // Take NECP_CLIENT_LOCK when accessing or modifying a single necp_client
524 // Take NECP_CLIENT_ROUTE_LOCK when accessing or modifying a client's route
526 // Precedence, where 1 is the first lock that must be taken
527 // 1. NECP_FD_LIST_LOCK
528 // 2. NECP_FD_LOCK (any)
529 // 3. NECP_CLIENT_TREE_LOCK
530 // 4. NECP_CLIENT_LOCK (any)
531 // 5. NECP_FLOW_TREE_LOCK
532 // 6. NECP_STATS_LIST_LOCK
533 // 7. NECP_CLIENT_ROUTE_LOCK (any)
535 static thread_call_t necp_client_update_tcall
;
538 /// NECP file descriptor functions
541 noop_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
543 #pragma unused(fp, uio, flags, ctx)
548 noop_write(struct fileproc
*fp
, struct uio
*uio
, int flags
,
551 #pragma unused(fp, uio, flags, ctx)
556 noop_ioctl(struct fileproc
*fp
, unsigned long com
, caddr_t data
,
559 #pragma unused(fp, com, data, ctx)
564 necp_fd_notify(struct necp_fd_data
*fd_data
, bool locked
)
566 struct selinfo
*si
= &fd_data
->si
;
569 NECP_FD_LOCK(fd_data
);
574 // use a non-zero hint to tell the notification from the
575 // call done in kqueue_scan() which uses 0
576 KNOTE(&si
->si_note
, 1); // notification
579 NECP_FD_UNLOCK(fd_data
);
584 necp_client_has_unread_flows(struct necp_client
*client
)
586 NECP_CLIENT_ASSERT_LOCKED(client
);
587 struct necp_client_flow_registration
*flow_registration
= NULL
;
588 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
589 if (!flow_registration
->flow_result_read
) {
597 necp_fd_poll(struct necp_fd_data
*fd_data
, int events
, void *wql
, struct proc
*p
, int is_kevent
)
599 #pragma unused(wql, p, is_kevent)
602 u_int want_rx
= events
& (POLLIN
| POLLRDNORM
);
604 if (fd_data
->flags
& NECP_OPEN_FLAG_PUSH_OBSERVER
) {
605 // Push-mode observers are readable when they have a new update
606 if (!TAILQ_EMPTY(&fd_data
->update_list
)) {
610 // Standard fds are readable when some client is unread
611 struct necp_client
*client
= NULL
;
612 bool has_unread_clients
= FALSE
;
613 RB_FOREACH(client
, _necp_client_tree
, &fd_data
->clients
) {
614 NECP_CLIENT_LOCK(client
);
615 if (!client
->result_read
|| necp_client_has_unread_flows(client
)) {
616 has_unread_clients
= TRUE
;
618 NECP_CLIENT_UNLOCK(client
);
619 if (has_unread_clients
) {
624 if (has_unread_clients
) {
634 necp_generate_client_id(uuid_t client_id
, bool is_flow
)
636 uuid_generate_random(client_id
);
639 client_id
[9] |= 0x01;
641 client_id
[9] &= ~0x01;
646 necp_client_id_is_flow(uuid_t client_id
)
648 return (client_id
[9] & 0x01);
651 static struct necp_client
*
652 necp_find_client_and_lock(uuid_t client_id
)
654 NECP_CLIENT_TREE_ASSERT_LOCKED();
656 struct necp_client
*client
= NULL
;
658 if (necp_client_id_is_flow(client_id
)) {
659 NECP_FLOW_TREE_LOCK_SHARED();
660 struct necp_client_flow_registration find
;
661 uuid_copy(find
.registration_id
, client_id
);
662 struct necp_client_flow_registration
*flow
= RB_FIND(_necp_client_flow_global_tree
, &necp_client_flow_global_tree
, &find
);
664 client
= flow
->client
;
666 NECP_FLOW_TREE_UNLOCK();
668 struct necp_client find
;
669 uuid_copy(find
.client_id
, client_id
);
670 client
= RB_FIND(_necp_client_global_tree
, &necp_client_global_tree
, &find
);
673 if (client
!= NULL
) {
674 NECP_CLIENT_LOCK(client
);
680 static struct necp_client_flow_registration
*
681 necp_client_find_flow(struct necp_client
*client
, uuid_t flow_id
)
683 NECP_CLIENT_ASSERT_LOCKED(client
);
684 struct necp_client_flow_registration
*flow
= NULL
;
686 if (necp_client_id_is_flow(flow_id
)) {
687 struct necp_client_flow_registration find
;
688 uuid_copy(find
.registration_id
, flow_id
);
689 flow
= RB_FIND(_necp_client_flow_tree
, &client
->flow_registrations
, &find
);
691 flow
= RB_ROOT(&client
->flow_registrations
);
697 static struct necp_client
*
698 necp_client_fd_find_client_unlocked(struct necp_fd_data
*client_fd
, uuid_t client_id
)
700 NECP_FD_ASSERT_LOCKED(client_fd
);
701 struct necp_client
*client
= NULL
;
703 if (necp_client_id_is_flow(client_id
)) {
704 struct necp_client_flow_registration find
;
705 uuid_copy(find
.registration_id
, client_id
);
706 struct necp_client_flow_registration
*flow
= RB_FIND(_necp_fd_flow_tree
, &client_fd
->flows
, &find
);
708 client
= flow
->client
;
711 struct necp_client find
;
712 uuid_copy(find
.client_id
, client_id
);
713 client
= RB_FIND(_necp_client_tree
, &client_fd
->clients
, &find
);
719 static struct necp_client
*
720 necp_client_fd_find_client_and_lock(struct necp_fd_data
*client_fd
, uuid_t client_id
)
722 struct necp_client
*client
= necp_client_fd_find_client_unlocked(client_fd
, client_id
);
723 if (client
!= NULL
) {
724 NECP_CLIENT_LOCK(client
);
731 necp_client_id_cmp(struct necp_client
*client0
, struct necp_client
*client1
)
733 return (uuid_compare(client0
->client_id
, client1
->client_id
));
737 necp_client_flow_id_cmp(struct necp_client_flow_registration
*flow0
, struct necp_client_flow_registration
*flow1
)
739 return (uuid_compare(flow0
->registration_id
, flow1
->registration_id
));
743 necpop_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
745 #pragma unused(fp, which, wql, ctx)
747 struct necp_fd_data
*fd_data
= NULL
;
752 fd_data
= (struct necp_fd_data
*)fp
->f_fglob
->fg_data
;
753 if (fd_data
== NULL
) {
757 procp
= vfs_context_proc(ctx
);
770 NECP_FD_LOCK(fd_data
);
771 revents
= necp_fd_poll(fd_data
, events
, wql
, procp
, 0);
772 NECP_FD_UNLOCK(fd_data
);
774 return ((events
& revents
) ? 1 : 0);
778 necp_fd_knrdetach(struct knote
*kn
)
780 struct necp_fd_data
*fd_data
= (struct necp_fd_data
*)kn
->kn_hook
;
781 struct selinfo
*si
= &fd_data
->si
;
783 NECP_FD_LOCK(fd_data
);
784 KNOTE_DETACH(&si
->si_note
, kn
);
785 NECP_FD_UNLOCK(fd_data
);
789 necp_fd_knread(struct knote
*kn
, long hint
)
791 #pragma unused(kn, hint)
792 return 1; /* assume we are ready */
796 necp_fd_knrprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
)
799 struct necp_fd_data
*fd_data
;
803 fd_data
= (struct necp_fd_data
*)kn
->kn_hook
;
805 NECP_FD_LOCK(fd_data
);
806 revents
= necp_fd_poll(fd_data
, POLLIN
, NULL
, current_proc(), 1);
807 res
= ((revents
& POLLIN
) != 0);
809 *kev
= kn
->kn_kevent
;
811 NECP_FD_UNLOCK(fd_data
);
816 necp_fd_knrtouch(struct knote
*kn
, struct kevent_internal_s
*kev
)
819 struct necp_fd_data
*fd_data
;
822 fd_data
= (struct necp_fd_data
*)kn
->kn_hook
;
824 NECP_FD_LOCK(fd_data
);
825 revents
= necp_fd_poll(fd_data
, POLLIN
, NULL
, current_proc(), 1);
826 NECP_FD_UNLOCK(fd_data
);
828 return ((revents
& POLLIN
) != 0);
831 SECURITY_READ_ONLY_EARLY(struct filterops
) necp_fd_rfiltops
= {
833 .f_detach
= necp_fd_knrdetach
,
834 .f_event
= necp_fd_knread
,
835 .f_touch
= necp_fd_knrtouch
,
836 .f_process
= necp_fd_knrprocess
,
840 necpop_kqfilter(struct fileproc
*fp
, struct knote
*kn
,
841 __unused
struct kevent_internal_s
*kev
, vfs_context_t ctx
)
843 #pragma unused(fp, ctx)
844 struct necp_fd_data
*fd_data
= NULL
;
847 if (kn
->kn_filter
!= EVFILT_READ
) {
848 NECPLOG(LOG_ERR
, "bad filter request %d", kn
->kn_filter
);
849 kn
->kn_flags
= EV_ERROR
;
850 kn
->kn_data
= EINVAL
;
854 fd_data
= (struct necp_fd_data
*)kn
->kn_fp
->f_fglob
->fg_data
;
855 if (fd_data
== NULL
) {
856 NECPLOG0(LOG_ERR
, "No channel for kqfilter");
857 kn
->kn_flags
= EV_ERROR
;
858 kn
->kn_data
= ENOENT
;
862 NECP_FD_LOCK(fd_data
);
863 kn
->kn_filtid
= EVFILTID_NECP_FD
;
864 kn
->kn_hook
= fd_data
;
865 KNOTE_ATTACH(&fd_data
->si
.si_note
, kn
);
867 revents
= necp_fd_poll(fd_data
, POLLIN
, NULL
, current_proc(), 1);
869 NECP_FD_UNLOCK(fd_data
);
871 return ((revents
& POLLIN
) != 0);
874 #define INTERFACE_FLAGS_SHIFT 32
875 #define INTERFACE_FLAGS_MASK 0xffff
876 #define INTERFACE_INDEX_SHIFT 0
877 #define INTERFACE_INDEX_MASK 0xffffffff
880 combine_interface_details(uint32_t interface_index
, uint16_t interface_flags
)
882 return (((uint64_t)interface_flags
& INTERFACE_FLAGS_MASK
) << INTERFACE_FLAGS_SHIFT
|
883 ((uint64_t)interface_index
& INTERFACE_INDEX_MASK
) << INTERFACE_INDEX_SHIFT
);
888 necp_defunct_flow_registration(struct necp_client
*client
,
889 struct necp_client_flow_registration
*flow_registration
,
890 struct _necp_flow_defunct_list
*defunct_list
)
892 NECP_CLIENT_ASSERT_LOCKED(client
);
894 if (!flow_registration
->defunct
) {
895 bool needs_defunct
= false;
896 struct necp_client_flow
*search_flow
= NULL
;
897 LIST_FOREACH(search_flow
, &flow_registration
->flow_list
, flow_chain
) {
898 if (search_flow
->nexus
&&
899 !uuid_is_null(search_flow
->u
.nexus_agent
)) {
901 // Save defunct values for the nexus
902 if (defunct_list
!= NULL
) {
903 // Sleeping alloc won't fail; copy only what's necessary
904 struct necp_flow_defunct
*flow_defunct
= _MALLOC(sizeof (struct necp_flow_defunct
),
905 M_NECP
, M_WAITOK
| M_ZERO
);
906 uuid_copy(flow_defunct
->nexus_agent
, search_flow
->u
.nexus_agent
);
907 uuid_copy(flow_defunct
->flow_id
, ((flow_registration
->flags
& NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID
) ?
909 flow_registration
->registration_id
));
910 flow_defunct
->proc_pid
= client
->proc_pid
;
911 flow_defunct
->agent_handle
= client
->agent_handle
;
913 // Add to the list provided by caller
914 LIST_INSERT_HEAD(defunct_list
, flow_defunct
, chain
);
917 needs_defunct
= true;
923 // Only set defunct if there was some assigned flow
924 flow_registration
->defunct
= true;
930 necp_defunct_client_for_policy(struct necp_client
*client
,
931 struct _necp_flow_defunct_list
*defunct_list
)
933 NECP_CLIENT_ASSERT_LOCKED(client
);
935 struct necp_client_flow_registration
*flow_registration
= NULL
;
936 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
937 necp_defunct_flow_registration(client
, flow_registration
, defunct_list
);
942 necp_client_free(struct necp_client
*client
)
944 NECP_CLIENT_ASSERT_LOCKED(client
);
946 NECP_CLIENT_UNLOCK(client
);
948 FREE(client
->extra_interface_options
, M_NECP
);
949 client
->extra_interface_options
= NULL
;
951 lck_mtx_destroy(&client
->route_lock
, necp_fd_mtx_grp
);
952 lck_mtx_destroy(&client
->lock
, necp_fd_mtx_grp
);
954 FREE(client
, M_NECP
);
958 necp_client_retain_locked(struct necp_client
*client
)
960 NECP_CLIENT_ASSERT_LOCKED(client
);
962 client
->reference_count
++;
963 ASSERT(client
->reference_count
!= 0);
967 necp_client_retain(struct necp_client
*client
)
969 NECP_CLIENT_LOCK(client
);
970 necp_client_retain_locked(client
);
971 NECP_CLIENT_UNLOCK(client
);
975 necp_client_release_locked(struct necp_client
*client
)
977 NECP_CLIENT_ASSERT_LOCKED(client
);
979 uint32_t old_ref
= client
->reference_count
;
981 ASSERT(client
->reference_count
!= 0);
982 if (--client
->reference_count
== 0) {
983 necp_client_free(client
);
986 return (old_ref
== 1);
991 necp_client_update_observer_add_internal(struct necp_fd_data
*observer_fd
, struct necp_client
*client
)
993 NECP_FD_LOCK(observer_fd
);
995 if (observer_fd
->update_count
>= necp_observer_message_limit
) {
996 NECP_FD_UNLOCK(observer_fd
);
1000 struct necp_client_update
*client_update
= _MALLOC(sizeof(struct necp_client_update
) + client
->parameters_length
,
1001 M_NECP
, M_WAITOK
| M_ZERO
);
1002 if (client_update
!= NULL
) {
1003 client_update
->update_length
= sizeof(struct necp_client_observer_update
) + client
->parameters_length
;
1004 uuid_copy(client_update
->client_id
, client
->client_id
);
1005 client_update
->update
.update_type
= NECP_CLIENT_UPDATE_TYPE_PARAMETERS
;
1006 memcpy(client_update
->update
.tlv_buffer
, client
->parameters
, client
->parameters_length
);
1007 TAILQ_INSERT_TAIL(&observer_fd
->update_list
, client_update
, chain
);
1008 observer_fd
->update_count
++;
1010 necp_fd_notify(observer_fd
, true);
1013 NECP_FD_UNLOCK(observer_fd
);
1017 necp_client_update_observer_update_internal(struct necp_fd_data
*observer_fd
, struct necp_client
*client
)
1019 NECP_FD_LOCK(observer_fd
);
1021 if (observer_fd
->update_count
>= necp_observer_message_limit
) {
1022 NECP_FD_UNLOCK(observer_fd
);
1026 struct necp_client_update
*client_update
= _MALLOC(sizeof(struct necp_client_update
) + client
->result_length
,
1027 M_NECP
, M_WAITOK
| M_ZERO
);
1028 if (client_update
!= NULL
) {
1029 client_update
->update_length
= sizeof(struct necp_client_observer_update
) + client
->result_length
;
1030 uuid_copy(client_update
->client_id
, client
->client_id
);
1031 client_update
->update
.update_type
= NECP_CLIENT_UPDATE_TYPE_RESULT
;
1032 memcpy(client_update
->update
.tlv_buffer
, client
->result
, client
->result_length
);
1033 TAILQ_INSERT_TAIL(&observer_fd
->update_list
, client_update
, chain
);
1034 observer_fd
->update_count
++;
1036 necp_fd_notify(observer_fd
, true);
1039 NECP_FD_UNLOCK(observer_fd
);
1043 necp_client_update_observer_remove_internal(struct necp_fd_data
*observer_fd
, struct necp_client
*client
)
1045 NECP_FD_LOCK(observer_fd
);
1047 if (observer_fd
->update_count
>= necp_observer_message_limit
) {
1048 NECP_FD_UNLOCK(observer_fd
);
1052 struct necp_client_update
*client_update
= _MALLOC(sizeof(struct necp_client_update
),
1053 M_NECP
, M_WAITOK
| M_ZERO
);
1054 if (client_update
!= NULL
) {
1055 client_update
->update_length
= sizeof(struct necp_client_observer_update
);
1056 uuid_copy(client_update
->client_id
, client
->client_id
);
1057 client_update
->update
.update_type
= NECP_CLIENT_UPDATE_TYPE_REMOVE
;
1058 TAILQ_INSERT_TAIL(&observer_fd
->update_list
, client_update
, chain
);
1059 observer_fd
->update_count
++;
1061 necp_fd_notify(observer_fd
, true);
1064 NECP_FD_UNLOCK(observer_fd
);
1068 necp_client_update_observer_add(struct necp_client
*client
)
1070 NECP_OBSERVER_LIST_LOCK_SHARED();
1072 if (LIST_EMPTY(&necp_fd_observer_list
)) {
1073 // No observers, bail
1074 NECP_OBSERVER_LIST_UNLOCK();
1078 struct necp_fd_data
*observer_fd
= NULL
;
1079 LIST_FOREACH(observer_fd
, &necp_fd_observer_list
, chain
) {
1080 necp_client_update_observer_add_internal(observer_fd
, client
);
1083 NECP_OBSERVER_LIST_UNLOCK();
1087 necp_client_update_observer_update(struct necp_client
*client
)
1089 NECP_OBSERVER_LIST_LOCK_SHARED();
1091 if (LIST_EMPTY(&necp_fd_observer_list
)) {
1092 // No observers, bail
1093 NECP_OBSERVER_LIST_UNLOCK();
1097 struct necp_fd_data
*observer_fd
= NULL
;
1098 LIST_FOREACH(observer_fd
, &necp_fd_observer_list
, chain
) {
1099 necp_client_update_observer_update_internal(observer_fd
, client
);
1102 NECP_OBSERVER_LIST_UNLOCK();
1106 necp_client_update_observer_remove(struct necp_client
*client
)
1108 NECP_OBSERVER_LIST_LOCK_SHARED();
1110 if (LIST_EMPTY(&necp_fd_observer_list
)) {
1111 // No observers, bail
1112 NECP_OBSERVER_LIST_UNLOCK();
1116 struct necp_fd_data
*observer_fd
= NULL
;
1117 LIST_FOREACH(observer_fd
, &necp_fd_observer_list
, chain
) {
1118 necp_client_update_observer_remove_internal(observer_fd
, client
);
1121 NECP_OBSERVER_LIST_UNLOCK();
1125 necp_destroy_client_flow_registration(struct necp_client
*client
,
1126 struct necp_client_flow_registration
*flow_registration
,
1127 pid_t pid
, bool abort
)
1129 NECP_CLIENT_ASSERT_LOCKED(client
);
1132 struct necp_client_flow
*search_flow
= NULL
;
1133 struct necp_client_flow
*temp_flow
= NULL
;
1134 LIST_FOREACH_SAFE(search_flow
, &flow_registration
->flow_list
, flow_chain
, temp_flow
) {
1135 if (search_flow
->nexus
&&
1136 !uuid_is_null(search_flow
->u
.nexus_agent
)) {
1137 // Note that if we had defuncted the client earlier, this would result in a harmless ENOENT
1138 int netagent_error
= netagent_client_message(search_flow
->u
.nexus_agent
,
1139 ((flow_registration
->flags
& NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID
) ?
1141 flow_registration
->registration_id
),
1142 pid
, client
->agent_handle
,
1143 (abort
? NETAGENT_MESSAGE_TYPE_ABORT_NEXUS
:
1144 NETAGENT_MESSAGE_TYPE_CLOSE_NEXUS
));
1145 if (netagent_error
!= 0 && netagent_error
!= ENOENT
) {
1146 NECPLOG(LOG_ERR
, "necp_client_remove close nexus error (%d)", netagent_error
);
1148 uuid_clear(search_flow
->u
.nexus_agent
);
1150 if (search_flow
->assigned_results
!= NULL
) {
1151 FREE(search_flow
->assigned_results
, M_NETAGENT
);
1152 search_flow
->assigned_results
= NULL
;
1154 LIST_REMOVE(search_flow
, flow_chain
);
1155 if (search_flow
->socket
) {
1156 OSDecrementAtomic(&necp_socket_flow_count
);
1158 OSDecrementAtomic(&necp_if_flow_count
);
1160 mcache_free(necp_flow_cache
, search_flow
);
1163 RB_REMOVE(_necp_client_flow_tree
, &client
->flow_registrations
, flow_registration
);
1164 flow_registration
->client
= NULL
;
1166 mcache_free(necp_flow_registration_cache
, flow_registration
);
1170 necp_destroy_client(struct necp_client
*client
, pid_t pid
, bool abort
)
1172 NECP_CLIENT_ASSERT_UNLOCKED(client
);
1174 necp_client_update_observer_remove(client
);
1176 NECP_CLIENT_LOCK(client
);
1179 NECP_CLIENT_ROUTE_LOCK(client
);
1180 if (client
->current_route
!= NULL
) {
1181 rtfree(client
->current_route
);
1182 client
->current_route
= NULL
;
1184 NECP_CLIENT_ROUTE_UNLOCK(client
);
1186 // Remove flow assignments
1187 struct necp_client_flow_registration
*flow_registration
= NULL
;
1188 struct necp_client_flow_registration
*temp_flow_registration
= NULL
;
1189 RB_FOREACH_SAFE(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
, temp_flow_registration
) {
1190 necp_destroy_client_flow_registration(client
, flow_registration
, pid
, abort
);
1193 // Remove agent assertions
1194 struct necp_client_assertion
*search_assertion
= NULL
;
1195 struct necp_client_assertion
*temp_assertion
= NULL
;
1196 LIST_FOREACH_SAFE(search_assertion
, &client
->assertion_list
, assertion_chain
, temp_assertion
) {
1197 int netagent_error
= netagent_client_message(search_assertion
->asserted_netagent
, client
->client_id
, pid
,
1198 client
->agent_handle
, NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT
);
1199 if (netagent_error
!= 0) {
1200 NECPLOG((netagent_error
== ENOENT
? LOG_DEBUG
: LOG_ERR
),
1201 "necp_client_remove unassert agent error (%d)", netagent_error
);
1203 LIST_REMOVE(search_assertion
, assertion_chain
);
1204 FREE(search_assertion
, M_NECP
);
1207 if (!necp_client_release_locked(client
)) {
1208 NECP_CLIENT_UNLOCK(client
);
1211 OSDecrementAtomic(&necp_client_count
);
1215 necpop_close(struct fileglob
*fg
, vfs_context_t ctx
)
1218 struct necp_fd_data
*fd_data
= NULL
;
1221 fd_data
= (struct necp_fd_data
*)fg
->fg_data
;
1224 if (fd_data
!= NULL
) {
1225 struct _necp_client_tree clients_to_close
;
1226 RB_INIT(&clients_to_close
);
1228 // Remove from list quickly
1229 if (fd_data
->flags
& NECP_OPEN_FLAG_PUSH_OBSERVER
) {
1230 NECP_OBSERVER_LIST_LOCK_EXCLUSIVE();
1231 LIST_REMOVE(fd_data
, chain
);
1232 NECP_OBSERVER_LIST_UNLOCK();
1234 NECP_FD_LIST_LOCK_EXCLUSIVE();
1235 LIST_REMOVE(fd_data
, chain
);
1236 NECP_FD_LIST_UNLOCK();
1239 NECP_FD_LOCK(fd_data
);
1240 pid_t pid
= fd_data
->proc_pid
;
1242 struct necp_client_flow_registration
*flow_registration
= NULL
;
1243 struct necp_client_flow_registration
*temp_flow_registration
= NULL
;
1244 RB_FOREACH_SAFE(flow_registration
, _necp_fd_flow_tree
, &fd_data
->flows
, temp_flow_registration
) {
1245 NECP_FLOW_TREE_LOCK_EXCLUSIVE();
1246 RB_REMOVE(_necp_client_flow_global_tree
, &necp_client_flow_global_tree
, flow_registration
);
1247 NECP_FLOW_TREE_UNLOCK();
1248 RB_REMOVE(_necp_fd_flow_tree
, &fd_data
->flows
, flow_registration
);
1251 struct necp_client
*client
= NULL
;
1252 struct necp_client
*temp_client
= NULL
;
1253 RB_FOREACH_SAFE(client
, _necp_client_tree
, &fd_data
->clients
, temp_client
) {
1254 NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
1255 RB_REMOVE(_necp_client_global_tree
, &necp_client_global_tree
, client
);
1256 NECP_CLIENT_TREE_UNLOCK();
1257 RB_REMOVE(_necp_client_tree
, &fd_data
->clients
, client
);
1258 RB_INSERT(_necp_client_tree
, &clients_to_close
, client
);
1261 struct necp_client_update
*client_update
= NULL
;
1262 struct necp_client_update
*temp_update
= NULL
;
1263 TAILQ_FOREACH_SAFE(client_update
, &fd_data
->update_list
, chain
, temp_update
) {
1264 // Flush pending updates
1265 TAILQ_REMOVE(&fd_data
->update_list
, client_update
, chain
);
1266 FREE(client_update
, M_NECP
);
1268 fd_data
->update_count
= 0;
1271 NECP_FD_UNLOCK(fd_data
);
1273 selthreadclear(&fd_data
->si
);
1275 lck_mtx_destroy(&fd_data
->fd_lock
, necp_fd_mtx_grp
);
1277 if (fd_data
->flags
& NECP_OPEN_FLAG_PUSH_OBSERVER
) {
1278 OSDecrementAtomic(&necp_observer_fd_count
);
1280 OSDecrementAtomic(&necp_client_fd_count
);
1283 zfree(necp_client_fd_zone
, fd_data
);
1286 RB_FOREACH_SAFE(client
, _necp_client_tree
, &clients_to_close
, temp_client
) {
1287 RB_REMOVE(_necp_client_tree
, &clients_to_close
, client
);
1288 necp_destroy_client(client
, pid
, true);
1295 /// NECP client utilities
1298 necp_address_is_wildcard(const union necp_sockaddr_union
* const addr
)
1300 return ((addr
->sa
.sa_family
== AF_INET
&& addr
->sin
.sin_addr
.s_addr
== INADDR_ANY
) ||
1301 (addr
->sa
.sa_family
== AF_INET6
&& IN6_IS_ADDR_UNSPECIFIED(&addr
->sin6
.sin6_addr
)));
1305 necp_find_fd_data(int fd
, struct necp_fd_data
**fd_data
)
1307 proc_t p
= current_proc();
1308 struct fileproc
*fp
= NULL
;
1311 proc_fdlock_spin(p
);
1312 if ((error
= fp_lookup(p
, fd
, &fp
, 1)) != 0) {
1315 if (fp
->f_fglob
->fg_ops
->fo_type
!= DTYPE_NETPOLICY
) {
1316 fp_drop(p
, fd
, fp
, 1);
1320 *fd_data
= (struct necp_fd_data
*)fp
->f_fglob
->fg_data
;
1322 if ((*fd_data
)->necp_fd_type
!= necp_fd_type_client
) {
1323 // Not a client fd, ignore
1334 static struct necp_client_flow
*
1335 necp_client_add_interface_flow(struct necp_client_flow_registration
*flow_registration
,
1336 uint32_t interface_index
)
1338 struct necp_client_flow
*new_flow
= mcache_alloc(necp_flow_cache
, MCR_SLEEP
);
1339 if (new_flow
== NULL
) {
1340 NECPLOG0(LOG_ERR
, "Failed to allocate interface flow");
1344 memset(new_flow
, 0, sizeof(*new_flow
));
1346 // Neither nexus nor socket
1347 new_flow
->interface_index
= interface_index
;
1348 new_flow
->u
.socket_handle
= flow_registration
->interface_handle
;
1349 new_flow
->u
.cb
= flow_registration
->interface_cb
;
1351 OSIncrementAtomic(&necp_if_flow_count
);
1353 LIST_INSERT_HEAD(&flow_registration
->flow_list
, new_flow
, flow_chain
);
1358 static struct necp_client_flow
*
1359 necp_client_add_interface_flow_if_needed(struct necp_client
*client
,
1360 struct necp_client_flow_registration
*flow_registration
,
1361 uint32_t interface_index
)
1363 if (!client
->allow_multiple_flows
||
1364 interface_index
== IFSCOPE_NONE
) {
1365 // Interface not set, or client not allowed to use this mode
1369 struct necp_client_flow
*flow
= NULL
;
1370 LIST_FOREACH(flow
, &flow_registration
->flow_list
, flow_chain
) {
1371 if (!flow
->nexus
&& !flow
->socket
&& flow
->interface_index
== interface_index
) {
1372 // Already have the flow
1373 flow
->invalid
= FALSE
;
1374 flow
->u
.socket_handle
= flow_registration
->interface_handle
;
1375 flow
->u
.cb
= flow_registration
->interface_cb
;
1379 return necp_client_add_interface_flow(flow_registration
, interface_index
);
1383 necp_client_add_interface_option_if_needed(struct necp_client
*client
,
1384 uint32_t interface_index
,
1385 uint32_t interface_generation
,
1386 uuid_t
*nexus_agent
)
1388 if (interface_index
== IFSCOPE_NONE
||
1389 (client
->interface_option_count
!= 0 && !client
->allow_multiple_flows
)) {
1390 // Interface not set, or client not allowed to use this mode
1394 if (client
->interface_option_count
>= NECP_CLIENT_MAX_INTERFACE_OPTIONS
) {
1395 // Cannot take any more interface options
1399 // Check if already present
1400 for (u_int32_t option_i
= 0; option_i
< client
->interface_option_count
; option_i
++) {
1401 if (option_i
< NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
) {
1402 struct necp_client_interface_option
*option
= &client
->interface_options
[option_i
];
1403 if (option
->interface_index
== interface_index
) {
1404 if (nexus_agent
== NULL
) {
1407 if (uuid_compare(option
->nexus_agent
, *nexus_agent
) == 0) {
1410 if (uuid_is_null(option
->nexus_agent
)) {
1411 uuid_copy(option
->nexus_agent
, *nexus_agent
);
1414 // If we get to this point, this is a new nexus flow
1417 struct necp_client_interface_option
*option
= &client
->extra_interface_options
[option_i
- NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
];
1418 if (option
->interface_index
== interface_index
) {
1419 if (nexus_agent
== NULL
) {
1422 if (uuid_compare(option
->nexus_agent
, *nexus_agent
) == 0) {
1425 if (uuid_is_null(option
->nexus_agent
)) {
1426 uuid_copy(option
->nexus_agent
, *nexus_agent
);
1429 // If we get to this point, this is a new nexus flow
1435 if (client
->interface_option_count
< NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
) {
1437 struct necp_client_interface_option
*option
= &client
->interface_options
[client
->interface_option_count
];
1438 option
->interface_index
= interface_index
;
1439 option
->interface_generation
= interface_generation
;
1440 if (nexus_agent
!= NULL
) {
1441 uuid_copy(option
->nexus_agent
, *nexus_agent
);
1443 client
->interface_option_count
++;
1446 if (client
->extra_interface_options
== NULL
) {
1447 client
->extra_interface_options
= _MALLOC(sizeof(struct necp_client_interface_option
) * NECP_CLIENT_INTERFACE_OPTION_EXTRA_COUNT
, M_NECP
, M_WAITOK
| M_ZERO
);
1449 if (client
->extra_interface_options
!= NULL
) {
1450 struct necp_client_interface_option
*option
= &client
->extra_interface_options
[client
->interface_option_count
- NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
];
1451 option
->interface_index
= interface_index
;
1452 option
->interface_generation
= interface_generation
;
1453 if (nexus_agent
!= NULL
) {
1454 uuid_copy(option
->nexus_agent
, *nexus_agent
);
1456 client
->interface_option_count
++;
1462 necp_client_flow_is_viable(proc_t proc
, struct necp_client
*client
,
1463 struct necp_client_flow
*flow
)
1465 struct necp_aggregate_result result
;
1466 bool ignore_address
= (client
->allow_multiple_flows
&& !flow
->nexus
&& !flow
->socket
);
1468 flow
->necp_flow_flags
= 0;
1469 int error
= necp_application_find_policy_match_internal(proc
, client
->parameters
,
1470 (u_int32_t
)client
->parameters_length
,
1471 &result
, &flow
->necp_flow_flags
,
1472 flow
->interface_index
,
1473 &flow
->local_addr
, &flow
->remote_addr
, NULL
, ignore_address
);
1475 return (error
== 0 &&
1476 result
.routed_interface_index
!= IFSCOPE_NONE
&&
1477 result
.routing_result
!= NECP_KERNEL_POLICY_RESULT_DROP
);
1481 necp_flow_add_interface_flows(proc_t proc
,
1482 struct necp_client
*client
,
1483 struct necp_client_flow_registration
*flow_registration
,
1486 // Traverse all interfaces and add a tracking flow if needed
1487 for (u_int32_t option_i
= 0; option_i
< client
->interface_option_count
; option_i
++) {
1488 if (option_i
< NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
) {
1489 struct necp_client_interface_option
*option
= &client
->interface_options
[option_i
];
1490 struct necp_client_flow
*flow
= necp_client_add_interface_flow_if_needed(client
, flow_registration
, option
->interface_index
);
1491 if (flow
!= NULL
&& send_initial
) {
1492 flow
->viable
= necp_client_flow_is_viable(proc
, client
, flow
);
1493 if (flow
->viable
&& flow
->u
.cb
) {
1494 bool viable
= flow
->viable
;
1495 flow
->u
.cb(flow_registration
->interface_handle
, NECP_CLIENT_CBACTION_INITIAL
, flow
->interface_index
, flow
->necp_flow_flags
, &viable
);
1496 flow
->viable
= viable
;
1500 struct necp_client_interface_option
*option
= &client
->extra_interface_options
[option_i
- NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
];
1501 struct necp_client_flow
*flow
= necp_client_add_interface_flow_if_needed(client
, flow_registration
, option
->interface_index
);
1502 if (flow
!= NULL
&& send_initial
) {
1503 flow
->viable
= necp_client_flow_is_viable(proc
, client
, flow
);
1504 if (flow
->viable
&& flow
->u
.cb
) {
1505 bool viable
= flow
->viable
;
1506 flow
->u
.cb(flow_registration
->interface_handle
, NECP_CLIENT_CBACTION_INITIAL
, flow
->interface_index
, flow
->necp_flow_flags
, &viable
);
1507 flow
->viable
= viable
;
1515 necp_client_update_flows(proc_t proc
,
1516 struct necp_client
*client
,
1517 struct _necp_flow_defunct_list
*defunct_list
)
1519 NECP_CLIENT_ASSERT_LOCKED(client
);
1521 bool client_updated
= FALSE
;
1522 struct necp_client_flow
*flow
= NULL
;
1523 struct necp_client_flow
*temp_flow
= NULL
;
1524 struct necp_client_flow_registration
*flow_registration
= NULL
;
1525 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
1526 if (flow_registration
->interface_cb
!= NULL
) {
1527 // Add any interface flows that are not already tracked
1528 necp_flow_add_interface_flows(proc
, client
, flow_registration
, false);
1531 LIST_FOREACH_SAFE(flow
, &flow_registration
->flow_list
, flow_chain
, temp_flow
) {
1532 // Check policy result for flow
1533 int old_flags
= flow
->necp_flow_flags
;
1534 bool viable
= necp_client_flow_is_viable(proc
, client
, flow
);
1536 // TODO: Defunct nexus flows that are blocked by policy
1538 if (flow
->viable
!= viable
) {
1539 flow
->viable
= viable
;
1540 client_updated
= TRUE
;
1543 if ((old_flags
& NECP_CLIENT_RESULT_FLAG_FORCE_UPDATE
) !=
1544 (flow
->necp_flow_flags
& NECP_CLIENT_RESULT_FLAG_FORCE_UPDATE
)) {
1545 client_updated
= TRUE
;
1548 if (flow
->viable
&& client_updated
&& (flow
->socket
|| (!flow
->socket
&& !flow
->nexus
)) && flow
->u
.cb
) {
1549 bool flow_viable
= flow
->viable
;
1550 flow
->u
.cb(flow
->u
.socket_handle
, NECP_CLIENT_CBACTION_VIABLE
, flow
->interface_index
, flow
->necp_flow_flags
, &viable
);
1551 flow
->viable
= flow_viable
;
1554 if (!flow
->viable
|| flow
->invalid
) {
1555 if (client_updated
&& (flow
->socket
|| (!flow
->socket
&& !flow
->nexus
)) && flow
->u
.cb
) {
1556 bool flow_viable
= flow
->viable
;
1557 flow
->u
.cb(flow
->u
.socket_handle
, NECP_CLIENT_CBACTION_NONVIABLE
, flow
->interface_index
, flow
->necp_flow_flags
, &viable
);
1558 flow
->viable
= flow_viable
;
1560 // The callback might change the viable-flag of the
1561 // flow depending on its policy. Thus, we need to
1562 // check the flags again after the callback.
1567 // Handle flows that no longer match
1568 if (!flow
->viable
|| flow
->invalid
) {
1569 // Drop them as long as they aren't assigned data
1570 if (!flow
->nexus
&& !flow
->assigned
) {
1571 if (flow
->assigned_results
!= NULL
) {
1572 FREE(flow
->assigned_results
, M_NETAGENT
);
1573 flow
->assigned_results
= NULL
;
1574 client_updated
= TRUE
;
1576 LIST_REMOVE(flow
, flow_chain
);
1578 OSDecrementAtomic(&necp_socket_flow_count
);
1580 OSDecrementAtomic(&necp_if_flow_count
);
1582 mcache_free(necp_flow_cache
, flow
);
1588 return (client_updated
);
1592 necp_client_mark_all_nonsocket_flows_as_invalid(struct necp_client
*client
)
1594 struct necp_client_flow_registration
*flow_registration
= NULL
;
1595 struct necp_client_flow
*flow
= NULL
;
1596 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
1597 LIST_FOREACH(flow
, &flow_registration
->flow_list
, flow_chain
) {
1598 if (!flow
->socket
) { // Socket flows are not marked as invalid
1599 flow
->invalid
= TRUE
;
1604 // Reset option count every update
1605 client
->interface_option_count
= 0;
1609 necp_netagent_applies_to_client(struct necp_client
*client
,
1610 const struct necp_client_parsed_parameters
*parameters
,
1611 uuid_t
*netagent_uuid
, bool allow_nexus
,
1612 uint32_t interface_index
, uint32_t interface_generation
)
1614 #pragma unused(interface_index, interface_generation)
1615 bool applies
= FALSE
;
1616 u_int32_t flags
= netagent_get_flags(*netagent_uuid
);
1617 if (!(flags
& NETAGENT_FLAG_REGISTERED
)) {
1618 // Unregistered agents never apply
1623 (flags
& NETAGENT_FLAG_NEXUS_PROVIDER
)) {
1624 // Hide nexus providers unless allowed
1625 // Direct interfaces and direct policies are allowed to use a nexus
1626 // Delegate interfaces or re-scoped interfaces are not allowed
1630 if (uuid_compare(client
->failed_trigger_agent
.netagent_uuid
, *netagent_uuid
) == 0) {
1631 if (client
->failed_trigger_agent
.generation
== netagent_get_generation(*netagent_uuid
)) {
1632 // If this agent was triggered, and failed, and hasn't changed, keep hiding it
1635 // Mismatch generation, clear out old trigger
1636 uuid_clear(client
->failed_trigger_agent
.netagent_uuid
);
1637 client
->failed_trigger_agent
.generation
= 0;
1641 if (flags
& NETAGENT_FLAG_SPECIFIC_USE_ONLY
) {
1642 // Specific use agents only apply when required
1643 bool required
= FALSE
;
1644 if (parameters
!= NULL
) {
1645 // Check required agent UUIDs
1646 for (int i
= 0; i
< NECP_MAX_PARSED_PARAMETERS
; i
++) {
1647 if (uuid_is_null(parameters
->required_netagents
[i
])) {
1650 if (uuid_compare(parameters
->required_netagents
[i
], *netagent_uuid
) == 0) {
1657 // Check required agent types
1658 bool fetched_type
= FALSE
;
1659 char netagent_domain
[NETAGENT_DOMAINSIZE
];
1660 char netagent_type
[NETAGENT_TYPESIZE
];
1661 memset(&netagent_domain
, 0, NETAGENT_DOMAINSIZE
);
1662 memset(&netagent_type
, 0, NETAGENT_TYPESIZE
);
1664 for (int i
= 0; i
< NECP_MAX_PARSED_PARAMETERS
; i
++) {
1665 if (strlen(parameters
->required_netagent_types
[i
].netagent_domain
) == 0 ||
1666 strlen(parameters
->required_netagent_types
[i
].netagent_type
) == 0) {
1670 if (!fetched_type
) {
1671 if (netagent_get_agent_domain_and_type(*netagent_uuid
, netagent_domain
, netagent_type
)) {
1672 fetched_type
= TRUE
;
1678 if ((strlen(parameters
->required_netagent_types
[i
].netagent_domain
) == 0 ||
1679 strncmp(netagent_domain
, parameters
->required_netagent_types
[i
].netagent_domain
, NETAGENT_DOMAINSIZE
) == 0) &&
1680 (strlen(parameters
->required_netagent_types
[i
].netagent_type
) == 0 ||
1681 strncmp(netagent_type
, parameters
->required_netagent_types
[i
].netagent_type
, NETAGENT_TYPESIZE
) == 0)) {
1699 necp_client_add_agent_interface_options(struct necp_client
*client
,
1700 const struct necp_client_parsed_parameters
*parsed_parameters
,
1703 if (ifp
!= NULL
&& ifp
->if_agentids
!= NULL
) {
1704 for (u_int32_t i
= 0; i
< ifp
->if_agentcount
; i
++) {
1705 if (uuid_is_null(ifp
->if_agentids
[i
])) {
1708 // Relies on the side effect that nexus agents that apply will create flows
1709 (void)necp_netagent_applies_to_client(client
, parsed_parameters
, &ifp
->if_agentids
[i
], TRUE
,
1710 ifp
->if_index
, ifnet_get_generation(ifp
));
1716 necp_client_address_is_valid(struct sockaddr
*address
)
1718 if (address
->sa_family
== AF_INET
) {
1719 return (address
->sa_len
== sizeof(struct sockaddr_in
));
1720 } else if (address
->sa_family
== AF_INET6
) {
1721 return (address
->sa_len
== sizeof(struct sockaddr_in6
));
1728 necp_client_parse_parameters(u_int8_t
*parameters
,
1729 u_int32_t parameters_size
,
1730 struct necp_client_parsed_parameters
*parsed_parameters
)
1735 u_int32_t num_prohibited_interfaces
= 0;
1736 u_int32_t num_prohibited_interface_types
= 0;
1737 u_int32_t num_required_agents
= 0;
1738 u_int32_t num_prohibited_agents
= 0;
1739 u_int32_t num_preferred_agents
= 0;
1740 u_int32_t num_avoided_agents
= 0;
1741 u_int32_t num_required_agent_types
= 0;
1742 u_int32_t num_prohibited_agent_types
= 0;
1743 u_int32_t num_preferred_agent_types
= 0;
1744 u_int32_t num_avoided_agent_types
= 0;
1746 if (parsed_parameters
== NULL
) {
1750 memset(parsed_parameters
, 0, sizeof(struct necp_client_parsed_parameters
));
1752 while ((offset
+ sizeof(struct necp_tlv_header
)) <= parameters_size
) {
1753 u_int8_t type
= necp_buffer_get_tlv_type(parameters
, offset
);
1754 u_int32_t length
= necp_buffer_get_tlv_length(parameters
, offset
);
1756 if (length
> (parameters_size
- (offset
+ sizeof(struct necp_tlv_header
)))) {
1757 // If the length is larger than what can fit in the remaining parameters size, bail
1758 NECPLOG(LOG_ERR
, "Invalid TLV length (%u)", length
);
1763 u_int8_t
*value
= necp_buffer_get_tlv_value(parameters
, offset
, NULL
);
1764 if (value
!= NULL
) {
1766 case NECP_CLIENT_PARAMETER_BOUND_INTERFACE
: {
1767 if (length
<= IFXNAMSIZ
&& length
> 0) {
1768 ifnet_t bound_interface
= NULL
;
1769 char interface_name
[IFXNAMSIZ
];
1770 memcpy(interface_name
, value
, length
);
1771 interface_name
[length
- 1] = 0; // Make sure the string is NULL terminated
1772 if (ifnet_find_by_name(interface_name
, &bound_interface
) == 0) {
1773 parsed_parameters
->required_interface_index
= bound_interface
->if_index
;
1774 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF
;
1775 ifnet_release(bound_interface
);
1780 case NECP_CLIENT_PARAMETER_LOCAL_ADDRESS
: {
1781 if (length
>= sizeof(struct necp_policy_condition_addr
)) {
1782 struct necp_policy_condition_addr
*address_struct
= (struct necp_policy_condition_addr
*)(void *)value
;
1783 if (necp_client_address_is_valid(&address_struct
->address
.sa
)) {
1784 memcpy(&parsed_parameters
->local_addr
, &address_struct
->address
, sizeof(address_struct
->address
));
1785 if (!necp_address_is_wildcard(&parsed_parameters
->local_addr
)) {
1786 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR
;
1788 if ((parsed_parameters
->local_addr
.sa
.sa_family
== AF_INET
&& parsed_parameters
->local_addr
.sin
.sin_port
) ||
1789 (parsed_parameters
->local_addr
.sa
.sa_family
== AF_INET6
&& parsed_parameters
->local_addr
.sin6
.sin6_port
)) {
1790 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_LOCAL_PORT
;
1796 case NECP_CLIENT_PARAMETER_LOCAL_ENDPOINT
: {
1797 if (length
>= sizeof(struct necp_client_endpoint
)) {
1798 struct necp_client_endpoint
*endpoint
= (struct necp_client_endpoint
*)(void *)value
;
1799 if (necp_client_address_is_valid(&endpoint
->u
.sa
)) {
1800 memcpy(&parsed_parameters
->local_addr
, &endpoint
->u
.sa
, sizeof(union necp_sockaddr_union
));
1801 if (!necp_address_is_wildcard(&parsed_parameters
->local_addr
)) {
1802 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR
;
1804 if ((parsed_parameters
->local_addr
.sa
.sa_family
== AF_INET
&& parsed_parameters
->local_addr
.sin
.sin_port
) ||
1805 (parsed_parameters
->local_addr
.sa
.sa_family
== AF_INET6
&& parsed_parameters
->local_addr
.sin6
.sin6_port
)) {
1806 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_LOCAL_PORT
;
1812 case NECP_CLIENT_PARAMETER_REMOTE_ADDRESS
: {
1813 if (length
>= sizeof(struct necp_policy_condition_addr
)) {
1814 struct necp_policy_condition_addr
*address_struct
= (struct necp_policy_condition_addr
*)(void *)value
;
1815 if (necp_client_address_is_valid(&address_struct
->address
.sa
)) {
1816 memcpy(&parsed_parameters
->remote_addr
, &address_struct
->address
, sizeof(address_struct
->address
));
1817 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_REMOTE_ADDR
;
1822 case NECP_CLIENT_PARAMETER_REMOTE_ENDPOINT
: {
1823 if (length
>= sizeof(struct necp_client_endpoint
)) {
1824 struct necp_client_endpoint
*endpoint
= (struct necp_client_endpoint
*)(void *)value
;
1825 if (necp_client_address_is_valid(&endpoint
->u
.sa
)) {
1826 memcpy(&parsed_parameters
->remote_addr
, &endpoint
->u
.sa
, sizeof(union necp_sockaddr_union
));
1827 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_REMOTE_ADDR
;
1832 case NECP_CLIENT_PARAMETER_PROHIBIT_INTERFACE
: {
1833 if (num_prohibited_interfaces
>= NECP_MAX_PARSED_PARAMETERS
) {
1836 if (length
<= IFXNAMSIZ
&& length
> 0) {
1837 memcpy(parsed_parameters
->prohibited_interfaces
[num_prohibited_interfaces
], value
, length
);
1838 parsed_parameters
->prohibited_interfaces
[num_prohibited_interfaces
][length
- 1] = 0; // Make sure the string is NULL terminated
1839 num_prohibited_interfaces
++;
1840 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IF
;
1844 case NECP_CLIENT_PARAMETER_REQUIRE_IF_TYPE
: {
1845 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE
) {
1848 if (length
>= sizeof(u_int8_t
)) {
1849 memcpy(&parsed_parameters
->required_interface_type
, value
, sizeof(u_int8_t
));
1850 if (parsed_parameters
->required_interface_type
) {
1851 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE
;
1856 case NECP_CLIENT_PARAMETER_PROHIBIT_IF_TYPE
: {
1857 if (num_prohibited_interface_types
>= NECP_MAX_PARSED_PARAMETERS
) {
1860 if (length
>= sizeof(u_int8_t
)) {
1861 memcpy(&parsed_parameters
->prohibited_interface_types
[num_prohibited_interface_types
], value
, sizeof(u_int8_t
));
1862 num_prohibited_interface_types
++;
1863 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IFTYPE
;
1867 case NECP_CLIENT_PARAMETER_REQUIRE_AGENT
: {
1868 if (num_required_agents
>= NECP_MAX_PARSED_PARAMETERS
) {
1871 if (length
>= sizeof(uuid_t
)) {
1872 memcpy(&parsed_parameters
->required_netagents
[num_required_agents
], value
, sizeof(uuid_t
));
1873 num_required_agents
++;
1874 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT
;
1878 case NECP_CLIENT_PARAMETER_PROHIBIT_AGENT
: {
1879 if (num_prohibited_agents
>= NECP_MAX_PARSED_PARAMETERS
) {
1882 if (length
>= sizeof(uuid_t
)) {
1883 memcpy(&parsed_parameters
->prohibited_netagents
[num_prohibited_agents
], value
, sizeof(uuid_t
));
1884 num_prohibited_agents
++;
1885 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT
;
1889 case NECP_CLIENT_PARAMETER_PREFER_AGENT
: {
1890 if (num_preferred_agents
>= NECP_MAX_PARSED_PARAMETERS
) {
1893 if (length
>= sizeof(uuid_t
)) {
1894 memcpy(&parsed_parameters
->preferred_netagents
[num_preferred_agents
], value
, sizeof(uuid_t
));
1895 num_preferred_agents
++;
1896 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT
;
1900 case NECP_CLIENT_PARAMETER_AVOID_AGENT
: {
1901 if (num_avoided_agents
>= NECP_MAX_PARSED_PARAMETERS
) {
1904 if (length
>= sizeof(uuid_t
)) {
1905 memcpy(&parsed_parameters
->avoided_netagents
[num_avoided_agents
], value
, sizeof(uuid_t
));
1906 num_avoided_agents
++;
1907 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT
;
1911 case NECP_CLIENT_PARAMETER_REQUIRE_AGENT_TYPE
: {
1912 if (num_required_agent_types
>= NECP_MAX_PARSED_PARAMETERS
) {
1915 if (length
>= sizeof(struct necp_client_parameter_netagent_type
)) {
1916 memcpy(&parsed_parameters
->required_netagent_types
[num_required_agent_types
], value
, sizeof(struct necp_client_parameter_netagent_type
));
1917 num_required_agent_types
++;
1918 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE
;
1922 case NECP_CLIENT_PARAMETER_PROHIBIT_AGENT_TYPE
: {
1923 if (num_prohibited_agent_types
>= NECP_MAX_PARSED_PARAMETERS
) {
1926 if (length
>= sizeof(struct necp_client_parameter_netagent_type
)) {
1927 memcpy(&parsed_parameters
->prohibited_netagent_types
[num_prohibited_agent_types
], value
, sizeof(struct necp_client_parameter_netagent_type
));
1928 num_prohibited_agent_types
++;
1929 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT_TYPE
;
1933 case NECP_CLIENT_PARAMETER_PREFER_AGENT_TYPE
: {
1934 if (num_preferred_agent_types
>= NECP_MAX_PARSED_PARAMETERS
) {
1937 if (length
>= sizeof(struct necp_client_parameter_netagent_type
)) {
1938 memcpy(&parsed_parameters
->preferred_netagent_types
[num_preferred_agent_types
], value
, sizeof(struct necp_client_parameter_netagent_type
));
1939 num_preferred_agent_types
++;
1940 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE
;
1944 case NECP_CLIENT_PARAMETER_AVOID_AGENT_TYPE
: {
1945 if (num_avoided_agent_types
>= NECP_MAX_PARSED_PARAMETERS
) {
1948 if (length
>= sizeof(struct necp_client_parameter_netagent_type
)) {
1949 memcpy(&parsed_parameters
->avoided_netagent_types
[num_avoided_agent_types
], value
, sizeof(struct necp_client_parameter_netagent_type
));
1950 num_avoided_agent_types
++;
1951 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE
;
1955 case NECP_CLIENT_PARAMETER_FLAGS
: {
1956 if (length
>= sizeof(u_int32_t
)) {
1957 memcpy(&parsed_parameters
->flags
, value
, sizeof(parsed_parameters
->flags
));
1958 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_FLAGS
;
1962 case NECP_CLIENT_PARAMETER_IP_PROTOCOL
: {
1963 if (length
>= sizeof(parsed_parameters
->ip_protocol
)) {
1964 memcpy(&parsed_parameters
->ip_protocol
, value
, sizeof(parsed_parameters
->ip_protocol
));
1965 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_IP_PROTOCOL
;
1969 case NECP_CLIENT_PARAMETER_PID
: {
1970 if (length
>= sizeof(parsed_parameters
->effective_pid
)) {
1971 memcpy(&parsed_parameters
->effective_pid
, value
, sizeof(parsed_parameters
->effective_pid
));
1972 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID
;
1976 case NECP_CLIENT_PARAMETER_APPLICATION
: {
1977 if (length
>= sizeof(parsed_parameters
->effective_uuid
)) {
1978 memcpy(&parsed_parameters
->effective_uuid
, value
, sizeof(parsed_parameters
->effective_uuid
));
1979 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_UUID
;
1983 case NECP_CLIENT_PARAMETER_TRAFFIC_CLASS
: {
1984 if (length
>= sizeof(parsed_parameters
->traffic_class
)) {
1985 memcpy(&parsed_parameters
->traffic_class
, value
, sizeof(parsed_parameters
->traffic_class
));
1986 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_TRAFFIC_CLASS
;
1997 offset
+= sizeof(struct necp_tlv_header
) + length
;
2004 necp_client_parse_result(u_int8_t
*result
,
2005 u_int32_t result_size
,
2006 union necp_sockaddr_union
*local_address
,
2007 union necp_sockaddr_union
*remote_address
,
2010 #pragma unused(flow_stats)
2014 while ((offset
+ sizeof(struct necp_tlv_header
)) <= result_size
) {
2015 u_int8_t type
= necp_buffer_get_tlv_type(result
, offset
);
2016 u_int32_t length
= necp_buffer_get_tlv_length(result
, offset
);
2018 if (length
> 0 && (offset
+ sizeof(struct necp_tlv_header
) + length
) <= result_size
) {
2019 u_int8_t
*value
= necp_buffer_get_tlv_value(result
, offset
, NULL
);
2020 if (value
!= NULL
) {
2022 case NECP_CLIENT_RESULT_LOCAL_ENDPOINT
: {
2023 if (length
>= sizeof(struct necp_client_endpoint
)) {
2024 struct necp_client_endpoint
*endpoint
= (struct necp_client_endpoint
*)(void *)value
;
2025 if (local_address
!= NULL
&& necp_client_address_is_valid(&endpoint
->u
.sa
)) {
2026 memcpy(local_address
, &endpoint
->u
.sa
, endpoint
->u
.sa
.sa_len
);
2031 case NECP_CLIENT_RESULT_REMOTE_ENDPOINT
: {
2032 if (length
>= sizeof(struct necp_client_endpoint
)) {
2033 struct necp_client_endpoint
*endpoint
= (struct necp_client_endpoint
*)(void *)value
;
2034 if (remote_address
!= NULL
&& necp_client_address_is_valid(&endpoint
->u
.sa
)) {
2035 memcpy(remote_address
, &endpoint
->u
.sa
, endpoint
->u
.sa
.sa_len
);
2047 offset
+= sizeof(struct necp_tlv_header
) + length
;
2053 static struct necp_client_flow_registration
*
2054 necp_client_create_flow_registration(struct necp_fd_data
*fd_data
, struct necp_client
*client
)
2056 NECP_FD_ASSERT_LOCKED(fd_data
);
2057 NECP_CLIENT_ASSERT_LOCKED(client
);
2059 struct necp_client_flow_registration
*new_registration
= mcache_alloc(necp_flow_registration_cache
, MCR_SLEEP
);
2060 if (new_registration
== NULL
) {
2064 memset(new_registration
, 0, sizeof(*new_registration
));
2066 new_registration
->last_interface_details
= combine_interface_details(IFSCOPE_NONE
, NSTAT_IFNET_IS_UNKNOWN_TYPE
);
2068 necp_generate_client_id(new_registration
->registration_id
, true);
2069 LIST_INIT(&new_registration
->flow_list
);
2071 // Add registration to client list
2072 RB_INSERT(_necp_client_flow_tree
, &client
->flow_registrations
, new_registration
);
2074 // Add registration to fd list
2075 RB_INSERT(_necp_fd_flow_tree
, &fd_data
->flows
, new_registration
);
2077 // Add registration to global tree for lookup
2078 NECP_FLOW_TREE_LOCK_EXCLUSIVE();
2079 RB_INSERT(_necp_client_flow_global_tree
, &necp_client_flow_global_tree
, new_registration
);
2080 NECP_FLOW_TREE_UNLOCK();
2082 new_registration
->client
= client
;
2084 // Start out assuming there is nothing to read from the flow
2085 new_registration
->flow_result_read
= true;
2087 return new_registration
;
2091 necp_client_add_socket_flow(struct necp_client_flow_registration
*flow_registration
,
2094 struct necp_client_flow
*new_flow
= mcache_alloc(necp_flow_cache
, MCR_SLEEP
);
2095 if (new_flow
== NULL
) {
2096 NECPLOG0(LOG_ERR
, "Failed to allocate socket flow");
2100 memset(new_flow
, 0, sizeof(*new_flow
));
2102 new_flow
->socket
= TRUE
;
2103 new_flow
->u
.socket_handle
= inp
;
2104 new_flow
->u
.cb
= inp
->necp_cb
;
2106 OSIncrementAtomic(&necp_socket_flow_count
);
2108 LIST_INSERT_HEAD(&flow_registration
->flow_list
, new_flow
, flow_chain
);
2112 necp_client_register_socket_flow(pid_t pid
, uuid_t client_id
, struct inpcb
*inp
)
2115 struct necp_fd_data
*client_fd
= NULL
;
2116 bool found_client
= FALSE
;
2118 NECP_FD_LIST_LOCK_SHARED();
2119 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
2120 NECP_FD_LOCK(client_fd
);
2121 struct necp_client
*client
= necp_client_fd_find_client_and_lock(client_fd
, client_id
);
2122 if (client
!= NULL
) {
2123 if (!pid
|| client
->proc_pid
== pid
) {
2124 struct necp_client_flow_registration
*flow_registration
= necp_client_find_flow(client
, client_id
);
2125 if (flow_registration
!= NULL
) {
2126 // Found the right client and flow registration, add a new flow
2127 found_client
= TRUE
;
2128 necp_client_add_socket_flow(flow_registration
, inp
);
2129 } else if (RB_EMPTY(&client
->flow_registrations
) && !necp_client_id_is_flow(client_id
)) {
2130 // No flows yet on this client, add a new registration
2131 flow_registration
= necp_client_create_flow_registration(client_fd
, client
);
2132 if (flow_registration
== NULL
) {
2136 found_client
= TRUE
;
2137 necp_client_add_socket_flow(flow_registration
, inp
);
2142 NECP_CLIENT_UNLOCK(client
);
2144 NECP_FD_UNLOCK(client_fd
);
2150 NECP_FD_LIST_UNLOCK();
2152 if (!found_client
) {
2155 // Count the sockets that have the NECP client UUID set
2156 struct socket
*so
= inp
->inp_socket
;
2157 if (!(so
->so_flags1
& SOF1_HAS_NECP_CLIENT_UUID
)) {
2158 so
->so_flags1
|= SOF1_HAS_NECP_CLIENT_UUID
;
2159 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_necp_clientuuid_total
);
2167 necp_client_add_multipath_interface_flows(struct necp_client_flow_registration
*flow_registration
,
2168 struct necp_client
*client
,
2171 flow_registration
->interface_handle
= mpp
;
2172 flow_registration
->interface_cb
= mpp
->necp_cb
;
2174 proc_t proc
= proc_find(client
->proc_pid
);
2175 if (proc
== PROC_NULL
) {
2179 // Traverse all interfaces and add a tracking flow if needed
2180 necp_flow_add_interface_flows(proc
, client
, flow_registration
, true);
2187 necp_client_register_multipath_cb(pid_t pid
, uuid_t client_id
, struct mppcb
*mpp
)
2190 struct necp_fd_data
*client_fd
= NULL
;
2191 bool found_client
= FALSE
;
2193 NECP_FD_LIST_LOCK_SHARED();
2194 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
2195 NECP_FD_LOCK(client_fd
);
2196 struct necp_client
*client
= necp_client_fd_find_client_and_lock(client_fd
, client_id
);
2197 if (client
!= NULL
) {
2198 if (!pid
|| client
->proc_pid
== pid
) {
2199 struct necp_client_flow_registration
*flow_registration
= necp_client_find_flow(client
, client_id
);
2200 if (flow_registration
!= NULL
) {
2201 // Found the right client and flow registration, add a new flow
2202 found_client
= TRUE
;
2203 necp_client_add_multipath_interface_flows(flow_registration
, client
, mpp
);
2204 } else if (RB_EMPTY(&client
->flow_registrations
) && !necp_client_id_is_flow(client_id
)) {
2205 // No flows yet on this client, add a new registration
2206 flow_registration
= necp_client_create_flow_registration(client_fd
, client
);
2207 if (flow_registration
== NULL
) {
2211 found_client
= TRUE
;
2212 necp_client_add_multipath_interface_flows(flow_registration
, client
, mpp
);
2217 NECP_CLIENT_UNLOCK(client
);
2219 NECP_FD_UNLOCK(client_fd
);
2225 NECP_FD_LIST_UNLOCK();
2227 if (!found_client
&& error
== 0) {
2234 #define NETAGENT_DOMAIN_RADIO_MANAGER "WirelessRadioManager"
2235 #define NETAGENT_TYPE_RADIO_MANAGER "WirelessRadioManager:BB Manager"
2238 necp_client_lookup_bb_radio_manager(struct necp_client
*client
,
2239 uuid_t netagent_uuid
)
2241 char netagent_domain
[NETAGENT_DOMAINSIZE
];
2242 char netagent_type
[NETAGENT_TYPESIZE
];
2243 struct necp_aggregate_result result
;
2247 proc
= proc_find(client
->proc_pid
);
2248 if (proc
== PROC_NULL
) {
2252 error
= necp_application_find_policy_match_internal(proc
, client
->parameters
, (u_int32_t
)client
->parameters_length
,
2253 &result
, NULL
, 0, NULL
, NULL
, NULL
, true);
2262 for (int i
= 0; i
< NECP_MAX_NETAGENTS
; i
++) {
2263 if (uuid_is_null(result
.netagents
[i
])) {
2264 // Passed end of valid agents
2268 memset(&netagent_domain
, 0, NETAGENT_DOMAINSIZE
);
2269 memset(&netagent_type
, 0, NETAGENT_TYPESIZE
);
2270 if (netagent_get_agent_domain_and_type(result
.netagents
[i
], netagent_domain
, netagent_type
) == FALSE
) {
2274 if (strncmp(netagent_domain
, NETAGENT_DOMAIN_RADIO_MANAGER
, NETAGENT_DOMAINSIZE
) != 0) {
2278 if (strncmp(netagent_type
, NETAGENT_TYPE_RADIO_MANAGER
, NETAGENT_TYPESIZE
) != 0) {
2282 uuid_copy(netagent_uuid
, result
.netagents
[i
]);
2291 necp_client_assert_bb_radio_manager_common(struct necp_client
*client
, bool assert)
2293 uuid_t netagent_uuid
;
2294 uint8_t assert_type
;
2297 error
= necp_client_lookup_bb_radio_manager(client
, netagent_uuid
);
2299 NECPLOG0(LOG_ERR
, "BB radio manager agent not found");
2303 // Before unasserting, verify that the assertion was already taken
2304 if (assert == FALSE
) {
2305 assert_type
= NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT
;
2307 if (!necp_client_remove_assertion(client
, netagent_uuid
)) {
2311 assert_type
= NETAGENT_MESSAGE_TYPE_CLIENT_ASSERT
;
2314 error
= netagent_client_message(netagent_uuid
, client
->client_id
, client
->proc_pid
, client
->agent_handle
, assert_type
);
2316 NECPLOG0(LOG_ERR
, "netagent_client_message failed");
2320 // Only save the assertion if the action succeeded
2321 if (assert == TRUE
) {
2322 necp_client_add_assertion(client
, netagent_uuid
);
2329 necp_client_assert_bb_radio_manager(uuid_t client_id
, bool assert)
2331 struct necp_client
*client
;
2334 NECP_CLIENT_TREE_LOCK_SHARED();
2336 client
= necp_find_client_and_lock(client_id
);
2339 // Found the right client!
2340 error
= necp_client_assert_bb_radio_manager_common(client
, assert);
2342 NECP_CLIENT_UNLOCK(client
);
2344 NECPLOG0(LOG_ERR
, "Couldn't find client");
2348 NECP_CLIENT_TREE_UNLOCK();
2354 necp_client_unregister_socket_flow(uuid_t client_id
, void *handle
)
2357 struct necp_fd_data
*client_fd
= NULL
;
2358 bool found_client
= FALSE
;
2359 bool client_updated
= FALSE
;
2361 NECP_FD_LIST_LOCK_SHARED();
2362 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
2363 NECP_FD_LOCK(client_fd
);
2365 struct necp_client
*client
= necp_client_fd_find_client_and_lock(client_fd
, client_id
);
2366 if (client
!= NULL
) {
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!
2370 found_client
= TRUE
;
2372 // Remove flow assignment
2373 struct necp_client_flow
*search_flow
= NULL
;
2374 struct necp_client_flow
*temp_flow
= NULL
;
2375 LIST_FOREACH_SAFE(search_flow
, &flow_registration
->flow_list
, flow_chain
, temp_flow
) {
2376 if (search_flow
->socket
&& search_flow
->u
.socket_handle
== handle
) {
2377 if (search_flow
->assigned_results
!= NULL
) {
2378 FREE(search_flow
->assigned_results
, M_NETAGENT
);
2379 search_flow
->assigned_results
= NULL
;
2381 client_updated
= TRUE
;
2382 flow_registration
->flow_result_read
= FALSE
;
2383 LIST_REMOVE(search_flow
, flow_chain
);
2384 OSDecrementAtomic(&necp_socket_flow_count
);
2385 mcache_free(necp_flow_cache
, search_flow
);
2390 NECP_CLIENT_UNLOCK(client
);
2393 if (client_updated
) {
2394 necp_fd_notify(client_fd
, true);
2396 NECP_FD_UNLOCK(client_fd
);
2402 NECP_FD_LIST_UNLOCK();
2404 if (!found_client
) {
2412 necp_client_unregister_multipath_cb(uuid_t client_id
, void *handle
)
2415 bool found_client
= FALSE
;
2417 NECP_CLIENT_TREE_LOCK_SHARED();
2419 struct necp_client
*client
= necp_find_client_and_lock(client_id
);
2420 if (client
!= NULL
) {
2421 struct necp_client_flow_registration
*flow_registration
= necp_client_find_flow(client
, client_id
);
2422 if (flow_registration
!= NULL
) {
2423 // Found the right client and flow!
2424 found_client
= TRUE
;
2426 // Remove flow assignment
2427 struct necp_client_flow
*search_flow
= NULL
;
2428 struct necp_client_flow
*temp_flow
= NULL
;
2429 LIST_FOREACH_SAFE(search_flow
, &flow_registration
->flow_list
, flow_chain
, temp_flow
) {
2430 if (!search_flow
->socket
&& !search_flow
->nexus
&&
2431 search_flow
->u
.socket_handle
== handle
) {
2432 search_flow
->u
.socket_handle
= NULL
;
2433 search_flow
->u
.cb
= NULL
;
2437 flow_registration
->interface_handle
= NULL
;
2438 flow_registration
->interface_cb
= NULL
;
2441 NECP_CLIENT_UNLOCK(client
);
2444 NECP_CLIENT_TREE_UNLOCK();
2446 if (!found_client
) {
2454 necp_client_assign_from_socket(pid_t pid
, uuid_t client_id
, struct inpcb
*inp
)
2457 struct necp_fd_data
*client_fd
= NULL
;
2458 bool found_client
= FALSE
;
2459 bool client_updated
= FALSE
;
2461 NECP_FD_LIST_LOCK_SHARED();
2462 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
2463 if (pid
&& client_fd
->proc_pid
!= pid
) {
2467 proc_t proc
= proc_find(client_fd
->proc_pid
);
2468 if (proc
== PROC_NULL
) {
2472 NECP_FD_LOCK(client_fd
);
2474 struct necp_client
*client
= necp_client_fd_find_client_and_lock(client_fd
, client_id
);
2475 if (client
!= NULL
) {
2476 struct necp_client_flow_registration
*flow_registration
= necp_client_find_flow(client
, client_id
);
2477 if (flow_registration
== NULL
&& RB_EMPTY(&client
->flow_registrations
) && !necp_client_id_is_flow(client_id
)) {
2478 // No flows yet on this client, add a new registration
2479 flow_registration
= necp_client_create_flow_registration(client_fd
, client
);
2480 if (flow_registration
== NULL
) {
2484 if (flow_registration
!= NULL
) {
2485 // Found the right client and flow!
2486 found_client
= TRUE
;
2488 struct necp_client_flow
*flow
= NULL
;
2489 LIST_FOREACH(flow
, &flow_registration
->flow_list
, flow_chain
) {
2490 if (flow
->socket
&& flow
->u
.socket_handle
== inp
) {
2491 // Release prior results and route
2492 if (flow
->assigned_results
!= NULL
) {
2493 FREE(flow
->assigned_results
, M_NETAGENT
);
2494 flow
->assigned_results
= NULL
;
2498 if ((inp
->inp_flags
& INP_BOUND_IF
) && inp
->inp_boundifp
) {
2499 ifp
= inp
->inp_boundifp
;
2501 ifp
= inp
->inp_last_outifp
;
2505 flow
->interface_index
= ifp
->if_index
;
2507 flow
->interface_index
= IFSCOPE_NONE
;
2510 if (inp
->inp_vflag
& INP_IPV4
) {
2511 flow
->local_addr
.sin
.sin_family
= AF_INET
;
2512 flow
->local_addr
.sin
.sin_len
= sizeof(struct sockaddr_in
);
2513 flow
->local_addr
.sin
.sin_port
= inp
->inp_lport
;
2514 memcpy(&flow
->local_addr
.sin
.sin_addr
, &inp
->inp_laddr
, sizeof(struct in_addr
));
2516 flow
->remote_addr
.sin
.sin_family
= AF_INET
;
2517 flow
->remote_addr
.sin
.sin_len
= sizeof(struct sockaddr_in
);
2518 flow
->remote_addr
.sin
.sin_port
= inp
->inp_fport
;
2519 memcpy(&flow
->remote_addr
.sin
.sin_addr
, &inp
->inp_faddr
, sizeof(struct in_addr
));
2520 } else if (inp
->inp_vflag
& INP_IPV6
) {
2521 in6_ip6_to_sockaddr(&inp
->in6p_laddr
, inp
->inp_lport
, &flow
->local_addr
.sin6
, sizeof(flow
->local_addr
));
2522 in6_ip6_to_sockaddr(&inp
->in6p_faddr
, inp
->inp_fport
, &flow
->remote_addr
.sin6
, sizeof(flow
->remote_addr
));
2525 flow
->viable
= necp_client_flow_is_viable(proc
, client
, flow
);
2528 uuid_clear(empty_uuid
);
2529 flow
->assigned
= TRUE
;
2530 flow
->assigned_results
= necp_create_nexus_assign_message(empty_uuid
, 0, NULL
, 0,
2531 (struct necp_client_endpoint
*)&flow
->local_addr
,
2532 (struct necp_client_endpoint
*)&flow
->remote_addr
,
2533 0, NULL
, &flow
->assigned_results_length
);
2534 flow_registration
->flow_result_read
= FALSE
;
2535 client_updated
= TRUE
;
2541 NECP_CLIENT_UNLOCK(client
);
2543 if (client_updated
) {
2544 necp_fd_notify(client_fd
, true);
2546 NECP_FD_UNLOCK(client_fd
);
2555 NECP_FD_LIST_UNLOCK();
2558 if (!found_client
) {
2560 } else if (!client_updated
) {
2569 necp_update_flow_protoctl_event(uuid_t netagent_uuid
, uuid_t client_id
,
2570 uint32_t protoctl_event_code
, uint32_t protoctl_event_val
,
2571 uint32_t protoctl_event_tcp_seq_number
)
2574 struct necp_fd_data
*client_fd
= NULL
;
2575 bool found_client
= FALSE
;
2576 bool client_updated
= FALSE
;
2578 NECP_FD_LIST_LOCK_SHARED();
2579 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
2580 proc_t proc
= proc_find(client_fd
->proc_pid
);
2581 if (proc
== PROC_NULL
) {
2585 NECP_FD_LOCK(client_fd
);
2587 struct necp_client
*client
= necp_client_fd_find_client_and_lock(client_fd
, 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 struct necp_client_flow
*flow
= NULL
;
2595 LIST_FOREACH(flow
, &flow_registration
->flow_list
, flow_chain
) {
2596 // Verify that the client nexus agent matches
2598 uuid_compare(flow
->u
.nexus_agent
,
2599 netagent_uuid
) == 0) {
2600 flow
->has_protoctl_event
= TRUE
;
2601 flow
->protoctl_event
.protoctl_event_code
= protoctl_event_code
;
2602 flow
->protoctl_event
.protoctl_event_val
= protoctl_event_val
;
2603 flow
->protoctl_event
.protoctl_event_tcp_seq_num
= protoctl_event_tcp_seq_number
;
2604 flow_registration
->flow_result_read
= FALSE
;
2605 client_updated
= TRUE
;
2611 NECP_CLIENT_UNLOCK(client
);
2614 if (client_updated
) {
2615 necp_fd_notify(client_fd
, true);
2618 NECP_FD_UNLOCK(client_fd
);
2626 NECP_FD_LIST_UNLOCK();
2628 if (!found_client
) {
2630 } else if (!client_updated
) {
2637 necp_assign_client_result_locked(struct proc
*proc
,
2638 struct necp_fd_data
*client_fd
,
2639 struct necp_client
*client
,
2640 struct necp_client_flow_registration
*flow_registration
,
2641 uuid_t netagent_uuid
,
2642 u_int8_t
*assigned_results
,
2643 size_t assigned_results_length
,
2646 bool client_updated
= FALSE
;
2648 NECP_FD_ASSERT_LOCKED(client_fd
);
2649 NECP_CLIENT_ASSERT_LOCKED(client
);
2651 struct necp_client_flow
*flow
= NULL
;
2652 LIST_FOREACH(flow
, &flow_registration
->flow_list
, flow_chain
) {
2653 // Verify that the client nexus agent matches
2655 uuid_compare(flow
->u
.nexus_agent
, netagent_uuid
) == 0) {
2656 // Release prior results and route
2657 if (flow
->assigned_results
!= NULL
) {
2658 FREE(flow
->assigned_results
, M_NETAGENT
);
2659 flow
->assigned_results
= NULL
;
2662 void *nexus_stats
= NULL
;
2663 if (assigned_results
!= NULL
&& assigned_results_length
> 0) {
2664 int error
= necp_client_parse_result(assigned_results
, (u_int32_t
)assigned_results_length
,
2665 &flow
->local_addr
, &flow
->remote_addr
, &nexus_stats
);
2669 flow
->viable
= necp_client_flow_is_viable(proc
, client
, flow
);
2671 flow
->assigned
= TRUE
;
2672 flow
->assigned_results
= assigned_results
;
2673 flow
->assigned_results_length
= assigned_results_length
;
2674 flow_registration
->flow_result_read
= FALSE
;
2675 client_updated
= TRUE
;
2680 if (client_updated
&& notify_fd
) {
2681 necp_fd_notify(client_fd
, true);
2684 // if not updated, client must free assigned_results
2685 return (client_updated
);
2689 necp_assign_client_result(uuid_t netagent_uuid
, uuid_t client_id
,
2690 u_int8_t
*assigned_results
, size_t assigned_results_length
)
2693 struct necp_fd_data
*client_fd
= NULL
;
2694 bool found_client
= FALSE
;
2695 bool client_updated
= FALSE
;
2697 NECP_FD_LIST_LOCK_SHARED();
2699 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
2700 proc_t proc
= proc_find(client_fd
->proc_pid
);
2701 if (proc
== PROC_NULL
) {
2705 NECP_FD_LOCK(client_fd
);
2706 struct necp_client
*client
= necp_client_fd_find_client_and_lock(client_fd
, client_id
);
2707 if (client
!= NULL
) {
2708 struct necp_client_flow_registration
*flow_registration
= necp_client_find_flow(client
, client_id
);
2709 if (flow_registration
!= NULL
) {
2710 // Found the right client and flow!
2711 found_client
= TRUE
;
2712 if (necp_assign_client_result_locked(proc
, client_fd
, client
, flow_registration
, netagent_uuid
,
2713 assigned_results
, assigned_results_length
, true)) {
2714 client_updated
= TRUE
;
2718 NECP_CLIENT_UNLOCK(client
);
2720 NECP_FD_UNLOCK(client_fd
);
2730 NECP_FD_LIST_UNLOCK();
2732 // upon error, client must free assigned_results
2733 if (!found_client
) {
2735 } else if (!client_updated
) {
2745 necp_update_parsed_parameters(struct necp_client_parsed_parameters
*parsed_parameters
,
2746 struct necp_aggregate_result
*result
)
2748 if (parsed_parameters
== NULL
||
2753 bool updated
= false;
2754 for (int i
= 0; i
< NECP_MAX_NETAGENTS
; i
++) {
2755 if (uuid_is_null(result
->netagents
[i
])) {
2756 // Passed end of valid agents
2760 if (!(result
->netagent_use_flags
[i
] & NECP_AGENT_USE_FLAG_SCOPE
)) {
2761 // Not a scoped agent, ignore
2765 // This is a scoped agent. Add it to the required agents.
2766 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT
) {
2767 // Already some required agents, add this at the end
2768 for (int j
= 0; j
< NECP_MAX_PARSED_PARAMETERS
; j
++) {
2769 if (uuid_compare(parsed_parameters
->required_netagents
[j
], result
->netagents
[i
]) == 0) {
2770 // Already required, break
2773 if (uuid_is_null(parsed_parameters
->required_netagents
[j
])) {
2775 memcpy(&parsed_parameters
->required_netagents
[j
], result
->netagents
[i
], sizeof(uuid_t
));
2781 // No required agents yet, add this one
2782 parsed_parameters
->valid_fields
|= NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT
;
2783 memcpy(&parsed_parameters
->required_netagents
[0], result
->netagents
[i
], sizeof(uuid_t
));
2787 // Remove requirements for agents of the same type
2788 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE
) {
2789 char remove_agent_domain
[NETAGENT_DOMAINSIZE
] = { 0 };
2790 char remove_agent_type
[NETAGENT_TYPESIZE
] = { 0 };
2791 if (netagent_get_agent_domain_and_type(result
->netagents
[i
], remove_agent_domain
, remove_agent_type
)) {
2792 for (int j
= 0; j
< NECP_MAX_PARSED_PARAMETERS
; j
++) {
2793 if (strlen(parsed_parameters
->required_netagent_types
[j
].netagent_domain
) == 0 &&
2794 strlen(parsed_parameters
->required_netagent_types
[j
].netagent_type
) == 0) {
2798 if (strncmp(parsed_parameters
->required_netagent_types
[j
].netagent_domain
, remove_agent_domain
, NETAGENT_DOMAINSIZE
) == 0 &&
2799 strncmp(parsed_parameters
->required_netagent_types
[j
].netagent_type
, remove_agent_type
, NETAGENT_TYPESIZE
) == 0) {
2803 if (j
== NECP_MAX_PARSED_PARAMETERS
- 1) {
2804 // Last field, just clear and break
2805 memset(&parsed_parameters
->required_netagent_types
[NECP_MAX_PARSED_PARAMETERS
- 1], 0, sizeof(struct necp_client_parameter_netagent_type
));
2808 // Move the parameters down, clear the last entry
2809 memmove(&parsed_parameters
->required_netagent_types
[j
],
2810 &parsed_parameters
->required_netagent_types
[j
+ 1],
2811 sizeof(struct necp_client_parameter_netagent_type
) * (NECP_MAX_PARSED_PARAMETERS
- (j
+ 1)));
2812 memset(&parsed_parameters
->required_netagent_types
[NECP_MAX_PARSED_PARAMETERS
- 1], 0, sizeof(struct necp_client_parameter_netagent_type
));
2813 // Continue, don't increment but look at the new shifted item instead
2818 // Increment j to look at the next agent type parameter
2826 parsed_parameters
->required_interface_index
!= IFSCOPE_NONE
&&
2827 (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IF
) == 0) {
2828 // A required interface index was added after the fact. Clear it.
2829 parsed_parameters
->required_interface_index
= IFSCOPE_NONE
;
2837 necp_agent_types_match(const char *agent_domain1
, const char *agent_type1
,
2838 const char *agent_domain2
, const char *agent_type2
)
2840 return ((strlen(agent_domain1
) == 0 ||
2841 strncmp(agent_domain2
, agent_domain1
, NETAGENT_DOMAINSIZE
) == 0) &&
2842 (strlen(agent_type1
) == 0 ||
2843 strncmp(agent_type2
, agent_type1
, NETAGENT_TYPESIZE
) == 0));
2847 necp_calculate_client_result(proc_t proc
,
2848 struct necp_client
*client
,
2849 struct necp_client_parsed_parameters
*parsed_parameters
,
2850 struct necp_aggregate_result
*result
,
2853 struct rtentry
*route
= NULL
;
2855 // Check parameters to find best interface
2856 bool validate_agents
= false;
2857 u_int matching_if_index
= 0;
2858 if (necp_find_matching_interface_index(parsed_parameters
, &matching_if_index
, &validate_agents
)) {
2859 if (matching_if_index
!= 0) {
2860 parsed_parameters
->required_interface_index
= matching_if_index
;
2862 // Interface found or not needed, match policy.
2863 memset(result
, 0, sizeof(*result
));
2864 int error
= necp_application_find_policy_match_internal(proc
, client
->parameters
,
2865 (u_int32_t
)client
->parameters_length
,
2866 result
, flags
, matching_if_index
,
2867 NULL
, NULL
, &route
, false);
2869 if (route
!= NULL
) {
2875 if (validate_agents
) {
2876 bool requirement_failed
= FALSE
;
2877 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT
) {
2878 for (int i
= 0; i
< NECP_MAX_PARSED_PARAMETERS
; i
++) {
2879 if (uuid_is_null(parsed_parameters
->required_netagents
[i
])) {
2883 bool requirement_found
= FALSE
;
2884 for (int j
= 0; j
< NECP_MAX_NETAGENTS
; j
++) {
2885 if (uuid_is_null(result
->netagents
[j
])) {
2889 if (uuid_compare(parsed_parameters
->required_netagents
[i
], result
->netagents
[j
]) == 0) {
2890 requirement_found
= TRUE
;
2895 if (!requirement_found
) {
2896 requirement_failed
= TRUE
;
2902 if (!requirement_failed
&& parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE
) {
2903 for (int i
= 0; i
< NECP_MAX_PARSED_PARAMETERS
; i
++) {
2904 if (strlen(parsed_parameters
->required_netagent_types
[i
].netagent_domain
) == 0 &&
2905 strlen(parsed_parameters
->required_netagent_types
[i
].netagent_type
) == 0) {
2909 bool requirement_found
= FALSE
;
2910 for (int j
= 0; j
< NECP_MAX_NETAGENTS
; j
++) {
2911 if (uuid_is_null(result
->netagents
[j
])) {
2915 char policy_agent_domain
[NETAGENT_DOMAINSIZE
] = { 0 };
2916 char policy_agent_type
[NETAGENT_TYPESIZE
] = { 0 };
2918 if (netagent_get_agent_domain_and_type(result
->netagents
[j
], policy_agent_domain
, policy_agent_type
)) {
2919 if (necp_agent_types_match(parsed_parameters
->required_netagent_types
[i
].netagent_domain
,
2920 parsed_parameters
->required_netagent_types
[i
].netagent_type
,
2921 policy_agent_domain
, policy_agent_type
)) {
2922 requirement_found
= TRUE
;
2928 if (!requirement_found
) {
2929 requirement_failed
= TRUE
;
2935 if (requirement_failed
) {
2936 // Agent requirement failed. Clear out the whole result, make everything fail.
2937 memset(result
, 0, sizeof(*result
));
2938 if (route
!= NULL
) {
2945 // Reset current route
2946 NECP_CLIENT_ROUTE_LOCK(client
);
2947 if (client
->current_route
!= NULL
) {
2948 rtfree(client
->current_route
);
2950 client
->current_route
= route
;
2951 NECP_CLIENT_ROUTE_UNLOCK(client
);
2953 // Interface not found. Clear out the whole result, make everything fail.
2954 memset(result
, 0, sizeof(*result
));
2961 necp_update_client_result(proc_t proc
,
2962 struct necp_fd_data
*client_fd
,
2963 struct necp_client
*client
,
2964 struct _necp_flow_defunct_list
*defunct_list
)
2966 struct necp_client_result_netagent netagent
;
2967 struct necp_aggregate_result result
;
2968 struct necp_client_parsed_parameters
*parsed_parameters
= NULL
;
2969 u_int32_t flags
= 0;
2971 NECP_CLIENT_ASSERT_LOCKED(client
);
2973 MALLOC(parsed_parameters
, struct necp_client_parsed_parameters
*, sizeof(*parsed_parameters
), M_NECP
, (M_WAITOK
| M_ZERO
));
2974 if (parsed_parameters
== NULL
) {
2975 NECPLOG0(LOG_ERR
, "Failed to allocate parsed parameters");
2979 // Nexus flows will be brought back if they are still valid
2980 necp_client_mark_all_nonsocket_flows_as_invalid(client
);
2982 int error
= necp_client_parse_parameters(client
->parameters
, (u_int32_t
)client
->parameters_length
, parsed_parameters
);
2984 FREE(parsed_parameters
, M_NECP
);
2988 // Update saved IP protocol
2989 client
->ip_protocol
= parsed_parameters
->ip_protocol
;
2991 // Calculate the policy result
2992 if (!necp_calculate_client_result(proc
, client
, parsed_parameters
, &result
, &flags
)) {
2993 FREE(parsed_parameters
, M_NECP
);
2997 if (necp_update_parsed_parameters(parsed_parameters
, &result
)) {
2998 // Changed the parameters based on result, try again (only once)
2999 if (!necp_calculate_client_result(proc
, client
, parsed_parameters
, &result
, &flags
)) {
3000 FREE(parsed_parameters
, M_NECP
);
3005 // Save the last policy id on the client
3006 client
->policy_id
= result
.policy_id
;
3008 if ((parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_MULTIPATH
) ||
3009 ((parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_LISTENER
) &&
3010 result
.routing_result
!= NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED
)) {
3011 client
->allow_multiple_flows
= TRUE
;
3013 client
->allow_multiple_flows
= FALSE
;
3016 // If the original request was scoped, and the policy result matches, make sure the result is scoped
3017 if ((result
.routing_result
== NECP_KERNEL_POLICY_RESULT_NONE
||
3018 result
.routing_result
== NECP_KERNEL_POLICY_RESULT_PASS
) &&
3019 result
.routed_interface_index
!= IFSCOPE_NONE
&&
3020 parsed_parameters
->required_interface_index
== result
.routed_interface_index
) {
3021 result
.routing_result
= NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED
;
3022 result
.routing_result_parameter
.scoped_interface_index
= result
.routed_interface_index
;
3025 if (defunct_list
!= NULL
&&
3026 result
.routing_result
== NECP_KERNEL_POLICY_RESULT_DROP
) {
3027 // If we are forced to drop the client, defunct it if it has flows
3028 necp_defunct_client_for_policy(client
, defunct_list
);
3031 // Recalculate flags
3032 if (parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_LISTENER
) {
3033 // Listeners are valid as long as they aren't dropped
3034 if (result
.routing_result
!= NECP_KERNEL_POLICY_RESULT_DROP
) {
3035 flags
|= NECP_CLIENT_RESULT_FLAG_SATISFIED
;
3037 } else if (result
.routed_interface_index
!= 0) {
3038 // Clients without flows determine viability based on having some routable interface
3039 flags
|= NECP_CLIENT_RESULT_FLAG_SATISFIED
;
3042 bool updated
= FALSE
;
3043 u_int8_t
*cursor
= client
->result
;
3044 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_FLAGS
, sizeof(flags
), &flags
, &updated
, client
->result
, sizeof(client
->result
));
3045 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_CLIENT_ID
, sizeof(uuid_t
), client
->client_id
, &updated
,
3046 client
->result
, sizeof(client
->result
));
3047 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_POLICY_RESULT
, sizeof(result
.routing_result
), &result
.routing_result
, &updated
,
3048 client
->result
, sizeof(client
->result
));
3049 if (result
.routing_result_parameter
.tunnel_interface_index
!= 0) {
3050 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_POLICY_RESULT_PARAMETER
,
3051 sizeof(result
.routing_result_parameter
), &result
.routing_result_parameter
, &updated
,
3052 client
->result
, sizeof(client
->result
));
3054 if (result
.filter_control_unit
!= 0) {
3055 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_FILTER_CONTROL_UNIT
,
3056 sizeof(result
.filter_control_unit
), &result
.filter_control_unit
, &updated
,
3057 client
->result
, sizeof(client
->result
));
3059 if (result
.routed_interface_index
!= 0) {
3060 u_int routed_interface_index
= result
.routed_interface_index
;
3061 if (result
.routing_result
== NECP_KERNEL_POLICY_RESULT_IP_TUNNEL
&&
3062 parsed_parameters
->required_interface_index
!= IFSCOPE_NONE
&&
3063 parsed_parameters
->required_interface_index
!= result
.routed_interface_index
) {
3064 routed_interface_index
= parsed_parameters
->required_interface_index
;
3067 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_INTERFACE_INDEX
,
3068 sizeof(routed_interface_index
), &routed_interface_index
, &updated
,
3069 client
->result
, sizeof(client
->result
));
3071 if (client_fd
&& client_fd
->flags
& NECP_OPEN_FLAG_BACKGROUND
) {
3072 u_int32_t effective_traffic_class
= SO_TC_BK_SYS
;
3073 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_EFFECTIVE_TRAFFIC_CLASS
,
3074 sizeof(effective_traffic_class
), &effective_traffic_class
, &updated
,
3075 client
->result
, sizeof(client
->result
));
3077 if (client
->background_update
) {
3078 u_int32_t background
= client
->background
;
3079 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_TRAFFIC_MGMT_BG
,
3080 sizeof(background
), &background
, &updated
,
3081 client
->result
, sizeof(client
->result
));
3083 client
->background_update
= 0;
3086 NECP_CLIENT_ROUTE_LOCK(client
);
3087 if (client
->current_route
!= NULL
) {
3088 const u_int32_t route_mtu
= get_maxmtu(client
->current_route
);
3089 if (route_mtu
!= 0) {
3090 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_EFFECTIVE_MTU
,
3091 sizeof(route_mtu
), &route_mtu
, &updated
,
3092 client
->result
, sizeof(client
->result
));
3095 NECP_CLIENT_ROUTE_UNLOCK(client
);
3097 if (result
.mss_recommended
!= 0) {
3098 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_RECOMMENDED_MSS
,
3099 sizeof(result
.mss_recommended
), &result
.mss_recommended
, &updated
,
3100 client
->result
, sizeof(client
->result
));
3103 for (int i
= 0; i
< NECP_MAX_NETAGENTS
; i
++) {
3104 if (uuid_is_null(result
.netagents
[i
])) {
3107 uuid_copy(netagent
.netagent_uuid
, result
.netagents
[i
]);
3108 netagent
.generation
= netagent_get_generation(netagent
.netagent_uuid
);
3109 if (necp_netagent_applies_to_client(client
, parsed_parameters
, &netagent
.netagent_uuid
, TRUE
, 0, 0)) {
3110 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_NETAGENT
, sizeof(netagent
), &netagent
, &updated
,
3111 client
->result
, sizeof(client
->result
));
3115 ifnet_head_lock_shared();
3116 ifnet_t direct_interface
= NULL
;
3117 ifnet_t delegate_interface
= NULL
;
3118 ifnet_t original_scoped_interface
= NULL
;
3120 if (result
.routed_interface_index
!= IFSCOPE_NONE
&& result
.routed_interface_index
<= (u_int32_t
)if_index
) {
3121 direct_interface
= ifindex2ifnet
[result
.routed_interface_index
];
3122 } else if (parsed_parameters
->required_interface_index
!= IFSCOPE_NONE
&&
3123 parsed_parameters
->required_interface_index
<= (u_int32_t
)if_index
) {
3124 // If the request was scoped, but the route didn't match, still grab the agents
3125 direct_interface
= ifindex2ifnet
[parsed_parameters
->required_interface_index
];
3126 } else if (result
.routed_interface_index
== IFSCOPE_NONE
&&
3127 result
.routing_result
== NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED
&&
3128 result
.routing_result_parameter
.scoped_interface_index
!= IFSCOPE_NONE
) {
3129 direct_interface
= ifindex2ifnet
[result
.routing_result_parameter
.scoped_interface_index
];
3131 if (direct_interface
!= NULL
) {
3132 delegate_interface
= direct_interface
->if_delegated
.ifp
;
3134 if (result
.routing_result
== NECP_KERNEL_POLICY_RESULT_IP_TUNNEL
&&
3135 parsed_parameters
->required_interface_index
!= IFSCOPE_NONE
&&
3136 parsed_parameters
->required_interface_index
!= result
.routing_result_parameter
.tunnel_interface_index
&&
3137 parsed_parameters
->required_interface_index
<= (u_int32_t
)if_index
) {
3138 original_scoped_interface
= ifindex2ifnet
[parsed_parameters
->required_interface_index
];
3141 if (original_scoped_interface
!= NULL
) {
3142 struct necp_client_result_interface interface_struct
;
3143 interface_struct
.index
= original_scoped_interface
->if_index
;
3144 interface_struct
.generation
= ifnet_get_generation(original_scoped_interface
);
3145 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_INTERFACE
, sizeof(interface_struct
), &interface_struct
, &updated
,
3146 client
->result
, sizeof(client
->result
));
3148 if (direct_interface
!= NULL
) {
3149 struct necp_client_result_interface interface_struct
;
3150 interface_struct
.index
= direct_interface
->if_index
;
3151 interface_struct
.generation
= ifnet_get_generation(direct_interface
);
3152 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_INTERFACE
, sizeof(interface_struct
), &interface_struct
, &updated
,
3153 client
->result
, sizeof(client
->result
));
3155 // Set the delta time since interface up/down
3156 struct timeval updown_delta
= {};
3157 if (ifnet_updown_delta(direct_interface
, &updown_delta
) == 0) {
3158 u_int32_t delta
= updown_delta
.tv_sec
;
3159 bool ignore_updated
= FALSE
;
3160 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_INTERFACE_TIME_DELTA
,
3161 sizeof(delta
), &delta
, &ignore_updated
,
3162 client
->result
, sizeof(client
->result
));
3165 if (delegate_interface
!= NULL
) {
3166 struct necp_client_result_interface interface_struct
;
3167 interface_struct
.index
= delegate_interface
->if_index
;
3168 interface_struct
.generation
= ifnet_get_generation(delegate_interface
);
3169 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_INTERFACE
, sizeof(interface_struct
), &interface_struct
, &updated
,
3170 client
->result
, sizeof(client
->result
));
3173 // Update multipath/listener interface flows
3174 if (parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_MULTIPATH
) {
3175 // Get multipath interface options from ordered list
3176 struct ifnet
*multi_interface
= NULL
;
3177 TAILQ_FOREACH(multi_interface
, &ifnet_ordered_head
, if_ordered_link
) {
3178 if (necp_ifnet_matches_parameters(multi_interface
, parsed_parameters
, NULL
, true)) {
3179 // Add multipath interface flows for kernel MPTCP
3180 necp_client_add_interface_option_if_needed(client
, multi_interface
->if_index
,
3181 ifnet_get_generation(multi_interface
), NULL
);
3183 // Add nexus agents for multipath
3184 necp_client_add_agent_interface_options(client
, parsed_parameters
, multi_interface
);
3187 } else if ((parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_LISTENER
) &&
3188 result
.routing_result
!= NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED
) {
3189 // Get listener interface options from global list
3190 struct ifnet
*listen_interface
= NULL
;
3191 TAILQ_FOREACH(listen_interface
, &ifnet_head
, if_link
) {
3192 if (necp_ifnet_matches_parameters(listen_interface
, parsed_parameters
, NULL
, true)) {
3193 // Add nexus agents for listeners
3194 necp_client_add_agent_interface_options(client
, parsed_parameters
, listen_interface
);
3200 if (original_scoped_interface
!= NULL
) {
3201 ifnet_lock_shared(original_scoped_interface
);
3202 if (original_scoped_interface
->if_agentids
!= NULL
) {
3203 for (u_int32_t i
= 0; i
< original_scoped_interface
->if_agentcount
; i
++) {
3204 if (uuid_is_null(original_scoped_interface
->if_agentids
[i
])) {
3207 uuid_copy(netagent
.netagent_uuid
, original_scoped_interface
->if_agentids
[i
]);
3208 netagent
.generation
= netagent_get_generation(netagent
.netagent_uuid
);
3209 if (necp_netagent_applies_to_client(client
, parsed_parameters
, &netagent
.netagent_uuid
, FALSE
,
3210 original_scoped_interface
->if_index
, ifnet_get_generation(original_scoped_interface
))) {
3211 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_NETAGENT
, sizeof(netagent
), &netagent
, &updated
,
3212 client
->result
, sizeof(client
->result
));
3216 ifnet_lock_done(original_scoped_interface
);
3218 if (direct_interface
!= NULL
) {
3219 ifnet_lock_shared(direct_interface
);
3220 if (direct_interface
->if_agentids
!= NULL
) {
3221 for (u_int32_t i
= 0; i
< direct_interface
->if_agentcount
; i
++) {
3222 if (uuid_is_null(direct_interface
->if_agentids
[i
])) {
3225 uuid_copy(netagent
.netagent_uuid
, direct_interface
->if_agentids
[i
]);
3226 netagent
.generation
= netagent_get_generation(netagent
.netagent_uuid
);
3227 if (necp_netagent_applies_to_client(client
, parsed_parameters
, &netagent
.netagent_uuid
, TRUE
,
3228 direct_interface
->if_index
, ifnet_get_generation(direct_interface
))) {
3229 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_NETAGENT
, sizeof(netagent
), &netagent
, &updated
,
3230 client
->result
, sizeof(client
->result
));
3234 ifnet_lock_done(direct_interface
);
3236 if (delegate_interface
!= NULL
) {
3237 ifnet_lock_shared(delegate_interface
);
3238 if (delegate_interface
->if_agentids
!= NULL
) {
3239 for (u_int32_t i
= 0; i
< delegate_interface
->if_agentcount
; i
++) {
3240 if (uuid_is_null(delegate_interface
->if_agentids
[i
])) {
3243 uuid_copy(netagent
.netagent_uuid
, delegate_interface
->if_agentids
[i
]);
3244 netagent
.generation
= netagent_get_generation(netagent
.netagent_uuid
);
3245 if (necp_netagent_applies_to_client(client
, parsed_parameters
, &netagent
.netagent_uuid
, FALSE
,
3246 delegate_interface
->if_index
, ifnet_get_generation(delegate_interface
))) {
3247 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_NETAGENT
, sizeof(netagent
), &netagent
, &updated
,
3248 client
->result
, sizeof(client
->result
));
3252 ifnet_lock_done(delegate_interface
);
3256 // Add interface options
3257 for (u_int32_t option_i
= 0; option_i
< client
->interface_option_count
; option_i
++) {
3258 if (option_i
< NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
) {
3259 struct necp_client_interface_option
*option
= &client
->interface_options
[option_i
];
3260 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_INTERFACE_OPTION
, sizeof(*option
), option
, &updated
,
3261 client
->result
, sizeof(client
->result
));
3263 struct necp_client_interface_option
*option
= &client
->extra_interface_options
[option_i
- NECP_CLIENT_INTERFACE_OPTION_STATIC_COUNT
];
3264 cursor
= necp_buffer_write_tlv_if_different(cursor
, NECP_CLIENT_RESULT_INTERFACE_OPTION
, sizeof(*option
), option
, &updated
,
3265 client
->result
, sizeof(client
->result
));
3269 size_t new_result_length
= (cursor
- client
->result
);
3270 if (new_result_length
!= client
->result_length
) {
3271 client
->result_length
= new_result_length
;
3275 // Update flow viability/flags
3276 if (necp_client_update_flows(proc
, client
, defunct_list
)) {
3281 client
->result_read
= FALSE
;
3282 necp_client_update_observer_update(client
);
3285 FREE(parsed_parameters
, M_NECP
);
3290 necp_defunct_client_fd_locked(struct necp_fd_data
*client_fd
, struct _necp_flow_defunct_list
*defunct_list
, struct proc
*proc
)
3292 #pragma unused(proc)
3293 bool updated_result
= FALSE
;
3294 struct necp_client
*client
= NULL
;
3296 NECP_FD_ASSERT_LOCKED(client_fd
);
3298 RB_FOREACH(client
, _necp_client_tree
, &client_fd
->clients
) {
3299 struct necp_client_flow_registration
*flow_registration
= NULL
;
3301 NECP_CLIENT_LOCK(client
);
3303 // Prepare close events to be sent to the nexus to effectively remove the flows
3304 struct necp_client_flow
*search_flow
= NULL
;
3305 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
3306 LIST_FOREACH(search_flow
, &flow_registration
->flow_list
, flow_chain
) {
3307 if (search_flow
->nexus
&&
3308 !uuid_is_null(search_flow
->u
.nexus_agent
)) {
3310 struct necp_flow_defunct
*flow_defunct
;
3312 // Sleeping alloc won't fail; copy only what's necessary
3313 flow_defunct
= _MALLOC(sizeof (struct necp_flow_defunct
), M_NECP
, M_WAITOK
| M_ZERO
);
3314 uuid_copy(flow_defunct
->nexus_agent
, search_flow
->u
.nexus_agent
);
3315 uuid_copy(flow_defunct
->flow_id
, ((flow_registration
->flags
& NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID
) ?
3317 flow_registration
->registration_id
));
3318 flow_defunct
->proc_pid
= client
->proc_pid
;
3319 flow_defunct
->agent_handle
= client
->agent_handle
;
3321 // Add to the list provided by caller
3322 LIST_INSERT_HEAD(defunct_list
, flow_defunct
, chain
);
3324 flow_registration
->defunct
= true;
3325 flow_registration
->flow_result_read
= false;
3326 updated_result
= true;
3330 NECP_CLIENT_UNLOCK(client
);
3334 if (updated_result
) {
3335 necp_fd_notify(client_fd
, true);
3340 necp_update_client_fd_locked(struct necp_fd_data
*client_fd
,
3342 struct _necp_flow_defunct_list
*defunct_list
)
3344 struct necp_client
*client
= NULL
;
3345 bool updated_result
= FALSE
;
3346 NECP_FD_ASSERT_LOCKED(client_fd
);
3347 RB_FOREACH(client
, _necp_client_tree
, &client_fd
->clients
) {
3348 NECP_CLIENT_LOCK(client
);
3349 if (necp_update_client_result(proc
, client_fd
, client
, defunct_list
)) {
3350 updated_result
= TRUE
;
3352 NECP_CLIENT_UNLOCK(client
);
3354 if (updated_result
) {
3355 necp_fd_notify(client_fd
, true);
3361 necp_update_all_clients_callout(__unused thread_call_param_t dummy
,
3362 __unused thread_call_param_t arg
)
3364 struct necp_fd_data
*client_fd
= NULL
;
3366 struct _necp_flow_defunct_list defunct_list
;
3367 LIST_INIT(&defunct_list
);
3369 NECP_FD_LIST_LOCK_SHARED();
3371 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
3372 proc_t proc
= proc_find(client_fd
->proc_pid
);
3373 if (proc
== PROC_NULL
) {
3377 // Update all clients on one fd
3378 NECP_FD_LOCK(client_fd
);
3379 necp_update_client_fd_locked(client_fd
, proc
, &defunct_list
);
3380 NECP_FD_UNLOCK(client_fd
);
3386 NECP_FD_LIST_UNLOCK();
3388 // Handle the case in which some clients became newly defunct
3389 if (!LIST_EMPTY(&defunct_list
)) {
3390 struct necp_flow_defunct
*flow_defunct
= NULL
;
3391 struct necp_flow_defunct
*temp_flow_defunct
= NULL
;
3393 // For each newly defunct client, send a message to the nexus to remove the flow
3394 LIST_FOREACH_SAFE(flow_defunct
, &defunct_list
, chain
, temp_flow_defunct
) {
3395 if (!uuid_is_null(flow_defunct
->nexus_agent
)) {
3396 int netagent_error
= netagent_client_message(flow_defunct
->nexus_agent
,
3397 flow_defunct
->flow_id
,
3398 flow_defunct
->proc_pid
,
3399 flow_defunct
->agent_handle
,
3400 NETAGENT_MESSAGE_TYPE_ABORT_NEXUS
);
3401 if (netagent_error
!= 0) {
3402 char namebuf
[MAXCOMLEN
+1];
3403 (void) strlcpy(namebuf
, "unknown", sizeof (namebuf
));
3404 proc_name(flow_defunct
->proc_pid
, namebuf
, sizeof (namebuf
));
3405 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
);
3408 LIST_REMOVE(flow_defunct
, chain
);
3409 FREE(flow_defunct
, M_NECP
);
3412 ASSERT(LIST_EMPTY(&defunct_list
));
3416 necp_update_all_clients(void)
3418 if (necp_client_update_tcall
== NULL
) {
3419 // Don't try to update clients if the module is not initialized
3423 uint64_t deadline
= 0;
3424 uint64_t leeway
= 0;
3425 clock_interval_to_deadline(necp_timeout_microseconds
, NSEC_PER_USEC
, &deadline
);
3426 clock_interval_to_absolutetime_interval(necp_timeout_leeway_microseconds
, NSEC_PER_USEC
, &leeway
);
3428 thread_call_enter_delayed_with_leeway(necp_client_update_tcall
, NULL
,
3429 deadline
, leeway
, THREAD_CALL_DELAY_LEEWAY
);
3433 necp_set_client_as_background(proc_t proc
,
3434 struct fileproc
*fp
,
3437 bool updated_result
= FALSE
;
3438 struct necp_client
*client
= NULL
;
3440 if (proc
== PROC_NULL
) {
3441 NECPLOG0(LOG_ERR
, "NULL proc");
3446 NECPLOG0(LOG_ERR
, "NULL fp");
3450 struct necp_fd_data
*client_fd
= (struct necp_fd_data
*)fp
->f_fglob
->fg_data
;
3451 if (client_fd
== NULL
) {
3452 NECPLOG0(LOG_ERR
, "Could not find client structure for backgrounded client");
3456 if (client_fd
->necp_fd_type
!= necp_fd_type_client
) {
3457 // Not a client fd, ignore
3458 NECPLOG0(LOG_ERR
, "Not a client fd, ignore");
3462 NECP_FD_LOCK(client_fd
);
3464 RB_FOREACH(client
, _necp_client_tree
, &client_fd
->clients
) {
3465 NECP_CLIENT_LOCK(client
);
3467 bool has_assigned_flow
= FALSE
;
3468 struct necp_client_flow_registration
*flow_registration
= NULL
;
3469 struct necp_client_flow
*search_flow
= NULL
;
3470 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
3471 LIST_FOREACH(search_flow
, &flow_registration
->flow_list
, flow_chain
) {
3472 if (search_flow
->assigned
) {
3473 has_assigned_flow
= TRUE
;
3479 if (has_assigned_flow
) {
3480 client
->background
= background
;
3481 client
->background_update
= TRUE
;
3482 updated_result
= TRUE
;
3485 NECP_CLIENT_UNLOCK(client
);
3487 if (updated_result
) {
3488 necp_update_client_fd_locked(client_fd
, proc
, NULL
);
3490 NECP_FD_UNLOCK(client_fd
);
3494 necp_fd_memstatus(proc_t proc
, uint32_t status
,
3495 struct necp_fd_data
*client_fd
)
3497 #pragma unused(proc, status, client_fd)
3498 ASSERT(proc
!= PROC_NULL
);
3499 ASSERT(client_fd
!= NULL
);
3501 // Nothing to reap for the process or client for now,
3502 // but this is where we would trigger that in future.
3506 necp_fd_defunct(proc_t proc
, struct necp_fd_data
*client_fd
)
3508 struct _necp_flow_defunct_list defunct_list
;
3510 ASSERT(proc
!= PROC_NULL
);
3511 ASSERT(client_fd
!= NULL
);
3513 if (client_fd
->necp_fd_type
!= necp_fd_type_client
) {
3514 // Not a client fd, ignore
3518 // Our local temporary list
3519 LIST_INIT(&defunct_list
);
3521 // Need to hold lock so ntstats defunct the same set of clients
3522 NECP_FD_LOCK(client_fd
);
3523 necp_defunct_client_fd_locked(client_fd
, &defunct_list
, proc
);
3524 NECP_FD_UNLOCK(client_fd
);
3526 if (!LIST_EMPTY(&defunct_list
)) {
3527 struct necp_flow_defunct
*flow_defunct
= NULL
;
3528 struct necp_flow_defunct
*temp_flow_defunct
= NULL
;
3530 // For each defunct client, remove flow from the nexus
3531 LIST_FOREACH_SAFE(flow_defunct
, &defunct_list
, chain
, temp_flow_defunct
) {
3532 if (!uuid_is_null(flow_defunct
->nexus_agent
)) {
3533 int netagent_error
= netagent_client_message(flow_defunct
->nexus_agent
,
3534 flow_defunct
->flow_id
,
3535 flow_defunct
->proc_pid
,
3536 flow_defunct
->agent_handle
,
3537 NETAGENT_MESSAGE_TYPE_ABORT_NEXUS
);
3538 if (netagent_error
!= 0) {
3539 NECPLOG((netagent_error
== ENOENT
? LOG_DEBUG
: LOG_ERR
), "necp_defunct_client abort nexus error (%d)", netagent_error
);
3542 LIST_REMOVE(flow_defunct
, chain
);
3543 FREE(flow_defunct
, M_NECP
);
3546 ASSERT(LIST_EMPTY(&defunct_list
));
3550 necp_client_remove_agent_from_result(struct necp_client
*client
, uuid_t netagent_uuid
)
3554 u_int8_t
*result_buffer
= client
->result
;
3555 while ((offset
+ sizeof(struct necp_tlv_header
)) <= client
->result_length
) {
3556 u_int8_t type
= necp_buffer_get_tlv_type(result_buffer
, offset
);
3557 u_int32_t length
= necp_buffer_get_tlv_length(result_buffer
, offset
);
3559 size_t tlv_total_length
= (sizeof(struct necp_tlv_header
) + length
);
3560 if (type
== NECP_CLIENT_RESULT_NETAGENT
&&
3561 length
== sizeof(struct necp_client_result_netagent
) &&
3562 (offset
+ tlv_total_length
) <= client
->result_length
) {
3563 struct necp_client_result_netagent
*value
= ((struct necp_client_result_netagent
*)(void *)
3564 necp_buffer_get_tlv_value(result_buffer
, offset
, NULL
));
3565 if (uuid_compare(value
->netagent_uuid
, netagent_uuid
) == 0) {
3566 // Found a netagent to remove
3567 // Shift bytes down to remove the tlv, and adjust total length
3568 // Don't adjust the current offset
3569 memmove(result_buffer
+ offset
,
3570 result_buffer
+ offset
+ tlv_total_length
,
3571 client
->result_length
- (offset
+ tlv_total_length
));
3572 client
->result_length
-= tlv_total_length
;
3573 memset(result_buffer
+ client
->result_length
, 0, sizeof(client
->result
) - client
->result_length
);
3578 offset
+= tlv_total_length
;
3583 necp_force_update_client(uuid_t client_id
, uuid_t remove_netagent_uuid
, u_int32_t agent_generation
)
3585 struct necp_fd_data
*client_fd
= NULL
;
3587 NECP_FD_LIST_LOCK_SHARED();
3589 LIST_FOREACH(client_fd
, &necp_fd_list
, chain
) {
3590 bool updated_result
= FALSE
;
3591 NECP_FD_LOCK(client_fd
);
3592 struct necp_client
*client
= necp_client_fd_find_client_and_lock(client_fd
, client_id
);
3593 if (client
!= NULL
) {
3594 client
->failed_trigger_agent
.generation
= agent_generation
;
3595 uuid_copy(client
->failed_trigger_agent
.netagent_uuid
, remove_netagent_uuid
);
3596 if (!uuid_is_null(remove_netagent_uuid
)) {
3597 necp_client_remove_agent_from_result(client
, remove_netagent_uuid
);
3599 client
->result_read
= FALSE
;
3600 // Found the client, break
3601 updated_result
= TRUE
;
3602 NECP_CLIENT_UNLOCK(client
);
3604 if (updated_result
) {
3605 necp_fd_notify(client_fd
, true);
3607 NECP_FD_UNLOCK(client_fd
);
3608 if (updated_result
) {
3609 // Found the client, break
3614 NECP_FD_LIST_UNLOCK();
3618 /// Interface matching
3620 #define NECP_PARSED_PARAMETERS_INTERESTING_IFNET_FIELDS (NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR | \
3621 NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IF | \
3622 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE | \
3623 NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IFTYPE | \
3624 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT | \
3625 NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT | \
3626 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT | \
3627 NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT | \
3628 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE | \
3629 NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT_TYPE | \
3630 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE | \
3631 NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE)
3633 #define NECP_PARSED_PARAMETERS_SCOPED_FIELDS (NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR | \
3634 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE | \
3635 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT | \
3636 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT | \
3637 NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT | \
3638 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE | \
3639 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE | \
3640 NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE)
3642 #define NECP_PARSED_PARAMETERS_SCOPED_IFNET_FIELDS (NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR | \
3643 NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE)
3645 #define NECP_PARSED_PARAMETERS_PREFERRED_FIELDS (NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT | \
3646 NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT | \
3647 NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE | \
3648 NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE)
3651 necp_ifnet_matches_type(struct ifnet
*ifp
, u_int8_t interface_type
, bool check_delegates
)
3653 struct ifnet
*check_ifp
= ifp
;
3655 if (if_functional_type(check_ifp
, TRUE
) == interface_type
) {
3658 if (!check_delegates
) {
3661 check_ifp
= check_ifp
->if_delegated
.ifp
;
3668 necp_ifnet_matches_name(struct ifnet
*ifp
, const char *interface_name
, bool check_delegates
)
3670 struct ifnet
*check_ifp
= ifp
;
3672 if (strncmp(check_ifp
->if_xname
, interface_name
, IFXNAMSIZ
) == 0) {
3675 if (!check_delegates
) {
3678 check_ifp
= check_ifp
->if_delegated
.ifp
;
3684 necp_ifnet_matches_agent(struct ifnet
*ifp
, uuid_t
*agent_uuid
, bool check_delegates
)
3686 struct ifnet
*check_ifp
= ifp
;
3688 while (check_ifp
!= NULL
) {
3689 ifnet_lock_shared(check_ifp
);
3690 if (check_ifp
->if_agentids
!= NULL
) {
3691 for (u_int32_t index
= 0; index
< check_ifp
->if_agentcount
; index
++) {
3692 if (uuid_compare(check_ifp
->if_agentids
[index
], *agent_uuid
) == 0) {
3693 ifnet_lock_done(check_ifp
);
3698 ifnet_lock_done(check_ifp
);
3700 if (!check_delegates
) {
3703 check_ifp
= check_ifp
->if_delegated
.ifp
;
3709 necp_ifnet_matches_agent_type(struct ifnet
*ifp
, const char *agent_domain
, const char *agent_type
, bool check_delegates
)
3711 struct ifnet
*check_ifp
= ifp
;
3713 while (check_ifp
!= NULL
) {
3714 ifnet_lock_shared(check_ifp
);
3715 if (check_ifp
->if_agentids
!= NULL
) {
3716 for (u_int32_t index
= 0; index
< check_ifp
->if_agentcount
; index
++) {
3717 if (uuid_is_null(check_ifp
->if_agentids
[index
])) {
3721 char if_agent_domain
[NETAGENT_DOMAINSIZE
] = { 0 };
3722 char if_agent_type
[NETAGENT_TYPESIZE
] = { 0 };
3724 if (netagent_get_agent_domain_and_type(check_ifp
->if_agentids
[index
], if_agent_domain
, if_agent_type
)) {
3725 if (necp_agent_types_match(agent_domain
, agent_type
, if_agent_domain
, if_agent_type
)) {
3726 ifnet_lock_done(check_ifp
);
3732 ifnet_lock_done(check_ifp
);
3734 if (!check_delegates
) {
3737 check_ifp
= check_ifp
->if_delegated
.ifp
;
3743 necp_ifnet_matches_local_address(struct ifnet
*ifp
, struct sockaddr
*sa
)
3745 struct ifaddr
*ifa
= NULL
;
3746 bool matched_local_address
= FALSE
;
3748 // Transform sa into the ifaddr form
3749 // IPv6 Scope IDs are always embedded in the ifaddr list
3750 struct sockaddr_storage address
;
3751 u_int ifscope
= IFSCOPE_NONE
;
3752 (void)sa_copy(sa
, &address
, &ifscope
);
3753 SIN(&address
)->sin_port
= 0;
3754 if (address
.ss_family
== AF_INET6
) {
3755 SIN6(&address
)->sin6_scope_id
= 0;
3758 ifa
= ifa_ifwithaddr_scoped_locked((struct sockaddr
*)&address
, ifp
->if_index
);
3759 matched_local_address
= (ifa
!= NULL
);
3762 ifaddr_release(ifa
);
3765 return (matched_local_address
);
3769 necp_interface_type_is_primary_eligible(u_int8_t interface_type
)
3771 switch (interface_type
) {
3772 // These types can never be primary, so a client requesting these types is allowed
3773 // to match an interface that isn't currently eligible to be primary (has default
3775 case IFRTYPE_FUNCTIONAL_WIFI_AWDL
:
3776 case IFRTYPE_FUNCTIONAL_INTCOPROC
:
3784 #define NECP_IFP_IS_ON_ORDERED_LIST(_ifp) ((_ifp)->if_ordered_link.tqe_next != NULL || (_ifp)->if_ordered_link.tqe_prev != NULL)
3786 // Secondary interface flag indicates that the interface is being
3787 // used for multipath or a listener as an extra path
3789 necp_ifnet_matches_parameters(struct ifnet
*ifp
,
3790 struct necp_client_parsed_parameters
*parsed_parameters
,
3791 u_int32_t
*preferred_count
,
3792 bool secondary_interface
)
3794 if (preferred_count
) {
3795 *preferred_count
= 0;
3798 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_LOCAL_ADDR
) {
3799 if (!necp_ifnet_matches_local_address(ifp
, &parsed_parameters
->local_addr
.sa
)) {
3804 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_FLAGS
) {
3805 if ((parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_EXPENSIVE
) &&
3806 IFNET_IS_EXPENSIVE(ifp
)) {
3811 if ((!secondary_interface
|| // Enforce interface type if this is the primary interface
3812 !(parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_FLAGS
) || // or if there are no flags
3813 !(parsed_parameters
->flags
& NECP_CLIENT_PARAMETER_FLAG_ONLY_PRIMARY_REQUIRES_TYPE
)) && // or if the flags don't give an exception
3814 (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE
) &&
3815 !necp_ifnet_matches_type(ifp
, parsed_parameters
->required_interface_type
, FALSE
)) {
3819 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IFTYPE
) {
3820 for (int i
= 0; i
< NECP_MAX_PARSED_PARAMETERS
; i
++) {
3821 if (parsed_parameters
->prohibited_interface_types
[i
] == 0) {
3825 if (necp_ifnet_matches_type(ifp
, parsed_parameters
->prohibited_interface_types
[i
], TRUE
)) {
3831 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_IF
) {
3832 for (int i
= 0; i
< NECP_MAX_PARSED_PARAMETERS
; i
++) {
3833 if (strlen(parsed_parameters
->prohibited_interfaces
[i
]) == 0) {
3837 if (necp_ifnet_matches_name(ifp
, parsed_parameters
->prohibited_interfaces
[i
], TRUE
)) {
3843 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT
) {
3844 for (int i
= 0; i
< NECP_MAX_PARSED_PARAMETERS
; i
++) {
3845 if (uuid_is_null(parsed_parameters
->required_netagents
[i
])) {
3849 if (!necp_ifnet_matches_agent(ifp
, &parsed_parameters
->required_netagents
[i
], FALSE
)) {
3855 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT
) {
3856 for (int i
= 0; i
< NECP_MAX_PARSED_PARAMETERS
; i
++) {
3857 if (uuid_is_null(parsed_parameters
->prohibited_netagents
[i
])) {
3861 if (necp_ifnet_matches_agent(ifp
, &parsed_parameters
->prohibited_netagents
[i
], TRUE
)) {
3867 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_AGENT_TYPE
) {
3868 for (int i
= 0; i
< NECP_MAX_PARSED_PARAMETERS
; i
++) {
3869 if (strlen(parsed_parameters
->required_netagent_types
[i
].netagent_domain
) == 0 &&
3870 strlen(parsed_parameters
->required_netagent_types
[i
].netagent_type
) == 0) {
3874 if (!necp_ifnet_matches_agent_type(ifp
, parsed_parameters
->required_netagent_types
[i
].netagent_domain
, parsed_parameters
->required_netagent_types
[i
].netagent_type
, FALSE
)) {
3880 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_PROHIBITED_AGENT_TYPE
) {
3881 for (int i
= 0; i
< NECP_MAX_PARSED_PARAMETERS
; i
++) {
3882 if (strlen(parsed_parameters
->prohibited_netagent_types
[i
].netagent_domain
) == 0 &&
3883 strlen(parsed_parameters
->prohibited_netagent_types
[i
].netagent_type
) == 0) {
3887 if (necp_ifnet_matches_agent_type(ifp
, parsed_parameters
->prohibited_netagent_types
[i
].netagent_domain
, parsed_parameters
->prohibited_netagent_types
[i
].netagent_type
, TRUE
)) {
3893 // Checked preferred properties
3894 if (preferred_count
) {
3895 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT
) {
3896 for (int i
= 0; i
< NECP_MAX_PARSED_PARAMETERS
; i
++) {
3897 if (uuid_is_null(parsed_parameters
->preferred_netagents
[i
])) {
3901 if (necp_ifnet_matches_agent(ifp
, &parsed_parameters
->preferred_netagents
[i
], TRUE
)) {
3902 (*preferred_count
)++;
3907 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_PREFERRED_AGENT_TYPE
) {
3908 for (int i
= 0; i
< NECP_MAX_PARSED_PARAMETERS
; i
++) {
3909 if (strlen(parsed_parameters
->preferred_netagent_types
[i
].netagent_domain
) == 0 &&
3910 strlen(parsed_parameters
->preferred_netagent_types
[i
].netagent_type
) == 0) {
3914 if (necp_ifnet_matches_agent_type(ifp
, parsed_parameters
->preferred_netagent_types
[i
].netagent_domain
, parsed_parameters
->preferred_netagent_types
[i
].netagent_type
, TRUE
)) {
3915 (*preferred_count
)++;
3920 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT
) {
3921 for (int i
= 0; i
< NECP_MAX_PARSED_PARAMETERS
; i
++) {
3922 if (uuid_is_null(parsed_parameters
->avoided_netagents
[i
])) {
3926 if (!necp_ifnet_matches_agent(ifp
, &parsed_parameters
->avoided_netagents
[i
], TRUE
)) {
3927 (*preferred_count
)++;
3932 if (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_AVOIDED_AGENT_TYPE
) {
3933 for (int i
= 0; i
< NECP_MAX_PARSED_PARAMETERS
; i
++) {
3934 if (strlen(parsed_parameters
->avoided_netagent_types
[i
].netagent_domain
) == 0 &&
3935 strlen(parsed_parameters
->avoided_netagent_types
[i
].netagent_type
) == 0) {
3939 if (!necp_ifnet_matches_agent_type(ifp
, parsed_parameters
->avoided_netagent_types
[i
].netagent_domain
,
3940 parsed_parameters
->avoided_netagent_types
[i
].netagent_type
, TRUE
)) {
3941 (*preferred_count
)++;
3951 necp_find_matching_interface_index(struct necp_client_parsed_parameters
*parsed_parameters
,
3952 u_int
*return_ifindex
, bool *validate_agents
)
3954 struct ifnet
*ifp
= NULL
;
3955 u_int32_t best_preferred_count
= 0;
3956 bool has_preferred_fields
= FALSE
;
3957 *return_ifindex
= 0;
3959 if (parsed_parameters
->required_interface_index
!= 0) {
3960 *return_ifindex
= parsed_parameters
->required_interface_index
;
3964 if (!(parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_INTERESTING_IFNET_FIELDS
)) {
3968 has_preferred_fields
= (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_PREFERRED_FIELDS
);
3970 // We have interesting parameters to parse and find a matching interface
3971 ifnet_head_lock_shared();
3973 if (!(parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_SCOPED_FIELDS
)) {
3974 // We do have fields to match, but they are only prohibitory
3975 // If the first interface in the list matches, or there are no ordered interfaces, we don't need to scope
3976 ifp
= TAILQ_FIRST(&ifnet_ordered_head
);
3977 if (ifp
== NULL
|| necp_ifnet_matches_parameters(ifp
, parsed_parameters
, NULL
, false)) {
3978 // Don't set return_ifindex, so the client doesn't need to scope
3984 // First check the ordered interface list
3985 TAILQ_FOREACH(ifp
, &ifnet_ordered_head
, if_ordered_link
) {
3986 u_int32_t preferred_count
= 0;
3987 if (necp_ifnet_matches_parameters(ifp
, parsed_parameters
, &preferred_count
, false)) {
3988 if (preferred_count
> best_preferred_count
||
3989 *return_ifindex
== 0) {
3991 // Everything matched, and is most preferred. Return this interface.
3992 *return_ifindex
= ifp
->if_index
;
3993 best_preferred_count
= preferred_count
;
3995 if (!has_preferred_fields
) {
4002 // Then check the remaining interfaces
4003 if ((parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_SCOPED_FIELDS
) &&
4004 ((!(parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_FIELD_REQUIRED_IFTYPE
)) ||
4005 !necp_interface_type_is_primary_eligible(parsed_parameters
->required_interface_type
)) &&
4006 *return_ifindex
== 0) {
4007 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
) {
4008 u_int32_t preferred_count
= 0;
4009 if (NECP_IFP_IS_ON_ORDERED_LIST(ifp
)) {
4010 // This interface was in the ordered list, skip
4013 if (necp_ifnet_matches_parameters(ifp
, parsed_parameters
, &preferred_count
, false)) {
4014 if (preferred_count
> best_preferred_count
||
4015 *return_ifindex
== 0) {
4017 // Everything matched, and is most preferred. Return this interface.
4018 *return_ifindex
= ifp
->if_index
;
4019 best_preferred_count
= preferred_count
;
4021 if (!has_preferred_fields
) {
4031 if ((parsed_parameters
->valid_fields
== (parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_PREFERRED_FIELDS
)) &&
4032 best_preferred_count
== 0) {
4033 // If only has preferred fields, and nothing was found, clear the interface index and return TRUE
4034 *return_ifindex
= 0;
4038 if (*return_ifindex
== 0 &&
4039 !(parsed_parameters
->valid_fields
& NECP_PARSED_PARAMETERS_SCOPED_IFNET_FIELDS
)) {
4040 // Has required fields, but not including specific interface fields. Pass for now, and check
4041 // to see if agents are satisfied by policy.
4042 *validate_agents
= TRUE
;
4046 return (*return_ifindex
!= 0);
4051 necp_skywalk_priv_check_cred(proc_t p
, kauth_cred_t cred
)
4053 #pragma unused(p, cred)
4060 necp_open(struct proc
*p
, struct necp_open_args
*uap
, int *retval
)
4062 #pragma unused(retval)
4064 struct necp_fd_data
*fd_data
= NULL
;
4065 struct fileproc
*fp
= NULL
;
4068 if (uap
->flags
& NECP_OPEN_FLAG_OBSERVER
||
4069 uap
->flags
& NECP_OPEN_FLAG_PUSH_OBSERVER
) {
4070 if (necp_skywalk_priv_check_cred(p
, kauth_cred_get()) != 0 &&
4071 priv_check_cred(kauth_cred_get(), PRIV_NET_PRIVILEGED_NETWORK_STATISTICS
, 0) != 0) {
4072 NECPLOG0(LOG_ERR
, "Client does not hold necessary entitlement to observe other NECP clients");
4078 error
= falloc(p
, &fp
, &fd
, vfs_context_current());
4083 if ((fd_data
= zalloc(necp_client_fd_zone
)) == NULL
) {
4088 memset(fd_data
, 0, sizeof(*fd_data
));
4090 fd_data
->necp_fd_type
= necp_fd_type_client
;
4091 fd_data
->flags
= uap
->flags
;
4092 RB_INIT(&fd_data
->clients
);
4093 RB_INIT(&fd_data
->flows
);
4094 TAILQ_INIT(&fd_data
->update_list
);
4095 lck_mtx_init(&fd_data
->fd_lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
4096 klist_init(&fd_data
->si
.si_note
);
4097 fd_data
->proc_pid
= proc_pid(p
);
4099 fp
->f_fglob
->fg_flag
= FREAD
;
4100 fp
->f_fglob
->fg_ops
= &necp_fd_ops
;
4101 fp
->f_fglob
->fg_data
= fd_data
;
4105 *fdflags(p
, fd
) |= (UF_EXCLOSE
| UF_FORKCLOSE
);
4106 procfdtbl_releasefd(p
, fd
, NULL
);
4107 fp_drop(p
, fd
, fp
, 1);
4111 if (fd_data
->flags
& NECP_OPEN_FLAG_PUSH_OBSERVER
) {
4112 NECP_OBSERVER_LIST_LOCK_EXCLUSIVE();
4113 LIST_INSERT_HEAD(&necp_fd_observer_list
, fd_data
, chain
);
4114 OSIncrementAtomic(&necp_observer_fd_count
);
4115 NECP_OBSERVER_LIST_UNLOCK();
4117 // Walk all existing clients and add them
4118 NECP_CLIENT_TREE_LOCK_SHARED();
4119 struct necp_client
*existing_client
= NULL
;
4120 RB_FOREACH(existing_client
, _necp_client_global_tree
, &necp_client_global_tree
) {
4121 NECP_CLIENT_LOCK(existing_client
);
4122 necp_client_update_observer_add_internal(fd_data
, existing_client
);
4123 necp_client_update_observer_update_internal(fd_data
, existing_client
);
4124 NECP_CLIENT_UNLOCK(existing_client
);
4126 NECP_CLIENT_TREE_UNLOCK();
4128 NECP_FD_LIST_LOCK_EXCLUSIVE();
4129 LIST_INSERT_HEAD(&necp_fd_list
, fd_data
, chain
);
4130 OSIncrementAtomic(&necp_client_fd_count
);
4131 NECP_FD_LIST_UNLOCK();
4142 if (fd_data
!= NULL
) {
4143 zfree(necp_client_fd_zone
, fd_data
);
4152 necp_client_add(struct proc
*p
, struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
4155 struct necp_client
*client
= NULL
;
4157 if (fd_data
->flags
& NECP_OPEN_FLAG_PUSH_OBSERVER
) {
4158 NECPLOG0(LOG_ERR
, "NECP client observers with push enabled may not add their own clients");
4162 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
) ||
4163 uap
->buffer_size
== 0 || uap
->buffer_size
> NECP_MAX_CLIENT_PARAMETERS_SIZE
|| uap
->buffer
== 0) {
4167 if ((client
= _MALLOC(sizeof(struct necp_client
) + uap
->buffer_size
, M_NECP
,
4168 M_WAITOK
| M_ZERO
)) == NULL
) {
4173 error
= copyin(uap
->buffer
, client
->parameters
, uap
->buffer_size
);
4175 NECPLOG(LOG_ERR
, "necp_client_add parameters copyin error (%d)", error
);
4179 lck_mtx_init(&client
->lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
4180 lck_mtx_init(&client
->route_lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
4181 necp_client_retain(client
); // Hold our reference until close
4183 client
->parameters_length
= uap
->buffer_size
;
4184 client
->proc_pid
= fd_data
->proc_pid
; // Save off proc pid in case the client will persist past fd
4185 client
->agent_handle
= (void *)fd_data
;
4186 client
->platform_binary
= ((csproc_get_platform_binary(p
) == 0) ? 0 : 1);
4188 necp_generate_client_id(client
->client_id
, false);
4189 LIST_INIT(&client
->assertion_list
);
4190 RB_INIT(&client
->flow_registrations
);
4192 error
= copyout(client
->client_id
, uap
->client_id
, sizeof(uuid_t
));
4194 NECPLOG(LOG_ERR
, "necp_client_add client_id copyout error (%d)", error
);
4198 necp_client_update_observer_add(client
);
4200 NECP_FD_LOCK(fd_data
);
4201 RB_INSERT(_necp_client_tree
, &fd_data
->clients
, client
);
4202 OSIncrementAtomic(&necp_client_count
);
4203 NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
4204 RB_INSERT(_necp_client_global_tree
, &necp_client_global_tree
, client
);
4205 NECP_CLIENT_TREE_UNLOCK();
4207 // Prime the client result
4208 NECP_CLIENT_LOCK(client
);
4209 (void)necp_update_client_result(current_proc(), fd_data
, client
, NULL
);
4210 NECP_CLIENT_UNLOCK(client
);
4211 NECP_FD_UNLOCK(fd_data
);
4214 if (client
!= NULL
) {
4215 FREE(client
, M_NECP
);
4225 necp_client_remove(struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
4228 uuid_t client_id
= {};
4229 struct ifnet_stats_per_flow flow_ifnet_stats
= {};
4231 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
)) {
4236 error
= copyin(uap
->client_id
, client_id
, sizeof(uuid_t
));
4238 NECPLOG(LOG_ERR
, "necp_client_remove copyin client_id error (%d)", error
);
4242 if (uap
->buffer
!= 0 && uap
->buffer_size
== sizeof(flow_ifnet_stats
)) {
4243 error
= copyin(uap
->buffer
, &flow_ifnet_stats
, uap
->buffer_size
);
4245 NECPLOG(LOG_ERR
, "necp_client_remove flow_ifnet_stats copyin error (%d)", error
);
4246 // Not fatal; make sure to zero-out stats in case of partial copy
4247 memset(&flow_ifnet_stats
, 0, sizeof (flow_ifnet_stats
));
4250 } else if (uap
->buffer
!= 0) {
4251 NECPLOG(LOG_ERR
, "necp_client_remove unexpected parameters length (%zu)", uap
->buffer_size
);
4254 NECP_FD_LOCK(fd_data
);
4256 pid_t pid
= fd_data
->proc_pid
;
4257 struct necp_client
*client
= necp_client_fd_find_client_unlocked(fd_data
, client_id
);
4258 if (client
!= NULL
) {
4259 // Remove any flow registrations that match
4260 struct necp_client_flow_registration
*flow_registration
= NULL
;
4261 struct necp_client_flow_registration
*temp_flow_registration
= NULL
;
4262 RB_FOREACH_SAFE(flow_registration
, _necp_fd_flow_tree
, &fd_data
->flows
, temp_flow_registration
) {
4263 if (flow_registration
->client
== client
) {
4264 NECP_FLOW_TREE_LOCK_EXCLUSIVE();
4265 RB_REMOVE(_necp_client_flow_global_tree
, &necp_client_flow_global_tree
, flow_registration
);
4266 NECP_FLOW_TREE_UNLOCK();
4267 RB_REMOVE(_necp_fd_flow_tree
, &fd_data
->flows
, flow_registration
);
4270 // Remove client from lists
4271 NECP_CLIENT_TREE_LOCK_EXCLUSIVE();
4272 RB_REMOVE(_necp_client_global_tree
, &necp_client_global_tree
, client
);
4273 NECP_CLIENT_TREE_UNLOCK();
4274 RB_REMOVE(_necp_client_tree
, &fd_data
->clients
, client
);
4278 NECP_FD_UNLOCK(fd_data
);
4280 if (client
!= NULL
) {
4282 necp_destroy_client(client
, pid
, true);
4285 NECPLOG(LOG_ERR
, "necp_client_remove invalid client_id (%d)", error
);
4295 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
)
4297 struct necp_client_parsed_parameters parsed_parameters
;
4300 error
= necp_client_parse_parameters(client
->parameters
,
4301 (u_int32_t
)client
->parameters_length
,
4302 &parsed_parameters
);
4304 NECPLOG(LOG_ERR
, "necp_client_parse_parameters error (%d)", error
);
4308 if ((flow
->remote_addr
.sa
.sa_family
!= AF_INET
&&
4309 flow
->remote_addr
.sa
.sa_family
!= AF_INET6
) ||
4310 (flow
->local_addr
.sa
.sa_family
!= AF_INET
&&
4311 flow
->local_addr
.sa
.sa_family
!= AF_INET6
)) {
4315 NECP_CLIENT_ROUTE_LOCK(client
);
4317 if (client
->current_route
== NULL
) {
4322 bool check_ecn
= false;
4324 if ((parsed_parameters
.flags
& NECP_CLIENT_PARAMETER_FLAG_ECN_ENABLE
) ==
4325 NECP_CLIENT_PARAMETER_FLAG_ECN_ENABLE
) {
4330 if ((parsed_parameters
.flags
& NECP_CLIENT_PARAMETER_FLAG_ECN_DISABLE
) ==
4331 NECP_CLIENT_PARAMETER_FLAG_ECN_DISABLE
) {
4335 if (client
->current_route
!= NULL
) {
4336 if (client
->current_route
->rt_ifp
->if_eflags
& IFEF_ECN_ENABLE
) {
4340 if (client
->current_route
->rt_ifp
->if_eflags
& IFEF_ECN_DISABLE
) {
4345 bool inbound
= ((parsed_parameters
.flags
& NECP_CLIENT_PARAMETER_FLAG_LISTENER
) == 0);
4346 if ((inbound
&& tcp_ecn_inbound
== 1) ||
4347 (!inbound
&& tcp_ecn_outbound
== 1)) {
4353 if (tcp_heuristic_do_ecn_with_address(client
->current_route
->rt_ifp
,
4354 (union sockaddr_in_4_6
*)&flow
->local_addr
)) {
4355 *flags
|= NECP_CLIENT_RESULT_FLAG_ECN_ENABLED
;
4359 if ((parsed_parameters
.flags
& NECP_CLIENT_PARAMETER_FLAG_TFO_ENABLE
) ==
4360 NECP_CLIENT_PARAMETER_FLAG_TFO_ENABLE
) {
4362 if (!tcp_heuristic_do_tfo_with_address(client
->current_route
->rt_ifp
,
4363 (union sockaddr_in_4_6
*)&flow
->local_addr
,
4364 (union sockaddr_in_4_6
*)&flow
->remote_addr
,
4365 tfo_cookie
, tfo_cookie_len
)) {
4366 *flags
|= NECP_CLIENT_RESULT_FLAG_FAST_OPEN_BLOCKED
;
4367 *tfo_cookie_len
= 0;
4370 *flags
|= NECP_CLIENT_RESULT_FLAG_FAST_OPEN_BLOCKED
;
4371 *tfo_cookie_len
= 0;
4374 NECP_CLIENT_ROUTE_UNLOCK(client
);
4380 necp_client_calculate_flow_tlv_size(struct necp_client_flow_registration
*flow_registration
)
4382 size_t assigned_results_size
= 0;
4383 struct necp_client_flow
*flow
= NULL
;
4384 LIST_FOREACH(flow
, &flow_registration
->flow_list
, flow_chain
) {
4385 if (flow
->assigned
) {
4386 size_t header_length
= 0;
4388 header_length
= sizeof(struct necp_client_nexus_flow_header
);
4390 header_length
= sizeof(struct necp_client_flow_header
);
4392 assigned_results_size
+= (header_length
+ flow
->assigned_results_length
);
4394 if (flow
->has_protoctl_event
) {
4395 assigned_results_size
+= sizeof(struct necp_client_flow_protoctl_event_header
);
4399 return assigned_results_size
;
4403 necp_client_fillout_flow_tlvs(struct necp_client
*client
,
4404 bool client_is_observed
,
4405 struct necp_client_flow_registration
*flow_registration
,
4406 struct necp_client_action_args
*uap
,
4407 size_t *assigned_results_cursor
)
4410 struct necp_client_flow
*flow
= NULL
;
4411 LIST_FOREACH(flow
, &flow_registration
->flow_list
, flow_chain
) {
4412 if (flow
->assigned
) {
4413 // Write TLV headers
4414 struct necp_client_nexus_flow_header header
= {};
4415 u_int32_t length
= 0;
4416 u_int32_t flags
= 0;
4417 u_int8_t tfo_cookie_len
= 0;
4420 type
= NECP_CLIENT_RESULT_FLOW_ID
;
4421 length
= sizeof(header
.flow_header
.flow_id
);
4422 memcpy(&header
.flow_header
.flow_id_tlv_header
.type
, &type
, sizeof(type
));
4423 memcpy(&header
.flow_header
.flow_id_tlv_header
.length
, &length
, sizeof(length
));
4424 uuid_copy(header
.flow_header
.flow_id
, flow_registration
->registration_id
);
4427 if (flow
->check_tcp_heuristics
) {
4428 u_int8_t tfo_cookie
[NECP_TFO_COOKIE_LEN_MAX
];
4429 tfo_cookie_len
= NECP_TFO_COOKIE_LEN_MAX
;
4431 if (necp_client_check_tcp_heuristics(client
, flow
, &flags
,
4432 tfo_cookie
, &tfo_cookie_len
) != 0) {
4435 flow
->check_tcp_heuristics
= FALSE
;
4437 if (tfo_cookie_len
!= 0) {
4438 type
= NECP_CLIENT_RESULT_TFO_COOKIE
;
4439 length
= tfo_cookie_len
;
4440 memcpy(&header
.tfo_cookie_tlv_header
.type
, &type
, sizeof(type
));
4441 memcpy(&header
.tfo_cookie_tlv_header
.length
, &length
, sizeof(length
));
4442 memcpy(&header
.tfo_cookie_value
, tfo_cookie
, tfo_cookie_len
);
4448 size_t header_length
= 0;
4450 if (tfo_cookie_len
!= 0) {
4451 header_length
= sizeof(struct necp_client_nexus_flow_header
) - (NECP_TFO_COOKIE_LEN_MAX
- tfo_cookie_len
);
4453 header_length
= sizeof(struct necp_client_nexus_flow_header
) - sizeof(struct necp_tlv_header
) - NECP_TFO_COOKIE_LEN_MAX
;
4456 header_length
= sizeof(struct necp_client_flow_header
);
4459 type
= NECP_CLIENT_RESULT_FLAGS
;
4460 length
= sizeof(header
.flow_header
.flags_value
);
4461 memcpy(&header
.flow_header
.flags_tlv_header
.type
, &type
, sizeof(type
));
4462 memcpy(&header
.flow_header
.flags_tlv_header
.length
, &length
, sizeof(length
));
4463 if (flow
->assigned
) {
4464 flags
|= NECP_CLIENT_RESULT_FLAG_FLOW_ASSIGNED
;
4467 flags
|= NECP_CLIENT_RESULT_FLAG_FLOW_VIABLE
;
4469 if (flow_registration
->defunct
) {
4470 flags
|= NECP_CLIENT_RESULT_FLAG_DEFUNCT
;
4472 flags
|= flow
->necp_flow_flags
;
4473 memcpy(&header
.flow_header
.flags_value
, &flags
, sizeof(flags
));
4475 type
= NECP_CLIENT_RESULT_INTERFACE
;
4476 length
= sizeof(header
.flow_header
.interface_value
);
4477 memcpy(&header
.flow_header
.interface_tlv_header
.type
, &type
, sizeof(type
));
4478 memcpy(&header
.flow_header
.interface_tlv_header
.length
, &length
, sizeof(length
));
4480 struct necp_client_result_interface interface_struct
;
4481 interface_struct
.generation
= 0;
4482 interface_struct
.index
= flow
->interface_index
;
4484 memcpy(&header
.flow_header
.interface_value
, &interface_struct
, sizeof(interface_struct
));
4486 type
= NECP_CLIENT_RESULT_NETAGENT
;
4487 length
= sizeof(header
.agent_value
);
4488 memcpy(&header
.agent_tlv_header
.type
, &type
, sizeof(type
));
4489 memcpy(&header
.agent_tlv_header
.length
, &length
, sizeof(length
));
4491 struct necp_client_result_netagent agent_struct
;
4492 agent_struct
.generation
= 0;
4493 uuid_copy(agent_struct
.netagent_uuid
, flow
->u
.nexus_agent
);
4495 memcpy(&header
.agent_value
, &agent_struct
, sizeof(agent_struct
));
4498 // Don't include outer TLV header in length field
4499 type
= NECP_CLIENT_RESULT_FLOW
;
4500 length
= (header_length
- sizeof(struct necp_tlv_header
) + flow
->assigned_results_length
);
4501 if (flow
->has_protoctl_event
) {
4502 length
+= sizeof(struct necp_client_flow_protoctl_event_header
);
4504 memcpy(&header
.flow_header
.outer_header
.type
, &type
, sizeof(type
));
4505 memcpy(&header
.flow_header
.outer_header
.length
, &length
, sizeof(length
));
4507 error
= copyout(&header
, uap
->buffer
+ client
->result_length
+ *assigned_results_cursor
, header_length
);
4509 NECPLOG(LOG_ERR
, "necp_client_copy assigned results tlv_header copyout error (%d)", error
);
4512 *assigned_results_cursor
+= header_length
;
4514 if (flow
->assigned_results
&& flow
->assigned_results_length
) {
4516 error
= copyout(flow
->assigned_results
, uap
->buffer
+ client
->result_length
+ *assigned_results_cursor
,
4517 flow
->assigned_results_length
);
4519 NECPLOG(LOG_ERR
, "necp_client_copy assigned results copyout error (%d)", error
);
4523 *assigned_results_cursor
+= flow
->assigned_results_length
;
4525 /* Read the protocol event and reset it */
4526 if (flow
->has_protoctl_event
) {
4527 struct necp_client_flow_protoctl_event_header protoctl_event_header
= {};
4529 type
= NECP_CLIENT_RESULT_PROTO_CTL_EVENT
;
4530 length
= sizeof(protoctl_event_header
.protoctl_event
);
4532 memcpy(&protoctl_event_header
.protoctl_tlv_header
.type
, &type
, sizeof(type
));
4533 memcpy(&protoctl_event_header
.protoctl_tlv_header
.length
, &length
, sizeof(length
));
4534 memcpy(&protoctl_event_header
.protoctl_event
, &flow
->protoctl_event
,
4535 sizeof(flow
->protoctl_event
));
4537 error
= copyout(&protoctl_event_header
, uap
->buffer
+ client
->result_length
+ *assigned_results_cursor
,
4538 sizeof(protoctl_event_header
));
4541 NECPLOG(LOG_ERR
, "necp_client_copy protocol control event results"
4542 " tlv_header copyout error (%d)", error
);
4545 *assigned_results_cursor
+= sizeof(protoctl_event_header
);
4546 flow
->has_protoctl_event
= FALSE
;
4547 flow
->protoctl_event
.protoctl_event_code
= 0;
4548 flow
->protoctl_event
.protoctl_event_val
= 0;
4549 flow
->protoctl_event
.protoctl_event_tcp_seq_num
= 0;
4553 if (!client_is_observed
) {
4554 flow_registration
->flow_result_read
= TRUE
;
4560 necp_client_copy_internal(struct necp_client
*client
, uuid_t client_id
, bool client_is_observed
, struct necp_client_action_args
*uap
, int *retval
)
4562 NECP_CLIENT_ASSERT_LOCKED(client
);
4565 if (uap
->action
== NECP_CLIENT_ACTION_COPY_PARAMETERS
) {
4566 if (uap
->buffer_size
< client
->parameters_length
) {
4569 error
= copyout(client
->parameters
, uap
->buffer
, client
->parameters_length
);
4571 NECPLOG(LOG_ERR
, "necp_client_copy parameters copyout error (%d)", error
);
4574 *retval
= client
->parameters_length
;
4575 } else if (uap
->action
== NECP_CLIENT_ACTION_COPY_UPDATED_RESULT
&&
4576 client
->result_read
&& !necp_client_has_unread_flows(client
)) {
4577 // Copy updates only, but nothing to read
4578 // Just return 0 for bytes read
4580 } else if (uap
->action
== NECP_CLIENT_ACTION_COPY_RESULT
||
4581 uap
->action
== NECP_CLIENT_ACTION_COPY_UPDATED_RESULT
) {
4582 size_t assigned_results_size
= 0;
4584 bool some_flow_is_defunct
= false;
4585 struct necp_client_flow_registration
*single_flow_registration
= NULL
;
4586 if (necp_client_id_is_flow(client_id
)) {
4587 single_flow_registration
= necp_client_find_flow(client
, client_id
);
4588 if (single_flow_registration
!= NULL
) {
4589 assigned_results_size
+= necp_client_calculate_flow_tlv_size(single_flow_registration
);
4592 // This request is for the client, so copy everything
4593 struct necp_client_flow_registration
*flow_registration
= NULL
;
4594 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
4595 if (flow_registration
->defunct
) {
4596 some_flow_is_defunct
= true;
4598 assigned_results_size
+= necp_client_calculate_flow_tlv_size(flow_registration
);
4601 if (uap
->buffer_size
< (client
->result_length
+ assigned_results_size
)) {
4605 u_int32_t original_flags
= 0;
4606 bool flags_updated
= false;
4607 if (some_flow_is_defunct
&& client
->legacy_client_is_flow
) {
4608 // If our client expects the defunct flag in the client, add it now
4609 u_int32_t client_flags
= 0;
4610 u_int32_t value_size
= 0;
4611 u_int8_t
*flags_pointer
= necp_buffer_get_tlv_value(client
->result
, 0, &value_size
);
4612 if (flags_pointer
!= NULL
&& value_size
== sizeof(client_flags
)) {
4613 memcpy(&client_flags
, flags_pointer
, value_size
);
4614 original_flags
= client_flags
;
4615 client_flags
|= NECP_CLIENT_RESULT_FLAG_DEFUNCT
;
4616 (void)necp_buffer_write_tlv_if_different(client
->result
, NECP_CLIENT_RESULT_FLAGS
,
4617 sizeof(client_flags
), &client_flags
, &flags_updated
,
4618 client
->result
, sizeof(client
->result
));
4622 error
= copyout(client
->result
, uap
->buffer
, client
->result_length
);
4624 if (flags_updated
) {
4625 // Revert stored flags
4626 (void)necp_buffer_write_tlv_if_different(client
->result
, NECP_CLIENT_RESULT_FLAGS
,
4627 sizeof(original_flags
), &original_flags
, &flags_updated
,
4628 client
->result
, sizeof(client
->result
));
4632 NECPLOG(LOG_ERR
, "necp_client_copy result copyout error (%d)", error
);
4636 size_t assigned_results_cursor
= 0;
4637 if (necp_client_id_is_flow(client_id
)) {
4638 if (single_flow_registration
!= NULL
) {
4639 error
= necp_client_fillout_flow_tlvs(client
, client_is_observed
, single_flow_registration
, uap
, &assigned_results_cursor
);
4645 // This request is for the client, so copy everything
4646 struct necp_client_flow_registration
*flow_registration
= NULL
;
4647 RB_FOREACH(flow_registration
, _necp_client_flow_tree
, &client
->flow_registrations
) {
4648 error
= necp_client_fillout_flow_tlvs(client
, client_is_observed
, flow_registration
, uap
, &assigned_results_cursor
);
4655 *retval
= client
->result_length
+ assigned_results_cursor
;
4657 if (!client_is_observed
) {
4658 client
->result_read
= TRUE
;
4666 necp_client_copy(struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
4669 struct necp_client
*client
= NULL
;
4671 uuid_clear(client_id
);
4675 if (uap
->buffer_size
== 0 || uap
->buffer
== 0) {
4679 if (uap
->action
!= NECP_CLIENT_ACTION_COPY_PARAMETERS
&&
4680 uap
->action
!= NECP_CLIENT_ACTION_COPY_RESULT
&&
4681 uap
->action
!= NECP_CLIENT_ACTION_COPY_UPDATED_RESULT
) {
4685 if (uap
->client_id
) {
4686 if (uap
->client_id_len
!= sizeof(uuid_t
)) {
4687 NECPLOG(LOG_ERR
, "Incorrect length (got %d, expected %d)", uap
->client_id_len
, sizeof(uuid_t
));
4691 error
= copyin(uap
->client_id
, client_id
, sizeof(uuid_t
));
4693 NECPLOG(LOG_ERR
, "necp_client_copy client_id copyin error (%d)", error
);
4698 const bool is_wildcard
= (bool)uuid_is_null(client_id
);
4700 NECP_FD_LOCK(fd_data
);
4703 if (uap
->action
== NECP_CLIENT_ACTION_COPY_RESULT
|| uap
->action
== NECP_CLIENT_ACTION_COPY_UPDATED_RESULT
) {
4704 struct necp_client
*find_client
= NULL
;
4705 RB_FOREACH(find_client
, _necp_client_tree
, &fd_data
->clients
) {
4706 NECP_CLIENT_LOCK(find_client
);
4707 if (!find_client
->result_read
|| necp_client_has_unread_flows(find_client
)) {
4708 client
= find_client
;
4709 // Leave the client locked, and break
4712 NECP_CLIENT_UNLOCK(find_client
);
4716 client
= necp_client_fd_find_client_and_lock(fd_data
, client_id
);
4719 if (client
!= NULL
) {
4720 // If client is set, it is locked
4721 error
= necp_client_copy_internal(client
, client_id
, FALSE
, uap
, retval
);
4722 NECP_CLIENT_UNLOCK(client
);
4725 // Unlock our own fd before moving on or returning
4726 NECP_FD_UNLOCK(fd_data
);
4728 if (client
== NULL
) {
4729 if (fd_data
->flags
& NECP_OPEN_FLAG_OBSERVER
) {
4730 // Observers are allowed to lookup clients on other fds
4733 NECP_CLIENT_TREE_LOCK_SHARED();
4735 bool found_client
= FALSE
;
4737 client
= necp_find_client_and_lock(client_id
);
4738 if (client
!= NULL
) {
4739 // Matched, copy out data
4740 found_client
= TRUE
;
4741 error
= necp_client_copy_internal(client
, client_id
, TRUE
, uap
, retval
);
4742 NECP_CLIENT_UNLOCK(client
);
4746 NECP_CLIENT_TREE_UNLOCK();
4748 // No client found, fail
4749 if (!found_client
) {
4753 // No client found, and not allowed to search other fds, fail
4762 necp_client_copy_client_update(struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
4768 if (!(fd_data
->flags
& NECP_OPEN_FLAG_PUSH_OBSERVER
)) {
4769 NECPLOG0(LOG_ERR
, "NECP fd is not observer, cannot copy client update");
4773 if (uap
->client_id_len
!= sizeof(uuid_t
) || uap
->client_id
== 0) {
4774 NECPLOG0(LOG_ERR
, "Client id invalid, cannot copy client update");
4778 if (uap
->buffer_size
== 0 || uap
->buffer
== 0) {
4779 NECPLOG0(LOG_ERR
, "Buffer invalid, cannot copy client update");
4783 NECP_FD_LOCK(fd_data
);
4784 struct necp_client_update
*client_update
= TAILQ_FIRST(&fd_data
->update_list
);
4785 if (client_update
!= NULL
) {
4786 TAILQ_REMOVE(&fd_data
->update_list
, client_update
, chain
);
4787 VERIFY(fd_data
->update_count
> 0);
4788 fd_data
->update_count
--;
4790 NECP_FD_UNLOCK(fd_data
);
4792 if (client_update
!= NULL
) {
4793 error
= copyout(client_update
->client_id
, uap
->client_id
, sizeof(uuid_t
));
4795 NECPLOG(LOG_ERR
, "Copy client update copyout client id error (%d)", error
);
4797 if (uap
->buffer_size
< client_update
->update_length
) {
4798 NECPLOG(LOG_ERR
, "Buffer size cannot hold update (%zu < %zu)", uap
->buffer_size
, client_update
->update_length
);
4801 error
= copyout(&client_update
->update
, uap
->buffer
, client_update
->update_length
);
4803 NECPLOG(LOG_ERR
, "Copy client update copyout error (%d)", error
);
4805 *retval
= client_update
->update_length
;
4810 FREE(client_update
, M_NECP
);
4811 client_update
= NULL
;
4820 necp_client_copy_parameters_locked(struct necp_client
*client
,
4821 struct necp_client_nexus_parameters
*parameters
)
4823 VERIFY(parameters
!= NULL
);
4825 struct necp_client_parsed_parameters parsed_parameters
= {};
4826 int error
= necp_client_parse_parameters(client
->parameters
, (u_int32_t
)client
->parameters_length
, &parsed_parameters
);
4828 parameters
->pid
= client
->proc_pid
;
4829 if (parsed_parameters
.valid_fields
& NECP_PARSED_PARAMETERS_FIELD_EFFECTIVE_PID
) {
4830 parameters
->epid
= parsed_parameters
.effective_pid
;
4832 parameters
->epid
= parameters
->pid
;
4834 memcpy(¶meters
->local_addr
, &parsed_parameters
.local_addr
, sizeof(parameters
->local_addr
));
4835 memcpy(¶meters
->remote_addr
, &parsed_parameters
.remote_addr
, sizeof(parameters
->remote_addr
));
4836 parameters
->ip_protocol
= parsed_parameters
.ip_protocol
;
4837 parameters
->traffic_class
= parsed_parameters
.traffic_class
;
4838 uuid_copy(parameters
->euuid
, parsed_parameters
.effective_uuid
);
4839 parameters
->is_listener
= (parsed_parameters
.flags
& NECP_CLIENT_PARAMETER_FLAG_LISTENER
) ? 1 : 0;
4840 parameters
->policy_id
= client
->policy_id
;
4842 // parse client result flag
4843 u_int32_t client_result_flags
= 0;
4844 u_int32_t value_size
= 0;
4845 u_int8_t
*flags_pointer
= NULL
;
4846 flags_pointer
= necp_buffer_get_tlv_value(client
->result
, 0, &value_size
);
4847 if (flags_pointer
&& value_size
== sizeof(client_result_flags
)) {
4848 memcpy(&client_result_flags
, flags_pointer
, value_size
);
4850 parameters
->allow_qos_marking
= (client_result_flags
& NECP_CLIENT_RESULT_FLAG_ALLOW_QOS_MARKING
) ? 1 : 0;
4856 necp_client_list(struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
4859 struct necp_client
*find_client
= NULL
;
4860 uuid_t
*list
= NULL
;
4861 u_int32_t requested_client_count
= 0;
4862 u_int32_t client_count
= 0;
4863 size_t copy_buffer_size
= 0;
4865 if (uap
->buffer_size
< sizeof(requested_client_count
) || uap
->buffer
== 0) {
4870 if (!(fd_data
->flags
& NECP_OPEN_FLAG_OBSERVER
)) {
4871 NECPLOG0(LOG_ERR
, "Client does not hold necessary entitlement to list other NECP clients");
4876 error
= copyin(uap
->buffer
, &requested_client_count
, sizeof(requested_client_count
));
4881 if (os_mul_overflow(sizeof(uuid_t
), requested_client_count
, ©_buffer_size
)) {
4886 if (uap
->buffer_size
- sizeof(requested_client_count
) != copy_buffer_size
) {
4891 if (copy_buffer_size
> NECP_MAX_CLIENT_LIST_SIZE
) {
4896 if (requested_client_count
> 0) {
4897 if ((list
= _MALLOC(copy_buffer_size
, M_NECP
, M_WAITOK
| M_ZERO
)) == NULL
) {
4904 NECP_CLIENT_TREE_LOCK_SHARED();
4907 RB_FOREACH(find_client
, _necp_client_global_tree
, &necp_client_global_tree
) {
4908 NECP_CLIENT_LOCK(find_client
);
4909 if (!uuid_is_null(find_client
->client_id
)) {
4910 if (client_count
< requested_client_count
) {
4911 uuid_copy(list
[client_count
], find_client
->client_id
);
4915 NECP_CLIENT_UNLOCK(find_client
);
4919 NECP_CLIENT_TREE_UNLOCK();
4921 error
= copyout(&client_count
, uap
->buffer
, sizeof(client_count
));
4923 NECPLOG(LOG_ERR
, "necp_client_list buffer copyout error (%d)", error
);
4927 if (requested_client_count
> 0 &&
4930 error
= copyout(list
, uap
->buffer
+ sizeof(client_count
), copy_buffer_size
);
4932 NECPLOG(LOG_ERR
, "necp_client_list client count copyout error (%d)", error
);
4947 necp_client_add_assertion(struct necp_client
*client
, uuid_t netagent_uuid
)
4949 struct necp_client_assertion
*new_assertion
= NULL
;
4951 MALLOC(new_assertion
, struct necp_client_assertion
*, sizeof(*new_assertion
), M_NECP
, M_WAITOK
);
4952 if (new_assertion
== NULL
) {
4953 NECPLOG0(LOG_ERR
, "Failed to allocate assertion");
4957 uuid_copy(new_assertion
->asserted_netagent
, netagent_uuid
);
4959 LIST_INSERT_HEAD(&client
->assertion_list
, new_assertion
, assertion_chain
);
4963 necp_client_remove_assertion(struct necp_client
*client
, uuid_t netagent_uuid
)
4965 struct necp_client_assertion
*found_assertion
= NULL
;
4966 struct necp_client_assertion
*search_assertion
= NULL
;
4967 LIST_FOREACH(search_assertion
, &client
->assertion_list
, assertion_chain
) {
4968 if (uuid_compare(search_assertion
->asserted_netagent
, netagent_uuid
) == 0) {
4969 found_assertion
= search_assertion
;
4974 if (found_assertion
== NULL
) {
4975 NECPLOG0(LOG_ERR
, "Netagent uuid not previously asserted");
4979 LIST_REMOVE(found_assertion
, assertion_chain
);
4980 FREE(found_assertion
, M_NECP
);
4985 necp_client_agent_action(struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
4988 struct necp_client
*client
= NULL
;
4990 bool acted_on_agent
= FALSE
;
4991 u_int8_t
*parameters
= NULL
;
4992 size_t parameters_size
= uap
->buffer_size
;
4994 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
) ||
4995 uap
->buffer_size
== 0 || uap
->buffer
== 0) {
4996 NECPLOG0(LOG_ERR
, "necp_client_agent_action invalid parameters");
5001 error
= copyin(uap
->client_id
, client_id
, sizeof(uuid_t
));
5003 NECPLOG(LOG_ERR
, "necp_client_agent_action copyin client_id error (%d)", error
);
5007 if (uap
->buffer_size
> NECP_MAX_AGENT_ACTION_SIZE
) {
5008 NECPLOG(LOG_ERR
, "necp_client_agent_action invalid buffer size (>%u)", NECP_MAX_AGENT_ACTION_SIZE
);
5013 if ((parameters
= _MALLOC(uap
->buffer_size
, M_NECP
, M_WAITOK
| M_ZERO
)) == NULL
) {
5014 NECPLOG0(LOG_ERR
, "necp_client_agent_action malloc failed");
5019 error
= copyin(uap
->buffer
, parameters
, uap
->buffer_size
);
5021 NECPLOG(LOG_ERR
, "necp_client_agent_action parameters copyin error (%d)", error
);
5025 NECP_FD_LOCK(fd_data
);
5026 client
= necp_client_fd_find_client_and_lock(fd_data
, client_id
);
5027 if (client
!= NULL
) {
5029 while ((offset
+ sizeof(struct necp_tlv_header
)) <= parameters_size
) {
5030 u_int8_t type
= necp_buffer_get_tlv_type(parameters
, offset
);
5031 u_int32_t length
= necp_buffer_get_tlv_length(parameters
, offset
);
5033 if (length
> (parameters_size
- (offset
+ sizeof(struct necp_tlv_header
)))) {
5034 // If the length is larger than what can fit in the remaining parameters size, bail
5035 NECPLOG(LOG_ERR
, "Invalid TLV length (%u)", length
);
5040 u_int8_t
*value
= necp_buffer_get_tlv_value(parameters
, offset
, NULL
);
5041 if (length
>= sizeof(uuid_t
) &&
5043 (type
== NECP_CLIENT_PARAMETER_TRIGGER_AGENT
||
5044 type
== NECP_CLIENT_PARAMETER_ASSERT_AGENT
||
5045 type
== NECP_CLIENT_PARAMETER_UNASSERT_AGENT
)) {
5048 uuid_copy(agent_uuid
, value
);
5049 u_int8_t netagent_message_type
= 0;
5050 if (type
== NECP_CLIENT_PARAMETER_TRIGGER_AGENT
) {
5051 netagent_message_type
= NETAGENT_MESSAGE_TYPE_CLIENT_TRIGGER
;
5052 } else if (type
== NECP_CLIENT_PARAMETER_ASSERT_AGENT
) {
5053 netagent_message_type
= NETAGENT_MESSAGE_TYPE_CLIENT_ASSERT
;
5054 } else if (type
== NECP_CLIENT_PARAMETER_UNASSERT_AGENT
) {
5055 netagent_message_type
= NETAGENT_MESSAGE_TYPE_CLIENT_UNASSERT
;
5058 // Before unasserting, verify that the assertion was already taken
5059 if (type
== NECP_CLIENT_PARAMETER_UNASSERT_AGENT
) {
5060 if (!necp_client_remove_assertion(client
, agent_uuid
)) {
5066 struct necp_client_nexus_parameters parsed_parameters
= {};
5067 necp_client_copy_parameters_locked(client
, &parsed_parameters
);
5069 error
= netagent_client_message_with_params(agent_uuid
,
5072 client
->agent_handle
,
5073 netagent_message_type
,
5077 acted_on_agent
= TRUE
;
5082 // Only save the assertion if the action succeeded
5083 if (type
== NECP_CLIENT_PARAMETER_ASSERT_AGENT
) {
5084 necp_client_add_assertion(client
, agent_uuid
);
5089 offset
+= sizeof(struct necp_tlv_header
) + length
;
5092 NECP_CLIENT_UNLOCK(client
);
5094 NECP_FD_UNLOCK(fd_data
);
5096 if (!acted_on_agent
&&
5102 if (parameters
!= NULL
) {
5103 FREE(parameters
, M_NECP
);
5111 necp_client_copy_agent(__unused
struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5116 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
) ||
5117 uap
->buffer_size
== 0 || uap
->buffer
== 0) {
5118 NECPLOG0(LOG_ERR
, "necp_client_copy_agent bad input");
5123 error
= copyin(uap
->client_id
, agent_uuid
, sizeof(uuid_t
));
5125 NECPLOG(LOG_ERR
, "necp_client_copy_agent copyin agent_uuid error (%d)", error
);
5129 error
= netagent_copyout(agent_uuid
, uap
->buffer
, uap
->buffer_size
);
5131 // netagent_copyout already logs appropriate errors
5141 necp_client_agent_use(struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5144 struct necp_client
*client
= NULL
;
5146 struct necp_agent_use_parameters parameters
;
5148 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
) ||
5149 uap
->buffer_size
!= sizeof(parameters
) || uap
->buffer
== 0) {
5154 error
= copyin(uap
->client_id
, client_id
, sizeof(uuid_t
));
5156 NECPLOG(LOG_ERR
, "Copyin client_id error (%d)", error
);
5160 error
= copyin(uap
->buffer
, ¶meters
, uap
->buffer_size
);
5162 NECPLOG(LOG_ERR
, "Parameters copyin error (%d)", error
);
5166 NECP_FD_LOCK(fd_data
);
5167 client
= necp_client_fd_find_client_and_lock(fd_data
, client_id
);
5168 if (client
!= NULL
) {
5169 error
= netagent_use(parameters
.agent_uuid
, ¶meters
.out_use_count
);
5170 NECP_CLIENT_UNLOCK(client
);
5175 NECP_FD_UNLOCK(fd_data
);
5178 error
= copyout(¶meters
, uap
->buffer
, uap
->buffer_size
);
5180 NECPLOG(LOG_ERR
, "Parameters copyout error (%d)", error
);
5192 necp_client_copy_interface(__unused
struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5195 u_int32_t interface_index
= 0;
5196 struct necp_interface_details interface_details
;
5198 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(u_int32_t
) ||
5199 uap
->buffer_size
< sizeof(interface_details
) || uap
->buffer
== 0) {
5200 NECPLOG0(LOG_ERR
, "necp_client_copy_interface bad input");
5205 error
= copyin(uap
->client_id
, &interface_index
, sizeof(u_int32_t
));
5207 NECPLOG(LOG_ERR
, "necp_client_copy_interface copyin interface_index error (%d)", error
);
5211 if (interface_index
== 0) {
5213 NECPLOG(LOG_ERR
, "necp_client_copy_interface bad interface_index (%d)", interface_index
);
5217 memset(&interface_details
, 0, sizeof(interface_details
));
5219 ifnet_head_lock_shared();
5220 ifnet_t interface
= NULL
;
5221 if (interface_index
!= IFSCOPE_NONE
&& interface_index
<= (u_int32_t
)if_index
) {
5222 interface
= ifindex2ifnet
[interface_index
];
5225 if (interface
!= NULL
) {
5226 if (interface
->if_xname
!= NULL
) {
5227 strlcpy((char *)&interface_details
.name
, interface
->if_xname
, sizeof(interface_details
.name
));
5229 interface_details
.index
= interface
->if_index
;
5230 interface_details
.generation
= ifnet_get_generation(interface
);
5231 if (interface
->if_delegated
.ifp
!= NULL
) {
5232 interface_details
.delegate_index
= interface
->if_delegated
.ifp
->if_index
;
5234 interface_details
.functional_type
= if_functional_type(interface
, TRUE
);
5235 if (IFNET_IS_EXPENSIVE(interface
)) {
5236 interface_details
.flags
|= NECP_INTERFACE_FLAG_EXPENSIVE
;
5238 if ((interface
->if_eflags
& IFEF_TXSTART
) == IFEF_TXSTART
) {
5239 interface_details
.flags
|= NECP_INTERFACE_FLAG_TXSTART
;
5241 if ((interface
->if_eflags
& IFEF_NOACKPRI
) == IFEF_NOACKPRI
) {
5242 interface_details
.flags
|= NECP_INTERFACE_FLAG_NOACKPRI
;
5244 if ((interface
->if_eflags
& IFEF_3CA
) == IFEF_3CA
) {
5245 interface_details
.flags
|= NECP_INTERFACE_FLAG_3CARRIERAGG
;
5247 if (IFNET_IS_LOW_POWER(interface
)) {
5248 interface_details
.flags
|= NECP_INTERFACE_FLAG_IS_LOW_POWER
;
5250 interface_details
.mtu
= interface
->if_mtu
;
5252 u_int8_t ipv4_signature_len
= sizeof(interface_details
.ipv4_signature
.signature
);
5253 u_int16_t ipv4_signature_flags
;
5254 if (ifnet_get_netsignature(interface
, AF_INET
, &ipv4_signature_len
, &ipv4_signature_flags
,
5255 (u_int8_t
*)&interface_details
.ipv4_signature
) != 0) {
5256 ipv4_signature_len
= 0;
5258 interface_details
.ipv4_signature
.signature_len
= ipv4_signature_len
;
5260 u_int8_t ipv6_signature_len
= sizeof(interface_details
.ipv6_signature
.signature
);
5261 u_int16_t ipv6_signature_flags
;
5262 if (ifnet_get_netsignature(interface
, AF_INET6
, &ipv6_signature_len
, &ipv6_signature_flags
,
5263 (u_int8_t
*)&interface_details
.ipv6_signature
) != 0) {
5264 ipv6_signature_len
= 0;
5266 interface_details
.ipv6_signature
.signature_len
= ipv6_signature_len
;
5271 error
= copyout(&interface_details
, uap
->buffer
, sizeof(interface_details
));
5273 NECPLOG(LOG_ERR
, "necp_client_copy_interface copyout error (%d)", error
);
5284 necp_client_copy_route_statistics(__unused
struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5287 struct necp_client
*client
= NULL
;
5290 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
) ||
5291 uap
->buffer_size
< sizeof(struct necp_stat_counts
) || uap
->buffer
== 0) {
5292 NECPLOG0(LOG_ERR
, "necp_client_copy_route_statistics bad input");
5297 error
= copyin(uap
->client_id
, client_id
, sizeof(uuid_t
));
5299 NECPLOG(LOG_ERR
, "necp_client_copy_route_statistics copyin client_id error (%d)", error
);
5304 NECP_FD_LOCK(fd_data
);
5305 client
= necp_client_fd_find_client_and_lock(fd_data
, client_id
);
5306 if (client
!= NULL
) {
5307 NECP_CLIENT_ROUTE_LOCK(client
);
5308 struct necp_stat_counts route_stats
= {};
5309 if (client
->current_route
!= NULL
&& client
->current_route
->rt_stats
!= NULL
) {
5310 struct nstat_counts
*rt_stats
= client
->current_route
->rt_stats
;
5311 atomic_get_64(route_stats
.necp_stat_rxpackets
, &rt_stats
->nstat_rxpackets
);
5312 atomic_get_64(route_stats
.necp_stat_rxbytes
, &rt_stats
->nstat_rxbytes
);
5313 atomic_get_64(route_stats
.necp_stat_txpackets
, &rt_stats
->nstat_txpackets
);
5314 atomic_get_64(route_stats
.necp_stat_txbytes
, &rt_stats
->nstat_txbytes
);
5315 route_stats
.necp_stat_rxduplicatebytes
= rt_stats
->nstat_rxduplicatebytes
;
5316 route_stats
.necp_stat_rxoutoforderbytes
= rt_stats
->nstat_rxoutoforderbytes
;
5317 route_stats
.necp_stat_txretransmit
= rt_stats
->nstat_txretransmit
;
5318 route_stats
.necp_stat_connectattempts
= rt_stats
->nstat_connectattempts
;
5319 route_stats
.necp_stat_connectsuccesses
= rt_stats
->nstat_connectsuccesses
;
5320 route_stats
.necp_stat_min_rtt
= rt_stats
->nstat_min_rtt
;
5321 route_stats
.necp_stat_avg_rtt
= rt_stats
->nstat_avg_rtt
;
5322 route_stats
.necp_stat_var_rtt
= rt_stats
->nstat_var_rtt
;
5323 route_stats
.necp_stat_route_flags
= client
->current_route
->rt_flags
;
5326 // Unlock before copying out
5327 NECP_CLIENT_ROUTE_UNLOCK(client
);
5328 NECP_CLIENT_UNLOCK(client
);
5329 NECP_FD_UNLOCK(fd_data
);
5331 error
= copyout(&route_stats
, uap
->buffer
, sizeof(route_stats
));
5333 NECPLOG(LOG_ERR
, "necp_client_copy_route_statistics copyout error (%d)", error
);
5337 NECP_FD_UNLOCK(fd_data
);
5348 necp_client_update_cache(struct necp_fd_data
*fd_data
, struct necp_client_action_args
*uap
, int *retval
)
5351 struct necp_client
*client
= NULL
;
5354 if (uap
->client_id
== 0 || uap
->client_id_len
!= sizeof(uuid_t
)) {
5359 error
= copyin(uap
->client_id
, client_id
, sizeof(uuid_t
));
5361 NECPLOG(LOG_ERR
, "necp_client_update_cache copyin client_id error (%d)", error
);
5365 NECP_FD_LOCK(fd_data
);
5366 client
= necp_client_fd_find_client_and_lock(fd_data
, client_id
);
5367 if (client
== NULL
) {
5368 NECP_FD_UNLOCK(fd_data
);
5373 struct necp_client_flow_registration
*flow_registration
= necp_client_find_flow(client
, client_id
);
5374 if (flow_registration
== NULL
) {
5375 NECP_CLIENT_UNLOCK(client
);
5376 NECP_FD_UNLOCK(fd_data
);
5381 NECP_CLIENT_ROUTE_LOCK(client
);
5382 // This needs to be changed when TFO/ECN is supported by multiple flows
5383 struct necp_client_flow
*flow
= LIST_FIRST(&flow_registration
->flow_list
);
5385 (flow
->remote_addr
.sa
.sa_family
!= AF_INET
&&
5386 flow
->remote_addr
.sa
.sa_family
!= AF_INET6
) ||
5387 (flow
->local_addr
.sa
.sa_family
!= AF_INET
&&
5388 flow
->local_addr
.sa
.sa_family
!= AF_INET6
)) {
5390 NECPLOG(LOG_ERR
, "necp_client_update_cache no flow error (%d)", error
);
5394 necp_cache_buffer cache_buffer
;
5395 memset(&cache_buffer
, 0, sizeof(cache_buffer
));
5397 if (uap
->buffer_size
!= sizeof(necp_cache_buffer
) ||
5398 uap
->buffer
== USER_ADDR_NULL
) {
5403 error
= copyin(uap
->buffer
, &cache_buffer
, sizeof(cache_buffer
));
5405 NECPLOG(LOG_ERR
, "necp_client_update_cache copyin cache buffer error (%d)", error
);
5409 if (cache_buffer
.necp_cache_buf_type
== NECP_CLIENT_CACHE_TYPE_ECN
&&
5410 cache_buffer
.necp_cache_buf_ver
== NECP_CLIENT_CACHE_TYPE_ECN_VER_1
) {
5411 if (cache_buffer
.necp_cache_buf_size
!= sizeof(necp_tcp_ecn_cache
) ||
5412 cache_buffer
.necp_cache_buf_addr
== USER_ADDR_NULL
) {
5417 necp_tcp_ecn_cache ecn_cache_buffer
;
5418 memset(&ecn_cache_buffer
, 0, sizeof(ecn_cache_buffer
));
5420 error
= copyin(cache_buffer
.necp_cache_buf_addr
, &ecn_cache_buffer
, sizeof(necp_tcp_ecn_cache
));
5422 NECPLOG(LOG_ERR
, "necp_client_update_cache copyin ecn cache buffer error (%d)", error
);
5426 if (client
->current_route
!= NULL
&& client
->current_route
->rt_ifp
!= NULL
) {
5427 if (!client
->platform_binary
) {
5428 ecn_cache_buffer
.necp_tcp_ecn_heuristics_success
= 0;
5430 tcp_heuristics_ecn_update(&ecn_cache_buffer
, client
->current_route
->rt_ifp
,
5431 (union sockaddr_in_4_6
*)&flow
->local_addr
);
5433 } else if (cache_buffer
.necp_cache_buf_type
== NECP_CLIENT_CACHE_TYPE_TFO
&&
5434 cache_buffer
.necp_cache_buf_ver
== NECP_CLIENT_CACHE_TYPE_TFO_VER_1
) {
5435 if (cache_buffer
.necp_cache_buf_size
!= sizeof(necp_tcp_tfo_cache
) ||
5436 cache_buffer
.necp_cache_buf_addr
== USER_ADDR_NULL
) {
5441 necp_tcp_tfo_cache tfo_cache_buffer
;
5442 memset(&tfo_cache_buffer
, 0, sizeof(tfo_cache_buffer
));
5444 error
= copyin(cache_buffer
.necp_cache_buf_addr
, &tfo_cache_buffer
, sizeof(necp_tcp_tfo_cache
));
5446 NECPLOG(LOG_ERR
, "necp_client_update_cache copyin tfo cache buffer error (%d)", error
);
5450 if (client
->current_route
!= NULL
&& client
->current_route
->rt_ifp
!= NULL
) {
5451 if (!client
->platform_binary
) {
5452 tfo_cache_buffer
.necp_tcp_tfo_heuristics_success
= 0;
5454 tcp_heuristics_tfo_update(&tfo_cache_buffer
, client
->current_route
->rt_ifp
,
5455 (union sockaddr_in_4_6
*)&flow
->local_addr
,
5456 (union sockaddr_in_4_6
*)&flow
->remote_addr
);
5462 NECP_CLIENT_ROUTE_UNLOCK(client
);
5463 NECP_CLIENT_UNLOCK(client
);
5464 NECP_FD_UNLOCK(fd_data
);
5471 necp_client_action(struct proc
*p
, struct necp_client_action_args
*uap
, int *retval
)
5475 int return_value
= 0;
5476 struct necp_fd_data
*fd_data
= NULL
;
5477 error
= necp_find_fd_data(uap
->necp_fd
, &fd_data
);
5479 NECPLOG(LOG_ERR
, "necp_client_action find fd error (%d)", error
);
5483 u_int32_t action
= uap
->action
;
5485 case NECP_CLIENT_ACTION_ADD
: {
5486 return_value
= necp_client_add(p
, fd_data
, uap
, retval
);
5489 case NECP_CLIENT_ACTION_REMOVE
: {
5490 return_value
= necp_client_remove(fd_data
, uap
, retval
);
5493 case NECP_CLIENT_ACTION_COPY_PARAMETERS
:
5494 case NECP_CLIENT_ACTION_COPY_RESULT
:
5495 case NECP_CLIENT_ACTION_COPY_UPDATED_RESULT
: {
5496 return_value
= necp_client_copy(fd_data
, uap
, retval
);
5499 case NECP_CLIENT_ACTION_COPY_LIST
: {
5500 return_value
= necp_client_list(fd_data
, uap
, retval
);
5503 case NECP_CLIENT_ACTION_AGENT
: {
5504 return_value
= necp_client_agent_action(fd_data
, uap
, retval
);
5507 case NECP_CLIENT_ACTION_COPY_AGENT
: {
5508 return_value
= necp_client_copy_agent(fd_data
, uap
, retval
);
5511 case NECP_CLIENT_ACTION_AGENT_USE
: {
5512 return_value
= necp_client_agent_use(fd_data
, uap
, retval
);
5515 case NECP_CLIENT_ACTION_COPY_INTERFACE
: {
5516 return_value
= necp_client_copy_interface(fd_data
, uap
, retval
);
5519 case NECP_CLIENT_ACTION_COPY_ROUTE_STATISTICS
: {
5520 return_value
= necp_client_copy_route_statistics(fd_data
, uap
, retval
);
5523 case NECP_CLIENT_ACTION_UPDATE_CACHE
: {
5524 return_value
= necp_client_update_cache(fd_data
, uap
, retval
);
5527 case NECP_CLIENT_ACTION_COPY_CLIENT_UPDATE
: {
5528 return_value
= necp_client_copy_client_update(fd_data
, uap
, retval
);
5532 NECPLOG(LOG_ERR
, "necp_client_action unknown action (%u)", action
);
5533 return_value
= EINVAL
;
5538 file_drop(uap
->necp_fd
);
5540 return (return_value
);
5543 #define NECP_MAX_MATCH_POLICY_PARAMETER_SIZE 1024
5546 necp_match_policy(struct proc
*p
, struct necp_match_policy_args
*uap
, int32_t *retval
)
5548 #pragma unused(retval)
5549 u_int8_t
*parameters
= NULL
;
5550 struct necp_aggregate_result returned_result
;
5558 if (uap
->parameters
== 0 || uap
->parameters_size
== 0 || uap
->parameters_size
> NECP_MAX_MATCH_POLICY_PARAMETER_SIZE
|| uap
->returned_result
== 0) {
5563 MALLOC(parameters
, u_int8_t
*, uap
->parameters_size
, M_NECP
, M_WAITOK
| M_ZERO
);
5564 if (parameters
== NULL
) {
5568 // Copy parameters in
5569 error
= copyin(uap
->parameters
, parameters
, uap
->parameters_size
);
5574 error
= necp_application_find_policy_match_internal(p
, parameters
, uap
->parameters_size
,
5575 &returned_result
, NULL
, 0, NULL
, NULL
, NULL
, false);
5580 // Copy return value back
5581 error
= copyout(&returned_result
, uap
->returned_result
, sizeof(struct necp_aggregate_result
));
5586 if (parameters
!= NULL
) {
5587 FREE(parameters
, M_NECP
);
5592 /// Socket operations
5593 #define NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH 253
5596 necp_set_socket_attribute(u_int8_t
*buffer
, size_t buffer_length
, u_int8_t type
, char **buffer_p
)
5600 size_t string_size
= 0;
5601 char *local_string
= NULL
;
5602 u_int8_t
*value
= NULL
;
5604 cursor
= necp_buffer_find_tlv(buffer
, buffer_length
, 0, type
, 0);
5606 // This will clear out the parameter
5610 string_size
= necp_buffer_get_tlv_length(buffer
, cursor
);
5611 if (string_size
== 0 || string_size
> NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH
) {
5612 // This will clear out the parameter
5616 MALLOC(local_string
, char *, string_size
+ 1, M_NECP
, M_WAITOK
| M_ZERO
);
5617 if (local_string
== NULL
) {
5618 NECPLOG(LOG_ERR
, "Failed to allocate a socket attribute buffer (size %d)", string_size
);
5622 value
= necp_buffer_get_tlv_value(buffer
, cursor
, NULL
);
5623 if (value
== NULL
) {
5624 NECPLOG0(LOG_ERR
, "Failed to get socket attribute");
5628 memcpy(local_string
, value
, string_size
);
5629 local_string
[string_size
] = 0;
5632 if (*buffer_p
!= NULL
) {
5633 FREE(*buffer_p
, M_NECP
);
5637 *buffer_p
= local_string
;
5640 if (local_string
!= NULL
) {
5641 FREE(local_string
, M_NECP
);
5647 necp_set_socket_attributes(struct socket
*so
, struct sockopt
*sopt
)
5650 u_int8_t
*buffer
= NULL
;
5651 struct inpcb
*inp
= NULL
;
5653 if ((SOCK_DOM(so
) != PF_INET
5655 && SOCK_DOM(so
) != PF_INET6
5662 inp
= sotoinpcb(so
);
5664 size_t valsize
= sopt
->sopt_valsize
;
5666 valsize
> ((sizeof(struct necp_tlv_header
) + NECP_MAX_SOCKET_ATTRIBUTE_STRING_LENGTH
) * 2)) {
5670 MALLOC(buffer
, u_int8_t
*, valsize
, M_NECP
, M_WAITOK
| M_ZERO
);
5671 if (buffer
== NULL
) {
5675 error
= sooptcopyin(sopt
, buffer
, valsize
, 0);
5680 error
= necp_set_socket_attribute(buffer
, valsize
, NECP_TLV_ATTRIBUTE_DOMAIN
, &inp
->inp_necp_attributes
.inp_domain
);
5682 NECPLOG0(LOG_ERR
, "Could not set domain TLV for socket attributes");
5686 error
= necp_set_socket_attribute(buffer
, valsize
, NECP_TLV_ATTRIBUTE_ACCOUNT
, &inp
->inp_necp_attributes
.inp_account
);
5688 NECPLOG0(LOG_ERR
, "Could not set account TLV for socket attributes");
5693 NECPLOG(LOG_DEBUG
, "Set on socket: Domain %s, Account %s", inp
->inp_necp_attributes
.inp_domain
, inp
->inp_necp_attributes
.inp_account
);
5696 if (buffer
!= NULL
) {
5697 FREE(buffer
, M_NECP
);
5704 necp_get_socket_attributes(struct socket
*so
, struct sockopt
*sopt
)
5707 u_int8_t
*buffer
= NULL
;
5708 u_int8_t
*cursor
= NULL
;
5710 struct inpcb
*inp
= NULL
;
5712 if ((SOCK_DOM(so
) != PF_INET
5714 && SOCK_DOM(so
) != PF_INET6
5721 inp
= sotoinpcb(so
);
5722 if (inp
->inp_necp_attributes
.inp_domain
!= NULL
) {
5723 valsize
+= sizeof(struct necp_tlv_header
) + strlen(inp
->inp_necp_attributes
.inp_domain
);
5725 if (inp
->inp_necp_attributes
.inp_account
!= NULL
) {
5726 valsize
+= sizeof(struct necp_tlv_header
) + strlen(inp
->inp_necp_attributes
.inp_account
);
5732 MALLOC(buffer
, u_int8_t
*, valsize
, M_NECP
, M_WAITOK
| M_ZERO
);
5733 if (buffer
== NULL
) {
5738 if (inp
->inp_necp_attributes
.inp_domain
!= NULL
) {
5739 cursor
= necp_buffer_write_tlv(cursor
, NECP_TLV_ATTRIBUTE_DOMAIN
, strlen(inp
->inp_necp_attributes
.inp_domain
), inp
->inp_necp_attributes
.inp_domain
,
5743 if (inp
->inp_necp_attributes
.inp_account
!= NULL
) {
5744 cursor
= necp_buffer_write_tlv(cursor
, NECP_TLV_ATTRIBUTE_ACCOUNT
, strlen(inp
->inp_necp_attributes
.inp_account
), inp
->inp_necp_attributes
.inp_account
,
5748 error
= sooptcopyout(sopt
, buffer
, valsize
);
5753 if (buffer
!= NULL
) {
5754 FREE(buffer
, M_NECP
);
5761 necp_create_nexus_assign_message(uuid_t nexus_instance
, u_int32_t nexus_port
, void *key
, uint32_t key_length
,
5762 struct necp_client_endpoint
*local_endpoint
, struct necp_client_endpoint
*remote_endpoint
,
5763 u_int32_t flow_adv_index
, void *flow_stats
, size_t *message_length
)
5765 u_int8_t
*buffer
= NULL
;
5766 u_int8_t
*cursor
= NULL
;
5768 bool has_nexus_assignment
= FALSE
;
5771 if (!uuid_is_null(nexus_instance
)) {
5772 has_nexus_assignment
= TRUE
;
5773 valsize
+= sizeof(struct necp_tlv_header
) + sizeof(uuid_t
);
5774 valsize
+= sizeof(struct necp_tlv_header
) + sizeof(u_int32_t
);
5776 if (flow_adv_index
!= NECP_FLOWADV_IDX_INVALID
) {
5777 valsize
+= sizeof(struct necp_tlv_header
) + sizeof(u_int32_t
);
5779 if (key
!= NULL
&& key_length
> 0) {
5780 valsize
+= sizeof(struct necp_tlv_header
) + key_length
;
5782 if (local_endpoint
!= NULL
) {
5783 valsize
+= sizeof(struct necp_tlv_header
) + sizeof(struct necp_client_endpoint
);
5785 if (remote_endpoint
!= NULL
) {
5786 valsize
+= sizeof(struct necp_tlv_header
) + sizeof(struct necp_client_endpoint
);
5788 if (flow_stats
!= NULL
) {
5789 valsize
+= sizeof(struct necp_tlv_header
) + sizeof(void *);
5795 MALLOC(buffer
, u_int8_t
*, valsize
, M_NETAGENT
, M_WAITOK
| M_ZERO
); // Use M_NETAGENT area, since it is expected upon free
5796 if (buffer
== NULL
) {
5801 if (has_nexus_assignment
) {
5802 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_RESULT_NEXUS_INSTANCE
, sizeof(uuid_t
), nexus_instance
, buffer
, valsize
);
5803 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_RESULT_NEXUS_PORT
, sizeof(u_int32_t
), &nexus_port
, buffer
, valsize
);
5805 if (flow_adv_index
!= NECP_FLOWADV_IDX_INVALID
) {
5806 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_RESULT_NEXUS_PORT_FLOW_INDEX
, sizeof(u_int32_t
), &flow_adv_index
, buffer
, valsize
);
5808 if (key
!= NULL
&& key_length
> 0) {
5809 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_PARAMETER_NEXUS_KEY
, key_length
, key
, buffer
, valsize
);
5811 if (local_endpoint
!= NULL
) {
5812 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_RESULT_LOCAL_ENDPOINT
, sizeof(struct necp_client_endpoint
), local_endpoint
, buffer
, valsize
);
5814 if (remote_endpoint
!= NULL
) {
5815 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_RESULT_REMOTE_ENDPOINT
, sizeof(struct necp_client_endpoint
), remote_endpoint
, buffer
, valsize
);
5817 if (flow_stats
!= NULL
) {
5818 cursor
= necp_buffer_write_tlv(cursor
, NECP_CLIENT_RESULT_NEXUS_FLOW_STATS
, sizeof(void *), &flow_stats
, buffer
, valsize
);
5821 *message_length
= valsize
;
5827 necp_inpcb_remove_cb(struct inpcb
*inp
)
5829 if (!uuid_is_null(inp
->necp_client_uuid
)) {
5830 necp_client_unregister_socket_flow(inp
->necp_client_uuid
, inp
);
5831 uuid_clear(inp
->necp_client_uuid
);
5836 necp_inpcb_dispose(struct inpcb
*inp
)
5838 necp_inpcb_remove_cb(inp
); // Clear out socket registrations if not yet done
5839 if (inp
->inp_necp_attributes
.inp_domain
!= NULL
) {
5840 FREE(inp
->inp_necp_attributes
.inp_domain
, M_NECP
);
5841 inp
->inp_necp_attributes
.inp_domain
= NULL
;
5843 if (inp
->inp_necp_attributes
.inp_account
!= NULL
) {
5844 FREE(inp
->inp_necp_attributes
.inp_account
, M_NECP
);
5845 inp
->inp_necp_attributes
.inp_account
= NULL
;
5850 necp_mppcb_dispose(struct mppcb
*mpp
)
5852 if (!uuid_is_null(mpp
->necp_client_uuid
)) {
5853 necp_client_unregister_multipath_cb(mpp
->necp_client_uuid
, mpp
);
5854 uuid_clear(mpp
->necp_client_uuid
);
5861 necp_client_init(void)
5863 necp_fd_grp_attr
= lck_grp_attr_alloc_init();
5864 if (necp_fd_grp_attr
== NULL
) {
5865 panic("lck_grp_attr_alloc_init failed\n");
5869 necp_fd_mtx_grp
= lck_grp_alloc_init("necp_fd", necp_fd_grp_attr
);
5870 if (necp_fd_mtx_grp
== NULL
) {
5871 panic("lck_grp_alloc_init failed\n");
5875 necp_fd_mtx_attr
= lck_attr_alloc_init();
5876 if (necp_fd_mtx_attr
== NULL
) {
5877 panic("lck_attr_alloc_init failed\n");
5881 necp_client_fd_size
= sizeof(struct necp_fd_data
);
5882 necp_client_fd_zone
= zinit(necp_client_fd_size
,
5883 NECP_CLIENT_FD_ZONE_MAX
* necp_client_fd_size
,
5884 0, NECP_CLIENT_FD_ZONE_NAME
);
5885 if (necp_client_fd_zone
== NULL
) {
5886 panic("zinit(necp_client_fd) failed\n");
5890 necp_flow_size
= sizeof(struct necp_client_flow
);
5891 necp_flow_cache
= mcache_create(NECP_FLOW_ZONE_NAME
, necp_flow_size
, sizeof (uint64_t), 0, MCR_SLEEP
);
5892 if (necp_flow_cache
== NULL
) {
5893 panic("mcache_create(necp_flow_cache) failed\n");
5897 necp_flow_registration_size
= sizeof(struct necp_client_flow_registration
);
5898 necp_flow_registration_cache
= mcache_create(NECP_FLOW_REGISTRATION_ZONE_NAME
, necp_flow_registration_size
, sizeof (uint64_t), 0, MCR_SLEEP
);
5899 if (necp_flow_registration_cache
== NULL
) {
5900 panic("mcache_create(necp_client_flow_registration) failed\n");
5904 necp_client_update_tcall
= thread_call_allocate_with_options(necp_update_all_clients_callout
, NULL
,
5905 THREAD_CALL_PRIORITY_KERNEL
, THREAD_CALL_OPTIONS_ONCE
);
5906 VERIFY(necp_client_update_tcall
!= NULL
);
5908 lck_rw_init(&necp_fd_lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
5909 lck_rw_init(&necp_observer_lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
5910 lck_rw_init(&necp_client_tree_lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
5911 lck_rw_init(&necp_flow_tree_lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
5912 lck_rw_init(&necp_collect_stats_list_lock
, necp_fd_mtx_grp
, necp_fd_mtx_attr
);
5914 LIST_INIT(&necp_fd_list
);
5915 LIST_INIT(&necp_fd_observer_list
);
5916 LIST_INIT(&necp_collect_stats_flow_list
);
5918 RB_INIT(&necp_client_global_tree
);
5919 RB_INIT(&necp_client_flow_global_tree
);
5925 necp_client_reap_caches(boolean_t purge
)
5927 mcache_reap_now(necp_flow_cache
, purge
);
5928 mcache_reap_now(necp_flow_registration_cache
, purge
);