xref: /xnu-11417.140.69/bsd/kern/socket_flows.h (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1 /*
2  * Copyright (c) 2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_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. Please obtain a copy of the License at
10  * http://www.opensource.apple.com/apsl/ and read it before using this
11  * file.
12  *
13  * The Original Code and all software distributed under the License are
14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18  * Please see the License for the specific language governing rights and
19  * limitations under the License.
20  *
21  * @APPLE_LICENSE_HEADER_END@
22  */
23 
24 #ifndef __SOCKET_FLOWS_H__
25 #define __SOCKET_FLOWS_H__
26 
27 
28 __BEGIN_DECLS
29 
30 #ifdef PRIVATE
31 
32 struct soflow_hash_entry {
33 	LIST_ENTRY(soflow_hash_entry)       soflow_entry_link;
34 	TAILQ_ENTRY(soflow_hash_entry)      soflow_entry_list_link;
35 	os_refcnt_t                         soflow_ref_count;
36 	struct soflow_db                    *soflow_db;
37 	uint16_t                            soflow_outifindex;
38 	in_port_t                           soflow_fport;
39 	in_port_t                           soflow_lport;
40 	sa_family_t                         soflow_family;
41 	uint32_t                            soflow_flowhash;
42 	uint64_t                            soflow_lastused;
43 	uint32_t                            soflow_faddr6_ifscope;
44 	uint32_t                            soflow_laddr6_ifscope;
45 	union {
46 		/* foreign host table entry */
47 		struct in_addr_4in6             addr46;
48 		struct in6_addr                 addr6;
49 	} soflow_faddr;
50 	union {
51 		/* local host table entry */
52 		struct in_addr_4in6             addr46;
53 		struct in6_addr                 addr6;
54 	} soflow_laddr;
55 	uint8_t                             soflow_outgoing: 1;
56 	uint8_t                             soflow_laddr_updated: 1;
57 	uint8_t                             soflow_lport_updated: 1;
58 	uint8_t                             soflow_gc: 1;
59 	uint8_t                             soflow_feat_gc: 1;
60 	uint8_t                             soflow_debug: 1;
61 	uint8_t                             soflow_reserved:2;
62 
63 	uint64_t                            soflow_rxpackets;
64 	uint64_t                            soflow_rxbytes;
65 	uint64_t                            soflow_txpackets;
66 	uint64_t                            soflow_txbytes;
67 
68 	// Feature support (i.e. CFIL, extensible to others)
69 	uint64_t                            soflow_feat_ctxt_id;
70 	void                                *soflow_feat_ctxt;
71 	uint32_t                            soflow_filter_control_unit;
72 	int32_t                             soflow_policies_gencount;
73 
74 #if defined(NSTAT_EXTENSION_FILTER_DOMAIN_INFO)
75 	uuid_t                              soflow_uuid;
76 	nstat_context                       soflow_nstat_context;
77 #endif
78 	struct timeval                      soflow_timestamp;
79 };
80 
81 #define SOFLOW_HASH_SIZE 16
82 LIST_HEAD(soflow_hash_head, soflow_hash_entry);
83 
84 /*
85  * struct soflow_db
86  *
87  * For each UDP socket, this is a hash table maintaining all flows
88  * keyed by the flow 4-tuples <lport,fport,laddr,faddr>.
89  */
90 struct soflow_db {
91 	os_refcnt_t                         soflow_db_ref_count;
92 	struct socket                       *soflow_db_so;
93 	uint32_t                            soflow_db_count;
94 	struct soflow_hash_head             * __counted_by(SOFLOW_HASH_SIZE) soflow_db_hashbase;
95 	u_long                              soflow_db_hashmask;
96 	struct soflow_hash_entry            *soflow_db_only_entry;
97 
98 	uint8_t                             soflow_db_debug:1;
99 	uint8_t                             soflow_db_reserved:7;
100 	uint64_t                            soflow_db_flags;
101 };
102 
103 /*
104  * Flags describing the owner socket or the soflow_db
105  */
106 #define SOFLOWF_SO_DELAYED_DEAD         0x0001  /* Delayed socket DEAD marking */
107 
108 bool soflow_fill_hash_entry_from_address(struct soflow_hash_entry *, bool, struct sockaddr *, bool);
109 bool soflow_fill_hash_entry_from_inp(struct soflow_hash_entry *, bool, struct inpcb *, bool);
110 void *soflow_db_get_feature_context(struct soflow_db *, u_int64_t);
111 u_int64_t soflow_db_get_feature_context_id(struct soflow_db *, struct sockaddr *, struct sockaddr *);
112 
113 // Per each flow, allow feature to indicate if garbage collection is needed
114 typedef bool (*soflow_feat_gc_needed_func)(struct socket *so, struct soflow_hash_entry *hash_entry, u_int64_t current_time);
115 
116 // Per each flow, allow feature to perform garbage collection
117 typedef bool (*soflow_feat_gc_perform_func)(struct socket *so, struct soflow_hash_entry *hash_entry);
118 
119 // Per each flow, allow feature to detach and clean up context
120 typedef bool (*soflow_feat_detach_entry_func)(struct socket *so, struct soflow_hash_entry *hash_entry);
121 
122 // Per DB, allow feature to detach and clean up context
123 typedef bool (*soflow_feat_detach_db_func)(struct socket *so, struct soflow_db *db);
124 
125 void soflow_feat_set_functions(soflow_feat_gc_needed_func, soflow_feat_gc_perform_func,
126     soflow_feat_detach_entry_func, soflow_feat_detach_db_func);
127 
128 typedef bool (*soflow_entry_apply_func)(struct socket *so,
129     struct soflow_hash_entry *hash_entry,
130     void *context);
131 
132 bool soflow_db_apply(struct soflow_db *, soflow_entry_apply_func, void *context);
133 
134 #endif /* BSD_KERNEL_PRIVATE */
135 
136 __END_DECLS
137 
138 #endif /* __SOCKET_FLOWS_H__ */
139