Source code of Windows XP (NT5)
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.

1002 lines
31 KiB

  1. /*++
  2. Copyright (c) 1989-1993 Microsoft Corporation
  3. Module Name:
  4. Tdihndlr.c
  5. Abstract:
  6. This file contains code relating to manipulation of address objects
  7. that is specific to the NT operating system. It creates address endpoints
  8. with the transport provider.
  9. Author:
  10. Jim Stewart (Jimst) 10-2-92
  11. Revision History:
  12. --*/
  13. #include "precomp.h"
  14. //******************* Pageable Routine Declarations ****************
  15. #ifdef ALLOC_PRAGMA
  16. #pragma CTEMakePageable(PAGE, NbtTdiOpenAddress)
  17. #pragma CTEMakePageable(PAGE, NbtTdiOpenControl)
  18. #pragma CTEMakePageable(PAGE, SetEventHandler)
  19. #pragma CTEMakePageable(PAGE, SubmitTdiRequest)
  20. #endif
  21. //******************* Pageable Routine Declarations ****************
  22. //----------------------------------------------------------------------------
  23. NTSTATUS
  24. NbtTdiOpenAddress (
  25. OUT PHANDLE pHandle,
  26. OUT PDEVICE_OBJECT *ppDeviceObject,
  27. OUT PFILE_OBJECT *ppFileObject,
  28. IN tDEVICECONTEXT *pDeviceContext,
  29. IN USHORT PortNumber,
  30. IN ULONG IpAddress,
  31. IN ULONG Flags
  32. )
  33. /*++
  34. Routine Description:
  35. Note: This synchronous call may take a number of seconds. It runs in
  36. the context of the caller. The code Opens an Address object with the
  37. transport provider and then sets up event handlers for Receive,
  38. Disconnect, Datagrams and Errors.
  39. THIS ROUTINE MUST BE CALLED IN THE CONTEXT OF THE FSP (I.E.
  40. PROBABLY AN EXECUTIVE WORKER THREAD).
  41. The address data structures are found in tdi.h , but they are rather
  42. confusing since the definitions have been spread across several data types.
  43. This section shows the complete data type for Ip address:
  44. typedef struct
  45. {
  46. int TA_AddressCount;
  47. struct _TA_ADDRESS
  48. {
  49. USHORT AddressType;
  50. USHORT AddressLength;
  51. struct _TDI_ADDRESS_IP
  52. {
  53. USHORT sin_port;
  54. USHORT in_addr;
  55. UCHAR sin_zero[8];
  56. } TDI_ADDRESS_IP
  57. } TA_ADDRESS[AddressCount];
  58. } TRANSPORT_ADDRESS
  59. An EA buffer is allocated (for the IRP), with an EA name of "TransportAddress"
  60. and value is a structure of type TRANSPORT_ADDRESS.
  61. Arguments:
  62. Return Value:
  63. The function value is the status of the operation.
  64. --*/
  65. {
  66. OBJECT_ATTRIBUTES AddressAttributes;
  67. IO_STATUS_BLOCK IoStatusBlock;
  68. PFILE_FULL_EA_INFORMATION EaBuffer;
  69. NTSTATUS status, locstatus;
  70. PWSTR pNameTcp=L"Tcp";
  71. PWSTR pNameUdp=L"Udp";
  72. UNICODE_STRING ucDeviceName;
  73. PTRANSPORT_ADDRESS pTransAddressEa;
  74. PTRANSPORT_ADDRESS pTransAddr;
  75. TDI_ADDRESS_IP IpAddr;
  76. BOOLEAN Attached = FALSE;
  77. PFILE_OBJECT pFileObject;
  78. HANDLE FileHandle;
  79. ULONG i, NumAddresses, EaBufferSize;
  80. CTEPagedCode();
  81. *ppFileObject = NULL;
  82. *ppDeviceObject = NULL;
  83. // copy device name into the unicode string - either Udp or Tcp
  84. //
  85. if (Flags & TCP_FLAG)
  86. {
  87. status = CreateDeviceString(pNameTcp,&ucDeviceName);
  88. }
  89. else
  90. {
  91. status = CreateDeviceString(pNameUdp,&ucDeviceName);
  92. }
  93. if (!NT_SUCCESS(status))
  94. {
  95. return(status);
  96. }
  97. NumAddresses = 1 + pDeviceContext->NumAdditionalIpAddresses;
  98. EaBufferSize = sizeof(FILE_FULL_EA_INFORMATION) - 1 +
  99. TDI_TRANSPORT_ADDRESS_LENGTH + 1 +
  100. sizeof(TRANSPORT_ADDRESS) +
  101. NumAddresses*sizeof(TDI_ADDRESS_IP);
  102. EaBuffer = NbtAllocMem (EaBufferSize, NBT_TAG('j'));
  103. if (EaBuffer == NULL)
  104. {
  105. DbgPrint ("Nbt.NbtTdiOpenAddress: FAILed to allocate memory for Eabuffer");
  106. CTEMemFree(ucDeviceName.Buffer);
  107. return(STATUS_INSUFFICIENT_RESOURCES);
  108. }
  109. // allocate Memory for the transport address
  110. //
  111. pTransAddr = NbtAllocMem (sizeof(TRANSPORT_ADDRESS)+NumAddresses*sizeof(TDI_ADDRESS_IP),NBT_TAG('k'));
  112. if (pTransAddr == NULL)
  113. {
  114. CTEMemFree(ucDeviceName.Buffer);
  115. CTEMemFree(EaBuffer);
  116. return(STATUS_INSUFFICIENT_RESOURCES);
  117. }
  118. EaBuffer->NextEntryOffset = 0;
  119. EaBuffer->Flags = 0;
  120. EaBuffer->EaNameLength = TDI_TRANSPORT_ADDRESS_LENGTH;
  121. EaBuffer->EaValueLength = (USHORT)(sizeof(TRANSPORT_ADDRESS) -1 + NumAddresses*sizeof(TDI_ADDRESS_IP));
  122. RtlMoveMemory (EaBuffer->EaName, TdiTransportAddress, EaBuffer->EaNameLength+1); // "TransportAddress"
  123. IF_DBG(NBT_DEBUG_TDIADDR)
  124. KdPrint(("EaValueLength = %d\n",EaBuffer->EaValueLength));
  125. // fill in the IP address and Port number
  126. //
  127. pTransAddressEa = (TRANSPORT_ADDRESS *)&EaBuffer->EaName[EaBuffer->EaNameLength+1];
  128. #ifdef _NETBIOSLESS
  129. //
  130. // For message-mode, open the ANY address regardless of what is passed in
  131. // This gives us an adapter independent handle
  132. //
  133. if (IsDeviceNetbiosless(pDeviceContext))
  134. {
  135. IpAddress = IP_ANY_ADDRESS;
  136. }
  137. #endif
  138. IpAddr.sin_port = htons(PortNumber); // put in network order
  139. IpAddr.in_addr = htonl(IpAddress);
  140. // zero fill the last component of the IP address
  141. //
  142. RtlFillMemory((PVOID)&IpAddr.sin_zero, sizeof(IpAddr.sin_zero), 0);
  143. // copy the ip address to the end of the structure
  144. //
  145. RtlMoveMemory(pTransAddr->Address[0].Address, (CONST PVOID)&IpAddr, sizeof(IpAddr));
  146. pTransAddr->Address[0].AddressLength = sizeof(TDI_ADDRESS_IP);
  147. pTransAddr->Address[0].AddressType = TDI_ADDRESS_TYPE_IP;
  148. for (i=0; i<pDeviceContext->NumAdditionalIpAddresses; i++)
  149. {
  150. IpAddr.sin_port = htons(PortNumber); // put in network order
  151. IpAddr.in_addr = htonl(pDeviceContext->AdditionalIpAddresses[i]);
  152. // copy the ip address to the structure
  153. RtlMoveMemory(pTransAddr->Address[i+1].Address, (CONST PVOID)&IpAddr, sizeof(IpAddr));
  154. pTransAddr->Address[i+1].AddressLength = sizeof(TDI_ADDRESS_IP);
  155. pTransAddr->Address[i+1].AddressType = TDI_ADDRESS_TYPE_IP;
  156. }
  157. pTransAddr->TAAddressCount = NumAddresses;
  158. // copy the ip address to the end of the name in the EA structure
  159. //
  160. RtlMoveMemory((PVOID)pTransAddressEa,
  161. (CONST PVOID)pTransAddr,
  162. NumAddresses*sizeof(TDI_ADDRESS_IP) + sizeof(TRANSPORT_ADDRESS)-1);
  163. IF_DBG(NBT_DEBUG_TDIADDR)
  164. KdPrint(("creating Address named %ws\n",ucDeviceName.Buffer));
  165. #ifdef HDL_FIX
  166. InitializeObjectAttributes (&AddressAttributes,
  167. &ucDeviceName,
  168. OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
  169. NULL,
  170. NULL);
  171. #else
  172. InitializeObjectAttributes (&AddressAttributes,
  173. &ucDeviceName,
  174. OBJ_CASE_INSENSITIVE,
  175. NULL,
  176. NULL);
  177. #endif // HDL_FIX
  178. status = ZwCreateFile (&FileHandle,
  179. GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
  180. &AddressAttributes,
  181. &IoStatusBlock,
  182. NULL,
  183. FILE_ATTRIBUTE_NORMAL,
  184. (PortNumber)? 0: FILE_SHARE_READ | FILE_SHARE_WRITE, // bug 296639: allow sharing for port 0
  185. FILE_OPEN_IF,
  186. 0,
  187. (PVOID)EaBuffer,
  188. sizeof(FILE_FULL_EA_INFORMATION) - 1 +
  189. EaBuffer->EaNameLength + 1 +
  190. EaBuffer->EaValueLength);
  191. IF_DBG(NBT_DEBUG_HANDLES)
  192. KdPrint (("\t===><%x>\tNbtTdiOpenAddress->ZwCreateFile, Status = <%x>\n", FileHandle, status));
  193. CTEMemFree((PVOID)pTransAddr);
  194. CTEMemFree((PVOID)EaBuffer);
  195. CTEMemFree(ucDeviceName.Buffer);
  196. if (NT_SUCCESS(status))
  197. {
  198. // if the ZwCreate passed set the status to the IoStatus
  199. status = IoStatusBlock.Status;
  200. if (!NT_SUCCESS(status))
  201. {
  202. IF_DBG(NBT_DEBUG_TDIADDR)
  203. KdPrint(("Nbt.NbtTdiOpenAddress: Failed to Open the Address to the transport, status = %X\n",
  204. status));
  205. return(status);
  206. }
  207. // dereference the file object to keep the device ptr around to avoid
  208. // this dereference at run time
  209. //
  210. status = ObReferenceObjectByHandle (FileHandle,
  211. (ULONG)0,
  212. 0,
  213. KernelMode,
  214. (PVOID *)&pFileObject,
  215. NULL);
  216. IF_DBG(NBT_DEBUG_HANDLES)
  217. KdPrint (("\t ++<%x>====><%x>\tNbtTdiOpenAddress->ObReferenceObjectByHandle, Status = <%x>\n", FileHandle, pFileObject, status));
  218. if (NT_SUCCESS(status))
  219. {
  220. // return the handle to the caller
  221. //
  222. *pHandle = FileHandle;
  223. //
  224. // return the parameter to the caller
  225. //
  226. *ppFileObject = pFileObject;
  227. *ppDeviceObject = IoGetRelatedDeviceObject(*ppFileObject);
  228. status = SetEventHandler (*ppDeviceObject,
  229. *ppFileObject,
  230. TDI_EVENT_ERROR,
  231. (PVOID)TdiErrorHandler,
  232. (PVOID)pDeviceContext);
  233. if (NT_SUCCESS(status))
  234. {
  235. // if this is a TCP address being opened, then create different
  236. // event handlers for connections
  237. //
  238. if (Flags & TCP_FLAG)
  239. {
  240. status = SetEventHandler (*ppDeviceObject,
  241. *ppFileObject,
  242. TDI_EVENT_RECEIVE,
  243. (PVOID)TdiReceiveHandler,
  244. (PVOID)pDeviceContext);
  245. if (NT_SUCCESS(status))
  246. {
  247. status = SetEventHandler (*ppDeviceObject,
  248. *ppFileObject,
  249. TDI_EVENT_DISCONNECT,
  250. (PVOID)TdiDisconnectHandler,
  251. (PVOID)pDeviceContext);
  252. if (NT_SUCCESS(status))
  253. {
  254. // only set a connect handler if the session flag is set.
  255. // In this case the address being opened is the Netbios session
  256. // port 139
  257. //
  258. if (Flags & SESSION_FLAG)
  259. {
  260. status = SetEventHandler (*ppDeviceObject,
  261. *ppFileObject,
  262. TDI_EVENT_CONNECT,
  263. (PVOID)TdiConnectHandler,
  264. (PVOID)pDeviceContext);
  265. if (NT_SUCCESS(status))
  266. {
  267. return(status);
  268. }
  269. }
  270. else
  271. return(status);
  272. }
  273. }
  274. }
  275. else
  276. {
  277. // Datagram ports only need this event handler
  278. #ifdef _NETBIOSLESS
  279. if (PortNumber == pDeviceContext->DatagramPort)
  280. #else
  281. if (PortNumber == NBT_DATAGRAM_UDP_PORT)
  282. #endif
  283. {
  284. // Datagram Udp Handler
  285. status = SetEventHandler (*ppDeviceObject,
  286. *ppFileObject,
  287. TDI_EVENT_RECEIVE_DATAGRAM,
  288. (PVOID)TdiRcvDatagramHandler,
  289. (PVOID)pDeviceContext);
  290. if (NT_SUCCESS(status))
  291. {
  292. return(status);
  293. }
  294. }
  295. else
  296. {
  297. // Name Service Udp handler
  298. status = SetEventHandler (*ppDeviceObject,
  299. *ppFileObject,
  300. TDI_EVENT_RECEIVE_DATAGRAM,
  301. (PVOID)TdiRcvNameSrvHandler,
  302. (PVOID)pDeviceContext);
  303. if (NT_SUCCESS(status))
  304. {
  305. return(status);
  306. }
  307. }
  308. }
  309. //
  310. // ERROR Case
  311. //
  312. ObDereferenceObject(pFileObject);
  313. IF_DBG(NBT_DEBUG_HANDLES)
  314. KdPrint (("\t --< ><====<%x>\tNbtTdiOpenAddress->ObDereferenceObject\n", pFileObject));
  315. locstatus = ZwClose(FileHandle);
  316. IF_DBG(NBT_DEBUG_HANDLES)
  317. KdPrint (("\t<===<%x>\tNbtTdiOpenAddress1->ZwClose, status = <%x>\n", FileHandle, locstatus));
  318. return(status);
  319. }
  320. }
  321. else
  322. {
  323. IF_DBG(NBT_DEBUG_TDIADDR)
  324. KdPrint(("Failed Open Address (Dereference Object) status = %X\n", status));
  325. locstatus = ZwClose(FileHandle);
  326. IF_DBG(NBT_DEBUG_HANDLES)
  327. KdPrint (("\t<===<%x>\tNbtTdiOpenAddress2->ZwClose, status = <%x>\n", FileHandle, locstatus));
  328. }
  329. }
  330. else
  331. {
  332. IF_DBG(NBT_DEBUG_TDIADDR)
  333. KdPrint(("Nbt.NbtTdiOpenAddress: ZwCreateFile Failed, status = %X\n", status));
  334. }
  335. return(status);
  336. }
  337. //----------------------------------------------------------------------------
  338. NTSTATUS
  339. NbtTdiOpenControl (
  340. IN tDEVICECONTEXT *pDeviceContext
  341. )
  342. /*++
  343. Routine Description:
  344. This routine opens a control object with the transport. It is very similar
  345. to opening an address object, above.
  346. Arguments:
  347. Return Value:
  348. Status of the operation.
  349. --*/
  350. {
  351. IO_STATUS_BLOCK IoStatusBlock;
  352. NTSTATUS Status, locstatus;
  353. OBJECT_ATTRIBUTES ObjectAttributes;
  354. PWSTR pName=L"Tcp";
  355. PFILE_FULL_EA_INFORMATION EaBuffer;
  356. UNICODE_STRING DeviceName;
  357. BOOLEAN Attached = FALSE;
  358. CTEPagedCode();
  359. // copy device name into the unicode string
  360. Status = CreateDeviceString(pName,&DeviceName);
  361. if (!NT_SUCCESS(Status))
  362. {
  363. return(Status);
  364. }
  365. #ifdef HDL_FIX
  366. InitializeObjectAttributes (&ObjectAttributes,
  367. &DeviceName,
  368. OBJ_KERNEL_HANDLE,
  369. NULL,
  370. NULL);
  371. #else
  372. InitializeObjectAttributes (&ObjectAttributes,
  373. &DeviceName,
  374. 0,
  375. NULL,
  376. NULL);
  377. #endif // HDL_FIX
  378. IF_DBG(NBT_DEBUG_TDIADDR)
  379. KdPrint(("Nbt.NbtTdiOpenControl: Tcp device to open = %ws\n", DeviceName.Buffer));
  380. EaBuffer = NULL;
  381. Status = ZwCreateFile ((PHANDLE)&pDeviceContext->hControl,
  382. GENERIC_READ | GENERIC_WRITE,
  383. &ObjectAttributes, // object attributes.
  384. &IoStatusBlock, // returned status information.
  385. NULL, // block size (unused).
  386. FILE_ATTRIBUTE_NORMAL, // file attributes.
  387. 0,
  388. FILE_CREATE,
  389. 0, // create options.
  390. (PVOID)EaBuffer, // EA buffer.
  391. 0); // Ea length
  392. CTEMemFree(DeviceName.Buffer);
  393. IF_DBG(NBT_DEBUG_HANDLES)
  394. KdPrint (("\t===><%x>\tNbtTdiOpenControl->ZwCreateFile, Status = <%x>\n", pDeviceContext->hControl, Status));
  395. if ( NT_SUCCESS( Status ))
  396. {
  397. // if the ZwCreate passed set the status to the IoStatus
  398. Status = IoStatusBlock.Status;
  399. if (!NT_SUCCESS(Status))
  400. {
  401. IF_DBG(NBT_DEBUG_TDIADDR)
  402. KdPrint(("Nbt:Failed to Open the control connection to the transport, status = %X\n",Status));
  403. }
  404. else
  405. {
  406. // get a reference to the file object and save it since we can't
  407. // dereference a file handle at DPC level so we do it now and keep
  408. // the ptr around for later.
  409. Status = ObReferenceObjectByHandle (pDeviceContext->hControl,
  410. 0L,
  411. NULL,
  412. KernelMode,
  413. (PVOID *)&pDeviceContext->pControlFileObject,
  414. NULL);
  415. IF_DBG(NBT_DEBUG_HANDLES)
  416. KdPrint (("\t ++<%x>====><%x>\tNbtTdiOpenControl->ObReferenceObjectByHandle, Status = <%x>\n", pDeviceContext->hControl, pDeviceContext->pControlFileObject, Status));
  417. if (!NT_SUCCESS(Status))
  418. {
  419. locstatus = ZwClose(pDeviceContext->hControl);
  420. IF_DBG(NBT_DEBUG_HANDLES)
  421. KdPrint (("\t<===<%x>\tNbtTdiOpenControl->ZwClose, status = <%x>\n", pDeviceContext->hControl, locstatus));
  422. pDeviceContext->hControl = NULL;
  423. }
  424. else
  425. {
  426. pDeviceContext->pControlDeviceObject =
  427. IoGetRelatedDeviceObject(pDeviceContext->pControlFileObject);
  428. }
  429. }
  430. }
  431. else
  432. {
  433. IF_DBG(NBT_DEBUG_TDIADDR)
  434. KdPrint(("Nbt:Failed to Open the control connection to the transport, status1 = %X\n", Status));
  435. // set control file object ptr to null so we know that we didnot open
  436. // the control point.
  437. //
  438. pDeviceContext->pControlFileObject = NULL;
  439. }
  440. return Status;
  441. } /* NbtTdiOpenControl */
  442. //----------------------------------------------------------------------------
  443. NTSTATUS
  444. NbtTdiCompletionRoutine(
  445. IN PDEVICE_OBJECT DeviceObject,
  446. IN PIRP Irp,
  447. IN PVOID Context
  448. )
  449. /*++
  450. Routine Description:
  451. This routine does not complete the Irp. It is used to signal to a
  452. synchronous part of the NBT driver that it can proceed (i.e.
  453. to allow some code that is waiting on a "KeWaitForSingleObject" to
  454. proceeed.
  455. Arguments:
  456. DeviceObject - unused.
  457. Irp - Supplies Irp that the transport has finished processing.
  458. Context - Supplies the event associated with the Irp.
  459. Return Value:
  460. The STATUS_MORE_PROCESSING_REQUIRED so that the IO system stops
  461. processing Irp stack locations at this point.
  462. --*/
  463. {
  464. IF_DBG(NBT_DEBUG_TDIADDR)
  465. KdPrint( ("Nbt.NbtTdiCompletionRoutine: CompletionEvent: %X, Irp: %X, DeviceObject: %X\n",
  466. Context, Irp, DeviceObject));
  467. KeSetEvent((PKEVENT )Context, 0, FALSE);
  468. return STATUS_MORE_PROCESSING_REQUIRED;
  469. UNREFERENCED_PARAMETER( DeviceObject );
  470. UNREFERENCED_PARAMETER( Irp );
  471. }
  472. //----------------------------------------------------------------------------
  473. NTSTATUS
  474. SetEventHandler (
  475. IN PDEVICE_OBJECT DeviceObject,
  476. IN PFILE_OBJECT FileObject,
  477. IN ULONG EventType,
  478. IN PVOID EventHandler,
  479. IN PVOID Context
  480. )
  481. /*++
  482. Routine Description:
  483. This routine registers an event handler with a TDI transport provider.
  484. Arguments:
  485. IN PDEVICE_OBJECT DeviceObject - Supplies the device object of the transport provider.
  486. IN PFILE_OBJECT FileObject - Supplies the address object's file object.
  487. IN ULONG EventType, - Supplies the type of event.
  488. IN PVOID EventHandler - Supplies the event handler.
  489. IN PVOID Context - Supplies the context passed into the event handler when it runs
  490. Return Value:
  491. NTSTATUS - Final status of the set event operation
  492. --*/
  493. {
  494. NTSTATUS Status;
  495. PIRP Irp;
  496. CTEPagedCode();
  497. Irp = IoAllocateIrp(IoGetRelatedDeviceObject(FileObject)->StackSize, FALSE);
  498. if (Irp == NULL)
  499. {
  500. return(STATUS_INSUFFICIENT_RESOURCES);
  501. }
  502. TdiBuildSetEventHandler(Irp, DeviceObject, FileObject,
  503. NULL, NULL,
  504. EventType, EventHandler, Context);
  505. Status = SubmitTdiRequest(FileObject, Irp);
  506. IoFreeIrp(Irp);
  507. return Status;
  508. }
  509. //----------------------------------------------------------------------------
  510. NTSTATUS
  511. NbtProcessIPRequest(
  512. IN ULONG IOControlCode,
  513. IN PVOID pInBuffer,
  514. IN ULONG InBufferLen,
  515. OUT PVOID *pOutBuffer,
  516. IN OUT ULONG *pOutBufferLen
  517. )
  518. /*++
  519. Routine Description:
  520. This routine performs iIOCTL queries into IP
  521. Arguments:
  522. IOControlCode - Ioctl to be made into IP
  523. pInBuffer - Buffer containing data to be passed into IP
  524. InBufferLen - Length of Input Buffer data
  525. pOutBuffer - Returned information
  526. pOutBufferLen - Initial expected length of Output Buffer + final length
  527. Return Value:
  528. NTSTATUS - Final status of the operation
  529. --*/
  530. {
  531. NTSTATUS Status;
  532. HANDLE hIP;
  533. OBJECT_ATTRIBUTES ObjectAttributes;
  534. UNICODE_STRING ucDeviceName;
  535. PWSTR pNameIP = L"IP";
  536. IO_STATUS_BLOCK IoStatusBlock;
  537. UCHAR *pIPInfo = NULL;
  538. ULONG OutBufferLen = 0;
  539. BOOLEAN fAttached = FALSE;
  540. HANDLE Event = NULL;
  541. CTEPagedCode();
  542. ucDeviceName.Buffer = NULL;
  543. Status = CreateDeviceString (pNameIP, &ucDeviceName);
  544. if (!NT_SUCCESS (Status))
  545. {
  546. KdPrint (("Nbt.NbtProcessIPRequest: ERROR <%x> -- CreateDeviceString\n", Status));
  547. return (STATUS_INSUFFICIENT_RESOURCES);
  548. }
  549. if (pOutBuffer)
  550. {
  551. ASSERT (pOutBufferLen);
  552. OutBufferLen = *pOutBufferLen; // Save the initial buffer length
  553. *pOutBuffer = NULL;
  554. *pOutBufferLen = 0; // Initialize the return parameter in case we fail below
  555. if (!OutBufferLen ||
  556. !(pIPInfo = NbtAllocMem (OutBufferLen, NBT_TAG2('a9'))))
  557. {
  558. if (ucDeviceName.Buffer)
  559. {
  560. CTEMemFree (ucDeviceName.Buffer);
  561. }
  562. KdPrint (("Nbt.NbtProcessIPRequest: ERROR <STATUS_INSUFFICIENT_RESOURCES>\n"));
  563. return (STATUS_INSUFFICIENT_RESOURCES);
  564. }
  565. }
  566. #ifdef HDL_FIX
  567. InitializeObjectAttributes (&ObjectAttributes,
  568. &ucDeviceName,
  569. OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
  570. NULL,
  571. NULL);
  572. #else
  573. InitializeObjectAttributes (&ObjectAttributes,
  574. &ucDeviceName,
  575. OBJ_CASE_INSENSITIVE,
  576. NULL,
  577. NULL);
  578. #endif // HDL_FIX
  579. CTEAttachFsp(&fAttached, REF_FSP_PROCESS_IP_REQUEST);
  580. Status = ZwCreateFile (&hIP,
  581. SYNCHRONIZE | GENERIC_READ,
  582. &ObjectAttributes,
  583. &IoStatusBlock,
  584. NULL,
  585. FILE_ATTRIBUTE_NORMAL,
  586. 0,
  587. FILE_OPEN,
  588. 0,
  589. NULL,
  590. 0);
  591. CTEMemFree(ucDeviceName.Buffer);
  592. //
  593. // If we succeeded above, let us also try to create the Event handle
  594. //
  595. if ((NT_SUCCESS (Status)) &&
  596. (!NT_SUCCESS (Status = ZwCreateEvent(&Event, EVENT_ALL_ACCESS, NULL, SynchronizationEvent, FALSE))))
  597. {
  598. ZwClose (hIP);
  599. }
  600. if (!NT_SUCCESS (Status))
  601. {
  602. CTEDetachFsp(fAttached, REF_FSP_PROCESS_IP_REQUEST);
  603. KdPrint (("Nbt.NbtProcessIPRequest: ERROR <%x> -- ZwCreate\n", Status));
  604. if (pIPInfo)
  605. {
  606. CTEMemFree (pIPInfo);
  607. }
  608. return (Status);
  609. }
  610. //
  611. // At this point, we have succeeded in creating the hIP and Event handles,
  612. // and possibly also the output buffer memory (pIPInfo)
  613. //
  614. do
  615. {
  616. Status = ZwDeviceIoControlFile(hIP, // g_hIPDriverHandle
  617. Event,
  618. NULL,
  619. NULL,
  620. &IoStatusBlock,
  621. IOControlCode, // Ioctl
  622. pInBuffer,
  623. InBufferLen,
  624. pIPInfo,
  625. OutBufferLen);
  626. if (Status == STATUS_PENDING)
  627. {
  628. Status = NtWaitForSingleObject (Event, FALSE, NULL);
  629. ASSERT(Status == STATUS_SUCCESS);
  630. }
  631. Status = IoStatusBlock.Status;
  632. if (Status == STATUS_BUFFER_OVERFLOW)
  633. {
  634. if (!OutBufferLen)
  635. {
  636. KdPrint (("Nbt.NbtProcessIPRequest: <%x> => overflow when no data expected\n",IOControlCode));
  637. Status = STATUS_UNSUCCESSFUL;
  638. break;
  639. }
  640. CTEMemFree (pIPInfo);
  641. OutBufferLen *=2;
  642. if (NULL == (pIPInfo = NbtAllocMem (OutBufferLen, NBT_TAG2('b0'))))
  643. {
  644. Status = STATUS_INSUFFICIENT_RESOURCES;
  645. }
  646. }
  647. } while (Status == STATUS_BUFFER_OVERFLOW);
  648. ZwClose (Event);
  649. ZwClose (hIP);
  650. CTEDetachFsp(fAttached, REF_FSP_PROCESS_IP_REQUEST);
  651. if (NT_SUCCESS(Status))
  652. {
  653. IF_DBG(NBT_DEBUG_PNP_POWER)
  654. KdPrint(("Nbt.NbtProcessIPRequest: Success, Ioctl=<%x>\n", IOControlCode));
  655. if ((pOutBuffer) && (pOutBufferLen))
  656. {
  657. *pOutBuffer = pIPInfo;
  658. *pOutBufferLen = OutBufferLen;
  659. }
  660. else if (pIPInfo)
  661. {
  662. CTEMemFree (pIPInfo);
  663. }
  664. }
  665. else
  666. {
  667. KdPrint(("Nbt.NbtProcessIPRequest: IOCTL <%x> FAILed <%x>\n", IOControlCode, Status));
  668. if (pIPInfo)
  669. {
  670. CTEMemFree (pIPInfo);
  671. }
  672. }
  673. return (Status);
  674. }
  675. #if FAST_DISP
  676. //----------------------------------------------------------------------------
  677. NTSTATUS
  678. NbtQueryIpHandler(
  679. IN PFILE_OBJECT FileObject,
  680. IN ULONG IOControlCode,
  681. OUT PVOID *EntryPoint
  682. )
  683. /*++
  684. Routine Description:
  685. This routine iIOCTL queries for fast send entry
  686. Arguments:
  687. IN PFILE_OBJECT FileObject - Supplies the address object's file object.
  688. IN PLONG EntryPoint - Holder of fast send address
  689. Return Value:
  690. NTSTATUS - Final status of the set event operation
  691. --*/
  692. {
  693. NTSTATUS Status;
  694. PIRP Irp;
  695. PIO_STACK_LOCATION irpSp;
  696. IO_STATUS_BLOCK iosb;
  697. CTEPagedCode();
  698. if (!(Irp = IoAllocateIrp(IoGetRelatedDeviceObject(FileObject)->StackSize, FALSE)))
  699. {
  700. return(STATUS_INSUFFICIENT_RESOURCES);
  701. }
  702. //Build IRP for sync io.
  703. Irp->MdlAddress = NULL;
  704. Irp->Flags = (LONG)IRP_SYNCHRONOUS_API;
  705. Irp->RequestorMode = KernelMode;
  706. Irp->PendingReturned = FALSE;
  707. Irp->UserIosb = &iosb;
  708. Irp->Overlay.AsynchronousParameters.UserApcRoutine = NULL;
  709. Irp->AssociatedIrp.SystemBuffer = NULL;
  710. Irp->UserBuffer = NULL;
  711. Irp->Tail.Overlay.Thread = PsGetCurrentThread();
  712. Irp->Tail.Overlay.OriginalFileObject = FileObject;
  713. Irp->Tail.Overlay.AuxiliaryBuffer = NULL;
  714. Irp->IoStatus.Status = 0;
  715. Irp->IoStatus.Information = 0;
  716. irpSp = IoGetNextIrpStackLocation( Irp );
  717. irpSp->FileObject = FileObject;
  718. irpSp->DeviceObject = IoGetRelatedDeviceObject(FileObject);
  719. irpSp->MajorFunction = IRP_MJ_DEVICE_CONTROL;
  720. irpSp->MinorFunction = 0;
  721. irpSp->Parameters.DeviceIoControl.IoControlCode = IOControlCode;
  722. irpSp->Parameters.DeviceIoControl.Type3InputBuffer = EntryPoint;
  723. // Now submit the Irp to know if tcp supports fast path
  724. Status = SubmitTdiRequest(FileObject, Irp);
  725. if (!NT_SUCCESS(Status))
  726. {
  727. *EntryPoint = NULL;
  728. IF_DBG(NBT_DEBUG_TDIADDR)
  729. KdPrint(("Nbt.NbtQueryDirectSendEntry: Query failed status %x \n", Status));
  730. }
  731. Irp->UserIosb = NULL;
  732. IoFreeIrp(Irp);
  733. return Status;
  734. }
  735. #endif
  736. //----------------------------------------------------------------------------
  737. NTSTATUS
  738. SubmitTdiRequest (
  739. IN PFILE_OBJECT FileObject,
  740. IN PIRP Irp
  741. )
  742. /*++
  743. Routine Description:
  744. This routine submits a request to TDI and waits for it to complete.
  745. Arguments:
  746. IN PFILE_OBJECT FileObject - Connection or Address handle for TDI request
  747. IN PIRP Irp - TDI request to submit.
  748. Return Value:
  749. NTSTATUS - Final status of request.
  750. --*/
  751. {
  752. KEVENT Event;
  753. NTSTATUS Status;
  754. CTEPagedCode();
  755. KeInitializeEvent (&Event, NotificationEvent, FALSE);
  756. // set the address of the routine to be executed when the IRP
  757. // finishes. This routine signals the event and allows the code
  758. // below to continue (i.e. KeWaitForSingleObject)
  759. //
  760. IoSetCompletionRoutine(Irp,
  761. (PIO_COMPLETION_ROUTINE)NbtTdiCompletionRoutine,
  762. (PVOID)&Event,
  763. TRUE, TRUE, TRUE);
  764. CHECK_COMPLETION(Irp);
  765. Status = IoCallDriver(IoGetRelatedDeviceObject(FileObject), Irp);
  766. //
  767. // If it failed immediately, return now, otherwise wait.
  768. //
  769. if (!NT_SUCCESS(Status))
  770. {
  771. IF_DBG(NBT_DEBUG_TDIADDR)
  772. KdPrint(("Nbt.SubmitTdiRequest: Failed to Submit Tdi Request, status = %X\n",Status));
  773. return Status;
  774. }
  775. if (Status == STATUS_PENDING)
  776. {
  777. Status = KeWaitForSingleObject ((PVOID)&Event, // Object to wait on.
  778. Executive, // Reason for waiting
  779. KernelMode, // Processor mode
  780. FALSE, // Alertable
  781. NULL); // Timeout
  782. ASSERT(Status == STATUS_SUCCESS);
  783. if (!NT_SUCCESS(Status))
  784. {
  785. IF_DBG(NBT_DEBUG_TDIADDR)
  786. KdPrint(("Nbt.SubmitTdiRequest: Failed on return from KeWaitForSingleObj, status = %X\n",
  787. Status));
  788. return Status;
  789. }
  790. Status = Irp->IoStatus.Status;
  791. IF_DBG(NBT_DEBUG_TDIADDR)
  792. KdPrint(("Nbt.SubmitTdiRequest: Io Status from setting event = %X\n",Status));
  793. }
  794. return(Status);
  795. }