From 1079962e4c06f88a54e50d997c1b7e84303d30b4 Mon Sep 17 00:00:00 2001 From: Bernhard Schmidt Date: Sat, 15 Aug 2020 21:29:50 +0200 Subject: New upstream version 2.5~beta1 --- src/tapctl/Makefile.am | 51 ++ src/tapctl/basic.h | 66 ++ src/tapctl/error.c | 36 + src/tapctl/error.h | 97 +++ src/tapctl/main.c | 445 ++++++++++++ src/tapctl/tap.c | 1441 +++++++++++++++++++++++++++++++++++++ src/tapctl/tap.h | 177 +++++ src/tapctl/tapctl.exe.manifest | 10 + src/tapctl/tapctl.props | 18 + src/tapctl/tapctl.vcxproj | 145 ++++ src/tapctl/tapctl.vcxproj.filters | 49 ++ src/tapctl/tapctl_resources.rc | 64 ++ 12 files changed, 2599 insertions(+) create mode 100644 src/tapctl/Makefile.am create mode 100644 src/tapctl/basic.h create mode 100644 src/tapctl/error.c create mode 100644 src/tapctl/error.h create mode 100644 src/tapctl/main.c create mode 100644 src/tapctl/tap.c create mode 100644 src/tapctl/tap.h create mode 100644 src/tapctl/tapctl.exe.manifest create mode 100644 src/tapctl/tapctl.props create mode 100644 src/tapctl/tapctl.vcxproj create mode 100644 src/tapctl/tapctl.vcxproj.filters create mode 100644 src/tapctl/tapctl_resources.rc (limited to 'src/tapctl') diff --git a/src/tapctl/Makefile.am b/src/tapctl/Makefile.am new file mode 100644 index 0000000..583a45f --- /dev/null +++ b/src/tapctl/Makefile.am @@ -0,0 +1,51 @@ +# +# tapctl -- Utility to manipulate TUN/TAP interfaces on Windows +# +# Copyright (C) 2002-2018 OpenVPN Inc +# Copyright (C) 2018 Simon Rozman +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 +# as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +include $(top_srcdir)/build/ltrc.inc + +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in + +EXTRA_DIST = \ + tapctl.vcxproj \ + tapctl.vcxproj.filters \ + tapctl.props \ + tapctl.exe.manifest + +AM_CPPFLAGS = \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat + +AM_CFLAGS = \ + $(TAP_CFLAGS) + +if WIN32 +sbin_PROGRAMS = tapctl +tapctl_CFLAGS = \ + -municode -D_UNICODE \ + -UNTDDI_VERSION -U_WIN32_WINNT \ + -D_WIN32_WINNT=_WIN32_WINNT_VISTA +tapctl_LDADD = -ladvapi32 -lole32 -lsetupapi +endif + +tapctl_SOURCES = \ + basic.h \ + error.c error.h \ + main.c \ + tap.c tap.h \ + tapctl_resources.rc diff --git a/src/tapctl/basic.h b/src/tapctl/basic.h new file mode 100644 index 0000000..a0a8851 --- /dev/null +++ b/src/tapctl/basic.h @@ -0,0 +1,66 @@ +/* + * basic -- Basic macros + * https://community.openvpn.net/openvpn/wiki/Tapctl + * + * Copyright (C) 2002-2018 OpenVPN Inc + * Copyright (C) 2018 Simon Rozman + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef BASIC_H +#define BASIC_H + +#ifdef _UNICODE +#define PRIsLPTSTR "ls" +#define PRIsLPOLESTR "ls" +#else +#define PRIsLPTSTR "s" +#define PRIsLPOLESTR "ls" +#endif +#define PRIXGUID "{%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}" +#define PRIGUID_PARAM(g) \ + (g).Data1, (g).Data2, (g).Data3, (g).Data4[0], (g).Data4[1], (g).Data4[2], (g).Data4[3], (g).Data4[4], (g).Data4[5], (g).Data4[6], (g).Data4[7] +#define PRIGUID_PARAM_REF(g) \ + &(g).Data1, &(g).Data2, &(g).Data3, &(g).Data4[0], &(g).Data4[1], &(g).Data4[2], &(g).Data4[3], &(g).Data4[4], &(g).Data4[5], &(g).Data4[6], &(g).Data4[7] + +#define __L(q) L ## q +#define _L(q) __L(q) + +#ifndef _In_ +#define _In_ +#endif +#ifndef _In_opt_ +#define _In_opt_ +#endif +#ifndef _In_z_ +#define _In_z_ +#endif +#ifndef _Inout_ +#define _Inout_ +#endif +#ifndef _Inout_opt_ +#define _Inout_opt_ +#endif +#ifndef _Out_ +#define _Out_ +#endif +#ifndef _Out_opt_ +#define _Out_opt_ +#endif +#ifndef _Out_z_cap_ +#define _Out_z_cap_(n) +#endif + +#endif /* ifndef BASIC_H */ diff --git a/src/tapctl/error.c b/src/tapctl/error.c new file mode 100644 index 0000000..d1f77d2 --- /dev/null +++ b/src/tapctl/error.c @@ -0,0 +1,36 @@ +/* + * error -- OpenVPN compatible error reporting API + * https://community.openvpn.net/openvpn/wiki/Tapctl + * + * Copyright (C) 2002-2018 OpenVPN Inc + * Copyright (C) 2018 Simon Rozman + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "error.h" + + +/* Globals */ +unsigned int x_debug_level; /* GLOBAL */ + + +void +x_msg(const unsigned int flags, const char *format, ...) +{ + va_list arglist; + va_start(arglist, format); + x_msg_va(flags, format, arglist); + va_end(arglist); +} diff --git a/src/tapctl/error.h b/src/tapctl/error.h new file mode 100644 index 0000000..924cbbe --- /dev/null +++ b/src/tapctl/error.h @@ -0,0 +1,97 @@ +/* + * error -- OpenVPN compatible error reporting API + * https://community.openvpn.net/openvpn/wiki/Tapctl + * + * Copyright (C) 2002-2018 OpenVPN Inc + * Copyright (C) 2018 Simon Rozman + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef ERROR_H +#define ERROR_H + +#include +#include +#include + +/* + * These globals should not be accessed directly, + * but rather through macros or inline functions defined below. + */ +extern unsigned int x_debug_level; +extern int x_msg_line_num; + +/* msg() flags */ + +#define M_DEBUG_LEVEL (0x0F) /* debug level mask */ + +#define M_FATAL (1<<4) /* exit program */ +#define M_NONFATAL (1<<5) /* non-fatal error */ +#define M_WARN (1<<6) /* call syslog with LOG_WARNING */ +#define M_DEBUG (1<<7) + +#define M_ERRNO (1<<8) /* show errno description */ + +#define M_NOMUTE (1<<11) /* don't do mute processing */ +#define M_NOPREFIX (1<<12) /* don't show date/time prefix */ +#define M_USAGE_SMALL (1<<13) /* fatal options error, call usage_small */ +#define M_MSG_VIRT_OUT (1<<14) /* output message through msg_status_output callback */ +#define M_OPTERR (1<<15) /* print "Options error:" prefix */ +#define M_NOLF (1<<16) /* don't print new line */ +#define M_NOIPREFIX (1<<17) /* don't print instance prefix */ + +/* flag combinations which are frequently used */ +#define M_ERR (M_FATAL | M_ERRNO) +#define M_USAGE (M_USAGE_SMALL | M_NOPREFIX | M_OPTERR) +#define M_CLIENT (M_MSG_VIRT_OUT | M_NOMUTE | M_NOIPREFIX) + + +/** Check muting filter */ +bool dont_mute(unsigned int flags); + +/* Macro to ensure (and teach static analysis tools) we exit on fatal errors */ +#ifdef _MSC_VER +#pragma warning(disable: 4127) /* EXIT_FATAL(flags) macro raises "warning C4127: conditional expression is constant" on each non M_FATAL invocation. */ +#endif +#define EXIT_FATAL(flags) do { if ((flags) & M_FATAL) {_exit(1);}} while (false) + +#define HAVE_VARARG_MACROS +#define msg(flags, ...) do { if (msg_test(flags)) {x_msg((flags), __VA_ARGS__);} EXIT_FATAL(flags); } while (false) +#ifdef ENABLE_DEBUG +#define dmsg(flags, ...) do { if (msg_test(flags)) {x_msg((flags), __VA_ARGS__);} EXIT_FATAL(flags); } while (false) +#else +#define dmsg(flags, ...) +#endif + +void x_msg(const unsigned int flags, const char *format, ...); /* should be called via msg above */ + +void x_msg_va(const unsigned int flags, const char *format, va_list arglist); + +/* Inline functions */ + +static inline bool +check_debug_level(unsigned int level) +{ + return (level & M_DEBUG_LEVEL) <= x_debug_level; +} + +/** Return true if flags represent and enabled, not muted log level */ +static inline bool +msg_test(unsigned int flags) +{ + return check_debug_level(flags) && dont_mute(flags); +} + +#endif /* ifndef ERROR_H */ diff --git a/src/tapctl/main.c b/src/tapctl/main.c new file mode 100644 index 0000000..31bb2ec --- /dev/null +++ b/src/tapctl/main.c @@ -0,0 +1,445 @@ +/* + * tapctl -- Utility to manipulate TUN/TAP adapters on Windows + * https://community.openvpn.net/openvpn/wiki/Tapctl + * + * Copyright (C) 2002-2018 OpenVPN Inc + * Copyright (C) 2008-2013 David Sommerseth + * Copyright (C) 2018 Simon Rozman + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#elif defined(_MSC_VER) +#include +#endif +#ifdef HAVE_CONFIG_VERSION_H +#include +#endif + +#include "tap.h" +#include "error.h" + +#include +#include +#include +#include + +#ifdef _MSC_VER +#pragma comment(lib, "ole32.lib") +#pragma comment(lib, "setupapi.lib") +#endif + + +const TCHAR title_string[] = + TEXT(PACKAGE_NAME) TEXT(" ") TEXT(PACKAGE_VERSION) + TEXT(" built on ") TEXT(__DATE__) +; + +static const TCHAR usage_message[] = + TEXT("%s\n") + TEXT("\n") + TEXT("Usage:\n") + TEXT("\n") + TEXT("tapctl []\n") + TEXT("\n") + TEXT("Commands:\n") + TEXT("\n") + TEXT("create Create a new TUN/TAP adapter\n") + TEXT("list List TUN/TAP adapters\n") + TEXT("delete Delete specified network adapter\n") + TEXT("help Display this text\n") + TEXT("\n") + TEXT("Hint: Use \"tapctl help \" to display help for particular command.\n") +; + +static const TCHAR usage_message_create[] = + TEXT("%s\n") + TEXT("\n") + TEXT("Creates a new TUN/TAP adapter\n") + TEXT("\n") + TEXT("Usage:\n") + TEXT("\n") + TEXT("tapctl create []\n") + TEXT("\n") + TEXT("Options:\n") + TEXT("\n") + TEXT("--name Set TUN/TAP adapter name. Should the adapter with given name \n") + TEXT(" already exist, an error is returned. If this option is not \n") + TEXT(" specified, a default adapter name is chosen by Windows. \n") + TEXT(" Note: This name can also be specified as OpenVPN's --dev-node \n") + TEXT(" option. \n") + TEXT("--hwid Adapter hardware ID. Default value is root\\tap0901, which \n") + TEXT(" describes tap-windows6 driver. To work with wintun driver, \n") + TEXT(" specify 'wintun'. \n") + TEXT("\n") + TEXT("Output:\n") + TEXT("\n") + TEXT("This command prints newly created TUN/TAP adapter's GUID to stdout. \n") +; + +static const TCHAR usage_message_list[] = + TEXT("%s\n") + TEXT("\n") + TEXT("Lists TUN/TAP adapters\n") + TEXT("\n") + TEXT("Usage:\n") + TEXT("\n") + TEXT("tapctl list\n") + TEXT("\n") + TEXT("Options:\n") + TEXT("\n") + TEXT("--hwid Adapter hardware ID. By default, root\\tap0901, tap0901 and \n") + TEXT(" wintun adapters are listed. Use this switch to limit the list. \n") + TEXT("\n") + TEXT("Output:\n") + TEXT("\n") + TEXT("This command prints all TUN/TAP adapters to stdout. \n") +; + +static const TCHAR usage_message_delete[] = + TEXT("%s\n") + TEXT("\n") + TEXT("Deletes the specified network adapter\n") + TEXT("\n") + TEXT("Usage:\n") + TEXT("\n") + TEXT("tapctl delete \n") +; + + +/** + * Print the help message. + */ +static void +usage(void) +{ + _ftprintf(stderr, + usage_message, + title_string); +} + + +/** + * Program entry point + */ +int __cdecl +_tmain(int argc, LPCTSTR argv[]) +{ + int iResult; + BOOL bRebootRequired = FALSE; + + /* Ask SetupAPI to keep quiet. */ + SetupSetNonInteractiveMode(TRUE); + + if (argc < 2) + { + usage(); + return 1; + } + else if (_tcsicmp(argv[1], TEXT("help")) == 0) + { + /* Output help. */ + if (argc < 3) + { + usage(); + } + else if (_tcsicmp(argv[2], TEXT("create")) == 0) + { + _ftprintf(stderr, usage_message_create, title_string); + } + else if (_tcsicmp(argv[2], TEXT("list")) == 0) + { + _ftprintf(stderr, usage_message_list, title_string); + } + else if (_tcsicmp(argv[2], TEXT("delete")) == 0) + { + _ftprintf(stderr, usage_message_delete, title_string); + } + else + { + _ftprintf(stderr, TEXT("Unknown command \"%s\". Please, use \"tapctl help\" to list supported commands.\n"), argv[2]); + } + + return 1; + } + else if (_tcsicmp(argv[1], TEXT("create")) == 0) + { + LPCTSTR szName = NULL; + LPCTSTR szHwId = TEXT("root\\") TEXT(TAP_WIN_COMPONENT_ID); + + /* Parse options. */ + for (int i = 2; i < argc; i++) + { + if (_tcsicmp(argv[i], TEXT("--name")) == 0) + { + szName = argv[++i]; + } + else + if (_tcsicmp(argv[i], TEXT("--hwid")) == 0) + { + szHwId = argv[++i]; + } + else + { + _ftprintf(stderr, TEXT("Unknown option \"%s\". Please, use \"tapctl help create\" to list supported options. Ignored.\n"), argv[i]); + } + } + + /* Create TUN/TAP adapter. */ + GUID guidAdapter; + LPOLESTR szAdapterId = NULL; + DWORD dwResult = tap_create_adapter( + NULL, + TEXT("Virtual Ethernet"), + szHwId, + &bRebootRequired, + &guidAdapter); + if (dwResult != ERROR_SUCCESS) + { + _ftprintf(stderr, TEXT("Creating TUN/TAP adapter failed (error 0x%x).\n"), dwResult); + iResult = 1; goto quit; + } + + if (szName) + { + /* Get existing network adapters. */ + struct tap_adapter_node *pAdapterList = NULL; + dwResult = tap_list_adapters(NULL, NULL, &pAdapterList); + if (dwResult != ERROR_SUCCESS) + { + _ftprintf(stderr, TEXT("Enumerating adapters failed (error 0x%x).\n"), dwResult); + iResult = 1; goto create_delete_adapter; + } + + /* Check for duplicates. */ + for (struct tap_adapter_node *pAdapter = pAdapterList; pAdapter; pAdapter = pAdapter->pNext) + { + if (_tcsicmp(szName, pAdapter->szName) == 0) + { + StringFromIID((REFIID)&pAdapter->guid, &szAdapterId); + _ftprintf(stderr, TEXT("Adapter \"%s\" already exists (GUID %") TEXT(PRIsLPOLESTR) TEXT(").\n"), pAdapter->szName, szAdapterId); + CoTaskMemFree(szAdapterId); + iResult = 1; goto create_cleanup_pAdapterList; + } + } + + /* Rename the adapter. */ + dwResult = tap_set_adapter_name(&guidAdapter, szName); + if (dwResult != ERROR_SUCCESS) + { + StringFromIID((REFIID)&guidAdapter, &szAdapterId); + _ftprintf(stderr, TEXT("Renaming TUN/TAP adapter %") TEXT(PRIsLPOLESTR) TEXT(" to \"%s\" failed (error 0x%x).\n"), szAdapterId, szName, dwResult); + CoTaskMemFree(szAdapterId); + iResult = 1; goto quit; + } + + iResult = 0; + +create_cleanup_pAdapterList: + tap_free_adapter_list(pAdapterList); + if (iResult) + { + goto create_delete_adapter; + } + } + + /* Output adapter GUID. */ + StringFromIID((REFIID)&guidAdapter, &szAdapterId); + _ftprintf(stdout, TEXT("%") TEXT(PRIsLPOLESTR) TEXT("\n"), szAdapterId); + CoTaskMemFree(szAdapterId); + + iResult = 0; goto quit; + +create_delete_adapter: + tap_delete_adapter( + NULL, + &guidAdapter, + &bRebootRequired); + iResult = 1; goto quit; + } + else if (_tcsicmp(argv[1], TEXT("list")) == 0) + { + TCHAR szzHwId[0x100] = + TEXT("root\\") TEXT(TAP_WIN_COMPONENT_ID) TEXT("\0") + TEXT(TAP_WIN_COMPONENT_ID) TEXT("\0") + TEXT("Wintun\0"); + + /* Parse options. */ + for (int i = 2; i < argc; i++) + { + if (_tcsicmp(argv[i], TEXT("--hwid")) == 0) + { + memset(szzHwId, 0, sizeof(szzHwId)); + ++i; + memcpy_s(szzHwId, sizeof(szzHwId) - 2*sizeof(TCHAR) /*requires double zero termination*/, argv[i], _tcslen(argv[i])*sizeof(TCHAR)); + } + else + { + _ftprintf(stderr, TEXT("Unknown option \"%s\". Please, use \"tapctl help list\" to list supported options. Ignored.\n"), argv[i]); + } + } + + /* Output list of adapters with given hardware ID. */ + struct tap_adapter_node *pAdapterList = NULL; + DWORD dwResult = tap_list_adapters(NULL, szzHwId, &pAdapterList); + if (dwResult != ERROR_SUCCESS) + { + _ftprintf(stderr, TEXT("Enumerating TUN/TAP adapters failed (error 0x%x).\n"), dwResult); + iResult = 1; goto quit; + } + + for (struct tap_adapter_node *pAdapter = pAdapterList; pAdapter; pAdapter = pAdapter->pNext) + { + LPOLESTR szAdapterId = NULL; + StringFromIID((REFIID)&pAdapter->guid, &szAdapterId); + _ftprintf(stdout, TEXT("%") TEXT(PRIsLPOLESTR) TEXT("\t%") TEXT(PRIsLPTSTR) TEXT("\n"), szAdapterId, pAdapter->szName); + CoTaskMemFree(szAdapterId); + } + + iResult = 0; + tap_free_adapter_list(pAdapterList); + } + else if (_tcsicmp(argv[1], TEXT("delete")) == 0) + { + if (argc < 3) + { + _ftprintf(stderr, TEXT("Missing adapter GUID or name. Please, use \"tapctl help delete\" for usage info.\n")); + return 1; + } + + GUID guidAdapter; + if (FAILED(IIDFromString(argv[2], (LPIID)&guidAdapter))) + { + /* The argument failed to covert to GUID. Treat it as the adapter name. */ + struct tap_adapter_node *pAdapterList = NULL; + DWORD dwResult = tap_list_adapters(NULL, NULL, &pAdapterList); + if (dwResult != ERROR_SUCCESS) + { + _ftprintf(stderr, TEXT("Enumerating TUN/TAP adapters failed (error 0x%x).\n"), dwResult); + iResult = 1; goto quit; + } + + for (struct tap_adapter_node *pAdapter = pAdapterList;; pAdapter = pAdapter->pNext) + { + if (pAdapter == NULL) + { + _ftprintf(stderr, TEXT("\"%s\" adapter not found.\n"), argv[2]); + iResult = 1; goto delete_cleanup_pAdapterList; + } + else if (_tcsicmp(argv[2], pAdapter->szName) == 0) + { + memcpy(&guidAdapter, &pAdapter->guid, sizeof(GUID)); + break; + } + } + + iResult = 0; + +delete_cleanup_pAdapterList: + tap_free_adapter_list(pAdapterList); + if (iResult) + { + goto quit; + } + } + + /* Delete the network adapter. */ + DWORD dwResult = tap_delete_adapter( + NULL, + &guidAdapter, + &bRebootRequired); + if (dwResult != ERROR_SUCCESS) + { + _ftprintf(stderr, TEXT("Deleting adapter \"%s\" failed (error 0x%x).\n"), argv[2], dwResult); + iResult = 1; goto quit; + } + + iResult = 0; goto quit; + } + else + { + _ftprintf(stderr, TEXT("Unknown command \"%s\". Please, use \"tapctl help\" to list supported commands.\n"), argv[1]); + return 1; + } + +quit: + if (bRebootRequired) + { + _ftprintf(stderr, TEXT("A system reboot is required.\n")); + } + + return iResult; +} + + +bool +dont_mute(unsigned int flags) +{ + UNREFERENCED_PARAMETER(flags); + + return true; +} + + +void +x_msg_va(const unsigned int flags, const char *format, va_list arglist) +{ + /* Output message string. Note: Message strings don't contain line terminators. */ + vfprintf(stderr, format, arglist); + _ftprintf(stderr, TEXT("\n")); + + if ((flags & M_ERRNO) != 0) + { + /* Output system error message (if possible). */ + DWORD dwResult = GetLastError(); + LPTSTR szErrMessage = NULL; + if (FormatMessage( + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, + 0, + dwResult, + 0, + (LPTSTR)&szErrMessage, + 0, + NULL) && szErrMessage) + { + /* Trim trailing whitespace. Set terminator after the last non-whitespace character. This prevents excessive trailing line breaks. */ + for (size_t i = 0, i_last = 0;; i++) + { + if (szErrMessage[i]) + { + if (!_istspace(szErrMessage[i])) + { + i_last = i + 1; + } + } + else + { + szErrMessage[i_last] = 0; + break; + } + } + + /* Output error message. */ + _ftprintf(stderr, TEXT("Error 0x%x: %s\n"), dwResult, szErrMessage); + + LocalFree(szErrMessage); + } + else + { + _ftprintf(stderr, TEXT("Error 0x%x\n"), dwResult); + } + } +} diff --git a/src/tapctl/tap.c b/src/tapctl/tap.c new file mode 100644 index 0000000..7cb3ded --- /dev/null +++ b/src/tapctl/tap.c @@ -0,0 +1,1441 @@ +/* + * tapctl -- Utility to manipulate TUN/TAP adapters on Windows + * https://community.openvpn.net/openvpn/wiki/Tapctl + * + * Copyright (C) 2018-2020 Simon Rozman + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#elif defined(_MSC_VER) +#include +#endif + +#include "tap.h" +#include "error.h" + +#include +#include +#include +#include +#include +#include + +#ifdef _MSC_VER +#pragma comment(lib, "advapi32.lib") +#pragma comment(lib, "ole32.lib") +#pragma comment(lib, "setupapi.lib") +#endif + +const static GUID GUID_DEVCLASS_NET = { 0x4d36e972L, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } }; + +const static TCHAR szAdapterRegKeyPathTemplate[] = TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\%") TEXT(PRIsLPOLESTR) TEXT("\\%") TEXT(PRIsLPOLESTR) TEXT("\\Connection"); +#define ADAPTER_REGKEY_PATH_MAX (_countof(TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\")) - 1 + 38 + _countof(TEXT("\\")) - 1 + 38 + _countof(TEXT("\\Connection"))) + + +/** + * Returns length of string of strings + * + * @param szz Pointer to a string of strings (terminated by an empty string) + * + * @return Number of characters not counting the final zero terminator + **/ +static inline size_t +_tcszlen(_In_z_ LPCTSTR szz) +{ + LPCTSTR s; + for (s = szz; s[0]; s += _tcslen(s) + 1) + { + } + return s - szz; +} + + +/** + * Checks if string is contained in the string of strings. Comparison is made case-insensitive. + * + * @param szzHay Pointer to a string of strings (terminated by an empty string) we are + * looking in + * + * @param szNeedle The string we are searching for + * + * @return Pointer to the string in szzHay that matches szNeedle is found; NULL otherwise + */ +static LPCTSTR +_tcszistr(_In_z_ LPCTSTR szzHay, _In_z_ LPCTSTR szNeedle) +{ + for (LPCTSTR s = szzHay; s[0]; s += _tcslen(s) + 1) + { + if (_tcsicmp(s, szNeedle) == 0) + { + return s; + } + } + + return NULL; +} + + +/** + * Function that performs a specific task on a device + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +typedef DWORD (*devop_func_t)( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _Inout_ LPBOOL pbRebootRequired); + + +/** + * Checks device install parameters if a system reboot is required. + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +check_reboot( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _Inout_ LPBOOL pbRebootRequired) +{ + if (pbRebootRequired == NULL) + { + return ERROR_BAD_ARGUMENTS; + } + + SP_DEVINSTALL_PARAMS devinstall_params = { .cbSize = sizeof(SP_DEVINSTALL_PARAMS) }; + if (!SetupDiGetDeviceInstallParams( + hDeviceInfoSet, + pDeviceInfoData, + &devinstall_params)) + { + DWORD dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiGetDeviceInstallParams failed", __FUNCTION__); + return dwResult; + } + + if ((devinstall_params.Flags & (DI_NEEDREBOOT | DI_NEEDRESTART)) != 0) + { + *pbRebootRequired = TRUE; + } + + return ERROR_SUCCESS; +} + + +/** + * Deletes the device. + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +delete_device( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _Inout_ LPBOOL pbRebootRequired) +{ + SP_REMOVEDEVICE_PARAMS params = + { + .ClassInstallHeader = + { + .cbSize = sizeof(SP_CLASSINSTALL_HEADER), + .InstallFunction = DIF_REMOVE, + }, + .Scope = DI_REMOVEDEVICE_GLOBAL, + .HwProfile = 0, + }; + + /* Set class installer parameters for DIF_REMOVE. */ + if (!SetupDiSetClassInstallParams( + hDeviceInfoSet, + pDeviceInfoData, + ¶ms.ClassInstallHeader, + sizeof(SP_REMOVEDEVICE_PARAMS))) + { + DWORD dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiSetClassInstallParams failed", __FUNCTION__); + return dwResult; + } + + /* Call appropriate class installer. */ + if (!SetupDiCallClassInstaller( + DIF_REMOVE, + hDeviceInfoSet, + pDeviceInfoData)) + { + DWORD dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiCallClassInstaller(DIF_REMOVE) failed", __FUNCTION__); + return dwResult; + } + + /* Check if a system reboot is required. */ + check_reboot(hDeviceInfoSet, pDeviceInfoData, pbRebootRequired); + return ERROR_SUCCESS; +} + + +/** + * Changes the device state. + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param bEnable TRUE to enable the device; FALSE to disable. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +change_device_state( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _In_ BOOL bEnable, + _Inout_ LPBOOL pbRebootRequired) +{ + SP_PROPCHANGE_PARAMS params = + { + .ClassInstallHeader = + { + .cbSize = sizeof(SP_CLASSINSTALL_HEADER), + .InstallFunction = DIF_PROPERTYCHANGE, + }, + .StateChange = bEnable ? DICS_ENABLE : DICS_DISABLE, + .Scope = DICS_FLAG_GLOBAL, + .HwProfile = 0, + }; + + /* Set class installer parameters for DIF_PROPERTYCHANGE. */ + if (!SetupDiSetClassInstallParams( + hDeviceInfoSet, + pDeviceInfoData, + ¶ms.ClassInstallHeader, + sizeof(SP_PROPCHANGE_PARAMS))) + { + DWORD dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiSetClassInstallParams failed", __FUNCTION__); + return dwResult; + } + + /* Call appropriate class installer. */ + if (!SetupDiCallClassInstaller( + DIF_PROPERTYCHANGE, + hDeviceInfoSet, + pDeviceInfoData)) + { + DWORD dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiCallClassInstaller(DIF_PROPERTYCHANGE) failed", __FUNCTION__); + return dwResult; + } + + /* Check if a system reboot is required. */ + check_reboot(hDeviceInfoSet, pDeviceInfoData, pbRebootRequired); + return ERROR_SUCCESS; +} + + +/** + * Enables the device. + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +enable_device( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _Inout_ LPBOOL pbRebootRequired) +{ + return change_device_state(hDeviceInfoSet, pDeviceInfoData, TRUE, pbRebootRequired); +} + + +/** + * Disables the device. + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +disable_device( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _Inout_ LPBOOL pbRebootRequired) +{ + return change_device_state(hDeviceInfoSet, pDeviceInfoData, FALSE, pbRebootRequired); +} + + +/** + * Reads string value from registry key. + * + * @param hKey Handle of the registry key to read from. Must be opened with read + * access. + * + * @param szName Name of the value to read. + * + * @param pszValue Pointer to string to retrieve registry value. If the value type is + * REG_EXPAND_SZ the value is expanded using ExpandEnvironmentStrings(). + * The string must be released with free() after use. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + */ +static DWORD +get_reg_string( + _In_ HKEY hKey, + _In_ LPCTSTR szName, + _Out_ LPTSTR *pszValue) +{ + if (pszValue == NULL) + { + return ERROR_BAD_ARGUMENTS; + } + + DWORD dwValueType = REG_NONE, dwSize = 0; + DWORD dwResult = RegQueryValueEx( + hKey, + szName, + NULL, + &dwValueType, + NULL, + &dwSize); + if (dwResult != ERROR_SUCCESS) + { + SetLastError(dwResult); /* MSDN does not mention RegQueryValueEx() to set GetLastError(). But we do have an error code. Set last error manually. */ + msg(M_NONFATAL | M_ERRNO, "%s: enumerating \"%" PRIsLPTSTR "\" registry value failed", __FUNCTION__, szName); + return dwResult; + } + + switch (dwValueType) + { + case REG_SZ: + case REG_EXPAND_SZ: + { + /* Read value. */ + LPTSTR szValue = (LPTSTR)malloc(dwSize); + if (szValue == NULL) + { + msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, dwSize); + return ERROR_OUTOFMEMORY; + } + + dwResult = RegQueryValueEx( + hKey, + szName, + NULL, + NULL, + (LPBYTE)szValue, + &dwSize); + if (dwResult != ERROR_SUCCESS) + { + SetLastError(dwResult); /* MSDN does not mention RegQueryValueEx() to set GetLastError(). But we do have an error code. Set last error manually. */ + msg(M_NONFATAL | M_ERRNO, "%s: reading \"%" PRIsLPTSTR "\" registry value failed", __FUNCTION__, szName); + free(szValue); + return dwResult; + } + + if (dwValueType == REG_EXPAND_SZ) + { + /* Expand the environment strings. */ + DWORD + dwSizeExp = dwSize * 2, + dwCountExp = +#ifdef UNICODE + dwSizeExp / sizeof(TCHAR); +#else + dwSizeExp / sizeof(TCHAR) - 1; /* Note: ANSI version requires one extra char. */ +#endif + LPTSTR szValueExp = (LPTSTR)malloc(dwSizeExp); + if (szValueExp == NULL) + { + free(szValue); + msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, dwSizeExp); + return ERROR_OUTOFMEMORY; + } + + DWORD dwCountExpResult = ExpandEnvironmentStrings( + szValue, + szValueExp, dwCountExp + ); + if (dwCountExpResult == 0) + { + msg(M_NONFATAL | M_ERRNO, "%s: expanding \"%" PRIsLPTSTR "\" registry value failed", __FUNCTION__, szName); + free(szValueExp); + free(szValue); + return dwResult; + } + else if (dwCountExpResult <= dwCountExp) + { + /* The buffer was big enough. */ + free(szValue); + *pszValue = szValueExp; + return ERROR_SUCCESS; + } + else + { + /* Retry with a bigger buffer. */ + free(szValueExp); +#ifdef UNICODE + dwSizeExp = dwCountExpResult * sizeof(TCHAR); +#else + /* Note: ANSI version requires one extra char. */ + dwSizeExp = (dwCountExpResult + 1) * sizeof(TCHAR); +#endif + dwCountExp = dwCountExpResult; + szValueExp = (LPTSTR)malloc(dwSizeExp); + if (szValueExp == NULL) + { + free(szValue); + msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, dwSizeExp); + return ERROR_OUTOFMEMORY; + } + + dwCountExpResult = ExpandEnvironmentStrings( + szValue, + szValueExp, dwCountExp); + free(szValue); + *pszValue = szValueExp; + return ERROR_SUCCESS; + } + } + else + { + *pszValue = szValue; + return ERROR_SUCCESS; + } + } + + default: + msg(M_NONFATAL, "%s: \"%" PRIsLPTSTR "\" registry value is not string (type %u)", __FUNCTION__, dwValueType); + return ERROR_UNSUPPORTED_TYPE; + } +} + + +/** + * Returns network adapter ID. + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param iNumAttempts After the device is created, it might take some time before the + * registry key is populated. This parameter specifies the number of + * attempts to read NetCfgInstanceId value from registry. A 1sec sleep + * is inserted between retry attempts. + * + * @param pguidAdapter A pointer to GUID that receives network adapter ID. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +get_net_adapter_guid( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _In_ int iNumAttempts, + _Out_ LPGUID pguidAdapter) +{ + DWORD dwResult = ERROR_BAD_ARGUMENTS; + + if (pguidAdapter == NULL || iNumAttempts < 1) + { + return ERROR_BAD_ARGUMENTS; + } + + /* Open HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\\ registry key. */ + HKEY hKey = SetupDiOpenDevRegKey( + hDeviceInfoSet, + pDeviceInfoData, + DICS_FLAG_GLOBAL, + 0, + DIREG_DRV, + KEY_READ); + if (hKey == INVALID_HANDLE_VALUE) + { + dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiOpenDevRegKey failed", __FUNCTION__); + return dwResult; + } + + while (iNumAttempts > 0) + { + /* Query the NetCfgInstanceId value. Using get_reg_string() right on might clutter the output with error messages while the registry is still being populated. */ + LPTSTR szCfgGuidString = NULL; + dwResult = RegQueryValueEx(hKey, TEXT("NetCfgInstanceId"), NULL, NULL, NULL, NULL); + if (dwResult != ERROR_SUCCESS) + { + if (dwResult == ERROR_FILE_NOT_FOUND && --iNumAttempts > 0) + { + /* Wait and retry. */ + Sleep(1000); + continue; + } + + SetLastError(dwResult); /* MSDN does not mention RegQueryValueEx() to set GetLastError(). But we do have an error code. Set last error manually. */ + msg(M_NONFATAL | M_ERRNO, "%s: querying \"NetCfgInstanceId\" registry value failed", __FUNCTION__); + break; + } + + /* Read the NetCfgInstanceId value now. */ + dwResult = get_reg_string( + hKey, + TEXT("NetCfgInstanceId"), + &szCfgGuidString); + if (dwResult != ERROR_SUCCESS) + { + break; + } + + dwResult = SUCCEEDED(CLSIDFromString(szCfgGuidString, (LPCLSID)pguidAdapter)) ? ERROR_SUCCESS : ERROR_INVALID_DATA; + free(szCfgGuidString); + break; + } + + RegCloseKey(hKey); + return dwResult; +} + + +/** + * Returns a specified Plug and Play device property. + * + * @param hDeviceInfoSet A handle to a device information set that contains a device + * information element that represents the device. + * + * @param pDeviceInfoData A pointer to an SP_DEVINFO_DATA structure that specifies the + * device information element in hDeviceInfoSet. + * + * @param dwProperty Specifies the property to be retrieved. See + * https://msdn.microsoft.com/en-us/library/windows/hardware/ff551967.aspx + * + * @pdwPropertyRegDataType A pointer to a variable that receives the data type of the + * property that is being retrieved. This is one of the standard + * registry data types. This parameter is optional and can be NULL. + * + * @param ppData A pointer to pointer to data that receives the device property. The + * data must be released with free() after use. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +get_device_reg_property( + _In_ HDEVINFO hDeviceInfoSet, + _In_ PSP_DEVINFO_DATA pDeviceInfoData, + _In_ DWORD dwProperty, + _Out_opt_ LPDWORD pdwPropertyRegDataType, + _Out_ LPVOID *ppData) +{ + DWORD dwResult = ERROR_BAD_ARGUMENTS; + + if (ppData == NULL) + { + return ERROR_BAD_ARGUMENTS; + } + + /* Try with stack buffer first. */ + BYTE bBufStack[128]; + DWORD dwRequiredSize = 0; + if (SetupDiGetDeviceRegistryProperty( + hDeviceInfoSet, + pDeviceInfoData, + dwProperty, + pdwPropertyRegDataType, + bBufStack, + sizeof(bBufStack), + &dwRequiredSize)) + { + /* Copy from stack. */ + *ppData = malloc(dwRequiredSize); + if (*ppData == NULL) + { + msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, dwRequiredSize); + return ERROR_OUTOFMEMORY; + } + + memcpy(*ppData, bBufStack, dwRequiredSize); + return ERROR_SUCCESS; + } + else + { + dwResult = GetLastError(); + if (dwResult == ERROR_INSUFFICIENT_BUFFER) + { + /* Allocate on heap and retry. */ + *ppData = malloc(dwRequiredSize); + if (*ppData == NULL) + { + msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, dwRequiredSize); + return ERROR_OUTOFMEMORY; + } + + if (SetupDiGetDeviceRegistryProperty( + hDeviceInfoSet, + pDeviceInfoData, + dwProperty, + pdwPropertyRegDataType, + *ppData, + dwRequiredSize, + &dwRequiredSize)) + { + return ERROR_SUCCESS; + } + else + { + dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiGetDeviceRegistryProperty(%u) failed", __FUNCTION__, dwProperty); + return dwResult; + } + } + else + { + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiGetDeviceRegistryProperty(%u) failed", __FUNCTION__, dwProperty); + return dwResult; + } + } +} + + +DWORD +tap_create_adapter( + _In_opt_ HWND hwndParent, + _In_opt_ LPCTSTR szDeviceDescription, + _In_ LPCTSTR szHwId, + _Inout_ LPBOOL pbRebootRequired, + _Out_ LPGUID pguidAdapter) +{ + DWORD dwResult; + + if (szHwId == NULL + || pbRebootRequired == NULL + || pguidAdapter == NULL) + { + return ERROR_BAD_ARGUMENTS; + } + + /* Create an empty device info set for network adapter device class. */ + HDEVINFO hDevInfoList = SetupDiCreateDeviceInfoList(&GUID_DEVCLASS_NET, hwndParent); + if (hDevInfoList == INVALID_HANDLE_VALUE) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiCreateDeviceInfoList failed", __FUNCTION__); + return dwResult; + } + + /* Get the device class name from GUID. */ + TCHAR szClassName[MAX_CLASS_NAME_LEN]; + if (!SetupDiClassNameFromGuid( + &GUID_DEVCLASS_NET, + szClassName, + _countof(szClassName), + NULL)) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiClassNameFromGuid failed", __FUNCTION__); + goto cleanup_hDevInfoList; + } + + /* Create a new device info element and add it to the device info set. */ + SP_DEVINFO_DATA devinfo_data = { .cbSize = sizeof(SP_DEVINFO_DATA) }; + if (!SetupDiCreateDeviceInfo( + hDevInfoList, + szClassName, + &GUID_DEVCLASS_NET, + szDeviceDescription, + hwndParent, + DICD_GENERATE_ID, + &devinfo_data)) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiCreateDeviceInfo failed", __FUNCTION__); + goto cleanup_hDevInfoList; + } + + /* Set a device information element as the selected member of a device information set. */ + if (!SetupDiSetSelectedDevice( + hDevInfoList, + &devinfo_data)) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiSetSelectedDevice failed", __FUNCTION__); + goto cleanup_hDevInfoList; + } + + /* Set Plug&Play device hardware ID property. */ + if (!SetupDiSetDeviceRegistryProperty( + hDevInfoList, + &devinfo_data, + SPDRP_HARDWAREID, + (const BYTE *)szHwId, (DWORD)((_tcslen(szHwId) + 1) * sizeof(TCHAR)))) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiSetDeviceRegistryProperty failed", __FUNCTION__); + goto cleanup_hDevInfoList; + } + + /* Search for the driver. */ + if (!SetupDiBuildDriverInfoList( + hDevInfoList, + &devinfo_data, + SPDIT_CLASSDRIVER)) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiBuildDriverInfoList failed", __FUNCTION__); + goto cleanup_hDevInfoList; + } + DWORDLONG dwlDriverVersion = 0; + DWORD drvinfo_detail_data_size = sizeof(SP_DRVINFO_DETAIL_DATA) + 0x100; + SP_DRVINFO_DETAIL_DATA *drvinfo_detail_data = (SP_DRVINFO_DETAIL_DATA *)malloc(drvinfo_detail_data_size); + if (drvinfo_detail_data == NULL) + { + msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, drvinfo_detail_data_size); + dwResult = ERROR_OUTOFMEMORY; goto cleanup_DriverInfoList; + } + + for (DWORD dwIndex = 0;; dwIndex++) + { + /* Get a driver from the list. */ + SP_DRVINFO_DATA drvinfo_data = { .cbSize = sizeof(SP_DRVINFO_DATA) }; + if (!SetupDiEnumDriverInfo( + hDevInfoList, + &devinfo_data, + SPDIT_CLASSDRIVER, + dwIndex, + &drvinfo_data)) + { + if (GetLastError() == ERROR_NO_MORE_ITEMS) + { + break; + } + else + { + /* Something is wrong with this driver. Skip it. */ + msg(M_WARN | M_ERRNO, "%s: SetupDiEnumDriverInfo(%u) failed", __FUNCTION__, dwIndex); + continue; + } + } + + /* Get driver info details. */ + DWORD dwSize; + drvinfo_detail_data->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA); + if (!SetupDiGetDriverInfoDetail( + hDevInfoList, + &devinfo_data, + &drvinfo_data, + drvinfo_detail_data, + drvinfo_detail_data_size, + &dwSize)) + { + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) + { + /* (Re)allocate buffer. */ + if (drvinfo_detail_data) + { + free(drvinfo_detail_data); + } + + drvinfo_detail_data_size = dwSize; + drvinfo_detail_data = (SP_DRVINFO_DETAIL_DATA *)malloc(drvinfo_detail_data_size); + if (drvinfo_detail_data == NULL) + { + msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, drvinfo_detail_data_size); + dwResult = ERROR_OUTOFMEMORY; goto cleanup_DriverInfoList; + } + + /* Re-get driver info details. */ + drvinfo_detail_data->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA); + if (!SetupDiGetDriverInfoDetail( + hDevInfoList, + &devinfo_data, + &drvinfo_data, + drvinfo_detail_data, + drvinfo_detail_data_size, + &dwSize)) + { + /* Something is wrong with this driver. Skip it. */ + continue; + } + } + else + { + /* Something is wrong with this driver. Skip it. */ + msg(M_WARN | M_ERRNO, "%s: SetupDiGetDriverInfoDetail(\"%hs\") failed", __FUNCTION__, drvinfo_data.Description); + continue; + } + } + + /* Check the driver version and hardware ID. */ + if (dwlDriverVersion < drvinfo_data.DriverVersion + && drvinfo_detail_data->HardwareID + && _tcszistr(drvinfo_detail_data->HardwareID, szHwId)) + { + /* Newer version and matching hardware ID found. Select the driver. */ + if (!SetupDiSetSelectedDriver( + hDevInfoList, + &devinfo_data, + &drvinfo_data)) + { + /* Something is wrong with this driver. Skip it. */ + msg(M_WARN | M_ERRNO, "%s: SetupDiSetSelectedDriver(\"%hs\") failed", __FUNCTION__, drvinfo_data.Description); + continue; + } + + dwlDriverVersion = drvinfo_data.DriverVersion; + } + } + if (drvinfo_detail_data) + { + free(drvinfo_detail_data); + } + + if (dwlDriverVersion == 0) + { + dwResult = ERROR_NOT_FOUND; + msg(M_NONFATAL, "%s: No driver for device \"%" PRIsLPTSTR "\" installed.", __FUNCTION__, szHwId); + goto cleanup_DriverInfoList; + } + + /* Call appropriate class installer. */ + if (!SetupDiCallClassInstaller( + DIF_REGISTERDEVICE, + hDevInfoList, + &devinfo_data)) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiCallClassInstaller(DIF_REGISTERDEVICE) failed", __FUNCTION__); + goto cleanup_DriverInfoList; + } + + /* Register device co-installers if any. */ + if (!SetupDiCallClassInstaller( + DIF_REGISTER_COINSTALLERS, + hDevInfoList, + &devinfo_data)) + { + dwResult = GetLastError(); + msg(M_WARN | M_ERRNO, "%s: SetupDiCallClassInstaller(DIF_REGISTER_COINSTALLERS) failed", __FUNCTION__); + } + + /* Install adapters if any. */ + if (!SetupDiCallClassInstaller( + DIF_INSTALLINTERFACES, + hDevInfoList, + &devinfo_data)) + { + dwResult = GetLastError(); + msg(M_WARN | M_ERRNO, "%s: SetupDiCallClassInstaller(DIF_INSTALLINTERFACES) failed", __FUNCTION__); + } + + /* Install the device. */ + if (!SetupDiCallClassInstaller( + DIF_INSTALLDEVICE, + hDevInfoList, + &devinfo_data)) + { + dwResult = GetLastError(); + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiCallClassInstaller(DIF_INSTALLDEVICE) failed", __FUNCTION__); + goto cleanup_remove_device; + } + + /* Check if a system reboot is required. (Ignore errors) */ + check_reboot(hDevInfoList, &devinfo_data, pbRebootRequired); + + /* Get network adapter ID from registry. Retry for max 30sec. */ + dwResult = get_net_adapter_guid(hDevInfoList, &devinfo_data, 30, pguidAdapter); + +cleanup_remove_device: + if (dwResult != ERROR_SUCCESS) + { + /* The adapter was installed. But, the adapter ID was unobtainable. Clean-up. */ + SP_REMOVEDEVICE_PARAMS removedevice_params = + { + .ClassInstallHeader = + { + .cbSize = sizeof(SP_CLASSINSTALL_HEADER), + .InstallFunction = DIF_REMOVE, + }, + .Scope = DI_REMOVEDEVICE_GLOBAL, + .HwProfile = 0, + }; + + /* Set class installer parameters for DIF_REMOVE. */ + if (SetupDiSetClassInstallParams( + hDevInfoList, + &devinfo_data, + &removedevice_params.ClassInstallHeader, + sizeof(SP_REMOVEDEVICE_PARAMS))) + { + /* Call appropriate class installer. */ + if (SetupDiCallClassInstaller( + DIF_REMOVE, + hDevInfoList, + &devinfo_data)) + { + /* Check if a system reboot is required. */ + check_reboot(hDevInfoList, &devinfo_data, pbRebootRequired); + } + else + { + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiCallClassInstaller(DIF_REMOVE) failed", __FUNCTION__); + } + } + else + { + msg(M_NONFATAL | M_ERRNO, "%s: SetupDiSetClassInstallParams failed", __FUNCTION__); + } + } + +cleanup_DriverInfoList: + SetupDiDestroyDriverInfoList( + hDevInfoList, + &devinfo_data, + SPDIT_CLASSDRIVER); + +cleanup_hDevInfoList: + SetupDiDestroyDeviceInfoList(hDevInfoList); + return dwResult; +} + + +/** + * Performs a given task on an adapter. + * + * @param hwndParent A handle to the top-level window to use for any user adapter that is + * related to non-device-specific actions (such as a select-device dialog + * box that uses the global class driver list). This handle is optional + * and can be NULL. If a specific top-level window is not required, set + * hwndParent to NULL. + * + * @param pguidAdapter A pointer to GUID that contains network adapter ID. + * + * @param funcOperation A pointer for the function to perform specific task on the adapter. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +static DWORD +execute_on_first_adapter( + _In_opt_ HWND hwndParent, + _In_ LPCGUID pguidAdapter, + _In_ devop_func_t funcOperation, + _Inout_ LPBOOL pbRebootRequired) +{ + DWORD dwResult; + + if (pguidAdapter == NULL) + { + return ERROR_BAD_ARGUMENTS; + } + + /* Create a list of network devices. */ + HDEVINFO hDevInfoList = SetupDiGetClassDevsEx( + &GUID_DEVCLASS_NET, + NULL, + hwndParent, + DIGCF_PRESENT, + NULL, + NULL, + NULL); + if (hDevInfoList == INVALID_HANDLE_VALUE) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiGetClassDevsEx failed", __FUNCTION__); + return dwResult; + } + + /* Retrieve information associated with a device information set. */ + SP_DEVINFO_LIST_DETAIL_DATA devinfo_list_detail_data = { .cbSize = sizeof(SP_DEVINFO_LIST_DETAIL_DATA) }; + if (!SetupDiGetDeviceInfoListDetail(hDevInfoList, &devinfo_list_detail_data)) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiGetDeviceInfoListDetail failed", __FUNCTION__); + goto cleanup_hDevInfoList; + } + + /* Iterate. */ + for (DWORD dwIndex = 0;; dwIndex++) + { + /* Get the device from the list. */ + SP_DEVINFO_DATA devinfo_data = { .cbSize = sizeof(SP_DEVINFO_DATA) }; + if (!SetupDiEnumDeviceInfo( + hDevInfoList, + dwIndex, + &devinfo_data)) + { + if (GetLastError() == ERROR_NO_MORE_ITEMS) + { + LPOLESTR szAdapterId = NULL; + StringFromIID((REFIID)pguidAdapter, &szAdapterId); + msg(M_NONFATAL, "%s: Adapter %" PRIsLPOLESTR " not found", __FUNCTION__, szAdapterId); + CoTaskMemFree(szAdapterId); + dwResult = ERROR_FILE_NOT_FOUND; + goto cleanup_hDevInfoList; + } + else + { + /* Something is wrong with this device. Skip it. */ + msg(M_WARN | M_ERRNO, "%s: SetupDiEnumDeviceInfo(%u) failed", __FUNCTION__, dwIndex); + continue; + } + } + + /* Get adapter GUID. */ + GUID guidAdapter; + dwResult = get_net_adapter_guid(hDevInfoList, &devinfo_data, 1, &guidAdapter); + if (dwResult != ERROR_SUCCESS) + { + /* Something is wrong with this device. Skip it. */ + continue; + } + + /* Compare GUIDs. */ + if (memcmp(pguidAdapter, &guidAdapter, sizeof(GUID)) == 0) + { + dwResult = funcOperation(hDevInfoList, &devinfo_data, pbRebootRequired); + break; + } + } + +cleanup_hDevInfoList: + SetupDiDestroyDeviceInfoList(hDevInfoList); + return dwResult; +} + + +DWORD +tap_delete_adapter( + _In_opt_ HWND hwndParent, + _In_ LPCGUID pguidAdapter, + _Inout_ LPBOOL pbRebootRequired) +{ + return execute_on_first_adapter(hwndParent, pguidAdapter, delete_device, pbRebootRequired); +} + + +DWORD +tap_enable_adapter( + _In_opt_ HWND hwndParent, + _In_ LPCGUID pguidAdapter, + _In_ BOOL bEnable, + _Inout_ LPBOOL pbRebootRequired) +{ + return execute_on_first_adapter(hwndParent, pguidAdapter, bEnable ? enable_device : disable_device, pbRebootRequired); +} + +/* stripped version of ExecCommand in interactive.c */ +static DWORD +ExecCommand(const WCHAR* cmdline) +{ + DWORD exit_code; + STARTUPINFOW si; + PROCESS_INFORMATION pi; + DWORD proc_flags = CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT; + WCHAR* cmdline_dup = NULL; + + ZeroMemory(&si, sizeof(si)); + ZeroMemory(&pi, sizeof(pi)); + + si.cb = sizeof(si); + + /* CreateProcess needs a modifiable cmdline: make a copy */ + cmdline_dup = _wcsdup(cmdline); + if (cmdline_dup && CreateProcessW(NULL, cmdline_dup, NULL, NULL, FALSE, + proc_flags, NULL, NULL, &si, &pi)) + { + WaitForSingleObject(pi.hProcess, INFINITE); + if (!GetExitCodeProcess(pi.hProcess, &exit_code)) + { + exit_code = GetLastError(); + } + + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); + } + else + { + exit_code = GetLastError(); + } + + free(cmdline_dup); + return exit_code; +} + +DWORD +tap_set_adapter_name( + _In_ LPCGUID pguidAdapter, + _In_ LPCTSTR szName) +{ + DWORD dwResult; + + if (pguidAdapter == NULL || szName == NULL) + { + return ERROR_BAD_ARGUMENTS; + } + + /* Get the device class GUID as string. */ + LPOLESTR szDevClassNetId = NULL; + StringFromIID((REFIID)&GUID_DEVCLASS_NET, &szDevClassNetId); + + /* Get the adapter GUID as string. */ + LPOLESTR szAdapterId = NULL; + StringFromIID((REFIID)pguidAdapter, &szAdapterId); + + /* Render registry key path. */ + TCHAR szRegKey[ADAPTER_REGKEY_PATH_MAX]; + _stprintf_s( + szRegKey, _countof(szRegKey), + szAdapterRegKeyPathTemplate, + szDevClassNetId, + szAdapterId); + + /* Open network adapter registry key. */ + HKEY hKey = NULL; + dwResult = RegOpenKeyEx( + HKEY_LOCAL_MACHINE, + szRegKey, + 0, + KEY_QUERY_VALUE, + &hKey); + if (dwResult != ERROR_SUCCESS) + { + SetLastError(dwResult); /* MSDN does not mention RegOpenKeyEx() to set GetLastError(). But we do have an error code. Set last error manually. */ + msg(M_NONFATAL | M_ERRNO, "%s: RegOpenKeyEx(HKLM, \"%" PRIsLPTSTR "\") failed", __FUNCTION__, szRegKey); + goto cleanup_szAdapterId; + } + + LPTSTR szOldName = NULL; + dwResult = get_reg_string(hKey, TEXT("Name"), &szOldName); + if (dwResult != ERROR_SUCCESS) + { + SetLastError(dwResult); + msg(M_NONFATAL | M_ERRNO, "%s: Error reading adapter name", __FUNCTION__); + goto cleanup_hKey; + } + + /* rename adapter via netsh call */ + const TCHAR* szFmt = _T("netsh interface set interface name=\"%s\" newname=\"%s\""); + size_t ncmdline = _tcslen(szFmt) + _tcslen(szOldName) + _tcslen(szName) + 1; + WCHAR* szCmdLine = malloc(ncmdline * sizeof(TCHAR)); + _stprintf_s(szCmdLine, ncmdline, szFmt, szOldName, szName); + + free(szOldName); + + dwResult = ExecCommand(szCmdLine); + free(szCmdLine); + + if (dwResult != ERROR_SUCCESS) + { + SetLastError(dwResult); + msg(M_NONFATAL | M_ERRNO, "%s: Error renaming adapter", __FUNCTION__); + goto cleanup_hKey; + } + +cleanup_hKey: + RegCloseKey(hKey); +cleanup_szAdapterId: + CoTaskMemFree(szAdapterId); + CoTaskMemFree(szDevClassNetId); + return dwResult; +} + + +DWORD +tap_list_adapters( + _In_opt_ HWND hwndParent, + _In_opt_ LPCTSTR szzHwIDs, + _Out_ struct tap_adapter_node **ppAdapter) +{ + DWORD dwResult; + + if (ppAdapter == NULL) + { + return ERROR_BAD_ARGUMENTS; + } + + /* Create a list of network devices. */ + HDEVINFO hDevInfoList = SetupDiGetClassDevsEx( + &GUID_DEVCLASS_NET, + NULL, + hwndParent, + DIGCF_PRESENT, + NULL, + NULL, + NULL); + if (hDevInfoList == INVALID_HANDLE_VALUE) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiGetClassDevsEx failed", __FUNCTION__); + return dwResult; + } + + /* Retrieve information associated with a device information set. */ + SP_DEVINFO_LIST_DETAIL_DATA devinfo_list_detail_data = { .cbSize = sizeof(SP_DEVINFO_LIST_DETAIL_DATA) }; + if (!SetupDiGetDeviceInfoListDetail(hDevInfoList, &devinfo_list_detail_data)) + { + dwResult = GetLastError(); + msg(M_NONFATAL, "%s: SetupDiGetDeviceInfoListDetail failed", __FUNCTION__); + goto cleanup_hDevInfoList; + } + + /* Get the device class GUID as string. */ + LPOLESTR szDevClassNetId = NULL; + StringFromIID((REFIID)&GUID_DEVCLASS_NET, &szDevClassNetId); + + /* Iterate. */ + *ppAdapter = NULL; + struct tap_adapter_node *pAdapterTail = NULL; + for (DWORD dwIndex = 0;; dwIndex++) + { + /* Get the device from the list. */ + SP_DEVINFO_DATA devinfo_data = { .cbSize = sizeof(SP_DEVINFO_DATA) }; + if (!SetupDiEnumDeviceInfo( + hDevInfoList, + dwIndex, + &devinfo_data)) + { + if (GetLastError() == ERROR_NO_MORE_ITEMS) + { + break; + } + else + { + /* Something is wrong with this device. Skip it. */ + msg(M_WARN | M_ERRNO, "%s: SetupDiEnumDeviceInfo(%u) failed", __FUNCTION__, dwIndex); + continue; + } + } + + /* Get device hardware ID(s). */ + DWORD dwDataType = REG_NONE; + LPTSTR szzDeviceHardwareIDs = NULL; + dwResult = get_device_reg_property( + hDevInfoList, + &devinfo_data, + SPDRP_HARDWAREID, + &dwDataType, + (LPVOID)&szzDeviceHardwareIDs); + if (dwResult != ERROR_SUCCESS) + { + /* Something is wrong with this device. Skip it. */ + continue; + } + + /* Check that hardware ID is REG_SZ/REG_MULTI_SZ, and optionally if it matches ours. */ + if (dwDataType == REG_SZ) + { + if (szzHwIDs && !_tcszistr(szzHwIDs, szzDeviceHardwareIDs)) + { + /* This is not our device. Skip it. */ + goto cleanup_szzDeviceHardwareIDs; + } + } + else if (dwDataType == REG_MULTI_SZ) + { + if (szzHwIDs) + { + for (LPTSTR s = szzDeviceHardwareIDs;; s += _tcslen(s) + 1) + { + if (s[0] == 0) + { + /* This is not our device. Skip it. */ + goto cleanup_szzDeviceHardwareIDs; + } + else if (_tcszistr(szzHwIDs, s)) + { + /* This is our device. */ + break; + } + } + } + } + else + { + /* Unexpected hardware ID format. Skip device. */ + goto cleanup_szzDeviceHardwareIDs; + } + + /* Get adapter GUID. */ + GUID guidAdapter; + dwResult = get_net_adapter_guid(hDevInfoList, &devinfo_data, 1, &guidAdapter); + if (dwResult != ERROR_SUCCESS) + { + /* Something is wrong with this device. Skip it. */ + goto cleanup_szzDeviceHardwareIDs; + } + + /* Get the adapter GUID as string. */ + LPOLESTR szAdapterId = NULL; + StringFromIID((REFIID)&guidAdapter, &szAdapterId); + + /* Render registry key path. */ + TCHAR szRegKey[ADAPTER_REGKEY_PATH_MAX]; + _stprintf_s( + szRegKey, _countof(szRegKey), + szAdapterRegKeyPathTemplate, + szDevClassNetId, + szAdapterId); + + /* Open network adapter registry key. */ + HKEY hKey = NULL; + dwResult = RegOpenKeyEx( + HKEY_LOCAL_MACHINE, + szRegKey, + 0, + KEY_READ, + &hKey); + if (dwResult != ERROR_SUCCESS) + { + SetLastError(dwResult); /* MSDN does not mention RegOpenKeyEx() to set GetLastError(). But we do have an error code. Set last error manually. */ + msg(M_WARN | M_ERRNO, "%s: RegOpenKeyEx(HKLM, \"%" PRIsLPTSTR "\") failed", __FUNCTION__, szRegKey); + goto cleanup_szAdapterId; + } + + /* Read adapter name. */ + LPTSTR szName = NULL; + dwResult = get_reg_string( + hKey, + TEXT("Name"), + &szName); + if (dwResult != ERROR_SUCCESS) + { + SetLastError(dwResult); + msg(M_WARN | M_ERRNO, "%s: Cannot determine %" PRIsLPOLESTR " adapter name", __FUNCTION__, szAdapterId); + goto cleanup_hKey; + } + + /* Append to the list. */ + size_t hwid_size = (_tcszlen(szzDeviceHardwareIDs) + 1) * sizeof(TCHAR); + size_t name_size = (_tcslen(szName) + 1) * sizeof(TCHAR); + struct tap_adapter_node *node = (struct tap_adapter_node *)malloc(sizeof(struct tap_adapter_node) + hwid_size + name_size); + if (node == NULL) + { + msg(M_FATAL, "%s: malloc(%u) failed", __FUNCTION__, sizeof(struct tap_adapter_node) + hwid_size + name_size); + dwResult = ERROR_OUTOFMEMORY; goto cleanup_szName; + } + + memcpy(&node->guid, &guidAdapter, sizeof(GUID)); + node->szzHardwareIDs = (LPTSTR)(node + 1); + memcpy(node->szzHardwareIDs, szzDeviceHardwareIDs, hwid_size); + node->szName = (LPTSTR)((LPBYTE)node->szzHardwareIDs + hwid_size); + memcpy(node->szName, szName, name_size); + node->pNext = NULL; + if (pAdapterTail) + { + pAdapterTail->pNext = node; + pAdapterTail = node; + } + else + { + *ppAdapter = pAdapterTail = node; + } + +cleanup_szName: + free(szName); +cleanup_hKey: + RegCloseKey(hKey); +cleanup_szAdapterId: + CoTaskMemFree(szAdapterId); +cleanup_szzDeviceHardwareIDs: + free(szzDeviceHardwareIDs); + } + + dwResult = ERROR_SUCCESS; + + CoTaskMemFree(szDevClassNetId); +cleanup_hDevInfoList: + SetupDiDestroyDeviceInfoList(hDevInfoList); + return dwResult; +} + + +void +tap_free_adapter_list( + _In_ struct tap_adapter_node *pAdapterList) +{ + /* Iterate over all nodes of the list. */ + while (pAdapterList) + { + struct tap_adapter_node *node = pAdapterList; + pAdapterList = pAdapterList->pNext; + + /* Free the adapter node. */ + free(node); + } +} diff --git a/src/tapctl/tap.h b/src/tapctl/tap.h new file mode 100644 index 0000000..102de32 --- /dev/null +++ b/src/tapctl/tap.h @@ -0,0 +1,177 @@ +/* + * tapctl -- Utility to manipulate TUN/TAP adapters on Windows + * https://community.openvpn.net/openvpn/wiki/Tapctl + * + * Copyright (C) 2018-2020 Simon Rozman + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef TAP_H +#define TAP_H + +#include +#include "basic.h" + + +/** + * Creates a TUN/TAP adapter. + * + * @param hwndParent A handle to the top-level window to use for any user adapter that is + * related to non-device-specific actions (such as a select-device dialog + * box that uses the global class driver list). This handle is optional + * and can be NULL. If a specific top-level window is not required, set + * hwndParent to NULL. + * + * @param szDeviceDescription A pointer to a NULL-terminated string that supplies the text + * description of the device. This pointer is optional and can be NULL. + * + * @param szHwId A pointer to a NULL-terminated string that supplies the hardware id + * of the device (e.g. "root\\tap0901", "Wintun"). + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @param pguidAdapter A pointer to GUID that receives network adapter ID. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +DWORD +tap_create_adapter( + _In_opt_ HWND hwndParent, + _In_opt_ LPCTSTR szDeviceDescription, + _In_ LPCTSTR szHwId, + _Inout_ LPBOOL pbRebootRequired, + _Out_ LPGUID pguidAdapter); + + +/** + * Deletes an adapter. + * + * @param hwndParent A handle to the top-level window to use for any user adapter that is + * related to non-device-specific actions (such as a select-device dialog + * box that uses the global class driver list). This handle is optional + * and can be NULL. If a specific top-level window is not required, set + * hwndParent to NULL. + * + * @param pguidAdapter A pointer to GUID that contains network adapter ID. + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +DWORD +tap_delete_adapter( + _In_opt_ HWND hwndParent, + _In_ LPCGUID pguidAdapter, + _Inout_ LPBOOL pbRebootRequired); + + +/** + * Enables or disables an adapter. + * + * @param hwndParent A handle to the top-level window to use for any user adapter that is + * related to non-device-specific actions (such as a select-device dialog + * box that uses the global class driver list). This handle is optional + * and can be NULL. If a specific top-level window is not required, set + * hwndParent to NULL. + * + * @param pguidAdapter A pointer to GUID that contains network adapter ID. + * + * @param bEnable TRUE to enable; FALSE to disable + * + * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, + * this flag is set to TRUE. Otherwise, the flag is left unmodified. This + * allows the flag to be globally initialized to FALSE and reused for multiple + * adapter manipulations. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +DWORD +tap_enable_adapter( + _In_opt_ HWND hwndParent, + _In_ LPCGUID pguidAdapter, + _In_ BOOL bEnable, + _Inout_ LPBOOL pbRebootRequired); + + +/** + * Sets adapter name. + * + * @param pguidAdapter A pointer to GUID that contains network adapter ID. + * + * @param szName New adapter name - must be unique + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + **/ +DWORD +tap_set_adapter_name( + _In_ LPCGUID pguidAdapter, + _In_ LPCTSTR szName); + + +/** + * Network adapter list node + */ +struct tap_adapter_node +{ + GUID guid; /** Adapter GUID */ + LPTSTR szzHardwareIDs; /** Device hardware ID(s) */ + LPTSTR szName; /** Adapter name */ + + struct tap_adapter_node *pNext; /** Pointer to next adapter */ +}; + + +/** + * Creates a list of existing network adapters. + * + * @param hwndParent A handle to the top-level window to use for any user adapter that is + * related to non-device-specific actions (such as a select-device dialog + * box that uses the global class driver list). This handle is optional + * and can be NULL. If a specific top-level window is not required, set + * hwndParent to NULL. + * + * @param szzHwIDs A string of strings that supplies the list of hardware IDs of the device. + * This pointer is optional and can be NULL. When NULL, all network adapters + * found are added to the list. + * + * @param ppAdapterList A pointer to the list to receive pointer to the first adapter in + * the list. After the list is no longer required, free it using + * tap_free_adapter_list(). + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise + */ +DWORD +tap_list_adapters( + _In_opt_ HWND hwndParent, + _In_opt_ LPCTSTR szzHwIDs, + _Out_ struct tap_adapter_node **ppAdapterList); + + +/** + * Frees a list of network adapters. + * + * @param pAdapterList A pointer to the first adapter in the list to free. + */ +void +tap_free_adapter_list( + _In_ struct tap_adapter_node *pAdapterList); + +#endif /* ifndef TAP_H */ diff --git a/src/tapctl/tapctl.exe.manifest b/src/tapctl/tapctl.exe.manifest new file mode 100644 index 0000000..1eb5ea8 --- /dev/null +++ b/src/tapctl/tapctl.exe.manifest @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/src/tapctl/tapctl.props b/src/tapctl/tapctl.props new file mode 100644 index 0000000..0257b9f --- /dev/null +++ b/src/tapctl/tapctl.props @@ -0,0 +1,18 @@ + + + + + + false + + + + _CONSOLE;_WIN32_WINNT=_WIN32_WINNT_VISTA;%(PreprocessorDefinitions) + ..\compat;$(TAP_WINDOWS_HOME)/include;%(AdditionalIncludeDirectories) + + + Console + + + + \ No newline at end of file diff --git a/src/tapctl/tapctl.vcxproj b/src/tapctl/tapctl.vcxproj new file mode 100644 index 0000000..1d593fc --- /dev/null +++ b/src/tapctl/tapctl.vcxproj @@ -0,0 +1,145 @@ + + + + + Debug + ARM64 + + + Debug + Win32 + + + Debug + x64 + + + Release + ARM64 + + + Release + Win32 + + + Release + x64 + + + + 15.0 + {A06436E7-D576-490D-8BA0-0751D920334A} + Win32Proj + tapctl + 10.0 + + + + Application + true + v142 + Unicode + true + + + Application + true + v142 + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + true + + + Application + false + v142 + true + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {8598c2c8-34c4-47a1-99b0-7c295a890615} + false + + + + + + + + + \ No newline at end of file diff --git a/src/tapctl/tapctl.vcxproj.filters b/src/tapctl/tapctl.vcxproj.filters new file mode 100644 index 0000000..c7f71e9 --- /dev/null +++ b/src/tapctl/tapctl.vcxproj.filters @@ -0,0 +1,49 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + + + Resource Files + + + + + Resource Files + + + \ No newline at end of file diff --git a/src/tapctl/tapctl_resources.rc b/src/tapctl/tapctl_resources.rc new file mode 100644 index 0000000..2b3ff23 --- /dev/null +++ b/src/tapctl/tapctl_resources.rc @@ -0,0 +1,64 @@ +/* + * tapctl -- Utility to manipulate TUN/TAP adapters on Windows + * + * Copyright (C) 2018 Simon Rozman + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include +#else +#include +#endif +#include + +#pragma code_page(65001) /* UTF8 */ + +LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL + +VS_VERSION_INFO VERSIONINFO + FILEVERSION OPENVPN_VERSION_RESOURCE + PRODUCTVERSION OPENVPN_VERSION_RESOURCE + FILEFLAGSMASK VS_FF_DEBUG | VS_FF_PRERELEASE | VS_FF_PATCHED | VS_FF_PRIVATEBUILD | VS_FF_SPECIALBUILD +#ifdef _DEBUG + FILEFLAGS VS_FF_DEBUG +#else + FILEFLAGS 0x0L +#endif + FILEOS VOS_NT_WINDOWS32 + FILETYPE VFT_APP + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904b0" + BEGIN + VALUE "CompanyName", "The OpenVPN Project" + VALUE "FileDescription", "Utility to manipulate TUN/TAP adapters on Windows" + VALUE "FileVersion", PACKAGE_VERSION ".0" + VALUE "InternalName", "OpenVPN" + VALUE "LegalCopyright", "Copyright © The OpenVPN Project" + VALUE "OriginalFilename", "tapctl.exe" + VALUE "ProductName", "OpenVPN" + VALUE "ProductVersion", PACKAGE_VERSION ".0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1200 + END +END + +1 RT_MANIFEST "tapctl.exe.manifest" -- cgit v1.2.3