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.

6610 lines
204 KiB

  1. /*++
  2. Copyright (c) 1989-1993 Microsoft Corporation
  3. Module Name:
  4. Ntisol.h
  5. Abstract:
  6. This file contains the interface between the TDI interface on the top
  7. of NBT and the OS independent code. It takes the parameters out of the
  8. irps and puts in into procedure calls for the OS independent code (which
  9. is mostly in name.c).
  10. Author:
  11. Jim Stewart (Jimst) 10-2-92
  12. Revision History:
  13. Notes:
  14. The Nbt routines have been modified to include an additional parameter, i.e,
  15. the transport type. This transport type is used primarily to distinguish the
  16. NETBIOS over TCP/IP implementation from the Messaging Over TCP/IP implementation.
  17. The primary difference between the two being that the later uses the NETBT framing
  18. without the associated NETBIOS name registartion/resolution. It primarily uses
  19. DNS for name resolution. All the names that are registered for the new transport
  20. are local names and are not defended on the network.
  21. The primary usage is in conjuntion with an extended NETBIOS address type defined
  22. in tdi.h. The NETBIOS name resolution/registration traffic occurs in two phases.
  23. The first phase contains all the broadcast traffic that ensues during NETBIOS
  24. name registration. Subsequently the NETBT implementation queries the remote
  25. adapter status to choose the appropriate called name. This approach results in
  26. additional traffic for querying the remote adapter status. The new address type
  27. defined in tdi.h enables the client of netbt to supply the name to be used in
  28. NETBT session setup. This avoids the network traffic for querying the adapter
  29. status.
  30. The original design which has not been fully implemented involved exposing two
  31. device objects from the NetBt driver -- the NetBt device object which would be
  32. the full implementation of NETBIOS over TCP/IP and the MoTcp device object which
  33. would be the implementation of Messaging over TCP/IP. The MoTcp device object
  34. would use the same port address as NetBt and use the same session setup protocol
  35. to talk to remote machines running old NetBt drivers and machines running new
  36. NetBt drivers.
  37. The transport type variations combined with the address type changes present us
  38. with four different cases which need to be handled -- the NetBt transport being
  39. presented with a TDI_ADDRESS_NETBIOS_EX structure, the NetBt transport being
  40. prsented with a TDI_ADDRESS_NETBIOS structure and the same two cases for the
  41. MoTcp transport.
  42. --*/
  43. #include "precomp.h"
  44. #include "ntprocs.h"
  45. #include <ipinfo.h>
  46. #include <ntddtcp.h> // for IOCTL_TCP_SET_INFORMATION_EX
  47. #ifdef RASAUTODIAL
  48. #include <acd.h>
  49. #include <acdapi.h>
  50. #endif // RASAUTODIAL
  51. #include <tcpinfo.h>
  52. #include <tdiinfo.h>
  53. #include "ntisol.tmh"
  54. #if BACK_FILL
  55. #define SESSION_HDR_SIZE sizeof(tSESSIONHDR)
  56. #endif
  57. NTSTATUS
  58. SendCompletion(
  59. IN PDEVICE_OBJECT DeviceObject,
  60. IN PIRP Irp,
  61. IN PVOID Context
  62. );
  63. NTSTATUS
  64. NTSendCleanupConnection(
  65. IN tCONNECTELE *pConnEle,
  66. IN PVOID pCompletionRoutine,
  67. IN PVOID Context,
  68. IN PIRP pIrp);
  69. VOID
  70. DpcSendSession(
  71. IN PKDPC pDpc,
  72. IN PVOID Context,
  73. IN PVOID SystemArgument1,
  74. IN PVOID SystemArgument2
  75. );
  76. NBT_WORK_ITEM_CONTEXT *
  77. FindLmhSvcRequest(
  78. IN PDEVICE_OBJECT DeviceContext,
  79. IN PIRP pIrp,
  80. IN tLMHSVC_REQUESTS *pLmhRequest
  81. );
  82. NTSTATUS
  83. NbtCancelCancelRoutine(
  84. IN PIRP pIrp
  85. );
  86. #ifdef RASAUTODIAL
  87. extern ACD_DRIVER AcdDriverG;
  88. BOOLEAN
  89. NbtCancelPostConnect(
  90. IN PIRP pIrp
  91. );
  92. #endif // RASAUTODIAL
  93. NTSTATUS
  94. NbtQueryGetAddressInfo(
  95. IN PIO_STACK_LOCATION pIrpSp,
  96. OUT PVOID *ppBuffer,
  97. OUT ULONG *pSize
  98. );
  99. VOID
  100. NbtCancelConnect(
  101. IN PDEVICE_OBJECT pDeviceContext,
  102. IN PIRP pIrp
  103. );
  104. VOID
  105. NbtCancelReceive(
  106. IN PDEVICE_OBJECT DeviceContext,
  107. IN PIRP pIrp
  108. );
  109. NTSTATUS
  110. GetIpAddrs(
  111. IN tDEVICECONTEXT *pDeviceContext,
  112. IN PIRP pIrp
  113. );
  114. typedef struct
  115. {
  116. struct _DeviceContext *pDeviceContext;
  117. PIRP pClientIrp;
  118. PIRP pLocalIrp;
  119. PTA_NETBT_INTERNAL_ADDRESS pTransportAddress;
  120. TDI_CONNECTION_INFORMATION LocalConnectionInformation;
  121. BOOLEAN ProcessingDone;
  122. TDI_ADDRESS_NETBIOS_UNICODE_EX *pUnicodeAddress; // First Readable buffer in the transport address list
  123. TDI_ADDRESS_NETBIOS_UNICODE_EX *pReturnBuffer; // First writable buffer in the transport address list
  124. LONG CurrIndex, NumberOfAddresses;
  125. LONG TaAddressLength, RemainingAddressLength;
  126. PUCHAR pTaAddress;
  127. } NBT_DELAYED_CONNECT_CONTEXT, *PNBT_DELAYED_CONNECT_CONTEXT;
  128. extern POBJECT_TYPE *IoFileObjectType;
  129. NTSTATUS
  130. InitDelayedNbtProcessConnect(
  131. IN PNBT_DELAYED_CONNECT_CONTEXT pDelConnCtx
  132. );
  133. //******************* Pageable Routine Declarations ****************
  134. #ifdef ALLOC_PRAGMA
  135. #pragma CTEMakePageable(PAGE, NTOpenControl)
  136. #pragma CTEMakePageable(PAGE, NTOpenAddr)
  137. #pragma CTEMakePageable(PAGE, NTCloseAddress)
  138. #pragma CTEMakePageable(PAGE, NTOpenConnection)
  139. #pragma CTEMakePageable(PAGE, NTAssocAddress)
  140. #pragma CTEMakePageable(PAGE, NTCloseConnection)
  141. #pragma CTEMakePageable(PAGE, NTSetSharedAccess)
  142. #pragma CTEMakePageable(PAGE, NTCheckSharedAccess)
  143. #pragma CTEMakePageable(PAGE, NTCleanUpConnection)
  144. #pragma CTEMakePageable(PAGE, NTCleanUpAddress)
  145. #pragma CTEMakePageable(PAGE, NTDisAssociateAddress)
  146. #pragma CTEMakePageable(PAGE, NTListen)
  147. #pragma CTEMakePageable(PAGE, DelayedNbtProcessConnect)
  148. #pragma CTEMakePageable(PAGE, InitDelayedNbtProcessConnect)
  149. #pragma CTEMakePageable(PAGE, DispatchIoctls)
  150. #pragma CTEMakePageable(PAGE, NTSendDatagram)
  151. #pragma CTEMakePageable(PAGE, NTSetInformation)
  152. #pragma CTEMakePageable(PAGE, NTSetEventHandler)
  153. //
  154. // Should not be pageable since AFD can call us at raised Irql in case of AcceptEx.
  155. //
  156. // #pragma CTEMakePageable(PAGE, NTQueryInformation)
  157. #endif
  158. //******************* Pageable Routine Declarations ****************
  159. int check_unicode_string(IN PUNICODE_STRING str);
  160. //----------------------------------------------------------------------------
  161. NTSTATUS
  162. NTOpenControl(
  163. IN tDEVICECONTEXT *pDeviceContext,
  164. IN PIRP pIrp)
  165. /*++
  166. Routine Description:
  167. This Routine handles opening the control object, which represents the
  168. driver itself. For example QueryInformation uses the control object
  169. as the destination of the Query message.
  170. Arguments:
  171. pIrp - a ptr to an IRP
  172. Return Value:
  173. NTSTATUS - status of the request
  174. --*/
  175. {
  176. PIO_STACK_LOCATION pIrpSp;
  177. NTSTATUS status;
  178. CTEPagedCode();
  179. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  180. pIrpSp->FileObject->FsContext2 = (PVOID)(NBT_CONTROL_TYPE);
  181. // return a ptr the control endpoint
  182. pIrpSp->FileObject->FsContext = (PVOID)pNbtGlobConfig->pControlObj;
  183. //
  184. // the following call opens a control object with the transport below since
  185. // several of the query information calls are passed directly on to the
  186. // transport below.
  187. //
  188. if (!pDeviceContext->pControlFileObject)
  189. {
  190. status = NbtTdiOpenControl(pDeviceContext);
  191. }
  192. else
  193. status = STATUS_SUCCESS;
  194. return(status);
  195. }
  196. //----------------------------------------------------------------------------
  197. NTSTATUS
  198. NTOpenAddr(
  199. IN tDEVICECONTEXT *pDeviceContext,
  200. IN PIRP pIrp,
  201. IN PFILE_FULL_EA_INFORMATION ea)
  202. /*++
  203. Routine Description:
  204. This Routine handles converting an Open Address Request from an IRP to
  205. a procedure call so that NbtOpenAddress can be called in an OS independent
  206. manner.
  207. Arguments:
  208. pIrp - a ptr to an IRP
  209. Return Value:
  210. NTSTATUS - status of the request
  211. --*/
  212. {
  213. TDI_REQUEST Request;
  214. PVOID pSecurityDesc;
  215. TA_ADDRESS *pAddress;
  216. int j;
  217. NTSTATUS status=STATUS_INVALID_ADDRESS_COMPONENT;
  218. ULONG BufferLength, MinBufferLength;
  219. TRANSPORT_ADDRESS UNALIGNED *pTransportAddr; // structure containing counted array of TA_ADDRESS
  220. PTDI_ADDRESS_NETBIOS pNetbiosAddress;
  221. PTDI_ADDRESS_NETBIOS_EX pNetbiosExAddress;
  222. CTEPagedCode();
  223. // make up the Request data structure from the IRP info
  224. Request.Handle.AddressHandle = NULL;
  225. //
  226. // Verify Minimum Buffer length!
  227. // Bug#: 120683
  228. //
  229. BufferLength = ea->EaValueLength;
  230. if (BufferLength < sizeof(TA_NETBIOS_ADDRESS))
  231. {
  232. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  233. KdPrint(("Nbt.NTOpenAddr[1]: ...Rejecting Open Address request -- BufferLength<%d> < Min<%d>\n",
  234. BufferLength, sizeof(TA_NETBIOS_ADDRESS)));
  235. NbtTrace(NBT_TRACE_LOCALNAMES, ("Rejecting Open Address request -- BufferLength<%d> < Min<%d>",
  236. BufferLength, sizeof(TA_NETBIOS_ADDRESS)));
  237. return (status);
  238. }
  239. MinBufferLength = FIELD_OFFSET(TRANSPORT_ADDRESS,Address); // Set for Address[0]
  240. pTransportAddr = (PTRANSPORT_ADDRESS)&ea->EaName[ea->EaNameLength+1];
  241. pAddress = (TA_ADDRESS *) &pTransportAddr->Address[0]; // this includes the address type + the actual address
  242. //
  243. // The Transport Address information is packed as follows:
  244. // Field: Length:
  245. // ------ -------
  246. // TAAddressCount --> LONG
  247. //
  248. // Address[0].AddressLength --> USHORT
  249. // Address[0].AddressType --> USHORT
  250. // Address[0].Address.. --> Address[0].AddressLength
  251. //
  252. // Address[1].AddressLength --> USHORT
  253. // Address[1].AddressType --> USHORT
  254. // Address[1].Address.. --> Address[1].AddressLength
  255. // :
  256. //
  257. // loop through the addresses passed in until ONE is successfully used
  258. // *TODO* do we need this loop or can we just assume the name is at the start of the address buffer...
  259. // *TODO* does this need to handle multiple names??
  260. for (j=0; j<pTransportAddr->TAAddressCount ;j++ )
  261. {
  262. //
  263. // We support only 2 address types:
  264. //
  265. if (pAddress->AddressType == TDI_ADDRESS_TYPE_NETBIOS)
  266. {
  267. pNetbiosAddress = (PTDI_ADDRESS_NETBIOS) pAddress->Address;
  268. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  269. KdPrint(("Nbt.NTOpenAddr: ...Opening NETBIOS Address=<%-16.16s:%x>, Device=<%p>, pIrp=<%p>\n",
  270. pNetbiosAddress->NetbiosName, pNetbiosAddress->NetbiosName[15], pDeviceContext, pIrp));
  271. if (pAddress->AddressLength != 0)
  272. {
  273. status = STATUS_SUCCESS;
  274. break;
  275. }
  276. ASSERT(0); // AddressLength should not be 0!
  277. }
  278. else if (pAddress->AddressType == TDI_ADDRESS_TYPE_NETBIOS_EX)
  279. {
  280. //
  281. // In our test earlier we had verified Buffer space for only TDI_NETBIOS_ADDRESS,
  282. // not TDI_NETBIOS_EX_ADDRESS, so verify that now!
  283. //
  284. if (BufferLength < (MinBufferLength +
  285. (sizeof(TA_NETBIOS_EX_ADDRESS)-FIELD_OFFSET(TRANSPORT_ADDRESS,Address))))
  286. {
  287. ASSERT(0);
  288. return (STATUS_INVALID_ADDRESS_COMPONENT);
  289. }
  290. pNetbiosExAddress = (PTDI_ADDRESS_NETBIOS_EX)pAddress->Address;
  291. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  292. KdPrint(("Nbt.NTOpenAddr: ...Opening NETBIOS_EX Address:Endpoint=<%16.16s:%x>:<%16.16s:%x>\n",
  293. pNetbiosExAddress->NetbiosAddress.NetbiosName,
  294. pNetbiosExAddress->NetbiosAddress.NetbiosName[15],
  295. pNetbiosExAddress->EndpointName, pNetbiosExAddress->EndpointName[15]));
  296. if (pAddress->AddressLength != 0)
  297. {
  298. status = STATUS_SUCCESS;
  299. break;
  300. }
  301. ASSERT(0); // AddressLength should not be 0!
  302. }
  303. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  304. KdPrint(("Nbt.NTOpenAddr[2]: ...Rejecting Open Address request for AddressType=<%d>\n",
  305. pAddress->AddressType));
  306. //
  307. // Verify that we have enough Buffer space to read in next address
  308. // Bug#: 120683
  309. //
  310. MinBufferLength += pAddress->AddressLength + FIELD_OFFSET(TA_ADDRESS,Address);
  311. if (BufferLength < (MinBufferLength +
  312. (sizeof(TA_NETBIOS_ADDRESS)-FIELD_OFFSET(TRANSPORT_ADDRESS,Address))))
  313. {
  314. return (status);
  315. }
  316. //
  317. // Set pAddress to point to the next address
  318. //
  319. pAddress = (TA_ADDRESS *) ((PUCHAR)pAddress
  320. + FIELD_OFFSET(TA_ADDRESS,Address)
  321. + pAddress->AddressLength);
  322. }
  323. if (status == STATUS_SUCCESS) // We found a valid address type!
  324. {
  325. // call the non-NT specific function to open an address
  326. status = NbtOpenAddress(&Request,
  327. pAddress,
  328. pDeviceContext->IpAddress,
  329. &pSecurityDesc,
  330. pDeviceContext,
  331. (PVOID)pIrp);
  332. if (status != STATUS_SUCCESS) {
  333. NbtTrace(NBT_TRACE_NAMESRV, ("NbtOpenAddress returns %!status!", status));
  334. }
  335. }
  336. return(status);
  337. }
  338. //----------------------------------------------------------------------------
  339. NTSTATUS
  340. NTCloseAddress(
  341. IN tDEVICECONTEXT *pDeviceContext,
  342. IN PIRP pIrp)
  343. /*++
  344. Routine Description:
  345. This Routine handles converting a Close Address Request from an IRP to
  346. a procedure call so that NbtCloseAddress can be called in an OS independent
  347. manner.
  348. Arguments:
  349. pIrp - a ptr to an IRP
  350. Return Value:
  351. NTSTATUS - status of the request
  352. --*/
  353. {
  354. TDI_REQUEST Request;
  355. TDI_REQUEST_STATUS RequestStatus;
  356. PIO_STACK_LOCATION pIrpSp;
  357. NTSTATUS status;
  358. tCLIENTELE *pClientEle;
  359. CTEPagedCode();
  360. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  361. pClientEle = Request.Handle.ConnectionContext = pIrpSp->FileObject->FsContext;
  362. if (!NBT_VERIFY_HANDLE2 (pClientEle, NBT_VERIFY_CLIENT, NBT_VERIFY_CLIENT_DOWN))
  363. {
  364. ASSERTMSG ("Nbt.NTCloseAddress: ERROR - Invalid Address Handle\n", 0);
  365. return (STATUS_INVALID_HANDLE);
  366. }
  367. status = NbtCloseAddress (&Request, &RequestStatus, pDeviceContext, (PVOID)pIrp);
  368. NbtTrace(NBT_TRACE_NAMESRV, ("NbtCloseAddress returns %!status! for ClientEle=%p", status, pClientEle));
  369. return(status);
  370. }
  371. //----------------------------------------------------------------------------
  372. NTSTATUS
  373. NTOpenConnection(
  374. IN tDEVICECONTEXT *pDeviceContext,
  375. IN PIRP pIrp,
  376. IN PFILE_FULL_EA_INFORMATION ea)
  377. /*++
  378. Routine Description:
  379. This Routine handles converting an Open Connection Request from an IRP to
  380. a procedure call so that NbtOpenConnection can be called in an OS independent
  381. manner. The connection must be associated with an address before it
  382. can be used, except for in inbound call where the client returns the
  383. connection ID in the accept.
  384. Arguments:
  385. pIrp - a ptr to an IRP
  386. Return Value:
  387. NTSTATUS - status of the request
  388. --*/
  389. {
  390. TDI_REQUEST Request;
  391. PIO_STACK_LOCATION pIrpSp;
  392. CONNECTION_CONTEXT ConnectionContext;
  393. NTSTATUS status;
  394. PFILE_OBJECT pFileObject;
  395. ULONG BufferLength;
  396. tCONNECTELE *pConnEle;
  397. CTEPagedCode();
  398. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  399. // make up the Request data structure from the IRP info
  400. Request.Handle.ConnectionContext = NULL;
  401. //
  402. // Verify Minimum Buffer length!
  403. // Bug#: 120682
  404. //
  405. BufferLength = ea->EaValueLength;
  406. if (BufferLength < sizeof(CONNECTION_CONTEXT))
  407. {
  408. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  409. KdPrint(("Nbt.NTOpenConnection: ERROR -- Open Connection request -- (BufferLength=%d < Min=%d)\n",
  410. BufferLength, sizeof(CONNECTION_CONTEXT)));
  411. ASSERT(0);
  412. return (STATUS_INVALID_ADDRESS_COMPONENT);
  413. }
  414. // the connection context value is stored in the string just after the
  415. // name "connectionContext", and it is most likely unaligned, so just
  416. // copy it out.( 4 bytes of copying ).
  417. CTEMemCopy(&ConnectionContext,
  418. (CONNECTION_CONTEXT)&ea->EaName[ea->EaNameLength+1],
  419. sizeof(CONNECTION_CONTEXT));
  420. // call the non-NT specific function to open an address
  421. status = NbtOpenConnection (&Request, ConnectionContext, pDeviceContext);
  422. pFileObject = pIrpSp->FileObject;
  423. if (!NT_SUCCESS(status))
  424. {
  425. pFileObject->FsContext = NULL;
  426. NbtTrace(NBT_TRACE_OUTBOUND, ("NbtOpenConnection returns %!status!", status));
  427. }
  428. else if (Request.Handle.ConnectionContext)
  429. {
  430. // fill the IRP with successful completion information so we can
  431. // find the connection object given the fileObject later.
  432. pConnEle = pFileObject->FsContext = Request.Handle.ConnectionContext;
  433. if (!NBT_VERIFY_HANDLE (pConnEle, NBT_VERIFY_CONNECTION))
  434. {
  435. ASSERTMSG ("Nbt.NTOpenConnection: ERROR - Invalid Connection Handle\n", 0);
  436. return (STATUS_UNSUCCESSFUL);
  437. }
  438. pFileObject->FsContext2 = (PVOID)(NBT_CONNECTION_TYPE);
  439. pConnEle->pClientFileObject = pFileObject;
  440. NbtTrace(NBT_TRACE_OUTBOUND, ("New connection %p", pConnEle));
  441. status = STATUS_SUCCESS;
  442. }
  443. return(status);
  444. }
  445. //----------------------------------------------------------------------------
  446. NTSTATUS
  447. NTAssocAddress(
  448. IN tDEVICECONTEXT *pDeviceContext,
  449. IN PIRP pIrp)
  450. /*++
  451. Routine Description:
  452. This Routine handles converting an Associate Address Request from an IRP to
  453. a procedure call so that NbtAssociateAddress can be called in an OS independent
  454. manner.
  455. Arguments:
  456. pIrp - a ptr to an IRP
  457. Return Value:
  458. NTSTATUS - status of the request
  459. --*/
  460. {
  461. TDI_REQUEST Request;
  462. PIO_STACK_LOCATION pIrpSp;
  463. PFILE_OBJECT fileObject;
  464. PTDI_REQUEST_KERNEL_ASSOCIATE parameters; // holds address handle
  465. NTSTATUS status;
  466. tCONNECTELE *pConnEle;
  467. tCLIENTELE *pClientEle;
  468. CTEPagedCode();
  469. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  470. pConnEle = Request.Handle.ConnectionContext = pIrpSp->FileObject->FsContext;
  471. if (!NBT_VERIFY_HANDLE (pConnEle, NBT_VERIFY_CONNECTION))
  472. {
  473. ASSERTMSG ("Nbt.NTAssocAddress: ERROR - Invalid Connection Handle\n", 0);
  474. return (STATUS_INVALID_HANDLE);
  475. }
  476. // the address handle is buried in the Irp...
  477. parameters = (PTDI_REQUEST_KERNEL_ASSOCIATE)&pIrpSp->Parameters;
  478. // now get a pointer to the file object, which points to the address
  479. // element by calling a kernel routine to convert this filehandle into
  480. // a file pointer.
  481. status = ObReferenceObjectByHandle (parameters->AddressHandle,
  482. FILE_READ_DATA,
  483. *IoFileObjectType,
  484. pIrp->RequestorMode,
  485. (PVOID *)&fileObject,
  486. NULL);
  487. IF_DBG(NBT_DEBUG_HANDLES)
  488. KdPrint (("\t ++<%x>====><%x>\tNTAssocAddress->ObReferenceObject, Status = <%x>\n", parameters->AddressHandle, fileObject, status));
  489. if ((NT_SUCCESS(status)) &&
  490. (fileObject->DeviceObject->DriverObject == NbtConfig.DriverObject) && // Bug# 202349
  491. NBT_VERIFY_HANDLE(((tDEVICECONTEXT*)fileObject->DeviceObject), NBT_VERIFY_DEVCONTEXT) && // Bug# 202349
  492. (PtrToUlong(fileObject->FsContext2) == TDI_TRANSPORT_ADDRESS_FILE))
  493. {
  494. pClientEle = fileObject->FsContext;
  495. if (NBT_VERIFY_HANDLE (pClientEle, NBT_VERIFY_CLIENT))
  496. {
  497. // call the non-NT specific function to associate the address with
  498. // the connection
  499. status = NbtAssociateAddress (&Request, pClientEle, (PVOID)pIrp);
  500. NbtTrace(NBT_TRACE_OUTBOUND, ("NbtAssociateAddress returns %!status!", status));
  501. }
  502. else
  503. {
  504. ASSERTMSG ("Nbt.NTAssocAddress: ERROR - Invalid Address Handle\n", 0);
  505. status = STATUS_INVALID_HANDLE;
  506. }
  507. // we are done with the file object, so release the reference
  508. ObDereferenceObject((PVOID)fileObject);
  509. return(status);
  510. }
  511. else
  512. {
  513. return(STATUS_INVALID_HANDLE);
  514. }
  515. }
  516. //----------------------------------------------------------------------------
  517. NTSTATUS
  518. NTCloseConnection(
  519. IN tDEVICECONTEXT *pDeviceContext,
  520. IN PIRP pIrp)
  521. /*++
  522. Routine Description:
  523. This Routine handles converting a Close Connection Request from an IRP to
  524. a procedure call so that NbtCloseConnection can be called in an OS independent
  525. manner.
  526. Arguments:
  527. pIrp - a ptr to an IRP
  528. Return Value:
  529. NTSTATUS - status of the request
  530. --*/
  531. {
  532. TDI_REQUEST Request;
  533. TDI_REQUEST_STATUS RequestStatus;
  534. PIO_STACK_LOCATION pIrpSp;
  535. NTSTATUS status;
  536. tCONNECTELE *pConnEle;
  537. CTEPagedCode();
  538. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  539. pConnEle = Request.Handle.ConnectionContext = pIrpSp->FileObject->FsContext;
  540. if (!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  541. {
  542. ASSERTMSG ("Nbt.NTCloseConnection: ERROR - Invalid Connection Handle\n", 0);
  543. return (STATUS_INVALID_HANDLE);
  544. }
  545. status = NbtCloseConnection(
  546. &Request,
  547. &RequestStatus,
  548. pDeviceContext,
  549. (PVOID)pIrp);
  550. NbtTrace(NBT_TRACE_OUTBOUND, ("Close connection %p returns %!status!", pConnEle, status));
  551. return(status);
  552. }
  553. //----------------------------------------------------------------------------
  554. VOID
  555. NTSetFileObjectContexts(
  556. IN PIRP pIrp,
  557. IN PVOID FsContext,
  558. IN PVOID FsContext2)
  559. /*++
  560. Routine Description:
  561. This Routine handles fills in two context values in the Irp stack location,
  562. that has to be done in an OS-dependent manner. This routine is called
  563. from NbtOpenAddress() when a name is being registered on the network( i.e.
  564. as a result of OpenAddress).
  565. Arguments:
  566. pIrp - a ptr to an IRP
  567. Return Value:
  568. NTSTATUS - status of the request
  569. --*/
  570. {
  571. PIO_STACK_LOCATION pIrpSp;
  572. PFILE_OBJECT pFileObject;
  573. //
  574. // fill the IRP with context information so we can
  575. // find the address object given the fileObject later.
  576. //
  577. // This must be done here, rather than after the call to NbtOpenAddress
  578. // because that call can complete the Irp before it returns. Soooo,
  579. // in the complete routine for the Irp, if the completion code is not
  580. // good, it Nulls these two context values.
  581. //
  582. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  583. pFileObject = pIrpSp->FileObject;
  584. pFileObject->FsContext = FsContext;
  585. pFileObject->FsContext2 =FsContext2;
  586. }
  587. //----------------------------------------------------------------------------
  588. VOID
  589. NTClearFileObjectContext(
  590. IN PIRP pIrp
  591. )
  592. /*++
  593. Routine Description:
  594. This Routine clears the context value in the file object when an address
  595. object is closed.
  596. Arguments:
  597. pIrp - a ptr to an IRP
  598. Return Value:
  599. none
  600. --*/
  601. {
  602. PIO_STACK_LOCATION pIrpSp;
  603. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  604. CHECK_PTR(pIrpSp->FileObject);
  605. pIrpSp->FileObject->FsContext = NULL;
  606. }
  607. //----------------------------------------------------------------------------
  608. NTSTATUS
  609. NTSetSharedAccess(
  610. IN tDEVICECONTEXT *pDeviceContext,
  611. IN PIRP pIrp,
  612. IN tADDRESSELE *pAddress)
  613. /*++
  614. Routine Description:
  615. This Routine handles setting the shared access on the file object.
  616. Arguments:
  617. pIrp - a ptr to an IRP
  618. Return Value:
  619. NTSTATUS - status of the request
  620. --*/
  621. {
  622. PACCESS_STATE AccessState;
  623. ULONG DesiredAccess;
  624. PIO_STACK_LOCATION pIrpSp;
  625. NTSTATUS status;
  626. static GENERIC_MAPPING AddressGenericMapping = { READ_CONTROL, READ_CONTROL, READ_CONTROL, READ_CONTROL };
  627. CTEPagedCode();
  628. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  629. if ((pIrpSp->Parameters.Create.ShareAccess & FILE_SHARE_READ) ||
  630. (pIrpSp->Parameters.Create.ShareAccess & FILE_SHARE_WRITE))
  631. {
  632. DesiredAccess = (ULONG)FILE_SHARE_READ;
  633. }
  634. else
  635. {
  636. DesiredAccess = (ULONG)0;
  637. }
  638. IoSetShareAccess (FILE_READ_DATA, DesiredAccess, pIrpSp->FileObject, &pAddress->ShareAccess);
  639. // assign the security descriptor ( need to to do this with the spinlock
  640. // released because the descriptor is not mapped. Assign and CheckAccess
  641. // are synchronized using a Resource.
  642. AccessState = pIrpSp->Parameters.Create.SecurityContext->AccessState;
  643. status = SeAssignSecurity (NULL, // Parent Descriptor
  644. AccessState->SecurityDescriptor,
  645. &pAddress->SecurityDescriptor,
  646. FALSE, // is a directory
  647. &AccessState->SubjectSecurityContext,
  648. &AddressGenericMapping,
  649. NonPagedPool);
  650. if (!NT_SUCCESS(status))
  651. {
  652. IoRemoveShareAccess (pIrpSp->FileObject, &pAddress->ShareAccess);
  653. }
  654. return status;
  655. }
  656. //----------------------------------------------------------------------------
  657. NTSTATUS
  658. NTCheckSharedAccess(
  659. IN tDEVICECONTEXT *pDeviceContext,
  660. IN PIRP pIrp,
  661. IN tADDRESSELE *pAddress)
  662. /*++
  663. Routine Description:
  664. This Routine handles setting the shared access on the file object.
  665. Arguments:
  666. pIrp - a ptr to an IRP
  667. Return Value:
  668. NTSTATUS - status of the request
  669. --*/
  670. {
  671. PACCESS_STATE AccessState;
  672. ACCESS_MASK GrantedAccess;
  673. BOOLEAN AccessAllowed;
  674. ULONG DesiredAccess;
  675. PIO_STACK_LOCATION pIrpSp;
  676. BOOLEAN duplicate=FALSE;
  677. NTSTATUS status;
  678. ULONG DesiredShareAccess;
  679. static GENERIC_MAPPING AddressGenericMapping =
  680. { READ_CONTROL, READ_CONTROL, READ_CONTROL, READ_CONTROL };
  681. CTEPagedCode();
  682. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  683. if ((pIrpSp->Parameters.Create.ShareAccess & FILE_SHARE_READ) ||
  684. (pIrpSp->Parameters.Create.ShareAccess & FILE_SHARE_WRITE))
  685. DesiredAccess = (ULONG)FILE_SHARE_READ;
  686. else
  687. DesiredAccess = (ULONG)0;
  688. //
  689. // The address already exists. Check the ACL and see if we
  690. // can access it. If so, simply use this address as our address.
  691. //
  692. AccessState = pIrpSp->Parameters.Create.SecurityContext->AccessState;
  693. status = STATUS_SUCCESS;
  694. // *TODO* check that this routine is doing the right thing...
  695. //
  696. AccessAllowed = SeAccessCheck(
  697. pAddress->SecurityDescriptor,
  698. &AccessState->SubjectSecurityContext,
  699. FALSE, // tokens locked
  700. pIrpSp->Parameters.Create.SecurityContext->DesiredAccess,
  701. (ACCESS_MASK)0, // previously granted
  702. NULL, // privileges
  703. &AddressGenericMapping,
  704. pIrp->RequestorMode,
  705. &GrantedAccess,
  706. &status);
  707. // use the status from the IoCheckShareAccess as the return access
  708. // event if SeAccessCheck fails....
  709. //
  710. // Hmmm .... Compare DesiredAccess to GrantedAccess?
  711. //
  712. //
  713. // Now check that we can obtain the desired share
  714. // access. We use read access to control all access.
  715. //
  716. DesiredShareAccess = (ULONG)
  717. (((pIrpSp->Parameters.Create.ShareAccess & FILE_SHARE_READ) ||
  718. (pIrpSp->Parameters.Create.ShareAccess & FILE_SHARE_WRITE)) ?
  719. FILE_SHARE_READ : 0);
  720. //ACQUIRE_SPIN_LOCK (&pDeviceContext->SpinLock, &oldirql);
  721. status = IoCheckShareAccess(
  722. FILE_READ_DATA,
  723. DesiredAccess,
  724. pIrpSp->FileObject,
  725. &pAddress->ShareAccess,
  726. TRUE);
  727. return(status);
  728. }
  729. //----------------------------------------------------------------------------
  730. NTSTATUS
  731. NTCleanUpAddress(
  732. IN tDEVICECONTEXT *pDeviceContext,
  733. IN PIRP pIrp)
  734. /*++
  735. Routine Description:
  736. This Routine handles the first stage of releasing an address object.
  737. Arguments:
  738. pIrp - a ptr to an IRP
  739. Return Value:
  740. NTSTATUS - status of the request
  741. --*/
  742. {
  743. NTSTATUS status;
  744. tCLIENTELE *pClientEle;
  745. PIO_STACK_LOCATION pIrpSp;
  746. CTEPagedCode();
  747. IF_DBG(NBT_DEBUG_NAMESRV)
  748. KdPrint(("Nbt.NTCleanUpAddress: Cleanup Address Hit ***\n"));
  749. //
  750. // Disconnect any active connections, and for each connection that is not
  751. // in use, remove one from the free list to the transport below.
  752. //
  753. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  754. pClientEle = (tCLIENTELE *) pIrpSp->FileObject->FsContext;
  755. if (!NBT_VERIFY_HANDLE (pClientEle, NBT_VERIFY_CLIENT))
  756. {
  757. ASSERTMSG ("Nbt.NTCleanUpAddress: ERROR - Invalid Address Handle\n", 0);
  758. return (STATUS_INVALID_HANDLE);
  759. }
  760. CTEVerifyHandle(pClientEle,NBT_VERIFY_CLIENT,tCLIENTELE,&status);
  761. status = NbtCleanUpAddress(pClientEle,pDeviceContext);
  762. NbtTrace(NBT_TRACE_NAMESRV, ("Cleanup address %p returns %!status!", pClientEle, status));
  763. return(status);
  764. }
  765. //----------------------------------------------------------------------------
  766. NTSTATUS
  767. NTCleanUpConnection(
  768. IN tDEVICECONTEXT *pDeviceContext,
  769. IN PIRP pIrp)
  770. /*++
  771. Routine Description:
  772. This Routine handles running down a connection in preparation for a close
  773. that will come in next. NtClose hits this entry first, and then it hits
  774. the NTCloseConnection next. If the connection was outbound, then the
  775. address object must be closed as well as the connection. This routine
  776. mainly deals with the pLowerconn connection to the transport whereas
  777. NbtCloseConnection deals with closing pConnEle, the connection to the client.
  778. Arguments:
  779. pIrp - a ptr to an IRP
  780. Return Value:
  781. NTSTATUS - status of the request
  782. --*/
  783. {
  784. NTSTATUS status;
  785. PIO_STACK_LOCATION pIrpSp;
  786. tCONNECTELE *pConnEle;
  787. CTEPagedCode();
  788. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  789. pConnEle = (tCONNECTELE *) pIrpSp->FileObject->FsContext;
  790. if (!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  791. {
  792. ASSERTMSG ("Nbt.NTCleanUpConnection: ERROR - Invalid Connection Handle\n", 0);
  793. return (STATUS_INVALID_HANDLE);
  794. }
  795. //CTEVerifyHandle(pConnEle,NBT_VERIFY_CONNECTION,tCONNECTELE,&status);
  796. IF_DBG(NBT_DEBUG_NAMESRV)
  797. KdPrint(("Nbt.NTCleanUpConnection: Cleanup Connection Hit state= %X\n",pConnEle->state));
  798. pConnEle->ConnectionCleanedUp = TRUE;
  799. status = NbtCleanUpConnection(pConnEle,pDeviceContext);
  800. NbtTrace(NBT_TRACE_NAMESRV, ("Cleanup connection %p returns %!status!", pConnEle, status));
  801. return(status);
  802. }
  803. //----------------------------------------------------------------------------
  804. NTSTATUS
  805. NTAccept(
  806. IN tDEVICECONTEXT *pDeviceContext,
  807. IN PIRP pIrp)
  808. /*++
  809. Routine Description:
  810. This Routine handles passing an accept for an inbound connect indication to
  811. the OS independent code.
  812. Arguments:
  813. pIrp - a ptr to an IRP
  814. Return Value:
  815. NTSTATUS - status of the request
  816. --*/
  817. {
  818. NTSTATUS status;
  819. TDI_REQUEST TdiRequest;
  820. PIO_STACK_LOCATION pIrpSp;
  821. PTDI_REQUEST_KERNEL_ACCEPT pRequest;
  822. tCONNECTELE *pConnEle;
  823. IF_DBG(NBT_DEBUG_NAMESRV)
  824. KdPrint(("Nbt.NTAccept: ** Got an Accept from the Client **\n"));
  825. // pull the junk out of the Irp and call the non-OS specific routine.
  826. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  827. // the Parameters value points to a Request structure...
  828. pRequest = (PTDI_REQUEST_KERNEL_ACCEPT)&pIrpSp->Parameters;
  829. // the pConnEle ptr was stored in the FsContext value when the connection
  830. // was initially created.
  831. pConnEle = TdiRequest.Handle.ConnectionContext = pIrpSp->FileObject->FsContext;
  832. if (!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  833. {
  834. ASSERTMSG ("Nbt.NTAccept: ERROR - Invalid Connection Handle\n", 0);
  835. return (STATUS_INVALID_HANDLE);
  836. }
  837. status = NbtAccept(
  838. &TdiRequest,
  839. pRequest->RequestConnectionInformation,
  840. pRequest->ReturnConnectionInformation,
  841. pIrp);
  842. return(status);
  843. }
  844. //----------------------------------------------------------------------------
  845. NTSTATUS
  846. NTDisAssociateAddress(
  847. IN tDEVICECONTEXT *pDeviceContext,
  848. IN PIRP pIrp)
  849. /*++
  850. Routine Description:
  851. This Routine handles
  852. Arguments:
  853. pIrp - a ptr to an IRP
  854. Return Value:
  855. NTSTATUS - status of the request
  856. --*/
  857. {
  858. NTSTATUS status;
  859. TDI_REQUEST TdiRequest;
  860. PIO_STACK_LOCATION pIrpSp;
  861. PTDI_REQUEST_KERNEL_ACCEPT pRequest;
  862. tCONNECTELE *pConnEle;
  863. CTEPagedCode();
  864. // pull the junk out of the Irp and call the non-OS specific routine.
  865. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  866. // the Parameters value points to a Request structure...
  867. pRequest = (PTDI_REQUEST_KERNEL_ACCEPT)&pIrpSp->Parameters;
  868. // the pConnEle ptr was stored in the FsContext value when the connection
  869. // was initially created.
  870. pConnEle = TdiRequest.Handle.ConnectionContext = pIrpSp->FileObject->FsContext;
  871. if (!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  872. {
  873. ASSERTMSG ("Nbt.NTCloseAddress: ERROR - Invalid Address Handle\n", 0);
  874. return (STATUS_INVALID_HANDLE);
  875. }
  876. status = NbtDisassociateAddress(&TdiRequest);
  877. return(status);
  878. }
  879. LONG
  880. NextTransportAddress(
  881. IN PNBT_DELAYED_CONNECT_CONTEXT pDelConnCtx
  882. )
  883. /*++
  884. Move the pointer to the next address.
  885. --*/
  886. {
  887. pDelConnCtx->RemainingAddressLength -= (pDelConnCtx->TaAddressLength + FIELD_OFFSET(TRANSPORT_ADDRESS,Address));
  888. pDelConnCtx->pTaAddress += pDelConnCtx->TaAddressLength + FIELD_OFFSET(TRANSPORT_ADDRESS,Address);
  889. RtlCopyMemory(&pDelConnCtx->TaAddressLength,
  890. (pDelConnCtx->pTaAddress+FIELD_OFFSET(TA_ADDRESS,AddressLength)), sizeof(USHORT));
  891. pDelConnCtx->CurrIndex++;
  892. /*
  893. * make sure we don't overrun the buffer
  894. */
  895. if(pDelConnCtx->RemainingAddressLength < (pDelConnCtx->TaAddressLength + FIELD_OFFSET(TRANSPORT_ADDRESS,Address))) {
  896. KdPrint(("netbt!NextTransportAddress: insufficient TaAddress buffer size\n"));
  897. pDelConnCtx->CurrIndex = pDelConnCtx->NumberOfAddresses;
  898. }
  899. return pDelConnCtx->CurrIndex;
  900. }
  901. NTSTATUS
  902. InitDelayedNbtProcessConnect(
  903. IN PNBT_DELAYED_CONNECT_CONTEXT pDelConnCtx
  904. )
  905. /*++
  906. Reset the NBT_DELAYED_CONNECT_CONTEXT
  907. Find the first readable unicode address and writable buffer. In compound address case, NetBT will first try
  908. to establish the connection using the first readable unicode address. If this fails, it will attempt to use
  909. OEM address, ie. only one readable unicode address is effective. If DNS name resolution is used, NetBT will
  910. return the result in the first writable buffer and update the NameBufferType to NBT_WRITTEN.
  911. --*/
  912. {
  913. PTDI_REQUEST_KERNEL pRequestKernel;
  914. PIO_STACK_LOCATION pIrpSp;
  915. PTRANSPORT_ADDRESS pRemoteAddress;
  916. PUCHAR pTaAddress;
  917. enum eNameBufferType NameBufferType, UnicodeAddressNameBufferType;
  918. NTSTATUS status;
  919. CTEPagedCode();
  920. pIrpSp = IoGetCurrentIrpStackLocation(pDelConnCtx->pClientIrp);
  921. pRequestKernel = (PTDI_REQUEST_KERNEL) &pIrpSp->Parameters;
  922. pRemoteAddress = pRequestKernel->RequestConnectionInformation->RemoteAddress;
  923. pDelConnCtx->NumberOfAddresses = pRemoteAddress->TAAddressCount;
  924. pDelConnCtx->RemainingAddressLength = pRequestKernel->RequestConnectionInformation->RemoteAddressLength;
  925. pDelConnCtx->pTaAddress = (PCHAR)&pRemoteAddress->Address[0];
  926. RtlCopyMemory(&pDelConnCtx->TaAddressLength,
  927. (pDelConnCtx->pTaAddress+FIELD_OFFSET(TA_ADDRESS,AddressLength)), sizeof(USHORT));
  928. pDelConnCtx->CurrIndex = 0;
  929. /*
  930. * Find the first writable buffer and readable unicode address
  931. */
  932. pDelConnCtx->pReturnBuffer = NULL;
  933. pDelConnCtx->pUnicodeAddress = NULL;
  934. for (pDelConnCtx->CurrIndex = 0; pDelConnCtx->CurrIndex < pDelConnCtx->NumberOfAddresses;
  935. NextTransportAddress(pDelConnCtx)) {
  936. USHORT TaAddressType;
  937. RtlCopyMemory(&TaAddressType, (pDelConnCtx->pTaAddress+FIELD_OFFSET(TA_ADDRESS,AddressType)), sizeof(USHORT));
  938. if (TaAddressType != TDI_ADDRESS_TYPE_NETBIOS_UNICODE_EX) {
  939. continue;
  940. }
  941. RtlCopyMemory(&NameBufferType,
  942. pDelConnCtx->pTaAddress +
  943. FIELD_OFFSET(TA_ADDRESS,Address)+
  944. FIELD_OFFSET(TDI_ADDRESS_NETBIOS_UNICODE_EX,NameBufferType),
  945. sizeof(NameBufferType));
  946. if (NameBufferType != NBT_READONLY && NameBufferType != NBT_WRITEONLY &&
  947. NameBufferType != NBT_READWRITE && NameBufferType != NBT_WRITTEN) {
  948. return STATUS_INVALID_ADDRESS;
  949. }
  950. if (NameBufferType == NBT_READONLY || NameBufferType == NBT_READWRITE) {
  951. if (pDelConnCtx->pUnicodeAddress == NULL) {
  952. pDelConnCtx->pUnicodeAddress = (TDI_ADDRESS_NETBIOS_UNICODE_EX*)
  953. (pDelConnCtx->pTaAddress + FIELD_OFFSET(TA_ADDRESS,Address));
  954. UnicodeAddressNameBufferType = NameBufferType;
  955. }
  956. }
  957. if (NameBufferType == NBT_WRITEONLY) {
  958. pDelConnCtx->pReturnBuffer = (TDI_ADDRESS_NETBIOS_UNICODE_EX*)
  959. (pDelConnCtx->pTaAddress + FIELD_OFFSET(TA_ADDRESS,Address));
  960. break;
  961. }
  962. if (NameBufferType == NBT_READWRITE) {
  963. pDelConnCtx->pReturnBuffer = (TDI_ADDRESS_NETBIOS_UNICODE_EX*)
  964. (pDelConnCtx->pTaAddress + FIELD_OFFSET(TA_ADDRESS,Address));
  965. /*
  966. * Only when no WRITEONLY buffer is presented, can we use a READWRITE buffer. So continue searching.
  967. */
  968. }
  969. }
  970. pDelConnCtx->NumberOfAddresses = pRemoteAddress->TAAddressCount;
  971. pDelConnCtx->RemainingAddressLength = pRequestKernel->RequestConnectionInformation->RemoteAddressLength;
  972. pDelConnCtx->pTaAddress = (PCHAR)&pRemoteAddress->Address[0];
  973. RtlCopyMemory(&pDelConnCtx->TaAddressLength,
  974. (pDelConnCtx->pTaAddress+FIELD_OFFSET(TA_ADDRESS,AddressLength)), sizeof(USHORT));
  975. pDelConnCtx->CurrIndex = 0;
  976. /*
  977. * Setup the first local transport address
  978. */
  979. if (pDelConnCtx->pUnicodeAddress != NULL) {
  980. pTaAddress = ((PUCHAR)pDelConnCtx->pUnicodeAddress - FIELD_OFFSET(TA_ADDRESS,Address));
  981. } else {
  982. pTaAddress = pDelConnCtx->pTaAddress;
  983. }
  984. status = NewInternalAddressFromTransportAddress(
  985. (PTRANSPORT_ADDRESS) (pTaAddress-FIELD_OFFSET(TRANSPORT_ADDRESS,Address)),
  986. pDelConnCtx->RemainingAddressLength, &pDelConnCtx->pTransportAddress);
  987. if (status != STATUS_SUCCESS) {
  988. ASSERT(pDelConnCtx->pTransportAddress == NULL);
  989. return status;
  990. }
  991. if (pDelConnCtx->pUnicodeAddress == NULL || UnicodeAddressNameBufferType != NBT_READWRITE) {
  992. pDelConnCtx->pTransportAddress->Address[0].Address[0].pNetbiosUnicodeEX = pDelConnCtx->pReturnBuffer;
  993. }
  994. ASSERT(pDelConnCtx->pTransportAddress);
  995. return STATUS_SUCCESS;
  996. }
  997. NTSTATUS
  998. NextDelayedNbtProcessConnect(
  999. IN PNBT_DELAYED_CONNECT_CONTEXT pDelConnCtx
  1000. )
  1001. /*++
  1002. Move the pointer to the next address.
  1003. --*/
  1004. {
  1005. USHORT TaAddressType;
  1006. enum eNameBufferType NameBufferType;
  1007. PTA_NETBT_INTERNAL_ADDRESS pTransportAddress = NULL;
  1008. TDI_ADDRESS_NETBT_INTERNAL *pAddr;
  1009. PIO_STACK_LOCATION pIrpSp;
  1010. tCONNECTELE *pConnEle;
  1011. NTSTATUS status;
  1012. pIrpSp = IoGetCurrentIrpStackLocation(pDelConnCtx->pClientIrp);
  1013. pConnEle = pIrpSp->FileObject->FsContext;
  1014. ASSERT (pConnEle->pIrp == NULL);
  1015. ASSERT(pDelConnCtx->pTransportAddress);
  1016. status = STATUS_SUCCESS;
  1017. while(1) {
  1018. /*
  1019. * Free memory allocated in previous loop
  1020. */
  1021. if (pTransportAddress) {
  1022. DeleteInternalAddress(pTransportAddress);
  1023. pTransportAddress = NULL;
  1024. }
  1025. if (pDelConnCtx->pUnicodeAddress == NULL) {
  1026. NextTransportAddress(pDelConnCtx);
  1027. } else {
  1028. pDelConnCtx->pUnicodeAddress = NULL;
  1029. }
  1030. if (pDelConnCtx->CurrIndex >= pDelConnCtx->NumberOfAddresses) {
  1031. break;
  1032. }
  1033. /*
  1034. * Skip UNICODE address.
  1035. * UNICODE address is always done first, ie. just after InitDelayedNbtProcessConnect gets called.
  1036. */
  1037. RtlCopyMemory(&TaAddressType, (pDelConnCtx->pTaAddress+FIELD_OFFSET(TA_ADDRESS,AddressType)), sizeof(USHORT));
  1038. if (TaAddressType != TDI_ADDRESS_TYPE_NETBIOS && TaAddressType != TDI_ADDRESS_TYPE_NETBIOS_EX) {
  1039. continue;
  1040. }
  1041. /*
  1042. * Since we only do OEM address, we can safely call NewInternalAddressFromTransportAddress (this guy will
  1043. * call Rtl* to convert UNICODE to OEM in UNICODE address case so that we may hit bug check.)
  1044. */
  1045. status = NewInternalAddressFromTransportAddress(
  1046. (PTRANSPORT_ADDRESS) (pDelConnCtx->pTaAddress-FIELD_OFFSET(TRANSPORT_ADDRESS,Address)),
  1047. pDelConnCtx->RemainingAddressLength, &pTransportAddress);
  1048. if (status != STATUS_SUCCESS) {
  1049. ASSERT(pTransportAddress == NULL);
  1050. continue;
  1051. }
  1052. ASSERT(pTransportAddress);
  1053. pAddr = pTransportAddress->Address[0].Address;
  1054. /*
  1055. * Always attach a writable buffer in OEM address case
  1056. */
  1057. pAddr->pNetbiosUnicodeEX = pDelConnCtx->pReturnBuffer;
  1058. /*
  1059. * Skip any address which is same as previous one.
  1060. * Since the previous one fails, there is no point to use it again.
  1061. */
  1062. if (IsDeviceNetbiosless(pDelConnCtx->pDeviceContext) ||
  1063. (pDelConnCtx->pLocalIrp->IoStatus.Status == STATUS_HOST_UNREACHABLE)) {
  1064. OEM_STRING RemoteName, PreviouseRemoteName;
  1065. CTEMemCopy (&RemoteName, &pAddr->OEMRemoteName, sizeof(OEM_STRING));
  1066. CTEMemCopy (&PreviouseRemoteName,
  1067. &pDelConnCtx->pTransportAddress->Address[0].Address[0].OEMRemoteName, sizeof(OEM_STRING));
  1068. if ((RemoteName.Length) && (RemoteName.Length == PreviouseRemoteName.Length) &&
  1069. (CTEMemEqu (RemoteName.Buffer, PreviouseRemoteName.Buffer, RemoteName.Length))) {
  1070. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  1071. KdPrint(("Nbt.DelayedNbtProcessConnect: Irp=<%x>, Names match!<%16.16s:%x>, Types=<%x:%x>\n",
  1072. pDelConnCtx->pClientIrp, pAddr->OEMRemoteName.Buffer, pAddr->OEMRemoteName.Buffer[15],
  1073. pAddr->AddressType, pDelConnCtx->pTransportAddress->Address[0].Address[0].AddressType));
  1074. continue;
  1075. }
  1076. }
  1077. if (pConnEle->RemoteNameDoesNotExistInDNS) {
  1078. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  1079. KdPrint(("netbt!DelayedNbtProcessConnect: Skipping address type %lx length %lx\n"
  1080. "\t\tfor nonexistent name, pIrp %lx, pLocalIrp %lx\n",
  1081. TaAddressType, pDelConnCtx->TaAddressLength,
  1082. pDelConnCtx->pClientIrp, pDelConnCtx->pLocalIrp));
  1083. // If the address type is such that we rely on DNS name resolution and
  1084. // if a prior attempt failed, there is no point in reissuing the request.
  1085. // We can fail them without having to go on the NET.
  1086. if (TaAddressType == TDI_ADDRESS_TYPE_NETBIOS_EX) {
  1087. status = STATUS_BAD_NETWORK_PATH;
  1088. continue;
  1089. } else if (pDelConnCtx->TaAddressLength != TDI_ADDRESS_LENGTH_NETBIOS) {
  1090. ASSERT(TaAddressType == TDI_ADDRESS_TYPE_NETBIOS);
  1091. status = STATUS_INVALID_ADDRESS_COMPONENT;
  1092. continue;
  1093. }
  1094. }
  1095. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  1096. KdPrint(("netbt!DelayedNbtProcessConnect: Sending local irp=%lx, %lx of %lx\n"
  1097. "\t\t\t\tTA=%lx Length=%lx\n",
  1098. pDelConnCtx->pLocalIrp, pDelConnCtx->CurrIndex+1, pDelConnCtx->NumberOfAddresses,
  1099. pDelConnCtx->pTaAddress, pDelConnCtx->TaAddressLength));
  1100. DeleteInternalAddress(pDelConnCtx->pTransportAddress);
  1101. pDelConnCtx->pTransportAddress = pTransportAddress;
  1102. pTransportAddress = NULL;
  1103. break;
  1104. }
  1105. if (pTransportAddress) {
  1106. DeleteInternalAddress(pTransportAddress);
  1107. pTransportAddress = NULL;
  1108. }
  1109. return status;
  1110. }
  1111. VOID
  1112. DoneDelayedNbtProcessConnect(
  1113. IN PNBT_DELAYED_CONNECT_CONTEXT pDelConnCtx,
  1114. NTSTATUS status
  1115. )
  1116. /*++
  1117. 1. Complete the client IRP
  1118. 2. Cleanup everything
  1119. --*/
  1120. {
  1121. ASSERT(pDelConnCtx->pLocalIrp);
  1122. ASSERT(pDelConnCtx->pClientIrp);
  1123. NbtCancelCancelRoutine(pDelConnCtx->pClientIrp);
  1124. ASSERT(status != STATUS_PENDING);
  1125. if (pDelConnCtx->pLocalIrp) {
  1126. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  1127. KdPrint(("netbt!DoneDelayedNbtProcessConnect: Freeing Local Irp=<%x>\n", pDelConnCtx->pLocalIrp));
  1128. IoFreeIrp(pDelConnCtx->pLocalIrp);
  1129. }
  1130. if (pDelConnCtx->pTransportAddress) {
  1131. DeleteInternalAddress(pDelConnCtx->pTransportAddress);
  1132. }
  1133. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  1134. KdPrint(("netbt!DoneDelayed...: Connect Complete, LocalIrp=<%x>, ClientIrp=<%x>, Status=<%x>\n",
  1135. pDelConnCtx->pLocalIrp, pDelConnCtx->pClientIrp, status));
  1136. NbtTrace(NBT_TRACE_OUTBOUND, ("Complete connection request pIrp=%p pLocalIrp=%p with %!status!",
  1137. pDelConnCtx->pClientIrp, pDelConnCtx->pLocalIrp, status));
  1138. NTIoComplete (pDelConnCtx->pClientIrp, status, 0);
  1139. pDelConnCtx->pLocalIrp = NULL;
  1140. pDelConnCtx->pClientIrp = NULL;
  1141. pDelConnCtx->pTransportAddress = NULL;
  1142. CTEFreeMem(pDelConnCtx);
  1143. }
  1144. //----------------------------------------------------------------------------
  1145. NTSTATUS
  1146. NbtpConnectCompletionRoutine(
  1147. PDEVICE_OBJECT pDeviceObject,
  1148. PIRP pIrp,
  1149. PVOID pCompletionContext
  1150. )
  1151. /*++
  1152. Routine Description:
  1153. This Routine is the completion routine for local IRPS that are generated
  1154. to handle compound transport addresses
  1155. Arguments:
  1156. pDeviceObject - the device object
  1157. pIrp - a ptr to an IRP
  1158. pCompletionContext - the completion context
  1159. Return Value:
  1160. NTSTATUS - status of the request
  1161. --*/
  1162. {
  1163. PNBT_DELAYED_CONNECT_CONTEXT pDelConnCtx;
  1164. NTSTATUS Status, Status2;
  1165. PIRP pLocalIrp;
  1166. tDEVICECONTEXT *pDeviceContext;
  1167. pDelConnCtx = pCompletionContext;
  1168. pDeviceContext = pDelConnCtx->pDeviceContext;
  1169. pLocalIrp = pDelConnCtx->pLocalIrp;
  1170. ASSERT (pIrp == pLocalIrp);
  1171. Status = pLocalIrp->IoStatus.Status;
  1172. ASSERT(Status != STATUS_PENDING);
  1173. pDelConnCtx->ProcessingDone = TRUE;
  1174. /*
  1175. * Let's move to next address
  1176. */
  1177. Status2 = NextDelayedNbtProcessConnect(pDelConnCtx);
  1178. /*
  1179. * Are we done
  1180. */
  1181. if (Status == STATUS_CANCELLED || Status == STATUS_SUCCESS || Status2 != STATUS_SUCCESS ||
  1182. pDelConnCtx->CurrIndex >= pDelConnCtx->NumberOfAddresses) {
  1183. if (Status2 != STATUS_SUCCESS) {
  1184. Status = Status2;
  1185. }
  1186. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  1187. KdPrint(("Nbt.NbtpC...:==>Connect Complete, LocalIrp=<%x>, ClientIrp=<%x>, Status=<%x> <==\n",
  1188. pIrp,pDelConnCtx->pClientIrp, Status));
  1189. if (Status == STATUS_HOST_UNREACHABLE) {
  1190. Status = STATUS_BAD_NETWORK_PATH;
  1191. }
  1192. DoneDelayedNbtProcessConnect(pDelConnCtx, Status);
  1193. return STATUS_MORE_PROCESSING_REQUIRED;
  1194. }
  1195. /*
  1196. * Start worker thread to process the Connect request on the next address
  1197. */
  1198. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  1199. KdPrint(("NbtpConnectCompletionRoutine: queuing worker item, local irp=%lx, previous status=%lx\n",
  1200. pIrp, Status));
  1201. if (STATUS_SUCCESS != CTEQueueForNonDispProcessing (DelayedNbtProcessConnect,
  1202. NULL,
  1203. pDelConnCtx,
  1204. NULL,
  1205. pDeviceContext,
  1206. FALSE))
  1207. {
  1208. KdPrint(("Nbt.NbtpConnectCompletionRoutine: Failed to Enqueue Worker thread\n"));
  1209. DoneDelayedNbtProcessConnect(pDelConnCtx, STATUS_INSUFFICIENT_RESOURCES);
  1210. }
  1211. return STATUS_MORE_PROCESSING_REQUIRED;
  1212. }
  1213. //----------------------------------------------------------------------------
  1214. NTSTATUS
  1215. DelayedNbtProcessConnect(
  1216. IN tDGRAM_SEND_TRACKING *pUnused1,
  1217. IN PVOID pClientContext,
  1218. IN PVOID pUnused2,
  1219. IN tDEVICECONTEXT *pUnused3
  1220. )
  1221. /*++
  1222. Routine Description:
  1223. This Routine is the worker thread for processing Connect Requests.
  1224. Arguments:
  1225. pContext
  1226. Return Value:
  1227. NONE
  1228. --*/
  1229. {
  1230. PNBT_DELAYED_CONNECT_CONTEXT pDelConnCtx;
  1231. PIRP pIrp, pLocalIrp;
  1232. PIO_STACK_LOCATION pIrpSp;
  1233. tCONNECTELE *pConnEle;
  1234. NTSTATUS Status;
  1235. PTDI_REQUEST_KERNEL pRequestKernel;
  1236. CTEPagedCode();
  1237. pDelConnCtx = (PNBT_DELAYED_CONNECT_CONTEXT) pClientContext;
  1238. pIrp = pDelConnCtx->pClientIrp;
  1239. pLocalIrp = pDelConnCtx->pLocalIrp;
  1240. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  1241. KdPrint(("netbt!DelayedNbtProcessConnect: Enter with local irp=%lx, %lx of %lx\n"
  1242. "\t\t\t\tTA=%lx Length=%lx\n",
  1243. pLocalIrp, pDelConnCtx->CurrIndex+1, pDelConnCtx->NumberOfAddresses,
  1244. pDelConnCtx->pTaAddress, pDelConnCtx->TaAddressLength));
  1245. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  1246. pConnEle = pIrpSp->FileObject->FsContext;
  1247. if (!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN)) {
  1248. DbgPrint ("Nbt.DelayedNbtProcessConnect: ERROR - Invalid Connection Handle\n");
  1249. DoneDelayedNbtProcessConnect(pDelConnCtx,
  1250. (pDelConnCtx->ProcessingDone)? pLocalIrp->IoStatus.Status: STATUS_UNSUCCESSFUL);
  1251. return STATUS_UNSUCCESSFUL;
  1252. }
  1253. CHECK_PTR (pConnEle);
  1254. Status = STATUS_UNSUCCESSFUL;
  1255. /*
  1256. * Set the Cancel routine and ensure that the original IRP was not cancelled before continuing.
  1257. */
  1258. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  1259. KdPrint (("Nbt.DelayedNbtProcessConnect: Setting Cancel=<NbtCancelConnect> for Irp:Device <%x:%x>\n",
  1260. pIrp, pDelConnCtx->pDeviceContext));
  1261. if (STATUS_CANCELLED == NTCheckSetCancelRoutine(pIrp, NbtCancelConnect, pDelConnCtx->pDeviceContext)) {
  1262. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  1263. KdPrint(("Nbt.DelayedNbtProcessConnect: Irp <%x> was cancelled\n", pIrp));
  1264. pConnEle->pIrp = NULL;
  1265. DoneDelayedNbtProcessConnect(pDelConnCtx, STATUS_CANCELLED);
  1266. return STATUS_CANCELLED;
  1267. }
  1268. /*
  1269. * InitDelayedNbtProcessConnect/NextDelayedNbtProcessConnect has set up pDelConnCtx->pTransportAddress properly
  1270. */
  1271. ASSERT(pDelConnCtx->pTransportAddress);
  1272. pConnEle->AddressType = pDelConnCtx->pTransportAddress->Address[0].Address[0].AddressType;
  1273. pRequestKernel = (PTDI_REQUEST_KERNEL) &pIrpSp->Parameters;
  1274. pDelConnCtx->LocalConnectionInformation = *(pRequestKernel->RequestConnectionInformation);
  1275. pDelConnCtx->LocalConnectionInformation.RemoteAddress = pDelConnCtx->pTransportAddress;
  1276. pDelConnCtx->LocalConnectionInformation.RemoteAddressLength = pDelConnCtx->pTransportAddress->Address[0].AddressLength;
  1277. //
  1278. // Save the thread info for debugging purposes!
  1279. //
  1280. pLocalIrp->Tail.Overlay.Thread = PsGetCurrentThread();
  1281. TdiBuildConnect (pLocalIrp,
  1282. &pDelConnCtx->pDeviceContext->DeviceObject,
  1283. pIrpSp->FileObject,
  1284. NbtpConnectCompletionRoutine,
  1285. pDelConnCtx,
  1286. pRequestKernel->RequestSpecific,
  1287. &pDelConnCtx->LocalConnectionInformation,
  1288. pRequestKernel->ReturnConnectionInformation);
  1289. Status = IoCallDriver(&pDelConnCtx->pDeviceContext->DeviceObject,pLocalIrp);
  1290. if (Status != STATUS_PENDING) {
  1291. IF_DBG(NBT_DEBUG_NETBIOS_EX)
  1292. KdPrint(("Nbt.DelayedNbtProcessConnect: IoCallDriver returned %lx for irp %lx (%lx)\n",
  1293. Status,pIrp,pLocalIrp));
  1294. // ASSERT(0);
  1295. }
  1296. return STATUS_PENDING;
  1297. }
  1298. //----------------------------------------------------------------------------
  1299. NTSTATUS
  1300. NTConnect(
  1301. IN tDEVICECONTEXT *pDeviceContext,
  1302. IN PIRP pIrp)
  1303. /*++
  1304. Routine Description:
  1305. This Routine handles calling the non OS specific code to open a session
  1306. connection to a destination.
  1307. Arguments:
  1308. pIrp - a ptr to an IRP
  1309. Return Value:
  1310. NTSTATUS - status of the request
  1311. --*/
  1312. {
  1313. PIO_STACK_LOCATION pIrpSp;
  1314. PTRANSPORT_ADDRESS pRemoteAddress;
  1315. PTDI_REQUEST_KERNEL pRequestKernel;
  1316. PIRP pLocalIrp;
  1317. PNBT_DELAYED_CONNECT_CONTEXT pDelConnCtx;
  1318. NTSTATUS Status;
  1319. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  1320. pRequestKernel = (PTDI_REQUEST_KERNEL)&pIrpSp->Parameters;
  1321. try
  1322. {
  1323. pRemoteAddress = pRequestKernel->RequestConnectionInformation->RemoteAddress;
  1324. if (pRequestKernel->RequestConnectionInformation->RemoteAddressLength < sizeof(TRANSPORT_ADDRESS)) {
  1325. NbtTrace(NBT_TRACE_OUTBOUND, ("Incorrect address length %d, required %d",
  1326. pRequestKernel->RequestConnectionInformation->RemoteAddressLength, sizeof(TRANSPORT_ADDRESS)));
  1327. return STATUS_INVALID_ADDRESS_COMPONENT;
  1328. }
  1329. }
  1330. except(EXCEPTION_EXECUTE_HANDLER)
  1331. {
  1332. KdPrint (("Nbt.NTConnect: Exception <0x%x> trying to access Connection info\n", GetExceptionCode()));
  1333. NbtTrace(NBT_TRACE_OUTBOUND, ("Exception <0x%x> trying to access Connection info\n", GetExceptionCode()));
  1334. return STATUS_INVALID_ADDRESS_COMPONENT;
  1335. }
  1336. if (pIrpSp->CompletionRoutine != NbtpConnectCompletionRoutine) {
  1337. pDelConnCtx = NbtAllocMem(sizeof(NBT_DELAYED_CONNECT_CONTEXT),NBT_TAG('e'));
  1338. if (!pDelConnCtx) {
  1339. NbtTrace(NBT_TRACE_OUTBOUND, ("Out of memory"));
  1340. return(STATUS_INSUFFICIENT_RESOURCES);
  1341. }
  1342. CTEZeroMemory(pDelConnCtx, sizeof(NBT_DELAYED_CONNECT_CONTEXT));
  1343. pLocalIrp = IoAllocateIrp(pDeviceContext->DeviceObject.StackSize,FALSE);
  1344. if (!pLocalIrp) {
  1345. NbtTrace(NBT_TRACE_OUTBOUND, ("Out of memory"));
  1346. CTEFreeMem(pDelConnCtx);
  1347. return(STATUS_INSUFFICIENT_RESOURCES);
  1348. }
  1349. pDelConnCtx->pDeviceContext = pDeviceContext;
  1350. pDelConnCtx->pClientIrp = pIrp;
  1351. pDelConnCtx->pLocalIrp = pLocalIrp;
  1352. pDelConnCtx->pTransportAddress = NULL;
  1353. pDelConnCtx->ProcessingDone = FALSE;
  1354. Status = InitDelayedNbtProcessConnect(pDelConnCtx);
  1355. if (!NT_SUCCESS(Status)) {
  1356. NbtTrace(NBT_TRACE_OUTBOUND, ("Out of memory status=%!status!", Status));
  1357. CTEFreeMem(pDelConnCtx);
  1358. IoFreeIrp(pLocalIrp);
  1359. return Status;
  1360. }
  1361. NbtTrace(NBT_TRACE_OUTBOUND, ("Connection request pIrp=%p pLocalIrp=%p", pIrp, pLocalIrp));
  1362. //return (DelayedNbtProcessConnect (NULL, pDelConnCtx, NULL, NULL));
  1363. DelayedNbtProcessConnect (NULL, pDelConnCtx, NULL, NULL);
  1364. // Ignore the return from DelayedNbtProcessConnect and always return STATUS_PENDING;
  1365. // our client completion routine will take care of completing the IRP
  1366. // Otherwise, we will complete the IRP twice.
  1367. return STATUS_PENDING;
  1368. }
  1369. else
  1370. {
  1371. TDI_REQUEST Request;
  1372. tCONNECTELE *pConnEle;
  1373. // call the non-NT specific function to setup the connection
  1374. pConnEle = Request.Handle.ConnectionContext = pIrpSp->FileObject->FsContext;
  1375. if (!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  1376. {
  1377. ASSERTMSG ("Nbt.NTConnect: ERROR - Invalid Connection Handle\n", 0);
  1378. NbtTrace(NBT_TRACE_OUTBOUND, ("Invalid pConnEle %p", pConnEle));
  1379. return (STATUS_INVALID_HANDLE);
  1380. }
  1381. /*
  1382. * A user mode process may send us a faked request with a completion routine
  1383. * equal to NbtpConnectCompletionRoutine.
  1384. * Never let it pass through.
  1385. */
  1386. if (pIrp->RequestorMode != KernelMode) {
  1387. ASSERTMSG ("Nbt.NTConnect: ERROR - Invalid request\n", 0);
  1388. NbtTrace(NBT_TRACE_OUTBOUND, ("Invalid requestor mode"));
  1389. return (STATUS_INVALID_PARAMETER);
  1390. }
  1391. return NbtConnect(&Request,
  1392. pRequestKernel->RequestSpecific, // Ulong
  1393. pRequestKernel->RequestConnectionInformation,
  1394. pIrp);
  1395. }
  1396. }
  1397. //----------------------------------------------------------------------------
  1398. NTSTATUS
  1399. NTDisconnect(
  1400. IN tDEVICECONTEXT *pDeviceContext,
  1401. IN PIRP pIrp)
  1402. /*++
  1403. Routine Description:
  1404. This Routine handles calling the Non OS specific code to disconnect a
  1405. session.
  1406. Arguments:
  1407. pIrp - a ptr to an IRP
  1408. Return Value:
  1409. NTSTATUS - status of the request
  1410. --*/
  1411. {
  1412. TDI_REQUEST Request;
  1413. PIO_STACK_LOCATION pIrpSp;
  1414. NTSTATUS status;
  1415. PTDI_REQUEST_KERNEL pRequestKernel;
  1416. tCONNECTELE *pConnEle;
  1417. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  1418. pRequestKernel = (PTDI_REQUEST_KERNEL)&pIrpSp->Parameters;
  1419. pConnEle = Request.Handle.ConnectionContext = pIrpSp->FileObject->FsContext;
  1420. if (!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  1421. {
  1422. ASSERTMSG ("Nbt.NTDisconnect: ERROR - Invalid Connection Handle\n", 0);
  1423. return (STATUS_INVALID_HANDLE);
  1424. }
  1425. // call the non-NT specific function to setup the connection
  1426. status = NbtDisconnect(
  1427. &Request,
  1428. pRequestKernel->RequestSpecific, // Large Integer
  1429. (ULONG) pRequestKernel->RequestFlags,
  1430. pRequestKernel->RequestConnectionInformation,
  1431. pRequestKernel->ReturnConnectionInformation,
  1432. pIrp
  1433. );
  1434. return(status);
  1435. }
  1436. //----------------------------------------------------------------------------
  1437. NTSTATUS
  1438. NTListen(
  1439. IN tDEVICECONTEXT *pDeviceContext,
  1440. IN PIRP pIrp)
  1441. /*++
  1442. Routine Description:
  1443. This Routine handles
  1444. Arguments:
  1445. pIrp - a ptr to an IRP
  1446. Return Value:
  1447. NTSTATUS - status of the request
  1448. --*/
  1449. {
  1450. NTSTATUS status;
  1451. TDI_REQUEST Request;
  1452. PTDI_REQUEST_KERNEL pRequestKernel;
  1453. PIO_STACK_LOCATION pIrpSp;
  1454. tCONNECTELE *pConnEle;
  1455. CTEPagedCode();
  1456. IF_DBG(NBT_DEBUG_NAMESRV)
  1457. KdPrint(("Nbt.NTListen: Got a LISTEN !!! *****************\n"));
  1458. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  1459. pRequestKernel = (PTDI_REQUEST_KERNEL)&pIrpSp->Parameters;
  1460. pConnEle = Request.Handle.ConnectionContext = pIrpSp->FileObject->FsContext;
  1461. if (NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  1462. {
  1463. try
  1464. {
  1465. PCHAR pName;
  1466. ULONG lNameType;
  1467. ULONG NameLen;
  1468. // Initialize Request data (may be needed by Vxd)
  1469. Request.RequestNotifyObject = NULL;
  1470. Request.RequestContext = NULL;
  1471. // call the non-NT specific function to setup the connection
  1472. status = NbtListen (&Request,
  1473. (ULONG) pRequestKernel->RequestFlags, // Ulong
  1474. pRequestKernel->RequestConnectionInformation,
  1475. pRequestKernel->ReturnConnectionInformation,
  1476. pIrp);
  1477. }
  1478. except(EXCEPTION_EXECUTE_HANDLER)
  1479. {
  1480. KdPrint (("Nbt.NTListen: Exception <0x%x> trying to access buffer\n", GetExceptionCode()));
  1481. status = STATUS_INVALID_ADDRESS;
  1482. }
  1483. }
  1484. else
  1485. {
  1486. ASSERTMSG ("Nbt.NTListen: ERROR - Invalid Connection Handle\n", 0);
  1487. status = STATUS_INVALID_HANDLE; // Bug# 202340: Have to complete Irp here!
  1488. }
  1489. if (status != STATUS_PENDING)
  1490. {
  1491. NTIoComplete(pIrp,status,0);
  1492. }
  1493. return(status);
  1494. }
  1495. //----------------------------------------------------------------------------
  1496. NBT_WORK_ITEM_CONTEXT *
  1497. FindLmhSvcRequest(
  1498. IN PDEVICE_OBJECT DeviceContext,
  1499. IN PIRP pIrp,
  1500. IN tLMHSVC_REQUESTS *pLmhRequest
  1501. )
  1502. /*++
  1503. Routine Description:
  1504. This routine handles the cancelling a Query to LmHost, so that the client's
  1505. irp can be returned to the client. This cancellation is instigated
  1506. by the client (i.e. RDR).
  1507. Arguments:
  1508. Return Value:
  1509. The final status from the operation.
  1510. --*/
  1511. {
  1512. tDGRAM_SEND_TRACKING *pTracker;
  1513. NBT_WORK_ITEM_CONTEXT *Context;
  1514. BOOLEAN FoundIt = FALSE;
  1515. PLIST_ENTRY pHead;
  1516. PLIST_ENTRY pEntry;
  1517. if (pLmhRequest->ResolvingNow && pLmhRequest->Context)
  1518. {
  1519. // this is the session setup tracker
  1520. //
  1521. Context = (NBT_WORK_ITEM_CONTEXT *) pLmhRequest->Context;
  1522. pTracker = (tDGRAM_SEND_TRACKING *) Context->pClientContext;
  1523. if (pTracker->pClientIrp == pIrp)
  1524. {
  1525. pLmhRequest->Context = NULL;
  1526. FoundIt = TRUE;
  1527. }
  1528. }
  1529. else
  1530. {
  1531. //
  1532. // go through the list of Queued requests to find the correct one
  1533. // and cancel it
  1534. //
  1535. pHead = pEntry = &pLmhRequest->ToResolve;
  1536. while ((pEntry = pEntry->Flink) != pHead)
  1537. {
  1538. Context = CONTAINING_RECORD (pEntry,NBT_WORK_ITEM_CONTEXT,Item.List);
  1539. // this is the session setup tracker
  1540. //
  1541. pTracker = (tDGRAM_SEND_TRACKING *)Context->pClientContext;
  1542. if (pTracker->pClientIrp == pIrp)
  1543. {
  1544. RemoveEntryList(pEntry);
  1545. FoundIt = TRUE;
  1546. break;
  1547. }
  1548. }
  1549. }
  1550. return (FoundIt ? Context : NULL);
  1551. }
  1552. //----------------------------------------------------------------------------
  1553. NTSTATUS
  1554. QueryProviderCompletion(
  1555. IN PDEVICE_OBJECT DeviceContext,
  1556. IN PIRP Irp,
  1557. IN PVOID Context
  1558. )
  1559. /*++
  1560. Routine Description:
  1561. This routine handles the completion event when the Query Provider
  1562. Information completes. This routine must decrement the MaxDgramSize
  1563. and max send size by the respective NBT header sizes.
  1564. Arguments:
  1565. DeviceObject - unused.
  1566. Irp - Supplies Irp that the transport has finished processing.
  1567. Context - not used
  1568. Return Value:
  1569. The final status from the operation (success or an exception).
  1570. --*/
  1571. {
  1572. PTDI_PROVIDER_INFO pProvider;
  1573. ULONG HdrSize;
  1574. ULONG SubnetAddr;
  1575. ULONG ThisSubnetAddr;
  1576. PLIST_ENTRY pHead;
  1577. PLIST_ENTRY pEntry;
  1578. tDEVICECONTEXT *pDeviceContext;
  1579. tDEVICECONTEXT *pDevContext;
  1580. CTELockHandle OldIrq;
  1581. if (NT_SUCCESS(Irp->IoStatus.Status))
  1582. {
  1583. pDeviceContext = (tDEVICECONTEXT *)DeviceContext;
  1584. pProvider = (PTDI_PROVIDER_INFO)MmGetMdlVirtualAddress(Irp->MdlAddress);
  1585. //
  1586. // Set the correct service flags to indicate what Netbt supports.
  1587. //
  1588. pProvider->ServiceFlags = TDI_SERVICE_MESSAGE_MODE |
  1589. TDI_SERVICE_CONNECTION_MODE |
  1590. TDI_SERVICE_CONNECTIONLESS_MODE |
  1591. TDI_SERVICE_ERROR_FREE_DELIVERY |
  1592. TDI_SERVICE_BROADCAST_SUPPORTED |
  1593. TDI_SERVICE_MULTICAST_SUPPORTED |
  1594. TDI_SERVICE_DELAYED_ACCEPTANCE |
  1595. TDI_SERVICE_ROUTE_DIRECTED |
  1596. TDI_SERVICE_FORCE_ACCESS_CHECK;
  1597. pProvider->MinimumLookaheadData = 128;
  1598. if (pProvider->MaxSendSize > sizeof(tSESSIONHDR))
  1599. {
  1600. //
  1601. // Nbt has just a two byte + 1 bit session message length, so it
  1602. // can't have a send size larger than 1ffff
  1603. //
  1604. if (pProvider->MaxSendSize > (0x1FFFF + sizeof(tSESSIONHDR)))
  1605. {
  1606. pProvider->MaxSendSize = 0x1FFFF;
  1607. }
  1608. else
  1609. {
  1610. pProvider->MaxSendSize -= sizeof(tSESSIONHDR);
  1611. }
  1612. }
  1613. else
  1614. {
  1615. pProvider->MaxSendSize = 0;
  1616. }
  1617. // subtract the datagram hdr size and the scope size (times 2)
  1618. HdrSize = DGRAM_HDR_SIZE + (NbtConfig.ScopeLength << 1);
  1619. if ((!IsDeviceNetbiosless (pDeviceContext)) &&
  1620. (pProvider->MaxDatagramSize > HdrSize))
  1621. {
  1622. pProvider->MaxDatagramSize -= HdrSize;
  1623. if (pProvider->MaxDatagramSize > MAX_NBT_DGRAM_SIZE)
  1624. {
  1625. pProvider->MaxDatagramSize = MAX_NBT_DGRAM_SIZE;
  1626. }
  1627. }
  1628. else
  1629. {
  1630. pProvider->MaxDatagramSize = 0;
  1631. }
  1632. //
  1633. // We need to hold the JointLock before we traverse
  1634. // the list of Devices
  1635. //
  1636. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  1637. //
  1638. // Check if any of the adapters with the same subnet address have
  1639. // the PointtoPoint bit set - and if so set it in the response.
  1640. //
  1641. SubnetAddr = pDeviceContext->IpAddress & pDeviceContext->SubnetMask;
  1642. pEntry = pHead = &NbtConfig.DeviceContexts;
  1643. while ((pEntry = pEntry->Flink) != pHead)
  1644. {
  1645. pDevContext = CONTAINING_RECORD(pEntry,tDEVICECONTEXT,Linkage);
  1646. ThisSubnetAddr = pDevContext->IpAddress & pDevContext->SubnetMask;
  1647. if ((SubnetAddr == ThisSubnetAddr) &&
  1648. (pDevContext->IpInterfaceFlags & IP_INTFC_FLAG_P2P))
  1649. {
  1650. pProvider->ServiceFlags |= TDI_SERVICE_POINT_TO_POINT;
  1651. break;
  1652. }
  1653. }
  1654. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  1655. }
  1656. //
  1657. // Must return a non-error status otherwise the IO system will not copy
  1658. // back into the users buffer.
  1659. //
  1660. return(STATUS_SUCCESS);
  1661. }
  1662. //----------------------------------------------------------------------------
  1663. NTSTATUS
  1664. NTQueryInformation(
  1665. IN tDEVICECONTEXT *pDeviceContext,
  1666. IN PIRP pIrp)
  1667. /*++
  1668. Routine Description:
  1669. This Routine handles
  1670. Arguments:
  1671. pIrp - a ptr to an IRP
  1672. Return Value:
  1673. NTSTATUS - status of the request
  1674. --*/
  1675. {
  1676. PIO_STACK_LOCATION pIrpSp;
  1677. PTDI_REQUEST_KERNEL_QUERY_INFORMATION Query;
  1678. NTSTATUS status = STATUS_UNSUCCESSFUL;
  1679. NTSTATUS Locstatus;
  1680. PVOID pBuffer = NULL;
  1681. LONG Size ;
  1682. PTA_NETBIOS_ADDRESS BroadcastAddress;
  1683. ULONG AddressLength;
  1684. ULONG BytesCopied = 0;
  1685. PDEVICE_OBJECT pDeviceObject;
  1686. //
  1687. // Should not be pageable since AFD can call us at raised Irql in case of AcceptEx.
  1688. //
  1689. // CTEPagedCode();
  1690. if (pDeviceContext == pWinsDeviceContext)
  1691. {
  1692. NTIoComplete(pIrp, STATUS_INVALID_DEVICE_REQUEST, 0);
  1693. return (STATUS_INVALID_DEVICE_REQUEST);
  1694. }
  1695. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  1696. Query = (PTDI_REQUEST_KERNEL_QUERY_INFORMATION)&pIrpSp->Parameters;
  1697. IF_DBG(NBT_DEBUG_NAMESRV)
  1698. KdPrint(("Nbt.NTQueryInformation: Query type = %X\n",Query->QueryType));
  1699. switch (Query->QueryType)
  1700. {
  1701. case TDI_QUERY_BROADCAST_ADDRESS:
  1702. {
  1703. // the broadcast address is the netbios name "*0000000..."
  1704. if ((!pIrp->MdlAddress) ||
  1705. (!(BroadcastAddress = (PTA_NETBIOS_ADDRESS)NbtAllocMem(sizeof(TA_NETBIOS_ADDRESS),NBT_TAG('b')))))
  1706. {
  1707. status = STATUS_INSUFFICIENT_RESOURCES;
  1708. break;
  1709. }
  1710. AddressLength = sizeof(TA_NETBIOS_ADDRESS);
  1711. BroadcastAddress->TAAddressCount = 1;
  1712. BroadcastAddress->Address[0].AddressLength = NETBIOS_NAME_SIZE +
  1713. sizeof(USHORT);
  1714. BroadcastAddress->Address[0].AddressType = TDI_ADDRESS_TYPE_NETBIOS;
  1715. BroadcastAddress->Address[0].Address[0].NetbiosNameType = TDI_ADDRESS_NETBIOS_TYPE_GROUP;
  1716. // the broadcast address to NetBios is "* 000000...", an * followed
  1717. // by 15 zeroes.
  1718. CTEZeroMemory(BroadcastAddress->Address[0].Address[0].NetbiosName,
  1719. NETBIOS_NAME_SIZE);
  1720. BroadcastAddress->Address[0].Address[0].NetbiosName[0] = '*';
  1721. status = TdiCopyBufferToMdl (
  1722. (PVOID)BroadcastAddress,
  1723. 0,
  1724. AddressLength,
  1725. pIrp->MdlAddress,
  1726. 0,
  1727. (PULONG)&pIrp->IoStatus.Information);
  1728. BytesCopied = (ULONG) pIrp->IoStatus.Information;
  1729. CTEMemFree((PVOID)BroadcastAddress);
  1730. break;
  1731. }
  1732. case TDI_QUERY_PROVIDER_INFO:
  1733. {
  1734. //
  1735. // Simply pass the Irp on by to the Transport, and let it
  1736. // fill in the provider info
  1737. //
  1738. if (!pDeviceContext->IpAddress)
  1739. {
  1740. status = STATUS_INVALID_DEVICE_STATE;
  1741. break;
  1742. }
  1743. if (StreamsStack)
  1744. {
  1745. TdiBuildQueryInformation(pIrp,
  1746. pDeviceContext->pFileObjects->pDgramDeviceObject,
  1747. pDeviceContext->pFileObjects->pDgramFileObject,
  1748. QueryProviderCompletion,
  1749. NULL,
  1750. TDI_QUERY_PROVIDER_INFO,
  1751. pIrp->MdlAddress);
  1752. }
  1753. else
  1754. {
  1755. TdiBuildQueryInformation(pIrp,
  1756. pDeviceContext->pControlDeviceObject,
  1757. pDeviceContext->pControlFileObject,
  1758. QueryProviderCompletion,
  1759. NULL,
  1760. TDI_QUERY_PROVIDER_INFO,
  1761. pIrp->MdlAddress);
  1762. }
  1763. CHECK_COMPLETION(pIrp);
  1764. status = IoCallDriver(pDeviceContext->pControlDeviceObject,pIrp);
  1765. //
  1766. // we must return the next drivers ret code back to the IO subsystem
  1767. //
  1768. return(status);
  1769. }
  1770. case TDI_QUERY_ADAPTER_STATUS:
  1771. {
  1772. if (!pIrp->MdlAddress)
  1773. {
  1774. break;
  1775. }
  1776. Size = MmGetMdlByteCount (pIrp->MdlAddress);
  1777. //
  1778. // check if it is a remote or local adapter status
  1779. //
  1780. if (Query->RequestConnectionInformation &&
  1781. Query->RequestConnectionInformation->RemoteAddress)
  1782. {
  1783. PCHAR pName;
  1784. ULONG lNameType;
  1785. ULONG NameLen;
  1786. tDGRAM_SEND_TRACKING *pTracker;
  1787. TDI_ADDRESS_NETBT_INTERNAL TdiAddr;
  1788. //
  1789. //
  1790. // in case the call results in a name query on the wire...
  1791. //
  1792. IoMarkIrpPending(pIrp);
  1793. status = STATUS_SUCCESS;
  1794. if (pIrp->RequestorMode != KernelMode) {
  1795. try
  1796. {
  1797. ProbeForRead(Query->RequestConnectionInformation->RemoteAddress,
  1798. Query->RequestConnectionInformation->RemoteAddressLength,
  1799. sizeof(BYTE));
  1800. } except(EXCEPTION_EXECUTE_HANDLER) {
  1801. status = STATUS_INVALID_PARAMETER;
  1802. }
  1803. }
  1804. if (NT_SUCCESS(status) && NT_SUCCESS (status = GetNetBiosNameFromTransportAddress(
  1805. (PTRANSPORT_ADDRESS) Query->RequestConnectionInformation->RemoteAddress,
  1806. Query->RequestConnectionInformation->RemoteAddressLength, &TdiAddr)))
  1807. {
  1808. pName = TdiAddr.OEMRemoteName.Buffer;
  1809. NameLen = TdiAddr.OEMRemoteName.Length;
  1810. lNameType = TdiAddr.NameType;
  1811. if ((lNameType == TDI_ADDRESS_NETBIOS_TYPE_UNIQUE) &&
  1812. (NameLen == NETBIOS_NAME_SIZE) &&
  1813. (NT_SUCCESS (status = GetTracker(&pTracker, NBT_TRACKER_ADAPTER_STATUS))))
  1814. {
  1815. pTracker->ClientContext = pIrp;
  1816. status = NbtSendNodeStatus (pDeviceContext,
  1817. pName,
  1818. NULL,
  1819. pTracker,
  1820. CopyNodeStatusResponseCompletion);
  1821. // only complete the irp (below) for failure status's
  1822. if (status == STATUS_PENDING)
  1823. {
  1824. return(status);
  1825. }
  1826. //
  1827. // We cannot have a Success status returned here!
  1828. //
  1829. if (status == STATUS_SUCCESS)
  1830. {
  1831. ASSERT (0);
  1832. status = STATUS_UNSUCCESSFUL;
  1833. }
  1834. FreeTracker (pTracker, RELINK_TRACKER);
  1835. }
  1836. else if (NT_SUCCESS(status))
  1837. {
  1838. status = STATUS_INVALID_PARAMETER; // The NameType or NameLen must be wrong!
  1839. }
  1840. }
  1841. // the request has been satisfied, so unmark the pending
  1842. // since we will return the irp below
  1843. //
  1844. pIrpSp->Control &= ~SL_PENDING_RETURNED;
  1845. }
  1846. else
  1847. {
  1848. // return an array of netbios names that are registered
  1849. status = NbtQueryAdapterStatus(pDeviceContext,
  1850. &pBuffer,
  1851. &Size,
  1852. NBT_LOCAL);
  1853. }
  1854. break;
  1855. }
  1856. case TDI_QUERY_CONNECTION_INFO:
  1857. {
  1858. tCONNECTELE *pConnectEle;
  1859. tLOWERCONNECTION *pLowerConn;
  1860. KIRQL OldIrq1, OldIrq2;
  1861. // pass to transport to get the current throughput, delay and
  1862. // reliability numbers
  1863. //
  1864. pConnectEle = (tCONNECTELE *)pIrpSp->FileObject->FsContext;
  1865. if (!NBT_VERIFY_HANDLE2 (pConnectEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  1866. {
  1867. ASSERTMSG ("Nbt.NTQueryInformation: ERROR - Invalid Connection Handle\n", 0);
  1868. status = STATUS_INVALID_HANDLE;
  1869. break;
  1870. }
  1871. CTESpinLock(pConnectEle, OldIrq1);
  1872. pLowerConn = (tLOWERCONNECTION *)pConnectEle->pLowerConnId;
  1873. if (!NBT_VERIFY_HANDLE (pLowerConn, NBT_VERIFY_LOWERCONN))
  1874. {
  1875. status = STATUS_CONNECTION_INVALID;
  1876. CTESpinFree(pConnectEle, OldIrq1);
  1877. break;
  1878. }
  1879. CTESpinLock(pLowerConn, OldIrq2);
  1880. NBT_REFERENCE_LOWERCONN (pLowerConn, REF_LOWC_QUERY_INFO); // Bug # 212632
  1881. CTESpinFree(pLowerConn, OldIrq2);
  1882. CTESpinFree(pConnectEle, OldIrq1);
  1883. //
  1884. // Simply pass the Irp on by to the Transport, and let it
  1885. // fill in the info
  1886. //
  1887. pDeviceObject = IoGetRelatedDeviceObject( pLowerConn->pFileObject );
  1888. TdiBuildQueryInformation(pIrp,
  1889. pDeviceObject,
  1890. pLowerConn->pFileObject,
  1891. NULL, NULL,
  1892. TDI_QUERY_CONNECTION_INFO,
  1893. pIrp->MdlAddress);
  1894. status = IoCallDriver(pDeviceObject,pIrp);
  1895. NBT_DEREFERENCE_LOWERCONN (pLowerConn, REF_LOWC_QUERY_INFO, FALSE);
  1896. //
  1897. // we must return the next drivers ret code back to the IO subsystem
  1898. //
  1899. return(status);
  1900. }
  1901. case TDI_QUERY_FIND_NAME:
  1902. {
  1903. //
  1904. //
  1905. // in case the call results in a name query on the wire...
  1906. //
  1907. if (pIrp->MdlAddress)
  1908. {
  1909. //
  1910. // Verify the request address space
  1911. //
  1912. try
  1913. {
  1914. status = STATUS_INVALID_ADDRESS_COMPONENT;
  1915. if (pIrp->RequestorMode == KernelMode)
  1916. {
  1917. //
  1918. // Since the TdiBuildQueryInformation macro NULLs out the
  1919. // RequestConnectionInformation field, we need to dereference
  1920. // it under Try/Except to ensure that the caller has filled
  1921. // the fields in properly
  1922. //
  1923. PTRANSPORT_ADDRESS pRemoteAddress=Query->RequestConnectionInformation->RemoteAddress;
  1924. if ((Query->RequestConnectionInformation->RemoteAddressLength
  1925. < sizeof(TRANSPORT_ADDRESS)) ||
  1926. (pRemoteAddress->TAAddressCount < 1) ||
  1927. (pRemoteAddress->Address[0].AddressType != TDI_ADDRESS_TYPE_NETBIOS))
  1928. {
  1929. break;
  1930. }
  1931. }
  1932. else // User-mode client
  1933. {
  1934. ProbeForRead(Query->RequestConnectionInformation->RemoteAddress,
  1935. Query->RequestConnectionInformation->RemoteAddressLength,
  1936. sizeof(BYTE));
  1937. }
  1938. }
  1939. except(EXCEPTION_EXECUTE_HANDLER)
  1940. {
  1941. KdPrint (("Nbt.TDI_QUERY_FIND_NAME: Exception <0x%x> during Probe\n",
  1942. GetExceptionCode()));
  1943. break;
  1944. }
  1945. IoMarkIrpPending(pIrp);
  1946. status = NbtQueryFindName(Query->RequestConnectionInformation, pDeviceContext, pIrp, FALSE);
  1947. if (status == STATUS_PENDING)
  1948. {
  1949. return(status);
  1950. }
  1951. // the request has been satisfied, so unmark the pending
  1952. // since we will return the irp below
  1953. //
  1954. pIrpSp->Control &= ~SL_PENDING_RETURNED;
  1955. }
  1956. break;
  1957. }
  1958. case TDI_QUERY_ADDRESS_INFO:
  1959. {
  1960. if (pIrp->MdlAddress)
  1961. {
  1962. status = NbtQueryGetAddressInfo (pIrpSp, &pBuffer, &Size);
  1963. }
  1964. break;
  1965. }
  1966. case TDI_QUERY_SESSION_STATUS:
  1967. default:
  1968. {
  1969. IF_DBG(NBT_DEBUG_NAMESRV)
  1970. KdPrint(("Nbt Query Info NOT SUPPORTED = %X\n",Query->QueryType));
  1971. status = STATUS_NOT_SUPPORTED;
  1972. break;
  1973. }
  1974. } // switch
  1975. if (!NT_ERROR(status) && // allow buffer overflow to pass by
  1976. ((Query->QueryType == TDI_QUERY_ADAPTER_STATUS) ||
  1977. (Query->QueryType == TDI_QUERY_ADDRESS_INFO)))
  1978. {
  1979. status = TdiCopyBufferToMdl (pBuffer, 0, Size, pIrp->MdlAddress, 0, &BytesCopied);
  1980. CTEMemFree((PVOID)pBuffer);
  1981. }
  1982. //
  1983. // either Success or an Error
  1984. // so complete the irp
  1985. //
  1986. NTIoComplete(pIrp,status,BytesCopied);
  1987. return(status);
  1988. }
  1989. //----------------------------------------------------------------------------
  1990. NTSTATUS
  1991. NbtQueryGetAddressInfo(
  1992. IN PIO_STACK_LOCATION pIrpSp,
  1993. OUT PVOID *ppBuffer,
  1994. OUT ULONG *pSize
  1995. )
  1996. {
  1997. NTSTATUS status;
  1998. BOOLEAN IsGroup;
  1999. PLIST_ENTRY p;
  2000. tADDRESSELE *pAddressEle;
  2001. tNAMEADDR *pNameAddr;
  2002. tADDRESS_INFO *pAddressInfo;
  2003. tCLIENTELE *pClientEle;
  2004. tCONNECTELE *pConnectEle;
  2005. CTELockHandle OldIrq;
  2006. CTELockHandle OldIrq1;
  2007. PNBT_ADDRESS_PAIR_INFO pAddressPairInfo;
  2008. //
  2009. // We are not sure whether this is a ConnectionContext or a ClientContext!
  2010. //
  2011. pConnectEle = (tCONNECTELE *) pClientEle = pIrpSp->FileObject->FsContext;
  2012. if (NBT_VERIFY_HANDLE2 (pConnectEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  2013. {
  2014. //
  2015. // We crashed here since the pLowerConn was NULL below.
  2016. // Check the state of the connection, since it is possible that the connection
  2017. // was aborted and the disconnect indicated, but this query came in before the client
  2018. // got the disconnect indication.
  2019. // If the state is idle (in case of TDI_DISCONNECT_ABORT) or DISCONNECTED
  2020. // (TDI_DISCONNECT_RELEASE), error out.
  2021. // Also check for NBT_ASSOCIATED.
  2022. //
  2023. // NOTE: If NbtOpenConnection is unable to allocate the lower conn block (say, if the session fileobj
  2024. // has not been created yet), the state will be still be IDLE, so we are covered here.
  2025. //
  2026. CTESpinLock(pConnectEle,OldIrq);
  2027. if (pConnectEle->Verify != NBT_VERIFY_CONNECTION)
  2028. {
  2029. CTESpinFree(pConnectEle,OldIrq);
  2030. return (STATUS_INVALID_HANDLE);
  2031. }
  2032. else if ((pConnectEle->state <= NBT_ASSOCIATED) || // includes NBT_IDLE
  2033. (pConnectEle->state == NBT_DISCONNECTED))
  2034. {
  2035. CTESpinFree(pConnectEle,OldIrq);
  2036. return (STATUS_CONNECTION_DISCONNECTED);
  2037. }
  2038. //
  2039. // A TdiQueryInformation() call requesting TDI_QUERY_ADDRESS_INFO
  2040. // on a connection. Fill in a TDI_ADDRESS_INFO containing both the
  2041. // NetBIOS address and the IP address of the remote. Some of the
  2042. // fields are fudged.
  2043. //
  2044. if (pAddressPairInfo = NbtAllocMem(sizeof (NBT_ADDRESS_PAIR_INFO), NBT_TAG('c')))
  2045. {
  2046. memset ( pAddressPairInfo, 0, sizeof(NBT_ADDRESS_PAIR_INFO) );
  2047. pAddressPairInfo->ActivityCount = 1;
  2048. pAddressPairInfo->AddressPair.TAAddressCount = 2;
  2049. pAddressPairInfo->AddressPair.AddressIP.AddressType = TDI_ADDRESS_TYPE_IP;
  2050. pAddressPairInfo->AddressPair.AddressIP.AddressLength = TDI_ADDRESS_LENGTH_IP;
  2051. pAddressPairInfo->AddressPair.AddressNetBIOS.AddressType = TDI_ADDRESS_TYPE_NETBIOS;
  2052. pAddressPairInfo->AddressPair.AddressNetBIOS.AddressLength = TDI_ADDRESS_LENGTH_NETBIOS;
  2053. pAddressPairInfo->AddressPair.AddressNetBIOS.Address.NetbiosNameType =
  2054. TDI_ADDRESS_NETBIOS_TYPE_UNIQUE;
  2055. memcpy( &pAddressPairInfo->AddressPair.AddressNetBIOS.Address.NetbiosName[0],
  2056. &pConnectEle->RemoteName[0],
  2057. NETBIOS_NAME_SIZE);
  2058. //
  2059. // Check for NULL (should not be NULL here since we check for states above).
  2060. //
  2061. if (pConnectEle->pLowerConnId)
  2062. {
  2063. pAddressPairInfo->AddressPair.AddressIP.Address.in_addr =
  2064. pConnectEle->pLowerConnId->SrcIpAddr;
  2065. *ppBuffer = (PVOID)pAddressPairInfo;
  2066. *pSize = sizeof(NBT_ADDRESS_PAIR_INFO);
  2067. status = STATUS_SUCCESS;
  2068. }
  2069. else
  2070. {
  2071. DbgPrint("pLowerConn NULL in pConnEle%lx, state: %lx\n", pConnectEle, pConnectEle->state);
  2072. CTEMemFree ((PVOID)pAddressPairInfo);
  2073. status = STATUS_CONNECTION_DISCONNECTED;
  2074. }
  2075. }
  2076. else
  2077. {
  2078. status = STATUS_INSUFFICIENT_RESOURCES;
  2079. }
  2080. CTESpinFree(pConnectEle,OldIrq);
  2081. }
  2082. else if (NBT_VERIFY_HANDLE2 (pClientEle, NBT_VERIFY_CLIENT, NBT_VERIFY_CLIENT_DOWN))
  2083. {
  2084. pAddressInfo = NbtAllocMem(sizeof(tADDRESS_INFO),NBT_TAG('c'));
  2085. if (pAddressInfo)
  2086. {
  2087. //
  2088. // count the clients attached to this address
  2089. // We need to spinlock the address element, which
  2090. // is why this routine is not pageable
  2091. //
  2092. pAddressInfo->ActivityCount = 0;
  2093. pAddressEle = pClientEle->pAddress;
  2094. //
  2095. // The Client can be removed from the AddressEle only under the JointLock,
  2096. // so we need to hold that while counting the Clients on htis address
  2097. //
  2098. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  2099. CTESpinLock(pAddressEle,OldIrq1);
  2100. for (p = pAddressEle->ClientHead.Flink; p != &pAddressEle->ClientHead; p = p->Flink)
  2101. {
  2102. ++pAddressInfo->ActivityCount;
  2103. }
  2104. CTESpinFree(pAddressEle,OldIrq1);
  2105. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  2106. pNameAddr = pAddressEle->pNameAddr;
  2107. IsGroup = (pNameAddr->NameTypeState & NAMETYPE_UNIQUE) ? FALSE : TRUE;
  2108. TdiBuildNetbiosAddress((PUCHAR)pNameAddr->Name, IsGroup, &pAddressInfo->NetbiosAddress);
  2109. *ppBuffer = (PVOID)pAddressInfo;
  2110. *pSize = sizeof(tADDRESS_INFO);
  2111. status = STATUS_SUCCESS;
  2112. }
  2113. else
  2114. {
  2115. status = STATUS_INSUFFICIENT_RESOURCES;
  2116. }
  2117. }
  2118. else // neither a client nor a connection context!
  2119. {
  2120. ASSERTMSG ("Nbt.NbtQueryGetAddressInfo: ERROR - Invalid Handle\n", 0);
  2121. return (STATUS_INVALID_HANDLE);
  2122. }
  2123. return status;
  2124. }
  2125. //----------------------------------------------------------------------------
  2126. NTSTATUS
  2127. NbtGetInterfaceInfo(
  2128. IN PIRP pIrp
  2129. )
  2130. /*++
  2131. Routine Description:
  2132. gets the interface to index mapping info
  2133. for all the interfaces
  2134. Arguments:
  2135. Irp - Pointer to I/O request packet to cancel.
  2136. IrpSp - pointer to current stack
  2137. Return Value:
  2138. NTSTATUS Indicates status success or failure
  2139. Notes:
  2140. Function does not pend.
  2141. --*/
  2142. {
  2143. NTSTATUS LocStatus, Status = STATUS_SUCCESS;
  2144. ULONG InfoBufferLen, MaxSize, i=0;
  2145. NETBT_INTERFACE_INFO *pInterfaceInfo;
  2146. KIRQL OldIrq;
  2147. PLIST_ENTRY pEntry,pHead;
  2148. tDEVICECONTEXT *pDeviceContext;
  2149. PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation (pIrp);
  2150. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  2151. IF_DBG(NBT_DEBUG_NAMESRV)
  2152. KdPrint(("Nbt.NbtGetInterfaceInfo: AdapterCount=<%x>\n", NbtConfig.AdapterCount));
  2153. InfoBufferLen = pIrpSp->Parameters.DeviceIoControl.OutputBufferLength;
  2154. MaxSize = (NbtConfig.AdapterCount+1)*sizeof(NETBT_ADAPTER_INDEX_MAP)+sizeof(ULONG);
  2155. if (MaxSize <= InfoBufferLen)
  2156. {
  2157. if (pInterfaceInfo = NbtAllocMem (MaxSize,NBT_TAG('P')))
  2158. {
  2159. pEntry = pHead = &NbtConfig.DeviceContexts;
  2160. while ((pEntry = pEntry->Flink) != pHead)
  2161. {
  2162. pDeviceContext = CONTAINING_RECORD(pEntry, tDEVICECONTEXT, Linkage);
  2163. CTEMemCopy (&pInterfaceInfo->Adapter[i].Name,
  2164. pDeviceContext->ExportName.Buffer,
  2165. pDeviceContext->ExportName.Length);
  2166. pInterfaceInfo->Adapter[i].Name[pDeviceContext->ExportName.Length/2] = 0;
  2167. pInterfaceInfo->Adapter[i].Index = i;
  2168. i++;
  2169. }
  2170. pInterfaceInfo->NumAdapters = i;
  2171. Status = TdiCopyBufferToMdl (pInterfaceInfo,
  2172. 0,
  2173. i*sizeof(NETBT_ADAPTER_INDEX_MAP)+sizeof(ULONG),
  2174. pIrp->MdlAddress,
  2175. 0,
  2176. (PULONG)&pIrp->IoStatus.Information);
  2177. CTEMemFree (pInterfaceInfo);
  2178. }
  2179. else
  2180. {
  2181. Status = STATUS_INSUFFICIENT_RESOURCES;
  2182. }
  2183. }
  2184. else
  2185. {
  2186. //KdPrint(("GetInterfaceInfo Buffer Overflow %x\n", pIrp));
  2187. //pIrp->IoStatus.Information = sizeof(ULONG);
  2188. Status = STATUS_BUFFER_OVERFLOW;
  2189. }
  2190. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  2191. //KdPrint(("GetInterfaceInfo exit status %x\n", Status));
  2192. return Status;
  2193. }
  2194. //----------------------------------------------------------------------------
  2195. NTSTATUS
  2196. NbtFlushEntryFromRemoteHashTable(
  2197. tNAME *pRemoteName
  2198. )
  2199. {
  2200. NTSTATUS status;
  2201. KIRQL OldIrq;
  2202. tNAMEADDR *pNameAddr = NULL;
  2203. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  2204. status = FindInHashTable (NbtConfig.pRemoteHashTbl, pRemoteName->Name, NbtConfig.pScope, &pNameAddr);
  2205. if (NT_SUCCESS (status))
  2206. {
  2207. if (pNameAddr->RefCount <= 1)
  2208. {
  2209. NBT_DEREFERENCE_NAMEADDR (pNameAddr, REF_NAME_REMOTE, TRUE);
  2210. }
  2211. else
  2212. {
  2213. status = STATUS_UNSUCCESSFUL;
  2214. }
  2215. }
  2216. else
  2217. {
  2218. status = STATUS_RESOURCE_NAME_NOT_FOUND;
  2219. }
  2220. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  2221. return (status);
  2222. }
  2223. //----------------------------------------------------------------------------
  2224. NTSTATUS
  2225. SetTcpInfo(
  2226. IN HANDLE FileHandle,
  2227. IN PVOID pInfoBuffer,
  2228. IN ULONG InfoBufferLength
  2229. )
  2230. {
  2231. IO_STATUS_BLOCK IoStatus;
  2232. HANDLE event;
  2233. BOOLEAN fAttached = FALSE;
  2234. NTSTATUS status;
  2235. CTEAttachFsp(&fAttached, REF_FSP_SET_TCP_INFO);
  2236. status = ZwCreateEvent (&event, EVENT_ALL_ACCESS, NULL, SynchronizationEvent, FALSE);
  2237. if (NT_SUCCESS(status))
  2238. {
  2239. //
  2240. // Make the actual TDI call
  2241. //
  2242. status = ZwDeviceIoControlFile (FileHandle,
  2243. event,
  2244. NULL,
  2245. NULL,
  2246. &IoStatus,
  2247. IOCTL_TCP_SET_INFORMATION_EX,
  2248. pInfoBuffer,
  2249. InfoBufferLength,
  2250. NULL,
  2251. 0);
  2252. //
  2253. // If the call pended and we were supposed to wait for completion,
  2254. // then wait.
  2255. //
  2256. if (status == STATUS_PENDING)
  2257. {
  2258. status = NtWaitForSingleObject (event, FALSE, NULL);
  2259. ASSERT(status == STATUS_SUCCESS);
  2260. }
  2261. status = ZwClose (event);
  2262. ASSERT (NT_SUCCESS(status));
  2263. status = IoStatus.Status;
  2264. }
  2265. CTEDetachFsp(fAttached, REF_FSP_SET_TCP_INFO);
  2266. return (status);
  2267. }
  2268. //----------------------------------------------------------------------------
  2269. NTSTATUS
  2270. NbtClientSetTcpInfo(
  2271. IN tCONNECTELE *pConnEle,
  2272. IN PVOID pInfoBuffer,
  2273. IN ULONG InfoBufferLength
  2274. )
  2275. /*++
  2276. Routine Description:
  2277. Sets the Tcp connection information as requested
  2278. by the client
  2279. Arguments:
  2280. pConnEle - NetBT's Connection object
  2281. pInfoBuffer - pointer to TCP_REQUEST_SET_INFORMATION_EX structure
  2282. pInfoBufferLength - length of pInfoBuffer
  2283. Return Value:
  2284. NTSTATUS Indicates status success or failure
  2285. Notes:
  2286. Function does not pend.
  2287. --*/
  2288. {
  2289. NTSTATUS status;
  2290. tLOWERCONNECTION *pLowerConn;
  2291. KIRQL OldIrq1, OldIrq2;
  2292. if (!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  2293. {
  2294. return STATUS_INVALID_HANDLE;
  2295. }
  2296. CTESpinLock(pConnEle, OldIrq1);
  2297. if ((!NBT_VERIFY_HANDLE ((pLowerConn = pConnEle->pLowerConnId), NBT_VERIFY_LOWERCONN)) ||
  2298. (pLowerConn->RefCount > 500)) // if queued for WipeOutLowerConn
  2299. {
  2300. CTESpinFree(pConnEle, OldIrq1);
  2301. return STATUS_BAD_NETWORK_PATH;
  2302. }
  2303. CTESpinLock(pLowerConn, OldIrq2);
  2304. //
  2305. // We have verified that the lower connection is up -- reference it
  2306. // so that the FileObject does not get Dereferenced by some disconnect
  2307. // from the transport
  2308. //
  2309. NBT_REFERENCE_LOWERCONN (pLowerConn, REF_LOWC_SET_TCP_INFO);
  2310. CTESpinFree(pLowerConn, OldIrq2);
  2311. CTESpinFree(pConnEle, OldIrq1);
  2312. status = SetTcpInfo (pLowerConn->FileHandle, pInfoBuffer, InfoBufferLength);
  2313. NBT_DEREFERENCE_LOWERCONN (pLowerConn, REF_LOWC_SET_TCP_INFO, FALSE);
  2314. return (status);
  2315. }
  2316. //----------------------------------------------------------------------------
  2317. NTSTATUS
  2318. NbtSetTcpInfo(
  2319. IN HANDLE FileHandle,
  2320. IN ULONG ToiId,
  2321. IN ULONG ToiType,
  2322. IN ULONG InfoBufferValue
  2323. )
  2324. {
  2325. NTSTATUS Status;
  2326. ULONG BufferLength;
  2327. TCP_REQUEST_SET_INFORMATION_EX *pTcpInfo;
  2328. TCPSocketOption *pSockOption;
  2329. BufferLength = sizeof(TCP_REQUEST_SET_INFORMATION_EX) + sizeof(TCPSocketOption);
  2330. if (!(pTcpInfo = (TCP_REQUEST_SET_INFORMATION_EX *) NbtAllocMem (BufferLength,NBT_TAG2('22'))))
  2331. {
  2332. return (STATUS_INSUFFICIENT_RESOURCES);
  2333. }
  2334. CTEZeroMemory(pTcpInfo, BufferLength);
  2335. pSockOption = (TCPSocketOption *) (&pTcpInfo->Buffer[0]);
  2336. pTcpInfo->ID.toi_entity.tei_entity = CO_TL_ENTITY;
  2337. pTcpInfo->ID.toi_class = INFO_CLASS_PROTOCOL;
  2338. pTcpInfo->BufferSize = sizeof (TCPSocketOption);
  2339. //
  2340. // Set the Configured values
  2341. //
  2342. pTcpInfo->ID.toi_id = ToiId;
  2343. pTcpInfo->ID.toi_type = ToiType;
  2344. pSockOption->tso_value = InfoBufferValue;
  2345. Status = SetTcpInfo (FileHandle, pTcpInfo, BufferLength);
  2346. if (!NT_SUCCESS(Status))
  2347. {
  2348. KdPrint (("Nbt.NbtSetTcpInfo: SetTcpInfo FAILed <%x>, Id=<0x%x>, Type=<0x%x>, Value=<%x>\n",
  2349. Status, ToiId, ToiType, InfoBufferValue));
  2350. }
  2351. CTEMemFree (pTcpInfo);
  2352. return (Status);
  2353. }
  2354. //----------------------------------------------------------------------------
  2355. NTSTATUS
  2356. NbtSetSmbBindingInfo2(
  2357. IN tDEVICECONTEXT *pDeviceContext,
  2358. IN NETBT_SMB_BIND_REQUEST *pSmbRequest
  2359. )
  2360. {
  2361. ULONG i, Operation;
  2362. PLIST_ENTRY pEntry,pHead;
  2363. KIRQL OldIrq;
  2364. ULONG NumBindings = 0;
  2365. CTEULONGLONG AddedAdapterMask = 0;
  2366. CTEULONGLONG DeletedAdapterMask = 0;
  2367. CTEULONGLONG BindListAdapterMask = 0;
  2368. CTEULONGLONG OriginalMask;
  2369. tDEVICECONTEXT *pDeviceContextBind = NULL;
  2370. if (!IsDeviceNetbiosless (pDeviceContext)) {
  2371. return (STATUS_UNSUCCESSFUL);
  2372. }
  2373. if (NULL == pSmbRequest) {
  2374. return STATUS_INVALID_PARAMETER;
  2375. }
  2376. if (pSmbRequest->RequestType == SMB_SERVER) {
  2377. OriginalMask = NbtConfig.ServerMask;
  2378. } else if (pSmbRequest->RequestType == SMB_CLIENT) {
  2379. OriginalMask = NbtConfig.ClientMask;
  2380. } else {
  2381. ASSERT(0);
  2382. return STATUS_INVALID_PARAMETER;
  2383. }
  2384. if (pSmbRequest->MultiSZBindList)
  2385. {
  2386. NTSTATUS status;
  2387. tDEVICES *pBindings = NULL;
  2388. ULONG MaxBindings;
  2389. MaxBindings = NBT_MAXIMUM_BINDINGS;
  2390. while (MaxBindings < 5000) {
  2391. status = STATUS_INSUFFICIENT_RESOURCES;
  2392. MaxBindings *= 2;
  2393. pBindings = NbtAllocMem ((sizeof(tDEVICES)+MaxBindings*sizeof(UNICODE_STRING)), NBT_TAG2('26'));
  2394. if (pBindings == NULL) {
  2395. break;
  2396. }
  2397. NumBindings = 0;
  2398. status = NbtParseMultiSzEntries (pSmbRequest->MultiSZBindList, (PVOID)(-1), MaxBindings, pBindings, &NumBindings);
  2399. if (status != STATUS_BUFFER_OVERFLOW) {
  2400. break;
  2401. }
  2402. CTEMemFree (pBindings);
  2403. pBindings = NULL;
  2404. }
  2405. if (status != STATUS_SUCCESS) {
  2406. if (pBindings) { // NbtParseMultiSzEntries can return failure other than STATUS_BUFFER_OVERFLOW
  2407. CTEMemFree (pBindings);
  2408. }
  2409. KdPrint(("Nbt.NbtSetSmbBindingInfo[STATUS_INSUFFICIENT_RESOURCES]: MaxBindings = <%d>\n",
  2410. MaxBindings));
  2411. return status;
  2412. }
  2413. ASSERT(pBindings);
  2414. //
  2415. // First, get the complete list of all bindings
  2416. //
  2417. for (i=0; i<NumBindings; i++)
  2418. {
  2419. if (pDeviceContextBind = NbtFindAndReferenceDevice (&pBindings->Names[i], FALSE))
  2420. {
  2421. BindListAdapterMask |= pDeviceContextBind->AdapterMask;
  2422. NBT_DEREFERENCE_DEVICE (pDeviceContextBind, REF_DEV_FIND_REF, FALSE);
  2423. }
  2424. }
  2425. CTEMemFree (pBindings);
  2426. }
  2427. else if (pSmbRequest->pDeviceName)
  2428. {
  2429. KdPrint (("Nbt.NbtSetSmbBindingInfo[WARNING]: NULL MultiSZBindList string!\n"));
  2430. BindListAdapterMask = OriginalMask;
  2431. if (pDeviceContextBind = NbtFindAndReferenceDevice (pSmbRequest->pDeviceName, FALSE))
  2432. {
  2433. switch (pSmbRequest->PnPOpCode)
  2434. {
  2435. case (TDI_PNP_OP_ADD):
  2436. {
  2437. BindListAdapterMask |= pDeviceContextBind->AdapterMask;
  2438. break;
  2439. }
  2440. case (TDI_PNP_OP_DEL):
  2441. {
  2442. BindListAdapterMask &= (~pDeviceContextBind->AdapterMask);
  2443. break;
  2444. }
  2445. default:
  2446. {
  2447. break;
  2448. }
  2449. }
  2450. NBT_DEREFERENCE_DEVICE (pDeviceContextBind, REF_DEV_FIND_REF, FALSE);
  2451. }
  2452. else
  2453. {
  2454. return (STATUS_SUCCESS);
  2455. }
  2456. }
  2457. else
  2458. {
  2459. ASSERTMSG ("Nbt.NbtSetSmbBindingInfo[ERROE]: NULL MultiSZBindList and NULL pDeviceName!\n", 0);
  2460. return (STATUS_UNSUCCESSFUL);
  2461. }
  2462. IF_DBG(NBT_DEBUG_PNP_POWER)
  2463. KdPrint (("Nbt.NbtSetSmbBindingInfo: PnPOpCode=<%x>, Bindings=<%d>, BindMask=[%lx:%lx]==>[%lx:%lx]\n",
  2464. pSmbRequest->PnPOpCode, NumBindings, OriginalMask, BindListAdapterMask));
  2465. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  2466. AddedAdapterMask = BindListAdapterMask & (~OriginalMask); // Devices Added
  2467. DeletedAdapterMask = OriginalMask & (~BindListAdapterMask); // Devices Removed
  2468. if ((!AddedAdapterMask) && (!DeletedAdapterMask))
  2469. {
  2470. //
  2471. // If there are no adapters to be added or deleted, just return
  2472. //
  2473. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  2474. return (STATUS_SUCCESS);
  2475. }
  2476. if (pSmbRequest->RequestType == SMB_SERVER) {
  2477. NbtConfig.ServerMask = BindListAdapterMask;
  2478. } else if (pSmbRequest->RequestType == SMB_CLIENT) {
  2479. NbtConfig.ClientMask = BindListAdapterMask;
  2480. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  2481. return STATUS_SUCCESS;
  2482. } else {
  2483. ASSERT(0);
  2484. }
  2485. pEntry = pHead = &NbtConfig.DeviceContexts;
  2486. while ((pEntry = pEntry->Flink) != pHead)
  2487. {
  2488. pDeviceContextBind = CONTAINING_RECORD(pEntry, tDEVICECONTEXT, Linkage);
  2489. if (pDeviceContext->IPInterfaceContext == (ULONG)-1) // For Cluster devices, etc
  2490. {
  2491. continue;
  2492. }
  2493. if (AddedAdapterMask & pDeviceContextBind->AdapterMask)
  2494. {
  2495. AddedAdapterMask &= ~(pDeviceContextBind->AdapterMask);
  2496. Operation = AO_OPTION_ADD_IFLIST;
  2497. }
  2498. else if (DeletedAdapterMask & pDeviceContextBind->AdapterMask)
  2499. {
  2500. DeletedAdapterMask &= ~(pDeviceContextBind->AdapterMask);
  2501. Operation = AO_OPTION_DEL_IFLIST;
  2502. }
  2503. else
  2504. {
  2505. continue;
  2506. }
  2507. NBT_REFERENCE_DEVICE (pDeviceContextBind, REF_DEV_FIND_REF, TRUE);
  2508. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  2509. IF_DBG(NBT_DEBUG_PNP_POWER)
  2510. KdPrint(("Nbt.NbtSetSmbBindingInfo: %sing Device=%wZ\n",
  2511. (Operation == AO_OPTION_ADD_IFLIST ? "ADD" : "REMOV"), &pDeviceContextBind->BindName));
  2512. //
  2513. // Set the Session port info
  2514. //
  2515. if (pDeviceContext->hSession)
  2516. {
  2517. NbtSetTcpInfo (pDeviceContext->hSession,
  2518. Operation,
  2519. INFO_TYPE_ADDRESS_OBJECT,
  2520. pDeviceContextBind->IPInterfaceContext);
  2521. }
  2522. //
  2523. // Now, set the same for the Datagram port
  2524. //
  2525. if ((pDeviceContext->pFileObjects) &&
  2526. (pDeviceContext->pFileObjects->hDgram))
  2527. {
  2528. NbtSetTcpInfo (pDeviceContext->pFileObjects->hDgram,
  2529. Operation,
  2530. INFO_TYPE_ADDRESS_OBJECT,
  2531. pDeviceContextBind->IPInterfaceContext);
  2532. }
  2533. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  2534. NBT_DEREFERENCE_DEVICE (pDeviceContextBind, REF_DEV_FIND_REF, TRUE);
  2535. //
  2536. // Set to restart from the beginning
  2537. //
  2538. pEntry = &NbtConfig.DeviceContexts;
  2539. }
  2540. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  2541. return STATUS_SUCCESS;
  2542. }
  2543. //----------------------------------------------------------------------------
  2544. NTSTATUS
  2545. NbtSetSmbBindingInfo(
  2546. IN tDEVICECONTEXT *pDeviceContext,
  2547. IN PIRP pIrp,
  2548. IN PIO_STACK_LOCATION pIrpSp
  2549. )
  2550. {
  2551. NETBT_SMB_BIND_REQUEST *pSmbRequest = (PNETBT_SMB_BIND_REQUEST)pIrp->AssociatedIrp.SystemBuffer;
  2552. PWSTR pBindListCache = NULL;
  2553. PWSTR pBindList = NULL;
  2554. PWSTR pOldBindList = NULL;
  2555. PWSTR *pTarget = NULL;
  2556. ULONG uInputLength = 0;
  2557. ULONG uLength = 0;
  2558. NTSTATUS status = STATUS_SUCCESS;
  2559. uInputLength = pIrpSp->Parameters.DeviceIoControl.InputBufferLength;
  2560. if (!(IsDeviceNetbiosless (pDeviceContext)) ||
  2561. (!pSmbRequest) ||
  2562. (uInputLength < sizeof(NETBT_SMB_BIND_REQUEST)))
  2563. {
  2564. KdPrint(("Nbt.NbtSetSmbBindingInfo: ERROR: pSmbRequest=<%p>, pDevice=<%p>\n",
  2565. pSmbRequest, pDeviceContext));
  2566. return (STATUS_UNSUCCESSFUL);
  2567. }
  2568. if (pIrp->RequestorMode != KernelMode) {
  2569. return STATUS_ACCESS_DENIED;
  2570. }
  2571. status = NbtSetSmbBindingInfo2 (pDeviceContext, pSmbRequest);
  2572. if (NT_SUCCESS(status) && pSmbRequest->MultiSZBindList) {
  2573. //
  2574. // Cache the binding info
  2575. //
  2576. pBindList = pSmbRequest->MultiSZBindList;
  2577. uInputLength = 0;
  2578. while (*pBindList) {
  2579. uLength = wcslen (pBindList) + 1;
  2580. uInputLength += uLength;
  2581. pBindList += uLength;
  2582. }
  2583. uInputLength++;
  2584. uInputLength *= sizeof(WCHAR);
  2585. pBindList = pSmbRequest->MultiSZBindList;
  2586. if (pSmbRequest->RequestType == SMB_SERVER) {
  2587. pTarget = &NbtConfig.pServerBindings;
  2588. } else if (pSmbRequest->RequestType == SMB_CLIENT) {
  2589. pTarget = &NbtConfig.pClientBindings;
  2590. } else {
  2591. ASSERT(0);
  2592. }
  2593. pBindListCache = NbtAllocMem (uInputLength, NBT_TAG2('27'));
  2594. if (NULL != pBindListCache) {
  2595. RtlCopyMemory (pBindListCache, pBindList, uInputLength);
  2596. pOldBindList = InterlockedExchangePointer (pTarget, pBindListCache);
  2597. //
  2598. // Free the old copy if any
  2599. //
  2600. if (NULL != pOldBindList) {
  2601. CTEFreeMem (pOldBindList);
  2602. pOldBindList = NULL;
  2603. }
  2604. }
  2605. }
  2606. return status;
  2607. }
  2608. //----------------------------------------------------------------------------
  2609. NTSTATUS
  2610. DispatchIoctls(
  2611. IN tDEVICECONTEXT *pDeviceContext,
  2612. IN PIRP pIrp,
  2613. IN PIO_STACK_LOCATION pIrpSp
  2614. )
  2615. /*++
  2616. Routine Description:
  2617. This Routine handles calling the OS independent routine depending on
  2618. the Ioctl passed in.
  2619. Arguments:
  2620. pIrp - a ptr to an IRP
  2621. Return Value:
  2622. NTSTATUS - status of the request
  2623. --*/
  2624. {
  2625. NTSTATUS status=STATUS_UNSUCCESSFUL;
  2626. ULONG ControlCode;
  2627. ULONG Size;
  2628. PVOID pBuffer;
  2629. ControlCode = pIrpSp->Parameters.DeviceIoControl.IoControlCode;
  2630. switch (ControlCode)
  2631. {
  2632. case IOCTL_NETBT_REREAD_REGISTRY:
  2633. {
  2634. status = NTReReadRegistry(pDeviceContext);
  2635. break;
  2636. }
  2637. case IOCTL_NETBT_ENABLE_EXTENDED_ADDR:
  2638. {
  2639. //
  2640. // Enable extended addressing - pass up IP addrs on Datagram Recvs.
  2641. //
  2642. PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation (pIrp);
  2643. tCLIENTELE *pClientEle = (tCLIENTELE *)pIrpSp->FileObject->FsContext;
  2644. if (!NBT_VERIFY_HANDLE2 (pClientEle, NBT_VERIFY_CLIENT, NBT_VERIFY_CLIENT_DOWN))
  2645. {
  2646. //
  2647. // To make the stresser (devctl.exe) happy. [JRuan 12/18/2000]
  2648. //
  2649. // ASSERTMSG ("Nbt.DispatchIoctls: ERROR - Invalid Address Handle\n", 0);
  2650. status = STATUS_INVALID_HANDLE;
  2651. }
  2652. else if (pIrpSp->FileObject->FsContext2 != (PVOID)NBT_ADDRESS_TYPE)
  2653. {
  2654. status = STATUS_INVALID_ADDRESS;
  2655. }
  2656. else
  2657. {
  2658. pClientEle->ExtendedAddress = TRUE;
  2659. status = STATUS_SUCCESS;
  2660. }
  2661. break;
  2662. }
  2663. case IOCTL_NETBT_DISABLE_EXTENDED_ADDR:
  2664. {
  2665. //
  2666. // Disable extended addressing - dont pass up IP addrs on Datagram Recvs.
  2667. //
  2668. PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation (pIrp);
  2669. tCLIENTELE *pClientEle = (tCLIENTELE *)pIrpSp->FileObject->FsContext;
  2670. if (!NBT_VERIFY_HANDLE2 (pClientEle, NBT_VERIFY_CLIENT, NBT_VERIFY_CLIENT_DOWN))
  2671. {
  2672. // ASSERTMSG ("Nbt.DispatchIoctls: ERROR - Invalid Address Handle\n", 0);
  2673. status = STATUS_INVALID_HANDLE;
  2674. }
  2675. else if (pIrpSp->FileObject->FsContext2 != (PVOID)NBT_ADDRESS_TYPE)
  2676. {
  2677. status = STATUS_INVALID_ADDRESS;
  2678. }
  2679. else
  2680. {
  2681. pClientEle->ExtendedAddress = FALSE;
  2682. status = STATUS_SUCCESS;
  2683. }
  2684. break;
  2685. }
  2686. case IOCTL_NETBT_GET_WINS_ADDR:
  2687. {
  2688. if ((pIrp->MdlAddress) &&
  2689. ((Size = MmGetMdlByteCount (pIrp->MdlAddress)) >= sizeof(tWINS_ADDRESSES)) &&
  2690. (pBuffer = (PVOID) MmGetSystemAddressForMdlSafe(pIrp->MdlAddress, HighPagePriority)))
  2691. {
  2692. status = STATUS_SUCCESS;
  2693. if (Size >= sizeof(tWINS_NODE_INFO))
  2694. {
  2695. tWINS_NODE_INFO UNALIGNED *pWinsBuffer = (tWINS_NODE_INFO *) pBuffer;
  2696. CTEMemCopy (&pWinsBuffer->AllNameServers,
  2697. pDeviceContext->lAllNameServers,
  2698. (sizeof(tIPADDRESS)*(2+MAX_NUM_OTHER_NAME_SERVERS)));
  2699. pWinsBuffer->NumOtherServers = pDeviceContext->lNumOtherServers;
  2700. pWinsBuffer->LastResponsive = pDeviceContext->lLastResponsive;
  2701. pWinsBuffer->NetbiosEnabled = pDeviceContext->NetbiosEnabled;
  2702. pWinsBuffer->NodeType = NodeType;
  2703. pIrp->IoStatus.Information = sizeof(tWINS_NODE_INFO);
  2704. }
  2705. else
  2706. {
  2707. tWINS_ADDRESSES UNALIGNED *pWinsBuffer = (tWINS_ADDRESSES *) pBuffer;
  2708. pWinsBuffer->PrimaryWinsServer = pDeviceContext->lNameServerAddress;
  2709. pWinsBuffer->BackupWinsServer = pDeviceContext->lBackupServer;
  2710. pIrp->IoStatus.Information = sizeof(tWINS_ADDRESSES);
  2711. }
  2712. }
  2713. break;
  2714. }
  2715. case IOCTL_NETBT_GET_IP_ADDRS:
  2716. {
  2717. status = GetIpAddrs (pDeviceContext, pIrp);
  2718. break;
  2719. }
  2720. case IOCTL_NETBT_GET_IP_SUBNET:
  2721. {
  2722. ULONG Length;
  2723. PULONG pIpAddr;
  2724. //
  2725. // return this devicecontext's ip address and all the other
  2726. // ip addrs after it.
  2727. //
  2728. if (pIrp->MdlAddress)
  2729. {
  2730. Length = MmGetMdlByteCount( pIrp->MdlAddress );
  2731. if (Length < 2*sizeof(ULONG))
  2732. {
  2733. status = STATUS_BUFFER_OVERFLOW;
  2734. }
  2735. else if (pIpAddr = (PULONG )MmGetSystemAddressForMdlSafe(pIrp->MdlAddress, HighPagePriority))
  2736. {
  2737. //
  2738. // Put this adapter first in the list
  2739. //
  2740. CTEMemCopy(pIpAddr, &pDeviceContext->AssignedIpAddress, sizeof(pIpAddr[0]));
  2741. pIpAddr++;
  2742. CTEMemCopy(pIpAddr, &pDeviceContext->SubnetMask, sizeof(pIpAddr[0]));
  2743. pIpAddr++;
  2744. if (Length >= 3*sizeof(ULONG))
  2745. {
  2746. CTEMemCopy(pIpAddr, &pDeviceContext->DeviceType, sizeof(pIpAddr[0]));
  2747. }
  2748. status = STATUS_SUCCESS;
  2749. }
  2750. }
  2751. break;
  2752. }
  2753. //
  2754. // The following Ioctl is used mainly by the Server service
  2755. //
  2756. case IOCTL_NETBT_SET_TCP_CONNECTION_INFO:
  2757. {
  2758. status = NbtClientSetTcpInfo ((tCONNECTELE *) pIrpSp->FileObject->FsContext,
  2759. pIrp->AssociatedIrp.SystemBuffer,
  2760. pIrpSp->Parameters.DeviceIoControl.InputBufferLength);
  2761. break;
  2762. }
  2763. //
  2764. // The following Ioctls are used mainly by NbtStat.exe for diagnostic purposes
  2765. //
  2766. case IOCTL_NETBT_GET_INTERFACE_INFO:
  2767. {
  2768. status = NbtGetInterfaceInfo (pIrp);
  2769. break;
  2770. }
  2771. case IOCTL_NETBT_PURGE_CACHE:
  2772. {
  2773. DelayedNbtResyncRemoteCache (NULL, NULL, NULL, NULL);
  2774. status = STATUS_SUCCESS;
  2775. break;
  2776. }
  2777. case IOCTL_NETBT_GET_CONNECTIONS:
  2778. {
  2779. if (pIrp->MdlAddress)
  2780. {
  2781. Size = MmGetMdlByteCount( pIrp->MdlAddress ) ;
  2782. // return an array of netbios names that are registered
  2783. status = NbtQueryConnectionList (pDeviceContext, &pBuffer, &Size);
  2784. }
  2785. break;
  2786. }
  2787. case IOCTL_NETBT_ADAPTER_STATUS:
  2788. {
  2789. if (pIrp->MdlAddress)
  2790. {
  2791. PIO_STACK_LOCATION pIrpSp;
  2792. tIPANDNAMEINFO *pIpAndNameInfo;
  2793. PCHAR pName;
  2794. ULONG lNameType;
  2795. ULONG NameLen;
  2796. ULONG IpAddrsList[2];
  2797. tIPADDRESS *pIpAddrs = NULL;
  2798. tDGRAM_SEND_TRACKING *pTracker;
  2799. //
  2800. // in case the call results in a name query on the wire...
  2801. //
  2802. IoMarkIrpPending(pIrp);
  2803. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  2804. pIpAndNameInfo = pIrp->AssociatedIrp.SystemBuffer;
  2805. NameLen = pIrpSp->Parameters.DeviceIoControl.InputBufferLength
  2806. - FIELD_OFFSET(tIPANDNAMEINFO,NetbiosAddress);
  2807. //
  2808. // Bug# 125288+120947: Make sure the data passed in + the Address type are good
  2809. //
  2810. if ((pIpAndNameInfo) &&
  2811. (pIrpSp->Parameters.DeviceIoControl.InputBufferLength >= sizeof(tIPANDNAMEINFO)))
  2812. {
  2813. TDI_ADDRESS_NETBT_INTERNAL TdiAddr;
  2814. // this routine gets a ptr to the netbios name out of the wierd
  2815. // TDI address syntax.
  2816. status = GetNetBiosNameFromTransportAddress(
  2817. (PTRANSPORT_ADDRESS) &pIpAndNameInfo->NetbiosAddress,
  2818. NameLen, &TdiAddr);
  2819. pName = TdiAddr.OEMRemoteName.Buffer;
  2820. NameLen = TdiAddr.OEMRemoteName.Length;
  2821. lNameType = TdiAddr.NameType;
  2822. }
  2823. if (NT_SUCCESS(status) &&
  2824. (lNameType == TDI_ADDRESS_NETBIOS_TYPE_UNIQUE) &&
  2825. (NameLen == NETBIOS_NAME_SIZE))
  2826. {
  2827. //
  2828. // Nbtstat sends down * in the first byte on Nbtstat -A <IP address>
  2829. // Make sure we let that case go ahead.
  2830. //
  2831. if (!pDeviceContext->NetbiosEnabled) {
  2832. status = STATUS_INVALID_DEVICE_STATE;
  2833. }
  2834. else if ((pName[0] == '*') && (pIpAndNameInfo->IpAddress == 0))
  2835. {
  2836. status = STATUS_BAD_NETWORK_PATH;
  2837. }
  2838. else if (NT_SUCCESS (status = GetTracker(&pTracker, NBT_TRACKER_ADAPTER_STATUS)))
  2839. {
  2840. if (pIpAndNameInfo->IpAddress)
  2841. {
  2842. IpAddrsList[0] = pIpAndNameInfo->IpAddress;
  2843. IpAddrsList[1] = 0;
  2844. pIpAddrs = IpAddrsList;
  2845. }
  2846. pTracker->ClientContext = pIrp;
  2847. status = NbtSendNodeStatus(pDeviceContext,
  2848. pName,
  2849. pIpAddrs,
  2850. pTracker,
  2851. CopyNodeStatusResponseCompletion);
  2852. // only complete the irp (below) for failure status's
  2853. if (status == STATUS_PENDING)
  2854. {
  2855. return(status);
  2856. }
  2857. //
  2858. // We cannot have a Success status returned here!
  2859. //
  2860. if (status == STATUS_SUCCESS)
  2861. {
  2862. ASSERT (0);
  2863. status = STATUS_UNSUCCESSFUL;
  2864. }
  2865. FreeTracker (pTracker, RELINK_TRACKER);
  2866. }
  2867. }
  2868. else if (NT_SUCCESS(status))
  2869. {
  2870. status = STATUS_INVALID_PARAMETER; // The NameType or NameLen must be wrong!
  2871. }
  2872. // the request has been satisfied, so unmark the pending
  2873. // since we will return the irp below
  2874. //
  2875. pIrpSp->Control &= ~SL_PENDING_RETURNED;
  2876. }
  2877. break;
  2878. }
  2879. case IOCTL_NETBT_GET_REMOTE_NAMES:
  2880. {
  2881. if (pIrp->MdlAddress)
  2882. {
  2883. Size = MmGetMdlByteCount( pIrp->MdlAddress ) ;
  2884. // return an array of netbios names that are registered
  2885. status = NbtQueryAdapterStatus(pDeviceContext, &pBuffer, &Size, NBT_REMOTE);
  2886. }
  2887. break;
  2888. }
  2889. case IOCTL_NETBT_GET_BCAST_NAMES:
  2890. {
  2891. if (pIrp->MdlAddress)
  2892. {
  2893. Size = MmGetMdlByteCount( pIrp->MdlAddress ) ;
  2894. // return an array of netbios names that are registered
  2895. status = NbtQueryBcastVsWins(pDeviceContext,&pBuffer,&Size);
  2896. }
  2897. break;
  2898. }
  2899. case IOCTL_NETBT_NAME_RELEASE_REFRESH:
  2900. {
  2901. status = ReRegisterLocalNames (NULL, TRUE);
  2902. break;
  2903. }
  2904. //
  2905. // The following Ioctls are used by the Cluster code
  2906. //
  2907. case IOCTL_NETBT_ADD_INTERFACE:
  2908. {
  2909. //
  2910. // Creates a dummy devicecontext which can be primed by the layer above
  2911. // with a DHCP address. This is to support multiple IP addresses per adapter
  2912. // for the Clusters group; but can be used by any module that needs support
  2913. // for more than one IP address per adapter. This private interface hides the
  2914. // devices thus created from the setup/regisrty and that is fine since the
  2915. // component (say, the clusters client) takes the responsibility for ensuring
  2916. // that the server (above us) comes to know of this new device.
  2917. //
  2918. PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation (pIrp);
  2919. pBuffer = pIrp->AssociatedIrp.SystemBuffer;
  2920. Size = pIrpSp->Parameters.DeviceIoControl.OutputBufferLength;
  2921. //
  2922. // return the export string created.
  2923. //
  2924. status = NbtAddNewInterface(pIrp, pBuffer, Size);
  2925. IF_DBG(NBT_DEBUG_PNP_POWER)
  2926. KdPrint(("Nbt.DispatchIoctls: ADD_INTERFACE -- status=<%x>\n", status));
  2927. NTIoComplete(pIrp,status,(ULONG)-1);
  2928. return status;
  2929. }
  2930. case IOCTL_NETBT_DELETE_INTERFACE:
  2931. {
  2932. //
  2933. // Dereference this device for the Reference taken in the
  2934. // Dispatch routine so that the cleanup can proceed properly
  2935. //
  2936. NBT_DEREFERENCE_DEVICE (pDeviceContext, REF_DEV_DISPATCH, FALSE);
  2937. if (pDeviceContext->DeviceType == NBT_DEVICE_CLUSTER)
  2938. {
  2939. //
  2940. // Delete the device this came down on..
  2941. //
  2942. status = NbtDestroyDevice (pDeviceContext, TRUE);
  2943. }
  2944. else
  2945. {
  2946. KdPrint(("Nbt.DispatchIoctls: ERROR: DELETE_INTERFACE <%x>, pDevice=<%p>\n",
  2947. status, pDeviceContext));
  2948. }
  2949. break;
  2950. }
  2951. case IOCTL_NETBT_QUERY_INTERFACE_INSTANCE:
  2952. {
  2953. PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation (pIrp);
  2954. //
  2955. // Validate input/output buffer size
  2956. //
  2957. Size = pIrpSp->Parameters.DeviceIoControl.OutputBufferLength;
  2958. if (Size < sizeof(NETBT_ADD_DEL_IF))
  2959. {
  2960. // IF_DBG(NBT_DEBUG_PNP_POWER)
  2961. KdPrint(("Nbt.DispatchIoctls: QUERY_INTERFACE_INSTANCE: Output buffer too small\n"));
  2962. status = STATUS_INVALID_PARAMETER;
  2963. }
  2964. else
  2965. {
  2966. PNETBT_ADD_DEL_IF pAddDelIf = (PNETBT_ADD_DEL_IF)pIrp->AssociatedIrp.SystemBuffer;
  2967. status = STATUS_SUCCESS;
  2968. ASSERT(pDeviceContext->DeviceType == NBT_DEVICE_CLUSTER);
  2969. pAddDelIf->InstanceNumber = pDeviceContext->InstanceNumber;
  2970. pAddDelIf->Status = status;
  2971. pIrp->IoStatus.Information = sizeof(NETBT_ADD_DEL_IF);
  2972. NTIoComplete(pIrp,status,(ULONG)-1);
  2973. return status;
  2974. }
  2975. break;
  2976. }
  2977. case IOCTL_NETBT_NEW_IPADDRESS:
  2978. {
  2979. tNEW_IP_ADDRESS *pNewAddress = (tNEW_IP_ADDRESS *)pIrp->AssociatedIrp.SystemBuffer;
  2980. status = STATUS_UNSUCCESSFUL;
  2981. //
  2982. // Bug# 202320: Make sure the data passed in is valid
  2983. //
  2984. if ((pDeviceContext->DeviceType == NBT_DEVICE_CLUSTER) &&
  2985. (pNewAddress) &&
  2986. (pIrpSp->Parameters.DeviceIoControl.InputBufferLength >= sizeof(tNEW_IP_ADDRESS)))
  2987. {
  2988. KdPrint (("Nbt.DispatchIoctls: Calling NbtNewDhcpAddress on ClusterDevice <%x>!\n",
  2989. pDeviceContext));
  2990. pDeviceContext->AssignedIpAddress = ntohl (pNewAddress->IpAddress);
  2991. status = NbtNewDhcpAddress (pDeviceContext, pNewAddress->IpAddress, pNewAddress->SubnetMask);
  2992. ASSERT (pDeviceContext->AssignedIpAddress == pDeviceContext->IpAddress);
  2993. IF_DBG(NBT_DEBUG_PNP_POWER)
  2994. KdPrint (("Nbt.DispatchIoctls: NEW_IPADDRESS, status=<%x>, IP=<%x>, pDevice=<%p>\n",
  2995. status, pNewAddress->IpAddress, pDeviceContext));
  2996. }
  2997. else
  2998. {
  2999. KdPrint(("Nbt.DispatchIoctls: ERROR: NEW_IPADDRESS status=<%x>, pDevice=<%p>\n",
  3000. status, pDeviceContext));
  3001. }
  3002. break;
  3003. }
  3004. case IOCTL_NETBT_SET_WINS_ADDRESS:
  3005. {
  3006. //
  3007. // Sets the WINS addresses for a dynamic adapter
  3008. //
  3009. PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation (pIrp);
  3010. //
  3011. // Validate input/output buffer size
  3012. //
  3013. Size = pIrpSp->Parameters.DeviceIoControl.InputBufferLength;
  3014. if (Size < sizeof(NETBT_SET_WINS_ADDR))
  3015. {
  3016. // IF_DBG(NBT_DEBUG_PNP_POWER)
  3017. KdPrint(("NbtSetWinsAddr: Input buffer too small for struct\n"));
  3018. status = STATUS_INVALID_PARAMETER;
  3019. }
  3020. else if (pDeviceContext->DeviceType == NBT_DEVICE_CLUSTER)
  3021. {
  3022. PNETBT_SET_WINS_ADDR pSetWinsAddr = (PNETBT_SET_WINS_ADDR)pIrp->AssociatedIrp.SystemBuffer;
  3023. status = STATUS_SUCCESS;
  3024. pDeviceContext->lNameServerAddress = pSetWinsAddr->PrimaryWinsAddr;
  3025. pDeviceContext->lBackupServer = pSetWinsAddr->SecondaryWinsAddr;
  3026. pDeviceContext->SwitchedToBackup = 0;
  3027. pDeviceContext->RefreshToBackup = 0;
  3028. pSetWinsAddr->Status = status;
  3029. pIrp->IoStatus.Information = 0; // We are not copying any data to the Output buffers
  3030. NTIoComplete (pIrp,status,(ULONG)-1);
  3031. return status;
  3032. }
  3033. else
  3034. {
  3035. KdPrint(("Nbt.DispatchIoctls: ERROR: SET_WINS_ADDRESS <%x>, pDevice=<%p>\n",
  3036. status, pDeviceContext));
  3037. }
  3038. break;
  3039. }
  3040. //
  3041. // The following Ioctls are used by the LmHost Services Dll (lmhSvc.dll) to
  3042. // help NetBT ping addresses in user space or resolve names in Dns
  3043. //
  3044. case IOCTL_NETBT_DNS_NAME_RESOLVE:
  3045. {
  3046. if (pIrp->MdlAddress)
  3047. {
  3048. Size = MmGetMdlByteCount( pIrp->MdlAddress ) ;
  3049. if (Size < sizeof (tIPADDR_BUFFER_DNS))
  3050. {
  3051. // IF_DBG(NBT_DEBUG_PNP_POWER)
  3052. KdPrint(("Nbt.DnsNameResolve: Input buffer size=<%d> < tIPADDR_BUFFER_DNS=<%d>\n",
  3053. Size, sizeof (tIPADDR_BUFFER_DNS)));
  3054. status = STATUS_INVALID_PARAMETER;
  3055. }
  3056. else if (pBuffer = MmGetSystemAddressForMdlSafe (pIrp->MdlAddress, HighPagePriority))
  3057. {
  3058. // return an array of netbios names that are registered
  3059. status = NtProcessLmHSvcIrp (pDeviceContext,pBuffer,Size,pIrp,NBT_RESOLVE_WITH_DNS);
  3060. return(status);
  3061. }
  3062. }
  3063. break;
  3064. }
  3065. case IOCTL_NETBT_CHECK_IP_ADDR:
  3066. {
  3067. IF_DBG(NBT_DEBUG_NAMESRV)
  3068. KdPrint(("Ioctl Value is %X (IOCTL_NETBT_CHECK_IP_ADDR)\n",ControlCode));
  3069. if (pIrp->MdlAddress)
  3070. {
  3071. Size = MmGetMdlByteCount( pIrp->MdlAddress ) ;
  3072. if (Size < sizeof (tIPADDR_BUFFER_DNS))
  3073. {
  3074. // IF_DBG(NBT_DEBUG_PNP_POWER)
  3075. KdPrint(("Nbt.CheckIpAddr: Input buffer size=<%d> < tIPADDR_BUFFER_DNS=<%d>\n",
  3076. Size, sizeof (tIPADDR_BUFFER_DNS)));
  3077. status = STATUS_INVALID_PARAMETER;
  3078. }
  3079. else if (pBuffer = MmGetSystemAddressForMdlSafe (pIrp->MdlAddress, HighPagePriority))
  3080. {
  3081. // return an array of netbios names that are registered
  3082. status = NtProcessLmHSvcIrp (pDeviceContext,pBuffer,Size,pIrp,NBT_PING_IP_ADDRS);
  3083. return(status);
  3084. }
  3085. }
  3086. break;
  3087. }
  3088. //
  3089. // The following Ioctl is used by the DNS resolver to resolve names through Wins/Bcast
  3090. //
  3091. case IOCTL_NETBT_FIND_NAME:
  3092. {
  3093. tIPADDR_BUFFER *pIpAddrBuffer;
  3094. PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation (pIrp);
  3095. //
  3096. // in case the call results in a name query on the wire...
  3097. //
  3098. IoMarkIrpPending(pIrp);
  3099. //
  3100. // Bug# 120957: Make sure the data passed in + the Address type are good
  3101. // Bug# 234627: Verify non-NULL MdlAddress ptr
  3102. //
  3103. pIpAddrBuffer = pIrp->AssociatedIrp.SystemBuffer;
  3104. if ((pIrp->MdlAddress) && // to copy the data back!
  3105. (pIpAddrBuffer) &&
  3106. (pIrpSp->Parameters.DeviceIoControl.InputBufferLength >= sizeof(tIPADDR_BUFFER)))
  3107. {
  3108. status = NbtQueryFindName((PTDI_CONNECTION_INFORMATION)pIpAddrBuffer,
  3109. pDeviceContext,
  3110. pIrp,
  3111. TRUE);
  3112. }
  3113. if (status == STATUS_PENDING)
  3114. {
  3115. return(status);
  3116. }
  3117. // the request has been satisfied, so unmark the pending
  3118. // since we will return the irp below
  3119. //
  3120. pIrpSp->Control &= ~SL_PENDING_RETURNED;
  3121. break;
  3122. }
  3123. //
  3124. // The following Ioctls are used by the Wins server
  3125. //
  3126. case IOCTL_NETBT_WINS_RCV:
  3127. {
  3128. tWINS_INFO *pWins = pIrpSp->FileObject->FsContext;
  3129. if ((pDeviceContext == pWinsDeviceContext) &&
  3130. (NBT_VERIFY_HANDLE (pWins, NBT_VERIFY_WINS_ACTIVE)))
  3131. {
  3132. if (pIrp->MdlAddress)
  3133. {
  3134. status = RcvIrpFromWins(pIrp);
  3135. return(status);
  3136. }
  3137. }
  3138. else
  3139. {
  3140. status = STATUS_INVALID_HANDLE;
  3141. }
  3142. break;
  3143. }
  3144. case IOCTL_NETBT_WINS_SEND:
  3145. {
  3146. tWINS_INFO *pWins = pIrpSp->FileObject->FsContext;
  3147. BOOLEAN MustSend;
  3148. if ((pDeviceContext == pWinsDeviceContext) &&
  3149. (NBT_VERIFY_HANDLE (pWins, NBT_VERIFY_WINS_ACTIVE)))
  3150. {
  3151. if ((pIrp->MdlAddress) && !(IsListEmpty(&NbtConfig.DeviceContexts)))
  3152. {
  3153. status = WinsSendDatagram (pDeviceContext,pIrp,(MustSend = FALSE));
  3154. return(status);
  3155. }
  3156. }
  3157. else
  3158. {
  3159. status = STATUS_INVALID_HANDLE;
  3160. }
  3161. break;
  3162. }
  3163. case IOCTL_NETBT_WINS_SET_INFO:
  3164. {
  3165. tWINS_INFO *pWins = pIrpSp->FileObject->FsContext;
  3166. tWINS_SET_INFO *pWinsSetInfo = (tWINS_SET_INFO *) pIrp->AssociatedIrp.SystemBuffer;
  3167. if ((pDeviceContext == pWinsDeviceContext) &&
  3168. (NBT_VERIFY_HANDLE (pWins, NBT_VERIFY_WINS_ACTIVE)))
  3169. {
  3170. //
  3171. // Validate input/output buffer size
  3172. //
  3173. Size = pIrpSp->Parameters.DeviceIoControl.InputBufferLength;
  3174. if (Size >= sizeof(tWINS_SET_INFO))
  3175. {
  3176. status = WinsSetInformation (pWins, pWinsSetInfo);
  3177. }
  3178. else
  3179. {
  3180. IF_DBG(NBT_DEBUG_WINS)
  3181. KdPrint(("Nbt.DispatchIoctls[IOCTL_NETBT_WINS_SET_INFO]: Input buffer too small\n"));
  3182. status = STATUS_INVALID_PARAMETER;
  3183. }
  3184. }
  3185. else
  3186. {
  3187. status = STATUS_INVALID_HANDLE;
  3188. }
  3189. break;
  3190. }
  3191. //
  3192. // The following Ioctl is used by the Remote boot code
  3193. //
  3194. case IOCTL_NETBT_ADD_TO_REMOTE_TABLE:
  3195. {
  3196. tREMOTE_CACHE *pRemoteEntry = (tREMOTE_CACHE *) pIrp->AssociatedIrp.SystemBuffer;
  3197. PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation (pIrp);
  3198. //
  3199. // Validate input/output buffer size
  3200. //
  3201. Size = pIrpSp->Parameters.DeviceIoControl.InputBufferLength;
  3202. if (Size >= sizeof(tREMOTE_CACHE))
  3203. {
  3204. //
  3205. // We need only the name, IpAddress, name_flags, and Ttl fields
  3206. //
  3207. status = NbtAddEntryToRemoteHashTable (pDeviceContext,
  3208. NAME_RESOLVED_BY_CLIENT,
  3209. pRemoteEntry->name,
  3210. pRemoteEntry->IpAddress,
  3211. pRemoteEntry->Ttl,
  3212. pRemoteEntry->name_flags);
  3213. }
  3214. else
  3215. {
  3216. IF_DBG(NBT_DEBUG_PNP_POWER)
  3217. KdPrint(("Nbt.DispatchIoctls[IOCTL_NETBT_ADD_TO_REMOTE_TABLE]: Input buffer too small for struct\n"));
  3218. status = STATUS_BUFFER_TOO_SMALL;
  3219. }
  3220. break;
  3221. }
  3222. //
  3223. // The following Ioctl is used by DsGetDcName
  3224. //
  3225. case IOCTL_NETBT_REMOVE_FROM_REMOTE_TABLE:
  3226. {
  3227. tNAME *pRemoteName = (tNAME *) pIrp->AssociatedIrp.SystemBuffer;
  3228. PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation (pIrp);
  3229. //
  3230. // Validate input/output buffer size
  3231. //
  3232. Size = pIrpSp->Parameters.DeviceIoControl.InputBufferLength;
  3233. if (Size >= sizeof(tNAME))
  3234. {
  3235. //
  3236. // We need only the name
  3237. //
  3238. status = NbtFlushEntryFromRemoteHashTable (pRemoteName);
  3239. }
  3240. else
  3241. {
  3242. IF_DBG(NBT_DEBUG_PNP_POWER)
  3243. KdPrint(("Nbt.DispatchIoctls[IOCTL_NETBT_REMOVE_FROM_REMOTE_TABLE]: Input buffer too small\n"));
  3244. status = STATUS_INVALID_PARAMETER;
  3245. }
  3246. break;
  3247. }
  3248. //
  3249. // The following Ioctl is used by the Rdr/Srv to add/remove addresses from the SmbDevice
  3250. //
  3251. case IOCTL_NETBT_SET_SMBDEVICE_BIND_INFO:
  3252. {
  3253. ASSERT (pDeviceContext == pNbtSmbDevice);
  3254. if ((pNbtSmbDevice) &&
  3255. (NBT_REFERENCE_DEVICE (pNbtSmbDevice, REF_DEV_SMB_BIND, FALSE)))
  3256. {
  3257. status = NbtSetSmbBindingInfo (pDeviceContext, pIrp, pIrpSp);
  3258. NBT_DEREFERENCE_DEVICE (pNbtSmbDevice, REF_DEV_SMB_BIND, FALSE);
  3259. }
  3260. else
  3261. {
  3262. ASSERT(0);
  3263. }
  3264. break;
  3265. }
  3266. default:
  3267. {
  3268. status = STATUS_INVALID_PARAMETER;
  3269. break;
  3270. }
  3271. } // switch
  3272. //
  3273. // copy the reponse to the client's Mdl
  3274. //
  3275. if (!NT_ERROR(status) && // allow buffer overflow to pass by
  3276. ((ControlCode == IOCTL_NETBT_GET_REMOTE_NAMES) ||
  3277. (ControlCode == IOCTL_NETBT_GET_BCAST_NAMES) ||
  3278. (ControlCode == IOCTL_NETBT_GET_CONNECTIONS)) )
  3279. {
  3280. status = TdiCopyBufferToMdl (pBuffer, 0, Size, pIrp->MdlAddress, 0,
  3281. (PULONG) &pIrp->IoStatus.Information);
  3282. CTEMemFree((PVOID)pBuffer);
  3283. }
  3284. //
  3285. // either Success or an Error
  3286. // so complete the irp
  3287. //
  3288. NTIoComplete(pIrp,status,0);
  3289. return(status);
  3290. }
  3291. //----------------------------------------------------------------------------
  3292. NTSTATUS
  3293. GetIpAddrs(
  3294. IN tDEVICECONTEXT *pDeviceContext,
  3295. IN PIRP pIrp
  3296. )
  3297. /*++
  3298. Routine Description:
  3299. This routine performs the IOCTL_GET_IP_ADDRS function.
  3300. It is in its own routine because it is non paged.
  3301. Arguments:
  3302. pDeviceContext -
  3303. pIrp -
  3304. Return Value:
  3305. NTSTATUS -
  3306. --*/
  3307. {
  3308. ULONG Length;
  3309. PULONG pIpAddr;
  3310. PLIST_ENTRY pEntry,pHead;
  3311. tDEVICECONTEXT *pDevContext;
  3312. KIRQL OldIrq;
  3313. tIPADDRESS IpAddr;
  3314. //
  3315. // return this devicecontext's ip address and all the other
  3316. // ip addrs after it.
  3317. //
  3318. if (!pIrp->MdlAddress)
  3319. {
  3320. return STATUS_INVALID_PARAMETER;
  3321. }
  3322. else if ((Length = MmGetMdlByteCount (pIrp->MdlAddress)) < sizeof(ULONG))
  3323. {
  3324. return STATUS_BUFFER_TOO_SMALL;
  3325. }
  3326. else if (!(pIpAddr = (PULONG )MmGetSystemAddressForMdlSafe (pIrp->MdlAddress, HighPagePriority)))
  3327. {
  3328. return STATUS_UNSUCCESSFUL;
  3329. }
  3330. //
  3331. // Put this adapter first in the list
  3332. // Don't include the smb device, its address is uninteresting
  3333. if (!IsDeviceNetbiosless (pDeviceContext))
  3334. {
  3335. // Don't use memcpy, optimizing compiler always try to use a simple store instruction
  3336. CTEMemCopy(pIpAddr, &pDeviceContext->AssignedIpAddress, sizeof(pIpAddr[0]));
  3337. pIpAddr++;
  3338. Length -= sizeof(ULONG);
  3339. }
  3340. // Return the others
  3341. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  3342. pEntry = pHead = &NbtConfig.DeviceContexts;
  3343. while ((pEntry = pEntry->Flink) != pHead)
  3344. {
  3345. if (Length < sizeof(ULONG))
  3346. {
  3347. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  3348. return STATUS_BUFFER_OVERFLOW;
  3349. }
  3350. pDevContext = CONTAINING_RECORD(pEntry, tDEVICECONTEXT, Linkage);
  3351. if ((pDevContext != pDeviceContext) &&
  3352. (pDevContext->AssignedIpAddress))
  3353. {
  3354. // Don't use memcpy, optimizing compiler always try to use a simple store instruction
  3355. CTEMemCopy(pIpAddr, &pDevContext->AssignedIpAddress, sizeof(pIpAddr[0]));
  3356. pIpAddr++;
  3357. Length -= sizeof(ULONG);
  3358. }
  3359. }
  3360. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  3361. //
  3362. // put a -1 address on the end
  3363. //
  3364. if (Length < sizeof(ULONG))
  3365. {
  3366. return STATUS_BUFFER_OVERFLOW;
  3367. }
  3368. IpAddr = -1;
  3369. CTEMemCopy(pIpAddr, &IpAddr, sizeof(pIpAddr[0]));
  3370. return STATUS_SUCCESS;
  3371. } // GetIpAddrs
  3372. //----------------------------------------------------------------------------
  3373. NTSTATUS
  3374. NTReceive(
  3375. IN tDEVICECONTEXT *pDeviceContext,
  3376. IN PIRP pIrp
  3377. )
  3378. /*++
  3379. Routine Description:
  3380. This Routine handles Queuing a receive buffer on a connection or passing
  3381. the recieve buffer to the transport if there is outstanding data waiting
  3382. to be received on the connection.
  3383. Arguments:
  3384. pIrp - a ptr to an IRP
  3385. Return Value:
  3386. NTSTATUS - status of the request
  3387. --*/
  3388. {
  3389. NTSTATUS status=STATUS_UNSUCCESSFUL;
  3390. PIO_STACK_LOCATION pIrpSp;
  3391. tCONNECTELE *pConnEle;
  3392. KIRQL OldIrq, OldIrq1;
  3393. ULONG ToCopy;
  3394. ULONG ClientRcvLen;
  3395. tLOWERCONNECTION *pLowerConn;
  3396. ULONG RemainingPdu;
  3397. PTDI_REQUEST_KERNEL_RECEIVE pParams;
  3398. PTDI_REQUEST_KERNEL_RECEIVE pClientParams;
  3399. ULONG BytesCopied;
  3400. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  3401. pConnEle = pIrpSp->FileObject->FsContext;
  3402. if ((pConnEle) &&
  3403. (pConnEle->state == NBT_SESSION_UP))
  3404. {
  3405. CTESpinLock(pConnEle,OldIrq1);
  3406. PUSH_LOCATION(0x30);
  3407. if (!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  3408. {
  3409. CTESpinFree(pConnEle,OldIrq1);
  3410. ASSERTMSG ("Nbt.NTReceive: ERROR - Invalid Connection Handle\n", 0);
  3411. status = STATUS_INVALID_HANDLE;
  3412. }
  3413. else if (pLowerConn = pConnEle->pLowerConnId)
  3414. {
  3415. status = STATUS_SUCCESS;
  3416. }
  3417. else
  3418. {
  3419. CTESpinFree(pConnEle,OldIrq1);
  3420. status = STATUS_REMOTE_DISCONNECT;
  3421. }
  3422. }
  3423. if (STATUS_SUCCESS != status)
  3424. {
  3425. PUSH_LOCATION(0x47);
  3426. //
  3427. // session in wrong state so reject the buffer posting
  3428. // complete the irp, since there must have been some sort of error
  3429. // to get to here
  3430. //
  3431. NTIoComplete(pIrp, status, 0);
  3432. return(status);
  3433. }
  3434. PUSH_LOCATION(0x31);
  3435. //
  3436. // We are already holding the ConnEle lock
  3437. CTESpinLock(pLowerConn,OldIrq);
  3438. if (pLowerConn->StateRcv != PARTIAL_RCV)
  3439. {
  3440. // **** Fast Path Code ****
  3441. //
  3442. // Queue this receive buffer on to the Rcv Head
  3443. //
  3444. InsertTailList(&pConnEle->RcvHead, &pIrp->Tail.Overlay.ListEntry);
  3445. status = NTCheckSetCancelRoutine(pIrp,(PVOID)NbtCancelReceive,pDeviceContext);
  3446. if (!NT_SUCCESS(status))
  3447. {
  3448. RemoveEntryList(&pIrp->Tail.Overlay.ListEntry);
  3449. CTESpinFree(pLowerConn,OldIrq);
  3450. CTESpinFree(pConnEle,OldIrq1);
  3451. NTIoComplete(pIrp,status,0);
  3452. return(status);
  3453. }
  3454. else
  3455. {
  3456. //
  3457. // if the irp is not cancelled, returning pending
  3458. //
  3459. CTESpinFree(pLowerConn,OldIrq);
  3460. CTESpinFree(pConnEle,OldIrq1);
  3461. return(STATUS_PENDING);
  3462. }
  3463. }
  3464. else
  3465. {
  3466. // ***** Partial Rcv - Data Still in Transport *****
  3467. BOOLEAN ZeroLengthSend;
  3468. PUSH_LOCATION(0x32);
  3469. IF_DBG(NBT_DEBUG_RCV)
  3470. KdPrint(("Nbt.NTReceive: A Rcv Buffer posted data in Xport,InXport= %X,InIndic %X RcvIndicated %X\n",
  3471. pConnEle->BytesInXport,pLowerConn->BytesInIndicate,
  3472. pConnEle->ReceiveIndicated));
  3473. // get the MDL chain length
  3474. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  3475. pClientParams = (PTDI_REQUEST_KERNEL_RECEIVE)&pIrpSp->Parameters;
  3476. // Reset the Irp pending flag
  3477. pIrpSp->Control &= ~SL_PENDING_RETURNED;
  3478. // fill in the next irp stack location with our completion routine.
  3479. pIrpSp = IoGetNextIrpStackLocation(pIrp);
  3480. pIrpSp->CompletionRoutine = CompletionRcv;
  3481. pIrpSp->Context = (PVOID)pConnEle->pLowerConnId;
  3482. pIrpSp->Flags = 0;
  3483. // set flags so the completion routine is always invoked.
  3484. pIrpSp->Control = SL_INVOKE_ON_SUCCESS | SL_INVOKE_ON_ERROR | SL_INVOKE_ON_CANCEL;
  3485. pIrpSp->MajorFunction = IRP_MJ_INTERNAL_DEVICE_CONTROL;
  3486. pIrpSp->MinorFunction = TDI_RECEIVE;
  3487. pIrpSp->DeviceObject = IoGetRelatedDeviceObject(pConnEle->pLowerConnId->pFileObject);
  3488. pIrpSp->FileObject = pConnEle->pLowerConnId->pFileObject;
  3489. pParams = (PTDI_REQUEST_KERNEL_RECEIVE)&pIrpSp->Parameters;
  3490. pParams->ReceiveFlags = pClientParams->ReceiveFlags;
  3491. // Since this irp is going to traverse through CompletionRcv, we
  3492. // need to set the following, since it undoes this stuff.
  3493. // This also prevents the LowerConn from being blown away before
  3494. // the irp has returned from the transport
  3495. //
  3496. NBT_REFERENCE_LOWERCONN (pLowerConn, REF_LOWC_RCV_HANDLER);
  3497. //
  3498. // pass the receive buffer directly to the transport, decrementing
  3499. // the number of receive bytes that have been indicated
  3500. //
  3501. ASSERT(pConnEle->TotalPcktLen >= pConnEle->BytesRcvd);
  3502. if (pClientParams->ReceiveLength > (pConnEle->TotalPcktLen - pConnEle->BytesRcvd))
  3503. {
  3504. pParams->ReceiveLength = pConnEle->TotalPcktLen - pConnEle->BytesRcvd;
  3505. }
  3506. else
  3507. {
  3508. pParams->ReceiveLength = pClientParams->ReceiveLength;
  3509. }
  3510. ClientRcvLen = pParams->ReceiveLength;
  3511. //
  3512. // Set the amount of data that we will receive so when the
  3513. // irp completes in completionRcv, we can fill in that
  3514. // info in the Irp
  3515. //
  3516. pConnEle->CurrentRcvLen = ClientRcvLen;
  3517. // if a zero length send occurs, then ReceiveIndicated is set
  3518. // to zero with the state set to RcvPartial. Or, the client may
  3519. // pass down an Irp with no MDL in it!!
  3520. //
  3521. if ((pConnEle->ReceiveIndicated == 0) || !pIrp->MdlAddress)
  3522. {
  3523. ZeroLengthSend = TRUE;
  3524. }
  3525. else
  3526. {
  3527. ZeroLengthSend = FALSE;
  3528. }
  3529. // calculate how many bytes are still remaining for the client.
  3530. if (pConnEle->ReceiveIndicated > ClientRcvLen)
  3531. {
  3532. PUSH_LOCATION(0x40);
  3533. pConnEle->ReceiveIndicated -= ClientRcvLen;
  3534. }
  3535. else
  3536. {
  3537. pConnEle->ReceiveIndicated = 0;
  3538. }
  3539. if (pLowerConn->BytesInIndicate || ZeroLengthSend)
  3540. {
  3541. PMDL Mdl;
  3542. PUSH_LOCATION(0x33);
  3543. if (ClientRcvLen > pLowerConn->BytesInIndicate)
  3544. {
  3545. ToCopy = pLowerConn->BytesInIndicate;
  3546. }
  3547. else
  3548. {
  3549. PUSH_LOCATION(0x41);
  3550. ToCopy = ClientRcvLen;
  3551. }
  3552. // copy data from the indicate buffer to the client's buffer,
  3553. // remembering that there is a session header in the indicate
  3554. // buffer at the start of it... so skip that. The
  3555. // client can pass down a null Mdl address for a zero length
  3556. // rcv so check for that.
  3557. //
  3558. if (Mdl = pIrp->MdlAddress)
  3559. {
  3560. TdiCopyBufferToMdl(MmGetMdlVirtualAddress(pLowerConn->pIndicateMdl),
  3561. 0, // src offset
  3562. ToCopy,
  3563. Mdl,
  3564. 0, // dest offset
  3565. &BytesCopied);
  3566. }
  3567. else
  3568. {
  3569. BytesCopied = 0;
  3570. }
  3571. // client's MDL is too short...
  3572. if (BytesCopied != ToCopy)
  3573. {
  3574. PUSH_LOCATION(0x42);
  3575. IF_DBG(NBT_DEBUG_INDICATEBUFF)
  3576. KdPrint(("Nbt:Receive Buffer too short for Indicate buff BytesCopied %X, ToCopy %X\n",
  3577. BytesCopied, ToCopy));
  3578. // ToCopy = BytesCopied;
  3579. // so the irp will be completed, below
  3580. ClientRcvLen = BytesCopied;
  3581. }
  3582. pLowerConn->BytesInIndicate -= (USHORT)BytesCopied;
  3583. // this case is only if the irp is full and should be returned
  3584. // now.
  3585. if (BytesCopied == ClientRcvLen)
  3586. {
  3587. PUSH_LOCATION(0x34);
  3588. // check if the indicate buffer is empty now. If not, then
  3589. // move the data forward to the start of the buffer.
  3590. //
  3591. if (pLowerConn->BytesInIndicate)
  3592. {
  3593. PUSH_LOCATION(0x43);
  3594. CopyToStartofIndicate(pLowerConn,BytesCopied);
  3595. }
  3596. //
  3597. // the irp is full so complete it
  3598. //
  3599. // the client MDL is full, so complete his irp
  3600. // CompletionRcv increments the number of bytes rcvd
  3601. // for this session pdu (pConnEle->BytesRcvd).
  3602. pIrp->IoStatus.Information = BytesCopied;
  3603. pIrp->IoStatus.Status = STATUS_SUCCESS;
  3604. // since we are completing it and TdiRcvHandler did not set the next
  3605. // one.
  3606. //
  3607. ASSERT(pIrp->CurrentLocation > 1);
  3608. IoSetNextIrpStackLocation(pIrp);
  3609. // we need to track how much of the client's MDL has filled
  3610. // up to know when to return it. CompletionRcv subtracts
  3611. // from this value as it receives bytes.
  3612. pConnEle->FreeBytesInMdl = ClientRcvLen;
  3613. pConnEle->CurrentRcvLen = ClientRcvLen;
  3614. CTESpinFree(pLowerConn,OldIrq);
  3615. CTESpinFree(pConnEle,OldIrq1);
  3616. IoCompleteRequest(pIrp,IO_NETWORK_INCREMENT);
  3617. return(STATUS_SUCCESS);
  3618. }
  3619. else
  3620. {
  3621. //
  3622. // clear the number of bytes in the indicate buffer since the client
  3623. // has taken more than the data left in the Indicate buffer
  3624. //
  3625. pLowerConn->BytesInIndicate = 0;
  3626. // decrement the client rcv len by the amount already put into the
  3627. // client Mdl
  3628. //
  3629. ClientRcvLen -= BytesCopied;
  3630. IF_DBG(NBT_DEBUG_RCV)
  3631. KdPrint(("Nbt: Pass Client Irp to Xport BytesinXport %X, ClientRcvLen %X\n",
  3632. pConnEle->BytesInXport,ClientRcvLen));
  3633. //
  3634. // Set the amount left inthe transport after this irp
  3635. // completes
  3636. if (pConnEle->BytesInXport < ClientRcvLen )
  3637. {
  3638. pConnEle->BytesInXport = 0;
  3639. }
  3640. else
  3641. {
  3642. pConnEle->BytesInXport -= ClientRcvLen;
  3643. }
  3644. // Adjust the number of bytes in the Mdl chain so far since the
  3645. // completion routine will only count the bytes filled in by the
  3646. // transport
  3647. pConnEle->BytesRcvd += BytesCopied;
  3648. // the client is going to take more data from the transport with
  3649. // this Irp. Set the new Rcv Length that accounts for the data just
  3650. // copied to the Irp.
  3651. //
  3652. pParams->ReceiveLength = ClientRcvLen;
  3653. IF_DBG(NBT_DEBUG_RCV)
  3654. KdPrint(("Nbt:ClientRcvLen = %X, LeftinXport= %X BytesCopied= %X %X\n",ClientRcvLen,
  3655. pConnEle->BytesInXport,BytesCopied,pLowerConn));
  3656. // set the state to this so we can undo the MDL footwork
  3657. // in completion rcv - since we have made a partial MDL and
  3658. // put that at the start of the chain.
  3659. //
  3660. SET_STATERCV_LOWER(pLowerConn, FILL_IRP, FillIrp);
  3661. // Note that the Irp Mdl address changes below
  3662. // when MakePartialMdl is called so this line cannot
  3663. // be moved to the common code below!!
  3664. pLowerConn->pMdl = pIrp->MdlAddress;
  3665. // setup the next MDL so we can create a partial mdl correctly
  3666. // in TdiReceiveHandler
  3667. //
  3668. pConnEle->pNextMdl = pIrp->MdlAddress;
  3669. // Build a partial Mdl to represent the client's Mdl chain since
  3670. // we have copied data to it, and the transport must copy
  3671. // more data to it after that data.
  3672. //
  3673. // Force the system to map and lock the user buffer
  3674. MmGetSystemAddressForMdlSafe (pIrp->MdlAddress, HighPagePriority);
  3675. MakePartialMdl(pConnEle,pIrp,BytesCopied);
  3676. // pass the Irp to the transport
  3677. //
  3678. //
  3679. IF_DBG(NBT_DEBUG_RCV)
  3680. KdPrint(("Nbt:Calling IoCallDriver\n"));
  3681. ASSERT(pIrp->CurrentLocation > 1);
  3682. }
  3683. }
  3684. else
  3685. {
  3686. PUSH_LOCATION(0x36);
  3687. IF_DBG(NBT_DEBUG_RCV)
  3688. KdPrint(("Nbt.NTReceive: Pass Irp To Xport Bytes in Xport %X, ClientRcvLen %X, RcvIndicated %X\n",
  3689. pConnEle->BytesInXport,ClientRcvLen,pConnEle->ReceiveIndicated));
  3690. //
  3691. // there are no bytes in the indicate buffer, so just pass the
  3692. // irp on down to the transport
  3693. //
  3694. //
  3695. // Decide the next state depending on whether the transport currently
  3696. // has enough data for this irp
  3697. //
  3698. if (pConnEle->BytesInXport < ClientRcvLen)
  3699. {
  3700. PUSH_LOCATION(0x37);
  3701. pConnEle->BytesInXport = 0;
  3702. //
  3703. // to get to here, the implication is that ReceiveIndicated
  3704. // equals zero too!! Since ReceiveInd cannot be more than
  3705. // BytesInXport, so we can change the state to fill irp without
  3706. // worrying about overwriting PartialRcv
  3707. //
  3708. SET_STATERCV_LOWER(pLowerConn, FILL_IRP, FillIrp);
  3709. // setup the next MDL so we can create a partial mdl correctly
  3710. // in TdiReceiveHandler
  3711. //
  3712. pConnEle->pNextMdl = pIrp->MdlAddress;
  3713. }
  3714. else
  3715. {
  3716. PUSH_LOCATION(0x38);
  3717. pConnEle->BytesInXport -= ClientRcvLen;
  3718. // set the state to this so we know what to do in completion rcv
  3719. //
  3720. if (pConnEle->ReceiveIndicated == 0)
  3721. {
  3722. PUSH_LOCATION(0x39);
  3723. SET_STATERCV_LOWER(pLowerConn, NORMAL, Normal);
  3724. }
  3725. }
  3726. //
  3727. // save the Irp so we can reconstruct things later
  3728. //
  3729. pLowerConn->pMdl = pIrp->MdlAddress;
  3730. }
  3731. // *** Common Code to passing irp to transport - when there is
  3732. // data in the indicate buffer and when there isn't
  3733. // keep track of data in MDL so we know when it is full
  3734. // and we need to return it to the user
  3735. //
  3736. pConnEle->FreeBytesInMdl = pParams->ReceiveLength;
  3737. // Force the system to map and lock the user buffer
  3738. MmGetSystemAddressForMdlSafe (pIrp->MdlAddress, HighPagePriority);
  3739. //
  3740. // Null the Irp since we are passing it to the transport.
  3741. //
  3742. pConnEle->pIrpRcv = NULL;
  3743. CTESpinFree(pLowerConn,OldIrq);
  3744. CTESpinFree(pConnEle,OldIrq1);
  3745. CHECK_COMPLETION(pIrp);
  3746. status = IoCallDriver(IoGetRelatedDeviceObject(pLowerConn->pFileObject),pIrp);
  3747. IF_DBG(NBT_DEBUG_RCV)
  3748. KdPrint(("Nbt.NTReceive: Returning=<%x>, IoStatus.Status=<%x>, IoStatus.Information=<%x>\n",
  3749. status, pIrp->IoStatus.Status, pIrp->IoStatus.Information));
  3750. }
  3751. return(status);
  3752. }
  3753. //----------------------------------------------------------------------------
  3754. NTSTATUS
  3755. NTReceiveDatagram(
  3756. IN tDEVICECONTEXT *pDeviceContext,
  3757. IN PIRP pIrp)
  3758. /*++
  3759. Routine Description:
  3760. This Routine handles receiving a datagram by passing the datagram rcv
  3761. buffer to the non-OS specific code.
  3762. Arguments:
  3763. pIrp - a ptr to an IRP
  3764. Return Value:
  3765. NTSTATUS - status of the request
  3766. --*/
  3767. {
  3768. NTSTATUS status;
  3769. PIO_STACK_LOCATION pIrpSp;
  3770. PTDI_REQUEST_KERNEL_RECEIVEDG pTdiRequest;
  3771. TDI_REQUEST Request;
  3772. ULONG ReceivedLength;
  3773. tCLIENTELE *pClientEle;
  3774. IF_DBG(NBT_DEBUG_RCV)
  3775. KdPrint(("Nbt: Got a Receive datagram that NBT was NOT \n"));
  3776. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  3777. pClientEle = (tCLIENTELE *)pIrpSp->FileObject->FsContext;
  3778. if (!NBT_VERIFY_HANDLE2 (pClientEle, NBT_VERIFY_CLIENT, NBT_VERIFY_CLIENT_DOWN))
  3779. {
  3780. ASSERTMSG ("Nbt.NTReceiveDatagram: ERROR - Invalid Address Handle\n", 0);
  3781. return (STATUS_INVALID_HANDLE);
  3782. }
  3783. // get the sending information out of the irp
  3784. pTdiRequest = (PTDI_REQUEST_KERNEL_RECEIVEDG)&pIrpSp->Parameters;
  3785. Request.Handle.AddressHandle = pClientEle;
  3786. status = NbtReceiveDatagram(
  3787. &Request,
  3788. pTdiRequest->ReceiveDatagramInformation,
  3789. pTdiRequest->ReturnDatagramInformation,
  3790. pTdiRequest->ReceiveLength,
  3791. &ReceivedLength,
  3792. (PVOID)pIrp->MdlAddress, // user data
  3793. (tDEVICECONTEXT *)pDeviceContext,
  3794. pIrp);
  3795. if (status != STATUS_PENDING)
  3796. {
  3797. NTIoComplete(pIrp,status,ReceivedLength);
  3798. }
  3799. return(status);
  3800. }
  3801. //----------------------------------------------------------------------------
  3802. NTSTATUS
  3803. NTSend(
  3804. IN tDEVICECONTEXT *pDeviceContext,
  3805. IN PIRP pIrp)
  3806. /*++
  3807. Routine Description:
  3808. This Routine handles sending session pdus across a connection. It is
  3809. all OS specific code.
  3810. Arguments:
  3811. pIrp - a ptr to an IRP
  3812. Return Value:
  3813. NTSTATUS - status of the request
  3814. --*/
  3815. {
  3816. PIO_STACK_LOCATION pIrpSp;
  3817. NTSTATUS status;
  3818. PTDI_REQUEST_KERNEL_SEND pTdiRequest;
  3819. PMDL pMdl;
  3820. PSINGLE_LIST_ENTRY pSingleListEntry;
  3821. tSESSIONHDR *pSessionHdr;
  3822. tCONNECTELE *pConnEle;
  3823. KIRQL OldIrq;
  3824. KIRQL OldIrq1;
  3825. PTDI_REQUEST_KERNEL_SEND pParams;
  3826. PFILE_OBJECT pFileObject;
  3827. tLOWERCONNECTION *pLowerConn;
  3828. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  3829. //
  3830. // This function could be called directly also while bypassing the
  3831. // Io subsystem, so we need to recheck the DeviceContext here
  3832. //
  3833. if (!NBT_REFERENCE_DEVICE (pDeviceContext, REF_DEV_DISPATCH, FALSE))
  3834. {
  3835. // IF_DBG(NBT_DEBUG_SEND)
  3836. KdPrint(("Nbt.NTSend: Invalid Device=<%x>\n", pDeviceContext));
  3837. status = STATUS_INVALID_DEVICE_STATE;
  3838. goto ErrorInvalidDevice;
  3839. }
  3840. // get the sending information out of the irp
  3841. pTdiRequest = (PTDI_REQUEST_KERNEL_SEND)&pIrpSp->Parameters;
  3842. pConnEle = (tCONNECTELE *)pIrpSp->FileObject->FsContext;
  3843. if (!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  3844. {
  3845. ASSERTMSG ("Nbt.NTSend: ERROR - Invalid Connection Handle\n", 0);
  3846. status = STATUS_INVALID_HANDLE;
  3847. goto ErrorExit; // Irp has to be completed in this routine! Bug# 202340
  3848. }
  3849. CTESpinLock(pConnEle,OldIrq);
  3850. if (!(pLowerConn = pConnEle->pLowerConnId))
  3851. {
  3852. CTESpinFree(pConnEle,OldIrq);
  3853. IF_DBG(NBT_DEBUG_SEND)
  3854. KdPrint(("Nbt.NTSend: attempting send when LowerConn has been freed!\n"));
  3855. status = STATUS_INVALID_HANDLE;
  3856. goto ErrorExit; // to save on indent levels use a goto here
  3857. }
  3858. //
  3859. // make sure lowerconn stays valid until the irp is done
  3860. //
  3861. CTESpinLock(pLowerConn,OldIrq1);
  3862. NBT_REFERENCE_LOWERCONN (pLowerConn, REF_LOWC_SEND);
  3863. CTESpinFree(pLowerConn,OldIrq1);
  3864. // check the state of the connection
  3865. if (pConnEle->state == NBT_SESSION_UP)
  3866. {
  3867. //
  3868. // send the data on downward to tcp
  3869. // allocate an MDL to allow us to put the session hdr in first and then
  3870. // put the users buffer on after that, chained to the session hdr MDL.
  3871. //
  3872. #if BACK_FILL
  3873. {
  3874. PMDL SmbMdl;
  3875. SmbMdl = (PMDL)pIrp->MdlAddress;
  3876. // Check if network header type is set
  3877. // if yes, then we can back fill the nbt session header
  3878. if ((SmbMdl) && (SmbMdl->MdlFlags & MDL_NETWORK_HEADER))
  3879. {
  3880. pSessionHdr = (tSESSIONHDR *)((ULONG_PTR)SmbMdl->MappedSystemVa - SESSION_HDR_SIZE);
  3881. (ULONG_PTR)SmbMdl->MappedSystemVa -= SESSION_HDR_SIZE;
  3882. SmbMdl->ByteOffset -= SESSION_HDR_SIZE;
  3883. SmbMdl->ByteCount+= SESSION_HDR_SIZE;
  3884. pSessionHdr->UlongLength = htonl(pTdiRequest->SendLength);
  3885. IF_DBG(NBT_DEBUG_SEND)
  3886. KdPrint(("Nbt: Backfilled mdl %x %x\n", pSessionHdr, SmbMdl));
  3887. }
  3888. else
  3889. {
  3890. CTESpinLockAtDpc(&NbtConfig);
  3891. if (NbtConfig.SessionMdlFreeSingleList.Next)
  3892. {
  3893. pSingleListEntry = PopEntryList(&NbtConfig.SessionMdlFreeSingleList);
  3894. pMdl = CONTAINING_RECORD(pSingleListEntry,MDL,Next);
  3895. ASSERT ( MmGetMdlByteCount ( pMdl ) == sizeof ( tSESSIONHDR ) );
  3896. }
  3897. else
  3898. {
  3899. NbtGetMdl(&pMdl,eNBT_FREE_SESSION_MDLS);
  3900. if (!pMdl)
  3901. {
  3902. IF_DBG(NBT_DEBUG_SEND)
  3903. KdPrint(("Nbt:Unable to get an MDL for a session send!\n"));
  3904. status = STATUS_INSUFFICIENT_RESOURCES;
  3905. CTESpinFreeAtDpc(&NbtConfig);
  3906. CTESpinFree(pConnEle,OldIrq);
  3907. // to save on indent levels use a goto here
  3908. goto ErrorExit1;
  3909. }
  3910. }
  3911. CTESpinFreeAtDpc(&NbtConfig);
  3912. // get the session hdr address out of the MDL
  3913. pSessionHdr = (tSESSIONHDR *)MmGetMdlVirtualAddress(pMdl);
  3914. // the type of PDU is always a session message, since the session
  3915. // request is sent when the client issues a "connect" rather than a send
  3916. //
  3917. pSessionHdr->UlongLength = htonl(pTdiRequest->SendLength);
  3918. // get the device object and file object for the TCP transport underneath
  3919. // link the user buffer on the end of the session header Mdl on the Irp
  3920. //
  3921. pMdl->Next = pIrp->MdlAddress;
  3922. pIrp->MdlAddress = pMdl;
  3923. }
  3924. }
  3925. #else
  3926. CTESpinLockAtDpc(&NbtConfig);
  3927. if (NbtConfig.SessionMdlFreeSingleList.Next)
  3928. {
  3929. pSingleListEntry = PopEntryList(&NbtConfig.SessionMdlFreeSingleList);
  3930. pMdl = CONTAINING_RECORD(pSingleListEntry,MDL,Next);
  3931. ASSERT ( MmGetMdlByteCount ( pMdl ) == sizeof ( tSESSIONHDR ) );
  3932. }
  3933. else
  3934. {
  3935. NbtGetMdl(&pMdl,eNBT_FREE_SESSION_MDLS);
  3936. if (!pMdl)
  3937. {
  3938. IF_DBG(NBT_DEBUG_SEND)
  3939. KdPrint(("Nbt:Unable to get an MDL for a session send!\n"));
  3940. status = STATUS_INSUFFICIENT_RESOURCES;
  3941. CTESpinFreeAtDpc(&NbtConfig);
  3942. CTESpinFree(pConnEle,OldIrq);
  3943. // to save on indent levels use a goto here
  3944. goto ErrorExit1;
  3945. }
  3946. }
  3947. CTESpinFreeAtDpc(&NbtConfig);
  3948. // get the session hdr address out of the MDL
  3949. pSessionHdr = (tSESSIONHDR *)MmGetMdlVirtualAddress(pMdl);
  3950. // the type of PDU is always a session message, since the session
  3951. // request is sent when the client issues a "connect" rather than a send
  3952. //
  3953. pSessionHdr->UlongLength = htonl(pTdiRequest->SendLength);
  3954. // get the device object and file object for the TCP transport underneath
  3955. // link the user buffer on the end of the session header Mdl on the Irp
  3956. //
  3957. pMdl->Next = pIrp->MdlAddress;
  3958. pIrp->MdlAddress = pMdl;
  3959. #endif //BACK_FILL
  3960. pIrpSp = IoGetNextIrpStackLocation(pIrp);
  3961. pParams = (PTDI_REQUEST_KERNEL_SEND)&pIrpSp->Parameters;
  3962. pParams->SendFlags = pTdiRequest->SendFlags;
  3963. pParams->SendLength = pTdiRequest->SendLength + sizeof(tSESSIONHDR);
  3964. pIrpSp->CompletionRoutine = SendCompletion;
  3965. pIrpSp->Context = (PVOID)pLowerConn;
  3966. pIrpSp->Control = SL_INVOKE_ON_SUCCESS | SL_INVOKE_ON_ERROR | SL_INVOKE_ON_CANCEL;
  3967. pIrpSp->MajorFunction = IRP_MJ_INTERNAL_DEVICE_CONTROL;
  3968. pIrpSp->MinorFunction = TDI_SEND;
  3969. pFileObject = pLowerConn->pFileObject;
  3970. pLowerConn->BytesSent += pTdiRequest->SendLength;
  3971. pIrpSp->FileObject = pFileObject;
  3972. pIrpSp->DeviceObject = IoGetRelatedDeviceObject(pFileObject);
  3973. CTESpinFree(pConnEle,OldIrq);
  3974. CHECK_COMPLETION(pIrp);
  3975. #if FAST_DISP
  3976. //if we are all set to do fast path, do so now.
  3977. if (pLowerConn->FastSend)
  3978. {
  3979. IoSetNextIrpStackLocation(pIrp);
  3980. IF_DBG(NBT_DEBUG_SEND)
  3981. KdPrint(("Nbt.NTSend: Fasttdi path %x %x \n", pIrp, pIrpSp));
  3982. status = pLowerConn->FastSend (pIrp, pIrpSp);
  3983. }
  3984. else
  3985. {
  3986. status = IoCallDriver (IoGetRelatedDeviceObject (pFileObject), pIrp);
  3987. }
  3988. #else
  3989. status = IoCallDriver(IoGetRelatedDeviceObject(pFileObject),pIrp);
  3990. #endif
  3991. NBT_DEREFERENCE_DEVICE (pDeviceContext, REF_DEV_DISPATCH, FALSE);
  3992. return(status);
  3993. } //correct state
  3994. CTESpinFree(pConnEle,OldIrq);
  3995. IF_DBG(NBT_DEBUG_SEND)
  3996. KdPrint(("Nbt:Invalid state for connection on an attempted send, %X\n",
  3997. pConnEle));
  3998. status = STATUS_INVALID_HANDLE;
  3999. ErrorExit1:
  4000. //
  4001. // Dereference pLowerConn->RefCount, referenced above.
  4002. //
  4003. NBT_DEREFERENCE_LOWERCONN (pLowerConn, REF_LOWC_SEND, FALSE);
  4004. ErrorExit:
  4005. NBT_DEREFERENCE_DEVICE (pDeviceContext, REF_DEV_DISPATCH, FALSE);
  4006. ErrorInvalidDevice:
  4007. //
  4008. // Reset the Irp pending flag
  4009. //
  4010. pIrpSp->Control &= ~SL_PENDING_RETURNED;
  4011. //
  4012. // complete the irp, since there must have been some sort of error
  4013. // to get to here
  4014. //
  4015. NTIoComplete (pIrp, status, 0);
  4016. return(status);
  4017. }
  4018. //----------------------------------------------------------------------------
  4019. NTSTATUS
  4020. SendCompletion(
  4021. IN PDEVICE_OBJECT DeviceObject,
  4022. IN PIRP Irp,
  4023. IN PVOID Context
  4024. )
  4025. /*++
  4026. Routine Description:
  4027. This routine handles the completion event when the send completes with
  4028. the underlying transport. It must put the session hdr buffer back in
  4029. the correct free list and free the active q entry and put it back on
  4030. its free list.
  4031. Arguments:
  4032. DeviceObject - unused.
  4033. Irp - Supplies Irp that the transport has finished processing.
  4034. Context - Supplies the pConnectEle - the connection data structure
  4035. Return Value:
  4036. The final status from the operation (success or an exception).
  4037. --*/
  4038. {
  4039. PMDL pMdl;
  4040. tLOWERCONNECTION *pLowerConn;
  4041. //
  4042. // Do some checking to keep the Io system happy - propagate the pending
  4043. // bit up the irp stack frame.... if it was set by the driver below then
  4044. // it must be set by me
  4045. //
  4046. if (Irp->PendingReturned)
  4047. {
  4048. IoMarkIrpPending(Irp);
  4049. }
  4050. // put the MDL we back on its free list and put the clients mdl back on the Irp
  4051. // as it was before the send
  4052. pMdl = Irp->MdlAddress;
  4053. #if BACK_FILL
  4054. // If the header is back filled
  4055. // we should adjust the pointers back to where it was.
  4056. if (pMdl->MdlFlags & MDL_NETWORK_HEADER)
  4057. {
  4058. (ULONG_PTR)pMdl->MappedSystemVa += SESSION_HDR_SIZE;
  4059. pMdl->ByteOffset += SESSION_HDR_SIZE;
  4060. pMdl->ByteCount -= SESSION_HDR_SIZE;
  4061. IF_DBG(NBT_DEBUG_SEND)
  4062. KdPrint(("Nbt: Done with Backfilled mdl %x\n", pMdl));
  4063. }
  4064. else
  4065. {
  4066. Irp->MdlAddress = pMdl->Next;
  4067. ASSERT ( MmGetMdlByteCount ( pMdl ) == SESSION_HDR_SIZE );
  4068. #if DBG
  4069. IF_DBG(NBT_DEBUG_SEND)
  4070. {
  4071. PMDL pMdl1;
  4072. ULONG ulen1,ulen2,ulen3;
  4073. UCHAR uc;
  4074. tSESSIONHDR *pSessionHdr;
  4075. PSINGLE_LIST_ENTRY pSingleListEntry;
  4076. KIRQL OldIrq;
  4077. pSessionHdr = (tSESSIONHDR *)MmGetMdlVirtualAddress(pMdl);
  4078. ulen1 = htonl ( pSessionHdr->UlongLength );
  4079. for ( ulen2 = 0 , pMdl1 = pMdl ; ( pMdl1 = pMdl1->Next ) != NULL ; )
  4080. {
  4081. ulen3 = MmGetMdlByteCount ( pMdl1 );
  4082. ASSERT ( ulen3 > 0 );
  4083. uc = ( ( UCHAR * ) MmGetMdlVirtualAddress ( pMdl1 ) ) [ ulen3 - 1 ];
  4084. ulen2 += ulen3;
  4085. }
  4086. ASSERT ( ulen2 == ulen1 );
  4087. CTESpinLock(&NbtConfig,OldIrq);
  4088. for ( pSingleListEntry = &NbtConfig.SessionMdlFreeSingleList ;
  4089. ( pSingleListEntry = pSingleListEntry->Next ) != NULL ;
  4090. )
  4091. {
  4092. pMdl1 = CONTAINING_RECORD(pSingleListEntry,MDL,Next);
  4093. ASSERT ( pMdl1 != pMdl );
  4094. }
  4095. CTESpinFree(&NbtConfig,OldIrq);
  4096. }
  4097. #endif // DBG
  4098. ExInterlockedPushEntryList(&NbtConfig.SessionMdlFreeSingleList,
  4099. (PSINGLE_LIST_ENTRY)pMdl,
  4100. &NbtConfig.LockInfo.SpinLock);
  4101. }
  4102. #else
  4103. Irp->MdlAddress = pMdl->Next;
  4104. ASSERT ( MmGetMdlByteCount ( pMdl ) == sizeof ( tSESSIONHDR ) );
  4105. #if DBG
  4106. IF_DBG(NBT_DEBUG_SEND)
  4107. {
  4108. PMDL pMdl1;
  4109. ULONG ulen1,ulen2,ulen3;
  4110. UCHAR uc;
  4111. tSESSIONHDR *pSessionHdr;
  4112. PSINGLE_LIST_ENTRY pSingleListEntry;
  4113. KIRQL OldIrq;
  4114. pSessionHdr = (tSESSIONHDR *)MmGetMdlVirtualAddress(pMdl);
  4115. ulen1 = htonl ( pSessionHdr->UlongLength );
  4116. for ( ulen2 = 0 , pMdl1 = pMdl ; ( pMdl1 = pMdl1->Next ) != NULL ; )
  4117. {
  4118. ulen3 = MmGetMdlByteCount ( pMdl1 );
  4119. ASSERT ( ulen3 > 0 );
  4120. uc = ( ( UCHAR * ) MmGetMdlVirtualAddress ( pMdl1 ) ) [ ulen3 - 1 ];
  4121. ulen2 += ulen3;
  4122. }
  4123. ASSERT ( ulen2 == ulen1 );
  4124. CTESpinLock(&NbtConfig,OldIrq);
  4125. for ( pSingleListEntry = &NbtConfig.SessionMdlFreeSingleList ;
  4126. ( pSingleListEntry = pSingleListEntry->Next ) != NULL ;
  4127. )
  4128. {
  4129. pMdl1 = CONTAINING_RECORD(pSingleListEntry,MDL,Next);
  4130. ASSERT ( pMdl1 != pMdl );
  4131. }
  4132. CTESpinFree(&NbtConfig,OldIrq);
  4133. }
  4134. #endif // DBG
  4135. ExInterlockedPushEntryList(&NbtConfig.SessionMdlFreeSingleList,
  4136. (PSINGLE_LIST_ENTRY)pMdl,
  4137. &NbtConfig.LockInfo.SpinLock);
  4138. #endif //BACK_FILL
  4139. // fill in the sent size so that it substracts off the session header size
  4140. //
  4141. if (Irp->IoStatus.Information > sizeof(tSESSIONHDR))
  4142. {
  4143. Irp->IoStatus.Information -= sizeof(tSESSIONHDR);
  4144. }
  4145. else
  4146. {
  4147. // nothing was sent
  4148. Irp->IoStatus.Information = 0;
  4149. IF_DBG(NBT_DEBUG_SEND)
  4150. KdPrint(("Nbt:Zero Send Length for a session send!\n"));
  4151. }
  4152. //
  4153. // we incremented this before the send: deref it now
  4154. //
  4155. pLowerConn = (tLOWERCONNECTION *)Context;
  4156. ASSERT (NBT_VERIFY_HANDLE (pLowerConn, NBT_VERIFY_LOWERCONN));
  4157. NBT_DEREFERENCE_LOWERCONN (pLowerConn, REF_LOWC_SEND, FALSE);
  4158. return(STATUS_SUCCESS);
  4159. UNREFERENCED_PARAMETER( DeviceObject );
  4160. }
  4161. //----------------------------------------------------------------------------
  4162. NTSTATUS
  4163. NTSendDatagram(
  4164. IN tDEVICECONTEXT *pDeviceContext,
  4165. IN PIRP pIrp)
  4166. /*++
  4167. Routine Description:
  4168. This Routine handles sending a datagram down to the transport.
  4169. Arguments:
  4170. pIrp - a ptr to an IRP
  4171. Return Value:
  4172. NTSTATUS - status of the request
  4173. --*/
  4174. {
  4175. PIO_STACK_LOCATION pIrpSp;
  4176. NTSTATUS status;
  4177. LONG lSentLength;
  4178. TDI_REQUEST Request;
  4179. PTDI_REQUEST_KERNEL_SENDDG pTdiRequest;
  4180. tCLIENTELE *pClientEle;
  4181. CTEPagedCode();
  4182. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  4183. pClientEle = (tCLIENTELE *)pIrpSp->FileObject->FsContext;
  4184. if (!NBT_VERIFY_HANDLE2 (pClientEle, NBT_VERIFY_CLIENT, NBT_VERIFY_CLIENT_DOWN))
  4185. {
  4186. ASSERTMSG ("Nbt.SendDatagram: ERROR - Invalid Address Handle\n", 0);
  4187. return (STATUS_INVALID_HANDLE);
  4188. }
  4189. // CTEVerifyHandle(pClientEle,NBT_VERIFY_CLIENT,tCLIENTELE,&status);
  4190. // get the sending information out of the irp
  4191. pTdiRequest = (PTDI_REQUEST_KERNEL_SENDDG)&pIrpSp->Parameters;
  4192. Request.Handle.AddressHandle = pClientEle;
  4193. lSentLength = 0;
  4194. //
  4195. // Mark IRP pending here
  4196. //
  4197. IoMarkIrpPending(pIrp);
  4198. status = NbtSendDatagram (&Request,
  4199. pTdiRequest->SendDatagramInformation,
  4200. pTdiRequest->SendLength,
  4201. &lSentLength,
  4202. (PVOID)pIrp->MdlAddress, // user data
  4203. (tDEVICECONTEXT *)pDeviceContext,
  4204. pIrp);
  4205. if (status != STATUS_PENDING)
  4206. {
  4207. //
  4208. // either Success or an Error
  4209. //
  4210. NTIoComplete(pIrp,status,lSentLength);
  4211. }
  4212. //
  4213. // To make driver verifier and IO system happy, always return
  4214. // STATUS_PENDING for the request marked as PENDING
  4215. //
  4216. return STATUS_PENDING;
  4217. }
  4218. //----------------------------------------------------------------------------
  4219. NTSTATUS
  4220. NTSetInformation(
  4221. IN tDEVICECONTEXT *pDeviceContext,
  4222. IN PIRP pIrp)
  4223. /*++
  4224. Routine Description:
  4225. This Routine handles sets up event handlers that the client passes in.
  4226. Arguments:
  4227. pIrp - a ptr to an IRP
  4228. Return Value:
  4229. NTSTATUS - status of the request
  4230. --*/
  4231. {
  4232. // *TODO*
  4233. CTEPagedCode();
  4234. IF_DBG(NBT_DEBUG_NAMESRV)
  4235. KdPrint(("Nbt:************ Got a Set Information that was NOT expected *******\n"));
  4236. return(STATUS_SUCCESS);
  4237. }
  4238. //----------------------------------------------------------------------------
  4239. NTSTATUS
  4240. NTQueueToWorkerThread(
  4241. IN PVOID DelayedWorkerRoutine,
  4242. IN tDGRAM_SEND_TRACKING *pTracker,
  4243. IN PVOID pClientContext,
  4244. IN PVOID ClientCompletion,
  4245. IN tDEVICECONTEXT *pDeviceContext,
  4246. IN BOOLEAN fJointLockHeld
  4247. )
  4248. /*++
  4249. Routine Description:
  4250. This routine simply queues a request on an excutive worker thread
  4251. for later execution. Scanning the LmHosts file must be down this way.
  4252. Arguments:
  4253. pTracker - the tracker block for context
  4254. DelayedWorkerRoutine- the routine for the Workerthread to call
  4255. pDeviceContext - the device context which is this delayed event
  4256. pertains to. This could be NULL (meaning it's an event
  4257. pertaining to not any specific device context)
  4258. Return Value:
  4259. --*/
  4260. {
  4261. NTSTATUS status;
  4262. NBT_WORK_ITEM_CONTEXT *pContext;
  4263. #ifdef _PNP_POWER_
  4264. KIRQL OldIrq;
  4265. #endif // _PNP_POWER_
  4266. if ((pDeviceContext) &&
  4267. (!NBT_REFERENCE_DEVICE(pDeviceContext, REF_DEV_WORKER, fJointLockHeld)))
  4268. {
  4269. return STATUS_INVALID_DEVICE_REQUEST;
  4270. }
  4271. if (pContext = (NBT_WORK_ITEM_CONTEXT *)NbtAllocMem(sizeof(NBT_WORK_ITEM_CONTEXT),NBT_TAG2('22')))
  4272. {
  4273. pContext->pTracker = pTracker;
  4274. pContext->pClientContext = pClientContext;
  4275. pContext->ClientCompletion = ClientCompletion;
  4276. #ifdef _PNP_POWER_
  4277. pContext->pDeviceContext = pDeviceContext;
  4278. CTESpinLock(&NbtConfig.WorkerQLock,OldIrq);
  4279. InitializeListHead(&pContext->NbtConfigLinkage);
  4280. pContext->WorkerRoutine = DelayedWorkerRoutine;
  4281. //
  4282. // Don't Queue this request onto the Worker Queue if we have
  4283. // already started unloading
  4284. //
  4285. if (NbtConfig.Unloading)
  4286. {
  4287. InsertTailList(&NbtConfig.WorkerQList, &pContext->NbtConfigLinkage);
  4288. }
  4289. else
  4290. {
  4291. ++NbtConfig.NumWorkerThreadsQueued;
  4292. ExInitializeWorkItem(&pContext->Item,NTExecuteWorker,pContext);
  4293. ExQueueWorkItem(&pContext->Item,DelayedWorkQueue);
  4294. }
  4295. CTESpinFree(&NbtConfig.WorkerQLock,OldIrq);
  4296. #else
  4297. ExInitializeWorkItem(&pContext->Item,CallBackRoutine,pContext);
  4298. ExQueueWorkItem(&pContext->Item,DelayedWorkQueue);
  4299. #endif // _PNP_POWER_
  4300. return (STATUS_SUCCESS);
  4301. }
  4302. //
  4303. // We would reach here only on Resource failure above!
  4304. //
  4305. if (pDeviceContext)
  4306. {
  4307. NBT_DEREFERENCE_DEVICE (pDeviceContext, REF_DEV_WORKER, fJointLockHeld);
  4308. }
  4309. return (STATUS_INSUFFICIENT_RESOURCES);
  4310. }
  4311. //----------------------------------------------------------------------------
  4312. VOID
  4313. NTExecuteWorker(
  4314. IN PVOID pContextInfo
  4315. )
  4316. /*++
  4317. Routine Description:
  4318. This routine handles executing delayed requests at non-Dpc level. If
  4319. the Device is currently being unloaded, we let the Unload Handler
  4320. complete the request.
  4321. Arguments:
  4322. pContext - the Context data for this Worker thread
  4323. Return Value:
  4324. none
  4325. --*/
  4326. {
  4327. NBT_WORK_ITEM_CONTEXT *pContext = (NBT_WORK_ITEM_CONTEXT *) pContextInfo;
  4328. PNBT_WORKER_THREAD_ROUTINE pDelayedWorkerRoutine = (PNBT_WORKER_THREAD_ROUTINE) pContext->WorkerRoutine;
  4329. KIRQL OldIrq;
  4330. (*pDelayedWorkerRoutine) (pContext->pTracker,
  4331. pContext->pClientContext,
  4332. pContext->ClientCompletion,
  4333. pContext->pDeviceContext);
  4334. if (pContext->pDeviceContext)
  4335. {
  4336. NBT_DEREFERENCE_DEVICE(pContext->pDeviceContext, REF_DEV_WORKER, FALSE);
  4337. }
  4338. CTEMemFree ((PVOID) pContext);
  4339. CTESpinLock(&NbtConfig.WorkerQLock,OldIrq);
  4340. if (!--NbtConfig.NumWorkerThreadsQueued)
  4341. {
  4342. CTESpinFree(&NbtConfig.WorkerQLock,OldIrq);
  4343. KeSetEvent(&NbtConfig.WorkerQLastEvent, 0, FALSE);
  4344. }
  4345. else
  4346. {
  4347. CTESpinFree(&NbtConfig.WorkerQLock,OldIrq);
  4348. }
  4349. }
  4350. //----------------------------------------------------------------------------
  4351. VOID
  4352. SecurityDelete(
  4353. IN PVOID pContext
  4354. )
  4355. /*++
  4356. Routine Description:
  4357. This routine handles deleting a security context at non-dpc level.
  4358. Arguments:
  4359. Return Value:
  4360. none
  4361. --*/
  4362. {
  4363. PSECURITY_CLIENT_CONTEXT pClientSecurity;
  4364. pClientSecurity = (PSECURITY_CLIENT_CONTEXT)((NBT_WORK_ITEM_CONTEXT *)pContext)->pClientContext;
  4365. SeDeleteClientSecurity(pClientSecurity);
  4366. CTEMemFree(pContext);
  4367. }
  4368. //----------------------------------------------------------------------------
  4369. NTSTATUS
  4370. NTSendSession(
  4371. IN tDGRAM_SEND_TRACKING *pTracker,
  4372. IN tLOWERCONNECTION *pLowerConn,
  4373. IN PVOID pCompletion
  4374. )
  4375. /*++
  4376. Routine Description:
  4377. This Routine handles seting up a DPC to send a session pdu so that the stack
  4378. does not get wound up in multiple sends for the keep alive timeout case.
  4379. Arguments:
  4380. pIrp - a ptr to an IRP
  4381. Return Value:
  4382. NTSTATUS - status of the request
  4383. --*/
  4384. {
  4385. PKDPC pDpc;
  4386. if (pDpc = NbtAllocMem(sizeof(KDPC),NBT_TAG('f')))
  4387. {
  4388. KeInitializeDpc(pDpc, DpcSendSession, (PVOID)pTracker);
  4389. KeInsertQueueDpc(pDpc,(PVOID)pLowerConn,pCompletion);
  4390. return (STATUS_SUCCESS);
  4391. }
  4392. return (STATUS_INSUFFICIENT_RESOURCES);
  4393. }
  4394. //----------------------------------------------------------------------------
  4395. VOID
  4396. DpcSendSession(
  4397. IN PKDPC pDpc,
  4398. IN PVOID Context,
  4399. IN PVOID SystemArgument1,
  4400. IN PVOID SystemArgument2
  4401. )
  4402. /*++
  4403. Routine Description:
  4404. This routine simply calls TcpSendSession from a Dpc started in
  4405. in NTSendSession (above).
  4406. Arguments:
  4407. Return Value:
  4408. --*/
  4409. {
  4410. CTEMemFree((PVOID)pDpc);
  4411. TcpSendSession((tDGRAM_SEND_TRACKING *)Context,
  4412. (tLOWERCONNECTION *)SystemArgument1,
  4413. (PVOID)SystemArgument2);
  4414. }
  4415. //----------------------------------------------------------------------------
  4416. NTSTATUS
  4417. NTSetEventHandler(
  4418. IN tDEVICECONTEXT *pDeviceContext,
  4419. IN PIRP pIrp)
  4420. /*++
  4421. Routine Description:
  4422. This Routine handles
  4423. Arguments:
  4424. pIrp - a ptr to an IRP
  4425. Return Value:
  4426. NTSTATUS - status of the request
  4427. --*/
  4428. {
  4429. PIO_STACK_LOCATION pIrpSp;
  4430. NTSTATUS status;
  4431. tCLIENTELE *pClientEle;
  4432. PTDI_REQUEST_KERNEL_SET_EVENT pKeSetEvent;
  4433. CTEPagedCode();
  4434. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  4435. pClientEle = pIrpSp->FileObject->FsContext;
  4436. if (!NBT_VERIFY_HANDLE2 (pClientEle, NBT_VERIFY_CLIENT, NBT_VERIFY_CLIENT_DOWN))
  4437. {
  4438. ASSERTMSG ("Nbt.NTSetEventHandler: ERROR - Invalid Address Handle\n", 0);
  4439. return (STATUS_INVALID_HANDLE);
  4440. }
  4441. pKeSetEvent = (PTDI_REQUEST_KERNEL_SET_EVENT)&pIrpSp->Parameters;
  4442. // call the not NT specific routine to setup the event handler in the
  4443. // nbt data structures
  4444. status = NbtSetEventHandler(
  4445. pClientEle,
  4446. pKeSetEvent->EventType,
  4447. pKeSetEvent->EventHandler,
  4448. pKeSetEvent->EventContext);
  4449. return(status);
  4450. }
  4451. //----------------------------------------------------------------------------
  4452. VOID
  4453. NTIoComplete(
  4454. IN PIRP pIrp,
  4455. IN NTSTATUS Status,
  4456. IN ULONG SentLength)
  4457. /*++
  4458. Routine Description:
  4459. This Routine handles calling the NT I/O system to complete an I/O.
  4460. Arguments:
  4461. status - a completion status for the Irp
  4462. Return Value:
  4463. NTSTATUS - status of the request
  4464. --*/
  4465. {
  4466. KIRQL OldIrq;
  4467. #if DBG
  4468. if (!NT_SUCCESS(Status))
  4469. {
  4470. IF_DBG(NBT_DEBUG_NAMESRV)
  4471. KdPrint(("Nbt.NTIoComplete: Returning Error status = %X\n",Status));
  4472. // ASSERTMSG("Nbt: Error Ret Code In IoComplete",0);
  4473. }
  4474. #endif
  4475. pIrp->IoStatus.Status = Status;
  4476. // use -1 as a flag to mean do not adjust the sent length since it is
  4477. // already set
  4478. if (SentLength != -1)
  4479. {
  4480. pIrp->IoStatus.Information = SentLength;
  4481. }
  4482. #if DBG
  4483. if ( (Status != STATUS_SUCCESS) &&
  4484. (Status != STATUS_PENDING) &&
  4485. (Status != STATUS_INVALID_DEVICE_REQUEST) &&
  4486. (Status != STATUS_INVALID_PARAMETER) &&
  4487. (Status != STATUS_IO_TIMEOUT) &&
  4488. (Status != STATUS_BUFFER_OVERFLOW) &&
  4489. (Status != STATUS_BUFFER_TOO_SMALL) &&
  4490. (Status != STATUS_INVALID_HANDLE) &&
  4491. (Status != STATUS_INSUFFICIENT_RESOURCES) &&
  4492. (Status != STATUS_CANCELLED) &&
  4493. (Status != STATUS_DUPLICATE_NAME) &&
  4494. (Status != STATUS_TOO_MANY_NAMES) &&
  4495. (Status != STATUS_TOO_MANY_SESSIONS) &&
  4496. (Status != STATUS_REMOTE_NOT_LISTENING) &&
  4497. (Status != STATUS_BAD_NETWORK_PATH) &&
  4498. (Status != STATUS_HOST_UNREACHABLE) &&
  4499. (Status != STATUS_CONNECTION_REFUSED) &&
  4500. (Status != STATUS_WORKING_SET_QUOTA) &&
  4501. (Status != STATUS_REMOTE_DISCONNECT) &&
  4502. (Status != STATUS_LOCAL_DISCONNECT) &&
  4503. (Status != STATUS_LINK_FAILED) &&
  4504. (Status != STATUS_SHARING_VIOLATION) &&
  4505. (Status != STATUS_UNSUCCESSFUL) &&
  4506. (Status != STATUS_ACCESS_VIOLATION) &&
  4507. #ifdef MULTIPLE_WINS
  4508. (Status != STATUS_NETWORK_UNREACHABLE) &&
  4509. #endif
  4510. (Status != STATUS_NONEXISTENT_EA_ENTRY) )
  4511. {
  4512. KdPrint(("Nbt.NTIoComplete: returning unusual status = %X\n",Status));
  4513. }
  4514. #endif
  4515. // set the Irps cancel routine to null or the system may bugcheck
  4516. // with a bug code of CANCEL_STATE_IN_COMPLETED_IRP
  4517. //
  4518. // refer to IoCancelIrp() ..\ntos\io\iosubs.c
  4519. //
  4520. IoAcquireCancelSpinLock(&OldIrq);
  4521. IoSetCancelRoutine(pIrp,NULL);
  4522. IoReleaseCancelSpinLock(OldIrq);
  4523. IoCompleteRequest(pIrp,IO_NETWORK_INCREMENT);
  4524. }
  4525. //----------------------------------------------------------------------------
  4526. // ***** ***** Cancel Utilities ***** *****
  4527. //----------------------------------------------------------------------------
  4528. NTSTATUS
  4529. NTGetIrpIfNotCancelled(
  4530. IN PIRP pIrp,
  4531. IN PIRP *ppIrpInStruct
  4532. )
  4533. /*++
  4534. Routine Description:
  4535. This Routine gets the IOCancelSpinLock to coordinate with cancelling
  4536. irps It then returns STATUS_SUCCESS. It also nulls the irp in the structure
  4537. pointed to by the second parameter - so that the irp cancel routine
  4538. will not also be called.
  4539. Arguments:
  4540. status - a completion status for the Irp
  4541. Return Value:
  4542. NTSTATUS - status of the request
  4543. --*/
  4544. {
  4545. KIRQL OldIrq;
  4546. NTSTATUS status;
  4547. IoAcquireCancelSpinLock(&OldIrq);
  4548. // this nulls the irp in the datastructure - i.e. pConnEle->pIrp = NULL
  4549. *ppIrpInStruct = NULL;
  4550. if (!pIrp->Cancel)
  4551. {
  4552. status = STATUS_SUCCESS;
  4553. }
  4554. else
  4555. {
  4556. status = STATUS_UNSUCCESSFUL;
  4557. }
  4558. IoSetCancelRoutine(pIrp,NULL);
  4559. IoReleaseCancelSpinLock(OldIrq);
  4560. return(status);
  4561. }
  4562. //----------------------------------------------------------------------------
  4563. NTSTATUS
  4564. NTCheckSetCancelRoutine(
  4565. IN PIRP pIrp,
  4566. IN PVOID CancelRoutine,
  4567. IN tDEVICECONTEXT *pDeviceContext
  4568. )
  4569. /*++
  4570. Routine Description:
  4571. This Routine sets the cancel routine for an Irp.
  4572. Arguments:
  4573. status - a completion status for the Irp
  4574. Return Value:
  4575. NTSTATUS - status of the request
  4576. --*/
  4577. {
  4578. NTSTATUS status;
  4579. //
  4580. // Check if the irp was cancelled yet and if not, then set the
  4581. // irp cancel routine.
  4582. //
  4583. IoAcquireCancelSpinLock(&pIrp->CancelIrql);
  4584. if (pIrp->Cancel)
  4585. {
  4586. pIrp->IoStatus.Status = STATUS_CANCELLED;
  4587. status = STATUS_CANCELLED;
  4588. }
  4589. else
  4590. {
  4591. // setup the cancel routine
  4592. IoMarkIrpPending(pIrp);
  4593. IoSetCancelRoutine(pIrp,CancelRoutine);
  4594. status = STATUS_SUCCESS;
  4595. }
  4596. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  4597. return(status);
  4598. }
  4599. //----------------------------------------------------------------------------
  4600. NTSTATUS
  4601. NbtSetCancelRoutine(
  4602. IN PIRP pIrp,
  4603. IN PVOID CancelRoutine,
  4604. IN tDEVICECONTEXT *pDeviceContext
  4605. )
  4606. /*++
  4607. Routine Description:
  4608. This Routine sets the cancel routine for an Irp.
  4609. Arguments:
  4610. status - a completion status for the Irp
  4611. Return Value:
  4612. NTSTATUS - status of the request
  4613. --*/
  4614. {
  4615. NTSTATUS status;
  4616. //
  4617. // Check if the irp was cancelled yet and if not, then set the
  4618. // irp cancel routine.
  4619. //
  4620. IoAcquireCancelSpinLock(&pIrp->CancelIrql);
  4621. if (pIrp->Cancel)
  4622. {
  4623. pIrp->IoStatus.Status = STATUS_CANCELLED;
  4624. status = STATUS_CANCELLED;
  4625. //
  4626. // Note the cancel spin lock is released by the Cancel routine
  4627. //
  4628. (*(PDRIVER_CANCEL)CancelRoutine)((PDEVICE_OBJECT)pDeviceContext,pIrp);
  4629. }
  4630. else
  4631. {
  4632. // setup the cancel routine and mark the irp pending
  4633. //
  4634. IoMarkIrpPending(pIrp);
  4635. IoSetCancelRoutine(pIrp,CancelRoutine);
  4636. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  4637. status = STATUS_SUCCESS;
  4638. }
  4639. return(status);
  4640. }
  4641. //----------------------------------------------------------------------------
  4642. VOID
  4643. NTClearContextCancel(
  4644. IN NBT_WORK_ITEM_CONTEXT *pContext
  4645. )
  4646. /*++
  4647. Routine Description:
  4648. This Routine sets the cancel routine for
  4649. ((tDGRAM_SEND_TRACKING *)(pContext->pClientContext))->pClientIrp
  4650. to NULL.
  4651. NbtConfig.JointLock should be held when this routine is called.
  4652. Arguments:
  4653. status - a completion status for the Irp
  4654. Return Value:
  4655. NTSTATUS - status of the request
  4656. --*/
  4657. {
  4658. NTSTATUS status;
  4659. status = NbtCancelCancelRoutine( ((tDGRAM_SEND_TRACKING *)(pContext->pClientContext))->pClientIrp );
  4660. ASSERT ( status != STATUS_CANCELLED );
  4661. }
  4662. //----------------------------------------------------------------------------
  4663. NTSTATUS
  4664. NbtCancelCancelRoutine(
  4665. IN PIRP pIrp
  4666. )
  4667. /*++
  4668. Routine Description:
  4669. This Routine sets the cancel routine for an Irp to NULL
  4670. Arguments:
  4671. status - a completion status for the Irp
  4672. Return Value:
  4673. NTSTATUS - status of the request
  4674. --*/
  4675. {
  4676. NTSTATUS status = STATUS_SUCCESS;
  4677. if ( pIrp )
  4678. {
  4679. //
  4680. // Check if the irp was cancelled yet and if not, then set the
  4681. // irp cancel routine.
  4682. //
  4683. IoAcquireCancelSpinLock(&pIrp->CancelIrql);
  4684. if (pIrp->Cancel)
  4685. {
  4686. status = STATUS_CANCELLED;
  4687. }
  4688. IoSetCancelRoutine(pIrp,NULL);
  4689. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  4690. }
  4691. return(status);
  4692. }
  4693. //----------------------------------------------------------------------------
  4694. // ***** ***** Cancel Routines ***** *****
  4695. //----------------------------------------------------------------------------
  4696. VOID
  4697. NbtCancelListen(
  4698. IN PDEVICE_OBJECT DeviceContext,
  4699. IN PIRP pIrp
  4700. )
  4701. /*++
  4702. Routine Description:
  4703. This routine handles the cancelling a listen Irp. It must release the
  4704. cancel spin lock before returning re: IoCancelIrp().
  4705. Arguments:
  4706. Return Value:
  4707. The final status from the operation.
  4708. --*/
  4709. {
  4710. tCONNECTELE *pConnEle;
  4711. tCLIENTELE *pClientEle;
  4712. KIRQL OldIrq;
  4713. PLIST_ENTRY pHead;
  4714. PLIST_ENTRY pEntry;
  4715. PIO_STACK_LOCATION pIrpSp;
  4716. tLISTENREQUESTS *pListenReq;
  4717. IF_DBG(NBT_DEBUG_NAMESRV)
  4718. KdPrint(("Nbt:Got a LISTEN Cancel !!! *****************\n"));
  4719. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  4720. pConnEle = (tCONNECTELE *)pIrpSp->FileObject->FsContext;
  4721. if ((!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN)) ||
  4722. (!NBT_VERIFY_HANDLE2 ((pClientEle = pConnEle->pClientEle), NBT_VERIFY_CLIENT,NBT_VERIFY_CLIENT_DOWN)))
  4723. {
  4724. ASSERTMSG ("Nbt.NbtCancelListen: ERROR - Invalid Connection Handle\n", 0);
  4725. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  4726. return;
  4727. }
  4728. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  4729. NbtTrace(NBT_TRACE_INBOUND, ("Cancel Listen Irp %p ClientEle=%p", pIrp, pConnEle->pClientEle));
  4730. // now search the client's listen queue looking for this connection
  4731. //
  4732. CTESpinLock(pClientEle,OldIrq);
  4733. pHead = &pClientEle->ListenHead;
  4734. pEntry = pHead->Flink;
  4735. while (pEntry != pHead)
  4736. {
  4737. pListenReq = CONTAINING_RECORD(pEntry,tLISTENREQUESTS,Linkage);
  4738. if ((pListenReq->pConnectEle == pConnEle) &&
  4739. (pListenReq->pIrp == pIrp))
  4740. {
  4741. RemoveEntryList(pEntry);
  4742. CTESpinFree(pClientEle,OldIrq);
  4743. // complete the irp
  4744. pIrp->IoStatus.Status = STATUS_CANCELLED;
  4745. IoCompleteRequest(pIrp,IO_NETWORK_INCREMENT);
  4746. CTEMemFree((PVOID)pListenReq);
  4747. return;
  4748. }
  4749. pEntry = pEntry->Flink;
  4750. }
  4751. CTESpinFree(pClientEle,OldIrq);
  4752. return;
  4753. }
  4754. //----------------------------------------------------------------------------
  4755. VOID
  4756. NbtCancelSession(
  4757. IN PDEVICE_OBJECT DeviceContext,
  4758. IN PIRP pIrp
  4759. )
  4760. /*++
  4761. Routine Description:
  4762. This routine handles the cancelling a connect Irp. It must release the
  4763. cancel spin lock before returning re: IoCancelIrp(). It is called when
  4764. the session setup pdu has been sent, and the state is still outbound.
  4765. The cancel routine is only setup when the timer is started to time
  4766. sending the session response pdu.
  4767. Arguments:
  4768. Return Value:
  4769. The final status from the operation.
  4770. --*/
  4771. {
  4772. tCONNECTELE *pConnEle;
  4773. KIRQL OldIrq;
  4774. PIO_STACK_LOCATION pIrpSp;
  4775. BOOLEAN DerefConnEle=FALSE;
  4776. tTIMERQENTRY *pTimer;
  4777. tDGRAM_SEND_TRACKING *pTracker;
  4778. COMPLETIONCLIENT pCompletion;
  4779. PVOID pContext;
  4780. IF_DBG(NBT_DEBUG_NAMESRV)
  4781. KdPrint(("Nbt.NbtCancelSession: Got a Cancel !!! *****************\n"));
  4782. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  4783. pConnEle = (tCONNECTELE *)pIrpSp->FileObject->FsContext;
  4784. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  4785. if (!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  4786. {
  4787. ASSERTMSG ("Nbt.NbtCancelSession: ERROR - Invalid Connection Handle\n", 0);
  4788. return;
  4789. }
  4790. NbtTrace(NBT_TRACE_OUTBOUND, ("Cancel Session Irp %p ConnEle=%p LowerConn=%p, ClientEle=%p",
  4791. pIrp, pConnEle, pConnEle->pLowerConnId, pConnEle->pClientEle));
  4792. #ifdef RASAUTODIAL
  4793. //
  4794. // Cancel the automatic connection if one's
  4795. // in progress. If we don't find the
  4796. // connection block in the automatic
  4797. // connection driver, then it's already
  4798. // been completed.
  4799. //
  4800. if (pConnEle->fAutoConnecting)
  4801. {
  4802. if (!NbtCancelPostConnect(pIrp))
  4803. {
  4804. return;
  4805. }
  4806. }
  4807. #endif // RASAUTODIAL
  4808. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  4809. if ((!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN)) ||
  4810. (!(pConnEle->pIrp)) || // the irp could get completed while acquiring the lock
  4811. (!(pTracker = (tDGRAM_SEND_TRACKING *)pConnEle->pIrpRcv)))
  4812. {
  4813. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  4814. return;
  4815. }
  4816. pTracker->Flags |= TRACKER_CANCELLED;
  4817. if (pTimer = pTracker->pTimer) // check for SessionStartupTimeout
  4818. {
  4819. pTracker->pTimer = NULL;
  4820. //
  4821. // stop the timer and only continue if the timer was stopped before
  4822. // it expired
  4823. //
  4824. StopTimer(pTimer, &pCompletion, &pContext);
  4825. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  4826. //
  4827. // pCompletion will be set if the timer had not expired
  4828. // We want to cause a forced timeout, so we will just call the
  4829. // timeout routine with STATUS_CANCELLED
  4830. //
  4831. if (pCompletion)
  4832. {
  4833. (*pCompletion) (pContext, STATUS_CANCELLED);
  4834. }
  4835. }
  4836. else if (pConnEle->state == NBT_SESSION_OUTBOUND)
  4837. {
  4838. //
  4839. // for some reason there is no timer, but the connection is still
  4840. // outbound, so call the timer completion routine to kill off
  4841. // the connection.
  4842. //
  4843. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  4844. SessionStartupTimeout (pTracker, ULongToPtr(STATUS_CANCELLED), (PVOID)1);
  4845. }
  4846. else
  4847. {
  4848. //
  4849. // Free the lock
  4850. //
  4851. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  4852. }
  4853. return;
  4854. }
  4855. //----------------------------------------------------------------------------
  4856. VOID
  4857. NbtCancelConnect(
  4858. IN PDEVICE_OBJECT pDeviceContext,
  4859. IN PIRP pIrp
  4860. )
  4861. /*++
  4862. Routine Description:
  4863. This routine handles cancelling an NTConnect Irp - which has been
  4864. passed down by a client (e.g. net view). Typically, when the request
  4865. succeeds on another adapter, it will issue this cancel.
  4866. On receiving the cancel, if we are processing a Local IRP, we just
  4867. pass the cancel on to the Local Irp which will complete this Irp also
  4868. in its Completion Routine.
  4869. Arguments:
  4870. Return Value:
  4871. The final status from the operation.
  4872. --*/
  4873. {
  4874. PIO_STACK_LOCATION pIrpSp;
  4875. IN PIRP pLocalIrp;
  4876. tCONNECTELE *pConnEle;
  4877. IF_DBG(NBT_DEBUG_NAMESRV)
  4878. KdPrint(("Nbt:NbtCancelConnect ********** Got an Irp Cancel !!! **********\n"));
  4879. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  4880. pConnEle = pIrpSp->FileObject->FsContext;
  4881. if (!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  4882. {
  4883. ASSERTMSG ("Nbt.NbtCancelConnect: ERROR - Invalid Connection Handle\n", 0);
  4884. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  4885. return;
  4886. }
  4887. NbtTrace(NBT_TRACE_OUTBOUND, ("Cancel Connect Irp %p ConnEle=%p LowerConn=%p, ClientEle=%p",
  4888. pIrp, pConnEle, pConnEle->pLowerConnId, pConnEle->pClientEle));
  4889. if ((pConnEle) &&
  4890. (pLocalIrp = pConnEle->pIrp))
  4891. {
  4892. IF_DBG(NBT_DEBUG_NAMESRV)
  4893. KdPrint (("Nbt.NbtCancelConnect: pClientIrp=<%x>, pLocalIrp=<%x>, Device=<%x>, CancelR=<%x>\n",
  4894. pIrp, pLocalIrp, pDeviceContext, pLocalIrp->CancelRoutine));
  4895. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  4896. IoCancelIrp(pLocalIrp);
  4897. }
  4898. else
  4899. {
  4900. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  4901. }
  4902. return;
  4903. }
  4904. //----------------------------------------------------------------------------
  4905. VOID
  4906. NbtCancelReceive(
  4907. IN PDEVICE_OBJECT DeviceContext,
  4908. IN PIRP pIrp
  4909. )
  4910. /*++
  4911. Routine Description:
  4912. This routine handles the cancelling a listen Irp. It must release the
  4913. cancel spin lock before returning re: IoCancelIrp().
  4914. Arguments:
  4915. Return Value:
  4916. The final status from the operation.
  4917. --*/
  4918. {
  4919. tCONNECTELE *pConnEle;
  4920. tLOWERCONNECTION *pLowerConn;
  4921. KIRQL OldIrq;
  4922. KIRQL OldIrq1;
  4923. PLIST_ENTRY pHead;
  4924. PLIST_ENTRY pEntry;
  4925. PIO_STACK_LOCATION pIrpSp;
  4926. PIRP pRcvIrp;
  4927. IF_DBG(NBT_DEBUG_NAMESRV)
  4928. KdPrint(("Nbt.NbtCancelReceive: Got a Cancel !!! *****************\n"));
  4929. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  4930. pConnEle = (tCONNECTELE *)pIrpSp->FileObject->FsContext;
  4931. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  4932. if (!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  4933. {
  4934. ASSERTMSG ("Nbt.NbtCancelReceive: ERROR - Invalid Connection Handle\n", 0);
  4935. return;
  4936. }
  4937. NbtTrace(NBT_TRACE_INBOUND, ("Cancel Receive Irp %p ConnEle=%p LowerConn=%p, ClientEle=%p",
  4938. pIrp, pConnEle, pConnEle->pLowerConnId, pConnEle->pClientEle));
  4939. CTESpinLock(&NbtConfig.JointLock,OldIrq1);
  4940. pLowerConn = pConnEle->pLowerConnId;
  4941. if (pLowerConn)
  4942. {
  4943. CTESpinLock(pLowerConn,OldIrq);
  4944. }
  4945. if (pConnEle->Verify == NBT_VERIFY_CONNECTION)
  4946. {
  4947. // now search the connection's receive queue looking for this Irp
  4948. //
  4949. pHead = &pConnEle->RcvHead;
  4950. pEntry = pHead->Flink;
  4951. while (pEntry != pHead)
  4952. {
  4953. pRcvIrp = CONTAINING_RECORD(pEntry,IRP,Tail.Overlay.ListEntry);
  4954. if (pRcvIrp == pIrp)
  4955. {
  4956. RemoveEntryList(pEntry);
  4957. // complete the irp
  4958. pIrp->IoStatus.Status = STATUS_CANCELLED;
  4959. if (pLowerConn)
  4960. {
  4961. CTESpinFree(pLowerConn,OldIrq);
  4962. }
  4963. CTESpinFree(&NbtConfig.JointLock,OldIrq1);
  4964. IoCompleteRequest(pIrp,IO_NETWORK_INCREMENT);
  4965. return;
  4966. }
  4967. pEntry = pEntry->Flink;
  4968. }
  4969. }
  4970. if (pLowerConn)
  4971. {
  4972. CTESpinFree(pLowerConn,OldIrq);
  4973. }
  4974. CTESpinFree(&NbtConfig.JointLock,OldIrq1);
  4975. return;
  4976. }
  4977. //----------------------------------------------------------------------------
  4978. VOID
  4979. NbtCancelRcvDgram(
  4980. IN PDEVICE_OBJECT DeviceContext,
  4981. IN PIRP pIrp
  4982. )
  4983. /*++
  4984. Routine Description:
  4985. This routine handles the cancelling a listen Irp. It must release the
  4986. cancel spin lock before returning re: IoCancelIrp().
  4987. Arguments:
  4988. Return Value:
  4989. The final status from the operation.
  4990. --*/
  4991. {
  4992. tCLIENTELE *pClientEle;
  4993. KIRQL OldIrq;
  4994. PLIST_ENTRY pHead;
  4995. PLIST_ENTRY pEntry;
  4996. PIO_STACK_LOCATION pIrpSp;
  4997. tRCVELE *pRcvEle;
  4998. IF_DBG(NBT_DEBUG_NAMESRV)
  4999. KdPrint(("Nbt.NbtCancelRcvDgram: Got a Cancel !!! *****************\n"));
  5000. //
  5001. // Need to acquire JointLock before Cancel lock!
  5002. // Bug#: 124405
  5003. //
  5004. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  5005. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  5006. IoAcquireCancelSpinLock(&pIrp->CancelIrql);
  5007. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  5008. pClientEle = (tCLIENTELE *)pIrpSp->FileObject->FsContext;
  5009. NbtTrace(NBT_TRACE_RECVDGRAM, ("Cancel RcvDgram Irp %p ClientEle=%p", pIrp, pClientEle));
  5010. if (NBT_VERIFY_HANDLE (pClientEle, NBT_VERIFY_CLIENT))
  5011. {
  5012. // now search the client's listen queue looking for this connection
  5013. //
  5014. pHead = &pClientEle->RcvDgramHead;
  5015. pEntry = pHead->Flink;
  5016. while (pEntry != pHead)
  5017. {
  5018. pRcvEle = CONTAINING_RECORD(pEntry,tRCVELE,Linkage);
  5019. if (pRcvEle->pIrp == pIrp)
  5020. {
  5021. RemoveEntryList(pEntry);
  5022. // complete the irp
  5023. pIrp->IoStatus.Status = STATUS_CANCELLED;
  5024. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  5025. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  5026. IoCompleteRequest(pIrp,IO_NETWORK_INCREMENT);
  5027. CTEMemFree((PVOID)pRcvEle);
  5028. return;
  5029. }
  5030. pEntry = pEntry->Flink;
  5031. }
  5032. }
  5033. else
  5034. {
  5035. ASSERTMSG ("Nbt.NbtCancelRcvDgram: ERROR - Invalid Address Handle\n", 0);
  5036. }
  5037. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  5038. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  5039. return;
  5040. }
  5041. //----------------------------------------------------------------------------
  5042. VOID
  5043. NbtCancelFindName(
  5044. IN PDEVICE_OBJECT DeviceContext,
  5045. IN PIRP pIrp
  5046. )
  5047. /*++
  5048. Routine Description:
  5049. This routine handles the cancelling a FindName Irp - which has
  5050. been passed down by a client (e.g. ping). Typically, when ping succeeds
  5051. on another adapter, it will issue this cancel.
  5052. On receiving the cancel, we stop any timer that is running in connection
  5053. with name query and then complete the irp with status_cancelled.
  5054. Arguments:
  5055. Return Value:
  5056. The final status from the operation.
  5057. --*/
  5058. {
  5059. tDGRAM_SEND_TRACKING *pTracker;
  5060. PIO_STACK_LOCATION pIrpSp;
  5061. IF_DBG(NBT_DEBUG_NAMESRV)
  5062. KdPrint(("Nbt.NbtCancelFindName: Got a Cancel !!! *****************\n"));
  5063. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  5064. pTracker = pIrpSp->Parameters.Others.Argument4;
  5065. NbtTrace(NBT_TRACE_NAMESRV, ("Cancel FindName Irp %p pTracker=%p", pIrp, pTracker));
  5066. //
  5067. // We want to ensure that the tracker supplied by FsContext
  5068. // is the right Tracker for this Irp
  5069. //
  5070. if (pTracker && (pIrp == pTracker->pClientIrp))
  5071. {
  5072. //
  5073. // if pClientIrp still valid, completion routine hasn't run yet: go ahead
  5074. // and complete the irp here
  5075. //
  5076. pIrpSp->Parameters.Others.Argument4 = NULL;
  5077. pTracker->pClientIrp = NULL;
  5078. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  5079. NTIoComplete(pIrp,STATUS_CANCELLED,(ULONG)-1);
  5080. } else
  5081. {
  5082. //
  5083. // the completion routine has run.
  5084. //
  5085. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  5086. }
  5087. return;
  5088. }
  5089. //----------------------------------------------------------------------------
  5090. VOID
  5091. NbtCancelLmhSvcIrp(
  5092. IN PDEVICE_OBJECT DeviceContext,
  5093. IN PIRP pIrp
  5094. )
  5095. /*++
  5096. Routine Description:
  5097. This routine handles the cancelling a DNS name query Irp or
  5098. the CheckIpAddrs Irp that is passed down to NBT from Lmhsvc
  5099. This routine will get the Resource Lock, and Null the Irp ptr in the
  5100. DnsQueries or CheckAddr structure (as approp) and then return the irp.
  5101. Arguments:
  5102. Return Value:
  5103. The final status from the operation.
  5104. --*/
  5105. {
  5106. tLMHSVC_REQUESTS *pLmhSvcRequest = NULL;
  5107. KIRQL OldIrq;
  5108. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  5109. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  5110. if (pIrp == CheckAddr.QueryIrp)
  5111. {
  5112. IF_DBG(NBT_DEBUG_NAMESRV)
  5113. KdPrint(("Nbt.NbtCancelLmhSvcIrp: Got a Cancel on CheckAddr Irp !!! *****************\n"));
  5114. NbtTrace(NBT_TRACE_NAMESRV, ("Cancel LmhSvc CheckAddr Irp %p", pIrp));
  5115. pLmhSvcRequest = &CheckAddr;
  5116. }
  5117. else if (pIrp == DnsQueries.QueryIrp)
  5118. {
  5119. IF_DBG(NBT_DEBUG_NAMESRV)
  5120. KdPrint(("Nbt.NbtCancelLmhSvcIrp: Got a Cancel on DnsQueries Irp !!! *****************\n"));
  5121. NbtTrace(NBT_TRACE_NAMESRV, ("Cancel LmhSvc DnsQueries Irp %p", pIrp));
  5122. pLmhSvcRequest = &DnsQueries;
  5123. }
  5124. if (pLmhSvcRequest)
  5125. {
  5126. pIrp->IoStatus.Status = STATUS_CANCELLED;
  5127. pLmhSvcRequest->QueryIrp = NULL;
  5128. pLmhSvcRequest->pIpAddrBuf = NULL;
  5129. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  5130. IoCompleteRequest(pIrp,IO_NETWORK_INCREMENT);
  5131. }
  5132. else
  5133. {
  5134. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  5135. }
  5136. return;
  5137. }
  5138. //----------------------------------------------------------------------------
  5139. VOID
  5140. NbtCancelDisconnectWait(
  5141. IN PDEVICE_OBJECT DeviceContext,
  5142. IN PIRP pIrp
  5143. )
  5144. /*++
  5145. Routine Description:
  5146. This routine handles the cancelling a Disconnect Wait Irp - which has
  5147. been passed down by a client so that when a disconnect occurs this
  5148. irp will complete and inform the client. The action here is to simply
  5149. complete the irp with status cancelled.
  5150. down to NBT from Lmhsvc, for the purpose of resolving a name with DNS.
  5151. Nbt will complete this irp each time it has a name to resolve with DNS.
  5152. This routine will get the Resource Lock, and Null the Irp ptr in the
  5153. DnsQueries structure and then return the irp.
  5154. Arguments:
  5155. Return Value:
  5156. The final status from the operation.
  5157. --*/
  5158. {
  5159. tCONNECTELE *pConnEle;
  5160. PIO_STACK_LOCATION pIrpSp;
  5161. CTELockHandle OldIrq;
  5162. IF_DBG(NBT_DEBUG_NAMESRV)
  5163. KdPrint(("Nbt:Got a Disc Wait Irp Cancel !!! *****************\n"));
  5164. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  5165. pConnEle = (tCONNECTELE *)pIrpSp->FileObject->FsContext;
  5166. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  5167. CTESpinLock(pConnEle,OldIrq);
  5168. NbtTrace(NBT_TRACE_OUTBOUND, ("Cancel DisconnectWait Irp %p ConnEle=%p LowerConn=%p, ClientEle=%p",
  5169. pIrp, pConnEle, pConnEle->pLowerConnId, pConnEle->pClientEle));
  5170. if (!NBT_VERIFY_HANDLE2 (pConnEle, NBT_VERIFY_CONNECTION, NBT_VERIFY_CONNECTION_DOWN))
  5171. {
  5172. ASSERTMSG ("Nbt.NbtCancelDisconnectWait: ERROR - Invalid Connection Handle\n", 0);
  5173. pIrp->IoStatus.Status = STATUS_INVALID_HANDLE;
  5174. }
  5175. else if (pConnEle->pIrpClose == pIrp)
  5176. {
  5177. pConnEle->pIrpClose = NULL;
  5178. pIrp->IoStatus.Status = STATUS_CANCELLED;
  5179. }
  5180. CTESpinFree(pConnEle,OldIrq);
  5181. IoCompleteRequest(pIrp,IO_NETWORK_INCREMENT);
  5182. return;
  5183. }
  5184. //----------------------------------------------------------------------------
  5185. VOID
  5186. NbtCancelWaitForLmhSvcIrp(
  5187. IN PDEVICE_OBJECT DeviceContext,
  5188. IN PIRP pIrp
  5189. )
  5190. /*++
  5191. Routine Description:
  5192. This routine handles the cancelling a Query to DNS, so that the client's
  5193. irp can be returned to the client. This cancellation is instigated
  5194. by the client (i.e. RDR).
  5195. Arguments:
  5196. Return Value:
  5197. The final status from the operation.
  5198. --*/
  5199. {
  5200. BOOLEAN FoundIt = FALSE;
  5201. NBT_WORK_ITEM_CONTEXT *Context;
  5202. CTELockHandle OldIrq;
  5203. tDGRAM_SEND_TRACKING *pTracker;
  5204. PVOID pClientCompletion;
  5205. PVOID pClientContext;
  5206. PLIST_ENTRY pHead;
  5207. PLIST_ENTRY pEntry;
  5208. IF_DBG(NBT_DEBUG_NAMESRV)
  5209. KdPrint(("Nbt.NbtCancelWaitForLmhSvcIrp: Got a Cancel !!! *****************\n"));
  5210. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  5211. CTESpinLock(&NbtConfig.JointLock,OldIrq);
  5212. //
  5213. // First check the lmhost list, then the CheckAddr list, then the Dns list
  5214. //
  5215. if (!(Context = FindLmhSvcRequest (DeviceContext, pIrp, &LmHostQueries)))
  5216. {
  5217. if (!(Context = FindLmhSvcRequest (DeviceContext, pIrp, &CheckAddr)))
  5218. {
  5219. Context = FindLmhSvcRequest (DeviceContext, pIrp, &DnsQueries);
  5220. }
  5221. }
  5222. CTESpinFree(&NbtConfig.JointLock,OldIrq);
  5223. NbtTrace(NBT_TRACE_NAMESRV, ("Cancel WaitForLmhsvc Irp %p", pIrp));
  5224. //
  5225. // Now complete the clients request to return the irp to the client
  5226. //
  5227. if (Context)
  5228. {
  5229. //
  5230. // this is the name Query tracker
  5231. //
  5232. pTracker = Context->pTracker;
  5233. pClientCompletion = Context->ClientCompletion;
  5234. pClientContext = Context->pClientContext;
  5235. // for dns names (NameLen>16), pTracker would be NULL
  5236. if (pTracker)
  5237. {
  5238. // name did not resolve, so delete from table
  5239. SetNameState (pTracker->pNameAddr, NULL, FALSE);
  5240. NBT_DEREFERENCE_TRACKER(pTracker, FALSE);
  5241. }
  5242. //
  5243. // this should complete any name queries that are waiting on
  5244. // this first name query - i.e. queries to the resolving name
  5245. //
  5246. CompleteClientReq(pClientCompletion, pClientContext, STATUS_CANCELLED);
  5247. CTEMemFree(Context);
  5248. }
  5249. }
  5250. //----------------------------------------------------------------------------
  5251. VOID
  5252. NbtCancelDgramSend(
  5253. IN PDEVICE_OBJECT DeviceContext,
  5254. IN PIRP pIrp
  5255. )
  5256. /*++
  5257. Routine Description:
  5258. This routine handles the cancelling of a Datagram Send. The action here is to simply
  5259. complete the irp with status cancelled.
  5260. This routine will Null the Irp ptr in the Tracker structure (if available) so that
  5261. SendDgramContinue does not find it.
  5262. Arguments:
  5263. Return Value:
  5264. The final status from the operation.
  5265. --*/
  5266. {
  5267. tDGRAM_SEND_TRACKING *pTracker;
  5268. PIO_STACK_LOCATION pIrpSp;
  5269. CTELockHandle OldIrq;
  5270. IF_DBG(NBT_DEBUG_NAMESRV)
  5271. KdPrint(("Nbt.NbtCancelDgramSend: Got a DatagramSend Irp Cancel !!! *****************\n"));
  5272. pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
  5273. pTracker = pIrpSp->Parameters.Others.Argument4;
  5274. NbtTrace(NBT_TRACE_SENDDGRAM, ("Cancel SendDgram Irp %p", pIrp));
  5275. if ((NBT_VERIFY_HANDLE (pTracker, NBT_VERIFY_TRACKER)) &&
  5276. (pTracker->pClientIrp == pIrp))
  5277. {
  5278. pTracker->pClientIrp = NULL;
  5279. pIrpSp->Parameters.Others.Argument4 = NULL;
  5280. pIrp->IoStatus.Status = STATUS_CANCELLED;
  5281. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  5282. NTIoComplete(pIrp,STATUS_CANCELLED,(ULONG)-1);
  5283. }
  5284. else
  5285. {
  5286. IoReleaseCancelSpinLock(pIrp->CancelIrql);
  5287. }
  5288. return;
  5289. }
  5290. int
  5291. check_unicode_string(IN PUNICODE_STRING str)
  5292. {
  5293. try {
  5294. ProbeForRead(str, sizeof (UNICODE_STRING), sizeof(BYTE));
  5295. if ((str->MaximumLength <= 0) || (str->Length <= 0) || (str->Length >= str->MaximumLength)) {
  5296. return (-1);
  5297. }
  5298. ProbeForRead(str->Buffer, str->MaximumLength, sizeof(WCHAR));
  5299. ASSERT((str->Length&1) == 0);
  5300. if (str->Buffer[str->Length/sizeof(WCHAR)]) {
  5301. return (-1);
  5302. }
  5303. return 0;
  5304. } except(EXCEPTION_EXECUTE_HANDLER) {
  5305. KdPrint (("Nbt.check_unicode_string: Exception <0x%x> trying to access unicode string 0x%x\n",
  5306. GetExceptionCode(), str));
  5307. return (-1);
  5308. }
  5309. }