2012-01-02 21:38:57 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS NDIS User I/O driver
|
|
|
|
* FILE: protocol.c
|
|
|
|
* PURPOSE: Protocol stuff
|
|
|
|
* PROGRAMMERS: Cameron Gutman (cameron.gutman@reactos.org)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ndisuio.h"
|
|
|
|
|
2012-01-07 05:54:14 +00:00
|
|
|
#define NDEBUG
|
2012-01-02 21:38:57 +00:00
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
NduOpenAdapterComplete(NDIS_HANDLE ProtocolBindingContext,
|
|
|
|
NDIS_STATUS Status,
|
|
|
|
NDIS_STATUS OpenStatus)
|
|
|
|
{
|
|
|
|
PNDISUIO_ADAPTER_CONTEXT AdapterContext = ProtocolBindingContext;
|
|
|
|
|
|
|
|
DPRINT("Asynchronous adapter open completed\n");
|
|
|
|
|
|
|
|
/* Store the final status and signal the event */
|
|
|
|
AdapterContext->AsyncStatus = Status;
|
|
|
|
KeSetEvent(&AdapterContext->AsyncEvent, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
NduCloseAdapterComplete(NDIS_HANDLE ProtocolBindingContext,
|
|
|
|
NDIS_STATUS Status)
|
|
|
|
{
|
|
|
|
PNDISUIO_ADAPTER_CONTEXT AdapterContext = ProtocolBindingContext;
|
|
|
|
|
|
|
|
DPRINT("Asynchronous adapter close completed\n");
|
|
|
|
|
|
|
|
/* Store the final status and signal the event */
|
|
|
|
AdapterContext->AsyncStatus = Status;
|
|
|
|
KeSetEvent(&AdapterContext->AsyncEvent, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
|
2014-08-22 19:13:31 +00:00
|
|
|
NDIS_STATUS
|
|
|
|
NTAPI
|
|
|
|
NduNetPnPEvent(NDIS_HANDLE ProtocolBindingContext,
|
|
|
|
PNET_PNP_EVENT NetPnPEvent)
|
|
|
|
{
|
2015-09-18 13:33:05 +00:00
|
|
|
PNDIS_DEVICE_POWER_STATE PowerState;
|
|
|
|
|
2014-08-22 19:13:31 +00:00
|
|
|
DPRINT("NetPnPEvent\n");
|
|
|
|
|
|
|
|
switch (NetPnPEvent->NetEvent)
|
|
|
|
{
|
|
|
|
case NetEventQueryRemoveDevice:
|
|
|
|
/* Nothing to do */
|
2015-09-18 13:33:05 +00:00
|
|
|
DPRINT1("NetPnPEvent: QueryRemoveDevice\n");
|
|
|
|
return NDIS_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
case NetEventSetPower:
|
|
|
|
ASSERT(NetPnPEvent->BufferLength >= sizeof(*PowerState));
|
|
|
|
|
|
|
|
PowerState = NetPnPEvent->Buffer;
|
|
|
|
switch (*PowerState)
|
|
|
|
{
|
|
|
|
case NdisDeviceStateD0:
|
|
|
|
DPRINT1("NetPnPEvent: SetPower D0\n");
|
|
|
|
return NDIS_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DPRINT1("NetPnPEvent: SetPower state %d not supported\n", *PowerState);
|
|
|
|
return NDIS_STATUS_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NetEventQueryPower:
|
|
|
|
DPRINT1("NetPnPEvent: QueryPower\n");
|
|
|
|
return NDIS_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
case NetEventCancelRemoveDevice:
|
|
|
|
DPRINT1("NetPnPEvent: CancelRemoveDevice\n");
|
|
|
|
return NDIS_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
case NetEventReconfigure:
|
|
|
|
DPRINT1("NetPnPEvent: Reconfigure\n");
|
|
|
|
return NDIS_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
case NetEventBindList:
|
|
|
|
DPRINT1("NetPnPEvent: BindList\n");
|
|
|
|
return NDIS_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
case NetEventBindsComplete:
|
2017-09-23 13:34:50 +00:00
|
|
|
DPRINT("NetPnPEvent: BindsComplete\n");
|
2015-09-18 13:33:05 +00:00
|
|
|
return NDIS_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
case NetEventPnPCapabilities:
|
|
|
|
DPRINT1("NetPnPEvent: PnPCapabilities\n");
|
2014-08-22 19:13:31 +00:00
|
|
|
return NDIS_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DPRINT1("NetPnPEvent unimplemented for net event 0x%x\n", NetPnPEvent->NetEvent);
|
|
|
|
return NDIS_STATUS_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-02 21:38:57 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
NduSendComplete(NDIS_HANDLE ProtocolBindingContext,
|
|
|
|
PNDIS_PACKET Packet,
|
|
|
|
NDIS_STATUS Status)
|
|
|
|
{
|
2012-01-03 17:54:01 +00:00
|
|
|
PNDISUIO_ADAPTER_CONTEXT AdapterContext = ProtocolBindingContext;
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
DPRINT("Asynchronous adapter send completed\n");
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
/* Store the final status and signal the event */
|
|
|
|
AdapterContext->AsyncStatus = Status;
|
|
|
|
KeSetEvent(&AdapterContext->AsyncEvent, IO_NO_INCREMENT, FALSE);
|
2012-01-02 21:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
NduTransferDataComplete(NDIS_HANDLE ProtocolBindingContext,
|
|
|
|
PNDIS_PACKET Packet,
|
|
|
|
NDIS_STATUS Status,
|
|
|
|
UINT BytesTransferred)
|
|
|
|
{
|
2012-01-03 17:54:01 +00:00
|
|
|
PNDISUIO_ADAPTER_CONTEXT AdapterContext = ProtocolBindingContext;
|
|
|
|
|
|
|
|
DPRINT("Asynchronous adapter transfer completed\n");
|
|
|
|
|
|
|
|
/* Store the final status and signal the event */
|
|
|
|
AdapterContext->AsyncStatus = Status;
|
|
|
|
KeSetEvent(&AdapterContext->AsyncEvent, IO_NO_INCREMENT, FALSE);
|
2012-01-02 21:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
NduResetComplete(NDIS_HANDLE ProtocolBindingContext,
|
|
|
|
NDIS_STATUS Status)
|
|
|
|
{
|
|
|
|
PNDISUIO_ADAPTER_CONTEXT AdapterContext = ProtocolBindingContext;
|
|
|
|
|
|
|
|
DPRINT("Asynchronous adapter reset completed\n");
|
|
|
|
|
|
|
|
/* Store the final status and signal the event */
|
|
|
|
AdapterContext->AsyncStatus = Status;
|
|
|
|
KeSetEvent(&AdapterContext->AsyncEvent, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
NduRequestComplete(NDIS_HANDLE ProtocolBindingContext,
|
|
|
|
PNDIS_REQUEST NdisRequest,
|
|
|
|
NDIS_STATUS Status)
|
|
|
|
{
|
|
|
|
PNDISUIO_ADAPTER_CONTEXT AdapterContext = ProtocolBindingContext;
|
|
|
|
|
|
|
|
DPRINT("Asynchronous adapter request completed\n");
|
|
|
|
|
|
|
|
/* Store the final status and signal the event */
|
|
|
|
AdapterContext->AsyncStatus = Status;
|
|
|
|
KeSetEvent(&AdapterContext->AsyncEvent, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
NDIS_STATUS
|
|
|
|
NTAPI
|
|
|
|
NduReceive(NDIS_HANDLE ProtocolBindingContext,
|
|
|
|
NDIS_HANDLE MacReceiveContext,
|
|
|
|
PVOID HeaderBuffer,
|
|
|
|
UINT HeaderBufferSize,
|
|
|
|
PVOID LookAheadBuffer,
|
|
|
|
UINT LookaheadBufferSize,
|
|
|
|
UINT PacketSize)
|
|
|
|
{
|
2012-01-03 17:54:01 +00:00
|
|
|
PNDISUIO_ADAPTER_CONTEXT AdapterContext = ProtocolBindingContext;
|
2012-01-06 05:59:56 +00:00
|
|
|
PNDISUIO_PACKET_ENTRY PacketEntry;
|
2012-01-03 17:54:01 +00:00
|
|
|
PVOID PacketBuffer;
|
|
|
|
PNDIS_PACKET Packet;
|
|
|
|
NDIS_STATUS Status;
|
2012-01-06 05:59:56 +00:00
|
|
|
UINT BytesTransferred;
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-10 01:52:18 +00:00
|
|
|
DPRINT("Received a %d byte packet\n", PacketSize);
|
2012-01-07 05:54:14 +00:00
|
|
|
|
2012-01-06 19:43:15 +00:00
|
|
|
/* Discard if nobody is waiting for it */
|
|
|
|
if (AdapterContext->OpenCount == 0)
|
|
|
|
return NDIS_STATUS_NOT_ACCEPTED;
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
/* Allocate a buffer to hold the packet data and header */
|
2012-01-10 01:52:18 +00:00
|
|
|
PacketBuffer = ExAllocatePool(NonPagedPool, PacketSize + HeaderBufferSize);
|
2012-01-03 17:54:01 +00:00
|
|
|
if (!PacketBuffer)
|
|
|
|
return NDIS_STATUS_NOT_ACCEPTED;
|
|
|
|
|
|
|
|
/* Allocate the packet descriptor and buffer */
|
2012-01-06 19:43:15 +00:00
|
|
|
Packet = CreatePacketFromPoolBuffer(AdapterContext,
|
|
|
|
(PUCHAR)PacketBuffer + HeaderBufferSize,
|
2012-01-03 17:54:01 +00:00
|
|
|
PacketSize);
|
|
|
|
if (!Packet)
|
|
|
|
{
|
|
|
|
ExFreePool(PacketBuffer);
|
|
|
|
return NDIS_STATUS_NOT_ACCEPTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transfer the packet data into our data buffer */
|
2012-01-10 01:52:18 +00:00
|
|
|
if (LookaheadBufferSize == PacketSize)
|
2012-01-03 17:54:01 +00:00
|
|
|
{
|
2012-01-10 01:52:18 +00:00
|
|
|
NdisCopyLookaheadData((PVOID)((PUCHAR)PacketBuffer + HeaderBufferSize),
|
|
|
|
LookAheadBuffer,
|
|
|
|
PacketSize,
|
|
|
|
AdapterContext->MacOptions);
|
|
|
|
BytesTransferred = PacketSize;
|
2012-01-03 17:54:01 +00:00
|
|
|
}
|
2012-01-10 01:52:18 +00:00
|
|
|
else
|
2012-01-03 17:54:01 +00:00
|
|
|
{
|
2012-01-10 01:52:18 +00:00
|
|
|
NdisTransferData(&Status,
|
|
|
|
AdapterContext->BindingHandle,
|
|
|
|
MacReceiveContext,
|
|
|
|
0,
|
|
|
|
PacketSize,
|
|
|
|
Packet,
|
|
|
|
&BytesTransferred);
|
|
|
|
if (Status == NDIS_STATUS_PENDING)
|
|
|
|
{
|
|
|
|
KeWaitForSingleObject(&AdapterContext->AsyncEvent,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
Status = AdapterContext->AsyncStatus;
|
|
|
|
}
|
|
|
|
if (Status != NDIS_STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
DPRINT1("Failed to transfer data with status 0x%x\n", Status);
|
2015-04-09 10:36:35 +00:00
|
|
|
CleanupAndFreePacket(Packet, FALSE);
|
2015-04-09 09:50:44 +00:00
|
|
|
ExFreePool(PacketBuffer);
|
2012-01-10 01:52:18 +00:00
|
|
|
return NDIS_STATUS_NOT_ACCEPTED;
|
|
|
|
}
|
2012-01-03 17:54:01 +00:00
|
|
|
}
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
/* Copy the header data */
|
|
|
|
RtlCopyMemory(PacketBuffer, HeaderBuffer, HeaderBufferSize);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
|
|
|
/* Free the packet descriptor and buffers
|
2012-01-03 17:54:01 +00:00
|
|
|
but not the pool because we still need it */
|
|
|
|
CleanupAndFreePacket(Packet, FALSE);
|
|
|
|
|
2012-01-07 05:54:14 +00:00
|
|
|
/* Allocate a packet entry from pool */
|
|
|
|
PacketEntry = ExAllocatePool(NonPagedPool, sizeof(NDISUIO_PACKET_ENTRY) + BytesTransferred + HeaderBufferSize - 1);
|
2012-01-03 17:54:01 +00:00
|
|
|
if (!PacketEntry)
|
|
|
|
{
|
|
|
|
ExFreePool(PacketBuffer);
|
|
|
|
return NDIS_STATUS_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the packet entry and copy in packet data */
|
|
|
|
PacketEntry->PacketLength = BytesTransferred + HeaderBufferSize;
|
2012-01-10 01:52:18 +00:00
|
|
|
RtlCopyMemory(PacketEntry->PacketData, PacketBuffer, PacketEntry->PacketLength);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-07 05:54:14 +00:00
|
|
|
/* Free the old buffer */
|
2012-01-03 17:54:01 +00:00
|
|
|
ExFreePool(PacketBuffer);
|
|
|
|
|
|
|
|
/* Insert the packet on the adapter's packet list */
|
|
|
|
ExInterlockedInsertTailList(&AdapterContext->PacketList,
|
|
|
|
&PacketEntry->ListEntry,
|
|
|
|
&AdapterContext->Spinlock);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-03 17:54:01 +00:00
|
|
|
/* Signal the read event */
|
|
|
|
KeSetEvent(&AdapterContext->PacketReadEvent,
|
|
|
|
IO_NETWORK_INCREMENT,
|
|
|
|
FALSE);
|
|
|
|
|
|
|
|
return NDIS_STATUS_SUCCESS;
|
2012-01-02 21:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
NduReceiveComplete(NDIS_HANDLE ProtocolBindingContext)
|
|
|
|
{
|
|
|
|
/* No op */
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
NduStatus(NDIS_HANDLE ProtocolBindingContext,
|
|
|
|
NDIS_STATUS GeneralStatus,
|
|
|
|
PVOID StatusBuffer,
|
|
|
|
UINT StatusBufferSize)
|
|
|
|
{
|
|
|
|
/* FIXME: Implement status tracking */
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
NduStatusComplete(NDIS_HANDLE ProtocolBindingContext)
|
|
|
|
{
|
|
|
|
/* FIXME: Implement status tracking */
|
|
|
|
}
|
|
|
|
|
2012-01-06 05:59:56 +00:00
|
|
|
static
|
2012-01-02 21:38:57 +00:00
|
|
|
NDIS_STATUS
|
|
|
|
UnbindAdapterByContext(PNDISUIO_ADAPTER_CONTEXT AdapterContext)
|
|
|
|
{
|
|
|
|
KIRQL OldIrql;
|
2012-01-06 19:43:15 +00:00
|
|
|
PLIST_ENTRY CurrentEntry;
|
2012-01-02 21:38:57 +00:00
|
|
|
PNDISUIO_OPEN_ENTRY OpenEntry;
|
2012-01-06 19:43:15 +00:00
|
|
|
PNDISUIO_PACKET_ENTRY PacketEntry;
|
2012-01-06 05:59:56 +00:00
|
|
|
NDIS_STATUS Status;
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-06 19:43:15 +00:00
|
|
|
DPRINT("Unbinding adapter %wZ\n", &AdapterContext->DeviceName);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-06 19:43:15 +00:00
|
|
|
/* FIXME: We don't do anything with outstanding reads */
|
|
|
|
|
2012-01-03 16:08:24 +00:00
|
|
|
/* Remove the adapter context from the global list */
|
|
|
|
KeAcquireSpinLock(&GlobalAdapterListLock, &OldIrql);
|
|
|
|
RemoveEntryList(&AdapterContext->ListEntry);
|
|
|
|
KeReleaseSpinLock(&GlobalAdapterListLock, OldIrql);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-06 19:43:15 +00:00
|
|
|
/* Free the device name string */
|
|
|
|
RtlFreeUnicodeString(&AdapterContext->DeviceName);
|
2012-01-03 16:08:24 +00:00
|
|
|
|
2012-01-02 21:38:57 +00:00
|
|
|
/* Invalidate all handles to this adapter */
|
2012-01-06 19:43:15 +00:00
|
|
|
CurrentEntry = AdapterContext->OpenEntryList.Flink;
|
|
|
|
while (CurrentEntry != &AdapterContext->OpenEntryList)
|
2012-01-02 21:38:57 +00:00
|
|
|
{
|
2012-01-06 19:43:15 +00:00
|
|
|
OpenEntry = CONTAINING_RECORD(CurrentEntry, NDISUIO_OPEN_ENTRY, ListEntry);
|
2012-01-02 21:38:57 +00:00
|
|
|
|
|
|
|
/* Make sure the entry is sane */
|
|
|
|
ASSERT(OpenEntry->FileObject);
|
|
|
|
|
|
|
|
/* Remove the adapter context pointer */
|
|
|
|
ASSERT(AdapterContext == OpenEntry->FileObject->FsContext);
|
|
|
|
OpenEntry->FileObject->FsContext = NULL;
|
|
|
|
AdapterContext->OpenCount--;
|
|
|
|
|
|
|
|
/* Remove the open entry pointer */
|
|
|
|
ASSERT(OpenEntry == OpenEntry->FileObject->FsContext2);
|
|
|
|
OpenEntry->FileObject->FsContext2 = NULL;
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-02 21:38:57 +00:00
|
|
|
/* Move to the next entry */
|
2012-01-06 19:43:15 +00:00
|
|
|
CurrentEntry = CurrentEntry->Flink;
|
2012-01-02 21:38:57 +00:00
|
|
|
|
|
|
|
/* Free the open entry */
|
|
|
|
ExFreePool(OpenEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this fails, we have a refcount mismatch somewhere */
|
|
|
|
ASSERT(AdapterContext->OpenCount == 0);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-06 19:43:15 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-02 21:38:57 +00:00
|
|
|
/* Send the close request */
|
2012-01-06 05:59:56 +00:00
|
|
|
NdisCloseAdapter(&Status,
|
2012-01-02 21:38:57 +00:00
|
|
|
AdapterContext->BindingHandle);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-02 21:38:57 +00:00
|
|
|
/* Wait for a pending close */
|
2012-01-06 05:59:56 +00:00
|
|
|
if (Status == NDIS_STATUS_PENDING)
|
2012-01-02 21:38:57 +00:00
|
|
|
{
|
|
|
|
KeWaitForSingleObject(&AdapterContext->AsyncEvent,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
2012-01-06 05:59:56 +00:00
|
|
|
Status = AdapterContext->AsyncStatus;
|
2012-01-02 21:38:57 +00:00
|
|
|
}
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-02 21:38:57 +00:00
|
|
|
/* Free the context */
|
|
|
|
ExFreePool(AdapterContext);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-06 05:59:56 +00:00
|
|
|
return Status;
|
2012-01-02 21:38:57 +00:00
|
|
|
}
|
|
|
|
|
2012-01-06 05:59:56 +00:00
|
|
|
static
|
2012-01-02 21:38:57 +00:00
|
|
|
NDIS_STATUS
|
2012-01-06 05:59:56 +00:00
|
|
|
BindAdapterByName(PNDIS_STRING DeviceName)
|
2012-01-02 21:38:57 +00:00
|
|
|
{
|
|
|
|
NDIS_STATUS OpenErrorStatus;
|
|
|
|
PNDISUIO_ADAPTER_CONTEXT AdapterContext;
|
2012-01-07 05:54:14 +00:00
|
|
|
NDIS_MEDIUM SupportedMedia[1] = {NdisMedium802_3};
|
2012-01-02 21:38:57 +00:00
|
|
|
UINT SelectedMedium;
|
|
|
|
NDIS_STATUS Status;
|
2012-01-10 01:52:18 +00:00
|
|
|
NDIS_REQUEST Request;
|
2012-01-07 05:54:14 +00:00
|
|
|
|
2012-01-02 21:38:57 +00:00
|
|
|
/* Allocate the adapter context */
|
|
|
|
AdapterContext = ExAllocatePool(NonPagedPool, sizeof(*AdapterContext));
|
|
|
|
if (!AdapterContext)
|
|
|
|
{
|
|
|
|
return NDIS_STATUS_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up the adapter context */
|
|
|
|
RtlZeroMemory(AdapterContext, sizeof(*AdapterContext));
|
|
|
|
KeInitializeEvent(&AdapterContext->AsyncEvent, SynchronizationEvent, FALSE);
|
2012-01-03 17:54:01 +00:00
|
|
|
KeInitializeEvent(&AdapterContext->PacketReadEvent, SynchronizationEvent, FALSE);
|
2012-01-02 21:38:57 +00:00
|
|
|
KeInitializeSpinLock(&AdapterContext->Spinlock);
|
|
|
|
InitializeListHead(&AdapterContext->PacketList);
|
|
|
|
InitializeListHead(&AdapterContext->OpenEntryList);
|
2012-01-03 16:08:24 +00:00
|
|
|
AdapterContext->OpenCount = 0;
|
2012-01-02 21:38:57 +00:00
|
|
|
|
2012-01-06 05:59:56 +00:00
|
|
|
AdapterContext->DeviceName.Length =
|
|
|
|
AdapterContext->DeviceName.MaximumLength = DeviceName->Length;
|
|
|
|
AdapterContext->DeviceName.Buffer = ExAllocatePool(NonPagedPool, DeviceName->Length);
|
|
|
|
if (!AdapterContext->DeviceName.Buffer)
|
|
|
|
{
|
|
|
|
ExFreePool(AdapterContext);
|
|
|
|
return NDIS_STATUS_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2012-01-06 19:43:15 +00:00
|
|
|
/* Copy the device name into the adapter context */
|
2012-01-06 05:59:56 +00:00
|
|
|
RtlCopyMemory(AdapterContext->DeviceName.Buffer, DeviceName->Buffer, DeviceName->Length);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-07 05:54:14 +00:00
|
|
|
DPRINT("Binding adapter %wZ\n", &AdapterContext->DeviceName);
|
2012-01-06 19:43:15 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2012-01-02 21:38:57 +00:00
|
|
|
/* Send the open request */
|
|
|
|
NdisOpenAdapter(&Status,
|
|
|
|
&OpenErrorStatus,
|
|
|
|
&AdapterContext->BindingHandle,
|
|
|
|
&SelectedMedium,
|
2012-01-07 05:54:14 +00:00
|
|
|
SupportedMedia,
|
2012-01-06 19:43:15 +00:00
|
|
|
1,
|
2012-01-02 21:38:57 +00:00
|
|
|
GlobalProtocolHandle,
|
|
|
|
AdapterContext,
|
|
|
|
DeviceName,
|
|
|
|
0,
|
|
|
|
NULL);
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-02 21:38:57 +00:00
|
|
|
/* Wait for a pending open */
|
|
|
|
if (Status == NDIS_STATUS_PENDING)
|
|
|
|
{
|
|
|
|
KeWaitForSingleObject(&AdapterContext->AsyncEvent,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
Status = AdapterContext->AsyncStatus;
|
|
|
|
}
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-02 21:38:57 +00:00
|
|
|
/* Check the final status */
|
|
|
|
if (Status != NDIS_STATUS_SUCCESS)
|
|
|
|
{
|
2012-01-06 05:59:56 +00:00
|
|
|
DPRINT1("Failed to open adapter for bind with status 0x%x\n", Status);
|
2012-01-06 19:43:15 +00:00
|
|
|
NdisFreePacketPool(AdapterContext->PacketPoolHandle);
|
|
|
|
NdisFreeBufferPool(AdapterContext->BufferPoolHandle);
|
|
|
|
RtlFreeUnicodeString(&AdapterContext->DeviceName);
|
2012-01-10 01:52:18 +00:00
|
|
|
ExFreePool(AdapterContext);
|
|
|
|
return Status;
|
|
|
|
}
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-10 01:52:18 +00:00
|
|
|
/* Get the MAC options */
|
|
|
|
Request.RequestType = NdisRequestQueryInformation;
|
|
|
|
Request.DATA.QUERY_INFORMATION.Oid = OID_GEN_MAC_OPTIONS;
|
|
|
|
Request.DATA.QUERY_INFORMATION.InformationBuffer = &AdapterContext->MacOptions;
|
|
|
|
Request.DATA.QUERY_INFORMATION.InformationBufferLength = sizeof(ULONG);
|
|
|
|
NdisRequest(&Status,
|
|
|
|
AdapterContext->BindingHandle,
|
|
|
|
&Request);
|
|
|
|
|
|
|
|
/* Wait for a pending request */
|
|
|
|
if (Status == NDIS_STATUS_PENDING)
|
|
|
|
{
|
|
|
|
KeWaitForSingleObject(&AdapterContext->AsyncEvent,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
Status = AdapterContext->AsyncStatus;
|
|
|
|
}
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-10 01:52:18 +00:00
|
|
|
/* Check the final status */
|
|
|
|
if (Status != NDIS_STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
NDIS_STATUS CloseStatus;
|
|
|
|
|
|
|
|
DPRINT1("Failed to get MAC options with status 0x%x\n", Status);
|
|
|
|
|
|
|
|
NdisCloseAdapter(&CloseStatus,
|
|
|
|
AdapterContext->BindingHandle);
|
|
|
|
if (CloseStatus == NDIS_STATUS_PENDING)
|
|
|
|
{
|
|
|
|
KeWaitForSingleObject(&AdapterContext->AsyncEvent,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
NdisFreePacketPool(AdapterContext->PacketPoolHandle);
|
|
|
|
NdisFreeBufferPool(AdapterContext->BufferPoolHandle);
|
|
|
|
RtlFreeUnicodeString(&AdapterContext->DeviceName);
|
2012-01-02 21:38:57 +00:00
|
|
|
ExFreePool(AdapterContext);
|
2012-01-06 05:59:56 +00:00
|
|
|
return Status;
|
2012-01-02 21:38:57 +00:00
|
|
|
}
|
2021-06-11 12:29:21 +00:00
|
|
|
|
2012-01-02 21:38:57 +00:00
|
|
|
/* Add the adapter context to the global list */
|
|
|
|
ExInterlockedInsertTailList(&GlobalAdapterList,
|
|
|
|
&AdapterContext->ListEntry,
|
|
|
|
&GlobalAdapterListLock);
|
2012-01-06 05:59:56 +00:00
|
|
|
|
2012-01-02 21:38:57 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
NduBindAdapter(PNDIS_STATUS Status,
|
|
|
|
NDIS_HANDLE BindContext,
|
|
|
|
PNDIS_STRING DeviceName,
|
|
|
|
PVOID SystemSpecific1,
|
|
|
|
PVOID SystemSpecific2)
|
|
|
|
{
|
2012-01-03 16:08:24 +00:00
|
|
|
/* Use our helper function to create a context for this adapter */
|
|
|
|
*Status = BindAdapterByName(DeviceName);
|
2012-01-02 21:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
NduUnbindAdapter(PNDIS_STATUS Status,
|
|
|
|
NDIS_HANDLE ProtocolBindingContext,
|
|
|
|
NDIS_HANDLE UnbindContext)
|
|
|
|
{
|
2021-06-11 12:29:21 +00:00
|
|
|
/* This is forced unbind. UnbindAdapterByContext() will take care of
|
2012-01-02 21:38:57 +00:00
|
|
|
* invalidating file handles pointer to this adapter for us */
|
|
|
|
*Status = UnbindAdapterByContext(ProtocolBindingContext);
|
|
|
|
}
|