-
Notifications
You must be signed in to change notification settings - Fork 22
/
peer.cpp
318 lines (245 loc) · 10.8 KB
/
peer.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* ovpn-dco-win OpenVPN protocol accelerator for Windows
*
* Copyright (C) 2020-2021 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 <ntifs.h>
#include "trace.h"
#include "peer.h"
#include "timer.h"
#include "socket.h"
static
VOID
OvpnPeerZeroStats(POVPN_STATS stats)
{
InterlockedExchange(&stats->LostInControlPackets, 0);
InterlockedExchange(&stats->LostInDataPackets, 0);
InterlockedExchange(&stats->LostOutControlPackets, 0);
InterlockedExchange(&stats->LostOutDataPackets, 0);
InterlockedExchange(&stats->ReceivedControlPackets, 0);
InterlockedExchange(&stats->ReceivedDataPackets, 0);
InterlockedExchange(&stats->SentControlPackets, 0);
InterlockedExchange(&stats->SentDataPackets, 0);
InterlockedExchange64(&stats->TransportBytesReceived, 0);
InterlockedExchange64(&stats->TransportBytesSent, 0);
InterlockedExchange64(&stats->TunBytesReceived, 0);
InterlockedExchange64(&stats->TunBytesSent, 0);
}
_Use_decl_annotations_
NTSTATUS
OvpnPeerNew(POVPN_DEVICE device, WDFREQUEST request)
{
LOG_ENTER();
POVPN_NEW_PEER peer = NULL;
NTSTATUS status;
GOTO_IF_NOT_NT_SUCCESS(done, status, WdfRequestRetrieveInputBuffer(request, sizeof(OVPN_NEW_PEER), (PVOID*)&peer, nullptr));
ULONG newPid = IoGetRequestorProcessId(WdfRequestWdmGetIrp(request));
LONG existingPid = InterlockedCompareExchange(&device->UserspacePid, 0, 0);
if (existingPid != 0) {
LOG_INFO("Peer already added, deleting existing peer");
LOG_IF_NOT_NT_SUCCESS(OvpnPeerDel(device));
}
InterlockedExchange(&device->UserspacePid, newPid);
LOG_INFO("Userspace client connected", TraceLoggingValue(newPid, "pid"));
POVPN_DRIVER driver = OvpnGetDriverContext(WdfGetDriver());
PWSK_SOCKET socket = NULL;
BOOLEAN proto_tcp = peer->Proto == OVPN_PROTO_TCP;
SIZE_T remoteAddrSize = peer->Remote.Addr4.sin_family == AF_INET ? sizeof(peer->Remote.Addr4) : sizeof(peer->Remote.Addr6);
GOTO_IF_NOT_NT_SUCCESS(done, status, OvpnSocketInit(&driver->WskProviderNpi, &driver->WskRegistration, peer->Local.Addr4.sin_family, proto_tcp, (PSOCKADDR)&peer->Local,
(PSOCKADDR)&peer->Remote, remoteAddrSize, device, &socket));
BCRYPT_ALG_HANDLE aesAlgHandle = NULL, chachaAlgHandle = NULL;
GOTO_IF_NOT_NT_SUCCESS(done, status, OvpnCryptoInitAlgHandles(&aesAlgHandle, &chachaAlgHandle));
KIRQL kirql = ExAcquireSpinLockExclusive(&device->SpinLock);
RtlZeroMemory(&device->CryptoContext, sizeof(OvpnCryptoContext));
device->CryptoContext.AesAlgHandle = aesAlgHandle;
device->CryptoContext.ChachaAlgHandle = chachaAlgHandle;
device->Socket.Socket = socket;
device->Socket.Tcp = proto_tcp;
RtlZeroMemory(&device->Socket.TcpState, sizeof(OvpnSocketTcpState));
RtlZeroMemory(&device->Socket.UdpState, sizeof(OvpnSocketUdpState));
ExReleaseSpinLockExclusive(&device->SpinLock, kirql);
OvpnPeerZeroStats(&device->Stats);
GOTO_IF_NOT_NT_SUCCESS(done, status, OvpnTimerCreate(device->WdfDevice, &device->Timer));
if (proto_tcp) {
LOG_IF_NOT_NT_SUCCESS(status = WdfRequestForwardToIoQueue(request, device->PendingNewPeerQueue));
// start async connect
LOG_IF_NOT_NT_SUCCESS(status = OvpnSocketTcpConnect(socket, device, (PSOCKADDR)&peer->Remote));
}
done:
LOG_EXIT();
return status;
}
_Use_decl_annotations_
NTSTATUS
OvpnPeerDel(POVPN_DEVICE device)
{
LOG_ENTER();
if (InterlockedCompareExchange(&device->UserspacePid, 0, 0) == 0) {
LOG_INFO("Peer not added.");
return STATUS_INVALID_DEVICE_REQUEST;
}
BCRYPT_ALG_HANDLE aesAlgHandle = NULL, chachaAlgHandle = NULL;
KIRQL kirql = ExAcquireSpinLockExclusive(&device->SpinLock);
OvpnTimerDestroy(&device->Timer);
aesAlgHandle = device->CryptoContext.AesAlgHandle;
chachaAlgHandle = device->CryptoContext.ChachaAlgHandle;
OvpnCryptoUninit(&device->CryptoContext);
InterlockedExchange(&device->UserspacePid, 0);
PWSK_SOCKET socket = device->Socket.Socket;
device->Socket.Socket = NULL;
RtlZeroMemory(&device->Socket.TcpState, sizeof(OvpnSocketTcpState));
RtlZeroMemory(&device->Socket.UdpState, sizeof(OvpnSocketUdpState));
// OvpnCryptoUninitAlgHandles and OvpnSocketClose require PASSIVE_LEVEL, so must release lock
ExReleaseSpinLockExclusive(&device->SpinLock, kirql);
OvpnCryptoUninitAlgHandles(aesAlgHandle, chachaAlgHandle);
LOG_IF_NOT_NT_SUCCESS(OvpnSocketClose(socket));
// flush buffers in control queue so that client won't get control channel messages from previous session
while (LIST_ENTRY* entry = OvpnBufferQueueDequeue(device->ControlRxBufferQueue)) {
OVPN_RX_BUFFER* buffer = CONTAINING_RECORD(entry, OVPN_RX_BUFFER, QueueListEntry);
// return buffer back to pool
OvpnRxBufferPoolPut(buffer);
}
WDFREQUEST request;
while (NT_SUCCESS(WdfIoQueueRetrieveNextRequest(device->PendingReadsQueue, &request))) {
ULONG_PTR bytesCopied = 0;
LOG_INFO("Cancel IO request from manual queue");
WdfRequestCompleteWithInformation(request, STATUS_CANCELLED, bytesCopied);
}
LOG_EXIT();
return STATUS_SUCCESS;
}
_Use_decl_annotations_
NTSTATUS OvpnPeerSet(POVPN_DEVICE device, WDFREQUEST request)
{
LOG_ENTER();
if (InterlockedCompareExchange(&device->UserspacePid, 0, 0) == 0) {
LOG_ERROR("Peer not added");
return STATUS_INVALID_DEVICE_REQUEST;
}
POVPN_SET_PEER peer = NULL;
NTSTATUS status;
GOTO_IF_NOT_NT_SUCCESS(done, status, WdfRequestRetrieveInputBuffer(request, sizeof(OVPN_SET_PEER), (PVOID*)&peer, nullptr));
LOG_INFO("Set peer", TraceLoggingValue(peer->KeepaliveInterval, "interval"),
TraceLoggingValue(peer->KeepaliveTimeout, "timeout"),
TraceLoggingValue(peer->MSS, "MSS"));
if (peer->MSS != -1) {
device->MSS = (UINT16)peer->MSS;
}
if (peer->KeepaliveInterval != -1) {
device->KeepaliveInterval = peer->KeepaliveInterval;
// keepalive xmit timer, sends ping packets
OvpnTimerSetXmitInterval(device->Timer, peer->KeepaliveInterval);
}
if (peer->KeepaliveTimeout != -1) {
device->KeepaliveTimeout = peer->KeepaliveTimeout;
// keepalive recv timer, detects keepalive timeout
OvpnTimerSetRecvTimeout(device->Timer, peer->KeepaliveTimeout);
}
done:
LOG_EXIT();
return status;
}
_Use_decl_annotations_
NTSTATUS
OvpnPeerGetStats(POVPN_DEVICE device, WDFREQUEST request, ULONG_PTR* bytesReturned)
{
if (InterlockedCompareExchange(&device->UserspacePid, 0, 0) == 0) {
LOG_ERROR("Peer not added");
return STATUS_INVALID_DEVICE_REQUEST;
}
POVPN_STATS stats = NULL;
NTSTATUS status;
GOTO_IF_NOT_NT_SUCCESS(done, status, WdfRequestRetrieveOutputBuffer(request, sizeof(OVPN_STATS), (PVOID*)&stats, NULL));
stats->LostInControlPackets = InterlockedCompareExchangeNoFence(&device->Stats.LostInControlPackets, 0, 0);
stats->LostInDataPackets = InterlockedCompareExchangeNoFence(&device->Stats.LostInDataPackets, 0, 0);
stats->LostOutControlPackets = InterlockedCompareExchangeNoFence(&device->Stats.LostOutControlPackets, 0, 0);
stats->LostOutDataPackets = InterlockedCompareExchangeNoFence(&device->Stats.LostOutDataPackets, 0, 0);
stats->ReceivedControlPackets = InterlockedCompareExchangeNoFence(&device->Stats.ReceivedControlPackets, 0, 0);
stats->ReceivedDataPackets = InterlockedCompareExchangeNoFence(&device->Stats.ReceivedDataPackets, 0, 0);
stats->SentControlPackets = InterlockedCompareExchangeNoFence(&device->Stats.SentControlPackets, 0, 0);
stats->SentDataPackets = InterlockedCompareExchangeNoFence(&device->Stats.SentDataPackets, 0, 0);
stats->TransportBytesReceived = InterlockedCompareExchangeNoFence64(&device->Stats.TransportBytesReceived, 0, 0);
stats->TransportBytesSent = InterlockedCompareExchangeNoFence64(&device->Stats.TransportBytesSent, 0, 0);
stats->TunBytesReceived = InterlockedCompareExchangeNoFence64(&device->Stats.TunBytesReceived, 0, 0);
stats->TunBytesSent = InterlockedCompareExchangeNoFence64(&device->Stats.TunBytesSent, 0, 0);
*bytesReturned = sizeof(OVPN_STATS);
done:
return status;
}
_Use_decl_annotations_
NTSTATUS
OvpnPeerStartVPN(POVPN_DEVICE device)
{
LOG_ENTER();
if (InterlockedCompareExchange(&device->UserspacePid, 0, 0) == 0) {
LOG_ERROR("Peer not added");
return STATUS_INVALID_DEVICE_REQUEST;
}
OvpnAdapterSetLinkState(OvpnGetAdapterContext(device->Adapter), MediaConnectStateConnected);
LOG_EXIT();
return STATUS_SUCCESS;
}
_Use_decl_annotations_
NTSTATUS
OvpnPeerNewKey(POVPN_DEVICE device, WDFREQUEST request)
{
LOG_ENTER();
if (InterlockedCompareExchange(&device->UserspacePid, 0, 0) == 0) {
LOG_ERROR("Peer not added");
return STATUS_INVALID_DEVICE_REQUEST;
}
POVPN_CRYPTO_DATA cryptoData = NULL;
OVPN_CRYPTO_DATA_V2 cryptoDataV2{};
NTSTATUS status;
GOTO_IF_NOT_NT_SUCCESS(done, status, WdfRequestRetrieveInputBuffer(request, sizeof(OVPN_CRYPTO_DATA), (PVOID*)&cryptoData, nullptr));
RtlCopyMemory(&cryptoDataV2.V1, cryptoData, sizeof(OVPN_CRYPTO_DATA));
GOTO_IF_NOT_NT_SUCCESS(done, status, OvpnCryptoNewKey(&device->CryptoContext, &cryptoDataV2));
done:
LOG_EXIT();
return status;
}
_Use_decl_annotations_
NTSTATUS
OvpnPeerNewKeyV2(POVPN_DEVICE device, WDFREQUEST request)
{
LOG_ENTER();
if (InterlockedCompareExchange(&device->UserspacePid, 0, 0) == 0) {
LOG_ERROR("Peer not added");
return STATUS_INVALID_DEVICE_REQUEST;
}
POVPN_CRYPTO_DATA_V2 cryptoData = NULL;
NTSTATUS status;
GOTO_IF_NOT_NT_SUCCESS(done, status, WdfRequestRetrieveInputBuffer(request, sizeof(OVPN_CRYPTO_DATA_V2), (PVOID*)&cryptoData, nullptr));
GOTO_IF_NOT_NT_SUCCESS(done, status, OvpnCryptoNewKey(&device->CryptoContext, cryptoData));
done:
LOG_EXIT();
return status;
}
_Use_decl_annotations_
NTSTATUS
OvpnPeerSwapKeys(POVPN_DEVICE device)
{
LOG_ENTER();
if (InterlockedCompareExchange(&device->UserspacePid, 0, 0) == 0) {
LOG_ERROR("Peer not added");
return STATUS_INVALID_DEVICE_REQUEST;
}
OvpnCryptoSwapKeys(&device->CryptoContext);
LOG_EXIT();
return STATUS_SUCCESS;
}