api: check for duplicate adapter status

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-11-01 03:19:21 +01:00 committed by Simon Rozman
parent 9adb49e13d
commit dfa53185d0
4 changed files with 58 additions and 4 deletions

View File

@ -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);

View File

@ -19,7 +19,7 @@ WintunSetLogger(_In_ WINTUN_LOGGER_CALLBACK_FUNC NewLogger);
_Post_equals_last_error_ DWORD
LoggerError(_In_z_ const WCHAR *Prefix, _In_ DWORD Error);
inline _Post_equals_last_error_ DWORD
static inline _Post_equals_last_error_ DWORD
LoggerLastError(_In_z_ const WCHAR *Prefix)
{
DWORD Error = GetLastError();

View File

@ -34,3 +34,4 @@ typedef struct _RTL_PROCESS_MODULES
#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) //TODO: #include <ntstatus.h> instead of this
#define STATUS_PNP_DEVICE_CONFIGURATION_PENDING ((NTSTATUS)0xC0000495L)

View File

@ -168,6 +168,7 @@ VOID __stdcall DoThingsForDebugging(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLin
WintunEndSession(Session);
assert(WintunDeleteAdapter(Adapter, TRUE, &RebootRequired) == ERROR_SUCCESS);
assert(!RebootRequired);
system("pause");
}
#endif