]> git.ipfire.org Git - thirdparty/wireguard-tools.git/commitdiff
ipc: windows: use devpkey instead of nci for name
authorJason A. Donenfeld <Jason@zx2c4.com>
Fri, 30 Jul 2021 22:39:58 +0000 (00:39 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Fri, 30 Jul 2021 23:01:53 +0000 (01:01 +0200)
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
src/Makefile
src/ipc-windows.h
src/uapi/windows/wireguard.h
src/wincompat/include/nci.h [deleted file]

index 931f261bed404318f0d7343b6cff72e058117e46..b91ade86208da0e20c3735b1dc02e168acba5820 100644 (file)
@@ -62,16 +62,13 @@ ifeq ($(PLATFORM),windows)
 CC := x86_64-w64-mingw32-clang
 WINDRES := $(shell $(CC) $(CFLAGS) -print-prog-name=windres 2>/dev/null)
 CFLAGS += -Iwincompat/include -include wincompat/compat.h -DWINVER=0x0601 -D_WIN32_WINNT=0x0601 -flto
-LDLIBS += -lws2_32 -lsetupapi -lole32 -ladvapi32 -lnci -Lwincompat
+LDLIBS += -lws2_32 -lsetupapi -lole32 -ladvapi32 -Lwincompat
 LDFLAGS += -flto -Wl,--dynamicbase -Wl,--nxcompat -Wl,--tsaware -mconsole
 LDFLAGS += -Wl,--major-os-version=6 -Wl,--minor-os-version=1 -Wl,--major-subsystem-version=6 -Wl,--minor-subsystem-version=1
 # The use of -Wl,/delayload: here implies we're using llvm-mingw
-LDFLAGS += -Wl,/delayload:ws2_32.dll -Wl,/delayload:setupapi.dll -Wl,/delayload:ole32.dll -Wl,/delayload:advapi32.dll -Wl,/delayload:nci.dll
+LDFLAGS += -Wl,/delayload:ws2_32.dll -Wl,/delayload:setupapi.dll -Wl,/delayload:ole32.dll -Wl,/delayload:advapi32.dll
 VERSION := $(patsubst "%",%,$(filter "%",$(file < version.h)))
-wg: wincompat/libc.o wincompat/init.o wincompat/loader.o wincompat/resources.o wincompat/nci.lib
-wincompat/nci.lib: wincompat/include/nci.h
-       -@$(if $(BUILT_IN_RM),$(BUILT_IN_RM),$(RM)) $@
-       $(LINK.o) -DGENERATE_LIB -shared -xc $< -o wincompat/nci.dll -Wl,--out-implib,$@
+wg: wincompat/libc.o wincompat/init.o wincompat/loader.o wincompat/resources.o
 wincompat/resources.o: wincompat/resources.rc wincompat/manifest.xml
        $(WINDRES) -DVERSION_STR=$(VERSION) -O coff -c 65001 -i $< -o $@
 endif
index d19860e6b3f9f92c4a41c8e0f15cb9ccfa1a5a9e..59fa3a96dfd8ba3a4cc7354920267dbe302818c6 100644 (file)
@@ -11,7 +11,6 @@
 #include <initguid.h>
 #include <devguid.h>
 #include <ddk/ndisguid.h>
-#include <nci.h>
 #include <wireguard.h>
 #include <hashtable.h>
 
@@ -19,6 +18,7 @@
 
 static bool have_cached_kernel_interfaces;
 static struct hashtable cached_kernel_interfaces;
+static const DEVPROPKEY devpkey_name = DEVPKEY_WG_NAME;
 
 static int kernel_get_wireguard_interfaces(struct string_list *list)
 {
@@ -32,11 +32,10 @@ static int kernel_get_wireguard_interfaces(struct string_list *list)
 
        for (DWORD i = 0;; ++i) {
                bool found = false;
-               DWORD buf_len = 0, value_type, ret;
+               DWORD buf_len = 0, value_type;
                WCHAR *buf = NULL, adapter_name[MAX_ADAPTER_NAME];
                SP_DEVINFO_DATA dev_info_data = { .cbSize = sizeof(SP_DEVINFO_DATA) };
-               HKEY key;
-               GUID instance_id;
+               DEVPROPTYPE prop_type;
                ULONG status, problem_code;
                char *interface_name;
                struct hashtable_entry *entry;
@@ -49,11 +48,12 @@ static int kernel_get_wireguard_interfaces(struct string_list *list)
 
                while (!SetupDiGetDeviceRegistryPropertyW(dev_info, &dev_info_data, SPDRP_HARDWAREID, &value_type, (BYTE *)buf, buf_len, &buf_len)) {
                        free(buf);
+                       buf = NULL;
                        if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
-                               goto skip;
+                               break;
                        buf = malloc(buf_len);
                        if (!buf)
-                               goto skip;
+                               break;
                }
 
                if (!buf || value_type != REG_MULTI_SZ || buf_len < sizeof(*buf) * 2 || buf[buf_len / sizeof(*buf) - 1] || buf[buf_len / sizeof(*buf) - 2]) {
@@ -70,45 +70,25 @@ static int kernel_get_wireguard_interfaces(struct string_list *list)
                free(buf);
                if (!found)
                        continue;
-               buf = NULL;
-               buf_len = 0;
 
-               key = SetupDiOpenDevRegKey(dev_info, &dev_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_QUERY_VALUE);
-               if (key == INVALID_HANDLE_VALUE)
-                       continue;
-               buf_len = 39 * sizeof(*buf);
-               buf = malloc(buf_len);
-               if (!buf)
+               if (!SetupDiGetDevicePropertyW(dev_info, &dev_info_data, &devpkey_name,
+                                              &prop_type, (PBYTE)adapter_name,
+                                              sizeof(adapter_name), NULL, 0) ||
+                               prop_type != DEVPROP_TYPE_STRING)
                        continue;
-               while ((ret = RegQueryValueExW(key, L"NetCfgInstanceId", NULL, &value_type, (BYTE *)buf, &buf_len)) != ERROR_SUCCESS) {
-                       free(buf);
-                       if (ret != ERROR_MORE_DATA)
-                               goto cleanup_key;
-                       buf = malloc(buf_len);
-                       if (!buf)
-                               goto cleanup_key;
-               }
-               if (!buf || value_type != REG_SZ || buf_len < sizeof(*buf) || buf[buf_len / sizeof(*buf) - 1])
-                       goto cleanup_buf;
-               if (FAILED(CLSIDFromString(buf, &instance_id)))
-                       goto cleanup_buf;
-
-               if (NciGetConnectionName(&instance_id, adapter_name, sizeof(adapter_name), NULL) != ERROR_SUCCESS)
-                       goto cleanup_buf;
                adapter_name[_countof(adapter_name) - 1] = L'0';
                if (!adapter_name[0])
-                       goto cleanup_buf;
-
+                       continue;
                buf_len = WideCharToMultiByte(CP_UTF8, 0, adapter_name, -1, NULL, 0, NULL, NULL);
                if (!buf_len)
-                       goto cleanup_buf;
+                       continue;
                interface_name = malloc(buf_len);
                if (!interface_name)
-                       goto cleanup_buf;
+                       continue;
                buf_len = WideCharToMultiByte(CP_UTF8, 0, adapter_name, -1, interface_name, buf_len, NULL, NULL);
                if (!buf_len) {
                        free(interface_name);
-                       goto cleanup_buf;
+                       continue;
                }
 
                if (CM_Get_DevNode_Status(&status, &problem_code, dev_info_data.DevInst, 0) == CR_SUCCESS &&
@@ -118,26 +98,20 @@ static int kernel_get_wireguard_interfaces(struct string_list *list)
                entry = hashtable_find_or_insert_entry(&cached_kernel_interfaces, interface_name);
                free(interface_name);
                if (!entry)
-                       goto cleanup_entry;
+                       continue;
 
                if (SetupDiGetDeviceInstanceIdW(dev_info, &dev_info_data, NULL, 0, &buf_len) || GetLastError() != ERROR_INSUFFICIENT_BUFFER)
-                       goto cleanup_entry;
+                       continue;
                entry->value = calloc(sizeof(WCHAR), buf_len);
                if (!entry->value)
-                       goto cleanup_entry;
+                       continue;
                if (!SetupDiGetDeviceInstanceIdW(dev_info, &dev_info_data, entry->value, buf_len, &buf_len)) {
                        free(entry->value);
                        entry->value = NULL;
-                       goto cleanup_entry;
+                       continue;
                }
 
-cleanup_entry:
-               will_have_cached_kernel_interfaces |= entry != NULL && entry->value != NULL;
-cleanup_buf:
-               free(buf);
-cleanup_key:
-               RegCloseKey(key);
-skip:;
+               will_have_cached_kernel_interfaces = true;
        }
        SetupDiDestroyDeviceInfoList(dev_info);
        have_cached_kernel_interfaces = will_have_cached_kernel_interfaces;
@@ -187,11 +161,10 @@ err_hash:
 
        for (DWORD i = 0; !interfaces; ++i) {
                bool found = false;
-               DWORD buf_len = 0, value_type, ret;
+               DWORD buf_len = 0, value_type;
                WCHAR *buf = NULL, adapter_name[MAX_ADAPTER_NAME];
                SP_DEVINFO_DATA dev_info_data = { .cbSize = sizeof(SP_DEVINFO_DATA) };
-               HKEY key;
-               GUID instance_id;
+               DEVPROPTYPE prop_type;
                char *interface_name;
 
                if (!SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data)) {
@@ -202,11 +175,12 @@ err_hash:
 
                while (!SetupDiGetDeviceRegistryPropertyW(dev_info, &dev_info_data, SPDRP_HARDWAREID, &value_type, (BYTE *)buf, buf_len, &buf_len)) {
                        free(buf);
+                       buf = NULL;
                        if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
-                               goto skip;
+                               break;
                        buf = malloc(buf_len);
                        if (!buf)
-                               goto skip;
+                               break;
                }
 
                if (!buf || value_type != REG_MULTI_SZ || buf_len < sizeof(*buf) * 2 || buf[buf_len / sizeof(*buf) - 1] || buf[buf_len / sizeof(*buf) - 2]) {
@@ -223,53 +197,28 @@ err_hash:
                free(buf);
                if (!found)
                        continue;
-               found = false;
-               buf = NULL;
-               buf_len = 0;
 
-               key = SetupDiOpenDevRegKey(dev_info, &dev_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_QUERY_VALUE);
-               if (key == INVALID_HANDLE_VALUE)
+               if (!SetupDiGetDevicePropertyW(dev_info, &dev_info_data, &devpkey_name,
+                                              &prop_type, (PBYTE)adapter_name,
+                                              sizeof(adapter_name), NULL, 0) ||
+                               prop_type != DEVPROP_TYPE_STRING)
                        continue;
-               buf_len = 39 * sizeof(*buf);
-               buf = malloc(buf_len);
-               if (!buf)
-                       continue;
-               while ((ret = RegQueryValueExW(key, L"NetCfgInstanceId", NULL, &value_type, (BYTE *)buf, &buf_len)) != ERROR_SUCCESS) {
-                       free(buf);
-                       if (ret != ERROR_MORE_DATA)
-                               goto cleanup_key;
-                       buf = malloc(buf_len);
-                       if (!buf)
-                               goto cleanup_key;
-               }
-               if (!buf || value_type != REG_SZ || buf_len < sizeof(*buf) || buf[buf_len / sizeof(*buf) - 1])
-                       goto cleanup_buf;
-               if (FAILED(CLSIDFromString(buf, &instance_id)))
-                       goto cleanup_buf;
-
-               if (NciGetConnectionName(&instance_id, adapter_name, sizeof(adapter_name), NULL) != ERROR_SUCCESS)
-                       goto cleanup_buf;
                adapter_name[_countof(adapter_name) - 1] = L'0';
                if (!adapter_name[0])
-                       goto cleanup_buf;
-
+                       continue;
                buf_len = WideCharToMultiByte(CP_UTF8, 0, adapter_name, -1, NULL, 0, NULL, NULL);
                if (!buf_len)
-                       goto cleanup_buf;
+                       continue;
                interface_name = malloc(buf_len);
                if (!interface_name)
-                       goto cleanup_buf;
+                       continue;
                buf_len = WideCharToMultiByte(CP_UTF8, 0, adapter_name, -1, interface_name, buf_len, NULL, NULL);
                if (!buf_len) {
                        free(interface_name);
-                       goto cleanup_buf;
+                       continue;
                }
                found = !strcmp(interface_name, iface);
                free(interface_name);
-cleanup_buf:
-               free(buf);
-cleanup_key:
-               RegCloseKey(key);
                if (!found)
                        continue;
 
@@ -296,7 +245,6 @@ cleanup_key:
                }
 cleanup_instance_id:
                free(buf);
-skip:;
        }
        SetupDiDestroyDeviceInfoList(dev_info);
        if (!interfaces) {
index 22fe9b6a9a374ac67b3ced034af70bd18729564f..5c5938e1d6b424e73b06ad037cb0a80897e9b662 100644 (file)
@@ -71,4 +71,10 @@ typedef struct _WG_IOCTL_INTERFACE
 #define WG_IOCTL_GET CTL_CODE(45208U, 321, METHOD_OUT_DIRECT, FILE_READ_DATA | FILE_WRITE_DATA)
 #define WG_IOCTL_SET CTL_CODE(45208U, 322, METHOD_IN_DIRECT, FILE_READ_DATA | FILE_WRITE_DATA)
 
+#define DEVPKEY_WG_NAME (DEVPROPKEY) { \
+               { 0x65726957, 0x7547, 0x7261, { 0x64, 0x4e, 0x61, 0x6d, 0x65, 0x4b, 0x65, 0x79 } }, \
+               DEVPROPID_FIRST_USABLE + 1 \
+       }
+
+
 #endif
diff --git a/src/wincompat/include/nci.h b/src/wincompat/include/nci.h
deleted file mode 100644 (file)
index 895e49a..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0
- *
- * Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
- */
-
-#ifndef _NCI_H
-#define _NCI_H
-
-#include <windows.h>
-
-#ifdef GENERATE_LIB
-#define DECLSPEC __declspec(dllexport)
-#define STUB { return 0; }
-#else
-#define DECLSPEC __declspec(dllimport)
-#define STUB ;
-#endif
-
-
-EXTERN_C DECLSPEC DWORD WINAPI
-NciSetConnectionName(const GUID *Guid, const WCHAR *NewName) STUB
-
-EXTERN_C DECLSPEC DWORD WINAPI
-NciGetConnectionName(
-    const GUID *Guid,
-    WCHAR *Name,
-    DWORD InDestNameBytes,
-    DWORD *OutDestNameBytes) STUB
-
-#endif