/* * -------------------------------------------------------------------------- * OvsCleanupBufferPool -- * Free Buffer pool for NBL and NB. * -------------------------------------------------------------------------- */ VOID OvsCleanupBufferPool(PVOID ovsContext) { POVS_NBL_POOL ovsPool; POVS_SWITCH_CONTEXT context = (POVS_SWITCH_CONTEXT)ovsContext; ovsPool = &context->ovsPool; OVS_LOG_TRACE("Enter: context: %p", context); #ifdef DBG ASSERT(ovsPool->fixNBLCount == 0); ASSERT(ovsPool->zeroNBLCount == 0); ASSERT(ovsPool->nblOnlyCount == 0); ASSERT(ovsPool->nbCount == 0); ASSERT(ovsPool->sysNBLCount == 0); ASSERT(ovsPool->fragNBLCount == 0); #endif if (ovsPool->fixSizePool) { NdisFreeNetBufferListPool(ovsPool->fixSizePool); ovsPool->fixSizePool = NULL; } if (ovsPool->zeroSizePool) { NdisFreeNetBufferListPool(ovsPool->zeroSizePool); ovsPool->zeroSizePool = NULL; } if (ovsPool->nblOnlyPool) { NdisFreeNetBufferListPool(ovsPool->nblOnlyPool); ovsPool->nblOnlyPool = NULL; } if (ovsPool->nbPool) { NdisFreeNetBufferPool(ovsPool->nbPool); ovsPool->nbPool = NULL; } OVS_LOG_TRACE("Exit: cleanup OVS Buffer pool"); }
// Free adapter context memory and associated resources. VOID tapAdapterContextFree(__in PTAP_ADAPTER_CONTEXT Adapter) { PLIST_ENTRY listEntry = &Adapter->AdapterListLink; DEBUGP(("[TAP] --> tapAdapterContextFree\n")); // Adapter context should already be removed. ASSERT((listEntry->Flink == listEntry) && (listEntry->Blink == listEntry)); // Insure that adapter context has been removed from global adapter list. RemoveEntryList(&Adapter->AdapterListLink); // Free the adapter lock. NdisFreeSpinLock(&Adapter->AdapterLock); // Free the ANSI NetCfgInstanceId buffer. if (Adapter->NetCfgInstanceIdAnsi.Buffer != NULL) { RtlFreeAnsiString(&Adapter->NetCfgInstanceIdAnsi); } Adapter->NetCfgInstanceIdAnsi.Buffer = NULL; // Free the receive NBL pool. if (Adapter->ReceiveNblPool != NULL) { NdisFreeNetBufferListPool(Adapter->ReceiveNblPool); } Adapter->ReceiveNblPool = NULL; NdisFreeMemory(Adapter, 0, 0); DEBUGP(("[TAP] <-- tapAdapterContextFree\n")); }
void StreamEditEvtDriverUnload( _In_ WDFDRIVER driverObject ) { UNREFERENCED_PARAMETER(driverObject); if (!configEditInline) { OobEditShutdown(&gStreamEditor); } if (gStreamEditor.scratchBuffer != NULL) { ExFreePoolWithTag( gStreamEditor.scratchBuffer, STREAM_EDITOR_FLAT_BUFFER_TAG ); } StreamEditUnregisterCallout(); FwpsInjectionHandleDestroy(gInjectionHandle); NdisFreeNetBufferListPool(gNetBufferListPool); NdisFreeGenericObject(gNdisGenericObj); IoFreeMdl(gStringToReplaceMdl); }
VOID DriverUnload( IN PDRIVER_OBJECT driverObject) { UNICODE_STRING dosDeviceName; UNREFERENCED_PARAMETER(driverObject); // set the unloading marker { KLOCK_QUEUE_HANDLE packetQueueLockHandle; KeAcquireInStackQueuedSpinLock( &gPacketQueueLock, &packetQueueLockHandle ); gDriverUnloading = TRUE; KeReleaseInStackQueuedSpinLock(&packetQueueLockHandle); } CleanupFlowContextList(); if (IsListEmpty(&gPacketQueue)) { KeSetEvent( &gWorkerEvent, IO_NO_INCREMENT, FALSE); } ASSERT(gThreadObj != NULL); KeWaitForSingleObject( gThreadObj, Executive, KernelMode, FALSE, NULL); ObDereferenceObject(gThreadObj); UnregisterCallouts(); NdisFreeNetBufferListPool(gNetBufferListPool); NdisFreeGenericObject(gNdisGenericObj); FwpsInjectionHandleDestroy0(gInjectionHandle); RtlInitUnicodeString(&dosDeviceName, SYMBOLIC_LINK_NAME); IoDeleteSymbolicLink(&dosDeviceName); IoDeleteDevice(gDeviceObject); }
// Release the packet buffer void NeoFreePacketBuffer(PACKET_BUFFER *p) { // Validate arguments if (p == NULL) { return; } // Release the NET_BUFFER_LIST NdisFreeNetBufferList(p->NetBufferList); // Release the NET_BUFFER_LIST pool NdisFreeNetBufferListPool(p->NetBufferListPool); // Release the memory NeoFree(p); }
_IRQL_requires_same_ inline VOID KrnlHlprNDISPoolDataPurge(_Inout_ NDIS_POOL_DATA* pNDISPoolData) { #if DBG DbgPrintEx(DPFLTR_IHVNETWORK_ID, DPFLTR_INFO_LEVEL, " ---> KrnlHlprNDISPoolDataPurge()\n"); #endif /// DBG NT_ASSERT(pNDISPoolData); if(pNDISPoolData->ndisHandle) { if(pNDISPoolData->nbPoolHandle) { NdisFreeNetBufferPool(pNDISPoolData->nbPoolHandle); pNDISPoolData->nbPoolHandle = 0; } if(pNDISPoolData->nblPoolHandle) { NdisFreeNetBufferListPool(pNDISPoolData->nblPoolHandle); pNDISPoolData->nblPoolHandle = 0; } NdisFreeGenericObject((PNDIS_GENERIC_OBJECT)(pNDISPoolData->ndisHandle)); pNDISPoolData->ndisHandle = 0; } RtlZeroMemory(pNDISPoolData, sizeof(NDIS_POOL_DATA)); #if DBG DbgPrintEx(DPFLTR_IHVNETWORK_ID, DPFLTR_INFO_LEVEL, " <--- KrnlHlprNDISPoolDataPurge()\n"); #endif /// DBG return; }
/********************************************************** NDIS6-related final initialization: Uninstalling interrupt handler Dellocate buffer list pool Parameters: context ***********************************************************/ VOID ParaNdis_FinalizeCleanup(PARANDIS_ADAPTER *pContext) { // we zero context members to be able examine them in the debugger/dump if (pContext->InterruptHandle) { NdisMDeregisterInterruptEx(pContext->InterruptHandle); pContext->InterruptHandle = NULL; } if (pContext->BufferListsPool) { NdisFreeNetBufferListPool(pContext->BufferListsPool); pContext->BufferListsPool = NULL; } if (pContext->DmaHandle) { NdisMDeregisterScatterGatherDma(pContext->DmaHandle); pContext->DmaHandle = NULL; } }
// Device is closed NTSTATUS SlDeviceCloseProc(DEVICE_OBJECT *device_object, IRP *irp) { SL_DEVICE *dev = *((SL_DEVICE **)device_object->DeviceExtension); NTSTATUS ret = STATUS_UNSUCCESSFUL; IO_STACK_LOCATION *irp_stack = IoGetCurrentIrpStackLocation(irp); if (dev->IsBasicDevice) { // Basic device ret = STATUS_SUCCESS; } else { // Adapter device SL_FILE *f = irp_stack->FileObject->FsContext; if (f != NULL) { bool clear_filter = false; // Wait until the number of packet being sent becomes the zero while (true) { if (f->NumSendingPacketets == 0) { break; } SlSleep(50); } SlLock(dev->OpenCloseLock); { // Delete the file from the list SlLockList(dev->FileList); { SlDelete(dev->FileList, f); if (SL_LIST_NUM(dev->FileList) == 0) { // Clear the filter when all files are closed clear_filter = true; } } SlUnlockList(dev->FileList); if (dev->Adapter->Halt) { clear_filter = false; } if (clear_filter) { InterlockedIncrement(&dev->Adapter->NumPendingOidRequests); } } SlUnlock(dev->OpenCloseLock); if (clear_filter) { // Clear the filter when all files are closed UINT filter = 0; SlSendOidRequest(dev->Adapter, true, OID_GEN_CURRENT_PACKET_FILTER, &filter, sizeof(filter)); InterlockedDecrement(&dev->Adapter->NumPendingOidRequests); } // Release the event SlFreeEvent(f->Event); // Release the receive queue if (true) { SL_PACKET *p = f->RecvPacketHead; while (p != NULL) { SL_PACKET *p_next = p->Next; SlFree(p); p = p_next; } } // Release the NET_BUFFER_LIST pool NdisFreeNetBufferListPool(f->NetBufferListPool); // Release the lock SlFreeLock(f->RecvLock); SlFree(f); ret = STATUS_SUCCESS; } } irp->IoStatus.Status = ret; IoCompleteRequest(irp, IO_NO_INCREMENT); return ret; }
NTSTATUS DriverEntry( DRIVER_OBJECT* driverObject, UNICODE_STRING* registryPath ) { NTSTATUS status; WDFDEVICE device; WDFDRIVER driver; WDFKEY configKey; NET_BUFFER_LIST_POOL_PARAMETERS nblPoolParams = {0}; // Request NX Non-Paged Pool when available ExInitializeDriverRuntime(DrvRtPoolNxOptIn); status = StreamEditInitDriverObjects( driverObject, registryPath, &driver, &device ); if (!NT_SUCCESS(status)) { goto Exit; } status = WdfDriverOpenParametersRegistryKey( driver, KEY_READ, WDF_NO_OBJECT_ATTRIBUTES, &configKey ); if (!NT_SUCCESS(status)) { goto Exit; } status = StreamEditLoadConfig(configKey); if (!NT_SUCCESS(status)) { goto Exit; } gStringToReplaceMdl = IoAllocateMdl( configStringToReplace, (ULONG) strlen(configStringToReplace), FALSE, FALSE, NULL ); if (gStringToReplaceMdl == NULL) { status = STATUS_NO_MEMORY; goto Exit; } MmBuildMdlForNonPagedPool(gStringToReplaceMdl); gNdisGenericObj = NdisAllocateGenericObject( driverObject, STREAM_EDITOR_NDIS_OBJ_TAG, 0 ); if (gNdisGenericObj == NULL) { status = STATUS_NO_MEMORY; goto Exit; } nblPoolParams.Header.Type = NDIS_OBJECT_TYPE_DEFAULT; nblPoolParams.Header.Revision = NET_BUFFER_LIST_POOL_PARAMETERS_REVISION_1; nblPoolParams.Header.Size = sizeof(nblPoolParams); nblPoolParams.fAllocateNetBuffer = TRUE; nblPoolParams.DataSize = 0; nblPoolParams.PoolTag = STREAM_EDITOR_NBL_POOL_TAG; gNetBufferListPool = NdisAllocateNetBufferListPool( gNdisGenericObj, &nblPoolParams ); if (gNetBufferListPool == NULL) { status = STATUS_NO_MEMORY; goto Exit; } status = FwpsInjectionHandleCreate( AF_UNSPEC, FWPS_INJECTION_TYPE_STREAM, &gInjectionHandle ); if (!NT_SUCCESS(status)) { goto Exit; } gWdmDevice = WdfDeviceWdmGetDeviceObject(device); status = StreamEditRegisterCallout( &gStreamEditor, gWdmDevice ); if (!NT_SUCCESS(status)) { goto Exit; } if (configEditInline) { InlineEditInit(&gStreamEditor); } else { status = OobEditInit(&gStreamEditor); if (!NT_SUCCESS(status)) { goto Exit; } } Exit: if (!NT_SUCCESS(status)) { if (gEngineHandle != NULL) { StreamEditUnregisterCallout(); } if (gInjectionHandle != NULL) { FwpsInjectionHandleDestroy(gInjectionHandle); } if (gNetBufferListPool != NULL) { NdisFreeNetBufferListPool(gNetBufferListPool); } if (gNdisGenericObj != NULL) { NdisFreeGenericObject(gNdisGenericObj); } if (gStringToReplaceMdl != NULL) { IoFreeMdl(gStringToReplaceMdl); } } return status; }
NTSTATUS DriverEntry( IN PDRIVER_OBJECT driverObject, IN PUNICODE_STRING registryPath) { NTSTATUS status = STATUS_SUCCESS; NTSTATUS symbolicLinkCreationStatus = STATUS_SUCCESS; UNICODE_STRING deviceName; UNICODE_STRING dosDeviceName; HANDLE threadHandle; NET_BUFFER_LIST_POOL_PARAMETERS nblPoolParams = {0}; UNICODE_STRING defaultSDDLString; #ifdef DEBUG DbgBreakPoint(); #endif status = drvCtlInit(driverObject); if (!NT_SUCCESS(status)) { goto Exit; } gDriverUnloading = FALSE; RtlInitUnicodeString(&defaultSDDLString, L"D:P(A;;GA;;;BU)"); RtlInitUnicodeString(&deviceName, DEVICE_NAME); status = IoCreateDeviceSecure( driverObject, 0, &deviceName, FILE_DEVICE_NETWORK, 0, FALSE, &defaultSDDLString, NULL, &gDeviceObject); if (!NT_SUCCESS(status)) { goto Exit; } RtlInitUnicodeString(&dosDeviceName, SYMBOLIC_LINK_NAME); status = IoCreateSymbolicLink(&dosDeviceName, &deviceName); symbolicLinkCreationStatus = status; if (!NT_SUCCESS(status)) { goto Exit; } status = FwpsInjectionHandleCreate0( AF_UNSPEC, FWPS_INJECTION_TYPE_STREAM, &gInjectionHandle); if (!NT_SUCCESS(status)) { goto Exit; } gNdisGenericObj = NdisAllocateGenericObject( driverObject, TAG_NDIS_OBJ, 0); if (gNdisGenericObj == NULL) { status = STATUS_NO_MEMORY; goto Exit; } nblPoolParams.Header.Type = NDIS_OBJECT_TYPE_DEFAULT; nblPoolParams.Header.Revision = NET_BUFFER_LIST_POOL_PARAMETERS_REVISION_1; nblPoolParams.Header.Size = NDIS_SIZEOF_NET_BUFFER_LIST_POOL_PARAMETERS_REVISION_1; nblPoolParams.fAllocateNetBuffer = TRUE; nblPoolParams.DataSize = 0; nblPoolParams.PoolTag = TAG_NBL_POOL; gNetBufferListPool = NdisAllocateNetBufferListPool( gNdisGenericObj, &nblPoolParams); if(gNetBufferListPool == NULL) { status = STATUS_NO_MEMORY; goto Exit; } InitializeListHead(&gPacketQueue); KeInitializeSpinLock(&gPacketQueueLock); InitializeListHead(&flowContextList); KeInitializeSpinLock(&flowContextListLock); KeInitializeEvent( &gWorkerEvent, NotificationEvent, FALSE ); status = RegisterCallouts(gDeviceObject); if (!NT_SUCCESS(status)) { goto Exit; } status = PsCreateSystemThread( &threadHandle, THREAD_ALL_ACCESS, NULL, NULL, NULL, thAnalyzer, NULL); if (!NT_SUCCESS(status)) { goto Exit; } status = ObReferenceObjectByHandle( threadHandle, 0, NULL, KernelMode, (PVOID*) &gThreadObj, NULL); ASSERT(NT_SUCCESS(status)); KeSetBasePriorityThread( (PKTHREAD) gThreadObj, -2); ZwClose(threadHandle); driverObject->DriverUnload = DriverUnload; Exit: if (!NT_SUCCESS(status)) { if (gFwpmEngineHandle != NULL) { UnregisterCallouts(); } if (gInjectionHandle != NULL) { FwpsInjectionHandleDestroy0(gInjectionHandle); } if (gDeviceObject) { IoDeleteDevice(gDeviceObject); } if(NT_SUCCESS(symbolicLinkCreationStatus)) { IoDeleteSymbolicLink(&dosDeviceName); } if (gNetBufferListPool != NULL) { NdisFreeNetBufferListPool(gNetBufferListPool); } if (gNdisGenericObj != NULL) { NdisFreeGenericObject(gNdisGenericObj); } } return status; }