aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rozman <simon@rozman.si>2020-10-14 09:09:27 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2020-10-30 16:51:00 +0100
commit85c32a5566f1025ff250dfe85c87d57450e720f8 (patch)
tree17e72a0e0e154eeb1efd2d946c7aef262a0226e1
parentapi: unify and document resource loading (diff)
downloadwintun-85c32a5566f1025ff250dfe85c87d57450e720f8.tar.xz
wintun-85c32a5566f1025ff250dfe85c87d57450e720f8.zip
api: disable driver management for wintun.dll that don't have a driver
Namely the 32-bit ARM platform, which we will ship wintun.dll for ARM on ARM64 clients exclusively. Signed-off-by: Simon Rozman <simon@rozman.si>
-rw-r--r--api/driver.c194
-rw-r--r--api/driver.h4
2 files changed, 102 insertions, 96 deletions
diff --git a/api/driver.c b/api/driver.c
index a65c0a9..d46f641 100644
--- a/api/driver.c
+++ b/api/driver.c
@@ -63,6 +63,102 @@ out:
}
/**
+ * Checks if the device (i.e. network adapter) is using Wintun driver.
+ *
+ * @param DevInfo A handle to the device information set that contains a device information element that
+ * represents the device.
+ *
+ * @param DevInfoData A pointer to a structure that specifies the device information element in DevInfo.
+ *
+ * @return non-zero when using Wintun driver; zero when not or error - use GetLastError().
+ */
+BOOL
+DriverIsWintunAdapter(_In_ HDEVINFO DevInfo, _In_opt_ SP_DEVINFO_DATA *DevInfoData)
+{
+ BOOL Found = FALSE;
+ if (!SetupDiBuildDriverInfoList(DevInfo, DevInfoData, SPDIT_COMPATDRIVER))
+ {
+ WINTUN_LOGGER_LAST_ERROR(L"Failed to build list of drivers");
+ return FALSE;
+ }
+ HANDLE Heap = GetProcessHeap();
+ for (DWORD EnumIndex = 0; !Found; ++EnumIndex)
+ {
+ SP_DRVINFO_DATA_W DrvInfoData = { .cbSize = sizeof(SP_DRVINFO_DATA_W) };
+ if (!SetupDiEnumDriverInfoW(DevInfo, DevInfoData, SPDIT_COMPATDRIVER, EnumIndex, &DrvInfoData))
+ {
+ if (GetLastError() == ERROR_NO_MORE_ITEMS)
+ break;
+ continue;
+ }
+ SP_DRVINFO_DETAIL_DATA_W *DrvInfoDetailData = DriverGetDrvInfoDetail(DevInfo, DevInfoData, &DrvInfoData);
+ if (!DrvInfoDetailData)
+ continue;
+ Found = !_wcsicmp(DrvInfoDetailData->HardwareID, WINTUN_HWID);
+ HeapFree(Heap, 0, DrvInfoDetailData);
+ }
+ SetupDiDestroyDriverInfoList(DevInfo, DevInfoData, SPDIT_COMPATDRIVER);
+ SetLastError(ERROR_SUCCESS);
+ return Found;
+}
+
+/**
+ * Returns a handle to the adapter device object.
+ *
+ * @param InstanceId Adapter device instance ID.
+ *
+ * @return device handle on success; INVALID_HANDLE_VALUE otherwise - use GetLastError().
+ */
+_Return_type_success_(return != INVALID_HANDLE_VALUE) HANDLE
+ DriverGetAdapterDeviceObject(_In_opt_z_ const WCHAR *InstanceId)
+{
+ HANDLE Heap = GetProcessHeap();
+ ULONG InterfacesLen;
+ HANDLE Handle = INVALID_HANDLE_VALUE;
+ DWORD Result = CM_Get_Device_Interface_List_SizeW(
+ &InterfacesLen, (GUID *)&GUID_DEVINTERFACE_NET, (DEVINSTID_W)InstanceId, CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
+ if (Result != CR_SUCCESS)
+ {
+ WINTUN_LOGGER(WINTUN_LOG_ERR, L"Failed to get device associated device instances size");
+ SetLastError(ERROR_GEN_FAILURE);
+ return INVALID_HANDLE_VALUE;
+ }
+ WCHAR *Interfaces = HeapAlloc(Heap, 0, InterfacesLen * sizeof(WCHAR));
+ if (!Interfaces)
+ {
+ SetLastError(ERROR_OUTOFMEMORY);
+ return INVALID_HANDLE_VALUE;
+ }
+ Result = CM_Get_Device_Interface_ListW(
+ (GUID *)&GUID_DEVINTERFACE_NET,
+ (DEVINSTID_W)InstanceId,
+ Interfaces,
+ InterfacesLen,
+ CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
+ if (Result != CR_SUCCESS)
+ {
+ WINTUN_LOGGER(WINTUN_LOG_ERR, L"Failed to get device associated device instances");
+ Result = ERROR_GEN_FAILURE;
+ goto cleanupBuf;
+ }
+ Handle = CreateFileW(
+ Interfaces,
+ GENERIC_READ | GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+ NULL,
+ OPEN_EXISTING,
+ 0,
+ NULL);
+ Result = Handle != INVALID_HANDLE_VALUE ? ERROR_SUCCESS : WINTUN_LOGGER_LAST_ERROR(L"Failed to connect to device");
+cleanupBuf:
+ HeapFree(Heap, 0, Interfaces);
+ SetLastError(Result);
+ return Handle;
+}
+
+#if defined(HAVE_EV) || defined(HAVE_WHQL)
+
+/**
* Checks if the Wintun driver is loaded.
*
* Note: This function does not log any errors, not to flood the log when called from the EnsureDriverUnloaded() loop.
@@ -267,8 +363,6 @@ InstallDriver(_In_ BOOL UpdateExisting)
UseWHQL = FALSE;
#elif defined(HAVE_WHQL)
UseWHQL = TRUE;
-#else
-# error No driver available
#endif
if (!UseWHQL && (Result = InstallCertificate(L"wintun.sys")) != ERROR_SUCCESS)
WINTUN_LOGGER_ERROR(L"Unable to install code signing certificate", Result);
@@ -353,100 +447,6 @@ cleanupDeviceInfoSet:
return Result;
}
-/**
- * Checks if the device (i.e. network adapter) is using Wintun driver.
- *
- * @param DevInfo A handle to the device information set that contains a device information element that
- * represents the device.
- *
- * @param DevInfoData A pointer to a structure that specifies the device information element in DevInfo.
- *
- * @return non-zero when using Wintun driver; zero when not or error - use GetLastError().
- */
-BOOL
-DriverIsWintunAdapter(_In_ HDEVINFO DevInfo, _In_opt_ SP_DEVINFO_DATA *DevInfoData)
-{
- BOOL Found = FALSE;
- if (!SetupDiBuildDriverInfoList(DevInfo, DevInfoData, SPDIT_COMPATDRIVER))
- {
- WINTUN_LOGGER_LAST_ERROR(L"Failed to build list of drivers");
- return FALSE;
- }
- HANDLE Heap = GetProcessHeap();
- for (DWORD EnumIndex = 0; !Found; ++EnumIndex)
- {
- SP_DRVINFO_DATA_W DrvInfoData = { .cbSize = sizeof(SP_DRVINFO_DATA_W) };
- if (!SetupDiEnumDriverInfoW(DevInfo, DevInfoData, SPDIT_COMPATDRIVER, EnumIndex, &DrvInfoData))
- {
- if (GetLastError() == ERROR_NO_MORE_ITEMS)
- break;
- continue;
- }
- SP_DRVINFO_DETAIL_DATA_W *DrvInfoDetailData = DriverGetDrvInfoDetail(DevInfo, DevInfoData, &DrvInfoData);
- if (!DrvInfoDetailData)
- continue;
- Found = !_wcsicmp(DrvInfoDetailData->HardwareID, WINTUN_HWID);
- HeapFree(Heap, 0, DrvInfoDetailData);
- }
- SetupDiDestroyDriverInfoList(DevInfo, DevInfoData, SPDIT_COMPATDRIVER);
- SetLastError(ERROR_SUCCESS);
- return Found;
-}
-
-/**
- * Returns a handle to the adapter device object.
- *
- * @param InstanceId Adapter device instance ID.
- *
- * @return device handle on success; INVALID_HANDLE_VALUE otherwise - use GetLastError().
- */
-_Return_type_success_(return != INVALID_HANDLE_VALUE) HANDLE
- DriverGetAdapterDeviceObject(_In_opt_z_ const WCHAR *InstanceId)
-{
- HANDLE Heap = GetProcessHeap();
- ULONG InterfacesLen;
- HANDLE Handle = INVALID_HANDLE_VALUE;
- DWORD Result = CM_Get_Device_Interface_List_SizeW(
- &InterfacesLen, (GUID *)&GUID_DEVINTERFACE_NET, (DEVINSTID_W)InstanceId, CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
- if (Result != CR_SUCCESS)
- {
- WINTUN_LOGGER(WINTUN_LOG_ERR, L"Failed to get device associated device instances size");
- SetLastError(ERROR_GEN_FAILURE);
- return INVALID_HANDLE_VALUE;
- }
- WCHAR *Interfaces = HeapAlloc(Heap, 0, InterfacesLen * sizeof(WCHAR));
- if (!Interfaces)
- {
- SetLastError(ERROR_OUTOFMEMORY);
- return INVALID_HANDLE_VALUE;
- }
- Result = CM_Get_Device_Interface_ListW(
- (GUID *)&GUID_DEVINTERFACE_NET,
- (DEVINSTID_W)InstanceId,
- Interfaces,
- InterfacesLen,
- CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
- if (Result != CR_SUCCESS)
- {
- WINTUN_LOGGER(WINTUN_LOG_ERR, L"Failed to get device associated device instances");
- Result = ERROR_GEN_FAILURE;
- goto cleanupBuf;
- }
- Handle = CreateFileW(
- Interfaces,
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
- NULL,
- OPEN_EXISTING,
- 0,
- NULL);
- Result = Handle != INVALID_HANDLE_VALUE ? ERROR_SUCCESS : WINTUN_LOGGER_LAST_ERROR(L"Failed to connect to device");
-cleanupBuf:
- HeapFree(Heap, 0, Interfaces);
- SetLastError(Result);
- return Handle;
-}
-
#define TUN_IOCTL_FORCE_CLOSE_HANDLES CTL_CODE(51820U, 0x971U, METHOD_NEITHER, FILE_READ_DATA | FILE_WRITE_DATA)
/**
@@ -695,3 +695,5 @@ WINTUN_STATUS DriverUninstall(VOID)
WINTUN_LOGGER(WINTUN_LOG_INFO, L"Uninstallation successful");
return Result;
}
+
+#endif
diff --git a/api/driver.h b/api/driver.h
index e8514df..c17787c 100644
--- a/api/driver.h
+++ b/api/driver.h
@@ -22,6 +22,10 @@ DriverIsWintunAdapter(_In_ HDEVINFO DevInfo, _In_opt_ SP_DEVINFO_DATA *DevInfoDa
_Return_type_success_(return != INVALID_HANDLE_VALUE) HANDLE
DriverGetAdapterDeviceObject(_In_opt_z_ const WCHAR *InstanceId);
+#if defined(HAVE_EV) || defined(HAVE_WHQL)
+
WINTUN_STATUS DriverInstallOrUpdate(VOID);
WINTUN_STATUS DriverUninstall(VOID);
+
+#endif \ No newline at end of file