2020-10-13 19:40:52 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pch.h"
|
|
|
|
|
2020-10-13 19:42:30 +02:00
|
|
|
#pragma warning(disable : 4221) /* nonstandard: address of automatic in initializer */
|
|
|
|
|
2020-10-14 09:09:27 +02:00
|
|
|
BOOL
|
2020-10-15 11:32:06 +02:00
|
|
|
DriverIsOurHardwareID(_In_z_ const WCHAR *Hwids)
|
2020-10-14 09:09:27 +02:00
|
|
|
{
|
2020-10-15 11:32:06 +02:00
|
|
|
for (; Hwids[0]; Hwids += wcslen(Hwids) + 1)
|
|
|
|
if (!_wcsicmp(Hwids, WINTUN_HWID))
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
2020-10-14 09:09:27 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 11:32:06 +02:00
|
|
|
BOOL
|
|
|
|
DriverIsOurDrvInfoDetail(_In_ const SP_DRVINFO_DETAIL_DATA_W *DrvInfoDetailData)
|
2020-10-14 09:09:27 +02:00
|
|
|
{
|
2020-10-15 11:32:06 +02:00
|
|
|
return DrvInfoDetailData->CompatIDsOffset > 1 && !_wcsicmp(DrvInfoDetailData->HardwareID, WINTUN_HWID) ||
|
|
|
|
DrvInfoDetailData->CompatIDsLength &&
|
|
|
|
DriverIsOurHardwareID(DrvInfoDetailData->HardwareID + DrvInfoDetailData->CompatIDsOffset);
|
2020-10-14 09:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HAVE_EV) || defined(HAVE_WHQL)
|
|
|
|
|
2020-10-17 15:12:20 +02:00
|
|
|
WINTUN_STATUS
|
|
|
|
DriverRemoveAllOurs(void)
|
2020-10-13 19:42:30 +02:00
|
|
|
{
|
|
|
|
HDEVINFO DevInfo = SetupDiGetClassDevsW(&GUID_DEVCLASS_NET, NULL, NULL, 0);
|
|
|
|
if (!DevInfo)
|
2020-10-14 13:04:29 +02:00
|
|
|
return LOG_LAST_ERROR(L"Failed to request device information");
|
2020-10-13 19:42:30 +02:00
|
|
|
DWORD Result = ERROR_SUCCESS;
|
|
|
|
if (!SetupDiBuildDriverInfoList(DevInfo, NULL, SPDIT_CLASSDRIVER))
|
|
|
|
{
|
2020-10-14 13:04:29 +02:00
|
|
|
Result = LOG_LAST_ERROR(L"Failed to build list of drivers");
|
2020-10-13 19:42:30 +02:00
|
|
|
goto cleanupDeviceInfoSet;
|
|
|
|
}
|
|
|
|
HANDLE Heap = GetProcessHeap();
|
|
|
|
for (DWORD EnumIndex = 0;; ++EnumIndex)
|
|
|
|
{
|
|
|
|
SP_DRVINFO_DATA_W DrvInfoData = { .cbSize = sizeof(DrvInfoData) };
|
|
|
|
if (!SetupDiEnumDriverInfoW(DevInfo, NULL, SPDIT_CLASSDRIVER, EnumIndex, &DrvInfoData))
|
|
|
|
{
|
|
|
|
if (GetLastError() == ERROR_NO_MORE_ITEMS)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-15 11:32:06 +02:00
|
|
|
SP_DRVINFO_DETAIL_DATA_W *DrvInfoDetailData;
|
|
|
|
if (AdapterGetDrvInfoDetail(DevInfo, NULL, &DrvInfoData, &DrvInfoDetailData) != ERROR_SUCCESS)
|
2020-10-15 12:21:55 +02:00
|
|
|
{
|
|
|
|
LOG(WINTUN_LOG_WARN, L"Failed getting driver info detail");
|
2020-10-13 19:42:30 +02:00
|
|
|
continue;
|
2020-10-15 12:21:55 +02:00
|
|
|
}
|
2020-10-15 11:32:06 +02:00
|
|
|
if (!DriverIsOurDrvInfoDetail(DrvInfoDetailData))
|
2020-10-13 19:42:30 +02:00
|
|
|
{
|
2020-10-15 11:32:06 +02:00
|
|
|
HeapFree(Heap, 0, DrvInfoDetailData);
|
2020-10-13 19:42:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-10-15 11:32:06 +02:00
|
|
|
PathStripPathW(DrvInfoDetailData->InfFileName);
|
|
|
|
LOG(WINTUN_LOG_INFO, L"Removing existing driver");
|
|
|
|
if (!SetupUninstallOEMInfW(DrvInfoDetailData->InfFileName, SUOI_FORCEDELETE, NULL))
|
2020-10-13 19:42:30 +02:00
|
|
|
{
|
2020-10-15 11:32:06 +02:00
|
|
|
LOG_LAST_ERROR(L"Unable to remove existing driver");
|
2020-10-13 19:42:30 +02:00
|
|
|
Result = Result != ERROR_SUCCESS ? Result : GetLastError();
|
|
|
|
}
|
2020-10-15 11:32:06 +02:00
|
|
|
HeapFree(Heap, 0, DrvInfoDetailData);
|
2020-10-13 19:42:30 +02:00
|
|
|
}
|
2020-10-15 11:32:06 +02:00
|
|
|
SetupDiDestroyDriverInfoList(DevInfo, NULL, SPDIT_CLASSDRIVER);
|
|
|
|
cleanupDeviceInfoSet:
|
|
|
|
SetupDiDestroyDeviceInfoList(DevInfo);
|
2020-10-13 19:42:30 +02:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2020-10-14 09:09:27 +02:00
|
|
|
#endif
|