From 46907991cd3da0d50df8b7d5079131929fdca9e8 Mon Sep 17 00:00:00 2001 From: Lev Stipakov Date: Thu, 5 Sep 2024 15:06:14 +0300 Subject: [PATCH] Add OVPN_IOCTL_SET_MODE THis is to select between client mode (OVPN_MODE_P2P, default) and server mode (OVPN_MODE_MP). At the moment only ioctl is added, no functional changes. Co-authored-by: Leon Dang Signed-off-by: Leon Dang Signed-off-by: Lev Stipakov --- Driver.cpp | 38 ++++++++++++++++++++++++++++++++++++++ Driver.h | 1 + uapi/ovpn-dco.h | 10 ++++++++++ 3 files changed, 49 insertions(+) diff --git a/Driver.cpp b/Driver.cpp index 4b4f0bc..97c0e65 100644 --- a/Driver.cpp +++ b/Driver.cpp @@ -229,6 +229,38 @@ OvpnEvtIoWrite(WDFQUEUE queue, WDFREQUEST request, size_t length) ExReleaseSpinLockShared(&device->SpinLock, kiqrl); } +NTSTATUS +OvpnSetMode(POVPN_DEVICE device, WDFREQUEST request) +{ + POVPN_SET_MODE mode; + NTSTATUS status = WdfRequestRetrieveInputBuffer(request, sizeof(OVPN_SET_MODE), (PVOID*)&mode, NULL); + if (!NT_SUCCESS(status)) { + return status; + } + + if (device->Mode != OVPN_MODE_P2P) { + LOG_ERROR("mode already set"); + return STATUS_ALREADY_INITIALIZED; + } + + status = STATUS_SUCCESS; + + LOG_INFO("Set mode", TraceLoggingValue(static_cast(mode->Mode), "mode")); + + switch (mode->Mode) { + case OVPN_MODE_P2P: + case OVPN_MODE_MP: + device->Mode = mode->Mode; + break; + + default: + status = STATUS_INVALID_PARAMETER; + break; + } + + return status; +} + EVT_WDF_IO_QUEUE_IO_DEVICE_CONTROL OvpnEvtIoDeviceControl; _Use_decl_annotations_ @@ -291,6 +323,12 @@ OvpnEvtIoDeviceControl(WDFQUEUE queue, WDFREQUEST request, size_t outputBufferLe status = OvpnGetVersion(request, &bytesReturned); break; + case OVPN_IOCTL_SET_MODE: + kirql = ExAcquireSpinLockExclusive(&device->SpinLock); + status = OvpnSetMode(device, request); + ExReleaseSpinLockExclusive(&device->SpinLock, kirql); + break; + default: LOG_WARN("Unknown ", TraceLoggingValue(ioControlCode, "ioControlCode")); status = STATUS_INVALID_DEVICE_REQUEST; diff --git a/Driver.h b/Driver.h index bf221ce..0045871 100644 --- a/Driver.h +++ b/Driver.h @@ -94,6 +94,7 @@ struct OVPN_DEVICE { _Guarded_by_(SpinLock) RTL_GENERIC_TABLE Peers; + OVPN_MODE Mode; }; typedef OVPN_DEVICE * POVPN_DEVICE; diff --git a/uapi/ovpn-dco.h b/uapi/ovpn-dco.h index 9e437f0..8163b8b 100644 --- a/uapi/ovpn-dco.h +++ b/uapi/ovpn-dco.h @@ -114,6 +114,15 @@ typedef struct _OVPN_VERSION { LONG Patch; } OVPN_VERSION, * POVPN_VERSION; +typedef enum { + OVPN_MODE_P2P, + OVPN_MODE_MP +} OVPN_MODE; + +typedef struct _OVPN_SET_MODE { + OVPN_MODE Mode; +} OVPN_SET_MODE, * POVPN_SET_MODE; + #define OVPN_IOCTL_NEW_PEER CTL_CODE(FILE_DEVICE_UNKNOWN, 1, METHOD_BUFFERED, FILE_ANY_ACCESS) #define OVPN_IOCTL_GET_STATS CTL_CODE(FILE_DEVICE_UNKNOWN, 2, METHOD_BUFFERED, FILE_ANY_ACCESS) #define OVPN_IOCTL_NEW_KEY CTL_CODE(FILE_DEVICE_UNKNOWN, 3, METHOD_BUFFERED, FILE_ANY_ACCESS) @@ -123,3 +132,4 @@ typedef struct _OVPN_VERSION { #define OVPN_IOCTL_DEL_PEER CTL_CODE(FILE_DEVICE_UNKNOWN, 7, METHOD_BUFFERED, FILE_ANY_ACCESS) #define OVPN_IOCTL_GET_VERSION CTL_CODE(FILE_DEVICE_UNKNOWN, 8, METHOD_BUFFERED, FILE_ANY_ACCESS) #define OVPN_IOCTL_NEW_KEY_V2 CTL_CODE(FILE_DEVICE_UNKNOWN, 9, METHOD_BUFFERED, FILE_ANY_ACCESS) +#define OVPN_IOCTL_SET_MODE CTL_CODE(FILE_DEVICE_UNKNOWN, 10, METHOD_BUFFERED, FILE_ANY_ACCESS)