forked from OpenVPN/ovpn-dco-win
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.cpp
160 lines (136 loc) · 4.79 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
/*
* 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 "driverhelper\buffers.h"
#include "driver.h"
#include "driverhelper\trace.h"
#include "timer.h"
#include "socket.h"
static const UCHAR OvpnKeepaliveMessage[] = {
0x2a, 0x18, 0x7b, 0xf3, 0x64, 0x1e, 0xb4, 0xcb,
0x07, 0xed, 0x2d, 0x0a, 0x98, 0x1f, 0xc7, 0x48
};
_Use_decl_annotations_
BOOLEAN OvpnTimerIsKeepaliveMessage(const PUCHAR buf, SIZE_T len)
{
return RtlCompareMemory(buf, OvpnKeepaliveMessage, len) == sizeof(OvpnKeepaliveMessage);
}
_Function_class_(EVT_WDF_TIMER)
static VOID OvpnTimerXmit(WDFTIMER timer)
{
POVPN_DEVICE device = OvpnGetDeviceContext(WdfTimerGetParentObject(timer));
OVPN_TX_BUFFER* buffer;
NTSTATUS status;
LOG_IF_NOT_NT_SUCCESS(status = OvpnTxBufferPoolGet(device->TxPool, &buffer));
if (!NT_SUCCESS(status)) {
return;
}
// copy keepalive magic message to the buffer
RtlCopyMemory(OvpnTxBufferPut(buffer, sizeof(OvpnKeepaliveMessage)), OvpnKeepaliveMessage, sizeof(OvpnKeepaliveMessage));
KIRQL kiqrl = ExAcquireSpinLockShared(&device->SpinLock);
if (device->CryptoContext.Encrypt) {
// in-place encrypt, always with primary key
status = device->CryptoContext.Encrypt(&device->CryptoContext.Primary, buffer);
}
else {
status = STATUS_INVALID_DEVICE_STATE;
LOG_WARN("CryptoContext not initialized");
}
if (NT_SUCCESS(status)) {
// start async send, completion handler will return ciphertext buffer to the pool
BOOLEAN wskSendCalled = FALSE;
LOG_IF_NOT_NT_SUCCESS(status = OvpnSocketSendTxBuffer(&device->Socket, buffer, &wskSendCalled));
if (!NT_SUCCESS(status)) {
if (!wskSendCalled) {
OvpnTxBufferPoolPut(buffer);
}
}
else {
LOG_INFO("Ping sent");
}
}
else {
OvpnTxBufferPoolPut(buffer);
}
ExReleaseSpinLockShared(&device->SpinLock, kiqrl);
}
_Function_class_(EVT_WDF_TIMER)
static VOID OvpnTimerRecv(WDFTIMER timer)
{
LOG_WARN("Keepalive timeout");
POVPN_DEVICE device = OvpnGetDeviceContext(WdfTimerGetParentObject(timer));
WDFREQUEST request;
NTSTATUS status = WdfIoQueueRetrieveNextRequest(device->PendingReadsQueue, &request);
if (!NT_SUCCESS(status)) {
LOG_WARN("No pending request for keepalive timeout notification");
}
else {
ULONG_PTR bytesSent = 0;
WdfRequestCompleteWithInformation(request, STATUS_CONNECTION_DISCONNECTED, bytesSent);
}
}
_Use_decl_annotations_
VOID OvpnTimerDestroy(WDFTIMER* timer)
{
if (*timer != WDF_NO_HANDLE) {
WdfTimerStop(*timer, FALSE);
WdfObjectDelete(*timer);
*timer = WDF_NO_HANDLE;
}
}
static NTSTATUS OvpnTimerCreate(WDFOBJECT parent, ULONG period, PFN_WDF_TIMER func, _Inout_ WDFTIMER* timer)
{
if (*timer != WDF_NO_HANDLE) {
WdfTimerStop(*timer, FALSE);
WdfObjectDelete(*timer);
*timer = WDF_NO_HANDLE;
}
WDF_TIMER_CONFIG timerConfig;
WDF_TIMER_CONFIG_INIT(&timerConfig, func);
timerConfig.Period = period * 1000;
WDF_OBJECT_ATTRIBUTES timerAttributes;
WDF_OBJECT_ATTRIBUTES_INIT(&timerAttributes);
timerAttributes.ParentObject = parent;
return WdfTimerCreate(&timerConfig, &timerAttributes, timer);
}
_Use_decl_annotations_
NTSTATUS OvpnTimerXmitCreate(WDFOBJECT parent, ULONG period, WDFTIMER* timer)
{
NTSTATUS status;
LOG_IF_NOT_NT_SUCCESS(status = OvpnTimerCreate(parent, period, OvpnTimerXmit, timer));
return status;
}
_Use_decl_annotations_
NTSTATUS OvpnTimerRecvCreate(WDFOBJECT parent, WDFTIMER* timer)
{
NTSTATUS status;
LOG_IF_NOT_NT_SUCCESS(status = OvpnTimerCreate(parent, 0, OvpnTimerRecv, timer));
return status;
}
VOID OvpnTimerReset(WDFTIMER timer, ULONG dueTime)
{
if (timer != WDF_NO_HANDLE) {
// if timer has already been created this will reset "due time" value to the new one
WdfTimerStart(timer, WDF_REL_TIMEOUT_IN_SEC(dueTime));
}
else {
LOG_ERROR("Timer not initialized");
}
}