Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3085 lines
94 KiB

  1. // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil -*- (for GNU Emacs)
  2. //
  3. // Copyright (c) 1985-2000 Microsoft Corporation
  4. //
  5. // This file is part of the Microsoft Research IPv6 Network Protocol Stack.
  6. // You should have received a copy of the Microsoft End-User License Agreement
  7. // for this software along with this release; see the file "license.txt".
  8. // If not, please see http://www.research.microsoft.com/msripv6/license.htm,
  9. // or write to Microsoft Research, One Microsoft Way, Redmond, WA 98052-6399.
  10. //
  11. // Abstract:
  12. //
  13. // NT specific routines for dispatching and handling IRPs.
  14. //
  15. #include <oscfg.h>
  16. #include <ndis.h>
  17. #include <tdikrnl.h>
  18. #include <tdint.h>
  19. #include <tdistat.h>
  20. #include <tdiinfo.h>
  21. #include <ip6imp.h>
  22. #include <ip6def.h>
  23. #include <ntddip6.h>
  24. #include "queue.h"
  25. #include "transprt.h"
  26. #include "addr.h"
  27. #include "tcp.h"
  28. #include "udp.h"
  29. #include "raw.h"
  30. #include <ntddtcp.h>
  31. #include "tcpcfg.h"
  32. #include "tcpconn.h"
  33. #include "tdilocal.h"
  34. //
  35. // Macros
  36. //
  37. //* Convert100nsToMillisconds
  38. //
  39. // Converts time expressed in hundreds of nanoseconds to milliseconds.
  40. //
  41. // REVIEW: replace RtlExtendedMagicDivide with 64 bit compiler support?
  42. //
  43. // LARGE_INTEGER // Returns: Time in milliseconds.
  44. // Convert100nsToMilliseconds(
  45. // IN LARGE_INTEGER HnsTime); // Time in hundreds of nanoseconds.
  46. //
  47. #define SHIFT10000 13
  48. static LARGE_INTEGER Magic10000 = {0xe219652c, 0xd1b71758};
  49. #define Convert100nsToMilliseconds(HnsTime) \
  50. RtlExtendedMagicDivide((HnsTime), Magic10000, SHIFT10000)
  51. //
  52. // Global variables
  53. //
  54. extern PSECURITY_DESCRIPTOR TcpAdminSecurityDescriptor;
  55. extern PDEVICE_OBJECT TCPDeviceObject, UDPDeviceObject;
  56. extern PDEVICE_OBJECT IPDeviceObject;
  57. extern PDEVICE_OBJECT RawIPDeviceObject;
  58. //
  59. // Local types
  60. //
  61. typedef struct {
  62. PIRP Irp;
  63. PMDL InputMdl;
  64. PMDL OutputMdl;
  65. TCP_REQUEST_QUERY_INFORMATION_EX QueryInformation;
  66. } TCP_QUERY_CONTEXT, *PTCP_QUERY_CONTEXT;
  67. //
  68. // General external function prototypes
  69. //
  70. extern
  71. NTSTATUS
  72. IPDispatch(
  73. IN PDEVICE_OBJECT DeviceObject,
  74. IN PIRP Irp
  75. );
  76. //
  77. // Other external functions
  78. //
  79. void
  80. TCPAbortAndIndicateDisconnect(
  81. CONNECTION_CONTEXT ConnnectionContext
  82. );
  83. //
  84. // Local pageable function prototypes
  85. //
  86. NTSTATUS
  87. TCPDispatchDeviceControl(
  88. IN PIRP Irp,
  89. IN PIO_STACK_LOCATION IrpSp
  90. );
  91. NTSTATUS
  92. TCPCreate(
  93. IN PDEVICE_OBJECT DeviceObject,
  94. IN PIRP Irp,
  95. IN PIO_STACK_LOCATION IrpSp
  96. );
  97. NTSTATUS
  98. TCPAssociateAddress(
  99. IN PIRP Irp,
  100. IN PIO_STACK_LOCATION IrpSp
  101. );
  102. NTSTATUS
  103. TCPSetEventHandler(
  104. IN PIRP Irp,
  105. IN PIO_STACK_LOCATION IrpSp
  106. );
  107. NTSTATUS
  108. TCPQueryInformation(
  109. IN PIRP Irp,
  110. IN PIO_STACK_LOCATION IrpSp
  111. );
  112. FILE_FULL_EA_INFORMATION UNALIGNED *
  113. FindEA(
  114. PFILE_FULL_EA_INFORMATION StartEA,
  115. CHAR *TargetName,
  116. USHORT TargetNameLength
  117. );
  118. BOOLEAN
  119. IsAdminIoRequest(
  120. PIRP Irp,
  121. PIO_STACK_LOCATION IrpSp
  122. );
  123. BOOLEAN
  124. IsDHCPZeroAddress(
  125. TRANSPORT_ADDRESS UNALIGNED *AddrList
  126. );
  127. ULONG
  128. RawExtractProtocolNumber(
  129. IN PUNICODE_STRING FileName
  130. );
  131. NTSTATUS
  132. CaptureCreatorSD(
  133. PIRP Irp,
  134. PIO_STACK_LOCATION IrpSp,
  135. OUT PSECURITY_DESCRIPTOR* CreatorSD
  136. );
  137. NTSTATUS
  138. TCPEnumerateConnectionList(
  139. IN PIRP Irp,
  140. IN PIO_STACK_LOCATION IrpSp
  141. );
  142. //
  143. // Local helper routine prototypes.
  144. //
  145. ULONG
  146. TCPGetMdlChainByteCount(
  147. PMDL Mdl
  148. );
  149. //
  150. // All of this code is pageable.
  151. //
  152. #ifdef ALLOC_PRAGMA
  153. #pragma alloc_text(PAGE, TCPDispatchDeviceControl)
  154. #pragma alloc_text(PAGE, TCPCreate)
  155. #pragma alloc_text(PAGE, TCPAssociateAddress)
  156. #pragma alloc_text(PAGE, TCPSetEventHandler)
  157. #pragma alloc_text(PAGE, FindEA)
  158. #pragma alloc_text(PAGE, IsDHCPZeroAddress)
  159. #pragma alloc_text(PAGE, RawExtractProtocolNumber)
  160. #pragma alloc_text(PAGE, IsAdminIoRequest)
  161. #pragma alloc_text(PAGE, CaptureCreatorSD)
  162. #endif // ALLOC_PRAGMA
  163. //
  164. // Generic Irp completion and cancellation routines.
  165. //
  166. //* TCPDataRequestComplete - Completes a UDP/TCP send/receive request.
  167. //
  168. NTSTATUS // Returns: Nothing.
  169. TCPDataRequestComplete(
  170. void *Context, // A pointer to the IRP for this request.
  171. unsigned int Status, // The final TDI status of the request.
  172. unsigned int ByteCount) // Bytes sent/received information.
  173. {
  174. KIRQL oldIrql;
  175. PIRP irp;
  176. PIO_STACK_LOCATION irpSp;
  177. PTCP_CONTEXT tcpContext;
  178. irp = (PIRP) Context;
  179. irpSp = IoGetCurrentIrpStackLocation(irp);
  180. tcpContext = (PTCP_CONTEXT) irpSp->FileObject->FsContext;
  181. if (IoSetCancelRoutine(irp, NULL) == NULL) {
  182. //
  183. // In case an old cancel routine is still running,
  184. // synchronize with it.
  185. //
  186. IoAcquireCancelSpinLock(&oldIrql);
  187. IoReleaseCancelSpinLock(oldIrql);
  188. }
  189. KeAcquireSpinLock(&tcpContext->EndpointLock, &oldIrql);
  190. #if DBG
  191. IF_TCPDBG(TCP_DEBUG_CANCEL) {
  192. PLIST_ENTRY entry, listHead;
  193. PIRP item = NULL;
  194. if (irp->Cancel) {
  195. ASSERT(irp->CancelRoutine == NULL);
  196. listHead = &(tcpContext->CancelledIrpList);
  197. } else {
  198. listHead = &(tcpContext->PendingIrpList);
  199. }
  200. //
  201. // Verify that the Irp is on the appropriate list.
  202. //
  203. for (entry = listHead->Flink; entry != listHead;
  204. entry = entry->Flink) {
  205. item = CONTAINING_RECORD(entry, IRP, Tail.Overlay.ListEntry);
  206. if (item == irp) {
  207. RemoveEntryList(&(irp->Tail.Overlay.ListEntry));
  208. break;
  209. }
  210. }
  211. ASSERT(item == irp);
  212. }
  213. #endif
  214. if ((Status == TDI_CANCELLED) && ByteCount) {
  215. Status = STATUS_SUCCESS;
  216. } else {
  217. if (irp->Cancel || tcpContext->CancelIrps) {
  218. IF_TCPDBG(TCP_DEBUG_IRP) {
  219. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  220. "TCPDataRequestComplete: Irp %lx was cancelled\n",
  221. irp));
  222. }
  223. Status = (unsigned int) STATUS_CANCELLED;
  224. ByteCount = 0;
  225. }
  226. }
  227. ASSERT(tcpContext->ReferenceCount > 0);
  228. IF_TCPDBG(TCP_DEBUG_IRP) {
  229. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  230. "TCPDataRequestComplete: "
  231. "Irp %lx fileobj %lx refcnt dec to %u\n",
  232. irp, irpSp->FileObject, tcpContext->ReferenceCount - 1));
  233. }
  234. if (--(tcpContext->ReferenceCount) == 0) {
  235. IF_TCPDBG(TCP_DEBUG_CANCEL) {
  236. ASSERT(IsListEmpty(&(tcpContext->CancelledIrpList)));
  237. ASSERT(IsListEmpty(&(tcpContext->PendingIrpList)));
  238. }
  239. KeReleaseSpinLock(&tcpContext->EndpointLock, oldIrql);
  240. //
  241. // Since the reference count on the tcpContext is now zero,
  242. // setting this event must be the last place we touch it.
  243. //
  244. KeSetEvent(&(tcpContext->CleanupEvent), 0, FALSE);
  245. } else {
  246. KeReleaseSpinLock(&tcpContext->EndpointLock, oldIrql);
  247. }
  248. IF_TCPDBG(TCP_DEBUG_IRP) {
  249. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  250. "TCPDataRequestComplete: completing irp %lx, status %lx,"
  251. " byte count %lx\n", irp, Status, ByteCount));
  252. }
  253. irp->IoStatus.Status = (NTSTATUS) Status;
  254. irp->IoStatus.Information = ByteCount;
  255. IoCompleteRequest(irp, IO_NETWORK_INCREMENT);
  256. return Status;
  257. } // TCPDataRequestComplete
  258. //* TCPRequestComplete - Completes a TDI request.
  259. //
  260. // Completes a cancellable TDI request which returns no data by
  261. // calling TCPDataRequestComplete with a ByteCount of zero.
  262. //
  263. void // Returns: Nothing.
  264. TCPRequestComplete(
  265. void *Context, // A pointer to the IRP for this request.
  266. unsigned int Status, // The final TDI status of the request.
  267. unsigned int UnUsed) // An unused parameter.
  268. {
  269. UNREFERENCED_PARAMETER(UnUsed);
  270. TCPDataRequestComplete(Context, Status, 0);
  271. } // TCPRequestComplete
  272. //* TCPNonCancellableRequestComplete - Complete uncancellable TDI request.
  273. //
  274. // Completes a TDI request which cannot be cancelled.
  275. //
  276. void // Returns: Nothing.
  277. TCPNonCancellableRequestComplete(
  278. void *Context, // A pointer to the IRP for this request.
  279. unsigned int Status, // The final TDI status of the request.
  280. unsigned int UnUsed) // An unused parameter.
  281. {
  282. PIRP irp;
  283. PIO_STACK_LOCATION irpSp;
  284. UNREFERENCED_PARAMETER(UnUsed);
  285. irp = (PIRP) Context;
  286. irpSp = IoGetCurrentIrpStackLocation(irp);
  287. IF_TCPDBG(TCP_DEBUG_CLOSE) {
  288. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  289. "TCPNonCancellableRequestComplete: irp %lx status %lx\n",
  290. irp, Status));
  291. }
  292. //
  293. // Complete the IRP.
  294. //
  295. irp->IoStatus.Status = (NTSTATUS) Status;
  296. irp->IoStatus.Information = 0;
  297. IoCompleteRequest(irp, IO_NETWORK_INCREMENT);
  298. return;
  299. } // TCPNonCancellableRequestComplete
  300. //* TCPCancelComplete
  301. //
  302. void
  303. TCPCancelComplete(
  304. void *Context,
  305. unsigned int Unused1,
  306. unsigned int Unused2)
  307. {
  308. PFILE_OBJECT fileObject = (PFILE_OBJECT) Context;
  309. PTCP_CONTEXT tcpContext = (PTCP_CONTEXT) fileObject->FsContext;
  310. KIRQL oldIrql;
  311. UNREFERENCED_PARAMETER(Unused1);
  312. UNREFERENCED_PARAMETER(Unused2);
  313. KeAcquireSpinLock(&tcpContext->EndpointLock, &oldIrql);
  314. //
  315. // Remove the reference placed on the endpoint by the cancel routine.
  316. // The cancelled Irp will be completed by the completion routine for the
  317. // request.
  318. //
  319. if (--(tcpContext->ReferenceCount) == 0) {
  320. IF_TCPDBG(TCP_DEBUG_CANCEL) {
  321. ASSERT(IsListEmpty(&(tcpContext->CancelledIrpList)));
  322. ASSERT(IsListEmpty(&(tcpContext->PendingIrpList)));
  323. }
  324. //
  325. // Set the cleanup event.
  326. //
  327. KeReleaseSpinLock(&tcpContext->EndpointLock, oldIrql);
  328. KeSetEvent(&(tcpContext->CleanupEvent), 0, FALSE);
  329. return;
  330. }
  331. IF_TCPDBG(TCP_DEBUG_IRP) {
  332. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  333. "TCPCancelComplete: fileobj %lx refcnt dec to %u\n",
  334. fileObject, tcpContext->ReferenceCount));
  335. }
  336. KeReleaseSpinLock(&tcpContext->EndpointLock, oldIrql);
  337. return;
  338. } // TCPCancelComplete
  339. //* TCPCancelRequest - Cancels an outstanding Irp.
  340. //
  341. // Cancel an outstanding Irp.
  342. //
  343. VOID // Returns: Nothing.
  344. TCPCancelRequest(
  345. PDEVICE_OBJECT Device, // Pointer to the device object for this request.
  346. PIRP Irp) // Pointer to I/O request packet.
  347. {
  348. PIO_STACK_LOCATION irpSp;
  349. PTCP_CONTEXT tcpContext;
  350. NTSTATUS status = STATUS_SUCCESS;
  351. PFILE_OBJECT fileObject;
  352. UCHAR minorFunction;
  353. TDI_REQUEST request;
  354. KIRQL CancelIrql;
  355. UNREFERENCED_PARAMETER(Device);
  356. irpSp = IoGetCurrentIrpStackLocation(Irp);
  357. fileObject = irpSp->FileObject;
  358. tcpContext = (PTCP_CONTEXT) fileObject->FsContext;
  359. minorFunction = irpSp->MinorFunction;
  360. CancelIrql = Irp->CancelIrql;
  361. KeAcquireSpinLockAtDpcLevel(&tcpContext->EndpointLock);
  362. ASSERT(Irp->Cancel);
  363. IoReleaseCancelSpinLock(DISPATCH_LEVEL);
  364. IF_TCPDBG(TCP_DEBUG_IRP) {
  365. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  366. "TCPCancelRequest: cancelling irp %lx, file object %lx\n",
  367. Irp, fileObject));
  368. }
  369. #if DBG
  370. IF_TCPDBG(TCP_DEBUG_CANCEL) {
  371. //
  372. // Verify that the Irp is on the pending list.
  373. //
  374. PLIST_ENTRY entry;
  375. PIRP item = NULL;
  376. for (entry = tcpContext->PendingIrpList.Flink;
  377. entry != &(tcpContext->PendingIrpList); entry = entry->Flink) {
  378. item = CONTAINING_RECORD(entry, IRP, Tail.Overlay.ListEntry);
  379. if (item == Irp) {
  380. RemoveEntryList( &(Irp->Tail.Overlay.ListEntry));
  381. break;
  382. }
  383. }
  384. ASSERT(item == Irp);
  385. InsertTailList(&(tcpContext->CancelledIrpList),
  386. &(Irp->Tail.Overlay.ListEntry));
  387. }
  388. #endif // DBG
  389. //
  390. // Add a reference so the object can't be closed while the cancel routine
  391. // is executing.
  392. //
  393. ASSERT(tcpContext->ReferenceCount > 0);
  394. tcpContext->ReferenceCount++;
  395. IF_TCPDBG(TCP_DEBUG_IRP) {
  396. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  397. "TCPCancelRequest: Irp %lx fileobj %lx refcnt inc to %u\n",
  398. Irp, fileObject, tcpContext->ReferenceCount));
  399. }
  400. //
  401. // Try to cancel the request.
  402. //
  403. switch(minorFunction) {
  404. case TDI_SEND:
  405. case TDI_RECEIVE:
  406. KeReleaseSpinLock(&tcpContext->EndpointLock, CancelIrql);
  407. ASSERT((PtrToUlong(fileObject->FsContext2)) == TDI_CONNECTION_FILE);
  408. #ifndef UDP_ONLY
  409. TCPAbortAndIndicateDisconnect(tcpContext->Handle.ConnectionContext);
  410. #endif
  411. break;
  412. case TDI_SEND_DATAGRAM:
  413. ASSERT(PtrToUlong(fileObject->FsContext2) == TDI_TRANSPORT_ADDRESS_FILE);
  414. TdiCancelSendDatagram(tcpContext->Handle.AddressHandle, Irp,
  415. &tcpContext->EndpointLock, CancelIrql);
  416. break;
  417. case TDI_RECEIVE_DATAGRAM:
  418. ASSERT(PtrToUlong(fileObject->FsContext2) == TDI_TRANSPORT_ADDRESS_FILE);
  419. TdiCancelReceiveDatagram(tcpContext->Handle.AddressHandle, Irp,
  420. &tcpContext->EndpointLock, CancelIrql);
  421. break;
  422. case TDI_DISASSOCIATE_ADDRESS:
  423. ASSERT(PtrToUlong(fileObject->FsContext2) == TDI_CONNECTION_FILE);
  424. //
  425. // This pends but is not cancellable. We put it thru the cancel code
  426. // anyway so a reference is made for it and so it can be tracked in
  427. // a debug build.
  428. //
  429. KeReleaseSpinLock(&tcpContext->EndpointLock, CancelIrql);
  430. break;
  431. default:
  432. //
  433. // Initiate a disconnect to cancel the request.
  434. //
  435. KeReleaseSpinLock(&tcpContext->EndpointLock, CancelIrql);
  436. request.Handle.ConnectionContext =
  437. tcpContext->Handle.ConnectionContext;
  438. request.RequestNotifyObject = TCPCancelComplete;
  439. request.RequestContext = fileObject;
  440. status = TdiDisconnect(&request, NULL, TDI_DISCONNECT_ABORT, NULL,
  441. NULL, NULL);
  442. break;
  443. }
  444. if (status != TDI_PENDING) {
  445. TCPCancelComplete(fileObject, 0, 0);
  446. }
  447. return;
  448. } // TCPCancelRequest
  449. //* TCPPrepareIrpForCancel
  450. //
  451. NTSTATUS
  452. TCPPrepareIrpForCancel(
  453. PTCP_CONTEXT TcpContext,
  454. PIRP Irp,
  455. PDRIVER_CANCEL CancelRoutine)
  456. {
  457. KIRQL oldIrql;
  458. //
  459. // Set up for cancellation.
  460. //
  461. KeAcquireSpinLock(&TcpContext->EndpointLock, &oldIrql);
  462. ASSERT(Irp->CancelRoutine == NULL);
  463. if (!Irp->Cancel) {
  464. IoMarkIrpPending(Irp);
  465. IoSetCancelRoutine(Irp, CancelRoutine);
  466. TcpContext->ReferenceCount++;
  467. IF_TCPDBG(TCP_DEBUG_IRP) {
  468. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  469. "TCPPrepareIrpForCancel: irp %lx fileobj %lx refcnt inc"
  470. " to %u\n", Irp,
  471. (IoGetCurrentIrpStackLocation(Irp))->FileObject,
  472. TcpContext->ReferenceCount));
  473. }
  474. #if DBG
  475. IF_TCPDBG(TCP_DEBUG_CANCEL) {
  476. PLIST_ENTRY entry;
  477. PIRP item = NULL;
  478. //
  479. // Verify that the Irp has not already been submitted.
  480. //
  481. for (entry = TcpContext->PendingIrpList.Flink;
  482. entry != &(TcpContext->PendingIrpList);
  483. entry = entry->Flink) {
  484. item = CONTAINING_RECORD(entry, IRP, Tail.Overlay.ListEntry);
  485. ASSERT(item != Irp);
  486. }
  487. for (entry = TcpContext->CancelledIrpList.Flink;
  488. entry != &(TcpContext->CancelledIrpList);
  489. entry = entry->Flink) {
  490. item = CONTAINING_RECORD(entry, IRP, Tail.Overlay.ListEntry);
  491. ASSERT(item != Irp);
  492. }
  493. InsertTailList(&(TcpContext->PendingIrpList),
  494. &(Irp->Tail.Overlay.ListEntry));
  495. }
  496. #endif // DBG
  497. KeReleaseSpinLock(&TcpContext->EndpointLock, oldIrql);
  498. return(STATUS_SUCCESS);
  499. }
  500. //
  501. // The IRP has already been cancelled. Complete it now.
  502. //
  503. IF_TCPDBG(TCP_DEBUG_IRP) {
  504. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  505. "TCP: irp %lx already cancelled, completing.\n", Irp));
  506. }
  507. KeReleaseSpinLock(&TcpContext->EndpointLock, oldIrql);
  508. Irp->IoStatus.Status = STATUS_CANCELLED;
  509. Irp->IoStatus.Information = 0;
  510. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  511. return(STATUS_CANCELLED);
  512. } // TCPPrepareIrpForCancel
  513. //
  514. // TDI functions.
  515. //
  516. //* TCPAssociateAddress - Handle TDI Associate Address IRP.
  517. //
  518. // Converts a TDI Associate Address IRP into a call to TdiAssociateAddress.
  519. //
  520. // This routine does not pend.
  521. //
  522. NTSTATUS // Returns: indication of whether the request was successful.
  523. TCPAssociateAddress(
  524. IN PIRP Irp, // I/O request packet.
  525. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  526. {
  527. NTSTATUS status;
  528. TDI_REQUEST request;
  529. PTCP_CONTEXT tcpContext;
  530. PTDI_REQUEST_KERNEL_ASSOCIATE associateInformation;
  531. PFILE_OBJECT fileObject;
  532. PAGED_CODE();
  533. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  534. request.Handle.ConnectionContext = tcpContext->Handle.ConnectionContext;
  535. associateInformation =
  536. (PTDI_REQUEST_KERNEL_ASSOCIATE) &(IrpSp->Parameters);
  537. //
  538. // Get the file object for the address. Then extract the Address Handle
  539. // from the TCP_CONTEXT associated with it.
  540. //
  541. status = ObReferenceObjectByHandle(associateInformation->AddressHandle,
  542. 0, *IoFileObjectType, Irp->RequestorMode,
  543. &fileObject, NULL);
  544. if (NT_SUCCESS(status)) {
  545. if ((fileObject->DeviceObject == TCPDeviceObject) &&
  546. (PtrToUlong(fileObject->FsContext2) == TDI_TRANSPORT_ADDRESS_FILE)) {
  547. tcpContext = (PTCP_CONTEXT) fileObject->FsContext;
  548. status = TdiAssociateAddress(&request,
  549. tcpContext->Handle.AddressHandle);
  550. ASSERT(status != STATUS_PENDING);
  551. ObDereferenceObject(fileObject);
  552. IF_TCPDBG(TCP_DEBUG_ASSOCIATE) {
  553. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  554. "TCPAssociateAddress complete on file object %lx\n",
  555. IrpSp->FileObject));
  556. }
  557. } else {
  558. ObDereferenceObject(fileObject);
  559. status = STATUS_INVALID_HANDLE;
  560. IF_TCPDBG(TCP_DEBUG_ASSOCIATE) {
  561. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  562. "TCPAssociateAddress: ObReference failed on object"
  563. " %lx, status %lx\n",
  564. associateInformation->AddressHandle, status));
  565. }
  566. }
  567. } else {
  568. IF_TCPDBG(TCP_DEBUG_ASSOCIATE) {
  569. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  570. "TCPAssociateAddress: ObReference failed on object %lx,"
  571. " status %lx\n", associateInformation->AddressHandle,
  572. status));
  573. }
  574. }
  575. return(status);
  576. }
  577. //* TCPDisassociateAddress - Handle TDI Disassociate Address IRP.
  578. //
  579. // Converts a TDI Disassociate Address IRP into a call to
  580. // TdiDisassociateAddress.
  581. NTSTATUS // Returns: Indication of whether the request was successful.
  582. TCPDisassociateAddress(
  583. IN PIRP Irp, // I/O request packet.
  584. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  585. {
  586. NTSTATUS status;
  587. TDI_REQUEST request;
  588. PTCP_CONTEXT tcpContext;
  589. IF_TCPDBG(TCP_DEBUG_ASSOCIATE) {
  590. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  591. "TCP disassociating address\n"));
  592. }
  593. ASSERT(PtrToUlong(IrpSp->FileObject->FsContext2) == TDI_CONNECTION_FILE);
  594. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  595. request.Handle.ConnectionContext = tcpContext->Handle.ConnectionContext;
  596. request.RequestNotifyObject = TCPRequestComplete;
  597. request.RequestContext = Irp;
  598. status = TCPPrepareIrpForCancel(tcpContext, Irp, TCPCancelRequest);
  599. if (NT_SUCCESS(status)) {
  600. status = TdiDisAssociateAddress(&request);
  601. if (status != TDI_PENDING) {
  602. TCPRequestComplete(Irp, status, 0);
  603. }
  604. //
  605. // Return PENDING because TCPPrepareIrpForCancel marks Irp as PENDING.
  606. //
  607. return(TDI_PENDING);
  608. }
  609. return(status);
  610. } // TCPDisassociateAddress
  611. //* TCPConnect - Handle TDI Connect IRP.
  612. //
  613. // Converts a TDI Connect IRP into a call to TdiConnect.
  614. //
  615. NTSTATUS // Returns: Whether the request was successfully queued.
  616. TCPConnect(
  617. IN PIRP Irp, // Pointer to I/O request packet.
  618. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  619. {
  620. NTSTATUS status;
  621. PTCP_CONTEXT tcpContext;
  622. TDI_REQUEST request;
  623. PTDI_CONNECTION_INFORMATION requestInformation, returnInformation;
  624. PTDI_REQUEST_KERNEL_CONNECT connectRequest;
  625. LARGE_INTEGER millisecondTimeout;
  626. PLARGE_INTEGER requestTimeout;
  627. IF_TCPDBG(TCP_DEBUG_CONNECT) {
  628. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  629. "TCPConnect irp %lx, file object %lx\n", Irp,
  630. IrpSp->FileObject));
  631. }
  632. ASSERT(PtrToUlong(IrpSp->FileObject->FsContext2) == TDI_CONNECTION_FILE);
  633. connectRequest = (PTDI_REQUEST_KERNEL_CONNECT) &(IrpSp->Parameters);
  634. requestInformation = connectRequest->RequestConnectionInformation;
  635. returnInformation = connectRequest->ReturnConnectionInformation;
  636. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  637. request.Handle.ConnectionContext = tcpContext->Handle.ConnectionContext;
  638. request.RequestNotifyObject = TCPRequestComplete;
  639. request.RequestContext = Irp;
  640. requestTimeout = (PLARGE_INTEGER) connectRequest->RequestSpecific;
  641. if (requestTimeout != NULL) {
  642. //
  643. // NT relative timeouts are negative. Negate first to get a positive
  644. // value to pass to the transport.
  645. //
  646. millisecondTimeout.QuadPart = -((*requestTimeout).QuadPart);
  647. millisecondTimeout = Convert100nsToMilliseconds(millisecondTimeout);
  648. } else {
  649. millisecondTimeout.LowPart = 0;
  650. millisecondTimeout.HighPart = 0;
  651. }
  652. ASSERT(millisecondTimeout.HighPart == 0);
  653. status = TCPPrepareIrpForCancel(tcpContext, Irp, TCPCancelRequest);
  654. if (NT_SUCCESS(status)) {
  655. status = TdiConnect(&request, ((millisecondTimeout.LowPart != 0) ?
  656. &(millisecondTimeout.LowPart) : NULL),
  657. requestInformation, returnInformation);
  658. if (status != STATUS_PENDING) {
  659. TCPRequestComplete(Irp, status, 0);
  660. }
  661. //
  662. // Return PENDING because TCPPrepareIrpForCancel marks Irp as PENDING.
  663. //
  664. return(STATUS_PENDING);
  665. }
  666. return(status);
  667. } // TCPConnect
  668. //* TCPDisconnect - Handler for TDI Disconnect IRP
  669. //
  670. // Converts a TDI Disconnect IRP into a call to TdiDisconnect.
  671. //
  672. NTSTATUS // Returns: whether the request was successfully queued.
  673. TCPDisconnect(
  674. IN PIRP Irp, // I/O request packet.
  675. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  676. {
  677. NTSTATUS status;
  678. PTCP_CONTEXT tcpContext;
  679. TDI_REQUEST request;
  680. PTDI_CONNECTION_INFORMATION requestInformation, returnInformation;
  681. PTDI_REQUEST_KERNEL_DISCONNECT disconnectRequest;
  682. LARGE_INTEGER millisecondTimeout;
  683. PLARGE_INTEGER requestTimeout;
  684. BOOLEAN abortive = FALSE;
  685. ASSERT(PtrToUlong(IrpSp->FileObject->FsContext2) == TDI_CONNECTION_FILE);
  686. disconnectRequest = (PTDI_REQUEST_KERNEL_CONNECT) &(IrpSp->Parameters);
  687. requestInformation = disconnectRequest->RequestConnectionInformation;
  688. returnInformation = disconnectRequest->ReturnConnectionInformation;
  689. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  690. request.Handle.ConnectionContext = tcpContext->Handle.ConnectionContext;
  691. request.RequestContext = Irp;
  692. //
  693. // Set up the timeout value.
  694. //
  695. if (disconnectRequest->RequestSpecific != NULL) {
  696. requestTimeout = (PLARGE_INTEGER) disconnectRequest->RequestSpecific;
  697. if ((requestTimeout->LowPart == -1) &&
  698. (requestTimeout->HighPart == -1)) {
  699. millisecondTimeout.LowPart = requestTimeout->LowPart;
  700. millisecondTimeout.HighPart = 0;
  701. } else {
  702. //
  703. // NT relative timeouts are negative. Negate first to get a
  704. // positive value to pass to the transport.
  705. //
  706. millisecondTimeout.QuadPart = -((*requestTimeout).QuadPart);
  707. millisecondTimeout = Convert100nsToMilliseconds(
  708. millisecondTimeout);
  709. }
  710. } else {
  711. millisecondTimeout.LowPart = 0;
  712. millisecondTimeout.HighPart = 0;
  713. }
  714. ASSERT(millisecondTimeout.HighPart == 0);
  715. if (disconnectRequest->RequestFlags & TDI_DISCONNECT_ABORT) {
  716. //
  717. // Abortive disconnects cannot be cancelled and must use
  718. // a specific completion routine.
  719. //
  720. abortive = TRUE;
  721. IoMarkIrpPending(Irp);
  722. request.RequestNotifyObject = TCPNonCancellableRequestComplete;
  723. status = STATUS_SUCCESS;
  724. } else {
  725. //
  726. // Non-abortive disconnects can use the generic cancellation and
  727. // completion routines.
  728. //
  729. status = TCPPrepareIrpForCancel(tcpContext, Irp, TCPCancelRequest);
  730. request.RequestNotifyObject = TCPRequestComplete;
  731. }
  732. IF_TCPDBG(TCP_DEBUG_CLOSE) {
  733. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  734. "TCPDisconnect "
  735. "irp %lx, flags %lx, fileobj %lx, abortive = %d\n",
  736. Irp, disconnectRequest->RequestFlags, IrpSp->FileObject,
  737. abortive));
  738. }
  739. if (NT_SUCCESS(status)) {
  740. status = TdiDisconnect(&request,((millisecondTimeout.LowPart != 0) ?
  741. &(millisecondTimeout.LowPart) : NULL),
  742. (ushort) disconnectRequest->RequestFlags,
  743. requestInformation, returnInformation,
  744. (TCPAbortReq*)&Irp->Tail.Overlay.DriverContext[0]);
  745. if (status != STATUS_PENDING) {
  746. if (abortive) {
  747. TCPNonCancellableRequestComplete(Irp, status, 0);
  748. } else {
  749. TCPRequestComplete(Irp, status, 0);
  750. }
  751. } else {
  752. IF_TCPDBG(TCP_DEBUG_CLOSE) {
  753. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  754. "TCPDisconnect pending irp %lx\n", Irp));
  755. }
  756. }
  757. //
  758. // return PENDING because TCPPrepareIrpForCancel marks Irp as PENDING
  759. //
  760. return(STATUS_PENDING);
  761. }
  762. return(status);
  763. } // TCPDisconnect
  764. //* TCPListen - Handler for TDI Listen IRP.
  765. //
  766. // Converts a TDI Listen IRP into a call to TdiListen.
  767. //
  768. NTSTATUS // Returns: whether or not the request was successful.
  769. TCPListen(
  770. IN PIRP Irp, // I/O request packet.
  771. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  772. {
  773. NTSTATUS status;
  774. PTCP_CONTEXT tcpContext;
  775. TDI_REQUEST request;
  776. PTDI_CONNECTION_INFORMATION requestInformation, returnInformation;
  777. PTDI_REQUEST_KERNEL_LISTEN listenRequest;
  778. IF_TCPDBG(TCP_DEBUG_CONNECT) {
  779. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  780. "TCPListen irp %lx on file object %lx\n",
  781. Irp, IrpSp->FileObject));
  782. }
  783. ASSERT(PtrToUlong(IrpSp->FileObject->FsContext2) == TDI_CONNECTION_FILE);
  784. listenRequest = (PTDI_REQUEST_KERNEL_CONNECT) &(IrpSp->Parameters);
  785. requestInformation = listenRequest->RequestConnectionInformation;
  786. returnInformation = listenRequest->ReturnConnectionInformation;
  787. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  788. request.Handle.ConnectionContext = tcpContext->Handle.ConnectionContext;
  789. request.RequestNotifyObject = TCPRequestComplete;
  790. request.RequestContext = Irp;
  791. status = TCPPrepareIrpForCancel(tcpContext, Irp, TCPCancelRequest);
  792. if (NT_SUCCESS(status)) {
  793. status = TdiListen(&request, (ushort) listenRequest->RequestFlags,
  794. requestInformation, returnInformation);
  795. if (status != TDI_PENDING) {
  796. TCPRequestComplete(Irp, status, 0);
  797. }
  798. //
  799. // return PENDING because TCPPrepareIrpForCancel marks Irp as PENDING
  800. //
  801. return(TDI_PENDING);
  802. }
  803. return(status);
  804. } // TCPListen
  805. //* TCPAccept - Handle a TDI Accept IRP.
  806. //
  807. // Converts a TDI Accept IRP into a call to TdiAccept.
  808. //
  809. NTSTATUS // Returns: whether the request was successfully queued.
  810. TCPAccept(
  811. IN PIRP Irp, // I/O request packet.
  812. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  813. {
  814. NTSTATUS status;
  815. PTCP_CONTEXT tcpContext;
  816. TDI_REQUEST request;
  817. PTDI_CONNECTION_INFORMATION requestInformation, returnInformation;
  818. PTDI_REQUEST_KERNEL_ACCEPT acceptRequest;
  819. IF_TCPDBG(TCP_DEBUG_CONNECT) {
  820. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  821. "TCPAccept irp %lx on file object %lx\n", Irp,
  822. IrpSp->FileObject));
  823. }
  824. ASSERT(PtrToUlong(IrpSp->FileObject->FsContext2) == TDI_CONNECTION_FILE);
  825. acceptRequest = (PTDI_REQUEST_KERNEL_ACCEPT) &(IrpSp->Parameters);
  826. requestInformation = acceptRequest->RequestConnectionInformation;
  827. returnInformation = acceptRequest->ReturnConnectionInformation;
  828. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  829. request.Handle.ConnectionContext = tcpContext->Handle.ConnectionContext;
  830. request.RequestNotifyObject = TCPRequestComplete;
  831. request.RequestContext = Irp;
  832. status = TCPPrepareIrpForCancel(tcpContext, Irp, TCPCancelRequest);
  833. if (NT_SUCCESS(status)) {
  834. status = TdiAccept(&request, requestInformation, returnInformation);
  835. if (status != TDI_PENDING) {
  836. TCPRequestComplete(Irp, status, 0);
  837. }
  838. //
  839. // Return PENDING because TCPPrepareIrpForCancel marks Irp as PENDING.
  840. //
  841. return(TDI_PENDING);
  842. }
  843. return(status);
  844. } // TCPAccept
  845. //* TCPSendData - Handle TDI Send IRP.
  846. //
  847. // Converts a TDI Send IRP into a call to TdiSend.
  848. //
  849. NTSTATUS
  850. TCPSendData(
  851. IN PIRP Irp, // I/O request packet.
  852. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  853. {
  854. TDI_STATUS status;
  855. TDI_REQUEST request;
  856. PTCP_CONTEXT tcpContext;
  857. PTDI_REQUEST_KERNEL_SEND requestInformation;
  858. KIRQL oldIrql;
  859. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  860. ASSERT(PtrToUlong(IrpSp->FileObject->FsContext2) == TDI_CONNECTION_FILE);
  861. requestInformation = (PTDI_REQUEST_KERNEL_SEND) &(IrpSp->Parameters);
  862. request.Handle.ConnectionContext = tcpContext->Handle.ConnectionContext;
  863. request.RequestNotifyObject = TCPDataRequestComplete;
  864. request.RequestContext = Irp;
  865. KeAcquireSpinLock(&tcpContext->EndpointLock, &oldIrql);
  866. IoSetCancelRoutine(Irp, TCPCancelRequest);
  867. if (!Irp->Cancel) {
  868. //
  869. // Set up for cancellation.
  870. //
  871. IoMarkIrpPending(Irp);
  872. tcpContext->ReferenceCount++;
  873. IF_TCPDBG(TCP_DEBUG_IRP) {
  874. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  875. "TCPSendData: irp %lx fileobj %lx refcnt inc to %u\n",
  876. Irp, IrpSp, tcpContext->ReferenceCount));
  877. }
  878. #if DBG
  879. IF_TCPDBG(TCP_DEBUG_CANCEL) {
  880. PLIST_ENTRY entry;
  881. PIRP item = NULL;
  882. //
  883. // Verify that the Irp has not already been submitted.
  884. //
  885. for (entry = tcpContext->PendingIrpList.Flink;
  886. entry != &(tcpContext->PendingIrpList);
  887. entry = entry->Flink) {
  888. item = CONTAINING_RECORD(entry, IRP, Tail.Overlay.ListEntry);
  889. ASSERT(item != Irp);
  890. }
  891. for (entry = tcpContext->CancelledIrpList.Flink;
  892. entry != &(tcpContext->CancelledIrpList);
  893. entry = entry->Flink) {
  894. item = CONTAINING_RECORD(entry, IRP, Tail.Overlay.ListEntry);
  895. ASSERT(item != Irp);
  896. }
  897. InsertTailList(&(tcpContext->PendingIrpList),
  898. &(Irp->Tail.Overlay.ListEntry));
  899. }
  900. #endif // DBG
  901. KeReleaseSpinLock(&tcpContext->EndpointLock, oldIrql);
  902. IF_TCPDBG(TCP_DEBUG_SEND) {
  903. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  904. "TCPSendData irp %lx sending %d bytes, flags %lx,"
  905. " fileobj %lx\n", Irp, requestInformation->SendLength,
  906. requestInformation->SendFlags, IrpSp->FileObject));
  907. }
  908. status = TdiSend(&request, (ushort) requestInformation->SendFlags,
  909. requestInformation->SendLength,
  910. (PNDIS_BUFFER) Irp->MdlAddress);
  911. if (status == TDI_PENDING) {
  912. IF_TCPDBG(TCP_DEBUG_SEND) {
  913. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  914. "TCPSendData pending irp %lx\n", Irp));
  915. }
  916. return(status);
  917. }
  918. //
  919. // The status is not pending. We reset the pending bit
  920. //
  921. IrpSp->Control &= ~SL_PENDING_RETURNED;
  922. if (status == TDI_SUCCESS) {
  923. ASSERT(requestInformation->SendLength == 0);
  924. status = TCPDataRequestComplete(Irp, status,
  925. requestInformation->SendLength);
  926. } else {
  927. IF_TCPDBG(TCP_DEBUG_SEND) {
  928. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  929. "TCPSendData - irp %lx send failed, status %lx\n",
  930. Irp, status));
  931. }
  932. status = TCPDataRequestComplete(Irp, status, 0);
  933. }
  934. } else {
  935. //
  936. // Irp was cancelled previously.
  937. //
  938. KeReleaseSpinLock(&tcpContext->EndpointLock, oldIrql);
  939. //
  940. // Ensure that the cancel-routine has executed.
  941. //
  942. IoAcquireCancelSpinLock(&oldIrql);
  943. IoReleaseCancelSpinLock(oldIrql);
  944. KeAcquireSpinLock(&tcpContext->EndpointLock, &oldIrql);
  945. IoSetCancelRoutine(Irp, NULL);
  946. KeReleaseSpinLock(&tcpContext->EndpointLock, oldIrql);
  947. IF_TCPDBG(TCP_DEBUG_SEND) {
  948. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  949. "TCPSendData: Irp %lx on fileobj %lx was cancelled\n",
  950. Irp, IrpSp->FileObject));
  951. }
  952. Irp->IoStatus.Status = STATUS_CANCELLED;
  953. Irp->IoStatus.Information = 0;
  954. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  955. status = STATUS_CANCELLED;
  956. }
  957. return(status);
  958. } // TCPSendData
  959. //* TCPReceiveData - Handler for TDI Receive IRP.
  960. //
  961. // Converts a TDI Receive IRP into a call to TdiReceive.
  962. //
  963. NTSTATUS // Returns: whether the request was successful.
  964. TCPReceiveData(
  965. IN PIRP Irp, // I/O request packet.
  966. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  967. {
  968. TDI_STATUS status;
  969. TDI_REQUEST request;
  970. PTCP_CONTEXT tcpContext;
  971. PTDI_REQUEST_KERNEL_RECEIVE requestInformation;
  972. KIRQL oldIrql;
  973. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  974. ASSERT(PtrToUlong(IrpSp->FileObject->FsContext2) == TDI_CONNECTION_FILE);
  975. requestInformation = (PTDI_REQUEST_KERNEL_RECEIVE) &(IrpSp->Parameters);
  976. request.Handle.ConnectionContext = tcpContext->Handle.ConnectionContext;
  977. request.RequestNotifyObject = TCPDataRequestComplete;
  978. request.RequestContext = Irp;
  979. KeAcquireSpinLock(&tcpContext->EndpointLock, &oldIrql);
  980. IoSetCancelRoutine(Irp, TCPCancelRequest);
  981. if (!Irp->Cancel) {
  982. //
  983. // Set up for cancellation.
  984. //
  985. IoMarkIrpPending(Irp);
  986. tcpContext->ReferenceCount++;
  987. IF_TCPDBG(TCP_DEBUG_IRP) {
  988. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  989. "TCPReceiveData: irp %lx fileobj %lx refcnt inc to %u\n",
  990. Irp, IrpSp->FileObject, tcpContext->ReferenceCount));
  991. }
  992. #if DBG
  993. IF_TCPDBG(TCP_DEBUG_CANCEL) {
  994. PLIST_ENTRY entry;
  995. PIRP item = NULL;
  996. //
  997. // Verify that the Irp has not already been submitted.
  998. //
  999. for (entry = tcpContext->PendingIrpList.Flink;
  1000. entry != &(tcpContext->PendingIrpList);
  1001. entry = entry->Flink) {
  1002. item = CONTAINING_RECORD(entry, IRP, Tail.Overlay.ListEntry);
  1003. ASSERT(item != Irp);
  1004. }
  1005. for (entry = tcpContext->CancelledIrpList.Flink;
  1006. entry != &(tcpContext->CancelledIrpList);
  1007. entry = entry->Flink) {
  1008. item = CONTAINING_RECORD(entry, IRP, Tail.Overlay.ListEntry);
  1009. ASSERT(item != Irp);
  1010. }
  1011. InsertTailList(&(tcpContext->PendingIrpList),
  1012. &(Irp->Tail.Overlay.ListEntry));
  1013. }
  1014. #endif // DBG
  1015. KeReleaseSpinLock(&tcpContext->EndpointLock, oldIrql);
  1016. IF_TCPDBG(TCP_DEBUG_RECEIVE) {
  1017. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1018. "TCPReceiveData irp %lx receiving %d bytes flags %lx"
  1019. " filobj %lx\n", Irp, requestInformation->ReceiveLength,
  1020. requestInformation->ReceiveFlags, IrpSp->FileObject));
  1021. }
  1022. status = TdiReceive(&request,
  1023. (ushort *) &(requestInformation->ReceiveFlags),
  1024. &(requestInformation->ReceiveLength),
  1025. (PNDIS_BUFFER) Irp->MdlAddress);
  1026. if (status == TDI_PENDING) {
  1027. IF_TCPDBG(TCP_DEBUG_RECEIVE) {
  1028. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1029. "TCPReceiveData: pending irp %lx\n", Irp));
  1030. }
  1031. return(status);
  1032. }
  1033. //
  1034. // The status is not pending. We reset the pending bit
  1035. //
  1036. IrpSp->Control &= ~SL_PENDING_RETURNED;
  1037. IF_TCPDBG(TCP_DEBUG_RECEIVE) {
  1038. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1039. "TCPReceiveData - irp %lx failed, status %lx\n",
  1040. Irp, status));
  1041. }
  1042. status = TCPDataRequestComplete(Irp, status, 0);
  1043. } else {
  1044. //
  1045. // Irp was cancelled previously.
  1046. //
  1047. KeReleaseSpinLock(&tcpContext->EndpointLock, oldIrql);
  1048. //
  1049. // Ensure that the cancel-routine has executed.
  1050. //
  1051. IoAcquireCancelSpinLock(&oldIrql);
  1052. IoReleaseCancelSpinLock(oldIrql);
  1053. KeAcquireSpinLock(&tcpContext->EndpointLock, &oldIrql);
  1054. IoSetCancelRoutine(Irp, NULL);
  1055. KeReleaseSpinLock(&tcpContext->EndpointLock, oldIrql);
  1056. IF_TCPDBG(TCP_DEBUG_SEND) {
  1057. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1058. "TCPReceiveData: Irp %lx on fileobj %lx was cancelled\n",
  1059. Irp, IrpSp->FileObject));
  1060. }
  1061. Irp->IoStatus.Status = STATUS_CANCELLED;
  1062. Irp->IoStatus.Information = 0;
  1063. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  1064. status = STATUS_CANCELLED;
  1065. }
  1066. return status;
  1067. } // TCPReceiveData
  1068. //* UDPSendDatagram -
  1069. //
  1070. NTSTATUS // Returns: whether the request was successfully queued.
  1071. UDPSendDatagram(
  1072. IN PIRP Irp, // I/O request packet.
  1073. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  1074. {
  1075. TDI_STATUS status;
  1076. TDI_REQUEST request;
  1077. PTCP_CONTEXT tcpContext;
  1078. PTDI_REQUEST_KERNEL_SENDDG datagramInformation;
  1079. ULONG bytesSent = 0;
  1080. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  1081. datagramInformation = (PTDI_REQUEST_KERNEL_SENDDG) &(IrpSp->Parameters);
  1082. ASSERT(PtrToUlong(IrpSp->FileObject->FsContext2) ==
  1083. TDI_TRANSPORT_ADDRESS_FILE);
  1084. request.Handle.AddressHandle = tcpContext->Handle.AddressHandle;
  1085. request.RequestNotifyObject = TCPDataRequestComplete;
  1086. request.RequestContext = Irp;
  1087. IF_TCPDBG(TCP_DEBUG_SEND_DGRAM) {
  1088. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1089. "UDPSendDatagram irp %lx sending %d bytes\n", Irp,
  1090. datagramInformation->SendLength));
  1091. }
  1092. status = TCPPrepareIrpForCancel(tcpContext, Irp, TCPCancelRequest);
  1093. if (NT_SUCCESS(status)) {
  1094. status = TdiSendDatagram(&request,
  1095. datagramInformation->SendDatagramInformation,
  1096. datagramInformation->SendLength, &bytesSent,
  1097. (PNDIS_BUFFER) Irp->MdlAddress);
  1098. if (status == TDI_PENDING) {
  1099. return(status);
  1100. }
  1101. ASSERT(status != TDI_SUCCESS);
  1102. ASSERT(bytesSent == 0);
  1103. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INTERNAL_ERROR,
  1104. "UDPSendDatagram - irp %lx send failed, status %lx\n",
  1105. Irp, status));
  1106. TCPDataRequestComplete(Irp, status, bytesSent);
  1107. //
  1108. // Return PENDING because TCPPrepareIrpForCancel marks Irp as PENDING.
  1109. //
  1110. return(TDI_PENDING);
  1111. }
  1112. return status;
  1113. } // UDPSendDatagram
  1114. //* UDPReceiveDatagram - Handle TDI ReceiveDatagram IRP.
  1115. //
  1116. // Converts a TDI ReceiveDatagram IRP into a call to TdiReceiveDatagram.
  1117. //
  1118. NTSTATUS // Returns: whether the request was successful.
  1119. UDPReceiveDatagram(
  1120. IN PIRP Irp, // I/O request packet.
  1121. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  1122. {
  1123. TDI_STATUS status;
  1124. TDI_REQUEST request;
  1125. PTCP_CONTEXT tcpContext;
  1126. PTDI_REQUEST_KERNEL_RECEIVEDG datagramInformation;
  1127. uint bytesReceived = 0;
  1128. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  1129. datagramInformation = (PTDI_REQUEST_KERNEL_RECEIVEDG) &(IrpSp->Parameters);
  1130. ASSERT(PtrToUlong(IrpSp->FileObject->FsContext2) ==
  1131. TDI_TRANSPORT_ADDRESS_FILE);
  1132. request.Handle.AddressHandle = tcpContext->Handle.AddressHandle;
  1133. request.RequestNotifyObject = TCPDataRequestComplete;
  1134. request.RequestContext = Irp;
  1135. IF_TCPDBG(TCP_DEBUG_RECEIVE_DGRAM) {
  1136. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1137. "UDPReceiveDatagram: irp %lx receiveing %d bytes\n", Irp,
  1138. datagramInformation->ReceiveLength));
  1139. }
  1140. status = TCPPrepareIrpForCancel(tcpContext, Irp, TCPCancelRequest);
  1141. if (NT_SUCCESS(status)) {
  1142. status = TdiReceiveDatagram(&request,
  1143. datagramInformation->ReceiveDatagramInformation,
  1144. datagramInformation->ReturnDatagramInformation,
  1145. datagramInformation->ReceiveLength, &bytesReceived,
  1146. Irp->MdlAddress);
  1147. if (status == TDI_PENDING) {
  1148. return(status);
  1149. }
  1150. ASSERT(status != TDI_SUCCESS);
  1151. ASSERT(bytesReceived == 0);
  1152. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INTERNAL_ERROR,
  1153. "UDPReceiveDatagram: irp %lx send failed, status %lx\n",
  1154. Irp, status));
  1155. TCPDataRequestComplete(Irp, status, bytesReceived);
  1156. //
  1157. // Return PENDING because TCPPrepareIrpForCancel marks Irp as PENDING.
  1158. //
  1159. return(TDI_PENDING);
  1160. }
  1161. return status;
  1162. } // UDPReceiveDatagram
  1163. //* TCPSetEventHandler - Handle TDI SetEventHandler IRP.
  1164. //
  1165. // Converts a TDI SetEventHandler IRP into a call to TdiSetEventHandler.
  1166. //
  1167. NTSTATUS // Returns: whether the request was successful.
  1168. TCPSetEventHandler(
  1169. IN PIRP Irp, // I/O request packet.
  1170. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  1171. {
  1172. NTSTATUS status;
  1173. PTDI_REQUEST_KERNEL_SET_EVENT event;
  1174. PTCP_CONTEXT tcpContext;
  1175. PAGED_CODE();
  1176. UNREFERENCED_PARAMETER(Irp);
  1177. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  1178. event = (PTDI_REQUEST_KERNEL_SET_EVENT) &(IrpSp->Parameters);
  1179. IF_TCPDBG(TCP_DEBUG_EVENT_HANDLER) {
  1180. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1181. "TCPSetEventHandler: "
  1182. "irp %lx event %lx handler %lx context %lx\n", Irp,
  1183. event->EventType, event->EventHandler, event->EventContext));
  1184. }
  1185. status = TdiSetEvent(tcpContext->Handle.AddressHandle, event->EventType,
  1186. event->EventHandler, event->EventContext);
  1187. ASSERT(status != TDI_PENDING);
  1188. return(status);
  1189. } // TCPSetEventHandler
  1190. //* TCPQueryInformation - Handle a TDI QueryInformation IRP.
  1191. //
  1192. // Converts a TDI QueryInformation IRP into a call to TdiQueryInformation.
  1193. //
  1194. NTSTATUS // Returns: whether request was successful.
  1195. TCPQueryInformation(
  1196. IN PIRP Irp, // I/O request packet.
  1197. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  1198. {
  1199. TDI_REQUEST request;
  1200. TDI_STATUS status = STATUS_SUCCESS;
  1201. PTCP_CONTEXT tcpContext;
  1202. PTDI_REQUEST_KERNEL_QUERY_INFORMATION queryInformation;
  1203. uint isConn = FALSE;
  1204. uint dataSize = 0;
  1205. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  1206. queryInformation = (PTDI_REQUEST_KERNEL_QUERY_INFORMATION)
  1207. &(IrpSp->Parameters);
  1208. request.RequestNotifyObject = TCPDataRequestComplete;
  1209. request.RequestContext = Irp;
  1210. switch(queryInformation->QueryType) {
  1211. case TDI_QUERY_BROADCAST_ADDRESS:
  1212. ASSERT(PtrToUlong(IrpSp->FileObject->FsContext2) ==
  1213. TDI_CONTROL_CHANNEL_FILE);
  1214. request.Handle.ControlChannel = tcpContext->Handle.ControlChannel;
  1215. break;
  1216. case TDI_QUERY_PROVIDER_INFO:
  1217. //
  1218. // NetBT does this. Reinstate the ASSERT when it is fixed.
  1219. //
  1220. // ASSERT(PtrToUlong(IrpSp->FileObject->FsContext2) ==
  1221. // TDI_CONTROL_CHANNEL_FILE);
  1222. request.Handle.ControlChannel = tcpContext->Handle.ControlChannel;
  1223. break;
  1224. case TDI_QUERY_ADDRESS_INFO:
  1225. if (PtrToUlong(IrpSp->FileObject->FsContext2) == TDI_CONNECTION_FILE) {
  1226. //
  1227. // This is a TCP connection object.
  1228. //
  1229. isConn = TRUE;
  1230. request.Handle.ConnectionContext =
  1231. tcpContext->Handle.ConnectionContext;
  1232. } else {
  1233. //
  1234. // This is an address object.
  1235. //
  1236. request.Handle.AddressHandle = tcpContext->Handle.AddressHandle;
  1237. }
  1238. break;
  1239. case TDI_QUERY_CONNECTION_INFO:
  1240. if (PtrToUlong(IrpSp->FileObject->FsContext2) != TDI_CONNECTION_FILE){
  1241. status = STATUS_INVALID_PARAMETER;
  1242. } else {
  1243. isConn = TRUE;
  1244. request.Handle.ConnectionContext = tcpContext->Handle.ConnectionContext;
  1245. }
  1246. break;
  1247. case TDI_QUERY_PROVIDER_STATISTICS:
  1248. request.Handle.ControlChannel = tcpContext->Handle.ControlChannel;
  1249. break;
  1250. default:
  1251. status = STATUS_NOT_IMPLEMENTED;
  1252. break;
  1253. }
  1254. if (NT_SUCCESS(status)) {
  1255. //
  1256. // This request isn't cancellable, but we put it through
  1257. // the cancel path because it handles some checks for us
  1258. // and tracks the irp.
  1259. //
  1260. status = TCPPrepareIrpForCancel(tcpContext, Irp, NULL);
  1261. if (NT_SUCCESS(status)) {
  1262. dataSize = TCPGetMdlChainByteCount(Irp->MdlAddress);
  1263. status = TdiQueryInformation(&request, queryInformation->QueryType,
  1264. Irp->MdlAddress, &dataSize, isConn);
  1265. if (status != TDI_PENDING) {
  1266. IrpSp->Control &= ~SL_PENDING_RETURNED;
  1267. status = TCPDataRequestComplete(Irp, status, dataSize);
  1268. return(status);
  1269. }
  1270. return(STATUS_PENDING);
  1271. }
  1272. return(status);
  1273. }
  1274. Irp->IoStatus.Status = (NTSTATUS) status;
  1275. Irp->IoStatus.Information = 0;
  1276. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  1277. return(status);
  1278. } // TCPQueryInformation
  1279. //* TCPQueryInformationExComplete - Complete a TdiQueryInformationEx request.
  1280. //
  1281. NTSTATUS
  1282. TCPQueryInformationExComplete(
  1283. void *Context, // A pointer to the IRP for this request.
  1284. TDI_STATUS Status, // Final TDI status of the request.
  1285. unsigned int ByteCount) // Bytes returned in output buffer.
  1286. {
  1287. PTCP_QUERY_CONTEXT queryContext = (PTCP_QUERY_CONTEXT) Context;
  1288. ULONG bytesCopied;
  1289. if (NT_SUCCESS(Status)) {
  1290. //
  1291. // Copy the returned context to the input buffer.
  1292. //
  1293. TdiCopyBufferToMdl(&(queryContext->QueryInformation.Context), 0,
  1294. CONTEXT_SIZE, queryContext->InputMdl,
  1295. FIELD_OFFSET(TCP_REQUEST_QUERY_INFORMATION_EX,
  1296. Context),
  1297. &bytesCopied);
  1298. if (bytesCopied != CONTEXT_SIZE) {
  1299. Status = STATUS_INSUFFICIENT_RESOURCES;
  1300. ByteCount = 0;
  1301. }
  1302. }
  1303. //
  1304. // Unlock the user's buffers and free the MDLs describing them.
  1305. //
  1306. MmUnlockPages(queryContext->InputMdl);
  1307. IoFreeMdl(queryContext->InputMdl);
  1308. MmUnlockPages(queryContext->OutputMdl);
  1309. IoFreeMdl(queryContext->OutputMdl);
  1310. //
  1311. // Complete the request.
  1312. //
  1313. Status = TCPDataRequestComplete(queryContext->Irp, Status, ByteCount);
  1314. ExFreePool(queryContext);
  1315. return Status;
  1316. }
  1317. //* TCPQueryInformationEx - Handle a TDI QueryInformationEx IRP.
  1318. //
  1319. // Converts a TDI QueryInformationEx IRP into a call to TdiQueryInformationEx.
  1320. //
  1321. NTSTATUS // Returns: whether the request was successful.
  1322. TCPQueryInformationEx(
  1323. IN PIRP Irp, // I/O request packet.
  1324. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  1325. {
  1326. TDI_REQUEST request;
  1327. TDI_STATUS status = STATUS_SUCCESS;
  1328. PTCP_CONTEXT tcpContext;
  1329. uint size;
  1330. PTCP_REQUEST_QUERY_INFORMATION_EX InputBuffer;
  1331. PVOID OutputBuffer;
  1332. PMDL inputMdl = NULL;
  1333. PMDL outputMdl = NULL;
  1334. ULONG InputBufferLength, OutputBufferLength;
  1335. PTCP_QUERY_CONTEXT queryContext;
  1336. BOOLEAN inputLocked = FALSE;
  1337. BOOLEAN outputLocked = FALSE;
  1338. BOOLEAN inputBufferValid = FALSE;
  1339. PAGED_CODE();
  1340. IF_TCPDBG(TCP_DEBUG_INFO) {
  1341. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1342. "QueryInformationEx starting - irp %lx fileobj %lx\n",
  1343. Irp, IrpSp->FileObject));
  1344. }
  1345. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  1346. switch (PtrToUlong(IrpSp->FileObject->FsContext2)) {
  1347. case TDI_TRANSPORT_ADDRESS_FILE:
  1348. request.Handle.AddressHandle = tcpContext->Handle.AddressHandle;
  1349. break;
  1350. case TDI_CONNECTION_FILE:
  1351. request.Handle.ConnectionContext =
  1352. tcpContext->Handle.ConnectionContext;
  1353. break;
  1354. case TDI_CONTROL_CHANNEL_FILE:
  1355. request.Handle.ControlChannel = tcpContext->Handle.ControlChannel;
  1356. break;
  1357. default:
  1358. ABORT();
  1359. Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
  1360. Irp->IoStatus.Information = 0;
  1361. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  1362. return(STATUS_INVALID_PARAMETER);
  1363. }
  1364. InputBufferLength = IrpSp->Parameters.DeviceIoControl.InputBufferLength;
  1365. OutputBufferLength = IrpSp->Parameters.DeviceIoControl.OutputBufferLength;
  1366. //
  1367. // Validate the input parameters.
  1368. //
  1369. if (InputBufferLength >= sizeof(TCP_REQUEST_QUERY_INFORMATION_EX) &&
  1370. InputBufferLength < MAXLONG) {
  1371. inputBufferValid = TRUE;
  1372. } else {
  1373. inputBufferValid = FALSE;
  1374. }
  1375. if (inputBufferValid && (OutputBufferLength != 0)) {
  1376. OutputBuffer = Irp->UserBuffer;
  1377. InputBuffer = (PTCP_REQUEST_QUERY_INFORMATION_EX)
  1378. IrpSp->Parameters.DeviceIoControl.Type3InputBuffer;
  1379. queryContext = ExAllocatePool(NonPagedPool, InputBufferLength
  1380. + FIELD_OFFSET(TCP_QUERY_CONTEXT, QueryInformation));
  1381. if (queryContext != NULL) {
  1382. status = TCPPrepareIrpForCancel(tcpContext, Irp, NULL);
  1383. if (!NT_SUCCESS(status)) {
  1384. ExFreePool(queryContext);
  1385. return(status);
  1386. }
  1387. //
  1388. // Allocate Mdls to describe the input and output buffers.
  1389. // Probe and lock the buffers.
  1390. //
  1391. try {
  1392. inputMdl = IoAllocateMdl(InputBuffer, InputBufferLength,
  1393. FALSE, TRUE, NULL);
  1394. outputMdl = IoAllocateMdl(OutputBuffer, OutputBufferLength,
  1395. FALSE, TRUE, NULL);
  1396. if ((inputMdl != NULL) && (outputMdl != NULL)) {
  1397. MmProbeAndLockPages(inputMdl, Irp->RequestorMode,
  1398. IoModifyAccess);
  1399. inputLocked = TRUE;
  1400. MmProbeAndLockPages(outputMdl, Irp->RequestorMode,
  1401. IoWriteAccess);
  1402. outputLocked = TRUE;
  1403. //
  1404. // Copy the input parameter to our pool block so
  1405. // TdiQueryInformationEx can manipulate it directly.
  1406. //
  1407. RtlCopyMemory(&(queryContext->QueryInformation),
  1408. InputBuffer,
  1409. InputBufferLength);
  1410. } else {
  1411. IF_TCPDBG(TCP_DEBUG_INFO) {
  1412. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1413. "QueryInfoEx: Couldn't allocate MDL\n"));
  1414. }
  1415. IrpSp->Control &= ~SL_PENDING_RETURNED;
  1416. status = STATUS_INSUFFICIENT_RESOURCES;
  1417. }
  1418. } except(EXCEPTION_EXECUTE_HANDLER) {
  1419. IF_TCPDBG(TCP_DEBUG_INFO) {
  1420. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1421. "QueryInfoEx: "
  1422. "exception copying input param %lx\n",
  1423. GetExceptionCode()));
  1424. }
  1425. status = GetExceptionCode();
  1426. }
  1427. if (NT_SUCCESS(status)) {
  1428. //
  1429. // It's finally time to do this thing.
  1430. //
  1431. size = TCPGetMdlChainByteCount(outputMdl);
  1432. queryContext->Irp = Irp;
  1433. queryContext->InputMdl = inputMdl;
  1434. queryContext->OutputMdl = outputMdl;
  1435. request.RequestNotifyObject = TCPQueryInformationExComplete;
  1436. request.RequestContext = queryContext;
  1437. status = TdiQueryInformationEx(&request,
  1438. &(queryContext->QueryInformation.ID),
  1439. outputMdl, &size,
  1440. &(queryContext->QueryInformation.Context),
  1441. InputBufferLength - FIELD_OFFSET(TCP_REQUEST_QUERY_INFORMATION_EX, Context));
  1442. if (status != TDI_PENDING) {
  1443. //
  1444. // Since status is not pending, clear the
  1445. // control flag to keep IO verifier happy.
  1446. //
  1447. IrpSp->Control &= ~SL_PENDING_RETURNED;
  1448. status = TCPQueryInformationExComplete(queryContext,
  1449. status, size);
  1450. return(status);
  1451. }
  1452. IF_TCPDBG(TCP_DEBUG_INFO) {
  1453. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1454. "QueryInformationEx - "
  1455. "pending irp %lx fileobj %lx\n",
  1456. Irp, IrpSp->FileObject));
  1457. }
  1458. return(STATUS_PENDING);
  1459. }
  1460. //
  1461. // If we get here, something failed. Clean up.
  1462. //
  1463. if (inputMdl != NULL) {
  1464. if (inputLocked) {
  1465. MmUnlockPages(inputMdl);
  1466. }
  1467. IoFreeMdl(inputMdl);
  1468. }
  1469. if (outputMdl != NULL) {
  1470. if (outputLocked) {
  1471. MmUnlockPages(outputMdl);
  1472. }
  1473. IoFreeMdl(outputMdl);
  1474. }
  1475. ExFreePool(queryContext);
  1476. // Since status is not pending, clear the
  1477. // control flag to keep IO verifier happy.
  1478. IrpSp->Control &= ~SL_PENDING_RETURNED;
  1479. status = TCPDataRequestComplete(Irp, status, 0);
  1480. return(status);
  1481. } else {
  1482. IrpSp->Control &= ~SL_PENDING_RETURNED;
  1483. status = STATUS_INSUFFICIENT_RESOURCES;
  1484. IF_TCPDBG(TCP_DEBUG_INFO) {
  1485. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1486. "QueryInfoEx: Unable to allocate query context\n"));
  1487. }
  1488. }
  1489. } else {
  1490. status = STATUS_INVALID_PARAMETER;
  1491. IF_TCPDBG(TCP_DEBUG_INFO) {
  1492. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1493. "QueryInfoEx: Bad buffer len, OBufLen %d, InBufLen %d\n",
  1494. OutputBufferLength, InputBufferLength));
  1495. }
  1496. }
  1497. IF_TCPDBG(TCP_DEBUG_INFO) {
  1498. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1499. "QueryInformationEx complete - irp %lx, status %lx\n",
  1500. Irp, status));
  1501. }
  1502. Irp->IoStatus.Status = (NTSTATUS) status;
  1503. Irp->IoStatus.Information = 0;
  1504. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  1505. return(status);
  1506. }
  1507. //* TCPSetInformationEx - Handle TDI SetInformationEx IRP.
  1508. //
  1509. // Converts a TDI SetInformationEx IRP into a call to TdiSetInformationEx.
  1510. //
  1511. // This routine does not pend.
  1512. //
  1513. NTSTATUS // Returns: whether the request was successful.
  1514. TCPSetInformationEx(
  1515. IN PIRP Irp, // I/O request packet.
  1516. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  1517. {
  1518. TDI_REQUEST request;
  1519. TDI_STATUS status;
  1520. PTCP_CONTEXT tcpContext;
  1521. PTCP_REQUEST_SET_INFORMATION_EX setInformation;
  1522. PAGED_CODE();
  1523. IF_TCPDBG(TCP_DEBUG_INFO) {
  1524. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1525. "SetInformationEx - irp %lx fileobj %lx\n", Irp,
  1526. IrpSp->FileObject));
  1527. }
  1528. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  1529. setInformation = (PTCP_REQUEST_SET_INFORMATION_EX)
  1530. Irp->AssociatedIrp.SystemBuffer;
  1531. if (IrpSp->Parameters.DeviceIoControl.InputBufferLength <
  1532. FIELD_OFFSET(TCP_REQUEST_SET_INFORMATION_EX, Buffer) ||
  1533. IrpSp->Parameters.DeviceIoControl.InputBufferLength -
  1534. FIELD_OFFSET(TCP_REQUEST_SET_INFORMATION_EX, Buffer) < setInformation->BufferSize) {
  1535. Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
  1536. Irp->IoStatus.Information = 0;
  1537. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  1538. return (STATUS_INVALID_PARAMETER);
  1539. }
  1540. switch (PtrToUlong(IrpSp->FileObject->FsContext2)) {
  1541. case TDI_TRANSPORT_ADDRESS_FILE:
  1542. request.Handle.AddressHandle = tcpContext->Handle.AddressHandle;
  1543. break;
  1544. case TDI_CONNECTION_FILE:
  1545. request.Handle.ConnectionContext =
  1546. tcpContext->Handle.ConnectionContext;
  1547. break;
  1548. case TDI_CONTROL_CHANNEL_FILE:
  1549. request.Handle.ControlChannel = tcpContext->Handle.ControlChannel;
  1550. break;
  1551. default:
  1552. ABORT();
  1553. Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
  1554. Irp->IoStatus.Information = 0;
  1555. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  1556. return(STATUS_INVALID_PARAMETER);
  1557. }
  1558. //
  1559. // Prevent non-privleged access (i.e. IOCTL_TCP_WSH_SET_INFORMATION_EX
  1560. // calls) from making changes outside of the transport layer.
  1561. // Privleged calls should be using IOCTL_TCP_SET_INFORMATION_EX instead.
  1562. //
  1563. if (IrpSp->Parameters.DeviceIoControl.IoControlCode ==
  1564. IOCTL_TCP_WSH_SET_INFORMATION_EX) {
  1565. uint Entity;
  1566. Entity = setInformation->ID.toi_entity.tei_entity;
  1567. if ((Entity != CO_TL_ENTITY) && (Entity != CL_TL_ENTITY) ) {
  1568. Irp->IoStatus.Status = STATUS_ACCESS_DENIED;
  1569. Irp->IoStatus.Information = 0;
  1570. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  1571. return (STATUS_ACCESS_DENIED);
  1572. }
  1573. }
  1574. status = TCPPrepareIrpForCancel(tcpContext, Irp, NULL);
  1575. if (NT_SUCCESS(status)) {
  1576. request.RequestNotifyObject = TCPDataRequestComplete;
  1577. request.RequestContext = Irp;
  1578. status = TdiSetInformationEx(&request, &(setInformation->ID),
  1579. &(setInformation->Buffer[0]),
  1580. setInformation->BufferSize);
  1581. if (status != TDI_PENDING) {
  1582. IrpSp->Control &= ~SL_PENDING_RETURNED;
  1583. status = TCPDataRequestComplete(Irp, status, 0);
  1584. return(status);
  1585. }
  1586. IF_TCPDBG(TCP_DEBUG_INFO) {
  1587. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1588. "SetInformationEx - pending irp %lx fileobj %lx\n",
  1589. Irp, IrpSp->FileObject));
  1590. }
  1591. return(STATUS_PENDING);
  1592. }
  1593. IF_TCPDBG(TCP_DEBUG_INFO) {
  1594. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1595. "SetInformationEx complete - irp %lx\n", Irp));
  1596. }
  1597. //
  1598. // The irp has already been completed.
  1599. //
  1600. return(status);
  1601. }
  1602. #if 0
  1603. //* TCPEnumerateConnectionList -
  1604. //
  1605. // Processes a request to enumerate the workstation connection list.
  1606. //
  1607. // This routine does not pend.
  1608. //
  1609. NTSTATUS // Return: whether the request was successful.
  1610. TCPEnumerateConnectionList(
  1611. IN PIRP Irp, // I/O request packet.
  1612. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  1613. {
  1614. TCPConnectionListEntry *request;
  1615. TCPConnectionListEnum *response;
  1616. ULONG requestLength, responseLength;
  1617. NTSTATUS status;
  1618. PAGED_CODE();
  1619. request = (TCPConnectionListEntry *) Irp->AssociatedIrp.SystemBuffer;
  1620. response = (TCPConnectionListEnum *) request;
  1621. requestLength = IrpSp->Parameters.DeviceIoControl.InputBufferLength;
  1622. responseLength = IrpSp->Parameters.DeviceIoControl.OutputBufferLength;
  1623. if (responseLength < sizeof(TCPConnectionListEnum)) {
  1624. status = STATUS_BUFFER_TOO_SMALL;
  1625. Irp->IoStatus.Information = 0;
  1626. } else {
  1627. EnumerateConnectionList((uchar *) (response + 1),
  1628. responseLength - sizeof(TCPConnectionListEnum),
  1629. &(response->tce_entries_returned),
  1630. &(response->tce_entries_available));
  1631. status = TDI_SUCCESS;
  1632. Irp->IoStatus.Information = sizeof(TCPConnectionListEnum) +
  1633. (response->tce_entries_returned * sizeof(TCPConnectionListEntry));
  1634. }
  1635. Irp->IoStatus.Status = status;
  1636. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  1637. return(status);
  1638. }
  1639. #endif
  1640. //* TCPCreate -
  1641. //
  1642. NTSTATUS // Returns: whether the request was successfully queued.
  1643. TCPCreate(
  1644. IN PDEVICE_OBJECT DeviceObject, // Device object for this request.
  1645. IN PIRP Irp, // I/O request packet.
  1646. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  1647. {
  1648. TDI_REQUEST Request;
  1649. NTSTATUS status;
  1650. FILE_FULL_EA_INFORMATION *ea;
  1651. FILE_FULL_EA_INFORMATION UNALIGNED *targetEA;
  1652. PTCP_CONTEXT tcpContext;
  1653. uint protocol;
  1654. PAGED_CODE();
  1655. tcpContext = ExAllocatePool(NonPagedPool, sizeof(TCP_CONTEXT));
  1656. if (tcpContext == NULL) {
  1657. return(STATUS_INSUFFICIENT_RESOURCES);
  1658. }
  1659. #if DBG
  1660. InitializeListHead(&(tcpContext->PendingIrpList));
  1661. InitializeListHead(&(tcpContext->CancelledIrpList));
  1662. #endif
  1663. tcpContext->ReferenceCount = 1; // Put initial reference on open object.
  1664. tcpContext->CancelIrps = FALSE;
  1665. KeInitializeEvent(&(tcpContext->CleanupEvent), SynchronizationEvent,
  1666. FALSE);
  1667. KeInitializeSpinLock(&tcpContext->EndpointLock);
  1668. ea = (PFILE_FULL_EA_INFORMATION) Irp->AssociatedIrp.SystemBuffer;
  1669. //
  1670. // See if this is a Control Channel open.
  1671. //
  1672. if (!ea) {
  1673. IF_TCPDBG(TCP_DEBUG_OPEN) {
  1674. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1675. "TCPCreate: "
  1676. "Opening control channel for file object %lx\n",
  1677. IrpSp->FileObject));
  1678. }
  1679. tcpContext->Handle.ControlChannel = NULL;
  1680. IrpSp->FileObject->FsContext = tcpContext;
  1681. IrpSp->FileObject->FsContext2 = (PVOID) TDI_CONTROL_CHANNEL_FILE;
  1682. return(STATUS_SUCCESS);
  1683. }
  1684. //
  1685. // See if this is an Address Object open.
  1686. //
  1687. targetEA = FindEA(ea, TdiTransportAddress, TDI_TRANSPORT_ADDRESS_LENGTH);
  1688. if (targetEA != NULL) {
  1689. UCHAR optionsBuffer[3];
  1690. PUCHAR optionsPointer = optionsBuffer;
  1691. PSECURITY_DESCRIPTOR addrSD = NULL;
  1692. if (DeviceObject == TCPDeviceObject) {
  1693. protocol = IP_PROTOCOL_TCP;
  1694. }
  1695. else if (DeviceObject == UDPDeviceObject) {
  1696. protocol = IP_PROTOCOL_UDP;
  1697. ASSERT(optionsPointer - optionsBuffer <= 3);
  1698. if (IsDHCPZeroAddress((TRANSPORT_ADDRESS UNALIGNED *)
  1699. &(targetEA->EaName[targetEA->EaNameLength + 1]))) {
  1700. if (!IsAdminIoRequest(Irp, IrpSp)) {
  1701. ExFreePool(tcpContext);
  1702. return(STATUS_ACCESS_DENIED);
  1703. }
  1704. *optionsPointer = TDI_ADDRESS_OPTION_DHCP;
  1705. optionsPointer++;
  1706. }
  1707. ASSERT(optionsPointer - optionsBuffer <= 3);
  1708. } else {
  1709. //
  1710. // This is a raw ip open.
  1711. //
  1712. //
  1713. // Only administrators can create raw addresses
  1714. // unless this is allowed through registry.
  1715. //
  1716. if (!AllowUserRawAccess && !IsAdminIoRequest(Irp, IrpSp)) {
  1717. ExFreePool(tcpContext);
  1718. return(STATUS_ACCESS_DENIED);
  1719. }
  1720. protocol = RawExtractProtocolNumber(
  1721. &(IrpSp->FileObject->FileName));
  1722. //
  1723. // We need to protect the IPv6Send routine's packet rewriting
  1724. // code (for fragmentation, header inclusion, etc) from getting
  1725. // confused by malformed extension headers coming from user-land.
  1726. // So we disallow these types.
  1727. //
  1728. if ((protocol == 0xFFFFFFFF) ||
  1729. IsExtensionHeader((uchar)protocol)) {
  1730. ExFreePool(tcpContext);
  1731. return(STATUS_INVALID_PARAMETER);
  1732. }
  1733. *optionsPointer = TDI_ADDRESS_OPTION_RAW;
  1734. optionsPointer++;
  1735. }
  1736. if ((IrpSp->Parameters.Create.ShareAccess & FILE_SHARE_READ) ||
  1737. (IrpSp->Parameters.Create.ShareAccess & FILE_SHARE_WRITE)) {
  1738. *optionsPointer = TDI_ADDRESS_OPTION_REUSE;
  1739. optionsPointer++;
  1740. }
  1741. *optionsPointer = TDI_OPTION_EOL;
  1742. Request.Handle.AddressHandle = NULL;
  1743. Request.RequestContext = Irp;
  1744. if (protocol == IP_PROTOCOL_TCP || protocol == IP_PROTOCOL_UDP) {
  1745. status = CaptureCreatorSD(Irp, IrpSp, &addrSD);
  1746. } else {
  1747. status = STATUS_SUCCESS;
  1748. }
  1749. IF_TCPDBG(TCP_DEBUG_OPEN) {
  1750. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1751. "TCPCreate: Opening address for file object %lx\n",
  1752. IrpSp->FileObject));
  1753. }
  1754. if (NT_SUCCESS(status)) {
  1755. status = TdiOpenAddress(&Request, (TRANSPORT_ADDRESS UNALIGNED *)
  1756. &(targetEA->EaName[targetEA->EaNameLength + 1]),
  1757. protocol, optionsBuffer, addrSD);
  1758. }
  1759. if (NT_SUCCESS(status)) {
  1760. //
  1761. // Save off the handle to the AO passed back.
  1762. //
  1763. tcpContext->Handle.AddressHandle = Request.Handle.AddressHandle;
  1764. IrpSp->FileObject->FsContext = tcpContext;
  1765. IrpSp->FileObject->FsContext2 = (PVOID) TDI_TRANSPORT_ADDRESS_FILE;
  1766. } else {
  1767. if (addrSD != NULL) {
  1768. ObDereferenceSecurityDescriptor(addrSD, 1);
  1769. }
  1770. ExFreePool(tcpContext);
  1771. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INTERNAL_ERROR,
  1772. "TdiOpenAddress failed, status %lx\n", status));
  1773. if (status == STATUS_ADDRESS_ALREADY_EXISTS) {
  1774. status = STATUS_SHARING_VIOLATION;
  1775. }
  1776. }
  1777. ASSERT(status != TDI_PENDING);
  1778. return(status);
  1779. }
  1780. //
  1781. // See if this is a Connection Object open.
  1782. //
  1783. targetEA = FindEA(ea, TdiConnectionContext, TDI_CONNECTION_CONTEXT_LENGTH);
  1784. if (targetEA != NULL) {
  1785. //
  1786. // This is an open of a Connection Object.
  1787. //
  1788. if (DeviceObject == TCPDeviceObject) {
  1789. //
  1790. // Keep W4 happy.
  1791. //
  1792. Request.Handle.ConnectionContext = NULL;
  1793. IF_TCPDBG(TCP_DEBUG_OPEN) {
  1794. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1795. "TCPCreate: Opening connection for file object %lx\n",
  1796. IrpSp->FileObject));
  1797. }
  1798. if (targetEA->EaValueLength < sizeof(CONNECTION_CONTEXT)) {
  1799. status = STATUS_EA_LIST_INCONSISTENT;
  1800. } else {
  1801. status = TdiOpenConnection(&Request,
  1802. *((CONNECTION_CONTEXT UNALIGNED *)
  1803. &(targetEA->EaName[targetEA->EaNameLength + 1])));
  1804. }
  1805. if (NT_SUCCESS(status)) {
  1806. //
  1807. // Save off the Connection Context passed back.
  1808. //
  1809. tcpContext->Handle.ConnectionContext =
  1810. Request.Handle.ConnectionContext;
  1811. IrpSp->FileObject->FsContext = tcpContext;
  1812. IrpSp->FileObject->FsContext2 = (PVOID) TDI_CONNECTION_FILE;
  1813. } else {
  1814. ExFreePool(tcpContext);
  1815. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INTERNAL_ERROR,
  1816. "TdiOpenConnection failed, status %lx\n", status));
  1817. }
  1818. } else {
  1819. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_USER_ERROR,
  1820. "TCP: TdiOpenConnection issued on UDP device!\n"));
  1821. status = STATUS_INVALID_DEVICE_REQUEST;
  1822. ExFreePool(tcpContext);
  1823. }
  1824. ASSERT(status != TDI_PENDING);
  1825. return(status);
  1826. }
  1827. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_USER_ERROR,
  1828. "TCPCreate: didn't find any useful ea's\n"));
  1829. status = STATUS_INVALID_EA_NAME;
  1830. ExFreePool(tcpContext);
  1831. ASSERT(status != TDI_PENDING);
  1832. return(status);
  1833. } // TCPCreate
  1834. //* IsAdminIoRequest -
  1835. //
  1836. // (Lifted from AFD - AfdPerformSecurityCheck)
  1837. // Compares security context of the endpoint creator to that
  1838. // of the administrator and local system.
  1839. //
  1840. BOOLEAN // return TRUE if socket creator has admin or local system privilege
  1841. IsAdminIoRequest(
  1842. PIRP Irp, // Pointer to I/O request packet.
  1843. PIO_STACK_LOCATION IrpSp) // Pointer to the I/O stack location to use for this request.
  1844. {
  1845. BOOLEAN accessGranted;
  1846. PACCESS_STATE accessState;
  1847. PIO_SECURITY_CONTEXT securityContext;
  1848. PPRIVILEGE_SET privileges = NULL;
  1849. ACCESS_MASK grantedAccess;
  1850. PGENERIC_MAPPING GenericMapping;
  1851. ACCESS_MASK AccessMask = GENERIC_ALL;
  1852. NTSTATUS Status;
  1853. //
  1854. // Enable access to all the globally defined SIDs
  1855. //
  1856. GenericMapping = IoGetFileObjectGenericMapping();
  1857. RtlMapGenericMask(&AccessMask, GenericMapping);
  1858. securityContext = IrpSp->Parameters.Create.SecurityContext;
  1859. accessState = securityContext->AccessState;
  1860. SeLockSubjectContext(&accessState->SubjectSecurityContext);
  1861. accessGranted = SeAccessCheck(TcpAdminSecurityDescriptor,
  1862. &accessState->SubjectSecurityContext,
  1863. TRUE,
  1864. AccessMask,
  1865. 0,
  1866. &privileges,
  1867. IoGetFileObjectGenericMapping(),
  1868. (KPROCESSOR_MODE) ((IrpSp->Flags & SL_FORCE_ACCESS_CHECK)
  1869. ? UserMode
  1870. : Irp->RequestorMode),
  1871. &grantedAccess,
  1872. &Status);
  1873. if (privileges) {
  1874. (VOID) SeAppendPrivileges(accessState,
  1875. privileges);
  1876. SeFreePrivileges(privileges);
  1877. }
  1878. if (accessGranted) {
  1879. accessState->PreviouslyGrantedAccess |= grantedAccess;
  1880. accessState->RemainingDesiredAccess &= ~(grantedAccess | MAXIMUM_ALLOWED);
  1881. ASSERT(NT_SUCCESS(Status));
  1882. } else {
  1883. ASSERT(!NT_SUCCESS(Status));
  1884. }
  1885. SeUnlockSubjectContext(&accessState->SubjectSecurityContext);
  1886. return accessGranted;
  1887. } // IsAdminIoRequest
  1888. //* TCPCloseObjectComplete -
  1889. //
  1890. // Completes a TdiCloseConnectoin or TdiCloseAddress request.
  1891. //
  1892. void // Returns: Nothing.
  1893. TCPCloseObjectComplete(
  1894. void *Context, // Pointer to the IRP for this request.
  1895. unsigned int Status, // Final status of the operation.
  1896. unsigned int UnUsed) // Unused parameter.
  1897. {
  1898. KIRQL oldIrql;
  1899. PIRP irp;
  1900. PIO_STACK_LOCATION irpSp;
  1901. PTCP_CONTEXT tcpContext;
  1902. UNREFERENCED_PARAMETER(UnUsed);
  1903. irp = (PIRP) Context;
  1904. irpSp = IoGetCurrentIrpStackLocation(irp);
  1905. tcpContext = (PTCP_CONTEXT) irpSp->FileObject->FsContext;
  1906. irp->IoStatus.Status = Status;
  1907. IF_TCPDBG(TCP_DEBUG_CLEANUP) {
  1908. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1909. "TCPCloseObjectComplete on file object %lx\n",
  1910. irpSp->FileObject));
  1911. }
  1912. KeAcquireSpinLock(&tcpContext->EndpointLock, &oldIrql);
  1913. ASSERT(tcpContext->ReferenceCount > 0);
  1914. ASSERT(tcpContext->CancelIrps);
  1915. //
  1916. // Remove the initial reference that was put on by TCPCreate.
  1917. //
  1918. ASSERT(tcpContext->ReferenceCount > 0);
  1919. IF_TCPDBG(TCP_DEBUG_IRP) {
  1920. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1921. "TCPCloseObjectComplete: "
  1922. "irp %lx fileobj %lx refcnt dec to %u\n",
  1923. irp, irpSp, tcpContext->ReferenceCount - 1));
  1924. }
  1925. if (--(tcpContext->ReferenceCount) == 0) {
  1926. IF_TCPDBG(TCP_DEBUG_CANCEL) {
  1927. ASSERT(IsListEmpty(&(tcpContext->CancelledIrpList)));
  1928. ASSERT(IsListEmpty(&(tcpContext->PendingIrpList)));
  1929. }
  1930. KeReleaseSpinLock(&tcpContext->EndpointLock, oldIrql);
  1931. KeSetEvent(&(tcpContext->CleanupEvent), 0, FALSE);
  1932. return;
  1933. }
  1934. KeReleaseSpinLock(&tcpContext->EndpointLock, oldIrql);
  1935. return;
  1936. } // TCPCloseObjectComplete
  1937. //* TCPCleanup -
  1938. //
  1939. // Cancels all outstanding Irps on a TDI object by calling the close
  1940. // routine for the object. It then waits for them to be completed
  1941. // before returning.
  1942. //
  1943. // This routine blocks, but does not pend.
  1944. //
  1945. NTSTATUS // Returns: whether the request was successfully queued.
  1946. TCPCleanup(
  1947. IN PDEVICE_OBJECT DeviceObject, // Device object for this request.
  1948. IN PIRP Irp, // I/O request packet.
  1949. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  1950. {
  1951. KIRQL oldIrql;
  1952. PTCP_CONTEXT tcpContext;
  1953. NTSTATUS status;
  1954. TDI_REQUEST request;
  1955. UNREFERENCED_PARAMETER(DeviceObject);
  1956. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  1957. KeAcquireSpinLock(&tcpContext->EndpointLock, &oldIrql);
  1958. tcpContext->CancelIrps = TRUE;
  1959. KeResetEvent(&(tcpContext->CleanupEvent));
  1960. KeReleaseSpinLock(&tcpContext->EndpointLock, oldIrql);
  1961. //
  1962. // Now call the TDI close routine for this object to force all of its Irps
  1963. // to complete.
  1964. //
  1965. request.RequestNotifyObject = TCPCloseObjectComplete;
  1966. request.RequestContext = Irp;
  1967. switch (PtrToUlong(IrpSp->FileObject->FsContext2)) {
  1968. case TDI_TRANSPORT_ADDRESS_FILE:
  1969. IF_TCPDBG(TCP_DEBUG_CLOSE) {
  1970. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1971. "TCPCleanup: Closing address object on file object %lx\n",
  1972. IrpSp->FileObject));
  1973. }
  1974. request.Handle.AddressHandle = tcpContext->Handle.AddressHandle;
  1975. status = TdiCloseAddress(&request);
  1976. break;
  1977. case TDI_CONNECTION_FILE:
  1978. IF_TCPDBG(TCP_DEBUG_CLOSE) {
  1979. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1980. "TCPCleanup: "
  1981. "Closing Connection object on file object %lx\n",
  1982. IrpSp->FileObject));
  1983. }
  1984. request.Handle.ConnectionContext =
  1985. tcpContext->Handle.ConnectionContext;
  1986. status = TdiCloseConnection(&request);
  1987. break;
  1988. case TDI_CONTROL_CHANNEL_FILE:
  1989. IF_TCPDBG(TCP_DEBUG_CLOSE) {
  1990. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  1991. "TCPCleanup: Closing Control Channel object on"
  1992. " file object %lx\n", IrpSp->FileObject));
  1993. }
  1994. status = STATUS_SUCCESS;
  1995. break;
  1996. default:
  1997. //
  1998. // This should never happen.
  1999. //
  2000. ABORT();
  2001. KeAcquireSpinLock(&tcpContext->EndpointLock, &oldIrql);
  2002. tcpContext->CancelIrps = FALSE;
  2003. KeReleaseSpinLock(&tcpContext->EndpointLock, oldIrql);
  2004. return(STATUS_INVALID_PARAMETER);
  2005. }
  2006. if (status != TDI_PENDING) {
  2007. TCPCloseObjectComplete(Irp, status, 0);
  2008. }
  2009. IF_TCPDBG(TCP_DEBUG_CLEANUP) {
  2010. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  2011. "TCPCleanup: waiting for completion of Irps on"
  2012. " file object %lx\n", IrpSp->FileObject));
  2013. }
  2014. status = KeWaitForSingleObject(&(tcpContext->CleanupEvent), UserRequest,
  2015. KernelMode, FALSE, NULL);
  2016. ASSERT(NT_SUCCESS(status));
  2017. IF_TCPDBG(TCP_DEBUG_CLEANUP) {
  2018. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  2019. "TCPCleanup: Wait on file object %lx finished\n",
  2020. IrpSp->FileObject));
  2021. }
  2022. //
  2023. // The cleanup Irp will be completed by the dispatch routine.
  2024. //
  2025. return(Irp->IoStatus.Status);
  2026. } // TCPCleanup
  2027. //* TCPClose -
  2028. //
  2029. // Dispatch routine for MJ_CLOSE IRPs. Performs final cleanup of the
  2030. // open endpoint.
  2031. //
  2032. // This request does not pend.
  2033. //
  2034. NTSTATUS // Returns: whether the request was successfully queued.
  2035. TCPClose(
  2036. IN PIRP Irp, // I/O request packet.
  2037. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  2038. {
  2039. PTCP_CONTEXT tcpContext;
  2040. UNREFERENCED_PARAMETER(Irp);
  2041. tcpContext = (PTCP_CONTEXT) IrpSp->FileObject->FsContext;
  2042. #if DBG
  2043. IF_TCPDBG(TCP_DEBUG_CANCEL) {
  2044. KIRQL oldIrql;
  2045. IoAcquireCancelSpinLock(&oldIrql);
  2046. ASSERT(tcpContext->ReferenceCount == 0);
  2047. ASSERT(IsListEmpty(&(tcpContext->PendingIrpList)));
  2048. ASSERT(IsListEmpty(&(tcpContext->CancelledIrpList)));
  2049. IoReleaseCancelSpinLock(oldIrql);
  2050. }
  2051. #endif // DBG
  2052. IF_TCPDBG(TCP_DEBUG_CLOSE) {
  2053. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_INFO_TCPDBG,
  2054. "TCPClose on file object %lx\n", IrpSp->FileObject));
  2055. }
  2056. ExFreePool(tcpContext);
  2057. return(STATUS_SUCCESS);
  2058. } // TCPClose
  2059. //* TCPDispatchDeviceControl -
  2060. //
  2061. NTSTATUS // Returns: whether the request was successfully queued.
  2062. TCPDispatchDeviceControl(
  2063. IN PIRP Irp, // I/O request packet.
  2064. IN PIO_STACK_LOCATION IrpSp) // Current stack location in the Irp.
  2065. {
  2066. NTSTATUS status;
  2067. PAGED_CODE();
  2068. //
  2069. // Set this in advance. Any IOCTL dispatch routine that cares about it
  2070. // will modify it itself.
  2071. //
  2072. Irp->IoStatus.Information = 0;
  2073. switch(IrpSp->Parameters.DeviceIoControl.IoControlCode) {
  2074. case IOCTL_TCP_QUERY_INFORMATION_EX:
  2075. return(TCPQueryInformationEx(Irp, IrpSp));
  2076. break;
  2077. case IOCTL_TCP_SET_INFORMATION_EX:
  2078. case IOCTL_TCP_WSH_SET_INFORMATION_EX:
  2079. return(TCPSetInformationEx(Irp, IrpSp));
  2080. break;
  2081. default:
  2082. status = STATUS_NOT_IMPLEMENTED;
  2083. break;
  2084. }
  2085. Irp->IoStatus.Status = status;
  2086. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  2087. return status;
  2088. } // TCPDispatchDeviceControl
  2089. //* TCPDispatchInternalDeviceControl -
  2090. //
  2091. // This is the dispatch routine for Internal Device Control IRPs.
  2092. // This is the hot path for kernel-mode clients.
  2093. //
  2094. NTSTATUS // Returns: whether the request was successfully queued.
  2095. TCPDispatchInternalDeviceControl(
  2096. IN PDEVICE_OBJECT DeviceObject, // Device object for target device.
  2097. IN PIRP Irp) // I/O request packet.
  2098. {
  2099. PIO_STACK_LOCATION irpSp;
  2100. NTSTATUS status;
  2101. if (DeviceObject != IPDeviceObject) {
  2102. irpSp = IoGetCurrentIrpStackLocation(Irp);
  2103. if (PtrToUlong(irpSp->FileObject->FsContext2) == TDI_CONNECTION_FILE) {
  2104. //
  2105. // Send and receive are the performance path, so check for them
  2106. // right away.
  2107. //
  2108. if (irpSp->MinorFunction == TDI_SEND) {
  2109. return(TCPSendData(Irp, irpSp));
  2110. }
  2111. if (irpSp->MinorFunction == TDI_RECEIVE) {
  2112. return(TCPReceiveData(Irp, irpSp));
  2113. }
  2114. switch(irpSp->MinorFunction) {
  2115. case TDI_ASSOCIATE_ADDRESS:
  2116. status = TCPAssociateAddress(Irp, irpSp);
  2117. Irp->IoStatus.Status = status;
  2118. Irp->IoStatus.Information = 0;
  2119. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  2120. return(status);
  2121. case TDI_DISASSOCIATE_ADDRESS:
  2122. return(TCPDisassociateAddress(Irp, irpSp));
  2123. case TDI_CONNECT:
  2124. return(TCPConnect(Irp, irpSp));
  2125. case TDI_DISCONNECT:
  2126. return(TCPDisconnect(Irp, irpSp));
  2127. case TDI_LISTEN:
  2128. return(TCPListen(Irp, irpSp));
  2129. case TDI_ACCEPT:
  2130. return(TCPAccept(Irp, irpSp));
  2131. default:
  2132. break;
  2133. }
  2134. //
  2135. // Fall through.
  2136. //
  2137. }
  2138. else if (PtrToUlong(irpSp->FileObject->FsContext2) ==
  2139. TDI_TRANSPORT_ADDRESS_FILE) {
  2140. if (irpSp->MinorFunction == TDI_SEND_DATAGRAM) {
  2141. return(UDPSendDatagram(Irp, irpSp));
  2142. }
  2143. if (irpSp->MinorFunction == TDI_RECEIVE_DATAGRAM) {
  2144. return(UDPReceiveDatagram(Irp, irpSp));
  2145. }
  2146. if (irpSp->MinorFunction == TDI_SET_EVENT_HANDLER) {
  2147. status = TCPSetEventHandler(Irp, irpSp);
  2148. Irp->IoStatus.Status = status;
  2149. Irp->IoStatus.Information = 0;
  2150. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  2151. return(status);
  2152. }
  2153. //
  2154. // Fall through.
  2155. //
  2156. }
  2157. ASSERT(
  2158. (PtrToUlong(irpSp->FileObject->FsContext2) == TDI_TRANSPORT_ADDRESS_FILE)
  2159. ||
  2160. (PtrToUlong(irpSp->FileObject->FsContext2) == TDI_CONNECTION_FILE)
  2161. ||
  2162. (PtrToUlong(irpSp->FileObject->FsContext2) == TDI_CONTROL_CHANNEL_FILE));
  2163. //
  2164. // These functions are common to all endpoint types.
  2165. //
  2166. switch(irpSp->MinorFunction) {
  2167. case TDI_QUERY_INFORMATION:
  2168. return(TCPQueryInformation(Irp, irpSp));
  2169. case TDI_SET_INFORMATION:
  2170. case TDI_ACTION:
  2171. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_USER_ERROR,
  2172. "TCP: Call to unimplemented TDI function 0x%p\n",
  2173. irpSp->MinorFunction));
  2174. status = STATUS_NOT_IMPLEMENTED;
  2175. break;
  2176. default:
  2177. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_USER_ERROR,
  2178. "TCP: call to invalid TDI function 0x%p\n",
  2179. irpSp->MinorFunction));
  2180. status = STATUS_INVALID_DEVICE_REQUEST;
  2181. }
  2182. ASSERT(status != TDI_PENDING);
  2183. Irp->IoStatus.Status = status;
  2184. Irp->IoStatus.Information = 0;
  2185. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  2186. return status;
  2187. }
  2188. return(IPDispatch(DeviceObject, Irp));
  2189. }
  2190. //* TCPDispatch -
  2191. //
  2192. // This is the generic dispatch routine for TCP/UDP/RawIP.
  2193. //
  2194. NTSTATUS // Returns: whether the request was successfully queued.
  2195. TCPDispatch(
  2196. IN PDEVICE_OBJECT DeviceObject, // Device object for target device.
  2197. IN PIRP Irp) // I/O request packet.
  2198. {
  2199. PIO_STACK_LOCATION irpSp;
  2200. NTSTATUS status;
  2201. if (DeviceObject != IPDeviceObject) {
  2202. irpSp = IoGetCurrentIrpStackLocation(Irp);
  2203. ASSERT(irpSp->MajorFunction != IRP_MJ_INTERNAL_DEVICE_CONTROL);
  2204. switch (irpSp->MajorFunction) {
  2205. case IRP_MJ_CREATE:
  2206. status = TCPCreate(DeviceObject, Irp, irpSp);
  2207. break;
  2208. case IRP_MJ_CLEANUP:
  2209. status = TCPCleanup(DeviceObject, Irp, irpSp);
  2210. break;
  2211. case IRP_MJ_CLOSE:
  2212. status = TCPClose(Irp, irpSp);
  2213. break;
  2214. case IRP_MJ_DEVICE_CONTROL:
  2215. status = TdiMapUserRequest(DeviceObject, Irp, irpSp);
  2216. if (status == STATUS_SUCCESS) {
  2217. return(TCPDispatchInternalDeviceControl(DeviceObject, Irp));
  2218. }
  2219. if (irpSp->Parameters.DeviceIoControl.IoControlCode ==
  2220. IOCTL_TDI_QUERY_DIRECT_SEND_HANDLER) {
  2221. if (Irp->RequestorMode == KernelMode) {
  2222. *(PULONG_PTR)irpSp->Parameters.DeviceIoControl.Type3InputBuffer = (ULONG_PTR)TCPSendData;
  2223. status = STATUS_SUCCESS;
  2224. } else {
  2225. status = STATUS_ACCESS_DENIED;
  2226. }
  2227. break;
  2228. }
  2229. return(TCPDispatchDeviceControl(Irp,
  2230. IoGetCurrentIrpStackLocation(Irp)));
  2231. break;
  2232. case IRP_MJ_QUERY_SECURITY:
  2233. //
  2234. // This is generated on Raw endpoints. We don't do anything
  2235. // for it.
  2236. //
  2237. status = STATUS_INVALID_DEVICE_REQUEST;
  2238. break;
  2239. case IRP_MJ_WRITE:
  2240. case IRP_MJ_READ:
  2241. default:
  2242. KdPrintEx((DPFLTR_TCPIP6_ID, DPFLTR_USER_ERROR,
  2243. "TCPDispatch: Irp %lx unsupported major function 0x%lx\n",
  2244. irpSp, irpSp->MajorFunction));
  2245. status = STATUS_INVALID_DEVICE_REQUEST;
  2246. break;
  2247. }
  2248. ASSERT(status != TDI_PENDING);
  2249. Irp->IoStatus.Status = status;
  2250. Irp->IoStatus.Information = 0;
  2251. IoCompleteRequest(Irp, IO_NETWORK_INCREMENT);
  2252. return status;
  2253. }
  2254. return(IPDispatch(DeviceObject, Irp));
  2255. } // TCPDispatch
  2256. //
  2257. // Private utility functions
  2258. //
  2259. //* FindEA -
  2260. //
  2261. // Parses and extended attribute list for a given target attribute.
  2262. //
  2263. FILE_FULL_EA_INFORMATION UNALIGNED * // Returns: requested attribute or NULL.
  2264. FindEA(
  2265. PFILE_FULL_EA_INFORMATION StartEA, // First extended attribute in list.
  2266. CHAR *TargetName, // Name of target attribute.
  2267. USHORT TargetNameLength) // Length of above.
  2268. {
  2269. USHORT i;
  2270. BOOLEAN found;
  2271. FILE_FULL_EA_INFORMATION UNALIGNED *CurrentEA;
  2272. PAGED_CODE();
  2273. do {
  2274. found = TRUE;
  2275. CurrentEA = StartEA;
  2276. StartEA += CurrentEA->NextEntryOffset;
  2277. if (CurrentEA->EaNameLength != TargetNameLength) {
  2278. continue;
  2279. }
  2280. for (i=0; i < CurrentEA->EaNameLength; i++) {
  2281. if (CurrentEA->EaName[i] == TargetName[i]) {
  2282. continue;
  2283. }
  2284. found = FALSE;
  2285. break;
  2286. }
  2287. if (found) {
  2288. return(CurrentEA);
  2289. }
  2290. } while(CurrentEA->NextEntryOffset != 0);
  2291. return(NULL);
  2292. }
  2293. //* IsDHCPZeroAddress -
  2294. //
  2295. // Checks a TDI IP address list for an address from DHCP binding
  2296. // to the IP address zero. Normally, binding to zero means wildcard.
  2297. // For DHCP, it really means bind to an interface with an address of
  2298. // zero. This semantic is flagged by a special value in an unused
  2299. // portion of the address structure (ie. this is a kludge).
  2300. //
  2301. BOOLEAN // Returns: TRUE iff first IP address found had the flag set.
  2302. IsDHCPZeroAddress(
  2303. TRANSPORT_ADDRESS UNALIGNED *AddrList) // TDI transport address list
  2304. // passed in the create IRP.
  2305. {
  2306. int i; // Index variable.
  2307. TA_ADDRESS *CurrentAddr; // Address we're examining and may use.
  2308. // First, verify that someplace in Address is an address we can use.
  2309. CurrentAddr = (TA_ADDRESS *)AddrList->Address;
  2310. for (i = 0; i < AddrList->TAAddressCount; i++) {
  2311. if (CurrentAddr->AddressType == TDI_ADDRESS_TYPE_IP) {
  2312. if (CurrentAddr->AddressLength == TDI_ADDRESS_LENGTH_IP) {
  2313. TDI_ADDRESS_IP UNALIGNED *ValidAddr;
  2314. ValidAddr = (TDI_ADDRESS_IP UNALIGNED *)CurrentAddr->Address;
  2315. if (*((ULONG UNALIGNED *) ValidAddr->sin_zero) == 0x12345678) {
  2316. return TRUE;
  2317. }
  2318. } else {
  2319. return FALSE; // Wrong length for address.
  2320. }
  2321. } else {
  2322. CurrentAddr = (TA_ADDRESS *)
  2323. (CurrentAddr->Address + CurrentAddr->AddressLength);
  2324. }
  2325. }
  2326. return FALSE; // Didn't find a match.
  2327. }
  2328. //* TCPGetMdlChainByteCount -
  2329. //
  2330. // Sums the byte counts of each MDL in a chain.
  2331. //
  2332. ULONG // Returns: byte count of the MDL chain.
  2333. TCPGetMdlChainByteCount(
  2334. PMDL Mdl) // MDL chain to sum.
  2335. {
  2336. ULONG count = 0;
  2337. while (Mdl != NULL) {
  2338. count += MmGetMdlByteCount(Mdl);
  2339. Mdl = Mdl->Next;
  2340. }
  2341. return(count);
  2342. }
  2343. //* RawExtractProtocolNumber -
  2344. //
  2345. // Extracts the protocol number from the file object name.
  2346. //
  2347. ULONG // Returns: the protocol number or 0xFFFFFFFF on error.
  2348. RawExtractProtocolNumber(
  2349. IN PUNICODE_STRING FileName) // File name (Unicode).
  2350. {
  2351. PWSTR name;
  2352. UNICODE_STRING unicodeString;
  2353. ULONG protocol;
  2354. NTSTATUS status;
  2355. PAGED_CODE();
  2356. name = FileName->Buffer;
  2357. if (FileName->Length < (sizeof(OBJ_NAME_PATH_SEPARATOR) + sizeof(WCHAR))) {
  2358. return(0xFFFFFFFF);
  2359. }
  2360. //
  2361. // Step over separator.
  2362. //
  2363. if (*name++ != OBJ_NAME_PATH_SEPARATOR) {
  2364. return(0xFFFFFFFF);
  2365. }
  2366. if (*name == UNICODE_NULL) {
  2367. return(0xFFFFFFFF);
  2368. }
  2369. //
  2370. // Convert the remaining name into a number.
  2371. //
  2372. RtlInitUnicodeString(&unicodeString, name);
  2373. status = RtlUnicodeStringToInteger(&unicodeString, 10, &protocol);
  2374. if (!NT_SUCCESS(status)) {
  2375. return(0xFFFFFFFF);
  2376. }
  2377. if (protocol > 255) {
  2378. return(0xFFFFFFFF);
  2379. }
  2380. return(protocol);
  2381. }
  2382. //* CaptureCreatorSD -
  2383. //
  2384. // Captures the security-descriptor associated with an IRP_MJ_CREATE request.
  2385. //
  2386. NTSTATUS // Returns: status of the capture attempt.
  2387. CaptureCreatorSD(
  2388. PIRP Irp, // IRP_MJ_CREATE request packet.
  2389. PIO_STACK_LOCATION IrpSp, // Stack-location containing create parameters.
  2390. OUT PSECURITY_DESCRIPTOR* CreatorSD) // Receives the captured SD.
  2391. {
  2392. NTSTATUS status;
  2393. PSECURITY_DESCRIPTOR mergedSD;
  2394. PACCESS_STATE accessState =
  2395. IrpSp->Parameters.Create.SecurityContext->AccessState;
  2396. PAGED_CODE();
  2397. if (Irp->RequestorMode == KernelMode &&
  2398. IrpSp->Parameters.Create.ShareAccess == 0 &&
  2399. accessState->SecurityDescriptor == NULL) {
  2400. *CreatorSD = NULL;
  2401. status = STATUS_SUCCESS;
  2402. } else {
  2403. //
  2404. // Take a read-lock on the subject security context for the request,
  2405. // and merge the request's SD into a new SD.
  2406. //
  2407. SeLockSubjectContext(&accessState->SubjectSecurityContext);
  2408. status = SeAssignSecurity(NULL, accessState->SecurityDescriptor,
  2409. &mergedSD, FALSE,
  2410. &accessState->SubjectSecurityContext,
  2411. IoGetFileObjectGenericMapping(), PagedPool);
  2412. SeUnlockSubjectContext(&accessState->SubjectSecurityContext);
  2413. if (NT_SUCCESS(status)) {
  2414. // Request a tracked and referenced copy of the merged SD.
  2415. status = ObLogSecurityDescriptor(mergedSD, CreatorSD, 1);
  2416. ExFreePool(mergedSD);
  2417. }
  2418. }
  2419. return status;
  2420. }