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