2020-07-03 12:26:06 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
2020-10-31 11:55:26 +01:00
|
|
|
#include "adapter.h"
|
|
|
|
#include "entry.h"
|
|
|
|
#include "logger.h"
|
|
|
|
#include "registry.h"
|
|
|
|
#include "namespace.h"
|
|
|
|
#include "wintun.h"
|
|
|
|
|
|
|
|
#include <Windows.h>
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4201)
|
|
|
|
/* nonstandard extension used: nameless struct/union */
|
|
|
|
#include <delayimp.h>
|
|
|
|
#pragma warning(pop)
|
|
|
|
#include <sddl.h>
|
2020-07-03 12:26:06 +02:00
|
|
|
|
|
|
|
HINSTANCE ResourceModule;
|
2020-10-24 22:12:47 +02:00
|
|
|
HANDLE ModuleHeap;
|
2020-10-30 13:26:36 +01:00
|
|
|
SECURITY_ATTRIBUTES SecurityAttributes = { .nLength = sizeof(SECURITY_ATTRIBUTES) };
|
2020-07-03 12:26:06 +02:00
|
|
|
|
2020-07-21 18:19:15 +02:00
|
|
|
WINTUN_STATUS WINAPI
|
|
|
|
WintunGetVersion(
|
|
|
|
_Out_ DWORD *DriverVersionMaj,
|
|
|
|
_Out_ DWORD *DriverVersionMin,
|
|
|
|
_Out_ DWORD *NdisVersionMaj,
|
|
|
|
_Out_ DWORD *NdisVersionMin)
|
|
|
|
{
|
|
|
|
HKEY Key;
|
|
|
|
DWORD Result =
|
|
|
|
RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Services\\Wintun", 0, KEY_QUERY_VALUE, &Key);
|
|
|
|
if (Result != ERROR_SUCCESS)
|
2020-10-14 13:04:29 +02:00
|
|
|
return LOG_ERROR(L"Failed to open registry key", Result);
|
2020-10-29 12:04:34 +01:00
|
|
|
if (RegistryQueryDWORD(Key, L"DriverMajorVersion", DriverVersionMaj, FALSE) != ERROR_SUCCESS ||
|
|
|
|
RegistryQueryDWORD(Key, L"DriverMinorVersion", DriverVersionMin, FALSE) != ERROR_SUCCESS)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2020-10-30 07:50:14 +01:00
|
|
|
/* TODO: Drop the fallback to WINTUN_VERSION_MAJ & WINTUN_VERSION_MIN when Windows 7 support is discontinued. */
|
2020-10-29 12:04:34 +01:00
|
|
|
*DriverVersionMaj = WINTUN_VERSION_MAJ;
|
|
|
|
*DriverVersionMin = WINTUN_VERSION_MIN;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
2020-10-29 12:04:34 +01:00
|
|
|
Result = RegistryQueryDWORD(Key, L"NdisMajorVersion", NdisVersionMaj, TRUE);
|
2020-07-21 18:19:15 +02:00
|
|
|
if (Result != ERROR_SUCCESS)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2020-10-15 12:21:55 +02:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Failed to query NdisMajorVersion value");
|
2020-07-21 18:19:15 +02:00
|
|
|
goto cleanupKey;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
2020-10-29 12:04:34 +01:00
|
|
|
Result = RegistryQueryDWORD(Key, L"NdisMinorVersion", NdisVersionMin, TRUE);
|
2020-10-13 19:40:52 +02:00
|
|
|
if (Result != ERROR_SUCCESS)
|
2020-10-15 12:21:55 +02:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Failed to query NdisMinorVersion value");
|
2020-07-21 18:19:15 +02:00
|
|
|
cleanupKey:
|
|
|
|
RegCloseKey(Key);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2020-10-30 11:43:47 +01:00
|
|
|
static FARPROC WINAPI DelayedLoadLibraryHook(unsigned dliNotify, PDelayLoadInfo pdli)
|
|
|
|
{
|
|
|
|
if (dliNotify != dliNotePreLoadLibrary)
|
|
|
|
return NULL;
|
|
|
|
HMODULE Library = LoadLibraryExA(pdli->szDll, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
|
|
|
|
if (!Library)
|
|
|
|
abort();
|
|
|
|
return (FARPROC)Library;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PfnDliHook __pfnDliNotifyHook2 = DelayedLoadLibraryHook;
|
|
|
|
|
2020-07-03 12:26:06 +02:00
|
|
|
BOOL APIENTRY
|
|
|
|
DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved)
|
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(lpvReserved);
|
|
|
|
|
|
|
|
switch (fdwReason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
ResourceModule = hinstDLL;
|
2020-10-24 22:12:47 +02:00
|
|
|
ModuleHeap = HeapCreate(0, 0, 0);
|
2020-10-31 08:15:52 +01:00
|
|
|
if (!ModuleHeap)
|
|
|
|
return FALSE;
|
2020-10-15 15:34:31 +02:00
|
|
|
ConvertStringSecurityDescriptorToSecurityDescriptorW(
|
2020-10-30 13:26:36 +01:00
|
|
|
L"O:SYD:P(A;;GA;;;SY)", SDDL_REVISION_1, &SecurityAttributes.lpSecurityDescriptor, NULL);
|
2020-10-15 11:32:06 +02:00
|
|
|
AdapterInit();
|
2020-07-03 16:49:47 +02:00
|
|
|
NamespaceInit();
|
|
|
|
break;
|
|
|
|
|
2020-07-03 12:26:06 +02:00
|
|
|
case DLL_PROCESS_DETACH:
|
2020-07-03 16:49:47 +02:00
|
|
|
NamespaceCleanup();
|
2020-10-30 13:26:36 +01:00
|
|
|
LocalFree(SecurityAttributes.lpSecurityDescriptor);
|
2020-10-24 22:12:47 +02:00
|
|
|
HeapDestroy(ModuleHeap);
|
2020-07-03 12:26:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|