xref: /xnu-12377.81.4/bsd/net/necp.h (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2013-2023 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #ifndef _NET_NECP_H_
30 #define _NET_NECP_H_
31 
32 #include <net/net_kev.h>
33 #ifdef PRIVATE
34 
35 #include <netinet/in.h>
36 #include <netinet/in_private.h>
37 #include <netinet/in_stat.h>
38 #include <sys/socket.h>
39 #include <net/if_private.h>
40 #include <net/if_var_private.h>
41 #include <netinet/tcp_private.h>
42 
43 #if SKYWALK
44 #include <skywalk/os_nexus_private.h>
45 #else /* !SKYWALK */
46 typedef uint16_t nexus_port_t;
47 #endif /* !SKYWALK */
48 
49 /*
50  * Name registered by the NECP
51  */
52 #define NECP_CONTROL_NAME "com.apple.net.necp_control"
53 
54 #define NECP_TLV_LENGTH_UINT32  1
55 
56 struct necp_packet_header {
57 	u_int8_t            packet_type;
58 	u_int8_t                flags;
59 	u_int32_t           message_id;
60 };
61 
62 typedef struct {
63 	uid_t               uid;
64 	uuid_t              effective_uuid;
65 	uid_t               persona_id;
66 } necp_application_id_t;
67 
68 #define NECP_DOMAIN_TRIE_SUPPORT 1
69 
70 /*
71  * Request sent from user-space to create a domain trie
72  */
73 #define NECP_DOMAIN_TRIE_FLAG_REVERSE_SEARCH        0x1
74 #define NECP_DOMAIN_TRIE_FLAG_ALLOW_PARTIAL_MATCH   0x2
75 
76 typedef struct necp_domain_trie_request {
77 	uint32_t id;
78 	uint32_t total_mem_size;
79 	uint32_t nodes_mem_size;
80 	uint32_t maps_mem_size;
81 	uint32_t bytes_mem_size;
82 	uint32_t nodes_count;
83 	uint32_t maps_count;
84 	uint32_t bytes_count;
85 	uint32_t partial_match_terminator;
86 	uint32_t flags;
87 	uint8_t  data[__counted_by(total_mem_size)];
88 } necp_domain_trie_request_t;
89 
90 /*
91  * Control message commands
92  */
93 #define NECP_PACKET_TYPE_POLICY_ADD                             1
94 #define NECP_PACKET_TYPE_POLICY_GET                             2
95 #define NECP_PACKET_TYPE_POLICY_DELETE                  3
96 #define NECP_PACKET_TYPE_POLICY_APPLY_ALL               4
97 #define NECP_PACKET_TYPE_POLICY_LIST_ALL                5
98 #define NECP_PACKET_TYPE_POLICY_DELETE_ALL              6
99 #define NECP_PACKET_TYPE_SET_SESSION_PRIORITY   7
100 #define NECP_PACKET_TYPE_LOCK_SESSION_TO_PROC   8
101 #define NECP_PACKET_TYPE_REGISTER_SERVICE               9
102 #define NECP_PACKET_TYPE_UNREGISTER_SERVICE             10
103 #define NECP_PACKET_TYPE_POLICY_DUMP_ALL                11
104 
105 /*
106  * Session actions
107  */
108 #define NECP_SESSION_ACTION_POLICY_ADD                          1       // In: Policy TLVs				Out: necp_policy_id
109 #define NECP_SESSION_ACTION_POLICY_GET                          2       // In: necp_policy_id			Out: Policy TLVs
110 #define NECP_SESSION_ACTION_POLICY_DELETE                       3       // In: necp_policy_id			Out: None
111 #define NECP_SESSION_ACTION_POLICY_APPLY_ALL            4       // In: None						Out: None
112 #define NECP_SESSION_ACTION_POLICY_LIST_ALL                     5       // In: None						Out: TLVs of IDs
113 #define NECP_SESSION_ACTION_POLICY_DELETE_ALL           6       // In: None						Out: None
114 #define NECP_SESSION_ACTION_SET_SESSION_PRIORITY        7       // In: necp_session_priority	Out: None
115 #define NECP_SESSION_ACTION_LOCK_SESSION_TO_PROC        8       // In: None						Out: None
116 #define NECP_SESSION_ACTION_REGISTER_SERVICE            9       // In: uuid_t					Out: None
117 #define NECP_SESSION_ACTION_UNREGISTER_SERVICE          10      // In: uuid_t					Out: None
118 #define NECP_SESSION_ACTION_POLICY_DUMP_ALL                     11      // In: None						Out: uint32_t bytes length, then Policy TLVs
119 #define NECP_SESSION_ACTION_ADD_DOMAIN_FILTER           12      // In: struct net_bloom_filter  Out: uint32_t, ID
120 #define NECP_SESSION_ACTION_REMOVE_DOMAIN_FILTER        13      // In: uint32_t, ID             Out: None
121 #define NECP_SESSION_ACTION_REMOVE_ALL_DOMAIN_FILTERS   14      // In: None                     Out: None
122 #define NECP_SESSION_ACTION_ADD_DOMAIN_TRIE             15      // In: struct necp_domain_trie_request  Out: uint32_t, ID
123 #define NECP_SESSION_ACTION_REMOVE_DOMAIN_TRIE          16      // In: uint32_t, ID             Out: None
124 #define NECP_SESSION_ACTION_REMOVE_ALL_DOMAIN_TRIES     17      // In: None                     Out: None
125 #define NECP_SESSION_ACTION_TRIE_DUMP_ALL               18      // In: None                     Out: uint8_t, count, then array of struct necp_domain_trie_request with no 'data'
126 
127 /*
128  * Control message flags
129  */
130 #define NECP_PACKET_FLAGS_RESPONSE                              0x01    // Used for acks, errors, and query responses
131 
132 /*
133  * Control message TLV types
134  */
135 #define NECP_TLV_NIL                                                    0
136 #define NECP_TLV_ERROR                                                  1       // u_int32_t
137 #define NECP_TLV_POLICY_ORDER                                   2       // u_int32_t
138 #define NECP_TLV_POLICY_CONDITION                               3
139 #define NECP_TLV_POLICY_RESULT                                  4
140 #define NECP_TLV_POLICY_ID                                              5       // u_int32_t
141 #define NECP_TLV_SESSION_PRIORITY                               6       // u_int32_t
142 #define NECP_TLV_ATTRIBUTE_DOMAIN                               7       // char[]
143 #define NECP_TLV_ATTRIBUTE_ACCOUNT                              8       // char[]
144 #define NECP_TLV_SERVICE_UUID                                   9       // uuid_t
145 #define NECP_TLV_ROUTE_RULE                                             10
146 #define NECP_TLV_ATTRIBUTE_DOMAIN_OWNER                        11       // char[]
147 #define NECP_TLV_ATTRIBUTE_TRACKER_DOMAIN                      12       // char[]
148 #define NECP_TLV_ATTRIBUTE_DOMAIN_CONTEXT                      13       // char[]
149 
150 /*
151  * Control message TLV sent only by the kernel to userspace
152  */
153 #define NECP_TLV_POLICY_OWNER                                   100     // char []
154 #define NECP_TLV_POLICY_DUMP                                    101
155 #define NECP_TLV_POLICY_RESULT_STRING                   102     // char []
156 #define NECP_TLV_POLICY_SESSION_ORDER                   103     // u_int32_t
157 
158 /*
159  * Condition flags
160  */
161 #define NECP_POLICY_CONDITION_FLAGS_NEGATIVE    0x01 // Negative
162 #define NECP_POLICY_CONDITION_FLAGS_EXACT       0x02 // For conditions that would otherwise have more inclusive matching, require an exact match
163 
164 /*
165  * Added support for dumping negative conditions
166  */
167 #define NECP_POLICY_CONDITION_FLAGS_NEGATIVE_SUPPORTS_DUMP    1
168 
169 /*
170  * Conditions
171  * Used for setting policies as well as passing parameters to necp_match_policy.
172  */
173 #define NECP_POLICY_CONDITION_DEFAULT                   0       // N/A, not valid with any other conditions
174 // Socket/Application conditions
175 #define NECP_POLICY_CONDITION_APPLICATION               1       // uuid_t, uses effective UUID when possible
176 #define NECP_POLICY_CONDITION_REAL_APPLICATION  2       // uuid_t, never uses effective UUID. Only valid with NECP_POLICY_CONDITION_APPLICATION
177 #define NECP_POLICY_CONDITION_DOMAIN                    3       // String, such as apple.com
178 #define NECP_POLICY_CONDITION_ACCOUNT                   4       // String
179 #define NECP_POLICY_CONDITION_ENTITLEMENT               5       // String
180 #define NECP_POLICY_CONDITION_PID                               6       // pid_t
181 #define NECP_POLICY_CONDITION_UID                               7       // uid_t
182 #define NECP_POLICY_CONDITION_ALL_INTERFACES    8       // N/A
183 #define NECP_POLICY_CONDITION_BOUND_INTERFACE   9       // String
184 #define NECP_POLICY_CONDITION_TRAFFIC_CLASS             10      // necp_policy_condition_tc_range
185 // Socket/IP conditions
186 #define NECP_POLICY_CONDITION_IP_PROTOCOL               11      // u_int16_t
187 #define NECP_POLICY_CONDITION_LOCAL_ADDR                12      // necp_policy_condition_addr
188 #define NECP_POLICY_CONDITION_REMOTE_ADDR               13      // necp_policy_condition_addr
189 #define NECP_POLICY_CONDITION_LOCAL_ADDR_RANGE  14      // necp_policy_condition_addr_range
190 #define NECP_POLICY_CONDITION_REMOTE_ADDR_RANGE 15      // necp_policy_condition_addr_range
191 #define NECP_POLICY_CONDITION_AGENT_TYPE                16      // struct necp_policy_condition_agent_type
192 #define NECP_POLICY_CONDITION_HAS_CLIENT                17      // N/A
193 #define NECP_POLICY_CONDITION_LOCAL_NETWORKS            18      // Matches all local networks
194 // Socket-only conditions
195 #define NECP_POLICY_CONDITION_FLOW_IP_PROTOCOL          19      // u_int16_t
196 #define NECP_POLICY_CONDITION_FLOW_LOCAL_ADDR           20      // necp_policy_condition_addr
197 #define NECP_POLICY_CONDITION_FLOW_REMOTE_ADDR          21      // necp_policy_condition_addr
198 #define NECP_POLICY_CONDITION_FLOW_LOCAL_ADDR_RANGE     22      // necp_policy_condition_addr_range
199 #define NECP_POLICY_CONDITION_FLOW_REMOTE_ADDR_RANGE    23      // necp_policy_condition_addr_range
200 #define NECP_POLICY_CONDITION_FLOW_IS_LOOPBACK          31      // N/A
201 // Socket/Application conditions, continued
202 #define NECP_POLICY_CONDITION_CLIENT_FLAGS              24      // u_int32_t, values from NECP_CLIENT_PARAMETER_FLAG_*
203 #define NECP_POLICY_CONDITION_FLOW_LOCAL_ADDR_EMPTY     25      // N/A
204 #define NECP_POLICY_CONDITION_FLOW_REMOTE_ADDR_EMPTY    26      // N/A
205 #define NECP_POLICY_CONDITION_PLATFORM_BINARY           27      // N/A
206 #define NECP_POLICY_CONDITION_SDK_VERSION               28      // struct necp_policy_condition_sdk_version
207 #define NECP_POLICY_CONDITION_SIGNING_IDENTIFIER        29      // String
208 #define NECP_POLICY_CONDITION_PACKET_FILTER_TAGS        30      // u_int16_t
209 #define NECP_POLICY_CONDITION_DELEGATE_IS_PLATFORM_BINARY      32      // N/A
210 #define NECP_POLICY_CONDITION_DOMAIN_OWNER              33      // String, owner of domain (for example, "Google Inc")
211 #define NECP_POLICY_CONDITION_DOMAIN_CONTEXT            34      // String, eTLD+1 leading to networking activities to the domain
212 #define NECP_POLICY_CONDITION_TRACKER_DOMAIN            35      // String, tracker domain
213 #define NECP_POLICY_CONDITION_ATTRIBUTED_BUNDLE_IDENTIFIER 36   // String, app to which traffic is attributed to
214 #define NECP_POLICY_CONDITION_SCHEME_PORT               37      // u_int16_t, the port associated with the scheme for a connection
215 #define NECP_POLICY_CONDITION_DOMAIN_FILTER             38      // struct net_bloom_filter
216 #define NECP_POLICY_CONDITION_SYSTEM_SIGNED_RESULT      39      // N/A
217 #define NECP_POLICY_CONDITION_REAL_UID                  40      // uid_t
218 #define NECP_POLICY_CONDITION_APPLICATION_ID            41      // necp_application_id_t
219 #define NECP_POLICY_CONDITION_URL                       42      // String, URL
220 #define NECP_POLICY_CONDITION_BOUND_INTERFACE_FLAGS     43      // Interface flags: u_int32_t flags, u_int32_t eflags, u_int32_t xflags
221 
222 
223 /*
224  * Policy Condition Bound Interface Flags Order
225  */
226 #define NECP_POLICY_CONDITION_BOUND_INTERFACE_FLAGS_IDX_FLAGS      0
227 #define NECP_POLICY_CONDITION_BOUND_INTERFACE_FLAGS_IDX_EFLAGS     1
228 #define NECP_POLICY_CONDITION_BOUND_INTERFACE_FLAGS_IDX_XFLAGS     2
229 #define NECP_POLICY_CONDITION_BOUND_INTERFACE_FLAGS_IDX_MAX        3
230 
231 /*
232  * Policy Packet tags
233  */
234 #define NECP_POLICY_CONDITION_PACKET_FILTER_TAG_STACK_DROP         0x01
235 #define NECP_POLICY_CONDITION_PACKET_FILTER_TAG_MAX                NECP_POLICY_CONDITION_PACKET_FILTER_TAG_STACK_DROP
236 
237 /*
238  * Results
239  */
240 #define NECP_POLICY_RESULT_PASS                                 1       // N/A
241 #define NECP_POLICY_RESULT_SKIP                                 2       // u_int32_t, policy order to skip to. 0 to skip all session policies.
242 #define NECP_POLICY_RESULT_DROP                                 3       // N/A
243 #define NECP_POLICY_RESULT_SOCKET_DIVERT                4       // u_int32_t, flow divert control unit
244 #define NECP_POLICY_RESULT_SOCKET_FILTER                5       // u_int32_t, filter control unit
245 #define NECP_POLICY_RESULT_IP_TUNNEL                    6       // String, interface name
246 #define NECP_POLICY_RESULT_IP_FILTER                    7       // ?
247 #define NECP_POLICY_RESULT_TRIGGER                              8       // Deprecated
248 #define NECP_POLICY_RESULT_TRIGGER_IF_NEEDED    9       // Deprecated
249 #define NECP_POLICY_RESULT_TRIGGER_SCOPED               10      // Deprecated
250 #define NECP_POLICY_RESULT_NO_TRIGGER_SCOPED    11      // Deprecated
251 #define NECP_POLICY_RESULT_SOCKET_SCOPED                12      // String, interface name
252 #define NECP_POLICY_RESULT_ROUTE_RULES                  13      // N/A, must have route rules defined
253 #define NECP_POLICY_RESULT_USE_NETAGENT                 14      // netagent uuid_t
254 #define NECP_POLICY_RESULT_NETAGENT_SCOPED              15      // netagent uuid_t
255 #define NECP_POLICY_RESULT_SCOPED_DIRECT                16      // N/A, scopes to primary physical interface
256 #define NECP_POLICY_RESULT_ALLOW_UNENTITLED             17      // N/A
257 #define NECP_POLICY_RESULT_REMOVE_NETAGENT              18      // netagent uuid_t
258 #define NECP_POLICY_RESULT_REMOVE_NETAGENT_TYPE         19      // necp_policy_condition_agent_type
259 
260 #define NECP_POLICY_RESULT_MAX                          NECP_POLICY_RESULT_REMOVE_NETAGENT_TYPE
261 
262 /*
263  * PASS Result Flags
264  */
265 #define NECP_POLICY_PASS_NO_SKIP_IPSEC                  0x01
266 #define NECP_POLICY_PASS_PF_TAG                         0x02
267 
268 /*
269  * DROP Result Flags
270  */
271 #define NECP_POLICY_DROP_FLAG_LOCAL_NETWORK             0x01
272 #define NECP_POLICY_DROP_FLAG_SUPPRESS_ALERTS           0x02
273 
274 /*
275  * Local-Networks Condition Flags
276  */
277 #define NECP_POLICY_LOCAL_NETWORKS_FLAG_INCLUDE_LOCAL_ADDRESSES 0x01     // Include addresses on local interfaces for Local-Networks condition
278 
279 /*
280  * Route Rules
281  * Detailed parameters for NECP_POLICY_RESULT_ROUTE_RULES.
282  */
283 #define NECP_ROUTE_RULE_NONE                                    0       // N/A
284 #define NECP_ROUTE_RULE_DENY_INTERFACE                  1       // String, or empty to match all
285 #define NECP_ROUTE_RULE_ALLOW_INTERFACE                 2       // String, or empty to match all
286 #define NECP_ROUTE_RULE_QOS_MARKING                             3       // String, or empty to match all
287 #define NECP_ROUTE_RULE_DENY_LQM_ABORT                  4       // String, or empty to match all
288 #define NECP_ROUTE_RULE_USE_NETAGENT                    5       // UUID, followed by string or empty
289 #define NECP_ROUTE_RULE_REMOVE_NETAGENT                 6       // UUID, followed by string or empty
290 #define NECP_ROUTE_RULE_DIVERT_SOCKET                   7       // u_int32_t control unit, followed by string or empty
291 #define NECP_ROUTE_RULE_DENY_INTERFACE_WITH_TYPE        8       // u_int32_t effective type, followed by string or empty
292 
293 #define NECP_ROUTE_RULE_FLAG_CELLULAR                   0x01
294 #define NECP_ROUTE_RULE_FLAG_WIFI                       0x02
295 #define NECP_ROUTE_RULE_FLAG_WIRED                      0x04
296 #define NECP_ROUTE_RULE_FLAG_EXPENSIVE                  0x08
297 #define NECP_ROUTE_RULE_FLAG_CONSTRAINED                0x10
298 #define NECP_ROUTE_RULE_FLAG_COMPANION                  0x20
299 #define NECP_ROUTE_RULE_FLAG_VPN                        0x40
300 #define NECP_ROUTE_RULE_FLAG_ULTRA_CONSTRAINED          0x90 // Note that this includes the 0x80 bit, so cannot be combined with agent UUID matching.
301 
302 #define NECP_ROUTE_RULE_FLAG_NETAGENT                   0x80 // Last bit, reserved to mark that this applies only when an agent UUID is present
303 
304 #define NECP_ROUTE_RULES_SUPPORT_NETAGENT_EXCEPTIONS    1
305 
306 /*
307  * Error types
308  */
309 #define NECP_ERROR_INTERNAL                                             0
310 #define NECP_ERROR_UNKNOWN_PACKET_TYPE                  1
311 #define NECP_ERROR_INVALID_TLV                                  2
312 #define NECP_ERROR_POLICY_RESULT_INVALID                3
313 #define NECP_ERROR_POLICY_CONDITIONS_INVALID    4
314 #define NECP_ERROR_POLICY_ID_NOT_FOUND                  5
315 #define NECP_ERROR_INVALID_PROCESS                              6
316 #define NECP_ERROR_ROUTE_RULES_INVALID                  7
317 
318 // Modifiers
319 #define NECP_MASK_USERSPACE_ONLY        0x80000000      // on filter_control_unit value
320 #define NECP_MASK_PRESERVE_CONNECTIONS  0x20000000      // on filter_control_unit value
321 
322 struct necp_policy_condition_tc_range {
323 	u_int32_t start_tc;
324 	u_int32_t end_tc;
325 } __attribute__((__packed__));
326 
327 struct necp_policy_condition_addr {
328 	u_int8_t                prefix;
329 	union {
330 		struct sockaddr                 sa;
331 		struct sockaddr_in              sin;
332 		struct sockaddr_in6             sin6;
333 	} address __attribute__((__packed__));
334 } __attribute__((__packed__));
335 
336 struct necp_policy_condition_addr_range {
337 	union {
338 		struct sockaddr                 sa;
339 		struct sockaddr_in              sin;
340 		struct sockaddr_in6             sin6;
341 	} start_address __attribute__((__packed__));
342 	union {
343 		struct sockaddr                 sa;
344 		struct sockaddr_in              sin;
345 		struct sockaddr_in6             sin6;
346 	} end_address __attribute__((__packed__));
347 } __attribute__((__packed__));
348 
349 struct necp_policy_condition_agent_type {
350 	char agent_domain[32];
351 	char agent_type[32];
352 } __attribute__((__packed__));
353 
354 struct necp_policy_condition_sdk_version {
355 	uint32_t platform; // e.g., PLATFORM_IOS
356 	uint32_t min_version; // Encoded as XXXX.YY.ZZ
357 	uint32_t version; // Encoded as XXXX.YY.ZZ
358 } __attribute__((__packed__));
359 
360 #define NECP_SESSION_PRIORITY_UNKNOWN                   0
361 #define NECP_SESSION_PRIORITY_CONTROL                   1
362 #define NECP_SESSION_PRIORITY_CONTROL_1                 2
363 #define NECP_SESSION_PRIORITY_PRIVILEGED_TUNNEL         3
364 #define NECP_SESSION_PRIORITY_HIGH                      4
365 #define NECP_SESSION_PRIORITY_HIGH_1                    5
366 #define NECP_SESSION_PRIORITY_HIGH_2                    6
367 #define NECP_SESSION_PRIORITY_HIGH_3                    7
368 #define NECP_SESSION_PRIORITY_HIGH_4                    8
369 #define NECP_SESSION_PRIORITY_HIGH_RESTRICTED           9
370 #define NECP_SESSION_PRIORITY_DEFAULT                   10
371 #define NECP_SESSION_PRIORITY_LOW                       11
372 #define NECP_SESSION_NUM_PRIORITIES                             NECP_SESSION_PRIORITY_LOW
373 
374 typedef u_int32_t necp_policy_id;
375 typedef u_int32_t necp_policy_order;
376 typedef u_int32_t necp_session_priority;
377 
378 typedef u_int32_t necp_kernel_policy_result;
379 typedef u_int32_t necp_kernel_policy_filter;
380 
381 typedef union {
382 	u_int                                           tunnel_interface_index;
383 	u_int                                           scoped_interface_index;
384 	u_int32_t                                       flow_divert_control_unit;
385 	u_int32_t                                       filter_control_unit;
386 	u_int32_t                                       pass_flags;
387 	u_int32_t                                       drop_flags;
388 } necp_kernel_policy_routing_result_parameter;
389 
390 #define NECP_SERVICE_FLAGS_REGISTERED                   0x01
391 #define NECP_MAX_NETAGENTS                                              16
392 #define NECP_MAX_REMOVE_NETAGENT_TYPES                                  4
393 
394 #define NECP_AGENT_USE_FLAG_SCOPE                               0x01
395 #define NECP_AGENT_USE_FLAG_REMOVE                              0x02
396 
397 #define NECP_TFO_COOKIE_LEN_MAX      16
398 struct necp_aggregate_result {
399 	necp_kernel_policy_result                       routing_result;
400 	necp_kernel_policy_routing_result_parameter     routing_result_parameter;
401 	necp_kernel_policy_filter                       filter_control_unit;
402 	u_int32_t                                       flow_divert_aggregate_unit;
403 	u_int                                                           routed_interface_index;
404 	u_int32_t                                                       policy_id;
405 	u_int32_t                                                       skip_policy_id;
406 	uuid_t                                                          netagents[NECP_MAX_NETAGENTS];
407 	u_int32_t                                                       netagent_use_flags[NECP_MAX_NETAGENTS];
408 	struct necp_policy_condition_agent_type                         remove_netagent_types[NECP_MAX_REMOVE_NETAGENT_TYPES];
409 	struct ipv6_prefix                                              nat64_prefixes[NAT64_MAX_NUM_PREFIXES];
410 	u_int8_t                                                        mss_recommended;
411 };
412 
413 /*
414  * Statistics.  It would be nice if the definitions in ntstat.h could be used,
415  * but they get entangled with #defines for v4 etc in pfvar.h and it may be better practice
416  * to have separate definitions here.
417  */
418 struct necp_stat_counts {
419 	/*	Counters	*/
420 	u_int64_t       necp_stat_rxpackets             __attribute__((aligned(8)));
421 	u_int64_t       necp_stat_rxbytes               __attribute__((aligned(8)));
422 	u_int64_t       necp_stat_txpackets             __attribute__((aligned(8)));
423 	u_int64_t       necp_stat_txbytes               __attribute__((aligned(8)));
424 
425 	u_int32_t       necp_stat_rxduplicatebytes;
426 	u_int32_t       necp_stat_rxoutoforderbytes;
427 	u_int32_t       necp_stat_txretransmit;
428 
429 	u_int32_t       necp_stat_connectattempts;
430 	u_int32_t       necp_stat_connectsuccesses;
431 
432 	u_int32_t       necp_stat_min_rtt;
433 	u_int32_t       necp_stat_avg_rtt;
434 	u_int32_t       necp_stat_var_rtt;
435 
436 #define NECP_STAT_ROUTE_FLAGS   1
437 	u_int32_t       necp_stat_route_flags;
438 };
439 
440 // Note, some metadata is implicit in the necp client itself:
441 // From the process itself : pid, upid, uuid, proc name.
442 // From the necp client parameters: local and remote addresses, euuid, traffic class, ifindex
443 //
444 // The following may well be supplied via future necp client parameters,
445 // but they are here so they don't get forgotten.
446 struct necp_basic_metadata {
447 	u_int32_t       rcvbufsize;
448 	u_int32_t       rcvbufused;
449 };
450 
451 struct necp_connection_probe_status {
452 	unsigned int    probe_activated : 1;
453 	unsigned int    write_probe_failed : 1;
454 	unsigned int    read_probe_failed : 1;
455 	unsigned int    conn_probe_failed : 1;
456 };
457 
458 struct necp_extra_tcp_metadata {
459 	struct necp_connection_probe_status probestatus;
460 
461 	u_int32_t       sndbufsize;
462 	u_int32_t       sndbufused;
463 	u_int32_t       txunacked;
464 	u_int32_t       txwindow;
465 	u_int32_t       txcwindow;
466 	u_int32_t       flags;                  // use SOF_*
467 	u_int32_t       flags1;                 // use SOF1_*
468 	u_int32_t       traffic_mgt_flags;
469 	u_int32_t       cc_alg_index;
470 	u_int32_t       state;
471 };
472 
473 struct necp_stats_hdr {
474 	u_int32_t                                       necp_stats_type __attribute__((aligned(8)));
475 	u_int32_t                                       necp_stats_ver;
476 	u_int64_t                                       __necp_stats_reserved; // Pad the field for future use
477 };
478 
479 #define NECP_CLIENT_STATISTICS_TYPE_TCP                         1       // Identifies use of necp_tcp_stats
480 #define NECP_CLIENT_STATISTICS_TYPE_UDP                         2       // Identifies use of necp_udp_stats
481 #define NECP_CLIENT_STATISTICS_TYPE_QUIC                        3       // Identifies use of necp_quic_stats
482 
483 #define NECP_CLIENT_STATISTICS_TYPE_TCP_VER_1           1       // Currently supported version for TCP
484 #define NECP_CLIENT_STATISTICS_TYPE_UDP_VER_1           1       // Currently supported version for UDP
485 #define NECP_CLIENT_STATISTICS_TYPE_QUIC_VER_1          1       // Currently supported version for QUIC
486 
487 #define NECP_CLIENT_STATISTICS_TYPE_TCP_CURRENT_VER             NECP_CLIENT_STATISTICS_TYPE_TCP_VER_1
488 #define NECP_CLIENT_STATISTICS_TYPE_UDP_CURRENT_VER             NECP_CLIENT_STATISTICS_TYPE_UDP_VER_1
489 #define NECP_CLIENT_STATISTICS_TYPE_QUIC_CURRENT_VER            NECP_CLIENT_STATISTICS_TYPE_QUIC_VER_1
490 
491 #define NECP_CLIENT_STATISTICS_EVENT_INIT                       0x00000000              // Register the flow
492 #define NECP_CLIENT_STATISTICS_EVENT_TIME_WAIT          0x00000001              // The flow is effectively finished but waiting on timer
493 
494 struct necp_tcp_stats {
495 	struct necp_stats_hdr                   necp_tcp_hdr;
496 	struct necp_stat_counts                 necp_tcp_counts;
497 	struct necp_basic_metadata              necp_tcp_basic;
498 	struct necp_extra_tcp_metadata  necp_tcp_extra;
499 };
500 
501 struct necp_udp_stats {
502 	struct necp_stats_hdr           necp_udp_hdr;
503 	struct necp_stat_counts         necp_udp_counts;
504 	struct necp_basic_metadata      necp_udp_basic;
505 };
506 
507 
508 /*
509  * The following reflects the special case for QUIC.
510  * It is a streaming protocol built on top of UDP.
511  * Therefore QUIC stats are defined as basic UDP stats
512  * with some extra meta data.
513  * TODO: For now the extra metadata is an exact replica
514  * of the metadata for TCP. However keeping that separate allows
515  * the structures to diverge later as new stats are added.
516  */
517 #define QUIC_STATELESS_RESET_TOKEN_SIZE               16
518 #define NECP_QUIC_HAS_FALLBACK 1
519 
520 struct necp_extra_quic_metadata {
521 	u_int32_t       sndbufsize;
522 	u_int32_t       sndbufused;
523 	u_int32_t       txunacked;
524 	u_int32_t       txwindow;
525 	u_int32_t       txcwindow;
526 	u_int32_t       traffic_mgt_flags;
527 	u_int32_t       cc_alg_index;
528 	u_int32_t       state;
529 	u_int32_t       fallback : 1,
530 	    unused : 31;
531 	u_int8_t        ssr_token[QUIC_STATELESS_RESET_TOKEN_SIZE];
532 	struct necp_connection_probe_status probestatus;
533 };
534 
535 #define necp_quic_hdr           necp_quic_udp_stats.necp_udp_hdr
536 #define necp_quic_counts        necp_quic_udp_stats.necp_udp_counts
537 #define necp_quic_basic         necp_quic_udp_stats.necp_udp_basic
538 struct necp_quic_stats {
539 	struct necp_udp_stats           necp_quic_udp_stats;
540 	struct necp_extra_quic_metadata necp_quic_extra;
541 };
542 
543 typedef struct necp_all_stats {
544 	union {
545 		struct necp_tcp_stats   tcp_stats;
546 		struct necp_udp_stats   udp_stats;
547 		struct necp_quic_stats  quic_stats;
548 	} all_stats_u;
549 } necp_all_stats;
550 
551 // Memory for statistics is requested via a necp_stats_bufreq
552 //
553 struct necp_stats_bufreq {
554 	u_int32_t                                       necp_stats_bufreq_id __attribute__((aligned(8)));
555 	u_int32_t                                       necp_stats_bufreq_type;         //  NECP_CLIENT_STATISTICS_TYPE_*
556 	u_int32_t                                       necp_stats_bufreq_ver;          //  NECP_CLIENT_STATISTICS_TYPE_*_VER
557 	u_int32_t                                       necp_stats_bufreq_size;
558 	union {
559 		void                                    *necp_stats_bufreq_addr;
560 		mach_vm_address_t               necp_stats_bufreq_uaddr;
561 	};
562 };
563 
564 #define NECP_CLIENT_STATISTICS_BUFREQ_ID                                0xbf    // Distinguishes from statistics actions taking a necp_all_stats struct
565 
566 // There is a limit to the number of statistics structures that may be allocated per process, subject to change
567 //
568 #define NECP_MAX_PER_PROCESS_CLIENT_STATISTICS_STRUCTS  512
569 
570 #define NECP_TCP_ECN_HEURISTICS_SYN_RST 1
571 typedef struct necp_tcp_ecn_cache {
572 	u_int8_t                necp_tcp_ecn_heuristics_success:1;
573 	u_int8_t                necp_tcp_ecn_heuristics_loss:1;
574 	u_int8_t                necp_tcp_ecn_heuristics_drop_rst:1;
575 	u_int8_t                necp_tcp_ecn_heuristics_drop_rxmt:1;
576 	u_int8_t                necp_tcp_ecn_heuristics_aggressive:1;
577 	u_int8_t                necp_tcp_ecn_heuristics_syn_rst:1;
578 } necp_tcp_ecn_cache;
579 
580 #define NECP_TCP_TFO_HEURISTICS_RST 1
581 typedef struct necp_tcp_tfo_cache {
582 	u_int8_t                necp_tcp_tfo_cookie[NECP_TFO_COOKIE_LEN_MAX];
583 	u_int8_t                necp_tcp_tfo_cookie_len;
584 	u_int8_t                necp_tcp_tfo_heuristics_success:1; // TFO succeeded with data in the SYN
585 	u_int8_t                necp_tcp_tfo_heuristics_loss:1; // TFO SYN-loss with data
586 	u_int8_t                necp_tcp_tfo_heuristics_middlebox:1; // TFO middlebox detected
587 	u_int8_t                necp_tcp_tfo_heuristics_success_req:1; // TFO succeeded with the TFO-option in the SYN
588 	u_int8_t                necp_tcp_tfo_heuristics_loss_req:1; // TFO SYN-loss with the TFO-option
589 	u_int8_t                necp_tcp_tfo_heuristics_rst_data:1; // Recevied RST upon SYN with data in the SYN
590 	u_int8_t                necp_tcp_tfo_heuristics_rst_req:1; // Received RST upon SYN with the TFO-option
591 } necp_tcp_tfo_cache;
592 
593 #define NECP_CLIENT_CACHE_TYPE_ECN                 1       // Identifies use of necp_tcp_ecn_cache
594 #define NECP_CLIENT_CACHE_TYPE_TFO                 2       // Identifies use of necp_tcp_tfo_cache
595 
596 #define NECP_CLIENT_CACHE_TYPE_ECN_VER_1           1       // Currently supported version for ECN
597 #define NECP_CLIENT_CACHE_TYPE_TFO_VER_1           1       // Currently supported version for TFO
598 
599 typedef struct necp_cache_buffer {
600 	u_int8_t                necp_cache_buf_type;    //  NECP_CLIENT_CACHE_TYPE_*
601 	u_int8_t                necp_cache_buf_ver;     //  NECP_CLIENT_CACHE_TYPE_*_VER
602 	u_int32_t               necp_cache_buf_size;
603 	mach_vm_address_t       necp_cache_buf_addr;
604 } necp_cache_buffer;
605 
606 /*
607  * NECP Client definitions
608  */
609 #define NECP_MAX_CLIENT_PARAMETERS_SIZE                                 1024
610 #define NECP_MAX_CLIENT_RESULT_SIZE                                     512 // Legacy
611 #define NECP_BASE_CLIENT_RESULT_SIZE                                    1024
612 #define NECP_CLIENT_FLOW_RESULT_SIZE                                    512
613 
614 #define NECP_OPEN_FLAG_OBSERVER                                                 0x01 // Observers can query clients they don't own
615 #define NECP_OPEN_FLAG_BACKGROUND                                               0x02 // Mark this fd as backgrounded
616 #define NECP_OPEN_FLAG_PUSH_OBSERVER                                    0x04 // When used with the OBSERVER flag, allows updates to be pushed. Adding clients is not allowed in this mode.
617 
618 #define NECP_FD_SUPPORTS_GUARD                                                  1
619 
620 #define NECP_CLIENT_ACTION_ADD                                                  1 // Register a new client. Input: parameters in buffer; Output: client_id
621 #define NECP_CLIENT_ACTION_REMOVE                                               2 // Unregister a client. Input: client_id, optional struct ifnet_stats_per_flow
622 #define NECP_CLIENT_ACTION_COPY_PARAMETERS                              3 // Copy client parameters. Input: client_id; Output: parameters in buffer
623 #define NECP_CLIENT_ACTION_COPY_RESULT                                  4 // Copy client result. Input: client_id; Output: result in buffer
624 #define NECP_CLIENT_ACTION_COPY_LIST                                    5 // Copy all client IDs. Output: struct necp_client_list in buffer
625 #define NECP_CLIENT_ACTION_REQUEST_NEXUS_INSTANCE               6 // Request a nexus instance from a nexus provider, optional struct necp_stats_bufreq
626 #define NECP_CLIENT_ACTION_AGENT                                                7 // Interact with agent. Input: client_id, agent parameters
627 #define NECP_CLIENT_ACTION_COPY_AGENT                                   8 // Copy agent content. Input: agent UUID; Output: struct netagent
628 #define NECP_CLIENT_ACTION_COPY_INTERFACE                               9 // Copy interface details. Input: ifindex cast to UUID; Output: struct necp_interface_details
629 #define NECP_CLIENT_ACTION_SET_STATISTICS                               10 // Deprecated
630 #define NECP_CLIENT_ACTION_COPY_ROUTE_STATISTICS                11 // Get route statistics. Input: client_id; Output: struct necp_stat_counts
631 #define NECP_CLIENT_ACTION_AGENT_USE                                    12 // Return the use count and increment the use count. Input/Output: struct necp_agent_use_parameters
632 #define NECP_CLIENT_ACTION_MAP_SYSCTLS                                  13 // Get the read-only sysctls memory location. Output: mach_vm_address_t
633 #define NECP_CLIENT_ACTION_UPDATE_CACHE                                 14 // Update heuristics and cache
634 #define NECP_CLIENT_ACTION_COPY_CLIENT_UPDATE                   15 // Fetch an updated client for push-mode observer. Output: Client id, struct necp_client_observer_update in buffer
635 #define NECP_CLIENT_ACTION_COPY_UPDATED_RESULT                  16 // Copy client result only if changed. Input: client_id; Output: result in buffer
636 #define NECP_CLIENT_ACTION_ADD_FLOW                                             17 // Add a flow. Input: client_id; Output: struct necp_client_add_flow
637 #define NECP_CLIENT_ACTION_REMOVE_FLOW                                  18 // Remove a flow. Input: flow_id, optional struct ifnet_stats_per_flow
638 #define NECP_CLIENT_ACTION_CLAIM                                       19 // Claim a client that has been added for this unique PID. Input: client_id
639 #define NECP_CLIENT_ACTION_SIGN                                       20 // Sign a query answer. Input: struct necp_client_signable; Output: struct necp_client_signature
640 #define NECP_CLIENT_ACTION_GET_INTERFACE_ADDRESS                       21 // Get the best interface local address for given remote address. Input: ifindex, remote sockaddr; Output: matching local sockaddr
641 #define NECP_CLIENT_ACTION_ACQUIRE_AGENT_TOKEN                         22 // Get a one-time use token from an agent. Input: agent UUID; Output: token buffer
642 #define NECP_CLIENT_ACTION_VALIDATE                                    23 // Validate a query answer. Input: struct necp_client_validatable; Output: None
643 #define NECP_CLIENT_ACTION_GET_SIGNED_CLIENT_ID                        24 // Get a client ID for the appliction along with a signature.
644 #define NECP_CLIENT_ACTION_SET_SIGNED_CLIENT_ID                        25 // Set a client ID for the appliction along with a signature.
645 #define NECP_CLIENT_ACTION_COPY_UPDATED_RESULT_FINAL                   26 // Copy client result only if changed, discard data if buffer is too small. Input: client_id; Output: result in buffer
646 #define NECP_CLIENT_ACTION_GET_FLOW_STATISTICS                         27 // Get flow statistics for aop flow
647 
648 #define NECP_CLIENT_PARAMETER_APPLICATION                               NECP_POLICY_CONDITION_APPLICATION               // Requires entitlement
649 #define NECP_CLIENT_PARAMETER_REAL_APPLICATION                  NECP_POLICY_CONDITION_REAL_APPLICATION  // Requires entitlement
650 #define NECP_CLIENT_PARAMETER_DOMAIN                                    NECP_POLICY_CONDITION_DOMAIN
651 #define NECP_CLIENT_PARAMETER_DOMAIN_OWNER                              NECP_POLICY_CONDITION_DOMAIN_OWNER
652 #define NECP_CLIENT_PARAMETER_DOMAIN_CONTEXT                            NECP_POLICY_CONDITION_DOMAIN_CONTEXT
653 #define NECP_CLIENT_PARAMETER_TRACKER_DOMAIN                            NECP_POLICY_CONDITION_TRACKER_DOMAIN
654 #define NECP_CLIENT_PARAMETER_URL                                       NECP_POLICY_CONDITION_URL
655 #define NECP_CLIENT_PARAMETER_ATTRIBUTED_BUNDLE_IDENTIFIER              NECP_POLICY_CONDITION_ATTRIBUTED_BUNDLE_IDENTIFIER
656 #define NECP_CLIENT_PARAMETER_ACCOUNT                                   NECP_POLICY_CONDITION_ACCOUNT
657 #define NECP_CLIENT_PARAMETER_PID                                               NECP_POLICY_CONDITION_PID                               // Requires entitlement
658 #define NECP_CLIENT_PARAMETER_UID                                               NECP_POLICY_CONDITION_UID                               // Requires entitlement
659 #define NECP_CLIENT_PARAMETER_BOUND_INTERFACE                   NECP_POLICY_CONDITION_BOUND_INTERFACE
660 #define NECP_CLIENT_PARAMETER_TRAFFIC_CLASS                             NECP_POLICY_CONDITION_TRAFFIC_CLASS
661 #define NECP_CLIENT_PARAMETER_IP_PROTOCOL                               NECP_POLICY_CONDITION_IP_PROTOCOL
662 #define NECP_CLIENT_PARAMETER_LOCAL_ADDRESS                             NECP_POLICY_CONDITION_LOCAL_ADDR
663 #define NECP_CLIENT_PARAMETER_REMOTE_ADDRESS                    NECP_POLICY_CONDITION_REMOTE_ADDR
664 #define NECP_CLIENT_PARAMETER_SCHEME_PORT                                               NECP_POLICY_CONDITION_SCHEME_PORT
665 #define NECP_CLIENT_PARAMETER_APPLICATION_ID                            NECP_POLICY_CONDITION_APPLICATION_ID
666 #define NECP_CLIENT_PARAMETER_NEXUS_KEY                                 102
667 
668 // "Prohibit" will never choose an interface with that property
669 #define NECP_CLIENT_PARAMETER_PROHIBIT_INTERFACE                100             // String, interface name
670 #define NECP_CLIENT_PARAMETER_PROHIBIT_IF_TYPE                  101             // u_int8_t, see ifru_functional_type in <net/if.h>
671 #define NECP_CLIENT_PARAMETER_PROHIBIT_AGENT                    102             // uuid_t, network agent UUID
672 #define NECP_CLIENT_PARAMETER_PROHIBIT_AGENT_TYPE               103             // struct necp_client_parameter_netagent_type
673 
674 // "Require" will choose an interface with that property, or none if not found
675 #define NECP_CLIENT_PARAMETER_REQUIRE_IF_TYPE                   111             // u_int8_t, see ifru_functional_type in <net/if.h>
676 #define NECP_CLIENT_PARAMETER_REQUIRE_AGENT                             112             // uuid_t, network agent UUID
677 #define NECP_CLIENT_PARAMETER_REQUIRE_AGENT_TYPE                113             // struct necp_client_parameter_netagent_type
678 
679 // "Prefer" will choose an interface with an agent, or best otherwise if not found
680 #define NECP_CLIENT_PARAMETER_PREFER_AGENT                              122             // uuid_t, network agent UUID
681 #define NECP_CLIENT_PARAMETER_PREFER_AGENT_TYPE                 123             // struct necp_client_parameter_netagent_type
682 
683 // "Avoid" will choose an interface without an agent, or best otherwise if unavoidable
684 #define NECP_CLIENT_PARAMETER_AVOID_AGENT                               124             // uuid_t, network agent UUID
685 #define NECP_CLIENT_PARAMETER_AVOID_AGENT_TYPE                  125             // struct necp_client_parameter_netagent_type
686 
687 // Use actions with NECP_CLIENT_ACTION_AGENT
688 #define NECP_CLIENT_PARAMETER_TRIGGER_AGENT                             130             // uuid_t, network agent UUID
689 #define NECP_CLIENT_PARAMETER_ASSERT_AGENT                              131             // uuid_t, network agent UUID
690 #define NECP_CLIENT_PARAMETER_UNASSERT_AGENT                    132             // uuid_t, network agent UUID
691 #define NECP_CLIENT_PARAMETER_AGENT_ADD_GROUP_MEMBERS                    133             // struct necp_client_group_action
692 #define NECP_CLIENT_PARAMETER_AGENT_REMOVE_GROUP_MEMBERS                    134             // struct necp_client_group_action
693 #define NECP_CLIENT_PARAMETER_REPORT_AGENT_ERROR                    135             // int32_t
694 
695 #define NECP_CLIENT_PARAMETER_FALLBACK_MODE                     140             // u_int8_t, see SO_FALLBACK_MODE_* values
696 
697 #define NECP_CLIENT_PARAMETER_PARENT_ID                                                 150 // uuid_t, client UUID
698 
699 #define NECP_CLIENT_PARAMETER_LOCAL_ENDPOINT                    200             // struct necp_client_endpoint
700 #define NECP_CLIENT_PARAMETER_REMOTE_ENDPOINT                   201             // struct necp_client_endpoint
701 #define NECP_CLIENT_PARAMETER_BROWSE_DESCRIPTOR                  202             // struct necp_client_endpoint
702 #define NECP_CLIENT_PARAMETER_RESOLVER_TAG                      203                             // struct necp_client_validatable
703 #define NECP_CLIENT_PARAMETER_ADVERTISE_DESCRIPTOR                  204             // struct necp_client_endpoint
704 #define NECP_CLIENT_PARAMETER_GROUP_DESCRIPTOR                  205             // struct necp_client_group
705 
706 #define NECP_CLIENT_PARAMETER_DELEGATED_UPID                              210 // u_int64_t, requires entitlement
707 
708 #define NECP_CLIENT_PARAMETER_ETHERTYPE                              220 // u_int16_t, ethertype
709 #define NECP_CLIENT_PARAMETER_TRANSPORT_PROTOCOL                        221 // u_int8_t, IPPROTO_
710 
711 #define NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE                        230 // u_int8_t, NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_
712 
713 #define NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_DEFAULT                    0
714 #define NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_TEMPORARY                    1
715 #define NECP_CLIENT_PARAMETER_LOCAL_ADDRESS_PREFERENCE_STABLE                    2
716 
717 #define NECP_CLIENT_PARAMETER_PERSONA_ID                                        231 // Used to send persona to agents
718 
719 #define NECP_CLIENT_PARAMETER_FLAGS                                             250             // u_int32_t, see NECP_CLIENT_PAREMETER_FLAG_* values
720 #define NECP_CLIENT_PARAMETER_FLOW_DEMUX_PATTERN                                251 // struct necp_demux_pattern
721 #define NECP_CLIENT_PARAMETER_EXTENDED_FLAGS                                    252 // u_int64_t, see NECP_CLIENT_PARAMETER_EXTENDED_FLAG_* values
722 
723 #define NECP_CLIENT_PARAMETER_FLAG_MULTIPATH                    0x0001  // Get multipath interface results
724 #define NECP_CLIENT_PARAMETER_FLAG_BROWSE                               0x0002  // Agent assertions on nexuses are requests to browse
725 #define NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_EXPENSIVE   0x0004  // Prohibit expensive interfaces
726 #define NECP_CLIENT_PARAMETER_FLAG_LISTENER                             0x0008  // Client is interested in listening for inbound connections
727 #define NECP_CLIENT_PARAMETER_FLAG_DISCRETIONARY                0x0010  // Client's traffic is discretionary, and eligible for early defuncting
728 #define NECP_CLIENT_PARAMETER_FLAG_ECN_ENABLE                   0x0020  // Client is requesting to enable ECN
729 #define NECP_CLIENT_PARAMETER_FLAG_ECN_DISABLE                  0x0040  // Client is requesting to disable ECN
730 #define NECP_CLIENT_PARAMETER_FLAG_TFO_ENABLE                   0x0080  // Client is requesting to enable TFO
731 #define NECP_CLIENT_PARAMETER_FLAG_ONLY_PRIMARY_REQUIRES_TYPE   0x0100    // Interpret NECP_CLIENT_PARAMETER_REQUIRE_IF_TYPE only for primary interface, and allow exceptions for multipath or listeners
732 #define NECP_CLIENT_PARAMETER_FLAG_CUSTOM_ETHER                 0x0200  // Client expects to open a custom ethernet channel
733 #define NECP_CLIENT_PARAMETER_FLAG_CUSTOM_IP                    0x0400  // Client expects to open a custom IP protocol channel
734 #define NECP_CLIENT_PARAMETER_FLAG_INTERPOSE                    0x0800  // Client expects to open an interpose filter channel
735 #define NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_CONSTRAINED       0x1000  // Prohibit constrained interfaces
736 #define NECP_CLIENT_PARAMETER_FLAG_FALLBACK_TRAFFIC           0x2000  // Fallback traffic
737 #define NECP_CLIENT_PARAMETER_FLAG_INBOUND                    0x4000  // Flow is inbound (passive)
738 #define NECP_CLIENT_PARAMETER_FLAG_SYSTEM_PROXY               0x8000  // Flow is a system proxy
739 #define NECP_CLIENT_PARAMETER_FLAG_KNOWN_TRACKER              0x10000  // Flow is contacting a known tracker
740 #define NECP_CLIENT_PARAMETER_FLAG_UNSAFE_SOCKET_ACCESS       0x20000  // Client allows direct access to sockets
741 #define NECP_CLIENT_PARAMETER_FLAG_NON_APP_INITIATED          0x40000  // Networking activities not initiated by application
742 #define NECP_CLIENT_PARAMETER_FLAG_THIRD_PARTY_WEB_CONTENT    0x80000  // Third-party web content, not main load
743 #define NECP_CLIENT_PARAMETER_FLAG_SILENT                    0x100000  // Private browsing - do not log/track
744 #define NECP_CLIENT_PARAMETER_FLAG_APPROVED_APP_DOMAIN       0x200000  // Approved associated app domain; domain is "owned" by app
745 #define NECP_CLIENT_PARAMETER_FLAG_NO_WAKE_FROM_SLEEP        0x400000  // Don't wake from sleep on traffic for this client
746 #define NECP_CLIENT_PARAMETER_FLAG_REUSE_LOCAL               0x800000  // Request support for local address/port reuse
747 #define NECP_CLIENT_PARAMETER_FLAG_ENHANCED_PRIVACY         0x1000000  // Attempt protocol upgrade or proxy usage for more privacy
748 #define NECP_CLIENT_PARAMETER_FLAG_WEB_SEARCH_CONTENT       0x2000000  // Web search traffic
749 #define NECP_CLIENT_PARAMETER_FLAG_ALLOW_ULTRA_CONSTRAINED  0x4000000  // Allow ultra-constrained interfaces
750 #define NECP_CLIENT_PARAMETER_FLAG_HAS_ACCOUNT_ID           0x8000000  // Client has provided an account identifier
751 #define NECP_CLIENT_PARAMETER_FLAG_PREFER_COMPANION        0x10000000  // Client prefers companion proxy for internet access
752 #define NECP_CLIENT_PARAMETER_FLAG_AVOID_COMPANION         0x20000000  // Client avoids companion proxy for internet access, potentially falling back
753 #define NECP_CLIENT_PARAMETER_FLAG_REQUIRE_COMPANION       0x40000000  // Client requires connecting to companion
754 #define NECP_CLIENT_PARAMETER_FLAG_PROHIBIT_COMPANION      0x80000000  // Client prohibits connecting to companion
755 
756 #define NECP_CLIENT_PARAMETER_EXTENDED_FLAG_AOP2_OFFLOAD   0x0000000000000001 // Flow is offloaded to AOP2
757 
758 #define NECP_CLIENT_RESULT_CLIENT_ID                                    1               // uuid_t
759 #define NECP_CLIENT_RESULT_POLICY_RESULT                                2               // u_int32_t
760 #define NECP_CLIENT_RESULT_POLICY_RESULT_PARAMETER              3               // u_int32_t
761 #define NECP_CLIENT_RESULT_FILTER_CONTROL_UNIT                  4               // u_int32_t
762 #define NECP_CLIENT_RESULT_INTERFACE_INDEX                              5               // u_int32_t
763 #define NECP_CLIENT_RESULT_NETAGENT                                             6               // struct necp_client_result_netagent
764 #define NECP_CLIENT_RESULT_FLAGS                                                7               // u_int32_t, see NECP_CLIENT_RESULT_FLAG_* values
765 #define NECP_CLIENT_RESULT_INTERFACE                                    8               // struct necp_client_result_interface
766 #define NECP_CLIENT_RESULT_INTERFACE_OPTION                             9               // struct necp_client_interface_option
767 #define NECP_CLIENT_RESULT_EFFECTIVE_MTU                                10              // u_int32_t
768 #define NECP_CLIENT_RESULT_FLOW                                                 11              // TLV array of a single flow's state
769 #define NECP_CLIENT_RESULT_PROTO_CTL_EVENT                              12
770 #define NECP_CLIENT_RESULT_TFO_COOKIE                                   13              // NECP_TFO_COOKIE_LEN_MAX
771 #define NECP_CLIENT_RESULT_TFO_FLAGS                                    14              // u_int8_t
772 #define NECP_CLIENT_RESULT_RECOMMENDED_MSS                              15              // u_int8_t
773 #define NECP_CLIENT_RESULT_FLOW_ID                                              16              // uuid_t
774 #define NECP_CLIENT_RESULT_INTERFACE_TIME_DELTA                 17              // u_int32_t, seconds since interface up/down
775 #define NECP_CLIENT_RESULT_REASON                                               18              // u_int32_t, see NECP_CLIENT_RESULT_REASON_* values
776 #define NECP_CLIENT_RESULT_FLOW_DIVERT_AGGREGATE_UNIT                   19              // u_int32_t
777 #define NECP_CLIENT_RESULT_REQUEST_IN_PROCESS_FLOW_DIVERT               20              // Empty
778 
779 #define NECP_CLIENT_RESULT_NEXUS_INSTANCE                               100             // uuid_t
780 #define NECP_CLIENT_RESULT_NEXUS_PORT                                   101             // nexus_port_t
781 #define NECP_CLIENT_RESULT_NEXUS_KEY                                    102             // uuid_t
782 #define NECP_CLIENT_RESULT_NEXUS_PORT_FLOW_INDEX                        103             // u_int32_t
783 #define NECP_CLIENT_RESULT_NEXUS_FLOW_STATS                             104             // struct sk_stats_flow *
784 
785 #define NECP_CLIENT_RESULT_LOCAL_ENDPOINT                               200             // struct necp_client_endpoint
786 #define NECP_CLIENT_RESULT_REMOTE_ENDPOINT                              201             // struct necp_client_endpoint
787 #define NECP_CLIENT_RESULT_DISCOVERED_ENDPOINT                  202             // struct necp_client_endpoint, result of browse
788 #define NECP_CLIENT_RESULT_RESOLVED_ENDPOINT                  203             // struct necp_client_endpoint, result of resolve
789 #define NECP_CLIENT_RESULT_LOCAL_ETHER_ADDR                                     204                     // struct ether_addr
790 #define NECP_CLIENT_RESULT_REMOTE_ETHER_ADDR                                    205                     // struct ether_addr
791 #define NECP_CLIENT_RESULT_EFFECTIVE_TRAFFIC_CLASS              210             // u_int32_t
792 #define NECP_CLIENT_RESULT_TRAFFIC_MGMT_BG                              211             // u_int32_t, 1: background, 0: not background
793 #define NECP_CLIENT_RESULT_GATEWAY                                      212             // struct necp_client_endpoint
794 #define NECP_CLIENT_RESULT_GROUP_MEMBER                                      213             // struct necp_client_endpoint
795 #define NECP_CLIENT_RESULT_NAT64                                        214             // struct ipv6_prefix[NAT64_MAX_NUM_PREFIXES]
796 #define NECP_CLIENT_RESULT_ESTIMATED_THROUGHPUT                         215             // struct necp_client_result_estimated_throughput
797 #define NECP_CLIENT_RESULT_AGENT_ERROR                                  216             // struct necp_client_result_agent_error
798 #define NECP_CLIENT_RESULT_UNIQUE_FLOW_TAG                              217             // u_int32_t
799 #define NECP_CLIENT_RESULT_LINK_QUALITY                                 218             // int8_t
800 #define NECP_CLIENT_RESULT_FLOW_STATS_INDEX                             219             // u_int32_t
801 
802 #define NECP_CLIENT_RESULT_FLAG_IS_LOCAL                                0x0001  // Routes to this device
803 #define NECP_CLIENT_RESULT_FLAG_IS_DIRECT                               0x0002  // Routes to directly accessible peer
804 #define NECP_CLIENT_RESULT_FLAG_HAS_IPV4                                0x0004  // Supports IPv4
805 #define NECP_CLIENT_RESULT_FLAG_HAS_IPV6                                0x0008  // Supports IPv6
806 #define NECP_CLIENT_RESULT_FLAG_DEFUNCT                                 0x0010  // Defunct
807 #define NECP_CLIENT_RESULT_FLAG_SATISFIED                               0x0020  // Satisfied path
808 #define NECP_CLIENT_RESULT_FLAG_FLOW_ASSIGNED                   0x0040  // Assigned, the flow is active
809 #define NECP_CLIENT_RESULT_FLAG_FLOW_VIABLE                             0x0080  // Viable, the flow has a valid route
810 #define NECP_CLIENT_RESULT_FLAG_PROBE_CONNECTIVITY              0x0100  // Flow should probe connectivity
811 #define NECP_CLIENT_RESULT_FLAG_ECN_ENABLED                             0x0200  // ECN should be used
812 #define NECP_CLIENT_RESULT_FLAG_FAST_OPEN_BLOCKED               0x0400  // Fast open should not be used
813 #define NECP_CLIENT_RESULT_FLAG_LINK_QUALITY_ABORT              0x0800  // Link quality is very bad, recommend close connections
814 #define NECP_CLIENT_RESULT_FLAG_ALLOW_QOS_MARKING               0x1000  // QoS marking is allowed
815 #define NECP_CLIENT_RESULT_FLAG_HAS_NAT64                       0x2000  // Has NAT64 prefix
816 #define NECP_CLIENT_RESULT_FLAG_INTERFACE_LOW_POWER             0x4000  // Interface is in low-power mode
817 #define NECP_CLIENT_RESULT_FLAG_SPECIFIC_LISTENER               0x8000  // Listener should not listen on all interfaces
818 #define NECP_CLIENT_RESULT_FLAG_KEXT_FILTER_PRESENT             0x10000 // Kernel extension filter present
819 #define NECP_CLIENT_RESULT_FLAG_PF_RULES_PRESENT                0x20000 // Firewall rules present
820 #define NECP_CLIENT_RESULT_FLAG_ALF_PRESENT                     0x40000 // Application Firewall enabled
821 #define NECP_CLIENT_RESULT_FLAG_PARENTAL_CONTROLS_PRESENT       0x80000 // Parental Controls present
822 #define NECP_CLIENT_RESULT_FLAG_IS_GLOBAL_INTERNET              0x100000 // Routes to global Internet
823 #define NECP_CLIENT_RESULT_FLAG_LINK_HEURISTICS                 0x200000 // Link heuristics enabled
824 
825 
826 #define NECP_CLIENT_RESULT_FLAG_FORCE_UPDATE (NECP_CLIENT_RESULT_FLAG_HAS_IPV4 | NECP_CLIENT_RESULT_FLAG_HAS_IPV6 | NECP_CLIENT_RESULT_FLAG_HAS_NAT64 | NECP_CLIENT_RESULT_FLAG_INTERFACE_LOW_POWER | NECP_CLIENT_RESULT_FLAG_LINK_HEURISTICS)
827 
828 #define NECP_CLIENT_RESULT_FAST_OPEN_SND_PROBE                  0x01    // DEPRECATED - Fast open send probe
829 #define NECP_CLIENT_RESULT_FAST_OPEN_RCV_PROBE                  0x02    // DEPRECATED - Fast open receive probe
830 
831 #define NECP_CLIENT_RESULT_RECOMMENDED_MSS_NONE                 0x01
832 #define NECP_CLIENT_RESULT_RECOMMENDED_MSS_LOW                  0x02
833 #define NECP_CLIENT_RESULT_RECOMMENDED_MSS_MEDIUM               0x04
834 
835 #define NECP_CLIENT_RESULT_REASON_EXPENSIVE_PROHIBITED          1  // Expensive networks were prohibited
836 #define NECP_CLIENT_RESULT_REASON_CONSTRAINED_PROHIBITED                2  // Constrained networks were prohibited
837 #define NECP_CLIENT_RESULT_REASON_CELLULAR_DENIED                3  // Denied by a cellular route rule
838 #define NECP_CLIENT_RESULT_REASON_WIFI_DENIED                4  // Denied by a wifi route rule
839 #define NECP_CLIENT_RESULT_REASON_LOCAL_NETWORK_PROHIBITED       5  // Local network access prohibited
840 #define NECP_CLIENT_RESULT_REASON_ULTRA_CONSTRAINED_NOT_ALLOWED  6  // Ultra constrained networks not allowed
841 
842 struct necp_interface_signature {
843 	u_int8_t signature[IFNET_SIGNATURELEN];
844 	u_int8_t signature_len;
845 };
846 
847 struct necp_interface_details {
848 	char name[IFXNAMSIZ];
849 	u_int32_t index;
850 	u_int32_t generation;
851 	u_int32_t functional_type;
852 	u_int32_t delegate_index;
853 	u_int32_t flags; // see NECP_INTERFACE_FLAG_*
854 	u_int32_t mtu;
855 	struct necp_interface_signature ipv4_signature;
856 	struct necp_interface_signature ipv6_signature;
857 	u_int32_t ipv4_netmask;
858 	u_int32_t ipv4_broadcast;
859 	/*
860 	 * This serves as a temporary header guard to protect libnetcore builds
861 	 * until the xnu changes are available in the build.
862 	 * XXX: Should be removed in a future build.
863 	 */
864 #define NECP_INTERFACE_SUPPORTS_TSO    1
865 	u_int32_t tso_max_segment_size_v4;
866 	u_int32_t tso_max_segment_size_v6;
867 #define NECP_INTERFACE_SUPPORTS_HWCSUM   1
868 	u_int32_t hwcsum_flags;
869 	u_int8_t  radio_type;
870 	u_int8_t  radio_channel;
871 #define NECP_INTERFACE_SUPPORTS_L4S 1
872 	u_int8_t l4s_mode;
873 };
874 
875 #define NECP_INTERFACE_FLAG_EXPENSIVE                                   0x0001
876 #define NECP_INTERFACE_FLAG_TXSTART                                     0X0002
877 #define NECP_INTERFACE_FLAG_NOACKPRI                                    0x0004
878 #define NECP_INTERFACE_FLAG_3CARRIERAGG                                 0x0008
879 #define NECP_INTERFACE_FLAG_IS_LOW_POWER                                0x0010
880 #define NECP_INTERFACE_FLAG_MPK_LOG                                     0x0020 // Multi-layer Packet Logging
881 #define NECP_INTERFACE_FLAG_CONSTRAINED                                 0x0040
882 #define NECP_INTERFACE_FLAG_HAS_NETMASK                                 0x0080
883 #define NECP_INTERFACE_FLAG_HAS_BROADCAST                               0x0100
884 #define NECP_INTERFACE_FLAG_SUPPORTS_MULTICAST                          0x0200
885 #define NECP_INTERFACE_FLAG_HAS_DNS                                     0x0400
886 #define NECP_INTERFACE_FLAG_HAS_NAT64                                   0x0800
887 #define NECP_INTERFACE_FLAG_IPV4_ROUTABLE                               0x1000
888 #define NECP_INTERFACE_FLAG_IPV6_ROUTABLE                               0x2000
889 #define NECP_INTERFACE_FLAG_ULTRA_CONSTRAINED                           0x4000
890 #define NECP_INTERFACE_FLAG_LOW_POWER_WAKE                              0x8000
891 
892 struct necp_client_parameter_netagent_type {
893 	char netagent_domain[32];
894 	char netagent_type[32];
895 };
896 
897 struct necp_client_result_netagent {
898 	u_int32_t generation;
899 	uuid_t netagent_uuid;
900 };
901 
902 struct necp_client_result_interface {
903 	u_int32_t generation;
904 	u_int32_t index;
905 };
906 
907 struct necp_client_result_estimated_throughput {
908 	u_int8_t up;
909 	u_int8_t down;
910 };
911 
912 struct necp_client_result_agent_error {
913 	u_int32_t code;
914 	u_int8_t domain;
915 };
916 
917 #define NECP_USES_INTERFACE_OPTIONS_FOR_BROWSE 1
918 
919 struct necp_client_interface_option {
920 	u_int32_t interface_index;
921 	u_int32_t interface_generation;
922 	uuid_t nexus_agent;
923 };
924 
925 #define NECP_CLIENT_ENDPOINT_TYPE_APPLICATION_SERVICE 6
926 
927 struct necp_client_endpoint {
928 	union {
929 		struct sockaddr sa;
930 		struct sockaddr_in sin;
931 		struct sockaddr_in6 sin6;
932 		struct {
933 			u_int8_t endpoint_length;
934 			u_int8_t endpoint_family; // Use AF_UNSPEC to target a name
935 			u_int16_t endpoint_port;
936 			u_int32_t endpoint_type; // Client-specific type
937 			char endpoint_data[0]; // Type-specific endpoint value
938 		} endpoint;
939 	} u;
940 };
941 
942 struct necp_client_group {
943 	u_int32_t group_type;
944 	uuid_t group_id;
945 };
946 
947 struct necp_client_list {
948 	u_int32_t client_count;
949 	uuid_t clients[0];
950 };
951 
952 struct kev_necp_policies_changed_data {
953 	u_int32_t               changed_count;  // Defaults to 0.
954 };
955 
956 #define NECP_CLIENT_FLOW_FLAGS_ALLOW_NEXUS                      0x01    // Request a nexus instance upon adding a flow
957 #define NECP_CLIENT_FLOW_FLAGS_USE_CLIENT_ID            0x02    // Register the client ID rather than the flow registration ID with network agents
958 #define NECP_CLIENT_FLOW_FLAGS_BROWSE                      0x04    // Create request with a browse agent
959 #define NECP_CLIENT_FLOW_FLAGS_RESOLVE                      0x08    // Create request with a resolution agent
960 #define NECP_CLIENT_FLOW_FLAGS_OVERRIDE_ADDRESS                      0x10    // Flow has a different remote address than the parent flow
961 #define NECP_CLIENT_FLOW_FLAGS_OVERRIDE_IP_PROTOCOL           0x20    // Flow has a different IP protocol than the parent flow
962 #define NECP_CLIENT_FLOW_FLAGS_OPEN_FLOW_ON_BEHALF_OF_CLIENT  0x40    // Flow is opened on behalf of a client
963 
964 struct necp_client_flow_stats {
965 	u_int32_t stats_type; // NECP_CLIENT_STATISTICS_TYPE_*
966 	u_int32_t stats_version; // NECP_CLIENT_STATISTICS_TYPE_*_VER
967 	u_int32_t stats_size;
968 	mach_vm_address_t stats_addr;
969 };
970 
971 struct necp_client_add_flow {
972 	uuid_t agent_uuid;
973 	uuid_t registration_id;
974 	u_int16_t flags; // NECP_CLIENT_FLOW_FLAGS_*
975 	u_int16_t stats_request_count;
976 	struct necp_client_flow_stats stats_requests[0];
977 	// sockaddr for override endpoint
978 	// uint8 for override ip protocol
979 } __attribute__((__packed__));
980 
981 struct necp_agent_use_parameters {
982 	uuid_t agent_uuid;
983 	uint64_t out_use_count;
984 };
985 
986 struct necp_client_group_action {
987 	uuid_t agent_uuid;
988 	u_int16_t group_member_count;
989 	struct necp_client_endpoint group_members[0];
990 } __attribute__((__packed__));
991 
992 struct necp_client_flow_protoctl_event {
993 	uint32_t        protoctl_event_code;
994 	uint32_t        protoctl_event_val;
995 	/* TCP seq number is in host byte order */
996 	uint32_t        protoctl_event_tcp_seq_num;
997 };
998 
999 #define NECP_CLIENT_UPDATE_TYPE_PARAMETERS              1       // Parameters, for a new client
1000 #define NECP_CLIENT_UPDATE_TYPE_RESULT                  2       // Result, for a udpated client
1001 #define NECP_CLIENT_UPDATE_TYPE_REMOVE                  3       // Empty, for a removed client
1002 
1003 struct necp_client_observer_update {
1004 	u_int32_t update_type;  // NECP_CLIENT_UPDATE_TYPE_*
1005 	u_int8_t tlv_buffer[0]; // Parameters or result as TLVs, based on type
1006 };
1007 
1008 // These sign types are tied to specific clients.
1009 // The client_id UUID is then the NECP client UUID that generated the query.
1010 #define NECP_CLIENT_SIGN_TYPE_RESOLVER_ANSWER                   1       // struct necp_client_host_resolver_answer
1011 #define NECP_CLIENT_SIGN_TYPE_BROWSE_RESULT                     2       // struct necp_client_browse_result
1012 #define NECP_CLIENT_SIGN_TYPE_SERVICE_RESOLVER_ANSWER           3       // struct necp_client_service_resolver_answer
1013 
1014 // These sign types are valid for system-wide use.
1015 // The client_id UUID is a unique per-query UUID.
1016 // These should be generated only on behalf of trusted or entitled processes.
1017 #define NECP_CLIENT_SIGN_TYPE_SYSTEM_RESOLVER_ANSWER            4       // struct necp_client_host_resolver_answer
1018 #define NECP_CLIENT_SIGN_TYPE_SYSTEM_BROWSE_RESULT              5       // struct necp_client_browse_result
1019 #define NECP_CLIENT_SIGN_TYPE_SYSTEM_SERVICE_RESOLVER_ANSWER    6       // struct necp_client_service_resolver_answer
1020 
1021 struct necp_client_signature {
1022 	u_int8_t signed_tag[32];
1023 } __attribute__((__packed__));
1024 
1025 //
1026 struct necp_client_signable {
1027 	uuid_t client_id; // Interpretation depends on sign_type
1028 	u_int32_t sign_type;
1029 	u_int8_t signable_data[0];
1030 } __attribute__((__packed__));
1031 
1032 struct necp_client_resolver_answer {
1033 	uuid_t client_id;
1034 	u_int32_t sign_type;
1035 	union sockaddr_in_4_6 address_answer; // Can include port
1036 	u_int32_t hostname_length;
1037 	char hostname[0];
1038 } __attribute__((__packed__));
1039 
1040 struct necp_client_host_resolver_answer {
1041 	struct necp_client_signable header;
1042 	u_int32_t metadata_hash; // Extra metadata that can be signed along with the answer, such as a network signature.
1043 	union sockaddr_in_4_6 address_answer; // Can include port
1044 	u_int32_t hostname_length;
1045 	char hostname[0];
1046 } __attribute__((__packed__));
1047 
1048 struct necp_client_browse_result {
1049 	struct necp_client_signable header;
1050 	u_int32_t metadata_hash; // Extra metadata that can be signed along with the service, which may include a TXT record or a network signature
1051 	u_int16_t service_length;
1052 	char service[0];
1053 } __attribute__((__packed__));
1054 
1055 struct necp_client_service_resolver_answer {
1056 	struct necp_client_signable header;
1057 	u_int32_t metadata_hash; // Extra metadata that can be signed along with the service, which may include a TXT record or a network signature
1058 	u_int16_t service_length;
1059 	u_int16_t port;
1060 	u_int16_t hostname_length;
1061 	char service[0];
1062 	char hostname[0];
1063 } __attribute__((__packed__));
1064 
1065 struct necp_client_validatable {
1066 	struct necp_client_signature signature;
1067 	struct necp_client_signable signable;
1068 } __attribute__((__packed__));
1069 
1070 #define NECP_CLIENT_ACTION_SIGN_MAX_STRING_LENGTH 1024
1071 
1072 #define NECP_CLIENT_ACTION_SIGN_TAG_LENGTH 32
1073 
1074 #define NECP_CLIENT_ACTION_SIGN_DEFAULT_DATA_LENGTH 128
1075 #define NECP_CLIENT_ACTION_SIGN_MAX_TOTAL_LENGTH 4096
1076 
1077 #define NECP_FILTER_UNIT_NO_FILTER              UINT32_MAX // Reserved filter unit value that prohibits all filters and socket filters
1078 
1079 // Type of signed client ID requests.
1080 #define NECP_CLIENT_SIGNED_CLIENT_ID_TYPE_UUID                   1       // struct necp_client_signed_client_id_uuid
1081 
1082 struct necp_client_signed_client_id_uuid {
1083 	uuid_t client_id;
1084 	u_int32_t signature_length;
1085 	u_int8_t signature_data[NECP_CLIENT_ACTION_SIGN_TAG_LENGTH];
1086 } __attribute__((__packed__));
1087 
1088 /*
1089  * The sysctl "net.necp.necp_drop_dest_level" controls the global drop rule policy for
1090  * a set of destinations addresses at the given level -- the drop rule is the last one
1091  * to be evaluated at this level.
1092  */
1093 #define MAX_NECP_DROP_DEST_LEVEL_ADDRS 8
1094 
1095 struct necp_drop_dest_entry {
1096 	u_int32_t                           level;          // priority level
1097 	u_int32_t                           order;          // session order (read only via sysctl)
1098 	struct necp_policy_condition_addr   cond_addr;
1099 };
1100 
1101 struct necp_drop_dest_policy {
1102 	u_int32_t entry_count;
1103 	struct necp_drop_dest_entry entries[MAX_NECP_DROP_DEST_LEVEL_ADDRS];
1104 };
1105 
1106 
1107 #define NECP_DEMUX_MAX_LEN  32
1108 struct necp_demux_pattern {
1109 	uint16_t offset;
1110 	uint16_t len;
1111 	uint8_t mask[NECP_DEMUX_MAX_LEN];
1112 	uint8_t value[NECP_DEMUX_MAX_LEN];
1113 };
1114 
1115 struct necp_flow_statistics {
1116 	union {
1117 		struct tcp_info tcpi;
1118 	} transport;
1119 	uint8_t transport_proto;
1120 	uint8_t pad[3];
1121 };
1122 
1123 #ifdef BSD_KERNEL_PRIVATE
1124 #include <stdbool.h>
1125 #include <sys/socketvar.h>
1126 #include <sys/kern_control.h>
1127 #include <netinet/ip_var.h>
1128 #include <netinet6/ip6_var.h>
1129 #include <net/if_var.h>
1130 #include <sys/syslog.h>
1131 #include <net/network_agent.h>
1132 #include <net/ethernet.h>
1133 #include <os/log.h>
1134 #if SKYWALK
1135 #include <skywalk/namespace/netns.h>
1136 #endif /* SKYWALK */
1137 
1138 
1139 SYSCTL_DECL(_net_necp);
1140 
1141 extern os_log_t necp_log_handle;
1142 extern os_log_t necp_data_trace_log_handle;
1143 
1144 #define NECPLOG(level, format, ...) do {                                                                                        \
1145 	if (level == LOG_ERR) {                                                          \
1146 	    os_log_error(necp_log_handle, "%s: " format "\n", __FUNCTION__, __VA_ARGS__); \
1147 	} else {                                                                         \
1148 	    os_log(necp_log_handle, "%s: " format "\n", __FUNCTION__, __VA_ARGS__);       \
1149 	}                                                                                \
1150 } while (0)
1151 
1152 #define NECPLOG0(level, msg) do {                                                                                       \
1153 	if (level == LOG_ERR) {                                                          \
1154 	    os_log_error(necp_log_handle, "%s: %s\n", __FUNCTION__, msg);                 \
1155 	} else {                                                                         \
1156 	    os_log(necp_log_handle, "%s: %s\n", __FUNCTION__, msg);                       \
1157 	}                                                                                \
1158 } while (0)
1159 
1160 #define NECPDATATRACELOG(level, format, ...) do {                                                                                        \
1161     if (level == LOG_ERR) {                                                          \
1162 	os_log_error(necp_data_trace_log_handle, "%s: " format "\n", __FUNCTION__, __VA_ARGS__); \
1163     } else {                                                                         \
1164 	os_log(necp_data_trace_log_handle, "%s: " format "\n", __FUNCTION__, __VA_ARGS__);       \
1165     }                                                                                \
1166 } while (0)
1167 
1168 enum necp_fd_type_t {
1169 	necp_fd_type_invalid = 0,
1170 	necp_fd_type_session = 1,
1171 	necp_fd_type_client = 2,
1172 };
1173 
1174 union necp_sockaddr_union {
1175 	struct sockaddr                 sa;
1176 	struct sockaddr_in              sin;
1177 	struct sockaddr_in6             sin6;
1178 };
1179 
1180 /*
1181  * kstats
1182  * The ustats and kstats region are mirrored. So when we allocate with
1183  * skmem_cache from kstats region, we also get an ustats object. To tie them
1184  * together, kstats has an extra *necp_stats_ustats pointer pointing to the
1185  * ustats object
1186  */
1187 struct necp_all_kstats {
1188 	struct necp_all_stats           necp_stats_comm;        /* kernel private stats snapshot */
1189 	struct necp_all_stats           *necp_stats_ustats;     /* points to user-visible stats (in shared ustats region) */
1190 };
1191 
1192 extern void necp_client_init(void);
1193 extern int necp_application_find_policy_match_internal(proc_t proc, u_int8_t *parameters __sized_by(parameters_size), u_int32_t parameters_size,
1194     struct necp_aggregate_result *returned_result,
1195     u_int32_t *flags, u_int32_t *reason, u_int required_interface_index,
1196     const union necp_sockaddr_union *override_local_addr,
1197     const union necp_sockaddr_union *override_remote_addr,
1198     struct necp_client_endpoint *returned_v4_gateway,
1199     struct necp_client_endpoint *returned_v6_gateway,
1200     struct rtentry **returned_route, bool ignore_address,
1201     bool has_client,
1202     uuid_t *returned_override_euuid);
1203 /*
1204  * TLV utilities
1205  *
1206  * Note that these functions (other than necp_buffer_find_tlv) do not check the length of the entire buffer,
1207  * so the caller must be sure that the entire TLV is within bounds.
1208  */
1209 struct necp_tlv_header {
1210 	u_int8_t type;
1211 	u_int32_t length;
1212 } __attribute__((__packed__));
1213 
1214 extern u_int8_t * __counted_by(0) necp_buffer_write_tlv(u_int8_t * __counted_by(0)cursor_, u_int8_t type, u_int32_t length, const void *value __sized_by(length), u_int8_t * buffer __sized_by(buffer_length), u_int32_t buffer_length);
1215 extern u_int8_t * __counted_by(0) necp_buffer_write_tlv_if_different(u_int8_t * __counted_by(0)cursor_, u_int8_t type,
1216     u_int32_t length, const void *value __sized_by(length), bool *updated,
1217     u_int8_t * buffer __sized_by(buffer_length), u_int32_t buffer_length);
1218 extern u_int8_t necp_buffer_get_tlv_type(u_int8_t * __counted_by(buffer_length)buffer, size_t buffer_length, u_int32_t tlv_offset);
1219 extern u_int32_t necp_buffer_get_tlv_length(u_int8_t * __counted_by(buffer_length)buffer, size_t buffer_length, u_int32_t tlv_offset);
1220 extern u_int8_t *__sized_by(*value_size) __necp_buffer_get_tlv_value(u_int8_t * __counted_by(buffer_length)buffer, size_t buffer_length, u_int32_t tlv_offset, u_int32_t * value_size);
1221 extern int necp_buffer_find_tlv(u_int8_t * buffer __sized_by(buffer_length), u_int32_t buffer_length, int offset, u_int8_t type, int *err, int next);
1222 
1223 /*
1224  * Inline shim to safely convert the result of `__necp_buffer_get_tlv_value'
1225  * to an indexable pointer, when appropriate.
1226  */
1227 __attribute__((always_inline))
1228 static inline u_int8_t * __header_indexable
necp_buffer_get_tlv_value(u_int8_t * __counted_by (buffer_length)buffer,size_t buffer_length,u_int32_t tlv_offset,u_int32_t * value_size)1229 necp_buffer_get_tlv_value(u_int8_t * __counted_by(buffer_length)buffer, size_t buffer_length, u_int32_t tlv_offset, u_int32_t *value_size)
1230 {
1231 	u_int32_t ensured_value_size = 0;
1232 	u_int8_t *value = __necp_buffer_get_tlv_value(buffer, buffer_length, tlv_offset, &ensured_value_size);
1233 	if (value_size) {
1234 		*value_size = ensured_value_size;
1235 	}
1236 	return value;
1237 }
1238 
1239 #define NECPCTL_DROP_ALL_LEVEL                          1       /* Drop all packets if no policy matches above this level */
1240 #define NECPCTL_DEBUG                                           2       /* Log all kernel policy matches */
1241 #define NECPCTL_PASS_LOOPBACK                           3       /* Pass all loopback traffic */
1242 #define NECPCTL_PASS_KEEPALIVES                         4       /* Pass all kernel-generated keepalive traffic */
1243 #define NECPCTL_SOCKET_POLICY_COUNT                     5       /* Count of all socket-level policies */
1244 #define NECPCTL_SOCKET_NON_APP_POLICY_COUNT     6       /* Count of non-per-app socket-level policies */
1245 #define NECPCTL_IP_POLICY_COUNT                         7       /* Count of all ip-level policies */
1246 #define NECPCTL_SESSION_COUNT                           8       /* Count of NECP sessions */
1247 #define NECPCTL_CLIENT_FD_COUNT                         9       /* Count of NECP client fds */
1248 #define NECPCTL_CLIENT_COUNT                            10      /* Count of NECP clients */
1249 #define NECPCTL_ARENA_COUNT                                     11      /* Count of NECP arenas (stats, etc) */
1250 #define NECPCTL_NEXUS_FLOW_COUNT                        12      /* Count of NECP nexus flows */
1251 #define NECPCTL_SOCKET_FLOW_COUNT                       13      /* Count of NECP socket flows */
1252 #define NECPCTL_IF_FLOW_COUNT                           14      /* Count of NECP socket flows */
1253 #define NECPCTL_OBSERVER_FD_COUNT                       15      /* Count of NECP observer fds */
1254 #define NECPCTL_OBSERVER_MESSAGE_LIMIT          16      /* Number of of NECP observer messages allowed to be queued */
1255 #define NECPCTL_SYSCTL_ARENA_COUNT                      17      /* Count of sysctl arenas */
1256 #define NECPCTL_DROP_UNENTITLED_LEVEL                   18      /* Drop unentitled process traffic above this level */
1257 #define NECPCTL_PASS_INTERPOSE                          19      /* Pass interpose */
1258 #define NECPCTL_RESTRICT_MULTICAST                      20      /* Restrict multicast access */
1259 #define NECPCTL_DEDUP_POLICIES                          21      /* Dedup overlapping policies */
1260 #define NECPCTL_CLIENT_TRACING_LEVEL                    22      /* Client tracing level */
1261 #define NECPCTL_CLIENT_TRACING_PID                      23      /* Apply client tracing only to specified pid */
1262 #define NECPCTL_DROP_MANAGEMENT_LEVEL                   24      /* Drop management traffic at this level */
1263 #define NECPCTL_TRIE_COUNT                              25      /* Count of tries */
1264 
1265 #define NECP_LOOPBACK_PASS_ALL         1  // Pass all loopback traffic
1266 #define NECP_LOOPBACK_PASS_WITH_FILTER 2  // Pass all loopback traffic, but activate content filter and/or flow divert if applicable
1267 
1268 #define NECPCTL_NAMES {                                 \
1269 	{ 0, 0 },                                                       \
1270 	{ "drop_all_level", CTLTYPE_INT },      \
1271 	{ "debug", CTLTYPE_INT },                       \
1272 	{ "pass_loopback", CTLTYPE_INT },       \
1273     { "pass_keepalives", CTLTYPE_INT },     \
1274     { "pass_interpose", CTLTYPE_INT },      \
1275 }
1276 
1277 typedef u_int32_t necp_kernel_policy_id;
1278 #define NECP_KERNEL_POLICY_ID_NONE                      0
1279 #define NECP_KERNEL_POLICY_ID_NO_MATCH          1
1280 #define NECP_KERNEL_POLICY_ID_FIRST_VALID_SOCKET        2
1281 #define NECP_KERNEL_POLICY_ID_FIRST_VALID_IP            UINT16_MAX
1282 
1283 typedef u_int32_t necp_app_id;
1284 
1285 #define NECP_KERNEL_POLICY_RESULT_NONE                                  0
1286 #define NECP_KERNEL_POLICY_RESULT_PASS                                  NECP_POLICY_RESULT_PASS
1287 #define NECP_KERNEL_POLICY_RESULT_SKIP                                  NECP_POLICY_RESULT_SKIP
1288 #define NECP_KERNEL_POLICY_RESULT_DROP                                  NECP_POLICY_RESULT_DROP
1289 #define NECP_KERNEL_POLICY_RESULT_SOCKET_DIVERT                 NECP_POLICY_RESULT_SOCKET_DIVERT
1290 #define NECP_KERNEL_POLICY_RESULT_SOCKET_FILTER                 NECP_POLICY_RESULT_SOCKET_FILTER
1291 #define NECP_KERNEL_POLICY_RESULT_IP_TUNNEL                             NECP_POLICY_RESULT_IP_TUNNEL
1292 #define NECP_KERNEL_POLICY_RESULT_IP_FILTER                             NECP_POLICY_RESULT_IP_FILTER
1293 #define NECP_KERNEL_POLICY_RESULT_SOCKET_SCOPED                 NECP_POLICY_RESULT_SOCKET_SCOPED
1294 #define NECP_KERNEL_POLICY_RESULT_ROUTE_RULES                   NECP_POLICY_RESULT_ROUTE_RULES
1295 #define NECP_KERNEL_POLICY_RESULT_USE_NETAGENT                  NECP_POLICY_RESULT_USE_NETAGENT
1296 #define NECP_KERNEL_POLICY_RESULT_NETAGENT_SCOPED               NECP_POLICY_RESULT_NETAGENT_SCOPED
1297 #define NECP_KERNEL_POLICY_RESULT_SCOPED_DIRECT                 NECP_POLICY_RESULT_SCOPED_DIRECT
1298 #define NECP_KERNEL_POLICY_RESULT_ALLOW_UNENTITLED              NECP_POLICY_RESULT_ALLOW_UNENTITLED
1299 #define NECP_KERNEL_POLICY_RESULT_REMOVE_NETAGENT               NECP_POLICY_RESULT_REMOVE_NETAGENT
1300 #define NECP_KERNEL_POLICY_RESULT_REMOVE_NETAGENT_TYPE          NECP_POLICY_RESULT_REMOVE_NETAGENT_TYPE
1301 
1302 #define NECP_KERNEL_POLICY_PASS_NO_SKIP_IPSEC                   NECP_POLICY_PASS_NO_SKIP_IPSEC
1303 #define NECP_KERNEL_POLICY_PASS_PF_TAG                          NECP_POLICY_PASS_PF_TAG
1304 
1305 #define NECP_KERNEL_POLICY_DROP_FLAG_LOCAL_NETWORK              NECP_POLICY_DROP_FLAG_LOCAL_NETWORK
1306 #define NECP_KERNEL_POLICY_DROP_FLAG_SUPPRESS_ALERTS            NECP_POLICY_DROP_FLAG_SUPPRESS_ALERTS
1307 
1308 typedef union {
1309 	u_int                                           tunnel_interface_index;
1310 	u_int                                           scoped_interface_index;
1311 	u_int32_t                                       flow_divert_control_unit;
1312 	u_int32_t                                       filter_control_unit;
1313 	u_int32_t                                       skip_policy_order;
1314 	u_int32_t                                       route_rule_id;
1315 	u_int32_t                                       netagent_id;
1316 	u_int32_t                                       pass_flags;
1317 	u_int32_t                                       drop_flags;
1318 } necp_kernel_policy_result_parameter;
1319 
1320 enum necp_boolean_state {
1321 	necp_boolean_state_unknown = 0,
1322 	necp_boolean_state_false = 1,
1323 	necp_boolean_state_true = 2,
1324 };
1325 
1326 struct necp_kernel_socket_policy {
1327 	LIST_ENTRY(necp_kernel_socket_policy)   chain;
1328 	necp_kernel_policy_id           id;
1329 	necp_policy_order                       order;
1330 	u_int32_t                                       session_order;
1331 	int                                                     session_pid;
1332 
1333 	u_int64_t                                       condition_mask;
1334 	u_int64_t                                       condition_negated_mask;
1335 	u_int32_t                                       cond_client_flags;
1336 	necp_kernel_policy_id           cond_policy_id;
1337 	u_int32_t                                       cond_app_id;                                    // Locally assigned ID value stored
1338 	u_int32_t                                       cond_real_app_id;                               // Locally assigned ID value stored
1339 	char                                            *cond_custom_entitlement __null_terminated;     // String
1340 	u_int32_t                                       cond_account_id;                                // Locally assigned ID value stored
1341 	char                                            *cond_domain __null_terminated;                 // String
1342 	u_int8_t                                        cond_domain_dot_count;                  // Number of dots in cond_domain
1343 	u_int32_t                                       cond_domain_filter;
1344 	pid_t                                           cond_pid;
1345 	uid_t                                           cond_uid;
1346 	uid_t                                           cond_real_uid;
1347 	ifnet_t                                         cond_bound_interface;                   // Matches specific binding only
1348 	struct necp_policy_condition_tc_range cond_traffic_class;       // Matches traffic class in range
1349 	u_int16_t                                       cond_protocol;                                  // Matches IP protcol number
1350 	union necp_sockaddr_union       cond_local_start;                               // Matches local IP address (or start)
1351 	union necp_sockaddr_union       cond_local_end;                                 // Matches IP address range
1352 	u_int8_t                                        cond_local_prefix;                              // Defines subnet
1353 	union necp_sockaddr_union       cond_remote_start;                              // Matches remote IP address (or start)
1354 	union necp_sockaddr_union       cond_remote_end;                                // Matches IP address range
1355 	u_int8_t                                        cond_remote_prefix;                             // Defines subnet
1356 	struct necp_policy_condition_agent_type cond_agent_type;
1357 	struct necp_policy_condition_sdk_version cond_sdk_version;
1358 	char                                            *cond_signing_identifier __null_terminated;   // String
1359 	char                                            *cond_url __null_terminated;// String
1360 	u_int16_t                                       cond_packet_filter_tags;
1361 	u_int16_t                                       cond_scheme_port;
1362 	int32_t                                         cond_pid_version;
1363 	u_int32_t                                       cond_bound_interface_flags;
1364 	u_int32_t                                       cond_bound_interface_eflags;
1365 	u_int32_t                                       cond_bound_interface_xflags;
1366 	u_int8_t                                        cond_local_networks_flags;
1367 
1368 	necp_kernel_policy_result       result;
1369 	necp_kernel_policy_result_parameter     result_parameter;
1370 };
1371 
1372 struct necp_kernel_ip_output_policy {
1373 	LIST_ENTRY(necp_kernel_ip_output_policy)        chain;
1374 	necp_kernel_policy_id           id;
1375 	necp_policy_order                       suborder;
1376 	necp_policy_order                       order;
1377 	u_int32_t                                       session_order;
1378 	int                                                     session_pid;
1379 
1380 	u_int64_t                                       condition_mask;
1381 	u_int64_t                                       condition_negated_mask;
1382 	necp_kernel_policy_id           cond_policy_id;
1383 	ifnet_t                                         cond_bound_interface;                   // Matches specific binding only
1384 	u_int16_t                                       cond_protocol;                                  // Matches IP protcol number
1385 	union necp_sockaddr_union       cond_local_start;                               // Matches local IP address (or start)
1386 	union necp_sockaddr_union       cond_local_end;                                 // Matches IP address range
1387 	u_int8_t                                        cond_local_prefix;                              // Defines subnet
1388 	union necp_sockaddr_union       cond_remote_start;                              // Matches remote IP address (or start)
1389 	union necp_sockaddr_union       cond_remote_end;                                // Matches IP address range
1390 	u_int8_t                                        cond_remote_prefix;                             // Defines subnet
1391 	u_int32_t                                       cond_last_interface_index;
1392 	u_int16_t                       cond_packet_filter_tags;
1393 	u_int16_t                       cond_scheme_port;
1394 	u_int32_t                                       cond_bound_interface_flags;
1395 	u_int32_t                                       cond_bound_interface_eflags;
1396 	u_int32_t                                       cond_bound_interface_xflags;
1397 	u_int8_t                                        cond_local_networks_flags;
1398 
1399 	necp_kernel_policy_result       result;
1400 	necp_kernel_policy_result_parameter     result_parameter;
1401 };
1402 
1403 #define MAX_KERNEL_SOCKET_POLICIES                      1
1404 #define MAX_KERNEL_IP_OUTPUT_POLICIES           4
1405 struct necp_session_policy {
1406 	LIST_ENTRY(necp_session_policy) chain;
1407 	bool                            applied;                        // Applied into the kernel table
1408 	bool                            pending_deletion;       // Waiting to be removed from kernel table
1409 	bool                            pending_update;         // Policy has been modified since creation/last application
1410 	necp_policy_id          local_id;
1411 	necp_policy_order       order;
1412 	u_int32_t                       result_size;
1413 	u_int8_t                        *result __sized_by(result_size);
1414 	u_int32_t                       conditions_size;
1415 	u_int8_t                        *conditions __sized_by(conditions_size); // Array of conditions, each with a u_int32_t length at start
1416 	u_int32_t                       route_rules_size;
1417 	u_int8_t                        *route_rules __sized_by(route_rules_size); // Array of route rules, each with a u_int32_t length at start
1418 
1419 	uuid_t                          applied_app_uuid;
1420 	uuid_t                          applied_real_app_uuid;
1421 	u_int32_t                       applied_account_size;
1422 	char                            *applied_account __sized_by(applied_account_size);
1423 
1424 	uuid_t                          applied_result_uuid;
1425 
1426 	u_int32_t                       applied_agent_type_id;
1427 
1428 	u_int32_t                       applied_route_rules_id;
1429 
1430 	necp_kernel_policy_id   kernel_socket_policies[MAX_KERNEL_SOCKET_POLICIES];
1431 	necp_kernel_policy_id   kernel_ip_output_policies[MAX_KERNEL_IP_OUTPUT_POLICIES];
1432 };
1433 
1434 struct necp_aggregate_socket_result {
1435 	necp_kernel_policy_result                       result;
1436 	necp_kernel_policy_result_parameter     result_parameter;
1437 	necp_kernel_policy_filter                       filter_control_unit;
1438 	u_int32_t                                       flow_divert_aggregate_unit;
1439 	u_int32_t                                                       route_rule_id;
1440 	int32_t                                         qos_marking_gencount;
1441 };
1442 
1443 struct necp_inpcb_result {
1444 	u_int32_t                                       app_id;
1445 	necp_kernel_policy_id                           policy_id;
1446 	necp_kernel_policy_id                           skip_policy_id;
1447 	int32_t                                         policy_gencount;
1448 	u_int32_t                                       flowhash;
1449 	u_int32_t                                       network_denied_notifies;// Notification count
1450 	struct necp_aggregate_socket_result             results;
1451 };
1452 
1453 extern void necp_init(void);
1454 
1455 struct inp_necp_attributes;
1456 extern errno_t necp_set_socket_attributes(struct inp_necp_attributes *attributes, struct sockopt *sopt);
1457 extern errno_t necp_get_socket_attributes(struct inp_necp_attributes *attributes, struct sockopt *sopt);
1458 extern errno_t necp_set_socket_domain_attributes(struct socket *so, const char *domain __null_terminated, const char *domain_owner __null_terminated);
1459 extern int necp_set_socket_resolver_signature(struct inpcb *inp, struct sockopt *sopt);
1460 extern int necp_get_socket_resolver_signature(struct inpcb *inp, struct sockopt *sopt);
1461 extern bool necp_socket_has_resolver_signature(struct inpcb *inp);
1462 extern bool necp_socket_resolver_signature_matches_address(struct inpcb *inp, union necp_sockaddr_union *address);
1463 extern void necp_inpcb_remove_cb(struct inpcb *inp);
1464 extern void necp_inpcb_dispose(struct inpcb *inp);
1465 
1466 extern u_int32_t necp_socket_get_content_filter_control_unit(struct socket *so);
1467 extern u_int32_t necp_socket_get_policy_gencount(struct socket *so);
1468 
1469 extern bool necp_socket_should_use_flow_divert(struct inpcb *inp);
1470 extern u_int32_t necp_socket_get_flow_divert_control_unit(struct inpcb *inp, uint32_t *aggregate_unit);
1471 
1472 extern bool necp_socket_should_rescope(struct inpcb *inp);
1473 extern u_int necp_socket_get_rescope_if_index(struct inpcb *inp);
1474 extern u_int32_t necp_socket_get_effective_mtu(struct inpcb *inp, u_int32_t current_mtu);
1475 
1476 extern bool necp_socket_is_allowed_to_recv_on_interface(struct inpcb *inp, ifnet_t interface);
1477 
1478 extern bool necp_socket_is_allowed_to_send_recv(struct inpcb *inp, ifnet_t input_interface, u_int16_t pf_tag,
1479     necp_kernel_policy_id *return_policy_id,
1480     u_int32_t *return_route_rule_id,
1481     necp_kernel_policy_id *return_skip_policy_id, u_int32_t *return_pass_flags);
1482 extern bool necp_socket_is_allowed_to_send_recv_v4(struct inpcb *inp, u_int16_t local_port,
1483     u_int16_t remote_port, struct in_addr *local_addr,
1484     struct in_addr *remote_addr, ifnet_t input_interface, u_int16_t pf_tag,
1485     necp_kernel_policy_id *return_policy_id, u_int32_t *return_route_rule_id,
1486     necp_kernel_policy_id *return_skip_policy_id, u_int32_t *return_pass_flags);
1487 extern bool necp_socket_is_allowed_to_send_recv_v6(struct inpcb *inp, u_int16_t local_port,
1488     u_int16_t remote_port, struct in6_addr *local_addr,
1489     struct in6_addr *remote_addr, ifnet_t input_interface, u_int16_t pf_tag,
1490     necp_kernel_policy_id *return_policy_id, u_int32_t *return_route_rule_id,
1491     necp_kernel_policy_id *return_skip_policy_id, u_int32_t *return_pass_flags);
1492 extern void necp_socket_update_qos_marking(struct inpcb *inp, struct rtentry *route, u_int32_t route_rule_id);
1493 extern bool necp_lookup_current_qos_marking(int32_t *qos_marking_gencount, struct rtentry *route, struct ifnet *interface, u_int32_t route_rule_id, bool old_qos_marking);
1494 extern int necp_mark_packet_from_socket(struct mbuf *packet, struct inpcb *inp, necp_kernel_policy_id policy_id,
1495     u_int32_t route_rule_id, necp_kernel_policy_id skip_policy_id, u_int32_t pass_flags);
1496 extern necp_kernel_policy_id necp_get_policy_id_from_packet(struct mbuf *packet);
1497 extern necp_kernel_policy_id necp_get_skip_policy_id_from_packet(struct mbuf *packet);
1498 extern u_int16_t necp_get_packet_filter_tags_from_packet(struct mbuf *packet);
1499 extern bool necp_packet_should_skip_filters(struct mbuf *packet);
1500 extern u_int32_t necp_get_last_interface_index_from_packet(struct mbuf *packet);
1501 extern u_int32_t necp_get_route_rule_id_from_packet(struct mbuf *packet);
1502 extern int necp_get_app_uuid_from_packet(struct mbuf *packet,
1503     uuid_t app_uuid);
1504 
1505 extern necp_kernel_policy_id necp_socket_find_policy_match(struct inpcb *inp, struct sockaddr *override_local_addr,
1506     struct sockaddr *override_remote_addr, u_int32_t override_bound_interface);
1507 extern necp_kernel_policy_id necp_ip_output_find_policy_match(struct mbuf *packet, int flags, struct ip_out_args *ipoa,
1508     struct rtentry *rt,
1509     necp_kernel_policy_result *result,
1510     necp_kernel_policy_result_parameter *result_parameter);
1511 extern necp_kernel_policy_id necp_ip6_output_find_policy_match(struct mbuf *packet, int flags, struct ip6_out_args *ip6oa,
1512     struct rtentry *rt,
1513     necp_kernel_policy_result *result,
1514     necp_kernel_policy_result_parameter *result_parameter);
1515 
1516 extern int necp_mark_packet_from_ip(struct mbuf *packet, necp_kernel_policy_id policy_id);
1517 extern int necp_mark_packet_from_ip_with_skip(struct mbuf *packet, necp_kernel_policy_id policy_id, necp_kernel_policy_id skip_policy_id);
1518 extern int necp_mark_packet_from_interface(struct mbuf *packet, ifnet_t interface);
1519 
1520 extern ifnet_t necp_get_ifnet_from_result_parameter(necp_kernel_policy_result_parameter *result_parameter);
1521 extern bool necp_packet_can_rebind_to_ifnet(struct mbuf *packet, struct ifnet *interface, struct route *new_route, int family);
1522 
1523 extern bool necp_packet_is_allowed_over_interface(struct mbuf *packet, struct ifnet *interface);
1524 
1525 extern int necp_mark_packet_as_keepalive(struct mbuf *packet, bool is_keepalive);
1526 extern bool necp_get_is_keepalive_from_packet(struct mbuf *packet);
1527 
1528 extern int necp_sign_resolver_answer(uuid_t client_id, u_int32_t sign_type,
1529     u_int8_t *data, size_t data_length,
1530     u_int8_t *tag, size_t *out_tag_length);
1531 
1532 extern bool necp_validate_resolver_answer(uuid_t client_id, u_int32_t sign_type,
1533     u_int8_t *data __sized_by(data_length), size_t data_length,
1534     u_int8_t *tag __sized_by(tag_length), size_t tag_length);
1535 
1536 extern int necp_sign_application_id(uuid_t client_id, u_int32_t sign_type,
1537     u_int8_t *__counted_by(*out_tag_length) tag, size_t *out_tag_length);
1538 
1539 extern bool necp_validate_application_id(uuid_t client_id, u_int32_t sign_type,
1540     u_int8_t *tag __sized_by(tag_length), size_t tag_length);
1541 
1542 extern void necp_update_all_clients(void); // Handle general re-evaluate event
1543 extern void necp_update_all_clients_immediately_if_needed(bool should_update_immediately); // Handle general re-evaluate event
1544 
1545 extern void necp_force_update_client(uuid_t client_id, uuid_t remove_netagent_uuid, u_int32_t agent_generation); // Cause a single client to get an update event
1546 
1547 extern bool necp_set_client_as_background(proc_t proc, struct fileproc *fp, bool background); // Set all clients for an fp as background or not
1548 
1549 struct necp_fd_data;
1550 extern void necp_fd_memstatus(proc_t proc, uint32_t status, struct necp_fd_data *client_fd); // Purge memory of clients for the process
1551 extern void necp_fd_defunct(proc_t proc, struct necp_fd_data *client_fd); // Set all clients for an process as defunct
1552 
1553 extern void necp_client_request_in_process_flow_divert(pid_t pid);
1554 
1555 extern int necp_client_register_socket_flow(pid_t pid, uuid_t client_id, struct inpcb *inp);
1556 
1557 extern int necp_client_register_socket_listener(pid_t pid, uuid_t client_id, struct inpcb *inp);
1558 
1559 #if SKYWALK
1560 extern int necp_client_get_netns_flow_info(uuid_t client_id, struct ns_flow_info *flow_info);
1561 #endif /* SKYWALK */
1562 
1563 extern int necp_client_assert_bb_radio_manager(uuid_t client_id, bool assert);
1564 
1565 extern int necp_client_assign_from_socket(pid_t pid, uuid_t client_id, struct inpcb *inp);
1566 
1567 extern int necp_assign_client_result(uuid_t netagent_uuid, uuid_t client_id,
1568     u_int8_t *assigned_results __sized_by(assigned_results_length), size_t assigned_results_length);
1569 extern int necp_assign_client_group_members(uuid_t netagent_uuid, uuid_t client_id,
1570     u_int8_t *__counted_by(assigned_group_members_length) assigned_group_members,
1571     size_t assigned_group_members_length);
1572 
1573 struct skmem_obj_info;  // forward declaration
1574 extern int necp_stats_ctor(struct skmem_obj_info *oi, struct skmem_obj_info *oim, void *arg, uint32_t skmflag);
1575 extern int necp_stats_dtor(void *addr, void *arg);
1576 
1577 /* value to denote invalid flow advisory index */
1578 struct netagent_session;
1579 extern int
1580 necp_update_flow_protoctl_event(uuid_t netagent_uuid, uuid_t client_id,
1581     uint32_t protoctl_event_code, uint32_t protoctl_event_val,
1582     uint32_t protoctl_event_tcp_seq_num);
1583 
1584 #define NECP_FLOWADV_IDX_INVALID        UINT32_MAX
1585 extern void * __sized_by(*message_length) necp_create_nexus_assign_message(uuid_t nexus_instance, nexus_port_t nexus_port, void *key __sized_by(key_length), uint32_t key_length,
1586     struct necp_client_endpoint *local_endpoint, struct necp_client_endpoint *remote_endpoint,
1587     struct ether_addr *local_ether_addr,
1588     u_int32_t flow_adv_index, void *flow_stats, uint32_t flow_id, size_t *message_length);
1589 
1590 
1591 #define NECP_MAX_DEMUX_PATTERNS 4
1592 struct necp_client_nexus_parameters {
1593 	pid_t pid;
1594 	pid_t epid;
1595 	uuid_t euuid;
1596 #if SKYWALK
1597 	netns_token port_reservation;
1598 #else /* !SKYWALK */
1599 	void *reserved;
1600 #endif /* !SKYWALK */
1601 	union necp_sockaddr_union local_addr;
1602 	union necp_sockaddr_union remote_addr;
1603 	u_int8_t ip_protocol;
1604 	u_int8_t transport_protocol;
1605 	u_int16_t ethertype;
1606 	u_int32_t traffic_class;
1607 	necp_policy_id policy_id;
1608 	necp_policy_id skip_policy_id;
1609 	unsigned is_listener:1;
1610 	unsigned is_interpose:1;
1611 	unsigned is_custom_ether:1;
1612 	unsigned allow_qos_marking:1;
1613 	unsigned override_address_selection:1;
1614 	unsigned use_stable_address:1; // Used if override_address_selection is set
1615 	unsigned no_wake_from_sleep:1;
1616 	unsigned is_demuxable_parent:1;
1617 	unsigned reuse_port:1;
1618 	unsigned use_aop_offload:1;
1619 
1620 	uuid_t parent_flow_uuid;
1621 	struct necp_demux_pattern demux_patterns[NECP_MAX_DEMUX_PATTERNS];
1622 	uint8_t demux_pattern_count;
1623 };
1624 
1625 struct necp_client_group_members {
1626 	size_t group_members_length;
1627 	u_int8_t *group_members __sized_by(group_members_length);
1628 };
1629 
1630 struct necp_client_error_parameters {
1631 	int32_t error;
1632 	bool force_report;
1633 };
1634 
1635 struct necp_client_agent_parameters {
1636 	union {
1637 		struct necp_client_nexus_parameters nexus_request;
1638 		u_int8_t close_token[QUIC_STATELESS_RESET_TOKEN_SIZE];
1639 		struct necp_client_group_members group_members;
1640 		struct necp_client_error_parameters error;
1641 	} u;
1642 };
1643 
1644 #define NECP_CLIENT_CBACTION_NONVIABLE  1
1645 #define NECP_CLIENT_CBACTION_VIABLE     2
1646 #define NECP_CLIENT_CBACTION_INITIAL    3
1647 
1648 struct necp_client_add_flow_default {
1649 	uuid_t agent_uuid;
1650 	uuid_t registration_id;
1651 	u_int16_t flags; // NECP_CLIENT_FLOW_FLAGS_*
1652 	u_int16_t stats_request_count;
1653 	struct necp_client_flow_stats stats_requests[1];
1654 } __attribute__((__packed__));
1655 
1656 typedef void (*necp_client_flow_cb)(void *handle, int action, uint32_t interface_index, uint32_t necp_flags, bool *viable);
1657 
1658 #if SKYWALK
1659 struct skmem_arena_mmap_info;
1660 
1661 extern pid_t necp_client_get_proc_pid_from_arena_info(struct skmem_arena_mmap_info *arena_info);
1662 
1663 extern void necp_client_early_close(uuid_t client_id); // Cause a single client to close stats, etc
1664 
1665 #endif /* SKYWALK */
1666 
1667 #endif /* BSD_KERNEL_PRIVATE */
1668 
1669 #ifdef KERNEL
1670 #ifdef KERNEL_PRIVATE
1671 struct nstat_domain_info;
1672 extern void necp_copy_inp_domain_info(struct inpcb *, struct socket *, struct nstat_domain_info *);
1673 extern void necp_with_inp_domain_name(struct socket *so, void *ctx, void (*with_func)(char *domain_name __null_terminated, void *ctx));
1674 extern bool net_domain_contains_hostname(char *hostname_string __null_terminated, char *domain_string __null_terminated);
1675 #endif /* KERNEL_PRIVATE */
1676 #endif /* KERNEL */
1677 
1678 #ifndef KERNEL
1679 
1680 extern int necp_match_policy(const uint8_t *parameters, size_t parameters_size, struct necp_aggregate_result *returned_result);
1681 
1682 extern int necp_open(int flags);
1683 
1684 extern int necp_client_action(int necp_fd, uint32_t action, uuid_t client_id,
1685     size_t client_id_len, uint8_t *buffer, size_t buffer_size);
1686 
1687 extern int necp_session_open(int flags);
1688 
1689 extern int necp_session_action(int necp_fd, uint32_t action,
1690     uint8_t *in_buffer, size_t in_buffer_length,
1691     uint8_t *out_buffer, size_t out_buffer_length);
1692 
1693 #endif /* !KERNEL */
1694 
1695 #endif /* PRIVATE */
1696 
1697 #endif
1698