aboutsummaryrefslogtreecommitdiffstats
path: root/wintun.c (follow)
Commit message (Collapse)AuthorAgeFilesLines
* Create copies of NBLs to complete them fasterJason A. Donenfeld2019-07-041-3/+3
| | | | | | | | | It turns out that waiting for them to come back in the return function introduces extremely high latency. We need to eventually stop doing this and move to a proper ring buffer. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Reorder ReturnNBL and DispatchWriteJason A. Donenfeld2019-07-031-36/+36
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Receiving happens at dispatch level due to transition lockJason A. Donenfeld2019-07-031-12/+10
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Read userspace packet size once, before checksJason A. Donenfeld2019-07-031-8/+10
| | | | | | Otherwise userspace can twiddle things between checks. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Use NDIS variable style for localsJason A. Donenfeld2019-07-031-465/+471
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Use only multi-line commentsJason A. Donenfeld2019-07-031-37/+36
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Account for device removal before initializationJason A. Donenfeld2019-07-031-0/+2
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Use synchronize_rcu()-like semantics for exclusive transition lockJason A. Donenfeld2019-07-031-8/+12
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Fix up comment about replacement for ->ReservedJason A. Donenfeld2019-07-031-3/+6
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Simplify IRP processing after mappingJason A. Donenfeld2019-07-031-52/+13
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Allow buffer mapping to happen concurrentlyJason A. Donenfeld2019-07-031-17/+39
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Correct and simplify page lockingJason A. Donenfeld2019-07-031-22/+18
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Separate out MJ_CLOSEJason A. Donenfeld2019-07-031-27/+29
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Manually clean up ugly cornersJason A. Donenfeld2019-07-031-196/+197
|
* Improve designated initializersJason A. Donenfeld2019-07-031-74/+81
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Add NDIS team's clang-format conventionsJason A. Donenfeld2019-07-031-1323/+1465
| | | | | | | | This needs clang-format 9. This reveals a lot of other things we should clean up. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> Link: https://community.osr.com/discussion/291376/clang-format-and-driver-code
* Map user buffer only onceJason A. Donenfeld2019-07-031-32/+113
| | | | | | | This avoids needless page table modifications and also lets us enforce having writable pages. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Consider receive NBLs to be immutableJason A. Donenfeld2019-07-031-5/+2
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Avoid allocating second MDLJason A. Donenfeld2019-07-031-12/+2
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Make NDIS 6.80 compliantSimon Rozman2019-07-031-4/+20
| | | | | | | | | Implement support for synchronous OID requests and declare the Wintun as NDIS 6.80 compliant. https://docs.microsoft.com/en-us/windows-hardware/drivers/network/introduction-to-ndis-6-80 Signed-off-by: Simon Rozman <simon@rozman.si>
* Declare NDIS 6.70 compliantSimon Rozman2019-07-031-3/+6
| | | | | | | | | | | | | | | After confirming with Microsoft Documentation that Wintun is already NDIS 6.70 compliant, we declare it so. Furthermore, determine NDIS version bounds from NDISxxx_MINIPORT automatically. https://docs.microsoft.com/en-us/windows-hardware/drivers/network/introduction-to-ndis-6-40 https://docs.microsoft.com/en-us/windows-hardware/drivers/network/introduction-to-ndis-6-50 https://docs.microsoft.com/en-us/windows-hardware/drivers/network/introduction-to-ndis-6-60 https://docs.microsoft.com/en-us/windows-hardware/drivers/network/introduction-to-ndis-6-70 Signed-off-by: Simon Rozman <simon@rozman.si>
* Use ULONG instead of ptrdiff_t for length measurementJason A. Donenfeld2019-06-211-2/+2
| | | | | | | | Even though we're comparing this with a ptrdiff_t in one place and adding it to a void* in another place, it's still a length and as such should be a size_t, which I guess in our weird universe here is a ULONG. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Synchronize accesses to MiniportAdapterHandleJason A. Donenfeld2019-06-201-3/+4
| | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Adopt "Tun" namespace at global AdapterCount variableSimon Rozman2019-06-201-4/+4
| | | | Signed-off-by: Simon Rozman <simon@rozman.si>
* Fix NdisQueryMdl() NULL-buffer checkSimon Rozman2019-06-201-1/+2
| | | | Signed-off-by: Simon Rozman <simon@rozman.si>
* Save some valuable lessons learned on Windows internalsSimon Rozman2019-06-201-0/+3
| | | | | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> Signed-off-by: Simon Rozman <simon@rozman.si>
* Accept IRP_MJ_WRITE when paused but silently drop the packetsSimon Rozman2019-06-201-2/+7
| | | | Signed-off-by: Simon Rozman <simon@rozman.si>
* Support surprise removalSimon Rozman2019-06-201-1/+2
| | | | | | | | | | | | | | | | WHLK 1903 CHAOS tests are reporting lots of issues regarding surprise removal misbehavior: WDTF_PNP: INFO : Result: TestSurpriseRemove operation timed out waiting for IRP_MN_REMOVE_DEVICE.. WDTF_PNP: ERROR : Result: Failed to receive IRP_MN_REMOVE_DEVICE after receiving IRP_MN_SURPRISE_REMOVAL. Ensure that there are no open handles or references to the test device (in user mode or in kernel mode) preventing IRP_MN_REMOVE_DEVICE from being sent. You may need to terminate any processes or services that may have open user mode handles to this device. ( 80004005 ). Signed-off-by: Simon Rozman <simon@rozman.si>
* Replace PnP notifications with IRP_MJ_PNP dispatch handlerSimon Rozman2019-06-201-92/+34
| | | | | | | | | | | | By replacing the NDIS' IRP_MJ_PNP dispatch handler we get the first chance to clear the NBL queue to make NDIS proceed to TunPause() on device removal. This method is simpler than PnP notifications and we are chasing surprise removal issues in WHLK tests. If this works, I'll hopefully come back and update this commit message. Signed-off-by: Simon Rozman <simon@rozman.si>
* Distinguish NDIS_STATUS and NTSTATUSSimon Rozman2019-06-201-5/+5
| | | | Signed-off-by: Simon Rozman <simon@rozman.si>
* Implement proper PnP notification re-registration on canceled removalSimon Rozman2019-06-201-51/+57
| | | | | | | | | | | | | | | | | | | | | | | | | | | | The Microsoft Documentation is clear: "The PnP manager can still call the driver's notification callback routine, but in such calls the file object in the NotificationStructure is not valid."[1] Therefore, we must not touch the notification->FileObject in GUID_TARGET_DEVICE_REMOVE_CANCELLED. "Because the driver closed the previous registration handle in response to the query-remove notification, the driver must open a new handle. The driver must: 1. Remove the old registration with IoUnregisterPlugPlayNotification. 2. Open a new handle to the device. 3. Reregister for notification on the new handle with IoRegisterPlugPlayNotification." Therefore, let's do it. Unfortunately, in order to implement this, we must save the driver object and device symbolic name. [1](https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/handling-a-guid-target-device-query-remove-event) [2](https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/handling-a-guid-target-device-remove-cancelled-event) Signed-off-by: Simon Rozman <simon@rozman.si>
* Rename ActiveTransactionCount to ActiveNBLCountSimon Rozman2019-06-201-9/+9
| | | | | | | As ActiveTransacrionCount is all about counting NBLs in flight (or just about to push some more NBLs), rename it to a more suitable name. Signed-off-by: Simon Rozman <simon@rozman.si>
* Reuse Device.RefCount value to prevent duplicate status indicationsSimon Rozman2019-06-201-1/+1
| | | | Signed-off-by: Simon Rozman <simon@rozman.si>
* Fix the cleanup order in TunDispatchWrite()Simon Rozman2019-06-201-1/+1
| | | | Signed-off-by: Simon Rozman <simon@rozman.si>
* Cleanup ref-counting overflow assertsSimon Rozman2019-06-201-7/+0
| | | | | | | | | | Asserting on 63-bit overflow seems a bit excessive. While 31-bit overflow is more likely to happen, we should introduce a real check if we are concerned about it. Rather than using an ASSERT in Debug configuration run by probably nobody else but me. Signed-off-by: Simon Rozman <simon@rozman.si>
* Add missing state transition lockSimon Rozman2019-06-201-0/+2
| | | | | | | | When we are transitioning to a state that suspends some operations, we must get an exclusive transition lock to wait for all operations that have a shared transition lock. And TunHaltEx() is no exception. Signed-off-by: Simon Rozman <simon@rozman.si>
* Revise adapter state checkingSimon Rozman2019-06-201-61/+81
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | TunCheckForPause() was inlined and adjusted: it had two calls with two potential new ones, but each call would have a slightly different optimal variant: some with ActiveTransactionCount increment, others without; some with Device.RefCount check, others without... No two particular use cases were completely identical: 1. TunSendNetBufferLists: - requires ActiveTransactionCount increment - requires Device.RefCount > 0 check - reports NDIS_STATUS in case of rejection 2. IRP_MJ_READ: - no ActiveTransactionCount increment - no Device.RefCount > 0 check, as IRP_MJ_READ implies it - reports NTSTATUS in case of rejection 3. IRP_MJ_WRITE: - requires ActiveTransactionCount increment - no Device.RefCount > 0 check, as IRP_MJ_WRITE implies it - reports NTSTATUS in case of rejection 4. IRP_MJ_CREATE: - no ActiveTransactionCount increment - no Device.RefCount > 0 check to allow initial client connection - reports NTSTATUS in case of rejection, with slightly different status codes than IRP_MJ_READ and IRP_MJ_WRITE. TUN_FLAGS_ENABLED was renamed to TUN_FLAGS_RUNNING: enabled/disabled adapter means initialized&running/paused&halted in Windows world. Signed-off-by: Simon Rozman <simon@rozman.si>
* Count active transactions in NBLs rather than IRPs in receive directionSimon Rozman2019-06-201-10/+13
| | | | | | | | | | | | | | | | This makes ActiveTransactionCount a sum of: - 1: initialized in TunRestart() - count of NBLs in send queue - count of NBLs in receive queue - TunSendNetBufferLists() unfinished - IRP_MJ_WRITE unfinished Mind that we do not need to explicitly check for TUN_FLAGS_PRESENT early in IRP_MJ_WRITE as it is implicitly checked by TunCheckForPause() along with other flags required to run the packet flow. The later is actually more correct by being shared-locked by the transition lock. Signed-off-by: Simon Rozman <simon@rozman.si>
* Cleanup adapter power monitoringSimon Rozman2019-06-201-15/+2
| | | | | | | | | | Wintun adapter is always paused before transition to a low-power state. Even on NDIS 6.30 we do not specify the NDIS_MINIPORT_ATTRIBUTES_NO_- PAUSE_ON_SUSPEND flag. Since OID_PNP_SET_POWER does not do anything else that TunPause() already takes care of it is redundant. The TUN_FLAGS_ENABLED and TUN_FLAGS_POWERED were also mostly identical. Signed-off-by: Simon Rozman <simon@rozman.si>
* Use more appropriate status when rejecting NBLs with no client connectedSimon Rozman2019-06-201-2/+2
| | | | Signed-off-by: Simon Rozman <simon@rozman.si>
* Restore adapter on PnP remove-cancel and reuse notification file objectSimon Rozman2019-06-201-27/+9
| | | | | | | | | | | In case target device removal was canceled, the adapter is now restored to present state. This is a part of research why Wintun adapters are misbehaving in some WHLK tests. PnP notifications already provide FILE_OBJECT of the device we are monitoring. We don't need to store it in adapter context. Signed-off-by: Simon Rozman <simon@rozman.si>
* Simplify state machineSimon Rozman2019-06-201-41/+28
| | | | Signed-off-by: Simon Rozman <simon@rozman.si>
* Set deny-all DACL instead of removing symlink on haltingSergei Mileshin2019-06-201-13/+25
| | | | | | | | | | | Deleting symbolic link on device removal only still makes it possible to open it from the real path. Setting the deny-all DACL instead is a more reliable way of preventing clients reopening the device when it is being removed. Signed-off-by: Sergei Mileshin <msvsysproger@gmail.com> Signed-off-by: Simon Rozman <simon@rozman.si>
* Revise buffer size calculation to work across 32/64-bit boundarySimon Rozman2019-06-101-3/+3
| | | | Signed-off-by: Simon Rozman <simon@rozman.si>
* Do not take handle reference with verifier enabledJason A. Donenfeld2019-06-081-4/+9
| | | | | | | | | This is actually very much wrong. In fact, it's bound to create all sorts of nasty issues. But without it, we can't use the reference function to check the validity of a potentially invalid handle while the verifier is enabled. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Clean TunWaitForReferencesToDropToZero()Simon Rozman2019-06-081-4/+4
| | | | Signed-off-by: Simon Rozman <simon@rozman.si>
* Refactor IRP_MJ_CLOSE TransitionLock unlockingSimon Rozman2019-06-081-4/+4
| | | | Signed-off-by: Simon Rozman <simon@rozman.si>
* Reorder source codeSimon Rozman2019-06-071-212/+210
| | | | Signed-off-by: Simon Rozman <simon@rozman.si>
* Revise constants requiring network-byte orderSimon Rozman2019-06-071-15/+14
| | | | Signed-off-by: Simon Rozman <simon@rozman.si>
* Use per-protocol NBL lists on writeSimon Rozman2019-06-071-36/+54
| | | | Signed-off-by: Simon Rozman <simon@rozman.si>