Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More robust way to get a version info #76

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 37 additions & 20 deletions Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <wdfrequest.h>

#include "bufferpool.h"
#include "control.h"
#include "driver.h"
#include "trace.h"
#include "peer.h"
Expand All @@ -49,13 +50,27 @@ OvpnEvtDriverUnload(_In_ WDFDRIVER driver)
{
UNREFERENCED_PARAMETER(driver);

LOG_ENTER();
LOG_EXIT();

TraceLoggingUnregister(g_hOvpnEtwProvider);

// tail call optimization incorrectly eliminates TraceLoggingUnregister() call
// add __nop() to prevent TCO
__nop();
}

EVT_WDF_OBJECT_CONTEXT_CLEANUP OvpnEvtDriverCleanup;

_Use_decl_annotations_
VOID OvpnEvtDriverCleanup(_In_ WDFOBJECT driver)
{
UNREFERENCED_PARAMETER(driver);

LOG_ENTER();
LOG_EXIT();
}

EXTERN_C DRIVER_INITIALIZE DriverEntry;

#ifdef ALLOC_PRAGMA
Expand All @@ -80,6 +95,7 @@ DriverEntry(_In_ PDRIVER_OBJECT driverObject, _In_ PUNICODE_STRING registryPath)
WDF_OBJECT_ATTRIBUTES driverAttrs;
WDF_OBJECT_ATTRIBUTES_INIT(&driverAttrs);
WDF_OBJECT_ATTRIBUTES_SET_CONTEXT_TYPE(&driverAttrs, OVPN_DRIVER);
driverAttrs.EvtCleanupCallback = OvpnEvtDriverCleanup;

WDF_DRIVER_CONFIG driverConfig;
WDF_DRIVER_CONFIG_INIT(&driverConfig, OvpnEvtDeviceAdd);
Expand Down Expand Up @@ -212,26 +228,6 @@ OvpnEvtIoWrite(WDFQUEUE queue, WDFREQUEST request, size_t length)
ExReleaseSpinLockShared(&device->SpinLock, kiqrl);
}

NTSTATUS
OvpnGetVersion(WDFREQUEST request, _Out_ ULONG_PTR* bytesReturned)
{
*bytesReturned = 0;

NTSTATUS status;
POVPN_VERSION version = NULL;
GOTO_IF_NOT_NT_SUCCESS(done, status, WdfRequestRetrieveOutputBuffer(request, sizeof(OVPN_VERSION), (PVOID*)&version, NULL));

version->Major = OVPN_DCO_VERSION_MAJOR;
version->Minor = OVPN_DCO_VERSION_MINOR;
version->Patch = OVPN_DCO_VERSION_PATCH;

*bytesReturned = sizeof(OVPN_VERSION);

done:
return status;
}


EVT_WDF_IO_QUEUE_IO_DEVICE_CONTROL OvpnEvtIoDeviceControl;

_Use_decl_annotations_
Expand Down Expand Up @@ -336,6 +332,16 @@ VOID OvpnEvtDeviceCleanup(WDFOBJECT obj) {
device->Adapter = WDF_NO_HANDLE;
ExReleaseSpinLockExclusive(&device->SpinLock, irql);

// delete control device if there are no devices left
POVPN_DRIVER driverCtx = OvpnGetDriverContext(WdfGetDriver());
LONG deviceCount = InterlockedDecrement(&driverCtx->DeviceCount);
LOG_INFO("Device count", TraceLoggingValue(deviceCount, "deviceCount"));
if ((deviceCount == 0) && (driverCtx->ControlDevice != NULL)) {
LOG_INFO("Delete control device");
WdfObjectDelete(driverCtx->ControlDevice);
driverCtx->ControlDevice = NULL;
}

LOG_EXIT();
}

Expand Down Expand Up @@ -433,6 +439,17 @@ OvpnEvtDeviceAdd(WDFDRIVER wdfDriver, PWDFDEVICE_INIT deviceInit) {
WDFDEVICE wdfDevice;
GOTO_IF_NOT_NT_SUCCESS(done, status, WdfDeviceCreate(&deviceInit, &objAttributes, &wdfDevice));

POVPN_DRIVER driverCtx = OvpnGetDriverContext(WdfGetDriver());
InterlockedIncrement(&driverCtx->DeviceCount);

LOG_INFO("Device count", TraceLoggingValue(driverCtx->DeviceCount, "count"));

if (driverCtx->DeviceCount == 1)
{
// create non-exclusive control device to get the version information
GOTO_IF_NOT_NT_SUCCESS(done, status, OvpnCreateControlDevice(wdfDriver));
}

// this will fail if one device has already been created but that's ok, since
// openvpn2/3 accesses devices via Device Interface GUID, and symlink is used only by test client.
LOG_IF_NOT_NT_SUCCESS(WdfDeviceCreateSymbolicLink(wdfDevice, &symLink));
Expand Down
2 changes: 2 additions & 0 deletions Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ EVT_WDF_IO_QUEUE_IO_DEVICE_CONTROL OvpnEvtIoDeviceControl;
typedef struct _OVPN_DRIVER {
WSK_PROVIDER_NPI WskProviderNpi;
WSK_REGISTRATION WskRegistration;
WDFDEVICE ControlDevice;
LONG DeviceCount;
} OVPN_DRIVER, * POVPN_DRIVER;
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(OVPN_DRIVER, OvpnGetDriverContext)

Expand Down
4 changes: 2 additions & 2 deletions PropertySheet.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros">
<OVPN_DCO_VERSION_MAJOR>1</OVPN_DCO_VERSION_MAJOR>
<OVPN_DCO_VERSION_MINOR>2</OVPN_DCO_VERSION_MINOR>
<OVPN_DCO_VERSION_PATCH>1</OVPN_DCO_VERSION_PATCH>
<OVPN_DCO_VERSION_MINOR>3</OVPN_DCO_VERSION_MINOR>
<OVPN_DCO_VERSION_PATCH>0</OVPN_DCO_VERSION_PATCH>
</PropertyGroup>
<PropertyGroup />
<ItemDefinitionGroup>
Expand Down
128 changes: 128 additions & 0 deletions control.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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 "control.h"
#include "Driver.h"
#include "uapi\ovpn-dco.h"
#include "trace.h"

_Use_decl_annotations_
NTSTATUS
OvpnGetVersion(WDFREQUEST request, ULONG_PTR* bytesReturned)
{
LOG_ENTER();

*bytesReturned = 0;

NTSTATUS status;
POVPN_VERSION version = NULL;
GOTO_IF_NOT_NT_SUCCESS(done, status, WdfRequestRetrieveOutputBuffer(request, sizeof(OVPN_VERSION), (PVOID*)&version, NULL));

version->Major = OVPN_DCO_VERSION_MAJOR;
version->Minor = OVPN_DCO_VERSION_MINOR;
version->Patch = OVPN_DCO_VERSION_PATCH;

LOG_INFO("Version", TraceLoggingValue(version->Major, "Major"), TraceLoggingValue(version->Minor, "Minor"), TraceLoggingValue(version->Patch, "Patch"));

*bytesReturned = sizeof(OVPN_VERSION);

done:
LOG_EXIT();

return status;
}

EVT_WDF_IO_QUEUE_IO_DEVICE_CONTROL OvpnEvtControlDeviceIOControl;

VOID
OvpnEvtControlDeviceIOControl(WDFQUEUE queue, WDFREQUEST request, size_t outputBufferLength, size_t inputBufferLength, ULONG ioControlCode)
{
UNREFERENCED_PARAMETER(queue);
UNREFERENCED_PARAMETER(inputBufferLength);
UNREFERENCED_PARAMETER(outputBufferLength);

NTSTATUS status = STATUS_SUCCESS;
ULONG_PTR bytesReturned = 0;

switch (ioControlCode)
{
case OVPN_IOCTL_GET_VERSION:
status = OvpnGetVersion(request, &bytesReturned);
break;

default:
status = STATUS_INVALID_DEVICE_REQUEST;
break;
}

WdfRequestCompleteWithInformation(request, status, bytesReturned);
}

NTSTATUS
OvpnCreateControlDevice(WDFDRIVER wdfDriver)
{
LOG_ENTER();

DECLARE_CONST_UNICODE_STRING(symLink, L"\\DosDevices\\ovpn-dco-ver"); // this will be used by CreateFile
DECLARE_CONST_UNICODE_STRING(deviceName, L"\\Device\\ovpn-dco-ver"); // this is required tp create symlink

// allocate control device initialization structure
PWDFDEVICE_INIT deviceInit = WdfControlDeviceInitAllocate(wdfDriver, &SDDL_DEVOBJ_SYS_ALL_ADM_RWX_WORLD_R_RES_R);
if (deviceInit == NULL)
{
return STATUS_INSUFFICIENT_RESOURCES;
}

// create the control device
WDF_OBJECT_ATTRIBUTES deviceAttributes;
WDF_OBJECT_ATTRIBUTES_INIT(&deviceAttributes);
WDFDEVICE controlDevice;
NTSTATUS status;

GOTO_IF_NOT_NT_SUCCESS(done, status, WdfDeviceInitAssignName(deviceInit, &deviceName));
GOTO_IF_NOT_NT_SUCCESS(done, status, WdfDeviceCreate(&deviceInit, &deviceAttributes, &controlDevice));

POVPN_DRIVER driverCtx = OvpnGetDriverContext(WdfGetDriver());
driverCtx->ControlDevice = controlDevice;

// symlink for control device
GOTO_IF_NOT_NT_SUCCESS(done, status, WdfDeviceCreateSymbolicLink(controlDevice, &symLink));

// queue to handle IO
WDF_IO_QUEUE_CONFIG queueConfig;
WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&queueConfig, WdfIoQueueDispatchParallel);
queueConfig.EvtIoDeviceControl = OvpnEvtControlDeviceIOControl;
WDFQUEUE queue;
GOTO_IF_NOT_NT_SUCCESS(done, status, WdfIoQueueCreate(controlDevice, &queueConfig, WDF_NO_OBJECT_ATTRIBUTES, &queue));

// Complete the control device initialization
WdfControlFinishInitializing(controlDevice);

done:
if (deviceInit)
{
WdfDeviceInitFree(deviceInit);
}

LOG_EXIT();

return status;
}
31 changes: 31 additions & 0 deletions control.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.
*/

#pragma once

#include <ntddk.h>
#include <wdf.h>

NTSTATUS
OvpnGetVersion(WDFREQUEST request, _Out_ ULONG_PTR* bytesReturned);

NTSTATUS
OvpnCreateControlDevice(WDFDRIVER wdfDriver);
Loading
Loading