forked from OpenVPN/ovpn-dco-win
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.cpp
268 lines (220 loc) · 8.54 KB
/
timer.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
/*
* 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.
*/
// without this include, RTL_GENERIC_TABLE in Device.h is undefined
#include <ntddk.h>
#include "bufferpool.h"
#include "driver.h"
#include "trace.h"
#include "timer.h"
#include "socket.h"
#include "peer.h"
static const UCHAR OvpnKeepaliveMessage[] = {
0x2a, 0x18, 0x7b, 0xf3, 0x64, 0x1e, 0xb4, 0xcb,
0x07, 0xed, 0x2d, 0x0a, 0x98, 0x1f, 0xc7, 0x48
};
// Context added to a timer's attributes
typedef struct _OVPN_PEER_TIMER_CONTEXT {
OvpnPeerContext* Peer;
LARGE_INTEGER lastXmit;
LARGE_INTEGER lastRecv;
// 0 means "not set"
LONG recvTimeout;
LONG xmitInterval;
} OVPN_PEER_TIMER_CONTEXT, * POVPN_PEER_TIMER_CONTEXT;
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(OVPN_PEER_TIMER_CONTEXT, OvpnGetPeerTimerContext);
_Use_decl_annotations_
BOOLEAN OvpnTimerIsKeepaliveMessage(const PUCHAR buf, SIZE_T len)
{
return RtlCompareMemory(buf, OvpnKeepaliveMessage, len) == sizeof(OvpnKeepaliveMessage);
}
static VOID OvpnTimerXmit(WDFTIMER timer)
{
POVPN_DEVICE device = OvpnGetDeviceContext(WdfTimerGetParentObject(timer));
POVPN_PEER_TIMER_CONTEXT timerCtx = OvpnGetPeerTimerContext(timer);
OVPN_TX_BUFFER* buffer;
NTSTATUS status;
LOG_IF_NOT_NT_SUCCESS(status = OvpnTxBufferPoolGet(device->TxBufferPool, &buffer));
if (!NT_SUCCESS(status)) {
LOG_EXIT();
return;
}
// copy keepalive magic message to the buffer
RtlCopyMemory(OvpnBufferPut(buffer, sizeof(OvpnKeepaliveMessage)), OvpnKeepaliveMessage, sizeof(OvpnKeepaliveMessage));
OvpnPeerContext* peer = timerCtx->Peer;
ExAcquireSpinLockSharedAtDpcLevel(&peer->SpinLock);
auto peerId = peer->PeerId;
auto addr = peer->TransportAddrs.Remote;
OvpnCryptoContext* cryptoContext = &peer->CryptoContext;
if (cryptoContext->Encrypt) {
// make space to crypto overhead
BOOLEAN pktId64bit = cryptoContext->CryptoOptions & CRYPTO_OPTIONS_64BIT_PKTID;
BOOLEAN aeadTagEnd = cryptoContext->CryptoOptions & CRYPTO_OPTIONS_AEAD_TAG_END;
OvpnTxBufferPush(buffer, OVPN_DATA_V2_LEN + (pktId64bit ? 8 : 4) + (aeadTagEnd ? 0 : AEAD_AUTH_TAG_LEN));
// in-place encrypt, always with primary key
status = cryptoContext->Encrypt(&cryptoContext->Primary, buffer->Data, buffer->Len, cryptoContext->CryptoOptions);
}
else {
status = STATUS_INVALID_DEVICE_STATE;
// LOG_WARN("CryptoContext not initialized");
}
ExReleaseSpinLockSharedFromDpcLevel(&peer->SpinLock);
if (NT_SUCCESS(status)) {
// start async send, completion handler will return ciphertext buffer to the pool
SOCKADDR* sa = (SOCKADDR*)&(addr);
LOG_IF_NOT_NT_SUCCESS(status = OvpnSocketSend(&device->Socket, buffer, sa));
if (NT_SUCCESS(status)) {
LOG_INFO("Ping sent", TraceLoggingValue(peerId, "peer-id"));
}
}
else {
OvpnTxBufferPoolPut(buffer);
}
}
static BOOLEAN OvpnTimerRecv(WDFTIMER timer)
{
POVPN_DEVICE device = OvpnGetDeviceContext(WdfTimerGetParentObject(timer));
POVPN_PEER_TIMER_CONTEXT timerCtx = OvpnGetPeerTimerContext(timer);
auto peerId = timerCtx->Peer->PeerId;
LOG_INFO("Keepalive timeout", TraceLoggingValue(peerId, "peer-id"));
WDFREQUEST request;
NTSTATUS status = STATUS_SUCCESS;
if (device->Mode == OVPN_MODE_P2P) {
status = WdfIoQueueRetrieveNextRequest(device->PendingReadsQueue, &request);
if (!NT_SUCCESS(status)) {
LOG_INFO("No pending request for keepalive timeout notification");
return FALSE;
}
ULONG_PTR bytesSent = 0;
WdfRequestCompleteWithInformation(request, STATUS_CONNECTION_DISCONNECTED, bytesSent);
}
else {
(VOID)OvpnDeletePeer(device, peerId);
status = WdfIoQueueRetrieveNextRequest(device->PendingNotificationRequestsQueue, &request);
if (!NT_SUCCESS(status)) {
LOG_INFO("Adding keepalive timeout notification to the queue");
return NT_SUCCESS(device->PendingNotificationsQueue.AddEvent(OVPN_NOTIFY_DEL_PEER, peerId, OVPN_DEL_PEER_REASON_EXPIRED));
}
else {
LOG_INFO("Notify userspace about expired peer");
OVPN_NOTIFY_EVENT *evt;
ULONG_PTR bytesSent = 0;
LOG_IF_NOT_NT_SUCCESS(status = WdfRequestRetrieveOutputBuffer(request, sizeof(OVPN_NOTIFY_EVENT), (PVOID*)&evt, nullptr));
if (NT_SUCCESS(status)) {
evt->Cmd = OVPN_NOTIFY_DEL_PEER;
evt->PeerId = peerId;
evt->DelPeerReason = OVPN_DEL_PEER_REASON_EXPIRED;
bytesSent = sizeof(OVPN_NOTIFY_EVENT);
}
WdfRequestCompleteWithInformation(request, status, bytesSent);
}
}
return NT_SUCCESS(status);
}
_Use_decl_annotations_
VOID OvpnTimerDestroy(WDFTIMER* timer)
{
if (*timer != WDF_NO_HANDLE) {
WdfTimerStop(*timer, FALSE);
WdfObjectDelete(*timer);
*timer = WDF_NO_HANDLE;
}
}
_Function_class_(EVT_WDF_TIMER)
static VOID OvpnTimerTick(WDFTIMER timer)
{
LARGE_INTEGER now;
KeQuerySystemTime(&now);
POVPN_PEER_TIMER_CONTEXT timerCtx = OvpnGetPeerTimerContext(timer);
if ((timerCtx->xmitInterval > 0) && (((now.QuadPart - timerCtx->lastXmit.QuadPart) / WDF_TIMEOUT_TO_SEC) > timerCtx->xmitInterval))
{
OvpnTimerXmit(timer);
timerCtx->lastXmit = now;
}
if ((timerCtx->recvTimeout > 0) && (((now.QuadPart - timerCtx->lastRecv.QuadPart) / WDF_TIMEOUT_TO_SEC) > timerCtx->recvTimeout))
{
// have we have completed pending read request?
if (OvpnTimerRecv(timer))
{
timerCtx->recvTimeout = 0; // one-off timer
}
}
}
_Use_decl_annotations_
NTSTATUS OvpnTimerCreate(WDFOBJECT parent, OvpnPeerContext* peer, _Inout_ WDFTIMER* timer)
{
LOG_ENTER();
if (*timer != WDF_NO_HANDLE) {
WdfTimerStop(*timer, FALSE);
WdfObjectDelete(*timer);
*timer = WDF_NO_HANDLE;
}
WDF_TIMER_CONFIG timerConfig;
WDF_TIMER_CONFIG_INIT(&timerConfig, OvpnTimerTick);
timerConfig.TolerableDelay = TolerableDelayUnlimited;
timerConfig.Period = 1000;
WDF_OBJECT_ATTRIBUTES timerAttributes;
WDF_OBJECT_ATTRIBUTES_INIT(&timerAttributes);
WDF_OBJECT_ATTRIBUTES_SET_CONTEXT_TYPE(&timerAttributes, OVPN_PEER_TIMER_CONTEXT);
timerAttributes.ParentObject = parent;
*timer = WDF_NO_HANDLE;
NTSTATUS status;
LOG_IF_NOT_NT_SUCCESS(status = WdfTimerCreate(&timerConfig, &timerAttributes, timer));
if (NT_SUCCESS(status)) {
POVPN_PEER_TIMER_CONTEXT pTimerContext = OvpnGetPeerTimerContext(*timer);
pTimerContext->Peer = peer;
WdfTimerStart(*timer, WDF_REL_TIMEOUT_IN_SEC(1));
}
LOG_EXIT();
return status;
}
#define CHECK_TIMER_HANDLE(timer) \
do { \
if ((timer) == WDF_NO_HANDLE) { \
LOG_ERROR("Timer handle is not initialized"); \
return; \
} \
} while (0)
VOID OvpnTimerSetXmitInterval(WDFTIMER timer, LONG xmitInterval)
{
CHECK_TIMER_HANDLE(timer);
POVPN_PEER_TIMER_CONTEXT timerCtx = OvpnGetPeerTimerContext(timer);
timerCtx->xmitInterval = xmitInterval;
KeQuerySystemTime(&timerCtx->lastXmit);
}
VOID OvpnTimerSetRecvTimeout(WDFTIMER timer, LONG recvTimeout)
{
CHECK_TIMER_HANDLE(timer);
POVPN_PEER_TIMER_CONTEXT timerCtx = OvpnGetPeerTimerContext(timer);
timerCtx->recvTimeout = recvTimeout;
KeQuerySystemTime(&timerCtx->lastRecv);
}
VOID OvpnTimerResetXmit(WDFTIMER timer)
{
CHECK_TIMER_HANDLE(timer);
POVPN_PEER_TIMER_CONTEXT timerCtx = OvpnGetPeerTimerContext(timer);
KeQuerySystemTime(&timerCtx->lastXmit);
}
VOID OvpnTimerResetRecv(WDFTIMER timer)
{
CHECK_TIMER_HANDLE(timer);
POVPN_PEER_TIMER_CONTEXT timerCtx = OvpnGetPeerTimerContext(timer);
KeQuerySystemTime(&timerCtx->lastRecv);
}