Commit Graph
100 Commits
Author SHA1 Message Date
Simon Rozman 3e08b8aee0 wintun: migrate to wintun.dll API
Rather than having every application using Wintun driver reinvent the
wheel, the Wintun device/adapter/interface management has been moved
from wireguard-go to wintun.dll deployed with Wintun itself.

Signed-off-by: Simon Rozman <[email protected]>
2020-11-07 12:46:35 +01:00
Simon Rozman fdba6c183a wintun: make remaining HWID comparisons case insensitive
c85e4a410f introduced preliminary HWID
checking to speed up Wintun adapter enumeration. However, all HWID are
case insensitive by Windows convention.

Furthermore, a device might have multiple HWIDs. When DevInfo's
DeviceRegistryProperty(SPDRP_HARDWAREID) method returns []string, all
strings returned should be checked against given hardware ID.

This issue was discovered when researching Wintun and wireguard-go on
Windows 10 ARM64. The Wintun adapter was created using devcon.exe
utility with "wintun" hardware ID, causing wireguard-go fail to
enumerate the adapter properly.

Signed-off-by: Simon Rozman <[email protected]>
2020-05-02 01:50:47 -06:00
Simon Rozman 250b9795f3 setupapi: extend struct size constant definitions for arm(64)
Signed-off-by: Simon Rozman <[email protected]>
2020-05-02 01:50:47 -06:00
Simon Rozman a12b765784 namespaceapi: initial version
Signed-off-by: Simon Rozman <[email protected]>
2019-08-30 15:34:17 +02:00
Simon Rozman 69c26dc258 wintun: introduce adapter pools
This makes wintun package reusable for non-WireGuard applications.

Signed-off-by: Simon Rozman <[email protected]>
2019-08-29 18:00:44 +02:00
Simon Rozman 7305b4ce93 wintun: upgrade deleting all interfaces and make it reusable
DeleteAllInterfaces() didn't check if SPDRP_DEVICEDESC == "WireGuard
Tunnel". It deleted _all_ Wintun adapters, not just WireGuard's.

Furthermore, the DeleteAllInterfaces() was upgraded into a new function
called DeleteMatchingInterfaces() for selectively deletion. This will
be used by WireGuard to clean stale Wintun adapters.

Signed-off-by: Simon Rozman <[email protected]>
2019-08-28 11:39:01 +02:00
Simon Rozman 1ff37e2b07 wintun: merge opening device registry key
This also introduces waiting for key to appear on initial access.

See if this resolves the issue caused by HDD power-up delay resulting in
failure to create the adapter.

Signed-off-by: Simon Rozman <[email protected]>
2019-08-02 16:08:49 +02:00
Simon Rozman f5e54932e6 wintun: simplify checking reboot requirement
We never checked checkReboot() reported error anyway.

Signed-off-by: Simon Rozman <[email protected]>
2019-08-02 16:08:49 +02:00
Simon Rozman 73698066d1 wintun: refactor err == nil error checking
Signed-off-by: Simon Rozman <[email protected]>
2019-08-02 15:18:58 +02:00
Simon Rozman 2e24e7dcae tun: windows: implement ring buffers
Signed-off-by: Simon Rozman <[email protected]>
2019-07-17 14:32:13 +02:00
Simon Rozman baafe92888 setupapi: add SetDeviceRegistryPropertyString description
Signed-off-by: Simon Rozman <[email protected]>
2019-06-10 10:43:04 +02:00
Simon Rozman a1a97d1e41 setupapi: unify ERROR_INSUFFICIENT_BUFFER handling
Signed-off-by: Simon Rozman <[email protected]>
2019-06-10 10:43:03 +02:00
Simon Rozman c2ed133df8 wintun: simplify DeleteInterface method signature
Signed-off-by: Simon Rozman <[email protected]>
2019-06-06 08:58:26 +02:00
Simon Rozman e4b0ef29a1 tun: windows: obsolete 256 packets per exchange buffer limitation
Signed-off-by: Simon Rozman <[email protected]>
2019-06-05 11:55:28 +02:00
Simon Rozman 625e445b22 setupapi, wintun: replace syscall with golang.org/x/sys/windows
Signed-off-by: Simon Rozman <[email protected]>
2019-06-04 14:54:56 +02:00
Simon Rozman 85b85e62e5 wintun: set DI_QUIETINSTALL flag for GUI-less device management
Signed-off-by: Simon Rozman <[email protected]>
2019-06-04 14:45:23 +02:00
Simon Rozman 014f736480 setupapi: define PropChangeParams struct
This structure is required for calling DIF_PROPERTYCHANGE installer
class.

Signed-off-by: Simon Rozman <[email protected]>
2019-06-04 14:45:23 +02:00
Simon Rozman 6a0a3a5406 wintun: revise GetInterface()
- Make foreign interface found error numeric to ease condition
  detection.
- Update GetInterface() documentation.
- Make tun.CreateTUN() quit when foreign interface found before
  attempting to create a Wintun interface with a duplicate name.
  Creation is futile.

Signed-off-by: Simon Rozman <[email protected]>
2019-05-24 09:29:57 +02:00
Simon Rozman dc9bbec9db setupapi: trim "Get" from getters
Signed-off-by: Simon Rozman <[email protected]>
2019-05-22 19:31:52 +02:00
Simon Rozman 7d5f5bcc0d wintun: change acronyms to uppercase
Signed-off-by: Simon Rozman <[email protected]>
2019-05-17 10:22:34 +02:00
Simon Rozman 6c1b66802f wintun: registry: revise value reading
- Make getStringValueRetry() reusable for reading any value type. This
  merges code from GetIntegerValueWait().
- expandString() >> toString() and extend to support REG_MULTI_SZ
  (to return first value of REG_MULTI_SZ). Furthermore, doing our own
  UTF-16 to UTF-8 conversion works around a bug in windows/registry's
  GetStringValue() non-zero terminated string handling.
- Provide toInteger() analogous to toString()
- GetStringValueWait() tolerates and reads REG_MULTI_SZ too now. It
  returns REG_MULTI_SZ[0], making GetFirstStringValueWait() redundant.

Signed-off-by: Simon Rozman <[email protected]>
2019-05-11 17:14:37 +02:00
Simon Rozman 1c92b48415 wintun: registry: replace REG_NOTIFY with NOTIFY
Signed-off-by: Simon Rozman <[email protected]>
2019-05-10 18:09:20 +02:00
Simon Rozman 7e962a9932 wintun: wait for interface registry key on device creation
By using RegNotifyChangeKeyValue(). Also disable dead gateway detection.

Signed-off-by: Simon Rozman <[email protected]>
2019-05-10 16:43:58 +02:00
Simon Rozman dcb8f1aa6b wintun: fix GUID leading zero padding
Signed-off-by: Simon Rozman <[email protected]>
2019-05-09 12:16:21 +02:00
Simon Rozman f1dc167901 setupapi: Fix struct size mismatches
Signed-off-by: Simon Rozman <[email protected]>
2019-04-19 10:08:11 +02:00
Simon Rozman 5811447b38 setupapi: Revise DrvInfoDetailData struct size calculation
Go adds trailing padding to DrvInfoDetailData struct in GOARCH=386 which
confuses SetupAPI expecting exactly sizeof(SP_DRVINFO_DETAIL_DATA).

Signed-off-by: Simon Rozman <[email protected]>
2019-04-18 10:39:22 +02:00
Simon Rozman ef5f3ad80a tun: windows: Adopt new error codes returned by Wintun
Signed-off-by: Simon Rozman <[email protected]>
2019-04-11 19:38:11 +02:00
Simon Rozman a291fdd746 tun: windows: do not sleep after OPERATION_ABORTED on write
Signed-off-by: Simon Rozman <[email protected]>
2019-04-11 19:37:04 +02:00
Simon Rozman 767c86f8cb tun: windows: Retry R/W on ERROR_OPERATION_ABORTED
Signed-off-by: Simon Rozman <[email protected]>
2019-04-04 09:20:18 +02:00
Simon Rozman 421c1f9143 tun: windows: Attempt to reopen handle on all errors
Signed-off-by: Simon Rozman <[email protected]>
2019-04-03 05:41:38 +02:00
Simon Rozman 2faf2dcf90 tun: windows: Make adapter rename asynchronous
Signed-off-by: Simon Rozman <[email protected]>
2019-03-22 16:36:30 +01:00
Simon Rozman 41c30a7279 tun: windows: Adapter devices renamed to WINTUN<LUID Index>
Signed-off-by: Simon Rozman <[email protected]>
2019-03-22 15:29:14 +01:00
Simon Rozman 4b1db1d39b tun: windows: Increase unavailable adapter timeout to 30sec
5 seconds was too short when debugging.

Signed-off-by: Simon Rozman <[email protected]>
2019-03-22 13:52:51 +01:00
Simon Rozman a80db5e65e tun: windows: Make writing persistent too
Signed-off-by: Simon Rozman <[email protected]>
2019-03-22 13:52:51 +01:00
Simon Rozman 9748a52073 tun: windows: Fix paused adapter test
Signed-off-by: Simon Rozman <[email protected]>
2019-03-22 13:52:51 +01:00
Simon Rozman 91b4e909bb wintun: Use native Win32 API for I/O
Signed-off-by: Simon Rozman <[email protected]>
2019-03-21 00:56:45 +01:00
Simon Rozman c2a2b8d739 wintun: Make errors more descriptive
Signed-off-by: Simon Rozman <[email protected]>
2019-03-08 10:03:57 +01:00
Simon Rozman 70449f1a97 wintun: Return correct reboot-req flag on CreateInterface() error too
Signed-off-by: Simon Rozman <[email protected]>
2019-03-08 10:03:57 +01:00
Simon Rozman 33c3528430 wintun: Fix double-quoted strings escaping on output
Signed-off-by: Simon Rozman <[email protected]>
2019-03-08 10:03:57 +01:00
Simon Rozman 30ab07e354 wintun: Introduce SetupAPI enumerator and machineName consts
Signed-off-by: Simon Rozman <[email protected]>
2019-03-08 10:03:57 +01:00
Simon Rozman 368dea72fe wintun: Cleanup
Signed-off-by: Simon Rozman <[email protected]>
2019-03-07 21:12:20 +01:00
Simon Rozman 9b22255cad wintun: Refactor network registry key name generation
Signed-off-by: Simon Rozman <[email protected]>
2019-03-07 21:12:20 +01:00
Simon Rozman 11f5780250 wintun: Revise interface creation wait
DIF_INSTALLDEVICE returns almost immediately, while the device
installation continues in the background. It might take a while, before
all registry keys and values are populated.

Previously, wireguard-go waited for HKLM\SYSTEM\CurrentControlSet\
Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\<id> registry key
only.

Followed by a SetInterfaceName() method of Wintun struct which tried to
access HKLM\SYSTEM\CurrentControlSet\Control\Network\
{4D36E972-E325-11CE-BFC1-08002BE10318}\<id>\Connection registry key
might not be available yet.

This commit loops until both registry keys are available before
returning from CreateInterface() function.

Signed-off-by: Simon Rozman <[email protected]>
2019-03-07 21:12:20 +01:00
Simon Rozman 1fdf7b19a3 wintun: Resolve some of golint warnings
Signed-off-by: Simon Rozman <[email protected]>
2019-03-04 16:37:11 +01:00
Simon Rozman a1aabb21ae Elaborate the failing step when forwarding errors on return
Signed-off-by: Simon Rozman <[email protected]>
2019-03-04 16:37:11 +01:00
Simon Rozman 9041d38e2d Simplify reading NetCfgInstanceId from registry
As querying non-existing registry value and reading non-existing
registry string value both return ERROR_FILE_NOT_FOUND, we can
use later only.

Signed-off-by: Simon Rozman <[email protected]>
2019-03-04 16:37:11 +01:00
Simon Rozman cddfd9a0d8 Unify interface-specific network registry key open
Signed-off-by: Simon Rozman <[email protected]>
2019-03-04 16:37:11 +01:00
Simon Rozman d002eff155 wintun: Read/write packet size from/to exchange buffer directly
Driver <-> user-space communication is local and using native endian.

Signed-off-by: Simon Rozman <[email protected]>
2019-02-22 16:16:14 +01:00
Simon Rozman e06a8f8f9f wintun: Make two-step slicing a one step
Stop relying to Go compiler optimizations and calculate the end offset
directly.

Signed-off-by: Simon Rozman <[email protected]>
2019-02-22 16:11:33 +01:00
Simon Rozman ac4944a708 wintun: Write exchange buffer increased back to 1MiB
Signed-off-by: Simon Rozman <[email protected]>
2019-02-20 20:13:33 +01:00
Simon Rozman 2491f9d454 wintun: Migrate from unsafe buffer handling to encoding/binary
Signed-off-by: Simon Rozman <[email protected]>
2019-02-20 20:10:24 +01:00
Simon Rozman 8091c6474a wintun: Adopt new packet data alignment
Signed-off-by: Simon Rozman <[email protected]>
2019-02-20 19:56:10 +01:00
Simon Rozman 040da43889 wintun: Cleanup
Signed-off-by: Simon Rozman <[email protected]>
2019-02-20 18:38:18 +01:00
Simon Rozman b7025b5627 wintun: Add TUN device locking
In case reading from TUN device detected TUN device was closed, it
closed the file handle and set tunFile to nil. The tunFile is
automatically reopened on retry, but... If another packet comes in the
WireGuard calls Write() method. With tunFile set to nil, this will
cause access violation.

Therefore, locking was introduced.

Signed-off-by: Simon Rozman <[email protected]>
2019-02-20 13:12:08 +01:00
Simon Rozman 6581cfb885 wintun: Move exchange buffer in separate struct on heap
This allows buffer alignment and keeps it together with its meta-data.

Furthermore, the write buffer has been reduced - as long as we flush
after _every_ write, we don't need a 1MiB write buffer.

Signed-off-by: Simon Rozman <[email protected]>
2019-02-20 11:41:37 +01:00
Simon Rozman 4863089120 wintun: Switch to dynamic packet sizes
Signed-off-by: Simon Rozman <[email protected]>
2019-02-19 18:50:42 +01:00
Simon Rozman b719a09a26 wintun: Auto-calculate TUN exchange buffer size
Signed-off-by: Simon Rozman <[email protected]>
2019-02-08 15:21:24 +01:00
Simon Rozman f05f52637f wintun: Simplify Read method()
Signed-off-by: Simon Rozman <[email protected]>
2019-02-08 14:31:05 +01:00
Simon Rozman 713477cfb1 wintun: Make constants private and adopt Go recommended case
Signed-off-by: Simon Rozman <[email protected]>
2019-02-08 08:55:23 +01:00
Simon Rozman 5981d5cacf wintun: Check for user close in read loop regardless the load
Do the WaitForSingleObject() always to provide high-load responsiveness.

Reorder events so TUN_SIGNAL_CLOSE has priority over
TUN_SIGNAL_DATA_AVAIL, to provide high-load responsiveness at all.

Signed-off-by: Simon Rozman <[email protected]>
2019-02-08 08:48:35 +01:00
Simon Rozman b13739ada2 wintun: Adjust tunRWQueue.left member to match Wintun driver
Signed-off-by: Simon Rozman <[email protected]>
2019-02-08 07:32:12 +01:00
Simon Rozman c4988999ac setupapi: Merge _SP_DRVINFO_DETAIL_DATA and DrvInfoDetailData
Signed-off-by: Simon Rozman <[email protected]>
2019-02-07 23:50:43 +01:00
Simon Rozman b662896cf4 setupapi: Merge SP_DRVINFO_DATA and DrvInfoData
Signed-off-by: Simon Rozman <[email protected]>
2019-02-07 23:50:43 +01:00
Simon Rozman 0525f6b112 setupapi: Rename SP_REMOVEDEVICE_PARAMS to RemoveDeviceParams
Signed-off-by: Simon Rozman <[email protected]>
2019-02-07 23:50:43 +01:00
Simon Rozman 9d830826c5 setupapi: Rename SP_CLASSINSTALL_HEADER to ClassInstallHeader
Signed-off-by: Simon Rozman <[email protected]>
2019-02-07 23:50:43 +01:00
Simon Rozman bd963497da setupapi: Merge _SP_DEVINSTALL_PARAMS and DevInstallParams
Signed-off-by: Simon Rozman <[email protected]>
2019-02-07 23:50:30 +01:00
Simon Rozman 05d25fd1b7 setupapi: Merge _SP_DEVINFO_LIST_DETAIL_DATA and DevInfoListDetailData
Signed-off-by: Simon Rozman <[email protected]>
2019-02-07 23:49:50 +01:00
Simon Rozman 6d2729dccc setupapi: Rename SP_DEVINFO_DATA to DevInfoData
Signed-off-by: Simon Rozman <[email protected]>
2019-02-07 22:43:02 +01:00
Simon Rozman d87cbeeb2f wintun: Detect if a foreign interface with the same name exists
Signed-off-by: Simon Rozman <[email protected]>
2019-02-07 22:02:51 +01:00
Simon Rozman 043b7e8013 wintun: Clean excessive setupapi.DevInfo.GetDeviceInfoListDetail() call
Signed-off-by: Simon Rozman <[email protected]>
2019-02-07 20:49:41 +01:00
Simon Rozman ef48d4fa95 wintun: Explain rationale behind case-insensitive interface names
Signed-off-by: Simon Rozman <[email protected]>
2019-02-07 19:42:59 +01:00
Simon Rozman f7276ed522 wintun: Implement TODO in TestSetupDiGetDeviceRegistryProperty()
Signed-off-by: Simon Rozman <[email protected]>
2019-02-07 18:59:34 +01:00
Simon Rozman cb2bc4b34c tun_windows: Introduce preliminary TUN interface creation
Signed-off-by: Simon Rozman <[email protected]>
2019-02-06 22:30:14 +01:00
Simon Rozman 46279ad0f9 tun_windows: Stop checking minimum size of received TUN packets
Signed-off-by: Simon Rozman <[email protected]>
2019-02-06 20:22:04 +01:00
Simon Rozman 73df1c0871 setupapi: Add DrvInfoDetailData.IsCompatible() to simplify HID detection
Signed-off-by: Simon Rozman <[email protected]>
2019-02-06 20:18:44 +01:00
Simon Rozman 069016bbc4 setupapi: Add SP_DRVINFO_DATA.IsNewer() method to simplify comparison
Signed-off-by: Simon Rozman <[email protected]>
2019-02-06 20:17:47 +01:00
Simon Rozman 3c29434a79 setupapi: Make toUTF16() public and add UTF16ToBuf() counterpart
Signed-off-by: Simon Rozman <[email protected]>
2019-02-06 20:15:40 +01:00
Simon Rozman 3e8f2e3fa5 setupapi: Add support for driver info lists
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 16:29:17 +01:00
Simon Rozman 7b636380e5 setupapi: Move Go<>Windows struct marshaling to types_windows.go
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 14:03:28 +01:00
Simon Rozman 99a3b628e9 setupapi: Add support for SetupDi(Get|Set)DeviceRegistryProperty()
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman e7ffce0d21 setupapi: Introduce DevInfo methods for cleaner code
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman 35f72239ac Add support for setupapi.SetupDi(Get|Set)SelectedDevice()
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman c15cbefc12 Reorder data-types and functions to match SetupAPI.h
Adding functions with non-consistent order made setupapi package a mess.
While we could reorder data-types and functions by alphabet - it would
make searching easier - it would put ...Get... and ...Set... functions
quite apart.

Therefore, the SetupAPI.h order was adopted.

Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman dd998ca86a Add support for setupapi.SetupDiCreateDeviceInfo()
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman 024a4916c2 Add support for setupapi.setupDiCreateDeviceInfoListEx()
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman 963be8e993 Stop accessing SetupDiGetDeviceInfoListDetail() output on error
The data returned by SetupDiGetDeviceInfoListDetail() is nil on error
which will cause the test to crash should the function fail.

Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman e821cdabd2 Unify certain variable names
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman 38c7acd70f Simplify SetupDiEnumDeviceInfo() synopsis
The SetupDiEnumDeviceInfo() now returns a SP_DEVINFO_DATA rather than
taking it on input to fill it on return.

Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman 20f1512b7c Change generic local variable names with meaningful replacements
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman 348b4e9f7c Add support for setupapi.SetupDiClassGuidsFromNameEx()
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman f81882ee8b Clean an unused constant
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman 3e0e61dd26 Replace SetupDiClassNameFromGuid() with SetupDiClassNameFromGuidEx()
The former is only a subset of the later. To minimize future
maintenance, we'll provide support for extended version only.

Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman 9635a0b3a6 Add support for setupapi.SetupDiClassNameFromGuid()
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman 90b6938ca0 Stop checking for valid handle in DevInfo.Close()
User should not have called or deferred the Close() method should
SetupDiGetClassDevsEx() return an error (and invalid handle). And even
if user does that, a SetupDiDestroyDeviceInfoList(INVALID_HANDLE_VALUE)
is harmless. It just returns ERROR_INVALID_HANDLE - we have a unit test
for this in TestSetupDiDestroyDeviceInfoList().

Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman 269944002f Add support for setupapi.SetupDiCallClassInstaller()
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman a5a1ece32f Add support for setupapi.SetupDi(Get|Set)ClassInstallParams()
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman f1d5db6547 Add support for setupapi.SetupDi(Get|Set)DeviceInstallParams()
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman dce5192d86 Add support for setupapi.SetupDiOpenDevRegKey()
Furthermore setupapi.DevInfoData has been obsoleted.
SetupDiEnumDeviceInfo() fills existing SP_DEVINFO_DATA structure now.
As other functions of SetupAPI use SP_DEVINFO_DATA, converting it to
DevInfoData and back would hurt performance.

Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman 955d8dfe04 Add support for setupapi.SetupDiEnumDeviceInfo()
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00
Simon Rozman 25e18d01e6 Update exported types and functions annotations
Signed-off-by: Simon Rozman <[email protected]>
2019-02-05 12:59:42 +01:00