NDIS_HANDLE GlobalProtocolHandle;
KSPIN_LOCK GlobalAdapterListLock;
LIST_ENTRY GlobalAdapterList;
-NDIS_HANDLE GlobalPacketPoolHandle;
-NDIS_HANDLE GlobalBufferPoolHandle;
NDIS_STRING ProtocolName = RTL_CONSTANT_STRING(L"NDISUIO");
return Status;
}
- /* Create the buffer pool */
- NdisAllocateBufferPool(&Status,
- &GlobalBufferPoolHandle,
- 100);
- if (Status != NDIS_STATUS_SUCCESS)
- {
- DPRINT1("Failed to allocate buffer pool with status 0x%x\n", Status);
- IoDeleteSymbolicLink(&DosDeviceName);
- IoDeleteDevice(GlobalDeviceObject);
- return Status;
- }
-
- /* Create the packet pool */
- NdisAllocatePacketPool(&Status,
- &GlobalPacketPoolHandle,
- 50,
- 0);
- if (Status != NDIS_STATUS_SUCCESS)
- {
- DPRINT1("Failed to allocate packet pool with status 0x%x\n", Status);
- NdisFreeBufferPool(GlobalBufferPoolHandle);
- IoDeleteSymbolicLink(&DosDeviceName);
- IoDeleteDevice(GlobalDeviceObject);
- return Status;
- }
-
/* Register the protocol with NDIS */
RtlZeroMemory(&Chars, sizeof(Chars));
Chars.MajorNdisVersion = NDIS_MAJOR_VERSION;
if (Status != NDIS_STATUS_SUCCESS)
{
DPRINT1("Failed to register protocol with status 0x%x\n", Status);
- NdisFreePacketPool(GlobalPacketPoolHandle);
- NdisFreeBufferPool(GlobalBufferPoolHandle);
IoDeleteSymbolicLink(&DosDeviceName);
IoDeleteDevice(GlobalDeviceObject);
return Status;
#include <debug.h>
NDIS_STATUS
-AllocateAndChainBuffer(PNDIS_PACKET Packet, PVOID Buffer, ULONG BufferSize, BOOLEAN Front)
+AllocateAndChainBuffer(PNDISUIO_ADAPTER_CONTEXT AdapterContext,
+ PNDIS_PACKET Packet,
+ PVOID Buffer,
+ ULONG BufferSize,
+ BOOLEAN Front)
{
NDIS_STATUS Status;
PNDIS_BUFFER NdisBuffer;
/* Allocate the NDIS buffer mapping the pool */
NdisAllocateBuffer(&Status,
&NdisBuffer,
- GlobalBufferPoolHandle,
+ AdapterContext->BufferPoolHandle,
Buffer,
BufferSize);
if (Status != NDIS_STATUS_SUCCESS)
}
PNDIS_PACKET
-CreatePacketFromPoolBuffer(PVOID Buffer, ULONG BufferSize)
+CreatePacketFromPoolBuffer(PNDISUIO_ADAPTER_CONTEXT AdapterContext,
+ PVOID Buffer,
+ ULONG BufferSize)
{
PNDIS_PACKET Packet;
NDIS_STATUS Status;
/* Allocate a packet descriptor */
NdisAllocatePacket(&Status,
&Packet,
- GlobalPacketPoolHandle);
+ AdapterContext->PacketPoolHandle);
if (Status != NDIS_STATUS_SUCCESS)
{
DPRINT1("No free packet descriptors\n");
}
/* Use the helper to chain the buffer */
- Status = AllocateAndChainBuffer(Packet, Buffer, BufferSize, TRUE);
+ Status = AllocateAndChainBuffer(AdapterContext, Packet,
+ Buffer, BufferSize, TRUE);
if (Status != NDIS_STATUS_SUCCESS)
{
NdisFreePacket(Packet);
#include "ndisuio.h"
-#define NDEBUG
+//#define NDEBUG
#include <debug.h>
PNDIS_MEDIUM SupportedMedia = {NdisMedium802_3};
NDIS_STATUS Status;
UINT BytesTransferred;
+ DPRINT("Received a %d byte packet on %wZ\n", PacketSize + HeaderBufferSize, &AdapterContext->DeviceName);
+
+ /* Discard if nobody is waiting for it */
+ if (AdapterContext->OpenCount == 0)
+ return NDIS_STATUS_NOT_ACCEPTED;
+
/* Allocate a buffer to hold the packet data and header */
PacketBuffer = ExAllocatePool(NonPagedPool, PacketSize);
if (!PacketBuffer)
return NDIS_STATUS_NOT_ACCEPTED;
/* Allocate the packet descriptor and buffer */
- Packet = CreatePacketFromPoolBuffer((PUCHAR)PacketBuffer + HeaderBufferSize,
+ Packet = CreatePacketFromPoolBuffer(AdapterContext,
+ (PUCHAR)PacketBuffer + HeaderBufferSize,
PacketSize);
if (!Packet)
{
UnbindAdapterByContext(PNDISUIO_ADAPTER_CONTEXT AdapterContext)
{
KIRQL OldIrql;
- PLIST_ENTRY CurrentOpenEntry;
+ PLIST_ENTRY CurrentEntry;
PNDISUIO_OPEN_ENTRY OpenEntry;
+ PNDISUIO_PACKET_ENTRY PacketEntry;
NDIS_STATUS Status;
+ DPRINT("Unbinding adapter %wZ\n", &AdapterContext->DeviceName);
+
+ /* FIXME: We don't do anything with outstanding reads */
+
/* Remove the adapter context from the global list */
KeAcquireSpinLock(&GlobalAdapterListLock, &OldIrql);
RemoveEntryList(&AdapterContext->ListEntry);
KeReleaseSpinLock(&GlobalAdapterListLock, OldIrql);
+
+ /* Free the device name string */
+ RtlFreeUnicodeString(&AdapterContext->DeviceName);
/* Invalidate all handles to this adapter */
- CurrentOpenEntry = AdapterContext->OpenEntryList.Flink;
- while (CurrentOpenEntry != &AdapterContext->OpenEntryList)
+ CurrentEntry = AdapterContext->OpenEntryList.Flink;
+ while (CurrentEntry != &AdapterContext->OpenEntryList)
{
- OpenEntry = CONTAINING_RECORD(CurrentOpenEntry, NDISUIO_OPEN_ENTRY, ListEntry);
+ OpenEntry = CONTAINING_RECORD(CurrentEntry, NDISUIO_OPEN_ENTRY, ListEntry);
/* Make sure the entry is sane */
ASSERT(OpenEntry->FileObject);
OpenEntry->FileObject->FsContext2 = NULL;
/* Move to the next entry */
- CurrentOpenEntry = CurrentOpenEntry->Flink;
+ CurrentEntry = CurrentEntry->Flink;
/* Free the open entry */
ExFreePool(OpenEntry);
/* If this fails, we have a refcount mismatch somewhere */
ASSERT(AdapterContext->OpenCount == 0);
+ /* Free all pending packet entries */
+ CurrentEntry = AdapterContext->PacketList.Flink;
+ while (CurrentEntry != &AdapterContext->PacketList)
+ {
+ PacketEntry = CONTAINING_RECORD(CurrentEntry, NDISUIO_PACKET_ENTRY, ListEntry);
+
+ /* Move to the next entry */
+ CurrentEntry = CurrentEntry->Flink;
+
+ /* Free the packet entry */
+ ExFreePool(PacketEntry);
+ }
+
/* Send the close request */
NdisCloseAdapter(&Status,
AdapterContext->BindingHandle);
UINT SelectedMedium;
NDIS_STATUS Status;
+ DPRINT("Binding adapter %wZ\n", &AdapterContext->DeviceName);
+
/* Allocate the adapter context */
AdapterContext = ExAllocatePool(NonPagedPool, sizeof(*AdapterContext));
if (!AdapterContext)
return NDIS_STATUS_RESOURCES;
}
+ /* Copy the device name into the adapter context */
RtlCopyMemory(AdapterContext->DeviceName.Buffer, DeviceName->Buffer, DeviceName->Length);
-
+
+ /* Create the buffer pool */
+ NdisAllocateBufferPool(&Status,
+ &AdapterContext->BufferPoolHandle,
+ 50);
+ if (Status != NDIS_STATUS_SUCCESS)
+ {
+ DPRINT1("Failed to allocate buffer pool with status 0x%x\n", Status);
+ RtlFreeUnicodeString(&AdapterContext->DeviceName);
+ ExFreePool(AdapterContext);
+ return Status;
+ }
+
+ /* Create the packet pool */
+ NdisAllocatePacketPool(&Status,
+ &AdapterContext->PacketPoolHandle,
+ 25,
+ PROTOCOL_RESERVED_SIZE_IN_PACKET);
+ if (Status != NDIS_STATUS_SUCCESS)
+ {
+ DPRINT1("Failed to allocate packet pool with status 0x%x\n", Status);
+ NdisFreeBufferPool(AdapterContext->BufferPoolHandle);
+ RtlFreeUnicodeString(&AdapterContext->DeviceName);
+ ExFreePool(AdapterContext);
+ return Status;
+ }
+
/* Send the open request */
NdisOpenAdapter(&Status,
&OpenErrorStatus,
&AdapterContext->BindingHandle,
&SelectedMedium,
- SupportedMedia,
- sizeof(SupportedMedia),
+ &SupportedMedia[0],
+ 1,
GlobalProtocolHandle,
AdapterContext,
DeviceName,
if (Status != NDIS_STATUS_SUCCESS)
{
DPRINT1("Failed to open adapter for bind with status 0x%x\n", Status);
+ NdisFreePacketPool(AdapterContext->PacketPoolHandle);
+ NdisFreeBufferPool(AdapterContext->BufferPoolHandle);
+ RtlFreeUnicodeString(&AdapterContext->DeviceName);
ExFreePool(AdapterContext);
return Status;
}