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