Commit Graph

177 Commits

Author SHA1 Message Date
Jason A. Donenfeld
a332f54a1b api: only sleep after force closing handles if required
Also force close handles when deleting the adapter, in case the function
is called from another process, for example an uninstaller.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-31 19:11:49 +01:00
Simon Rozman
9b61d0d851 Accept WoW64 clients
Signed-off-by: Simon Rozman <simon@rozman.si>
2020-10-30 16:50:58 +01:00
Shawn Hoffman
9c39b2b567 Use standard volatile semantics
Make all archs are use the standardized concept of volatile.
This patch will cause the most changes to arm64 codegen.

Signed-off-by: Shawn Hoffman <godisgovernment@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-30 17:08:10 +01:00
Jason A. Donenfeld
b19c7abd41 Fix potential TunDispatchSecurityDescriptor leak
TunDispatchSecurityDescriptor will leak if RtlAbsoluteToSelfRelativeSD
fails. Add cleanup in error path.

Reported-by: Shawn Hoffman <godisgovernment@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-30 17:04:34 +01:00
Shawn Hoffman
cb1f62156e Use RtlSubAuthoritySid instead of directly poking SID
Signed-off-by: Shawn Hoffman <godisgovernment@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2020-10-30 17:00:20 +01:00
Jason A. Donenfeld
5f681022c9 Invert skipPacket condition so code matches
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-12-10 14:39:06 +01:00
Jason A. Donenfeld
96553f4217 Increment discarded packets properly
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-12-10 14:38:59 +01:00
Simon Rozman
6cc9786b47 Process send NBLs in batches
When using packet forwarding on Windows computer, adjacent NBLs may
represent packet fragments. Those NBLs must not be completed separately,
but in a single NdisMSendNetBufferListsComplete() call.

This fixes a bugcheck on Windows Server with RRAS role and IP forwarding
packets to Wintun adapter.

Signed-off-by: Simon Rozman <simon@rozman.si>
2019-12-10 14:22:50 +01:00
Simon Rozman
c4becc5b90 Remove excessive ASSERT
Signed-off-by: Simon Rozman <simon@rozman.si>
2019-12-10 14:22:50 +01:00
Jason A. Donenfeld
fd52a3a429 Ensure that buffers are unmapped on process exit and adapter deletion
Before duplicating a handle elsewhere and closing the original process
would result in disaster. Also, it turns out that TunHaltEx can be
called before the handles are all closed, so we need to unregister prior
to freeing the ctx, lest disaster occurs. Finally, now that we have a
few different things happening with registration and deregistration, we
serialize all accesses with an eresource, a bit heavy-weight but
sufficient.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-10-06 12:37:51 +00:00
Jason A. Donenfeld
5b872e8cf9 Use performance counter for less spinning
Previously we had to spin for a minimum of 15ms because the tick
interval is 156250 on NT. On linux, usually trips to the high
performance timers are discouraged because if they don't hit the RDTSC
path (due to being unstable or the like), they hit more expensive
hardware. I assume that's probably the same on NT, but all of tcpip.sys
and ndis.sys uses the performance counters too, so what are we going to
do?

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-08-04 11:13:39 +00:00
Jason A. Donenfeld
85a8076d08 Spin less and more efficiently
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-08-04 07:06:50 +00:00
Simon Rozman
cd6fe285b4 Cleanup NBL reference counting
The Empty event state is now set according to
Ctx->Device.Receive.ActiveNbls.Head != NULL. But, we still have to clear
the Empty event inside the TransitionLock to prevent race with
TunPause().

Signed-off-by: Simon Rozman <simon@rozman.si>
2019-08-02 12:25:22 +02:00
Simon Rozman
408665270f Skip packet on NBL allocation failure properly
Should NBL allocation persist to fail, the receive ring could eventually
fill up as there will be no TunReturnNetBufferLists() calls to advance
its head.

Signed-off-by: Simon Rozman <simon@rozman.si>
2019-08-02 12:04:02 +02:00
Simon Rozman
799413a776 Use reference counter and KEVENT instead of remove locks
Driver verifier doesn't like re-initializing remove locks.

Signed-off-by: Simon Rozman <simon@rozman.si>
2019-08-02 09:43:32 +00:00
Jason A. Donenfeld
9e9f1ac9b3 Rearrange comment to make clang-format happy
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-08-02 09:43:32 +00:00
Jason A. Donenfeld
a2d46f3c00 Disable APC when taking rwlock
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-08-02 09:43:32 +00:00
Simon Rozman
afdbc75bf9 Resolve SDV reported "defect"
NdisMGetDeviceProperty() should always return non-NULL
FunctionalDeviceObject according to _Outptr_opt_. An explicit
FunctionalDeviceObject NULL check has been added to keep the SDV happy
and not calling our driver "defective".

Signed-off-by: Simon Rozman <simon@rozman.si>
2019-08-02 09:43:32 +00:00
Jason A. Donenfeld
28ba2d4600 Separate out atomic helpers
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-08-02 09:43:32 +00:00
Jason A. Donenfeld
22e2da002d Rewrite installer logic in C
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-08-02 09:43:32 +00:00
Jason A. Donenfeld
18cfd522aa Use explicit running boolean and use set instead of exchange
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-31 13:59:54 +00:00
Simon Rozman
999a6744db Assert that adapter is always running when client is registering rings
By attaching to NDIS device instead of creating our own device for I/O,
the adapter is always running before client is able to connect and
register rings. NDIS also won't allow adapter to pause with connected
clients.

Signed-off-by: Simon Rozman <simon@rozman.si>
2019-07-31 13:59:54 +00:00
Simon Rozman
7f49a66494 Cleanup TUN_FLAGS_PRESENT
With no PnP notifications and custom surprise removal code we do not
need the TUN_FLAGS_PRESENT any more. The traffic is stopped when handle
is closed or adapter is somehow paused. Though by reusing the NDIS device
for our I/O, the adapter will not be able to pause with a client
connected.

Signed-off-by: Simon Rozman <simon@rozman.si>
2019-07-31 13:59:54 +00:00
Jason A. Donenfeld
5239d53db0 Add handle closing ioctl
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-31 13:59:54 +00:00
Simon Rozman
0d9b9b925a Reinitialize active NBL remove lock to allow adapter reuse
Before the TunProcessReceiveData() thread terminates or adapter is
paused, we wait for all receive NBLs to be returned. Unfortunately,
IoReleaseRemoveLockAndWait() leaves the remove lock in non reusable
state.

To be able to start receiving packets on existing adapter again, we
(re)initialize the remove lock on ring registration or adapter resume.
The former addresses TunProcessReceiveData()'s
IoReleaseRemoveLockAndWait() call, the later addresses the TunPause()'s.

Signed-off-by: Simon Rozman <simon@rozman.si>
2019-07-31 13:59:46 +00:00
Jason A. Donenfeld
f48b0b2883 Compile on 32-bit and arm64
No popcnt intrinsic on arm, no PopulationCount64 function on 32bit.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-19 10:28:02 +00:00
Jason A. Donenfeld
970e22d8e4 Use more specific IOCTL code
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-19 06:31:26 +00:00
Jason A. Donenfeld
6ebdbf77d2 Require the usual SDDL_DEVOBJ_SYS_ALL permissions
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-18 19:35:41 +00:00
Simon Rozman
14e5532dc8 Make receiving NBLs asynchronous
This commit moves NBL post-processing (moving ring head, releasing NBL)
to MINIPORT_RETURN_NET_BUFFER_LISTS handler.

Signed-off-by: Simon Rozman <simon@rozman.si>
2019-07-18 15:27:18 +02:00
Simon Rozman
f5eadb50c4 Minimize TransitionLock when receiving packets
We do not need to share-lock the TransitionLock for the whole life of
receiver thread.

Signed-off-by: Simon Rozman <simon@rozman.si>
2019-07-18 14:53:38 +02:00
Jason A. Donenfeld
9bf25d873b Fix insane coding style
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-18 12:49:32 +00:00
Jason A. Donenfeld
cdf0d0bf9a Fix awkward comment style
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-18 12:45:34 +00:00
Jason A. Donenfeld
5502b77218 Do not hijack PNP notifier
We no longer pend send-side NBLs, so we don't have a real use for this.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-18 12:41:04 +00:00
Jason A. Donenfeld
aac642e3dc Switch to paged dispatch handlers
We're not totally sure this is kosher, unfortunately.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-18 11:28:25 +00:00
Simon Rozman
043abc0a8e Minimize TransitionLock when sending packets
We do not need to share-lock the TransitionLock for complete NBL chain.
This commit should improve better state transition response, thou until
NDIS is sending a single NBL per MINIPORT_SEND_NET_BUFFER_LISTS call,
this should not have a considerable effect.

Since the skibNbl: call of NdisMSendNetBufferListsComplete() is made
inside the TransactionLock at dispatch IRQL, a dispatch IRQL hint was
added to the NdisMSendNetBufferListsComplete() call.

Signed-off-by: Simon Rozman <simon@rozman.si>
2019-07-18 13:52:16 +02:00
Jason A. Donenfeld
58ce3c5000 Piggy-back on top of NDIS' device object instead of adding our own
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-18 10:34:38 +00:00
Simon Rozman
82ede2f96d Replace TUN_FLAGS_CONNECTED by an event
On client closing the handle there is a potential race if somebody
resets the TailMoved event of receive ring at the right time. Rather
than rely on TailMoved event, we introduce our own Disconnected
notification event and have the receive thread wait on both. The
Disconnected  event is also usable as TUN_FLAGS_CONNECTED substitute.

Signed-off-by: Simon Rozman <simon@rozman.si>
2019-07-17 14:53:09 +02:00
Simon Rozman
e7fad38a07 Improve lock retention when sending
NDIS may call MINIPORT_SEND_NET_BUFFER_LISTS from parallel threads to
queue as many packets as fast as possible. Initial implementation of
ring buffers used a spin lock to completely serialize sending packets
making it sub-optimal and burning large amount of CPU.

This commit uses locked section to allocate space for packet(s) in the
ring. It copies the packets unlocked, then it locks again to adjust the
ring tail.

Signed-off-by: Simon Rozman <simon@rozman.si>
2019-07-17 12:22:48 +02:00
Jason A. Donenfeld
1914547ab3 Spin for a bit before falling back to event object
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-17 10:17:46 +00:00
Simon Rozman
66e51bd08f Switch to ring buffers for exchanging packets
This demonstrates the use of ring buffers in its simplest, purest form.
No performance optimizations were made.

Signed-off-by: Simon Rozman <simon@rozman.si>
2019-07-16 20:06:54 +00:00
Simon Rozman
39939e3550 Rename remaining status -> Status and ctx -> Ctx
Signed-off-by: Simon Rozman <simon@rozman.si>
2019-07-08 11:10:43 +02:00
Jason A. Donenfeld
fb738767a0 Remove TunMapIrp
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-05 16:29:00 +00:00
Jason A. Donenfeld
17a0e2647a Simplify ActiveNblCount decrement
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-05 16:29:00 +00:00
Odd Stranne
50aa1f924c Rename NBL -> Nbl
Signed-off-by: Odd Stranne <odd@mullvad.net>
2019-07-05 16:28:59 +00:00
Odd Stranne
d268f03bc1 Rename labels to be more consistent
Signed-off-by: Odd Stranne <odd@mullvad.net>
2019-07-05 16:28:59 +00:00
Odd Stranne
75e434e3b8 Make error branching more compact
Signed-off-by: Odd Stranne <odd@mullvad.net>
2019-07-05 16:28:59 +00:00
Jason A. Donenfeld
20586e37b2 Treat ReferenceCount as an atomic
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-05 13:56:57 +00:00
Jason A. Donenfeld
6fe055d0e8 Cleanup atomic getters
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-05 13:45:39 +00:00
Jason A. Donenfeld
ee3254e46f Return with proper error status for bad address
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-05 08:49:07 +00:00
Jason A. Donenfeld
ef12ddb2be Do not take extra ActiveNBL when holding TransitionLock
The transition lock ensures that TunPause won't drop its last reference
until the shared transition lock is dropped.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2019-07-05 07:54:40 +00:00