[TCPIP][IP] Use an ERESOURCE as mutex for address files & connection end points

Spinlocks are not reentrant (and this is done a lot), using them forces us to have
an horrible hack in the kernel, which unschedules threads which are at DISPATCH_LEVEL
thus allowing another thread to take ownership of the spinlock while the unscheduled
thread should already hold it.

CORE-6473
This commit is contained in:
Jérôme Gardou 2021-05-21 09:36:27 +02:00 committed by Jérôme Gardou
parent 10e48faf1d
commit 27fcfe66a2
14 changed files with 326 additions and 343 deletions

View file

@ -201,13 +201,13 @@ VOID
FlushConnectQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status); FlushConnectQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status);
VOID VOID
FlushReceiveQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status, const BOOLEAN interlocked); FlushReceiveQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status);
VOID VOID
FlushSendQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status, const BOOLEAN interlocked); FlushSendQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status);
VOID VOID
FlushShutdownQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status, const BOOLEAN interlocked); FlushShutdownQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status);
VOID VOID
FlushAllQueues(PCONNECTION_ENDPOINT Connection, NTSTATUS Status); FlushAllQueues(PCONNECTION_ENDPOINT Connection, NTSTATUS Status);

View file

@ -29,44 +29,26 @@
} }
/* /*
* VOID LockObject(PVOID Object, PKIRQL OldIrql) * VOID LockObject(PVOID Object)
*/ */
#define LockObject(Object, Irql) \ #define LockObject(Object) do \
{ \ { \
ReferenceObject(Object); \ ReferenceObject(Object); \
KeAcquireSpinLock(&((Object)->Lock), Irql); \ KeEnterCriticalRegion(); \
memcpy(&(Object)->OldIrql, Irql, sizeof(KIRQL)); \ ExAcquireResourceExclusiveLite(&(Object)->Resource, TRUE); \
} } while(0)
/* /*
* VOID LockObjectAtDpcLevel(PVOID Object) * VOID UnlockObject(PVOID Object)
*/ */
#define LockObjectAtDpcLevel(Object) \ #define UnlockObject(Object) do \
{ \
ReferenceObject(Object); \
KeAcquireSpinLockAtDpcLevel(&((Object)->Lock)); \
(Object)->OldIrql = DISPATCH_LEVEL; \
}
/*
* VOID UnlockObject(PVOID Object, KIRQL OldIrql)
*/
#define UnlockObject(Object, OldIrql) \
{ \ { \
KeReleaseSpinLock(&((Object)->Lock), OldIrql); \ ExReleaseResourceLite(&(Object)->Resource); \
KeLeaveCriticalRegion(); \
DereferenceObject(Object); \ DereferenceObject(Object); \
} } while(0)
/*
* VOID UnlockObjectFromDpcLevel(PVOID Object)
*/
#define UnlockObjectFromDpcLevel(Object) \
{ \
KeReleaseSpinLockFromDpcLevel(&((Object)->Lock)); \
DereferenceObject(Object); \
}
#define ASSERT_TCPIP_OBJECT_LOCKED(Object) ASSERT(ExIsResourceAcquiredExclusiveLite(&(Object)->Resource))
#include <ip.h> #include <ip.h>
@ -131,8 +113,7 @@ typedef struct _ADDRESS_FILE {
LIST_ENTRY ListEntry; /* Entry on list */ LIST_ENTRY ListEntry; /* Entry on list */
LONG RefCount; /* Reference count */ LONG RefCount; /* Reference count */
OBJECT_FREE_ROUTINE Free; /* Routine to use to free resources for the object */ OBJECT_FREE_ROUTINE Free; /* Routine to use to free resources for the object */
KSPIN_LOCK Lock; /* Spin lock to manipulate this structure */ ERESOURCE Resource; /* Resource to manipulate this structure */
KIRQL OldIrql; /* Currently not used */
IP_ADDRESS Address; /* Address of this address file */ IP_ADDRESS Address; /* Address of this address file */
USHORT Family; /* Address family */ USHORT Family; /* Address family */
USHORT Protocol; /* Protocol number */ USHORT Protocol; /* Protocol number */
@ -142,7 +123,6 @@ typedef struct _ADDRESS_FILE {
UINT DF; /* Don't fragment */ UINT DF; /* Don't fragment */
UINT BCast; /* Receive broadcast packets */ UINT BCast; /* Receive broadcast packets */
UINT HeaderIncl; /* Include header in RawIP packets */ UINT HeaderIncl; /* Include header in RawIP packets */
WORK_QUEUE_ITEM WorkItem; /* Work queue item handle */
DATAGRAM_COMPLETION_ROUTINE Complete; /* Completion routine for delete request */ DATAGRAM_COMPLETION_ROUTINE Complete; /* Completion routine for delete request */
PVOID Context; /* Delete request context */ PVOID Context; /* Delete request context */
DATAGRAM_SEND_ROUTINE Send; /* Routine to send a datagram */ DATAGRAM_SEND_ROUTINE Send; /* Routine to send a datagram */
@ -262,8 +242,7 @@ typedef struct _CONNECTION_ENDPOINT {
LIST_ENTRY ListEntry; /* Entry on list */ LIST_ENTRY ListEntry; /* Entry on list */
LONG RefCount; /* Reference count */ LONG RefCount; /* Reference count */
OBJECT_FREE_ROUTINE Free; /* Routine to use to free resources for the object */ OBJECT_FREE_ROUTINE Free; /* Routine to use to free resources for the object */
KSPIN_LOCK Lock; /* Spin lock to protect this structure */ ERESOURCE Resource; /* The lock protecting this structure */
KIRQL OldIrql; /* The old irql is stored here for use in HandleSignalledConnection */
PVOID ClientContext; /* Pointer to client context information */ PVOID ClientContext; /* Pointer to client context information */
PADDRESS_FILE AddressFile; /* Associated address file object (NULL if none) */ PADDRESS_FILE AddressFile; /* Associated address file object (NULL if none) */
@ -279,6 +258,7 @@ typedef struct _CONNECTION_ENDPOINT {
/* Disconnect Timer */ /* Disconnect Timer */
KTIMER DisconnectTimer; KTIMER DisconnectTimer;
KDPC DisconnectDpc; KDPC DisconnectDpc;
PIO_WORKITEM DisconnectWorkItem;
/* Socket state */ /* Socket state */
BOOLEAN SendShutdown; BOOLEAN SendShutdown;

View file

@ -13,17 +13,15 @@ TDI_STATUS SetAddressFileInfo(TDIObjectID *ID,
PVOID Buffer, PVOID Buffer,
UINT BufferSize) UINT BufferSize)
{ {
KIRQL OldIrql;
switch (ID->toi_id) switch (ID->toi_id)
{ {
case AO_OPTION_TTL: case AO_OPTION_TTL:
if (BufferSize < sizeof(UINT)) if (BufferSize < sizeof(UINT))
return TDI_INVALID_PARAMETER; return TDI_INVALID_PARAMETER;
LockObject(AddrFile, &OldIrql); LockObject(AddrFile);
AddrFile->TTL = *((PUCHAR)Buffer); AddrFile->TTL = *((PUCHAR)Buffer);
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
return TDI_SUCCESS; return TDI_SUCCESS;
@ -31,9 +29,9 @@ TDI_STATUS SetAddressFileInfo(TDIObjectID *ID,
if (BufferSize < sizeof(UINT)) if (BufferSize < sizeof(UINT))
return TDI_INVALID_PARAMETER; return TDI_INVALID_PARAMETER;
LockObject(AddrFile, &OldIrql); LockObject(AddrFile);
AddrFile->DF = *((PUINT)Buffer); AddrFile->DF = *((PUINT)Buffer);
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
return TDI_SUCCESS; return TDI_SUCCESS;
@ -41,9 +39,9 @@ TDI_STATUS SetAddressFileInfo(TDIObjectID *ID,
if (BufferSize < sizeof(UINT)) if (BufferSize < sizeof(UINT))
return TDI_INVALID_PARAMETER; return TDI_INVALID_PARAMETER;
LockObject(AddrFile, &OldIrql); LockObject(AddrFile);
AddrFile->BCast = *((PUINT)Buffer); AddrFile->BCast = *((PUINT)Buffer);
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
return TDI_SUCCESS; return TDI_SUCCESS;
@ -51,9 +49,9 @@ TDI_STATUS SetAddressFileInfo(TDIObjectID *ID,
if (BufferSize < sizeof(UINT)) if (BufferSize < sizeof(UINT))
return TDI_INVALID_PARAMETER; return TDI_INVALID_PARAMETER;
LockObject(AddrFile, &OldIrql); LockObject(AddrFile);
AddrFile->HeaderIncl = *((PUINT)Buffer); AddrFile->HeaderIncl = *((PUINT)Buffer);
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
return TDI_SUCCESS; return TDI_SUCCESS;

View file

@ -284,7 +284,6 @@ NTSTATUS DispTdiAssociateAddress(
PFILE_OBJECT FileObject; PFILE_OBJECT FileObject;
PADDRESS_FILE AddrFile = NULL; PADDRESS_FILE AddrFile = NULL;
NTSTATUS Status; NTSTATUS Status;
KIRQL OldIrql;
TI_DbgPrint(DEBUG_IRP, ("Called.\n")); TI_DbgPrint(DEBUG_IRP, ("Called.\n"));
@ -319,18 +318,18 @@ NTSTATUS DispTdiAssociateAddress(
return STATUS_INVALID_PARAMETER; return STATUS_INVALID_PARAMETER;
} }
LockObject(Connection, &OldIrql); LockObject(Connection);
if (Connection->AddressFile) { if (Connection->AddressFile) {
ObDereferenceObject(FileObject); ObDereferenceObject(FileObject);
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
TI_DbgPrint(MID_TRACE, ("An address file is already associated.\n")); TI_DbgPrint(MID_TRACE, ("An address file is already associated.\n"));
return STATUS_INVALID_PARAMETER; return STATUS_INVALID_PARAMETER;
} }
if (FileObject->FsContext2 != (PVOID)TDI_TRANSPORT_ADDRESS_FILE) { if (FileObject->FsContext2 != (PVOID)TDI_TRANSPORT_ADDRESS_FILE) {
ObDereferenceObject(FileObject); ObDereferenceObject(FileObject);
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
TI_DbgPrint(MID_TRACE, ("Bad address file object. Magic (0x%X).\n", TI_DbgPrint(MID_TRACE, ("Bad address file object. Magic (0x%X).\n",
FileObject->FsContext2)); FileObject->FsContext2));
return STATUS_INVALID_PARAMETER; return STATUS_INVALID_PARAMETER;
@ -341,20 +340,20 @@ NTSTATUS DispTdiAssociateAddress(
TranContext = FileObject->FsContext; TranContext = FileObject->FsContext;
if (!TranContext) { if (!TranContext) {
ObDereferenceObject(FileObject); ObDereferenceObject(FileObject);
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
TI_DbgPrint(MID_TRACE, ("Bad transport context.\n")); TI_DbgPrint(MID_TRACE, ("Bad transport context.\n"));
return STATUS_INVALID_PARAMETER; return STATUS_INVALID_PARAMETER;
} }
AddrFile = (PADDRESS_FILE)TranContext->Handle.AddressHandle; AddrFile = (PADDRESS_FILE)TranContext->Handle.AddressHandle;
if (!AddrFile) { if (!AddrFile) {
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
ObDereferenceObject(FileObject); ObDereferenceObject(FileObject);
TI_DbgPrint(MID_TRACE, ("No address file object.\n")); TI_DbgPrint(MID_TRACE, ("No address file object.\n"));
return STATUS_INVALID_PARAMETER; return STATUS_INVALID_PARAMETER;
} }
LockObjectAtDpcLevel(AddrFile); LockObject(AddrFile);
ReferenceObject(AddrFile); ReferenceObject(AddrFile);
Connection->AddressFile = AddrFile; Connection->AddressFile = AddrFile;
@ -373,8 +372,8 @@ NTSTATUS DispTdiAssociateAddress(
ObDereferenceObject(FileObject); ObDereferenceObject(FileObject);
UnlockObjectFromDpcLevel(AddrFile); UnlockObject(AddrFile);
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
return STATUS_SUCCESS; return STATUS_SUCCESS;
} }
@ -563,7 +562,6 @@ NTSTATUS DispTdiListen(
PTRANSPORT_CONTEXT TranContext; PTRANSPORT_CONTEXT TranContext;
PIO_STACK_LOCATION IrpSp; PIO_STACK_LOCATION IrpSp;
NTSTATUS Status = STATUS_SUCCESS; NTSTATUS Status = STATUS_SUCCESS;
KIRQL OldIrql;
TI_DbgPrint(DEBUG_IRP, ("Called.\n")); TI_DbgPrint(DEBUG_IRP, ("Called.\n"));
@ -596,17 +594,17 @@ NTSTATUS DispTdiListen(
Irp, Irp,
(PDRIVER_CANCEL)DispCancelListenRequest); (PDRIVER_CANCEL)DispCancelListenRequest);
LockObject(Connection, &OldIrql); LockObject(Connection);
if (Connection->AddressFile == NULL) if (Connection->AddressFile == NULL)
{ {
TI_DbgPrint(MID_TRACE, ("No associated address file\n")); TI_DbgPrint(MID_TRACE, ("No associated address file\n"));
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
Status = STATUS_INVALID_PARAMETER; Status = STATUS_INVALID_PARAMETER;
goto done; goto done;
} }
LockObjectAtDpcLevel(Connection->AddressFile); LockObject(Connection->AddressFile);
/* Listening will require us to create a listening socket and store it in /* Listening will require us to create a listening socket and store it in
* the address file. It will be signalled, and attempt to complete an irp * the address file. It will be signalled, and attempt to complete an irp
@ -647,8 +645,8 @@ NTSTATUS DispTdiListen(
Irp ); Irp );
} }
UnlockObjectFromDpcLevel(Connection->AddressFile); UnlockObject(Connection->AddressFile);
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
done: done:
if (Status != STATUS_PENDING) { if (Status != STATUS_PENDING) {
@ -1116,7 +1114,6 @@ NTSTATUS DispTdiSetEventHandler(PIRP Irp)
PIO_STACK_LOCATION IrpSp; PIO_STACK_LOCATION IrpSp;
PADDRESS_FILE AddrFile; PADDRESS_FILE AddrFile;
NTSTATUS Status; NTSTATUS Status;
KIRQL OldIrql;
TI_DbgPrint(DEBUG_IRP, ("Called.\n")); TI_DbgPrint(DEBUG_IRP, ("Called.\n"));
@ -1139,7 +1136,7 @@ NTSTATUS DispTdiSetEventHandler(PIRP Irp)
Parameters = (PTDI_REQUEST_KERNEL_SET_EVENT)&IrpSp->Parameters; Parameters = (PTDI_REQUEST_KERNEL_SET_EVENT)&IrpSp->Parameters;
Status = STATUS_SUCCESS; Status = STATUS_SUCCESS;
LockObject(AddrFile, &OldIrql); LockObject(AddrFile);
/* Set the event handler. if an event handler is associated with /* Set the event handler. if an event handler is associated with
a specific event, it's flag (RegisteredXxxHandler) is TRUE. a specific event, it's flag (RegisteredXxxHandler) is TRUE.
@ -1260,7 +1257,7 @@ NTSTATUS DispTdiSetEventHandler(PIRP Irp)
Status = STATUS_INVALID_PARAMETER; Status = STATUS_INVALID_PARAMETER;
} }
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
return Status; return Status;
} }

View file

@ -310,7 +310,7 @@ VOID AddrFileFree(
PADDRESS_FILE AddrFile = Object; PADDRESS_FILE AddrFile = Object;
KIRQL OldIrql; KIRQL OldIrql;
PDATAGRAM_RECEIVE_REQUEST ReceiveRequest; PDATAGRAM_RECEIVE_REQUEST ReceiveRequest;
PDATAGRAM_SEND_REQUEST SendRequest; // PDATAGRAM_SEND_REQUEST SendRequest; See WTF below
PLIST_ENTRY CurrentEntry; PLIST_ENTRY CurrentEntry;
TI_DbgPrint(MID_TRACE, ("Called.\n")); TI_DbgPrint(MID_TRACE, ("Called.\n"));
@ -330,20 +330,26 @@ VOID AddrFileFree(
TI_DbgPrint(DEBUG_ADDRFILE, ("Aborting receive requests on AddrFile at (0x%X).\n", AddrFile)); TI_DbgPrint(DEBUG_ADDRFILE, ("Aborting receive requests on AddrFile at (0x%X).\n", AddrFile));
/* Go through pending receive request list and cancel them all */ /* Go through pending receive request list and cancel them all */
while ((CurrentEntry = ExInterlockedRemoveHeadList(&AddrFile->ReceiveQueue, &AddrFile->Lock))) { while (!IsListEmpty(&AddrFile->ReceiveQueue))
{
CurrentEntry = RemoveHeadList(&AddrFile->ReceiveQueue);
ReceiveRequest = CONTAINING_RECORD(CurrentEntry, DATAGRAM_RECEIVE_REQUEST, ListEntry); ReceiveRequest = CONTAINING_RECORD(CurrentEntry, DATAGRAM_RECEIVE_REQUEST, ListEntry);
(*ReceiveRequest->Complete)(ReceiveRequest->Context, STATUS_CANCELLED, 0); (*ReceiveRequest->Complete)(ReceiveRequest->Context, STATUS_CANCELLED, 0);
/* ExFreePoolWithTag(ReceiveRequest, DATAGRAM_RECV_TAG); FIXME: WTF? */ ExFreePoolWithTag(ReceiveRequest, DATAGRAM_RECV_TAG);
} }
TI_DbgPrint(DEBUG_ADDRFILE, ("Aborting send requests on address file at (0x%X).\n", AddrFile)); TI_DbgPrint(DEBUG_ADDRFILE, ("Aborting send requests on address file at (0x%X).\n", AddrFile));
#if 0 /* Biggest WTF. All of this was taken care of above as DATAGRAM_RECEIVE_REQUEST. */
/* Go through pending send request list and cancel them all */ /* Go through pending send request list and cancel them all */
while ((CurrentEntry = ExInterlockedRemoveHeadList(&AddrFile->ReceiveQueue, &AddrFile->Lock))) { while (!IsListEmpty(&AddrFile->ReceiveQueue))
{
CurrentEntry = RemoveHeadList(&AddrFile->ReceiveQueue);
SendRequest = CONTAINING_RECORD(CurrentEntry, DATAGRAM_SEND_REQUEST, ListEntry); SendRequest = CONTAINING_RECORD(CurrentEntry, DATAGRAM_SEND_REQUEST, ListEntry);
(*SendRequest->Complete)(SendRequest->Context, STATUS_CANCELLED, 0); (*SendRequest->Complete)(SendRequest->Context, STATUS_CANCELLED, 0);
ExFreePoolWithTag(SendRequest, DATAGRAM_SEND_TAG); ExFreePoolWithTag(SendRequest, DATAGRAM_SEND_TAG);
} }
#endif
/* Protocol specific handling */ /* Protocol specific handling */
switch (AddrFile->Protocol) { switch (AddrFile->Protocol) {
@ -361,6 +367,8 @@ VOID AddrFileFree(
RemoveEntityByContext(AddrFile); RemoveEntityByContext(AddrFile);
ExDeleteResourceLite(&AddrFile->Resource);
ExFreePoolWithTag(Object, ADDR_FILE_TAG); ExFreePoolWithTag(Object, ADDR_FILE_TAG);
} }
@ -553,7 +561,7 @@ NTSTATUS FileOpenAddress(
InitializeListHead(&AddrFile->TransmitQueue); InitializeListHead(&AddrFile->TransmitQueue);
/* Initialize spin lock that protects the address file object */ /* Initialize spin lock that protects the address file object */
KeInitializeSpinLock(&AddrFile->Lock); ExInitializeResourceLite(&AddrFile->Resource);
/* Return address file object */ /* Return address file object */
Request->Handle.AddressHandle = AddrFile; Request->Handle.AddressHandle = AddrFile;
@ -581,27 +589,27 @@ NTSTATUS FileCloseAddress(
PTDI_REQUEST Request) PTDI_REQUEST Request)
{ {
PADDRESS_FILE AddrFile = Request->Handle.AddressHandle; PADDRESS_FILE AddrFile = Request->Handle.AddressHandle;
KIRQL OldIrql; PCONNECTION_ENDPOINT Listener;
if (!Request->Handle.AddressHandle) return STATUS_INVALID_PARAMETER; if (!Request->Handle.AddressHandle) return STATUS_INVALID_PARAMETER;
LockObject(AddrFile, &OldIrql); LockObject(AddrFile);
if (InterlockedDecrement(&AddrFile->Sharers) != 0) if (InterlockedDecrement(&AddrFile->Sharers) != 0)
{ {
/* Still other guys have open handles to this, so keep it around */ /* Still other guys have open handles to this, so keep it around */
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
return STATUS_SUCCESS; return STATUS_SUCCESS;
} }
/* We have to close this listener because we started it */ /* We have to close this listener because we started it */
if( AddrFile->Listener ) Listener = AddrFile->Listener;
UnlockObject(AddrFile);
if( Listener )
{ {
TCPClose( AddrFile->Listener ); TCPClose( Listener );
} }
UnlockObject(AddrFile, OldIrql);
DereferenceObject(AddrFile); DereferenceObject(AddrFile);
TI_DbgPrint(MAX_TRACE, ("Leaving.\n")); TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));

View file

@ -74,11 +74,9 @@ VOID
ClearReceiveHandler( ClearReceiveHandler(
_In_ PADDRESS_FILE AddrFile) _In_ PADDRESS_FILE AddrFile)
{ {
KIRQL OldIrql; LockObject(AddrFile);
LockObject(AddrFile, &OldIrql);
AddrFile->RegisteredReceiveDatagramHandler = FALSE; AddrFile->RegisteredReceiveDatagramHandler = FALSE;
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
} }
IO_WORKITEM_ROUTINE EndRequestHandler; IO_WORKITEM_ROUTINE EndRequestHandler;
@ -95,6 +93,8 @@ EndRequestHandler(
UINT32 nReplies; UINT32 nReplies;
KIRQL OldIrql; KIRQL OldIrql;
ClearReceiveHandler((PADDRESS_FILE)Context->TdiRequest.Handle.AddressHandle);
KeWaitForSingleObject(&Context->DatagramProcessedEvent, Executive, KernelMode, FALSE, NULL); KeWaitForSingleObject(&Context->DatagramProcessedEvent, Executive, KernelMode, FALSE, NULL);
TI_DbgPrint(DEBUG_ICMP, ("Finishing request Context: %p\n", Context)); TI_DbgPrint(DEBUG_ICMP, ("Finishing request Context: %p\n", Context));
@ -267,8 +267,6 @@ TimeoutHandler(
_In_opt_ PVOID SystemArgument2) _In_opt_ PVOID SystemArgument2)
{ {
PICMP_PACKET_CONTEXT Context = (PICMP_PACKET_CONTEXT)_Context; PICMP_PACKET_CONTEXT Context = (PICMP_PACKET_CONTEXT)_Context;
PADDRESS_FILE AddrFile = (PADDRESS_FILE)Context->TdiRequest.Handle.AddressHandle;
ClearReceiveHandler(AddrFile);
IoQueueWorkItem(Context->FinishWorker, &EndRequestHandler, DelayedWorkQueue, _Context); IoQueueWorkItem(Context->FinishWorker, &EndRequestHandler, DelayedWorkQueue, _Context);
} }
@ -290,7 +288,6 @@ DispEchoRequest(
PUCHAR Buffer; PUCHAR Buffer;
UINT16 RequestSize; UINT16 RequestSize;
PICMP_PACKET_CONTEXT SendContext; PICMP_PACKET_CONTEXT SendContext;
KIRQL OldIrql;
LARGE_INTEGER RequestTimeout; LARGE_INTEGER RequestTimeout;
UINT8 SavedTtl; UINT8 SavedTtl;
@ -390,14 +387,14 @@ DispEchoRequest(
RtlZeroMemory(Irp->AssociatedIrp.SystemBuffer, OutputBufferLength); RtlZeroMemory(Irp->AssociatedIrp.SystemBuffer, OutputBufferLength);
LockObject(AddrFile, &OldIrql); LockObject(AddrFile);
AddrFile->TTL = SavedTtl; AddrFile->TTL = SavedTtl;
AddrFile->ReceiveDatagramHandlerContext = SendContext; AddrFile->ReceiveDatagramHandlerContext = SendContext;
AddrFile->ReceiveDatagramHandler = ReceiveDatagram; AddrFile->ReceiveDatagramHandler = ReceiveDatagram;
AddrFile->RegisteredReceiveDatagramHandler = TRUE; AddrFile->RegisteredReceiveDatagramHandler = TRUE;
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
Status = AddrFile->Send(AddrFile, &ConnectionInfo, (PCHAR)Buffer, RequestSize, &DataUsed); Status = AddrFile->Send(AddrFile, &ConnectionInfo, (PCHAR)Buffer, RequestSize, &DataUsed);

View file

@ -16,13 +16,12 @@ BOOLEAN DGRemoveIRP(
{ {
PLIST_ENTRY ListEntry; PLIST_ENTRY ListEntry;
PDATAGRAM_RECEIVE_REQUEST ReceiveRequest; PDATAGRAM_RECEIVE_REQUEST ReceiveRequest;
KIRQL OldIrql;
BOOLEAN Found = FALSE; BOOLEAN Found = FALSE;
TI_DbgPrint(MAX_TRACE, ("Called (Cancel IRP %08x for file %08x).\n", TI_DbgPrint(MAX_TRACE, ("Called (Cancel IRP %08x for file %08x).\n",
Irp, AddrFile)); Irp, AddrFile));
LockObject(AddrFile, &OldIrql); LockObject(AddrFile);
for( ListEntry = AddrFile->ReceiveQueue.Flink; for( ListEntry = AddrFile->ReceiveQueue.Flink;
ListEntry != &AddrFile->ReceiveQueue; ListEntry != &AddrFile->ReceiveQueue;
@ -42,7 +41,7 @@ BOOLEAN DGRemoveIRP(
} }
} }
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
TI_DbgPrint(MAX_TRACE, ("Done.\n")); TI_DbgPrint(MAX_TRACE, ("Done.\n"));
@ -73,7 +72,6 @@ DGDeliverData(
* handler if it exists, otherwise we drop the packet. * handler if it exists, otherwise we drop the packet.
*/ */
{ {
KIRQL OldIrql;
LONG AddressLength; LONG AddressLength;
PVOID SourceAddress; PVOID SourceAddress;
ULONG BytesTaken; ULONG BytesTaken;
@ -82,7 +80,7 @@ DGDeliverData(
TI_DbgPrint(MIN_TRACE, ("Called.\n")); TI_DbgPrint(MIN_TRACE, ("Called.\n"));
LockObject(AddrFile, &OldIrql); LockObject(AddrFile);
if (AddrFile->Protocol == IPPROTO_UDP) if (AddrFile->Protocol == IPPROTO_UDP)
{ {
@ -145,7 +143,7 @@ DGDeliverData(
SrcAddress->Address.IPv4Address, SrcPort)); SrcAddress->Address.IPv4Address, SrcPort));
ReferenceObject(AddrFile); ReferenceObject(AddrFile);
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
/* Complete the receive request */ /* Complete the receive request */
if (Current->BufferSize < DataSize) if (Current->BufferSize < DataSize)
@ -153,12 +151,12 @@ DGDeliverData(
else else
Current->Complete(Current->Context, STATUS_SUCCESS, DataSize); Current->Complete(Current->Context, STATUS_SUCCESS, DataSize);
LockObject(AddrFile, &OldIrql); LockObject(AddrFile);
DereferenceObject(AddrFile); DereferenceObject(AddrFile);
} }
} }
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
} }
else if (AddrFile->RegisteredReceiveDatagramHandler) else if (AddrFile->RegisteredReceiveDatagramHandler)
{ {
@ -186,7 +184,7 @@ DGDeliverData(
} }
ReferenceObject(AddrFile); ReferenceObject(AddrFile);
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
TI_DbgPrint(MIN_TRACE, ("OptionsSize %d DataSize: %u\n", OptionsSize, DataSize)); TI_DbgPrint(MIN_TRACE, ("OptionsSize %d DataSize: %u\n", OptionsSize, DataSize));
@ -209,7 +207,7 @@ DGDeliverData(
} }
else else
{ {
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
TI_DbgPrint(MAX_TRACE, ("Discarding datagram.\n")); TI_DbgPrint(MAX_TRACE, ("Discarding datagram.\n"));
} }
@ -254,11 +252,10 @@ NTSTATUS DGReceiveDatagram(
{ {
NTSTATUS Status; NTSTATUS Status;
PDATAGRAM_RECEIVE_REQUEST ReceiveRequest; PDATAGRAM_RECEIVE_REQUEST ReceiveRequest;
KIRQL OldIrql;
TI_DbgPrint(MAX_TRACE, ("Called.\n")); TI_DbgPrint(MAX_TRACE, ("Called.\n"));
LockObject(AddrFile, &OldIrql); LockObject(AddrFile);
ReceiveRequest = ExAllocatePoolWithTag(NonPagedPool, sizeof(DATAGRAM_RECEIVE_REQUEST), ReceiveRequest = ExAllocatePoolWithTag(NonPagedPool, sizeof(DATAGRAM_RECEIVE_REQUEST),
DATAGRAM_RECV_TAG); DATAGRAM_RECV_TAG);
@ -276,7 +273,7 @@ NTSTATUS DGReceiveDatagram(
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
ExFreePoolWithTag(ReceiveRequest, DATAGRAM_RECV_TAG); ExFreePoolWithTag(ReceiveRequest, DATAGRAM_RECV_TAG);
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
return Status; return Status;
} }
} }
@ -304,13 +301,13 @@ NTSTATUS DGReceiveDatagram(
TI_DbgPrint(MAX_TRACE, ("Leaving (pending %08x).\n", ReceiveRequest)); TI_DbgPrint(MAX_TRACE, ("Leaving (pending %08x).\n", ReceiveRequest));
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
return STATUS_PENDING; return STATUS_PENDING;
} }
else else
{ {
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
Status = STATUS_INSUFFICIENT_RESOURCES; Status = STATUS_INSUFFICIENT_RESOURCES;
} }

View file

@ -191,9 +191,8 @@ NTSTATUS RawIPSendDatagram(
USHORT RemotePort; USHORT RemotePort;
NTSTATUS Status; NTSTATUS Status;
PNEIGHBOR_CACHE_ENTRY NCE; PNEIGHBOR_CACHE_ENTRY NCE;
KIRQL OldIrql;
LockObject(AddrFile, &OldIrql); LockObject(AddrFile);
TI_DbgPrint(MID_TRACE,("Sending Datagram(%x %x %x %d)\n", TI_DbgPrint(MID_TRACE,("Sending Datagram(%x %x %x %d)\n",
AddrFile, ConnInfo, BufferData, DataSize)); AddrFile, ConnInfo, BufferData, DataSize));
@ -208,7 +207,7 @@ NTSTATUS RawIPSendDatagram(
break; break;
default: default:
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
return STATUS_UNSUCCESSFUL; return STATUS_UNSUCCESSFUL;
} }
@ -222,7 +221,7 @@ NTSTATUS RawIPSendDatagram(
* interface we're sending over * interface we're sending over
*/ */
if(!(NCE = RouteGetRouteToDestination( &RemoteAddress ))) { if(!(NCE = RouteGetRouteToDestination( &RemoteAddress ))) {
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
return STATUS_NETWORK_UNREACHABLE; return STATUS_NETWORK_UNREACHABLE;
} }
@ -231,7 +230,7 @@ NTSTATUS RawIPSendDatagram(
else else
{ {
if(!(NCE = NBLocateNeighbor( &LocalAddress, NULL ))) { if(!(NCE = NBLocateNeighbor( &LocalAddress, NULL ))) {
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
return STATUS_INVALID_PARAMETER; return STATUS_INVALID_PARAMETER;
} }
} }
@ -245,7 +244,7 @@ NTSTATUS RawIPSendDatagram(
BufferData, BufferData,
DataSize ); DataSize );
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
if( !NT_SUCCESS(Status) ) if( !NT_SUCCESS(Status) )
return Status; return Status;

View file

@ -48,12 +48,11 @@ NTSTATUS TCPListen(PCONNECTION_ENDPOINT Connection, UINT Backlog)
{ {
NTSTATUS Status = STATUS_SUCCESS; NTSTATUS Status = STATUS_SUCCESS;
struct ip_addr AddressToBind; struct ip_addr AddressToBind;
KIRQL OldIrql;
TA_IP_ADDRESS LocalAddress; TA_IP_ADDRESS LocalAddress;
ASSERT(Connection); ASSERT(Connection);
LockObject(Connection, &OldIrql); LockObject(Connection);
ASSERT_KM_POINTER(Connection->AddressFile); ASSERT_KM_POINTER(Connection->AddressFile);
@ -93,7 +92,7 @@ NTSTATUS TCPListen(PCONNECTION_ENDPOINT Connection, UINT Backlog)
Status = STATUS_UNSUCCESSFUL; Status = STATUS_UNSUCCESSFUL;
} }
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
TI_DbgPrint(DEBUG_TCP,("[IP, TCPListen] Leaving. Status = %x\n", Status)); TI_DbgPrint(DEBUG_TCP,("[IP, TCPListen] Leaving. Status = %x\n", Status));
@ -106,10 +105,9 @@ BOOLEAN TCPAbortListenForSocket
{ {
PLIST_ENTRY ListEntry; PLIST_ENTRY ListEntry;
PTDI_BUCKET Bucket; PTDI_BUCKET Bucket;
KIRQL OldIrql;
BOOLEAN Found = FALSE; BOOLEAN Found = FALSE;
LockObject(Listener, &OldIrql); LockObject(Listener);
ListEntry = Listener->ListenRequest.Flink; ListEntry = Listener->ListenRequest.Flink;
while (ListEntry != &Listener->ListenRequest) while (ListEntry != &Listener->ListenRequest)
@ -128,7 +126,7 @@ BOOLEAN TCPAbortListenForSocket
ListEntry = ListEntry->Flink; ListEntry = ListEntry->Flink;
} }
UnlockObject(Listener, OldIrql); UnlockObject(Listener);
return Found; return Found;
} }
@ -141,9 +139,8 @@ NTSTATUS TCPAccept ( PTDI_REQUEST Request,
{ {
NTSTATUS Status; NTSTATUS Status;
PTDI_BUCKET Bucket; PTDI_BUCKET Bucket;
KIRQL OldIrql;
LockObject(Listener, &OldIrql); LockObject(Listener);
Bucket = ExAllocateFromNPagedLookasideList(&TdiBucketLookasideList); Bucket = ExAllocateFromNPagedLookasideList(&TdiBucketLookasideList);
@ -160,7 +157,7 @@ NTSTATUS TCPAccept ( PTDI_REQUEST Request,
else else
Status = STATUS_NO_MEMORY; Status = STATUS_NO_MEMORY;
UnlockObject(Listener, OldIrql); UnlockObject(Listener);
return Status; return Status;
} }

View file

@ -50,125 +50,66 @@ CompleteBucket(PCONNECTION_ENDPOINT Connection, PTDI_BUCKET Bucket, const BOOLEA
} }
VOID VOID
FlushReceiveQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status, const BOOLEAN interlocked) FlushReceiveQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status)
{ {
PTDI_BUCKET Bucket; PTDI_BUCKET Bucket;
PLIST_ENTRY Entry; PLIST_ENTRY Entry;
ReferenceObject(Connection); ASSERT_TCPIP_OBJECT_LOCKED(Connection);
if (interlocked) while (!IsListEmpty(&Connection->ReceiveRequest))
{ {
while ((Entry = ExInterlockedRemoveHeadList(&Connection->ReceiveRequest, &Connection->Lock))) Entry = RemoveHeadList(&Connection->ReceiveRequest);
{
Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
TI_DbgPrint(DEBUG_TCP, Bucket = CONTAINING_RECORD(Entry, TDI_BUCKET, Entry);
("Completing Receive request: %x %x\n",
Bucket->Request, Status));
Bucket->Status = Status; Bucket->Information = 0;
Bucket->Information = 0; Bucket->Status = Status;
CompleteBucket(Connection, Bucket, FALSE); CompleteBucket(Connection, Bucket, FALSE);
}
} }
else
{
while (!IsListEmpty(&Connection->ReceiveRequest))
{
Entry = RemoveHeadList(&Connection->ReceiveRequest);
Bucket = CONTAINING_RECORD(Entry, TDI_BUCKET, Entry);
Bucket->Information = 0;
Bucket->Status = Status;
CompleteBucket(Connection, Bucket, FALSE);
}
}
DereferenceObject(Connection);
} }
VOID VOID
FlushSendQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status, const BOOLEAN interlocked) FlushSendQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status)
{ {
PTDI_BUCKET Bucket; PTDI_BUCKET Bucket;
PLIST_ENTRY Entry; PLIST_ENTRY Entry;
ReferenceObject(Connection); ASSERT_TCPIP_OBJECT_LOCKED(Connection);
if (interlocked) while (!IsListEmpty(&Connection->SendRequest))
{ {
while ((Entry = ExInterlockedRemoveHeadList(&Connection->SendRequest, &Connection->Lock))) Entry = RemoveHeadList(&Connection->SendRequest);
{
Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
TI_DbgPrint(DEBUG_TCP, Bucket = CONTAINING_RECORD(Entry, TDI_BUCKET, Entry);
("Completing Send request: %x %x\n",
Bucket->Request, Status));
Bucket->Status = Status; Bucket->Information = 0;
Bucket->Information = 0; Bucket->Status = Status;
CompleteBucket(Connection, Bucket, FALSE); CompleteBucket(Connection, Bucket, FALSE);
}
} }
else
{
while (!IsListEmpty(&Connection->SendRequest))
{
Entry = RemoveHeadList(&Connection->SendRequest);
Bucket = CONTAINING_RECORD(Entry, TDI_BUCKET, Entry);
Bucket->Information = 0;
Bucket->Status = Status;
CompleteBucket(Connection, Bucket, FALSE);
}
}
DereferenceObject(Connection);
} }
VOID VOID
FlushShutdownQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status, const BOOLEAN interlocked) FlushShutdownQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status)
{ {
PTDI_BUCKET Bucket; PTDI_BUCKET Bucket;
PLIST_ENTRY Entry; PLIST_ENTRY Entry;
ReferenceObject(Connection); ASSERT_TCPIP_OBJECT_LOCKED(Connection);
if (interlocked) while (!IsListEmpty(&Connection->ShutdownRequest))
{ {
while ((Entry = ExInterlockedRemoveHeadList(&Connection->ShutdownRequest, &Connection->Lock))) Entry = RemoveHeadList(&Connection->ShutdownRequest);
{
Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
Bucket->Status = Status; Bucket = CONTAINING_RECORD(Entry, TDI_BUCKET, Entry);
Bucket->Information = 0;
CompleteBucket(Connection, Bucket, FALSE); Bucket->Information = 0;
} Bucket->Status = Status;
CompleteBucket(Connection, Bucket, FALSE);
} }
else
{
while (!IsListEmpty(&Connection->ShutdownRequest))
{
Entry = RemoveHeadList(&Connection->ShutdownRequest);
Bucket = CONTAINING_RECORD(Entry, TDI_BUCKET, Entry);
Bucket->Information = 0;
Bucket->Status = Status;
CompleteBucket(Connection, Bucket, FALSE);
}
}
DereferenceObject(Connection);
} }
VOID VOID
@ -177,10 +118,11 @@ FlushConnectQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status)
PTDI_BUCKET Bucket; PTDI_BUCKET Bucket;
PLIST_ENTRY Entry; PLIST_ENTRY Entry;
ReferenceObject(Connection); ASSERT_TCPIP_OBJECT_LOCKED(Connection);
while ((Entry = ExInterlockedRemoveHeadList(&Connection->ConnectRequest, &Connection->Lock))) while (!IsListEmpty(&Connection->ConnectRequest))
{ {
Entry = RemoveHeadList(&Connection->ConnectRequest);
Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry ); Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
Bucket->Status = Status; Bucket->Status = Status;
@ -188,8 +130,6 @@ FlushConnectQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status)
CompleteBucket(Connection, Bucket, FALSE); CompleteBucket(Connection, Bucket, FALSE);
} }
DereferenceObject(Connection);
} }
VOID VOID
@ -198,10 +138,11 @@ FlushListenQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status)
PTDI_BUCKET Bucket; PTDI_BUCKET Bucket;
PLIST_ENTRY Entry; PLIST_ENTRY Entry;
ReferenceObject(Connection); ASSERT_TCPIP_OBJECT_LOCKED(Connection);
while ((Entry = ExInterlockedRemoveHeadList(&Connection->ListenRequest, &Connection->Lock))) while (!IsListEmpty(&Connection->ListenRequest))
{ {
Entry = RemoveHeadList(&Connection->ListenRequest);
Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry ); Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
Bucket->Status = Status; Bucket->Status = Status;
@ -210,17 +151,13 @@ FlushListenQueue(PCONNECTION_ENDPOINT Connection, const NTSTATUS Status)
DereferenceObject(Bucket->AssociatedEndpoint); DereferenceObject(Bucket->AssociatedEndpoint);
CompleteBucket(Connection, Bucket, FALSE); CompleteBucket(Connection, Bucket, FALSE);
} }
DereferenceObject(Connection);
} }
VOID VOID
FlushAllQueues(PCONNECTION_ENDPOINT Connection, NTSTATUS Status) FlushAllQueues(PCONNECTION_ENDPOINT Connection, NTSTATUS Status)
{ {
ReferenceObject(Connection);
// flush receive queue // flush receive queue
FlushReceiveQueue(Connection, Status, TRUE); FlushReceiveQueue(Connection, Status);
/* We completed the reads successfully but we need to return failure now */ /* We completed the reads successfully but we need to return failure now */
if (Status == STATUS_SUCCESS) if (Status == STATUS_SUCCESS)
@ -232,15 +169,13 @@ FlushAllQueues(PCONNECTION_ENDPOINT Connection, NTSTATUS Status)
FlushListenQueue(Connection, Status); FlushListenQueue(Connection, Status);
// flush send queue // flush send queue
FlushSendQueue(Connection, Status, TRUE); FlushSendQueue(Connection, Status);
// flush connect queue // flush connect queue
FlushConnectQueue(Connection, Status); FlushConnectQueue(Connection, Status);
// flush shutdown queue // flush shutdown queue
FlushShutdownQueue(Connection, Status, TRUE); FlushShutdownQueue(Connection, Status);
DereferenceObject(Connection);
} }
VOID VOID
@ -248,18 +183,17 @@ TCPFinEventHandler(void *arg, const err_t err)
{ {
PCONNECTION_ENDPOINT Connection = (PCONNECTION_ENDPOINT)arg, LastConnection; PCONNECTION_ENDPOINT Connection = (PCONNECTION_ENDPOINT)arg, LastConnection;
const NTSTATUS Status = TCPTranslateError(err); const NTSTATUS Status = TCPTranslateError(err);
KIRQL OldIrql;
ASSERT(Connection->SocketContext == NULL); ASSERT(Connection->SocketContext == NULL);
ASSERT(Connection->AddressFile); ASSERT(Connection->AddressFile);
ASSERT(err != ERR_OK); ASSERT(err != ERR_OK);
LockObject(Connection);
/* Complete all outstanding requests now */ /* Complete all outstanding requests now */
FlushAllQueues(Connection, Status); FlushAllQueues(Connection, Status);
LockObject(Connection, &OldIrql); LockObject(Connection->AddressFile);
LockObjectAtDpcLevel(Connection->AddressFile);
/* Unlink this connection from the address file */ /* Unlink this connection from the address file */
if (Connection->AddressFile->Connection == Connection) if (Connection->AddressFile->Connection == Connection)
@ -284,13 +218,13 @@ TCPFinEventHandler(void *arg, const err_t err)
} }
} }
UnlockObjectFromDpcLevel(Connection->AddressFile); UnlockObject(Connection->AddressFile);
/* Remove the address file from this connection */ /* Remove the address file from this connection */
DereferenceObject(Connection->AddressFile); DereferenceObject(Connection->AddressFile);
Connection->AddressFile = NULL; Connection->AddressFile = NULL;
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
} }
VOID VOID
@ -301,14 +235,15 @@ TCPAcceptEventHandler(void *arg, PTCP_PCB newpcb)
PLIST_ENTRY Entry; PLIST_ENTRY Entry;
PIRP Irp; PIRP Irp;
NTSTATUS Status; NTSTATUS Status;
KIRQL OldIrql;
ReferenceObject(Connection); LockObject(Connection);
while ((Entry = ExInterlockedRemoveHeadList(&Connection->ListenRequest, &Connection->Lock))) while (!IsListEmpty(&Connection->ListenRequest))
{ {
PIO_STACK_LOCATION IrpSp; PIO_STACK_LOCATION IrpSp;
Entry = RemoveHeadList(&Connection->ListenRequest);
Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry ); Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
Irp = Bucket->Request.RequestContext; Irp = Bucket->Request.RequestContext;
@ -326,7 +261,7 @@ TCPAcceptEventHandler(void *arg, PTCP_PCB newpcb)
if (Status == STATUS_SUCCESS) if (Status == STATUS_SUCCESS)
{ {
LockObject(Bucket->AssociatedEndpoint, &OldIrql); LockObject(Bucket->AssociatedEndpoint);
/* sanity assert...this should never be in anything else but a CLOSED state */ /* sanity assert...this should never be in anything else but a CLOSED state */
ASSERT( ((PTCP_PCB)Bucket->AssociatedEndpoint->SocketContext)->state == CLOSED ); ASSERT( ((PTCP_PCB)Bucket->AssociatedEndpoint->SocketContext)->state == CLOSED );
@ -337,9 +272,9 @@ TCPAcceptEventHandler(void *arg, PTCP_PCB newpcb)
/* free previously created socket context (we don't use it, we use newpcb) */ /* free previously created socket context (we don't use it, we use newpcb) */
Bucket->AssociatedEndpoint->SocketContext = newpcb; Bucket->AssociatedEndpoint->SocketContext = newpcb;
LibTCPAccept(newpcb, (PTCP_PCB)Connection->SocketContext, Bucket->AssociatedEndpoint); UnlockObject(Bucket->AssociatedEndpoint);
UnlockObject(Bucket->AssociatedEndpoint, OldIrql); LibTCPAccept(newpcb, (PTCP_PCB)Connection->SocketContext, Bucket->AssociatedEndpoint);
} }
DereferenceObject(Bucket->AssociatedEndpoint); DereferenceObject(Bucket->AssociatedEndpoint);
@ -352,7 +287,7 @@ TCPAcceptEventHandler(void *arg, PTCP_PCB newpcb)
} }
} }
DereferenceObject(Connection); UnlockObject(Connection);
} }
VOID VOID
@ -367,12 +302,17 @@ TCPSendEventHandler(void *arg, const u16_t space)
ULONG BytesSent; ULONG BytesSent;
ReferenceObject(Connection); ReferenceObject(Connection);
LockObject(Connection);
while ((Entry = ExInterlockedRemoveHeadList(&Connection->SendRequest, &Connection->Lock))) while (!IsListEmpty(&Connection->SendRequest))
{ {
UINT SendLen = 0; UINT SendLen = 0;
PVOID SendBuffer = 0; PVOID SendBuffer = 0;
Entry = RemoveHeadList(&Connection->SendRequest);
UnlockObject(Connection);
Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry ); Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
Irp = Bucket->Request.RequestContext; Irp = Bucket->Request.RequestContext;
@ -400,9 +340,8 @@ TCPSendEventHandler(void *arg, const u16_t space)
if( Status == STATUS_PENDING ) if( Status == STATUS_PENDING )
{ {
ExInterlockedInsertHeadList(&Connection->SendRequest, LockObject(Connection);
&Bucket->Entry, InsertHeadList(&Connection->SendRequest, &Bucket->Entry);
&Connection->Lock);
break; break;
} }
else else
@ -416,13 +355,15 @@ TCPSendEventHandler(void *arg, const u16_t space)
CompleteBucket(Connection, Bucket, FALSE); CompleteBucket(Connection, Bucket, FALSE);
} }
LockObject(Connection);
} }
// If we completed all outstanding send requests then finish all pending shutdown requests, // If we completed all outstanding send requests then finish all pending shutdown requests,
// cancel the timer and dereference the connection // cancel the timer and dereference the connection
if (IsListEmpty(&Connection->SendRequest)) if (IsListEmpty(&Connection->SendRequest))
{ {
FlushShutdownQueue(Connection, STATUS_SUCCESS, FALSE); FlushShutdownQueue(Connection, STATUS_SUCCESS);
if (KeCancelTimer(&Connection->DisconnectTimer)) if (KeCancelTimer(&Connection->DisconnectTimer))
{ {
@ -430,6 +371,8 @@ TCPSendEventHandler(void *arg, const u16_t space)
} }
} }
UnlockObject(Connection);
DereferenceObject(Connection); DereferenceObject(Connection);
} }
@ -446,10 +389,11 @@ TCPRecvEventHandler(void *arg)
PUCHAR RecvBuffer; PUCHAR RecvBuffer;
NTSTATUS Status; NTSTATUS Status;
ReferenceObject(Connection); LockObject(Connection);
while ((Entry = ExInterlockedRemoveHeadList(&Connection->ReceiveRequest, &Connection->Lock))) while(!IsListEmpty(&Connection->ReceiveRequest))
{ {
Entry = RemoveHeadList(&Connection->ReceiveRequest);
Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry ); Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
Irp = Bucket->Request.RequestContext; Irp = Bucket->Request.RequestContext;
@ -460,9 +404,7 @@ TCPRecvEventHandler(void *arg)
Status = LibTCPGetDataFromConnectionQueue(Connection, RecvBuffer, RecvLen, &Received); Status = LibTCPGetDataFromConnectionQueue(Connection, RecvBuffer, RecvLen, &Received);
if (Status == STATUS_PENDING) if (Status == STATUS_PENDING)
{ {
ExInterlockedInsertHeadList(&Connection->ReceiveRequest, InsertHeadList(&Connection->ReceiveRequest, &Bucket->Entry);
&Bucket->Entry,
&Connection->Lock);
break; break;
} }
@ -471,8 +413,7 @@ TCPRecvEventHandler(void *arg)
CompleteBucket(Connection, Bucket, FALSE); CompleteBucket(Connection, Bucket, FALSE);
} }
UnlockObject(Connection);
DereferenceObject(Connection);
} }
VOID VOID
@ -482,10 +423,11 @@ TCPConnectEventHandler(void *arg, const err_t err)
PTDI_BUCKET Bucket; PTDI_BUCKET Bucket;
PLIST_ENTRY Entry; PLIST_ENTRY Entry;
ReferenceObject(Connection); LockObject(Connection);
while ((Entry = ExInterlockedRemoveHeadList(&Connection->ConnectRequest, &Connection->Lock))) while (!IsListEmpty(&Connection->ConnectRequest))
{ {
Entry = RemoveHeadList(&Connection->ConnectRequest);
Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry ); Bucket = CONTAINING_RECORD( Entry, TDI_BUCKET, Entry );
@ -495,5 +437,5 @@ TCPConnectEventHandler(void *arg, const err_t err)
CompleteBucket(Connection, Bucket, FALSE); CompleteBucket(Connection, Bucket, FALSE);
} }
DereferenceObject(Connection); UnlockObject(Connection);
} }

View file

@ -25,21 +25,27 @@ PORT_SET TCPPorts;
NPAGED_LOOKASIDE_LIST TdiBucketLookasideList; NPAGED_LOOKASIDE_LIST TdiBucketLookasideList;
VOID NTAPI static
DisconnectTimeoutDpc(PKDPC Dpc, IO_WORKITEM_ROUTINE
PVOID DeferredContext, DisconnectWorker;
PVOID SystemArgument1,
PVOID SystemArgument2) _Use_decl_annotations_
VOID
NTAPI
DisconnectWorker(
_Unreferenced_parameter_ PDEVICE_OBJECT DeviceObject,
_In_ PVOID Context
)
{ {
PCONNECTION_ENDPOINT Connection = (PCONNECTION_ENDPOINT)DeferredContext; PCONNECTION_ENDPOINT Connection = (PCONNECTION_ENDPOINT)Context;
PLIST_ENTRY Entry; PLIST_ENTRY Entry;
PTDI_BUCKET Bucket; PTDI_BUCKET Bucket;
LockObjectAtDpcLevel(Connection);
/* We timed out waiting for pending sends so force it to shutdown */ /* We timed out waiting for pending sends so force it to shutdown */
TCPTranslateError(LibTCPShutdown(Connection, 0, 1)); TCPTranslateError(LibTCPShutdown(Connection, 0, 1));
LockObject(Connection);
while (!IsListEmpty(&Connection->SendRequest)) while (!IsListEmpty(&Connection->SendRequest))
{ {
Entry = RemoveHeadList(&Connection->SendRequest); Entry = RemoveHeadList(&Connection->SendRequest);
@ -64,11 +70,23 @@ DisconnectTimeoutDpc(PKDPC Dpc,
CompleteBucket(Connection, Bucket, FALSE); CompleteBucket(Connection, Bucket, FALSE);
} }
UnlockObjectFromDpcLevel(Connection); UnlockObject(Connection);
DereferenceObject(Connection); DereferenceObject(Connection);
} }
VOID
NTAPI
DisconnectTimeoutDpc(PKDPC Dpc,
PVOID DeferredContext,
PVOID SystemArgument1,
PVOID SystemArgument2)
{
PCONNECTION_ENDPOINT Connection = (PCONNECTION_ENDPOINT)DeferredContext;
IoQueueWorkItem(Connection->DisconnectWorkItem, DisconnectWorker, DelayedWorkQueue, Connection);
}
VOID ConnectionFree(PVOID Object) VOID ConnectionFree(PVOID Object)
{ {
PCONNECTION_ENDPOINT Connection = (PCONNECTION_ENDPOINT)Object; PCONNECTION_ENDPOINT Connection = (PCONNECTION_ENDPOINT)Object;
@ -80,6 +98,9 @@ VOID ConnectionFree(PVOID Object)
RemoveEntryList(&Connection->ListEntry); RemoveEntryList(&Connection->ListEntry);
TcpipReleaseSpinLock(&ConnectionEndpointListLock, OldIrql); TcpipReleaseSpinLock(&ConnectionEndpointListLock, OldIrql);
ExDeleteResourceLite(&Connection->Resource);
IoFreeWorkItem(Connection->DisconnectWorkItem);
ExFreePoolWithTag( Connection, CONN_ENDPT_TAG ); ExFreePoolWithTag( Connection, CONN_ENDPT_TAG );
} }
@ -96,7 +117,7 @@ PCONNECTION_ENDPOINT TCPAllocateConnectionEndpoint( PVOID ClientContext )
RtlZeroMemory(Connection, sizeof(CONNECTION_ENDPOINT)); RtlZeroMemory(Connection, sizeof(CONNECTION_ENDPOINT));
/* Initialize spin lock that protects the connection endpoint file object */ /* Initialize spin lock that protects the connection endpoint file object */
KeInitializeSpinLock(&Connection->Lock); ExInitializeResourceLite(&Connection->Resource);
InitializeListHead(&Connection->ConnectRequest); InitializeListHead(&Connection->ConnectRequest);
InitializeListHead(&Connection->ListenRequest); InitializeListHead(&Connection->ListenRequest);
InitializeListHead(&Connection->ReceiveRequest); InitializeListHead(&Connection->ReceiveRequest);
@ -107,6 +128,13 @@ PCONNECTION_ENDPOINT TCPAllocateConnectionEndpoint( PVOID ClientContext )
/* Initialize disconnect timer */ /* Initialize disconnect timer */
KeInitializeTimer(&Connection->DisconnectTimer); KeInitializeTimer(&Connection->DisconnectTimer);
KeInitializeDpc(&Connection->DisconnectDpc, DisconnectTimeoutDpc, Connection); KeInitializeDpc(&Connection->DisconnectDpc, DisconnectTimeoutDpc, Connection);
Connection->DisconnectWorkItem = IoAllocateWorkItem(TCPDeviceObject);
if (!Connection->DisconnectWorkItem)
{
ExDeleteResourceLite(&Connection->Resource);
ExFreePoolWithTag( Connection, CONN_ENDPT_TAG );
return NULL;
}
/* Save client context pointer */ /* Save client context pointer */
Connection->ClientContext = ClientContext; Connection->ClientContext = ClientContext;
@ -126,9 +154,8 @@ NTSTATUS TCPSocket( PCONNECTION_ENDPOINT Connection,
UINT Family, UINT Type, UINT Proto ) UINT Family, UINT Type, UINT Proto )
{ {
NTSTATUS Status; NTSTATUS Status;
KIRQL OldIrql;
LockObject(Connection, &OldIrql); LockObject(Connection);
TI_DbgPrint(DEBUG_TCP,("[IP, TCPSocket] Called: Connection %x, Family %d, Type %d, " TI_DbgPrint(DEBUG_TCP,("[IP, TCPSocket] Called: Connection %x, Family %d, Type %d, "
"Proto %d, sizeof(CONNECTION_ENDPOINT) = %d\n", "Proto %d, sizeof(CONNECTION_ENDPOINT) = %d\n",
@ -140,7 +167,7 @@ NTSTATUS TCPSocket( PCONNECTION_ENDPOINT Connection,
else else
Status = STATUS_INSUFFICIENT_RESOURCES; Status = STATUS_INSUFFICIENT_RESOURCES;
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
TI_DbgPrint(DEBUG_TCP,("[IP, TCPSocket] Leaving. Status = 0x%x\n", Status)); TI_DbgPrint(DEBUG_TCP,("[IP, TCPSocket] Leaving. Status = 0x%x\n", Status));
@ -149,15 +176,13 @@ NTSTATUS TCPSocket( PCONNECTION_ENDPOINT Connection,
NTSTATUS TCPClose( PCONNECTION_ENDPOINT Connection ) NTSTATUS TCPClose( PCONNECTION_ENDPOINT Connection )
{ {
KIRQL OldIrql; LockObject(Connection);
LockObject(Connection, &OldIrql);
FlushAllQueues(Connection, STATUS_CANCELLED); FlushAllQueues(Connection, STATUS_CANCELLED);
LibTCPClose(Connection, FALSE, TRUE); UnlockObject(Connection);
UnlockObject(Connection, OldIrql); LibTCPClose(Connection, FALSE, TRUE);
DereferenceObject(Connection); DereferenceObject(Connection);
@ -287,7 +312,6 @@ NTSTATUS TCPConnect
TA_IP_ADDRESS LocalAddress; TA_IP_ADDRESS LocalAddress;
PTDI_BUCKET Bucket; PTDI_BUCKET Bucket;
PNEIGHBOR_CACHE_ENTRY NCE; PNEIGHBOR_CACHE_ENTRY NCE;
KIRQL OldIrql;
TI_DbgPrint(DEBUG_TCP,("[IP, TCPConnect] Called\n")); TI_DbgPrint(DEBUG_TCP,("[IP, TCPConnect] Called\n"));
@ -308,11 +332,11 @@ NTSTATUS TCPConnect
RemoteAddress.Address.IPv4Address, RemoteAddress.Address.IPv4Address,
RemotePort)); RemotePort));
LockObject(Connection, &OldIrql); LockObject(Connection);
if (!Connection->AddressFile) if (!Connection->AddressFile)
{ {
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
return STATUS_INVALID_PARAMETER; return STATUS_INVALID_PARAMETER;
} }
@ -320,7 +344,7 @@ NTSTATUS TCPConnect
{ {
if (!(NCE = RouteGetRouteToDestination(&RemoteAddress))) if (!(NCE = RouteGetRouteToDestination(&RemoteAddress)))
{ {
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
return STATUS_NETWORK_UNREACHABLE; return STATUS_NETWORK_UNREACHABLE;
} }
@ -335,48 +359,51 @@ NTSTATUS TCPConnect
&bindaddr, &bindaddr,
Connection->AddressFile->Port)); Connection->AddressFile->Port));
if (NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
/* Copy bind address into connection */ UnlockObject(Connection);
Connection->AddressFile->Address.Address.IPv4Address = bindaddr.addr; return Status;
/* Check if we had an unspecified port */
if (!Connection->AddressFile->Port)
{
/* We did, so we need to copy back the port */
Status = TCPGetSockAddress(Connection, (PTRANSPORT_ADDRESS)&LocalAddress, FALSE);
if (NT_SUCCESS(Status))
{
/* Allocate the port in the port bitmap */
Connection->AddressFile->Port = TCPAllocatePort(LocalAddress.Address[0].Address[0].sin_port);
/* This should never fail */
ASSERT(Connection->AddressFile->Port != 0xFFFF);
}
}
if (NT_SUCCESS(Status))
{
connaddr.addr = RemoteAddress.Address.IPv4Address;
Bucket = ExAllocateFromNPagedLookasideList(&TdiBucketLookasideList);
if (!Bucket)
{
UnlockObject(Connection, OldIrql);
return STATUS_NO_MEMORY;
}
Bucket->Request.RequestNotifyObject = (PVOID)Complete;
Bucket->Request.RequestContext = Context;
InsertTailList( &Connection->ConnectRequest, &Bucket->Entry );
Status = TCPTranslateError(LibTCPConnect(Connection,
&connaddr,
RemotePort));
}
} }
UnlockObject(Connection, OldIrql); /* Copy bind address into connection */
Connection->AddressFile->Address.Address.IPv4Address = bindaddr.addr;
/* Check if we had an unspecified port */
if (!Connection->AddressFile->Port)
{
/* We did, so we need to copy back the port */
Status = TCPGetSockAddress(Connection, (PTRANSPORT_ADDRESS)&LocalAddress, FALSE);
if (!NT_SUCCESS(Status))
{
UnlockObject(Connection);
return Status;
}
/* Allocate the port in the port bitmap */
Connection->AddressFile->Port = TCPAllocatePort(LocalAddress.Address[0].Address[0].sin_port);
/* This should never fail */
ASSERT(Connection->AddressFile->Port != 0xFFFF);
}
connaddr.addr = RemoteAddress.Address.IPv4Address;
Bucket = ExAllocateFromNPagedLookasideList(&TdiBucketLookasideList);
if (!Bucket)
{
UnlockObject(Connection);
return STATUS_NO_MEMORY;
}
Bucket->Request.RequestNotifyObject = (PVOID)Complete;
Bucket->Request.RequestContext = Context;
InsertTailList( &Connection->ConnectRequest, &Bucket->Entry );
UnlockObject(Connection);
Status = TCPTranslateError(LibTCPConnect(Connection,
&connaddr,
RemotePort));
TI_DbgPrint(DEBUG_TCP,("[IP, TCPConnect] Leaving. Status = 0x%x\n", Status)); TI_DbgPrint(DEBUG_TCP,("[IP, TCPConnect] Leaving. Status = 0x%x\n", Status));
@ -394,12 +421,11 @@ NTSTATUS TCPDisconnect
{ {
NTSTATUS Status = STATUS_INVALID_PARAMETER; NTSTATUS Status = STATUS_INVALID_PARAMETER;
PTDI_BUCKET Bucket; PTDI_BUCKET Bucket;
KIRQL OldIrql;
LARGE_INTEGER ActualTimeout; LARGE_INTEGER ActualTimeout;
TI_DbgPrint(DEBUG_TCP,("[IP, TCPDisconnect] Called\n")); TI_DbgPrint(DEBUG_TCP,("[IP, TCPDisconnect] Called\n"));
LockObject(Connection, &OldIrql); LockObject(Connection);
if (Connection->SocketContext) if (Connection->SocketContext)
{ {
@ -407,12 +433,20 @@ NTSTATUS TCPDisconnect
{ {
if (IsListEmpty(&Connection->SendRequest)) if (IsListEmpty(&Connection->SendRequest))
{ {
ReferenceObject(Connection);
UnlockObject(Connection);
Status = TCPTranslateError(LibTCPShutdown(Connection, 0, 1)); Status = TCPTranslateError(LibTCPShutdown(Connection, 0, 1));
LockObject(Connection);
DereferenceObject(Connection);
} }
else if (Timeout && Timeout->QuadPart == 0) else if (Timeout && Timeout->QuadPart == 0)
{ {
FlushSendQueue(Connection, STATUS_FILE_CLOSED, FALSE); FlushSendQueue(Connection, STATUS_FILE_CLOSED);
TCPTranslateError(LibTCPShutdown(Connection, 0, 1)); ReferenceObject(Connection);
UnlockObject(Connection);
LibTCPShutdown(Connection, 0, 1);
LockObject(Connection);
DereferenceObject(Connection);
Status = STATUS_TIMEOUT; Status = STATUS_TIMEOUT;
} }
else else
@ -431,7 +465,7 @@ NTSTATUS TCPDisconnect
Bucket = ExAllocateFromNPagedLookasideList(&TdiBucketLookasideList); Bucket = ExAllocateFromNPagedLookasideList(&TdiBucketLookasideList);
if (!Bucket) if (!Bucket)
{ {
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
return STATUS_NO_MEMORY; return STATUS_NO_MEMORY;
} }
@ -441,11 +475,11 @@ NTSTATUS TCPDisconnect
InsertTailList(&Connection->ShutdownRequest, &Bucket->Entry); InsertTailList(&Connection->ShutdownRequest, &Bucket->Entry);
ReferenceObject(Connection); ReferenceObject(Connection);
if (KeCancelTimer(&Connection->DisconnectTimer)) if (KeSetTimer(&Connection->DisconnectTimer, ActualTimeout, &Connection->DisconnectDpc))
{ {
/* Timer was already in the queue. */
DereferenceObject(Connection); DereferenceObject(Connection);
} }
KeSetTimer(&Connection->DisconnectTimer, ActualTimeout, &Connection->DisconnectDpc);
Status = STATUS_PENDING; Status = STATUS_PENDING;
} }
@ -453,20 +487,26 @@ NTSTATUS TCPDisconnect
if ((Flags & TDI_DISCONNECT_ABORT) || !Flags) if ((Flags & TDI_DISCONNECT_ABORT) || !Flags)
{ {
FlushReceiveQueue(Connection, STATUS_FILE_CLOSED, FALSE); FlushReceiveQueue(Connection, STATUS_FILE_CLOSED);
FlushSendQueue(Connection, STATUS_FILE_CLOSED, FALSE); FlushSendQueue(Connection, STATUS_FILE_CLOSED);
FlushShutdownQueue(Connection, STATUS_FILE_CLOSED, FALSE); FlushShutdownQueue(Connection, STATUS_FILE_CLOSED);
ReferenceObject(Connection);
UnlockObject(Connection);
Status = TCPTranslateError(LibTCPShutdown(Connection, 1, 1)); Status = TCPTranslateError(LibTCPShutdown(Connection, 1, 1));
DereferenceObject(Connection);
}
else
{
UnlockObject(Connection);
} }
} }
else else
{ {
UnlockObject(Connection);
/* We already got closed by the other side so just return success */ /* We already got closed by the other side so just return success */
Status = STATUS_SUCCESS; Status = STATUS_SUCCESS;
} }
UnlockObject(Connection, OldIrql);
TI_DbgPrint(DEBUG_TCP,("[IP, TCPDisconnect] Leaving. Status = 0x%x\n", Status)); TI_DbgPrint(DEBUG_TCP,("[IP, TCPDisconnect] Leaving. Status = 0x%x\n", Status));
return Status; return Status;
@ -506,7 +546,9 @@ NTSTATUS TCPReceiveData
Bucket->Request.RequestNotifyObject = Complete; Bucket->Request.RequestNotifyObject = Complete;
Bucket->Request.RequestContext = Context; Bucket->Request.RequestContext = Context;
ExInterlockedInsertTailList( &Connection->ReceiveRequest, &Bucket->Entry, &Connection->Lock ); LockObject(Connection);
InsertTailList( &Connection->ReceiveRequest, &Bucket->Entry );
UnlockObject(Connection);
TI_DbgPrint(DEBUG_TCP,("[IP, TCPReceiveData] Queued read irp\n")); TI_DbgPrint(DEBUG_TCP,("[IP, TCPReceiveData] Queued read irp\n"));
TI_DbgPrint(DEBUG_TCP,("[IP, TCPReceiveData] Leaving. Status = STATUS_PENDING\n")); TI_DbgPrint(DEBUG_TCP,("[IP, TCPReceiveData] Leaving. Status = STATUS_PENDING\n"));
@ -532,9 +574,8 @@ NTSTATUS TCPSendData
{ {
NTSTATUS Status; NTSTATUS Status;
PTDI_BUCKET Bucket; PTDI_BUCKET Bucket;
KIRQL OldIrql;
LockObject(Connection, &OldIrql); ReferenceObject(Connection);
TI_DbgPrint(DEBUG_TCP,("[IP, TCPSendData] Called for %d bytes (on socket %x)\n", TI_DbgPrint(DEBUG_TCP,("[IP, TCPSendData] Called for %d bytes (on socket %x)\n",
SendLength, Connection->SocketContext)); SendLength, Connection->SocketContext));
@ -558,7 +599,7 @@ NTSTATUS TCPSendData
Bucket = ExAllocateFromNPagedLookasideList(&TdiBucketLookasideList); Bucket = ExAllocateFromNPagedLookasideList(&TdiBucketLookasideList);
if (!Bucket) if (!Bucket)
{ {
UnlockObject(Connection, OldIrql); DereferenceObject(Connection);
TI_DbgPrint(DEBUG_TCP,("[IP, TCPSendData] Failed to allocate bucket\n")); TI_DbgPrint(DEBUG_TCP,("[IP, TCPSendData] Failed to allocate bucket\n"));
return STATUS_NO_MEMORY; return STATUS_NO_MEMORY;
} }
@ -566,13 +607,15 @@ NTSTATUS TCPSendData
Bucket->Request.RequestNotifyObject = Complete; Bucket->Request.RequestNotifyObject = Complete;
Bucket->Request.RequestContext = Context; Bucket->Request.RequestContext = Context;
LockObject(Connection);
InsertTailList( &Connection->SendRequest, &Bucket->Entry ); InsertTailList( &Connection->SendRequest, &Bucket->Entry );
TI_DbgPrint(DEBUG_TCP,("[IP, TCPSendData] Queued write irp\n")); TI_DbgPrint(DEBUG_TCP,("[IP, TCPSendData] Queued write irp\n"));
UnlockObject(Connection);
} }
UnlockObject(Connection, OldIrql);
TI_DbgPrint(DEBUG_TCP, ("[IP, TCPSendData] Leaving. Status = %x\n", Status)); TI_DbgPrint(DEBUG_TCP, ("[IP, TCPSendData] Leaving. Status = %x\n", Status));
DereferenceObject(Connection);
return Status; return Status;
} }
@ -606,13 +649,12 @@ NTSTATUS TCPGetSockAddress
PTA_IP_ADDRESS AddressIP = (PTA_IP_ADDRESS)Address; PTA_IP_ADDRESS AddressIP = (PTA_IP_ADDRESS)Address;
struct ip_addr ipaddr; struct ip_addr ipaddr;
NTSTATUS Status; NTSTATUS Status;
KIRQL OldIrql;
AddressIP->TAAddressCount = 1; AddressIP->TAAddressCount = 1;
AddressIP->Address[0].AddressLength = TDI_ADDRESS_LENGTH_IP; AddressIP->Address[0].AddressLength = TDI_ADDRESS_LENGTH_IP;
AddressIP->Address[0].AddressType = TDI_ADDRESS_TYPE_IP; AddressIP->Address[0].AddressType = TDI_ADDRESS_TYPE_IP;
LockObject(Connection, &OldIrql); LockObject(Connection);
if (GetRemote) if (GetRemote)
{ {
@ -627,7 +669,7 @@ NTSTATUS TCPGetSockAddress
&AddressIP->Address[0].Address[0].sin_port)); &AddressIP->Address[0].Address[0].sin_port));
} }
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
AddressIP->Address[0].Address[0].in_addr = ipaddr.addr; AddressIP->Address[0].Address[0].in_addr = ipaddr.addr;
@ -641,7 +683,6 @@ BOOLEAN TCPRemoveIRP( PCONNECTION_ENDPOINT Endpoint, PIRP Irp )
{ {
PLIST_ENTRY Entry; PLIST_ENTRY Entry;
PLIST_ENTRY ListHead[5]; PLIST_ENTRY ListHead[5];
KIRQL OldIrql;
PTDI_BUCKET Bucket; PTDI_BUCKET Bucket;
UINT i = 0; UINT i = 0;
BOOLEAN Found = FALSE; BOOLEAN Found = FALSE;
@ -652,7 +693,7 @@ BOOLEAN TCPRemoveIRP( PCONNECTION_ENDPOINT Endpoint, PIRP Irp )
ListHead[3] = &Endpoint->ListenRequest; ListHead[3] = &Endpoint->ListenRequest;
ListHead[4] = &Endpoint->ShutdownRequest; ListHead[4] = &Endpoint->ShutdownRequest;
LockObject(Endpoint, &OldIrql); LockObject(Endpoint);
for( i = 0; i < 5; i++ ) for( i = 0; i < 5; i++ )
{ {
@ -671,7 +712,7 @@ BOOLEAN TCPRemoveIRP( PCONNECTION_ENDPOINT Endpoint, PIRP Irp )
} }
} }
UnlockObject(Endpoint, OldIrql); UnlockObject(Endpoint);
return Found; return Found;
} }

View file

@ -171,9 +171,8 @@ NTSTATUS UDPSendDatagram(
USHORT RemotePort; USHORT RemotePort;
NTSTATUS Status; NTSTATUS Status;
PNEIGHBOR_CACHE_ENTRY NCE; PNEIGHBOR_CACHE_ENTRY NCE;
KIRQL OldIrql;
LockObject(AddrFile, &OldIrql); LockObject(AddrFile);
TI_DbgPrint(MID_TRACE,("Sending Datagram(%x %x %x %d)\n", TI_DbgPrint(MID_TRACE,("Sending Datagram(%x %x %x %d)\n",
AddrFile, ConnInfo, BufferData, DataSize)); AddrFile, ConnInfo, BufferData, DataSize));
@ -188,7 +187,7 @@ NTSTATUS UDPSendDatagram(
break; break;
default: default:
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
return STATUS_UNSUCCESSFUL; return STATUS_UNSUCCESSFUL;
} }
@ -200,7 +199,7 @@ NTSTATUS UDPSendDatagram(
* interface we're sending over * interface we're sending over
*/ */
if(!(NCE = RouteGetRouteToDestination( &RemoteAddress ))) { if(!(NCE = RouteGetRouteToDestination( &RemoteAddress ))) {
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
return STATUS_NETWORK_UNREACHABLE; return STATUS_NETWORK_UNREACHABLE;
} }
@ -209,7 +208,7 @@ NTSTATUS UDPSendDatagram(
else else
{ {
if(!(NCE = NBLocateNeighbor( &LocalAddress, NULL ))) { if(!(NCE = NBLocateNeighbor( &LocalAddress, NULL ))) {
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
return STATUS_INVALID_PARAMETER; return STATUS_INVALID_PARAMETER;
} }
} }
@ -223,7 +222,7 @@ NTSTATUS UDPSendDatagram(
BufferData, BufferData,
DataSize ); DataSize );
UnlockObject(AddrFile, OldIrql); UnlockObject(AddrFile);
if( !NT_SUCCESS(Status) ) if( !NT_SUCCESS(Status) )
return Status; return Status;

View file

@ -31,6 +31,9 @@ struct lwip_callback_msg
struct { struct {
PVOID Arg; PVOID Arg;
} Socket; } Socket;
struct {
struct tcp_pcb* pcb;
} FreeSocket;
struct { struct {
PCONNECTION_ENDPOINT Connection; PCONNECTION_ENDPOINT Connection;
struct ip_addr *IpAddress; struct ip_addr *IpAddress;
@ -99,6 +102,7 @@ extern void TCPRecvEventHandler(void *arg);
/* TCP functions */ /* TCP functions */
PTCP_PCB LibTCPSocket(void *arg); PTCP_PCB LibTCPSocket(void *arg);
VOID LibTCPFreeSocket(PTCP_PCB pcb);
err_t LibTCPBind(PCONNECTION_ENDPOINT Connection, struct ip_addr *const ipaddr, const u16_t port); err_t LibTCPBind(PCONNECTION_ENDPOINT Connection, struct ip_addr *const ipaddr, const u16_t port);
PTCP_PCB LibTCPListen(PCONNECTION_ENDPOINT Connection, const u8_t backlog); PTCP_PCB LibTCPListen(PCONNECTION_ENDPOINT Connection, const u8_t backlog);
err_t LibTCPSend(PCONNECTION_ENDPOINT Connection, void *const dataptr, const u16_t len, u32_t *sent, const int safe); err_t LibTCPSend(PCONNECTION_ENDPOINT Connection, void *const dataptr, const u16_t len, u32_t *sent, const int safe);

View file

@ -81,7 +81,9 @@ void LibTCPEnqueuePacket(PCONNECTION_ENDPOINT Connection, struct pbuf *p)
qp->p = p; qp->p = p;
qp->Offset = 0; qp->Offset = 0;
ExInterlockedInsertTailList(&Connection->PacketQueue, &qp->ListEntry, &Connection->Lock); LockObject(Connection);
InsertTailList(&Connection->PacketQueue, &qp->ListEntry);
UnlockObject(Connection);
} }
PQUEUE_ENTRY LibTCPDequeuePacket(PCONNECTION_ENDPOINT Connection) PQUEUE_ENTRY LibTCPDequeuePacket(PCONNECTION_ENDPOINT Connection)
@ -104,11 +106,10 @@ NTSTATUS LibTCPGetDataFromConnectionQueue(PCONNECTION_ENDPOINT Connection, PUCHA
struct pbuf* p; struct pbuf* p;
NTSTATUS Status; NTSTATUS Status;
UINT ReadLength, PayloadLength, Offset, Copied; UINT ReadLength, PayloadLength, Offset, Copied;
KIRQL OldIrql;
(*Received) = 0; (*Received) = 0;
LockObject(Connection, &OldIrql); LockObject(Connection);
if (!IsListEmpty(&Connection->PacketQueue)) if (!IsListEmpty(&Connection->PacketQueue))
{ {
@ -132,13 +133,9 @@ NTSTATUS LibTCPGetDataFromConnectionQueue(PCONNECTION_ENDPOINT Connection, PUCHA
qp = NULL; qp = NULL;
} }
UnlockObject(Connection, OldIrql);
Copied = pbuf_copy_partial(p, RecvBuffer, ReadLength, Offset); Copied = pbuf_copy_partial(p, RecvBuffer, ReadLength, Offset);
ASSERT(Copied == ReadLength); ASSERT(Copied == ReadLength);
LockObject(Connection, &OldIrql);
/* Update trackers */ /* Update trackers */
RecvLen -= ReadLength; RecvLen -= ReadLength;
RecvBuffer += ReadLength; RecvBuffer += ReadLength;
@ -172,7 +169,7 @@ NTSTATUS LibTCPGetDataFromConnectionQueue(PCONNECTION_ENDPOINT Connection, PUCHA
Status = STATUS_PENDING; Status = STATUS_PENDING;
} }
UnlockObject(Connection, OldIrql); UnlockObject(Connection);
return Status; return Status;
} }
@ -364,6 +361,33 @@ LibTCPSocket(void *arg)
return NULL; return NULL;
} }
static
void
LibTCPFreeSocketCallback(void *arg)
{
struct lwip_callback_msg *msg = arg;
ASSERT(msg);
/* Calling tcp_close will free it */
tcp_close(msg->Input.FreeSocket.pcb);
KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
}
void LibTCPFreeSocket(PTCP_PCB pcb)
{
struct lwip_callback_msg msg;
KeInitializeEvent(&msg.Event, NotificationEvent, FALSE);
msg.Input.FreeSocket.pcb = pcb;
tcpip_callback_with_block(LibTCPFreeSocketCallback, &msg, 1);
WaitForEventSafely(&msg.Event);
}
static static
void void
LibTCPBindCallback(void *arg) LibTCPBindCallback(void *arg)