2020-07-24 09:39:02 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
2021-01-30 16:45:26 +01:00
|
|
|
* Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
|
2020-07-24 09:39:02 +02:00
|
|
|
*/
|
|
|
|
|
2021-07-28 13:50:40 +02:00
|
|
|
#include "adapter.h"
|
|
|
|
#include "logger.h"
|
|
|
|
#include "wintun.h"
|
2020-11-03 02:09:00 +01:00
|
|
|
|
2021-07-28 13:50:40 +02:00
|
|
|
#include <Windows.h>
|
|
|
|
#include <shellapi.h>
|
|
|
|
#include <cfgmgr32.h>
|
|
|
|
#include <objbase.h>
|
|
|
|
#include <assert.h>
|
2020-10-31 11:55:26 +01:00
|
|
|
|
2021-07-28 13:50:40 +02:00
|
|
|
#ifdef ACCEPT_WOW64
|
2020-07-24 09:39:02 +02:00
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
# define EXPORT comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__)
|
2020-10-19 22:23:09 +02:00
|
|
|
|
|
|
|
static DWORD
|
|
|
|
WriteFormatted(_In_ DWORD StdHandle, _In_z_ const WCHAR *Template, ...)
|
|
|
|
{
|
|
|
|
WCHAR *FormattedMessage = NULL;
|
|
|
|
DWORD SizeWritten;
|
|
|
|
va_list Arguments;
|
|
|
|
va_start(Arguments, Template);
|
2020-10-31 16:02:34 +01:00
|
|
|
DWORD Len = FormatMessageW(
|
|
|
|
FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
|
|
|
Template,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
(void *)&FormattedMessage,
|
|
|
|
0,
|
|
|
|
&Arguments);
|
2020-11-02 22:09:52 +01:00
|
|
|
WriteFile(GetStdHandle(StdHandle), FormattedMessage, Len * sizeof(WCHAR), &SizeWritten, NULL);
|
2020-10-19 22:23:09 +02:00
|
|
|
LocalFree(FormattedMessage);
|
|
|
|
va_end(Arguments);
|
|
|
|
return SizeWritten / sizeof(WCHAR);
|
|
|
|
}
|
2020-07-24 09:39:02 +02:00
|
|
|
|
2020-11-10 22:06:25 +01:00
|
|
|
static void CALLBACK
|
2020-10-31 08:35:39 +01:00
|
|
|
ConsoleLogger(_In_ WINTUN_LOGGER_LEVEL Level, _In_z_ const WCHAR *LogLine)
|
2020-10-15 15:23:23 +02:00
|
|
|
{
|
|
|
|
const WCHAR *Template;
|
|
|
|
switch (Level)
|
|
|
|
{
|
|
|
|
case WINTUN_LOG_INFO:
|
2020-10-19 22:23:09 +02:00
|
|
|
Template = L"[+] %1\n";
|
2020-10-15 15:23:23 +02:00
|
|
|
break;
|
|
|
|
case WINTUN_LOG_WARN:
|
2020-10-19 22:23:09 +02:00
|
|
|
Template = L"[-] %1\n";
|
2020-10-15 15:23:23 +02:00
|
|
|
break;
|
|
|
|
case WINTUN_LOG_ERR:
|
2020-10-19 22:23:09 +02:00
|
|
|
Template = L"[!] %1\n";
|
2020-10-15 15:23:23 +02:00
|
|
|
break;
|
|
|
|
default:
|
2020-11-10 22:06:25 +01:00
|
|
|
return;
|
2020-10-15 15:23:23 +02:00
|
|
|
}
|
2020-10-19 22:23:09 +02:00
|
|
|
WriteFormatted(STD_ERROR_HANDLE, Template, LogLine);
|
2020-10-15 15:23:23 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 13:26:04 +02:00
|
|
|
static int Argc;
|
|
|
|
static WCHAR **Argv;
|
|
|
|
|
2020-10-19 22:23:09 +02:00
|
|
|
static void
|
|
|
|
Init(void)
|
2020-10-15 15:23:23 +02:00
|
|
|
{
|
|
|
|
WintunSetLogger(ConsoleLogger);
|
2020-10-16 13:26:04 +02:00
|
|
|
Argv = CommandLineToArgvW(GetCommandLineW(), &Argc);
|
2020-10-15 15:23:23 +02:00
|
|
|
}
|
|
|
|
|
2020-10-19 22:23:09 +02:00
|
|
|
static void
|
|
|
|
Done(void)
|
2020-10-15 15:23:23 +02:00
|
|
|
{
|
2020-10-16 13:26:04 +02:00
|
|
|
LocalFree(Argv);
|
2020-10-15 15:23:23 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
# pragma warning(disable : 4100) /* unreferenced formal parameter */
|
2020-11-03 10:58:58 +01:00
|
|
|
|
2020-10-19 22:23:09 +02:00
|
|
|
VOID __stdcall CreateAdapter(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
|
2020-07-24 09:39:02 +02:00
|
|
|
{
|
2020-10-19 22:23:09 +02:00
|
|
|
# pragma EXPORT
|
2020-07-24 09:39:02 +02:00
|
|
|
|
2020-10-16 13:26:04 +02:00
|
|
|
Init();
|
2020-07-24 09:39:02 +02:00
|
|
|
if (Argc < 4)
|
2020-10-16 13:26:04 +02:00
|
|
|
goto cleanup;
|
2020-10-31 08:53:32 +01:00
|
|
|
if (wcslen(Argv[2]) >= WINTUN_MAX_POOL)
|
2020-10-16 13:26:04 +02:00
|
|
|
goto cleanup;
|
2020-07-24 09:39:02 +02:00
|
|
|
if (wcslen(Argv[3]) >= MAX_ADAPTER_NAME)
|
2020-10-16 13:26:04 +02:00
|
|
|
goto cleanup;
|
2020-07-24 09:39:02 +02:00
|
|
|
GUID RequestedGUID;
|
|
|
|
if (Argc > 4 && FAILED(CLSIDFromString(Argv[4], &RequestedGUID)))
|
2020-10-16 13:26:04 +02:00
|
|
|
goto cleanup;
|
|
|
|
|
2020-10-31 15:33:54 +01:00
|
|
|
BOOL RebootRequired;
|
2020-11-03 12:29:34 +01:00
|
|
|
WINTUN_ADAPTER *Adapter = WintunCreateAdapter(Argv[2], Argv[3], Argc > 4 ? &RequestedGUID : NULL, &RebootRequired);
|
|
|
|
DWORD LastError = Adapter ? ERROR_SUCCESS : GetLastError();
|
2020-10-19 22:23:09 +02:00
|
|
|
WCHAR GuidStr[MAX_GUID_STRING_LEN];
|
|
|
|
WriteFormatted(
|
|
|
|
STD_OUTPUT_HANDLE,
|
|
|
|
L"%1!X! %2!.*s! %3!X!",
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError,
|
|
|
|
StringFromGUID2(Adapter ? &Adapter->CfgInstanceID : &GUID_NULL, GuidStr, _countof(GuidStr)),
|
2020-10-19 22:23:09 +02:00
|
|
|
GuidStr,
|
|
|
|
RebootRequired);
|
2020-11-03 12:29:34 +01:00
|
|
|
if (Adapter)
|
|
|
|
WintunFreeAdapter(Adapter);
|
2020-10-16 13:26:04 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
Done();
|
2020-07-24 09:39:02 +02:00
|
|
|
}
|
|
|
|
|
2020-10-19 22:23:09 +02:00
|
|
|
VOID __stdcall DeleteAdapter(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
|
2020-07-24 09:39:02 +02:00
|
|
|
{
|
2020-10-19 22:23:09 +02:00
|
|
|
# pragma EXPORT
|
2020-07-24 09:39:02 +02:00
|
|
|
|
2020-10-16 13:26:04 +02:00
|
|
|
Init();
|
2020-07-24 09:39:02 +02:00
|
|
|
if (Argc < 3)
|
2020-10-16 13:26:04 +02:00
|
|
|
goto cleanup;
|
2020-07-24 09:39:02 +02:00
|
|
|
|
|
|
|
WINTUN_ADAPTER Adapter = { 0 };
|
2020-10-30 14:34:40 +01:00
|
|
|
BOOL ForceCloseSessions = wcstoul(Argv[2], NULL, 10);
|
|
|
|
if (FAILED(CLSIDFromString(Argv[3], &Adapter.CfgInstanceID)))
|
2020-10-16 13:26:04 +02:00
|
|
|
goto cleanup;
|
2020-10-31 15:33:54 +01:00
|
|
|
BOOL RebootRequired;
|
2020-11-03 12:29:34 +01:00
|
|
|
DWORD LastError =
|
|
|
|
WintunDeleteAdapter(&Adapter, ForceCloseSessions, &RebootRequired) ? ERROR_SUCCESS : GetLastError();
|
|
|
|
WriteFormatted(STD_OUTPUT_HANDLE, L"%1!X! %2!X!", LastError, RebootRequired);
|
2020-07-24 09:39:02 +02:00
|
|
|
|
2020-10-16 13:26:04 +02:00
|
|
|
cleanup:
|
|
|
|
Done();
|
2020-07-24 09:39:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 12:27:42 +01:00
|
|
|
VOID __stdcall DeletePoolDriver(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
|
2020-11-02 16:28:51 +01:00
|
|
|
{
|
|
|
|
# pragma EXPORT
|
|
|
|
|
|
|
|
Init();
|
2020-11-03 12:27:42 +01:00
|
|
|
if (Argc < 2)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
BOOL RebootRequired;
|
2020-11-03 12:29:34 +01:00
|
|
|
DWORD LastError = WintunDeletePoolDriver(Argv[2], &RebootRequired) ? ERROR_SUCCESS : GetLastError();
|
|
|
|
WriteFormatted(STD_OUTPUT_HANDLE, L"%1!X! %2!X!", LastError, RebootRequired);
|
2020-11-03 12:27:42 +01:00
|
|
|
|
|
|
|
cleanup:
|
2020-11-02 16:28:51 +01:00
|
|
|
Done();
|
|
|
|
}
|
2020-07-24 09:39:02 +02:00
|
|
|
#endif
|