Commit Graph

177 Commits

Author SHA1 Message Date
Simon Rozman
bc5cde8916 api: upgrade logging
Log runtime information to quickly check whether the values are sane
when analyzing error logs sent in by users.

Signed-off-by: Simon Rozman <simon@rozman.si>
2021-02-16 04:19:21 +01:00
Simon Rozman
542a281808 api: tighten function parameter code analysis annotations
Signed-off-by: Simon Rozman <simon@rozman.si>
2021-02-04 19:00:17 +01:00
Simon Rozman
41b77964ca api: truncate long log lines with …
Signed-off-by: Simon Rozman <simon@rozman.si>
2021-02-04 18:39:10 +01:00
Simon Rozman
7a6bab7f04 api: unify NetCfgInstanceId registry retrieval
Signed-off-by: Simon Rozman <simon@rozman.si>
2021-02-04 18:39:10 +01:00
Jason A. Donenfeld
fb416747dc api: ensure that device object exists before returning from open/create
Some users are seeing errors like this after rebooting from Windows
Update:

2021-01-28 18:39:45.220197: [TUN] Creating Wintun interface
2021-01-28 18:39:49.420116: [TUN] [Wintun] CreateAdapter: Creating adapter
2021-01-28 18:39:53.704007: [TUN] [Wintun] OpenDeviceObject: Failed to connect to adapter: The system cannot find the path specified. (Code 0x00000003)
2021-01-28 18:39:53.704007: [TUN] [Wintun] WintunStartSession: Failed to open adapter device object
2021-01-28 18:39:54.097037: [TUN] Unable to create Wintun interface: Error starting session: The system cannot find the path specified.

It appears that creation of the device object file might happen
asynchronously, so this commit polls for it.

Reported-by: Artem Kuroptev <artem@kuroptev.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-02-03 22:36:41 +01:00
Jason A. Donenfeld
2628412d71 global: bump copyright
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-01-30 16:45:26 +01:00
Jason A. Donenfeld
49b7692d5b api: use custom devpkey for pool
It seems like the friendly name is still getting reset sometimes. Rather
than swimming upstream, it turns out we can just use a custom devpkey
that, according to msdn, is respected.

https://docs.microsoft.com/en-us/windows-hardware/drivers/install/creating-custom-device-properties

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2021-01-30 16:41:16 +01:00
Jason A. Donenfeld
7710ff187b api: close private namespace when unloading DLL
Prior, people making calls to LoadLibrary/FreeLibrary would experience
a failure to create or open adapters, because the private namespace was
already loaded into the process and not cleaned up on DLL detachment.
While this pattern is probably a misuse of the library, we should still
support cleaning that up. This commit makes the right calls to free the
boundary descriptor and close the private namespace. It does not,
however, destroy the private namespace using the flag on the second
parameter, in case of races with other processes.

Reported-by: Brad Spencer <bspencer@blackberry.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-12-17 19:00:25 +01:00
Jason A. Donenfeld
a3a1fa8931 api: delay load remaining dlls to work around forwarder gotchas
RtlGenRandom forwards to cryptbase.dll, which is not in KnownDlls.
Therefore it's not a good idea to link to advapi32.dll at link time. How
many other gotchas of unusual forwarded functions are there? I don't
really want to find out. Therefore, delay load everything else.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-27 14:52:03 +01:00
Simon Rozman
ecf70261da api: skip notifying driver when there are no receive packets yet
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-27 14:52:03 +01:00
Jason A. Donenfeld
6af568d1e0 api: allow header to be mit
This doesn't change much, but it does make it mildly more convenient
plop this into mixed-use codebases.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-27 14:52:01 +01:00
Jason A. Donenfeld
a9f8b2b764 api: do not make dead gateway detection failures fatal
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-17 16:27:11 +01:00
Simon Rozman
f7c2ea4ded api: retry on ERROR_TRANSACTION_NOT_ACTIVE when disabling dead GW detect
There seems to be a race in the TCP/IP adapter registry key. Sometimes,
the adapter TCP/IP key is created, but setting the value
EnableDeadGWDetect fails with ERROR_TRANSACTION_NOT_ACTIVE.

Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-17 15:30:00 +01:00
Jason A. Donenfeld
c18dd0a216 api: remove useless line
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-17 12:46:53 +01:00
Jason A. Donenfeld
e73613ee24 api: remove return value from logger function
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-10 22:06:25 +01:00
Jason A. Donenfeld
fc348f519b api: relax IsPoolMember estimation
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-09 15:34:38 +01:00
Jason A. Donenfeld
0d214d7254 api: do not call UpdateDriverForPlugAndPlayDevicesW
This seems to reset a number of device properties, and our update flow
seems to update old adapters without needing to call this.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-09 15:14:33 +01:00
Jason A. Donenfeld
8ec14e5f6d api: take pool mutex when deleting
This prevents us from racing with driver deletion. Mutexes are
recursive, so we shouldn't deadlock if called from Enum.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-09 13:15:57 +01:00
Jason A. Donenfeld
7c5233a80e api: account for adapter disappearing during deletion
This makes the race less fatal.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-09 13:11:07 +01:00
Jason A. Donenfeld
3dbaafd4ae api: manipulate process token if thread token didn't require impersonation
Otherwise rundll32.exe fails if we're already SYSTEM.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-07 15:04:33 +01:00
Simon Rozman
c581a9f6cd api: fix Function and Prefix logging order
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-07 15:04:33 +01:00
Simon Rozman
b0ebb6d0ad api: fix LastError override
The LastError was overridden by the stdout reader thread exit code
masking the true reason why ExecuteRunDll32() failed and even worse: as
the thread exited gracefully, the true reason was overridden by
ERROR_SUCCESS and returning TRUE (success).

Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-07 15:04:33 +01:00
Jason A. Donenfeld
ed0465b8a7 api: avoid loading version.dll if not used
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-06 10:25:15 +01:00
Jason A. Donenfeld
c891e84259 api: include arm64 in amd64
ARM64 will still run AMD64 apps.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-05 16:58:43 +01:00
Jason A. Donenfeld
9f3d466791 api: remove WintunOpenAdapterDeviceObject
Discourage use of kernel interface, which gives us more flexibility if
we ever want to change it.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-05 16:58:43 +01:00
Jason A. Donenfeld
82c41bdb4b api: rearrange wintun.h to have better grouping and improve docs
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-05 16:58:43 +01:00
Jason A. Donenfeld
1285b8f528 api: rename WintunGetAdapter to WintunOpenAdapter
"Create" and "Open" natural names for these.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-05 16:58:43 +01:00
Jason A. Donenfeld
e9e790605a api: rename ReceiveRelease to ReleaseReceivePacket
This makes the API parallel:
Wintun*Allocate*SendPacket -> WintunSendPacket
WintunReceivePacket -> Wintun*Release*ReceivePacket

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-05 16:58:43 +01:00
Jason A. Donenfeld
8861fdc316 api: add cfgmgr32.dll to delayed load list
It's in the registry but not in the NT object key.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-05 16:58:43 +01:00
Simon Rozman
abbf658211 api: fix typo in ring-management function prototype declarations
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-05 08:36:28 +01:00
Jason A. Donenfeld
3158b553c4 api: document enum argument properly
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 21:22:47 +01:00
Jason A. Donenfeld
fc27d8ccd2 api: document log enum
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 21:22:47 +01:00
Jason A. Donenfeld
65dee05229 api: remove enum name for logger level
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 21:22:47 +01:00
Jason A. Donenfeld
2b4e164d69 api: document adapter handle return value properly
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 16:19:53 +01:00
Jason A. Donenfeld
c26b16e06f api: CALLBACK_FUNC -> CALLBACK
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 16:46:24 +01:00
Simon Rozman
b8401ad25b api: upgrade nci.lib and wintun-inf.h building
The additional build steps performed are now attached to the build
process using BeforeTargets/AfterTargets.

Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-04 16:44:01 +01:00
Jason A. Donenfeld
dc99f96da7 api: rename GetVersion to GetRunningDriverVersion
This makes our intentions a lot more clear, and in case we ever add
other version functions, makes the forward path simpler.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 14:53:39 +01:00
Jason A. Donenfeld
5d1efa847f api: use a logging alloc function
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 13:21:43 +01:00
Jason A. Donenfeld
9c349273f5 api: concatenate function name at runtime
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-04 13:21:43 +01:00
Simon Rozman
0a51e26730 api: include the rundll32 helpers the MSVC-typical way
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-04 13:21:42 +01:00
Simon Rozman
552821f59a api: translate NTSTATUS to Win32 error codes
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-04 13:21:42 +01:00
Simon Rozman
f657e6fd27 api: use GetLastError() to report failures like standard Win32
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-11-04 13:21:42 +01:00
Jason A. Donenfeld
677ba8680f wintun: extract inf driverver at compile time into C header
This requires us to make some insane conversions between INF date,
JavaScript time, and finally Windows file time. The point is to mimic
SystemTimeToFileTime, which is what SpInf.dll's pSetupStringToDriverDate
does on the YYYY-MM-DD from the INF.

The result is that we no longer have to parse an ancient text format in
C at runtime.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 18:29:52 +01:00
Jason A. Donenfeld
55345ae386 api: move nci.lib generation to custom step
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 16:10:16 +01:00
Jason A. Donenfeld
90a33d9cc6 api: move undocumented ntdll symbols to ntdll.h
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 15:28:17 +01:00
Jason A. Donenfeld
080b6896e0 api: constify pool argument
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 12:47:26 +01:00
Jason A. Donenfeld
8963f7258e api: remove guid getter
The iphlpapi takes both LUIDs and GUIDs but prefers LUIDs, so exposing
the NET_LUID makes sense. However, we were previously exposing the
configuration GUID, rather than the net GUID, which is confusing, so
just make it all go away.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 12:34:19 +01:00
Jason A. Donenfeld
2af7fbd64a api: use 'open' name since caller must close handle
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 12:31:49 +01:00
Jason A. Donenfeld
7dede73406 api: add pool/driver removal for uninstaller semantics
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 12:27:42 +01:00
Jason A. Donenfeld
0c85a2ebf1 api: move _L macro where it belongs
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-11-03 11:42:48 +01:00