aboutsummaryrefslogtreecommitdiffstats
path: root/api/adapter.c
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2020-11-01 03:19:21 +0100
committerSimon Rozman <simon@rozman.si>2020-11-02 09:17:47 +0100
commitdfa53185d0624365fc2fc3c0fe0aa098589d7b5b (patch)
treead549ba384b61a0c6450361ed707082277862721 /api/adapter.c
parentapi: separate out driver installation (diff)
downloadwintun-dfa53185d0624365fc2fc3c0fe0aa098589d7b5b.tar.xz
wintun-dfa53185d0624365fc2fc3c0fe0aa098589d7b5b.zip
api: check for duplicate adapter status
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'api/adapter.c')
-rw-r--r--api/adapter.c58
1 files changed, 55 insertions, 3 deletions
diff --git a/api/adapter.c b/api/adapter.c
index 9ae6608..80582d0 100644
--- a/api/adapter.c
+++ b/api/adapter.c
@@ -1030,6 +1030,8 @@ CreateAdapter(
_Out_ WINTUN_ADAPTER **Adapter,
_Inout_ BOOL *RebootRequired)
{
+ LOG(WINTUN_LOG_INFO, L"Creating adapter");
+
DWORD Result;
HANDLE Mutex = NamespaceTakeMutex(Pool);
if (!Mutex)
@@ -1281,13 +1283,63 @@ CreateAdapter(
Result = RegSetKeyValueW(
TcpipInterfaceRegKey, NULL, L"EnableDeadGWDetect", REG_DWORD, &EnableDeadGWDetect, sizeof(EnableDeadGWDetect));
if (Result != ERROR_SUCCESS)
+ {
LOG_ERROR(L"Failed to set EnableDeadGWDetect", Result);
+ goto cleanupTcpipInterfaceRegKey;
+ }
Result = WintunSetAdapterName(a, Name);
- if (Result == ERROR_SUCCESS)
- *Adapter = a;
- else
+ if (Result != ERROR_SUCCESS)
+ {
LOG_ERROR(L"Failed to set adapter name", Result);
+ goto cleanupTcpipInterfaceRegKey;
+ }
+
+ ULONG Status, ProblemCode;
+ Result = CM_MapCrToWin32Err(
+ CM_Get_DevNode_Status(&Status, &ProblemCode, DevInfoData.DevInst, 0), ERROR_DEVICE_NOT_AVAILABLE);
+ if (Result != ERROR_SUCCESS)
+ {
+ LOG_ERROR(L"Failed to get status of adapter", Result);
+ goto cleanupTcpipInterfaceRegKey;
+ }
+ if (Status & DN_HAS_PROBLEM)
+ {
+ /* TODO: This GUIDs is in devpkey.h, but we can't link to it? Which dll? */
+ static const DEVPROPKEY DEVPKEY_Device_ProblemStatus = {
+ { 0x4340a6c5, 0x93fa, 0x4706, { 0x97, 0x2c, 0x7b, 0x64, 0x80, 0x08, 0xa5, 0xa7 } }, 12
+ };
+ DEVPROPTYPE PropertyType;
+ for (int Tries = 0; Tries < 1000; Sleep(10), ++Tries)
+ {
+ NTSTATUS ProblemStatus;
+ if (SetupDiGetDevicePropertyW(
+ DevInfo,
+ &DevInfoData,
+ &DEVPKEY_Device_ProblemStatus,
+ &PropertyType,
+ (PBYTE)&ProblemStatus,
+ sizeof(ProblemStatus),
+ NULL,
+ 0) &&
+ PropertyType == DEVPROP_TYPE_NTSTATUS)
+ {
+ Result = RtlNtStatusToDosError(ProblemStatus);
+ if (ProblemStatus != STATUS_PNP_DEVICE_CONFIGURATION_PENDING || Tries == 999)
+ {
+ LOG_ERROR(L"Failed to setup adapter", Result);
+ goto cleanupTcpipInterfaceRegKey;
+ }
+ }
+ else
+ break;
+ }
+ LOG(WINTUN_LOG_WARN, L"Adapter setup error code vanished after initially there");
+ }
+
+ *Adapter = a;
+
+cleanupTcpipInterfaceRegKey:
RegCloseKey(TcpipInterfaceRegKey);
cleanupTcpipAdapterRegKey:
RegCloseKey(TcpipAdapterRegKey);