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 | |
72 | #if defined(NSTAT_EXTENSION_FILTER_DOMAIN_INFO) |
73 | uuid_t soflow_uuid; |
74 | nstat_context soflow_nstat_context; |
75 | #endif |
76 | }; |
77 | |
78 | /* |
79 | * struct soflow_db |
80 | * |
81 | * For each UDP socket, this is a hash table maintaining all flows |
82 | * keyed by the flow 4-tuples <lport,fport,laddr,faddr>. |
83 | */ |
84 | struct soflow_db { |
85 | os_refcnt_t soflow_db_ref_count; |
86 | struct socket *soflow_db_so; |
87 | uint32_t soflow_db_count; |
88 | struct soflow_hash_head *soflow_db_hashbase; |
89 | u_long soflow_db_hashmask; |
90 | struct soflow_hash_entry *soflow_db_only_entry; |
91 | |
92 | uint8_t soflow_db_debug:1; |
93 | uint8_t soflow_db_reserved:7; |
94 | uint64_t soflow_db_flags; |
95 | }; |
96 | |
97 | /* |
98 | * Flags describing the owner socket or the soflow_db |
99 | */ |
100 | #define SOFLOWF_SO_DELAYED_DEAD 0x0001 /* Delayed socket DEAD marking */ |
101 | |
102 | bool soflow_fill_hash_entry_from_address(struct soflow_hash_entry *, bool, struct sockaddr *, bool); |
103 | bool soflow_fill_hash_entry_from_inp(struct soflow_hash_entry *, bool, struct inpcb *, bool); |
104 | void *soflow_db_get_feature_context(struct soflow_db *, u_int64_t); |
105 | u_int64_t soflow_db_get_feature_context_id(struct soflow_db *, struct sockaddr *, struct sockaddr *); |
106 | |
107 | // Per each flow, allow feature to indicate if garbage collection is needed |
108 | typedef bool (*soflow_feat_gc_needed_func)(struct socket *so, struct soflow_hash_entry *hash_entry, u_int64_t current_time); |
109 | |
110 | // Per each flow, allow feature to perform garbage collection |
111 | typedef bool (*soflow_feat_gc_perform_func)(struct socket *so, struct soflow_hash_entry *hash_entry); |
112 | |
113 | // Per each flow, allow feature to detach and clean up context |
114 | typedef bool (*soflow_feat_detach_entry_func)(struct socket *so, struct soflow_hash_entry *hash_entry); |
115 | |
116 | // Per DB, allow feature to detach and clean up context |
117 | typedef bool (*soflow_feat_detach_db_func)(struct socket *so, struct soflow_db *db); |
118 | |
119 | void soflow_feat_set_functions(soflow_feat_gc_needed_func, soflow_feat_gc_perform_func, |
120 | soflow_feat_detach_entry_func, soflow_feat_detach_db_func); |
121 | |
122 | typedef bool (*soflow_entry_apply_func)(struct socket *so, |
123 | struct soflow_hash_entry *hash_entry, |
124 | void *context); |
125 | |
126 | bool soflow_db_apply(struct soflow_db *, soflow_entry_apply_func, void *context); |
127 | |
128 | #endif /* BSD_KERNEL_PRIVATE */ |
129 | |
130 | __END_DECLS |
131 | |
132 | #endif /* __SOCKET_FLOWS_H__ */ |
133 | |