diff --git a/wintun.c b/wintun.c index a659ee1..d7208f6 100644 --- a/wintun.c +++ b/wintun.c @@ -52,23 +52,15 @@ typedef enum _TUN_STATE { TUN_STATE_PAUSING, // In the Pausing state, a miniport driver completes any operations that are required to stop send and receive operations for an adapter. } TUN_STATE; -typedef enum _TUN_POWER { - TUN_POWER_RUNNING = 0, // D0 - TUN_POWER_SUSPENDED, // D1-D3 - TUN_POWER_RESUMING, // D1-D3 -> D0 - TUN_POWER_SUSPENDING, // D0 -> D1-D3 -} TUN_POWER; - typedef struct _TUN_CTX { volatile TUN_STATE State; - volatile TUN_POWER Power; + + volatile NDIS_DEVICE_POWER_STATE PowerState; NDIS_HANDLE MiniportAdapterHandle; NDIS_STATISTICS_INFO Statistics; - volatile LONG64 RunningTransactionCount; // Number of active transactions that require running adapter (active NBLs + active IRPs) - volatile LONG64 PoweredTransactionCount; // Number of active transactions that require powered up adapter (active NBLs) - NDIS_OID_REQUEST *PendingPowerOIDReq; + volatile LONG64 ActiveTransactionCount; struct { NDIS_HANDLE Handle; @@ -174,71 +166,32 @@ static void TunCompleteRequest(_Inout_ IRP *Irp, _In_ ULONG_PTR Information, _In IoCompleteRequest(Irp, IO_NO_INCREMENT); } -_IRQL_requires_same_ -static void TunTransactionIncrement(_Inout_ TUN_CTX *ctx, _In_ LONG64 increment_nbl, _In_ LONG64 increment_irp) -{ - ASSERT(InterlockedGet64(&ctx->RunningTransactionCount) <= MAXLONG64 - increment_nbl - increment_irp); - InterlockedAdd64(&ctx->RunningTransactionCount, increment_nbl + increment_irp); - - ASSERT(InterlockedGet64(&ctx->PoweredTransactionCount) <= MAXLONG64 - increment_nbl); - InterlockedAdd64(&ctx->PoweredTransactionCount, increment_nbl); -} - _IRQL_requires_same_ _Must_inspect_result_ -static NTSTATUS TunTransactionStart(_Inout_ TUN_CTX *ctx, _In_ LONG64 increment_nbl, _In_ LONG64 increment_irp) +static NTSTATUS TunCheckForPause(_Inout_ TUN_CTX *ctx, _In_ LONG64 increment) { - TunTransactionIncrement(ctx, increment_nbl, increment_irp); - + ASSERT(InterlockedGet64(&ctx->ActiveTransactionCount) <= MAXLONG64 - increment); + InterlockedAdd64(&ctx->ActiveTransactionCount, increment); return - (increment_nbl || increment_irp) && InterlockedGet((LONG *)&ctx->State) != TUN_STATE_RUNNING ? STATUS_NDIS_PAUSED : - (increment_nbl ) && InterlockedGet((LONG *)&ctx->Power) != TUN_POWER_RUNNING ? STATUS_NDIS_LOW_POWER_STATE : + InterlockedGet((LONG *)&ctx->State) != TUN_STATE_RUNNING ? STATUS_NDIS_PAUSED : + ctx->PowerState >= NdisDeviceStateD1 ? STATUS_NDIS_LOW_POWER_STATE : STATUS_SUCCESS; } -_IRQL_requires_same_ -static void TunTransactionDecrement(_Inout_ TUN_CTX *ctx, _In_ LONG64 decrement_nbl, _In_ LONG64 decrement_irp, _Out_opt_ LONG64 *running_count, _Out_opt_ LONG64 *powered_count) -{ - LONG64 result; - - ASSERT(decrement_nbl + decrement_irp <= InterlockedGet64(&ctx->RunningTransactionCount)); - result = InterlockedSubtract64(&ctx->RunningTransactionCount, decrement_nbl + decrement_irp); - if (running_count) - *running_count = result; - - ASSERT(decrement_nbl <= InterlockedGet64(&ctx->PoweredTransactionCount)); - result = InterlockedSubtract64(&ctx->PoweredTransactionCount, decrement_nbl); - if (powered_count) - *powered_count = result; -} - _IRQL_requires_max_(DISPATCH_LEVEL) -static void TunTransactionEnd(_Inout_ TUN_CTX *ctx, _In_ LONG64 decrement_nbl, _In_ LONG64 decrement_irp, _Out_opt_ NDIS_STATUS *status_state, _Out_opt_ NDIS_STATUS *status_power) +static NDIS_STATUS TunCompletePause(_Inout_ TUN_CTX *ctx, _In_ LONG64 decrement, _In_ BOOLEAN async_completion) { - LONG64 running_count, powered_count; - TunTransactionDecrement(ctx, decrement_nbl, decrement_irp, &running_count, &powered_count); - - if (status_state) - *status_state = NDIS_STATUS_PENDING; - if (status_power) - *status_power = NDIS_STATUS_PENDING; - - if (!running_count && InterlockedCompareExchange((LONG *)&ctx->State, TUN_STATE_PAUSED, TUN_STATE_PAUSING) == TUN_STATE_PAUSING) { + ASSERT(decrement <= InterlockedGet64(&ctx->ActiveTransactionCount)); + if (!InterlockedSubtract64(&ctx->ActiveTransactionCount, decrement) && + InterlockedCompareExchange((LONG *)&ctx->State, TUN_STATE_PAUSED, TUN_STATE_PAUSING) == TUN_STATE_PAUSING) { InterlockedExchange64(&ctx->Device.RefCount, 0); TunIndicateStatus(ctx); - if (status_state) - *status_state = NDIS_STATUS_SUCCESS; - else + if (async_completion) NdisMPauseComplete(ctx->MiniportAdapterHandle); + return NDIS_STATUS_SUCCESS; } - if (!powered_count && InterlockedCompareExchange((LONG *)&ctx->Power, TUN_POWER_SUSPENDED, TUN_POWER_SUSPENDING) == TUN_POWER_SUSPENDING) { - NDIS_OID_REQUEST *oid_req = InterlockedExchangePointer(&ctx->PendingPowerOIDReq, NULL); - if (status_power) - *status_power = NDIS_STATUS_SUCCESS; - else if (oid_req) - NdisMOidRequestComplete(ctx->MiniportAdapterHandle, oid_req, NDIS_STATUS_SUCCESS); - } + return NDIS_STATUS_PENDING; } static IO_CSQ_INSERT_IRP_EX TunCsqInsertIrpEx; @@ -307,7 +260,7 @@ static VOID TunCsqCompleteCanceledIrp(IO_CSQ *Csq, IRP *Irp) { TUN_CTX *ctx = CONTAINING_RECORD(Csq, TUN_CTX, Device.ReadQueue.Csq); TunCompleteRequest(Irp, 0, STATUS_CANCELLED); - TunTransactionEnd(ctx, 0, 1, NULL, NULL); + TunCompletePause(ctx, 1, TRUE); } _IRQL_requires_same_ @@ -382,7 +335,7 @@ retry: if (!NT_SUCCESS(status)) { irp->IoStatus.Status = status; IoCompleteRequest(irp, IO_NETWORK_INCREMENT); - TunTransactionEnd(ctx, 0, 1, NULL, NULL); + TunCompletePause(ctx, 1, TRUE); goto retry; } @@ -426,10 +379,8 @@ static NTSTATUS TunWriteIntoIrp(_Inout_ TUN_CTX *ctx, _Inout_ IRP *Irp, _Inout_ _IRQL_requires_same_ static void TunNBLRefInit(_Inout_ TUN_CTX *ctx, _Inout_ NET_BUFFER_LIST *nbl) { - TunTransactionIncrement(ctx, 1, 0); - - ASSERT(InterlockedGet(&ctx->PacketQueue.NumNbl) < MAXLONG); - InterlockedIncrement(&ctx->PacketQueue.NumNbl); + InterlockedAdd64(&ctx->ActiveTransactionCount, 1); + InterlockedAdd(&ctx->PacketQueue.NumNbl, 1); InterlockedExchange64(NET_BUFFER_LIST_MINIPORT_RESERVED_REFCOUNT(nbl), 1); } @@ -437,8 +388,7 @@ _IRQL_requires_same_ static void TunNBLRefInc(_Inout_ NET_BUFFER_LIST *nbl) { ASSERT(InterlockedGet64(NET_BUFFER_LIST_MINIPORT_RESERVED_REFCOUNT(nbl))); - ASSERT(InterlockedGet64(NET_BUFFER_LIST_MINIPORT_RESERVED_REFCOUNT(nbl)) < MAXLONG); - InterlockedIncrement64(NET_BUFFER_LIST_MINIPORT_RESERVED_REFCOUNT(nbl)); + InterlockedAdd64(NET_BUFFER_LIST_MINIPORT_RESERVED_REFCOUNT(nbl), 1); } _When_( (SendCompleteFlags & NDIS_SEND_COMPLETE_FLAGS_DISPATCH_LEVEL), _IRQL_requires_ (DISPATCH_LEVEL)) @@ -446,12 +396,11 @@ _When_(!(SendCompleteFlags & NDIS_SEND_COMPLETE_FLAGS_DISPATCH_LEVEL), _IRQL_req static BOOLEAN TunNBLRefDec(_Inout_ TUN_CTX *ctx, _Inout_ NET_BUFFER_LIST *nbl, _In_ ULONG SendCompleteFlags) { ASSERT(InterlockedGet64(NET_BUFFER_LIST_MINIPORT_RESERVED_REFCOUNT(nbl))); - if (!InterlockedDecrement64(NET_BUFFER_LIST_MINIPORT_RESERVED_REFCOUNT(nbl))) { + if (!InterlockedSubtract64(NET_BUFFER_LIST_MINIPORT_RESERVED_REFCOUNT(nbl), 1)) { NET_BUFFER_LIST_NEXT_NBL(nbl) = NULL; NdisMSendNetBufferListsComplete(ctx->MiniportAdapterHandle, nbl, SendCompleteFlags); - ASSERT(InterlockedGet(&ctx->PacketQueue.NumNbl)); - InterlockedDecrement(&ctx->PacketQueue.NumNbl); - TunTransactionEnd(ctx, 1, 0, NULL, NULL); + InterlockedSubtract(&ctx->PacketQueue.NumNbl, 1); + TunCompletePause(ctx, 1, TRUE); return TRUE; } return FALSE; @@ -626,7 +575,7 @@ static void TunQueueProcess(_Inout_ TUN_CTX *ctx) } else { irp->IoStatus.Status = STATUS_SUCCESS; IoCompleteRequest(irp, IO_NETWORK_INCREMENT); - TunTransactionEnd(ctx, 0, 1, NULL, NULL); + TunCompletePause(ctx, 1, TRUE); irp = NULL; } @@ -647,15 +596,15 @@ static NTSTATUS TunDispatchCreate(DEVICE_OBJECT *DeviceObject, IRP *Irp) goto cleanup_complete_req; } - if (!NT_SUCCESS(status = TunTransactionStart(ctx, 0, 1))) - goto cleanup_TunTransactionEnd; + if (!NT_SUCCESS(status = TunCheckForPause(ctx, 1))) + goto cleanup_TunCompletePause; ASSERT(InterlockedGet64(&ctx->Device.RefCount) < MAXLONG64); InterlockedIncrement64(&ctx->Device.RefCount); TunIndicateStatus(ctx); -cleanup_TunTransactionEnd: - TunTransactionEnd(ctx, 0, 1, NULL, NULL); +cleanup_TunCompletePause: + TunCompletePause(ctx, 1, TRUE); cleanup_complete_req: TunCompleteRequest(Irp, 0, status); return status; @@ -673,15 +622,15 @@ static NTSTATUS TunDispatchClose(DEVICE_OBJECT *DeviceObject, IRP *Irp) goto cleanup_complete_req; } - if (!NT_SUCCESS(status = TunTransactionStart(ctx, 0, 1))) - goto cleanup_TunTransactionEnd; + if (!NT_SUCCESS(status = TunCheckForPause(ctx, 1))) + goto cleanup_TunCompletePause; ASSERT(InterlockedGet64(&ctx->Device.RefCount) > 0); InterlockedDecrement64(&ctx->Device.RefCount); TunIndicateStatus(ctx); -cleanup_TunTransactionEnd: - TunTransactionEnd(ctx, 0, 1, NULL, NULL); +cleanup_TunCompletePause: + TunCompletePause(ctx, 1, TRUE); cleanup_complete_req: TunCompleteRequest(Irp, 0, status); return status; @@ -699,24 +648,24 @@ static NTSTATUS TunDispatchRead(DEVICE_OBJECT *DeviceObject, IRP *Irp) goto cleanup_complete_req; } - if (!NT_SUCCESS(status = TunTransactionStart(ctx, 0, 1))) - goto cleanup_TunTransactionEnd; + if (!NT_SUCCESS(status = TunCheckForPause(ctx, 1))) + goto cleanup_TunCompletePause; Irp->IoStatus.Information = 0; - TunTransactionIncrement(ctx, 0, 1); + InterlockedIncrement64(&ctx->ActiveTransactionCount); status = IoCsqInsertIrpEx(&ctx->Device.ReadQueue.Csq, Irp, NULL, TUN_CSQ_INSERT_TAIL); if (!NT_SUCCESS(status)) { - TunTransactionDecrement(ctx, 0, 1, NULL, NULL); - goto cleanup_TunTransactionEnd; + InterlockedDecrement64(&ctx->ActiveTransactionCount); + goto cleanup_TunCompletePause; } TunQueueProcess(ctx); - TunTransactionEnd(ctx, 0, 1, NULL, NULL); + TunCompletePause(ctx, 1, TRUE); return STATUS_PENDING; -cleanup_TunTransactionEnd: - TunTransactionEnd(ctx, 0, 1, NULL, NULL); +cleanup_TunCompletePause: + TunCompletePause(ctx, 1, TRUE); cleanup_complete_req: TunCompleteRequest(Irp, 0, status); return status; @@ -735,14 +684,14 @@ static NTSTATUS TunDispatchWrite(DEVICE_OBJECT *DeviceObject, IRP *Irp) goto cleanup_complete_req; } - if (!NT_SUCCESS(status = TunTransactionStart(ctx, 1, 1))) - goto cleanup_TunTransactionEnd; + if (!NT_SUCCESS(status = TunCheckForPause(ctx, 1))) + goto cleanup_TunCompletePause; UCHAR *buffer; ULONG size; status = TunGetIrpBuffer(Irp, &buffer, &size); if (!NT_SUCCESS(status)) - goto cleanup_TunTransactionEnd; + goto cleanup_TunCompletePause; const UCHAR *b = buffer, *b_end = buffer + size; ULONG nbl_count = 0; @@ -846,8 +795,8 @@ static NTSTATUS TunDispatchWrite(DEVICE_OBJECT *DeviceObject, IRP *Irp) information = b - buffer; -cleanup_TunTransactionEnd: - TunTransactionEnd(ctx, 1, 1, NULL, NULL); +cleanup_TunCompletePause: + TunCompletePause(ctx, 1, TRUE); cleanup_complete_req: TunCompleteRequest(Irp, information, status); return status; @@ -865,19 +814,19 @@ static NTSTATUS TunDispatchCleanup(DEVICE_OBJECT *DeviceObject, IRP *Irp) goto cleanup_complete_req; } - LONG64 count_irp = 1; - if (!NT_SUCCESS(status = TunTransactionStart(ctx, 0, count_irp))) - goto cleanup_TunTransactionEnd; + LONG64 count = 1; + if (!NT_SUCCESS(status = TunCheckForPause(ctx, count))) + goto cleanup_TunCompletePause; IO_STACK_LOCATION *stack = IoGetCurrentIrpStackLocation(Irp); IRP *pending_irp; while ((pending_irp = IoCsqRemoveNextIrp(&ctx->Device.ReadQueue.Csq, stack->FileObject)) != NULL) { - count_irp++; + count++; TunCompleteRequest(pending_irp, 0, STATUS_CANCELLED); } -cleanup_TunTransactionEnd: - TunTransactionEnd(ctx, 0, count_irp, NULL, NULL); +cleanup_TunCompletePause: + TunCompletePause(ctx, count, TRUE); cleanup_complete_req: TunCompleteRequest(Irp, 0, status); return status; @@ -897,10 +846,11 @@ static NDIS_STATUS TunPause(NDIS_HANDLE MiniportAdapterContext, PNDIS_MINIPORT_P { TUN_CTX *ctx = (TUN_CTX *)MiniportAdapterContext; - LONG64 count_irp = 1; - TunTransactionIncrement(ctx, 1, count_irp); + LONG64 count = 1; + InterlockedAdd64(&ctx->ActiveTransactionCount, count); + if (InterlockedCompareExchange((LONG *)&ctx->State, TUN_STATE_PAUSING, TUN_STATE_RUNNING) != TUN_STATE_RUNNING) { - TunTransactionDecrement(ctx, 1, count_irp, NULL, NULL); + InterlockedDecrement64(&ctx->ActiveTransactionCount); return NDIS_STATUS_FAILURE; } @@ -909,13 +859,11 @@ static NDIS_STATUS TunPause(NDIS_HANDLE MiniportAdapterContext, PNDIS_MINIPORT_P /* Cancel pending IRPs to unblock waiting clients. */ IRP *pending_irp; while ((pending_irp = IoCsqRemoveNextIrp(&ctx->Device.ReadQueue.Csq, NULL)) != NULL) { - count_irp++; + count++; TunCompleteRequest(pending_irp, 0, STATUS_CANCELLED); } - NDIS_STATUS status; - TunTransactionEnd(ctx, 1, count_irp, &status, NULL); - return status; + return TunCompletePause(ctx, count, FALSE); } static MINIPORT_RESTART TunRestart; @@ -926,10 +874,7 @@ static NDIS_STATUS TunRestart(NDIS_HANDLE MiniportAdapterContext, PNDIS_MINIPORT if (InterlockedCompareExchange((LONG *)&ctx->State, TUN_STATE_RESTARTING, TUN_STATE_PAUSED) != TUN_STATE_PAUSED) return NDIS_STATUS_FAILURE; - ASSERT(!InterlockedGet64(&ctx->RunningTransactionCount)); - ASSERT(!InterlockedGet64(&ctx->PoweredTransactionCount)); ASSERT(!InterlockedGet64(&ctx->Device.RefCount)); - TunIndicateStatus(ctx); InterlockedExchange((LONG *)&ctx->State, TUN_STATE_RUNNING); @@ -1014,7 +959,7 @@ static NDIS_STATUS TunInitializeEx(NDIS_HANDLE MiniportAdapterHandle, NDIS_HANDL NdisZeroMemory(ctx, sizeof(*ctx)); ctx->State = TUN_STATE_INITIALIZING; - ctx->Power = TUN_POWER_RUNNING; + ctx->PowerState = NdisDeviceStateD0; ctx->MiniportAdapterHandle = MiniportAdapterHandle; ctx->Statistics.Header.Type = NDIS_OBJECT_TYPE_DEFAULT; @@ -1046,7 +991,7 @@ static NDIS_STATUS TunInitializeEx(NDIS_HANDLE MiniportAdapterHandle, NDIS_HANDL .Revision = NdisVersion < NDIS_RUNTIME_VERSION_630 ? NDIS_MINIPORT_ADAPTER_REGISTRATION_ATTRIBUTES_REVISION_1 : NDIS_MINIPORT_ADAPTER_REGISTRATION_ATTRIBUTES_REVISION_2, .Size = NdisVersion < NDIS_RUNTIME_VERSION_630 ? NDIS_SIZEOF_MINIPORT_ADAPTER_REGISTRATION_ATTRIBUTES_REVISION_1 : NDIS_SIZEOF_MINIPORT_ADAPTER_REGISTRATION_ATTRIBUTES_REVISION_2 }, - .AttributeFlags = NDIS_MINIPORT_ATTRIBUTES_NO_HALT_ON_SUSPEND | (NdisVersion >= NDIS_RUNTIME_VERSION_630 ? NDIS_MINIPORT_ATTRIBUTES_NO_PAUSE_ON_SUSPEND : 0), + .AttributeFlags = NDIS_MINIPORT_ATTRIBUTES_NO_HALT_ON_SUSPEND, .InterfaceType = NdisInterfaceInternal, .MiniportAdapterContext = ctx }; @@ -1250,8 +1195,7 @@ static void TunHaltEx(NDIS_HANDLE MiniportAdapterContext, NDIS_HALT_ACTION HaltA if (InterlockedGet((LONG *)&ctx->State) != TUN_STATE_PAUSED) return; - ASSERT(!InterlockedGet64(&ctx->RunningTransactionCount)); - ASSERT(!InterlockedGet64(&ctx->PoweredTransactionCount)); + ASSERT(!InterlockedGet64(&ctx->ActiveTransactionCount)); ASSERT(!InterlockedGet64(&ctx->Device.RefCount)); /* Reset adapter context in device object, as Windows keeps calling dispatch handlers even after NdisDeregisterDeviceEx(). */ @@ -1296,27 +1240,7 @@ static NDIS_STATUS TunOidSet(_Inout_ TUN_CTX *ctx, _Inout_ NDIS_OID_REQUEST *Oid return NDIS_STATUS_INVALID_LENGTH; } OidRequest->DATA.SET_INFORMATION.BytesRead = sizeof(NDIS_DEVICE_POWER_STATE); - if (*((NDIS_DEVICE_POWER_STATE *)OidRequest->DATA.SET_INFORMATION.InformationBuffer) >= NdisDeviceStateD1) { - TunTransactionIncrement(ctx, 1, 0); - if (InterlockedCompareExchange((LONG *)&ctx->Power, TUN_POWER_SUSPENDING, TUN_POWER_RUNNING) != TUN_POWER_RUNNING) { - TunTransactionDecrement(ctx, 1, 0, NULL, NULL); - return NDIS_STATUS_FAILURE; - } - - TunQueueClear(ctx); - InterlockedExchangePointer(&ctx->PendingPowerOIDReq, OidRequest); - - NDIS_STATUS status; - TunTransactionEnd(ctx, 1, 0, NULL, &status); - return status; - } else { - if (InterlockedCompareExchange((LONG *)&ctx->Power, TUN_POWER_RESUMING, TUN_POWER_SUSPENDED) != TUN_POWER_SUSPENDED) - return NDIS_STATUS_FAILURE; - - ASSERT(!InterlockedGet64(&ctx->PoweredTransactionCount)); - - InterlockedExchange((LONG *)&ctx->Power, TUN_POWER_RUNNING); - } + ctx->PowerState = *((NDIS_DEVICE_POWER_STATE *)OidRequest->DATA.SET_INFORMATION.InformationBuffer); return NDIS_STATUS_SUCCESS; } @@ -1451,17 +1375,17 @@ static void TunSendNetBufferLists(NDIS_HANDLE MiniportAdapterContext, NET_BUFFER TUN_CTX *ctx = (TUN_CTX *)MiniportAdapterContext; NDIS_STATUS status; - if (!NT_SUCCESS(status = TunTransactionStart(ctx, 1, 0))) { + if (!NT_SUCCESS(status = TunCheckForPause(ctx, 1))) { TunSetNBLStatus(NetBufferLists, status); NdisMSendNetBufferListsComplete(ctx->MiniportAdapterHandle, NetBufferLists, SendFlags & NDIS_SEND_FLAGS_DISPATCH_LEVEL ? NDIS_SEND_COMPLETE_FLAGS_DISPATCH_LEVEL : 0); - goto cleanup_TunTransactionEnd; + goto cleanup_TunCompletePause; } TunQueueAppend(ctx, NetBufferLists, TUN_QUEUE_MAX_NBLS); TunQueueProcess(ctx); -cleanup_TunTransactionEnd: - TunTransactionEnd(ctx, 1, 0, NULL, NULL); +cleanup_TunCompletePause: + TunCompletePause(ctx, 1, TRUE); } DRIVER_INITIALIZE DriverEntry;