xref: /xnu-11215.41.3/bsd/netinet/flow_divert.h (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 /*
2  * Copyright (c) 2012-2017, 2020, 2022, 2024 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 __FLOW_DIVERT_H__
30 #define __FLOW_DIVERT_H__
31 
32 #include <sys/mbuf.h>
33 
34 #define FLOW_DIVERT_ORDER_LAST      INT_MAX
35 
36 struct flow_divert_group;
37 
38 struct flow_divert_trie_node {
39 	uint16_t                        start;
40 	uint16_t                        length;
41 	uint16_t                        child_map;
42 };
43 
44 
45 struct flow_divert_pcb {
46 	decl_lck_mtx_data(, mtx);
47 	socket_t                        so;
48 	RB_ENTRY(flow_divert_pcb)       rb_link;
49 	uint32_t                        hash;
50 	mbuf_t                          connect_token;
51 	uint32_t                        flags;
52 	uint32_t                        send_window;
53 	struct flow_divert_group        *group;
54 	uint32_t                        control_group_unit;
55 	uint32_t                        aggregate_unit;
56 	uint32_t                        policy_control_unit;
57 	int32_t                         ref_count;
58 	uint64_t                        bytes_written_by_app;
59 	uint64_t                        bytes_sent;
60 	uint64_t                        bytes_received;
61 	uint8_t                         log_level;
62 	SLIST_ENTRY(flow_divert_pcb)    tmp_list_entry;
63 	mbuf_t                          connect_packet;
64 	uint8_t                         *app_data __counted_by(app_data_length);
65 	size_t                          app_data_length;
66 	union sockaddr_in_4_6           local_endpoint;
67 	struct sockaddr                 *original_remote_endpoint;
68 	struct ifnet                    *original_last_outifp6;
69 	struct ifnet                    *original_last_outifp;
70 	uint8_t                         original_vflag;
71 };
72 
73 RB_HEAD(fd_pcb_tree, flow_divert_pcb);
74 
75 struct flow_divert_trie {
76 	struct flow_divert_trie_node    *nodes      __counted_by(nodes_count);
77 	uint16_t                        *child_maps __sized_by(child_maps_size);
78 	uint8_t                         *bytes      __counted_by(bytes_count);
79 	void                            *memory     __sized_by(memory_size);
80 	uint16_t                        nodes_count;
81 	uint16_t                        child_maps_count;
82 	uint16_t                        bytes_count;
83 	uint16_t                        nodes_free_next;
84 	uint16_t                        child_maps_free_next;
85 	uint16_t                        bytes_free_next;
86 	uint16_t                        root;
87 	size_t                          memory_size;
88 	size_t                          child_maps_size;
89 };
90 
91 struct flow_divert_group {
92 	decl_lck_rw_data(, lck);
93 	TAILQ_ENTRY(flow_divert_group)  chain;
94 	struct fd_pcb_tree              pcb_tree;
95 	uint32_t                        ctl_unit;
96 	uint8_t                         atomic_bits;
97 	MBUFQ_HEAD(send_queue_head)     send_queue;
98 	uint8_t                         *token_key __counted_by(token_key_size);
99 	size_t                          token_key_size;
100 	uint32_t                        flags;
101 	struct flow_divert_trie         signing_id_trie;
102 	int32_t                         ref_count;
103 	pid_t                           in_process_pid;
104 	int32_t                         order;
105 };
106 
107 void            flow_divert_init(void);
108 void            flow_divert_detach(struct socket *so);
109 errno_t         flow_divert_token_set(struct socket *so, struct sockopt *sopt);
110 errno_t         flow_divert_token_get(struct socket *so, struct sockopt *sopt);
111 errno_t         flow_divert_pcb_init(struct socket *so);
112 errno_t         flow_divert_connect_out(struct socket *so, struct sockaddr *to, proc_t p);
113 errno_t         flow_divert_implicit_data_out(struct socket *so, int flags, mbuf_t data, struct sockaddr *to, mbuf_t control, struct proc *p);
114 
115 #endif /* __FLOW_DIVERT_H__ */
116