aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2021-10-06 23:03:00 +0000
committerJason A. Donenfeld <Jason@zx2c4.com>2021-10-11 16:12:09 +0000
commit860a0c4480c18736b9ebd18ce3a68cbcc4bf0ea6 (patch)
tree242d7c8dc6ff3119be76fc17ec17cc15a70e250e
parentdriver: add icon to device manager (diff)
downloadwireguard-nt-860a0c4480c18736b9ebd18ce3a68cbcc4bf0ea6.tar.xz
wireguard-nt-860a0c4480c18736b9ebd18ce3a68cbcc4bf0ea6.zip
api: adapter: cleanup ROOT\NET enumerated devices on DLL load
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--api/adapter.h5
-rw-r--r--api/adapter_win7.h33
-rw-r--r--api/main.c1
3 files changed, 39 insertions, 0 deletions
diff --git a/api/adapter.h b/api/adapter.h
index 75358c2..195f464 100644
--- a/api/adapter.h
+++ b/api/adapter.h
@@ -89,6 +89,11 @@ AdapterGetDeviceObjectFileName(_In_z_ LPCWSTR InstanceId);
VOID AdapterCleanupOrphanedDevices(VOID);
/**
+ * Cleans up adapters that use the old enumerator.
+ */
+VOID AdapterCleanupLegacyDevices(VOID);
+
+/**
* Removes the specified device instance.
*
* @param DevInfo Device info handle from SetupAPI.
diff --git a/api/adapter_win7.h b/api/adapter_win7.h
index abb89e6..51d6902 100644
--- a/api/adapter_win7.h
+++ b/api/adapter_win7.h
@@ -307,4 +307,37 @@ VOID AdapterCleanupOrphanedDevicesWin7(VOID)
LOG(WIREGUARD_LOG_INFO, L"Removed orphaned adapter \"%s\"", Name);
}
SetupDiDestroyDeviceInfoList(DevInfo);
+}
+
+VOID AdapterCleanupLegacyDevices(VOID)
+{
+ HDEVINFO DevInfo = SetupDiGetClassDevsExW(&GUID_DEVCLASS_NET, L"ROOT\\NET", NULL, 0, NULL, NULL, NULL);
+ if (DevInfo == INVALID_HANDLE_VALUE)
+ return;
+ SP_DEVINFO_DATA DevInfoData = { .cbSize = sizeof(DevInfoData) };
+ for (DWORD EnumIndex = 0;; ++EnumIndex)
+ {
+ if (!SetupDiEnumDeviceInfo(DevInfo, EnumIndex, &DevInfoData))
+ {
+ if (GetLastError() == ERROR_NO_MORE_ITEMS)
+ break;
+ continue;
+ }
+ WCHAR HardwareIDs[0x400] = { 0 };
+ DWORD ValueType, Size = sizeof(HardwareIDs) - sizeof(HardwareIDs[0]);
+ if (!SetupDiGetDeviceRegistryPropertyW(
+ DevInfo, &DevInfoData, SPDRP_HARDWAREID, &ValueType, (PBYTE)HardwareIDs, Size, &Size) ||
+ Size > sizeof(HardwareIDs) - sizeof(HardwareIDs[0]))
+ continue;
+ Size /= sizeof(HardwareIDs[0]);
+ for (WCHAR *P = HardwareIDs; P < HardwareIDs + Size; P += wcslen(P) + 1)
+ {
+ if (!_wcsicmp(P, WIREGUARD_HWID))
+ {
+ AdapterRemoveInstance(DevInfo, &DevInfoData);
+ break;
+ }
+ }
+ }
+ SetupDiDestroyDeviceInfoList(DevInfo);
} \ No newline at end of file
diff --git a/api/main.c b/api/main.c
index 405893a..28937fa 100644
--- a/api/main.c
+++ b/api/main.c
@@ -122,6 +122,7 @@ DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved)
}
EnvInit();
NamespaceInit();
+ AdapterCleanupLegacyDevices();
break;
case DLL_PROCESS_DETACH: