-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtrie.cpp
280 lines (224 loc) · 7.67 KB
/
trie.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* ovpn-dco-win OpenVPN protocol accelerator for Windows
*
* Copyright (C) 2024- OpenVPN Inc <[email protected]>
*
* Author: Lev Stipakov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "trie.h"
#include "trace.h"
#include "peer.h"
class IPTrie::TrieNode {
public:
TrieNode* children[2]; // Two branches: 0 and 1
OvpnPeerContext* peer; // Associated peer context (if this is a valid prefix)
bool isRoute; // Marks if this node represents a valid route
TrieNode() : peer(nullptr), isRoute(false) {
children[0] = nullptr;
children[1] = nullptr;
}
static TrieNode* AllocateNode() {
TrieNode* node = (TrieNode*)ExAllocatePool2(POOL_FLAG_NON_PAGED, sizeof(TrieNode), 'ovpn');
if (node) {
RtlZeroMemory(node, sizeof(TrieNode));
}
return node;
}
static void FreeNode(TrieNode* node) {
if (node) {
ExFreePoolWithTag(node, 'ovpn');
}
}
};
VOID
IPTrie::Init(BOOLEAN isIPv6) {
maxBits = isIPv6 ? 128 : 32;
root = nullptr;
}
VOID
IPTrie::FreeTrie(TrieNode* node) {
if (!node) return;
if (node->children[0]) FreeTrie(node->children[0]);
if (node->children[1]) FreeTrie(node->children[1]);
TrieNode::FreeNode(node);
}
VOID
IPTrie::Cleanup() {
LOG_ENTER();
KIRQL oldIrql;
LIST_ENTRY cleanupList;
InitializeListHead(&cleanupList);
oldIrql = ExAcquireSpinLockExclusive(&Lock);
if (root) {
CleanupNode(root, &cleanupList);
root = nullptr; // Reset the root after cleaning up
}
ExReleaseSpinLockExclusive(&Lock, oldIrql);
// Perform deferred cleanup of peers outside the lock
PLIST_ENTRY entry;
while (!IsListEmpty(&cleanupList)) {
entry = RemoveHeadList(&cleanupList);
OvpnPeerContext* peer = CONTAINING_RECORD(entry, OvpnPeerContext, ListEntry);
OvpnPeerCtxRelease(peer);
}
LOG_EXIT();
}
VOID IPTrie::CleanupNode(TrieNode* node, PLIST_ENTRY cleanupList) {
if (!node) return;
CleanupNode(node->children[0], cleanupList);
CleanupNode(node->children[1], cleanupList);
if (node->peer) {
InsertTailList(cleanupList, &node->peer->ListEntry);
node->peer = nullptr;
}
TrieNode::FreeNode(node);
}
NTSTATUS
IPTrie::Insert(const UCHAR* ip, int prefixLength, OvpnPeerContext* peer) {
LOG_ENTER();
NTSTATUS status = STATUS_SUCCESS;
KIRQL kirql = ExAcquireSpinLockExclusive(&Lock);
if (!root) {
root = TrieNode::AllocateNode();
if (!root) {
ExReleaseSpinLockExclusive(&Lock, kirql);
status = STATUS_INSUFFICIENT_RESOURCES;
goto done;
}
}
TrieNode* current = root;
for (int i = 0; i < prefixLength && i < maxBits; ++i) {
int bit = (ip[i / 8] >> (7 - (i % 8))) & 1;
if (!current->children[bit]) {
current->children[bit] = TrieNode::AllocateNode();
if (!current->children[bit]) {
ExReleaseSpinLockExclusive(&Lock, kirql);
status = STATUS_INSUFFICIENT_RESOURCES;
goto done;
}
}
current = current->children[bit];
}
// update the node with the peer info
current->peer = peer;
current->isRoute = true;
// increment peer refcnt since it has been stored in a trie
InterlockedIncrement(&peer->RefCounter);
ExReleaseSpinLockExclusive(&Lock, kirql);
done:
LOG_EXIT();
return status;
}
OvpnPeerContext*
IPTrie::Find(const UCHAR* ip) {
if (!root) return nullptr;
KIRQL kirql = ExAcquireSpinLockShared(&Lock);
TrieNode* current = root;
OvpnPeerContext* peer = nullptr;
for (int i = 0; i < maxBits && current; ++i) {
if (current->isRoute) {
peer = current->peer;
}
// Calculate the next bit of the IP address
int bit = (ip[i / 8] >> (7 - (i % 8))) & 1;
current = current->children[bit];
}
if (current && current->isRoute) {
peer = current->peer;
}
ExReleaseSpinLockShared(&Lock, kirql);
// before returning the peer, increment refcnt
if (peer) {
InterlockedIncrement(&peer->RefCounter);
}
return peer;
}
VOID
IPTrie::RemoveByPeerId(INT32 peerId) {
LOG_ENTER();
LIST_ENTRY cleanupList;
InitializeListHead(&cleanupList);
// collect nodes to be deleted into the list
KIRQL oldIrql = ExAcquireSpinLockExclusive(&Lock);
root = RemoveByPeerId(root, peerId, &cleanupList);
ExReleaseSpinLockExclusive(&Lock, oldIrql);
// perform cleanup outside of the lock
PLIST_ENTRY entry;
while (!IsListEmpty(&cleanupList)) {
entry = RemoveHeadList(&cleanupList);
OvpnPeerContext* peer = CONTAINING_RECORD(entry, OvpnPeerContext, ListEntry);
OvpnPeerCtxRelease(peer);
}
LOG_EXIT();
}
IPTrie::TrieNode*
IPTrie::RemoveByPeerId(TrieNode* node, INT32 peerId, PLIST_ENTRY cleanupList) {
if (!node) return nullptr;
// Recursively process left and right children
node->children[0] = RemoveByPeerId(node->children[0], peerId, cleanupList);
node->children[1] = RemoveByPeerId(node->children[1], peerId, cleanupList);
// Check if this node's peer matches the target peerId
if (node->peer && node->peer->PeerId == peerId) {
// if we're last to hold a reference, defer the cleanup by adding the peer to the cleanup list
if (InterlockedDecrement(&node->peer->RefCounter) == 0) {
InsertTailList(cleanupList, &node->peer->ListEntry);
}
node->peer = nullptr;
node->isRoute = false;
}
// If this node has no children and is no longer a route, delete it
if (!node->children[0] && !node->children[1] && !node->isRoute) {
TrieNode::FreeNode(node);
return nullptr;
}
return node;
}
NTSTATUS
IPTrie::Remove(const UCHAR* ip, int prefixLength) {
if (prefixLength < 0 || prefixLength > maxBits) {
return STATUS_INVALID_PARAMETER;
}
OvpnPeerContext* peerToRelease = nullptr;
KIRQL oldIrql = ExAcquireSpinLockExclusive(&Lock);
root = RemoveRouteNode(root, ip, prefixLength, &peerToRelease);
ExReleaseSpinLockExclusive(&Lock, oldIrql);
// Release the peer outside the lock if needed
if (peerToRelease) {
OvpnPeerCtxRelease(peerToRelease);
}
return STATUS_SUCCESS;
}
IPTrie::TrieNode*
IPTrie::RemoveRouteNode(TrieNode* node, const UCHAR* ip, int prefixLength, OvpnPeerContext** peerToRelease) {
if (!node) return nullptr;
if (prefixLength > 0) {
int bit = (ip[(maxBits - prefixLength) / 8] >> (7 - ((maxBits - prefixLength) % 8))) & 1;
node->children[bit] = RemoveRouteNode(node->children[bit], ip, prefixLength - 1, peerToRelease);
}
else {
if (node->peer) {
*peerToRelease = node->peer;
node->peer = nullptr;
}
node->isRoute = false;
}
// Cleanup: If the node has no children and is not a route, delete it
if (!node->children[0] && !node->children[1] && !node->isRoute) {
TrieNode::FreeNode(node);
return nullptr;
}
return node;
}